UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

41 lines (40 loc) • 2.76 kB
import { EVENT_PROMPT_REGISTRY } from "./event-registry.js"; import { bandOf } from "./bands.js"; import { SEMA_DEFAULT_CATALOG, catalogText } from "./catalog/index.js"; function safeId(raw) { const cleaned = raw.replace(/[\u0000-\u001f\u007f]/g, "␀"); return cleaned.length > 120 ? `${cleaned.slice(0, 117)}...` : cleaned; } export function explainPromptAssembly(input) { const lines = []; const short = (d) => (d === undefined ? "none" : `${safeId(d).slice(0, 23)}…`); lines.push(`identity: ${short(input.identity.artifact)} (record #${input.identity.epoch} via ${safeId(input.identity.activatedBy)})`); lines.push(` layers: center=${short(input.identity.layers.center)} deploy=${short(input.identity.layers.deploy)} task=${short(input.identity.layers.task)}`); const s = input.selectors; lines.push(` selectors: role=${safeId(s.host.role)} form=${safeId(s.host.form)} traits=[${s.traits.map(safeId).join(", ")}]${s.binding !== undefined ? ` binding=${safeId(s.binding)}` : ""}`); lines.push(`system blocks: ${input.blocks.map((b) => `${safeId(b.id)}[${b.chars}ch]`).join(" → ")}`); if (input.sections.length > 0) { lines.push("sections:"); for (const sec of input.sections) { const chain = sec.chain.map((l) => (l.source === "bundled" ? "bundled" : `${safeId(l.source)}:${l.op ?? "?"} ${safeId(l.overlay ?? "?")}${l.superseded === true ? " (superseded)" : ""}`)).join(" > "); lines.push(` ${safeId(sec.id)} band=${safeId(sec.band)}${sec.variant !== undefined ? ` variant=${safeId(sec.variant)}` : ""} ${sec.chars}ch ${safeId(sec.locale)} ${safeId(sec.contentHash).slice(0, 19)}… ${chain}`); } } if (input.superseded?.length) { lines.push(`superseded: ${input.superseded.map((o) => `${safeId(o.id)} (${safeId(o.source)}) on ${safeId(o.target)} by ${safeId(o.supersededBy)}`).join("; ")}`); } if (input.tools?.length) { const legacy = input.tools.filter((t) => t.contractId.startsWith("legacy:")); lines.push(`tools: ${input.tools.length} mounted (${input.tools.length - legacy.length} contract-declared, ${legacy.length} legacy/external)`); } const kinds = [...EVENT_PROMPT_REGISTRY.values()]; lines.push(`event kinds registered: ${kinds.length} (default-on: ${kinds.filter((k) => k.defaultPolicy === "on").map((k) => k.kind).join(", ")})`); return lines.join("\n"); } export function describeCatalog() { return { catalogId: SEMA_DEFAULT_CATALOG.id, roleBaseText: catalogText("core/role.base", "default"), sections: [...SEMA_DEFAULT_CATALOG.sections].sort((a, b) => a.rank - b.rank).map((s) => ({ id: s.id, band: s.band, rank: s.rank, ops: bandOf(s.band).ops })), }; }