wire

package
v0.0.0-...-53bd38a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wire reads an inference request body into the shape github.com/rossoctl/accountant hashes: tools, then system, then messages, one []byte per position, in the provider's cache order (accountant/docs/cortex-wiring.md W6.3). It ports the context-guru proxy's accParse (proxy/accountant_savings_body.go) to Cortex, so both hosts build the same accountant.Body for the same bytes, with these deliberate departures from accParse: scan keeps reading for a Responses input when messages is not an array; a top-level cache_control (Anthropic's automatic caching) counts as a mark and gives a breakpoint; only cache_control keys in cache-metadata locations leave the hash, so a tool parameter named cache_control stays in it; a mark nested inside a tool_result's own content is still found and still a mark, at any depth up to maxContentDepth; a null cache_control is not a mark; and an escaped cache_control key ("cache\u005fcontrol") is still caught, because gjson decodes it the same as the literal key.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllMarksAsk1h

func AllMarksAsk1h(raw []byte) bool

AllMarksAsk1h reports whether raw has at least one cache_control mark and every one of them asks for the 1h tier. It answers the question the context-guru proxy's apply.AllBreakpointsAskExtendedTTL (apply/apply.go) answers, and facts.For uses it for the same "is this whole entry 1h" question W6.4's entryFacts answers. It counts raw marks, not merged positions: a position with two marks (a 5m one and a 1h one) counts as two marks here, unlike breakpointsOf's one merged Breakpoint per position. That is why this exists apart from the Breakpoint slice Parse returns: it must see a lone 5m mark even when it shares a position with a 1h one and the merged Breakpoint says "1h".

A mark is what eachMark finds: a cache_control object on a tool, system block or message, or on a content block at any depth (a block inside a tool_result's content). The proxy's version reads only one level of content, so it misses a nested 5m mark; this one does not, so that it agrees with breakpointsOf and with the marks withoutCacheMarks strips from the hash. A null cache_control is not a mark.

The top-level cache_control (automatic caching) is one more mark: a body whose only mark is a top-level 1h one asks for 1h, and a top-level 5m mark next to 1h block marks makes this false.

Anthropic-only in the common case, but not always: Cortex's own chat decoder reads cache_control on Chat content blocks (core/translate/chat/decode.go), and gateways such as LiteLLM and OpenRouter send it on /v1/chat/completions for Claude models. A Chat or Responses body shaped that way still raises marks here, even though facts.Classify gives it OpenAI cache facts regardless — the same gap the proxy has. No Format parameter is needed to gate this function; it only reads cache_control, whichever wire format carries it.

func Parse

func Parse(f Format, raw []byte) (accountant.Body, []accountant.Breakpoint, error)

Parse splits raw into the sections accountant.NewBody wants, and lists its cache breakpoints. It returns an error only when raw is not valid JSON; a body that parses but has no messages (or input) array gives an empty accountant.Body, as the proxy's accBodies does for the same case.

The size of each position keeps the cache_control bytes; only the hash leaves them out (W6.3, accountant#21): the provider's own cache key ignores the marks, and a client moves its marks every turn, so a hash that included them would never find a common prefix with the previous request.

f is not used to pick which top-level fields are read: like the proxy's accScan and responsesSections, Parse detects the body's own shape (does it have a messages array, or an input array/string instead), not the endpoint it arrived on. A body whose shape does not match the dialect its path would suggest is still read by its actual shape, which is what the provider itself will read. f stays a parameter for symmetry with FormatOf and for a caller that wants to record which endpoint this body came from.

func ParseCounted

func ParseCounted(f Format, raw []byte, count accountant.OpaqueCount) (accountant.Body, []accountant.Breakpoint, error)

ParseCounted is Parse with the host's token count for each thinking block (accountant design D16, rossoctl/cortex#2280 item 4). count looks up a block by its signature's (or redacted_thinking data's) accountant.OpaqueKey; a block with a known count is priced at it, and one with none keeps the fixed price. A nil count is Parse: no block is counted. The sizes and the breakpoints do not depend on count.

Types

type Format

type Format int

Format is the wire shape of an inference request body: which top-level fields hold its tools, system prompt and conversation turns.

const (
	FormatAnthropic Format = iota // POST /v1/messages: tools, system, messages
	FormatChat                    // POST /v1/chat/completions: tools, messages (no top-level system)
	FormatResponses               // POST /v1/responses: tools, instructions, input
)

func FormatOf

func FormatOf(path string) (Format, bool)

FormatOf reports the wire format of an inference endpoint from its path, by calling core/parse/inference's ProtocolFor rather than re-deriving the path suffixes: that is the function the request parser and translator already use to decide a path's dialect, and a second, narrower table here previously disagreed with it on endpoints such as Azure's /openai/deployments/<d>/chat/completions and Codex's /backend-api/codex/responses.

ok is false when the path has no translation protocol: legacy completions (/v1/completions, /v1/engines/<id>/completions) included, deliberately — Cortex's parser does accept and forward them, but their body has no messages array and never carries cache_control, so this package has no positions to read and no mark to find either way. A caller that wants a cost record for those requests regardless (as the proxy's provider-based classification does) must get it some other way; this package stays silent on them rather than fabricate a Format for a body shape it cannot parse.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL