gateway: a relayed Chat stream's tool calls are numbered from 0 in the order they start (hixonchan on X)
An upstream that turns Responses into Chat itself (OpenDesign, deepseek-v4.1-flash) passed output_index on as tool_calls[].index. With a sentence of text first the calls came as 1,1,2,3, and a client that keeps calls in an array by index broke on the whole turn. chatTidy's renumber gave a call its upstream index unless another call had it; it now gives each call the next index in the order calls start, so 1,2,3 is 0,1,2 and the 0,0,1 case still is 0,1,2. A stream numbered from 0 in order is unchanged. The Responses to Chat encoder (chatEncoder) and the Messages and Gemini translations already count calls, not positions; plugin streams go through chatTidy. Run: TestChatTidyNumbersCallsFromZero (text then three parallel calls), red with the old renumber (frames 1-5 kept 1,2,3), green with it; the chatTidy tests; go test ./internal/gateway on mote; vet on darwin, linux nogui and windows. Not run: the full suite before the tag; a real OpenDesign account.
This commit is contained in:
@@ -471,17 +471,23 @@ type openCall struct {
|
||||
// goes by the next free one; the calls after it, by one of their own.
|
||||
// Only a fragment that names its function starts a call: one that
|
||||
// carries only arguments goes on with its index's call, whatever id it
|
||||
// has. A stream whose calls each have their own index goes as it came.
|
||||
// has. The calls are numbered 0, 1, 2 in the order they start, whatever
|
||||
// the upstream numbered them by: one that passes Responses' output_index
|
||||
// on starts its first call at 1 when a text item took 0 (1,1,2,3,
|
||||
// hixonchan on X), and a client that keeps calls in an array by index
|
||||
// broke on it. A stream already numbered from 0 in order goes as it came.
|
||||
func (o *openCall) renumber(n int, id string, starts bool) int {
|
||||
if o.up == nil {
|
||||
o.up, o.upID, o.used = map[int]int{}, map[int]string{}, map[int]bool{}
|
||||
}
|
||||
out, known := o.up[n]
|
||||
if !known || starts && id != "" && o.upID[n] != "" && id != o.upID[n] {
|
||||
out = n
|
||||
if known || o.used[n] {
|
||||
out = o.next
|
||||
}
|
||||
// the calls count from 0 in the order they start, whatever
|
||||
// index the upstream numbered them by: a Responses
|
||||
// output_index has the reply's text at 0, and the first call
|
||||
// at 1 (#4900)
|
||||
out = o.next
|
||||
o.next++
|
||||
o.up[n], o.used[out] = out, true
|
||||
}
|
||||
if id != "" {
|
||||
@@ -517,7 +523,7 @@ func (s streamCallIndexes) give(choice int, raw json.RawMessage) (json.RawMessag
|
||||
if decodeJSON(v, &n) == nil {
|
||||
out := o.renumber(n, id, namesFunction(c["function"]))
|
||||
o.open, o.id, o.index = true, cmp.Or(id, o.id), out
|
||||
o.next = max(o.next, n+1, out+1)
|
||||
o.next = max(o.next, out+1)
|
||||
if out != n {
|
||||
c["index"], _ = marshalPlain(out)
|
||||
changed = true
|
||||
|
||||
@@ -242,3 +242,37 @@ func TestChatTidyRenumbersAReusedIndex(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hixonchan on X (OpenDesign, deepseek-v4.1-flash): a reply of one
|
||||
// sentence and then three parallel calls came with Responses'
|
||||
// output_index as the calls' index, the text holding 0: 1,1,2,3. A client
|
||||
// that keeps calls in an array by index broke on the gap. The calls go
|
||||
// by 0, 1, 2 in the order they start.
|
||||
func TestChatTidyNumbersCallsFromZero(t *testing.T) {
|
||||
var tidy chatTidy
|
||||
frames := []string{
|
||||
`{"index":1,"id":"call_a","type":"function","function":{"name":"read_file","arguments":""}}`,
|
||||
`{"index":1,"function":{"arguments":"{\"path\":\"a\"}"}}`,
|
||||
`{"index":2,"id":"call_b","type":"function","function":{"name":"list_dir","arguments":""}}`,
|
||||
`{"index":2,"function":{"arguments":"{}"}}`,
|
||||
`{"index":3,"id":"call_c","type":"function","function":{"name":"pwd","arguments":""}}`,
|
||||
`{"index":3,"function":{"arguments":"{}"}}`,
|
||||
}
|
||||
want := []float64{0, 0, 1, 1, 2, 2}
|
||||
for i, f := range frames {
|
||||
out := tidy.write([]byte(`data: {"id":"resp_03","choices":[{"index":0,"delta":{"tool_calls":[` + f + `]}}]}` + "\n"))
|
||||
var chunk struct {
|
||||
Choices []struct {
|
||||
Delta struct {
|
||||
ToolCalls []map[string]any `json:"tool_calls"`
|
||||
} `json:"delta"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if err := json.Unmarshal(bytes.TrimPrefix(bytes.TrimSpace(out), []byte("data:")), &chunk); err != nil {
|
||||
t.Fatalf("frame %d unreadable %q: %v", i, out, err)
|
||||
}
|
||||
if got := chunk.Choices[0].Delta.ToolCalls[0]["index"]; got != want[i] {
|
||||
t.Errorf("frame %d: index %v, want %v: %s", i, got, want[i], out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user