UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.62 kB
import{l as normalizeOptionalString}from"./string-coerce-CpTFcSwY.mjs";import{n as ok,t as err}from"./result-CkiK_t2O.mjs";import{r as isIncognitoSessionKey}from"./session-key-DYtwLLn1.mjs";import"./session-key-BPgKfU2m.mjs";import{i as resolveSessionWorkStartError}from"./session-work-start-BNx7xs8p.mjs";import{Z as captureSessionTarget}from"./session-controller.state-BIDbpLPn.mjs";import{i as ErrorCodes}from"./missing-scope-error-CkgBgh6d.mjs";import{p as errorShape}from"./error-codes-CQNGhXT-.mjs";import{v as retainPreparedSessionEntryPredicate}from"./session-accessor.sqlite-entry-cache-publication-state-D1mC85IV.mjs";import{O as resolveEffectiveChatHistoryMaxChars}from"./chat-display-projection.canvas-BobptIQs.mjs";import{t as beginSessionEffect}from"./session-controller.lifecycle-M3NLAkoz.mjs";import{r as resolveRequestedSessionAgentId}from"./session-request-agent-DmeXmT1P.mjs";import"./sessions-DQD7zQ95.mjs";import{t as findCanonicalStoreMatch}from"./session-utils-store-selection-DiiUIML7.mjs";import{t as loadGatewaySessionEntryReadOnlyInWorker}from"./session-utils-store-worker-43BDHG7V.mjs";import{i as loadIncognitoGatewaySessionEntryReadOnly}from"./session-utils-store-CbjJRFJe.mjs";import{t as formatForLog}from"./ws-log-DRCSDKLv.mjs";import{o as projectChatDisplayMessage}from"./chat-display-projection.core-Cc0urtjV.mjs";import"./session-utils-Cd7daNqX.mjs";import"./chat-display-projection-CtPpZudi.mjs";import{l as resolveGlobalAwareNodeChatDeliveryKeys,u as sendGlobalAwareNodeChatPayload}from"./chat-broadcast-B3al3md-.mjs";import{t as appendInjectedAssistantMessageToTranscript}from"./chat-transcript-inject-HHNNtf8K.mjs"; //!src/gateway/server-methods/chat-inject.ts async function injectGatewayAssistantMessage(e,t,n){return await injectGatewayMessage(e,t,n)}async function injectGatewaySystemNotice(e,t,n){return await injectGatewayMessage(e,{...t,literal:true},n,{sourceTool:t.sourceTool})}async function injectGatewayMessage(e,t,n,r){const i=t.sessionKey;const a=normalizeOptionalString(t.agentId);const o=e.getRuntimeConfig();const s=resolveRequestedSessionAgentId(o,i,a);if(!s.ok)return err(s.error);const c={agentId:s.agentId};let l;const{agentId:u,storePath:d,entry:f,canonicalKey:p}=isIncognitoSessionKey(i)?loadIncognitoGatewaySessionEntryReadOnly(i,c,o):await loadGatewaySessionEntryReadOnlyInWorker({excludeInternalEffects:true,cfg:o,key:i,...c,onSelected:e=>{l?.release();const t=findCanonicalStoreMatch(e.store,e.storeKeys)?.entry;if(typeof e.capturedReadSource?.databaseIdentity===`string`)l=retainPreparedSessionEntryPredicate({databaseIdentity:`file:${e.capturedReadSource.databaseIdentity}`,sessionKey:e.canonicalKey,entry:t,matches:(e,n)=>n?.sessionId===t?.sessionId})}}).catch(e=>{l?.release();throw e});const m=f?.sessionId;if(!m||!d){l?.release();return err(errorShape(ErrorCodes.INVALID_REQUEST,`session not found`))}let h;try{const e=await beginSessionEffect({agentId:u,target:captureSessionTarget({storeScope:d,sessionKey:p,agentId:u,incarnation:m}),storeWriterIdentities:[p],assertAllowed:()=>{n?.();const e=l?.readCurrent();if(l&&!e)throw new Error(`Session "${p}" changed while starting work. Retry.`);const t=isIncognitoSessionKey(p)?loadIncognitoGatewaySessionEntryReadOnly(i,c).entry:e?.entry??f;if(t?.sessionId!==m)throw new Error(`Session "${p}" changed while starting work. Retry.`);const r=resolveSessionWorkStartError(p,t);if(r)throw new Error(r)}});try{h=await e.run(async()=>{n?.();return await appendInjectedAssistantMessageToTranscript({sessionKey:p,message:t.message,content:t.content,label:t.label,systemNotice:r,idempotencyKey:t.idempotencyKey,humanMentions:t.humanMentions,...t.literal?{literal:true}:{},...n?{assertCurrent:n}:{},onMessageCommitted:t.onMessageCommitted,sessionId:m,storePath:d,agentId:u,config:o})})}finally{e.release()}}catch(e){return err(errorShape(ErrorCodes.INVALID_REQUEST,formatForLog(e)))}finally{l?.release()}if(!h.ok||!h.messageId||!h.message)return err(errorShape(ErrorCodes.UNAVAILABLE,`failed to write chat history: ${h.error??`unknown error`}`));if(h.replayed)return ok({messageId:h.messageId,replayed:true});const g=projectChatDisplayMessage(h.message,{maxChars:resolveEffectiveChatHistoryMaxChars()});const _={runId:`inject-${h.messageId}`,sessionKey:p,...u?{agentId:u}:{},seq:0,state:`final`,message:g};e.broadcast(`chat`,_,{sessionKeys:resolveGlobalAwareNodeChatDeliveryKeys({cfg:o,sessionKey:p,agentId:u})});sendGlobalAwareNodeChatPayload({context:e,sessionKey:p,agentId:u,event:`chat`,payload:_});return ok({messageId:h.messageId})}export{injectGatewaySystemNotice as n,injectGatewayAssistantMessage as t};