refactor: remove dead parseStreamResponse function
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 <noreply@anthropic.com>
This commit is contained in:
parent
10439a62e4
commit
005254c39d
1 changed files with 0 additions and 90 deletions
|
|
@ -542,93 +542,3 @@ type streamToolCallAcc struct {
|
||||||
Arguments strings.Builder
|
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
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue