All files / dingtalk-openclaw-connector/src/utils utils-legacy.ts

7.58% Statements 17/224
100% Branches 0/0
0% Functions 0/14
7.58% Lines 17/224

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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427                  1x     1x     1x 1x                                     1x                                                                                                                                                   1x                                 1x 1x                                                                                                                       1x                                                                                 1x     1x         1x                       1x               1x                           1x             1x               1x                                                                                                                                                                                                                                                                                          
/**
 * 钉钉插件工具函数
 */
 
import type { DingtalkConfig, ResolvedDingtalkAccount } from '../types/index.ts';
 
// ============ 常量 ============
 
/** 默认账号 ID,用于标记单账号模式(无 accounts 配置)时的内部标识 */
export const DEFAULT_ACCOUNT_ID = '__default__';
 
/** 新会话触发命令 */
export const NEW_SESSION_COMMANDS = ['/new', '/reset', '/clear', '新会话', '重新开始', '清空对话'];
 
/** 钉钉 API 常量 */
export const DINGTALK_API = 'https://api.dingtalk.com';
export const DINGTALK_OAPI = 'https://oapi.dingtalk.com';
 
// ============ 会话管理 ============
 
/** OpenClaw 标准会话上下文 */
export interface SessionContext {
  channel: 'dingtalk-connector';
  accountId: string;
  chatType: 'direct' | 'group';
  peerId: string;
  conversationId?: string;
  senderName?: string;
  groupSubject?: string;
}
 
/**
 * 构建 OpenClaw 标准会话上下文
 * 遵循 OpenClaw session.dmScope 机制,让 Gateway 根据配置自动处理会话隔离
 */
export function buildSessionContext(params: {
  accountId: string;
  senderId: string;
  senderName?: string;
  conversationType: string;
  conversationId?: string;
  groupSubject?: string;
  separateSessionByConversation?: boolean;
  groupSessionScope?: 'group' | 'group_sender';
}): SessionContext {
  const {
    accountId,
    senderId,
    senderName,
    conversationType,
    conversationId,
    groupSubject,
    separateSessionByConversation,
    groupSessionScope,
  } = params;
  const isDirect = conversationType === '1';
 
  // separateSessionByConversation=false 时,不区分单聊/群聊,按用户维度维护 session
  if (separateSessionByConversation === false) {
    return {
      channel: 'dingtalk-connector',
      accountId,
      chatType: isDirect ? 'direct' : 'group',
      peerId: senderId, // 只用 senderId,不区分会话
      senderName,
    };
  }
 
  // 以下是 separateSessionByConversation=true(默认)的逻辑
  if (isDirect) {
    // 单聊:peerId 为发送者 ID,由 OpenClaw Gateway 根据 dmScope 配置处理
    return {
      channel: 'dingtalk-connector',
      accountId,
      chatType: 'direct',
      peerId: senderId,
      senderName,
    };
  }
 
  // 群聊:根据 groupSessionScope 配置决定会话隔离策略
  if (groupSessionScope === 'group_sender') {
    // 群内每个用户独立会话
    return {
      channel: 'dingtalk-connector',
      accountId,
      chatType: 'group',
      peerId: `${conversationId}:${senderId}`,
      conversationId,
      senderName,
      groupSubject,
    };
  }
 
  // 默认:整个群共享一个会话
  return {
    channel: 'dingtalk-connector',
    accountId,
    chatType: 'group',
    peerId: conversationId || senderId,
    conversationId,
    senderName,
    groupSubject,
  };
}
 
/**
 * 检查消息是否是新会话命令
 */
export function normalizeSlashCommand(text: string): string {
  const trimmed = text.trim();
  const lower = trimmed.toLowerCase();
  if (NEW_SESSION_COMMANDS.some((cmd) => lower === cmd.toLowerCase())) {
    return '/new';
  }
  return text;
}
 
// ============ Access Token 缓存 ============
 
type CachedToken = {
  token: string;
  expiryMs: number;
};
 
// 注意:这里仍被部分新逻辑引用(如 message-handler),必须支持多账号,不能用全局单例缓存
const apiTokenCache = new Map<string, CachedToken>();
const oapiTokenCache = new Map<string, CachedToken>();
 
function cacheKey(config: DingtalkConfig): string {
  return String((config as any)?.clientId ?? '').trim();
}
 
/**
 * 获取钉钉 Access Token(新版 API)
 */
