From 385cd26e4ba857d5d5ad64255c02c7c15d20e353 Mon Sep 17 00:00:00 2001 From: sausheong Date: Sat, 13 Jun 2026 12:44:06 +0800 Subject: [PATCH] fix(openai): strings.Builder for tool-arg accumulation (R7c) --- providers/openai/openai.go | 10 +++++----- providers/openai/openai_test.go | 9 +++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/providers/openai/openai.go b/providers/openai/openai.go index da0188b..17b632a 100644 --- a/providers/openai/openai.go +++ b/providers/openai/openai.go @@ -332,7 +332,7 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (< } } if tc.Function.Arguments != "" { - pending.argsJSON += tc.Function.Arguments + pending.args.WriteString(tc.Function.Arguments) } } @@ -353,9 +353,9 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (< // streamed tool-call index is the map key in toolCalls; the id, name, and // arguments arrive in separate chunks. type pendingTC struct { - id string - name string - argsJSON string + id string + name string + args strings.Builder } // emitToolCalls sends EventToolCallDone for each completed tool call in @@ -379,7 +379,7 @@ func emitToolCalls(events chan<- llm.ChatEvent, toolCalls map[int]*pendingTC) { ToolCall: &llm.ToolCall{ ID: tc.id, Name: tc.name, - Input: json.RawMessage(tc.argsJSON), + Input: json.RawMessage(tc.args.String()), }, } } diff --git a/providers/openai/openai_test.go b/providers/openai/openai_test.go index 667a522..7940c77 100644 --- a/providers/openai/openai_test.go +++ b/providers/openai/openai_test.go @@ -75,11 +75,12 @@ func TestOpenAIChatStreamPartsBeatString(t *testing.T) { } func TestEmitToolCalls_OrderedByIndex(t *testing.T) { - toolCalls := map[int]*pendingTC{ - 2: {id: "c", name: "third", argsJSON: "{}"}, - 0: {id: "a", name: "first", argsJSON: "{}"}, - 1: {id: "b", name: "second", argsJSON: "{}"}, + mk := func(id, name string) *pendingTC { + p := &pendingTC{id: id, name: name} + p.args.WriteString("{}") + return p } + toolCalls := map[int]*pendingTC{2: mk("c", "third"), 0: mk("a", "first"), 1: mk("b", "second")} ch := make(chan llm.ChatEvent, 10) emitToolCalls(ch, toolCalls) close(ch)