UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

61 lines (60 loc) • 2.68 kB
import { CONSTITUTION_MESSAGES } from "./constitution.js"; import { CORE_BEHAVIOR_MESSAGES } from "./core-behavior.js"; import { MEMORY_MESSAGES } from "./memory.js"; import { MODE_MESSAGES } from "./modes.js"; import { ROLE_MESSAGES } from "./role.js"; import { SECTIONS } from "./sections.js"; import { TEAM_MESSAGES } from "./team.js"; import { BANDS } from "../bands.js"; import { RENDERER_IDS, isWordOf } from "./schema.js"; export const SEMA_DEFAULT_CATALOG = { id: "sema-default@2", bands: BANDS, messages: [...ROLE_MESSAGES, ...CONSTITUTION_MESSAGES, ...CORE_BEHAVIOR_MESSAGES, ...MODE_MESSAGES, ...TEAM_MESSAGES, ...MEMORY_MESSAGES], sections: SECTIONS, }; export function validateCatalog(catalog) { const fail = (why) => { throw new Error(`prompt catalog ${catalog.id}: ${why}`); }; const messages = new Set(); for (const m of catalog.messages) { if (messages.has(m.id)) fail(`duplicate message id "${m.id}"`); messages.add(m.id); } const bands = new Set(catalog.bands.map((b) => b.id)); const ids = new Set(); const ranks = new Set(); for (const s of catalog.sections) { if (ids.has(s.id)) fail(`duplicate section id "${s.id}"`); ids.add(s.id); if (ranks.has(s.rank)) fail(`duplicate rank ${s.rank} ("${s.id}")`); ranks.add(s.rank); if (!bands.has(s.band)) fail(`section "${s.id}" names undeclared band "${s.band}"`); const referenced = "message" in s.body ? [s.body.message] : "lines" in s.body ? s.body.lines.map((l) => l.message) : []; for (const id of referenced) if (!messages.has(id)) fail(`section "${s.id}" references unknown message "${id}"`); if ("renderer" in s.body && !isWordOf(RENDERER_IDS, s.body.renderer)) fail(`section "${s.id}" names unknown renderer "${s.body.renderer}"`); } } validateCatalog(SEMA_DEFAULT_CATALOG); const MESSAGE_BY_ID = new Map(SEMA_DEFAULT_CATALOG.messages.map((m) => [m.id, m])); export function catalogMessage(id, catalog = SEMA_DEFAULT_CATALOG) { const m = catalog === SEMA_DEFAULT_CATALOG ? MESSAGE_BY_ID.get(id) : catalog.messages.find((x) => x.id === id); if (m === undefined) throw new Error(`prompt catalog ${catalog.id}: no message "${id}"`); return m; } export function catalogText(id, variantId) { const m = catalogMessage(id); const v = variantId === undefined ? m.variants[0] : m.variants.find((x) => x.id === variantId); if (v === undefined) throw new Error(`prompt catalog: message "${id}" has no variant "${variantId}"`); return v.text; }