usage

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package usage gives a caller a per-session running total of provider.Usage. A caller calls Record once per completed model call and reads the accumulated total for that session at any time. usage adds no gate and no policy; it only counts.

Map: accumulator.go = Accumulator, New, Record, Total, Reset, and the sentinel error ErrBlankSessionID. Rationale: ../docs/plans/usage.md. Contribution rules: ../AGENTS.md.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBlankSessionID is Record's and Reset's error when sessionID is
	// blank after trimming, and WrapCompleter's construction error for
	// the same cause.
	ErrBlankSessionID = errors.New("usage: sessionID must not be blank")
	// ErrNilAccumulator is WrapCompleter's construction error for a
	// nil Accumulator.
	ErrNilAccumulator = errors.New("usage: accumulator must not be nil")
	// ErrNilCompleter is WrapCompleter's construction error for a nil
	// Completer.
	ErrNilCompleter = errors.New("usage: completer must not be nil")
)

The sentinels below cover Record's, Reset's, and WrapCompleter's rejection causes, checked with errors.Is.

Functions

func WrapCompleter

func WrapCompleter(sessionID string, a *Accumulator, c provider.Completer) (provider.Completer, error)

WrapCompleter returns a provider.Completer that records every completed Chat turn's usage under sessionID in a. The wrapper keeps the inner Completer's name and messages. A blank sessionID, a nil Accumulator, or a nil Completer fails construction, so counts are never silently dropped. A turn that errors records nothing; a streamed turn records nothing, matching ChatStream's passthrough.

Types

type Accumulator

type Accumulator struct {
	// contains filtered or unexported fields
}

Accumulator holds one running provider.Usage total per session identifier, guarded for concurrent access. Its fields stay unexported; a caller reaches the state only through Record, Total, and Reset. New is the constructor; the zero value also works, because Record initializes the map on first call.

func New

func New() *Accumulator

New creates an empty Accumulator ready to record.

func (*Accumulator) Record

func (a *Accumulator) Record(sessionID string, u provider.Usage) error

Record adds u's four fields onto the running total keyed by sessionID. Rejects a blank sessionID (empty after strings.TrimSpace) with ErrBlankSessionID, wrapped. Creates the session's total on its first Record call; every later call for the same sessionID adds onto the existing total. Safe to call from more than one goroutine for the same or different sessionID values.

func (*Accumulator) Reset

func (a *Accumulator) Reset(sessionID string) error

Reset clears sessionID's total back to zero, as if no Record call had ever named it. Rejects a blank sessionID (empty after strings.TrimSpace) with ErrBlankSessionID, wrapped. Reset on a sessionID with no prior Record call is a no-op that returns nil, not an error.

func (*Accumulator) Total

func (a *Accumulator) Total(sessionID string) (provider.Usage, bool)

Total returns the current summed provider.Usage for sessionID and true, or the zero provider.Usage and false when no Record call has ever named that sessionID.

Jump to

Keyboard shortcuts

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