Documentation
¶
Overview ¶
Package accwire reads a request body for the accountant, one adapter per wire.
The accountant library is wire-agnostic: it takes a Body of hashed positions, cache-mark breakpoints and operations. This package turns a provider request into those. Each wire reads its own shape in four places, and they all sit behind one Wire value, chosen once per body:
- Body and Parse cut a body into tools, system and messages (the hash space of the positions);
- Breakpoints finds the cache marks;
- EnvelopeOps and MessageOps report the edits that no pipeline component reports;
- WireIndex maps a pipeline slot path to the wire position it sits in.
A new wire (accountant#17) adds one kind here, and no other place. The package is a library: it imports no host package (proxy, proxyapp, dash).
The write side stays out. apply's slot types rewrite the request that goes upstream, so only this read side sits behind the interface (#1164).
Index ¶
- Constants
- func ContentChangeID(prefix string, parts ...json.RawMessage) string
- func FindCacheMark(v gjson.Result, prefix string) string
- func Operator(name string) string
- func OperatorsOutsideRegistry() []string
- func TextChangeID(prefix, text string) string
- func WithoutCacheMarks(body []byte, v gjson.Result) []byte
- type Wire
Constants ¶
const ( // OpKeeper is accountant.PingOps's operator: the keep-alive ping's spend. It has no host prefix. // The library exports it as accountant.PingOperator (v0.17.0); TestOpKeeperIsThePingOperator checks PingOps books it. OpKeeper = accountant.PingOperator // OpHeadTTL is the 1h head TTL, a host option (apply/headttl.go, envelopeOps). OpHeadTTL = "headttl" // OpTailBreakpoint is the trailing cache mark apply adds (apply/ops.go). OpTailBreakpoint = "tailbreakpoint" // OpToolDefer is tool deferral (accrec's billed-body pass, the proxy's MessageOps). OpToolDefer = "tooldefer" // OpExpand and OpAdjudicate are the tool declarations expand and adjudicate inject (envelopeOwner). OpExpand = "expand" OpAdjudicate = "adjudicate" // OpExpandRestore is the copy fixed_restore inserts (apply/restore.go, #481). OpExpandRestore = "expand_restore" )
The operators that book savings but are not registered components (components.Names()). Each emit site uses its constant, so OperatorsOutsideRegistry cannot miss a name the code books.
const OperatorPrefix = "context-guru."
OperatorPrefix is the host prefix of every operator name context-guru books. The accountant's metrics sink cuts it off, so the `component` label is the bare name.
const ( // SpendParamEstimated is "true" when the usage is our estimate, not a count the provider billed. It documents the // call on the wire only: accountant.Figure marks its figure estimated from the pricer, not from Params. SpendParamEstimated = "estimated" )
The Params keys of a spend operation the host prices from its own Call (tool deferral's search turns, #1441).
Variables ¶
This section is empty.
Functions ¶
func ContentChangeID ¶ added in v0.7.13
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 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). It goes down 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 (#1272). It never looks inside a string.
func Operator ¶ added in v0.7.9
Operator returns the booked operator name of name: the host prefix and the bare name.
func OperatorsOutsideRegistry ¶ added in v0.7.9
func OperatorsOutsideRegistry() []string
OperatorsOutsideRegistry lists every operator that books savings and is not a registered component. A metrics sink built with WithComponents(components.Names()...) pushes these as component="other"; accrec.MetricComponents adds them.
func TextChangeID ¶ added in v0.7.13
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 (textOf). It is not for wire JSON. Give that to ContentChangeID. The hash is the one these ChangeIDs had before this helper existed, so the ChangeIDs of old threads still match.
func WithoutCacheMarks ¶
WithoutCacheMarks is one element's JSON without its cache_control marks: the element's own, the ones on its content blocks, and the ones nested deeper in content, such as a mark inside a tool_result block's own content array (issue #917). A key named cache_control anywhere else is data, not a mark, and stays (see FindCacheMark). An element with none is returned as it is, byte for byte, so the common case costs no allocation. The result is only hashed. It is never sent anywhere.
It is not schema's dropCacheControl, on purpose. That one decodes and re-encodes a message, drops cache_control at every depth and sorts the keys, to compare two messages by meaning. This one keeps the wire bytes and removes only the marks the provider ignores (#1272), so the hash and the sizes stay those of the wire.
Types ¶
type Wire ¶
type Wire interface {
// Name is "messages" or "responses".
Name() string
// Body hashes a request body in the provider's order: tools, system, messages. A body without a
// conversation (or with invalid JSON) gives an empty Body.
Body(raw []byte) accountant.Body
// Parse is Body and Breakpoints on one parse of the body. count prices each thinking block the
// proxy saw a model make (accountant design D16); nil gives every block the fixed price. The
// hashes and sizes do not depend on it.
Parse(raw []byte, count accountant.OpaqueCount) (accountant.Body, []accountant.Breakpoint)
// Breakpoints lists the cache marks as positions in the same space as Body.
Breakpoints(raw []byte) []accountant.Breakpoint
// EnvelopeOps lists the operations that explain the tools and system sections' difference
// between the client body and the forwarded body. It is nil when either has no conversation.
EnvelopeOps(client, forwarded []byte) []accountant.Operation
// MessageOps explains a change to the messages that an edit made AFTER the pipeline, with the
// edit's own operator.
MessageOps(operator string, before, after []byte) []accountant.Operation
// WireIndex maps a normalized slot path, such as "messages.3.content.1.text" or
// "input.3.content.1.text", to the wire position it sits in. ok is false when the path names
// no position of this wire.
WireIndex(slotPath string) (index int, ok bool)
}
Wire is the accountant's reader for one provider wire.