Documentation
¶
Overview ¶
Package wire reads a provider's 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. Every host built on accountant parsed a provider's request body this way on its own before this package existed (accountant#150); this is their shared logic, with these rules: a body with no messages array is read as a Responses body, by its input array or string, when it has one; 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_control") is still caught, because gjson decodes it the same as the literal key.
Format and FormatOf stay with each host: Parse does not need Format to pick which top-level fields it reads (it detects the body's own shape), and a host's own endpoint-to-dialect mapping is a host concept, not a generic one (accountant#157).
Index ¶
- Constants
- func AllMarksAsk1h(raw []byte) bool
- func ContentChangeID(prefix string, parts ...json.RawMessage) string
- func EnvelopeOps(client, forwarded []byte, operatorPrefix, changeNS string, owner Owner) []accountant.Operation
- func FindCacheMark(v gjson.Result, prefix string) string
- func Parse(raw []byte) (accountant.Body, []accountant.Breakpoint, error)
- func ParseCounted(raw []byte, count accountant.OpaqueCount) (accountant.Body, []accountant.Breakpoint, error)
- func TextChangeID(prefix, text string) string
- func WithoutCacheMarks(body []byte, v gjson.Result) []byte
- type Owner
Constants ¶
const ( // OpHeadTTL is the relabel of a cache mark to the one-hour tier, in place. It is its own // operation so the engine can book the 1h premium; the engine finds it by the ChangeID prefix // "headttl:" (design §4.4), not by this component name. OpHeadTTL = "headttl" // OpToolSchema is the default attribution of a tool declaration rewritten in place, or added // with no Owner match: the host component that rewrites tool schemas. OpToolSchema = "toolschema" // OpToolFilter is the default attribution of a tool declaration removed, or a system block // rewritten in place: the host component that drops a declaration or a listing. OpToolFilter = "toolfilter" )
The operator components EnvelopeOps books by default. Each is a bare component name, with no host prefix: EnvelopeOps puts the host's own operator prefix in front of it (see EnvelopeOps). A host reads these to match an envelope op's operator, and the engine keys the 1h relabel on OpHeadTTL's own ChangeID prefix "headttl:" (design §4.4).
The operator of a declaration a host's own injector added does NOT live in this package: a host names it through the Owner callback instead, so this package imports no injector and names no injector component. A host's own operator prefix is not here either, because it names the host; the host passes it to EnvelopeOps.
Variables ¶
This section is empty.
Functions ¶
func AllMarksAsk1h ¶
AllMarksAsk1h reports whether raw has at least one cache_control mark and every one of them asks for the 1h tier. 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), so that this 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: some gateways send cache_control on Chat Completions content blocks for a Claude model behind them. A Chat or Responses body shaped that way still raises marks here, even when a host's own provider classification gives it OpenAI cache facts regardless. No Format parameter is needed to gate this function; it only reads cache_control, whichever wire format carries it.
func ContentChangeID ¶
func ContentChangeID(prefix string, parts ...json.RawMessage) string
ContentChangeID is prefix plus a hash of the wire JSON parts, each read without its cache marks (WithoutCacheMarks). Use it when the ChangeID follows a message, a block, a declaration or any other element as it sits in the body. Every part must be valid JSON, such as gjson.Result.Raw.
func EnvelopeOps ¶
func EnvelopeOps(client, forwarded []byte, operatorPrefix, changeNS string, owner Owner) []accountant.Operation
EnvelopeOps lists the operations that explain the difference between the client body and the forwarded body, in the tools array, the system section, and the top-level cache_control TTL. It returns nil when either body has no conversation (no messages array). On the Responses wire the system section is instructions and the messages section is input.
operatorPrefix is the host's own prefix on every booked operator: this package cannot name a host (accountant#150), so the host passes the prefix it books on its other operators, and EnvelopeOps puts it on each operator here too, so the engine groups the two alike. A metrics sink cuts it off again for the component label.
changeNS is the host's ChangeID namespace for the envelope ops. It does not reach the one-hour relabel (OpHeadTTL): the engine keys that by its own prefix "headttl:" (design §4.4), the same on every host, so that op keeps the bare "headttl:" ChangeID whatever the namespace is.
owner names the operator of a declaration a host injector added; see Owner.
func FindCacheMark ¶
FindCacheMark returns the gjson path of the first cache-control mark under v, or "" when there is none. A mark is a cache_control key on v itself or on a block of a content array, at any depth of content nesting (a tool_result block's own content), down to maxContentDepth. It descends only through "content" arrays, so a cache_control key anywhere else stays in the hash: a tool's input_schema.properties.cache_control parameter, or a tool_use block's input value, is part of what the provider caches, and a change to it must change the hash.
func Parse ¶
func Parse(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.
The size of each position keeps the cache_control bytes; only the hash leaves them out (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.
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.
func ParseCounted ¶
func ParseCounted(raw []byte, count accountant.OpaqueCount) (accountant.Body, []accountant.Breakpoint, error)
ParseCounted is Parse with the token count of each thinking block the host saw a model make (accountant design D16): count is looked up by each thinking signature's accountant.OpaqueKey, so a block the client sends back is priced at its own count, not a fixed one. A nil count gives every block the fixed price, which is exactly what Parse does. The hashes, sizes and breakpoints do not depend on count.
func TextChangeID ¶
TextChangeID is prefix plus a hash of a plain string that holds no cache mark: a tool name, the names of removed tools joined by a zero byte, or the text of a block (see textOf). It is not for wire JSON; give that to ContentChangeID.
func WithoutCacheMarks ¶
WithoutCacheMarks is one position's JSON without its cache_control marks: its own, the ones on its content blocks, and ones nested deeper (a mark inside a tool_result block's own content array). A key named cache_control anywhere else is data, not a mark, and stays (see FindCacheMark). A position with none is returned as it is, byte for byte, so the common case costs no allocation. The fast path's check is mayMarkCacheControl, not a plain literal-text search: a block holding "cache_control":{"ttl":"1h"} has no literal "cache_control" substring, but gjson decodes the escape when it reads the key, so eachMark still reports it as a mark. Leaving a block like that in the hash would make adding or moving that metadata look like a prefix change.
Types ¶
type Owner ¶
Owner, when it returns ok=true for name, names the operator component of an ADDED tools declaration whose name is name -- a declaration a host's own injector added, which the default attribution (OpToolSchema for a rewrite, OpToolFilter for a removal) does not cover. A host with injectors like this gives their names and components here, so this package does not import the injector packages. A nil Owner means no host injector: every added declaration then gets the default attribution.