usage

package
v0.0.199 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout bounds one provider's usage fetch.

View Source
const EndpointCommandCodeUsage = "/alpha/usage/summary"

EndpointCommandCodeUsage labels the composite Command Code usage fetch.

View Source
const EnvFixtureDir = "AGENT_PRO_USAGE_FIXTURE_DIR"

EnvFixtureDir points grok and codex fetches at JSON fixtures instead of their HTTP endpoints. Tests only; unset in normal runs.

View Source
const SchemaID = "agent-pro/usage-snapshot/v1"

SchemaID is the on-disk schema for one usage snapshot record.

View Source
const SnapshotSuffix = "-snapshot.jsonl"

SnapshotSuffix is the file name suffix for stored snapshots.

Variables

This section is empty.

Functions

This section is empty.

Types

type CollectOptions added in v0.0.199

type CollectOptions struct {
	// Now is the clock; nil means time.Now.
	Now func() time.Time
	// Timeout bounds each provider's usage fetch; 0 means DefaultTimeout.
	Timeout time.Duration
	// GrokHome is the grok home directory, used for both auth and sessions.
	GrokHome string
	// CodexHome is the codex home directory, used for both auth and sessions.
	CodexHome string
	// CommandCodeHome is the Command Code home directory.
	CommandCodeHome string
	// CommandCodeAPIURL overrides the Command Code API base URL; empty uses
	// COMMANDCODE_API_URL when COMMANDCODE_SANDBOX=true, else the default.
	CommandCodeAPIURL string
	// FixtureDir serves grok and codex usage from JSON files in this directory
	// instead of HTTP. Tests only.
	FixtureDir string
}

CollectOptions configures Collect. Home fields empty mean the provider default ($GROK_HOME or ~/.grok, $CODEX_HOME or ~/.codex, ~/.commandcode).

type FetchOptions added in v0.0.87

type FetchOptions struct {
	// GrokCommand overrides GROK_SHOW_USAGE_COMMAND.
	GrokCommand string
	// CodexCommand overrides CODEX_SHOW_STATUS_COMMAND.
	CodexCommand string
	// CodexSessionID overrides CODEX_SHOW_STATUS_SESSION_ID.
	CodexSessionID string
	// CodexTimeoutSeconds overrides CODEX_SHOW_STATUS_TIMEOUT when > 0.
	CodexTimeoutSeconds int
	// TTYWatchHome overrides TTY_WATCH_HOME (Codex).
	TTYWatchHome string
}

FetchOptions configures provider-specific hooks without process env mutation.

type FileFailure added in v0.0.199

type FileFailure struct {
	Path string
	Err  error
}

FileFailure is one snapshot file that could not be read, with the path so a caller can report or skip it.

type ListOptions added in v0.0.199

type ListOptions struct {
	// Last limits the records returned, newest first; 0 reads all of them.
	Last int
	// Since drops records collected before it. Day directories that cannot hold
	// an in-window record are skipped without being read. Zero reads the whole
	// tree.
	Since time.Time
}

ListOptions selects what ListFiltered reads.

type ProviderID

type ProviderID string

ProviderID identifies a usage fetch provider.

const (
	Grok  ProviderID = "grok"
	Codex ProviderID = "codex"
	// CommandCode is fetched over the Command Code HTTP API by Collect; the
	// TTY facade in FetchWithOptions does not serve it.
	CommandCode ProviderID = "commandcode"
)

func AllProviders added in v0.0.199

func AllProviders() []ProviderID

AllProviders is the provider set every collect covers, in output order.

type Record added in v0.0.199

type Record struct {
	Schema   string        `json:"schema"`
	TS       time.Time     `json:"ts"`
	Provider ProviderID    `json:"provider"`
	Usage    UsageBlock    `json:"usage"`
	Sessions SessionsBlock `json:"sessions"`
}

Record is one collected snapshot for a single provider: the account usage read over that provider's HTTP API plus the counts of the sessions the provider has on disk. Records are appended one per line to $AGENT_PRO_HOME/usages/<provider>/<date>/<time>-snapshot.jsonl.

func Collect added in v0.0.199

func Collect(ctx context.Context, opts CollectOptions) []Record

Collect fetches every provider's usage over its API and counts each provider's on-disk sessions, in parallel, returning one Record per provider in AllProviders order. A provider whose usage fetch fails still yields a Record: Usage.OK is false with the error, and Sessions is unaffected because it only reads local files.

func (Record) Headline added in v0.0.199

func (r Record) Headline() string

Headline renders the short human usage summary ("usage" display key).

func (Record) LocalTime added in v0.0.199

func (r Record) LocalTime() time.Time

LocalTime is the record timestamp in local time, used for file names.

func (Record) MarshalJSONLine added in v0.0.199

