@sema-agent/core
Version:
Stateless, task-oriented AI agent core
93 lines (92 loc) • 5.45 kB
JavaScript
import { extensionNamed, extensionQuestionText, extensionSpeaker, refusalOf } from "./tool-policy.js";
import { answerAsShown, shownDigestOf } from "./rewrite-rejudge.js";
import { hookAskReasonOf, joinAskReasons } from "./ask-origin.js";
import { allowRuleShadowOf, withAllowRuleShadow } from "./allow-entry.js";
import { failedHandlerClause } from "./mods/stations.js";
const STANDS = Object.freeze({ kind: "stands" });
function readToolCheck(heard, standing, call, mayClear) {
const { toolName } = call;
const refused = (message, by) => ({ kind: "denied", deny: refusalOf({ action: "deny", message, decisionReason: "hook" }, call.judged), ...(by !== undefined ? { by } : {}) });
if (heard.kind === "aborted")
return refused(`the task was cancelled while a tool.check handler was still checking this call to "${toolName}"; the call was NOT executed (fail-closed)`);
if (heard.kind === "failed")
return refused(`a tool.check handler ${failedHandlerClause(heard)} while checking this call to "${toolName}"; the call was NOT executed (fail-closed)`, heard.registration);
if (heard.by === undefined && heard.decision === standing.action)
return STANDS;
const { who, why } = speakerOf(heard);
const rewritten = call.rewrite !== undefined ? { updatedInput: call.rewrite } : {};
if (heard.decision === "deny")
return refused(`tool "${toolName}" denied by ${who}${why}`, heard.by);
if (heard.decision === "ask") {
if (standing.action === "ask") {
const askReasons = joinAskReasons(standing.askReasons, [entryOf(heard)]);
return { kind: "asked", ask: { ...standing, decisionReason: "hook", ...(heard.reason !== undefined ? { message: heard.reason } : {}), ...(askReasons !== undefined ? { askReasons } : {}) }, minted: false };
}
return { kind: "asked", ask: { ...questionOf(heard, toolName), ...rewritten }, minted: true };
}
if (standing.action === "allow")
return STANDS;
const entries = heard.by !== undefined ? [heard.by] : [];
if (mayClear?.() === true)
return { kind: "cleared", allow: extensionAllowOf(entries, call.rewrite), entries };
return { kind: "shadowed", shadows: [allowRuleShadowOf("hook", entries)] };
}
export function extensionAllowOf(speakers, rewrite) {
const who = speakers.length > 0 ? speakers.map(extensionNamed).join(", ") : "an extension";
return { action: "allow", message: `${who} allowed this call`, decisionReason: "hook", ...(rewrite !== undefined ? { updatedInput: rewrite } : {}) };
}
function speakerOf(heard) {
return extensionSpeaker(heard.by, heard.reason);
}
function entryOf(heard) {
return hookAskReasonOf(heard.by, "asked", heard.reason);
}
function questionOf(heard, toolName) {
const entry = entryOf(heard);
return { action: "ask", message: extensionQuestionText(toolName, entry), decisionReason: "hook", askReasons: Object.freeze([entry]) };
}
export async function readToolChecks(chains, standing, call, project) {
let raised = false;
let current = standing;
let moved;
let shadows = [];
for (const chain of chains) {
const heard = await chain.raise(project(current, chain.mayClear));
if (heard === undefined)
continue;
raised = true;
const reading = readToolCheck(heard, current, call, chain.mayClear);
if (reading.kind === "denied")
return reading;
if (reading.kind === "asked" && chain.settle !== undefined && heard.kind === "answered") {
const settled = answerAsShown(shownDigestOf(call.request), await chain.settle(questionOf(heard, call.toolName), call.request, call.signal), call.request);
if (settled.action === "deny")
return { kind: "denied", deny: refusalOf(settled, call.judged) };
if (settled.action === "allow") {
if (settled.updatedInput !== undefined)
return { kind: "approved", allow: { ...settled, updatedInput: settled.updatedInput }, ...(moved?.kind === "asked" ? { outstanding: moved.ask } : {}) };
continue;
}
const joined = current.action === "ask" ? joinAskReasons(current.askReasons, settled.askReasons) : undefined;
const ask = current.action === "ask" ? (joined !== undefined ? { ...current, askReasons: joined } : current) : settled;
moved = { kind: "asked", ask, minted: current.action !== "ask" || (moved?.kind === "asked" && moved.minted), marked: true };
current = ask;
continue;
}
if (reading.kind === "shadowed")
shadows = reading.shadows.reduce(withAllowRuleShadow, shadows);
if (reading.kind === "asked") {
moved = { ...reading, minted: reading.minted || (moved?.kind === "asked" && moved.minted), ...(moved?.kind === "asked" && moved.marked === true ? { marked: true } : {}) };
current = reading.ask;
}
if (reading.kind === "cleared") {
moved = reading;
current = reading.allow;
}
}
if (!raised)
return undefined;
if (moved?.kind === "asked" && shadows.length > 0)
return { ...moved, ask: { ...moved.ask, allowRuleShadowed: shadows.reduce(withAllowRuleShadow, [...(moved.ask.allowRuleShadowed ?? [])]) } };
return moved ?? (shadows.length > 0 ? { kind: "shadowed", shadows } : STANDS);
}