@sema-agent/core
Version:
Stateless, task-oriented AI agent core
86 lines (85 loc) • 3.5 kB
JavaScript
import { canAccess } from "./task-registry-shared.js";
import { stopWorkflowLane } from "./task-registry-workflow.js";
function stopUnyieldable(core, handle, source) {
core.markStopSource(handle.id, source);
handle.abort.abort();
}
export function requestYieldLane(core, handle, opts) {
if (handle.type === "workflow") {
if (handle.yieldIntent !== undefined) {
handle.yieldIntent.abort();
return undefined;
}
if (opts.stopUnyieldable !== undefined)
void stopWorkflowLane(handle);
return "no_park_lane";
}
const seat = handle.yieldSeat;
seat.requested = opts.stopUnyieldable !== undefined ? { stopUnyieldable: opts.stopUnyieldable } : {};
if (seat.lane !== "parkable") {
if (opts.stopUnyieldable !== undefined)
stopUnyieldable(core, handle, opts.stopUnyieldable);
return seat.lane;
}
if (seat.leg !== undefined && !seat.leg.ready) {
if (opts.stopUnyieldable !== undefined)
stopUnyieldable(core, handle, opts.stopUnyieldable);
else
seat.leg.controller.abort();
return "leg_not_ready";
}
seat.leg?.controller.abort();
return undefined;
}
export function yieldRequested(handle) {
return handle.type === "workflow" ? handle.yieldIntent?.signal.aborted === true : handle.yieldSeat.requested !== undefined;
}
export function attachYieldLegLane(core, id, controller, ready) {
const handle = core.handles.get(id);
if (handle === undefined || handle.type !== "background_agent")
return;
const seat = handle.yieldSeat;
seat.leg = { controller, ready };
const requested = seat.requested;
if (requested === undefined)
return;
if (!ready && requested.stopUnyieldable !== undefined)
stopUnyieldable(core, handle, requested.stopUnyieldable);
else
controller.abort();
}
export function yieldCovers(handle, access, opts) {
if (handle.status !== "running")
return false;
if (handle.type === "workflow")
return access === undefined && !(opts.skipSessionScoped === true && handle.sessionScoped === true);
if (handle.type !== "background_agent")
return false;
if (access !== undefined && !canAccess(handle, access))
return false;
return !(opts.skipSessionScoped === true && handle.sessionScoped);
}
export function yieldPopulationLane(core, access, opts) {
return [...core.handles.values()].filter((handle) => yieldCovers(handle, access, opts));
}
export function yieldPopulationLeftRunning(core, ids) {
for (const id of ids) {
const handle = core.handles.get(id);
if (handle !== undefined && (handle.type === "background_agent" || handle.type === "workflow") && handle.status === "running")
return false;
}
return true;
}
export async function yieldPopulationWrites(core, ids) {
const lanes = [];
const runs = [];
for (const id of ids) {
const handle = core.handles.get(id);
if (handle !== undefined && handle.type === "background_agent" && handle.durable !== undefined)
lanes.push({ id, lane: handle.durable });
if (handle !== undefined && handle.type === "workflow")
runs.push(handle.handle.done.catch(() => undefined));
}
await Promise.all([...lanes.map(({ lane }) => lane.chain.catch(() => undefined)), ...runs]);
return lanes.filter(({ lane }) => lane.flushFailed).map(({ id }) => id);
}