fix: complete per-bot agent preset lifecycle

This commit is contained in:
xmanrui 2026-08-22 00:20:23 +08:00
parent 8ab4b9e7de
commit 01576f65c6
27 changed files with 721 additions and 208 deletions

View file

@ -57,9 +57,8 @@ export function AgentPresetEditor({ agentPreset = '', disabled = false, onSave }
seen.add(item.id);
items.push(item);
}
if (current && !seen.has(current)) {
items.push({ id: current, label: current });
}
const currentUnavailable = Boolean(current && !seen.has(current));
if (currentUnavailable) items.push({ id: current, label: current, unavailable: true });
const inheritLabel = '跟随 Host 默认';
@ -92,10 +91,20 @@ export function AgentPresetEditor({ agentPreset = '', disabled = false, onSave }
...items.map((item) => h(
'option',
{ key: item.id, value: item.id },
item.label && item.label !== item.id ? `${item.label}(${item.id})` : item.id,
item.unavailable
? [item.id, '(已不可用)']
: item.label && item.label !== item.id ? `${item.label}(${item.id})` : item.id,
)),
),
h('small', { className: 'dim-presetHelp' }, '只影响之后新建的会话。'),
error ? h('p', { className: 'dim-presetError', role: 'alert' }, error) : null,
h(
'small',
{ className: 'dim-presetHelp' },
'只影响新建会话;若当前聊天已有会话,先发送 /new,再发送普通消息生效。',
),
error || currentUnavailable ? h(
'p',
{ className: 'dim-presetError', role: error ? 'alert' : 'status' },
error ?? '当前 Agent Preset 已不可用,请选择其他 Preset 或跟随 Host 默认。',
) : null,
);
}

View file

