A Codex Responses request carries the whole conversation, and the gateway
walked it several times per request for what it reads at the top level:
- requestSessionMetadata searched the body for "client_metadata", which
Codex sends last, before asking topMember. A long body now asks the
memoized split straight away.
- bodyTier asked gjson for speed and service_tier, walking the body each
time; a long body now asks topMember too (topValue).
- userWords ran its reminder and "## My request" regexes on every text;
each now runs only when its marker is present.
- readFacts (turn count, work folder) ran the full parse. It now parses
in facts mode (readResponses): views over the raw bytes, no parts for
assistant replies, and tool outputs as bare ToolResult parts, which is
all turnIn and workPlace read.
Bench (a fake ChatGPT backend, ~2MB Codex body, client_metadata last),
per request: 31.7ms, 24.2MB, 71.2k allocs on origin/main; 19.2-21.4ms,
17.2MB, 59.6k allocs with these and the provider commit after it.
Real account: a sandbox HOME with only the deepseek provider, 310KB
Responses bodies to deepseek/deepseek-flash through `magpie serve`:
base 15ms process CPU per request (19/20 ok), new 13ms (20/20, then
8/8 again).
TestResponsesFactsAsTheWholeParse compares readFacts with the full
parse's turnIn and workPlace over constructed bodies and five real Codex
fixtures. Broken by hand twice (user parts skipped; ToolResult turned
into Text): red both times. tier_record_test adds four long-body cases
(tier after the conversation, a nested one ignored, Anthropic speed
fast, a Chat numeric tier).
Run at this head (720e491d2 + these two commits): gofmt clean, go build,
GOOS=linux and GOOS=windows go vet -tags nogui on gateway and provider;
go test -run 'Facts|Tier|Session|Responses|Metadata|UserWords|Prompt|Memo'
./internal/gateway ok. The full gateway and provider packages are
running and are reported on #3394.
108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
package gateway
|
|
|
|
// The service tier a request asked for, what went out on each try and
|
|
// what the vendor said it ran at, for the usage log, its CSV and the
|
|
// route (#1784). Fast's and Ultrafast's priority processing bills at
|
|
// more than the default, and the tier a request went at could change on
|
|
// the way: a fast pick asks for priority, and an account whose plan
|
|
// doesn't offer Ultrafast is asked for priority in its place.
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// bodyTier is the service tier a request body in proto asks for, in
|
|
// OpenAI's words (service_tier: "priority", "ultrafast", "flex" …);
|
|
// Claude's speed "fast" reads as "fast". "" when it asks for none.
|
|
func bodyTier(proto provider.Protocol, body []byte) string {
|
|
switch proto {
|
|
case provider.Anthropic:
|
|
if topValue(body, "speed").String() == "fast" {
|
|
return "fast"
|
|
}
|
|
fallthrough
|
|
case provider.Chat, provider.Responses:
|
|
if t := topValue(body, "service_tier"); t.Type == gjson.String {
|
|
return tierWord(t.String())
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// topValue is gjson's value of body's top-level member key. A long body is
|
|
// asked through its split (topMember), made once a request: gjson walked
|
|
// the whole conversation for a member Codex sends after it (#3394).
|
|
func topValue(body []byte, key string) gjson.Result {
|
|
if len(body) >= validMemoMin {
|
|
if v, found, ok := topMember(body, key); ok {
|
|
if !found {
|
|
return gjson.Result{}
|
|
}
|
|
return gjson.ParseBytes(v)
|
|
}
|
|
}
|
|
return gjson.GetBytes(body, key)
|
|
}
|
|
|
|
// tierWord is a tier as the log keeps it: short, and nothing else.
|
|
func tierWord(t string) string {
|
|
t = strings.TrimSpace(t)
|
|
if len(t) > 32 {
|
|
return ""
|
|
}
|
|
return t
|
|
}
|
|
|
|
// noteWireTier leaves, where the request's context has a place for it
|
|
// (wireAt), the tier body goes out to the vendor at: the last request a
|
|
// try sent is the one that answered or failed.
|
|
func noteWireTier(ctx context.Context, to provider.Protocol, body []byte) {
|
|
if w, _ := ctx.Value(wireAtKey{}).(*wireAt); w != nil {
|
|
w.tier = bodyTier(to, body)
|
|
}
|
|
}
|
|
|
|
// upstreamTier is the service tier a reply (a JSON body, or one event of
|
|
// its stream) says it ran at: a top-level service_tier (a Chat body and
|
|
// its chunks, a Responses body), a Responses stream's response.service_tier,
|
|
// an Anthropic usage's service_tier. "" when it says none, which is never
|
|
// read as the tier that was asked.
|
|
func upstreamTier(b []byte) string {
|
|
if !bytes.Contains(b, []byte(`"service_tier"`)) {
|
|
return ""
|
|
}
|
|
var v struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
Response struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
} `json:"response"`
|
|
Usage struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
} `json:"usage"`
|
|
Message struct {
|
|
Usage struct {
|
|
Tier json.RawMessage `json:"service_tier"`
|
|
} `json:"usage"`
|
|
} `json:"message"`
|
|
}
|
|
if json.Unmarshal(b, &v) != nil {
|
|
return ""
|
|
}
|
|
for _, raw := range []json.RawMessage{v.Tier, v.Response.Tier, v.Usage.Tier, v.Message.Usage.Tier} {
|
|
var s string
|
|
if json.Unmarshal(raw, &s) == nil {
|
|
if s = tierWord(s); s != "" {
|
|
return s
|
|
}
|
|
}
|
|
}
|
|
return ""
|
|
}
|