openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
98 lines (97 loc) • 2.78 kB
JavaScript
import { t as AsyncContext } from "./async-context-BYDAvG9A.mjs";
import { o as getGatewayToolCallerIdentity } from "./gateway-caller-context-BNidAW8G.mjs";
import { t as InternalToolLoopBlocked } from "./internal-hooks-Ch3rqxs-.mjs";
import { s as getAgentToolActionDescriptor } from "./agent-tool-metadata-nrQgRaqv.mjs";
import { i as recordExecutionDecisionWork } from "./execution-decision-work-CSwdYVxn.mjs";
import { createHash } from "node:crypto";
//#region src/agents/agent-tools.before-tool-call.decision.ts
const genericDecisions = {
allowed: [
"allowed",
"attribution-only",
"generic_action_attributed"
],
denied: [
"denied",
"enforced",
"generic_action_policy_denied"
],
suppressed: [
"not-applicable",
"attribution-only",
"generic_action_suppressed"
]
};
const toolDecisionOwner = new AsyncContext(Symbol("toolDecisionOwner"));
/** Marks the current tool call after its owner-native decision record is registered. */
function markToolDecisionRecorded() {
const e = toolDecisionOwner.getStore();
if (e) e.recorded = true;
}
/** Records generic attribution only when execution creates no owner-native record. */
async function runWithGenericToolActionDecision(e, t, n) {
const r = { recorded: false };
let i = "allowed";
try {
return await toolDecisionOwner.run(r, n);
} catch (e) {
if (e instanceof InternalToolLoopBlocked) i = "suppressed";
throw e;
} finally {
if (!r.recorded) recordGenericToolActionDecision(e, t, i);
}
}
function recordGenericToolActionDecision(e, t, n) {
const r = getAgentToolActionDescriptor(e);
const i = getGatewayToolCallerIdentity();
const a = i?.executionIdentityToken;
const o = i?.receiptAuthority;
if (!r || !t?.trim() || !a || !o) return false;
const [s, c, l] = genericDecisions[n];
const u = `tool-action:${createHash("sha256").update(JSON.stringify([
a.contextId,
a.executionId,
t,
r,
l
])).digest("base64url").slice(0, 32)}`;
try {
const e = Date.now();
if (o() === false) return false;
return recordExecutionDecisionWork({
workVersion: 1,
token: a,
receipt: {
schemaVersion: 1,
receiptId: u,
occurredAt: e,
action: r,
decision: {
outcome: s,
reasonCode: l
},
enforcement: {
coverageState: c,
policyRefs: n === "denied" ? ["tool-action-policy"] : [],
grantRefs: [],
contextFieldsUsed: [
"contextId",
"executionId",
"runId"
]
},
source: {
owner: "tool-action",
recordRef: u,
decisionBoundary: "agent-tool.before-execute"
},
missingEvidence: [],
remediation: []
}
});
} catch {
return false;
}
}
//#endregion
export { recordGenericToolActionDecision as n, runWithGenericToolActionDecision as r, markToolDecisionRecorded as t };