accwire

package
v0.7.8 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 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 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

This section is empty.

Variables

This section is empty.

Functions

func FindCacheMark

func FindCacheMark(v gjson.Result, prefix string) string

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 WithoutCacheMarks

func WithoutCacheMarks(body []byte, v gjson.Result) []byte

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.

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.

var (
	// Messages reads Anthropic Messages and Chat Completions bodies.
	Messages Wire = messages
	// Responses reads OpenAI Responses bodies.
	Responses Wire = responses
)

func For

func For(raw []byte) Wire

For chooses the wire of a request body, once. A body with no messages array and an input (an array of items, or one string) is Responses. Every other body is Messages, which gives an empty Body when there is no conversation.

Jump to

Keyboard shortcuts

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