UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

5 lines • 4.63 kB
import{t as pruneMapToMaxSize}from"./map-size-DLQqorvK.mjs"; //!src/auto-reply/reply/history.ts const HISTORY_CONTEXT_MARKER=`[Chat messages since your last reply - for context]`;const RECENT_HISTORY_CONTEXT_MARKER=`[Recent chat messages - for context]`;const CURRENT_MESSAGE_MARKER=`[Current message - respond to this]`;const MAX_HISTORY_KEYS=1e3;function evictOldHistoryKeys(e,t=MAX_HISTORY_KEYS){pruneMapToMaxSize(e,t)}function buildHistoryContext(e){const{historyText:t,currentMessage:n}=e;const r=e.lineBreak??` `;if(!t.trim())return n;return[e.historyKind===`recent`?RECENT_HISTORY_CONTEXT_MARKER:HISTORY_CONTEXT_MARKER,t,``,CURRENT_MESSAGE_MARKER,n].join(r)}function recordChannelHistoryEntryIfEnabled(e){const{historyMap:t,historyKey:n,entry:r}=e;if(!r||e.limit<=0)return[];const i=t.get(n)??[];i.push(r);const a=i.length-e.limit;if(a>0)i.splice(0,a);t.delete(n);t.set(n,i);evictOldHistoryKeys(t);return i}const recordPendingHistoryEntryIfEnabled=recordChannelHistoryEntryIfEnabled;const DEFAULT_HISTORY_MEDIA_LIMIT=4;function isLocalHistoryMediaPath(e){return/^[a-z]:[\\/]/i.test(e)||!/^[a-z][a-z0-9+.-]*:/i.test(e)}function isImageHistoryMediaEntry(e){if(e.kind&&e.kind!==`unknown`)return e.kind===`image`||e.kind===`sticker`;return e.contentType?.split(`;`)[0]?.trim().toLowerCase().startsWith(`image/`)===true}function normalizeHistoryMediaEntries(e){const t=Math.max(0,e.limit??DEFAULT_HISTORY_MEDIA_LIMIT);if(t<=0||!e.media?.length)return[];const n=[];const r=/* @__PURE__ */ new Set;for(const i of e.media){if(!isImageHistoryMediaEntry(i))continue;const a=i.path?.trim();if(!a||!isLocalHistoryMediaPath(a))continue;const o=`${i.messageId??e.messageId??``}\0${a}`;if(r.has(o))continue;r.add(o);n.push({path:a,contentType:i.contentType,kind:i.kind===`sticker`?`sticker`:`image`,messageId:i.messageId??e.messageId});if(n.length>=t)break}return n}async function recordChannelHistoryEntryWithMedia(e){if(!e.entry||e.limit<=0)return[];if(e.shouldRecord&&!e.shouldRecord())return[];const t=e.entry;const n=typeof e.media===`function`?recordChannelHistoryEntryIfEnabled(e):void 0;const r=typeof e.media===`function`?await e.media():e.media;if(e.shouldRecord&&!e.shouldRecord())return n??[];const i=normalizeHistoryMediaEntries({media:r,limit:e.mediaLimit,messageId:e.messageId??e.entry.messageId});if(n){const r=e.historyMap.get(e.historyKey);const a=r?.indexOf(t)??-1;if(i.length>0&&r&&a>=0)r[a]={...t,media:i};return n}return recordChannelHistoryEntryIfEnabled({...e,entry:i.length>0?{...e.entry,media:i}:e.entry})}const recordPendingHistoryEntryWithMedia=recordChannelHistoryEntryWithMedia;function buildChannelPendingHistoryContext(e){return buildHistoryContextFromMap({...e,entry:void 0,excludeLast:false})}const buildPendingHistoryContextFromMap=buildChannelPendingHistoryContext;function buildChannelInboundHistory(e){return buildInboundHistoryFromEntries({entries:e.historyMap.get(e.historyKey)??[],limit:e.limit})}const buildInboundHistoryFromMap=buildChannelInboundHistory;function buildInboundHistoryFromEntries(e){if(e.limit<=0)return;return e.entries.slice(-e.limit).map(e=>{const t={sender:e.sender,body:e.body,timestamp:e.timestamp};if(e.messageId)t.messageId=e.messageId;if(e.media?.length)t.media=e.media;return t})}function buildHistoryContextFromMap(e){if(e.limit<=0)return e.currentMessage;return buildHistoryContextFromEntries({entries:e.entry?recordChannelHistoryEntryIfEnabled(e):e.historyMap.get(e.historyKey)??[],currentMessage:e.currentMessage,formatEntry:e.formatEntry,lineBreak:e.lineBreak,excludeLast:e.excludeLast})}function clearChannelHistoryIfEnabled(e){if(e.limit>0)e.historyMap.set(e.historyKey,[])}const clearHistoryEntriesIfEnabled=clearChannelHistoryIfEnabled;function buildHistoryContextFromEntries(e){const t=e.lineBreak??` `;return buildHistoryContext({historyText:(e.excludeLast===false?e.entries:e.entries.slice(0,-1)).map(e.formatEntry).join(t),currentMessage:e.currentMessage,lineBreak:t,historyKind:e.historyKind})}export{recordChannelHistoryEntryWithMedia as _,buildChannelPendingHistoryContext as a,buildHistoryContextFromMap as c,buildPendingHistoryContextFromMap as d,clearChannelHistoryIfEnabled as f,recordChannelHistoryEntryIfEnabled as g,normalizeHistoryMediaEntries as h,buildChannelInboundHistory as i,buildInboundHistoryFromEntries as l,evictOldHistoryKeys as m,HISTORY_CONTEXT_MARKER as n,buildHistoryContext as o,clearHistoryEntriesIfEnabled as p,RECENT_HISTORY_CONTEXT_MARKER as r,buildHistoryContextFromEntries as s,CURRENT_MESSAGE_MARKER as t,buildInboundHistoryFromMap as u,recordPendingHistoryEntryIfEnabled as v,recordPendingHistoryEntryWithMedia as y};