UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

109 lines (108 loc) • 5.37 kB
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs"; //#region packages/gateway-protocol/src/gateway-error-details.ts /** Display projection for an assistant failure without visible reply content. */ const GATEWAY_ASSISTANT_ERROR_FALLBACK_TEXT = "The agent run failed before producing a reply."; /** Gateway JSON-RPC style error codes shared by clients and server handlers. */ const ErrorCodes = { /** @deprecated Retained for source compatibility; no current server emitter. */ NOT_LINKED: "NOT_LINKED", /** Device exists but still needs an explicit pairing approval. */ NOT_PAIRED: "NOT_PAIRED", /** @deprecated Retained for source compatibility; no current server emitter. */ AGENT_TIMEOUT: "AGENT_TIMEOUT", /** Request payload failed protocol validation or method preconditions. */ INVALID_REQUEST: "INVALID_REQUEST", /** Authenticated caller lacks permission for the requested operation. */ FORBIDDEN: "FORBIDDEN", /** Approval resolution referenced a missing or expired approval request. */ APPROVAL_NOT_FOUND: "APPROVAL_NOT_FOUND", /** Gateway service or required backend is temporarily unavailable. */ UNAVAILABLE: "UNAVAILABLE" }; /** Stable discriminants for structured method-level failures. */ const GatewayErrorDetailCodes = { CRON_JOB_NOT_FOUND: "CRON_JOB_NOT_FOUND", MISSING_SCOPE: "MISSING_SCOPE", MCP_APP_VIEW_EXPIRED: "MCP_APP_VIEW_EXPIRED", OUTBOUND_DELIVERY_QUEUED: "OUTBOUND_DELIVERY_QUEUED", USER_PREFS_LIMIT_EXCEEDED: "USER_PREFS_LIMIT_EXCEEDED", SESSION_COMPANION_BUSY: "SESSION_COMPANION_BUSY", PROJECT_CLONE_FAILED: "PROJECT_CLONE_FAILED", UNKNOWN_AGENT_ID: "UNKNOWN_AGENT_ID", WIZARD_NOT_FOUND: "WIZARD_NOT_FOUND", SETUP_ADMISSION_BUSY: "SETUP_ADMISSION_BUSY", GITHUB_PUBLICATION_SELECTION_REJECTED: "GITHUB_PUBLICATION_SELECTION_REJECTED", SESSION_WORKSPACE_RECOVERY_REQUIRED: "SESSION_WORKSPACE_RECOVERY_REQUIRED", TASK_WORKTREE_SOURCE_REQUIRED: "TASK_WORKTREE_SOURCE_REQUIRED", TASK_HISTORY_PREVIEW_CAPACITY: "TASK_HISTORY_PREVIEW_CAPACITY", CLAWHUB_RISK_ACKNOWLEDGEMENT_REQUIRED: "CLAWHUB_RISK_ACKNOWLEDGEMENT_REQUIRED", TEMPLATE_REVIEW_REQUIRED: "TEMPLATE_REVIEW_REQUIRED", PENDING_INPUT_NOT_RETIRED: "PENDING_INPUT_NOT_RETIRED", OPENCLAW_CONFIG_READONLY: "OPENCLAW_CONFIG_READONLY" }; function readClawHubRiskAcknowledgementRequiredError(e) { const t = asNullableRecord(e); const n = asNullableRecord(t?.details); const r = typeof n?.warning === "string" ? n.warning.trim() : ""; return t?.code === ErrorCodes.INVALID_REQUEST && n?.code === GatewayErrorDetailCodes.CLAWHUB_RISK_ACKNOWLEDGEMENT_REQUIRED && r ? { code: GatewayErrorDetailCodes.CLAWHUB_RISK_ACKNOWLEDGEMENT_REQUIRED, warning: r } : null; } function readTemplateReviewRequiredError(e) { const t = asNullableRecord(e); const n = asNullableRecord(t?.details); return t?.code === ErrorCodes.INVALID_REQUEST && n?.code === GatewayErrorDetailCodes.TEMPLATE_REVIEW_REQUIRED && (n.kind === "plugin" || n.kind === "skill") ? { code: GatewayErrorDetailCodes.TEMPLATE_REVIEW_REQUIRED, kind: n.kind } : null; } const LEGACY_MISSING_SCOPE_PATTERN = /\bmissing scope:\s*([a-z0-9._-]+)/i; /** Reads a typed cron lookup miss without parsing operator-facing prose. */ function readCronJobNotFoundError(e) { const t = asNullableRecord(e); const n = asNullableRecord(t?.details); if (n?.code !== GatewayErrorDetailCodes.CRON_JOB_NOT_FOUND) return null; const r = typeof n.jobId === "string" ? n.jobId.trim() : ""; return r ? { code: GatewayErrorDetailCodes.CRON_JOB_NOT_FOUND, jobId: r } : null; } /** Reads validated missing-scope details from an untrusted protocol payload. */ function readMissingScopeErrorDetails(e) { const t = asNullableRecord(e); if (t?.code !== GatewayErrorDetailCodes.MISSING_SCOPE) return null; const n = typeof t.missingScope === "string" ? t.missingScope.trim() : ""; const r = Array.isArray(t.requiredScopes) ? t.requiredScopes.map((e) => typeof e === "string" ? e.trim() : "") : []; if (!n || r.length === 0 || r.some((e) => !e)) return null; return { code: GatewayErrorDetailCodes.MISSING_SCOPE, missingScope: n, requiredScopes: r }; } function isMcpAppViewExpiredError(e) { const t = asNullableRecord(e); return asNullableRecord(t?.details)?.code === GatewayErrorDetailCodes.MCP_APP_VIEW_EXPIRED; } /** * Reads a method-level missing-scope failure, preferring structured details. * The message fallback keeps clients compatible with gateways predating structured details. */ function readMissingScopeError(e) { const t = asNullableRecord(e); if (!t) return null; const n = readMissingScopeErrorDetails(t.details); if (n) return n; const r = typeof t.gatewayCode === "string" ? t.gatewayCode : typeof t.code === "string" ? t.code : ""; if (r !== ErrorCodes.FORBIDDEN && r !== ErrorCodes.INVALID_REQUEST) return null; const i = (typeof t.message === "string" ? t.message : "").match(LEGACY_MISSING_SCOPE_PATTERN)?.[1]; return i ? { code: GatewayErrorDetailCodes.MISSING_SCOPE, missingScope: i, requiredScopes: [i] } : null; } //#endregion export { readClawHubRiskAcknowledgementRequiredError as a, readMissingScopeErrorDetails as c, isMcpAppViewExpiredError as i, readTemplateReviewRequiredError as l, GATEWAY_ASSISTANT_ERROR_FALLBACK_TEXT as n, readCronJobNotFoundError as o, GatewayErrorDetailCodes as r, readMissingScopeError as s, ErrorCodes as t };