@sema-agent/core
Version:
Stateless, task-oriented AI agent core
62 lines (61 loc) • 4.26 kB
TypeScript
/**
* The engine's account LINE and its one reading. A host that keeps a session's spend (a result frame's total, a `/cost`
* view, a server's session aggregate) keeps the engine-priced lines it is handed — one per finished run, one per side query — and
* reads them back through {@link foldAccount}; no host re-sums, re-prices, or decides for itself what an unpriced line means.
*
* What the engine owns: the pricing (`priceReply`, the arithmetic every run lane already uses) and the line's shape. What it
* does not own: where lines are kept — persistence is the host's, so "a resumed session includes its history" is the fold of
* the history lines the host supplies plus this run's line.
*/
import type { TaskResult } from "./task-result.js";
/** Who caused a line of the account: a run (the user's turns and everything the run's own agent caused — its compaction,
* classifier, mods, advisor and delegated sub-agents are INSIDE the run's line) or a side query (`Runner.sideQuery`, the one-shot
* utility call that belongs to no run). A closed set: the fold's `byOrigin` is total over it by its type. */
export declare const ACCOUNT_ORIGINS: readonly ["run", "side_query"];
export type AccountOrigin = (typeof ACCOUNT_ORIGINS)[number];
/** One priced line. `costMicroUsd` is integer micro-USD and ABSENT when any spend in the line was unpriced — unknown, never a
* fabricated 0 (the run's `stats.costMicroUsd` law); `usageMissing` marks figures that are a lower bound because a reply
* reported no usage. */
export interface AccountRow {
origin: AccountOrigin;
tokens: number;
costMicroUsd?: number;
usageMissing?: true;
}
/**
* The line a finished run contributes: its own usage plus its delegated sub-agents' (`stats.nested`) — the fully reconciled
* spend of the run. Unknown stays unknown: a run whose own or delegated spend was unpriced yields a line without a cost.
*
* The line is exactly what the result's `stats` says, so it is exact for a run that did not durably resume and that no
* background delegate outlived. `stats` itself is not one interval across a durable resume chain — a leg's own usage is per leg
* (a `resource_limit` chain's folds the earlier slices in), its delegated usage is cumulative over the chain, and an unknown
* mark does not survive the chain or the delegation rollup — so the lines of ONE chain are not additive, and spend a background
* delegate books after the result settled is in no line. Spend the engine books after the result — the prompt-suggestion pass,
* memory consolidation — is on its own face. The reading that has none of these gaps is an account the engine keeps, minted where
* each interval is known.
*/
export declare function accountRowOfResult(result: {
readonly stats: Pick<TaskResult["stats"], "tokens" | "costMicroUsd" | "usageMissing" | "nested">;
}): AccountRow;
/** A fold of lines: how many, their tokens, their cost (absent as soon as any one line's cost is unknown), and whether any
* figure is a lower bound. An empty fold is a known zero. */
export interface AccountTotal {
rows: number;
tokens: number;
costMicroUsd?: number;
usageMissing?: true;
}
/** The session's account: the total over every line, and the same fold per origin. */
export interface SessionAccount extends AccountTotal {
byOrigin: Record<AccountOrigin, AccountTotal>;
}
/**
* THE reading of a session's account: fold the lines a host holds — history lines it persisted, this run's line, side-query lines —
* into one total and one total per origin. A line whose cost is unknown makes every total that contains it unknown (cost
* absent), because a sum that skipped it would present a lower bound as the whole. A malformed line (origin outside the closed set,
* a negative or non-finite number, a cost spelled as anything but an absent key) is refused loudly rather than summed.
*
* Which total a field shows is the host's mapping, stated once so the hosts agree: a run result frame's `total_cost_usd` is
* `byOrigin.run` (the run lines, history included, side queries not in it); a session's cumulative figure is the whole fold.
*/
export declare function foldAccount(rows: Iterable<AccountRow>): SessionAccount;