fix(openai): emit tool calls in index order for prompt-cache determinism (R7a)
This commit is contained in:
1 parent
b85ffeac69
commit
e40733145f
2 files changed
+54
-17
No files matched your search
+36
-17
@@ -271,11 +271,6 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
|||||||
defer stream.Close()
|
defer stream.Close()
|
||||||
|
|
||||||
// Track tool calls being built up across deltas
|
// Track tool calls being built up across deltas
|
||||||
type pendingTC struct {
|
|
||||||
id string
|
|
||||||
name string
|
|
||||||
argsJSON string
|
|
||||||
}
|
|
||||||
toolCalls := make(map[int]*pendingTC)
|
toolCalls := make(map[int]*pendingTC)
|
||||||
|
|
||||||
var lastUsage *llm.Usage
|
var lastUsage *llm.Usage
|
||||||
@@ -343,9 +338,42 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
|||||||
|
|
||||||
// Finish reason
|
// Finish reason
|
||||||
if choice.FinishReason == openai.FinishReasonToolCalls || choice.FinishReason == openai.FinishReasonStop {
|
if choice.FinishReason == openai.FinishReasonToolCalls || choice.FinishReason == openai.FinishReasonStop {
|
||||||
// Emit completed tool calls
|
emitToolCalls(events, toolCalls)
|
||||||
for _, tc := range toolCalls {
|
}
|
||||||
if tc.name != "" {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
events <- llm.ChatEvent{Type: llm.EventDone, Usage: lastUsage}
|
||||||
|
}()
|
||||||
|
|
||||||
|
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{
|
events <- llm.ChatEvent{
|
||||||
Type: llm.EventToolCallDone,
|
Type: llm.EventToolCallDone,
|
||||||
ToolCall: &llm.ToolCall{
|
ToolCall: &llm.ToolCall{
|
||||||
@@ -356,15 +384,6 @@ func (p *OpenAIProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
events <- llm.ChatEvent{Type: llm.EventDone, Usage: lastUsage}
|
|
||||||
}()
|
|
||||||
|
|
||||||
return events, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// openaiUnsupportedFields are JSON Schema fields the OpenAI function-
|
// openaiUnsupportedFields are JSON Schema fields the OpenAI function-
|
||||||
// calling schema rejects. anyOf/oneOf/format are accepted and kept.
|
// calling schema rejects. anyOf/oneOf/format are accepted and kept.
|
||||||
|
|||||||
@@ -73,3 +73,21 @@ func TestOpenAIChatStreamPartsBeatString(t *testing.T) {
|
|||||||
require.GreaterOrEqual(t, len(captured.Messages), 1)
|
require.GreaterOrEqual(t, len(captured.Messages), 1)
|
||||||
require.Equal(t, "new", captured.Messages[0].Content)
|
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")
|
||||||
|
}
|
||||||
Reference in new issue
Block a user