UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 5.67 kB
import{x as e}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{p as t}from"./transcript-sqlite-error-diagnostics-CLL4Z7ly.mjs";import{t as n}from"./transcript-sqlite-jsonl-budget-error-DacbQ4UF.mjs";import{u as r}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{n as i}from"./transcript-session-transcript-projection-error-BJpBXQR9.mjs";import{a}from"./transcript-session-history-worker-errors-CaYY_L96.mjs";import{c as o,l as s,o as c,p as l,r as u}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{t as d}from"./transcript-json-values-C_3KtpDU.mjs";import{t as f}from"./transcript-openclaw-agent-db-identity-BPgUuROx.mjs";import{x as p}from"./transcript-openclaw-agent-db-validation-cache-CKkKL5V4.mjs";import{t as m}from"./transcript-adapters-VitZFwpd.mjs";import{t as h}from"./transcript-openclaw-agent-db-readonly-open-BK-8SP7K.mjs";import"./transcript-openclaw-agent-db-readonly-BfbPmTwU.mjs";import{i as g}from"./transcript-session-accessor.sqlite-scope-helpers-LCM13IZK.mjs";import{f as _,u as v}from"./transcript-session-accessor.sqlite-entry-read-CL38_Mxj.mjs";import{f as y,m as b,n as x}from"./transcript-transcript-payload-F5t-l1WZ.mjs";import{r as S}from"./transcript-session-transcript-read-bytes-DJiUSe6g.mjs";import{t as C}from"./transcript-session-accessor.sqlite-transcript-state-CfLiYzkm.mjs";import"./transcript-session-accessor.sqlite-scope-6_lvO2un.mjs";import{n as w}from"./transcript-session-transcript-read-fence-Fb_G9Rx1.mjs";import{i as T}from"./transcript-session-accessor.sqlite-read-BJEDuBkE.mjs";import{n as E}from"./transcript-session-accessor.sqlite-conversation-read-CX-e9aVf.mjs";import{t as D}from"./transcript-session-cold-storage-chunks-CQ0oOB0A.mjs";import{t as O}from"./transcript-text-bytes-DrHX89-B.mjs";const k=65536,A=1048576;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]=E(e,{primarySession:{sessionId:i.sessionId,sessionKey:n}});return{sessionTarget:i,entry:a,runtimeEntry:o,primaryConversation:s}}async function streamSessionTranscriptHydration(_,v,E,j){return await E.runNativeSection(async()=>{let M=h(g(_.resolvedScope));if(!M.found)throw new i(M.reason);let{database:N}=M,P,F;try{_.expectedIdentity&&f(N,_.expectedIdentity),P=m(N.db);let t=w({database:N,..._.resolvedScope}),{version:i,coldArchive:a}=C(N,_.resolvedScope.sessionId),p=a?`UTF-8`:x(N.db),h=j?.(N),g=h?new Set(h.sequences):void 0,M=T(N,_.resolvedScope.sessionId,{..._.target,beforeEventSeq:t?.beforeRawSeq});h&&(M=M.where(`seq`,`in`,l`(SELECT CAST(value AS BIGINT) FROM ${d(N.db,l.val(JSON.stringify(h.sequences)))})`));let eventSlice=(e,t=k)=>O(N.db,b(N.db),e,t),I=M.select(e=>[`seq`,e.case().when(e.and([e(`event_json`,`is`,null),e(`event_utf8_bytes`,`>=`,p===`UTF-8`?k:k/2)])).then(null).else(eventSlice(1)).end().as(`data`)]).orderBy(`seq`,`asc`),L=s(N.db,e=>M.select(eventSlice(e(e=>e.offset)).as(`data`)).where(`seq`,`=`,e(e=>e.seq))),R=s(N.db,e=>M.select(eventSlice(1,y*2).as(`data`)).where(`seq`,`=`,e(e=>e)));function*readRows(){if(a){let e=_.target.maxEventBytes,r=e!==void 0&&Number.isFinite(e)&&e>=0?Math.floor(e):void 0,i=0,o=0;for(let e of D(N.db,N.path,a))if(E.throwIfCancelled(),!(e.kind!==`event`||t&&e.row.seq>=t.beforeRawSeq)){if(r!==void 0&&(i+=Buffer.byteLength(e.row.event_json,`utf8`)+o,o=1,i>r))throw new n(`Trajectory transcript store is too large to export (at least ${i} bytes; limit ${r})`);(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(!r(N.db)){E.throwIfCancelled();let e=h?void 0:_.afterSeq;yield*c(N.db,e===void 0?I:I.where(`seq`,`>`,e));return}let i=4194304,s=i/64,l=u(N.db,(h||_.afterSeq===void 0?I:I.where(`seq`,`>`,_.afterSeq)).limit(63)).rows;if(yield*l,l.length<63)return;let d=o(N.db,e=>M.where(`seq`,`>`,e(e=>e)).select(`seq`).select(e=>e.fn(`least`,[e.fn.coalesce(S(),e.val(k)),e.val(k)]).as(`slice_bytes`)).orderBy(`seq`,`asc`).limit(s)),f=o(N.db,e=>I.where(`seq`,`>=`,e(e=>e.firstSeq)).where(`seq`,`<=`,e(e=>e.lastSeq))),p=e(l.at(-1),`initial transcript page`).seq;for(;;){E.throwIfCancelled();let t=d(p).rows;for(let e=0;e<t.length;){let n=e+1,r=t[e].slice_bytes+64;for(;n<t.length&&r+t[n].slice_bytes+64<=i;)r+=t[n].slice_bytes+64,n++;E.throwIfCancelled(),yield*f({firstSeq:t[e].seq,lastSeq:t[n-1].seq}).rows,e=n}if(t.length<s)return;p=e(t.at(-1),`transcript metadata page`).seq}}let z=[],B=[],V=0,H=0,flush=async()=>{z.length!==0&&((await v.request({kind:`transcript-hydration-chunk`,encoding:p,frames:z},B)).consumed(),z=[],B=[],V=0,E.throwIfCancelled())};for(let t of readRows()){let n=t.complete??t.data??e(R(t.seq),`compressed transcript snapshot row`).data;for(let r=1;;r+=k){E.throwIfCancelled();let i=t.data===null?Uint8Array.from(n.subarray(r-1,r-1+k)):void 0,a=i??(r===1?n:e(L({seq:t.seq,offset:r}),`transcript snapshot row`).data);V+a.byteLength>A&&await flush(),i&&B.push(i.buffer);let o=a.byteLength<k;if(z.push({data:a,endOfEvent:o,...(h||_.includeEventJson)&&o?{seq:t.seq}:{}}),V+=a.byteLength,(V>=A||z.length>=128)&&await flush(),o){H++;break}}}await flush(),E.throwIfCancelled(),F={value:h?{kind:`streamed-bounded`,...h,eventCount:H}:{kind:`full`,version:i,eventCount:H}}}catch(e){F={error:t(e)===1?p(N.db,e):e}}try{try{P?.close()}finally{N.close()}}catch(e){throw`error`in F?a(F.error,e,`database close`):e}if(`error`in F)throw F.error;return F.value})}export{streamSessionTranscriptHydration as n,readSessionCompactionPreparation as t};