All files / dingtalk-openclaw-connector/src/core provider.ts

0% Statements 0/62
0% Branches 0/1
0% Functions 0/1
0% Lines 0/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                                                                                                                                                                   
/**
 * 钉钉消息流 Provider 入口
 *
 * 职责:
 * - 提供 monitorDingtalkProvider 函数作为钉钉消息流的统一入口
 * - 协调单账号和多账号监控场景
 * - 并行导入连接层和消息处理模块,避免循环依赖
 *
 * 主要功能:
 * - 根据 accountId 参数决定启动单账号或所有账号
 * - 验证账号配置状态
 * - 并行启动多个账号的消息流连接
 */
import type { ClawdbotConfig, RuntimeEnv } from "openclaw/plugin-sdk";
import * as monitorState from "./state";
 
// 只解构 monitorState 的导出
const {
  clearDingtalkWebhookRateLimitStateForTest,
  getDingtalkWebhookRateLimitStateSizeForTest,
  isWebhookRateLimitedForTest,
  stopDingtalkMonitorState,
} = monitorState;
 
export type MonitorDingtalkOpts = {
  config?: ClawdbotConfig;
  runtime?: RuntimeEnv;
  abortSignal?: AbortSignal;
  accountId?: string;
};
 
export {
  clearDingtalkWebhookRateLimitStateForTest,
  getDingtalkWebhookRateLimitStateSizeForTest,
  isWebhookRateLimitedForTest,
} from "./state";
 
// 只导出类型,不 re-export 函数(避免循环依赖)
export type { DingtalkReactionCreatedEvent } from "./core/connection.ts";
 
export async function monitorDingtalkProvider(opts: MonitorDingtalkOpts = {}): Promise<void> {
  const cfg = opts.config;
  if (!cfg) {
    throw new Error("Config is required for DingTalk monitor");
  }
 
  const log = opts.runtime?.log ?? console.log;
 
  // 并行导入所有模块(无循环依赖,可以并行)
  const [accountsModule, monitorAccountModule, monitorSingleModule] = await Promise.all([
    import("../config/accounts"),
    import("./message-handler"),
    import("./connection"),
  ]);
  
  const { resolveDingtalkAccount, listEnabledDingtalkAccounts } = accountsModule;
  const { handleDingTalkMessage } = monitorAccountModule;
  const { monitorSingleAccount, resolveReactionSyntheticEvent } = monitorSingleModule;
 
  if (opts.accountId) {
    const account = resolveDingtalkAccount({ cfg, accountId: opts.accountId });
    if (!account.enabled || !account.configured) {
      throw new Error(`DingTalk account "${opts.accountId}" not configured or disabled`);
    }
    return monitorSingleAccount({
      cfg,
      account,
      runtime: opts.runtime,
      abortSignal: opts.abortSignal,
      messageHandler: handleDingTalkMessage,
    });
  }
 
  const accounts = listEnabledDingtalkAccounts(cfg);
  if (accounts.length === 0) {
    throw new Error("No enabled DingTalk accounts configured");
  }
 
  log?.info?.(
    `dingtalk-connector: starting ${accounts.length} account(s): ${accounts.map((a) => a.accountId).join(", ")}`,
  );
 
  const monitorPromises: Promise<void>[] = [];
  for (const account of accounts) {
    if (opts.abortSignal?.aborted) {
      log("dingtalk-connector: abort signal received during startup preflight; stopping startup");
      break;
    }
 
    monitorPromises.push(
      monitorSingleAccount({
        cfg,
        account,
        runtime: opts.runtime,
        abortSignal: opts.abortSignal,
        messageHandler: handleDingTalkMessage,
      }),
    );
  }
 
  await Promise.all(monitorPromises);
}
 
export function stopDingtalkMonitor(accountId?: string): void {
  stopDingtalkMonitorState(accountId);
}