UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 9.27 kB
import{t as e}from"./transcript-global-singleton-DIJgobpU.mjs";import{V as t}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{C as n,c as r,g as i}from"./transcript-agent-creation-claim-Da8lP1e6.mjs";import{ct as a}from"./transcript-sqlite-database-admission-IPmtadyc.mjs";import{p as o}from"./transcript-sqlite-error-diagnostics-CLL4Z7ly.mjs";import{t as s}from"./transcript-subsystem-C0UTtuvS.mjs";import{a as c,i as l,ot as u,r as d,u as f}from"./transcript-sqlite-readonly-worker-cXU3LbpW.mjs";import{t as p}from"./transcript-deferred-B75K7ut7.mjs";import"./transcript-sqlite-post-commit-B_pko32i.mjs";import{h as m}from"./transcript-sqlite-user-version-CMolZa8i.mjs";import{a as h}from"./transcript-kysely-sync-cache-state-DHCXK7mb.mjs";import{o as g,p as _,t as v}from"./transcript-sqlite-schema-facts-C1_zV4Yg.mjs";import{n as y}from"./transcript-agent-id-DmscQLD6.mjs";import{c as b,r as x}from"./transcript-openclaw-agent-db.paths-DbFl2s5M.mjs";import{n as S}from"./transcript-agent-deletion-journal-status-BsvZaPJC.mjs";import{o as C}from"./transcript-sqlite-transaction-CBQXI8WC.mjs";import{a as w}from"./transcript-runtime-binary-4JwD1Zy9.mjs";import{r as T,t as E}from"./transcript-runtime-worker-url-Bi-zitXH.mjs";import{t as D}from"./transcript-runtime-process-url-DysJEE9m.mjs";import{o as O}from"./transcript-openclaw-agent-db-resources-W6NYYBYU.mjs";import{n as ee,r as k}from"./transcript-openclaw-quarantine-store-DP1LZEnM.mjs";import{t as A}from"./transcript-sqlite-integrity-DC7pAsT6.mjs";import{_ as j}from"./transcript-sqlite-private-snapshot-fork-CKFbjfTN.mjs";import{n as M,t as N}from"./transcript-node-sqlite-RfCz-ZxM.mjs";import{r as P,t as F}from"./transcript-child-runtime-viability-BK7RH2CJ.mjs";import{s as I}from"./transcript-openclaw-state-db-schema-helpers-B1k_oHgC.mjs";import{r as L}from"./transcript-openclaw-agent-db-metadata-CRoaperl.mjs";import{r as R}from"./transcript-sqlite-handle-lifecycle-fOjKc_YY.mjs";import{o as z}from"./transcript-openclaw-agent-db-identity-BPgUuROx.mjs";import{C as B,b as V,c as H,t as U,v as W,x as G}from"./transcript-openclaw-agent-db-validation-cache-CKkKL5V4.mjs";import{n as K,t as q}from"./transcript-openclaw-agent-db-engine-CCFr9cWR.mjs";import{i as J,r as Y,t as X}from"./transcript-openclaw-agent-db-schema-read-Bseq6-pe.mjs";import{t as Z}from"./transcript-native-error-response-CX3XH2xm.mjs";import{isMainThread as Q}from"node:worker_threads";import{AsyncLocalStorage as te}from"node:async_hooks";import ne from"node:fs";import{performance as $}from"node:perf_hooks";import{fork as re,spawnSync as ie}from"node:child_process";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 ae=new te,oe=s(`state/sqlite`);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,n,r,i=e,a,o){let s=ae.getStore(),run=async s=>{let u=c(r)??r;a&&(delete a.workerCheckElapsedMs,delete a.workerLifetimeElapsedMs),u.throwIfAborted(),s?.assertCurrent();let m=j(e),{timeoutMs:h,size:g}=l(`integrity check`,i,m.size),_=$.now(),v=s?.queue.process;if(!v||v.retired){let e=D(`sqliteIntegrity`);F();let n=re(e,[],{execArgv:E(e).slice(0,-1),serialization:`advanced`,stdio:[`ignore`,`ignore`,`pipe`,`ipc`],timeout:s||d()?void 0:h,killSignal:`SIGKILL`,...s?{}:{signal:u}}),r=P(n),{promise:i,resolve:a}=p();v={child:n,closed:i,retired:!1,closeBudgetMs:h};let o=v;n.on(`error`,e=>{o.failure=t(e),o.retired=!0,n.kill(`SIGKILL`)}),n.once(`close`,()=>{o.runtimeFailure=r(),o.retired=!0,a()}),s&&(s.queue.process=v)}v.closeBudgetMs=h;let y=v,b=y.child;return new Promise((r,c)=>{let l,p,v=`starting`,x,S=setInterval(()=>{oe.info(`SQLite integrity check still running: ${i} (${g}, ${Math.floor(($.now()-_)/1e3)}s elapsed, phase=${v}).`)},1e4);S.unref();let deadlineError=()=>{let e=f(`integrity check`,i,h,g);return e.message+=` (lastObservedPhase=${v})`,e},kill=()=>{y.retired=!0,b.kill(`SIGKILL`)},onAbort=()=>{p=t(u.reason),kill()},finish=(n,i)=>{if(clearTimeout(x),clearInterval(S),u.removeEventListener(`abort`,onAbort),b.off(`message`,onMessage),b.off(`close`,finish),a){a.workerLifetimeElapsedMs=$.now()-_;let e=l?.checkElapsedMs;typeof e==`number`&&Number.isFinite(e)&&e>=0&&(a.workerCheckElapsedMs=e)}try{u.throwIfAborted(),s?.assertCurrent();let t=y.runtimeFailure??p??y.failure;if(t)throw t;if(b.killed&&i===`SIGKILL`)throw deadlineError();if(n!==0||!l)throw!l&&i?new SqliteIntegrityWorkerInterruptedError(i,v):Error(`SQLite integrity worker exited ${n} without a completed check (lastObservedPhase=${v})`);if(j(e,m),!l.ok){let e=Z(l.error);if(e.name===`SqliteIntegrityError`){let t=new A(e.message,e.cause,l.error.findings);throw t.message=e.message,t}throw e}r()}catch(e){s&&!y.retired?(kill(),y.closed.then(()=>c(t(e)))):c(t(e))}},onMessage=e=>{`type`in e&&e.type===`phase`?!l&&(e.phase===`opening`||e.phase===`checking`||e.phase===`closing`)&&(v=e.phase):`ok`in e&&(l=e,v=`result-received`,s&&(e.ok&&!y.retired&&!p&&!y.failure?finish(0,null):y.retired=!0))};b.on(`message`,onMessage),b.once(`close`,finish),s&&(u.addEventListener(`abort`,onAbort,{once:!0}),d()||(x=setTimeout(()=>{(!l||l.ok)&&(p??=y.failure??deadlineError()),kill()},h),x.unref())),u.aborted?s&&onAbort():b.send({pathname:e,databaseLabel:i,identity:m,busyTimeoutMs:n,integrity:o,...s?{reuse:!0}:{}},e=>{e&&(p=e,kill())})})};if(!s)return run();if(!s.accepting)return Promise.reject(/* @__PURE__ */ Error(`SQLite integrity maintenance scope is closed`));let u=s.queue.tail.then(async()=>{try{s.queue.process?.retired&&await s.queue.process.closed,await run(s)}catch(e){throw s.queue.process&&await closeIntegrityProcess(s.queue.process),e}});return s.queue.tail=u.catch(()=>void 0),s.pending.add(u),u.finally(()=>s.pending.delete(u)).catch(()=>void 0),u}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=T(w.databaseVerify),i=ie(process.execPath,[...E(r),`--openclaw-database-verify-sync`,e],{timeout:l(`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,m,n),t?.(),r.peek(e)===i&&r.clear(e)),t?.(),assertAgentDatabaseTerminalOpenAllowed(e)}function readOpenClawAgentDatabase(e,t){try{return{found:!0,value:t(e)}}catch(t){throw o(t)===1?G(e.db,t):t}}function hasAdmittedAgentReadOnlySchema(e){let t=J(e.db,e.path),n=L(e.db);return X(e.db,e.path,t),n?(Y(n,e.agentId,e.path),V(e.db),!0):!1}function readOpenClawAgentDatabaseSnapshot(e,t){return{found:!0,value:C(e.db,()=>readOpenClawAgentDatabase(e,t).value)}}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,t={}){let o=q();o&&R();let s=y(e.agentId),c=b({...e,agentId:s});if(x(c,{agentId:s,env:e.env}))return{found:!1,reason:`database-missing`};if(n(c)){let t=i(e.env??process.env),a=r(s,t);if(!a||!n(c,a)||S(t,s)()!==`complete`)return{found:!1,reason:`database-missing`}}if(!ne.existsSync(c))return{found:!1,reason:`database-missing`};revalidateAgentDatabaseTerminalOpen(c);let l=ee(`agent`,c,{env:e.env});if(l)throw B(l),l;let d;try{d=o?N(M(c),{timeout:m}):k(c,{timeout:m,...t.allowExtension?{allowExtension:!0}:{}})}catch(e){throw B(e),e}let f=!1,p=d,C,close=()=>{f||(p!==d&&p.isOpen&&p.close(),d.isOpen&&d.close(),f=!0,C?.(),C=void 0)};try{let e;if(o)p=K(d,s,c,!0,(e,t)=>{throw p=e,t},!0),z(p),e={agentId:s,db:p,path:c,close};else{h(d),z(d);let t=u(`agent`,c)?d.location():void 0;if(e={agentId:s,db:d,path:t??c,close},!(U(e)||_(d,()=>{if(v(d),!hasAdmittedAgentReadOnlySchema(e))return!1;let t=g(e.db);if(t&&a(t.tableSql))throw new I(`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`};Q||(W(e),H(e))}return t.lifecycle===`agent`&&(C=O({agentId:s,path:c,revoke:close,close})),{found:!0,database:e}}catch(e){throw close(),B(e),e}}export{assertAgentDatabaseTerminalOpenAllowed as a,assertSqliteIntegrityInWorker as c,withFreshOpenClawAgentDatabaseReadOnly as i,readOpenClawAgentDatabase as n,revalidateAgentDatabaseTerminalOpen as o,readOpenClawAgentDatabaseSnapshot as r,revalidateAgentDatabaseTerminalOpenAsync as s,openOpenClawAgentDatabaseReadOnly as t};