@sema-agent/core
Version:
Stateless, task-oriented AI agent core
67 lines (66 loc) • 3.49 kB
JavaScript
import { spendNodeMint } from "../core/spend-account.js";
import { createCumulativeStatsTracker } from "./cumulative-stats.js";
export function legSpendOf(stats) {
const t = createCumulativeStatsTracker();
t.add(stats);
const own = t.spend();
const nestedCost = stats.nested === undefined ? 0 : stats.nested.costMicroUsd;
return { tokens: own.tokens + (stats.nested?.tokens ?? 0), turns: own.turns, costMicroUsd: own.costMicroUsd === undefined || nestedCost === undefined ? undefined : own.costMicroUsd + nestedCost };
}
const ceilingOf = (v) => (v !== undefined && Number.isFinite(v) ? v : undefined);
const known = (v) => v !== undefined && Number.isFinite(v) && v >= 0;
export function compositionAccount(spec, parent) {
const suspendable = spec.resourceSuspend !== undefined;
const tokens = ceilingOf(suspendable ? spec.resourceSuspend?.totalTokens : spec.limits?.maxTokens);
const costUsd = ceilingOf(suspendable ? spec.resourceSuspend?.totalBudgetUsd : spec.limits?.maxCostUsd);
if (tokens === undefined && costUsd === undefined)
return undefined;
const account = spendNodeMint(parent, "RunInternals.spendParent")();
account.setCeiling({ ...(tokens !== undefined ? { tokens } : {}), ...(costUsd !== undefined ? { costMicroUsd: Math.round(costUsd * 1e6) } : {}) });
return { spendParent: { seat: account, link: "task" }, account, unreadable: { tokens: false, cost: false } };
}
export function onComposition(internals, c) {
return c === undefined ? internals : { ...internals, spendParent: c.spendParent };
}
export function chargeFirstLeg(c, first) {
if (c === undefined)
return;
const { account } = c;
const seen = account.subtreeSettled;
const { tokens, costMicroUsd } = account.ceilings;
c.unreadable.tokens = tokens !== undefined && !known(first.tokens);
c.unreadable.cost = costMicroUsd !== undefined && !known(first.costMicroUsd);
account.setCeiling({
...(tokens !== undefined ? { tokens: c.unreadable.tokens ? 0 : Math.max(0, tokens - Math.max(0, first.tokens - seen.tokens)) } : {}),
...(costMicroUsd !== undefined ? { costMicroUsd: c.unreadable.cost ? 0 : Math.max(0, costMicroUsd - Math.max(0, (first.costMicroUsd ?? 0) - seen.costMicroUsd)) } : {}),
});
}
export function compositionExhausted(c) {
if (c === undefined)
return undefined;
const { account, unreadable } = c;
if (account.ceilings.tokens !== undefined) {
if (unreadable.tokens)
return { axis: "tokens" };
if ((account.remaining("tokens") ?? 0) <= 0)
return { axis: "tokens", remaining: 0 };
}
if (account.ceilings.costMicroUsd !== undefined) {
if (unreadable.cost || account.nested()?.anyUnpriced === true)
return { axis: "cost" };
if ((account.remaining("cost") ?? 0) <= 0)
return { axis: "cost", remaining: 0 };
}
return undefined;
}
export function laterLegTurns(spec, turnsSpent) {
const turnCeiling = spec.resourceSuspend !== undefined || spec.limits?.maxTurns === 0 ? undefined : ceilingOf(spec.limits?.maxTurns);
if (turnCeiling === undefined)
return { spec };
if (!known(turnsSpent))
return { exhausted: { axis: "turns" } };
const turnsLeft = turnCeiling - turnsSpent;
if (turnsLeft <= 0)
return { exhausted: { axis: "turns", remaining: 0 } };
return { spec: { ...spec, limits: { ...spec.limits, maxTurns: turnsLeft } } };
}