UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

10 lines • 14.1 kB
import{p as e,s as t}from"./transcript-sqlite-schema-facts-C1_zV4Yg.mjs";import{n}from"./transcript-session-key-normalization-3Mbu27cw.mjs";import"./transcript-session-key-MDDhuEG9.mjs";import{r,t as i}from"./transcript-store-entry-Ch3rW6qd.mjs";import{a,i as o,l as s,m as c,o as l,p as u,r as d,t as f,u as p}from"./transcript-kysely-sync-CAP5xe3V.mjs";import{i as m}from"./transcript-openclaw-state-db-schema-helpers-B1k_oHgC.mjs";import{l as h,n as g}from"./transcript-json-values-C_3KtpDU.mjs";import{t as _}from"./transcript-session-accessor.sqlite-owner-projection-B_z42siZ.mjs";import{t as v}from"./transcript-session-actor-transaction-LsCQWbNw.mjs";import{n as y,r as b}from"./transcript-session-accessor.sqlite-participant-projection-DbuxdQha.mjs";import{i as x,n as S,s as C}from"./transcript-session-entry-snapshots-B1SDC3sn.mjs";import{n as w,t as T}from"./transcript-session-accessor.sqlite-status-BxqCNBm4.mjs";import{t as E}from"./transcript-internal-session-key-aELBBJcF.mjs";import{d as D,n as O,r as k,s as A,u as j}from"./transcript-session-canonical-key-9wlUjrYM.mjs";import{toUSVString as M}from"node:util";function createSessionEntryQueryCache(e){let n;return r=>{let i,clear=()=>{i=void 0};return a=>{let o=t(e);if(o&&i?.revision===o&&i.key===a)return i.row&&{...i.row};n?.();let s=r(a);return o&&t(e)===o&&Object.values(s??{}).reduce((e,t)=>e+(typeof t==`string`?t.length*2:0),a.length*2)<=1048576&&(i={key:a,revision:o,row:s},n=clear),s&&{...s}}}}const N=f(e=>{let t=createSessionEntryQueryCache(e),n=/* @__PURE__ */ new Map,r=/* @__PURE__ */ new Map;return{row:(r,i=`full`)=>{let a=`${JSON.stringify(i)}:${_(e)}`,o=n.get(a);return o||(o=t(s(e,t=>selectReadableSessionEntryRows({db:e},i).where(`session_key`,`=`,t(e=>e)))),n.set(a,o)),o(r)},canonical:(n,i,a=!0)=>{let o=`${JSON.stringify(i)}:${a}:${a&&_(e)}`,c=r.get(o);return c||(c=t(s(e,t=>k({db:e},{metadata:a}).$if(!a,e=>e.select(`session_nodes.updated_at`)).select(S(void 0,i)).where(`session_nodes.session_key`,`=`,t(e=>e)))),r.set(o,c)),c(n)}}});function selectReadableSessionEntryRows(e,t){if(t===`delivery`){let t=u` CASE WHEN json_valid(entry_json) AND json_type(entry_json, '$.sessionId') = 'text' AND json_type(entry_json, '$.updatedAt') IN ('integer', 'real') AND length(CAST(entry_json AS BLOB)) = length(CAST(printf('%s', entry_json) AS BLOB)) THEN (SELECT CASE WHEN count(*) = count(DISTINCT key) THEN json_group_object(key, json(entry_json -> fullkey)) ELSE entry_json END FROM json_each(entry_json) WHERE key IN ('sessionId', 'updatedAt', 'delivery', 'groupId')) ELSE entry_json END`.as(`entry_json`);return a(e.db).selectFrom(`session_nodes`).select([`session_key`,`current_session_id`,`updated_at`,t])}return t===`full`?a(e.db).selectFrom(`session_nodes`).selectAll(`session_nodes`).select(S(void 0,t)):w(e,t).select([`current_session_id`,`updated_at`])}const P=/* @__PURE__ */ `session_id.session_key.reason.created_at.updated_at.session_entry_provenance.acp_owned.plugin_owner_id.hook_external_content_source.previous_session_id.session_scope.started_at.ended_at.status.chat_type.channel.account_id.model_provider.model.agent_harness_id.parent_session_key.spawned_by.display_name.primary_conversation_id.transcript_observed_at.transcript_updated_at`.split(`.`),F=P.map(e=>`session_windows.${e} as window_${e}`);function selectSessionEntryWindowFacts(e){return a(e.db).selectFrom(`session_windows`).select(F).as(`entry_window`)}function takeSessionEntryWindowFacts(e){let t=e.window_session_id!==null,n=P.map(t=>{let n=`window_${t}`,r=e[n];return delete e[n],[t,r]});return t?Object.fromEntries(n):null}function readSelectedSessionEntryRows(e,t,n,r,i){let a=typeof t==`string`?t:t.length===1?t[0]:void 0;if(a!==void 0&&n!==`delivery`&&!i?.includeBoardPresence&&!i?.includeMembership&&!i?.includeWindowFacts&&!i?.includeRowTranscriptProjection){let t=N(e.db),i=r===`canonical`?t.canonical(a,n):t.row(a,n);return i?[i]:[]}let o=(r===`canonical`?k(e,{metadata:!0}).select(`session_nodes.updated_at`).select(S(void 0,n===`delivery`?`list`:n)):selectReadableSessionEntryRows(e,n)).$if(i?.includeWindowFacts===!0,t=>t.leftJoin(selectSessionEntryWindowFacts(e),`entry_window.window_session_id`,`session_nodes.current_session_id`).selectAll(`entry_window`).select(e=>[e.selectFrom(`transcript_rewrite_watermarks`).select(`generation`).whereRef(`session_id`,`=`,`session_nodes.current_session_id`).as(`transcript_generation`),e.fn.coalesce(e.selectFrom(`session_transcript_cold_archives`).select(`last_seq`).whereRef(`session_id`,`=`,`session_nodes.current_session_id`),e.selectFrom(`transcript_events`).select(e=>e.fn.max(`seq`).as(`max_seq`)).whereRef(`session_id`,`=`,`session_nodes.current_session_id`)).as(`transcript_max_seq`)])).$if((i?.includeWindowFacts===!0||i?.includeRowTranscriptProjection===!0)&&i.projectParticipants!==!1&&n!==`delivery`,t=>t.select(t=>m(e.db,`session_participants`)?t.selectFrom(`session_participants`).select(t=>h(e.db,g(e.db,[t.ref(`identity_namespace`),t.ref(`actor_id`)]),[t.ref(`first_prompted_at`),t.ref(`actor_id`),t.ref(`identity_namespace`)]).as(`identities`)).whereRef(`session_participants.session_key`,`=`,`session_nodes.session_key`).$asScalar().as(`participant_identities_json`):t.val(`[]`).as(`participant_identities_json`))).$if(i?.includeRowTranscriptProjection===!0,e=>e.leftJoin(`session_windows as row_transcript_window`,`row_transcript_window.session_id`,`session_nodes.current_session_id`).leftJoin(`session_transcript_index_state as row_transcript_index`,`row_transcript_index.session_id`,`session_nodes.current_session_id`).select([`row_transcript_window.session_id as row_transcript_window_id`,`row_transcript_window.row_transcript_json`,`row_transcript_window.row_transcript_updated_at`,`row_transcript_window.transcript_updated_at as row_transcript_source_updated_at`,`row_transcript_index.indexed_seq as row_transcript_indexed_seq`])),s=c(),l=i?.includeBoardPresence?o.select((m(e.db,`board_widgets`)?s.exists(s.selectFrom(`board_tabs`).select(`session_key`).whereRef(`board_tabs.session_key`,`=`,`session_nodes.session_key`)):s.lit(0)).as(`board_present`)):o,u=i?.includeMembership?l.select(t=>m(e.db,`session_members`)?t.selectFrom(`session_members`).select(t=>h(e.db,t.ref(`identity_id`),[t.ref(`identity_id`)],`not-null`).as(`ids`)).whereRef(`session_members.session_key`,`=`,`session_nodes.session_key`).$asScalar().as(`member_ids_json`):t.val(`[]`).as(`member_ids_json`)):l,f=d(e.db,(typeof t==`string`?u.where(`session_nodes.session_key`,`=`,t):u.where(`session_nodes.session_key`,`in`,p(t))).orderBy(`session_nodes.session_key`,`asc`)).rows;return i?.includeWindowFacts?f.map(e=>{let t=takeSessionEntryWindowFacts(e),{transcript_generation:n,transcript_max_seq:r,...i}=e;return{...i,window:t,transcriptWatermark:{sessionId:e.current_session_id,generation:n??null,maxSeq:r??null}}}):f}function parseReadableSessionEntryData(e,t,n){let r=n===`delivery`?C(t):T(t,n);if(t.retained_window_id!==void 0)return D(t,r,`read`)??null;if(r){if(validateDeliveryCanonicalSessionEntry(t.session_key,r),n===`delivery`){let{sessionId:e,updatedAt:t,delivery:n,groupId:i}=r;return{sessionId:e,updatedAt:t,delivery:n,groupId:i}}return r}if(t.entry_json===`{}`&&o(e.db,a(e.db).selectFrom(`session_windows`).select(`session_id`).where(`session_id`,`=`,t.current_session_id).where(`session_key`,`=`,t.session_key)))return null;throw j(`invalid persisted session row requires repair for ${t.session_key}`)}function validateDeliveryCanonicalSessionEntry(e,t){let n=r(e,t);if(n!==e)throw j(`non-canonical persisted row resolves to session key ${n}`);return t}function parseReadableSqliteSessionEntryRow(e,t,n=`full`){let r=parseReadableSessionEntryData(e,t,n);return r?b(e.db,t.session_key,r,t.participant_identities_json):null}function prepareSqliteSessionEntryRowDecoder(e,t,n=`full`,r=!0,i){let a=n===`delivery`||!r?(e,t)=>t:y(e.db,t.filter(e=>e.entry_json!==`{}`).map(e=>e.session_key));return t=>{let r=parseReadableSessionEntryData(e,t,n);if(!r)return null;try{return a(t.session_key,r,t.participant_identities_json)}catch(e){if(!i)throw e;return i(t.session_key),r}}}function readSessionKeyBySessionIdInDatabase(e,t){let n=a(e.db);return o(e.db,n.selectFrom(`session_windows`).select(`session_key`).where(`session_id`,`=`,t).limit(1))?.session_key}function readSessionEntryRow(e,t,n=`full`,r){return scanSessionEntryRows(e,t,n,!0,r)?.selected}function readSessionEntrySelectionInDatabase(e,t,n,r={}){return r.keyFormat===`agent-qualified`?readQualifiedSessionEntryRow(e,t,n,r):readSessionEntryRow(e,n,r.projection)}function readSessionEntryIdentity(e,t){let n=scanSessionEntryRows(e,t,`list`,!1)?.selected?.entry;return n&&{sessionId:n.sessionId,updatedAt:n.updatedAt,lifecycleRevision:n.lifecycleRevision}}function scanSessionEntryRows(t,n,r,a=!0,o){if(v(t,{sessionKey:n})){let e=i(n),a=/* @__PURE__ */ new Map,s;for(let i of e){let e=readExactSessionEntryRow(t,i,r,void 0,o);e&&(a.set(i,e.entry),i===n&&(s=e))}return{lookupKeys:e,entries:a,selected:s}}return e(t.db,()=>{O(t);let e=i(n);if(e[0]===void 0)return;let s=readSelectedSessionEntryRows(t,e,r,void 0,o?{includeWindowFacts:o,includeBoardPresence:!0,includeMembership:!0}:void 0),c=/* @__PURE__ */ new Map,l;for(let e of s){let i=a?parseReadableSqliteSessionEntryRow(t,e,r):parseReadableSessionEntryData(t,e,r);i&&(c.set(e.session_key,i),e.session_key===n.trim()&&(l={entry:i,row:e}))}return{lookupKeys:e,entries:c,selected:l}})}function readSessionChildEntriesInDatabase(e,t,n=`full`){let r=typeof t==`string`?[t]:t;if(r.length===0)return[];let i=a(e.db).selectFrom(`session_nodes`).select(`session_key`),o=i.where(`parent_session_key`,`in`,r).union(i.where(`spawned_by`,`in`,r)),s=d(e.db,selectReadableSessionEntryRows(e,n).where(`session_key`,`in`,o).where(`session_key`,`not in`,r).orderBy(`session_key`,`asc`)).rows.filter(e=>!E(e.session_key)),c=prepareSqliteSessionEntryRowDecoder(e,s,n);return s.flatMap(e=>{let t=c(e);return t?[{sessionKey:e.session_key,entry:t}]:[]})}function readExactSessionEntryRow(t,n,r=`full`,i,a){let o=v(t,{sessionKey:n});if(o){let e=o.entryRows.get(n);return e&&{row:{...structuredClone(e.row),...a?{window:o.window?structuredClone(o.window):null,member_ids_json:JSON.stringify(o.hot.members.map(e=>e.identityId)),board_present:+!!o.hasBoard,transcriptWatermark:{sessionId:e.entry.sessionId,...o.hot.transcript.watermark}}:{}},entry:x(structuredClone(e.entry),{},r)}}return e(t.db,()=>{let e=readSelectedSessionEntryRows(t,n,r,i,a?{includeWindowFacts:a,includeBoardPresence:!0,includeMembership:!0}:void 0)[0];if(!e)return;let o=parseReadableSqliteSessionEntryRow(t,e,r);return o?{entry:o,row:e}:void 0})}function prepareExactSessionEntryRowReads(t,n,r=`full`,i,a){let o=v(t);return o&&!a?.includeRowTranscriptProjection&&r!==`delivery`&&n.every(e=>o.entryRows.has(e)&&(e===o.hot.target.sessionKey||!o.entryRows.get(e)))?e=>{let n=readExactSessionEntryRow(t,e,r,i,a?.includeWindowFacts);return n&&(a?.includeBoardPresence&&(n.row.board_present=+!!o.hasBoard),a?.includeMembership&&(n.row.member_ids_json=JSON.stringify(o.hot.members.map(e=>e.identityId)))),n}:e(t.db,()=>{let readRows=e=>readSelectedSessionEntryRows(t,e,r,i,a),o;try{o=readRows(n)}catch{return a?.includeBoardPresence||a?.includeMembership||a?.includeWindowFacts||a?.includeRowTranscriptProjection||a?.onParticipantProjectionError||a?.projectParticipants===!1?n=>e(t.db,()=>{let e=readRows(n)[0],i=e&&(a.projectParticipants===!1?parseReadableSessionEntryData(t,e,r):prepareSqliteSessionEntryRowDecoder(t,[e],r===`delivery`?`list`:r,!0,a.onParticipantProjectionError)(e));return e&&i?{entry:i,row:e}:void 0}):e=>readExactSessionEntryRow(t,e,r===`delivery`?`list`:r,i)}let s=new Map(o.map(e=>[e.session_key,e])),c=prepareSqliteSessionEntryRowDecoder(t,o,r,a?.projectParticipants!==!1,a?.onParticipantProjectionError);return n=>e(t.db,()=>{let e=s.get(M(n));if(!e)return;let t=c(e);return t?{entry:t,row:e}:void 0})})}function readExactSessionEntryRowValidated(t,n,r=`full`){return e(t.db,()=>(O(t),readExactSessionEntryRow(t,n,r)))}function readSessionEntryTargetRow(t,n,r={}){return e(t.db,()=>{O(t);let e=n.storeKeys.flatMap(e=>{let n=readSelectedSessionEntryRows(t,e.trim(),r.projection??`full`,void 0,r.includeWindowFacts?{includeWindowFacts:!0,includeMembership:!0,includeBoardPresence:!0}:void 0)[0];if(!n)return[];let i=parseReadableSqliteSessionEntryRow(t,n,r.projection);return i||r.guardRetainedWindows?[{entry:i,row:n}]:[]});if(e.length>1)throw j(`duplicate rows resolve to canonical session key ${n.canonicalKey}`);let i=e[0];if(i&&i.row.session_key!==n.canonicalKey&&!r.allowCanonicalMove)throw j(`non-canonical persisted row resolves to session key ${n.canonicalKey}`);return i})}function readQualifiedSessionEntryRow(e,t,r,i={}){let a=n(r),o=a?.rest??r;return t!==e.agentId||a&&a.agentId!==t||o!==`global`&&o!==`unknown`?readSessionEntryRow(e,r,i.projection,i.includeWindowFacts):readSessionEntryTargetRow(e,{canonicalKey:r,storeKeys:[o,`agent:${t}:${o}`]},{...i,guardRetainedWindows:!0})}const I=` \v\f\r \xA0            \u2028\u2029   `;function readSessionEntryByIdInDatabase(e,t){return A(e,()=>{O(e);let n=a(e.db).selectFrom(`session_nodes`).select(`session_key`).$if(t.orderBy===`updatedAt`,e=>e.orderBy(`updated_at`,`desc`)).orderBy(`session_key`);for(let r of t.orderBy===`updatedAt`?[!0]:[!1,!0]){let i=l(e.db,r?n.where(e=>e(e.fn(`trim`,[`current_session_id`,e.val(I)]),`=`,t.sessionId)):n.where(`current_session_id`,`=`,t.sessionId));for(let{session_key:n}of i){if(E(n))continue;let r=readExactSessionEntryRowValidated(e,n,t.projection);if(r)return{sessionKey:n,entry:r.entry}}}})}export{readExactSessionEntryRow as a,readSessionEntryByIdInDatabase as c,readSessionEntrySelectionInDatabase as d,readSessionKeyBySessionIdInDatabase as f,prepareSqliteSessionEntryRowDecoder as i,readSessionEntryIdentity as l,parseReadableSessionEntryData as n,readExactSessionEntryRowValidated as o,validateDeliveryCanonicalSessionEntry as p,prepareExactSessionEntryRowReads as r,readSessionChildEntriesInDatabase as s,I as t,readSessionEntryRow as u};