Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

gateway: DeepSeek's calls close left in a reply's text (</||DSML|| calls>) is dropped, after a call the upstream made too

yetoned1173c8c16b9
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 26a0d0c71..601232be5 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -161,7 +161,7 @@ recorded as a prediction failure (`noPrediction`).
- A Responses upstream that wants a turn's reasoning back (`replaysReasoning` in [`responses.go`](../../internal/gateway/responses.go): a model with `deepseek` in its name, on any host but OpenAI's, xAI's, Copilot's and Azure's) gets every reasoning item with a `reasoning_text` and every tool call after one, relayed (`withReasoningText` in `passthrough`) or translated (`buildResponses`). DeepSeek's `/v1/responses` turns the whole request away ("The `reasoning_text` in the thinking mode must be passed back to the API") over a reasoning item without text, one with an empty text, and a `function_call` whose step (since the last user message or tool output) has no reasoning item. Codex sends all three in a routing group that falls to DeepSeek after ChatGPT: the backend's sealed items (`encrypted_content` only), magpie's summary-only items, and another member's calls with none (#1104, #1592). DeepSeek reads a step (a run of reasoning, assistant messages and calls) as one assistant message whose thinking is the item it starts with: in the current turn, a step with a call or after a tool output must lead with a reasoning item, so [message, reasoning, call] and [output, message] are refused while [reasoning, message, call] and [output, reasoning, message] are taken (checked against api.deepseek.com on 2026-10-12). Codex sends a step that said something before its call with no reasoning for another model's turn, which magpie used to send [message, reasoning " ", call] and DeepSeek refused intermittently, only while the turn lasted (#1822, through OpenCode Go). An item's own text is kept; one without gets its summary, else a space (`noReasoning`); each such step leads with its reasoning (`reasonFirst`): its own first reasoning item moved ahead, else one holding a space. A sealed item keeps its id and `encrypted_content`; a body already in that shape goes byte for byte.
- A Messages upstream that wants a turn's thinking back (`anthropicReplaysThinking` in [`anthropic.go`](../../internal/gateway/anthropic.go): a host or a model with `deepseek` in it, never `api.anthropic.com` or a Claude model) gets a thinking block on every assistant message with a `tool_use` or right after tool results alone (refused the same way, checked 2026-10-12, #1822), relayed (`withThinkingOnToolTurns` in `passthrough`) or translated (`build`, which also keeps unsigned thinking as its text there). DeepSeek's `/anthropic/v1/messages`, thinking (its default, or asked for), turns the whole request away with 400 "The `content[].thinking` in the thinking mode must be passed back to the API" when such a message has none, and answers any `redacted_thinking` with 422, a block it can't read. Claude Code sends both in a routing group that falls to DeepSeek after Claude (#1611). A message with no thinking gets one holding a space (`noReasoning`) first; Claude's `redacted_thinking` becomes that block, or is left out beside thinking of the message's own. Signed thinking, DeepSeek's own and every other message go as they came. Checked against the real endpoint on 2026-10-11: it takes Claude's signed thinking, unsigned thinking and a space, anywhere in the message.
- A Grok model writes a whole number in a call's arguments with a fraction of zeros (`2500.0`, `17277.0`), whoever serves it: the Grok subscription or its plugin, xAI, OpenRouter or a relay. Codex reads its integer fields as u64, usize or i32 even though their schemas say `number`, so serde turns away the whole call (#1431). In a reply to a request whose model, as magpie names it or as it is sent upstream, has a last `/` segment beginning with `grok` (`grokModel`, `grokServed`), every number in a call's arguments written with a fraction of zeros and no exponent loses the fraction: `2500.0` becomes `2500`, `-3.00` becomes `-3`. Strings, keys, other fractions (`2.5`, `2.50`) and exponents (`1.0e3`) stay as written. No tool schema is consulted, since JSON has one number type. The rule runs over the arguments as a stream of text (`wholeNums`), holding back only a fraction of zeros until the next byte. This gives the same arguments whether they come whole or in deltas cut anywhere, so what a call's deltas add up to is what its done events say. Relayed replies are rewritten in `passthrough` by `intTidy`: Responses' function_call items, argument deltas and done events and the completed response; Chat's `tool_calls` deltas and messages; Messages' `tool_use` input and `input_json_delta`, streamed or whole. Translated replies are rewritten at the encoder (`wholeEncoder`, outside `usageEncoder`) and at `render` (`wholeResult`) when `Request.Grok` is set. Any other model's reply is relayed byte for byte ([`grok_integral.go`](../../internal/gateway/grok_integral.go)).
-- A tool call a model writes into its reply's text, when the request offered tools, is made the call it is ([`textcalls.go`](../../internal/gateway/textcalls.go), #823): Hermes' `<tool_call>{…}</tool_call>`, DeepSeek's DSML (`<|DSML|invoke name=…>` with its parameters; its mark found as the parse regexes read it, by `dsmlMark`, not from a list of spellings: any run of pipes, ASCII or full-width or mixed, spaces after `<` and around `/`, `<||DSML||` too, #2623), DeepSeek's half-Hermes `<parameter>` form (#917) and GLM's `<arg_key>`/`<arg_value>` (#906), read by `parseTextCalls`. Each block must name an offered tool and have arguments that can be read; else the text goes on as it came. The text before the first block is kept, what is left of the tags goes, and the reply ends as one that called tools (`finish_reason` `tool_calls`, `stop_reason` `tool_use`). A reply whose model made a call itself is left alone. Every route reads it the same way: a relayed Chat stream (`textCallTidy`), a relayed Chat reply that came whole, JSON or under `text/plain` or no type (`chatWhole` with `wholeTextCalls`, #1845: a `stream:false` request kept the markup and `finish_reason` `stop`), which also covers a vendor that answered a streamed request whole (`wholeAsStream`), and every translation, which magpie always asks streamed, a plugin's included, whatever the client asked (`textCallSee`). A relayed Responses or Messages upstream's text is not read for calls. Covered by `TestTextWritten*`, `TestGLMTextCall*`, `TestPluginTextWrittenCall` and `TestQuietWholeReplyStreamed`.
+- A tool call a model writes into its reply's text, when the request offered tools, is made the call it is ([`textcalls.go`](../../internal/gateway/textcalls.go), #823): Hermes' `<tool_call>{…}</tool_call>`, DeepSeek's DSML (`<|DSML|invoke name=…>` with its parameters; its mark found as the parse regexes read it, by `dsmlMark`, not from a list of spellings: any run of pipes, ASCII or full-width or mixed, spaces after `<` and around `/`, `<||DSML||` too, #2623), DeepSeek's half-Hermes `<parameter>` form (#917) and GLM's `<arg_key>`/`<arg_value>` (#906), read by `parseTextCalls`. Each block must name an offered tool and have arguments that can be read; else the text goes on as it came. The text before the first block is kept, what is left of the tags goes, and the reply ends as one that called tools (`finish_reason` `tool_calls`, `stop_reason` `tool_use`). A DSML close with no block before it (`</||DSML|| calls>`) is held as a block is, and dropped when it is tags and nothing else. A reply whose model made a call itself has no call read out of its text, but tags and nothing else left in it after the call go, with the spaces before them (喜欢一个人 on Discord: a relay made the calls of DeepSeek's invokes and left the wrapper's close as text). Every route reads it the same way: a relayed Chat stream (`textCallTidy`), a relayed Chat reply that came whole, JSON or under `text/plain` or no type (`chatWhole` with `wholeTextCalls`, #1845: a `stream:false` request kept the markup and `finish_reason` `stop`), which also covers a vendor that answered a streamed request whole (`wholeAsStream`), and every translation, which magpie always asks streamed, a plugin's included, whatever the client asked (`textCallSee`). A relayed Responses or Messages upstream's text is not read for calls. Covered by `TestTextWritten*`, `TestDSMLClose*`, `TestDSMLBlockClosedWithCalls`, `TestGLMTextCall*`, `TestPluginTextWrittenCall` and `TestQuietWholeReplyStreamed`.
- Structured output is carried across every translation (`Format`, [`format.go`](../../internal/gateway/format.go)). It is read from Responses' `text.format`, Chat's `response_format`, Anthropic's `output_config.format` and Gemini's `responseMimeType` with `responseJsonSchema`/`responseSchema`: `json_schema` with its name, description, schema and strict, or `json_object`. Each upstream is asked in its own field: `text.format` on Responses, `response_format` on Chat, `output_config.format` on Messages, and `generationConfig.responseMimeType`/`responseJsonSchema` on Gemini and Code Assist. An API with no name for it gets `response`. Messages and the Claude subscription's `--json-schema` take a schema only, so `json_object` is told in the system prompt there (`inSystem`). A 400 or 422 that names the field (`refusesFormat`: DeepSeek's "This response_format type is unavailable now", a schema Anthropic can't hold) marks the provider and model (`formatRefused`), and the request is asked again with the format told in the system prompt, as are later requests to that provider and model. A relayed request keeps its format as sent. A Gemini client's format is also still told in its system prompt, as before. A model may still wrap its JSON answer in a Markdown code fence; a client that asked for structured output, in a field or told in the system prompt (`wantsJSON`), gets the JSON inside a single unlabelled or json-labelled fence when that content is a complete valid JSON value, streamed (`fenceEncoder`) or whole (`unfencedResult`), in [`format_fence.go`](../../internal/gateway/format_fence.go). Invalid or truncated JSON, other fence languages, text outside the fence, and answers to plain-text clients reach the client as they came. This normalization applies to translated replies; native protocol passthrough stays byte-preserving.
- A tool's images reach the model on every translation (`toolOutput` in [`ir.go`](../../internal/gateway/ir.go) reads them from Anthropic's `tool_result`, Responses' `function_call_output`, such as Codex's `view_image` output `[{"type":"input_image","image_url":"data:…"}]`, and a Chat tool message). Anthropic's Messages keeps them in the `tool_result`. Chat puts them in a user message right after the results, and the result says they follow; Gemini and Code Assist after the function's response. Kiro's results hold text alone, so Kiro's plugin (from 0.1.9 on) sends them with the message that answers the call, and only the latest images go again (#369, #1861). `TestCodexToolImageReachesAnthropicPlugin` and the Kiro plugin's `tool-images.test.mjs`.
- Group members are `provider/model[:effort]`. A member with a fixed effort is asked for that effort, bounded by the group's levels (#671).
diff --git a/internal/gateway/textcalls.go b/internal/gateway/textcalls.go
index 2370020ac..5da3075ec 100644
--- a/internal/gateway/textcalls.go
+++ b/internal/gateway/textcalls.go
@@ -152,7 +152,10 @@ func chatToolNames(body []byte) map[string]bool {
// textCallAt is where in s a call written into the text begins, -1 for
// nowhere; and, when it doesn't, how much of s's end could be one's
-// beginning still being written.
+// beginning still being written. A DSML close begins one too: DeepSeek
+// writes its calls wrapper's close however it likes (</||DSML|| calls>,
+// 喜欢一个人 on Discord), and one left behind with nothing before it, after
+// calls an upstream made of the invokes, is held to be dropped, not shown.
func textCallAt(s string) (at, tail int) {
at = -1
for _, m := range textCallMarks {
@@ -161,7 +164,7 @@ func textCallAt(s string) (at, tail int) {
}
}
for i := strings.IndexByte(s, '<'); i >= 0 && (at < 0 || i < at); {
- n, more := dsmlMark(s[i:], false)
+ n, more := dsmlMark(s[i:], true)
if n > 0 {
at = i
break
@@ -242,6 +245,39 @@ func parseTextCalls(s string, names map[string]bool) (calls []writtenCall, rest
return calls, rest, len(calls) > 0 || rest == ""
}
+// tagsOnly reports whether held text is the tags of calls and nothing
+// else: no call read out of it, and no text left once they go.
+func tagsOnly(s string) bool {
+ calls, rest, ok := parseTextCalls(s, nil)
+ return ok && len(calls) == 0 && rest == ""
+}
+
+// spaceBack keeps s, the text about to be handed on, back with what is
+// held or pending when it is only spaces after a call the reply made
+// itself (native).
+func spaceBack(native, holding bool, pending, held, s string) (string, string, string) {
+ if !native || s == "" || strings.TrimSpace(s) != "" {
+ return pending, held, s
+ }
+ if holding {
+ return pending, s + held, ""
+ }
+ return s + pending, held, ""
+}
+
+// settled is what is held and pending once the reply ends or makes a
+// call: held tags and nothing else go, and so does what is only spaces
+// after a call the reply made itself.
+func settled(native, holding bool, pending, held string) (string, string, bool) {
+ if holding && tagsOnly(held) {
+ held, holding = "", false
+ }
+ if native && strings.TrimSpace(pending+held) == "" {
+ pending, held, holding = "", "", false
+ }
+ return pending, held, holding
+}
+
// cutMark is the end of s that is a mark's beginning cut short (</ of
// </tool_call>), "" when there's none; a lone < may be text.
func cutMark(s string) string {
@@ -531,15 +567,15 @@ type textCallSee struct {
pending string // the text's end, which may be a mark begun
held string
holding bool
- native bool // the reply made a call itself: nothing is read out of its text
+ // the reply made a call itself: no call is read out of its text, but
+ // tags left in it (an upstream that made calls of DeepSeek's invokes
+ // left the wrapper's </||DSML|| calls>) are still dropped
+ native bool
}
func (t *textCallSee) event(ev Event) {
switch ev.Kind {
case KText:
- if t.native {
- break
- }
s := t.pending + ev.Text
t.pending = ""
if t.holding {
@@ -552,14 +588,18 @@ func (t *textCallSee) event(ev Event) {
} else {
t.pending, s = s[len(s)-tail:], s[:len(s)-tail]
}
+ s = t.keepSpace(s)
if s != "" {
t.see(Event{Kind: KText, Text: s})
}
return
case KToolStart:
- t.release()
+ t.settle()
t.native = true
case KStop:
+ if t.native {
+ t.settle()
+ }
if t.holding && !t.native {
if calls, rest, ok := parseTextCalls(t.held, t.names); ok {
t.holding, t.held = false, ""
@@ -582,6 +622,21 @@ func (t *textCallSee) event(ev Event) {
t.see(ev)
}
+// keepSpace keeps back text that is only spaces after a call the reply
+// made itself: the line break before tags left after the calls goes with
+// them. What it gives is the text to hand on now.
+func (t *textCallSee) keepSpace(s string) string {
+ t.pending, t.held, s = spaceBack(t.native, t.holding, t.pending, t.held, s)
+ return s
+}
+
+// settle drops what is held when it is tags and nothing else, or after a
+// call the reply made only spaces, and hands it on as text when it isn't.
+func (t *textCallSee) settle() {
+ t.pending, t.held, t.holding = settled(t.native, t.holding, t.pending, t.held)
+ t.release()
+}
+
// release hands on as text what is held.
func (t *textCallSee) release() {
if s := t.pending + t.held; s != "" {
@@ -637,6 +692,13 @@ func (t *textCallTidy) chunk(delta map[string]any, finish any) []byte {
return append(append([]byte("data: "), b...), '\n', '\n')
}
+// settle is release, but what is held goes as textCallSee's settle has
+// it go.
+func (t *textCallTidy) settle() []byte {
+ t.pending, t.held, t.holding = settled(t.native, t.holding, t.pending, t.held)
+ return t.release()
+}
+
func (t *textCallTidy) release() []byte {
s := t.pending + t.held
t.pending, t.held, t.holding = "", "", false
@@ -675,9 +737,9 @@ func (t *textCallTidy) line(line []byte) []byte {
var pre []byte
changed := false
if calls, _ := delta["tool_calls"].([]any); len(calls) > 0 && !t.native {
- pre, t.native = t.release(), true
+ pre, t.native = t.settle(), true
}
- if s, isStr := delta["content"].(string); isStr && s != "" && !t.native {
+ if s, isStr := delta["content"].(string); isStr && s != "" {
s = t.pending + s
t.pending = ""
if t.holding {
@@ -688,6 +750,7 @@ func (t *textCallTidy) line(line []byte) []byte {
} else {
t.pending, s = s[len(s)-tail:], s[:len(s)-tail]
}
+ t.pending, t.held, s = spaceBack(t.native, t.holding, t.pending, t.held, s)
if s == "" {
delete(delta, "content")
} else {
@@ -713,7 +776,7 @@ func (t *textCallTidy) line(line []byte) []byte {
changed = true
}
} else {
- pre = append(pre, t.release()...)
+ pre = append(pre, t.settle()...)
}
}
if !changed {
@@ -748,9 +811,7 @@ func wholeTextCalls(msg map[string]json.RawMessage, names map[string]bool) (wrot
return false, false
}
var native []json.RawMessage
- if decodeJSON(msg["tool_calls"], &native) == nil && len(native) > 0 {
- return false, false
- }
+ made := decodeJSON(msg["tool_calls"], &native) == nil && len(native) > 0
var s string
if decodeJSON(msg["content"], &s) != nil {
return false, false
@@ -759,6 +820,19 @@ func wholeTextCalls(msg map[string]json.RawMessage, names map[string]bool) (wrot
if at < 0 {
return false, false
}
+ if made {
+ // the upstream made the calls itself: only tags left in its
+ // text go (</||DSML|| calls>, 喜欢一个人 on Discord)
+ if !tagsOnly(s[at:]) {
+ return false, false
+ }
+ if text := strings.TrimRight(s[:at], " \t\r\n"); text != "" {
+ msg["content"], _ = marshalPlain(text)
+ } else {
+ msg["content"] = json.RawMessage("null")
+ }
+ return true, true
+ }
calls, rest, ok := parseTextCalls(s[at:], names)
if !ok {
return false, false
diff --git a/internal/gateway/textcalls_close_test.go b/internal/gateway/textcalls_close_test.go
new file mode 100644
index 000000000..ac1c7a0b9
--- /dev/null
+++ b/internal/gateway/textcalls_close_test.go
@@ -0,0 +1,160 @@
+package gateway
+
+import (
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ "github.com/yetone/magpie/internal/provider"
+)
+
+// 喜欢一个人 on Discord (v0.1.1299, a Responses client, deepseek-v4.1-flash
+// through a relay): the client got the text "\n</||DSML|| calls>",
+// DeepSeek's calls wrapper closed as it writes it (no tool_, a space after
+// the pipes), and nothing went on. These are the reporter's bytes: the
+// close as it came, and the invokes and parameters with two pipes as their
+// client's history has them.
+const reportedClose = "\n</||DSML|| calls>"
+
+const reportedBlock = "<||DSML||tool_calls>\n<||DSML||invoke name=\"exec_command\">\n<||DSML||parameter name=\"cmd\" string=\"true\">ls -la</||DSML||parameter>\n</||DSML||invoke>" + reportedClose
+
+const execChatTool = `"tools":[{"type":"function","function":{"name":"exec_command","parameters":{"type":"object","properties":{"cmd":{"type":"string"}}}}}]`
+
+const execResponsesTool = `"tools":[{"type":"function","name":"exec_command","parameters":{"type":"object","properties":{"cmd":{"type":"string"}}}}]`
+
+// nativeExec is a chunk in which the upstream made the call itself.
+const nativeExec = `data: {"id":"c1","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_up","type":"function","function":{"name":"exec_command","arguments":"{\"cmd\":\"ls -la\"}"}}]},"finish_reason":null}]}`
+
+// cuts are s sent as two chunks at every place a chunk's JSON can cut it.
+func cuts(s string) [][]string {
+ var out [][]string
+ for i := 1; i < len(s); i++ {
+ if utf8.ValidString(s[:i]) {
+ out = append(out, []string{chunkOf(s[:i]), chunkOf(s[i:])})
+ }
+ }
+ return out
+}
+
+// A relay that made the call of DeepSeek's invokes itself left the
+// wrapper's close in the text after it: the close goes, with the line
+// break before it, on every route and wherever the stream cuts it.
+func TestDSMLCloseLeftAfterCalls(t *testing.T) {
+ chat := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"go"}],` + execChatTool + `}`
+ for _, before := range []string{"", "<||DSML|| calls>\n", "<||DSML||tool_calls>"} {
+ for _, parts := range cuts(reportedClose) {
+ up := []string{nativeExec}
+ if before != "" {
+ up = []string{chunkOf(before), nativeExec}
+ }
+ text, calls, _, raw := relayedChat(t, chat, append(append(up, parts...), stopChunk)...)
+ if text != "" || calls["exec_command"] != `{"cmd":"ls -la"}` || strings.Contains(raw, "DSML") {
+ t.Fatalf("chat, %q before, %q: text %q calls %v\n%s", before, parts, text, calls, raw)
+ }
+ }
+ }
+
+ whole := `{"id":"c1","object":"chat.completion","created":1791370000,"model":"m","choices":[{"index":0,"message":{"role":"assistant","content":"Running it.` + strings.ReplaceAll(reportedClose, "\n", `\n`) + `","tool_calls":[{"id":"call_up","type":"function","function":{"name":"exec_command","arguments":"{\"cmd\":\"ls -la\"}"}}]},"finish_reason":"tool_calls"}]}`
+ text, wcalls, finish, raw := wholeChat(t, `{"model":"a/m","messages":[{"role":"user","content":"go"}],`+execChatTool+`}`, "application/json", whole)
+ if strOr(text) != "Running it." || len(wcalls) != 1 || finish != "tool_calls" || strings.Contains(raw, "DSML") {
+ t.Fatalf("chat whole: text %q calls %+v finish %q\n%s", strOr(text), wcalls, finish, raw)
+ }
+
+ for _, stream := range []string{"true", "false"} {
+ items, raw := responsesItems(t, `{"model":"a/m","stream":`+stream+`,"input":"go",`+execResponsesTool+`}`,
+ nativeExec, chunkOf(reportedClose[:6]), chunkOf(reportedClose[6:]), stopChunk)
+ if len(items) != 1 || items[0]["type"] != "function_call" || items[0]["arguments"] != `{"cmd":"ls -la"}` || strings.Contains(raw, "DSML") {
+ t.Fatalf("responses stream=%s: %v\n%s", stream, items, raw)
+ }
+ }
+
+ fresh(t)
+ a := &scripted{}
+ scriptedOn(t, "a", provider.Chat, a)
+ for _, stream := range []string{"true", "false"} {
+ a.replies = []reply{{200, "text/event-stream", sse(nativeExec, chunkOf(reportedClose), stopChunk, `data: [DONE]`)}}
+ rec := httptest.NewRecorder()
+ New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(
+ `{"model":"a/m","max_tokens":100,"stream":`+stream+`,"messages":[{"role":"user","content":"go"}],"tools":[{"name":"exec_command","input_schema":{"type":"object"}}]}`)))
+ body := rec.Body.String()
+ if rec.Code != 200 || strings.Contains(body, "DSML") || strings.Contains(body, `"type":"text"`) ||
+ !strings.Contains(body, `"tool_use"`) || !strings.Contains(body, `"stop_reason":"tool_use"`) {
+ t.Fatalf("anthropic stream=%s: %d %s", stream, rec.Code, body)
+ }
+ }
+}
+
+// The whole block, closed as the reporter's was, is the call on every
+// route, streamed at every cut and whole, opened with tool_calls, with
+// " calls" or not at all.
+func TestDSMLBlockClosedWithCalls(t *testing.T) {
+ chat := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"go"}],` + execChatTool + `}`
+ for _, block := range []string{
+ reportedBlock,
+ strings.Replace(reportedBlock, "tool_calls>", " calls>", 1),
+ reportedBlock[strings.Index(reportedBlock, "\n")+1:],
+ } {
+ s := "Let me look.\n\n" + block
+ for _, parts := range cuts(s) {
+ text, calls, finish, raw := relayedChat(t, chat, append(parts, stopChunk)...)
+ if text != "Let me look.\n\n" || calls["exec_command"] != `{"cmd":"ls -la"}` || finish != "tool_calls" || strings.Contains(raw, "DSML") {
+ t.Fatalf("chat %q: text %q calls %v finish %q\n%s", parts, text, calls, finish, raw)
+ }
+ }
+ text, wcalls, finish, raw := wholeChat(t, `{"model":"a/m","messages":[{"role":"user","content":"go"}],`+execChatTool+`}`, "application/json", wholeChatUp(s))
+ if strOr(text) != "Let me look.\n\n" || len(wcalls) != 1 || finish != "tool_calls" || strings.Contains(raw, "DSML") {
+ t.Fatalf("chat whole: text %q calls %+v finish %q\n%s", strOr(text), wcalls, finish, raw)
+ }
+ for _, stream := range []string{"true", "false"} {
+ for i := 1; i < len(s); i += 5 {
+ if !utf8.ValidString(s[:i]) {
+ continue
+ }
+ items, raw := responsesItems(t, `{"model":"a/m","stream":`+stream+`,"input":"go",`+execResponsesTool+`}`, chunkOf(s[:i]), chunkOf(s[i:]), stopChunk)
+ var call map[string]any
+ for _, it := range items {
+ if it["type"] == "function_call" {
+ call = it
+ }
+ }
+ if call == nil || call["arguments"] != `{"cmd":"ls -la"}` || strings.Contains(raw, "DSML") {
+ t.Fatalf("responses stream=%s cut %d: %v\n%s", stream, i, items, raw)
+ }
+ }
+ }
+ }
+}
+
+// A DSML close with nothing written before it and no call is dropped as
+// other tags and nothing else are; text after it keeps it, as written.
+func TestDSMLCloseAlone(t *testing.T) {
+ chat := `{"model":"a/m","stream":true,"messages":[{"role":"user","content":"go"}],` + execChatTool + `}`
+ for _, parts := range cuts("Done." + reportedClose) {
+ text, calls, finish, raw := relayedChat(t, chat, append(parts, stopChunk)...)
+ if text != "Done.\n" || len(calls) != 0 || finish != "stop" || strings.Contains(raw, "DSML") {
+ t.Fatalf("%q: text %q calls %v finish %q\n%s", parts, text, calls, finish, raw)
+ }
+ }
+ text, _, finish, raw := wholeChat(t, `{"model":"a/m","messages":[{"role":"user","content":"go"}],`+execChatTool+`}`, "application/json", wholeChatUp("Done."+reportedClose))
+ if strOr(text) != "Done.\n" || finish != "stop" || strings.Contains(raw, "DSML") {
+ t.Fatalf("whole: text %q finish %q\n%s", strOr(text), finish, raw)
+ }
+ for _, stream := range []string{"true", "false"} {
+ items, raw := responsesItems(t, `{"model":"a/m","stream":`+stream+`,"input":"go",`+execResponsesTool+`}`, chunkOf("Done."+reportedClose), stopChunk)
+ if len(items) != 1 || items[0]["type"] != "message" || strings.Contains(raw, "DSML") {
+ t.Fatalf("responses stream=%s: %v\n%s", stream, items, raw)
+ }
+ }
+ // prose about a close: the text is kept whole
+ prose := "The close is </|DSML|invoke> and it ends a call."
+ text2, calls, finish, _ := relayedChat(t, chat, chunkOf(prose), stopChunk)
+ if text2 != prose || len(calls) != 0 || finish != "stop" {
+ t.Fatalf("prose: text %q calls %v finish %q", text2, calls, finish)
+ }
+ // and after a call the upstream made, too
+ text2, _, _, _ = relayedChat(t, chat, nativeExec, chunkOf(prose), stopChunk)
+ if text2 != prose {
+ t.Fatalf("prose after a call: text %q", text2)
+ }
+}