@sema-agent/core
Version:
Stateless, task-oriented AI agent core
64 lines (63 loc) • 3.22 kB
JavaScript
import { readCallerSpec } from "../task-spec-members.js";
import { receivePeerDeclarations, receivedDeclarations, received, receiving } from "../locked-config.js";
import { heldDeclarationsOf, snapshotDefinitionLists } from "../../agents/agent-definition.js";
import { receiveGovernanceBaseline } from "../../orchestration/governance-baseline-validity.js";
import { peerEntriesTakenIn } from "../secret-env.js";
import { snapshotCallerRoster } from "./caller-roster.js";
const guarded = (read) => {
try {
return read();
}
catch {
return undefined;
}
};
function receiveHeld(read, receipt) {
const held = guarded(read);
if ((typeof held === "object" || typeof held === "function") && held !== null)
guarded(() => receipt(held));
}
export function receiveHeldDeclarations(held) {
const definitions = guarded(() => {
const listed = held.agents;
return Array.isArray(listed) ? listed : undefined;
});
if (definitions !== undefined) {
const count = guarded(() => definitions.length);
for (let i = 0; typeof count === "number" && i < count; i++)
receiveHeld(() => definitions[i], (definition) => snapshotDefinitionLists(definition));
}
receiveHeld(() => held.workflowGovernanceBaseline, (baseline) => receiveGovernanceBaseline(baseline));
}
export function receiveLeg(spec, deps) {
const { value, entries } = peerEntriesTakenIn(() => {
receiveHeldDeclarations(deps);
const taken = readCallerSpec(spec).spec;
const peers = receivePeerDeclarations(taken, deps);
const roster = receiving(() => snapshotCallerRoster(taken));
const listed = taken.agents;
const agents = listed === undefined ? undefined : receiving(() => listed.map((definition) => receiving(() => snapshotDefinitionLists(definition))));
return { ...peers, roster, ...(agents !== undefined ? { agents } : {}) };
});
return { ...value, peerEntries: entries };
}
export function receiptWith(receipt, members) {
if (!("read" in receipt.roster))
return receipt;
const read = receipt.roster.read;
const laid = {};
for (const [key, value] of Object.entries(members))
laid[key] = { value, enumerable: value !== undefined, writable: false, configurable: false };
const spec = Object.create(Object.getPrototypeOf(read.spec), { ...Object.getOwnPropertyDescriptors(read.spec), ...laid });
return { ...receipt, roster: { read: { ...read, spec } } };
}
export function receiptAtPrepare(taken, spec, deps) {
const receipt = taken ?? receiveLeg(spec, deps);
return { receipt, callerRoster: received(receipt.roster) };
}
const readCopies = (list) => (list === undefined || "refused" in list ? [] : list.read.flatMap((entry) => ("read" in entry ? [entry.read] : [])));
export function legDeclarations(receipt) {
const roster = "read" in receipt.roster ? receipt.roster.read : undefined;
const tools = roster === undefined ? [] : [...(roster.roster.handed ?? []), ...(Array.isArray(roster.spec.tools) ? roster.spec.tools : [])];
return heldDeclarationsOf({ ...receivedDeclarations(receipt), agents: readCopies(receipt.agents), tools });
}