gateway: a relayed Chat body's unpaired tool calls are paired before Kimi sees them

Paidaixinbao on X: Qoder through magpie on Kimi Code, a conversation
forked at an earlier turn fails on every prompt while a new one works.
A Chat client's body to a Chat upstream was relayed as-is, and only
translated requests had their tool exchanges mended (pairToolMessages).
A forked or stopped history can hold a call with no tool answer, calls
split over two assistant messages, a user turn between a call and its
answer, or an answer to no call. Kimi turns the whole request away with
400 "an assistant message with 'tool_calls' must be followed by tool
messages responding to each 'tool_call_id'..." or "tool_call_id  is not
found". toolPairsOut now pairs such a body on the Chat relay (joining
split calls, answering the unanswered, turning an orphan answer into
user text); a paired body goes out byte for byte.

Run:
- Real Kimi Code API (kimi.com/coding/v1, 2026-10-26; the account's
  plan has lapsed, so a body past validation answers 403 and a refused
  one 400): unanswered call, split calls, user between call and answer,
  one of two answered and an orphan answer are each 400; every one of
  them after toolPairsOut is 403, past validation. A tool turn without
  reasoning_content, null content, repeated ids, out-of-order and
  double answers, prefill and a reasoning field all pass as-is, so
  they are not the cause and are left alone.
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and
  GOOS=windows.
- go test -timeout 45m ./internal/gateway: ok, at this commit.
- Two fixtures held an unpaired body only by accident and are paired
  now: image_input's chat-tool case (a lone tool message) and the
  large-chat upstream golden (its last call left unanswered); the new
  large-chat hash is the same on the old code.

Broken by hand: without the toolPairsOut call all six subtests of
TestChatRelayPairsToolsForKimi go red against a fake Kimi with Kimi's
own wording; without joinSplitChatCalls the split case goes red;
reading only string content in toolAsUser turns the parts case red.

Not tried: the real Qoder app and the reporter's own forked history,
so which shape their fork holds is not proven; Qoder's "unsupported
request" text is not magpie's wording. A real Kimi model answer (the
plan has lapsed, only validation was seen). A non-Kimi Chat upstream,
though the rule is OpenAI's too and runs for every Chat relay.
This commit is contained in:
yetone
2026-10-26 22:40:48 +08:00
parent de54b4ff99
commit 43001be863
8 changed files with 342 additions and 6 deletions
+1
View File
@@ -175,6 +175,7 @@ recorded as a prediction failure (`noPrediction`).
- A turn the agent stopped with its tool call still out is replayed by the client with the call and no result, and Google's APIs turn a history whose call turn goes unanswered away with 400, every later turn of the conversation with it. The Gemini build (`buildCodeAssistSent`, which serves a Google sign-in, Vertex AI and a custom provider's Gemini API alike) answers such a call with the synthetic result the chat build pairs an interrupted turn's call with (`pairToolMessages`, #1275), right after the calls (`pairFunctionTurns`, #1560): beside the answers the client sent there, or in a user turn of its own before what the user said, since a turn mixing a `functionResponse` with text is turned away too (#1533); a Claude model on Antigravity gets it in the one tool_result turn. A call with no id counts as answered by a response of its name. A second answer to one call is left out, with the images it carried, before the turns are built (`dropSecondAnswers`, #2262): Google refuses a call turn whose `functionResponse` count differs from its `functionCall` count, and the Chat and Messages builds already keep the first answer. The Responses build (`buildResponses`) leaves it out the same way. A call made again under the same id takes an answer of its own. A Gemini client that keeps no ids answers two calls of one function by name in their order, and each answer goes to its own call (`parseGemini`), not both to the latest. `TestGeminiDropsASecondAnswer`, `TestSecondAnswerDroppedWithItsImages`, `TestGeminiClientUnnamedCallsAnsweredInOrder`.
- A request built for a Messages upstream (`buildAnthropic`: a Chat, Responses or Gemini client translated) has its tool exchanges mended after its messages are built (`pairToolBlocks`, #1558, ggbdpq), since Anthropic answers 400 for a `tool_use` the next message doesn't answer and for a `tool_result` naming no call of the message before it. A call left unanswered gets the synthetic interrupted result, `is_error` set, first in the user message after it or in one of its own; a result that answers no call goes as a text block with what it said (its images as they came); a second answer to one call is dropped. A well-formed exchange goes as it was built. An Anthropic client relayed to a Messages upstream isn't mended. [`anthropic_tool_pairing_test.go`](../../internal/gateway/anthropic_tool_pairing_test.go).
- A Responses request bound for anything but a ChatGPT sign-in has its tool exchanges mended in `attempt`, before it is relayed or translated, plugins included ([`responses.go`](../../internal/gateway/responses.go)). An item inside a run of calls and results (after one of the run's results and before the last, or before any result when the model didn't write it) moves to the end of that run (`joinToolRuns`, #1752, Edward4226): Codex writes its `<image_resize_notice>` right after each result whose image it resized, so a batch of two calls had it between their results, and a strict upstream answered 400 (OpenCode Go/DeepSeek "No tool output found for tool call …", Volcengine's "must be followed by tool messages", #1822); a Chat translation answered the second call with a synthetic result. An item carrying a `call_id` never moves, the model's own reasoning between its calls stays, and its text between them (an assistant message, with the reasoning right before it) goes ahead of the run's first call, so the step's calls stand together: OpenCode Go opens a new assistant message at such text and answered the call before it "No tool output found for tool call …" (#2100). A call opens a run only when a result after it answers it. A result with no call id becomes a user message after its run (`orphanedToolOutputs`), and a call nothing answers gets a synthetic result at the end of its run (`pairToolItems`, #1341). `TestRelayedCodexImageBatchKeepsItsResultsTogether`, `TestJoinToolRunsOnlyMovesWhatBreaksARun`, `TestRelayedTextBetweenABatchsCallsIsAnswered`.
- A Chat client's own body relayed to a Chat upstream (`passthrough`) has its tool exchanges mended when they don't pair (`toolPairsOut` in [`chattidy.go`](../../internal/gateway/chattidy.go)), by the rule a translated one is built with (`pairToolMessages` in [`chat.go`](../../internal/gateway/chat.go)): Kimi's Chat API, like OpenAI's, turns the whole request away for an assistant's call the tool messages right after it don't answer ("an assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'…"), a user message between a call and its answer included, and for a tool message with no call before it ("tool_call_id is not found"), both checked against api.kimi.com/coding on 2026-10-26. Qoder's chat forked at an earlier turn, its model switched to Kimi Code, was refused on every prompt (Paidaixinbao on X). A turn's calls split over consecutive assistant messages join into one first (`joinSplitChatCalls`); a call left unanswered gets the synthetic interrupted result; an answer to no call becomes the user's text, a parts content by its text parts. A body whose exchange pairs goes byte for byte, answers out of the calls' order included, which Kimi takes (`TestChatRelayPairsToolsForKimi`, `TestChatRelayPairedToolsUntouched`).
- A Chat request has every tool call that came with no id (`""`, null or none) given one in `attempt`, before it is relayed or translated, plugins included (`chatCallIDs` in [`chat_call_ids.go`](../../internal/gateway/chat_call_ids.go), #2611, RunMintOn). An upstream that sometimes streams a call without an id (OpenCode Zen's free DeepSeek) has the agent keep it as `""`, which that upstream takes back, while WorkBuddy AI turns a streamed request carrying one away (400, code 11133) and Anthropic takes no empty `tool_use` id. The id is made from the call's place in the request, its name and its arguments, never at random, so the next turn's history goes byte for byte and the upstream's prompt cache holds; the tool messages after the call that answer with no `tool_call_id` take the made ids in the calls' order. A body whose calls all have ids goes as it came. On the reply side, a relayed Chat stream's call whose first fragment has no id is given one, and a later fragment's empty id is left out (`chatTidy`); a whole reply's call is given one too (`chatWhole`). `TestEmptyCallIDRelayedWithOne`, `TestEmptyCallIDReachesPluginWithOne`, `TestEmptyCallIDTranslatedWithOne`, `TestStreamedCallGivenAnID` in [`chat_call_ids_test.go`](../../internal/gateway/chat_call_ids_test.go).
- A relayed Chat stream's tool call fragment with no `index` (none, or null) is given its call's (`streamCallIndexes` in [`chattidy.go`](../../internal/gateway/chattidy.go), #3386). Gemini's OpenAI-compatible API sends its calls with none, where the OpenAI contract has one on every fragment: the AI SDK's OpenAI provider (Cursor's Private Inference) turned the chunk away ("expected number, received undefined" at `tool_calls[0].index`), and OpenAI's node SDK's stream helper kept no call at all. A fragment with an id other than the open call's, or after the first in its delta, is the next call; any other goes on with the open one. So two calls in chunks of their own are 0 and 1, never both 0, which that helper merges into one call. A fragment with an index keeps it. `TestChatTidyGivesCallsAnIndex` in [`chattidy_test.go`](../../internal/gateway/chattidy_test.go), `TestGeminiCompatRelayedCallsHaveAnIndex` in [`gemini_compat_cut_test.go`](../../internal/gateway/gemini_compat_cut_test.go).
- A relayed Chat reply whose choice made calls and ended `stop` ends `tool_calls`, streamed (`chatTidy`) and whole (`chatWhole`), as the translations' collector already ends it (`collector.finish`): told it stopped, a client that reads `finish_reason` ends its turn with the calls not run (喜欢一个人 on Discord, a relay in front of deepseek-v4.1-flash). Any other reason stays as it came. `TestChatCallsEndToolCalls` in [`chattidy_finish_test.go`](../../internal/gateway/chattidy_finish_test.go).
+23 -3
View File
@@ -592,8 +592,8 @@ func buildChat(r *Request, model, host string, rejectTemp bool) []byte {
// 400 "tool_call_id is not found" or "an assistant message with
// 'tool_calls' must be followed by tool messages…", and OpenAI-style
// upstreams refuse the same shapes. It runs on every request built for a
// Chat upstream (Zed's xAI path included); Chat→Chat traffic relays
// as-is and never reaches this path.
// Chat upstream (Zed's xAI path included), and on a Chat client's own
// body relayed whose exchange doesn't pair (toolPairsOut).
//
// - the tool messages answering an assistant's tool_calls go in the
// calls' order (an agent returns parallel results out of order);
@@ -645,7 +645,7 @@ func pairToolMessages(msgs []map[string]any) []map[string]any {
// the text in, so two user messages never stand in a row (some
// models' chat templates turn them away).
toolAsUser := func(m map[string]any) {
text, _ := m["content"].(string)
text := contentText(m["content"])
if strings.TrimSpace(text) == "" {
text = "Tool result received."
}
@@ -703,6 +703,26 @@ func pairToolMessages(msgs []map[string]any) []map[string]any {
return out
}
// contentText is a Chat message content's text: the string, or its text
// parts joined, as a relayed client's tool message may hold them.
func contentText(c any) string {
switch c := c.(type) {
case string:
return c
case []any:
var parts []string
for _, p := range c {
if pm, _ := p.(map[string]any); pm != nil {
if t, _ := pm["text"].(string); t != "" {
parts = append(parts, t)
}
}
}
return strings.Join(parts, "\n")
}
return ""
}
// toolMsgID is a tool message's tool_call_id.
func toolMsgID(m map[string]any) string {
id, _ := m["tool_call_id"].(string)
@@ -0,0 +1,168 @@
package gateway
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// kimiRefusePairs answers as api.kimi.com/coding/v1 did on 2026-10-26 a
// Chat request whose tool exchange doesn't pair: a tool message with no
// call before it, and an assistant message's call that the tool messages
// right after it don't answer. Kimi names a call by its function and
// place ("read_file:0") whatever id the client gave it; here it is the
// client's.
func kimiRefusePairs(body []byte) (int, string) {
var q struct {
Messages []map[string]any `json:"messages"`
}
json.Unmarshal(body, &q)
var pending []string
answered := map[string]bool{}
unanswered := func() string {
var left []string
for _, id := range pending {
if !answered[id] {
left = append(left, id)
}
}
pending, answered = nil, map[string]bool{}
return strings.Join(left, ", ")
}
refuse := func(left string) (int, string) {
return 400, fmt.Sprintf(`{"error":{"message":"an assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: %s","type":"invalid_request_error"}}`, left)
}
for _, m := range q.Messages {
if m["role"] == "tool" {
id, _ := m["tool_call_id"].(string)
if !strings.Contains(" "+strings.Join(pending, " ")+" ", " "+id+" ") {
return 400, `{"error":{"message":"tool_call_id is not found","type":"invalid_request_error"}}`
}
answered[id] = true
continue
}
if left := unanswered(); left != "" {
return refuse(left)
}
calls, _ := m["tool_calls"].([]any)
for _, c := range calls {
id, _ := c.(map[string]any)["id"].(string)
pending = append(pending, id)
}
}
if left := unanswered(); left != "" {
return refuse(left)
}
return 0, ""
}
func readCall(id string) string {
return `{"id":"` + id + `","type":"function","function":{"name":"read_file","arguments":"{\"path\":\"a.txt\"}"}}`
}
// Paidaixinbao (X): Qoder's chat, its model switched to Kimi Code, forked
// at an earlier turn, was refused on every prompt while a new chat worked.
// A Chat agent's history can hold a call no tool message answers (a fork
// or a stopped reply cut the turn at its call), an answer whose call is
// gone, a user message between a call and its answer, or a turn's calls
// split over two assistant messages, and Kimi turns the whole request
// away for each, as it does on its real API. Relayed to Kimi, each one
// goes paired and is answered.
func TestChatRelayPairsToolsForKimi(t *testing.T) {
f := &fake{t: t, reply: chatWholeOK, ctype: "application/json", refuse: kimiRefusePairs}
up := setup(t, provider.Chat, f)
if err := provider.Save(provider.Provider{ID: "kimi-code-cn", Name: "Kimi Code (China)", Key: "k", Models: []string{"k3"},
Preset: "kimi-code-cn", Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
srv := New()
user := func(s string) string { return `{"role":"user","content":"` + s + `"}` }
calls := func(ids ...string) string {
var cs []string
for _, id := range ids {
cs = append(cs, readCall(id))
}
return `{"role":"assistant","content":"","tool_calls":[` + strings.Join(cs, ",") + `]}`
}
tool := func(id, out string) string {
return `{"role":"tool","tool_call_id":"` + id + `","content":"` + out + `"}`
}
for _, c := range []struct{ name, turns, shape, has string }{
{"call unanswered", user("read a.txt") + "," + calls("a") + "," + user("go on"),
"user assistant(a) tool(a) user", "interrupted"},
{"one of two calls answered", user("read a.txt") + "," + calls("a", "b") + "," + tool("a", "hello") + "," + user("go on"),
"user assistant(a,b) tool(a) tool(b) user", "hello"},
{"answer with no call", user("read a.txt") + "," + tool("a", "hello") + "," + user("go on"),
"user user", "hello"},
{"user between call and answer", user("read a.txt") + "," + calls("a") + "," + user("wait") + "," + tool("a", "hello") + "," + user("go on"),
"user assistant(a) tool(a) user user", "hello"},
{"calls split over two messages", user("read a.txt") + "," + calls("a") + "," + calls("b") + "," + tool("a", "1") + "," + tool("b", "2") + "," + user("go on"),
"user assistant(a,b) tool(a) tool(b) user", "go on"},
{"answer in parts with no call", user("read a.txt") + `,{"role":"tool","tool_call_id":"a","content":[{"type":"text","text":"hello in parts"}]},` + user("go on"),
"user user", "hello in parts"},
} {
t.Run(c.name, func(t *testing.T) {
f.calls = 0
code, body := postTo(t, srv, "/v1/chat/completions", `{"model":"kimi-code-cn/k3","stream":false,"messages":[`+c.turns+`]}`)
if code != 200 || f.calls != 1 {
t.Fatalf("%d after %d calls, sent %s: %s", code, f.calls, f.got, body)
}
var msgs []map[string]any
for _, raw := range sentMessages(t, f.got) {
var m map[string]any
json.Unmarshal(raw, &m)
msgs = append(msgs, m)
}
if shape := relayTrace(msgs); shape != c.shape {
t.Errorf("sent %s, want %s: %s", shape, c.shape, f.got)
}
if !strings.Contains(string(f.got), c.has) {
t.Errorf("sent without %q: %s", c.has, f.got)
}
if c.name == "user between call and answer" && !bytes.Contains(f.got, []byte(`"wait\n\nhello"`)) {
t.Errorf("the late answer didn't join the user's message: %s", f.got)
}
})
}
}
// relayTrace is the roles of a sent Chat request's messages, with their
// tool call ids, as chatTrace has them.
func relayTrace(msgs []map[string]any) string {
var b strings.Builder
for _, m := range msgs {
role, _ := m["role"].(string)
b.WriteString(" " + role)
if calls, ok := m["tool_calls"].([]any); ok {
var ids []string
for _, c := range calls {
id, _ := c.(map[string]any)["id"].(string)
ids = append(ids, id)
}
b.WriteString("(" + strings.Join(ids, ",") + ")")
}
if id, ok := m["tool_call_id"].(string); ok {
b.WriteString("(" + id + ")")
}
}
return strings.TrimSpace(b.String())
}
// A relayed body whose exchange pairs goes byte for byte, answers out of
// the calls' order included (Kimi takes them), and so does one with no
// tools at all.
func TestChatRelayPairedToolsUntouched(t *testing.T) {
for _, body := range []string{
`{"model":"m","temperature":0.70,"messages":[{"role":"user","content":"a <b> & c"},{"role":"assistant","content":null,"tool_calls":[` + readCall("a") + `,` + readCall("b") + `]},{"role":"tool","tool_call_id":"b","content":"2"},{"role":"tool","tool_call_id":"a","content":"1"},{"role":"assistant","content":"done"},{"role":"user","content":"go"}]}`,
`{"model":"m","messages":[{"role":"user","content":"hi"},{"role":"assistant","content":"hello","n":1.50},{"role":"user","content":"go"}]}`,
`{"model":"m","messages":[{"role":"user","content":"hi"}],"tools":[{"type":"function","function":{"name":"f","parameters":{}}}]}`,
} {
if got := toolPairsOut([]byte(body)); string(got) != body {
t.Errorf("a paired body was rewritten:\n%s\n%s", body, got)
}
}
}
+141
View File
@@ -877,3 +877,144 @@ func mistralThinking(body []byte) []byte {
return true
})
}
// toolPairsOut is pairToolMessages for a Chat request relayed as the
// client sent it. Kimi's Chat API, like OpenAI's, turns the whole request
// away when its tool exchange doesn't pair: a call with no tool message
// answering it ("an assistant message with 'tool_calls' must be followed
// by tool messages responding to each 'tool_call_id'. The following
// tool_call_ids did not have response messages: …"), and a tool message
// with no call before it ("tool_call_id is not found"), both checked
// against api.kimi.com/coding on 2026-10-26. A Chat agent's history holds
// such a turn after a fork at an earlier turn, a reply stopped with its
// call out, or a call split over two assistant messages, and every later
// request of the conversation was refused (Paidaixinbao on X, Qoder on
// Kimi Code). The translations had it mended (#554); the relay sent it
// as it came. A turn's calls split over consecutive assistant messages
// join into one first, as joinSplitCalls does for the translations. A
// body whose exchange pairs comes back byte for byte.
func toolPairsOut(body []byte) []byte {
if !bytes.Contains(body, []byte(`"tool`)) {
return body
}
var q map[string]json.RawMessage
if decodeJSON(body, &q) != nil {
return body
}
dec := json.NewDecoder(bytes.NewReader(q["messages"]))
dec.UseNumber()
var msgs []map[string]any
if dec.Decode(&msgs) != nil {
return body
}
for _, m := range msgs {
if m == nil {
return body
}
raw, ok := m["tool_calls"]
if !ok || raw == nil {
continue
}
list, ok := raw.([]any)
if !ok {
return body
}
calls := make([]map[string]any, 0, len(list))
for _, c := range list {
cm, ok := c.(map[string]any)
if !ok {
return body
}
calls = append(calls, cm)
}
m["tool_calls"] = calls
}
if toolsPaired(msgs) {
return body
}
q["messages"], _ = marshalPlain(pairToolMessages(joinSplitChatCalls(msgs)))
nb, err := marshalPlain(q)
if err != nil {
return body
}
return nb
}
// chatCalls is a Chat message's tool calls, as toolPairsOut typed them.
func chatCalls(m map[string]any) []map[string]any {
calls, _ := m["tool_calls"].([]map[string]any)
return calls
}
// toolsPaired reports whether every assistant message's tool calls are
// answered, each once, by the tool messages right after it, and every
// tool message answers such a call: what Kimi and OpenAI hold a Chat
// request to.
func toolsPaired(msgs []map[string]any) bool {
var pending map[string]bool
closed := func() bool {
for _, open := range pending {
if open {
return false
}
}
pending = nil
return true
}
for _, m := range msgs {
if m["role"] == "tool" {
id := toolMsgID(m)
if !pending[id] {
return false
}
pending[id] = false
continue
}
if !closed() {
return false
}
if m["role"] == "assistant" {
if calls := chatCalls(m); len(calls) > 0 {
pending = map[string]bool{}
for _, c := range calls {
id, _ := c["id"].(string)
if _, twice := pending[id]; twice {
return false
}
pending[id] = true
}
}
}
}
return closed()
}
// joinSplitChatCalls joins a turn's calls split over consecutive
// assistant messages into the first, as joinSplitCalls does for the
// translations (#1275): the second message's calls follow the first's,
// its text and thinking join the first's where both are strings, and
// stand in for the first's when that has none.
func joinSplitChatCalls(msgs []map[string]any) []map[string]any {
out := make([]map[string]any, 0, len(msgs))
for _, m := range msgs {
n := len(out)
if n == 0 || m["role"] != "assistant" || out[n-1]["role"] != "assistant" || len(chatCalls(out[n-1])) == 0 || len(chatCalls(m)) == 0 {
out = append(out, m)
continue
}
prev := out[n-1]
prev["tool_calls"] = append(chatCalls(prev), chatCalls(m)...)
for _, k := range []string{"content", "reasoning_content"} {
was, isText := prev[k].(string)
add, _ := m[k].(string)
if strings.TrimSpace(add) == "" || prev[k] != nil && !isText {
continue
}
if strings.TrimSpace(was) != "" {
add = was + "\n\n" + add
}
prev[k] = add
}
}
return out
}
+4
View File
@@ -4434,6 +4434,10 @@ func (s *Server) passthrough(w http.ResponseWriter, r *http.Request, p provider.
// a turn the client cut off before it said anything, which
// DeepSeek and Kimi turn every later request away for (#2599)
body = silentAssistantsOut(body)
// a call no tool message answers, or an answer to no call, which
// Kimi and OpenAI turn the whole request away for: what a fork
// at an earlier turn or a stopped reply leaves (Paidaixinbao on X)
body = toolPairsOut(body)
if mistral := p.Preset == "mistral" || p.Host() == "api.mistral.ai"; !mistral && (chatReplaysReasoning(buildHost(p), model) || chatReplaysReasoning(buildHost(p), upstream)) {
// a tool call the client kept no thinking for, which DeepSeek
// turns the whole request away for (#1462)
+1 -1
View File
@@ -162,7 +162,7 @@ func TestTextOnlyModelOmitsToolImages(t *testing.T) {
{"responses-current", "/v1/responses", "responses", `{"model":"probe/text","input":[{"type":"function_call_output","call_id":"call_1","output":[{"type":"input_text","text":"screenshot"},{"type":"input_image","image_url":"data:image/png;base64,aGVsbG8="}]},{"role":"user","content":[{"type":"input_text","text":"answer"}]}]}`},
{"responses-last", "/v1/responses", "responses", `{"model":"probe/text","input":[{"role":"user","content":[{"type":"input_text","text":"read"}]},{"type":"function_call_output","call_id":"call_1","output":[{"type":"input_image","image_url":"data:image/png;base64,aGVsbG8="}]}]}`},
{"responses-to-chat", "/v1/responses", "chat", `{"model":"probe/text","input":[{"type":"function_call_output","call_id":"call_1","output":[{"type":"input_text","text":"screenshot"},{"type":"input_image","image_url":"data:image/png;base64,aGVsbG8="}]},{"role":"user","content":[{"type":"input_text","text":"answer"}]}]}`},
{"chat-tool", "/v1/chat/completions", "chat", `{"model":"probe/text","messages":[{"role":"tool","tool_call_id":"call_1","content":[{"type":"text","text":"screenshot"},{"type":"image_url","image_url":{"url":"data:image/png;base64,aGVsbG8="}}]}]}`},
{"chat-tool", "/v1/chat/completions", "chat", `{"model":"probe/text","messages":[{"role":"user","content":"look"},{"role":"assistant","content":null,"tool_calls":[{"id":"call_1","type":"function","function":{"name":"shot","arguments":"{}"}}]},{"role":"tool","tool_call_id":"call_1","content":[{"type":"text","text":"screenshot"},{"type":"image_url","image_url":{"url":"data:image/png;base64,aGVsbG8="}}]}]}`},
} {
t.Run(tc.name, func(t *testing.T) {
fresh(t)
+3 -1
View File
@@ -63,7 +63,9 @@ func largeConversation(proto provider.Protocol, model string, words int) []byte
body["system"] = []any{schema{"type": "text", "text": para + para, "cache_control": schema{"type": "ephemeral"}}}
body["messages"] = msgs
case provider.Chat:
msgs[len(msgs)-1] = schema{"role": "user", "content": "go on"}
// the user's message follows the last call's answer: a call left
// unanswered is mended before it is relayed (toolPairsOut)
msgs = append(msgs, schema{"role": "user", "content": "go on"})
body["messages"] = append([]any{schema{"role": "system", "content": para + para}}, msgs...)
default:
delete(body, "max_tokens")
@@ -1 +1 @@
f9400fa84e77c783c7d67c4b4380275af3a6859be7d69bc562663e5f5e316b03
b168f2f26ed07ef23b9c3fdbb3416c092afaf0f9ec853125d15286074790cf2d