diff --git a/internal/gateway/chattidy.go b/internal/gateway/chattidy.go index 6408b979b..86276043b 100644 --- a/internal/gateway/chattidy.go +++ b/internal/gateway/chattidy.go @@ -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 diff --git a/internal/gateway/chattidy_test.go b/internal/gateway/chattidy_test.go index 283f92f7b..9c392ab0b 100644 --- a/internal/gateway/chattidy_test.go +++ b/internal/gateway/chattidy_test.go @@ -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) + } + } +}