UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

111 lines (110 loc) • 4.42 kB
import { PushQueue } from "../core/push-queue.js"; const channels = new Map(); const activeRuns = new Map(); export function markWorkflowActive(runId, scope) { activeRuns.set(runId, scope); } export function isWorkflowRunActive(runId) { return activeRuns.has(runId); } const parkDeliveries = new Map(); export function registerWorkflowParkDelivery(runId, deliver) { parkDeliveries.set(runId, deliver); } export function readWorkflowParkedDecisions(decisions, door) { const refuse = (why) => { throw Object.assign(new TypeError(`${door}: ${why}`), { code: "config.invalid_argument" }); }; const isRecord = (v) => v !== null && typeof v === "object" && !Array.isArray(v); if (!Array.isArray(decisions)) return refuse("decisions must be an array of { token, outcome, inheritedGate?, decisionId? }"); return decisions.map((entry, at) => { const e = isRecord(entry) ? entry : undefined; const token = e?.token, outcome = e?.outcome, inheritedGate = e?.inheritedGate, decisionId = e?.decisionId; if (typeof token !== "string" || token === "") return refuse(`decision ${at} names no checkpoint token`); if (!isRecord(outcome)) return refuse(`decision ${at} carries no outcome — every entry is a decision (a checkpoint no longer pending is settled without one)`); if (decisionId !== undefined && (typeof decisionId !== "string" || decisionId === "")) return refuse(`decision ${at}'s decisionId is not a non-empty string`); return { token, outcome, ...(inheritedGate !== undefined ? { inheritedGate } : {}), ...(decisionId !== undefined ? { decisionId } : {}) }; }); } export async function redeemWorkflowParks(runId, scope, decisions) { const read = readWorkflowParkedDecisions(decisions, "redeemWorkflowParks"); const deliver = activeRuns.get(runId) === scope ? parkDeliveries.get(runId) : undefined; if (deliver === undefined) return { kind: "not_active" }; return deliver(read); } export function publishWorkflowEvent(runId, event) { const subs = channels.get(runId); if (!subs) return; for (const q of subs) q.push(event); } export function closeWorkflowChannel(runId) { activeRuns.delete(runId); parkDeliveries.delete(runId); const subs = channels.get(runId); if (!subs) return; for (const q of subs) q.close(); channels.delete(runId); } export function subscribeWorkflow(runId, scope) { if (activeRuns.get(runId) !== scope) { return { async *[Symbol.asyncIterator]() { }, }; } const queue = new PushQueue(); let subs = channels.get(runId); if (!subs) { subs = new Set(); channels.set(runId, subs); } subs.add(queue); return { async *[Symbol.asyncIterator]() { try { for await (const ev of queue) yield ev; } finally { const live = channels.get(runId); if (live) { live.delete(queue); if (live.size === 0) channels.delete(runId); } } }, }; } export function listWorkflowRuns(store, scope, opts) { return store.listByScope(scope, opts); } export async function getWorkflowRun(store, id, scope) { const run = await store.get(id); return run && run.scope === scope ? run : null; } const PARK_WAIT_KINDS = new Set(["waiting", "moved_on", "lapsed", "unknown"]); export function deriveAgentDisplayStatus(agent, runStatus, opts) { const park = opts?.park; if (park !== undefined && !PARK_WAIT_KINDS.has(park?.kind)) { throw new TypeError("deriveAgentDisplayStatus: `park` is the engine's answer for the row's park (`workflowParkWaitOf` / `readWorkflowParkWaits`, a `{ kind }` of waiting / moved_on / lapsed / unknown) — the 7.69.0 `{ held }` form is retired"); } if (agent.status === "completed") return "done"; if (agent.status === "failed") return "failed"; if (agent.status === "parked") return opts?.park?.kind === "moved_on" || opts?.park?.kind === "lapsed" ? "interrupted" : "parked"; if (runStatus !== "running") return "interrupted"; return agent.startedAt === undefined ? "queued" : "running"; }