From f2c7536d7d921f208988711f71740a898ff1b83c Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 18:01:19 +0800 Subject: [PATCH] =?UTF-8?q?gateway:=20a=20relayed=20WorkBuddy=20reply=20is?= =?UTF-8?q?=20one=20block=20in=20Qoder=20again,=20not=20a=20word=20to=20a?= =?UTF-8?q?=20line=20(ARNO=20on=20Discord:=20workbuddy=20provider=E6=96=AD?= =?UTF-8?q?=E5=8F=A5=E5=87=BA=E9=94=99)=20=E2=80=94=20copilot.tencent.com?= =?UTF-8?q?=20sends=20"tool=5Fcalls":=20[]=20on=20every=20delta,=20text=20?= =?UTF-8?q?too,=20and=20Qoder's=20stream=20adapter=20(qodercli=201.1.65)?= =?UTF-8?q?=20tests=20delta.tool=5Fcalls=20for=20being=20there=20rather=20?= =?UTF-8?q?than=20for=20holding=20a=20call,=20so=20it=20closed=20its=20tex?= =?UTF-8?q?t=20block=20on=20each=20chunk=20and=20opened=20a=20new=20one=20?= =?UTF-8?q?for=20the=20next=20piece=20of=20text.=20chatTidy,=20which=20alr?= =?UTF-8?q?eady=20mends=20that=20stream's=20empty=20reasoning=5Fcontent,?= =?UTF-8?q?=20finish=5Freason=20and=20tool=20name,=20now=20leaves=20an=20e?= =?UTF-8?q?mpty=20tool=5Fcalls=20list=20out;=20a=20real=20call's=20list=20?= =?UTF-8?q?goes=20on=20as=20it=20came.=20Translated=20to=20Messages=20or?= =?UTF-8?q?=20Responses=20the=20stream=20was=20already=20one=20text=20bloc?= =?UTF-8?q?k=20(the=20decoder=20reads=20an=20empty=20list=20as=20no=20call?= =?UTF-8?q?).=20TestChatRelayDropsEmptyToolCalls=20fails=20without=20the?= =?UTF-8?q?=20change;=20TestChatTidyKeepsToolCalls,=20TestWorkBuddyTextIsO?= =?UTF-8?q?neBlock=20guard=20the=20rest.=20go=20vet,=20go=20build=20-tags?= =?UTF-8?q?=20nogui=20(darwin,=20GOOS=3Dlinux),=20GOOS=3Dwindows=20go=20bu?= =?UTF-8?q?ild,=20go=20test=20-tags=20nogui=20./...=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gateway/chattidy.go | 16 ++++- internal/gateway/workbuddy_blocks_test.go | 82 +++++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 internal/gateway/workbuddy_blocks_test.go 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) + } + } +}