UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.23 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{A as resolveExpiresAtMsFromDurationMs,y as isFutureDateTimestampMs}from"./number-coercion-B3CfI447.mjs";import{t as cloneExecElevatedDefaults}from"./bash-tools.exec-types-B3bngGkY.mjs";import{randomUUID}from"node:crypto"; //!src/agents/bash-tools.exec-approval-followup-state.ts 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)}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}}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)}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}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}function isExecApprovalFollowupSessionRebound(e){const t=normalizeOptionalString(e.expectedSessionId);const n=normalizeOptionalString(e.resolvedSessionId);return Boolean(t&&n&&t!==n)}export{parseExecApprovalFollowupApprovalId as a,isExecApprovalFollowupSessionRebound as i,claimExecApprovalFollowupRuntimeHandoff as n,registerExecApprovalFollowupRuntimeHandoff as o,finalizeExecApprovalFollowupRuntimeHandoff as r,releaseExecApprovalFollowupRuntimeHandoff as s,buildExecApprovalFollowupIdempotencyKey as t};