import type { IncomingMessage, ServerResponse } from 'http'; import { deleteChannelAccountConfig, deleteChannelConfig, getChannelFormValues, listConfiguredChannelAccounts, listConfiguredChannels, readOpenClawConfig, saveChannelConfig, setChannelDefaultAccount, setChannelEnabled, validateChannelConfig, validateChannelCredentials, } from '../../utils/channel-config'; import { assignChannelAccountToAgent, clearAllBindingsForChannel, clearChannelBinding, listAgentsSnapshot, } from '../../utils/agent-config'; import { ensureDingTalkPluginInstalled, ensureFeishuPluginInstalled, ensureQQBotPluginInstalled, ensureWeComPluginInstalled, } from '../../utils/plugin-install'; import { computeChannelRuntimeStatus, pickChannelRuntimeStatus, type ChannelRuntimeAccountSnapshot, } from '../../../src/lib/channel-status'; import { whatsAppLoginManager } from '../../utils/whatsapp-login'; import type { HostApiContext } from '../context'; import { parseJsonBody, sendJson } from '../route-utils'; function scheduleGatewayChannelRestart(ctx: HostApiContext, reason: string): void { if (ctx.gatewayManager.getStatus().state === 'stopped') { return; } ctx.gatewayManager.debouncedRestart(); void reason; } // Plugin-based channels require a full Gateway process restart to properly // initialize / tear-down plugin connections. SIGUSR1 in-process reload is // not sufficient for channel plugins (see restartGatewayForAgentDeletion). const FORCE_RESTART_CHANNELS = new Set(['dingtalk', 'wecom', 'whatsapp', 'feishu', 'qqbot']); function scheduleGatewayChannelSaveRefresh( ctx: HostApiContext, channelType: string, reason: string, ): void { if (ctx.gatewayManager.getStatus().state === 'stopped') { return; } if (FORCE_RESTART_CHANNELS.has(channelType)) { ctx.gatewayManager.debouncedRestart(); void reason; return; } ctx.gatewayManager.debouncedReload(); void reason; } function toComparableConfig(input: Record): Record { const next: Record = {}; for (const [key, value] of Object.entries(input)) { if (value === undefined || value === null) continue; if (typeof value === 'string') { next[key] = value.trim(); continue; } if (typeof value === 'number' || typeof value === 'boolean') { next[key] = String(value); } } return next; } function isSameConfigValues( existing: Record | undefined, incoming: Record, ): boolean { if (!existing) return false; const next = toComparableConfig(incoming); const keys = new Set([...Object.keys(existing), ...Object.keys(next)]); if (keys.size === 0) return false; for (const key of keys) { if ((existing[key] ?? '') !== (next[key] ?? '')) { return false; } } return true; } async function ensureScopedChannelBinding(channelType: string, accountId?: string): Promise { // Multi-agent safety: only bind when the caller explicitly scopes the account. // Global channel saves (no accountId) must not override routing to "main". if (!accountId) return; const agents = await listAgentsSnapshot(); if (!agents.entries || agents.entries.length === 0) return; // Keep backward compatibility for the legacy default account. if (accountId === 'default') { if (agents.entries.some((entry) => entry.id === 'main')) { await assignChannelAccountToAgent('main', channelType, 'default'); } return; } // Legacy compatibility: if accountId matches an existing agentId, keep auto-binding. if (agents.entries.some((entry) => entry.id === accountId)) { await assignChannelAccountToAgent(accountId, channelType, accountId); } } interface GatewayChannelStatusPayload { channelOrder?: string[]; channels?: Record; channelAccounts?: Record>; channelDefaultAccountId?: Record; } interface ChannelAccountView { accountId: string; name: string; configured: boolean; connected: boolean; running: boolean; linked: boolean; lastError?: string; status: 'connected' | 'connecting' | 'disconnected' | 'error'; isDefault: boolean; agentId?: string; } interface ChannelAccountsView { channelType: string; defaultAccountId: string; status: 'connected' | 'connecting' | 'disconnected' | 'error'; accounts: ChannelAccountView[]; } async function buildChannelAccountsView(ctx: HostApiContext): Promise { const [configuredChannels, configuredAccounts, openClawConfig, agentsSnapshot] = await Promise.all([ listConfiguredChannels(), listConfiguredChannelAccounts(), readOpenClawConfig(), listAgentsSnapshot(), ]); let gatewayStatus: GatewayChannelStatusPayload | null; try { gatewayStatus = await ctx.gatewayManager.rpc('channels.status', { probe: true }); } catch { gatewayStatus = null; } const channelTypes = new Set([ ...configuredChannels, ...Object.keys(configuredAccounts), ...Object.keys(gatewayStatus?.channelAccounts || {}), ]); const channels: ChannelAccountsView[] = []; for (const channelType of channelTypes) { const channelAccountsFromConfig = configuredAccounts[channelType]?.accountIds ?? []; const hasLocalConfig = configuredChannels.includes(channelType) || Boolean(configuredAccounts[channelType]); const channelSection = openClawConfig.channels?.[channelType]; const channelSummary = (gatewayStatus?.channels?.[channelType] as { error?: string; lastError?: string } | undefined) ?? undefined; const fallbackDefault = typeof channelSection?.defaultAccount === 'string' && channelSection.defaultAccount.trim() ? channelSection.defaultAccount : 'default'; const defaultAccountId = configuredAccounts[channelType]?.defaultAccountId ?? gatewayStatus?.channelDefaultAccountId?.[channelType] ?? fallbackDefault; const runtimeAccounts = gatewayStatus?.channelAccounts?.[channelType] ?? []; const hasRuntimeConfigured = runtimeAccounts.some((account) => account.configured === true); if (!hasLocalConfig && !hasRuntimeConfigured) { continue; } const runtimeAccountIds = runtimeAccounts .map((account) => account.accountId) .filter((accountId): accountId is string => typeof accountId === 'string' && accountId.trim().length > 0); const accountIds = Array.from(new Set([...channelAccountsFromConfig, ...runtimeAccountIds, defaultAccountId])); const accounts: ChannelAccountView[] = accountIds.map((accountId) => { const runtime = runtimeAccounts.find((item) => item.accountId === accountId); const runtimeSnapshot: ChannelRuntimeAccountSnapshot = runtime ?? {}; const status = computeChannelRuntimeStatus(runtimeSnapshot); return { accountId, name: runtime?.name || accountId, configured: channelAccountsFromConfig.includes(accountId) || runtime?.configured === true, connected: runtime?.connected === true, running: runtime?.running === true, linked: runtime?.linked === true, lastError: typeof runtime?.lastError === 'string' ? runtime.lastError : undefined, status, isDefault: accountId === defaultAccountId, agentId: agentsSnapshot.channelAccountOwners[`${channelType}:${accountId}`], }; }).sort((left, right) => { if (left.accountId === defaultAccountId) return -1; if (right.accountId === defaultAccountId) return 1; return left.accountId.localeCompare(right.accountId); }); channels.push({ channelType, defaultAccountId, status: pickChannelRuntimeStatus(runtimeAccounts, channelSummary), accounts, }); } return channels.sort((left, right) => left.channelType.localeCompare(right.channelType)); } export async function handleChannelRoutes( req: IncomingMessage, res: ServerResponse, url: URL, ctx: HostApiContext, ): Promise { if (url.pathname === '/api/channels/configured' && req.method === 'GET') { sendJson(res, 200, { success: true, channels: await listConfiguredChannels() }); return true; } if (url.pathname === '/api/channels/accounts' && req.method === 'GET') { try { const channels = await buildChannelAccountsView(ctx); sendJson(res, 200, { success: true, channels }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname === '/api/channels/default-account' && req.method === 'PUT') { try { const body = await parseJsonBody<{ channelType: string; accountId: string }>(req); await setChannelDefaultAccount(body.channelType, body.accountId); scheduleGatewayChannelSaveRefresh(ctx, body.channelType, `channel:setDefaultAccount:${body.channelType}`); sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname === '/api/channels/binding' && req.method === 'PUT') { try { const body = await parseJsonBody<{ channelType: string; accountId: string; agentId: string }>(req); await assignChannelAccountToAgent(body.agentId, body.channelType, body.accountId); scheduleGatewayChannelSaveRefresh(ctx, body.channelType, `channel:setBinding:${body.channelType}`); sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname === '/api/channels/binding' && req.method === 'DELETE') { try { const body = await parseJsonBody<{ channelType: string; accountId: string }>(req); await clearChannelBinding(body.channelType, body.accountId); scheduleGatewayChannelSaveRefresh(ctx, body.channelType, `channel:clearBinding:${body.channelType}`); sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname === '/api/channels/config/validate' && req.method === 'POST') { try { const body = await parseJsonBody<{ channelType: string }>(req); sendJson(res, 200, { success: true, ...(await validateChannelConfig(body.channelType)) }); } catch (error) { sendJson(res, 500, { success: false, valid: false, errors: [String(error)], warnings: [] }); } return true; } if (url.pathname === '/api/channels/credentials/validate' && req.method === 'POST') { try { const body = await parseJsonBody<{ channelType: string; config: Record }>(req); sendJson(res, 200, { success: true, ...(await validateChannelCredentials(body.channelType, body.config)) }); } catch (error) { sendJson(res, 500, { success: false, valid: false, errors: [String(error)], warnings: [] }); } return true; } if (url.pathname === '/api/channels/whatsapp/start' && req.method === 'POST') { try { const body = await parseJsonBody<{ accountId: string }>(req); await whatsAppLoginManager.start(body.accountId); sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname === '/api/channels/whatsapp/cancel' && req.method === 'POST') { try { await whatsAppLoginManager.stop(); sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname === '/api/channels/config' && req.method === 'POST') { try { const body = await parseJsonBody<{ channelType: string; config: Record; accountId?: string }>(req); if (body.channelType === 'dingtalk') { const installResult = await ensureDingTalkPluginInstalled(); if (!installResult.installed) { sendJson(res, 500, { success: false, error: installResult.warning || 'DingTalk plugin install failed' }); return true; } } if (body.channelType === 'wecom') { const installResult = await ensureWeComPluginInstalled(); if (!installResult.installed) { sendJson(res, 500, { success: false, error: installResult.warning || 'WeCom plugin install failed' }); return true; } } if (body.channelType === 'qqbot') { const installResult = await ensureQQBotPluginInstalled(); if (!installResult.installed) { sendJson(res, 500, { success: false, error: installResult.warning || 'QQ Bot plugin install failed' }); return true; } } if (body.channelType === 'feishu') { const installResult = await ensureFeishuPluginInstalled(); if (!installResult.installed) { sendJson(res, 500, { success: false, error: installResult.warning || 'Feishu plugin install failed' }); return true; } } const existingValues = await getChannelFormValues(body.channelType, body.accountId); if (isSameConfigValues(existingValues, body.config)) { await ensureScopedChannelBinding(body.channelType, body.accountId); sendJson(res, 200, { success: true, noChange: true }); return true; } await saveChannelConfig(body.channelType, body.config, body.accountId); await ensureScopedChannelBinding(body.channelType, body.accountId); scheduleGatewayChannelSaveRefresh(ctx, body.channelType, `channel:saveConfig:${body.channelType}`); sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname === '/api/channels/config/enabled' && req.method === 'PUT') { try { const body = await parseJsonBody<{ channelType: string; enabled: boolean }>(req); await setChannelEnabled(body.channelType, body.enabled); scheduleGatewayChannelRestart(ctx, `channel:setEnabled:${body.channelType}`); sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname.startsWith('/api/channels/config/') && req.method === 'GET') { try { const channelType = decodeURIComponent(url.pathname.slice('/api/channels/config/'.length)); const accountId = url.searchParams.get('accountId') || undefined; sendJson(res, 200, { success: true, values: await getChannelFormValues(channelType, accountId), }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } if (url.pathname.startsWith('/api/channels/config/') && req.method === 'DELETE') { try { const channelType = decodeURIComponent(url.pathname.slice('/api/channels/config/'.length)); const accountId = url.searchParams.get('accountId') || undefined; if (accountId) { await deleteChannelAccountConfig(channelType, accountId); await clearChannelBinding(channelType, accountId); scheduleGatewayChannelSaveRefresh(ctx, channelType, `channel:deleteAccount:${channelType}`); } else { await deleteChannelConfig(channelType); await clearAllBindingsForChannel(channelType); scheduleGatewayChannelRestart(ctx, `channel:deleteConfig:${channelType}`); } sendJson(res, 200, { success: true }); } catch (error) { sendJson(res, 500, { success: false, error: String(error) }); } return true; } void ctx; return false; }