UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

39 lines (38 loc) • 1.84 kB
import { primaryActivityArg } from "../arg-summary.js"; import { askApproverIdentity, attest, attestationOf } from "../tool-policy.js"; import { boundInputHashOf } from "../canonical-json.js"; export function askGrantCovers(granted, asked) { return asked !== undefined && asked.length > 0 && granted.length > 0 && asked.every((c) => c === "ordinary" || granted.includes(c)); } export function askGrantShapeOf(args) { try { return JSON.stringify(args ?? null); } catch { return undefined; } } export function consumeInheritedAskGrant(grants, onAsk, humanReviewRef, decision, req) { const grant = grants.get(req.toolCallId); if (grant === undefined || askApproverIdentity(grant.approver) !== askApproverIdentity(onAsk) || grant.argsJson !== askGrantShapeOf(req.args)) return undefined; if (!askGrantCovers(grant.classes, decision.action === "ask" ? decision.classes : undefined)) return undefined; grants.delete(req.toolCallId); humanReviewRef.count += 1; humanReviewRef.totalWaitMs += grant.waitMs; const toolArg = primaryActivityArg(req.args); humanReviewRef.gates.push({ kind: "human", waitMs: grant.waitMs, decision: "allow", toolName: req.toolName, toolCallId: req.toolCallId, ...(toolArg !== undefined ? { toolArg } : {}), }); const reused = { action: "allow", presentedInput: grant.presented }; const settlement = attestationOf(grant.settled, req)?.settlement; if (settlement === undefined || decision.action !== "ask") return reused; return attest(reused, req, { settlement, ...(decision.origin !== undefined ? { origin: decision.origin } : {}), ...(decision.classes !== undefined ? { classes: decision.classes } : {}), inputDigest: boundInputHashOf(grant.presented) }); }