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 ¶
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 (*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.