Documentation
¶
Overview ¶
Package parsercommon holds small helpers shared by the protocol parser libraries in core/parse (mcp, a2a, inference), the mcp-parser and inference-parser plugins, and the cpex plugin. Lives under core/internal/ so only packages under core/ can import it — third-party plugins are expected to either depend on the public pipeline surface or roll their own parsing. The contents here are stable but intentionally undocumented as a public API.
Index ¶
Constants ¶
const DebugBodyMax = 512
DebugBodyMax caps how many characters of a body/content string a parser writes into debug logs. Large enough to capture a short user message or a tool_call response verbatim, small enough to keep log lines tractable.
const MaxDecodedRequestSize = 64 << 20
MaxDecodedRequestSize caps how large a zstd-decompressed request body is allowed to grow to, independent of the wire-size cap the listener already enforces (32 MiB as of this writing). zstd.NewReader's own default (WithDecoderMaxMemory) is 64 GiB — a highly compressible body well within the wire cap could otherwise decompress far past it. 64 MiB is generous relative to any real request seen so far (a live Codex sample decompressed to ~65 KB) while still bounding the worst case.
Variables ¶
This section is empty.
Functions ¶
func DecodeRequestBody ¶
DecodeRequestBody returns body decompressed per the request's Content-Encoding header, or body unchanged when the header names an encoding this function doesn't handle, or isn't present at all.
zstd only, for now — the one encoding seen on live Codex traffic to the Responses endpoint. Nothing upstream of the parsers decompresses a request body (every listener hands plugins the raw bytes it read off the wire), so inference-parser decodes with this before parsing, and any plugin that re-reads a parsed body must decode it the same way to see the same bytes.
A failed decompression (truncated body, a header that lied, or a body that would exceed MaxDecodedRequestSize) returns the original compressed bytes rather than erroring: the caller's subsequent JSON decode then fails too, and the request is treated the same as any other body it cannot read.
func NormalizeSSE ¶
NormalizeSSE puts a buffered SSE body into the one shape the line scanners read: no leading byte-order mark, and LF line endings.
BOTH HALVES ARE WIRE-LEGAL AND BOTH WERE MISSED. The event-stream format allows CRLF, LF or CR as the line terminator and requires a decoder to strip one leading BOM — while detection looked for "data:" after trimming " \t\r\n" (which does not include a BOM) or for a literal "\ndata:" (which a CR-only stream never contains), and the parsers split on LF alone. So a BOM-prefixed body, or a CR-only body whose first field is not `data:`, matched no data: line at all.
SHARED BECAUSE THE MISS WAS A COPY PATTERN (#1567): inference-parser had this fix and the a2a and mcp parsers did not — a BOM-prefixed or CR-only MCP/A2A stream left the extension empty, and IBAC treats an unclassified exchange as one that passes by default. Detection and parsing use the same function deliberately: fixing only a detector would route such a body to a parser that still cannot read it, which trades a silent miss for a silent empty parse.
Allocation-free for the overwhelming majority: a body with no BOM and no CR is returned as it came.
Types ¶
type JSONRPCRequest ¶
type JSONRPCRequest struct {
Method string `json:"method"`
ID any `json:"id"`
Params map[string]any `json:"params"`
}
JSONRPCRequest is the minimal JSON-RPC 2.0 request shape the a2a and mcp parsers decode. Fields intentionally match the permissive `any`-typed variant of the protocol: the parsers never re-emit these on the wire so strict typing here would only cost flexibility.
func (*JSONRPCRequest) MapParam ¶
func (r *JSONRPCRequest) MapParam(key string) map[string]any
MapParam returns the Params[key] value cast to map[string]any, or nil if absent / wrong type.
func (*JSONRPCRequest) StringParam ¶
func (r *JSONRPCRequest) StringParam(key string) string
StringParam returns the Params[key] value cast to string, or "" if absent / wrong type.
type Kind ¶
type Kind uint8
Kind is a bitmask naming which sub-kinds the provider populated. A zero on a set bit is "reported zero"; a zero on an unset bit is "not exposed."
type TokenUsage ¶
type TokenUsage struct {
Input int // uncached prompt tokens
CacheRead int // prompt tokens served from cache
CacheWrite int // prompt tokens written to cache
// CacheWrite1h is the part of CacheWrite written with a 1-hour TTL (Anthropic's
// cache_creation.ephemeral_1h_input_tokens), never an addition to CacheWrite.
// Zero means no 1h writes were reported; it carries no Present bit of its own
// because it is a subset of the KindCacheWrite bit CacheWrite already sets.
CacheWrite1h int
Output int // generated completion tokens
Reasoning int // reasoning-only output (subset of Output)
ReportedTotal int // provider's own total_tokens if reported, else 0
Present Kind // which sub-kinds the provider reported
}
TokenUsage is the provider-neutral token accounting shape. Parsers normalize their wire format into these fields before publishing via Fill, and set Present to name which sub-kinds the wire carried.
func (TokenUsage) Fill ¶
func (u TokenUsage) Fill(ext *model.InferenceExtension)
Fill writes the split counters, the Present bitmask, and the derived legacy aggregates onto ext. TotalTokens prefers the provider's own reported total when present — a gateway that reports only total_tokens (with prompt/completion zero) would otherwise record 0 here.
func (TokenUsage) PromptTotal ¶
func (u TokenUsage) PromptTotal() int
PromptTotal is the sum of all prompt-side sub-kinds.
func (TokenUsage) Total ¶
func (u TokenUsage) Total() int
Total is PromptTotal + Output. Reasoning is a subset of Output and intentionally not added.