mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-11 06:33:17 +08:00
Internalize all IM channel implementations
This commit is contained in:
parent
8996476693
commit
bd469f58f8
95 changed files with 25012 additions and 100 deletions
130
plugin-src/host/channels/dingtalk/connection-supervisor.mjs
Normal file
130
plugin-src/host/channels/dingtalk/connection-supervisor.mjs
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
const DEFAULT_RETRY_DELAYS_MS = Object.freeze([250, 1_000, 3_000, 5_000, 10_000, 30_000]);
|
||||
|
||||
function retryDelays(value) {
|
||||
if (!Array.isArray(value) || value.length === 0) return [...DEFAULT_RETRY_DELAYS_MS];
|
||||
const valid = value.filter((delay) => Number.isFinite(delay) && delay >= 0);
|
||||
return valid.length > 0 ? valid : [...DEFAULT_RETRY_DELAYS_MS];
|
||||
}
|
||||
|
||||
export class ConnectionSupervisor {
|
||||
#controller;
|
||||
#harness;
|
||||
#logger;
|
||||
#retryDelays;
|
||||
#healthyIntervalMs;
|
||||
#setTimeout;
|
||||
#clearTimeout;
|
||||
#timer = null;
|
||||
#running = null;
|
||||
#retryIndex = 0;
|
||||
#closed = false;
|
||||
#started = false;
|
||||
#ready;
|
||||
#resolveReady;
|
||||
|
||||
constructor({
|
||||
controller,
|
||||
harness,
|
||||
logger = console,
|
||||
retryDelaysMs,
|
||||
healthyIntervalMs = 15_000,
|
||||
setTimeoutImpl = setTimeout,
|
||||
clearTimeoutImpl = clearTimeout,
|
||||
}) {
|
||||
if (!controller || typeof controller.initialize !== 'function' || typeof controller.status !== 'function') {
|
||||
throw new TypeError('ConnectionSupervisor requires a controller');
|
||||
}
|
||||
if (!harness || typeof harness.ensureRunning !== 'function') {
|
||||
throw new TypeError('ConnectionSupervisor requires a Harness client');
|
||||
}
|
||||
this.#controller = controller;
|
||||
this.#harness = harness;
|
||||
this.#logger = logger;
|
||||
this.#retryDelays = retryDelays(retryDelaysMs);
|
||||
this.#healthyIntervalMs = Number.isFinite(healthyIntervalMs) && healthyIntervalMs >= 0
|
||||
? healthyIntervalMs
|
||||
: 15_000;
|
||||
this.#setTimeout = setTimeoutImpl;
|
||||
this.#clearTimeout = clearTimeoutImpl;
|
||||
this.#ready = new Promise((resolve) => {
|
||||
this.#resolveReady = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
get ready() {
|
||||
return this.#ready;
|
||||
}
|
||||
|
||||
start() {
|
||||
if (this.#started || this.#closed) return this;
|
||||
this.#started = true;
|
||||
this.#schedule(0);
|
||||
return this;
|
||||
}
|
||||
|
||||
async close() {
|
||||
if (this.#closed) return;
|
||||
this.#closed = true;
|
||||
if (this.#timer !== null) this.#clearTimeout(this.#timer);
|
||||
this.#timer = null;
|
||||
await this.#running?.catch(() => undefined);
|
||||
this.#resolveReady?.(null);
|
||||
this.#resolveReady = null;
|
||||
}
|
||||
|
||||
#schedule(delayMs) {
|
||||
if (this.#closed) return;
|
||||
this.#timer = this.#setTimeout(() => {
|
||||
this.#timer = null;
|
||||
void this.#run();
|
||||
}, delayMs);
|
||||
this.#timer?.unref?.();
|
||||
}
|
||||
|
||||
async #run() {
|
||||
if (this.#closed || this.#running) return;
|
||||
const operation = this.#reconcile();
|
||||
this.#running = operation;
|
||||
try {
|
||||
await operation;
|
||||
} finally {
|
||||
if (this.#running === operation) this.#running = null;
|
||||
}
|
||||
}
|
||||
|
||||
async #reconcile() {
|
||||
try {
|
||||
await this.#harness.ensureRunning();
|
||||
if (this.#closed) return;
|
||||
const status = await this.#controller.initialize();
|
||||
if (this.#closed) return;
|
||||
this.#resolveReady?.(status);
|
||||
this.#resolveReady = null;
|
||||
const { configured, connected } = status.totals;
|
||||
if (connected < configured) {
|
||||
const delayMs = this.#retryDelays[Math.min(this.#retryIndex, this.#retryDelays.length - 1)];
|
||||
this.#retryIndex += 1;
|
||||
this.#logger.warn?.(
|
||||
`[dsh-dingtalk] ${connected}/${configured} bots connected; retrying in ${delayMs}ms`,
|
||||
);
|
||||
this.#schedule(delayMs);
|
||||
return;
|
||||
}
|
||||
this.#retryIndex = 0;
|
||||
this.#schedule(this.#healthyIntervalMs);
|
||||
} catch (error) {
|
||||
if (this.#closed) return;
|
||||
const delayMs = this.#retryDelays[Math.min(this.#retryIndex, this.#retryDelays.length - 1)];
|
||||
this.#retryIndex += 1;
|
||||
this.#logger.warn?.(
|
||||
`[dsh-dingtalk] connection reconciliation failed; retrying in ${delayMs}ms`,
|
||||
error,
|
||||
);
|
||||
this.#schedule(delayMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createConnectionSupervisor(options) {
|
||||
return new ConnectionSupervisor(options);
|
||||
}
|
||||
32
plugin-src/host/channels/dingtalk/index.mjs
Normal file
32
plugin-src/host/channels/dingtalk/index.mjs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { createProductionController } from './production.mjs';
|
||||
import { installDingtalkRpc } from './rpc.mjs';
|
||||
|
||||
export const name = 'dsh-dingtalk-host';
|
||||
export const inject = ['connection', 'credentials', 'webServer'];
|
||||
|
||||
export async function apply(ctx, config = {}) {
|
||||
if (config?.controller) return installDingtalkRpc(ctx, config.controller, config.rpcOptions);
|
||||
|
||||
const production = await createProductionController(ctx, config, config.internals);
|
||||
const disposeRpc = installDingtalkRpc(ctx, production.controller, config.rpcOptions);
|
||||
ctx.effect(() => async () => {
|
||||
await production.close();
|
||||
}, 'dsh-dingtalk: close bot connections');
|
||||
return disposeRpc;
|
||||
}
|
||||
|
||||
export function createDingtalkHostPlugin(config) {
|
||||
return Object.freeze({ name, inject, apply: (ctx) => apply(ctx, config) });
|
||||
}
|
||||
|
||||
export { createConnectionSupervisor, ConnectionSupervisor } from './connection-supervisor.mjs';
|
||||
export { createProductionController } from './production.mjs';
|
||||
export {
|
||||
DINGTALK_ENDPOINTS,
|
||||
DINGTALK_RPC_CHANNEL,
|
||||
DINGTALK_RPC_ENDPOINTS,
|
||||
createDingtalkRpcHandler,
|
||||
installDingtalkRpc,
|
||||
} from './rpc.mjs';
|
||||
export { DingtalkController } from '../../../../src/channels/dingtalk/dingtalk-controller.mjs';
|
||||
export { DingtalkRuntime } from '../../../../src/channels/dingtalk/dingtalk-runtime.mjs';
|
||||
122
plugin-src/host/channels/dingtalk/production.mjs
Normal file
122
plugin-src/host/channels/dingtalk/production.mjs
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
import { unlink } from 'node:fs/promises';
|
||||
import { homedir } from 'node:os';
|
||||
import { join, resolve } from 'node:path';
|
||||
|
||||
import { DingtalkConfigStore } from '../../../../src/channels/dingtalk/config-store.mjs';
|
||||
import { DingtalkDeviceAuth } from '../../../../src/channels/dingtalk/device-auth.mjs';
|
||||
import { DingtalkController } from '../../../../src/channels/dingtalk/dingtalk-controller.mjs';
|
||||
import { DingtalkRuntime } from '../../../../src/channels/dingtalk/dingtalk-runtime.mjs';
|
||||
import { HarnessClient } from '../../../../src/channels/dingtalk/harness-client.mjs';
|
||||
import { DingtalkStateStore } from '../../../../src/channels/dingtalk/state-store.mjs';
|
||||
import { createConnectionSupervisor } from './connection-supervisor.mjs';
|
||||
|
||||
function harnessOrigin(webServer, configured) {
|
||||
if (configured !== undefined) return new URL(configured);
|
||||
const port = webServer?.port;
|
||||
if (!Number.isInteger(port) || port < 1 || port > 65_535) {
|
||||
throw new Error('dsh-dingtalk requires an initialized DSH webServer port');
|
||||
}
|
||||
return new URL(`http://127.0.0.1:${port}`);
|
||||
}
|
||||
|
||||
function pluginPaths(config) {
|
||||
const dshHome = resolve(config.dshHome ?? process.env.DSH_HOME ?? join(homedir(), '.dsh'));
|
||||
const root = resolve(config.dataDir ?? join(dshHome, 'integrations', 'dsh-dingtalk'));
|
||||
return {
|
||||
root,
|
||||
config: resolve(config.configPath ?? join(root, 'config.json')),
|
||||
bots: resolve(config.botsDir ?? join(root, 'bots')),
|
||||
};
|
||||
}
|
||||
|
||||
export async function createProductionController(ctx, config = {}, internals = {}) {
|
||||
if (!ctx?.credentials) throw new TypeError('dsh-dingtalk requires ctx.credentials');
|
||||
if (!ctx?.webServer) throw new TypeError('dsh-dingtalk requires ctx.webServer');
|
||||
|
||||
const ConfigStore = internals.ConfigStore ?? DingtalkConfigStore;
|
||||
const DeviceAuth = internals.DeviceAuth ?? DingtalkDeviceAuth;
|
||||
const StateStore = internals.StateStore ?? DingtalkStateStore;
|
||||
const Harness = internals.HarnessClient ?? HarnessClient;
|
||||
const Controller = internals.Controller ?? DingtalkController;
|
||||
const Runtime = internals.Runtime ?? DingtalkRuntime;
|
||||
const createSupervisor = internals.createConnectionSupervisor ?? createConnectionSupervisor;
|
||||
const logger = typeof ctx.logger === 'function'
|
||||
? ctx.logger('dsh-dingtalk')
|
||||
: (ctx.logger ?? console);
|
||||
const paths = pluginPaths(config);
|
||||
const configStore = await new ConfigStore(paths.config).load();
|
||||
const deviceAuth = internals.deviceAuth ?? new DeviceAuth({
|
||||
baseUrl: config.registrationBaseUrl,
|
||||
});
|
||||
const stateStores = new Map();
|
||||
|
||||
const statePath = (botId) => resolve(paths.bots, botId, 'state.json');
|
||||
const stateFor = async (botId) => {
|
||||
let state = stateStores.get(botId);
|
||||
if (!state) {
|
||||
state = await new StateStore(statePath(botId)).load();
|
||||
stateStores.set(botId, state);
|
||||
}
|
||||
return state;
|
||||
};
|
||||
const harness = new Harness({
|
||||
baseUrl: harnessOrigin(ctx.webServer, config.harnessBaseUrl),
|
||||
workspace: resolve(config.workspace ?? process.cwd()),
|
||||
agentPreset: config.agentPreset ?? 'standard',
|
||||
autostart: false,
|
||||
dshBin: config.dshBin ?? 'dsh',
|
||||
});
|
||||
const controller = new Controller({
|
||||
deviceAuth,
|
||||
credentials: ctx.credentials,
|
||||
configStore,
|
||||
logger,
|
||||
createRuntime: async ({ botId, config: botConfig, clientSecret }) => {
|
||||
const state = await stateFor(botId);
|
||||
return new Runtime({
|
||||
config: botConfig,
|
||||
clientSecret,
|
||||
harness,
|
||||
state,
|
||||
replyTimeoutMs: config.replyTimeoutMs ?? 600_000,
|
||||
maxMessageChars: config.maxMessageChars ?? 4_000,
|
||||
connectTimeoutMs: config.connectTimeoutMs ?? 15_000,
|
||||
logger: {
|
||||
error: (...args) => logger.error?.(`[${botId}]`, ...args),
|
||||
warn: (...args) => logger.warn?.(`[${botId}]`, ...args),
|
||||
info: (...args) => logger.info?.(`[${botId}]`, ...args),
|
||||
debug: (...args) => logger.debug?.(`[${botId}]`, ...args),
|
||||
},
|
||||
});
|
||||
},
|
||||
deleteState: async ({ botId }) => {
|
||||
const state = stateStores.get(botId);
|
||||
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;
|
||||
}
|
||||
},
|
||||
});
|
||||
const supervisor = createSupervisor({
|
||||
controller,
|
||||
harness,
|
||||
logger,
|
||||
retryDelaysMs: config.retryDelaysMs,
|
||||
healthyIntervalMs: config.healthyIntervalMs,
|
||||
}).start();
|
||||
return {
|
||||
controller,
|
||||
ready: supervisor.ready,
|
||||
async close() {
|
||||
await supervisor.close();
|
||||
await controller.close();
|
||||
harness.stopManagedProcess();
|
||||
},
|
||||
};
|
||||
}
|
||||
221
plugin-src/host/channels/dingtalk/rpc.mjs
Normal file
221
plugin-src/host/channels/dingtalk/rpc.mjs
Normal file
|
|
@ -0,0 +1,221 @@
|
|||
import QRCode from 'qrcode';
|
||||
|
||||
export const DINGTALK_RPC_CHANNEL = '/dingtalk';
|
||||
export const DINGTALK_ENDPOINTS = Object.freeze({
|
||||
status: 'connection.status',
|
||||
beginProvisioning: 'provision.begin',
|
||||
pollProvisioning: 'provision.poll',
|
||||
cancelProvisioning: 'provision.cancel',
|
||||
reconnectBot: 'bot.reconnect',
|
||||
deleteBot: 'bot.delete',
|
||||
approveSender: 'bot.sender.approve',
|
||||
revokeSender: 'bot.sender.revoke',
|
||||
});
|
||||
export const DINGTALK_RPC_ENDPOINTS = Object.freeze(Object.values(DINGTALK_ENDPOINTS));
|
||||
|
||||
const FORBIDDEN_PUBLIC_KEYS = new Set([
|
||||
'clientSecret',
|
||||
'client_secret',
|
||||
'deviceCode',
|
||||
'device_code',
|
||||
'secretRef',
|
||||
'staffId',
|
||||
'senderStaffId',
|
||||
'verificationUrl',
|
||||
'verificationUri',
|
||||
'userCode',
|
||||
]);
|
||||
|
||||
function isRecord(value) {
|
||||
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function exactKeys(value, allowed) {
|
||||
return isRecord(value) && Object.keys(value).every((key) => allowed.includes(key));
|
||||
}
|
||||
|
||||
function validId(value) {
|
||||
return typeof value === 'string' && /^[A-Za-z0-9_-]{1,128}$/.test(value);
|
||||
}
|
||||
|
||||
function payloadFailure(endpoint, payload) {
|
||||
if (!isRecord(payload)) return 'Payload must be an object.';
|
||||
if (endpoint === DINGTALK_ENDPOINTS.status) {
|
||||
return exactKeys(payload, []) ? null : 'connection.status does not accept fields.';
|
||||
}
|
||||
if (endpoint === DINGTALK_ENDPOINTS.beginProvisioning) {
|
||||
return exactKeys(payload, ['locale']) && (payload.locale === undefined || payload.locale === 'zh-CN')
|
||||
? null
|
||||
: 'provision.begin received unsupported fields.';
|
||||
}
|
||||
if ([DINGTALK_ENDPOINTS.pollProvisioning, DINGTALK_ENDPOINTS.cancelProvisioning].includes(endpoint)) {
|
||||
return exactKeys(payload, ['attemptId']) && validId(payload.attemptId)
|
||||
? null
|
||||
: `${endpoint} requires an attemptId.`;
|
||||
}
|
||||
if (endpoint === DINGTALK_ENDPOINTS.reconnectBot) {
|
||||
return exactKeys(payload, ['botId']) && validId(payload.botId)
|
||||
? null
|
||||
: 'bot.reconnect requires a botId.';
|
||||
}
|
||||
if (endpoint === DINGTALK_ENDPOINTS.deleteBot) {
|
||||
return exactKeys(payload, ['botId', 'confirm']) && validId(payload.botId) && payload.confirm === true
|
||||
? null
|
||||
: 'bot.delete requires a botId and confirm=true.';
|
||||
}
|
||||
if (endpoint === DINGTALK_ENDPOINTS.approveSender) {
|
||||
return exactKeys(payload, ['botId', 'requestId', 'confirm'])
|
||||
&& validId(payload.botId)
|
||||
&& validId(payload.requestId)
|
||||
&& payload.confirm === true
|
||||
? null
|
||||
: 'bot.sender.approve requires botId, requestId, and confirm=true.';
|
||||
}
|
||||
if (endpoint === DINGTALK_ENDPOINTS.revokeSender) {
|
||||
return exactKeys(payload, ['botId', 'senderKey', 'confirm'])
|
||||
&& validId(payload.botId)
|
||||
&& validId(payload.senderKey)
|
||||
&& payload.confirm === true
|
||||
? null
|
||||
: 'bot.sender.revoke requires botId, senderKey, and confirm=true.';
|
||||
}
|
||||
return 'Unknown DingTalk endpoint.';
|
||||
}
|
||||
|
||||
function badRequest(message) {
|
||||
return { ok: false, error: { code: 'bad-request', message } };
|
||||
}
|
||||
|
||||
function cancelled() {
|
||||
return { ok: false, error: { code: 'cancelled', message: 'The request was cancelled.' } };
|
||||
}
|
||||
|
||||
function internalFailure() {
|
||||
return {
|
||||
ok: false,
|
||||
error: { code: 'dingtalk-operation-failed', message: '钉钉操作失败,请稍后重试。' },
|
||||
};
|
||||
}
|
||||
|
||||
function sanitizePublic(value) {
|
||||
if (Array.isArray(value)) return value.map(sanitizePublic);
|
||||
if (!isRecord(value)) return value;
|
||||
const safe = {};
|
||||
for (const [key, child] of Object.entries(value)) {
|
||||
if (!FORBIDDEN_PUBLIC_KEYS.has(key)) safe[key] = sanitizePublic(child);
|
||||
}
|
||||
return safe;
|
||||
}
|
||||
|
||||
async function qrDataUrl(value) {
|
||||
return QRCode.toDataURL(value, {
|
||||
type: 'image/png',
|
||||
errorCorrectionLevel: 'M',
|
||||
margin: 2,
|
||||
width: 320,
|
||||
});
|
||||
}
|
||||
|
||||
async function withEncodedQr(value, encodeQr) {
|
||||
if (!value || typeof value.verificationUrl !== 'string') return sanitizePublic(value);
|
||||
return sanitizePublic({
|
||||
...value,
|
||||
qrCodeDataUrl: await encodeQr(value.verificationUrl),
|
||||
});
|
||||
}
|
||||
|
||||
async function publicStatus(status, encodeQr) {
|
||||
const value = structuredClone(status);
|
||||
if (value?.provisioning) {
|
||||
value.provisioning = await withEncodedQr(value.provisioning, encodeQr);
|
||||
}
|
||||
return sanitizePublic(value);
|
||||
}
|
||||
|
||||
function assertController(controller) {
|
||||
for (const method of [
|
||||
'status',
|
||||
'startProvisioning',
|
||||
'registrationStatus',
|
||||
'cancelProvisioning',
|
||||
'reconnectBot',
|
||||
'deleteBot',
|
||||
'approveSender',
|
||||
'revokeSender',
|
||||
]) {
|
||||
if (typeof controller?.[method] !== 'function') {
|
||||
throw new TypeError(`A complete DingTalk controller is required (${method})`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createDingtalkRpcHandler(controller, { encodeQr = qrDataUrl } = {}) {
|
||||
assertController(controller);
|
||||
const qrCache = new Map();
|
||||
const cachedEncode = (url) => {
|
||||
let encoded = qrCache.get(url);
|
||||
if (!encoded) {
|
||||
if (qrCache.size >= 16) qrCache.delete(qrCache.keys().next().value);
|
||||
encoded = Promise.resolve().then(() => encodeQr(url));
|
||||
qrCache.set(url, encoded);
|
||||
}
|
||||
return encoded;
|
||||
};
|
||||
|
||||
return async (endpoint, payload, signal) => {
|
||||
if (signal?.aborted) return cancelled();
|
||||
if (!DINGTALK_RPC_ENDPOINTS.includes(endpoint)) return badRequest('Unknown DingTalk endpoint.');
|
||||
const invalid = payloadFailure(endpoint, payload);
|
||||
if (invalid) return badRequest(invalid);
|
||||
|
||||
try {
|
||||
let value;
|
||||
if (endpoint === DINGTALK_ENDPOINTS.status) {
|
||||
value = await publicStatus(await controller.status(), cachedEncode);
|
||||
} else if (endpoint === DINGTALK_ENDPOINTS.beginProvisioning) {
|
||||
const started = await controller.startProvisioning({ signal });
|
||||
if (signal?.aborted) {
|
||||
await controller.cancelProvisioning(started.attemptId);
|
||||
return cancelled();
|
||||
}
|
||||
value = await withEncodedQr(started, cachedEncode);
|
||||
} else if (endpoint === DINGTALK_ENDPOINTS.pollProvisioning) {
|
||||
const current = await controller.registrationStatus(payload.attemptId);
|
||||
if (!current) return badRequest('The provisioning attempt no longer exists.');
|
||||
value = await withEncodedQr(current, cachedEncode);
|
||||
} else if (endpoint === DINGTALK_ENDPOINTS.cancelProvisioning) {
|
||||
value = await controller.cancelProvisioning(payload.attemptId);
|
||||
if (!value) return badRequest('The provisioning attempt no longer exists.');
|
||||
value = sanitizePublic(value);
|
||||
} else if (endpoint === DINGTALK_ENDPOINTS.reconnectBot) {
|
||||
value = await publicStatus(await controller.reconnectBot(payload.botId), cachedEncode);
|
||||
} else if (endpoint === DINGTALK_ENDPOINTS.deleteBot) {
|
||||
value = await publicStatus(await controller.deleteBot(payload.botId), cachedEncode);
|
||||
} else if (endpoint === DINGTALK_ENDPOINTS.approveSender) {
|
||||
value = await publicStatus(
|
||||
await controller.approveSender(payload.botId, payload.requestId),
|
||||
cachedEncode,
|
||||
);
|
||||
} else {
|
||||
value = await publicStatus(
|
||||
await controller.revokeSender(payload.botId, payload.senderKey),
|
||||
cachedEncode,
|
||||
);
|
||||
}
|
||||
return signal?.aborted ? cancelled() : { ok: true, value };
|
||||
} catch {
|
||||
return signal?.aborted ? cancelled() : internalFailure();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function installDingtalkRpc(ctx, controller, options) {
|
||||
if (!ctx?.connection?.rpc || typeof ctx.connection.rpc.handle !== 'function') {
|
||||
throw new TypeError('DSH Host Connection RPC is required');
|
||||
}
|
||||
return ctx.connection.rpc.handle(
|
||||
DINGTALK_RPC_CHANNEL,
|
||||
createDingtalkRpcHandler(controller, options),
|
||||
{ authority: 'loopback' },
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue