UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

62 lines (61 loc) • 2.53 kB
import { SEMA_DEFAULT_CATALOG } from "./catalog/index.js"; import { composeCatalog, layoutSystemBlocks } from "./composer.js"; import { ABSENT_THIS_RUN } from "./overlay.js"; function supersededLine(s) { return s.supersededBy === ABSENT_THIS_RUN ? `prompt overlay "${s.id}" (${s.source}, ${s.by}) on "${s.target}" changed nothing: what it names is not in this run (the selector chose no variant of its target, or of the catalog message it takes its text from; or the lower entry it drops was never added — its own condition left it out, or the layer that added it is not in force; or its text is the text an earlier replacement gave another message of the same frame, which a reader tells apart by their words)` : `prompt overlay "${s.id}" (${s.source}, ${s.by}) on "${s.target}" is superseded by "${s.supersededBy}", applied after it (source order task < center < deploy; within a source, declaration order)`; } export function assemblePrompt(inputs) { const onWarn = (message) => { try { inputs.onWarn?.(message, "prompt-constitution"); } catch { } }; const plan = { catalog: SEMA_DEFAULT_CATALOG, selectors: inputs.selectors, overlays: [...inputs.taskOverlays, ...inputs.centerOverlays, ...inputs.deployOverlays], }; const toolNames = inputs.tools.map((t) => t.name); const renderInputs = (date) => ({ facts: inputs.facts, toolNames, ...(inputs.workspaceFolder !== undefined ? { workspaceFolder: inputs.workspaceFolder } : {}), mcpInstructionsBlock: inputs.mcpInstructionsBlock, environmentBlock: inputs.buildEnvBlock(date), memoryBlock: inputs.memoryBlock, }); let last = composeCatalog(plan, renderInputs(inputs.date)); const named = new Set(); for (const s of last.superseded) { if (named.has(s.id)) continue; named.add(s.id); onWarn(supersededLine(s)); } return { renderWithDate: (d) => { last = composeCatalog(plan, renderInputs(d)); return last.text; }, get systemPrompt() { return last.text; }, get blocks() { return last.blocks; }, get sections() { return last.sections; }, get systemBlocks() { return layoutSystemBlocks(last); }, superseded: last.superseded, get eventCopy() { return last.eventCopy; }, }; }