@@ -184,6 +184,8 @@ async function invokeAgent(
184184 respond ?: ReturnType < typeof vi . fn > ;
185185 reqId ?: string ;
186186 context ?: GatewayRequestContext ;
187+ client ?: AgentHandlerArgs [ "client" ] ;
188+ isWebchatConnect ?: AgentHandlerArgs [ "isWebchatConnect" ] ;
187189 } ,
188190) {
189191 const respond = options ?. respond ?? vi . fn ( ) ;
@@ -192,8 +194,8 @@ async function invokeAgent(
192194 respond : respond as never ,
193195 context : options ?. context ?? makeContext ( ) ,
194196 req : { type : "req" , id : options ?. reqId ?? "agent-test-req" , method : "agent" } ,
195- client : null ,
196- isWebchatConnect : ( ) => false ,
197+ client : options ?. client ?? null ,
198+ isWebchatConnect : options ?. isWebchatConnect ?? ( ( ) => false ) ,
197199 } ) ;
198200 return respond ;
199201}
@@ -346,6 +348,56 @@ describe("gateway agent handler", () => {
346348 expect ( callArgs . bestEffortDeliver ) . toBe ( false ) ;
347349 } ) ;
348350
351+ it ( "keeps origin messageChannel as webchat while delivery channel uses last session channel" , async ( ) => {
352+ mockMainSessionEntry ( {
353+ sessionId : "existing-session-id" ,
354+ lastChannel : "telegram" ,
355+ lastTo : "12345" ,
356+ } ) ;
357+ mocks . updateSessionStore . mockImplementation ( async ( _path , updater ) => {
358+ const store : Record < string , unknown > = {
359+ "agent:main:main" : {
360+ sessionId : "existing-session-id" ,
361+ updatedAt : Date . now ( ) ,
362+ lastChannel : "telegram" ,
363+ lastTo : "12345" ,
364+ } ,
365+ } ;
366+ return await updater ( store ) ;
367+ } ) ;
368+ mocks . agentCommand . mockResolvedValue ( {
369+ payloads : [ { text : "ok" } ] ,
370+ meta : { durationMs : 100 } ,
371+ } ) ;
372+
373+ await invokeAgent (
374+ {
375+ message : "webchat turn" ,
376+ sessionKey : "agent:main:main" ,
377+ idempotencyKey : "test-webchat-origin-channel" ,
378+ } ,
379+ {
380+ reqId : "webchat-origin-1" ,
381+ client : {
382+ connect : {
383+ client : { id : "webchat-ui" , mode : "webchat" } ,
384+ } ,
385+ } as AgentHandlerArgs [ "client" ] ,
386+ isWebchatConnect : ( ) => true ,
387+ } ,
388+ ) ;
389+
390+ await vi . waitFor ( ( ) => expect ( mocks . agentCommand ) . toHaveBeenCalled ( ) ) ;
391+ const callArgs = mocks . agentCommand . mock . calls . at ( - 1 ) ?. [ 0 ] as {
392+ channel ?: string ;
393+ messageChannel ?: string ;
394+ runContext ?: { messageChannel ?: string } ;
395+ } ;
396+ expect ( callArgs . channel ) . toBe ( "telegram" ) ;
397+ expect ( callArgs . messageChannel ) . toBe ( "webchat" ) ;
398+ expect ( callArgs . runContext ?. messageChannel ) . toBe ( "webchat" ) ;
399+ } ) ;
400+
349401 it ( "handles missing cliSessionIds gracefully" , async ( ) => {
350402 mockMainSessionEntry ( { } ) ;
351403
0 commit comments