1- import { normalizeChannelId } from "../../channels/plugins/index.js" ;
21import type { ChannelId } from "../../channels/plugins/types.js" ;
32import { DEFAULT_CHAT_CHANNEL } from "../../channels/registry.js" ;
43import type { ClawdbotConfig } from "../../config/config.js" ;
98} from "../../config/sessions.js" ;
109import { resolveMessageChannelSelection } from "../../infra/outbound/channel-selection.js" ;
1110import type { OutboundChannel } from "../../infra/outbound/targets.js" ;
12- import { resolveOutboundTarget } from "../../infra/outbound/targets.js" ;
13- import { INTERNAL_MESSAGE_CHANNEL , normalizeMessageChannel } from "../../utils/message-channel.js" ;
11+ import { resolveOutboundTarget , resolveSessionDeliveryTarget } from "../../infra/outbound/targets.js" ;
1412
1513export async function resolveDeliveryTarget (
1614 cfg : ClawdbotConfig ,
@@ -26,56 +24,63 @@ export async function resolveDeliveryTarget(
2624 mode : "explicit" | "implicit" ;
2725 error ?: Error ;
2826} > {
29- const requestedRaw = typeof jobPayload . channel === "string" ? jobPayload . channel : "last" ;
30- const requestedChannelHint = normalizeMessageChannel ( requestedRaw ) ?? requestedRaw ;
31- const explicitTo =
32- typeof jobPayload . to === "string" && jobPayload . to . trim ( ) ? jobPayload . to . trim ( ) : undefined ;
27+ const requestedChannel = typeof jobPayload . channel === "string" ? jobPayload . channel : "last" ;
28+ const explicitTo = typeof jobPayload . to === "string" ? jobPayload . to : undefined ;
3329
3430 const sessionCfg = cfg . session ;
3531 const mainSessionKey = resolveAgentMainSessionKey ( { cfg, agentId } ) ;
3632 const storePath = resolveStorePath ( sessionCfg ?. store , { agentId } ) ;
3733 const store = loadSessionStore ( storePath ) ;
3834 const main = store [ mainSessionKey ] ;
39- const lastChannel =
40- main ?. lastChannel && main . lastChannel !== INTERNAL_MESSAGE_CHANNEL
41- ? normalizeChannelId ( main . lastChannel )
42- : undefined ;
43- const lastTo = typeof main ?. lastTo === "string" ? main . lastTo . trim ( ) : "" ;
44- const lastAccountId = main ?. lastAccountId ;
4535
46- let channel : Exclude < OutboundChannel , "none" > | undefined =
47- requestedChannelHint === "last"
48- ? ( lastChannel ?? undefined )
49- : requestedChannelHint === INTERNAL_MESSAGE_CHANNEL
50- ? undefined
51- : ( normalizeChannelId ( requestedChannelHint ) ?? undefined ) ;
52- if ( ! channel ) {
36+ const preliminary = resolveSessionDeliveryTarget ( {
37+ entry : main ,
38+ requestedChannel,
39+ explicitTo,
40+ allowMismatchedLastTo : true ,
41+ } ) ;
42+
43+ let fallbackChannel : Exclude < OutboundChannel , "none" > | undefined ;
44+ if ( ! preliminary . channel ) {
5345 try {
5446 const selection = await resolveMessageChannelSelection ( { cfg } ) ;
55- channel = selection . channel ;
47+ fallbackChannel = selection . channel ;
5648 } catch {
57- channel = lastChannel ?? DEFAULT_CHAT_CHANNEL ;
49+ fallbackChannel = preliminary . lastChannel ?? DEFAULT_CHAT_CHANNEL ;
5850 }
5951 }
6052
61- const toCandidate = explicitTo ?? ( lastTo || undefined ) ;
62- const mode : "explicit" | "implicit" = explicitTo ? "explicit" : "implicit" ;
53+ const resolved = fallbackChannel
54+ ? resolveSessionDeliveryTarget ( {
55+ entry : main ,
56+ requestedChannel,
57+ explicitTo,
58+ fallbackChannel,
59+ allowMismatchedLastTo : true ,
60+ mode : preliminary . mode ,
61+ } )
62+ : preliminary ;
63+
64+ const channel = resolved . channel ?? fallbackChannel ?? DEFAULT_CHAT_CHANNEL ;
65+ const mode = resolved . mode as "explicit" | "implicit" ;
66+ const toCandidate = resolved . to ;
67+
6368 if ( ! toCandidate ) {
64- return { channel, to : undefined , accountId : lastAccountId , mode } ;
69+ return { channel, to : undefined , accountId : resolved . accountId , mode } ;
6570 }
6671
67- const resolved = resolveOutboundTarget ( {
72+ const docked = resolveOutboundTarget ( {
6873 channel,
6974 to : toCandidate ,
7075 cfg,
71- accountId : channel === lastChannel ? lastAccountId : undefined ,
76+ accountId : resolved . accountId ,
7277 mode,
7378 } ) ;
7479 return {
7580 channel,
76- to : resolved . ok ? resolved . to : undefined ,
77- accountId : channel === lastChannel ? lastAccountId : undefined ,
81+ to : docked . ok ? docked . to : undefined ,
82+ accountId : resolved . accountId ,
7883 mode,
79- error : resolved . ok ? undefined : resolved . error ,
84+ error : docked . ok ? undefined : docked . error ,
8085 } ;
8186}
0 commit comments