UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

121 lines (120 loc) • 5.5 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { A as resolveExpiresAtMsFromDurationMs, y as isFutureDateTimestampMs } from "./number-coercion-D6u-4YD8.mjs"; import { t as cloneExecElevatedDefaults } from "./bash-tools.exec-types-CoZ_EDOu.mjs"; import { randomUUID } from "node:crypto"; //#region src/agents/bash-tools.exec-approval-followup-state.ts /** * Runtime handoff state for exec approval follow-up turns. * Stores short-lived elevated defaults so an approved async exec can resume in * the same session without persisting approval capabilities. */ const EXEC_APPROVAL_FOLLOWUP_IDEMPOTENCY_PREFIX = "exec-approval-followup:"; const EXEC_APPROVAL_FOLLOWUP_IDEMPOTENCY_NONCE_MARKER = ":nonce:"; const EXEC_APPROVAL_FOLLOWUP_RUNTIME_HANDOFF_TTL_MS = 3e5; const execApprovalFollowupRuntimeHandoffs = /* @__PURE__ */ new Map(); function cloneExecApprovalFollowupRuntimeHandoff(e) { return { kind: e.kind, approvalId: e.approvalId, sessionKey: e.sessionKey, idempotencyKey: e.idempotencyKey, ...e.bashElevated ? { bashElevated: cloneExecElevatedDefaults(e.bashElevated) } : {}, ...e.resultText !== void 0 ? { resultText: e.resultText } : {} }; } function pruneExpiredExecApprovalFollowupRuntimeHandoffs(e) { for (const [t, n] of execApprovalFollowupRuntimeHandoffs) if (!isFutureDateTimestampMs(n.expiresAtMs, { nowMs: e })) execApprovalFollowupRuntimeHandoffs.delete(t); } function buildExecApprovalFollowupIdempotencyKey(e) { const t = `${EXEC_APPROVAL_FOLLOWUP_IDEMPOTENCY_PREFIX}${e.approvalId}`; const n = normalizeOptionalString(e.nonce); return n ? `${t}${EXEC_APPROVAL_FOLLOWUP_IDEMPOTENCY_NONCE_MARKER}${n}` : t; } function parseExecApprovalFollowupApprovalId(e) { const t = normalizeOptionalString(e); if (!t?.startsWith(EXEC_APPROVAL_FOLLOWUP_IDEMPOTENCY_PREFIX)) return; const n = t.slice(23); const r = n.lastIndexOf(EXEC_APPROVAL_FOLLOWUP_IDEMPOTENCY_NONCE_MARKER); return normalizeOptionalString(r >= 0 ? n.slice(0, r) : n); } /** Register a short-lived exec approval handoff for the next follow-up turn. */ function registerExecApprovalFollowupRuntimeHandoff(e) { const t = normalizeOptionalString(e.approvalId); const n = normalizeOptionalString(e.sessionKey); if (!t || !n || !e.bashElevated && e.resultText === void 0) return; const r = Date.now(); pruneExpiredExecApprovalFollowupRuntimeHandoffs(r); const i = resolveExpiresAtMsFromDurationMs(EXEC_APPROVAL_FOLLOWUP_RUNTIME_HANDOFF_TTL_MS, { nowMs: r }); if (i === void 0) return; const a = randomUUID(); const o = buildExecApprovalFollowupIdempotencyKey({ approvalId: t, nonce: randomUUID() }); execApprovalFollowupRuntimeHandoffs.set(a, { kind: "exec-approval-followup", approvalId: t, sessionKey: n, idempotencyKey: o, ...e.bashElevated ? { bashElevated: cloneExecElevatedDefaults(e.bashElevated) } : {}, ...e.resultText !== void 0 ? { resultText: e.resultText } : {}, expiresAtMs: i }); return { handoffId: a, idempotencyKey: o }; } /** Claim a matching handoff while fallible run setup completes. */ function claimExecApprovalFollowupRuntimeHandoff(e) { const t = normalizeOptionalString(e.handoffId); const n = normalizeOptionalString(e.approvalId); const r = normalizeOptionalString(e.idempotencyKey); const i = normalizeOptionalString(e.claimId); if (!t || !n || !r || !i) return; pruneExpiredExecApprovalFollowupRuntimeHandoffs(Date.now()); const a = execApprovalFollowupRuntimeHandoffs.get(t); if (!a) return; const o = normalizeOptionalString(e.sessionKey); if (a.approvalId !== n || a.idempotencyKey !== r || a.sessionKey !== o) return; if (a.claimId && a.claimId !== i) return; a.claimId = i; return cloneExecApprovalFollowupRuntimeHandoff(a); } /** Consume a claimed handoff at its final privileged use. */ function finalizeExecApprovalFollowupRuntimeHandoff(e) { const t = normalizeOptionalString(e.handoffId); const n = normalizeOptionalString(e.claimId); if (!t || !n) return false; const r = execApprovalFollowupRuntimeHandoffs.get(t); if (r && !isFutureDateTimestampMs(r.expiresAtMs)) { execApprovalFollowupRuntimeHandoffs.delete(t); return false; } if (r?.claimId !== n) return false; execApprovalFollowupRuntimeHandoffs.delete(t); return true; } /** Release a claimed handoff when setup fails before dispatch. */ function releaseExecApprovalFollowupRuntimeHandoff(e) { const t = normalizeOptionalString(e.handoffId); const n = normalizeOptionalString(e.claimId); if (!t || !n) return false; const r = execApprovalFollowupRuntimeHandoffs.get(t); if (r?.claimId !== n) return false; delete r.claimId; return true; } /** * A persisted exec-approval followup is stale when the session key it targeted * has since been rebound to a different session id (via `/new` or `/reset`). * Delivering it would leak the old approval result into the new session, so the * gateway drops the followup instead of resuming the rebound session. */ function isExecApprovalFollowupSessionRebound(e) { const t = normalizeOptionalString(e.expectedSessionId); const n = normalizeOptionalString(e.resolvedSessionId); return Boolean(t && n && t !== n); } //#endregion export { parseExecApprovalFollowupApprovalId as a, isExecApprovalFollowupSessionRebound as i, claimExecApprovalFollowupRuntimeHandoff as n, registerExecApprovalFollowupRuntimeHandoff as o, finalizeExecApprovalFollowupRuntimeHandoff as r, releaseExecApprovalFollowupRuntimeHandoff as s, buildExecApprovalFollowupIdempotencyKey as t };