Files
magpie/internal/gateway/grok_schema.go
T
yetone 9309e28ced plugins: a plugin's provider in front of a vendor's API is that vendor to the gateway (songlairui on X, #5462, #5464)
A plugin whose provider's `api` is on a vendor's preset host (a Mistral
Vibe plugin's https://api.mistral.ai/v1) got none of what magpie does for
that vendor, since its Host() is the plugin's id. An earlier turn's
thinking went to the plugin's fetch as reasoning_content, and Mistral
turned the whole request away (422 extra_forbidden); the plugin had to
rewrite the body itself.

provider.VendorHost and VendorPreset now resolve a plugin's declared `api`
host to the preset that names it (every Chat, Responses and Anthropic base
of every preset and region; local and remote magpie presets excluded, and
an api on loopback, a private address, .local or .localhost names no
vendor). For every other provider, and a moved subscription, they are
Host() and Preset as before. The gateway's vendor checks read them: 38
p.Host() sites, Mistral's, OpenRouter's, DeepSeek's, Kimi's and
DashScope's preset checks. Host() stays the plugin's id where calls are
kept and shown; requests still go only to the plugin's fetch. Search and
the video plan check are unchanged. No community plugin's api is on a
preset host today (checked at plugins 7b52e3fc), so none changes.

Any plugin gets this with no new field; documented in
docs/subsystems/provider-plugins.md and the plugin docs (en, zh, ja).

Run (mote): go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows;
go test -count=1 -timeout 45m ./internal/provider ./internal/plugin
./internal/gateway: ok (provider 209s, plugin 182s, gateway 1002s). gofmt -l clean.
Broken by hand: pluginVendor resolving nothing (the old behaviour) turns
TestPluginVendor red, and TestPluginInFrontOfMistralIsMistral red on both
halves: the translated Claude Code turn is answered Mistral's 422
extra_forbidden, and the relayed Chat request carries reasoning_content.
Not tried: a real Mistral Vibe key through the reporter's plugin; Windows
and Linux runs of the tests (no OS-specific code).
2026-11-10 10:01:59 +08:00

69 lines
2.2 KiB
Go

package gateway
import (
"bytes"
"strings"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// grokToolPatterns is body with each "pattern" in its tools' schemas
// spelled so a Rust regex reads it as the ECMAScript regex JSON Schema
// says it is. xAI turns away a tool whose pattern Rust's regex syntax
// can't parse, and there a "[" inside a character class opens a nested
// class (and "&&", "--" and "~~" are set operations), where ECMAScript
// reads a plain character. A tool in a Claude Code session on Windows has
// "^[^\s[\]]+$" for a breakpoint's class_name and selector, so a Grok
// model was turned away on every turn: "Invalid request content: Schema
// validation failed: … pattern: "^[^\\s[\\]]+$" is not a "regex"" (#2317,
// grok-4.6 through a relay). Such a character is escaped instead, which is
// the same character to ECMAScript.
//
// It is Grok's whoever serves it (grokModel), the model named in the body
// as it goes upstream, or xAI's own API; every other model's tools go as
// they came.
func grokToolPatterns(p provider.Provider, to provider.Protocol, body []byte) []byte {
if p.VendorHost() != "api.x.ai" && !grokModel(gjson.GetBytes(body, "model").String()) {
return body
}
if !bytes.Contains(body, []byte(`"pattern"`)) {
return body
}
return toolPatterns(body, ecmaClassForRust)
}
// ecmaClassForRust escapes, inside each character class of an ECMAScript
// pattern, a "[" and the second character of "&&", "--" and "~~", which a
// Rust regex would read as a nested class or a set operation. Escapes are
// kept as they are, and nothing outside a class changes.
func ecmaClassForRust(pat string) string {
var b strings.Builder
in := false
for i := 0; i < len(pat); i++ {
c := pat[i]
switch {
case c == '\\' && i+1 < len(pat):
b.WriteByte(c)
b.WriteByte(pat[i+1])
i++
continue
case !in && c == '[':
in = true
case in && c == ']':
in = false
case in && c == '[':
b.WriteString(`\[`)
continue
case in && (c == '&' || c == '-' || c == '~') && i+1 < len(pat) && pat[i+1] == c:
b.WriteByte(c)
b.WriteByte('\\')
b.WriteByte(c)
i++
continue
}
b.WriteByte(c)
}
return b.String()
}