UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

7 lines • 5.11 kB
import{i as extractErrorCode,r as collectNestedErrorCandidates,t as coerceErrorMessage}from"./error-coercion-B-1Loz4b.mjs";import{a as isRecord,n as asOptionalObjectRecord}from"./record-coerce-DwDZngEy.mjs";import{l as resolveGlobalSingleton}from"./string-coerce-BBqv4GXj.mjs";import{t as isPromiseLike}from"./promise-like-OKzBwzLG.mjs";import{AsyncLocalStorage}from"node:async_hooks";import{isProxy}from"node:util/types"; //!src/infra/sqlite-error-diagnostics.ts function readOwnErrorDataProperty(e,t){try{return Object.getOwnPropertyDescriptor(e,t)?.value}catch{return}}function isStateDatabaseAdmissionClosedError(e){return collectNestedErrorCandidates(e).some(e=>{const t=typeof e===`string`?{message:e}:isRecord(e)?e:{};const n=readOwnErrorDataProperty(t,`errorCode`)??readOwnErrorDataProperty(t,`code`);const r=readOwnErrorDataProperty(t,`errorMessage`)??readOwnErrorDataProperty(t,`message`);return n===`STATE_DATABASE_READ_ADMISSION_INVALIDATED`||typeof r===`string`&&r.includes(`Shared-state worker is retiring`)})}const SQLITE_INSPECTION_OPERATIONS={source:`opening the source database`,snapshot:`creating its private snapshot`};const inspectionOperations=resolveGlobalSingleton(Symbol.for(`openclaw.sqliteInspectionOperations`),()=>/* @__PURE__ */ new WeakMap);const nativeOpenFailures=resolveGlobalSingleton(Symbol.for(`openclaw.sqliteNativeOpenFailures`),()=>/* @__PURE__ */ new WeakSet);const snapshotFailureScopes=resolveGlobalSingleton(Symbol.for(`openclaw.sqliteSnapshotFailureScopes`),()=>new AsyncLocalStorage);function recordSqliteSnapshotFailure(e){const t=snapshotFailureScopes.getStore();if(t)t.failure??={error:e}}function markSqliteNativeOpenFailure(e){if(e!==null&&typeof e===`object`)nativeOpenFailures.add(e)}function withSqliteNativeOpen(e){try{return e()}catch(e){markSqliteNativeOpenFailure(e);throw e}}function isSqliteNativeOpenFailure(e){return e!==null&&typeof e===`object`&&nativeOpenFailures.has(e)}function markSqliteInspectionOperation(e,t){if(e!==null&&typeof e===`object`&&!inspectionOperations.has(e))inspectionOperations.set(e,t);return e}function withSqliteInspectionOperation(e,t){try{return t()}catch(t){throw markSqliteInspectionOperation(t,e)}}function formatSqliteReadOnlyInspectionFailure(e){const t=coerceErrorMessage(e);const{suffix:n,operation:r}=readSqliteErrorDetails(e);const i=`${t}${n}`;return r===void 0?i:`failed while ${SQLITE_INSPECTION_OPERATIONS[r]}: ${i}`}function readSqliteInspectionOperation(e){return readSqliteErrorDetails(e).operation}function readSqliteErrorDetails(e){const t=/* @__PURE__ */ new Set;let n;for(let r=e,i=0;i<8&&isRecord(r);i+=1){n=inspectionOperations.get(r)??n;const e=extractErrorCode(r);if(e&&/^[A-Z0-9_]{1,64}$/u.test(e))t.add(`code=${e}`);const{errcode:i}=r;if(typeof i===`number`&&Number.isInteger(i)&&i>=0&&i<=2147483647)t.add(`errcode=${i}`);r=r.cause}return{suffix:t.size>0?` (${[...t].join(`, `)})`:``,operation:n}}const SQLITE_LOCK_ERROR_CODES=/* @__PURE__ */ new Set([`SQLITE_BUSY`,`SQLITE_LOCKED`]);const SQLITE_BUSY_RESULT_CODE=5;const SQLITE_LOCKED_RESULT_CODE=6;const SQLITE_CORRUPT_RESULT_CODE=11;const SQLITE_NOTADB_RESULT_CODE=26;const SQLITE_PRIMARY_RESULT_CODE_MASK=255;function sqliteErrorCode(e){const t=asOptionalObjectRecord(e)?.code;return typeof t===`string`?t:void 0}function sqliteExtendedResultCode(e){const t=asOptionalObjectRecord(e)?.errcode;return typeof t===`number`&&Number.isInteger(t)?t:void 0}function sqlitePrimaryResultCode(e){const t=sqliteExtendedResultCode(e);return t===void 0?void 0:t&SQLITE_PRIMARY_RESULT_CODE_MASK}function isSqliteLockError(e){const t=sqliteErrorCode(e);if(t!==void 0&&SQLITE_LOCK_ERROR_CODES.has(t))return true;const n=sqlitePrimaryResultCode(e);return n===SQLITE_BUSY_RESULT_CODE||n===SQLITE_LOCKED_RESULT_CODE}function isSqliteCorruptionError(e){const t=sqlitePrimaryResultCode(e);return t===SQLITE_CORRUPT_RESULT_CODE||t===SQLITE_NOTADB_RESULT_CODE} //!src/shared/listeners.ts function notifyListeners(e,t,n){for(const r of e)try{r(t)}catch(e){n?.(e)}}function registerListener(e,t){e.add(t);return()=>e.delete(t)} //!src/infra/sql-connection.ts function assertSyncTransactionResult(e){if(isPromiseLike(e))throw new Error(`SQLite write transactions must be synchronous; Promise returns are not supported.`)}const postgresConnectionBrand=Symbol.for(`openclaw.postgres-sync-connection`);function isPostgresSqlConnection(e){return postgresConnectionBrand in e&&e[postgresConnectionBrand]===true}function sqlPublicationAnchor(e){return isPostgresSqlConnection(e)?e.anchor:e}export{sqlitePrimaryResultCode as _,registerListener as a,isSqliteLockError as c,markSqliteInspectionOperation as d,markSqliteNativeOpenFailure as f,sqliteExtendedResultCode as g,sqliteErrorCode as h,notifyListeners as i,isSqliteNativeOpenFailure as l,recordSqliteSnapshotFailure as m,isPostgresSqlConnection as n,formatSqliteReadOnlyInspectionFailure as o,readSqliteInspectionOperation as p,sqlPublicationAnchor as r,isSqliteCorruptionError as s,assertSyncTransactionResult as t,isStateDatabaseAdmissionClosedError as u,withSqliteInspectionOperation as v,withSqliteNativeOpen as y};