diff --git a/internal/gateway/chattidy.go b/internal/gateway/chattidy.go index fe7e23e6a..093f21dfe 100644 --- a/internal/gateway/chattidy.go +++ b/internal/gateway/chattidy.go @@ -77,7 +77,11 @@ import ( // own log says the reply went (AricRedemption). A nameless call runs // nowhere, so it is no call, as a written block naming no offered tool // is; a name of another kind on a later fragment is left out, the first -// one's kept. +// one's kept. And a finish of tool_calls no delta ever carried a call +// for (#4944, Hermes: a reply of text alone whose upstream ends it +// "tool_calls" all the same) ends stop too — a client that reads +// finish_reason took the turn for one whose calls were dropped and +// re-prompted, three times a reply. // // And a legacy function_call that carries nothing — a name and arguments // both empty — is left out too (#4659): WorkBuddy's stream ends a reply @@ -292,9 +296,18 @@ func (t *chatTidy) line(line []byte) []byte { delete(delta, "tool_calls") inDelta = true } - if fc, ok := delta["function_call"]; ok && emptyFunctionCall(fc) { - delete(delta, "function_call") - inDelta = true + if fc, ok := delta["function_call"]; ok { + if fullFunctionCall(fc) { + // a legacy call a client may still be reading (#4659) + // keeps a finish that says the turn ended in a call + if t.named == nil { + t.named = map[int]bool{} + } + t.named[choice] = true + } else if emptyFunctionCall(fc) { + delete(delta, "function_call") + inDelta = true + } } if calls, ok := delta["tool_calls"]; ok { if t.indexes == nil { @@ -338,20 +351,19 @@ func (t *chatTidy) line(line []byte) []byte { c["delta"], _ = json.Marshal(delta) changed = true } - if t.called[choice] { - switch end := string(bytes.TrimSpace(c["finish_reason"])); end { - case `"stop"`: - if t.named[choice] { - c["finish_reason"] = json.RawMessage(`"tool_calls"`) - changed = true - } - case `"tool_calls"`: - if !t.named[choice] { - // the call the finish named named no function: it ends - // stop, as a reply that made no call (#1475) - c["finish_reason"] = json.RawMessage(`"stop"`) - changed = true - } + switch end := string(bytes.TrimSpace(c["finish_reason"])); end { + case `"stop"`: + if t.called[choice] && t.named[choice] { + c["finish_reason"] = json.RawMessage(`"tool_calls"`) + changed = true + } + case `"tool_calls"`: + if !t.named[choice] { + // the call the finish named named no function, or no call + // was ever made and no delta carried the key (#4944): it + // ends stop, as a reply that made no call (#1475) + c["finish_reason"] = json.RawMessage(`"stop"`) + changed = true } } } @@ -384,6 +396,21 @@ func emptyFunctionCall(raw json.RawMessage) bool { return decodeJSON(raw, &fc) == nil && fc.Name == "" && fc.Arguments == "" } +// fullFunctionCall reports whether raw is a legacy function_call a client +// may still be reading: a name or arguments that hold something (#4659). +// A null is the contract's own nothing and an unreadable shape is no call +// a client reads. +func fullFunctionCall(raw json.RawMessage) bool { + if string(bytes.TrimSpace(raw)) == "null" { + return false + } + var fc struct { + Name string `json:"name"` + Arguments string `json:"arguments"` + } + return decodeJSON(raw, &fc) == nil && (fc.Name != "" || fc.Arguments != "") +} + // withoutEmptyNames drops the empty "name" on a delta's tool-call // fragments and reports whether any was dropped. A fragment whose // function carries no name at all is left as it is; one with a real name diff --git a/internal/gateway/chattidy_finish_test.go b/internal/gateway/chattidy_finish_test.go index 4087353af..ad1114702 100644 --- a/internal/gateway/chattidy_finish_test.go +++ b/internal/gateway/chattidy_finish_test.go @@ -53,3 +53,58 @@ func TestChatCallsEndToolCalls(t *testing.T) { t.Fatalf("whole, no call: finish %q\n%s", finish, raw) } } + +// #4944 (Hermes through magpie): a stream whose reply is text alone ends, +// on the reporter's upstream, on a frame that says finish_reason +// "tool_calls" and carries no tool_calls key in any delta — the reporter's +// bare call of the model ends its real calls with just that shape +// ("delta":{"reasoning_content":null},"finish_reason":"tool_calls", +// "matched_stop":1), and its text-only turns end there too. A client that +// reads finish_reason took every such turn for one whose calls were +// dropped and re-prompted, three times a reply. A finish of tool_calls +// with no call the client could read ends stop, whether or not a delta +// ever carried the key. +func TestChatNoCallToolCallsFinishEndsStop(t *testing.T) { + up := []string{ + `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"The user said hi."}}]}`, + `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"收到"}}]}`, + `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"reasoning_content":null},"logprobs":null,"finish_reason":"tool_calls","matched_stop":1}]}`, + } + for _, body := range []string{ + `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"只回复两个字:收到"}],"reasoning_effort":"max",` + execChatTool + `}`, + `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`, + } { + _, calls, finish, raw := relayedChat(t, body, up...) + if finish != "stop" || len(calls) != 0 { + t.Fatalf("stream: calls %v finish %q\n%s", calls, finish, raw) + } + } + // the finish naming a call with the final delta's "tool_calls": null + // beside it (#3259's shape) ends stop the same way + _, _, finish, raw := relayedChat(t, `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`, + `data: {"choices":[{"index":0,"delta":{"content":"hi"}}]}`, + `data: {"choices":[{"index":0,"delta":{"content":null,"tool_calls":null},"finish_reason":"tool_calls"}]}`) + if finish != "stop" { + t.Fatalf("stream, tool_calls null and no call: finish %q\n%s", finish, raw) + } +} + +// A legacy function_call a client may still be reading keeps a finish +// that says the turn ended in a call, "tool_calls" or not (#4659); a +// "function_call": null is the contract's own nothing and is not one. +func TestChatRelayLegacyCallFinish(t *testing.T) { + tools := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"weather?"}],"tools":[{"type":"function","function":{"name":"get_weather","parameters":{"type":"object"}}}]}` + _, _, finish, raw := relayedChat(t, tools, + `data: {"choices":[{"index":0,"delta":{"function_call":{"name":"get_weather","arguments":""}}}]}`, + `data: {"choices":[{"index":0,"delta":{"function_call":{"name":"","arguments":"{\"city\":\"北京\"}"}}}]}`, + `data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`) + if finish != "tool_calls" { + t.Fatalf("stream, a legacy call: finish %q\n%s", finish, raw) + } + _, _, finish, raw = relayedChat(t, tools, + `data: {"choices":[{"index":0,"delta":{"content":"hi","function_call":null}}]}`, + `data: {"choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]}`) + if finish != "stop" { + t.Fatalf("stream, function_call null and no call: finish %q\n%s", finish, raw) + } +}