@@ -9,8 +9,6 @@ import { resolveDefaultAgentId } from "../../agents/agent-scope.js";
99import { resolveProviderIdForAuth } from "../../agents/provider-auth-aliases.js" ;
1010import { createAgentRunRestartAbortError } from "../../agents/run-termination.js" ;
1111import { dispatchInboundMessageWithProjectedDispatcher } from "../../auto-reply/dispatch.js" ;
12- import { resolveQueueSettings } from "../../auto-reply/reply/queue/settings-runtime.js" ;
13- import { beginReplyMessageInjectionTarget } from "../../auto-reply/reply/reply-run-registry.js" ;
1412import { getAgentEventLifecycleGeneration } from "../../infra/agent-events.js" ;
1513import {
1614 emitDiagnosticsTimelineEvent ,
@@ -38,8 +36,12 @@ import {
3836 createChatSendDispatchErrorLifecycle ,
3937 handleChatSendSetupError ,
4038} from "./chat-send-dispatch-errors.js" ;
41- import { finalizeAcceptedChatSendMessageInjection } from "./chat-send-message-injection.js" ;
39+ import {
40+ beginChatSendMessageInjection ,
41+ finalizeAcceptedChatSendMessageInjection ,
42+ } from "./chat-send-message-injection.js" ;
4243import { finalizeChatSendNonAgentReplies } from "./chat-send-nonagent-finalization.js" ;
44+ import { ACTIVE_RUN_CHANGED_ERROR_REASON } from "./chat-send-pre-admission.js" ;
4345import {
4446 applyChatSendReplyContextFields ,
4547 resolveChatSendReplyContext ,
@@ -228,39 +230,35 @@ export async function handleChatSend(
228230 logGateway : context . logGateway ,
229231 userTurn,
230232 } ) ;
231- const queueSettings = resolveQueueSettings ( {
232- cfg,
233- channel : ctx . Provider ,
234- sessionEntry : entry ,
235- inlineMode : p . queueMode ,
236- } ) ;
237- const messageInjectionAttempt =
238- messageInjectionTarget && ! isInternalTextSlashCommandTurn && ! p . replyToId
239- ? beginReplyMessageInjectionTarget (
240- messageInjectionTarget ,
241- ctx . BodyForAgent ?? ctx . Body ?? rawMessage ,
242- {
243- steeringMode : "all" ,
244- isInboundUserMessage : true ,
245- ...( replyOptionImages ?. length ? { images : replyOptionImages } : { } ) ,
246- ...( imageOrder . length > 0 ? { imageOrder } : { } ) ,
247- ...( replyOptionMedia ?. length ? { media : replyOptionMedia } : { } ) ,
248- waitForTranscriptCommit : true ,
249- ...( queueSettings . debounceMs !== undefined
250- ? { debounceMs : queueSettings . debounceMs }
251- : { } ) ,
252- taskSuggestionDeliveryMode : supportsTaskSuggestions ? "gateway" : undefined ,
253- userTurnTranscriptRecorder : userTurnRecorder ,
233+ const beginCapturedMessageInjection = ( ) =>
234+ messageInjectionTarget && ! isInternalTextSlashCommandTurn
235+ ? beginChatSendMessageInjection ( {
236+ target : messageInjectionTarget ,
237+ text : ctx . BodyForAgent ?? ctx . Body ?? rawMessage ,
238+ replyContext : p . replyToId
239+ ? { body : ctx . BodyForAgent ?? ctx . Body ?? rawMessage , cfg, ctx, sessionEntry : entry }
240+ : undefined ,
241+ images : replyOptionImages ,
242+ imageOrder,
243+ media : replyOptionMedia ,
244+ queueSettings : {
245+ cfg,
246+ channel : ctx . Provider ,
247+ sessionEntry : entry ,
248+ inlineMode : p . queueMode ,
254249 } ,
255- )
250+ taskSuggestionDeliveryMode : supportsTaskSuggestions ? "gateway" : undefined ,
251+ userTurnTranscriptRecorder : userTurnRecorder ,
252+ } )
256253 : undefined ;
257- if ( messageInjectionAttempt ) {
254+ let messageInjectionAttempt = p . replyToId ? undefined : beginCapturedMessageInjection ( ) ;
255+ if ( messageInjectionTarget && ! isInternalTextSlashCommandTurn ) {
258256 // Accepted injection never consumes plugin-bound media, but the shared
259257 // persistence promise still needs a rejection observer.
260258 void pluginBoundMediaPromise . catch ( ( ) => undefined ) ;
261- if ( messageInjectionAttempt . rejectBeforeAck ) {
262- return admitted . value . rejectActiveLeafChanged ( ) ;
263- }
259+ }
260+ if ( messageInjectionAttempt ?. rejectBeforeAck ) {
261+ return admitted . value . rejectActiveLeafChanged ( ) ;
264262 }
265263
266264 const serverTiming = shouldIncludeChatSendAckServerTiming ( clientInfo )
@@ -393,6 +391,10 @@ export async function handleChatSend(
393391 measureDiagnosticsTimelineSpan (
394392 "gateway.chat_send.dispatch_inbound" ,
395393 async ( ) => {
394+ if ( replyContextFieldsPromise ) {
395+ applyChatSendReplyContextFields ( ctx , await replyContextFieldsPromise ) ;
396+ messageInjectionAttempt = beginCapturedMessageInjection ( ) ;
397+ }
396398 if ( messageInjectionAttempt ) {
397399 const outcome = await messageInjectionAttempt . outcome ;
398400 if ( outcome . status === "accepted" ) {
@@ -412,11 +414,11 @@ export async function handleChatSend(
412414 counts : { tool : 0 , block : 0 , final : 0 } ,
413415 } ;
414416 }
417+ if ( p . replyToId ) {
418+ throw new Error ( ACTIVE_RUN_CHANGED_ERROR_REASON ) ;
419+ }
415420 }
416421 applyChatSendManagedMedia ( ctx , await pluginBoundMediaPromise ) ;
417- if ( replyContextFieldsPromise ) {
418- applyChatSendReplyContextFields ( ctx , await replyContextFieldsPromise ) ;
419- }
420422 const dispatchResult = await dispatchInboundMessageWithProjectedDispatcher ( {
421423 ctx,
422424 cfg,
@@ -517,7 +519,9 @@ export async function handleChatSend(
517519 fastModeOverride : p . fastMode ,
518520 queueModeOverride : p . queueMode ,
519521 userTurnTranscriptRecorder : userTurnRecorder ,
520- ...( messageInjectionAttempt ? { messageInjectionAttempted : true as const } : { } ) ,
522+ ...( messageInjectionTarget && ! isInternalTextSlashCommandTurn
523+ ? { messageInjectionAttempted : true as const }
524+ : { } ) ,
521525 ...( restartSafeAdmission ? { suppressNextUserMessagePersistence : true } : { } ) ,
522526 fastModeAutoOnSecondsOverride : p . fastAutoOnSeconds ,
523527 onAgentRunStart : ( runId ) => {
0 commit comments