UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

55 lines (54 loc) • 2.89 kB
import { applyWorkflowAgentType, workflowAgentRegistry } from "./workflow.js"; import { workflowModelNamesOf } from "./workflow-governance.js"; const namesOf = (list) => new Set(Array.isArray(list) ? list.map((x) => (typeof x === "string" ? x : typeof x?.name === "string" ? x.name : undefined)).filter((n) => n !== undefined) : []); export function workflowRequirementGaps(requires, mount) { if (requires === undefined) return []; const gaps = []; const base = (mount.governanceBaseline?.base ?? {}); let registry; for (const m of requires.members ?? []) { let spec = base; if (m.agent !== undefined) { try { registry ??= workflowAgentRegistry({ agents: mount.agents, builtinAgents: mount.builtinAgents }); spec = applyWorkflowAgentType(base, m.agent, registry).spec; } catch { gaps.push({ kind: "agent", name: m.agent }); continue; } } const who = m.agent !== undefined ? { member: m.agent } : {}; const skills = namesOf(spec.skills); for (const s of m.skills ?? []) if (!skills.has(s)) gaps.push({ kind: "skill", name: s, ...who }); const servers = namesOf(spec.mcp); for (const s of m.mcpServers ?? []) if (!servers.has(s)) gaps.push({ kind: "mcpServer", name: s, ...who }); } if ((requires.models?.length ?? 0) > 0) { const models = new Set(workflowModelNamesOf(mount.governanceBaseline, mount.models)); for (const n of requires.models ?? []) if (!models.has(n)) gaps.push({ kind: "model", name: n }); } return gaps; } export const WORKFLOW_REQUIREMENTS_UNMET_CODE = "workflow.requirements_unmet"; export function workflowRequirementsUnmetSentence(name, gaps) { const group = (kind, label) => { const rows = gaps.filter((g) => g.kind === kind).map((g) => (g.member !== undefined ? `${g.name} (for ${g.member})` : g.name)); return rows.length > 0 ? `${label}: ${rows.join(", ")}` : undefined; }; const parts = [group("agent", "agent definitions"), group("skill", "skills"), group("mcpServer", "MCP servers"), group("model", "models")].filter((p) => p !== undefined); return `workflow "${name}" was not started: this deployment does not provide what it requires — ${parts.join("; ")}. Nothing ran and nothing was spent. Ask the operator to provide them, or pick a workflow listed in this tool's description.`; } export async function listWorkflowCatalogRows(catalog, mount) { return workflowCatalogRowsOf(await catalog.list(), mount); } export function workflowCatalogRowsOf(listing, mount) { return { rows: listing.entries.map((e) => ({ ...e, gaps: workflowRequirementGaps(e.requires, mount) })), notices: listing.notices }; }