adapter

package
v1.0.0-alpha.13 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 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 adapter holds what every agent adapter hands the wrapper.

Index

Constants

View Source
const (
	GradeGatewayPrecheck = "gateway-precheck"
	GradeAgentFlag       = "agent-flag"
	GradeWrapperWatchdog = "wrapper-watchdog"
	GradeNone            = "none"
)

Enforcement grades.

View Source
const UnitAICredits = "AIC"

UnitAICredits is the unit of gh-aw's AI credits.

Variables

View Source
var ErrClaimed = errors.New("another costroid run holds the agent's state directory")

ErrClaimed means another run holds the directory an agent keeps its state in.

Functions

This section is empty.

Types

type Agent

type Agent interface {
	// Name is the costroid/agent tag.
	Name() string
	// Plan rewrites a command to run under the budget, or refuses it.
	Plan(argv []string, budget decimal.Decimal) (Plan, error)
	// NewSession starts reading a run of the planned command.
	NewSession(plan Plan) Session
}

Agent is one supported agent.

type AgentError

type AgentError string

AgentError is the class of error a run's agent ended in. The class is read from the agent's own fields, never from its text. The values are a closed list; a class the agent states that is not on it reads as unknown.

const (
	AgentErrorAuthentication AgentError = "authentication_failed"
	AgentErrorBilling        AgentError = "billing_error"
	AgentErrorRateLimit      AgentError = "rate_limit"
	AgentErrorInvalidRequest AgentError = "invalid_request"
	AgentErrorServer         AgentError = "server_error"
	AgentErrorUnknown        AgentError = "unknown"
)

The classes an agent's run can end in. Empty means it ended in none.

func ParseAgentError

func ParseAgentError(stated string) AgentError

ParseAgentError maps the class an agent states onto the closed list.

type Count

type Count struct {
	Class  focus.TokenClass
	Tokens int64
}

Count is one class and its tokens.

type Enforcement

type Enforcement struct {
	Grade    string
	Note     string
	Watchdog bool
}

Enforcement is the grade a run was held at. Note states the limit of the grade in words and travels on every row.

type Exclusive

type Exclusive interface {
	// Claim holds the directory for one run, and release ends the hold. It
	// fails with ErrClaimed while another run holds it.
	Claim() (release func(), err error)
}

Exclusive is an Agent whose runs cannot share one state directory, because a run reads the directory and would count another run's spend as its own.

type GatewayBilled

type GatewayBilled interface {
	BilledThroughGateway()
}

GatewayBilled is a session that reads its cost basis from the credential its agent starts with. A run through a gateway starts the agent on the run's key, which the gateway bills per token upstream, and says so.

type Gatewayed

type Gatewayed interface {
	// GatewayEnv is the environment, as NAME=value entries, that points the
	// agent at the gateway's base URL with the key as its credential. An entry
	// with no value names a credential the agent must not hold beside the
	// key, and the run removes it.
	GatewayEnv(base, key string) []string
	// GatewayArgs is what the agent's command line needs to reach the gateway
	// at base. The run places it after every option the command already
	// holds, so it overrides any value the command or a configuration file
	// sets.
	GatewayArgs(base string) []string
}

Gatewayed is an agent that can send every request through a gateway on a key the run holds. A run refuses a gateway key for an agent that is not.

type Grain

type Grain string

Grain says what one Request covers.

const (
	// PerRequest is one model request.
	PerRequest Grain = "request"
	// PerTurn is one agent turn, which may hold many model requests.
	PerTurn Grain = "turn"
	// PerRun is one model's total across a run.
	PerRun Grain = "run"
)

The grains an agent can report at.

type Plan

type Plan struct {
	Argv        []string
	Enforcement Enforcement
}

Plan is how one agent command runs under a budget.

type Report

type Report struct {
	SessionID    string
	AgentVersion string
	CostBasis    focus.CostBasis
	CostSource   string
	Requests     []Request
	Warnings     []string
	// AgentError is the class of error the agent ended the run in. Empty means
	// the run did not end in one.
	AgentError AgentError
	// Enforcement is the grade the run earned, when it differs from what the
	// plan could promise. Empty means the plan's grade held.
	Enforcement Enforcement
	// Nested holds the sessions of other agent processes that started and
	// ended inside the run, each under its own session id. Their requests are
	// never in Requests.
	Nested []Report
	// Unrecorded counts the model requests the run sent whose usage no source
	// reported, so no row holds them. A warning says which.
	Unrecorded int
	// AgentStated is the agent's own figure for the run, in the agent's own
	// unit, nil when it states none. No row holds it, and it is never
	// converted or added to an amount.
	AgentStated *Stated
}

Report is what a run's usage came to.

type Request

type Request struct {
	// ID is the provider's request id, or the adapter's stated stand-in when
	// the source has none.
	ID          string
	Provider    string
	Model       string
	Start, End  time.Time
	ServiceTier string
	Region      string
	// ContextInputTokens is the request's whole input. At a coarser grain it
	// is the whole input of every request the entry sums, which no request in
	// it can pass, or zero when the adapter states none.
	ContextInputTokens int64
	Grain              Grain
	Tokens             Tokens
	// AssumedWrite is the cache write class that took tokens whose lifetime the
	// source did not report, by the adapter's rule. Empty when it reported all.
	AssumedWrite focus.TokenClass
	// CostSource names where this entry came from when it differs from the
	// report's. Empty means the report's.
	CostSource string
	// Note says in words how this entry was read, when the report's own
	// source does not.
	Note string
}

Request is one unit of usage at the finest grain the source reports.

type Session

type Session interface {
	// Feed reads one line of the agent's stdout, seen at the given time.
	Feed(line []byte, at time.Time) error
	// Finish reads whatever the agent left behind and returns the run's usage.
	Finish() (Report, error)
}

Session reads one run.

type Stated

type Stated struct {
	// Unit is the agent's own unit, such as UnitAICredits.
	Unit string
	// Quantity is a non-negative decimal string, never a JSON number.
	Quantity string
	// Source names the agent that stated it.
	Source string
}

Stated is a figure an agent states about its own run, as it states it.

type Tailer

type Tailer interface {
	// Tail follows the file until ctx ends. It calls settled with the run's
	// requests so far each time a new one settles while the agent is working.
	Tail(ctx context.Context, settled func([]Request))
}

Tailer is a Session that watches a file the agent writes during a run.

type Tokens

type Tokens struct {
	InputUncached       int64
	CacheRead           int64
	CacheWrite5m        int64
	CacheWrite1h        int64
	CacheWrite30m       int64
	Output              int64
	ReasoningOutput     int64
	CachedInputReported int64
}

Tokens holds the canonical classes. Billed classes are disjoint; the two informational classes repeat tokens a billed class already counts.

func (Tokens) Add

func (t Tokens) Add(other Tokens) Tokens

Add returns the class-by-class sum.

func (Tokens) Check

func (t Tokens) Check() error

Check refuses a negative class. No source counts fewer than no tokens, and a negative count would price as a negative cost.

func (Tokens) Counts

func (t Tokens) Counts() []Count

Counts lists the non-zero classes in a fixed order.

Directories

Path Synopsis
Package claudecode reads Claude Code runs.
Package claudecode reads Claude Code runs.
Package codex reads `codex exec --json` runs.
Package codex reads `codex exec --json` runs.
Package ghaw reads what a GitHub Agentic Workflows (gh-aw) run spent, from the usage artifact its conclusion job uploads.
Package ghaw reads what a GitHub Agentic Workflows (gh-aw) run spent, from the usage artifact its conclusion job uploads.

Jump to

Keyboard shortcuts

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