UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

229 lines (228 loc) • 9.35 kB
import { TOOL_MOUNT_TAGS, pathTargetSlotOnly } from "./tool-face.js"; import { CARD_SKELETONS, ENGINE_CARDS, TOOL_CATALOG_ENTRIES } from "./tool-catalog-entries.js"; import { strayMembersOf } from "./own-property-receipt.js"; import { seatOf } from "./tool-name-readings.js"; export function describeToolCatalog() { return TOOL_CATALOG_ENTRIES; } export function catalogEntriesNamed(name) { return TOOL_CATALOG_ENTRIES.filter((e) => e.name === name); } export function catalogToolNames() { const out = new Set(); for (const e of TOOL_CATALOG_ENTRIES) { out.add(e.name); for (const a of e.face.aliases ?? []) out.add(a); } return out; } const names = (pred) => { const out = new Set(); for (const e of TOOL_CATALOG_ENTRIES) if (pred(e)) out.add(e.name); return out; }; export function pathConfinableWriteToolNames() { return names((e) => e.face.pathTarget !== undefined && e.face.pathTarget.access !== "read"); } function toolCanPersist(t, facts) { if (facts.exclude?.includes(t.name) ?? false) return false; const effect = t.effect ?? facts.effects?.get(t.name) ?? "write"; if (effect === "read") return false; const pathTarget = t.pathTarget ?? pathTargetOf(t.name); const family = t.family ?? catalogEntriesNamed(t.name)[0]?.face.family; if (!((pathTarget !== undefined && pathTarget.access !== "read") || family === "shell")) return false; if (facts.definitionOf?.(t)?.writtenContent === "kept") return false; return !facts.handsOffHost; } export function persistenceCapableToolNames(tools, facts) { const out = new Set(); for (const t of tools) if (toolCanPersist(t, facts)) out.add(t.name); return [...out]; } export function rosterCanPersistMemory(tools, facts, declared) { return declared ?? tools.some((t) => toolCanPersist(t, facts)); } export function persistenceCandidateToolNames() { const rowOf = new Map(); for (const e of TOOL_CATALOG_ENTRIES) rowOf.set({ name: e.name, effect: e.face.effect, pathTarget: e.face.pathTarget, family: e.face.family }, e); return persistenceCapableToolNames([...rowOf.keys()], { handsOffHost: false, definitionOf: (t) => rowOf.get(t) }); } export function pathTargetingToolNames() { return names((e) => e.face.pathTarget !== undefined); } export function hostTargetingToolNames() { return names((e) => e.face.hostTarget !== undefined); } const onShellFace = (e) => e.face.ruleFace?.grammar === "command"; export function shellFaceToolNames() { return names(onShellFace); } export function isShellFaceToolName(name) { for (const e of TOOL_CATALOG_ENTRIES) if (e.name === name && onShellFace(e)) return true; return false; } export function readTargetToolNames() { return names((e) => e.face.pathTarget?.access === "read"); } export function writeTargetToolNames() { return names((e) => e.face.pathTarget !== undefined && e.face.pathTarget.access !== "read"); } export function compactableToolNames() { return names((e) => e.face.compactable === true); } export function budgetExemptToolNames() { return names((e) => e.face.budgetExempt === true); } export function pathTargetValue(args, pt) { if (args === null || typeof args !== "object") return undefined; const a = args; for (const k of [pt.param, ...(pt.aliases ?? [])]) { const v = a[k]; if (typeof v === "string" && v.length > 0) return v; } return undefined; } export const PATH_TARGET_ELEMENTS_MAX = 100; export function declaredReadSlotOf(call) { const f = call.face; return f !== undefined && f.engineMounted !== true && f.pathTarget?.access === "read" ? f.pathTarget : undefined; } function readDeclaredSlot(args, pt) { if (args === null || typeof args !== "object") return { values: [], present: false }; const a = args; const spellings = [...new Set([pt.param, ...(pt.aliases ?? [])])]; const own = spellings.filter((k) => Object.hasOwn(a, k)); const chosen = own.length > 0 ? own : spellings; let present = false; const out = []; for (const k of chosen) { const v = a[k]; if (v === undefined) continue; present = true; if (pt.each === true && Array.isArray(v)) out.push(...Array.from(v)); else out.push(v); } return { values: out, present }; } export function declaredSlotValues(args, pt) { return readDeclaredSlot(args, pt).values; } export function declaredReadValuesOf(call) { const slot = declaredReadSlotOf(call); return slot === undefined ? undefined : { slot, ...readDeclaredSlot(call.args, slot) }; } const FILE_ARG_PATH_TARGET = { param: "file_path", aliases: ["path"] }; export function fileArgPath(args) { return pathTargetValue(args, FILE_ARG_PATH_TARGET); } export function protectivePathTargetOf(call) { if (call.face?.pathTarget !== undefined) return call.face.pathTarget; const floor = seatOf("floor.protectivePathTarget", { get: pathTargetOf }, call)?.value; return floor === undefined || call.face === undefined ? floor : pathTargetSlotOnly(floor); } export const BARE_REQUEST_CODE = "policy.bare_request"; export function declaredPathTargetOf(call) { if (call.face === undefined) { const e = new Error(`tool call ${JSON.stringify(call.toolName)} carries no face: a containment fence admits a write only by the tool's OWN declared path target, which a prepared leg's roster stamps on every gated request — a policy invoked outside a Runner must stamp \`face\` itself (toolCallFaceOf) rather than expect the catalog's declaration for the name`); e.code = BARE_REQUEST_CODE; throw e; } return call.face.pathTarget; } export function isProtectedWrite(call) { const pt = protectivePathTargetOf(call); return pt !== undefined && pt.access !== "read"; } export function writesProtectedPath(names, pathTarget) { return names.some((toolName) => isProtectedWrite({ toolName, args: {}, face: pathTarget !== undefined ? { pathTarget } : {} })); } export function isDeclaredWrite(call) { const pt = declaredPathTargetOf(call); return pt !== undefined && pt.access !== "read"; } export function fileOpAccessOf(name) { for (const e of TOOL_CATALOG_ENTRIES) { if (e.name === name && (e.face.family === "file-read" || e.face.family === "file-write") && e.face.pathTarget !== undefined) return e.face.pathTarget.access; } return undefined; } export function pathTargetOf(name) { for (const e of TOOL_CATALOG_ENTRIES) if (e.name === name && e.face.pathTarget !== undefined) return e.face.pathTarget; return undefined; } export function hostTargetOf(name) { for (const e of TOOL_CATALOG_ENTRIES) if (e.name === name && e.face.hostTarget !== undefined) return e.face.hostTarget; return undefined; } export function catalogRuleFaceOf(name) { let face; for (const e of TOOL_CATALOG_ENTRIES) { if (e.name !== name && !(e.face.aliases ?? []).includes(name)) continue; const pt = e.face.pathTarget; const target = pt !== undefined ? [pt.param, ...(pt.aliases ?? [])] : undefined; const primary = e.face.ruleFace?.primaryParams ?? target ?? []; const declared = e.face.ruleFace?.params ?? target; const params = declared === undefined ? face?.params : [...new Set([...(face?.params ?? []), ...declared, ...primary])]; face = { primaryParams: face?.primaryParams ?? [...primary], ...(params !== undefined ? { params } : {}) }; } return face; } export function handBandEffects() { const out = new Map(); for (const e of TOOL_CATALOG_ENTRIES) { if (!e.mountedBy.includes("hands")) continue; const eff = e.face.effect ?? "write"; const prior = out.get(e.name); if (prior === undefined || prior === "read") out.set(e.name, eff); } return out; } export function engineCardTypes() { const out = new Set(Object.keys(ENGINE_CARDS)); for (const e of TOOL_CATALOG_ENTRIES) for (const card of e.cards) out.add(card); return out; } { const cards = [...engineCardTypes()]; const moment = "when the tool catalog loads"; const rowWithoutCard = strayMembersOf(CARD_SKELETONS, { subject: "CARD_SKELETONS", unit: "card", moment }, cards); const cardWithoutRow = strayMembersOf(Object.fromEntries(cards.map((c) => [c, true])), { subject: "the engine card set", unit: "skeleton row", moment }, Object.keys(CARD_SKELETONS)); for (const [stray, what] of [[rowWithoutCard, "CARD_SKELETONS rows naming no card the engine mints"], [cardWithoutRow, "engine cards with no CARD_SKELETONS row"]]) { if (stray !== undefined) throw new Error(`tool catalog: ${what}: ${stray.names.map((n) => JSON.stringify(String(n))).join(", ")} — refused ${moment}`); } } export function cardSkeletonMembers(type) { return Object.hasOwn(CARD_SKELETONS, type) ? CARD_SKELETONS[type] : []; } export const TOOL_MOUNT_TAG_SET = new Set(TOOL_MOUNT_TAGS); export { HANDS_BAND_TAGS, MOUNT_TAG_IN_HANDS_BAND } from "./tool-face.js";