UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 13 kB
import{H as e,L as t,R as n}from"./transcript-sqlite-database-admission-IPmtadyc.mjs";import{u as r}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{a as i,c as a,i as o,p as s,r as c,u as l}from"./transcript-kysely-sync-CAP5xe3V.mjs";import"./transcript-sqlite-transaction-CBQXI8WC.mjs";import{r as u}from"./transcript-adapters-VitZFwpd.mjs";import{t as d}from"./transcript-openclaw-agent-search-schema-BPW1qurF.mjs";import"./transcript-session-actor-transaction-LsCQWbNw.mjs";import{m as f}from"./transcript-transcript-payload-F5t-l1WZ.mjs";import{t as p}from"./transcript-transcript-tree-CEw6GxlK.mjs";import{r as m}from"./transcript-session-transcript-projection-append-BX6beDU2.mjs";import{r as h}from"./transcript-session-transcript-read-bytes-DJiUSe6g.mjs";function withPostgresSessionTranscriptFtsRows(e,t,n){return buildPostgresSessionTranscriptFtsRows(i(e),t,n)}function buildPostgresSessionTranscriptFtsRows(e,t,n){return e.with(`entries(position, message_id, text, role, timestamp)`,()=>n).with(e=>e(`ids`).materialized(),e=>e.selectFrom(`entries`).select(e=>e.fn(`nextval`,[e.fn(`pg_get_serial_sequence`,[s.lit(`session_transcript_fts_rows`),s.lit(`id`)])]).as(`id`))).with(e=>e(`allocated`).materialized(),e=>e.selectFrom(e.selectFrom(`entries`).selectAll().select(e=>e.fn.agg(`row_number`,[]).over(e=>e.orderBy(`position`)).as(`rn`)).as(`ordered_entries`)).innerJoin(e.selectFrom(`ids`).select(`id`).select(e=>e.fn.agg(`row_number`,[]).over(e=>e.orderBy(`id`)).as(`rn`)).as(`ordered_ids`),`ordered_ids.rn`,`ordered_entries.rn`).selectAll(`ordered_entries`).select(`ordered_ids.id`)).with(`inserted`,e=>e.insertInto(`session_transcript_fts_rows`).columns([`id`,`session_id`,`message_id`]).expression(e=>e.selectFrom(`allocated`).select(e=>[`id`,e.val(t).as(`session_id`),`message_id`]).orderBy(`position`)).returning(`id`)).with(`inserted_content`,e=>e.insertInto(`session_transcript_fts`).columns([`rowid`,`text`,`session_id`,`message_id`,`role`,`timestamp`]).expression(e=>e.selectFrom(`inserted`).innerJoin(`allocated`,`allocated.id`,`inserted.id`).select(e=>[`allocated.id`,`allocated.text`,e.val(t).as(`session_id`),`allocated.message_id`,`allocated.role`,e.cast(`allocated.timestamp`,`double precision`).as(`timestamp`)]).orderBy(`allocated.position`)).returning(`rowid`))}function createSessionTranscriptFtsInserter(n,o){let c=i(n),l=r(n),f=l?void 0:u(n,d);if(f&&!f.fullText)throw Error(f.diagnostics.join(`; `));let prepare=e=>{let t=Array.from({length:e},(e,t)=>t),r=a(n,r=>{let i=s`(values ${s.join(t.map(e=>s`(${s.lit(e)}, ${r(t=>t[e].messageId)}, ${r(t=>t[e].text)}, ${r(t=>t[e].role)}, ${r(t=>t[e].timestamp)})`))})`;return l?withPostgresSessionTranscriptFtsRows(n,o,i).selectNoFrom(s.lit(1).as(`written`)):c.with(`entries(position, message_id, text, role, timestamp)`,()=>i).with(e=>e(`allocated`).materialized(),e=>e.selectNoFrom(f.fullText.allocatedIdentity(f.text.transcript).as(`last_id`))).insertInto(`session_transcript_fts`).columns([`rowid`,`text`,`session_id`,`message_id`,`role`,`timestamp`]).expression(t=>t.selectFrom(`entries`).crossJoin(`allocated`).select(t=>[t(t(`allocated.last_id`,`-`,s.lit(e-1)),`+`,t.ref(`entries.position`)).as(`rowid`),`entries.text`,t.val(o).as(`session_id`),`entries.message_id`,`entries.role`,`entries.timestamp`]).orderBy(`entries.position`))});if(l)return r;let i=a(n,e=>c.insertInto(`session_transcript_fts_rows`).values(t.map(t=>({session_id:o,message_id:e(e=>e[t].messageId)}))));return e=>{i(e),r(e)}},p=/* @__PURE__ */ new Map;return r=>{t(n,[e(o)],()=>{for(let e=0;e<r.length;e+=512){let t=r.slice(e,e+512),n=p.get(t.length)??prepare(t.length);p.set(t.length,n),n(t)}})}}function deleteSessionTranscriptFtsRowsInTransaction(n,r,a={}){let o=i(n),s=o.selectFrom(`session_transcript_fts_rows`).select(`id`);return s=typeof r==`string`?s.where(`session_id`,`=`,r):s.where(`session_id`,`in`,l(r)),a.messageIds&&(s=s.where(`message_id`,`in`,a.messageIds.length<=400?a.messageIds:l(a.messageIds))),a.maxRows!==void 0&&(s=s.limit(a.maxRows)),Number(t(n,(typeof r==`string`?[r]:r).map(e),()=>c(n,o.deleteFrom(`session_transcript_fts_rows`).where(`id`,`in`,s)).numAffectedRows)??0n)}function getProjectionKysely(e){return i(e)}function readCanonicalEventId(e){return!p(e)||typeof e.id!=`string`?null:e.id.trim()||null}function readProjectionSession(e,t){return o(e,getProjectionKysely(e).selectFrom(`session_windows as session`).leftJoin(`transcript_rewrite_watermarks as rewrite`,`rewrite.session_id`,`session.session_id`).select([`session.transcript_updated_at`,`rewrite.generation`]).where(`session.session_id`,`=`,t))}function readProjectionSourceSnapshot(e,t){let n=getProjectionKysely(e),r=readProjectionSession(e,t),i=o(e,n.selectFrom(`transcript_events`).select(`seq`).where(`session_id`,`=`,t).orderBy(`seq`,`desc`).limit(1));return{generation:r?.generation??null,latestSeq:i?.seq,transcriptUpdatedAt:r?.transcript_updated_at??null}}function sourceSnapshotMatches(e,t){return e.generation===t.sourceTranscriptGeneration&&e.latestSeq===t.sourceIndexedSeq&&e.transcriptUpdatedAt===t.sourceTranscriptUpdatedAt}function projectionTailFitsCatchUpBounds(e,t,n){if(t.sourceTranscriptGeneration===null||n.generation!==t.sourceTranscriptGeneration||n.latestSeq===void 0||n.latestSeq<t.sourceIndexedSeq)return!1;let r=n.latestSeq-t.sourceIndexedSeq;if(r>512)return!1;let i=c(e,getProjectionKysely(e).selectFrom(`transcript_events`).select([`seq`,h().as(`event_bytes`)]).where(`session_id`,`=`,t.sessionId).where(`seq`,`>`,t.sourceIndexedSeq).orderBy(`seq`,`asc`).limit(513)).rows;return i.length===r&&(i.at(-1)?.seq??t.sourceIndexedSeq)===n.latestSeq&&i.reduce((e,t)=>e+t.event_bytes,0)<=262144}function claimPreparedSessionTranscriptProjectionInTransaction(e,t,n){let r=getProjectionKysely(e),i={active_event_count:0,active_message_count:0,indexed_seq:-1,leaf_event_id:null,needs_rebuild:1,updated_at:n},a=c(e,r.insertInto(`session_transcript_index_state`).columns([`session_id`,`active_event_count`,`active_message_count`,`indexed_seq`,`leaf_event_id`,`needs_rebuild`,`updated_at`]).expression(r.selectFrom(`session_windows`).select(e=>[`session_id`,e.val(i.active_event_count).as(`active_event_count`),e.val(i.active_message_count).as(`active_message_count`),e.val(i.indexed_seq).as(`indexed_seq`),e.val(i.leaf_event_id).as(`leaf_event_id`),e.val(i.needs_rebuild).as(`needs_rebuild`),e.val(i.updated_at).as(`updated_at`)]).where(`session_id`,`=`,t.sessionId)).onConflict(e=>e.column(`session_id`).doUpdateSet(i)));return Number(a.numAffectedRows??0n)>0}function deletePreparedSessionTranscriptProjectionChunkInTransaction(e,t){let n=getProjectionKysely(e),i=r(e)?`event_seq`:`rowid`,a=Number(c(e,n.deleteFrom(`session_transcript_active_events`).$if(r(e),e=>e.where(`session_id`,`=`,t.sessionId)).where(i,`in`,n.selectFrom(`session_transcript_active_events`).select(i).where(`session_id`,`=`,t.sessionId).limit(t.maxRowsPerTable))).numAffectedRows??0n),o=deleteSessionTranscriptFtsRowsInTransaction(e,t.sessionId,{maxRows:t.maxRowsPerTable});return{hasMore:a===t.maxRowsPerTable||o===t.maxRowsPerTable}}function appendPreparedSessionTranscriptProjectionChunkInTransaction(e,t){insertPreparedSessionTranscriptProjectionRows(e,t)}function insertPreparedSessionTranscriptProjectionRows(e,t){let n=getProjectionKysely(e);t.activeRows&&t.activeRows.length>0&&c(e,n.insertInto(`session_transcript_active_events`).values(t.activeRows.map(e=>({active_position:e.activePosition,context_eligible:e.contextEligible,event_seq:e.eventSeq,message_position:e.messagePosition,session_id:t.sessionId})))),t.ftsRows&&t.ftsRows.length>0&&createSessionTranscriptFtsInserter(e,t.sessionId)(t.ftsRows)}function prepareProjectionTailCatchUp(e,t,n){let r=n.latestSeq;if(t.sourceHasInvalidLeafControl||r===void 0||!projectionTailFitsCatchUpBounds(e,t,n))return;let i=c(e,getProjectionKysely(e).selectFrom(`transcript_events`).select([f(e).as(`event_json`),`seq`,`created_at`]).where(`session_id`,`=`,t.sessionId).where(`seq`,`>`,t.sourceIndexedSeq).where(`seq`,`<=`,r).orderBy(`seq`,`asc`)).rows,a=[],o=[],s={activeEventCount:t.activeEventCount,activeMessageCount:t.activeMessageCount,indexedSeq:t.sourceIndexedSeq,leafEventId:t.leafEventId};for(let e of i){let t=JSON.parse(e.event_json);if(p(t)&&t.type===`reset`)return;let n=m({createdAt:e.created_at,cursor:s,event:t,eventId:readCanonicalEventId(t),seq:e.seq});if(!n)return;s=n.cursor,n.activeRow&&a.push(n.activeRow),n.ftsRow&&o.push(n.ftsRow)}return{...t,activeEventCount:s.activeEventCount,activeMessageCount:s.activeMessageCount,activeRows:a,ftsRows:o,leafEventId:s.leafEventId,sourceIndexedSeq:s.indexedSeq,sourceTranscriptUpdatedAt:n.transcriptUpdatedAt}}function finalizePreparedSessionTranscriptProjectionInTransaction(e,t,n){let r=readProjectionSourceSnapshot(e,t.sessionId),i=sourceSnapshotMatches(r,t),a=i?void 0:prepareProjectionTailCatchUp(e,t,r);if(!i&&!a)return!1;let o=a??t;a&&insertPreparedSessionTranscriptProjectionRows(e,{activeRows:a.activeRows,ftsRows:a.ftsRows,sessionId:a.sessionId});let s=c(e,getProjectionKysely(e).updateTable(`session_transcript_index_state`).set({active_event_count:o.activeEventCount,active_message_count:o.activeMessageCount,indexed_seq:o.sourceIndexedSeq,leaf_event_id:o.leafEventId,needs_rebuild:0,updated_at:Date.now()}).where(`session_id`,`=`,o.sessionId).where(`needs_rebuild`,`!=`,0).where(`updated_at`,`=`,n));return Number(s.numAffectedRows??0n)>0}function getIndexKysely(e){return i(e)}function selectSessionTranscriptProjectionState(e,t){return getIndexKysely(e).selectFrom(`session_transcript_index_state`).select([`active_event_count`,`active_message_count`,`indexed_seq`,`leaf_event_id`,`needs_rebuild`]).select(e=>e.exists(e.selectFrom(`session_transcript_active_events`).select(`session_id`).where(`session_id`,`=`,t).where(`context_eligible`,`is`,null).limit(1)).as(`has_unclassified`)).where(`session_id`,`=`,t)}function selectSessionTranscriptIndexStatus(e,t){let n=getIndexKysely(e),i=r(e),a=i?`is distinct from`:`is not`;return n.selectFrom(n.selectFrom(`transcript_events`).select(`seq`).where(`session_id`,`=`,t).orderBy(`seq`,`desc`).limit(1).as(`latest`)).leftJoin(selectSessionTranscriptProjectionState(e,t).as(`state`),e=>e.onTrue()).select(`latest.seq as latestSeq`).select(e=>e.or([e(`state.needs_rebuild`,a,0),e(`state.indexed_seq`,a,e.ref(`latest.seq`)),e(`state.has_unclassified`,`=`,i?s.lit(!0):1)]).as(`needs_reconcile`))}function readSessionTranscriptIndexStatus(e,t){let n=o(e,selectSessionTranscriptIndexStatus(e,t));return n?{needsReconcile:!!n.needs_reconcile}:void 0}function sessionTranscriptIndexNeedsReconcile(e,t){return readSessionTranscriptIndexStatus(e,t)?.needsReconcile??!1}function selectSessionsNeedingTranscriptIndexReconcile(e){return getIndexKysely(e).selectFrom(`session_windows`).innerJoin(`transcript_events as latest`,e=>e.onRef(`latest.session_id`,`=`,`session_windows.session_id`).on(e=>e(`latest.seq`,`=`,e.selectFrom(`transcript_events as candidate`).select(`candidate.seq`).whereRef(`candidate.session_id`,`=`,`session_windows.session_id`).orderBy(`candidate.seq`,`desc`).limit(1)))).leftJoin(`session_transcript_index_state as st`,`st.session_id`,`session_windows.session_id`).select(`session_windows.session_id`).where(e=>e.or([e(e.fn.coalesce(`st.needs_rebuild`,e.val(1)),`!=`,0),e(`latest.seq`,`>`,e.fn.coalesce(`st.indexed_seq`,e.val(-1))),e.and([e.exists(e.selectFrom(`session_transcript_active_events as any_pending`).select(`any_pending.session_id`).where(`any_pending.context_eligible`,`is`,null).limit(1)),e.exists(e.selectFrom(`session_transcript_active_events as pending`).select(`pending.session_id`).whereRef(`pending.session_id`,`=`,`session_windows.session_id`).where(`pending.context_eligible`,`is`,null))])])).orderBy(`session_windows.session_id`)}function listSessionsNeedingTranscriptIndexReconcile(e,t){let n=selectSessionsNeedingTranscriptIndexReconcile(e);return c(e,t===void 0?n:n.limit(t)).rows.flatMap(e=>typeof e.session_id==`string`?[e.session_id]:[])}const g=[`session_transcript_active_events`,`session_transcript_fts_rows`,`session_transcript_index_state`];function selectOrphanedTranscriptOwners(e,t){let n=getIndexKysely(e);return n.selectFrom(t).select(`session_id`).distinct().except(n.selectFrom(`transcript_events`).select(`session_id`).distinct()).orderBy(`session_id`)}function deleteOrphanedTranscriptIndexRowsInTransaction(e){let t=getIndexKysely(e);for(let r of g)n(e,()=>c(e,t.deleteFrom(r).where(`session_id`,`in`,selectOrphanedTranscriptOwners(e,r))))}export{sessionTranscriptIndexNeedsReconcile as a,deletePreparedSessionTranscriptProjectionChunkInTransaction as c,selectSessionTranscriptProjectionState as i,finalizePreparedSessionTranscriptProjectionInTransaction as l,listSessionsNeedingTranscriptIndexReconcile as n,appendPreparedSessionTranscriptProjectionChunkInTransaction as o,selectSessionTranscriptIndexStatus as r,claimPreparedSessionTranscriptProjectionInTransaction as s,deleteOrphanedTranscriptIndexRowsInTransaction as t};