UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

65 lines (64 loc) • 4.65 kB
/** * The ONE reading of what a provider's usage frames stated, for the three first-party wire adapters: each adapter merges its own * frames and maps its own spellings onto a {@link UsageStatement}; what a statement MEANS is decided here, once. * * The law, member by member: * - a member is STATED when a frame carried a count for it; `undefined` / `null` state nothing. A member that holds something that * is no count (a string, a fraction, a negative) is a statement nobody can read: the usage is unknown (`missing`) and the member * reads as not stated; * - `input` and `output` are the necessary operands of every total: either one not stated makes the usage `missing` (its numbers * are then a lower bound, as everywhere a usage is `usageMissing`) and reads as 0 in those numbers; * - `cacheRead` and `cacheWrite` are left ABSENT on the {@link Usage} when not stated — absent is "not reported", never 0 (the * same law `Usage.reasoning` follows). A stated 0 is a measured 0 and is kept. * - `cacheWriteLong` is the part of `cacheWrite` the wire says was written at the one-hour lifetime: a SUBSET of it, absent when * the wire did not state the split, and read by the subset law `reasoning` follows — a count beyond the writes it is a part of * (or one with no write count beside it, a stated 0 included) is not recorded and the usage is unknown (`missing`). The write count stays the * total of both lifetimes; the cost lines price the two parts at their own rates. * * Merging is the adapter's business, and so is deciding what counts as a statement across frames (the Anthropic closing frame's * 0 for an input or a cache count says "unchanged", not "0", so the adapter does not take it). This reading only sees the * merged statement. * * How the wire counts its input against the cache is its {@link CacheFamily}: an `input-excludes-cached` wire (Anthropic) adds the * cache counts to its total and prices them at their own rates; an `input-includes-cached` wire (the OpenAI shapes) states its * own total when it has one and prices the cached part inside `input` (its cache cost lines are 0). */ import type { Model, Usage } from "../internal/llm.js"; import type { CacheFamily } from "../core/runner/usage-accounting.js"; import { type ReasoningReading } from "./usage-reasoning.js"; /** * Whether a call was SERVED at fast speed — the one judgment of a call's speed tier, read off what the response said * (`Usage.speed`, which only a stated `"fast"` sets). What the request asked for is not read: a request that asked for fast * speed and was answered at standard speed (an endpoint that did not serve it, a re-send at standard speed) is a standard * call. Every reader of a call's tier asks here: its price (`callRatesOf`), the count of fast calls, the turn's word. */ export declare function servedAtFastSpeed(usage: { readonly speed?: unknown; }): boolean; export declare function markRefusedBeforeAnyStream(usage: object): void; export declare function wasRefusedBeforeAnyStream(usage: unknown): boolean; /** What a provider's merged usage frames carried, per member, as it came (`undefined` = the member was not stated). */ export interface UsageStatement { readonly input?: unknown; readonly output?: unknown; readonly cacheRead?: unknown; readonly cacheWrite?: unknown; /** The part of `cacheWrite` the wire says was written at the one-hour lifetime; absent ⇒ the wire did not state the split. */ readonly cacheWriteLong?: unknown; /** The wire's own total, on a wire that states one (the OpenAI shapes); absent ⇒ the total is the sum of the stated parts. */ readonly total?: unknown; /** The speed tier the wire says served the response (Anthropic `usage.speed`); only `"fast"` is a statement — any other value * or none leaves `Usage.speed` absent (the reference merges it the same way, cli321.js:287627 "speed:n.speed??e.speed"). The * tier a response is priced at ({@link servedAtFastSpeed}). */ readonly speed?: unknown; } /** The reading of one statement: the usage to record, and whether it is unknown (`missing` — the adapter's `usageMissing`). */ export interface UsageReading { readonly usage: Usage; readonly missing: boolean; } /** * Read `statement` under the wire's cache family, at `model`'s declared price; `reasoning` is the wire's reasoning reading (its * `unreadable` makes the usage unknown, as it always has). */ export declare function usageOfStatement(model: Model, family: CacheFamily, statement: UsageStatement, reasoning: ReasoningReading): UsageReading;