A long or reasoning reply looked stuck on the Routing page. A request still out now shows, on its row and beside the account answering it, the bytes the agent has had, "≈ n tokens" once a rate is learned, and how long ago content last came; after 15s with no content the account's dot stops pulsing. Nothing stays once the request is done. Ported from Canda15's 7f7bdb189 (github.com/Canda15/magpie), the idea and the per-protocol content extractors kept, the hot path and storage reworked: - every write to the agent passes captureResponseWriter.Write, which calls liveRow.observe; the row hangs on the request's inflight (an atomic pointer), not a context value made per request - rates are in memory only (256 pairs); the fork wrote a JSON file under the estimate lock on every finished request - a whole JSON reply is counted in bytes only: the fork held and decoded up to 8 MiB of it at the end; a stream is cut into lines in place, only a line cut by a write's end is copied, a line over 1 MiB passed over, and each event decoded once (the fork ran json.Valid then Unmarshal) - the reply's kind comes from its first byte, not a header read on every write; the keepalive check allocates nothing (the fork split each write) - everything a row counts is under its own lock, and inflight.tr is read under f.mu; the estimate is in float64 (the int64 product overflowed) - the route's Seq is bumped on every write and traceGather batches the wake-ups; the fork's global throttle could drop the last update before a pause - learned only from a streamed reply that did not fail and had the vendor's output tokens - left out: the finished row's "Received" metric and the ended caption (layout restraint; an ended row has its own metrics), a duplicate "Received" i18n key, the receive-monitor doc (the design is in gateway-routing.md now), the fork's xmod and Windows CI commits The Routing plugin and i18n.js are outside TestCoreScriptBudget. Run on mote at this head unless named: gofmt -l; go vet (darwin), GOOS=linux -tags nogui and GOOS=windows go vet ./...; go build ./...; go test -race on the live tests; go test ./internal/gateway ./internal/gui (gateway ok; gui red only on TestCoreScriptBudget, #5139, 1533907 > 1533802 before this change and untouched by it, and TestMainWindowCorners/macOS_26_SDK, #3949); routing-live-bytes.test.cjs and i18n-keys in Chromium and WebKit, en/zh/zh-TW/ja/de, 1100 and 420px (24/24); every routing-* and panel-routing* GUI file with i18n-keys in WSL Linux (Chromium all green; WebKit reds in 12 files, the same class at origin/main7dd2434d3run the same way: 25 there, 28 here, the sets differing by timing samples; routing-live-patch alone red 2 of 3 on main, 1 of 3 here; filed as #4121, #4520, #5156, #2174). Rebased onto38f4f74e6(gateway.go, trace.go and the Routing plugin moved): build, gateway vet, the live tests, routing-live-bytes and i18n-keys run again there, green (24/24). Broken by hand, each red, then put back: - no observe in capture.Write: TestLiveCountsARealStream - commentOnly never true: TestLiveHeartbeatIsBytesNotContent, TestCommentOnlyTellsHeartbeatsApart - learning from failed replies: TestRatesLearnFromRepliesTheVendorFinished - a whole reply read as a stream: TestContentCounterLeavesAWholeReply - no Seq bump in liveBytes: TestLiveWritesReachTheRoute - no quiet class, no per-second repaint: routing-live-bytes 0/20 Not tried: a real vendor's stream in the running app; the ChatGPT backend chain (codex_backend.go) writes past the capture writer and isn't counted; the full Go and GUI suites run after the tag. Co-authored-by: Canda15 <124772373+Canda15@users.noreply.github.com>
289 lines
7.0 KiB
Go
289 lines
7.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The tokens of a reply in flight can only be estimated: the vendor's
|
|
// usage comes with the last event. What is counted is its content, the
|
|
// text, reasoning and tool names and arguments, not every byte written:
|
|
// the framing grows with the number of events, and differs by protocol,
|
|
// so it would read more events as more written. Content is counted in the
|
|
// agent's protocol, translated replies too, as what the agent received.
|
|
//
|
|
// Only a stream is counted: a whole JSON reply arrives at once and has
|
|
// nothing to show while it is out, and counting it would hold a copy of
|
|
// it. An event is decoded and dropped; only its content's length stays.
|
|
// What isn't known counts 0.
|
|
|
|
// liveLineMost is the longest SSE line kept across writes: one longer is
|
|
// not counted rather than held.
|
|
const liveLineMost = 1 << 20
|
|
|
|
// liveContentCounter counts a reply's content bytes, event by event.
|
|
type liveContentCounter struct {
|
|
proto provider.Protocol
|
|
// known: the first write said whether this is a stream (sse)
|
|
known, sse bool
|
|
// part: a line cut by the end of a write, until its end comes
|
|
part []byte
|
|
// data: the data lines of the event being read
|
|
data []byte
|
|
// skip: the line being read is too long, and is passed over to its end
|
|
skip bool
|
|
total int64
|
|
}
|
|
|
|
// add reads one write, line by line. A whole write's lines are read in
|
|
// place; only a line a write cuts is copied, to be joined with the next.
|
|
func (c *liveContentCounter) add(p []byte) {
|
|
if !c.known {
|
|
i := 0
|
|
for i < len(p) && (p[i] == ' ' || p[i] == '\r' || p[i] == '\n' || p[i] == '\t') {
|
|
i++
|
|
}
|
|
if i == len(p) {
|
|
return
|
|
}
|
|
c.known = true
|
|
c.sse = p[i] != '{' && p[i] != '['
|
|
}
|
|
if !c.sse {
|
|
return
|
|
}
|
|
for len(p) > 0 {
|
|
i := bytes.IndexByte(p, '\n')
|
|
if i < 0 {
|
|
if !c.skip && len(c.part)+len(p) <= liveLineMost {
|
|
c.part = append(c.part, p...)
|
|
} else {
|
|
c.part, c.skip = c.part[:0], true
|
|
}
|
|
return
|
|
}
|
|
line := p[:i]
|
|
p = p[i+1:]
|
|
if c.skip {
|
|
c.skip = false
|
|
continue
|
|
}
|
|
if len(c.part) > 0 {
|
|
if len(c.part)+len(line) > liveLineMost {
|
|
c.part = c.part[:0]
|
|
continue
|
|
}
|
|
c.part = append(c.part, line...)
|
|
line = c.part
|
|
}
|
|
c.line(bytes.TrimSuffix(line, []byte{'\r'}))
|
|
c.part = c.part[:0]
|
|
}
|
|
}
|
|
|
|
// line reads one SSE line: a blank one ends the event.
|
|
func (c *liveContentCounter) line(line []byte) {
|
|
if len(line) == 0 {
|
|
c.event()
|
|
return
|
|
}
|
|
rest, ok := bytes.CutPrefix(line, []byte("data:"))
|
|
if !ok {
|
|
return
|
|
}
|
|
rest = bytes.TrimPrefix(rest, []byte{' '})
|
|
if len(c.data)+len(rest)+1 > liveLineMost {
|
|
c.data = c.data[:0]
|
|
return
|
|
}
|
|
if len(c.data) > 0 {
|
|
c.data = append(c.data, '\n')
|
|
}
|
|
c.data = append(c.data, rest...)
|
|
}
|
|
|
|
// event counts the event read, and forgets it.
|
|
func (c *liveContentCounter) event() {
|
|
if len(c.data) > 0 {
|
|
c.total += int64(liveContentOf(c.proto, bytes.TrimSpace(c.data)))
|
|
c.data = c.data[:0]
|
|
}
|
|
}
|
|
|
|
// flush counts what the stream ended without a blank line after.
|
|
func (c *liveContentCounter) flush() {
|
|
if !c.sse {
|
|
return
|
|
}
|
|
if len(c.part) > 0 && !c.skip {
|
|
c.line(bytes.TrimSuffix(c.part, []byte{'\r'}))
|
|
}
|
|
c.part = nil
|
|
c.event()
|
|
c.data = nil
|
|
}
|
|
|
|
// liveContentOf is the content bytes of one event, read by its protocol.
|
|
func liveContentOf(proto provider.Protocol, b []byte) int {
|
|
if len(b) == 0 || b[0] != '{' {
|
|
return 0
|
|
}
|
|
switch proto {
|
|
case provider.Chat:
|
|
return liveChatContent(b)
|
|
case provider.Responses:
|
|
return liveResponsesContent(b)
|
|
case provider.Gemini:
|
|
return liveGeminiContent(b)
|
|
default:
|
|
return liveAnthropicContent(b)
|
|
}
|
|
}
|
|
|
|
// liveChatDelta is a Chat delta: its text, reasoning and tool calls.
|
|
type liveChatDelta struct {
|
|
Content json.RawMessage `json:"content"`
|
|
ReasoningContent string `json:"reasoning_content"`
|
|
Reasoning string `json:"reasoning"`
|
|
ToolCalls []liveChatToolCall `json:"tool_calls"`
|
|
}
|
|
|
|
type liveChatToolCall struct {
|
|
Function struct {
|
|
Name string `json:"name"`
|
|
Arguments string `json:"arguments"`
|
|
} `json:"function"`
|
|
}
|
|
|
|
func liveChatContent(b []byte) int {
|
|
var v struct {
|
|
Choices []struct {
|
|
Delta liveChatDelta `json:"delta"`
|
|
} `json:"choices"`
|
|
}
|
|
if json.Unmarshal(b, &v) != nil {
|
|
return 0
|
|
}
|
|
n := 0
|
|
for _, ch := range v.Choices {
|
|
n += liveChatDeltaLen(ch.Delta)
|
|
}
|
|
return n
|
|
}
|
|
|
|
func liveChatDeltaLen(d liveChatDelta) int {
|
|
n := len(d.ReasoningContent) + len(d.Reasoning) + liveContentLen(d.Content)
|
|
for _, tc := range d.ToolCalls {
|
|
n += len(tc.Function.Name) + len(tc.Function.Arguments)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// liveContentLen is a content field's: a string, or text parts.
|
|
func liveContentLen(raw json.RawMessage) int {
|
|
if len(raw) == 0 {
|
|
return 0
|
|
}
|
|
switch raw[0] {
|
|
case '"':
|
|
var s string
|
|
if json.Unmarshal(raw, &s) == nil {
|
|
return len(s)
|
|
}
|
|
case '[':
|
|
var parts []struct {
|
|
Text string `json:"text"`
|
|
}
|
|
if json.Unmarshal(raw, &parts) == nil {
|
|
n := 0
|
|
for _, p := range parts {
|
|
n += len(p.Text)
|
|
}
|
|
return n
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// liveResponsesContent counts *.delta events only: *.done says the same
|
|
// text again.
|
|
func liveResponsesContent(b []byte) int {
|
|
var v struct {
|
|
Type string `json:"type"`
|
|
Delta json.RawMessage `json:"delta"`
|
|
Item struct {
|
|
Type string `json:"type"`
|
|
Name string `json:"name"`
|
|
} `json:"item"`
|
|
}
|
|
if json.Unmarshal(b, &v) != nil {
|
|
return 0
|
|
}
|
|
if strings.HasSuffix(v.Type, ".delta") {
|
|
return liveContentLen(v.Delta)
|
|
}
|
|
// a function call starts with its name; its arguments come as deltas
|
|
if v.Type == "response.output_item.added" && v.Item.Type == "function_call" {
|
|
return len(v.Item.Name)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// liveAnthropicContent counts content_block_delta's text, thinking and
|
|
// tool input, and a tool_use block's name.
|
|
func liveAnthropicContent(b []byte) int {
|
|
var v struct {
|
|
Type string `json:"type"`
|
|
Delta struct {
|
|
Text string `json:"text"`
|
|
Thinking string `json:"thinking"`
|
|
PartialJSON string `json:"partial_json"`
|
|
} `json:"delta"`
|
|
ContentBlock struct {
|
|
Type string `json:"type"`
|
|
Name string `json:"name"`
|
|
} `json:"content_block"`
|
|
}
|
|
if json.Unmarshal(b, &v) != nil {
|
|
return 0
|
|
}
|
|
n := len(v.Delta.Text) + len(v.Delta.Thinking) + len(v.Delta.PartialJSON)
|
|
if v.ContentBlock.Type == "tool_use" {
|
|
n += len(v.ContentBlock.Name)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// liveGeminiContent counts a chunk's parts, each new.
|
|
func liveGeminiContent(b []byte) int {
|
|
var v struct {
|
|
Candidates []struct {
|
|
Content struct {
|
|
Parts []struct {
|
|
Text string `json:"text"`
|
|
FunctionCall struct {
|
|
Name string `json:"name"`
|
|
Args json.RawMessage `json:"args"`
|
|
} `json:"functionCall"`
|
|
} `json:"parts"`
|
|
} `json:"content"`
|
|
} `json:"candidates"`
|
|
}
|
|
if json.Unmarshal(b, &v) != nil {
|
|
return 0
|
|
}
|
|
n := 0
|
|
for _, c := range v.Candidates {
|
|
for _, p := range c.Content.Parts {
|
|
n += len(p.Text)
|
|
if p.FunctionCall.Name != "" || len(p.FunctionCall.Args) > 0 {
|
|
n += len(p.FunctionCall.Name) + len(p.FunctionCall.Args)
|
|
}
|
|
}
|
|
}
|
|
return n
|
|
}
|