UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

65 lines (64 loc) • 2.6 kB
import { A as resolveExpiresAtMsFromDurationMs, R as resolveTimerTimeoutMs } from "./number-coercion-D6u-4YD8.mjs"; import { i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs"; import { randomUUID } from "node:crypto"; //#region src/gateway/exec-approval-registration.ts const EXPLICIT_APPROVAL_ID_INVALID_CHAR_PATTERN = /[^A-Za-z0-9._:-]/; /** Typed creation failure for an explicit approval id outside the shared safe format. */ var InvalidApprovalIdError = class extends Error { constructor() { super("approval id must be 1-128 characters using only letters, numbers, '.', '_', ':', or '-', and cannot be '.' or '..'"); this.code = "EXEC_APPROVAL_ID_INVALID"; this.reason = "INVALID_APPROVAL_ID"; this.name = "InvalidApprovalIdError"; } }; function createExecApprovalRecord(e, t, n) { const r = Date.now(); const i = resolveTimerTimeoutMs(t, 1); const a = resolveExpiresAtMsFromDurationMs(i, { nowMs: r }); if (a === void 0) throw new Error("approval expiry is unavailable"); const o = n !== null && n !== void 0 && n.length > 0; if (o && (n.length > 128 || n === "." || n === ".." || EXPLICIT_APPROVAL_ID_INVALID_CHAR_PATTERN.test(n))) throw new InvalidApprovalIdError(); return { id: o ? n : randomUUID(), request: e, createdAtMs: r, expiresAtMs: a }; } function readRequestString(e, t) { const n = asOptionalObjectRecord(e)?.[t]; return typeof n === "string" && n.trim() ? n.trim() : null; } async function prepareExecApprovalRegistration(e) { const { record: t } = e; const n = { agentId: readRequestString(t.request, "agentId"), sessionKey: readRequestString(t.request, "sessionKey"), sessionId: readRequestString(t.request, "sessionId"), runId: readRequestString(t.request, "runId"), toolCallId: readRequestString(t.request, "toolCallId"), toolName: readRequestString(t.request, "toolName") }; let r = []; if (n.sessionKey) r = await e.resolveAudienceSessionKeys?.(n.sessionKey, n.agentId) ?? [n.sessionKey]; return { id: t.id, kind: e.kind, presentation: e.presentation, requester: { deviceId: t.requestedByDeviceId, clientId: t.requestedByClientId, deviceTokenAuth: t.requestedByDeviceTokenAuth === true }, reviewerDeviceIds: t.approvalReviewerDeviceIds, source: n, audienceSessionKeys: r, runtimeEpoch: e.runtimeEpoch, createdAtMs: t.createdAtMs, expiresAtMs: t.expiresAtMs, ...t.executionIdentityToken ? { executionIdentityToken: t.executionIdentityToken } : {} }; } //#endregion export { createExecApprovalRecord as n, prepareExecApprovalRegistration as r, InvalidApprovalIdError as t };