openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.46 kB
JavaScript
import{n as e}from"./transcript-openclaw-agent-search-schema-BQwqsKhe.mjs";import{v as t}from"./transcript-plugin-instance-invocation-7pP9ZY8b.mjs";import{p as n}from"./transcript-sqlite-error-diagnostics-Bzt01wuM.mjs";import{t as r}from"./transcript-sqlite-jsonl-budget-error-DacbQ4UF.mjs";import{n as i}from"./transcript-sql-connection-CRz-dHXM.mjs";import{n as a}from"./transcript-session-transcript-projection-error-BeJK3y26.mjs";import{C as o}from"./session-transcript.worker.js";import{c as s,l as c,m as l,o as u,r as d}from"./transcript-kysely-sync-DQ_Yn8jW.mjs";import{t as f}from"./transcript-json-values-CmANsVU4.mjs";import{t as p}from"./transcript-openclaw-agent-db-identity-BwKDyjmN.mjs";import{x as m}from"./transcript-openclaw-agent-db-validation-cache-CPj39b9d.mjs";import{t as h}from"./transcript-openclaw-agent-db-readonly-open-Cx-tx28d.mjs";import"./transcript-openclaw-agent-db-readonly-DE1r7rt-.mjs";import{i as g}from"./transcript-session-accessor.sqlite-scope-helpers-B6vEQkYq.mjs";import{d as _,l as v}from"./transcript-session-accessor.sqlite-entry-read-DATS7lGC.mjs";import{r as y}from"./transcript-transcript-payload-C58U6XNI.mjs";import{r as b,t as x}from"./transcript-transcript-payload-read-BfcV5CyI.mjs";import{t as S}from"./transcript-sqlite-jsonl-budget-CgWzkWnk.mjs";import{r as C}from"./transcript-session-transcript-read-bytes-C7AU5F9V.mjs";import{t as w}from"./transcript-session-accessor.sqlite-transcript-state-DUR6CMiT.mjs";import{n as T}from"./transcript-session-transcript-read-fence-CgDWkLBX.mjs";import{i as E}from"./transcript-session-accessor.sqlite-read-7nEXcu6J.mjs";import{n as D}from"./transcript-session-accessor.sqlite-conversation-read-C_uxYiDH.mjs";import{t as O}from"./transcript-session-cold-storage-chunks-CB5Ergft.mjs";import{t as k}from"./transcript-text-bytes-Bs3XOZW4.mjs";const A=65536,j=1048576;function createSessionTranscriptHydrationStream(e,t,n){let r=[],i=[],a=0,o=0,flush=async()=>{r.length&&(n.throwIfCancelled(),await t({kind:`transcript-hydration-chunk`,encoding:e,frames:r},i),r=[],i=[],a=0,n.throwIfCancelled())},writeFrame=(e,t)=>{if(n.throwIfCancelled(),r.length&&a+e.data.byteLength>j)return flush().then(()=>writeFrame(e,t));if(t&&i.push(t),r.push(e),a+=e.data.byteLength,e.endOfEvent&&o++,a>=j||r.length>=128)return flush()},s=new TextEncoder;return{writeFrame,async writeJsonEvent(e){let t=s.encode(JSON.stringify(e));for(let e=0;;e+=A){let n=t.slice(e,e+A),r=n.byteLength<A,i=writeFrame({data:n,endOfEvent:r},n.buffer);if(i&&await i,r)return}},async finish(){return await flush(),n.throwIfCancelled(),o}}}function readSessionCompactionPreparation(e,t){let n=t.target.sessionKey??``,r=_(e,t.resolvedScope.sessionId)??n,i={agentId:t.resolvedScope.agentId,sessionId:t.resolvedScope.sessionId,sessionKey:r,storePath:t.database.path},a=v(e,n)?.entry,o=n===r?a:v(e,r)?.entry,[s]=D(e,{primarySession:{sessionId:i.sessionId,sessionKey:n}});return{sessionTarget:i,entry:a,runtimeEntry:o,primaryConversation:s}}async function streamSessionTranscriptHydration(_,v,D,j,M){return await D.runNativeSection(async()=>{let N=h(g(_.resolvedScope));if(!N.found)throw new a(N.reason);let{database:P}=N,F,I;try{_.expectedIdentity&&p(P,_.expectedIdentity),F=e(P.db);let n=T({database:P,..._.resolvedScope}),{version:a,coldArchive:o}=w(P,_.resolvedScope.sessionId),m=o?`UTF-8`:y(P.db),h=j?.(P),g=h?new Set(h.sequences):void 0,N=E(P,_.resolvedScope.sessionId,{..._.target,beforeEventSeq:n?.beforeRawSeq});if(h&&(N=N.where(`seq`,`in`,l`(SELECT CAST(value AS BIGINT) FROM ${f(P.db,l.val(JSON.stringify(h.sequences)))})`)),M!==void 0){if(o)throw new r(`Cold transcript preparation requires streamed host hydration`);S(P.db,N.select([`event_json`,`event_utf8_bytes`]).as(`events`),M,`Compaction materialized preparation byte limit`)}let eventSlice=(e,t=A)=>k(P.db,b(P.db),e,t),L=N.select(e=>[`seq`,e.case().when(e.and([e(`event_json`,`is`,null),e(`event_utf8_bytes`,`>=`,m===`UTF-8`?A:A/2)])).then(null).else(eventSlice(1)).end().as(`data`)]).orderBy(`seq`,`asc`),R=c(P.db,e=>N.select(eventSlice(e(e=>e.offset)).as(`data`)).where(`seq`,`=`,e(e=>e.seq))),z=c(P.db,e=>N.select(eventSlice(1,x*2).as(`data`)).where(`seq`,`=`,e(e=>e)));function*readRows(){if(o){let e=_.target.maxEventBytes,t=e!==void 0&&Number.isFinite(e)&&e>=0?Math.floor(e):void 0,i=0,a=0;for(let e of O(P.db,P.path,o))if(D.throwIfCancelled(),!(e.kind!==`event`||n&&e.row.seq>=n.beforeRawSeq)){if(t!==void 0&&(i+=Buffer.byteLength(e.row.event_json,`utf8`)+a,a=1,i>t))throw new r(`Trajectory transcript store is too large to export (at least ${i} bytes; limit ${t})`);(g?g.has(e.row.seq):_.afterSeq===void 0||e.row.seq>_.afterSeq)&&(yield{seq:e.row.seq,data:null,complete:Buffer.from(e.row.event_json,`utf8`)})}return}if(!i(P.db)){D.throwIfCancelled();let e=h?void 0:_.afterSeq;yield*u(P.db,e===void 0?L:L.where(`seq`,`>`,e));return}let e=4194304,a=e/64,c=d(P.db,(h||_.afterSeq===void 0?L:L.where(`seq`,`>`,_.afterSeq)).limit(63)).rows;if(yield*c,c.length<63)return;let l=s(P.db,e=>N.where(`seq`,`>`,e(e=>e)).select(`seq`).select(e=>e.fn(`least`,[e.fn.coalesce(C(),e.val(A)),e.val(A)]).as(`slice_bytes`)).orderBy(`seq`,`asc`).limit(a)),f=s(P.db,e=>L.where(`seq`,`>=`,e(e=>e.firstSeq)).where(`seq`,`<=`,e(e=>e.lastSeq))),p=t(c.at(-1),`initial transcript page`).seq;for(;;){D.throwIfCancelled();let n=l(p).rows;for(let t=0;t<n.length;){let r=t+1,i=n[t].slice_bytes+64;for(;r<n.length&&i+n[r].slice_bytes+64<=e;)i+=n[r].slice_bytes+64,r++;D.throwIfCancelled(),yield*f({firstSeq:n[t].seq,lastSeq:n[r-1].seq}).rows,t=r}if(n.length<a)return;p=t(n.at(-1),`transcript metadata page`).seq}}let B=createSessionTranscriptHydrationStream(m,v,D);for(let e of readRows()){let n=e.complete??e.data??t(z(e.seq),`compressed transcript snapshot row`).data;for(let r=1;;r+=A){D.throwIfCancelled();let i=e.data===null?Uint8Array.from(n.subarray(r-1,r-1+A)):void 0,a=i??(r===1?n:t(R({seq:e.seq,offset:r}),`transcript snapshot row`).data),o=a.byteLength<A,s=B.writeFrame({data:a,endOfEvent:o,...(h||_.includeEventJson)&&o?{seq:e.seq}:{}},i?.buffer);if(s&&await s,o)break}}let V=await B.finish();I={value:h&&`snapshot`in h?{kind:`streamed-bounded`,...h,eventCount:V}:{kind:`full`,version:a,eventCount:V}}}catch(e){I={error:n(e)===1?m(P.db,e):e}}try{try{F?.close()}finally{P.close()}}catch(e){throw`error`in I?o(I.error,e,`database close`):e}if(`error`in I)throw I.error;return I.value})}export{readSessionCompactionPreparation,streamSessionTranscriptHydration};