UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

57 lines (56 loc) • 4.57 kB
/** * The meter: the one door every real model request of a run passes before it is sent. It sits between the model lane and * the deployment Brain — under the lane's fallback walk, so each hop of a walk is admitted on its own, at its own model's * price — and asks the run's spend account (`core/spend-account.ts`) for room: estimated input plus the output ceiling the * request will carry. Admitted, the request goes out carrying the output ceiling it reserved (narrowed when the account had * less, marked `maxTokensDynamic`); refused, nothing is sent and the request is answered with an engine-minted error the * lane does not walk past. When the provider's terminal arrives the reservation is settled with what it reported; a request * whose usage never arrived settles at its whole reservation. Either way the one judge of whether the call has a cost figure * (`callPriced`) marks one that has none, so every run above it that a money ceiling holds reads it. * * A request whose output ceiling the account narrowed and that used all of it is the budget's stop, not the model's: its * terminal is replaced by the same engine error (the text it produced is kept, its tool calls are not), so neither the model * nor a re-ask is asked to finish under a ceiling that cannot hold the answer. * * Estimating input: characters / 4 of what the request presents (system prompt, messages, tool definitions; an image counts * a fixed amount), scaled by what the last settled request of the same model actually cost per estimated token when that is * more — never below characters / 4. The figure is the account node's; a run starts from the figure of the run that started it. */ import { type Context } from "../internal/llm.js"; import type { Brain } from "../core/types.js"; import { type ModelPricing } from "../core/pricing.js"; import { type SpendAccount, type SpendStop } from "../core/spend-account.js"; /** What a run hands the meter: its account node and the deployment's price tables. */ export interface SpendMeter { readonly account: SpendAccount; readonly pricing: () => Record<string, ModelPricing> | undefined; } /** The diagnostic type the meter's own refusal carries (it survives a copy of the message). */ export declare const SPEND_REFUSED_DIAGNOSTIC_TYPE = "spend_refused"; /** The terminal code of a run that ended on a refusal because the run its work belongs to had already ended (its spend * account was closed): not a ceiling — raising a limit does not help; the work has to be started again under a live run. */ export declare const SPEND_ACCOUNT_CLOSED_CODE = "spend.account_closed"; /** Whether `msg` is the meter's own refusal (the lane does not walk past it; the run ends on the ceiling it names). */ export declare function isSpendRefusal(msg: unknown): boolean; /** The reason of the meter's own refusal carried by `msg` (`ceiling` / `closed`), or `undefined` when `msg` is not one. */ export declare function spendRefusalReasonOf(msg: unknown): SpendStop["reason"] | undefined; /** * The meter's own refusal carried by `msg`: the stop it names, and whether the request reached a provider (`sent: false` — the * account refused it before it was sent, so it produced nothing and is no turn of the run; `sent: true` — a request whose * narrowed output ceiling it used up, its text kept). `undefined` when `msg` is not one. */ export declare function spendRefusalOf(msg: unknown): { readonly stop: SpendStop; readonly sent: boolean; } | undefined; /** The sentence a stopped request's error carries: one the account refused (`sent` false — nothing went out), or one that went * out with an output ceiling the account narrowed and used all of it (`sent` true — the text it produced is kept). */ export declare function spendStopSentence(stop: SpendStop, sent?: boolean): string; /** Characters a request presents: system prompt, messages, tool definitions. A mounted card's other forms (each tool object's * own `descriptionVariants`, read at mount for the description the face carries) ride the context's tool objects but no * adapter sends them, so they are not counted — the member of the tool object only (a tool parameter of that name is a * parameter). The tool objects' other members no adapter sends are still counted: the estimate stays above what is sent. */ export declare function requestCharsOf(context: Pick<Context, "systemPrompt" | "messages"> & { tools?: Context["tools"]; }): number; export declare function meteredBrain(inner: Brain, meter: SpendMeter): Brain;