UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.98 kB
import{r as e}from"./transcript-paths-EL7FYwW9.mjs";import{a as t,i as n,r}from"./transcript-kysely-sync-DQ_Yn8jW.mjs";import{c as i,d as a,f as o,g as s,h as c,s as l}from"./transcript-schemas-C_W5KYqZ.mjs";import u from"node:path";import{createHash as d}from"node:crypto";import{readFileSync as f}from"node:fs";import"node:fs/promises";import p from"node:zlib";function verifySessionColdArchiveBytes(e,t){if(e.length!==t.archive_bytes||d(`sha256`).update(e).digest(`hex`)!==t.archive_sha256)throw Error(`Cold transcript archive ${t.archive_name} failed verification. Restore it from a verified backup.`);return e}function missingSessionColdArchive(e,t){return Error(`Cold transcript archive ${e} is missing or unreadable. Restore it from a backup; its transcript has not been replaced with empty history.`,{cause:t})}function resolveSessionColdArchivePath(t,n){if(!/^[a-f0-9]{64}\.jsonl\.zst$/.test(n))throw Error(`Invalid cold transcript archive name`);return u.join(e(t),`cold`,n)}function readVerifiedSessionColdArchiveSync(e){let{archive:t}=e;if(t.storage===`sqlite`)return verifySessionColdArchiveBytes(Buffer.from(t.archive_blob??[]),t);let n;try{n=f(resolveSessionColdArchivePath(e.storePath,t.archive_name))}catch(e){throw missingSessionColdArchive(t.archive_name,e)}return verifySessionColdArchiveBytes(n,t)}const m=a().int(),h=c().nullable(),g=l(`kind`,[o({kind:i(`header`),version:i(1),sessionId:c(),generation:c()}),o({kind:i(`event`),row:o({seq:m,event_json:c(),created_at:m})}),o({kind:i(`identity`),row:o({event_id:c(),seq:m,event_type:h,parent_id:h,message_idempotency_key:h,created_at:m})}),o({kind:i(`active`),row:o({active_position:m,event_seq:m,message_position:m.nullable(),context_eligible:m.nullable()})}),o({kind:i(`index`),row:o({indexed_seq:m,leaf_event_id:h,needs_rebuild:m,active_event_count:m,active_message_count:m,updated_at:m})}),o({kind:i(`fts`),row:o({text:h,message_id:h,role:h,timestamp:s([c(),a()]).nullable()})})]);function*decodeSessionColdRecords(e,t){let n=p.zstdDecompressSync(e,{maxOutputLength:67108864}),r=!1,i=0,a=-1,o;for(let e=0;e<n.length;){let s=n.indexOf(10,e),c=s<0?n.length:s,l=n.toString(`utf8`,e,c);if(!l.trim()&&!n.toString(`utf8`,e).trimEnd())break;let u=g.parse(JSON.parse(l));if(!r){if(u.kind!==`header`||u.sessionId!==t.session_id||u.generation!==t.generation)throw Error(`Cold transcript archive metadata does not match its contents`);r=!0}else if(u.kind===`header`)throw Error(`Cold transcript archive metadata does not match its contents`);if(u.kind===`event`){if(i===0&&t.first_seq!==void 0&&u.row.seq!==t.first_seq||o!==void 0&&u.row.seq<=o)throw Error(`Cold transcript archive metadata does not match its contents`);i++,a+=Buffer.byteLength(u.row.event_json)+1,o=u.row.seq}yield u,e=c+1}if(!r||i!==t.event_count||o!==t.last_seq||a!==t.raw_bytes)throw Error(`Cold transcript archive metadata does not match its contents`)}const _=[`session_id`,`chunk_index`,`archive_name`,`archive_sha256`,`first_seq`,`last_seq`,`event_count`,`raw_bytes`,`archive_bytes`];function hashSessionColdChunks(e){return d(`sha256`).update(JSON.stringify(e.map(e=>[e.chunk_index,e.archive_name,e.archive_sha256,e.first_seq,e.last_seq,e.event_count,e.raw_bytes,e.archive_bytes]))).digest(`hex`)}function readSessionColdChunks(e,n){let i=r(e,t(e).selectFrom(`session_transcript_cold_archive_chunks`).select(_).where(`session_id`,`=`,n.session_id).orderBy(`chunk_index`)).rows;if(i.length===0)return i;let a=hashSessionColdChunks(i);if(i.some((e,t)=>e.chunk_index!==t||t>0&&e.first_seq<=i[t-1].last_seq)||i.reduce((e,t)=>e+t.event_count,0)!==n.event_count||i.reduce((e,t)=>e+t.raw_bytes,i.length-1)!==n.raw_bytes||i.reduce((e,t)=>e+t.archive_bytes,0)!==n.archive_bytes||i.at(-1)?.last_seq!==n.last_seq||a!==n.archive_sha256||`${a}.jsonl.zst`!==n.archive_name)throw Error(`Cold transcript archive chunk metadata does not match its parent`);return i}function readVerifiedSessionColdChunk(e,r,i,a){let o=t(e),s=i.storage===`sqlite`?a?n(e,o.selectFrom(`session_transcript_cold_archive_chunks`).select(`archive_blob`).where(`session_id`,`=`,i.session_id).where(`chunk_index`,`=`,a.chunk_index))?.archive_blob:n(e,o.selectFrom(`session_transcript_cold_archives`).select(`archive_blob`).where(`session_id`,`=`,i.session_id))?.archive_blob:null;return{bytes:readVerifiedSessionColdArchiveSync({storePath:r,archive:{...a??i,storage:i.storage,archive_blob:s??null}}),archive:a?{...a,generation:i.generation}:i}}function*readVerifiedSessionColdChunks(e,t,n,r=!1){let i=readSessionColdChunks(e,n);if(i.length===0)yield readVerifiedSessionColdChunk(e,t,n);else for(let a=0;a<i.length;a++)yield readVerifiedSessionColdChunk(e,t,n,i[r?i.length-1-a:a])}function*iterateSessionColdRecords(e,t,n){for(let r of readVerifiedSessionColdChunks(e,t,n))yield*decodeSessionColdRecords(r.bytes,r.archive)}export{decodeSessionColdRecords as a,readVerifiedSessionColdChunks as i,readSessionColdChunks as n,readVerifiedSessionColdChunk as r,iterateSessionColdRecords as t};