@sema-agent/core
Version:
Stateless, task-oriented AI agent core
28 lines (27 loc) • 1.18 kB
JavaScript
import { parentLegIds } from "../engine-notice.js";
const COUNTS = new WeakMap();
export function recordStationRaise(bus, event, outcomes) {
const dispatched = outcomes.filter((o) => o !== undefined);
if (dispatched.length === 0)
return;
let counts = COUNTS.get(bus);
if (counts === undefined)
COUNTS.set(bus, (counts = new Map()));
const row = counts.get(event) ?? { raised: 0, handlerFailures: 0 };
row.raised += 1;
for (const o of dispatched)
row.handlerFailures += o.skipped.length;
counts.set(event, row);
}
const sealed = (members) => Object.freeze(Object.assign(Object.create(null), members));
export function stationCountOf(bus, event) {
const row = COUNTS.get(bus)?.get(event);
return sealed({ raised: row?.raised ?? 0, handlerFailures: row?.handlerFailures ?? 0 });
}
export function delegationTerminalReceipt(bus, internals) {
const parent = parentLegIds(internals);
return {
stations: sealed({ start: stationCountOf(bus, "classic.SubagentStart"), stop: stationCountOf(bus, "classic.SubagentStop") }),
origin: parent === undefined ? "no_parent" : sealed({ ...parent }),
};
}