export async function getAccessToken(config: DingtalkConfig): Promise<string> {
  const now = Date.now();
  const key = cacheKey(config);
  const cached = apiTokenCache.get(key);
  if (cached && cached.expiryMs > now + 60_000) {
    return cached.token;
  }
 
  const axios = (await import('axios')).default;
  const response = await axios.post(`${DINGTALK_API}/v1.0/oauth2/accessToken`, {
    appKey: config.clientId,
    appSecret: config.clientSecret,
  });
 
  const token = response.data.accessToken as string;
  const expireInSec = Number(response.data.expireIn ?? 0);
  apiTokenCache.set(key, { token, expiryMs: now + expireInSec * 1000 });
  return token;
}
 
/**
 * 获取钉钉 OAPI Access Token(旧版 API,用于媒体上传等)
 */
export async function getOapiAccessToken(config: DingtalkConfig): Promise<string | null> {
  try {
    const now = Date.now();
    const key = cacheKey(config);
    const cached = oapiTokenCache.get(key);
    if (cached && cached.expiryMs > now + 60_000) {
      return cached.token;
    }
 
    const axios = (await import('axios')).default;
    const resp = await axios.get(`${DINGTALK_OAPI}/gettoken`, {
      params: { appkey: config.clientId, appsecret: config.clientSecret },
    });
    if (resp.data?.errcode === 0 && resp.data?.access_token) {
      const token = String(resp.data.access_token);
      const expiresInSec = Number(resp.data.expires_in ?? 7200);
      oapiTokenCache.set(key, { token, expiryMs: now + expiresInSec * 1000 });
      return token;
    }
    return null;
  } catch {
    return null;
  }
}
 
// ============ 用户 ID 转换 ============
 
/** staffId → unionId 缓存 */
const unionIdCache = new Map<string, string>();
 
/**
 * 通过 oapi 旧版接口将 staffId 转换为 unionId
 */
export async function getUnionId(
  staffId: string,
  config: DingtalkConfig,
  log?: any,
): Promise<string | null> {
  const cached = unionIdCache.get(staffId);
  if (cached) return cached;
 
  try {
    const token = await getOapiAccessToken(config);
    if (!token) {
      log?.error?.('[DingTalk] getUnionId: 无法获取 oapi access_token');
      return null;
    }
    const axios = (await import('axios')).default;
    const resp = await axios.get(`${DINGTALK_OAPI}/user/get`, {
      params: { access_token: token, userid: staffId },
      timeout: 10_000,
    });
    const unionId = resp.data?.unionid;
    if (unionId) {
      unionIdCache.set(staffId, unionId);
      log?.info?.(`[DingTalk] getUnionId: ${staffId} → ${unionId}`);
      return unionId;
    }
    log?.error?.(`[DingTalk] getUnionId: 响应中无 unionid 字段: ${JSON.stringify(resp.data)}`);
    return null;
  } catch (err: any) {
    log?.error?.(`[DingTalk] getUnionId 失败: ${err.message}`);
    return null;
  }
}
 
// ============ 消息去重 ============
 
/** 消息去重缓存 Map<messageId, timestamp> - 防止同一消息被重复处理 */
const processedMessages = new Map<string, number>();
 
/** 消息去重缓存过期时间(5分钟) */
const MESSAGE_DEDUP_TTL = 5 * 60 * 1000;
 
/**
 * 清理过期的消息去重缓存
 */
export function cleanupProcessedMessages(): void {
  const now = Date.now();
  for (const [msgId, timestamp] of processedMessages.entries()) {
    if (now - timestamp > MESSAGE_DEDUP_TTL) {
      processedMessages.delete(msgId);
    }
  }
}
 
/**
 * 检查消息是否已处理过(去重)
 */
export function isMessageProcessed(messageId: string): boolean {
  if (!messageId) return false;
  return processedMessages.has(messageId);
}
 
/**
 * 标记消息为已处理
 */
export function markMessageProcessed(messageId: string): void {
  if (!messageId) return;
  processedMessages.set(messageId, Date.now());
  // 定期清理(每处理100条消息清理一次)
  if (processedMessages.size >= 100) {
    cleanupProcessedMessages();
  }
}
 
// ============ 配置工具 ============
 
/**
 * 获取钉钉配置
 */
export function getDingtalkConfig(cfg: any): DingtalkConfig {
  return (cfg?.channels as any)?.['dingtalk-connector'] || {};
}
 
/**
 * 检查是否已配置
 */
export function isDingtalkConfigured(cfg: any): boolean {
  const config = getDingtalkConfig(cfg);
  return Boolean(config.clientId && config.clientSecret);
}
 
/**
 * 构建媒体系统提示词
 */
