Files
CLIProxyAPI/internal/runtime/executor/helps/chat_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

117 lines
3.1 KiB
Go

package helps
import (
"bytes"
"github.com/MoeForge/moego/ojson"
)
// IsChatTokenEvent reports whether the given OpenAI-compatible Chat Completions SSE or JSON chunk
// carries substantive output token content (such as text delta, reasoning content, or tool call arguments).
// It filters out container metadata, role announcements, and empty delta frames.
func IsChatTokenEvent(payload []byte) bool {
payload = bytes.TrimSpace(payload)
if len(payload) == 0 {
return false
}
// Handle SSE stream terminal marker
if bytes.Equal(payload, []byte("data: [DONE]")) || bytes.Equal(payload, []byte("[DONE]")) {
return true
}
// Handle SSE line format (e.g., "data: {...}")
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
}
if bytes.Equal(payload, []byte("[DONE]")) {
return true
}
}
// Terminal error envelope fallback
if ojson.GetBytesNoCopy(payload, "error.message").Exists() || ojson.GetBytesNoCopy(payload, "error").Exists() {
return true
}
choices := ojson.GetBytesNoCopy(payload, "choices").Array()
if len(choices) == 0 {
return false
}
for _, choice := range choices {
// Streaming delta checks
delta := choice.Get("delta")
if delta.Exists() {
if len(delta.Get("content").String()) > 0 {
return true
}
if len(delta.Get("reasoning_content").String()) > 0 {
return true
}
if len(delta.Get("reasoning").String()) > 0 {
return true
}
if len(delta.Get("refusal").String()) > 0 {
return true
}
for _, tc := range delta.Get("tool_calls").Array() {
if len(tc.Get("function.arguments").String()) > 0 || len(tc.Get("function.name").String()) > 0 {
return true
}
if len(tc.Get("custom.input").String()) > 0 {
return true
}
}
}
// Non-streaming / completed message fallback
message := choice.Get("message")
if message.Exists() {
if len(message.Get("content").String()) > 0 {
return true
}
if len(message.Get("reasoning_content").String()) > 0 {
return true
}
if len(message.Get("refusal").String()) > 0 {
return true
}
for _, tc := range message.Get("tool_calls").Array() {
if len(tc.Get("function.arguments").String()) > 0 || len(tc.Get("function.name").String()) > 0 {
return true
}
}
}
// Finish reason terminal fallback
if len(choice.Get("finish_reason").String()) > 0 {
return true
}
}
return false
}
// ObserveChatTokenEvent inspects an OpenAI Chat Completions 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 ObserveChatTokenEvent(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(IsChatTokenEvent(payload))
}