@sema-agent/core
Version:
Stateless, task-oriented AI agent core
132 lines (131 loc) • 6.15 kB
JavaScript
import { cacheHitOperandsOf } from "../core/runner/cache-hit-account.js";
const USAGE_SUM_KEYS = ["promptTokens", "totalInputTokens", "cachedTokens", "outputTokens", "cacheWriteTokens", "cacheWriteTokensLong", "apiDurationMs", "fastSpeedCalls"];
export function emptyCumulativeUsageFacts() {
return { published: new Set(), usageMissing: false, cacheReadUnreported: false, cacheWriteUnreported: false, cacheHitRateExcludes: new Map(), rateNumeratorShort: false };
}
export function foldUsageFacts(facts, s) {
for (const k of USAGE_SUM_KEYS)
if (typeof s[k] === "number")
facts.published.add(k);
if (typeof s.totalInputTokens === "number" && typeof s.cachedTokens !== "number")
facts.rateNumeratorShort = true;
if (s.usageMissing === true)
facts.usageMissing = true;
if (s.cacheReadUnreported === true)
facts.cacheReadUnreported = true;
if (s.cacheWriteUnreported === true)
facts.cacheWriteUnreported = true;
for (const e of s.cacheHitRateExcludes ?? []) {
const merged = facts.cacheHitRateExcludes.get(e.lane) ?? { cachedTokens: 0, totalInputTokens: 0 };
merged.cachedTokens += e.cachedTokens;
merged.totalInputTokens += e.totalInputTokens;
facts.cacheHitRateExcludes.set(e.lane, merged);
}
}
export function buildCumulativeStats(base, acc) {
const facts = acc.usageFacts;
const sum = (k) => (facts.published.has(k) ? acc[k] : undefined);
const excludes = [...facts.cacheHitRateExcludes].map(([lane, e]) => ({ lane, cachedTokens: e.cachedTokens, totalInputTokens: e.totalInputTokens }));
const operands = cacheHitOperandsOf(acc, excludes);
return {
...base,
tokens: acc.tokens,
turns: acc.turns,
promptTokens: sum("promptTokens"),
totalInputTokens: sum("totalInputTokens"),
cachedTokens: sum("cachedTokens"),
outputTokens: sum("outputTokens"),
cacheWriteTokens: sum("cacheWriteTokens"),
cacheWriteTokensLong: sum("cacheWriteTokensLong"),
apiDurationMs: sum("apiDurationMs"),
fastSpeedCalls: sum("fastSpeedCalls"),
cacheHitRate: !facts.rateNumeratorShort && operands.totalInputTokens > 0 ? Math.min(1, operands.cachedTokens / operands.totalInputTokens) : undefined,
cacheHitRateExcludes: excludes.length > 0 ? excludes : undefined,
usageMissing: facts.usageMissing ? true : undefined,
cacheReadUnreported: facts.cacheReadUnreported ? true : undefined,
cacheWriteUnreported: facts.cacheWriteUnreported ? true : undefined,
costMicroUsd: acc.anyLegUnpriced ? undefined : acc.costMicroUsd,
nested: acc.nested.tokens > 0 || acc.nested.tasks > 0
? { tokens: acc.nested.tokens, turns: acc.nested.turns, tasks: acc.nested.tasks, costMicroUsd: acc.nested.costMicroUsd || undefined }
: undefined,
costBreakdown: acc.anyLegUnpriced
? undefined
: {
llmRootMicroUsd: acc.llmRootMicroUsd,
nestedSubagentMicroUsd: acc.nested.costMicroUsd,
compactionMicroUsd: acc.compactionMicroUsd,
...(acc.sideCallMicroUsd !== undefined ? { sideCallMicroUsd: acc.sideCallMicroUsd } : {}),
},
humanReview: acc.humanReview.kept
? { count: acc.humanReview.count, totalWaitMs: acc.humanReview.totalWaitMs, gates: acc.humanReview.gates }
: undefined,
};
}
export function createCumulativeStatsTracker() {
let legs = 0;
const acc = {
tokens: 0,
turns: 0,
promptTokens: 0,
totalInputTokens: 0,
cachedTokens: 0,
outputTokens: 0,
cacheWriteTokens: 0,
cacheWriteTokensLong: 0,
apiDurationMs: 0,
fastSpeedCalls: 0,
costMicroUsd: 0,
anyLegUnpriced: false,
llmRootMicroUsd: 0,
compactionMicroUsd: 0,
nested: { tokens: 0, turns: 0, tasks: 0, costMicroUsd: 0 },
humanReview: { kept: false, count: 0, totalWaitMs: 0, gates: [] },
usageFacts: emptyCumulativeUsageFacts(),
};
return {
get legs() {
return legs;
},
add(s) {
legs += 1;
acc.tokens += s.tokens;
acc.turns += s.turns;
acc.promptTokens += s.promptTokens ?? 0;
acc.totalInputTokens += s.totalInputTokens ?? 0;
acc.cachedTokens += s.cachedTokens ?? 0;
acc.outputTokens += s.outputTokens ?? 0;
acc.cacheWriteTokens += s.cacheWriteTokens ?? 0;
acc.cacheWriteTokensLong += s.cacheWriteTokensLong ?? 0;
acc.apiDurationMs += s.apiDurationMs ?? 0;
acc.fastSpeedCalls += s.fastSpeedCalls ?? 0;
foldUsageFacts(acc.usageFacts, s);
acc.costMicroUsd += s.costMicroUsd ?? 0;
if (s.costMicroUsd === undefined)
acc.anyLegUnpriced = true;
if (s.costBreakdown) {
acc.llmRootMicroUsd += s.costBreakdown.llmRootMicroUsd;
acc.compactionMicroUsd += s.costBreakdown.compactionMicroUsd;
if (s.costBreakdown.sideCallMicroUsd !== undefined)
acc.sideCallMicroUsd = (acc.sideCallMicroUsd ?? 0) + s.costBreakdown.sideCallMicroUsd;
}
if (s.humanReview) {
acc.humanReview.kept = true;
acc.humanReview.count += s.humanReview.count;
acc.humanReview.totalWaitMs += s.humanReview.totalWaitMs;
acc.humanReview.gates.push(...s.humanReview.gates);
}
if (s.nested) {
acc.nested.tokens += s.nested.tokens;
acc.nested.turns += s.nested.turns;
acc.nested.tasks += s.nested.tasks;
acc.nested.costMicroUsd += s.nested.costMicroUsd ?? 0;
}
},
build(base) {
return buildCumulativeStats(base, acc);
},
spend() {
return { costMicroUsd: acc.anyLegUnpriced ? undefined : acc.costMicroUsd, tokens: acc.tokens, turns: acc.turns };
},
};
}