mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-09 07:53:20 +08:00
feat: support per-bot workspace switching
This commit is contained in:
parent
adcace3512
commit
44dd50216a
64 changed files with 3878 additions and 712 deletions
|
|
@ -12,6 +12,12 @@ import {
|
|||
} from '../../../../src/channels/feishu/plugin-config-store.mjs';
|
||||
import { MultiBotDshFeishuController } from '../../../../src/channels/feishu/multi-bot-controller.mjs';
|
||||
import { StateStore } from '../../../../src/channels/feishu/state-store.mjs';
|
||||
import {
|
||||
BotWorkspaceStore,
|
||||
createBotWorkspaceScope,
|
||||
createWorkspaceAwareController,
|
||||
observeBotWorkspaceRemovals,
|
||||
} from '../../../../src/channels/shared/bot-workspace-store.mjs';
|
||||
|
||||
function harnessOrigin(webServer, configured) {
|
||||
if (configured !== undefined) return new URL(configured);
|
||||
|
|
@ -34,6 +40,7 @@ function pluginPaths(config) {
|
|||
config: resolve(config.configPath ?? join(root, 'config.json')),
|
||||
legacyState: resolve(config.statePath ?? join(root, 'state.json')),
|
||||
bots: resolve(config.botsDir ?? join(root, 'bots')),
|
||||
workspaces: resolve(config.workspacesPath ?? join(root, 'workspaces.json')),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -58,6 +65,24 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|||
: (ctx.logger ?? console);
|
||||
const paths = pluginPaths(config);
|
||||
const configStore = await new ConfigStore(paths.config).load();
|
||||
const defaultWorkspace = resolve(config.workspace ?? process.cwd());
|
||||
const WorkspaceStore = internals.WorkspaceStore ?? BotWorkspaceStore;
|
||||
const workspaces = internals.workspaces
|
||||
?? await new WorkspaceStore(paths.workspaces, { defaultWorkspace }).load();
|
||||
const canListConfiguredBots = typeof configStore.list === 'function';
|
||||
const listConfiguredBots = () => canListConfiguredBots ? configStore.list() : [];
|
||||
const configuredBots = listConfiguredBots();
|
||||
if (canListConfiguredBots) {
|
||||
await workspaces.reconcile(configuredBots.map((bot) => bot.id));
|
||||
}
|
||||
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.id)));
|
||||
const observedConfigStore = typeof configStore.removeBot === 'function'
|
||||
? observeBotWorkspaceRemovals(configStore, {
|
||||
workspaces,
|
||||
method: 'removeBot',
|
||||
botIdFromRemoved: (removed) => removed.id,
|
||||
})
|
||||
: configStore;
|
||||
// State is lazy per bot. A corrupt legacy file can therefore fail only the
|
||||
// migrated bot and cannot prevent healthy v2 bots from starting.
|
||||
const stateStores = new Map();
|
||||
|
|
@ -75,9 +100,14 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|||
}
|
||||
return state;
|
||||
};
|
||||
const stateForBotId = async (botId) => {
|
||||
const botConfig = listConfiguredBots().find((bot) => bot.id === botId);
|
||||
if (!botConfig) throw new Error('Unknown Feishu bot');
|
||||
return stateFor(botConfig);
|
||||
};
|
||||
const harness = new Harness({
|
||||
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
|
||||
workspace: resolve(config.workspace ?? process.cwd()),
|
||||
workspace: defaultWorkspace,
|
||||
agentPreset: config.agentPreset ?? 'standard',
|
||||
// This plugin is already hosted by a running DSH process. Starting a
|
||||
// second DSH would create a competing server and lifecycle.
|
||||
|
|
@ -85,21 +115,24 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|||
dshBin: config.dshBin ?? 'dsh',
|
||||
});
|
||||
|
||||
const controller = new Controller({
|
||||
const coreController = new Controller({
|
||||
registerApp: (options) => lark.registerApp(options),
|
||||
verifyApp,
|
||||
credentials: ctx.credentials,
|
||||
configStore,
|
||||
configStore: observedConfigStore,
|
||||
createRuntime: async ({ botId, config: botConfig, appSecret }) => {
|
||||
const state = await stateFor(botConfig);
|
||||
const id = botId ?? botConfig.id ?? botConfig.appId;
|
||||
await workspaces.ensure(id);
|
||||
const workspaceScope = createBotWorkspaceScope(harness, { botId: id, workspaces, state });
|
||||
return new Runtime({
|
||||
lark,
|
||||
appId: botConfig.appId,
|
||||
appSecret,
|
||||
domain: botConfig.domain,
|
||||
ownerOpenIds: botConfig.ownerOpenIds ?? [botConfig.ownerOpenId],
|
||||
harness,
|
||||
state,
|
||||
harness: workspaceScope.harness,
|
||||
state: workspaceScope.state,
|
||||
replyTimeoutMs: config.replyTimeoutMs ?? 600_000,
|
||||
logger: {
|
||||
error: (...args) => logger.error?.(`[${botId ?? botConfig.id}]`, ...args),
|
||||
|
|
@ -118,6 +151,10 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|||
}
|
||||
},
|
||||
});
|
||||
const controller = createWorkspaceAwareController(coreController, {
|
||||
workspaces,
|
||||
stateFor: stateForBotId,
|
||||
});
|
||||
|
||||
const supervisor = createSupervisor({
|
||||
controller,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import QRCode from 'qrcode';
|
||||
import { resolveRpcAuthority } from '../../rpc-authority.mjs';
|
||||
import { publicWorkspaceError, validWorkspacePayload } from '../shared/workspace-rpc.mjs';
|
||||
import {
|
||||
FEISHU_ENDPOINTS,
|
||||
FEISHU_RPC_CHANNEL,
|
||||
|
|
@ -174,6 +175,7 @@ function publicBotEntry(entry) {
|
|||
bot: publicBot(source.bot),
|
||||
health: publicHealth(source, connected),
|
||||
};
|
||||
if (typeof source.workspace === 'string' && source.workspace) result.workspace = source.workspace;
|
||||
const error = publicError(source.error);
|
||||
if (error) result.error = error;
|
||||
return result;
|
||||
|
|
@ -268,6 +270,10 @@ function validPayload(endpoint, payload) {
|
|||
? null
|
||||
: 'Deleting a bot requires a valid botId and confirm=true.';
|
||||
}
|
||||
if (endpoint === FEISHU_ENDPOINTS.setWorkspace) {
|
||||
return validWorkspacePayload(payload)
|
||||
? null : '请输入工作区绝对路径。';
|
||||
}
|
||||
return 'Unknown Feishu endpoint.';
|
||||
}
|
||||
|
||||
|
|
@ -429,14 +435,23 @@ export function createFeishuRpcHandler(controller, { encodeQr = qrCodeDataUrl }
|
|||
} else if (endpoint === FEISHU_MULTI_ENDPOINTS.disconnectBot) {
|
||||
if (typeof controller.disconnectBot !== 'function') throw new Error('Multi-bot disconnect is unavailable');
|
||||
value = await toPublicFeishuStatus(await controller.disconnectBot(payload.botId), { encodeQr: cachedEncodeQr });
|
||||
} else if (endpoint === FEISHU_ENDPOINTS.setWorkspace) {
|
||||
if (typeof controller.updateWorkspace !== 'function') throw new Error('Workspace update is unavailable');
|
||||
value = await toPublicFeishuStatus(
|
||||
await controller.updateWorkspace(payload.botId, payload.workspace),
|
||||
{ encodeQr: cachedEncodeQr },
|
||||
);
|
||||
} else {
|
||||
if (typeof controller.deleteBot !== 'function') throw new Error('Multi-bot delete is unavailable');
|
||||
value = await toPublicFeishuStatus(await controller.deleteBot(payload.botId), { encodeQr: cachedEncodeQr });
|
||||
}
|
||||
if (signal?.aborted) return cancelled();
|
||||
return { ok: true, value };
|
||||
} catch {
|
||||
return signal?.aborted ? cancelled() : internalFailure();
|
||||
} catch (error) {
|
||||
const workspaceError = publicWorkspaceError(error);
|
||||
return signal?.aborted ? cancelled() : workspaceError
|
||||
? { ok: false, error: { ...workspaceError, details: {} } }
|
||||
: internalFailure();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue