Files
magpie/internal/gateway/claude_text_calls.go
T
yetone 39bade71d1 gateway: a plain <invoke> written into a reply's text is the call it is, on a relayed Messages reply too
packinggoodsllc_07668 on Discord (#general): Claude Code v2.1.316, magpie's
routing group "Hard to strong, easy to cheap", asked to open Google in
magpie's browser. The reply showed
  <invoke name="ToolSearch"><parameter name="query">select:…</parameter></invoke>
  *Result: Tool loaded.*
  <invoke name="mcp__magpie__magpie-browser__browser_navigate">…</invoke>
  *Result: Navigated to https://www.google.com — page title: "Google"*
  已用 magpie 的浏览器打开了谷歌 ...
as text. Nothing ran: the model wrote its calls and made up their results.
Asked, the reporter said the group holds only Claude models (v0.1.2545).

Cause: Claude wrote its own call form as text, and none of magpie's
readers of written calls knew that form on the routes Claude takes.
- The plain <invoke name=…><parameter name=…> shape (Claude's own calls,
  MiniMax's in <minimax:tool_call>, Claude's in <function_calls>) wasn't
  a mark: only <tool_call> and DeepSeek's DSML invoke were.
- A reply relayed as it came on the Messages API (a group member on a
  provider that speaks Messages itself: MiniMax, Kimi, GLM, DeepSeek, Claude
  relays) was never read for written calls at all; the subsystem doc said
  so. Claude Code's request to such a member takes that route.
- A Claude subscription member runs Claude Code itself (the bridge), whose
  textCalls held back only "[tool call …]" lines.
Not the cause: history isn't flattened (tool_use/tool_result translate to
native calls on every API), and this was the conversation's first turn.

Fix:
- textcalls.go: the invoke regexes take DSML's pipes, a namespace, or
  nothing; <invoke name=, <function_calls> and <minimax:tool_call> are
  marks; their leftover tags go. Text after a call read out, other than
  tags, ends the calls and goes with all after it: it is the model going
  on as if the call had run. This changes the old rule for every shape:
  prose after a Hermes call ("Done.") went on as text and now goes
  (TestTextWrittenCallCutEarlyNotCalled updated).
- textcalls_anthropic.go: anthropicTextCalls in passthrough, for a
  streamed Messages reply (holds the text block's start and stop with the
  held text, adds tool_use blocks after it or in its place, stop_reason
  tool_use) and a whole one.
- claude_text_calls.go: the bridge's textCalls also holds a line opening
  <invoke name= or <function_calls>, so the run is asked once to make the
  calls (textCallsAsk no longer names the "[tool call …]" form only).
- Responses and Gemini relays still aren't read: #4743.

Tests (textcalls_invoke_test.go), on the reporter's literal text: parse,
a relayed Chat stream, a Messages passthrough through a routing group
(streamed, with lead text, a lookalike left alone, and whole), and a
Chat upstream translated for a Messages client.
TestClaudeTextInvokeCallsAreMadeForReal: the bridge, bare and wrapped.

Run:
- On origin/main 586b59fe7 with only the new test file: all four
  TestInvokeReported* red. With the parser change but not the passthrough
  wiring: TestInvokeReportedAnthropicPassthrough red.
- mote-run at a6c37f936 (on origin/main 9feb1024c): gofmt -l clean, go vet ./internal/gateway for darwin, linux, windows ok, go test ./internal/gateway -timeout 45m -count=1 ok 716.8s.
- Rebased onto origin/main 5d63b3f99 (5 commits, two in internal/gateway: Codex
  loaded tools, HEIC images; no file of this change): gofmt clean, vet ok,
  and -run TestInvokeReported|TestTextWritten|TestTextCall|TestClaudeText|
  TestWritten|Passthrough ok. The full gateway suite was not rerun there.
Not tried: a real Claude Code against a real group member that writes
invokes (which provider serves the reporter's Claude models is unknown), and Responses/Gemini relays.
2026-11-01 15:08:24 +08:00

89 lines
3.2 KiB
Go

package gateway
import "strings"
// A run started anew, or a turn that goes on with the client's own calls in
// it, is told the conversation as text, each past call a "[tool call …]"
// line (renderCachedParts). The model can take that for how it calls a
// tool and write such lines itself: Claude Code then has no call to make,
// the turn ends, and the client shows the lines as the reply's words
// (Zhenzhen on Discord, Claude Desktop on Fable 5.1).
const textCallOpen = "[tool call "
// textCallOpens are the lines that open a call told in text: the form
// above, and Claude's own call written out (packinggoodsllc_07668 on
// Discord: Claude Code through a routing group of Claude models was shown
// <invoke name="ToolSearch"><parameter name="query">…</parameter></invoke>
// and a "*Result: …*" the model made up, with nothing run), bare or in
// its <function_calls>.
var textCallOpens = []string{textCallOpen, "<invoke name=", "<function_calls>"}
// textCallsAsk is what a run whose reply wrote its calls as text is told,
// once, so it makes them (readOutput).
const textCallsAsk = `Your last message wrote tool calls out as text instead of calling the tools. ` +
`Text like that runs nothing: no tool ran, and the user doesn't see those lines. Make the calls with your tools now.`
// pastCallsNote ends a prompt that tells past calls as text: the lines
// record them, and writing one calls nothing.
const pastCallsNote = "(The \"[tool call …]\" and \"[tool result …]\" lines above record calls made earlier. " +
"Writing such a line calls nothing: call a tool only with the tool itself.)"
// textCalls holds back a reply's words from the first line that opens as a
// call told in text (textCallOpens), so the client isn't shown it. Words up
// to that line go on as they come; a line is held only while it could
// still open so.
type textCalls struct {
mid bool // the words so far end inside a line that can't be one
line string // the start of a line that may open as one
seen bool // a line opened as one: the rest is held
held strings.Builder
}
// push takes the next of the reply's words and says which go to the client
// now.
func (c *textCalls) push(s string) string {
if c.seen {
c.held.WriteString(s)
return ""
}
var out strings.Builder
for i := 0; i < len(s); i++ {
ch := s[i]
if c.mid {
out.WriteByte(ch)
c.mid = ch != '\n'
continue
}
line := c.line + s[i:i+1] // a byte, as a UTF-8 rune may be cut across chunks
open := strings.TrimLeft(line, " \t")
opens, may := false, false
for _, o := range textCallOpens {
opens = opens || strings.HasPrefix(open, o)
may = may || strings.HasPrefix(o, open)
}
switch {
case opens:
c.seen, c.line = true, ""
c.held.WriteString(line)
c.held.WriteString(s[i+1:])
return out.String()
case may:
c.line = line
default:
out.WriteString(line)
c.line, c.mid = "", ch != '\n'
}
}
return out.String()
}
// end is what was held at the reply's end: the words from the call told in
// text on when seen, else a line's start that never became one.
func (c *textCalls) end() (held string, seen bool) {
if !c.seen {
held, c.line = c.line, ""
return held, false
}
return c.held.String(), true
}