diff --git a/internal/gateway/chattidy.go b/internal/gateway/chattidy.go index 427eeeb13..a4bef2dde 100644 --- a/internal/gateway/chattidy.go +++ b/internal/gateway/chattidy.go @@ -24,6 +24,13 @@ import ( // "Tool not found", so the empty duplicate is dropped and the real name // is only sent once. // +// And every delta carries "tool_calls": [], text and thinking too. An +// empty list says nothing, but Qoder (qodercli 1.1.65's stream adapter +// tests delta.tool_calls for being there, not for holding any) closes its +// text block on each one, so each piece of text after it opened a block +// of its own and a reply read a word to a line ("我来 / 看 / 一下 / …"); +// the empty list is left out. +// // Lines pass whole, as they came, unless one has any of them. type chatTidy struct { buf []byte @@ -33,6 +40,7 @@ var ( emptyReasoning = []byte(`"reasoning_content":""`) emptyFinishReason = []byte(`"finish_reason":""`) emptyToolName = []byte(`"name":""`) + emptyToolCalls = []byte(`"tool_calls":[]`) ) // write takes what was read and gives back what to send on: every line @@ -77,7 +85,8 @@ func tidyLines(b []byte) []byte { func needsTidy(line []byte) bool { return bytes.Contains(line, emptyReasoning) || bytes.Contains(line, emptyFinishReason) || - bytes.Contains(line, emptyToolName) + bytes.Contains(line, emptyToolName) || + bytes.Contains(line, emptyToolCalls) } func tidyLine(line []byte) []byte { @@ -113,7 +122,10 @@ func tidyLine(line []byte) []byte { delete(delta, "reasoning_content") inDelta = true } - if calls, ok := delta["tool_calls"]; ok { + if calls, ok := delta["tool_calls"]; ok && string(calls) == "[]" { + delete(delta, "tool_calls") + inDelta = true + } else if ok { if mended, chg := withoutEmptyNames(calls); chg { delta["tool_calls"] = mended inDelta = true diff --git a/internal/gateway/workbuddy_blocks_test.go b/internal/gateway/workbuddy_blocks_test.go new file mode 100644 index 000000000..f1db4b178 --- /dev/null +++ b/internal/gateway/workbuddy_blocks_test.go @@ -0,0 +1,82 @@ +package gateway + +import ( + "net/http/httptest" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// wbTextStream is a WorkBuddy (copilot.tencent.com) reply as it streams: +// every delta, text too, carries "tool_calls": [] and, once the thinking +// is over, an empty reasoning_content and finish_reason. +func wbTextStream() string { + chunk := func(delta, finish string) string { + return `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":` + delta + `,"finish_reason":` + finish + `}]}` + } + return sse( + chunk(`{"role":"assistant","content":"","reasoning_content":"Look first.","tool_calls":[]}`, `""`), + chunk(`{"content":"我来","reasoning_content":"","tool_calls":[]}`, `""`), + chunk(`{"content":"看","reasoning_content":"","tool_calls":[]}`, `""`), + chunk(`{"content":"一下","reasoning_content":"","tool_calls":[]}`, `""`), + chunk(`{"content":"。","reasoning_content":"","tool_calls":[]}`, `""`), + chunk(`{"content":"","reasoning_content":"","tool_calls":[]}`, `"stop"`), + `data: [DONE]`) +} + +// A relayed WorkBuddy stream reaches a Chat client without the empty +// tool_calls list on each delta: Qoder closed its text block on each one, +// so its reply read a word to a line (ARNO on Discord: workbuddy provider +// 断句出错). The text, the thinking and the finish are as they came. +func TestChatRelayDropsEmptyToolCalls(t *testing.T) { + fresh(t) + a := &scripted{replies: []reply{{200, "text/event-stream", wbTextStream()}}} + scriptedOn(t, "a", provider.Chat, a) + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", + strings.NewReader(`{"model":"a/m","stream":true,"messages":[{"role":"user","content":"hi"}]}`))) + body := rec.Body.String() + if rec.Code != 200 || strings.Contains(body, `"tool_calls":[]`) { + t.Fatalf("%d %s", rec.Code, body) + } + for _, s := range []string{`"content":"我来"`, `"content":"看"`, `"content":"一下"`, `"content":"。"`, + `"reasoning_content":"Look first."`, `"finish_reason":"stop"`} { + if !strings.Contains(body, s) { + t.Fatalf("no %s in %s", s, body) + } + } + if !strings.HasSuffix(strings.TrimSpace(body), "data: [DONE]") { + t.Fatal(body) + } +} + +// A tool call's own list is left as it came. +func TestChatTidyKeepsToolCalls(t *testing.T) { + line := `data: {"choices":[{"delta":{"content":"","tool_calls":[{"index":0,"id":"call_1","function":{"name":"grep","arguments":""}}]}}]}` + "\n" + if got := string(tidyLine([]byte(line))); got != line { + t.Fatalf("%s", got) + } +} + +// The same stream spoken to Claude Code's API or to Codex's is one text +// block, one output_text item, after the thinking: the decoder already +// takes an empty list for no call. +func TestWorkBuddyTextIsOneBlock(t *testing.T) { + for _, c := range []struct{ path, body, open string }{ + {"/v1/messages", `{"model":"a/m","max_tokens":100,"stream":true,"messages":[{"role":"user","content":"hi"}]}`, + `"content_block":{"text":"","type":"text"}`}, + {"/v1/responses", `{"model":"a/m","stream":true,"input":"hi"}`, + `"type":"response.content_part.added"`}, + } { + fresh(t) + a := &scripted{replies: []reply{{200, "text/event-stream", wbTextStream()}}} + scriptedOn(t, "a", provider.Chat, a) + rec := httptest.NewRecorder() + New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", c.path, strings.NewReader(c.body))) + body := rec.Body.String() + if rec.Code != 200 || strings.Count(body, c.open) != 1 || !strings.Contains(body, "我来") || !strings.Contains(body, "一下") { + t.Fatalf("%s: %d %s", c.path, rec.Code, body) + } + } +}