@sema-agent/core
Version:
Stateless, task-oriented AI agent core
45 lines (44 loc) • 1.46 kB
JavaScript
export class ToolDisclosureSeat {
#pending = [];
#sink;
#last;
publish(snapshot) {
if (this.#last !== undefined && sameDisclosure(this.#last, snapshot))
return;
this.#last = snapshot;
if (this.#sink !== undefined)
this.#sink(snapshot);
else {
this.#pending.push(snapshot);
if (this.#pending.length > 64)
this.#pending.shift();
}
}
subscribe(sink) {
this.#sink = sink;
const pending = this.#pending;
this.#pending = [];
for (const s of pending)
sink(s);
}
unsubscribe() {
this.#sink = undefined;
}
}
function sameDisclosure(a, b) {
const sameList = (x, y) => x.length === y.length && x.every((n, i) => n === y[i]);
return a.policy === b.policy && a.thresholdPercent === b.thresholdPercent && sameList(a.deferred, b.deferred) && sameList(a.activated, b.activated);
}
export function createDeferralCensus(deferred, policy) {
const seat = new ToolDisclosureSeat();
const names = Object.freeze([...deferred].sort());
return {
seat,
publish: (active) => seat.publish({
policy: policy.policy,
...(policy.thresholdPercent !== undefined ? { thresholdPercent: policy.thresholdPercent } : {}),
deferred: names,
activated: [...active].filter((n) => deferred.has(n)).sort(),
}),
};
}