openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 6.4 kB
JavaScript
import{u as e}from"./transcript-sqlite-post-commit-B_pko32i.mjs";import{q as t}from"./transcript-sqlite-schema-facts-C1_zV4Yg.mjs";import{i as n}from"./transcript-session-key-MDDhuEG9.mjs";import{a as r,i,o as a,p as o,r as s,u as c}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{o as l}from"./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{n as f,t as p}from"./transcript-openclaw-agent-db-readonly-BfbPmTwU.mjs";import{i as m}from"./transcript-session-accessor.sqlite-scope-helpers-LCM13IZK.mjs";import"./transcript-session-accessor.sqlite-scope-6_lvO2un.mjs";import{a as h}from"./transcript-session-accessor.sqlite-read-scope-B5jiReWs.mjs";import{i as g,n as _,o as v,r as y,t as b}from"./transcript-session-transcript-search-policy-Beeb27B8.mjs";function engineUnboundedRowLimit(t){return e(t)?o`all`:o.lit(-1)}function searchSessionTranscriptsReadOnlySync(e,t){let n={...e,query:v(e.query)},r=t??m(h(e)),i=f(e=>l(e.db,()=>{let t=readSessionTranscriptSearch(e,n,!1).next();if(!t.done)throw Error(`Unfiltered search requested host authorization`);return t.value},{databaseLabel:e.path,operationLabel:`session transcript search`}),r);return i.found?i.value:{found:!1,hits:[],truncated:!1}}async function searchSessionTranscriptsFiltered(e,t,n,r){let i={...e,query:v(e.query)},a=p(t);if(!a.found)return{found:!1,hits:[],truncated:!1};let o=readSessionTranscriptSearch(a.database,i,!0);try{let e=o.next();for(;!e.done;){r();let t=await n.request(e.value);try{if(!Array.isArray(t.input)||t.input.length!==e.value.length||!t.input.every(e=>typeof e==`boolean`))throw Error(`Invalid transcript search authorization response`);a.claim.assertCurrent(),e=o.next(t.input)}finally{t.consumed()}}return e.value}finally{try{o.return({found:!1,hits:[],truncated:!1})}finally{a.claim.release()}}}function*readSessionTranscriptSearch(l,f,p){let m=f.query,h=g(f.limit),v=r(l.db),x=e(l.db);function*iterateRows(e){if(!x)yield*a(l.db,e);else for(let t=0;;t+=h+1){let n=s(l.db,e.limit(h+1).offset(t)).rows;if(yield*n,n.length<=h)return}}let S=u(l.db,d),C=S.fullText;if(!C)throw new t(`fullText`,S.diagnostics.join(`; `));let w=S.text.transcript,T=y(m,f.match),E=n({agentId:f.agentId,requestKey:`*`}).slice(0,-1),D=v.selectFrom(`session_windows`).select([`session_id`,`session_key`]).where(t=>f.sessionKeys===void 0?t.or([e(l.db)?o`${t.ref(`session_key`)} ^@ ${o.lit(E)}`:o`${t.ref(`session_key`)} GLOB ${n({agentId:f.agentId,requestKey:`*`})}`,t(`session_key`,`in`,[`global`,`unknown`])]):f.sessionKeys.length>0?t(`session_key`,`in`,c(f.sessionKeys)):t.and([])).$if(f.sessionId!==void 0,e=>e.where(`session_id`,`=`,f.sessionId)),O=v.selectFrom(`session_transcript_cold_archives as cold`).innerJoin(D.as(`window`),`window.session_id`,`cold.session_id`),k=0;if(p){let e=[];for(let t of iterateRows(O.select(`window.session_key`).$if(x,e=>e.orderBy(`window.session_id`))))e.push(t.session_key),e.length===25&&(k+=(yield e).filter(Boolean).length,e=[]);e.length&&(k+=(yield e).filter(Boolean).length)}else k=i(l.db,O.select(e=>e.fn.countAll().as(`count`)))?.count??0;let A;{let t=C.rank(w,T),n=C.match(w,T),r=f.order===`recent`?o`session_transcript_fts.timestamp desc, fts_rowid desc`:o`rank ${o.raw(t.order)}, session_transcript_fts.timestamp desc, session_transcript_fts.message_id asc`,i=v.selectFrom(`session_transcript_fts`).crossJoin(`session_transcript_fts_rows as mapped`).crossJoin(D.as(`window`)).where(`mapped.id`,`=`,C.identity(w)).whereRef(`window.session_id`,`=`,`mapped.session_id`).where(n).$if(!!f.role,t=>e(l.db)?t.where(`role`,`=`,f.role):t.where(e=>e.case().when(`window.session_key`,`is not`,null).then(e(`role`,`=`,f.role)).else(!1).end())).where(e=>e.not(e.exists(e.selectFrom(`session_transcript_index_state`).select(`session_id`).whereRef(`session_id`,`=`,`mapped.session_id`).where(`needs_rebuild`,`!=`,0)))).select([`window.session_key`,C.identity(w).as(`fts_rowid`),t.value.as(`rank`)]),a=i.select([`session_transcript_fts.session_id`,`session_transcript_fts.message_id`,`session_transcript_fts.role`,`session_transcript_fts.timestamp`]).orderBy(r).orderBy(C.identity(w)),c=C.snippet(w,`text`,48,T).as(`snippet`),u=p&&!e(l.db)?v.selectFrom(a.limit(engineUnboundedRowLimit(l.db)).offset(0).as(`ordered`)).crossJoin(`session_transcript_fts`).where(e=>e(C.identity(w),`=`,e.ref(`ordered.fts_rowid`))).where(n).selectAll(`ordered`).select(c):a.select(c),decode=e=>{if(typeof e.session_key!=`string`||typeof e.session_id!=`string`||typeof e.message_id!=`string`||e.role!==`user`&&e.role!==`assistant`||typeof e.snippet!=`string`)return;let n=Number(e.timestamp);return{sessionKey:e.session_key,sessionId:e.session_id,messageId:e.message_id,role:e.role,timestamp:Number.isFinite(n)?n:0,snippet:b(e.snippet),score:t.toRank(e.rank)}};if(p){A=[];let e=[];function*retainBatch(e){let t=yield e.map(e=>e.sessionKey);for(let[n,r]of e.entries()){if(!t[n])continue;let e=A.findIndex(e=>_(r,e)<0);A.splice(e<0?A.length:e,0,r),A.length>h+1&&A.pop()}}for(let t of iterateRows(u)){let n=decode(t);if(!n)continue;let r=A[h];if(r&&(n.score<r.score||n.score===r.score&&n.timestamp<r.timestamp))break;e.push(n),e.length===h+1&&(yield*retainBatch(e),e=[])}e.length&&(yield*retainBatch(e))}else{let e=f.order===`recent`?i.orderBy(r).limit(h+1):v.with(e=>e(`scores`).materialized(),()=>i).selectFrom(`scores`).crossJoin(`session_transcript_fts`).where(e=>e(C.identity(w),`=`,e.ref(`scores.fts_rowid`))).where(`scores.rank`,`in`,e=>e.selectFrom(`scores`).select(`rank`).orderBy(`rank`,t.order).limit(h+1)).select([`scores.session_key`,`scores.fts_rowid`,`scores.rank`]).orderBy(`scores.rank`,t.order).orderBy(`session_transcript_fts.timestamp`,`desc`).orderBy(`session_transcript_fts.message_id`,`asc`).limit(h+1);A=s(l.db,v.with(e=>e(`hits`).materialized(),()=>e).selectFrom(`hits`).crossJoin(`session_transcript_fts`).where(e=>e(C.identity(w),`=`,e.ref(`hits.fts_rowid`))).where(n).select([`hits.session_key`,`session_transcript_fts.session_id`,`message_id`,`role`,`timestamp`,`hits.rank as rank`,C.snippet(w,`text`,48,T).as(`snippet`)]).orderBy(r)).rows.flatMap(e=>{let t=decode(e);return t?[t]:[]})}}return{found:!0,hits:A.slice(0,h),truncated:A.length>h,...k>0?{archivedTranscriptsExcluded:k}:{}}}export{searchSessionTranscriptsFiltered,searchSessionTranscriptsReadOnlySync};