fix: stop hardcoded channel fallback and auto-pick sole configured channel (#23357) (thanks @lbo728)

Co-authored-by: lbo728 <extreme0728@gmail.com>
This commit is contained in:
Peter Steinberger
2026-02-22 11:20:33 +01:00
parent e33d7fcd13
commit 1cd3b30907
18 changed files with 355 additions and 91 deletions

View File

@@ -1,8 +1,8 @@
import { resolveChannelDefaultAccountId } from "../channels/plugins/helpers.js";
import { getChannelPlugin, normalizeChannelId } from "../channels/plugins/index.js";
import { DEFAULT_CHAT_CHANNEL } from "../channels/registry.js";
import { loadConfig } from "../config/config.js";
import { loadConfig, type OpenClawConfig } from "../config/config.js";
import { setVerbose } from "../globals.js";
import { resolveMessageChannelSelection } from "../infra/outbound/channel-selection.js";
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
type ChannelAuthOptions = {
@@ -14,11 +14,15 @@ type ChannelAuthOptions = {
type ChannelPlugin = NonNullable<ReturnType<typeof getChannelPlugin>>;
type ChannelAuthMode = "login" | "logout";
function resolveChannelPluginForMode(
async function resolveChannelPluginForMode(
opts: ChannelAuthOptions,
mode: ChannelAuthMode,
): { channelInput: string; channelId: string; plugin: ChannelPlugin } {
const channelInput = opts.channel ?? DEFAULT_CHAT_CHANNEL;
cfg: OpenClawConfig,
): Promise<{ channelInput: string; channelId: string; plugin: ChannelPlugin }> {
const explicitChannel = opts.channel?.trim();
const channelInput = explicitChannel
? explicitChannel
: (await resolveMessageChannelSelection({ cfg })).channel;
const channelId = normalizeChannelId(channelInput);
if (!channelId) {
throw new Error(`Unsupported channel: ${channelInput}`);
@@ -32,24 +36,28 @@ function resolveChannelPluginForMode(
return { channelInput, channelId, plugin: plugin as ChannelPlugin };
}
function resolveAccountContext(plugin: ChannelPlugin, opts: ChannelAuthOptions) {
const cfg = loadConfig();
function resolveAccountContext(
plugin: ChannelPlugin,
opts: ChannelAuthOptions,
cfg: OpenClawConfig,
) {
const accountId = opts.account?.trim() || resolveChannelDefaultAccountId({ plugin, cfg });
return { cfg, accountId };
return { accountId };
}
export async function runChannelLogin(
opts: ChannelAuthOptions,
runtime: RuntimeEnv = defaultRuntime,
) {
const { channelInput, plugin } = resolveChannelPluginForMode(opts, "login");
const cfg = loadConfig();
const { channelInput, plugin } = await resolveChannelPluginForMode(opts, "login", cfg);
const login = plugin.auth?.login;
if (!login) {
throw new Error(`Channel ${channelInput} does not support login`);
}
// Auth-only flow: do not mutate channel config here.
setVerbose(Boolean(opts.verbose));
const { cfg, accountId } = resolveAccountContext(plugin, opts);
const { accountId } = resolveAccountContext(plugin, opts, cfg);
await login({
cfg,
accountId,
@@ -63,13 +71,14 @@ export async function runChannelLogout(
opts: ChannelAuthOptions,
runtime: RuntimeEnv = defaultRuntime,
) {
const { channelInput, plugin } = resolveChannelPluginForMode(opts, "logout");
const cfg = loadConfig();
const { channelInput, plugin } = await resolveChannelPluginForMode(opts, "logout", cfg);
const logoutAccount = plugin.gateway?.logoutAccount;
if (!logoutAccount) {
throw new Error(`Channel ${channelInput} does not support logout`);
}
// Auth-only flow: resolve account + clear session state only.
const { cfg, accountId } = resolveAccountContext(plugin, opts);
const { accountId } = resolveAccountContext(plugin, opts, cfg);
const account = plugin.config.resolveAccount(cfg, accountId);
await logoutAccount({
cfg,