diff --git a/providers/openai/openai.go b/providers/openai/openai.go index e75bd93..da0188b 100644 --- a/providers/openai/openai.go +++ b/providers/openai/openai.go @@ -271,11 +271,6 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (< defer stream.Close() // Track tool calls being built up across deltas - type pendingTC struct { - id string - name string - argsJSON string - } toolCalls := make(map[int]*pendingTC) var lastUsage *llm.Usage @@ -343,19 +338,7 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (< // Finish reason if choice.FinishReason == openai.FinishReasonToolCalls || choice.FinishReason == openai.FinishReasonStop { - // Emit completed tool calls - for _, tc := range toolCalls { - if tc.name != "" { - events <- llm.ChatEvent{ - Type: llm.EventToolCallDone, - ToolCall: &llm.ToolCall{ - ID: tc.id, - Name: tc.name, - Input: json.RawMessage(tc.argsJSON), - }, - } - } - } + emitToolCalls(events, toolCalls) } } } @@ -366,6 +349,42 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (< return events, nil } +// pendingTC accumulates a single streamed tool call across deltas. The +// 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 +} + +// emitToolCalls sends EventToolCallDone for each completed tool call in +// ascending index order. The map key is the streamed tool-call index; +// iterating in order keeps the assistant message's tool_use block order +// deterministic across turns so the prompt cache stays warm. +func emitToolCalls(events chan<- llm.ChatEvent, toolCalls map[int]*pendingTC) { + maxIdx := -1 + for idx := range toolCalls { + if idx > maxIdx { + maxIdx = idx + } + } + for idx := 0; idx <= maxIdx; idx++ { + tc, ok := toolCalls[idx] + if !ok || tc.name == "" { + continue + } + events <- llm.ChatEvent{ + Type: llm.EventToolCallDone, + ToolCall: &llm.ToolCall{ + ID: tc.id, + Name: tc.name, + Input: json.RawMessage(tc.argsJSON), + }, + } + } +} + // openaiUnsupportedFields are JSON Schema fields the OpenAI function- // calling schema rejects. anyOf/oneOf/format are accepted and kept. var openaiUnsupportedFields = []string{"$ref", "definitions"} diff --git a/providers/openai/openai_test.go b/providers/openai/openai_test.go index b8aecef..667a522 100644 --- a/providers/openai/openai_test.go +++ b/providers/openai/openai_test.go @@ -73,3 +73,21 @@ func TestOpenAIChatStreamPartsBeatString(t *testing.T) { require.GreaterOrEqual(t, len(captured.Messages), 1) require.Equal(t, "new", captured.Messages[0].Content) } + +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: "{}"}, + } + ch := make(chan llm.ChatEvent, 10) + emitToolCalls(ch, toolCalls) + close(ch) + var ids []string + for ev := range ch { + if ev.Type == llm.EventToolCallDone { + ids = append(ids, ev.ToolCall.ID) + } + } + require.Equal(t, []string{"a", "b", "c"}, ids, "tool calls must emit in index order") +}