UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 11.1 kB
import{t as e}from"./transcript-global-singleton-DIJgobpU.mjs";import{l as t,p as n}from"./transcript-openclaw-agent-search-schema-BQwqsKhe.mjs";import{a as r}from"./transcript-agent-database-readers-C97uH1F6.mjs";import{ht as i,j as a}from"./transcript-sqlite-database-admission-Cro6ZHgY.mjs";import{p as o}from"./transcript-sqlite-error-diagnostics-Bzt01wuM.mjs";import{t as s}from"./transcript-subsystem-er2pCebD.mjs";import{D as c,N as l,O as u,R as d}from"./transcript-sqlite-table-layout-migration-6FYoXQ_h.mjs";import{t as f}from"./transcript-deferred-B75K7ut7.mjs";import"./transcript-sqlite-post-commit-BDZjIt2B.mjs";import{s as p}from"./transcript-openclaw-state-db-contract-DiFTYPe3.mjs";import{a as m}from"./transcript-kysely-sync-cache-state-9JTloBF9.mjs";import{o as h,p as g,t as _}from"./transcript-sqlite-schema-facts-C4Tsn8VX.mjs";import{n as v}from"./transcript-agent-id-DmscQLD6.mjs";import{l as y,r as b}from"./transcript-openclaw-agent-db.paths-5mfY5--s.mjs";import{s as x}from"./transcript-openclaw-state-db.paths-DYRyvCKa.mjs";import{n as S}from"./transcript-artifact-preserving-state-reads-BHzs8nJG.mjs";import{_ as C,l as w}from"./session-transcript.worker.js";import{a as T,c as ee}from"./transcript-sqlite-transaction-BxeoS_ke.mjs";import{n as E}from"./transcript-sqlite-busy-timeout-DspDnAK7.mjs";import{l as D,r as O,t as k}from"./transcript-runtime-worker-url-D1R86-Xb.mjs";import{t as A}from"./transcript-runtime-process-url-BahjIwf7.mjs";import{E as te,H as j,i as ne,l as M,r as N,t as P}from"./transcript-openclaw-agent-db-schema-read-0j86l7zG.mjs";import{m as F,n as I,p as L,r as R}from"./transcript-openclaw-quarantine-store-Cpfq2f44.mjs";import{l as z,s as B}from"./transcript-child-CaF3s4Mr.mjs";import{n as V,t as H}from"./transcript-node-sqlite-dPq0UNII.mjs";import{s as U}from"./transcript-openclaw-state-db-schema-helpers-DTvLbSq6.mjs";import{s as W}from"./transcript-openclaw-agent-db-identity-BwKDyjmN.mjs";import{C as G,b as K,c as q,t as J,v as Y,x as X}from"./transcript-openclaw-agent-db-validation-cache-CPj39b9d.mjs";import{n as re,t as ie}from"./transcript-openclaw-agent-db-engine-YRrMbR9J.mjs";import{deserialize as ae,serialize as oe}from"node:v8";import{isMainThread as se}from"node:worker_threads";import{AsyncLocalStorage as ce}from"node:async_hooks";import Z from"node:fs";import{performance as Q}from"node:perf_hooks";import{fork as le,spawnSync as $}from"node:child_process";function nativeErrorDetails(e){let t=e;return{message:e.message,code:t.code,errcode:t.errcode}}function serializeNativeErrorResponse(e){let t=n(e);return{name:t.name,...nativeErrorDetails(t),...t.cause instanceof Error?{cause:nativeErrorDetails(t.cause)}:{}}}function restoreNativeErrorResponse(e){let t=e.cause?Object.assign(Error(e.cause.message),e.cause):void 0;return Object.assign(Error(e.message,t?{cause:t}:void 0),{name:e.name,code:e.code,errcode:e.errcode})}var SqliteIntegrityWorkerInterruptedError=class extends Error{constructor(e,t){super(`SQLite integrity check stopped by ${e} before completion (lastObservedPhase=${t})`),this.signal=e,this.name=`SqliteIntegrityWorkerInterruptedError`}};const ue=new ce,de=s(`state/sqlite`);function runSqliteIntegrityCheckInWorkerSync(e){let t=L(e.databaseLabel),n=A(`sqliteIntegrity`),r=Q.now();B();let i=$(process.execPath,[...k(n),`--check-sync`],{input:oe({pathname:e.databaseLabel,databaseLabel:e.databaseLabel,identity:t,busyTimeoutMs:E(e.database),integrity:{check:e.check,schemaOnly:e.schemaOnly,tables:e.tables,ftsSchemas:e.ftsSchemas}}),timeout:u(`integrity check`,e.databaseLabel,t.size).timeoutMs,killSignal:`SIGKILL`,maxBuffer:1048576});if(e.timing&&(e.timing.workerLifetimeElapsedMs=Q.now()-r),i.error)throw i.error;if(i.status!==0||!i.stdout.length)throw Error(`SQLite integrity worker exited ${i.status} (${i.signal??`no signal`})`);let o=ae(i.stdout);if(L(e.databaseLabel,t),e.timing&&(e.timing.workerCheckElapsedMs=o.checkElapsedMs),!o.ok){let e=restoreNativeErrorResponse(o.error);if(e.name===`SqliteIntegrityError`){let t=new j(e.message,e.cause,o.error.findings);throw t.message=e.message,t}throw e}e.check!==`quick`&&!o.staleFts&&a(e.database)}async function closeIntegrityProcess(e){e.retired||(e.retired=!0,e.child.connected&&e.child.send({type:`close`},t=>{t&&e.child.kill(`SIGKILL`)}));let t=setTimeout(()=>e.child.kill(`SIGKILL`),e.closeBudgetMs);t.unref();try{await e.closed}finally{clearTimeout(t)}}function assertSqliteIntegrityInWorker(e,t,r,i=e,a,o,s){let p=ue.getStore(),run=async p=>{let m=d(r)??r;a&&(delete a.workerCheckElapsedMs,delete a.workerLifetimeElapsedMs),m.throwIfAborted(),p?.assertCurrent();let h=s?.preparedIdentity??await F(e,void 0,s?.expectedIdentity);m.throwIfAborted(),p?.assertCurrent();let{timeoutMs:g,size:_}=u(`integrity check`,i,h.size),v=Q.now(),y=p?.queue.process;if(!y||y.retired){let e=A(`sqliteIntegrity`);B();let t=le(e,[],{execArgv:k(e).slice(0,-1),serialization:`advanced`,stdio:[`ignore`,`ignore`,`pipe`,`ipc`],timeout:p||c()?void 0:g,killSignal:`SIGKILL`,...p?{}:{signal:m}}),r=z(t),{promise:i,resolve:a}=f();y={child:t,closed:i.then(async()=>{o.runtimeFailure=await r().catch(n)}),retired:!1,closeBudgetMs:g};let o=y;t.on(`error`,e=>{o.failure=n(e),o.retired=!0,t.kill(`SIGKILL`)}),t.once(`close`,()=>{o.retired=!0,a()}),p&&(p.queue.process=y)}y.closeBudgetMs=g;let b=y,x=b.child;return new Promise((r,u)=>{let d,f,y=`starting`,S,C=setInterval(()=>{de.info(`SQLite integrity check still running: ${i} (${_}, ${Math.floor((Q.now()-v)/1e3)}s elapsed, phase=${y}).`)},1e4);C.unref();let deadlineError=()=>{let e=l(`integrity check`,i,g,_);return e.message+=` (lastObservedPhase=${y})`,e},kill=()=>{b.retired=!0,x.kill(`SIGKILL`)},onAbort=()=>{f=n(m.reason),kill()},finish=async(t,i,o=!1)=>{if(clearTimeout(S),clearInterval(C),m.removeEventListener(`abort`,onAbort),x.off(`message`,onMessage),x.off(`close`,onClose),a){a.workerLifetimeElapsedMs=Q.now()-v;let e=d?.checkElapsedMs;typeof e==`number`&&Number.isFinite(e)&&e>=0&&(a.workerCheckElapsedMs=e)}try{o&&await b.closed,m.throwIfAborted(),p?.assertCurrent();let n=b.runtimeFailure??f??b.failure;if(n)throw n;if(x.killed&&i===`SIGKILL`)throw deadlineError();if(t!==0||!d)throw!d&&i?new SqliteIntegrityWorkerInterruptedError(i,y):Error(`SQLite integrity worker exited ${t} without a completed check (lastObservedPhase=${y})`);if(await F(e,h,s?.expectedIdentity),m.throwIfAborted(),p?.assertCurrent(),!d.ok){let e=restoreNativeErrorResponse(d.error);if(e.name===`SqliteIntegrityError`){let t=new j(e.message,e.cause,d.error.findings);throw t.message=e.message,t}throw e}d.staleFts&&s?.onToleratedFts?.(),r()}catch(e){p&&!b.retired?(kill(),b.closed.then(()=>u(n(e)))):u(n(e))}},onClose=(e,t)=>{finish(e,t,!0)},onMessage=e=>{`type`in e&&e.type===`phase`?d||e.phase!==`opening`&&e.phase!==`checking`&&e.phase!==`closing`||(y=e.phase):`ok`in e&&(d=e,y=`result-received`,p&&(e.ok&&!b.retired&&!f&&!b.failure?finish(0,null):b.retired=!0))};x.on(`message`,onMessage),x.once(`close`,onClose),p&&(m.addEventListener(`abort`,onAbort,{once:!0}),c()||(S=setTimeout(()=>{(!d||d.ok)&&(f??=b.failure??deadlineError()),kill()},g),S.unref())),m.aborted?p&&onAbort():x.send({pathname:e,databaseLabel:i,identity:h,busyTimeoutMs:t,integrity:o,...s?.priority===`background`?{background:!0}:{},...p?{reuse:!0}:{}},e=>{e&&(f=e,kill())})})};if(!p)return run();if(!p.accepting)return Promise.reject(/* @__PURE__ */ Error(`SQLite integrity maintenance scope is closed`));let m=p.queue.tail.then(async()=>{try{p.queue.process?.retired&&await p.queue.process.closed,await run(p)}catch(e){throw p.queue.process&&await closeIntegrityProcess(p.queue.process),e}});return p.queue.tail=m.catch(()=>void 0),p.pending.add(m),m.finally(()=>p.pending.delete(m)).catch(()=>void 0),m}function terminalLatch(){return e(Symbol.for(`openclaw.agentDatabaseLifecycle`))?.terminal}function assertAgentDatabaseTerminalOpenAllowed(e){let t=terminalLatch()?.get(e);if(t)throw t}function revalidateAgentDatabaseTerminalOpen(e){let t=terminalLatch(),n=t?.peek(e);if(t&&n?.name===`SqliteIntegrityError`){let r=O(D.databaseVerify),i=$(process.execPath,[...k(r),`--openclaw-database-verify-sync`,e],{timeout:u(`integrity check`,e).timeoutMs,killSignal:`SIGKILL`,stdio:`ignore`});if(i.error||i.status!==0)throw n;t.clear(e)}assertAgentDatabaseTerminalOpenAllowed(e)}async function revalidateAgentDatabaseTerminalOpenAsync(e,t,n=new AbortController().signal){let r=terminalLatch(),i=r?.peek(e);r&&i?.name===`SqliteIntegrityError`&&(await assertSqliteIntegrityInWorker(e,p,n),t?.(),r.peek(e)===i&&r.clear(e)),t?.(),assertAgentDatabaseTerminalOpenAllowed(e)}function readOpenClawAgentDatabase(e,t){try{return g(e.db,()=>hasAdmittedAgentReadOnlySchema(e)?{found:!0,value:t(e)}:{found:!1,reason:`schema-missing`})}catch(t){throw o(t)===1?X(e.db,t):t}}function hasAdmittedAgentReadOnlySchema(e){let t=ne(e.db,e.path),n=M(e.db);return P(e.db,e.path,t),n?(N(n,e.agentId,e.path),K(e.db),!0):!1}function readOpenClawAgentDatabaseSnapshot(e,t){let n;return T(e.db,()=>(n=readOpenClawAgentDatabase(e,t),n.found?n.value:void 0)),n}function withFreshOpenClawAgentDatabaseReadOnly(e,t,n={}){let r=openOpenClawAgentDatabaseReadOnly(t,n);if(!r.found)return r;try{return n.snapshot?readOpenClawAgentDatabaseSnapshot(r.database,e):readOpenClawAgentDatabase(r.database,e)}finally{r.database.close()}}function openOpenClawAgentDatabaseReadOnly(e,n={}){let a=ie();a&&t();let o=v(e.agentId),s=y({...e,agentId:o});if(b(s,{agentId:o,env:e.env}))return{found:!1,reason:`database-missing`};if(r(s)){let t=x(e.env??process.env),n=C(o,t);if(!n||!r(s,n)||w(t,o)()!==`complete`)return{found:!1,reason:`database-missing`}}if(!Z.existsSync(s))return{found:!1,reason:`database-missing`};revalidateAgentDatabaseTerminalOpen(s);let c=I(`agent`,s,{env:e.env});if(c)throw G(c),c;let l;try{l=a?H(V(s),{timeout:p}):R(s,{timeout:p,...n.allowExtension?{allowExtension:!0}:{}})}catch(e){throw G(e),e}let u=!1,d=l,f,close=()=>{u||(d!==l&&d.isOpen&&d.close(),l.isOpen&&l.close(),u=!0,f?.(),f=void 0)};try{let e;if(a)d=re(l,o,s,!0,(e,t)=>{throw d=e,t},!0),W(d),e={agentId:o,db:d,path:s,close};else{ee(l),m(l),W(l);let t=S(`agent`,s)?l.location():void 0;if(e={agentId:o,db:l,path:t??s,close},!(J(e)||g(l,()=>{if(_(l),!hasAdmittedAgentReadOnlySchema(e))return!1;let t=h(e.db);if(t&&i(t.tableSql))throw new U(`OpenClaw agent database ${e.path} requires writable startup admission before session reads; restart the Gateway or run openclaw doctor --fix.`);return!0})))return close(),{found:!1,reason:`schema-missing`};se||(Y(e),q(e))}return n.lifecycle===`agent`&&(f=te({agentId:o,path:s,revoke:close,close})),{found:!0,database:e}}catch(e){throw close(),G(e),e}}export{assertAgentDatabaseTerminalOpenAllowed as a,assertSqliteIntegrityInWorker as c,serializeNativeErrorResponse as d,withFreshOpenClawAgentDatabaseReadOnly as i,runSqliteIntegrityCheckInWorkerSync as l,readOpenClawAgentDatabase as n,revalidateAgentDatabaseTerminalOpen as o,readOpenClawAgentDatabaseSnapshot as r,revalidateAgentDatabaseTerminalOpenAsync as s,openOpenClawAgentDatabaseReadOnly as t,restoreNativeErrorResponse as u};