UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 10.6 kB
import{a as e,r as t}from"./transcript-record-coerce-CPLgmAk9.mjs";import{i as n}from"./transcript-global-singleton-DIJgobpU.mjs";import{B as r,F as i,M as a,P as o}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{AsyncLocalStorage as s}from"node:async_hooks";import{isNativeError as c,isProxy as l}from"node:util/types";import{isDeepStrictEqual as u}from"node:util";const d=Symbol.for(`openclaw.sqliteWorkerErrorCode`);var SqliteWorkerError=class extends Error{constructor(e,t){super(e),this.code=t,this.name=`SqliteWorkerError`,Object.defineProperty(this,d,{value:t})}};function wrapSqliteWorkerError(e,t,...n){let i=n.filter(e=>e!==void 0).map(t=>r(t,e)).filter((e,t,n)=>n.findIndex(t=>u(t,e))===t);return Object.assign(t===void 0?Error(e):new SqliteWorkerError(e,t),{cause:i.length>1?AggregateError(i,e):i[0]})}function retainSqliteWorkerErrorCode(e,t){let n;try{n=Object.getOwnPropertyDescriptor(t,d)?.value}catch{return e}return(n===`closed`||n===`overloaded`||n===`unavailable`||n===`outcome-unknown`)&&(Object.defineProperty(e,d,{value:n}),Object.assign(e,{code:n})),e}function hasSqliteWorkerErrorCode(e,t){if(!(e instanceof Error))return!1;try{return Object.getOwnPropertyDescriptor(e,d)?.value===t}catch{return!1}}function isSqliteWorkerError(e,t){return!(e instanceof AggregateError)&&hasSqliteWorkerErrorCode(e,t)}function hasSqliteWorkerOutcomeUnknown(e){let t=[e],n=/* @__PURE__ */ new Set;for(;t.length>0;){let e=t.pop();if(n.has(e)||l(e)||!c(e))continue;if(n.add(e),Object.getOwnPropertyDescriptor(e,d)?.value===`outcome-unknown`)return!0;let r=Object.getOwnPropertyDescriptor(e,`cause`);r&&`value`in r&&t.push(r.value);let i=Object.getPrototypeOf(e),a=!1;for(;i&&!l(i);){let e=Object.getOwnPropertyDescriptor(i,`constructor`)?.value;if(i===AggregateError.prototype||typeof e==`function`&&!l(e)&&Object.getOwnPropertyDescriptor(e,`name`)?.value===`AggregateError`&&Object.getOwnPropertyDescriptor(e,`prototype`)?.value===i&&Object.getOwnPropertyDescriptor(i,`name`)?.value===`AggregateError`){a=!0;break}i=Object.getPrototypeOf(i)}if(!a)continue;let o=Object.getOwnPropertyDescriptor(e,`errors`)?.value;if(!l(o)&&Array.isArray(o))for(let e of Object.keys(o)){if(!/^(0|[1-9][0-9]*)$/.test(e))continue;let n=Object.getOwnPropertyDescriptor(o,e);n&&`value`in n&&t.push(n.value)}}return!1}function preparePostgresQuery(e){let t=``,n=0,r=!0,i,a;for(let o=0;o<e.length;){let s=e.slice(o),c=/^(?:[eE]'(?:\\[\s\S]|''|[^'\\])*'|'(?:''|[^'])*'|"(?:""|[^"])*"|`(?:``|[^`])*`|\[[^\]]*\]|--[^\n]*(?:\n|$))/.exec(s),l=/^\$(?:[A-Za-z_][A-Za-z_0-9]*)?\$/.exec(s)?.[0];if(s.startsWith(`/*`)){let n=o+2,r=1;for(;n<e.length&&r>0;)e.startsWith(`/*`,n)?(r++,n+=2):e.startsWith(`*/`,n)?(r--,n+=2):n++;t+=e.slice(o,n),o=n}else if(l){let n=e.indexOf(l,o+l.length),a=n<0?e.length:n+l.length;t+=e.slice(o,a),o=a,i=void 0,r=!1}else if(c)t+=c[0],o+=c[0].length,c[0].startsWith(`--`)||(i=void 0,r=!1);else{if(r&&/^pragma\b/i.test(s))throw Error(`The PostgreSQL engine does not support PRAGMA statements`);let c=/^(?:\d[\d_]*(?:\.[\d_]*)?|\.[\d_]+)(?:[eE][+-]?[\d_]+)?/.exec(s)?.[0];if(c){t+=c,o+=c.length,i=void 0,r=!1;continue}let l=/^[A-Za-z_][A-Za-z_0-9$]*/.exec(s)?.[0];if(l){let e=l.toUpperCase();i!==`AS`&&i!==`.`&&(e===`INTO`&&i!==`INSERT`&&i!==`MERGE`?a=`unscoped`:a===void 0&&/^(?:INSERT|UPDATE|DELETE|MERGE)$/.test(e)&&(e!==`UPDATE`||i!==`FOR`&&i!==`KEY`)&&(a=`scoped`)),i=e,t+=l,o+=l.length,r=!1;continue}let u=e.charAt(o);/\s/.test(u)||(i=u),u===`?`&&e[o+1]===`?`?(t+=`?`,o+=2):u===`?`?(t+=`$${++n}`,o++):(t+=u,o++),u===`;`?r=!0:/\s/.test(u)||(r=!1)}}return{sql:t,mutation:a}}function validatePostgresBindings(e){for(let t of e)if(t!==null&&typeof t!=`string`&&typeof t!=`number`&&typeof t!=`bigint`&&!ArrayBuffer.isView(t))throw TypeError(`SQL bindings must be null, numbers, bigints, strings, or ArrayBuffer views`)}function decodePostgresRows(e,t){return e.rows.map(n=>Object.fromEntries(e.fields.map(e=>{let r=n[e.name],i;if(r===null)i=null;else if(e.dataTypeID===20){if(typeof r!=`string`)throw TypeError(`PostgreSQL int8 results must be text`);let e=BigInt(r);if(t)i=e;else{let t=Number(e);if(!Number.isSafeInteger(t))throw RangeError(`PostgreSQL int8 ${e} cannot be represented safely as a number`);i=t}}else if(r instanceof Uint8Array)i=new Uint8Array(r);else if(typeof r==`boolean`)i=Number(r);else if(typeof r==`string`||typeof r==`number`||typeof r==`bigint`)i=r;else throw TypeError(`Unsupported PostgreSQL result type for column ${e.name}`);return[e.name,i]})))}function isPostgresConnectionError(e){return!e||e.startsWith(`08`)||/^57P0[123]$/.test(e)}function decodePostgresError(e){let t=Object.assign(Error(e.message),e);e.code===`57014`&&(t.name=`PostgresQueryCanceledError`);let n={23505:[2067,`UNIQUE`],23503:[787,`FOREIGN KEY`],23502:[1299,`NOT NULL`],23514:[275,`CHECK`]}[e.code??``];return n?Object.assign(Error(`${n[1]} constraint failed: ${e.constraint??e.column??`unknown`}`,{cause:t}),{code:`ERR_SQLITE_ERROR`,errcode:n[0]}):t}const f=[[`SQLITE_BUSY`,`database is locked`,5],[`SQLITE_LOCKED`,`database table is locked`,6],[`SQLITE_READONLY`,`attempt to write a readonly database`,8],[`SQLITE_IOERR`,`disk I/O error`,10],[`SQLITE_IOERR`,`unable to open database file`,14],[`SQLITE_FULL`,`database or disk is full`,13],[`transcript_writer_fenced`,`session writer claim changed before transcript persistence`,-1],[`session_actor_reconciliation`,`Session actor requires reconciliation before another command`,-2]];function readOwnErrorDataProperty(e,t){try{return Object.getOwnPropertyDescriptor(e,t)?.value}catch{return}}function isStateDatabaseAdmissionClosedError(t){return o(t).some(t=>{let n=typeof t==`string`?{message:t}:e(t)?t:{},r=readOwnErrorDataProperty(n,`errorCode`)??readOwnErrorDataProperty(n,`code`),i=readOwnErrorDataProperty(n,`errorMessage`)??readOwnErrorDataProperty(n,`message`);return r===`STATE_DATABASE_READ_ADMISSION_INVALIDATED`||typeof i==`string`&&i.includes(`Shared-state worker is retiring`)})}function classifyGatewayStorageFailure(e){let t=e,n=!1,r,i=/* @__PURE__ */ new Set;for(let a=e;a instanceof Error&&!i.has(a);){i.add(a),n||=hasSqliteWorkerErrorCode(a,`outcome-unknown`),isSqliteWorkerError(a,`unavailable`)&&(r=`unavailable`);let e=readOwnErrorDataProperty(a,`cause`);if(a instanceof AggregateError){let n=readOwnErrorDataProperty(a,`errors`);if(!(e instanceof Error)||!Array.isArray(n)||readOwnErrorDataProperty(n,`0`)!==e)break;t=e}a=e}if(n||hasSqliteWorkerOutcomeUnknown(t))return`session_actor_reconciliation`;let a,s;for(let e of o(t)){let t=classifyStorageFailure(e);if(t===`session_actor_reconciliation`)s=t;else if(t===`57014`)a=t;else if(t===`unavailable`)r=t;else if(t)return t}return r??s??a??(hasSqliteWorkerOutcomeUnknown(e)?`session_actor_reconciliation`:void 0)}function classifyStorageFailure(t){if(isSqliteWorkerError(t,`unavailable`))return`unavailable`;let n=typeof t==`string`?{message:t}:e(t)?t:{},r=readOwnErrorDataProperty(n,`errorCode`)??readOwnErrorDataProperty(n,`code`);if(r===`unavailable`&&readOwnErrorDataProperty(n,`errorType`)===`SqliteWorkerError`)return`unavailable`;if(readOwnErrorDataProperty(n,`errorType`)===`PostgresQueryCanceledError`||readOwnErrorDataProperty(n,`name`)===`PostgresQueryCanceledError`)return`57014`;if(r===`ENOSPC`)return`SQLITE_FULL`;if(r===`EIO`||typeof r==`string`&&(r===`SQLITE_CANTOPEN`||r.startsWith(`SQLITE_CANTOPEN_`)))return`SQLITE_IOERR`;let i=readOwnErrorDataProperty(n,`errcode`),a=typeof i==`number`&&Number.isInteger(i)&&i>=0?i&255:void 0;return(f.find(([e,,t])=>a===t||typeof r==`string`&&(r===e||e.startsWith(`SQLITE_`)&&r.startsWith(`${e}_`)))??f.find(([,e])=>[readOwnErrorDataProperty(n,`errstr`),readOwnErrorDataProperty(n,`errorMessage`),readOwnErrorDataProperty(n,`message`)].some(t=>typeof t==`string`&&t.trim()===e)))?.[0]}const p={source:`opening the source database`,snapshot:`creating its private snapshot`},m=n(Symbol.for(`openclaw.sqliteInspectionOperations`),()=>/* @__PURE__ */ new WeakMap),h=n(Symbol.for(`openclaw.sqliteNativeOpenFailures`),()=>/* @__PURE__ */ new WeakSet),g=n(Symbol.for(`openclaw.sqliteSnapshotFailureScopes`),()=>new s);function recordSqliteSnapshotFailure(e){let t=g.getStore();t&&(t.failure??={error:e})}function markSqliteNativeOpenFailure(e){typeof e==`object`&&e&&h.add(e)}function withSqliteNativeOpen(e){try{return e()}catch(e){throw markSqliteNativeOpenFailure(e),e}}function isSqliteNativeOpenFailure(e){return typeof e==`object`&&!!e&&h.has(e)}function markSqliteInspectionOperation(e,t){return typeof e==`object`&&e&&!m.has(e)&&m.set(e,t),e}function withSqliteInspectionOperation(e,t){try{return t()}catch(t){throw markSqliteInspectionOperation(t,e)}}function formatSqliteReadOnlyInspectionFailure(e){let t=a(e),{suffix:n,operation:r}=readSqliteErrorDetails(e),i=`${t}${n}`;return r===void 0?i:`failed while ${p[r]}: ${i}`}function readSqliteInspectionOperation(e){return readSqliteErrorDetails(e).operation}function readSqliteErrorDetails(t){let n=/* @__PURE__ */ new Set,r;for(let a=t,o=0;o<8&&e(a);o+=1){r=m.get(a)??r;let e=i(a);e&&/^[A-Z0-9_]{1,64}$/u.test(e)&&n.add(`code=${e}`);let{errcode:t}=a;typeof t==`number`&&Number.isInteger(t)&&t>=0&&t<=2147483647&&n.add(`errcode=${t}`),a=a.cause}return{suffix:n.size>0?` (${[...n].join(`, `)})`:``,operation:r}}const _=/* @__PURE__ */ new Set([`SQLITE_BUSY`,`SQLITE_LOCKED`]);function sqliteErrorCode(e){let n=t(e)?.code;return typeof n==`string`?n:void 0}function sqliteExtendedResultCode(e){let n=t(e)?.errcode;return typeof n==`number`&&Number.isInteger(n)?n:void 0}function sqlitePrimaryResultCode(e){let t=sqliteExtendedResultCode(e);return t===void 0?void 0:t&255}function isSqliteLockError(e){let t=sqliteErrorCode(e);if(t!==void 0&&_.has(t))return!0;let n=sqlitePrimaryResultCode(e);return n===5||n===6}function isSqliteCorruptionError(e){let t=sqlitePrimaryResultCode(e);return t===11||t===26}export{hasSqliteWorkerOutcomeUnknown as C,wrapSqliteWorkerError as E,hasSqliteWorkerErrorCode as S,retainSqliteWorkerErrorCode as T,decodePostgresRows as _,isSqliteNativeOpenFailure as a,validatePostgresBindings as b,markSqliteNativeOpenFailure as c,sqliteErrorCode as d,sqliteExtendedResultCode as f,decodePostgresError as g,withSqliteNativeOpen as h,isSqliteLockError as i,readSqliteInspectionOperation as l,withSqliteInspectionOperation as m,formatSqliteReadOnlyInspectionFailure as n,isStateDatabaseAdmissionClosedError as o,sqlitePrimaryResultCode as p,isSqliteCorruptionError as r,markSqliteInspectionOperation as s,classifyGatewayStorageFailure as t,recordSqliteSnapshotFailure as u,isPostgresConnectionError as v,isSqliteWorkerError as w,SqliteWorkerError as x,preparePostgresQuery as y};