Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: a Chat relay's text-part contents go as a string (Atria on Discord)
yetone4cc331bab4be
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex ed1a0290e..1daa5a9df 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -241,6 +241,7 @@ recorded as a prediction failure (`noPrediction`).- A Chat request's tool in another API's shape is put in the Chat shape, `{"type":"function","function":{name, description, parameters, strict}}` (`shapeChatTools` in [`chat_tool_shape.go`](../../internal/gateway/chat_tool_shape.go), #2622, jinyue806): MCP's `{"name","description","inputSchema"}` (ZCode), Anthropic's `input_schema`, a bare `{"name","parameters"}` and Responses' flat `{"type":"function","name",…}`. No Chat API takes those (SenseNova: "field Tools[0].Type invalid, should be set" and "function cannot be empty"), and `parseChat` read no name from them, so a translated request offered a tool named "". It runs right after `liftMessageTools`, in `handle` for every Chat body and in `parseChat`, so it holds for every Chat upstream, not SenseNova's alone. A tool with its `function`, a hosted tool (any other `type`) and one with no name are left as they came, and a request with none to shape is relayed byte for byte. `TestChatToolInAnotherShapeIsShaped` sends the report's request with each of its four tools through the Chat relay, Messages and Responses; `TestShapeChatToolsLeavesChatShape`.- Every request to a key+URL provider goes with magpie's User-Agent (`magpie/<Version>`, `userAgentFor` in [`user_agent.go`](../../internal/gateway/user_agent.go)), except a provider set `passUserAgent` (#1851, `magpie provider set <id> user-agent=agent`, the editor's "Send the agent's own User-Agent"): it gets the User-Agent the inbound request carried, unchanged, for a coding plan that serves only the agents it names. One the agent didn't send, and magpie's own (`magpiesOwn`: another magpie's `magpie/…`, `magpie-router/1`, `magpie-vision/1`, `magpie-search/1`, and the image tool's `<client> magpie-image/1`, built from MCP's clientInfo, not a User-Agent), go as magpie; magpie never makes up another client's. `forwardOnce` sets it for chat, Anthropic, Responses, Gemini and count_tokens; embeddings, images and videos (`retrieveFrom`, `sendWith`, `remoteVideoContent`) set it with `stampUA` from the caller the request came with (`caller.ua`, `withCaller`), and before #1851 went with Go's default. Claude Code's and Codex's own headers are copied after, as before, and the user's `header.User-Agent` is put on when the request is signed, so it wins. `provider.PassesUserAgent` refuses it for a signed-in account, a plugin's, and a vendor whose own client magpie goes as (`OwnClient`: OpenCode, Cline, Kilo, another magpie); their signing sets their own. `TestUserAgentPassedOn`, `TestUserAgentAccountUntouched`, `TestUserAgentFor`, `TestPassesUserAgent`, `provider-user-agent.test.cjs`.- A Chat upstream that wants a turn's reasoning back (`chatReplaysReasoning` in [`chat.go`](../../internal/gateway/chat.go): a host or a model, as magpie or the provider names it, with `deepseek` in it, such as OpenCode Go's `deepseek-v4.1-flash`, or Command Code's plugin) gets every assistant tool call with `reasoning_content`, relayed (`withReasoningOnToolTurns` in `passthrough`) or translated (`buildChat`). DeepSeek turns the whole request away ("The `reasoning_content` in the thinking mode must be passed back to the API") when a tool call made since the user's last message has none, which a client sends for a turn it saw no thinking on: another model's, a translated one (#1462). The turn's own thinking goes back as it came; one sent in `reasoning` or `reasoning_details` is copied in, and `reasoning` is then left out (`asReasoningContent`): a self-hosted DeepSeek server reads the two as one field and refused pi's tool turn, which carried its thinking in `reasoning`, as "duplicate field reasoning" (ArvinCharl on X, v0.1.1560), while `reasoning_details`, a list of typed entries that can hold an encrypted one, stays; otherwise it goes with a space (`noReasoning`: DeepSeek's Chat API takes an empty one, but a relay that leaves empty fields out would drop it). An assistant message right after a tool result, a reply with no call of its own, gets one too: DeepSeek refuses it the same way (checked 2026-10-12, #1822). A reply to the user, any other model and Mistral (#494) are sent as before.+- A message content that is a list of plain text parts (`[{"type":"text","text":…}]`, nothing else in a part) is relayed to a Chat upstream as the string it says, the parts joined by a newline (`textPartsAsString` in [`chat_text_parts.go`](../../internal/gateway/chat_text_parts.go), in `passthrough` before `silentAssistantsOut`). OpenAI takes either, but a vendor's own server may take a string alone: the Intern discovery API (a custom provider on OpenAI compatible, Atria-Dawn, Discord) answered DeepSeek Harness, whose pi-ai sends every user message as such a list, 422 with an "Input should be a valid string" per message. A list with an image, a file, a thinking part or a `cache_control` mark stays a list, and every other byte of the body stays as sent; `buildChat` already writes text as a string. `TestChatRelayTextPartsAsString`, `TestTextPartsAsStringOnlyPlainText`.- An assistant turn the user cut off before it said anything (only its thinking, an empty text, nothing; an isolated Codex reasoning item; a Gemini thought alone) stays in the agent's history and is sent on every later turn. DeepSeek and Kimi turn the whole request away over an assistant message with neither content nor tool calls, `reasoning_content` alone included ("Invalid assistant message: content or tool_calls must be set", #2599). Every Chat upstream gets such a turn left out, translated (`buildChat`, before `pairToolMessages`) or relayed (`silentAssistantsOut` in `passthrough`), by one rule (`withoutSilentAssistants` in [`chat.go`](../../internal/gateway/chat.go)): the two user messages it stood between join into one, and after a tool result the user's message follows the tool message. A prefill to go on from (`prefix`, `partial`, a Resume), a legacy `function_call`, a refusal and an audio reply stay, and a relayed body without such a turn goes byte for byte. `TestChatEmptyAssistantLeftOutFromAnthropic`, `…FromResponses`, `…FromGemini`, `TestChatRelayLeavesOutEmptyAssistant`, `TestChatRelaySilentAssistantsOnly`.- Kimi's Chat API (`isKimi`: the Kimi platform's and a Kimi Code membership's hosts or presets) turns `reasoning_details` in a message away in any shape: an entry with a stream's `index` ("the reasoning_details at position 1 entry 0 must not contain streaming index") and any other entry ("… has an invalid type"), checked against api.kimi.com/coding on 2026-10-12. pi keeps another vendor's thinking there (MiMo's, through Command Code, with `index: 0`), so a conversation routed on to Kimi was refused on every turn (#1765). `passthrough` sends it to Kimi as `reasoning_content` (`reasoningDetailsAsContent`, with the message's `reasoning` left out once its text is there), which Kimi takes; a message with `reasoning_content` of its own keeps that. Another upstream that refuses a thinking field "at position N", as Kimi words it (a relay in front of it), is asked once more the same way and marked (`refusedInMessages`, `withoutMessageReasoning`).- Vertex AI takes a function declaration's parameters as its own Schema object, whose `type` is required, and turns the whole request away over one schema without it, though the tool is never called: 400 "`mcp__cua-driver__drag` functionDeclaration `parameters.target` schema didn't specify the schema type field." (kthink on Discord: dsh with cua-driver's MCP tools, from before cua's own fix in its #4888, on Command Code's `google/gemini-3.8-flash`, which Command Code's gateway serves on Vertex; `target` was an anyOf of an untyped oneOf and null, `scope` a const). A body whose `model` names Gemini (`geminiNamed`: the id's last `/` segment starts with `gemini`, whoever serves it) has each untyped schema under its tools' `function.parameters`, `parameters` or `input_schema` given its type in place (`geminiToolTypes` in [`gemini_schema.go`](../../internal/gateway/gemini_schema.go), in `forwardOnce`, so a plugin's request too): from its enum or const values, its keywords or its branches, null left out (an object's or array's keywords, `properties` or `items`, before its values: a string enum beside `items` typed `"string"` sent items on a string, and Antigravity turned the turn away with "properties[display].items: field predicate failed: $type == Type.ARRAY", ferock on Discord; `TestGeminiToolTypesItemsBeforeEnum`), `{}` or a description alone a string, a tool's root an object. A node whose branches have more than one type, an unresolved `$ref` or union, and anything under `not` or `if` are left as they came, so such a schema can still be refused; every other model's tools go byte for byte. Google's own Gemini API and Code Assist are sent `parametersJsonSchema` and Antigravity `plainSchema`, so a Gemini-shaped body isn't walked (`TestGeminiToolTypesCuaDriverDrag`, with cua-driver's drag schema as its server listed it, `TestGeminiToolTypes`). `plainSchema` itself keeps an array's keywords (`items`, `minItems`, `maxItems`) on an array only and an object's (`properties`, `required` …) on an object only, and of a type list takes the type its `items` or `properties` are for, so a body a magpie from before sends is taken too (`TestAntigravityItemsOnlyOnArrays`).diff --git a/internal/gateway/chat_empty_assistant_test.go b/internal/gateway/chat_empty_assistant_test.goindex 6eefc2f69..f3ecbf1fa 100644--- a/internal/gateway/chat_empty_assistant_test.go+++ b/internal/gateway/chat_empty_assistant_test.go@@ -182,7 +182,7 @@ func TestChatRelayLeavesOutEmptyAssistant(t *testing.T) {{"no content", "ds/glm-5.2", `{"role":"user","content":"hi"},{"role":"assistant"},{"role":"user","content":"continue"}`,"user", `"hi\n\ncontinue"`},{"blank parts", "ds/glm-5.2", `{"role":"user","content":"hi"},{"role":"assistant","content":[{"type":"text","text":""}]},{"role":"user","content":[{"type":"text","text":"continue"}]}`,- "user", `[{"text":"hi","type":"text"},{"text":"continue","type":"text"}]`},+ "user", `"hi\n\ncontinue"`},{"after a tool result", "ds/deepseek-v4.1-flash", `{"role":"user","content":"what time is it?"},{"role":"assistant","content":null,"reasoning_content":"Ask.","tool_calls":[` + timeCall + `]},{"role":"tool","tool_call_id":"call_1","content":"12:00"},diff --git a/internal/gateway/chat_text_parts.go b/internal/gateway/chat_text_parts.gonew file mode 100644index 000000000..912ed56ad--- /dev/null+++ b/internal/gateway/chat_text_parts.go@@ -0,0 +1,106 @@+package gateway++import (+ "bytes"+ "strings"++ "github.com/tidwall/gjson"+)++// textPartsAsString turns a Chat request's message content that is a list+// of plain text parts ([{"type":"text","text":"…"}]) into the string the+// parts say, for a request relayed as the client sent it. OpenAI's Chat+// API takes either for the same text, but a vendor's own OpenAI-compatible+// server may take a string alone: Shanghai AI Lab's Intern discovery API+// (a custom provider, Atria-Dawn, tim072844 on Discord) answered DeepSeek+// Harness, whose pi-ai sends every user message as such a list, with a+// validation error per message ("Input should be a valid string", 238 of+// them), and every turn failed. magpie's own Chat requests, built from+// another API, already send text as a string (buildChat).+//+// Only a list of text parts with nothing else in them is joined, with a+// newline between parts, as vLLM's chat template renders such a list: a+// part with an image, a file, a thinking part or a cache_control mark+// (OpenRouter's Claude cache points) keeps the whole list as it is. Every+// other byte of the body stays as sent, and a body with no such list+// comes back as it was.+func textPartsAsString(body []byte) []byte {+ if !bytes.Contains(body, contentParts) && !bytes.Contains(body, contentPartsSpace) {+ return body+ }+ msgs := gjson.GetBytes(body, "messages")+ if !msgs.IsArray() {+ return body+ }+ type edit struct {+ at, n int+ s []byte+ }+ var edits []edit+ msgs.ForEach(func(_, m gjson.Result) bool {+ if !m.IsObject() {+ return true+ }+ c := m.Get("content")+ if !c.IsArray() || c.Index <= 0 || c.Index+len(c.Raw) > len(body) || string(body[c.Index:c.Index+len(c.Raw)]) != c.Raw {+ return true+ }+ text, ok := plainTextParts(c)+ if !ok {+ return true+ }+ s, err := marshalPlain(text)+ if err != nil {+ return true+ }+ edits = append(edits, edit{c.Index, len(c.Raw), s})+ return true+ })+ if len(edits) == 0 {+ return body+ }+ out := make([]byte, 0, len(body))+ last := 0+ for _, e := range edits {+ out = append(out, body[last:e.at]...)+ out = append(out, e.s...)+ last = e.at + e.n+ }+ return append(out, body[last:]...)+}++// plainTextParts is the text of a content list whose every part is+// {"type":"text","text":"…"} and nothing more, its parts joined by a+// newline; ok is false for an empty list or any other part.+func plainTextParts(c gjson.Result) (string, bool) {+ var texts []string+ ok := true+ c.ForEach(func(_, p gjson.Result) bool {+ if !p.IsObject() {+ ok = false+ return false+ }+ isText, hasText := false, false+ p.ForEach(func(k, v gjson.Result) bool {+ switch k.Str {+ case "type":+ isText = v.Type == gjson.String && v.Str == "text"+ case "text":+ hasText = v.Type == gjson.String+ if hasText {+ texts = append(texts, v.Str)+ }+ default:+ isText = false+ return false+ }+ return isText || k.Str == "text"+ })+ ok = isText && hasText+ return ok+ })+ if !ok || len(texts) == 0 {+ return "", false+ }+ return strings.Join(texts, "\n"), true+}diff --git a/internal/gateway/chat_text_parts_test.go b/internal/gateway/chat_text_parts_test.gonew file mode 100644index 000000000..f9ab9af9c--- /dev/null+++ b/internal/gateway/chat_text_parts_test.go@@ -0,0 +1,100 @@+package gateway++import (+ "bytes"+ "encoding/json"+ "fmt"+ "strings"+ "testing"++ "github.com/yetone/magpie/internal/provider"+)++// stringContentOnly answers as an OpenAI-compatible server whose Chat+// schema takes a message's content as a string alone (pydantic, as the+// Intern discovery API's Atria answered DeepSeek Harness on Discord): a+// 422 with one "Input should be a valid string" per message whose content+// is a list, the list itself as the error's "input".+func stringContentOnly(body []byte) (int, string) {+ var q struct {+ Messages []map[string]json.RawMessage `json:"messages"`+ }+ json.Unmarshal(body, &q)+ var errs []string+ for i, m := range q.Messages {+ c := bytes.TrimSpace(m["content"])+ if len(c) > 0 && c[0] == '[' {+ errs = append(errs, fmt.Sprintf(`{"type":"string_type","loc":["body","messages",%d,"content"],"msg":"Input should be a valid string","input":%s}`, i, c))+ }+ }+ if len(errs) == 0 {+ return 0, ""+ }+ return 422, `{"detail":[` + strings.Join(errs, ",") + `]}`+}++// DeepSeek Harness (pi-ai's openai-completions) sends every user message+// as a list of text parts, its system prompt, assistant text and tool+// results as strings. Relayed as it came to a custom provider on OpenAI+// compatible whose server takes a string alone, every request was turned+// away. The lists go as the string they say, the rest as it was sent.+func TestChatRelayTextPartsAsString(t *testing.T) {+ f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: stringContentOnly}+ up := setup(t, provider.Chat, f)+ chatProvider(t, up.URL, provider.Provider{ID: "atria", Models: []string{"Atria-Dawn"}})+ code, body := postTo(t, New(), "/v1/chat/completions", `{"model":"atria/Atria-Dawn","stream":false,+ "tools":[{"type":"function","function":{"name":"read","parameters":{"type":"object","properties":{"path":{"type":"string"}}}}}],+ "messages":[+ {"role":"system","content":"You are dsh. <rules> & more"},+ {"role":"user","content":[{"type":"text","text":"read a.go"}]},+ {"role":"assistant","content":"Reading.","tool_calls":[{"id":"call_1","type":"function","function":{"name":"read","arguments":"{\"path\":\"a.go\"}"}}]},+ {"role":"tool","content":"package a","tool_call_id":"call_1"},+ {"role":"user","content":[{"text":"now <b>","type":"text"},{"type":"text","text":"and c.go"}]}]}`)+ if code != 200 || f.calls != 1 {+ t.Fatalf("%d after %d calls, sent %s: %s", code, f.calls, f.got, body)+ }+ want := []string{+ `{"role":"system","content":"You are dsh. <rules> & more"}`,+ `{"role":"user","content":"read a.go"}`,+ `{"role":"assistant","content":"Reading.","tool_calls":[{"id":"call_1","type":"function","function":{"name":"read","arguments":"{\"path\":\"a.go\"}"}}]}`,+ `{"role":"tool","content":"package a","tool_call_id":"call_1"}`,+ `{"role":"user","content":"now <b>\nand c.go"}`,+ }+ got := sentMessages(t, f.got)+ if len(got) != len(want) {+ t.Fatalf("sent %s", f.got)+ }+ for i, w := range want {+ sameJSON(t, got[i], w)+ }+}++// A list with anything but plain text in it stays a list: an image, a+// cache point (OpenRouter's Claude), a part of another type. A body with+// no list of parts comes back byte for byte, and in one with, every byte+// around the lists joined stays as sent.+func TestTextPartsAsStringOnlyPlainText(t *testing.T) {+ clean := []byte(`{"model":"m","messages":[{"role":"user","content":"a <b> & c"},{"role":"assistant","content":"x","n":1.50}]}`)+ if got := textPartsAsString(clean); !bytes.Equal(got, clean) {+ t.Fatalf("a clean body was rewritten: %s", got)+ }+ for _, kept := range []string{+ `[{"type":"text","text":"see"},{"type":"image_url","image_url":{"url":"data:image/png;base64,AAAA"}}]`,+ `[{"type":"text","text":"long prompt","cache_control":{"type":"ephemeral"}}]`,+ `[{"type":"thinking","thinking":"hm"},{"type":"text","text":"ok"}]`,+ `[{"type":"input_text","text":"hi"}]`,+ `[{"type":"text"}]`,+ `["hi"]`,+ `[]`,+ } {+ body := []byte(`{"model":"m","messages":[{"role":"user","content":` + kept + `}]}`)+ if got := textPartsAsString(body); !bytes.Equal(got, body) {+ t.Errorf("%s was rewritten: %s", kept, got)+ }+ }+ body := []byte(`{"model":"m","temperature":0.70,"messages":[{"role":"user","content": [{"type":"text","text":"a <b>"}]},{"role":"user","content":[{"type":"image_url","image_url":{"url":"x"}}]},{"role":"user","content":[{"type":"text","text":"b"},{"type":"text","text":"c"}],"name":"u"}],"stream":true}`)+ want := `{"model":"m","temperature":0.70,"messages":[{"role":"user","content": "a <b>"},{"role":"user","content":[{"type":"image_url","image_url":{"url":"x"}}]},{"role":"user","content":"b\nc","name":"u"}],"stream":true}`+ if got := textPartsAsString(body); string(got) != want {+ t.Fatalf("got %s\nwant %s", got, want)+ }+}diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex faf3debdc..ad984ceaa 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -4587,6 +4587,9 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.}case provider.Chat:body = developerAsSystem(body)+ // a content of text parts, which a vendor's server that takes a+ // string alone turns the whole request away for (Atria on Discord)+ body = textPartsAsString(body)// a turn the client cut off before it said anything, which// DeepSeek and Kimi turn every later request away for (#2599)body = silentAssistantsOut(body)