UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

78 lines (77 loc) • 3.55 kB
import { captureWorkflowFanOutScope, MAX_WORKFLOW_ITEMS } from "./workflow.js"; import { WorkflowScriptError } from "./workflow-meta.js"; const KINDS = new Set(["parallel", "pipeline"]); const shown = (v) => (typeof v === "string" ? JSON.stringify(v.slice(0, 40)) : v === null ? "null" : typeof v === "number" ? String(v) : typeof v); let protocols = 0; export function fanOutPlaces(ctx, agentBody) { const rootScope = captureWorkflowFanOutScope(); const tag = ++protocols; const slots = new Map(); const fans = new Map(); let minted = 0; const scopeAt = (at, verb) => { if (at === undefined) return rootScope; if (typeof at !== "string") throw new WorkflowScriptError(`fanOut.${verb}: the place is a slot token fanOut.open answered, or absent for the root chain (got ${shown(at)})`); const inSlot = slots.get(at); if (inSlot === undefined) throw new WorkflowScriptError(`fanOut.${verb}: ${shown(at)} is not a slot this run opened — a slot's token is what fanOut.open answered for it`); return inSlot; }; const agent = (at, spec, opts) => { let inScope; try { inScope = scopeAt(at, "agent"); } catch (refusal) { return Promise.reject(refusal); } return inScope(() => agentBody(spec, opts)); }; const open = (kind, count, at) => { let inScope; try { if (!KINDS.has(kind)) throw new WorkflowScriptError(`fanOut.open: the kind is "parallel" or "pipeline" (got ${shown(kind)})`); if (typeof count !== "number" || !Number.isInteger(count) || count < 0 || count > MAX_WORKFLOW_ITEMS) { throw new WorkflowScriptError(`fanOut.open: the slot count is a whole number from 0 to ${MAX_WORKFLOW_ITEMS} (got ${shown(count)})`); } inScope = scopeAt(at, "open"); } catch (refusal) { return Promise.reject(refusal); } const id = `fan:${tag}.${++minted}`; let release; const released = new Promise((r) => (release = r)); const tokens = []; const started = []; const bodies = Array.from({ length: count }, (_, i) => { const token = `slot:${tag}.${minted}.${i}`; tokens.push(token); let start; started.push(new Promise((r) => (start = r))); return async () => { slots.set(token, captureWorkflowFanOutScope()); start(); await released; }; }); const done = inScope(() => (kind === "parallel" ? ctx.parallel(bodies) : ctx.pipeline(bodies.map((_, i) => i), (_prev, _item, i) => bodies[i]()))); fans.set(id, { joined: false, release, done }); done.catch(() => undefined); return Promise.race([Promise.all(started), done.then(() => Promise.all(started))]).then(() => ({ fanOut: id, slots: [...tokens] })); }; const join = async (fanOut) => { const fan = typeof fanOut === "string" ? fans.get(fanOut) : undefined; if (fan === undefined) throw new WorkflowScriptError(`fanOut.join: ${shown(fanOut)} is not a fan-out this run opened`); if (fan.joined) throw new WorkflowScriptError(`fanOut.join: the fan-out ${shown(fanOut)} was already joined — a fan-out is joined once`); fan.joined = true; fan.release(); await fan.done; }; return { open, agent, join }; }