diff options
| author | Paul Buetow <paul@buetow.org> | 2025-11-02 23:42:15 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2025-11-02 23:42:15 +0200 |
| commit | 35e1de6f975088ade5dbf0af533fe6fdac8fcc94 (patch) | |
| tree | c9fc9b6ad86cc10a777b3f510c3c4b2d62c41ebd /internal/llm | |
| parent | c60d5703d25b7d76d1da2f368b0632f08a161644 (diff) | |
some linter fixes
Diffstat (limited to 'internal/llm')
| -rw-r--r-- | internal/llm/copilot.go | 20 | ||||
| -rw-r--r-- | internal/llm/copilot_http_test.go | 16 | ||||
| -rw-r--r-- | internal/llm/ollama.go | 14 | ||||
| -rw-r--r-- | internal/llm/openai.go | 14 | ||||
| -rw-r--r-- | internal/llm/openai_http_test.go | 14 | ||||
| -rw-r--r-- | internal/llm/openai_sse_negative_test.go | 4 | ||||
| -rw-r--r-- | internal/llm/openrouter.go | 12 | ||||
| -rw-r--r-- | internal/llm/openrouter_test.go | 4 |
8 files changed, 67 insertions, 31 deletions
diff --git a/internal/llm/copilot.go b/internal/llm/copilot.go index d3b1a9d..b439ed3 100644 --- a/internal/llm/copilot.go +++ b/internal/llm/copilot.go @@ -118,7 +118,11 @@ func (c copilotClient) Chat(ctx context.Context, messages []Message, opts ...Req logging.Logf("llm/copilot ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase) return "", err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/copilot", "failed to close response body: %v", err) + } + }() if err := handleCopilotNon2xx(resp, start); err != nil { return "", err } @@ -144,7 +148,7 @@ func buildCopilotChatRequest(o Options, messages []Message, defaultTemp *float64 req := copilotChatRequest{Model: o.Model} req.Messages = make([]copilotMessage, len(messages)) for i, m := range messages { - req.Messages[i] = copilotMessage{Role: m.Role, Content: m.Content} + req.Messages[i] = copilotMessage(m) } if o.Temperature != 0 { req.Temperature = &o.Temperature @@ -220,7 +224,11 @@ func (c *copilotClient) ensureSession(ctx context.Context) error { if err != nil { return err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/copilot", "failed to close response body: %v", err) + } + }() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return fmt.Errorf("copilot token http error: %d", resp.StatusCode) } @@ -354,7 +362,11 @@ func (c copilotClient) CodeCompletion(ctx context.Context, prompt string, suffix if err != nil { return nil, err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/copilot", "failed to close response body: %v", err) + } + }() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return nil, fmt.Errorf("copilot codex http error: %d", resp.StatusCode) } diff --git a/internal/llm/copilot_http_test.go b/internal/llm/copilot_http_test.go index 9dd4aee..1371f71 100644 --- a/internal/llm/copilot_http_test.go +++ b/internal/llm/copilot_http_test.go @@ -73,8 +73,8 @@ func TestCopilot_CodeCompletion_Success(t *testing.T) { if r.URL.Host == "copilot-proxy.githubusercontent.com" && strings.HasSuffix(r.URL.Path, "/v1/engines/copilot-codex/completions") { rw := httptest.NewRecorder() // two choices for index 0 and 1 - rw.WriteString("data: {\"choices\":[{\"index\":0,\"text\":\"A\"}]}\n") - rw.WriteString("data: {\"choices\":[{\"index\":1,\"text\":\"B\"}]}\n") + _, _ = rw.WriteString("data: {\"choices\":[{\"index\":0,\"text\":\"A\"}]}\n") + _, _ = rw.WriteString("data: {\"choices\":[{\"index\":1,\"text\":\"B\"}]}\n") res := rw.Result() res.StatusCode = 200 return res, nil @@ -164,7 +164,7 @@ func TestCopilot_Chat_DecodeError_StatusOK(t *testing.T) { } // Chat returns 200 but invalid JSON; expect decode error srv := newIPv4Server(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "{invalid") + _, _ = io.WriteString(w, "{invalid") })) defer srv.Close() c := newCopilot(srv.URL, "gpt-4o-mini", "KEY", f64p(0.1)).(copilotClient) @@ -197,9 +197,9 @@ func TestCopilot_CodeCompletion_MalformedAndEmpty(t *testing.T) { if r.URL.Host == "copilot-proxy.githubusercontent.com" && strings.HasSuffix(r.URL.Path, "/v1/engines/copilot-codex/completions") { rw := httptest.NewRecorder() // malformed line - rw.WriteString("data: {bad}\n") + _, _ = rw.WriteString("data: {bad}\n") // done; should produce empty suggestions - rw.WriteString("data: [DONE]\n") + _, _ = rw.WriteString("data: [DONE]\n") res := rw.Result() res.StatusCode = 200 return res, nil @@ -226,9 +226,9 @@ func TestCopilot_CodeCompletion_MalformedAndEmpty(t *testing.T) { } if r.URL.Host == "copilot-proxy.githubusercontent.com" && strings.HasSuffix(r.URL.Path, "/v1/engines/copilot-codex/completions") { rw := httptest.NewRecorder() - rw.WriteString("data: {bad}\n") - rw.WriteString("data: {\"choices\":[{\"index\":0,\"text\":\"OK\"}]}\n") - rw.WriteString("data: [DONE]\n") + _, _ = rw.WriteString("data: {bad}\n") + _, _ = rw.WriteString("data: {\"choices\":[{\"index\":0,\"text\":\"OK\"}]}\n") + _, _ = rw.WriteString("data: [DONE]\n") res := rw.Result() res.StatusCode = 200 return res, nil diff --git a/internal/llm/ollama.go b/internal/llm/ollama.go index 374a771..f355166 100644 --- a/internal/llm/ollama.go +++ b/internal/llm/ollama.go @@ -81,7 +81,11 @@ func (c ollamaClient) Chat(ctx context.Context, messages []Message, opts ...Requ logging.Logf("llm/ollama ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase) return "", err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/ollama", "failed to close response body: %v", err) + } + }() if err := handleOllamaNon2xx(resp, start); err != nil { return "", err } @@ -129,7 +133,11 @@ func (c ollamaClient) ChatStream(ctx context.Context, messages []Message, onDelt logging.Logf("llm/ollama ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase) return err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/ollama", "failed to close response body: %v", err) + } + }() if err := handleOllamaNon2xx(resp, start); err != nil { return err } @@ -172,7 +180,7 @@ func buildOllamaRequest(o Options, messages []Message, defaultTemp *float64, str req := ollamaChatRequest{Model: o.Model, Stream: stream} req.Messages = make([]oaMessage, len(messages)) for i, m := range messages { - req.Messages[i] = oaMessage{Role: m.Role, Content: m.Content} + req.Messages[i] = oaMessage(m) } optsMap := map[string]any{} if o.Temperature != 0 { diff --git a/internal/llm/openai.go b/internal/llm/openai.go index c284bb3..b97111d 100644 --- a/internal/llm/openai.go +++ b/internal/llm/openai.go @@ -121,7 +121,11 @@ func (c openAIClient) Chat(ctx context.Context, messages []Message, opts ...Requ logging.Logf("llm/openai ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase) return "", err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/openai", "failed to close response body: %v", err) + } + }() if err := handleOpenAINon2xx(resp, start, "llm/openai ", "openai"); err != nil { return "", err } @@ -172,7 +176,11 @@ func (c openAIClient) ChatStream(ctx context.Context, messages []Message, onDelt logging.Logf("llm/openai ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase) return err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/openai", "failed to close response body: %v", err) + } + }() if err := handleOpenAINon2xx(resp, start, "llm/openai ", "openai"); err != nil { return err } @@ -200,7 +208,7 @@ func buildOAChatRequest(o Options, messages []Message, defaultTemp *float64, str req := oaChatRequest{Model: o.Model, Stream: stream} req.Messages = make([]oaMessage, len(messages)) for i, m := range messages { - req.Messages[i] = oaMessage{Role: m.Role, Content: m.Content} + req.Messages[i] = oaMessage(m) } if o.Temperature != 0 { req.Temperature = &o.Temperature diff --git a/internal/llm/openai_http_test.go b/internal/llm/openai_http_test.go index affcae9..d0fc828 100644 --- a/internal/llm/openai_http_test.go +++ b/internal/llm/openai_http_test.go @@ -45,8 +45,8 @@ func TestOpenAI_ChatStream_SSE(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Return SSE-like stream w.Header().Set("Content-Type", "text/event-stream") - io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}]}\n\n") - io.WriteString(w, "data: [DONE]\n") + _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}]}\n\n") + _, _ = io.WriteString(w, "data: [DONE]\n") })) defer srv.Close() c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) @@ -71,8 +71,8 @@ func TestOpenAI_ChatStream_SSE_ErrorChunk(t *testing.T) { } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") - io.WriteString(w, "data: {\"error\":{\"message\":\"oops\"}}\n\n") - io.WriteString(w, "data: [DONE]\n") + _, _ = io.WriteString(w, "data: {\"error\":{\"message\":\"oops\"}}\n\n") + _, _ = io.WriteString(w, "data: [DONE]\n") })) defer srv.Close() c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) @@ -104,8 +104,8 @@ func TestOpenAI_ChatStream_SSE_EmptyDelta_NoError(t *testing.T) { } srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") - io.WriteString(w, "data: {\\\"choices\\\":[{\\\"delta\\\":{\\\"content\\\":\\\"\\\"}}]}\\n\\n") - io.WriteString(w, "data: [DONE]\\n") + _, _ = io.WriteString(w, "data: {\\\"choices\\\":[{\\\"delta\\\":{\\\"content\\\":\\\"\\\"}}]}\\n\\n") + _, _ = io.WriteString(w, "data: [DONE]\\n") })) defer srv.Close() c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) @@ -126,7 +126,7 @@ func TestOpenAI_Chat_DecodeError_StatusOK(t *testing.T) { // Return status 200 but invalid JSON body; Chat should return an error srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) - io.WriteString(w, "{invalid") + _, _ = io.WriteString(w, "{invalid") })) defer srv.Close() c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) diff --git a/internal/llm/openai_sse_negative_test.go b/internal/llm/openai_sse_negative_test.go index de2ff71..7f4f7db 100644 --- a/internal/llm/openai_sse_negative_test.go +++ b/internal/llm/openai_sse_negative_test.go @@ -16,8 +16,8 @@ func TestOpenAI_ChatStream_SSE_MalformedChunk(t *testing.T) { // Malformed JSON chunk should be skipped; no onDelta calls; no error. srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") - io.WriteString(w, "data: {not json}\n\n") - io.WriteString(w, "data: [DONE]\n") + _, _ = io.WriteString(w, "data: {not json}\n\n") + _, _ = io.WriteString(w, "data: [DONE]\n") })) defer srv.Close() c := newOpenAI(srv.URL, "g", "KEY", f64p(0.2)).(openAIClient) diff --git a/internal/llm/openrouter.go b/internal/llm/openrouter.go index f03844a..4aae398 100644 --- a/internal/llm/openrouter.go +++ b/internal/llm/openrouter.go @@ -65,7 +65,11 @@ func (c openRouterClient) Chat(ctx context.Context, messages []Message, opts ... logging.Logf("llm/openrouter ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase) return "", err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/openrouter", "failed to close response body: %v", err) + } + }() if err := handleOpenAINon2xx(resp, start, "llm/openrouter ", "openrouter"); err != nil { return "", err } @@ -111,7 +115,11 @@ func (c openRouterClient) ChatStream(ctx context.Context, messages []Message, on logging.Logf("llm/openrouter ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase) return err } - defer resp.Body.Close() + defer func() { + if err := resp.Body.Close(); err != nil { + logging.Logf("llm/openrouter", "failed to close response body: %v", err) + } + }() if err := handleOpenAINon2xx(resp, start, "llm/openrouter ", "openrouter"); err != nil { return err } diff --git a/internal/llm/openrouter_test.go b/internal/llm/openrouter_test.go index 2a07be0..f8efe16 100644 --- a/internal/llm/openrouter_test.go +++ b/internal/llm/openrouter_test.go @@ -75,8 +75,8 @@ func TestOpenRouter_ChatStream_SendsHeaders(t *testing.T) { acceptHeader = r.Header.Get("Accept") referer = r.Header.Get("HTTP-Referer") w.Header().Set("Content-Type", "text/event-stream") - io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\n") - io.WriteString(w, "data: [DONE]\n") + _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\n") + _, _ = io.WriteString(w, "data: [DONE]\n") })) defer srv.Close() |
