From 25614ad874b33884d23d5ec7d5143accea2f62e7 Mon Sep 17 00:00:00 2001 From: dj-oyu <68707227+dj-oyu@users.noreply.github.com> Date: Tue, 24 Feb 2026 15:45:26 +0900 Subject: [PATCH] refactor: remove dead parseStreamResponse function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parseStreamResponse in openai_compat provider was never called — all streaming is handled by processStreamResponse. Removing dead code that contained the originally flagged redundant Unmarshal. Co-Authored-By: Claude Opus 4.6 --- pkg/providers/openai_compat/provider.go | 90 ------------------------- 1 file changed, 90 deletions(-) diff --git a/pkg/providers/openai_compat/provider.go b/pkg/providers/openai_compat/provider.go index f695fd596..e09485edf 100644 --- a/pkg/providers/openai_compat/provider.go +++ b/pkg/providers/openai_compat/provider.go @@ -542,93 +542,3 @@ type streamToolCallAcc struct { Arguments strings.Builder } -// parseStreamResponse reads an SSE (text/event-stream) response and -// accumulates it into a single LLMResponse. -func parseStreamResponse(r io.Reader) (*LLMResponse, error) { - scanner := bufio.NewScanner(r) - // Allow up to 1 MB per SSE line to handle large argument deltas. - scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024) - - var content strings.Builder - var toolCalls []streamToolCallAcc - var finishReason string - var usage *UsageInfo - - for scanner.Scan() { - line := scanner.Text() - if !strings.HasPrefix(line, "data: ") { - continue - } - data := strings.TrimPrefix(line, "data: ") - if data == "[DONE]" { - break - } - - var chunk streamChunk - if err := json.Unmarshal([]byte(data), &chunk); err != nil { - continue // skip malformed chunks - } - - if len(chunk.Choices) == 0 { - if chunk.Usage != nil { - usage = chunk.Usage - } - continue - } - - choice := chunk.Choices[0] - if choice.Delta.Content != "" { - content.WriteString(choice.Delta.Content) - } - if choice.FinishReason != "" { - finishReason = choice.FinishReason - } - - // Accumulate streaming tool calls by index. - for _, tc := range choice.Delta.ToolCalls { - for len(toolCalls) <= tc.Index { - toolCalls = append(toolCalls, streamToolCallAcc{}) - } - if tc.ID != "" { - toolCalls[tc.Index].ID = tc.ID - } - if tc.Function != nil { - if tc.Function.Name != "" { - toolCalls[tc.Index].Name = tc.Function.Name - } - toolCalls[tc.Index].Arguments.WriteString(tc.Function.Arguments) - } - } - - if chunk.Usage != nil { - usage = chunk.Usage - } - } - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("reading stream: %w", err) - } - - result := &LLMResponse{ - Content: content.String(), - FinishReason: finishReason, - Usage: usage, - } - - for _, tc := range toolCalls { - arguments := make(map[string]any) - argStr := tc.Arguments.String() - if argStr != "" { - if err := json.Unmarshal([]byte(argStr), &arguments); err != nil { - log.Printf("openai_compat: failed to decode streamed tool call arguments for %q: %v", tc.Name, err) - arguments["raw"] = argStr - } - } - result.ToolCalls = append(result.ToolCalls, ToolCall{ - ID: tc.ID, - Name: tc.Name, - Arguments: arguments, - }) - } - - return result, nil -}