@ -119,10 +119,13 @@ const EN = Object.freeze({
'找不到要修改的机器人。': 'The bot could not be found.',
'Agent Preset': 'Agent Preset',
'跟随 Host 默认': 'Follow the Host default',
'只影响之后新建的会话。': 'Only later new sessions use this preset.',
'(已不可用)': ' (unavailable)',
'只影响新建会话;若当前聊天已有会话,先发送 /new,再发送普通消息生效。': 'This affects only new sessions. If the current chat already has a session, send /new, then send a regular message to apply it.',
'当前 Agent Preset 已不可用,请选择其他 Preset 或跟随 Host 默认。': 'The current Agent Preset is unavailable. Choose another preset or follow the Host default.',
'Agent Preset 修改失败,请重试。': 'Could not update the Agent Preset. Try again.',
'请选择 Agent Preset。': 'Choose an Agent Preset.',
'Agent Preset 无效。': 'The Agent Preset is invalid.',
'Agent Preset 不存在或不可用。': 'The Agent Preset does not exist or is unavailable.',
'尚未检查': 'Not checked yet',
'刚刚': 'Just now',
'检查连接': 'Check connection',

View file

@ -64,7 +64,9 @@ export async function createProductionController(ctx, config = {}, internals = {
if (canListConfiguredBots) {
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
}
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.remove === 'function'
? observeBotWorkspaceRemovals(configStore, { workspaces })
: configStore;
@ -90,7 +92,6 @@ export async function createProductionController(ctx, config = {}, internals = {
const harness = new Harness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
autostart: false,
dshBin: config.dshBin ?? 'dsh',
...(commandExecutor ? { commandExecutor } : {}),
@ -104,7 +105,7 @@ export async function createProductionController(ctx, config = {}, internals = {
logger,
createRuntime: async ({ botId, config: botConfig, clientSecret }) => {
const state = await stateFor(botId);
await workspaces.ensure(botId);
await workspaces.ensure(botId, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
return new Runtime({
config: botConfig,
@ -139,7 +140,7 @@ export async function createProductionController(ctx, config = {}, internals = {
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({
controller,

View file

@ -129,7 +129,7 @@ export class ConnectionSupervisor {
await this.#controller.initialize();
if (this.#closed) return;
const status = this.#controller.status();
const status = await this.#controller.status();
this.#resolveReady?.(status);
this.#resolveReady = null;
const current = totals(status);

View file

@ -78,7 +78,9 @@ export async function createProductionController(ctx, config = {}, internals = {
if (canListConfiguredBots) {
await workspaces.reconcile(configuredBots.map((bot) => bot.id));
}
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.id)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.id, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.removeBot === 'function'
? observeBotWorkspaceRemovals(configStore, {
workspaces,
@ -116,7 +118,6 @@ export async function createProductionController(ctx, config = {}, internals = {
const harness = new Harness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
// This plugin is already hosted by a running DSH process. Starting a
// second DSH would create a competing server and lifecycle.
autostart: false,
@ -134,7 +135,7 @@ export async function createProductionController(ctx, config = {}, internals = {
createRuntime: async ({ botId, config: botConfig, appSecret, repair }) => {
const state = await stateFor(botConfig);
const id = botId ?? botConfig.id ?? botConfig.appId;
await workspaces.ensure(id);
await workspaces.ensure(id, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId: id, workspaces, state });
return new Runtime({
lark,
@ -167,7 +168,7 @@ export async function createProductionController(ctx, config = {}, internals = {
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor: stateForBotId,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({

View file

@ -1,4 +1,8 @@
import QRCode from 'qrcode';
import {
normalizeAgentPresetCatalog,
normalizeAgentPresetId,
} from '../../../../src/channels/shared/agent-preset.mjs';
import { publicConnectionTestResult } from '../../../../src/channels/shared/connection-test.mjs';
import { resolveRpcAuthority } from '../../rpc-authority.mjs';
import { publicWorkspaceError, validWorkspacePayload } from '../shared/workspace-rpc.mjs';
@ -254,6 +258,7 @@ function publicBotEntry(entry) {
state: connectionState(source, registration, connected),
connected,
configured: source.configured === true,
agentPreset: normalizeAgentPresetId(source.agentPreset),
bot: publicBot(source.bot),
health: publicHealth(source, connected),
};
@ -295,6 +300,7 @@ export async function toPublicFeishuStatus(status, { encodeQr = qrCodeDataUrl }
bot: publicBot(source.bot),
health: publicHealth(source, connected),
bots,
agentPresetCatalog: normalizeAgentPresetCatalog(source.agentPresetCatalog),
totals: {
configured: bots.length || (source.configured === true ? 1 : 0),
connected: bots.length ? bots.filter((bot) => bot.connected).length : (connected ? 1 : 0),

View file

@ -58,7 +58,9 @@ export async function createProductionController(ctx, config = {}, internals = {
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
const configuredBots = configStore.list();
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.remove === 'function'
? observeBotWorkspaceRemovals(configStore, { workspaces })
: configStore;
@ -81,7 +83,6 @@ export async function createProductionController(ctx, config = {}, internals = {
const harness = new Harness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
autostart: false,
dshBin: config.dshBin ?? 'dsh',
...(commandExecutor ? { commandExecutor } : {}),
@ -95,7 +96,7 @@ export async function createProductionController(ctx, config = {}, internals = {
logger,
createRuntime: async ({ botId, config: botConfig, appSecret }) => {
const state = await stateFor(botId);
await workspaces.ensure(botId);
await workspaces.ensure(botId, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
return new Runtime({
config: botConfig,
@ -129,7 +130,7 @@ export async function createProductionController(ctx, config = {}, internals = {
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({
controller,

View file

@ -16,6 +16,10 @@ export function validAgentPresetPayload(payload) {
}
export function publicAgentPresetError(error) {
if (!['agent-preset-invalid', 'workspace-bot-not-found'].includes(error?.code)) return null;
if (![
'agent-preset-invalid',
'agent-preset-unavailable',
'workspace-bot-not-found',
].includes(error?.code)) return null;
return { code: error.code, message: error.message };
}

View file

@ -60,7 +60,9 @@ export async function createTokenProductionController(ctx, config, internals, de
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
const configuredBots = configStore.list();
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.remove === 'function'
? observeBotWorkspaceRemovals(configStore, { workspaces })
: configStore;
@ -82,7 +84,6 @@ export async function createTokenProductionController(ctx, config, internals, de
const harness = new ResolvedHarness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
autostart: false,
dshBin: config.dshBin ?? 'dsh',
...(commandExecutor ? { commandExecutor } : {}),
@ -96,7 +97,7 @@ export async function createTokenProductionController(ctx, config, internals, de
...(internals.inspectToken ? { inspectToken: internals.inspectToken } : {}),
createRuntime: async ({ botId, config: botConfig, token }) => {
const state = await stateFor(botId);
await workspaces.ensure(botId);
await workspaces.ensure(botId, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
return new ResolvedRuntime({
...channelRuntimeOptions,
@ -131,7 +132,7 @@ export async function createTokenProductionController(ctx, config, internals, de
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({
channel,

View file

@ -21,6 +21,7 @@ export function publicWorkspaceError(error) {
'workspace-not-directory',
'workspace-bot-not-found',
'agent-preset-invalid',
'agent-preset-unavailable',
].includes(error?.code)) return null;
return { code: error.code, message: error.message };
}

View file

@ -38,7 +38,9 @@ export async function createProductionController(ctx, config = {}, internals = {
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
const configuredBots = configStore.list();
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.remove === 'function'
? observeBotWorkspaceRemovals(configStore, { workspaces })
: configStore;
@ -60,7 +62,6 @@ export async function createProductionController(ctx, config = {}, internals = {
const harness = new ResolvedHarness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
autostart: false,
dshBin: config.dshBin ?? 'dsh',
...(commandExecutor ? { commandExecutor } : {}),
@ -74,7 +75,7 @@ export async function createProductionController(ctx, config = {}, internals = {
...(internals.inspectCredentials ? { inspectCredentials: internals.inspectCredentials } : {}),
createRuntime: async ({ botId, config: botConfig, botToken, appToken }) => {
const state = await stateFor(botId);
await workspaces.ensure(botId);
await workspaces.ensure(botId, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
return new ResolvedRuntime({
config: botConfig,
@ -109,7 +110,7 @@ export async function createProductionController(ctx, config = {}, internals = {
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({
channel: 'slack',

View file

@ -58,7 +58,9 @@ export async function createProductionController(ctx, config = {}, internals = {
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
const configuredBots = configStore.list();
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.remove === 'function'
? observeBotWorkspaceRemovals(configStore, { workspaces })
: configStore;
@ -84,7 +86,6 @@ export async function createProductionController(ctx, config = {}, internals = {
const harness = new Harness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
autostart: false,
dshBin: config.dshBin ?? 'dsh',
...(commandExecutor ? { commandExecutor } : {}),
@ -98,7 +99,7 @@ export async function createProductionController(ctx, config = {}, internals = {
logger,
createRuntime: async ({ botId, config: botConfig, secret }) => {
const state = await stateFor(botId);
await workspaces.ensure(botId);
await workspaces.ensure(botId, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
return new Runtime({
config: botConfig,
@ -133,7 +134,7 @@ export async function createProductionController(ctx, config = {}, internals = {
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({
controller,

View file

@ -61,7 +61,9 @@ export async function createProductionController(ctx, config = {}, internals = {
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
const configuredBots = configStore.list();
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.remove === 'function'
? observeBotWorkspaceRemovals(configStore, { workspaces })
: configStore;
@ -84,7 +86,6 @@ export async function createProductionController(ctx, config = {}, internals = {
const harness = new Harness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
autostart: false,
dshBin: config.dshBin ?? 'dsh',
...(commandExecutor ? { commandExecutor } : {}),
@ -98,7 +99,7 @@ export async function createProductionController(ctx, config = {}, internals = {
logger,
createRuntime: async ({ botId, config: accountConfig, token }) => {
const state = await stateFor(botId);
await workspaces.ensure(botId);
await workspaces.ensure(botId, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
return new Runtime({
api,
@ -133,7 +134,7 @@ export async function createProductionController(ctx, config = {}, internals = {
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({
controller,

View file

@ -65,7 +65,9 @@ export async function createProductionController(ctx, config = {}, internals = {
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
const configuredBots = configStore.list();
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId, {
defaultAgentPreset: config.agentPreset,
})));
const observedConfigStore = typeof configStore.remove === 'function'
? observeBotWorkspaceRemovals(configStore, { workspaces })
: configStore;
@ -87,7 +89,6 @@ export async function createProductionController(ctx, config = {}, internals = {
const harness = new Harness({
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
workspace: defaultWorkspace,
...(config.agentPreset == null ? {} : { agentPreset: config.agentPreset }),
autostart: false,
dshBin: config.dshBin ?? 'dsh',
...(commandExecutor ? { commandExecutor } : {}),
@ -101,7 +102,7 @@ export async function createProductionController(ctx, config = {}, internals = {
logger,
createRuntime: async ({ botId, config: botConfig, authDir }) => {
const state = await stateFor(botId);
await workspaces.ensure(botId);
await workspaces.ensure(botId, { defaultAgentPreset: config.agentPreset });
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
return new Runtime({
config: botConfig,
@ -140,7 +141,7 @@ export async function createProductionController(ctx, config = {}, internals = {
const controller = createWorkspaceAwareController(coreController, {
workspaces,
stateFor,
agentPresetCatalog: await listAgentPresetCatalog(ctx),
agentPresetCatalog: () => listAgentPresetCatalog(ctx),
});
const supervisor = createSupervisor({
channel: 'whatsapp',