Files
magpie/internal/gateway/ttft.go
T
yetone d924802de5 usage: a streamed reply's time to its first token is kept, and its first text's (#196: usage 记录首字延迟(TTFT),用于计算模型的真实生成速度) — usage.jsonl had only a call's whole ms and its out tokens, so how fast a model writes couldn't be told from how long it waited or thought. The reply is now read as it goes to the agent, in the agent's protocol whatever the vendor spoke (Anthropic content_block_delta text/thinking/input_json and a tool_use's start, Chat content/reasoning_content/reasoning/tool_calls, Responses output_text/reasoning/function_call_arguments deltas and a function call's start, Gemini parts), and each record gets ttft_ms to the first content and first_text_ms to the first text, counted from the request as ms is, so ms − ttft_ms is the writing time; a reply that isn't streamed gets neither, and omitempty leaves old records as they were. Only the stream's events up to its first text are read (a JSON reply, or past 8 MB, not at all). Each try in the routing trace keeps its own from when it was sent, so a group that fails over shows which member was slow, and the route, the request log and the Codex backend's relay keep them too. Shown on the Routing page (TTFT in the list, first token / first text / tok/s in a try's story), in the Gateway's request log, and on the Usage page and in magpie usage per model as the mean TTFT and tok/s = out ÷ (ms − ttft) over the answered streamed calls. Tested with fake vendors streaming a gap apart for Anthropic, Chat and Responses, as they came and translated (Chat from Anthropic, Responses from Chat), the ChatGPT backend without a Content-Type, a whole reply, a group whose first member fails (its try untimed, the second's timed from its own start, the request's including the failed try), each protocol's events and split writes, and the summary's sums; the GUI's browser tests pass
2026-09-29 16:40:03 +08:00

199 lines
5.6 KiB
Go

package gateway
// Time to first token (#196): how long a streamed reply took to begin —
// the first of its text, reasoning or a tool call — and to show its first
// text, so how fast a model writes can be told from how long it thought
// or waited. Read off the stream as it goes to the agent, in the agent's
// protocol, whichever the vendor spoke: one place for every way a reply
// comes, translated or not. A reply that isn't streamed isn't timed.
import (
"bytes"
"encoding/json"
"time"
)
// firstMost is the most of a stream read for its first tokens: a reply
// that is all tool calls has no text to wait for.
const firstMost = 8 << 20
// firstToken times a stream's first content and first text from start.
type firstToken struct {
start time.Time
first time.Duration // to the first content; 0 until it comes
text time.Duration // to the first text
off bool // not a stream, or read as far as it is
began bool
read int
pend []byte
}
// see reads what was written of the reply.
func (f *firstToken) see(b []byte) {
if f.off || len(b) == 0 {
return
}
if !f.began {
// a JSON reply is no stream
if t := bytes.TrimLeft(b, " \t\r\n"); len(t) == 0 {
return
} else if t[0] == '{' || t[0] == '[' {
f.off = true
return
}
f.began = true
}
if f.read += len(b); f.read > firstMost {
f.off, f.pend = true, nil
return
}
f.pend = append(f.pend, b...)
for !f.off {
end := eventEnd(f.pend)
if end < 0 {
break
}
content, text := firstKind(f.pend[:end])
f.pend = f.pend[end:]
if !content && !text {
continue
}
d := max(time.Since(f.start), time.Millisecond)
if f.first == 0 {
f.first = d
}
if text {
f.text = d
f.off, f.pend = true, nil // all there is to know
}
}
if len(f.pend) == 0 {
f.pend = nil
}
}
// ms are the two as milliseconds from start, 0 for none.
func (f *firstToken) ms() (first, text int64) {
return f.first.Milliseconds(), f.text.Milliseconds()
}
// firstKind says whether a server-sent event of a reply, in any protocol an
// agent speaks, carries content — text, reasoning or a tool call — and
// whether that is text the reader sees.
func firstKind(ev []byte) (content, text bool) {
var name string
var data []byte
for _, ln := range bytes.Split(ev, []byte("\n")) {
ln = bytes.TrimRight(ln, "\r")
switch {
case bytes.HasPrefix(ln, []byte("event:")):
name = string(bytes.TrimSpace(ln[6:]))
case bytes.HasPrefix(ln, []byte("data:")):
data = append(data, bytes.TrimSpace(ln[5:])...)
}
}
// only deltas, a tool call's start and Gemini's parts carry any: the
// rest go by unread
if len(data) == 0 || !(bytes.Contains(data, []byte("delta")) || bytes.Contains(data, []byte(`"parts"`)) ||
bytes.Contains(data, []byte("content_block_start")) || bytes.Contains(data, []byte("output_item.added"))) {
return false, false
}
var v struct {
Type string `json:"type"`
Delta json.RawMessage `json:"delta"` // Anthropic's an object, Responses' a string
ContentBlock *struct {
Type string `json:"type"`
} `json:"content_block"`
Item *struct {
Type string `json:"type"`
} `json:"item"`
Choices []struct {
Delta struct {
Content json.RawMessage `json:"content"`
Refusal json.RawMessage `json:"refusal"`
ReasoningContent json.RawMessage `json:"reasoning_content"`
Reasoning json.RawMessage `json:"reasoning"`
ToolCalls json.RawMessage `json:"tool_calls"`
FunctionCall json.RawMessage `json:"function_call"`
} `json:"delta"`
} `json:"choices"`
Candidates []struct {
Content struct {
Parts []struct {
Text string `json:"text"`
Thought bool `json:"thought"`
FunctionCall json.RawMessage `json:"functionCall"`
} `json:"parts"`
} `json:"content"`
} `json:"candidates"`
}
if json.Unmarshal(data, &v) != nil {
return false, false
}
typ := v.Type
if typ == "" {
typ = name
}
switch typ {
case "content_block_delta": // Anthropic
var d struct {
Type string `json:"type"`
Text string `json:"text"`
Thinking string `json:"thinking"`
PartialJSON string `json:"partial_json"`
}
json.Unmarshal(v.Delta, &d)
switch {
case d.Text != "":
return true, true
case d.Thinking != "", d.PartialJSON != "":
return true, false
}
return false, false
case "content_block_start":
if b := v.ContentBlock; b != nil && (b.Type == "tool_use" || b.Type == "server_tool_use") {
return true, false
}
return false, false
case "response.output_text.delta", "response.refusal.delta": // Responses
return filled(v.Delta), filled(v.Delta)
case "response.reasoning_summary_text.delta", "response.reasoning_text.delta",
"response.function_call_arguments.delta", "response.custom_tool_call_input.delta":
return filled(v.Delta), false
case "response.output_item.added":
if it := v.Item; it != nil && (it.Type == "function_call" || it.Type == "custom_tool_call") {
return true, false
}
return false, false
}
for _, c := range v.Choices { // Chat
d := c.Delta
if filled(d.Content) || filled(d.Refusal) {
return true, true
}
if filled(d.ReasoningContent) || filled(d.Reasoning) || filled(d.ToolCalls) || filled(d.FunctionCall) {
content = true
}
}
for _, c := range v.Candidates { // Gemini
for _, p := range c.Content.Parts {
switch {
case p.Text != "" && !p.Thought:
return true, true
case p.Text != "", filled(p.FunctionCall):
content = true
}
}
}
return content, false
}
// filled is a JSON value with something in it.
func filled(raw json.RawMessage) bool {
switch string(bytes.TrimSpace(raw)) {
case "", "null", `""`, "[]", "{}":
return false
}
return true
}