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

@ -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',