Files
CLIProxyAPI/internal/runtime/executor/helps/gemini_ttft_helpers.go
Luis Pater 6e6afab99a refactor: migrate gjson, sjson, and base64 to MoeForge/moego
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson
  and encoding/base64 with github.com/MoeForge/moego/base64 across the main
  module and examples
- Introduce cross-method *ojson.Doc request rewriting pipelines across
  thinking, signature, client/codex, runtime/executor/helps, pluginhost, and
  all provider executors with FinalizePayloadDoc as the final semantic barrier
- Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations)
  to batch edits on ojson.Doc
- Upgrade read-only byte paths to ojson.GetBytesNoCopy,
  ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit
  strings.Clone ownership protection across streaming and persistent state
- Use base64.StdEncoding.ValidateString for zero-allocation signature checks
  and base64.EncodeDataURL for single-allocation data URL construction
2026-11-03 03:05:04 +08:00

89 lines
2.5 KiB
Go

package helps
import (
"bytes"
"github.com/MoeForge/moego/ojson"
)
// IsGeminiTokenEvent reports whether a Google Gemini / Antigravity streaming chunk carries
// substantive output token content (such as text parts, thought traces, or function calls).
// It filters out metadata-only chunks (e.g. standalone usageMetadata) and empty parts.
func IsGeminiTokenEvent(payload []byte) bool {
payload = bytes.TrimSpace(payload)
if len(payload) == 0 {
return false
}
// Strip "data:" SSE prefix if present
if bytes.HasPrefix(payload, []byte("data:")) {
prefixLen := len("data:")
if bytes.HasPrefix(payload, []byte("data: ")) {
prefixLen = len("data: ")
}
payload = bytes.TrimSpace(payload[prefixLen:])
if len(payload) == 0 {
return false
}
}
// Terminal error fallback
if ojson.GetBytesNoCopy(payload, "error.message").Exists() || ojson.GetBytesNoCopy(payload, "error").Exists() || ojson.GetBytesNoCopy(payload, "response.error").Exists() {
return true
}
candidatesNode := ojson.GetBytesNoCopy(payload, "candidates")
if !candidatesNode.Exists() {
candidatesNode = ojson.GetBytesNoCopy(payload, "response.candidates")
}
candidates := candidatesNode.Array()
if len(candidates) == 0 {
return false
}
for _, candidate := range candidates {
// Inspect content parts
parts := candidate.Get("content.parts").Array()
for _, part := range parts {
if len(part.Get("text").String()) > 0 {
return true
}
if len(part.Get("thoughtText").String()) > 0 {
return true
}
if thought := part.Get("thought"); thought.Type == ojson.String && len(thought.String()) > 0 {
return true
}
if len(part.Get("functionCall.name").String()) > 0 {
return true
}
if len(part.Get("inlineData.data").String()) > 0 {
return true
}
}
// Terminal finishReason fallback (e.g., STOP, MAX_TOKENS, SAFETY)
if len(candidate.Get("finishReason").String()) > 0 {
return true
}
}
return false
}
// ObserveGeminiTokenEvent inspects a Google Gemini / Antigravity streaming chunk and records TTFT
// if the frame represents the first meaningful token event. It records first-packet arrival time
// as fallback and returns immediately with zero allocations once effective token TTFT is set.
func ObserveGeminiTokenEvent(reporter *UsageReporter, payload []byte) {
if reporter != nil && len(payload) > 0 {
reporter.ObserveResponseModel(payload)
}
if reporter == nil || len(payload) == 0 {
return
}
if reporter.IsTTFTSet() {
return
}
reporter.ObserveTokenEvent(IsGeminiTokenEvent(payload))
}