export function buildMediaSystemPrompt(): string {
  return `## 钉钉图片和文件显示规则
 
你正在钉钉中与用户对话。
 
### 一、图片显示
 
显示图片时,直接使用本地文件路径,系统会自动上传处理。
 
**正确方式**:
\`\`\`markdown
![描述](file:///path/to/image.jpg)
![描述](/tmp/screenshot.png)
![描述](/Users/xxx/photo.jpg)
\`\`\`
 
**禁止**:
- 不要自己执行 curl 上传
- 不要猜测或构造 URL
- **不要对路径进行转义(如使用反斜杠 \\ )**
 
直接输出本地路径即可,系统会自动上传到钉钉。
 
### 二、视频分享
 
**何时分享视频**:
- ✅ 用户明确要求**分享、发送、上传**视频时
- ❌ 仅生成视频保存到本地时,**不需要**分享
 
**视频标记格式**:
当需要分享视频时,在回复**末尾**添加:
 
\`\`\`
[DINGTALK_VIDEO]{"path":"<本地视频路径>"}[/DINGTALK_VIDEO]
\`\`\`
 
**支持格式**:mp4(最大 20MB)
 
**重要**:
- 视频大小不得超过 20MB,超过限制时告知用户
- 仅支持 mp4 格式
- 系统会自动提取视频时长、分辨率并生成封面
 
### 三、音频分享
 
**何时分享音频**:
- ✅ 用户明确要求**分享、发送、上传**音频/语音文件时
- ❌ 仅生成音频保存到本地时,**不需要**分享
 
**音频标记格式**:
当需要分享音频时,在回复**末尾**添加:
 
\`\`\`
[DINGTALK_AUDIO]{"path":"<本地音频路径>"}[/DINGTALK_AUDIO]
\`\`\`
 
**支持格式**:ogg、amr(最大 20MB)
 
**重要**:
- 音频大小不得超过 20MB,超过限制时告知用户
- 系统会自动提取音频时长
 
### 四、文件分享
 
**何时分享文件**:
- ✅ 用户明确要求**分享、发送、上传**文件时
- ❌ 仅生成文件保存到本地时,**不需要**分享
 
**文件标记格式**:
当需要分享文件时,在回复**末尾**添加:
 
\`\`\`
[DINGTALK_FILE]{"path":"<本地文件路径>","fileName":"<文件名>","fileType":"<扩展名>"}[/DINGTALK_FILE]
\`\`\`
 
**支持的文件类型**:几乎所有常见格式
 
**重要**:文件大小不得超过 20MB,超过限制时告知用户文件过大。`;
}
 
// ============ 消息表情回复 ============
 
/**
 * 在用户消息上贴 🤔思考中 表情,表示正在处理
 */
export async function addEmotionReply(config: DingtalkConfig, data: any, log?: any): Promise<void> {
  if (!data.msgId || !data.conversationId) return;
  try {
    const token = await getAccessToken(config);
    const axios = (await import('axios')).default;
    await axios.post(`${DINGTALK_API}/v1.0/robot/emotion/reply`, {
      robotCode: data.robotCode ?? config.clientId,
      openMsgId: data.msgId,
      openConversationId: data.conversationId,
      emotionType: 2,
      emotionName: '🤔思考中',
      textEmotion: {
        emotionId: '2659900',
        emotionName: '🤔思考中',
        text: '🤔思考中',
        backgroundId: 'im_bg_1',
      },
    }, {
      headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' },
      timeout: 5_000,
    });
    log?.info?.(`[DingTalk][Emotion] 贴表情成功: msgId=${data.msgId}`);
  } catch (err: any) {
    log?.warn?.(`[DingTalk][Emotion] 贴表情失败(不影响主流程): ${err.message}`);
  }
}
 
/**
 * 撤回用户消息上的 🤔思考中 表情
 */
export async function recallEmotionReply(config: DingtalkConfig, data: any, log?: any): Promise<void> {
  if (!data.msgId || !data.conversationId) return;
  try {
    const token = await getAccessToken(config);
    const axios = (await import('axios')).default;
    await axios.post(`${DINGTALK_API}/v1.0/robot/emotion/recall`, {
      robotCode: data.robotCode ?? config.clientId,
      openMsgId: data.msgId,
      openConversationId: data.conversationId,
      emotionType: 2,
      emotionName: '🤔思考中',
      textEmotion: {
        emotionId: '2659900',
        emotionName: '🤔思考中',
        text: '🤔思考中',
        backgroundId: 'im_bg_1',
      },
    }, {
      headers: { 'x-acs-dingtalk-access-token': token, 'Content-Type': 'application/json' },
      timeout: 5_000,
    });
    log?.info?.(`[DingTalk][Emotion] 撤回表情成功: msgId=${data.msgId}`);
  } catch (err: any) {
    log?.warn?.(`[DingTalk][Emotion] 撤回表情失败(不影响主流程): ${err.message}`);
  }
}