openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.83 kB
JavaScript
import{n as normalizeAgentId}from"./agent-id-cOfqmfln.mjs";import{N as parseAgentSessionKey}from"./session-key-BPgKfU2m.mjs";import{t as GATEWAY_CLIENT_CAPS}from"./client-info-Dr3-kuD1.mjs";import{l as getAgentRunContext}from"./agent-run-registry-ONwZSqWq.mjs";import{c as getReplyPayloadMetadata}from"./reply-payload-C7eQHHoS.mjs";import{s as tryResolveSessionCompatibilityOwnerAgentId}from"./session-request-agent-DmeXmT1P.mjs";import{t as capLiveAssistantText}from"./live-chat-projector-Ctx_O46F.mjs";import{o as projectChatDisplayMessage}from"./chat-display-projection.core-Cc0urtjV.mjs";import"./chat-display-projection-CtPpZudi.mjs";import{i as markChatAbortTerminalOutcome,n as captureChatAbortRegistrationGuard}from"./chat-abort-lifecycle-internal-zVsPSOIf.mjs";
//!src/gateway/server-methods/chat-broadcast.ts
function nextChatSeq(e,t){const n=(e.agentRunSeq.get(t)??0)+1;e.agentRunSeq.set(t,n);return n}function resolveGlobalAwareNodeChatDeliveryKeys(e){if(parseAgentSessionKey(e.sessionKey))return[e.sessionKey];const t=tryResolveSessionCompatibilityOwnerAgentId(e.cfg,e.sessionKey);const n=e.agentId??t;if(!n)return[e.sessionKey];const r=normalizeAgentId(n);const i=[`agent:${r}:${e.sessionKey}`];if(t&&normalizeAgentId(t)===r)i.push(e.sessionKey);return i}function resolveChatSessionKeys(e){return resolveGlobalAwareNodeChatDeliveryKeys({cfg:e.context.getRuntimeConfig?.()??{},sessionKey:e.sessionKey,agentId:e.agentId})}function sendGlobalAwareNodeChatPayload(e){const t=e.opts?.sessionKeys??resolveChatSessionKeys(e);if(t[0]){const n=e.opts?.sessionKeys?e.opts:{...e.opts,sessionKeys:t};e.context.nodeSendToSession(t[0],e.event,e.payload,n)}}function broadcastChatPayload(e,t,n,r){e.context.broadcast(t,n,r??{sessionKeys:resolveChatSessionKeys(e)});sendGlobalAwareNodeChatPayload({...e,event:t,payload:n,opts:r})}function broadcastChatFrame(e,t,n){const r=getAgentRunContext(e.runId);if(r?.isControlUiVisible===false&&r.projectSessionMessages===false)return;const i=nextChatSeq(e.context,e.runId);const a=parseAgentSessionKey(e.sessionKey)?void 0:e.agentId;const o=e.state===`delta`?{state:e.state,deltaText:e.text,replace:true,message:projectChatDisplayMessage({role:`assistant`,content:[{type:`text`,text:e.text}]})}:e.state!==`error`?{state:e.state,message:projectChatDisplayMessage(e.message),...e.stopReason?{stopReason:e.stopReason}:{}}:{state:e.state,errorMessage:e.errorMessage,...e.stopReason?{stopReason:e.stopReason}:{},...e.errorKind?{errorKind:e.errorKind}:{}};const s={runId:e.runId,sessionKey:e.sessionKey,...a?{agentId:a}:{},seq:i,...o};const c=e.context.chatRunState?.runs.get(e.runId)?.liveTextGroup?.signal;const l=t??(c?{group:c,settle:e.state===`final`||e.state===`error`?true:void 0}:void 0);const u={...n?{excludeClientCapability:n}:{},...l?{liveText:l}:{},...t?{dropIfSlow:true}:{},sessionKeys:resolveChatSessionKeys({context:e.context,sessionKey:e.sessionKey,agentId:a})};broadcastChatPayload({...e,agentId:a},`chat`,s,u)}function broadcastChatQueueNotice(e){broadcastChatFrame({...e,state:`delta`},void 0,GATEWAY_CLIENT_CAPS.CHAT_QUEUE_STATUS)}function broadcastChatDelta(e){if(!e.isCurrent())return;const t=capLiveAssistantText({text:e.text});const n=e.context.chatRunState.getOrCreate(e.runId);n.buffer=t;n.bufferIsCurrent=e.isCurrent;n.liveTextGroup??=new AbortController;broadcastChatFrame({...e,state:`delta`,text:t},{group:n.liveTextGroup.signal,isCurrent:e.isCurrent,coalesce:{key:JSON.stringify([`chat`,e.sessionKey,e.agentId]),merge:(e,t)=>t}})}function broadcastChatTerminal(e){if(e.terminalEntry&&!captureChatAbortRegistrationGuard([{runId:e.runId,entry:e.terminalEntry}])())return;e.context.chatRunState?.flushPendingText(e.runId);markChatAbortTerminalOutcome(e.terminalEntry);broadcastChatFrame(e);e.context.agentRunSeq.delete(e.runId)}function broadcastChatFinal(e){broadcastChatTerminal({...e,state:`final`})}function isBtwReplyPayload(e){return typeof e?.btw?.question===`string`&&e.btw.question.trim().length>0&&typeof e.text===`string`&&e.text.trim().length>0}function broadcastSideResult(e){const t=nextChatSeq(e.context,e.payload.runId);const n=parseAgentSessionKey(e.payload.sessionKey)?void 0:e.payload.agentId;const r={...e.payload,...n?{agentId:n}:{},seq:t};broadcastChatPayload({context:e.context,sessionKey:e.payload.sessionKey,agentId:n},`chat.side_result`,r)}function broadcastChatError(e){broadcastChatTerminal({...e,state:`error`})}function isSourceReplyTranscriptMirrorPayload(e){return Boolean(e&&getReplyPayloadMetadata(e)?.sourceReplyTranscriptMirror)}export{broadcastChatTerminal as a,isSourceReplyTranscriptMirrorPayload as c,broadcastChatQueueNotice as i,resolveGlobalAwareNodeChatDeliveryKeys as l,broadcastChatError as n,broadcastSideResult as o,broadcastChatFinal as r,isBtwReplyPayload as s,broadcastChatDelta as t,sendGlobalAwareNodeChatPayload as u};