UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

117 lines (116 loc) • 4.8 kB
import { r as sha256HexPrefixCore } from "./node-crypto-B6yuTjmm.mjs"; import "./crypto-digest-B2m8wIBj.mjs"; //#region src/infra/diagnostic-error-metadata.ts const HTTP_STATUS_MIN = 100; const HTTP_STATUS_MAX = 599; const REQUEST_ID_HASH_PREFIX_LEN = 12; const PROVIDER_REQUEST_ID_KEYS = [ "upstreamRequestId", "providerRequestId", "requestId", "request_id" ]; const PROVIDER_REQUEST_ID_RE = /^[A-Za-z0-9._:-]{1,128}$/u; const PROVIDER_REQUEST_ID_TEXT_PATTERN = /\b(?:x-request-id|request-id|request_id|requestId|trace-id|trace_id)\b["'\s:=([]+([A-Za-z0-9._:-]{1,128})/i; const FAILURE_KIND_BY_CODE = /* @__PURE__ */ new Map([ ["ABORT_ERR", "aborted"], ["ECONNABORTED", "aborted"], ["ERR_ABORTED", "aborted"], ["ECONNRESET", "connection_reset"], ["ERR_STREAM_PREMATURE_CLOSE", "connection_closed"], ["UND_ERR_SOCKET", "connection_closed"], ["ETIMEDOUT", "timeout"], ["ERR_SOCKET_CONNECTION_TIMEOUT", "timeout"] ]); const FAILURE_KIND_BY_MESSAGE = [ [/\b(?:terminated|sigkill|sigterm)\b/i, "terminated"], [/\b(?:econnreset|connection reset)\b/i, "connection_reset"], [/\b(?:socket hang up|premature close|connection closed|other side closed)\b/i, "connection_closed"], [/\b(?:timed out|timeout|etimedout)\b/i, "timeout"], [/\b(?:aborted|abort_err|operation was aborted)\b/i, "aborted"] ]; function isObjectLike(e) { return (typeof e === "object" || typeof e === "function") && e !== null; } function readOwnDataProperty(e, t) { if (!isObjectLike(e)) return; try { const n = Object.getOwnPropertyDescriptor(e, t); return n && "value" in n ? n.value : void 0; } catch { return; } } function findDiagnosticErrorProperty(e, t, n = /* @__PURE__ */ new Set()) { const r = t(e); if (r !== void 0) return r; if (!isObjectLike(e) || n.has(e)) return; n.add(e); return findDiagnosticErrorProperty(readOwnDataProperty(e, "error"), t, n) ?? findDiagnosticErrorProperty(readOwnDataProperty(e, "cause"), t, n); } function normalizeProviderRequestId(e) { if (typeof e === "string") { const t = e.trim(); return PROVIDER_REQUEST_ID_RE.test(t) ? t : void 0; } if (typeof e === "number" && Number.isFinite(e) || typeof e === "bigint") { const t = String(e); return PROVIDER_REQUEST_ID_RE.test(t) ? t : void 0; } } function readDirectMessage(e) { if (typeof e === "string") return e; const t = readOwnDataProperty(e, "message"); return typeof t === "string" ? t : void 0; } /** Returns a low-cardinality error category without trusting mutable `Error.name`. */ function diagnosticErrorCategory(e) { try { if (e instanceof TypeError) return "TypeError"; if (e instanceof RangeError) return "RangeError"; if (e instanceof ReferenceError) return "ReferenceError"; if (e instanceof SyntaxError) return "SyntaxError"; if (e instanceof URIError) return "URIError"; if (typeof AggregateError !== "undefined" && e instanceof AggregateError) return "AggregateError"; if (e instanceof Error) return "Error"; } catch { return "unknown"; } if (e === null) return "null"; return typeof e; } /** Reads only an own data property so diagnostics never invoke a user-defined getter. */ function diagnosticErrorMessage(e) { return readDirectMessage(e)?.trim() || void 0; } /** Extracts a safe HTTP status code from own `status` or `statusCode` data properties. */ function diagnosticHttpStatusCode(e) { for (const t of ["status", "statusCode"]) { const n = readOwnDataProperty(e, t); if (typeof n === "number" && Number.isInteger(n) && n >= HTTP_STATUS_MIN && n <= HTTP_STATUS_MAX) return String(n); } } /** Classifies transport-style failures without exposing raw error messages. */ function diagnosticErrorFailureKind(e) { const t = findDiagnosticErrorProperty(e, (e) => { const t = readOwnDataProperty(e, "code"); return typeof t === "string" ? t : void 0; })?.trim().toUpperCase(); const n = t === void 0 ? void 0 : FAILURE_KIND_BY_CODE.get(t); if (n) return n; const r = findDiagnosticErrorProperty(e, readDirectMessage); if (!r) return; return FAILURE_KIND_BY_MESSAGE.find(([e]) => e.test(r))?.[1]; } /** Extracts and hashes bounded provider request ids so diagnostics never expose raw ids. */ function diagnosticProviderRequestIdHash(e) { const t = findDiagnosticErrorProperty(e, (e) => { for (const t of PROVIDER_REQUEST_ID_KEYS) { const n = normalizeProviderRequestId(readOwnDataProperty(e, t)); if (n) return n; } }) ?? findDiagnosticErrorProperty(e, (e) => normalizeProviderRequestId(readDirectMessage(e)?.match(PROVIDER_REQUEST_ID_TEXT_PATTERN)?.[1])); return t ? `sha256:${sha256HexPrefixCore(t, REQUEST_ID_HASH_PREFIX_LEN)}` : void 0; } //#endregion export { diagnosticProviderRequestIdHash as a, diagnosticHttpStatusCode as i, diagnosticErrorFailureKind as n, diagnosticErrorMessage as r, diagnosticErrorCategory as t };