openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
207 lines (206 loc) • 8.13 kB
JavaScript
import { r as captureOpenClawStateWorkerContext } from "./openclaw-state-worker-context-DAKL_dnZ.mjs";
import { s as normalizeExecApprovalsInternal } from "./exec-approvals-config-BBzEUJlE.mjs";
import { n as GATEWAY_CLIENT_IDS, r as GATEWAY_CLIENT_MODES } from "./client-info-Cm1BcrMm.mjs";
import { Ii as validateExecApprovalsSetParams, Ni as validateExecApprovalsGetParams, Pi as validateExecApprovalsNativeCommitParams, ct as validateExecApprovalsNodeSetParams, lt as validateExecApprovalsNodeSnapshot, st as validateExecApprovalsNodeGetParams } from "./src-BX8OAe1J.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { t as resolveExecApprovalsFromFileInternal } from "./exec-approvals-resolver-C6IwmycS.mjs";
import { r as readExecApprovalsSocket } from "./exec-approvals-socket-DEOHtzox.mjs";
import { d as updateExecApprovals, l as readExecApprovalsSnapshotAsync, n as ensureExecApprovalsSnapshot } from "./exec-approvals-store-BUZ9uOWF.mjs";
import { t as redactExecApprovals } from "./exec-approvals-rOfoWejC.mjs";
import { r as respondUnavailableOnThrow, t as respondError } from "./response-C7fEiIN1.mjs";
import { n as defineValidatedGatewayHandler, t as assertValidParams } from "./validation-DwNk6A-B.mjs";
import { a as readGatewayRequestMutationAuthority } from "./session-mutation-guards-Bjnk_O0z.mjs";
import { l as resolveNodeCommandAllowlist, o as isNodeCommandAllowed } from "./node-command-policy-DkH-pkTr.mjs";
import { n as respondUnavailableOnNodeInvokeErrorWithProvenance, r as parseGatewayPayload } from "./nodes.helpers-CItZ-oW7.mjs";
import { n as localStateOwnerChangedError, t as captureLocalStateMutationGuard } from "./local-state-owner-CnxUSfeW.mjs";
import { t as commitNativeExecAuthorization } from "./exec-approvals-native-Bb9hLtyx.mjs";
import { t as resolveBaseHashParam } from "./base-hash-C6Uj-zyN.mjs";
//#region src/gateway/server-methods/exec-approvals.ts
function requireApprovalsBaseHash(e, t, n) {
const r = resolveBaseHashParam(e);
if (t.exists && (!t.hash || !r)) {
respondError(n, ErrorCodes.INVALID_REQUEST, `exec approvals base hash ${t.hash ? "required" : "unavailable"}; re-run exec.approvals.get and retry`);
return false;
}
if (r && r !== t.hash) {
respondApprovalsChanged(n);
return false;
}
return true;
}
function respondApprovalsChanged(e) {
respondError(e, ErrorCodes.INVALID_REQUEST, "exec approvals changed since last load; re-run exec.approvals.get and retry");
}
function toExecApprovalsPayload(e) {
return {
...redactExecApprovals(e),
resolvedDefaults: resolveExecApprovalsFromFileInternal({ file: e.file }).defaults
};
}
function captureExecApprovalsOwnerGuard(e, t) {
const n = readGatewayRequestMutationAuthority(t);
try {
const r = e ? captureLocalStateMutationGuard(e, t) : void 0;
return {
assertPreparationCurrent: n.assertPreparationCurrent,
assertCurrent: () => {
if (n.family === "worker") {
n.assertWorkerCurrent();
n.expectedProfileBinding?.assertCurrent();
} else n.assertCurrent();
r?.();
}
};
} catch (e) {
t.respond(false, void 0, localStateOwnerChangedError(e));
return null;
}
}
function isMacAppNode(e) {
const t = e?.platform?.trim().toLowerCase();
return e?.clientId === GATEWAY_CLIENT_IDS.MACOS_APP && e.clientMode === GATEWAY_CLIENT_MODES.NODE && (t === "macos" || t?.startsWith("macos ") === true);
}
function execApprovalsNodeHandler(e) {
return defineValidatedGatewayHandler(e.method, e.validate, async ({ params: t, context: n, respond: r }) => {
const i = t.nodeId.trim();
if (!i) {
respondError(r, ErrorCodes.INVALID_REQUEST, "nodeId required");
return;
}
const a = n.nodeRegistry.get(i);
if (a) {
const t = isNodeCommandAllowed({
command: e.command,
declaredCommands: a.commands,
allowlist: resolveNodeCommandAllowlist(n.getRuntimeConfig(), {
...a,
approvedCommands: a.commands
})
});
if (!t.ok) {
respondError(r, ErrorCodes.INVALID_REQUEST, `node command not allowed: ${e.command} (${t.reason})`, { details: {
command: e.command,
reason: t.reason
} });
return;
}
}
await respondUnavailableOnThrow(r, async () => {
let o = false;
const s = await n.nodeRegistry.invoke({
nodeId: i,
...a ? {
expectedConnId: a.connId,
...a.pairingGeneration ? { expectedPairingGeneration: a.pairingGeneration } : {}
} : {},
command: e.command,
params: e.commandParams(t, a),
onDispatchReady: () => {
o = true;
}
});
if (!respondUnavailableOnNodeInvokeErrorWithProvenance(r, s, { nodeCommandDispatched: o })) return;
const c = s.payloadJSON ? parseGatewayPayload(s.payloadJSON) : s.payload;
if (e.validatePayload && !e.validatePayload(c)) {
respondError(r, ErrorCodes.INVALID_REQUEST, "node returned invalid exec approvals payload");
return;
}
r(true, c, void 0);
});
});
}
const execApprovalsHandlers = {
"exec.approvals.native.commit": async (e) => {
const { params: t, respond: n } = e;
if (!assertValidParams(t, validateExecApprovalsNativeCommitParams, "exec.approvals.native.commit", n)) return;
const r = captureExecApprovalsOwnerGuard(t.expectedOwnerId, e);
if (r === null) return;
await respondUnavailableOnThrow(n, async () => {
r.assertPreparationCurrent();
const e = await commitNativeExecAuthorization(t, r.assertCurrent, captureOpenClawStateWorkerContext());
r.assertCurrent();
n(true, e, void 0);
});
},
"exec.approvals.get": async (e) => {
const { params: t, respond: n } = e;
if (!assertValidParams(t, validateExecApprovalsGetParams, "exec.approvals.get", n)) return;
const r = captureExecApprovalsOwnerGuard(t.expectedOwnerId, e);
if (r === null) return;
await respondUnavailableOnThrow(n, async () => {
r.assertPreparationCurrent();
let e;
if (t.expectedOwnerId) {
const t = captureOpenClawStateWorkerContext();
const assertCurrent = () => {
t.admission.assertCurrent();
r.assertCurrent();
};
e = await readExecApprovalsSnapshotAsync(t);
assertCurrent();
const n = await readExecApprovalsSocket(t.initializationEnvironment ?? t.environment, { assertCurrent });
assertCurrent();
if (n?.socket_path) e = {
...e,
file: {
...e.file,
socket: { path: n.socket_path }
}
};
} else e = await ensureExecApprovalsSnapshot(r.assertCurrent, r.assertPreparationCurrent);
r.assertCurrent();
n(true, toExecApprovalsPayload(e), void 0);
});
},
"exec.approvals.set": async (e) => {
const { params: t, respond: n } = e;
if (!assertValidParams(t, validateExecApprovalsSetParams, "exec.approvals.set", n)) return;
const r = captureExecApprovalsOwnerGuard(t.expectedOwnerId, e);
if (r === null) return;
await respondUnavailableOnThrow(n, async () => {
r.assertPreparationCurrent();
const e = captureOpenClawStateWorkerContext();
const i = await readExecApprovalsSnapshotAsync(e);
r.assertPreparationCurrent();
if (!requireApprovalsBaseHash(t, i, n)) return;
const a = normalizeExecApprovalsInternal(t.file);
const o = await updateExecApprovals({
baseHash: i.hash,
...r,
update: {
kind: "replace",
file: a,
preserveSocket: true
}
}, e);
if (!o) {
respondApprovalsChanged(n);
return;
}
e.admission.assertCurrent();
r.assertCurrent();
n(true, toExecApprovalsPayload(o), void 0);
});
},
"exec.approvals.node.get": execApprovalsNodeHandler({
method: "exec.approvals.node.get",
validate: validateExecApprovalsNodeGetParams,
command: "system.execApprovals.get",
commandParams: (e, t) => isMacAppNode(t) ? { includeResolvedDefaults: true } : {},
validatePayload: validateExecApprovalsNodeSnapshot
}),
"exec.approvals.node.set": execApprovalsNodeHandler({
method: "exec.approvals.node.set",
validate: validateExecApprovalsNodeSetParams,
command: "system.execApprovals.set",
commandParams: (e) => "native" in e ? {
...e.native,
baseHash: e.baseHash
} : {
file: e.file,
baseHash: e.baseHash
}
})
};
//#endregion
export { execApprovalsHandlers };