UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 4.93 kB
import{a as e,i as t,r as n}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{r}from"./transcript-paths-CuGliRzI.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(e,t){if(!/^[a-f0-9]{64}\.jsonl\.zst$/.test(t))throw Error(`Invalid cold transcript archive name`);return u.join(r(e),`cold`,t)}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(t,r){let i=n(t,e(t).selectFrom(`session_transcript_cold_archive_chunks`).select(_).where(`session_id`,`=`,r.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)!==r.event_count||i.reduce((e,t)=>e+t.raw_bytes,i.length-1)!==r.raw_bytes||i.reduce((e,t)=>e+t.archive_bytes,0)!==r.archive_bytes||i.at(-1)?.last_seq!==r.last_seq||a!==r.archive_sha256||`${a}.jsonl.zst`!==r.archive_name)throw Error(`Cold transcript archive chunk metadata does not match its parent`);return i}function*readVerifiedSessionColdChunks(n,r,i,a=!1){let o=e(n),s=readSessionColdChunks(n,i);if(s.length===0){let e=i.storage===`sqlite`?t(n,o.selectFrom(`session_transcript_cold_archives`).select(`archive_blob`).where(`session_id`,`=`,i.session_id))?.archive_blob:null;yield{bytes:readVerifiedSessionColdArchiveSync({storePath:r,archive:{...i,archive_blob:e??null}}),archive:i}}else for(let e=0;e<s.length;e++){let c=s[a?s.length-1-e:e],l=i.storage===`sqlite`?t(n,o.selectFrom(`session_transcript_cold_archive_chunks`).select(`archive_blob`).where(`session_id`,`=`,i.session_id).where(`chunk_index`,`=`,c.chunk_index))?.archive_blob:null;yield{bytes:readVerifiedSessionColdArchiveSync({storePath:r,archive:{...c,storage:i.storage,archive_blob:l??null}}),archive:{...c,generation:i.generation}}}}function*iterateSessionColdRecords(e,t,n){for(let r of readVerifiedSessionColdChunks(e,t,n))yield*decodeSessionColdRecords(r.bytes,r.archive)}export{readVerifiedSessionColdChunks as n,decodeSessionColdRecords as r,iterateSessionColdRecords as t};