UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

61 lines (60 loc) • 3.28 kB
import { boundInputHashOf, canonicalize } from "./canonical-json.js"; export const BOUND_GRANT_ABSENCES = Object.freeze(["not_bindable", "unreadable"]); const BOUND_GRANT_ABSENCE_SET = new Set(BOUND_GRANT_ABSENCES); const isBoundGrantAbsence = (v) => BOUND_GRANT_ABSENCE_SET.has(v); export function readBoundSkillGrants(raw) { if (typeof raw !== "object" || raw === null) return undefined; const { skill, allowedTools: allowed, disallowedTools: disallowed } = raw; if (skill !== null && typeof skill !== "string") return undefined; const entries = (list) => { if (!Array.isArray(list)) return undefined; const copy = Array.from(list); return copy.every((e) => typeof e === "string") ? copy : undefined; }; const allowedTools = entries(allowed); const disallowedTools = entries(disallowed); return allowedTools !== undefined && disallowedTools !== undefined ? { skill, allowedTools, disallowedTools } : undefined; } export const NOT_BINDABLE = Object.freeze({ boundGrantAbsence: "not_bindable" }); export function readBoundBinding(row) { const digest = row.boundGrantDigest; if (typeof digest === "string") { const grants = readBoundSkillGrants(row.boundGrants); return { boundGrantDigest: digest, ...(grants !== undefined && boundInputHashOf(grants) === digest ? { boundGrants: grants } : {}) }; } const word = row.boundGrantAbsence; return { boundGrantAbsence: digest === undefined && isBoundGrantAbsence(word) ? word : "unreadable" }; } const REMINT = new WeakMap(); export function requestGrantBinding(of, call, args) { const members = of(call, args); if (members.boundGrants === undefined) return members; const boundGrants = frozenGrants(members.boundGrants); const toolName = call.toolName; REMINT.set(boundGrants, { toolName, digest: members.boundGrantDigest, remint: (moved) => requestGrantBinding(of, { toolName }, moved) }); return { boundGrantDigest: members.boundGrantDigest, boundGrants }; } export function withArgsBinding(carrier, args) { if (carrier.boundGrantDigest === undefined && carrier.boundGrants === undefined && carrier.boundGrantAbsence === undefined) return { ...carrier, args }; return { ...carrier, args, boundGrantDigest: undefined, boundGrants: undefined, boundGrantAbsence: undefined, ...movedBinding(carrier, args) }; } function movedBinding(carrier, args) { const grants = carrier.boundGrants; const note = typeof grants === "object" && grants !== null ? REMINT.get(grants) : undefined; if (note !== undefined && note.digest === carrier.boundGrantDigest && note.toolName === carrier.toolName) return note.remint(args); const read = readBoundBinding(carrier); if (!("boundGrantDigest" in read)) return read; if (canonicalize(args) !== canonicalize(carrier.args)) return { boundGrantAbsence: "unreadable" }; return read.boundGrants !== undefined ? { boundGrantDigest: read.boundGrantDigest, boundGrants: frozenGrants(read.boundGrants) } : read; } function frozenGrants(g) { return Object.freeze({ skill: g.skill, allowedTools: Object.freeze([...g.allowedTools]), disallowedTools: Object.freeze([...g.disallowedTools]) }); }