UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

133 lines (132 loc) • 6.43 kB
import { CONSTITUTION_MESSAGES } from "./constitution.js"; import { CORE_BEHAVIOR_MESSAGES } from "./core-behavior.js"; import { ENGINE_MESSAGES } from "./engine.js"; import { ENVIRONMENT_MESSAGES } from "./environment.js"; import { EVENT_MESSAGES } from "./events.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 { textContentHashOf } from "../../core/canonical-json.js"; import { slotNames } from "../slots.js"; import { LOCALE_PACKS } from "./locales/index.js"; import { PROMPT_LOCALES, 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, ...ENVIRONMENT_MESSAGES, ...ENGINE_MESSAGES, ...EVENT_MESSAGES], sections: SECTIONS, locales: LOCALE_PACKS, }; export function sourceDigestOf(text) { return textContentHashOf(text); } 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 variants = new Set(); for (const v of m.variants) { if (variants.has(v.id)) fail(`message "${m.id}" has two variants "${v.id}"`); variants.add(v.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}"`); } validateLocales(catalog, fail); } function validateLocales(catalog, fail) { const rendered = PROMPT_LOCALES.filter((l) => l !== "canonical"); const held = Object.keys(catalog.locales ?? {}); for (const l of rendered) if (!held.includes(l)) fail(`no ${l} rendering (every locale word but canonical names a whole rendering of the catalog)`); for (const l of held) if (!isWordOf(rendered, l)) fail(`a rendering for "${l}", which is not a rendered locale (${rendered.join(", ")})`); const byId = new Map(catalog.messages.map((m) => [m.id, m])); for (const locale of rendered) { const pack = catalog.locales[locale]; for (const [id, rows] of Object.entries(pack)) { const m = byId.get(id); if (m === undefined) fail(`${locale} renders "${id}", which is not a catalog message`); for (const variant of Object.keys(rows)) if (!m.variants.some((v) => v.id === variant)) fail(`${locale} renders ${id}#${variant}, which is not a variant of it`); } for (const m of catalog.messages) { for (const v of m.variants) { const at = `${locale} rendering of ${m.id}#${v.id}`; const row = Object.prototype.hasOwnProperty.call(pack, m.id) ? pack[m.id][v.id] : undefined; if (row === undefined) fail(`no ${at}`); if (typeof row.text !== "string" || row.text.trim() === "") fail(`the ${at} is blank`); if (row.sourceDigest !== sourceDigestOf(v.text)) fail(`the ${at} was written from another text than the variant's (sourceDigest ${String(row.sourceDigest).slice(0, 19)}… ≠ ${sourceDigestOf(v.text).slice(0, 19)}…): render the current text again`); const src = v.text.split("\n"); const out = row.text.split("\n"); if (out.length !== src.length) fail(`the ${at} has ${out.length} lines; its source has ${src.length}`); for (let i = 0; i < src.length; i++) { if ((src[i].trim() === "") !== (out[i].trim() === "")) fail(`the ${at} line ${i + 1} is ${src[i].trim() === "" ? "not blank where its source is" : "blank where its source is not"}`); const want = slotNames(src[i]).sort().join(","); const got = slotNames(out[i]).sort().join(","); if (want !== got) fail(`the ${at} line ${i + 1} carries placeholders [${got}]; its source line carries [${want}]`); } } } } } function deepFreeze(value, seen = new Set()) { if (value === null || typeof value !== "object" || seen.has(value)) return value; seen.add(value); for (const v of Object.values(value)) deepFreeze(v, seen); return Object.freeze(value); } validateCatalog(SEMA_DEFAULT_CATALOG); deepFreeze(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; }