63c91cae5moved the request path's key checks with a literal needle to bodyHas. A profile of BenchmarkLargeRequest at that head still had bytes.Contains at ~14% of the gateway's CPU: the checks whose needle is kept as a []byte (openaiOnly in forVendor alone took 0.49s of 6.8s, cacheControlField, thinkingType, toolUseType, the Gemini signature fields, contentParts), and those built as []byte(`"`+f+`"`). They go through bodyHasBytes/bodyHas now, and the stream's bytes.Index loops (turndone, refusal_handover, routing) through bodyIndexBytes; a line under 1 KB takes strings.Index as before. grokToolPatterns asked gjson for the model before it looked for a "pattern" at all, so every request to every provider walked the body to its model (0.43s of the same profile; Responses bodies carry the model after the conversation). The pattern check now goes first; the answer is the same, since both had to hold. anthropic_shapes copied a tool_result's raw content into a []byte to look in it; it uses strings.Contains on the string. BenchmarkLargeRequest on mica, 200x, before -> after: anthropic 23.0 -> 21.9 ms anthropic-group 24.2 -> 22.9 ms chat 29.0 -> 28.4 ms responses 26.8 -> 22.8 ms Run (mica, at this head): gofmt -l clean, go vet ./internal/gateway for darwin, linux and windows, the benchmark above, go test -count=1 ./internal/gateway ok (860s). bodyIndex is held to bytes.Index by TestBodyIndexIsIndex (63c91cae5). Real account: the built app (mote, sandbox HOME) sent a 6KB request to DeepSeek on /v1/responses and /v1/messages, both 200 "pong". Not tried: Codex and Grok (the sandbox's Codex sign-in is stale; renewing it there would rotate the live refresh token).
87 lines
2.5 KiB
Go
87 lines
2.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Codex offers its image tool, the namespace image_gen with its function
|
|
// imagegen, to a provider it reads as the OpenAI actor, as magpie's is
|
|
// (#870). A Codex backend (a ChatGPT sign-in, sub2api) takes it, and a
|
|
// call to it is drawn through magpie (drawing_route.go). A vendor's
|
|
// Responses API that knows no namespaces turns the whole request away with
|
|
// a bare 400, so a plain chat broke (#949). Such a vendor is asked again
|
|
// without the tool, and once that works it isn't sent it again.
|
|
|
|
// imageNamespace is the namespace Codex's image tool is offered in.
|
|
const imageNamespace = "image_gen"
|
|
|
|
// imageToolRefused is how unfit remembers a provider refusing it.
|
|
const imageToolRefused = "tool:" + imageNamespace
|
|
|
|
// mayDropImageTool reports whether p is one that may not take Codex's
|
|
// image tool: anything but a ChatGPT sign-in and OpenAI's own API.
|
|
func mayDropImageTool(p provider.Provider) bool {
|
|
return (p.Account == nil || p.Account.Agent != "codex") && !strings.HasSuffix(p.VendorHost(), "openai.com")
|
|
}
|
|
|
|
// withoutImageTool is a Responses body without Codex's image tool among its
|
|
// tools, and whether it had it.
|
|
func withoutImageTool(body []byte) ([]byte, bool) {
|
|
if !bodyHas(body, `"`+imageNamespace+`"`) {
|
|
return body, false
|
|
}
|
|
dec := json.NewDecoder(bytes.NewReader(body))
|
|
dec.UseNumber()
|
|
var m map[string]any
|
|
if dec.Decode(&m) != nil {
|
|
return body, false
|
|
}
|
|
tools, _ := m["tools"].([]any)
|
|
kept := make([]any, 0, len(tools))
|
|
for _, t := range tools {
|
|
if tm, _ := t.(map[string]any); tm != nil && tm["type"] == "namespace" && tm["name"] == imageNamespace {
|
|
continue
|
|
}
|
|
kept = append(kept, t)
|
|
}
|
|
if len(kept) == len(tools) {
|
|
return body, false
|
|
}
|
|
if len(kept) == 0 {
|
|
delete(m, "tools")
|
|
delete(m, "tool_choice")
|
|
} else {
|
|
m["tools"] = kept
|
|
}
|
|
out, err := marshalPlain(m)
|
|
if err != nil {
|
|
return body, false
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
// withoutImageToolReq is a translated request without Codex's image tool,
|
|
// which it offers flat (image_gen__imagegen), and whether it had it.
|
|
func withoutImageToolReq(req *Request) (*Request, bool) {
|
|
var kept []Tool
|
|
for _, t := range req.Tools {
|
|
if ns, ok := req.Namespaced[t.Name]; ok && ns.Namespace == imageNamespace {
|
|
continue
|
|
}
|
|
kept = append(kept, t)
|
|
}
|
|
if len(kept) == len(req.Tools) {
|
|
return req, false
|
|
}
|
|
r := *req
|
|
r.Tools = kept
|
|
if len(kept) == 0 && r.ToolChoice != "" && r.ToolChoice != "none" {
|
|
r.ToolChoice = ""
|
|
}
|
|
return &r, true
|
|
}
|