UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

37 lines (36 loc) • 1.5 kB
import { primaryActivityArg } from "../arg-summary.js"; import { askApproverIdentity, carryEngineAttestations } from "../tool-policy.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 }; if (grant.settled !== undefined) carryEngineAttestations(grant.settled, reused); return reused; }