UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.64 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{t as normalizeChatType}from"./chat-type-BzCV5nTQ.mjs";import{_ as resolveStagedMediaFacts,f as projectMediaFacts,g as resolveMediaFacts,v as stripLegacyMediaContextFields}from"./media-facts-03ZYaqC1.mjs";import{t as normalizeInternalTurnContext}from"./internal-turn-source-CN2hYUwe.mjs";import{t as resolveConversationLabel}from"./conversation-label-BnjavK5J.mjs";import{l as resolveCommandTurnContext,n as createCommandTurnContext}from"./command-turn-context-vtXH8Cqw.mjs";import{t as normalizeInboundTextNewlines}from"./inbound-text-BoSvn4e1.mjs"; //!src/auto-reply/reply/inbound-context.ts const FINALIZED_INBOUND_CONTEXT=Symbol(`openclaw.finalizedInboundContext`);function normalizeTextField(e){if(typeof e!==`string`)return;return normalizeInboundTextNewlines(e)}function isFinalizedInboundContext(e){return e[FINALIZED_INBOUND_CONTEXT]===true}function resolveCanonicalInboundText(e,t={}){const n=e.Body??``;const r=e.RawBody??e.Transcript??normalizeTextField(e.BodyStripped)??n;const i=t.forceBodyForAgent||t.forceBodyForCommands?r:normalizeTextField(e.rawText)??r;const a=t.forceBodyForAgent?n:normalizeTextField(e.agentText)??normalizeTextField(e.BodyForAgent)??e.CommandBody??i;const o=t.forceBodyForCommands?e.CommandBody??i:normalizeTextField(e.commandText)??normalizeTextField(e.BodyForCommands)??e.CommandBody??i;return{commandText:e.CommandInterpretationSuppressed===true?``:o,agentText:a,rawText:i}}function foldDeprecatedPromptContextFields(e){if(e.ChannelPromptContext===void 0&&e.UntrustedContext!==void 0)e.ChannelPromptContext=e.UntrustedContext;delete e.UntrustedContext;if(e.ChannelStructuredContext===void 0&&e.UntrustedStructuredContext!==void 0)e.ChannelStructuredContext=e.UntrustedStructuredContext;delete e.UntrustedStructuredContext}function applySupplementalContext(e){const t=e.SupplementalContext;if(!t)return;if(t.channelStructuredContext===void 0&&t.untrustedContext!==void 0)t.channelStructuredContext=t.untrustedContext;delete t.untrustedContext;const n={ReplyToId:t.quote?.id,ReplyToIdFull:t.quote?.fullId,ReplyToBody:t.quote?.body,ReplyToSender:t.quote?.sender,ReplyToIsQuote:t.quote?.isQuote,ForwardedFrom:t.forwarded?.from,ForwardedFromType:t.forwarded?.fromType,ForwardedFromId:t.forwarded?.fromId,ForwardedDate:t.forwarded?.date,ThreadStarterBody:t.thread?.starterBody,ThreadHistoryBody:t.thread?.historyBody,ThreadLabel:t.thread?.label,GroupSystemPrompt:t.groupSystemPrompt,ChannelStructuredContext:t.channelStructuredContext};for(const[t,r]of Object.entries(n))if(r!==void 0&&e[t]===void 0)e[t]=r;delete e.SupplementalContext}function finalizeInboundContextImpl(e,t,n){const r=e;normalizeInternalTurnContext(r);foldDeprecatedPromptContextFields(r);applySupplementalContext(r);r.Body=normalizeTextField(r.Body)??``;r.RawBody=normalizeTextField(r.RawBody);r.CommandBody=normalizeTextField(r.CommandBody);r.Transcript=normalizeTextField(r.Transcript);r.ThreadStarterBody=normalizeTextField(r.ThreadStarterBody);r.ThreadHistoryBody=normalizeTextField(r.ThreadHistoryBody);r.GroupSystemPrompt=normalizeTextField(r.GroupSystemPrompt);if(Array.isArray(r.ChannelPromptContext))r.ChannelPromptContext=r.ChannelPromptContext.map(normalizeTextField).filter(e=>Boolean(e));const i=normalizeChatType(r.ChatType);if(i&&(t.forceChatType||r.ChatType!==i))r.ChatType=i;Object.assign(r,resolveCanonicalInboundText(r,t));r.BodyForAgent=r.agentText;r.BodyForCommands=r.commandText;const a=resolveConversationLabel(r);if(a)r.ConversationLabel=a;const o=r.CommandInterpretationSuppressed===true;r.CommandAuthorized=!o&&r.CommandAuthorized===true;r.CommandTurn=o?createCommandTurnContext(`message`,{authorized:false,body:``}):resolveCommandTurnContext(r);if(r.CommandTurn.source===`native`||r.CommandTurn.source===`text`){r.CommandSource=r.CommandTurn.source;r.CommandAuthorized=r.CommandTurn.authorized}else r.CommandSource=void 0;const s=(r.MediaStaged===true||normalizeOptionalString(r.MediaWorkspaceDir)?resolveStagedMediaFacts(r):resolveMediaFacts(r)).map(e=>(e.path||e.url)&&!e.contentType&&!e.kind?Object.assign(e,{contentType:`application/octet-stream`}):e);if(s.length>0){r.media=s;if(n)Object.assign(r,projectMediaFacts(s))}if(!n)stripLegacyMediaContextFields(r);Object.defineProperty(r,FINALIZED_INBOUND_CONTEXT,{configurable:true,value:true});return r}function finalizeInboundContext(e,t={}){return finalizeInboundContextImpl(e,t,false)}function finalizeInboundContextForSdk(e,t={}){return finalizeInboundContextImpl(e,t,true)}export{finalizeInboundContextForSdk as n,isFinalizedInboundContext as r,finalizeInboundContext as t};