func (r Record) MarshalJSONLine() ([]byte, error)

MarshalJSONLine returns the record as compact single-line JSON, the form written to the store and printed by --json.

type SessionsBlock added in v0.0.199

type SessionsBlock struct {
	Total  int        `json:"total"`
	Oldest *time.Time `json:"oldest,omitempty"`
	Newest *time.Time `json:"newest,omitempty"`
	Error  string     `json:"error,omitempty"`
}

SessionsBlock counts the provider's sessions as found on disk. Oldest is the earliest session start and Newest the latest recorded activity; both are omitted when the provider has no readable sessions.

func CountCodexSessions added in v0.0.199

func CountCodexSessions(home string) (SessionsBlock, error)

CountCodexSessions counts codex rollout files under <home>/sessions. Oldest comes from the rollout file name (the session start, recorded in local time) and Newest from the file modification time.

func CountCommandCodeSessions added in v0.0.199

func CountCommandCodeSessions(home string) (SessionsBlock, error)

CountCommandCodeSessions counts Command Code transcripts under <home>/projects. Oldest is the earliest session timestamp in a transcript's first record and Newest the latest file modification time.

func CountGrokSessions added in v0.0.199

func CountGrokSessions(home string) (SessionsBlock, error)

CountGrokSessions counts grok sessions under <home>/sessions. Every session directory counts, including subagent and fork children. Oldest is the earliest summary created_at, Newest the latest summary updated_at.

func (*SessionsBlock) Observe added in v0.0.199

func (b *SessionsBlock) Observe(started, modified time.Time)

Observe folds one session into the totals. started may be zero when the session's start time is unknown; modified is used for the newest activity.

type Snapshot

type Snapshot struct {
	Provider     ProviderID
	UsagePercent string
	Reset        string
	CreditsUsed  string
	CreditsTotal string
}

Snapshot is the normalized usage payload returned by Fetch.

func Fetch

func Fetch(ctx context.Context, id ProviderID) (*Snapshot, error)

Fetch retrieves usage for the given provider in-process.

func FetchWithOptions added in v0.0.87

func FetchWithOptions(ctx context.Context, id ProviderID, opts FetchOptions) (*Snapshot, error)

FetchWithOptions retrieves usage with explicit provider hooks (parallel-safe tests).

type Store added in v0.0.199

type Store struct {
	Root string
}

Store writes and reads usage snapshots under a usages root, laid out as <root>/<provider>/<YYYY-MM-DD>/<HH-MM-SS>-snapshot.jsonl. Times in paths are local, so the tree is browsable with ls; record timestamps inside are UTC.

func NewStore added in v0.0.199

func NewStore(root string) *Store

NewStore returns a store rooted at root (typically $AGENT_PRO_HOME/usages).

func (*Store) Append added in v0.0.199

func (s *Store) Append(rec Record) (string, error)

Append writes one record and returns the file path it went to. A second snapshot within the same second appends another line to the same file, so runs are never overwritten.

func (*Store) DayDir added in v0.0.199

func (s *Store) DayDir(provider ProviderID, at time.Time) string

DayDir returns the directory holding one provider's snapshots for a day.

func (*Store) List added in v0.0.199

func (s *Store) List(last int) ([]StoredRecord, error)

List returns up to last stored records, newest first, across every provider. last <= 0 means all of them. Files are visited newest first and reading stops once enough records are collected, always on an instant boundary. A file that cannot be read fails the whole call.

func (*Store) ListFiltered added in v0.0.199

func (s *Store) ListFiltered(opts ListOptions) ([]StoredRecord, []FileFailure, error)

ListFiltered is List with a time bound, and it reports unreadable snapshot files instead of failing: a dashboard should chart the files it can read. Failures are in visit order, which is newest instant first.

type StoredRecord added in v0.0.199

type StoredRecord struct {
	Record Record
	Path   string
}

StoredRecord is a record read back from the store with its provenance.

type UsageBlock added in v0.0.199

type UsageBlock struct {
	OK         bool               `json:"ok"`
	Endpoint   string             `json:"endpoint,omitempty"`
	DurationMS int64              `json:"duration_ms"`
	Values     map[string]float64 `json:"values,omitempty"`
	Display    map[string]string  `json:"display,omitempty"`
	Error      string             `json:"error,omitempty"`
}

UsageBlock is one provider's account usage as returned by its API. Values holds the numbers worth charting; Display keeps the provider's own rendered strings (plan name, email, reset text) that do not belong in Values.

Directories

Path Synopsis
Package view serves a read-only dashboard of the usage snapshots written by `agent-pro usage collect`.
Package view serves a read-only dashboard of the usage snapshots written by `agent-pro usage collect`.

Jump to

Keyboard shortcuts

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