UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 5.29 kB
import"./src-0ci69Prg.mjs";import{n as safeParseJsonRecord}from"./json-coercion-DNNwq4_v.mjs";import{t as stableStringify}from"./stable-stringify-Cy0VZ0ci.mjs";import{n as groupPluginRecords}from"./record-groups-BlmybP6F.mjs";import{l as hasInterSessionUserProvenance,r as INTER_SESSION_PROMPT_PREFIX_BASE}from"./input-provenance-TIcSrNt5.mjs";import{s as markInboundContextLabel}from"./strip-inbound-meta-ftpq205E.mjs";import{n as isTextContentBlock}from"./content-blocks-BxEK1-dg.mjs";import{r as formatContextJsonBlock}from"./channel-prompt-context-BnX4GzIH.mjs";import{t as isRunnerToolCallBlock}from"./attempt-tool-call-block-type-BYJ18lfz.mjs"; //!src/agents/embedded-agent-runner/run/attempt-history.ts const LEADING_TIMESTAMP_ENVELOPE_RE=/^\[[A-Za-z]{3} \d{4}-\d{2}-\d{2} \d{2}:\d{2}[^\]]*\] */;const CONVERSATION_INFO_LABEL=markInboundContextLabel(`Conversation info:`);function splitLeadingTimestampEnvelope(e){const t=e.match(LEADING_TIMESTAMP_ENVELOPE_RE)?.[0]??``;return{envelope:t,body:e.slice(t.length)}}function readFirstUserText(e){if(typeof e===`string`)return e;return Array.isArray(e)?e.find(isTextContentBlock)?.text:void 0}function hasNonBlankUserText(e){return typeof e===`string`?Boolean(e.trim()):Array.isArray(e)&&e.some(e=>isTextContentBlock(e)&&Boolean(e.text.trim()))}function contentMatchesTimestampOverride(e,t){const n=readFirstUserText(e);return n!==void 0&&(n===t.text||n===t.alternateText)}function resolveUserTranscriptMessages(e,t,n){if(!t?.length)return;const r=Array.from({length:e.length},()=>void 0);const i=new Set(t);const a=groupPluginRecords(i,e=>e.runtimeMessage);for(const[t,n]of e.entries()){if(n.role!==`user`||n.operatorMessage)continue;const e=a.get(n)?.shift();if(!e)continue;r[t]=e.transcriptMessage;i.delete(e)}if(i.size===0)return r;const o=/* @__PURE__ */ new Map;for(const e of i){const t=e.runtimeMessage.timestamp;if(typeof t!==`number`||!Number.isFinite(t))continue;const n=o.get(t)??[];n.push(e);o.set(t,n)}const s=findActiveUserMessageIndex(e);for(const[t,a]of e.entries()){if(a.role!==`user`||a.operatorMessage||r[t])continue;const e=a.timestamp;const c=(typeof e===`number`?o.get(e):void 0)?.find(r=>i.has(r)&&userContentMatchesTranscriptContext(a.content,r.runtimeMessage.content,t===s||typeof n?.runtimeTimestamp===`number`&&n.runtimeTimestamp===e?n:void 0));if(!c)continue;r[t]=c.transcriptMessage;i.delete(c)}return r}function userContentMatchesTranscriptContext(e,t,n){const r=readFirstUserText(e);const i=readFirstUserText(t);if(r!==void 0&&r===i)return true;if(r===void 0&&i===void 0&&Array.isArray(e)&&Array.isArray(t)&&stableStringify(e)===stableStringify(t))return true;return Boolean(n&&contentMatchesTimestampOverride(e,n)&&contentMatchesTimestampOverride(t,n))}function normalizePersistedSenderValue(e){if(typeof e!==`string`)return;return e.replaceAll(`\0`,``).trim()||void 0}function readPersistedSender(e){const t=Reflect.get(e,`__openclaw`);if(!t||typeof t!==`object`||Array.isArray(t))return;const n=t;const r={id:normalizePersistedSenderValue(n[`senderId`]),name:normalizePersistedSenderValue(n[`senderName`]),username:normalizePersistedSenderValue(n[`senderUsername`])};return Object.values(r).some(e=>e!==void 0)?r:void 0}function mergeSenderIntoLeadingConversationInfo(e,t){const{body:n,envelope:r}=splitLeadingTimestampEnvelope(e);const i=`${CONVERSATION_INFO_LABEL}\n\`\`\`json\n`;if(!n.startsWith(i))return;const a=n.indexOf("\n```",i.length);if(a===-1)return;const o=safeParseJsonRecord(n.slice(i.length,a));if(!o)return;const s=n.slice(a+4);return`${r}${formatContextJsonBlock(CONVERSATION_INFO_LABEL,{...o,sender:t})}${s}`}function prependContextToUserMessage(e,t){const n=formatContextJsonBlock(CONVERSATION_INFO_LABEL,{sender:t});const projectText=e=>{const{body:r,envelope:i}=splitLeadingTimestampEnvelope(e);if(r===n||r.startsWith(`${n}\n\n`))return;return mergeSenderIntoLeadingConversationInfo(e,t)??`${i}${r?`${n}\n\n${r}`:n}`};const r=e.content;if(typeof r===`string`){const t=projectText(r);return t===void 0||t===r?e:{...e,content:t}}if(!Array.isArray(r))return e;const i=r.findIndex(isTextContentBlock);if(i===-1)return{...e,content:[{type:`text`,text:n},...r]};const a=r[i];const o=projectText(a.text);if(o===void 0)return e;const s=r.slice();s[i]={...a,text:o};return{...e,content:s}}function hasInterSessionPromptPrefix(e){const t=readFirstUserText(e.content);return t!==void 0&&splitLeadingTimestampEnvelope(t).body.startsWith(`[Inter-session message]`)}function projectPersistedSenderContext(e,t){let n=false;const r=e.map((e,r)=>{if(e.role!==`user`||e.operatorMessage)return e;const i=t?.[r]??e;if(hasInterSessionUserProvenance(e)||hasInterSessionUserProvenance(i)||hasInterSessionPromptPrefix(e)||hasInterSessionPromptPrefix(i))return e;const a=readPersistedSender(i);if(!a)return e;const o=prependContextToUserMessage(e,a);n||=o!==e;return o});return n?r:e}function findActiveUserMessageIndex(e){for(let t=e.length-1;t>=0;t-=1){const n=e[t];if(n?.role===`user`&&!n.operatorMessage)return t;if(n?.role===`assistant`&&(!Array.isArray(n.content)||!n.content.some(isRunnerToolCallBlock)))return-1}return-1}export{resolveUserTranscriptMessages as a,projectPersistedSenderContext as i,findActiveUserMessageIndex as n,hasNonBlankUserText as r,contentMatchesTimestampOverride as t};