UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

6 lines • 3.14 kB
import{i as e,r as t}from"./transcript-record-coerce-CPLgmAk9.mjs";import{v as n}from"./transcript-plugin-instance-invocation-7y9lfaeO.mjs";import{u as r}from"./transcript-string-coerce-DTk-8HSr.mjs";import{createHash as i}from"node:crypto";function createSessionEntryHasher(e){let t=i(`sha256`),n=[],r=0,a,capturePrefix=()=>{e===r&&(a=t.copy())};capturePrefix();let digest=(e,t,r)=>{let i=``,write=t=>{i+=t,i.length>=65536&&(e.update(i),i=``)},writeFacts=(e,r)=>{let i=t,a=!0;for(let t of n){if(i<=0)break;let n=e(t),o=Math.min(t.lines,i);for(let e=0;e<o;e++)write(a?n:r+n),a=!1;i-=o}};return write(` `),writeFacts(e=>String(e.sourceLine),`,`),write(` `),writeFacts(e=>String(e.timestampMs),`,`),write(` [`),writeFacts(e=>JSON.stringify(e.provenance),`,`),write(`] `),write(JSON.stringify(r)),e.update(i),e.digest(`hex`)};return{get lineCount(){return r},append(e,i){for(let n of e)t.update(r===0?n:`\n${n}`),r++,capturePrefix();e.length>0&&n.push({...i,lines:e.length})},reset(){t=i(`sha256`),n=[],r=0,a=void 0,capturePrefix()},finish(n){let i=a&&e!==void 0?digest(a,e,n):void 0;return{hash:digest(t,r,n),...i?{prefixHash:i}:{}}}}}function collectRawSessionText(e){if(typeof e==`string`)return e;if(!Array.isArray(e))return null;let n=[];for(let r of e){let e=t(r);e?.type===`text`&&typeof e.text==`string`&&n.push(e.text)}return n.length>0?n.join(` `):null}function projectSessionEntryRecord(t,n){let i=e(t);if(!i)return null;let a=i.type===`message`?e(i.message):void 0,o=e(a?.__openclaw),s=e(a?.provenance),c=e(i.data),timestamp=e=>typeof e==`string`||typeof e==`number`?e:void 0;return{type:r(i.type),id:r(i.id),firstKeptEntryId:i.firstKeptEntryId===void 0?void 0:r(i.firstKeptEntryId)??null,customType:r(i.customType),runId:r(i.runId),sessionKey:r(i.sessionKey),data:{runId:r(c?.runId),sessionKey:r(c?.sessionKey)},timestamp:timestamp(i.timestamp),message:a?{role:r(a.role),display:a.display===!1?!1:a.role===`assistant`||a.display===!0||void 0,timestamp:timestamp(a.timestamp),content:a.role===`user`||a.role===`assistant`?n(a,collectRawSessionText(a.content)??void 0)??null:null,provenance:{kind:r(s?.kind),sourceTool:r(s?.sourceTool)},__openclaw:{runId:r(o?.runId),senderIsOwner:o?.senderIsOwner===!0,turnTainted:o?.turnTainted===!0}}:void 0}}function splitLongSessionLine(e){let t=e.trim();if(!t)return[];if(t.length<=800)return[t];let r=[],i=0;for(;i<t.length;){if(t.length-i<=800){r.push(t.slice(i).trim());break}let e=i+800,a=e;for(let n=e;n>i;--n)if(t[n]===` `){a=n;break}for(a=n(t,i,a),r.push(t.slice(i,a).trim()),i=a;i<t.length&&t[i]===` `;)i+=1}return r.filter(Boolean)}function renderSessionExportLines(e,t){return splitLongSessionLine(t).map(t=>`${e}: ${t}`)}function parseSessionTimestampMs(e,t){let n=[t.timestamp,e.timestamp];for(let e of n){if(typeof e==`number`&&Number.isFinite(e)){let t=e>0&&e<1e11?e*1e3:e;if(Number.isFinite(t)&&t>0&&t<=864e13)return Math.floor(t)}if(typeof e==`string`){let t=Date.parse(e);if(Number.isFinite(t)&&t>0)return t}}return 0}export{renderSessionExportLines as a,projectSessionEntryRecord as i,createSessionEntryHasher as n,parseSessionTimestampMs as r,collectRawSessionText as t};