gateway: a relayed WorkBuddy reply is one block in Qoder again, not a word to a line (ARNO on Discord: workbuddy provider断句出错) — copilot.tencent.com sends "tool_calls": [] on every delta, text too, and Qoder's stream adapter (qodercli 1.1.65) tests delta.tool_calls for being there rather than for holding a call, so it closed its text block on each chunk and opened a new one for the next piece of text. chatTidy, which already mends that stream's empty reasoning_content, finish_reason and tool name, now leaves an empty tool_calls list out; a real call's list goes on as it came. Translated to Messages or Responses the stream was already one text block (the decoder reads an empty list as no call). TestChatRelayDropsEmptyToolCalls fails without the change; TestChatTidyKeepsToolCalls, TestWorkBuddyTextIsOneBlock guard the rest. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user