UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

1 lines • 2.98 kB
import{i as e}from"./transcript-record-coerce-CPLgmAk9.mjs";import"./transcript-subsystem-C0UTtuvS.mjs";import"./transcript-number-coercion-D2B2TTt-.mjs";import{a as t,i as n,n as r}from"./transcript-tool-content-DtMBDv21.mjs";import{n as i}from"./transcript-json-utf8-bytes-CWHhKtrl.mjs";import{h as a}from"./transcript-chat-display-projection.history-Dd7fL_uC.mjs";import{a as o}from"./transcript-transcript-image-artifacts-mFbveLhE.mjs";import{i as s}from"./transcript-session-history-tail-Db3CHtT1.mjs";const getMaxChatHistoryMessagesBytes=()=>6291456,c=131072;function createChatHistoryActivityProjection(t,i=[]){let a=new Map(i.map(e=>[e.messageId,e])),o=/* @__PURE__ */ new Map;for(let i of t){let t=s(i),c=t?a.get(t):void 0,l=e(i);c&&l&&(n(l.role)||l.role===`tool`||l.role===`function`||Array.isArray(l.content)&&l.content.some(t=>{let i=e(t)?.type;return r(i)||n(i)}))&&o.set(i,c)}return o}function createChatHistoryByteCounter(e,t=i){let n=/* @__PURE__ */ new Map,messageBytes=r=>{let i=n.get(r);if(i!==void 0)return i;let a=e?.get(r),o=t(r)+(a?t(a)+1:0);return n.set(r,o),o};return{messageBytes,framingBytes:t=>t.some(t=>e?.has(t))?13:0,messagesBytes:t=>(t.some(t=>e?.has(t))?15:2)+t.reduce((e,t)=>e+messageBytes(t),0)+Math.max(0,t.length-1)}}function buildChatHistoryUnavailableSentinel(){return{role:`assistant`,timestamp:Date.now(),content:[{type:`text`,text:`[chat.history unavailable: transcript too large to display; the full history is preserved on disk]`}]}}function buildOversizedHistoryPlaceholder(n){let r=e(n)??{},i=typeof r.role==`string`?r.role:`assistant`,a=typeof r.timestamp==`number`?r.timestamp:Date.now(),s=e(r.__openclaw)??{},c=Object.fromEntries([`toolCallId`,`tool_call_id`,`toolUseId`,`tool_use_id`,`toolName`,`tool_name`,`name`].filter(e=>typeof r[e]==`string`).map(e=>[e,r[e]])),l=t(r),u=typeof s.id==`string`?s.id:void 0,d=typeof s.seq==`number`?s.seq:void 0,f=typeof s.idempotencyKey==`string`?s.idempotencyKey:void 0,p=s.turnBoundary===!0,m=o(s.transcriptPosition),h=e(s.transport);return{role:i,timestamp:a,content:[{type:`text`,text:`[chat.history omitted: message too large]`}],...c,...l===void 0?{}:{isError:l},__openclaw:{...s.toolOutput?{toolOutput:s.toolOutput}:{},...u?{id:u}:{},...typeof s.runId==`string`?{runId:s.runId}:{},...d===void 0?{}:{seq:d},...f?{idempotencyKey:f}:{},...p?{turnBoundary:!0}:{},...m?{transcriptPosition:m}:{},...h?{transport:h}:{},truncated:!0,reason:`oversized`}}}function replaceOversizedChatHistoryMessages(e){let{messages:t,maxSingleMessageBytes:n}=e,r=e.byteCounter??createChatHistoryByteCounter(),i=0;return{messages:a(t,e=>{if(r.messageBytes(e)<=n)return e;i+=1;let t=buildOversizedHistoryPlaceholder(e);return r.messageBytes(t)<=n?t:buildChatHistoryUnavailableSentinel()}),replacedCount:i}}export{getMaxChatHistoryMessagesBytes as a,createChatHistoryByteCounter as i,buildOversizedHistoryPlaceholder as n,replaceOversizedChatHistoryMessages as o,createChatHistoryActivityProjection as r,c as t};