fix(qwen): index-order tool emit + strings.Builder accumulation (R7a/R7c)
This commit is contained in:
1 parent
e40733145f
commit
79a69e5dce
2 files changed
+57
-19
No files matched your search
+38
-19
@@ -14,6 +14,41 @@ import (
|
|||||||
"github.com/sausheong/harness/llm"
|
"github.com/sausheong/harness/llm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// pendingTC accumulates a streamed tool call across deltas. args uses
|
||||||
|
// strings.Builder so repeated argument fragments append in O(n) total
|
||||||
|
// (not O(n²) from string concatenation).
|
||||||
|
type pendingTC struct {
|
||||||
|
id string
|
||||||
|
name string
|
||||||
|
args strings.Builder
|
||||||
|
}
|
||||||
|
|
||||||
|
// emitToolCalls sends EventToolCallDone for each completed tool call in
|
||||||
|
// ascending index order so tool_use block order is deterministic across
|
||||||
|
// turns (prompt-cache stability).
|
||||||
|
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.args.String()),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// QwenProvider implements llm.LLMProvider using the Qwen Chat Completions API.
|
// QwenProvider implements llm.LLMProvider using the Qwen Chat Completions API.
|
||||||
// Qwen uses an OpenAI-compatible API, so we reuse the go-openai client.
|
// Qwen uses an OpenAI-compatible API, so we reuse the go-openai client.
|
||||||
type QwenProvider struct {
|
type QwenProvider struct {
|
||||||
@@ -217,11 +252,6 @@ func (p *QwenProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<-c
|
|||||||
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)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
@@ -271,25 +301,14 @@ func (p *QwenProvider) ChatStream(ctx context.Context, req llm.ChatRequest) (<-c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if tc.Function.Arguments != "" {
|
if tc.Function.Arguments != "" {
|
||||||
pending.argsJSON += tc.Function.Arguments
|
pending.args.WriteString(tc.Function.Arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// Emit completed tool calls in index order.
|
||||||
for _, tc := range toolCalls {
|
emitToolCalls(events, toolCalls)
|
||||||
if tc.name != "" {
|
|
||||||
events <- llm.ChatEvent{
|
|
||||||
Type: llm.EventToolCallDone,
|
|
||||||
ToolCall: &llm.ToolCall{
|
|
||||||
ID: tc.id,
|
|
||||||
Name: tc.name,
|
|
||||||
Input: json.RawMessage(tc.argsJSON),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,3 +24,22 @@ func TestQwenResolveSystemPromptEmpty(t *testing.T) {
|
|||||||
got := qwenResolveSystemPrompt(llm.ChatRequest{})
|
got := qwenResolveSystemPrompt(llm.ChatRequest{})
|
||||||
require.Equal(t, "", got)
|
require.Equal(t, "", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEmitToolCalls_OrderedByIndex(t *testing.T) {
|
||||||
|
mk := func(id, name string) *pendingTC {
|
||||||
|
p := &pendingTC{id: id, name: name}
|
||||||
|
p.args.WriteString("{}")
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
toolCalls := map[int]*pendingTC{1: mk("b", "second"), 0: mk("a", "first")}
|
||||||
|
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"}, ids)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user