feat(channels): enhance channel configuration with account support and improve agent handling (#420)

This commit is contained in:
Haze
2026-03-11 18:44:55 +08:00
committed by GitHub
Unverified
parent 53a51642ce
commit 050ee10850
7 changed files with 388 additions and 111 deletions

View File

@@ -5,9 +5,10 @@ import {
createAgent,
deleteAgentConfig,
listAgentsSnapshot,
resolveAccountIdForAgent,
updateAgentName,
} from '../../utils/agent-config';
import { deleteChannelConfig } from '../../utils/channel-config';
import { deleteChannelAccountConfig } from '../../utils/channel-config';
import type { HostApiContext } from '../context';
import { parseJsonBody, sendJson } from '../route-utils';
@@ -91,9 +92,11 @@ export async function handleAgentRoutes(
if (parts.length === 3 && parts[1] === 'channels') {
try {
const agentId = decodeURIComponent(parts[0]);
const channelType = decodeURIComponent(parts[2]);
await deleteChannelConfig(channelType);
const snapshot = await clearChannelBinding(channelType);
const accountId = resolveAccountIdForAgent(agentId);
await deleteChannelAccountConfig(channelType, accountId);
const snapshot = await clearChannelBinding(channelType, accountId);
scheduleGatewayReload(ctx, 'remove-agent-channel');
sendJson(res, 200, { success: true, ...snapshot });
} catch (error) {

View File

@@ -12,6 +12,7 @@ import {
validateChannelConfig,
validateChannelCredentials,
} from '../../utils/channel-config';
import { clearAllBindingsForChannel } from '../../utils/agent-config';
import { whatsAppLoginManager } from '../../utils/whatsapp-login';
import type { HostApiContext } from '../context';
import { parseJsonBody, sendJson } from '../route-utils';
@@ -242,7 +243,7 @@ export async function handleChannelRoutes(
if (url.pathname === '/api/channels/config' && req.method === 'POST') {
try {
const body = await parseJsonBody<{ channelType: string; config: Record<string, unknown> }>(req);
const body = await parseJsonBody<{ channelType: string; config: Record<string, unknown>; accountId?: string }>(req);
if (body.channelType === 'dingtalk') {
const installResult = await ensureDingTalkPluginInstalled();
if (!installResult.installed) {
@@ -271,7 +272,7 @@ export async function handleChannelRoutes(
return true;
}
}
await saveChannelConfig(body.channelType, body.config);
await saveChannelConfig(body.channelType, body.config, body.accountId);
scheduleGatewayChannelRestart(ctx, `channel:saveConfig:${body.channelType}`);
sendJson(res, 200, { success: true });
} catch (error) {
@@ -295,9 +296,10 @@ export async function handleChannelRoutes(
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),
values: await getChannelFormValues(channelType, accountId),
});
} catch (error) {
sendJson(res, 500, { success: false, error: String(error) });
@@ -309,6 +311,7 @@ export async function handleChannelRoutes(
try {
const channelType = decodeURIComponent(url.pathname.slice('/api/channels/config/'.length));
await deleteChannelConfig(channelType);
await clearAllBindingsForChannel(channelType);
scheduleGatewayChannelRestart(ctx, `channel:deleteConfig:${channelType}`);
sendJson(res, 200, { success: true });
} catch (error) {