mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-13 00:10:47 +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
|
|
@ -8,6 +8,12 @@ import { WeixinStateStore } from '../../../../src/channels/weixin/state-store.mj
|
|||
import { createWeixinApi } from '../../../../src/channels/weixin/weixin-api.mjs';
|
||||
import { WeixinController } from '../../../../src/channels/weixin/weixin-controller.mjs';
|
||||
import { WeixinRuntime } from '../../../../src/channels/weixin/weixin-runtime.mjs';
|
||||
import {
|
||||
BotWorkspaceStore,
|
||||
createBotWorkspaceScope,
|
||||
createWorkspaceAwareController,
|
||||
observeBotWorkspaceRemovals,
|
||||
} from '../../../../src/channels/shared/bot-workspace-store.mjs';
|
||||
import { createConnectionSupervisor } from './connection-supervisor.mjs';
|
||||
|
||||
function harnessOrigin(webServer, configured) {
|
||||
|
|
@ -26,6 +32,7 @@ function pluginPaths(config) {
|
|||
root,
|
||||
config: resolve(config.configPath ?? join(root, 'config.json')),
|
||||
accounts: resolve(config.accountsDir ?? join(root, 'accounts')),
|
||||
workspaces: resolve(config.workspacesPath ?? join(root, 'workspaces.json')),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -45,6 +52,16 @@ 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 configuredBots = configStore.list();
|
||||
await workspaces.reconcile(configuredBots.map((bot) => bot.botId));
|
||||
await Promise.all(configuredBots.map((bot) => workspaces.ensure(bot.botId)));
|
||||
const observedConfigStore = typeof configStore.remove === 'function'
|
||||
? observeBotWorkspaceRemovals(configStore, { workspaces })
|
||||
: configStore;
|
||||
const stateStores = new Map();
|
||||
|
||||
const statePath = (botId) => resolve(paths.accounts, botId, 'state.json');
|
||||
|
|
@ -58,24 +75,26 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|||
};
|
||||
const harness = new Harness({
|
||||
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
|
||||
workspace: resolve(config.workspace ?? process.cwd()),
|
||||
workspace: defaultWorkspace,
|
||||
agentPreset: config.agentPreset ?? 'standard',
|
||||
autostart: false,
|
||||
dshBin: config.dshBin ?? 'dsh',
|
||||
});
|
||||
const controller = new Controller({
|
||||
const coreController = new Controller({
|
||||
api,
|
||||
credentials: ctx.credentials,
|
||||
configStore,
|
||||
configStore: observedConfigStore,
|
||||
logger,
|
||||
createRuntime: async ({ botId, config: accountConfig, token }) => {
|
||||
const state = await stateFor(botId);
|
||||
await workspaces.ensure(botId);
|
||||
const workspaceScope = createBotWorkspaceScope(harness, { botId, workspaces, state });
|
||||
return new Runtime({
|
||||
api,
|
||||
config: accountConfig,
|
||||
token,
|
||||
harness,
|
||||
state,
|
||||
harness: workspaceScope.harness,
|
||||
state: workspaceScope.state,
|
||||
replyTimeoutMs: config.replyTimeoutMs ?? 600_000,
|
||||
maxMessageChars: config.maxMessageChars ?? 4_000,
|
||||
logger: {
|
||||
|
|
@ -91,15 +110,16 @@ export async function createProductionController(ctx, config = {}, internals = {
|
|||
stateStores.delete(botId);
|
||||
if (state && typeof state.remove === 'function') {
|
||||
await state.remove();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await unlink(statePath(botId));
|
||||
} catch (error) {
|
||||
if (error?.code !== 'ENOENT') throw error;
|
||||
} else {
|
||||
try {
|
||||
await unlink(statePath(botId));
|
||||
} catch (error) {
|
||||
if (error?.code !== 'ENOENT') throw error;
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
const controller = createWorkspaceAwareController(coreController, { workspaces, stateFor });
|
||||
const supervisor = createSupervisor({
|
||||
controller,
|
||||
harness,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,10 @@
|
|||
import QRCode from 'qrcode';
|
||||
import { resolveRpcAuthority } from '../../rpc-authority.mjs';
|
||||
import {
|
||||
publicWorkspaceError,
|
||||
SET_WORKSPACE_ENDPOINT,
|
||||
validWorkspacePayload,
|
||||
} from '../shared/workspace-rpc.mjs';
|
||||
|
||||
export const WEIXIN_RPC_CHANNEL = '/weixin';
|
||||
export const WEIXIN_ENDPOINTS = Object.freeze({
|
||||
|
|
@ -10,6 +15,7 @@ export const WEIXIN_ENDPOINTS = Object.freeze({
|
|||
cancelProvisioning: 'provision.cancel',
|
||||
reconnectBot: 'bot.reconnect',
|
||||
deleteBot: 'bot.delete',
|
||||
setWorkspace: SET_WORKSPACE_ENDPOINT,
|
||||
});
|
||||
export const WEIXIN_RPC_ENDPOINTS = Object.freeze(Object.values(WEIXIN_ENDPOINTS));
|
||||
|
||||
|
|
@ -58,6 +64,10 @@ function payloadFailure(endpoint, payload) {
|
|||
? null
|
||||
: 'bot.delete requires a botId and confirm=true.';
|
||||
}
|
||||
if (endpoint === WEIXIN_ENDPOINTS.setWorkspace) {
|
||||
return validWorkspacePayload(payload)
|
||||
? null : '请输入工作区绝对路径。';
|
||||
}
|
||||
return 'Unknown Weixin endpoint.';
|
||||
}
|
||||
|
||||
|
|
@ -156,12 +166,21 @@ export function createWeixinRpcHandler(controller, { encodeQr = qrDataUrl } = {}
|
|||
if (!value) return badRequest('The provisioning attempt no longer exists.');
|
||||
} else if (endpoint === WEIXIN_ENDPOINTS.reconnectBot) {
|
||||
value = await publicStatus(await controller.reconnectBot(payload.botId), cachedEncode);
|
||||
} else if (endpoint === WEIXIN_ENDPOINTS.setWorkspace) {
|
||||
if (typeof controller.updateWorkspace !== 'function') throw new Error('Workspace update is unavailable');
|
||||
value = await publicStatus(
|
||||
await controller.updateWorkspace(payload.botId, payload.workspace),
|
||||
cachedEncode,
|
||||
);
|
||||
} else {
|
||||
value = await publicStatus(await controller.deleteBot(payload.botId), cachedEncode);
|
||||
}
|
||||
return signal?.aborted ? cancelled() : { ok: true, value };
|
||||
} catch {
|
||||
return signal?.aborted ? cancelled() : internalFailure();
|
||||
} catch (error) {
|
||||
const workspaceError = publicWorkspaceError(error);
|
||||
return signal?.aborted ? cancelled() : workspaceError
|
||||
? { ok: false, error: workspaceError }
|
||||
: internalFailure();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue