Skip to content

Commit 6139b9e

Browse files
committed
chore: lint fixes part 6
1 parent 02b6daf commit 6139b9e

File tree

23 files changed

+109
-100
lines changed

23 files changed

+109
-100
lines changed

api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ func NewMetrics(reg prometheus.Registerer) *metrics.Metrics {
6262
}
6363

6464
func NewRecorder(logger slog.Logger, tracer trace.Tracer, clientFn func() (Recorder, error)) Recorder {
65-
return recorder.NewRecorder(logger, tracer, clientFn)
65+
return recorder.NewWrappedRecorder(logger, tracer, clientFn)
6666
}

circuitbreaker/circuitbreaker_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,11 @@ func TestExecute_OnStateChange(t *testing.T) {
177177

178178
// Trip circuit
179179
w := httptest.NewRecorder()
180-
cbs.Execute(endpoint, model, w, func(rw http.ResponseWriter) error {
180+
err := cbs.Execute(endpoint, model, w, func(rw http.ResponseWriter) error {
181181
rw.WriteHeader(http.StatusTooManyRequests)
182182
return nil
183183
})
184+
assert.NoError(t, err)
184185

185186
// Verify state change callback was called with correct parameters
186187
assert.Len(t, stateChanges, 1)

intercept/apidump/apidump.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ func (d *dumper) dumpRequest(req *http.Request) error {
107107
if err != nil {
108108
return xerrors.Errorf("write request header terminator: %w", err)
109109
}
110-
buf.Write(prettyBody)
111-
buf.WriteByte('\n')
110+
// bytes.Buffer writes to in-memory storage and never return errors.
111+
_, _ = buf.Write(prettyBody)
112+
_ = buf.WriteByte('\n')
112113

113114
return os.WriteFile(dumpPath, buf.Bytes(), 0o644) //nolint:gosec // https://github.com/coder/aibridge/pull/256#discussion_r3072143983
114115
}

intercept/apidump/streaming.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (s *streamingBodyDumper) init() {
3737
// Write headers first.
3838
if _, err := s.file.Write(s.headerData); err != nil {
3939
s.initErr = xerrors.Errorf("write headers: %w", err)
40-
s.file.Close()
40+
_ = s.file.Close() // best-effort cleanup on header write failure
4141
s.file = nil
4242
}
4343
})

intercept/apidump/streaming_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ func TestMiddleware_StreamingResponse(t *testing.T) {
4242
// Create a pipe to simulate streaming
4343
pr, pw := io.Pipe()
4444
go func() {
45+
defer pw.Close() //nolint:revive // error handled via pipe read side
4546
for _, chunk := range chunks {
46-
pw.Write([]byte(chunk))
47+
if _, err := pw.Write([]byte(chunk)); err != nil {
48+
return
49+
}
4750
}
48-
pw.Close()
4951
}()
5052

5153
resp, err := middleware(req, func(r *http.Request) (*http.Response, error) {
@@ -65,7 +67,7 @@ func TestMiddleware_StreamingResponse(t *testing.T) {
6567
for {
6668
n, err := resp.Body.Read(buf)
6769
if n > 0 {
68-
receivedData.Write(buf[:n])
70+
_, _ = receivedData.Write(buf[:n]) // bytes.Buffer.Write never fails
6971
}
7072
if err == io.EOF {
7173
break

intercept/chatcompletions/streaming.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,11 @@ func (i *StreamingInterception) marshalErr(err error) ([]byte, error) {
390390
}
391391

392392
func (*StreamingInterception) encodeForStream(payload []byte) []byte {
393+
// bytes.Buffer writes to in-memory storage and never return errors.
393394
var buf bytes.Buffer
394-
buf.WriteString("data: ")
395-
buf.Write(payload)
396-
buf.WriteString("\n\n")
395+
_, _ = buf.WriteString("data: ")
396+
_, _ = buf.Write(payload)
397+
_, _ = buf.WriteString("\n\n")
397398
return buf.Bytes()
398399
}
399400

intercept/messages/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var bedrockSupportedBetaFlags = map[string]bool{
6767
type interceptionBase struct {
6868
id uuid.UUID
6969
providerName string
70-
reqPayload MessagesRequestPayload
70+
reqPayload RequestPayload
7171

7272
cfg aibconfig.Anthropic
7373
bedrockCfg *aibconfig.AWSBedrock

intercept/messages/base_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,10 +763,10 @@ func TestAugmentRequestForBedrock_AdaptiveThinking(t *testing.T) {
763763
}
764764
}
765765

766-
func mustMessagesPayload(t *testing.T, requestBody string) MessagesRequestPayload {
766+
func mustMessagesPayload(t *testing.T, requestBody string) RequestPayload {
767767
t.Helper()
768768

769-
payload, err := NewMessagesRequestPayload([]byte(requestBody))
769+
payload, err := NewRequestPayload([]byte(requestBody))
770770
require.NoError(t, err)
771771

772772
return payload

intercept/messages/blocking.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type BlockingInterception struct {
3232

3333
func NewBlockingInterceptor(
3434
id uuid.UUID,
35-
reqPayload MessagesRequestPayload,
35+
reqPayload RequestPayload,
3636
providerName string,
3737
cfg config.Anthropic,
3838
bedrockCfg *config.AWSBedrock,

intercept/messages/reqpayload.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,35 @@ var (
8282
}
8383
)
8484

85-
// MessagesRequestPayload is raw JSON bytes of an Anthropic Messages API request.
85+
// RequestPayload is raw JSON bytes of an Anthropic Messages API request.
8686
// Methods provide package-specific reads and rewrites while preserving the
8787
// original body for upstream pass-through.
88-
type MessagesRequestPayload []byte
88+
type RequestPayload []byte
8989

90-
func NewMessagesRequestPayload(raw []byte) (MessagesRequestPayload, error) {
90+
func NewRequestPayload(raw []byte) (RequestPayload, error) {
9191
if len(bytes.TrimSpace(raw)) == 0 {
9292
return nil, xerrors.New("messages empty request body")
9393
}
9494
if !json.Valid(raw) {
9595
return nil, xerrors.New("messages invalid JSON request body")
9696
}
9797

98-
return MessagesRequestPayload(raw), nil
98+
return RequestPayload(raw), nil
9999
}
100100

101-
func (p MessagesRequestPayload) Stream() bool {
101+
func (p RequestPayload) Stream() bool {
102102
v := gjson.GetBytes(p, messagesReqPathStream)
103103
if !v.IsBool() {
104104
return false
105105
}
106106
return v.Bool()
107107
}
108108

109-
func (p MessagesRequestPayload) model() string {
109+
func (p RequestPayload) model() string {
110110
return gjson.GetBytes(p, messagesReqPathModel).Str
111111
}
112112

113-
func (p MessagesRequestPayload) correlatingToolCallID() *string {
113+
func (p RequestPayload) correlatingToolCallID() *string {
114114
messages := gjson.GetBytes(p, messagesReqPathMessages)
115115
if !messages.IsArray() {
116116
return nil
@@ -147,7 +147,7 @@ func (p MessagesRequestPayload) correlatingToolCallID() *string {
147147
// lastUserPrompt returns the prompt text from the last user message. If no prompt
148148
// is found, it returns empty string, false, nil. Unexpected shapes are treated as
149149
// unsupported and do not fail the request path.
150-
func (p MessagesRequestPayload) lastUserPrompt() (string, bool, error) {
150+
func (p RequestPayload) lastUserPrompt() (string, bool, error) {
151151
messages := gjson.GetBytes(p, messagesReqPathMessages)
152152
if !messages.Exists() || messages.Type == gjson.Null {
153153
return "", false, nil
@@ -195,7 +195,7 @@ func (p MessagesRequestPayload) lastUserPrompt() (string, bool, error) {
195195
return "", false, nil
196196
}
197197

198-
func (p MessagesRequestPayload) injectTools(injected []anthropic.ToolUnionParam) (MessagesRequestPayload, error) {
198+
func (p RequestPayload) injectTools(injected []anthropic.ToolUnionParam) (RequestPayload, error) {
199199
if len(injected) == 0 {
200200
return p, nil
201201
}
@@ -221,7 +221,7 @@ func (p MessagesRequestPayload) injectTools(injected []anthropic.ToolUnionParam)
221221
return p.set(messagesReqPathTools, allTools)
222222
}
223223

224-
func (p MessagesRequestPayload) disableParallelToolCalls() (MessagesRequestPayload, error) {
224+
func (p RequestPayload) disableParallelToolCalls() (RequestPayload, error) {
225225
toolChoice := gjson.GetBytes(p, messagesReqPathToolChoice)
226226

227227
// If no tool_choice was defined, assume auto.
@@ -258,7 +258,7 @@ func (p MessagesRequestPayload) disableParallelToolCalls() (MessagesRequestPaylo
258258
}
259259
}
260260

261-
func (p MessagesRequestPayload) appendedMessages(newMessages []anthropic.MessageParam) (MessagesRequestPayload, error) {
261+
func (p RequestPayload) appendedMessages(newMessages []anthropic.MessageParam) (RequestPayload, error) {
262262
if len(newMessages) == 0 {
263263
return p, nil
264264
}
@@ -285,11 +285,11 @@ func (p MessagesRequestPayload) appendedMessages(newMessages []anthropic.Message
285285
return p.set(messagesReqPathMessages, allMessages)
286286
}
287287

288-
func (p MessagesRequestPayload) withModel(model string) (MessagesRequestPayload, error) {
288+
func (p RequestPayload) withModel(model string) (RequestPayload, error) {
289289
return p.set(messagesReqPathModel, model)
290290
}
291291

292-
func (p MessagesRequestPayload) messages() ([]json.RawMessage, error) {
292+
func (p RequestPayload) messages() ([]json.RawMessage, error) {
293293
messages := gjson.GetBytes(p, messagesReqPathMessages)
294294
if !messages.Exists() || messages.Type == gjson.Null {
295295
return nil, nil
@@ -301,7 +301,7 @@ func (p MessagesRequestPayload) messages() ([]json.RawMessage, error) {
301301
return p.resultToRawMessage(messages.Array()), nil
302302
}
303303

304-
func (p MessagesRequestPayload) tools() ([]json.RawMessage, error) {
304+
func (p RequestPayload) tools() ([]json.RawMessage, error) {
305305
tools := gjson.GetBytes(p, messagesReqPathTools)
306306
if !tools.Exists() || tools.Type == gjson.Null {
307307
return nil, nil
@@ -313,7 +313,7 @@ func (p MessagesRequestPayload) tools() ([]json.RawMessage, error) {
313313
return p.resultToRawMessage(tools.Array()), nil
314314
}
315315

316-
func (MessagesRequestPayload) resultToRawMessage(items []gjson.Result) []json.RawMessage {
316+
func (RequestPayload) resultToRawMessage(items []gjson.Result) []json.RawMessage {
317317
// gjson.Result conversion to json.RawMessage is needed because
318318
// gjson.Result does not implement json.Marshaler — would
319319
// serialize its struct fields instead of the raw JSON it represents.
@@ -326,7 +326,7 @@ func (MessagesRequestPayload) resultToRawMessage(items []gjson.Result) []json.Ra
326326

327327
// convertAdaptiveThinkingForBedrock converts thinking.type "adaptive" to "enabled" with a calculated budget_tokens
328328
// conversion is needed for Bedrock models that does not support the "adaptive" thinking.type
329-
func (p MessagesRequestPayload) convertAdaptiveThinkingForBedrock() (MessagesRequestPayload, error) {
329+
func (p RequestPayload) convertAdaptiveThinkingForBedrock() (RequestPayload, error) {
330330
thinkingType := gjson.GetBytes(p, messagesReqPathThinkingType)
331331
if thinkingType.String() != constAdaptive {
332332
return p, nil
@@ -377,7 +377,7 @@ func (p MessagesRequestPayload) convertAdaptiveThinkingForBedrock() (MessagesReq
377377
// removed when the corresponding flag is absent from the Anthropic-Beta header.
378378
// Model-specific beta flags must already be filtered from the header before
379379
// calling this method (see filterBedrockBetaFlags).
380-
func (p MessagesRequestPayload) removeUnsupportedBedrockFields(headers http.Header) (MessagesRequestPayload, error) {
380+
func (p RequestPayload) removeUnsupportedBedrockFields(headers http.Header) (RequestPayload, error) {
381381
var payloadMap map[string]any
382382
if err := json.Unmarshal(p, &payloadMap); err != nil {
383383
return p, xerrors.Errorf("failed to unmarshal request payload when removing unsupported Bedrock fields: %w", err)
@@ -400,13 +400,13 @@ func (p MessagesRequestPayload) removeUnsupportedBedrockFields(headers http.Head
400400
if err != nil {
401401
return p, xerrors.Errorf("failed to marshal request payload when removing unsupported Bedrock fields: %w", err)
402402
}
403-
return MessagesRequestPayload(result), nil
403+
return RequestPayload(result), nil
404404
}
405405

406-
func (p MessagesRequestPayload) set(path string, value any) (MessagesRequestPayload, error) {
406+
func (p RequestPayload) set(path string, value any) (RequestPayload, error) {
407407
out, err := sjson.SetBytes(p, path, value)
408408
if err != nil {
409409
return p, xerrors.Errorf("set %s: %w", path, err)
410410
}
411-
return MessagesRequestPayload(out), nil
411+
return RequestPayload(out), nil
412412
}

0 commit comments

Comments
 (0)