@@ -31,6 +31,7 @@ import type { EmbeddedPiQueueMessageOutcome } from "./pi-embedded-runner/runs.js
3131import {
3232 callGateway ,
3333 createBoundDeliveryRouter ,
34+ dispatchGatewayMethodInProcess ,
3435 getGlobalHookRunner ,
3536 isEmbeddedPiRunActive ,
3637 getRuntimeConfig ,
@@ -58,7 +59,7 @@ const MAX_TIMER_SAFE_TIMEOUT_MS = 2_147_000_000;
5859const AGENT_MEDIATED_COMPLETION_TOOLS = new Set ( [ "music_generate" , "video_generate" ] ) ;
5960
6061type SubagentAnnounceDeliveryDeps = {
61- callGateway : typeof callGateway ;
62+ dispatchGatewayMethodInProcess : typeof dispatchGatewayMethodInProcess ;
6263 getRuntimeConfig : typeof getRuntimeConfig ;
6364 getRequesterSessionActivity : ( requesterSessionKey : string ) => {
6465 sessionId ?: string ;
@@ -72,7 +73,7 @@ type SubagentAnnounceDeliveryDeps = {
7273} ;
7374
7475const defaultSubagentAnnounceDeliveryDeps : SubagentAnnounceDeliveryDeps = {
75- callGateway ,
76+ dispatchGatewayMethodInProcess ,
7677 getRuntimeConfig,
7778 getRequesterSessionActivity : ( requesterSessionKey : string ) => {
7879 const sessionId =
@@ -101,6 +102,21 @@ async function resolveQueueEmbeddedPiMessageOutcome(
101102 ) ;
102103}
103104
105+ async function runAnnounceAgentCall ( params : {
106+ agentParams : Record < string , unknown > ;
107+ expectFinal ?: boolean ;
108+ timeoutMs ?: number ;
109+ } ) : Promise < unknown > {
110+ return await subagentAnnounceDeliveryDeps . dispatchGatewayMethodInProcess (
111+ "agent" ,
112+ params . agentParams ,
113+ {
114+ expectFinal : params . expectFinal ,
115+ timeoutMs : params . timeoutMs ,
116+ } ,
117+ ) ;
118+ }
119+
104120function formatQueueWakeFailureError (
105121 fallback : string ,
106122 outcome : EmbeddedPiQueueMessageOutcome ,
@@ -638,6 +654,36 @@ async function sendSubagentAnnounceDirectly(params: {
638654 path : "none" ,
639655 } ;
640656 }
657+ const directAgentParams : Record < string , unknown > = {
658+ sessionKey : canonicalRequesterSessionKey ,
659+ message : params . triggerMessage ,
660+ deliver : shouldDeliverAgentFinal ,
661+ bestEffortDeliver : params . bestEffortDeliver ,
662+ internalEvents : params . internalEvents ,
663+ channel : shouldDeliverAgentFinal ? deliveryTarget . channel : sessionOnlyOriginChannel ,
664+ accountId : shouldDeliverAgentFinal
665+ ? deliveryTarget . accountId
666+ : sessionOnlyOriginChannel
667+ ? sessionOnlyOrigin ?. accountId
668+ : undefined ,
669+ to : shouldDeliverAgentFinal
670+ ? deliveryTarget . to
671+ : sessionOnlyOriginChannel
672+ ? sessionOnlyOrigin ?. to
673+ : undefined ,
674+ threadId : shouldDeliverAgentFinal
675+ ? deliveryTarget . threadId
676+ : sessionOnlyOriginChannel
677+ ? sessionOnlyOrigin ?. threadId
678+ : undefined ,
679+ inputProvenance : {
680+ kind : "inter_session" ,
681+ sourceSessionKey : params . sourceSessionKey ,
682+ sourceChannel : params . sourceChannel ?? INTERNAL_MESSAGE_CHANNEL ,
683+ sourceTool : params . sourceTool ?? "subagent_announce" ,
684+ } ,
685+ idempotencyKey : params . directIdempotencyKey ,
686+ } ;
641687 let directAnnounceResponse : unknown ;
642688 try {
643689 directAnnounceResponse = await runAnnounceDeliveryWithRetry ( {
@@ -646,38 +692,8 @@ async function sendSubagentAnnounceDirectly(params: {
646692 : "direct announce agent call" ,
647693 signal : params . signal ,
648694 run : async ( ) =>
649- await subagentAnnounceDeliveryDeps . callGateway ( {
650- method : "agent" ,
651- params : {
652- sessionKey : canonicalRequesterSessionKey ,
653- message : params . triggerMessage ,
654- deliver : shouldDeliverAgentFinal ,
655- bestEffortDeliver : params . bestEffortDeliver ,
656- internalEvents : params . internalEvents ,
657- channel : shouldDeliverAgentFinal ? deliveryTarget . channel : sessionOnlyOriginChannel ,
658- accountId : shouldDeliverAgentFinal
659- ? deliveryTarget . accountId
660- : sessionOnlyOriginChannel
661- ? sessionOnlyOrigin ?. accountId
662- : undefined ,
663- to : shouldDeliverAgentFinal
664- ? deliveryTarget . to
665- : sessionOnlyOriginChannel
666- ? sessionOnlyOrigin ?. to
667- : undefined ,
668- threadId : shouldDeliverAgentFinal
669- ? deliveryTarget . threadId
670- : sessionOnlyOriginChannel
671- ? sessionOnlyOrigin ?. threadId
672- : undefined ,
673- inputProvenance : {
674- kind : "inter_session" ,
675- sourceSessionKey : params . sourceSessionKey ,
676- sourceChannel : params . sourceChannel ?? INTERNAL_MESSAGE_CHANNEL ,
677- sourceTool : params . sourceTool ?? "subagent_announce" ,
678- } ,
679- idempotencyKey : params . directIdempotencyKey ,
680- } ,
695+ await runAnnounceAgentCall ( {
696+ agentParams : directAgentParams ,
681697 expectFinal : true ,
682698 timeoutMs : announceTimeoutMs ,
683699 } ) ,
@@ -797,11 +813,30 @@ export async function deliverSubagentAnnouncement(params: {
797813}
798814
799815export const __testing = {
800- setDepsForTest ( overrides ?: Partial < SubagentAnnounceDeliveryDeps > ) {
816+ setDepsForTest (
817+ overrides ?: Partial < SubagentAnnounceDeliveryDeps > & {
818+ callGateway ?: typeof callGateway ;
819+ } ,
820+ ) {
821+ const callGatewayOverride = overrides ?. callGateway ;
822+ const dispatchGatewayMethodInProcessOverride =
823+ overrides ?. dispatchGatewayMethodInProcess ??
824+ ( callGatewayOverride
825+ ? ( ( async ( method , agentParams , options ) =>
826+ await callGatewayOverride ( {
827+ method,
828+ params : agentParams ,
829+ expectFinal : options ?. expectFinal ,
830+ timeoutMs : options ?. timeoutMs ,
831+ } ) ) satisfies typeof dispatchGatewayMethodInProcess )
832+ : undefined ) ;
801833 subagentAnnounceDeliveryDeps = overrides
802834 ? {
803835 ...defaultSubagentAnnounceDeliveryDeps ,
804836 ...overrides ,
837+ ...( dispatchGatewayMethodInProcessOverride
838+ ? { dispatchGatewayMethodInProcess : dispatchGatewayMethodInProcessOverride }
839+ : { } ) ,
805840 }
806841 : defaultSubagentAnnounceDeliveryDeps ;
807842 } ,
0 commit comments