UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

148 lines (147 loc) • 8.83 kB
import { createPreToolUseScreening, formatHookFeedback } from "../hooks.js"; import { classicEventBase, dispatchClassic, stringMembersAnswer, stringMembersOf } from "../mods/stations.js"; import { beltUnanswered } from "../real-approval.js"; import { decisionText } from "../tool-policy.js"; import { rosterToolName } from "../tool-roster.js"; import { wireOf } from "../tool-name-readings.js"; import { noteHostRewrite, settleWithheldResult } from "../frame-settlement.js"; import { foreignLine } from "../protocol-http-auth.js"; function postToolUseAnswer(answer) { const strings = stringMembersAnswer("classic.PostToolUse", ["additionalContext"])(answer); if (strings !== undefined || typeof answer !== "object" || answer === null) return strings; const out = Reflect.get(answer, "updatedOutput"); return out === undefined || Array.isArray(out) ? undefined : "answered a non-array \"updatedOutput\"; it replaces the content array"; } function updatedOutputOf(answer) { if (typeof answer !== "object" || answer === null) return undefined; const out = Reflect.get(answer, "updatedOutput"); return Array.isArray(out) ? out.filter(isContentBlock) : undefined; } function isContentBlock(c) { return typeof c === "object" && c !== null && typeof Reflect.get(c, "type") === "string"; } export function createToolResultStation(input) { const { modBus, blockedToolCalls, toolRosterDeltas, preToolContexts, inheritedScreens, abortController, spec, handsCwdRef, hookEnvFace, hookIdentity, permissionMode, reminderMark, frameSettlement } = input; const toolResultStationArmed = modBus.hasHandlers("classic.PostToolUse") || modBus.hasHandlers("classic.PreToolUse") || modBus.hasHandlers("classic.PostToolUseFailure") || inheritedScreens; if (toolResultStationArmed) { return async (e) => { if (blockedToolCalls.delete(e.toolCallId)) { return undefined; } let content = e.content; let changed = false; let withheld = false; const post = { toolName: rosterToolName(toolRosterDeltas.current, e.toolName) }; const callFacts = (tool) => ({ ...classicEventBase(hookIdentity, permissionMode.observed), tool, tool_use_id: e.toolCallId, input: e.input, ...(handsCwdRef?.current !== undefined ? { cwd: handsCwdRef.current } : {}), ...(hookEnvFace !== undefined ? { env: hookEnvFace } : {}), }); if (e.isError) { if (modBus.hasHandlers("classic.PostToolUseFailure")) { let clonedDetails = e.details; try { clonedDetails = structuredClone(e.details); } catch { } const failure = { error: e.content .filter((c) => c.type === "text") .map((c) => c.text) .join("\n"), isInterrupt: abortController.signal.aborted || spec.signal?.aborted === true, content: e.content.map((c) => ({ ...c })), details: clonedDetails, }; const outcome = await dispatchClassic(modBus, "classic.PostToolUseFailure", { ...callFacts(wireOf("hook.postToolUseFailure", post)), failure }, { signal: abortController.signal, checkAnswer: stringMembersAnswer("classic.PostToolUseFailure", ["additionalContext"]) }).catch(() => undefined); const patch = outcome?.kind === "answered" ? stringMembersOf(outcome.answer, ["additionalContext"]) : undefined; if (patch?.additionalContext) { content = [...content, { type: "text", text: formatHookFeedback(patch.additionalContext, reminderMark) }]; changed = true; } } } else if (modBus.hasHandlers("classic.PostToolUse")) { const outcome = await dispatchClassic(modBus, "classic.PostToolUse", { ...callFacts(wireOf("hook.postToolUse", post)), output: { content: e.content, details: e.details, isError: e.isError } }, { signal: abortController.signal, checkAnswer: postToolUseAnswer }).catch(() => ({ kind: "rejected" })); if (outcome?.kind === "failed" || outcome?.kind === "rejected") { content = [{ type: "text", text: formatHookFeedback(`the result of this call to "${foreignLine(post.toolName, 80)}" could not be given to the classic.PostToolUse extensions that check it: a hook that checks this result could not hand back its answer, so the result is withheld — the call ran (fail-closed)`, reminderMark) }]; withheld = true; changed = true; settleWithheldResult(frameSettlement, e.toolCallId, content); } const answer = outcome?.kind === "answered" ? outcome.answer : undefined; const updatedOutput = updatedOutputOf(answer); const patch = stringMembersOf(answer, ["additionalContext"]); if (updatedOutput) { content = updatedOutput; changed = true; noteHostRewrite(frameSettlement, e.toolCallId); } if (patch.additionalContext) { content = [...content, { type: "text", text: formatHookFeedback(patch.additionalContext, reminderMark) }]; changed = true; } } const pre = preToolContexts.get(e.toolCallId); if (pre) { preToolContexts.delete(e.toolCallId); content = [...content, ...pre.map((t) => ({ type: "text", text: formatHookFeedback(t, reminderMark) }))]; changed = true; } return changed ? { content, ...(withheld ? { details: {}, isError: true } : {}) } : undefined; }; } return undefined; } const HOOK_CLASSES = Object.freeze(["hook"]); export function createApprovedCallScreen(input) { const { modBus, permissionMode, abortController, handsCwdRef, hookEnvFace, hookIdentity, preToolContexts, whileScreened } = input; if (!modBus.hasHandlers("classic.PreToolUse")) return undefined; return async ({ toolCallId, toolName, args, answered }) => { const screening = createPreToolUseScreening({ event: { toolCallId, toolName, input: {} }, modBus, observedPermissionMode: () => permissionMode.observed, ...(handsCwdRef !== undefined ? { trackedCwd: () => handsCwdRef.current } : {}), ...(hookEnvFace !== undefined ? { hookEnv: hookEnvFace } : {}), identity: hookIdentity, abortSignal: abortController.signal, whileScreened, }); if (screening === undefined) return { admit: () => undefined }; const subject = `this call to "${toolName}"`; const screened = await screening.screen(args, subject); if (screened.kind === "refused") return { refused: screened.reason, ...(screened.extension !== undefined ? { extension: screened.extension } : {}) }; if (screened.kind === "settled") return { refused: decisionText(screened.deny) ?? `${subject} was refused` }; const r = screened.answer; if (r?.action === "deny") return { refused: screening.deniedReason(r), ...(screened.answeredBy !== undefined ? { extension: screened.answeredBy } : {}) }; if (r !== undefined && beltUnanswered(r, HOOK_CLASSES, answered)) return { refused: `a PreToolUse hook requires a person's approval for ${subject}, and the approval this call had was not given to a question the hook asked — not executed; send the call again to have it asked` }; const contexts = screened.contexts; return { admit: () => { if (contexts.length > 0) preToolContexts.set(toolCallId, [...contexts]); } }; }; } export function chainCannotJudge(ig) { if (ig?.requiresParentConstraint !== true) return undefined; const chain = ig.constraintChain; if (chain === undefined) return "the checkpoint predates the frozen projection chain (no constraintChain recorded)"; if (chain.some((e) => "opaque" in e)) return "the inherited chain carries an OPAQUE layer (no projection exported)"; return chain.some((e) => !("opaque" in e) && e.requiresLiveRemainder) ? "an inherited chain layer declares a live remainder beyond its projection" : undefined; }