11import type { SessionEntry } from "../../config/sessions/types.js" ;
2+ import type { InputProvenance } from "../../sessions/input-provenance.js" ;
3+ import {
4+ INTERNAL_MESSAGE_CHANNEL ,
5+ isDeliverableMessageChannel ,
6+ normalizeMessageChannel ,
7+ } from "../../utils/message-channel.js" ;
28import type { FinalizedMsgContext } from "../templating.js" ;
39
410export type EffectiveReplyRouteContext = Pick <
511 FinalizedMsgContext ,
6- "Provider" | "OriginatingChannel" | "OriginatingTo" | "AccountId"
12+ "Provider" | "Surface" | " OriginatingChannel" | "OriginatingTo" | "AccountId" | "InputProvenance "
713> ;
814
915export type EffectiveReplyRouteEntry = Pick <
@@ -15,24 +21,52 @@ export type EffectiveReplyRoute = {
1521 channel ?: string ;
1622 to ?: string ;
1723 accountId ?: string ;
24+ inheritedExternalRoute ?: boolean ;
1825} ;
1926
2027export function isSystemEventProvider ( provider ?: string ) : boolean {
2128 return provider === "heartbeat" || provider === "cron-event" || provider === "exec-event" ;
2229}
2330
31+ function isSessionsSendInterSessionHandoff ( inputProvenance : InputProvenance | undefined ) : boolean {
32+ return (
33+ inputProvenance ?. kind === "inter_session" &&
34+ inputProvenance . sourceTool ?. toLowerCase ( ) === "sessions_send"
35+ ) ;
36+ }
37+
2438export function resolveEffectiveReplyRoute ( params : {
2539 ctx : EffectiveReplyRouteContext ;
2640 entry ?: EffectiveReplyRouteEntry ;
2741} ) : EffectiveReplyRoute {
42+ const currentSurface =
43+ normalizeMessageChannel ( params . ctx . Provider ) ??
44+ normalizeMessageChannel ( params . ctx . Surface ) ??
45+ normalizeMessageChannel ( params . ctx . OriginatingChannel ) ;
46+ const persistedDeliveryContext = params . entry ?. deliveryContext ;
47+ const persistedDeliveryChannel = normalizeMessageChannel ( persistedDeliveryContext ?. channel ) ;
48+ if (
49+ isSessionsSendInterSessionHandoff ( params . ctx . InputProvenance ) &&
50+ currentSurface === INTERNAL_MESSAGE_CHANNEL &&
51+ persistedDeliveryChannel &&
52+ persistedDeliveryChannel !== INTERNAL_MESSAGE_CHANNEL &&
53+ isDeliverableMessageChannel ( persistedDeliveryChannel ) &&
54+ persistedDeliveryContext ?. to
55+ ) {
56+ return {
57+ channel : persistedDeliveryChannel ,
58+ to : persistedDeliveryContext . to ,
59+ accountId : persistedDeliveryContext . accountId ,
60+ inheritedExternalRoute : true ,
61+ } ;
62+ }
2863 if ( ! isSystemEventProvider ( params . ctx . Provider ) ) {
2964 return {
3065 channel : params . ctx . OriginatingChannel ,
3166 to : params . ctx . OriginatingTo ,
3267 accountId : params . ctx . AccountId ,
3368 } ;
3469 }
35- const persistedDeliveryContext = params . entry ?. deliveryContext ;
3670 return {
3771 channel :
3872 params . ctx . OriginatingChannel ??
0 commit comments