@@ -4,17 +4,36 @@ import type { OpenClawConfig } from "../../config/config.js";
44import {
55 listDeliverableMessageChannels ,
66 type DeliverableMessageChannel ,
7+ isDeliverableMessageChannel ,
78 normalizeMessageChannel ,
89} from "../../utils/message-channel.js" ;
910
1011export type MessageChannelId = DeliverableMessageChannel ;
12+ export type MessageChannelSelectionSource =
13+ | "explicit"
14+ | "tool-context-fallback"
15+ | "single-configured" ;
1116
1217const getMessageChannels = ( ) => listDeliverableMessageChannels ( ) ;
1318
1419function isKnownChannel ( value : string ) : boolean {
1520 return getMessageChannels ( ) . includes ( value as MessageChannelId ) ;
1621}
1722
23+ function resolveKnownChannel ( value ?: string | null ) : MessageChannelId | undefined {
24+ const normalized = normalizeMessageChannel ( value ) ;
25+ if ( ! normalized ) {
26+ return undefined ;
27+ }
28+ if ( ! isDeliverableMessageChannel ( normalized ) ) {
29+ return undefined ;
30+ }
31+ if ( ! isKnownChannel ( normalized ) ) {
32+ return undefined ;
33+ }
34+ return normalized as MessageChannelId ;
35+ }
36+
1837function isAccountEnabled ( account : unknown ) : boolean {
1938 if ( ! account || typeof account !== "object" ) {
2039 return true ;
@@ -67,21 +86,44 @@ export async function listConfiguredMessageChannels(
6786export async function resolveMessageChannelSelection ( params : {
6887 cfg : OpenClawConfig ;
6988 channel ?: string | null ;
70- } ) : Promise < { channel : MessageChannelId ; configured : MessageChannelId [ ] } > {
89+ fallbackChannel ?: string | null ;
90+ } ) : Promise < {
91+ channel : MessageChannelId ;
92+ configured : MessageChannelId [ ] ;
93+ source : MessageChannelSelectionSource ;
94+ } > {
7195 const normalized = normalizeMessageChannel ( params . channel ) ;
7296 if ( normalized ) {
7397 if ( ! isKnownChannel ( normalized ) ) {
98+ const fallback = resolveKnownChannel ( params . fallbackChannel ) ;
99+ if ( fallback ) {
100+ return {
101+ channel : fallback ,
102+ configured : await listConfiguredMessageChannels ( params . cfg ) ,
103+ source : "tool-context-fallback" ,
104+ } ;
105+ }
74106 throw new Error ( `Unknown channel: ${ String ( normalized ) } ` ) ;
75107 }
76108 return {
77109 channel : normalized as MessageChannelId ,
78110 configured : await listConfiguredMessageChannels ( params . cfg ) ,
111+ source : "explicit" ,
112+ } ;
113+ }
114+
115+ const fallback = resolveKnownChannel ( params . fallbackChannel ) ;
116+ if ( fallback ) {
117+ return {
118+ channel : fallback ,
119+ configured : await listConfiguredMessageChannels ( params . cfg ) ,
120+ source : "tool-context-fallback" ,
79121 } ;
80122 }
81123
82124 const configured = await listConfiguredMessageChannels ( params . cfg ) ;
83125 if ( configured . length === 1 ) {
84- return { channel : configured [ 0 ] , configured } ;
126+ return { channel : configured [ 0 ] , configured, source : "single-configured" } ;
85127 }
86128 if ( configured . length === 0 ) {
87129 throw new Error ( "Channel is required (no configured channels detected)." ) ;
0 commit comments