openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.35 kB
JavaScript
import{a as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import{s as t}from"./transcript-chat-message-content-DeOIjx3p.mjs";import{D as n}from"./transcript-chat-display-projection.canvas-BVwGQyPV.mjs";import{r}from"./transcript-transcript-only-openclaw-assistant-DcGn8i-E.mjs";import{r as i}from"./transcript-session-accessor.sqlite-read-BJEDuBkE.mjs";import{t as a}from"./transcript-session-accessor.sqlite-active-projection-BdMyLOaK.mjs";import{n as o}from"./transcript-session-accessor.sqlite-active-events-read-CHG4jzRS.mjs";import{t as s}from"./transcript-session-provenance-B1NPzgd1.mjs";import{t as c}from"./transcript-external-content-DpLkqT6p.mjs";import{t as l}from"./transcript-transcript-visible-events-CHgxSdgW.mjs";const u=String.raw`(?:(?:\|DSML\|)|(?:\uFF5CDSML\uFF5C))?`,d=String.raw`(?:tool_calls?|function_calls?|tool_use_error)`,f=new RegExp(String.raw`<${u}${d}\b[^>]*>`+String.raw`[\s\S]*?(?:<\/${u}${d}>|$)`,`gi`),p=/<(system|assistant|user)\b[^>]*>[\s\S]*?<\/\1>/gi,m=/<\/?(?:system|assistant|user)\b[^>]*>/gi,h=/[\u0085\u2028\u2029]/gu;function quoteSessionMemoryText(e){return JSON.stringify(e).replace(h,e=>`\\u${e.charCodeAt(0).toString(16).padStart(4,`0`)}`)}function sanitizeSessionMemoryTranscriptText(e){return c(e).replace(f,``).replace(p,``).replace(m,``).trim()||null}function extractTextMessageContent(e){if(typeof e==`string`)return e;if(!Array.isArray(e))return;let t=e.flatMap(e=>{if(!e||typeof e!=`object`)return[];let t=e;return t.type===`text`&&typeof t.text==`string`?[t.text]:[]});return t.length>0?t.join(`
`):void 0}function renderSessionMemoryRecords(e){let i=[],a,o=`untrusted`;for(let c of e){if(!c||typeof c!=`object`)continue;let e=c;if(e.type!==`message`||!e.message)continue;let l=e.message.role;if(l!==`user`&&l!==`assistant`||!(`content`in e.message))continue;l===`user`&&(o=s(e.message,o));let u=s(e.message,o);if(l===`user`&&n(e.message)||e.message.display===!1)continue;let d=t(e.message,extractTextMessageContent(e.message.content)),f=d?sanitizeSessionMemoryTranscriptText(d):null;f&&(l===`user`&&(a=void 0),!f.startsWith(`/`)&&(r(e.message)&&f===a||(i.push({line:`${l}: ${quoteSessionMemoryText(f)}`,originClass:u}),l===`assistant`&&(a=f))))}return i}function countSessionMemoryMessages(e){return renderSessionMemoryRecords(e).length}function getRecentSessionProjectionFromEvents(e,t=15){let n=Number.isFinite(t)?Math.max(0,Math.floor(t)):0;if(n===0)return null;let r=renderSessionMemoryRecords(e).slice(-n);return r.length===0?null:{content:r.map(e=>e.line).join(`
`),originClass:r.some(e=>e.originClass===`untrusted`||e.originClass===`system`)?`untrusted`:`agent`}}const g=8388608,_=4096;function relinkCapturedActiveMessageEvents(t){let n=null;return t.map((t,r)=>{if(!e(t)||t.type!==`message`)return t;let i=typeof t.id==`string`?t.id:`session-memory-${r+1}`,a={...t,id:i,parentId:n};return n=i,a})}function captureRecentSessionMemoryEvents(e,t){let n=[],r=0,i=0,a=1/0;for(;i<a&&i<_&&r<g&&countSessionMemoryMessages(l(relinkCapturedActiveMessageEvents(n)))<t;){let t=o(e,{maxBytes:g-r,maxMessages:Math.min(256,_-i),offset:i});if(a=t.totalMessages,t.scannedMessages===0)break;n.unshift(...t.events.map(({event:e})=>e)),r+=t.serializedBytes,i+=t.scannedMessages}return relinkCapturedActiveMessageEvents(n)}function selectCurrentMemoryWindow(t){let n=l(t),r=n.findLastIndex(t=>e(t)&&t.type===`reset`),i=n[r];if(!e(i))return n;let a=typeof i.firstKeptEntryId==`string`?n.findIndex(t=>e(t)&&t.id===i.firstKeptEntryId):-1;return[...(a>=0&&a<r?n.slice(a,r):[]).filter(t=>e(t)&&t.type===`message`&&e(t.message)&&(t.message.role===`user`||t.message.role===`assistant`)),...n.slice(r+1)]}function captureAuthoritativeMemoryEvents(t,n){let r=selectCurrentMemoryWindow(i(t,{readOnly:!0,resolvedScope:n}).events).filter(t=>e(t)&&t.type===`message`).slice(-4096),a=[],o=0;for(let e of r.toReversed()){let t=Buffer.byteLength(JSON.stringify(e))+1;o+t<=g&&(a.unshift(e),o+=t)}return relinkCapturedActiveMessageEvents(a)}function readSessionMemoryCapture({scope:e,resolvedScope:t,messageCount:n}){let r;try{r=a(e,e=>captureRecentSessionMemoryEvents(e,n),{readOnly:!0,resolvedScope:t})}catch{r=captureAuthoritativeMemoryEvents(e,t)}let i=getRecentSessionProjectionFromEvents(r,n);return i?{status:`available`,...i}:{status:`available`,content:null,originClass:`agent`}}export{readSessionMemoryCapture};