UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

120 lines (119 loc) • 6.75 kB
import { createHash } from "node:crypto"; import { catalogText } from "../prompt-assembly/catalog/index.js"; import { shippedReadings } from "../prompt-assembly/engine-text.js"; import { slotNames } from "../prompt-assembly/slots.js"; import { PRESENT_PLAN_TOOL_NAME } from "./present-plan-tool.js"; import { engineRegionTextsOf, markedReminderBodiesOf } from "./reminder-mint.js"; import { TOOL_CATALOG_ENTRIES } from "./tool-catalog-entries.js"; import { rosterEntryNamed } from "./tool-roster.js"; const catalogName = (id) => TOOL_CATALOG_ENTRIES.find((e) => e.id === id).name; const BASH_FIRST_SHELL = catalogName("bash"); const BASH_FIRST_EDITORS = Object.freeze([catalogName("edit"), catalogName("write")]); export function bashFirstToolsMountedOn(roster) { return rosterEntryNamed(roster, BASH_FIRST_SHELL) !== undefined && BASH_FIRST_EDITORS.some((name) => rosterEntryNamed(roster, name) !== undefined); } function namesPlanFile(id) { return slotNames(catalogText(id)).includes("planFile"); } export function postureText(id, facts, say) { const carried = new Set(slotNames(catalogText(id))); return say(id, { ...(carried.has("exitTool") ? { exitTool: PRESENT_PLAN_TOOL_NAME } : {}), ...(carried.has("planFile") ? { planFile: facts.planFile ?? "" } : {}) }); } const PLAN_CADENCE = { everyTurns: 5, fullEvery: 5, sparse: "core/event.permission-posture.plan-sparse" }; const PLAN_EXIT = "core/event.permission-posture.plan-exit"; export const POSTURE_TABLE = Object.freeze([ { family: "plan", owedBy: (f) => f.mode === "plan" && f.isChildLeg, frame: "core/event.permission-posture.plan-child", exit: PLAN_EXIT, cadence: { ...PLAN_CADENCE, sparse: "core/event.permission-posture.plan-child" } }, { family: "plan", owedBy: (f) => f.mode === "plan" && f.exitToolMounted && f.planFile !== undefined, frame: "core/event.permission-posture.plan-file", exit: PLAN_EXIT, cadence: { ...PLAN_CADENCE, sparse: "core/event.permission-posture.plan-file-sparse" } }, { family: "plan", owedBy: (f) => f.mode === "plan" && f.exitToolMounted, frame: "core/event.permission-posture.plan", exit: PLAN_EXIT, cadence: PLAN_CADENCE }, { family: "plan", owedBy: (f) => f.mode === "plan", frame: "core/event.permission-posture.plan-no-exit", exit: PLAN_EXIT, cadence: { ...PLAN_CADENCE, sparse: "core/event.permission-posture.plan-no-exit-sparse" } }, { family: "auto", owedBy: (f) => f.mode === "bypassPermissions" || (f.mode !== "dontAsk" && f.askEffective === "auto_allow"), needs: (f) => f.bashFirstToolsMounted, frame: "core/event.permission-posture.bypass", exit: "core/event.permission-posture.bypass-exit" }, { family: "auto", owedBy: (f) => f.mode === "auto", frame: "core/event.permission-posture.auto", exit: "core/event.permission-posture.auto-exit" }, ]); export function owedPostureRow(facts) { return POSTURE_TABLE.find((row) => row.owedBy(facts) && (row.needs?.(facts) ?? true)); } function vocabularyTextOf(id, say) { if (!namesPlanFile(id)) return postureText(id, {}, say); const [head, tail] = postureText(id, { planFile: "{{planFile}}" }, say).split("{{planFile}}"); return { head, tail }; } const POSTURE_VOCABULARY = shippedReadings().flatMap((say) => POSTURE_TABLE.flatMap((row) => [ [vocabularyTextOf(row.frame, say), { row, exit: false }], [vocabularyTextOf(row.exit, say), { row, exit: true }], ...(row.cadence !== undefined ? [[vocabularyTextOf(row.cadence.sparse, say), { row, exit: false }]] : []), ])); export function postureVocabulary() { return POSTURE_VOCABULARY.map(([text, item]) => ({ text, row: item.row, exit: item.exit })); } function vocabularyLengthAt(rest, text) { if (typeof text === "string") return rest.startsWith(text) ? text.length : undefined; if (!rest.startsWith(text.head)) return undefined; const end = rest.indexOf(text.tail, text.head.length); if (end <= text.head.length || rest.slice(text.head.length, end).includes("\n")) return undefined; return end + text.tail.length; } function postureItemsOf(body) { const out = []; let rest = body; while (rest.length > 0) { let matched; for (const [text, item] of POSTURE_VOCABULARY) { const length = vocabularyLengthAt(rest, text); if (length !== undefined && (length === rest.length || rest.startsWith("\n\n", length))) { matched = { item: typeof text === "string" ? item : { ...item, file: rest.slice(text.head.length, length - text.tail.length) }, length }; break; } } if (matched === undefined) return undefined; out.push(matched.item); rest = matched.length === rest.length ? "" : rest.slice(matched.length + 2); } return out.length > 0 ? out : undefined; } export function postureStandingOf(messages, reminderMark) { const standing = new Map(); for (const m of messages) { if (m.role === "assistant") { for (const [family, s] of standing) standing.set(family, { ...s, turnsSince: s.turnsSince + 1 }); continue; } for (const region of engineRegionTextsOf([m])) { for (const body of markedReminderBodiesOf(region, reminderMark)) { for (const item of postureItemsOf(body) ?? []) { if (item.exit) standing.delete(item.row.family); else standing.set(item.row.family, { row: item.row, statements: (standing.get(item.row.family)?.statements ?? 0) + 1, turnsSince: 0, ...(item.file !== undefined ? { file: item.file } : {}) }); } } } } return standing; } export function composePostureFrames(facts, standing, say) { const owed = owedPostureRow(facts); const held = POSTURE_TABLE.find((row) => row.owedBy(facts)); const frames = []; for (const [family, s] of standing) if (family !== owed?.family && s.row !== held) frames.push(postureText(s.row.exit, facts, say)); if (owed === undefined) return frames; const s = standing.get(owed.family); const fileMoved = s !== undefined && namesPlanFile(owed.frame) && s.file !== (facts.planFile ?? ""); if (s === undefined || s.row !== owed || fileMoved) frames.push(postureText(owed.frame, facts, say)); else if (owed.cadence !== undefined && s.turnsSince >= owed.cadence.everyTurns) { frames.push(postureText((s.statements + 1) % owed.cadence.fullEvery === 1 ? owed.frame : owed.cadence.sparse, facts, say)); } return frames; } export function directiveDigest(body) { return createHash("sha256").update(body, "utf8").digest("hex"); }