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,5 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { DEFAULT_CHAT_CHANNEL } from "../channels/registry.js";
import { runChannelLogin, runChannelLogout } from "./channel-auth.js";
const mocks = vi.hoisted(() => ({
@@ -7,6 +6,7 @@ const mocks = vi.hoisted(() => ({
getChannelPlugin: vi.fn(),
normalizeChannelId: vi.fn(),
loadConfig: vi.fn(),
resolveMessageChannelSelection: vi.fn(),
setVerbose: vi.fn(),
login: vi.fn(),
logoutAccount: vi.fn(),
@@ -26,6 +26,10 @@ vi.mock("../config/config.js", () => ({
loadConfig: mocks.loadConfig,
}));
vi.mock("../infra/outbound/channel-selection.js", () => ({
resolveMessageChannelSelection: mocks.resolveMessageChannelSelection,
}));
vi.mock("../globals.js", () => ({
setVerbose: mocks.setVerbose,
}));
@@ -43,6 +47,10 @@ describe("channel-auth", () => {
mocks.normalizeChannelId.mockReturnValue("whatsapp");
mocks.getChannelPlugin.mockReturnValue(plugin);
mocks.loadConfig.mockReturnValue({ channels: {} });
mocks.resolveMessageChannelSelection.mockResolvedValue({
channel: "whatsapp",
configured: ["whatsapp"],
});
mocks.resolveChannelDefaultAccountId.mockReturnValue("default-account");
mocks.resolveAccount.mockReturnValue({ id: "resolved-account" });
mocks.login.mockResolvedValue(undefined);
@@ -65,22 +73,27 @@ describe("channel-auth", () => {
);
});
it("runs login with default channel/account when opts are empty", async () => {
it("auto-picks the single configured channel when opts are empty", async () => {
await runChannelLogin({}, runtime);
expect(mocks.normalizeChannelId).toHaveBeenCalledWith(DEFAULT_CHAT_CHANNEL);
expect(mocks.resolveChannelDefaultAccountId).toHaveBeenCalledWith({
plugin,
cfg: { channels: {} },
});
expect(mocks.resolveMessageChannelSelection).toHaveBeenCalledWith({ cfg: { channels: {} } });
expect(mocks.normalizeChannelId).toHaveBeenCalledWith("whatsapp");
expect(mocks.login).toHaveBeenCalledWith(
expect.objectContaining({
accountId: "default-account",
channelInput: DEFAULT_CHAT_CHANNEL,
channelInput: "whatsapp",
}),
);
});
it("propagates channel ambiguity when channel is omitted", async () => {
mocks.resolveMessageChannelSelection.mockRejectedValueOnce(
new Error("Channel is required when multiple channels are configured: telegram, slack"),
);
await expect(runChannelLogin({}, runtime)).rejects.toThrow("Channel is required");
expect(mocks.login).not.toHaveBeenCalled();
});
it("throws for unsupported channel aliases", async () => {
mocks.normalizeChannelId.mockReturnValueOnce(undefined);

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,

View File

@@ -221,7 +221,7 @@ export function registerChannelsCli(program: Command) {
channels
.command("login")
.description("Link a channel account (if supported)")
.option("--channel <channel>", "Channel alias (default: whatsapp)")
.option("--channel <channel>", "Channel alias (auto when only one is configured)")
.option("--account <id>", "Account id (accountId)")
.option("--verbose", "Verbose connection logs", false)
.action(async (opts) => {
@@ -240,7 +240,7 @@ export function registerChannelsCli(program: Command) {
channels
.command("logout")
.description("Log out of a channel session (if supported)")
.option("--channel <channel>", "Channel alias (default: whatsapp)")
.option("--channel <channel>", "Channel alias (auto when only one is configured)")
.option("--account <id>", "Account id (accountId)")
.action(async (opts) => {
await runChannelsCommandWithDanger(async () => {

View File

@@ -1,5 +1,4 @@
import type { Command } from "commander";
import { DEFAULT_CHAT_CHANNEL } from "../../channels/registry.js";
import { agentCliCommand } from "../../commands/agent-via-gateway.js";
import {
agentsAddCommand,
@@ -29,7 +28,7 @@ export function registerAgentCommands(program: Command, args: { agentChannelOpti
.option("--verbose <on|off>", "Persist agent verbose level for the session")
.option(
"--channel <channel>",
`Delivery channel: ${args.agentChannelOptions} (default: ${DEFAULT_CHAT_CHANNEL})`,
`Delivery channel: ${args.agentChannelOptions} (omit to use the main session channel)`,
)
.option("--reply-to <target>", "Delivery target override (separate from session routing)")
.option("--reply-channel <channel>", "Delivery channel override (separate from routing)")