@wecom/wecom-openclaw-plugin 2026.3.25-beta.2 → 2026.3.25-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1193,24 +1193,31 @@ const MODE_ALIASES = {
1193
1193
  };
1194
1194
  /**
1195
1195
  * 修正 checkbox.mode:
1196
- * - 整数 → 原样
1196
+ * - 0 → 单选,1 → 多选,仅允许这两个值
1197
1197
  * - 字符串数字 "0"/"1" → 整数
1198
1198
  * - 语义别名 "multi"/"single" 等 → 对应整数
1199
+ * - 其他正整数(如 2)→ clamp 到 1(多选)
1199
1200
  * - 无法识别 → 删除(让服务端使用默认值 0)
1200
1201
  */
1201
1202
  function coerceCheckboxMode(value) {
1203
+ let num;
1202
1204
  if (typeof value === "number" && Number.isFinite(value)) {
1203
- return Math.round(value);
1205
+ num = Math.round(value);
1204
1206
  }
1205
- if (typeof value === "string") {
1207
+ else if (typeof value === "string") {
1206
1208
  const trimmed = value.trim().toLowerCase();
1207
1209
  if (trimmed in MODE_ALIASES)
1208
1210
  return MODE_ALIASES[trimmed];
1209
- const num = Number(trimmed);
1210
- if (!Number.isNaN(num))
1211
- return Math.round(num);
1211
+ const parsed = Number(trimmed);
1212
+ if (!Number.isNaN(parsed))
1213
+ num = Math.round(parsed);
1212
1214
  }
1213
- return undefined;
1215
+ if (num === undefined)
1216
+ return undefined;
1217
+ // mode 只允许 0(单选)或 1(多选),超出范围 clamp
1218
+ if (num <= 0)
1219
+ return 0;
1220
+ return 1;
1214
1221
  }
1215
1222
  /**
1216
1223
  * 对 LLM 生成的模板卡片 JSON 做字段类型修正,确保符合企业微信 API 的类型要求。
@@ -1369,6 +1376,273 @@ function normalizeTemplateCardFields(card, log) {
1369
1376
  }
1370
1377
  return card;
1371
1378
  }
1379
+ /**
1380
+ * 校验并补全模板卡片的必填字段。
1381
+ *
1382
+ * 在 normalizeTemplateCardFields(类型修正)之后调用,确保卡片结构满足企业微信 API 的必填要求。
1383
+ *
1384
+ * 补全策略:
1385
+ * - task_id:所有卡片统一自动补全(交互型 API 必填,通知型插件也需要用于缓存回写)
1386
+ * - main_title:除 text_notice 外的 4 种卡片 API 必填,自动补 { title: "通知" }
1387
+ * text_notice 要求 main_title.title 与 sub_title_text 至少填一个,缺两个时补 sub_title_text
1388
+ * - card_action:text_notice / news_notice API 必填,自动补 { type: 1, url: "https://work.weixin.qq.com" }
1389
+ * - checkbox:vote_interaction API 必填,无法凭空补全,仅记告警
1390
+ * - submit_button:vote_interaction / multiple_interaction API 必填,自动补 { text: "提交", key: "submit_default" }
1391
+ * - button_list:button_interaction API 必填,无法凭空补全,仅记告警
1392
+ * - select_list:multiple_interaction API 必填,无法凭空补全,仅记告警
1393
+ */
1394
+ function validateAndFixRequiredFields(card, log) {
1395
+ const cardType = card.card_type;
1396
+ const fixes = [];
1397
+ const warnings = [];
1398
+ // ── task_id(所有卡片:始终确保唯一性) ─────────────────────────────
1399
+ // LLM 可能编造时间戳导致重复,因此无论是否提供了 task_id,
1400
+ // 都提取语义前缀 + 代码追加真实时间戳和随机后缀来保证唯一。
1401
+ const rawTid = (typeof card.task_id === "string" && card.task_id.trim()) ? card.task_id.trim() : "";
1402
+ const rand = Math.random().toString(36).slice(2, 6);
1403
+ const ts = Date.now();
1404
+ let finalTid;
1405
+ if (rawTid) {
1406
+ // 提取 LLM 的语义前缀:去掉尾部的数字串(LLM 编造的假时间戳)
1407
+ const prefix = rawTid.replace(/_\d{8,}$/, "").replace(/[^a-zA-Z0-9_\-@]/g, "_").slice(0, 80);
1408
+ finalTid = prefix ? `${prefix}_${ts}_${rand}` : `task_${cardType}_${ts}_${rand}`;
1409
+ }
1410
+ else {
1411
+ finalTid = `task_${cardType}_${ts}_${rand}`;
1412
+ }
1413
+ if (finalTid !== rawTid) {
1414
+ fixes.push(`task_id: "${rawTid || "(missing)"}" → "${finalTid}"`);
1415
+ }
1416
+ card.task_id = finalTid;
1417
+ // ── main_title ────────────────────────────────────────────────────────
1418
+ const mainTitle = card.main_title;
1419
+ const hasMainTitle = mainTitle && typeof mainTitle === "object" &&
1420
+ (typeof mainTitle.title === "string" && mainTitle.title.trim());
1421
+ const hasSubTitleText = typeof card.sub_title_text === "string" && card.sub_title_text.trim();
1422
+ switch (cardType) {
1423
+ case "text_notice":
1424
+ // text_notice: main_title.title 和 sub_title_text 至少一个
1425
+ if (!hasMainTitle && !hasSubTitleText) {
1426
+ card.sub_title_text = card.sub_title_text || "通知";
1427
+ fixes.push(`sub_title_text: (missing, no main_title either) → fallback "通知"`);
1428
+ }
1429
+ break;
1430
+ case "news_notice":
1431
+ case "button_interaction":
1432
+ case "vote_interaction":
1433
+ case "multiple_interaction":
1434
+ // 这四种 main_title 为必填
1435
+ if (!mainTitle || typeof mainTitle !== "object") {
1436
+ card.main_title = { title: "通知" };
1437
+ fixes.push(`main_title: (missing) → { title: "通知" }`);
1438
+ }
1439
+ else if (!hasMainTitle) {
1440
+ mainTitle.title = "通知";
1441
+ fixes.push(`main_title.title: (empty) → "通知"`);
1442
+ }
1443
+ break;
1444
+ }
1445
+ // ── card_action(text_notice / news_notice 必填) ──────────────────
1446
+ if (cardType === "text_notice" || cardType === "news_notice") {
1447
+ if (!card.card_action || typeof card.card_action !== "object") {
1448
+ card.card_action = { type: 1, url: "https://work.weixin.qq.com" };
1449
+ fixes.push(`card_action: (missing) → { type: 1, url: "https://work.weixin.qq.com" }`);
1450
+ }
1451
+ }
1452
+ // ── submit_button(vote_interaction / multiple_interaction 必填) ──
1453
+ if (cardType === "vote_interaction" || cardType === "multiple_interaction") {
1454
+ if (!card.submit_button || typeof card.submit_button !== "object") {
1455
+ card.submit_button = { text: "提交", key: `submit_${cardType}_${Date.now()}` };
1456
+ fixes.push(`submit_button: (missing) → auto-generated`);
1457
+ }
1458
+ }
1459
+ // ── 核心业务字段(无法凭空补全,仅告警) ────────────────────────────
1460
+ if (cardType === "button_interaction") {
1461
+ if (!Array.isArray(card.button_list) || card.button_list.length === 0) {
1462
+ warnings.push(`button_list is missing or empty (required for button_interaction)`);
1463
+ }
1464
+ }
1465
+ if (cardType === "vote_interaction") {
1466
+ if (!card.checkbox || typeof card.checkbox !== "object") {
1467
+ warnings.push(`checkbox is missing (required for vote_interaction)`);
1468
+ }
1469
+ }
1470
+ if (cardType === "multiple_interaction") {
1471
+ if (!Array.isArray(card.select_list) || card.select_list.length === 0) {
1472
+ warnings.push(`select_list is missing or empty (required for multiple_interaction)`);
1473
+ }
1474
+ }
1475
+ if (fixes.length > 0) {
1476
+ log?.(`[template-card-parser] validateAndFixRequiredFields: ${fixes.length} fix(es): ${fixes.join("; ")}`);
1477
+ }
1478
+ if (warnings.length > 0) {
1479
+ log?.(`[template-card-parser] validateAndFixRequiredFields: ${warnings.length} warning(s): ${warnings.join("; ")}`);
1480
+ }
1481
+ return card;
1482
+ }
1483
+ // ============================================================================
1484
+ // 简化格式 → 企微 API 格式转换(vote_interaction / multiple_interaction)
1485
+ // ============================================================================
1486
+ /**
1487
+ * 生成唯一的 question_key / submit_button.key。
1488
+ */
1489
+ function generateKey(prefix) {
1490
+ const rand = Math.random().toString(36).slice(2, 6);
1491
+ return `${prefix}_${Date.now()}_${rand}`;
1492
+ }
1493
+ /**
1494
+ * 将 vote_interaction 的简化格式转换为企微 API 格式。
1495
+ *
1496
+ * 简化格式字段:
1497
+ * title → main_title.title
1498
+ * description → main_title.desc
1499
+ * options → checkbox.option_list(每个 {id, text} 直接透传)
1500
+ * mode → checkbox.mode(0=单选 1=多选)
1501
+ * submit_text → submit_button.text
1502
+ *
1503
+ * 代码自动生成:checkbox.question_key, submit_button.key
1504
+ *
1505
+ * 如果 LLM 已输出了合法的 API 原始格式(有 checkbox.option_list),则跳过转换直接透传。
1506
+ */
1507
+ function transformVoteInteraction(card, log) {
1508
+ // 防御性:如果已经是合法 API 格式,跳过
1509
+ const existingCheckbox = card.checkbox;
1510
+ if (existingCheckbox && typeof existingCheckbox === "object" && Array.isArray(existingCheckbox.option_list)) {
1511
+ log?.(`[template-card-parser] transformVoteInteraction: already has checkbox.option_list, skipping transform`);
1512
+ return card;
1513
+ }
1514
+ // 提取 options(简化格式的核心字段)
1515
+ const options = card.options;
1516
+ if (!Array.isArray(options) || options.length === 0) {
1517
+ log?.(`[template-card-parser] transformVoteInteraction: no "options" array found, skipping transform`);
1518
+ return card;
1519
+ }
1520
+ log?.(`[template-card-parser] transformVoteInteraction: transforming simplified format → API format`);
1521
+ log?.(`[template-card-parser] transformVoteInteraction: input=${JSON.stringify(card)}`);
1522
+ // ── 构建 main_title ──
1523
+ const title = card.title;
1524
+ const description = card.description;
1525
+ if (title || description) {
1526
+ card.main_title = {
1527
+ ...(title ? { title } : {}),
1528
+ ...(description ? { desc: description } : {}),
1529
+ };
1530
+ delete card.title;
1531
+ delete card.description;
1532
+ }
1533
+ // ── 构建 checkbox(最多 20 个选项) ──
1534
+ const mode = coerceCheckboxMode(card.mode) ?? 0;
1535
+ const questionKey = generateKey("vote");
1536
+ const clampedOptions = options.slice(0, 20);
1537
+ if (options.length > 20) {
1538
+ log?.(`[template-card-parser] transformVoteInteraction: options count ${options.length} exceeds max 20, clamped to 20`);
1539
+ }
1540
+ card.checkbox = {
1541
+ question_key: questionKey,
1542
+ mode,
1543
+ option_list: clampedOptions.map((opt) => ({
1544
+ id: String(opt.id ?? opt.value ?? `opt_${Math.random().toString(36).slice(2, 6)}`),
1545
+ text: String(opt.text ?? opt.label ?? opt.name ?? ""),
1546
+ })),
1547
+ };
1548
+ delete card.options;
1549
+ delete card.mode;
1550
+ // ── 构建 submit_button ──
1551
+ const submitText = card.submit_text || "提交";
1552
+ card.submit_button = {
1553
+ text: submitText,
1554
+ key: generateKey("submit_vote"),
1555
+ };
1556
+ delete card.submit_text;
1557
+ // ── 清理 LLM 可能杜撰的无效字段 ──
1558
+ delete card.vote_question;
1559
+ delete card.vote_option;
1560
+ delete card.vote_options;
1561
+ log?.(`[template-card-parser] transformVoteInteraction: output=${JSON.stringify(card)}`);
1562
+ return card;
1563
+ }
1564
+ /**
1565
+ * 将 multiple_interaction 的简化格式转换为企微 API 格式。
1566
+ *
1567
+ * 简化格式字段:
1568
+ * title → main_title.title
1569
+ * description → main_title.desc
1570
+ * selectors → select_list(每个 {title, options: [{id, text}]} → {question_key, title, option_list})
1571
+ * submit_text → submit_button.text
1572
+ *
1573
+ * 代码自动生成:select_list[].question_key, submit_button.key
1574
+ *
1575
+ * 如果 LLM 已输出了合法的 API 原始格式(有 select_list[0].option_list),则跳过转换直接透传。
1576
+ */
1577
+ function transformMultipleInteraction(card, log) {
1578
+ // 防御性:如果已经是合法 API 格式,跳过
1579
+ const existingSelectList = card.select_list;
1580
+ if (Array.isArray(existingSelectList) &&
1581
+ existingSelectList.length > 0 &&
1582
+ Array.isArray(existingSelectList[0]?.option_list)) {
1583
+ log?.(`[template-card-parser] transformMultipleInteraction: already has select_list[].option_list, skipping transform`);
1584
+ return card;
1585
+ }
1586
+ // 提取 selectors(简化格式的核心字段)
1587
+ const selectors = card.selectors;
1588
+ if (!Array.isArray(selectors) || selectors.length === 0) {
1589
+ log?.(`[template-card-parser] transformMultipleInteraction: no "selectors" array found, skipping transform`);
1590
+ return card;
1591
+ }
1592
+ log?.(`[template-card-parser] transformMultipleInteraction: transforming simplified format → API format`);
1593
+ log?.(`[template-card-parser] transformMultipleInteraction: input=${JSON.stringify(card)}`);
1594
+ // ── 构建 main_title ──
1595
+ const title = card.title;
1596
+ const description = card.description;
1597
+ if (title || description) {
1598
+ card.main_title = {
1599
+ ...(title ? { title } : {}),
1600
+ ...(description ? { desc: description } : {}),
1601
+ };
1602
+ delete card.title;
1603
+ delete card.description;
1604
+ }
1605
+ // ── 构建 select_list(最多 3 个选择器,每个最多 10 个选项) ──
1606
+ const clampedSelectors = selectors.slice(0, 3);
1607
+ if (selectors.length > 3) {
1608
+ log?.(`[template-card-parser] transformMultipleInteraction: selectors count ${selectors.length} exceeds max 3, clamped to 3`);
1609
+ }
1610
+ card.select_list = clampedSelectors.map((sel, idx) => {
1611
+ const selectorOptions = (sel.options ?? []).slice(0, 10);
1612
+ return {
1613
+ question_key: generateKey(`sel_${idx}`),
1614
+ title: String(sel.title ?? sel.label ?? `选择${idx + 1}`),
1615
+ option_list: selectorOptions.map((opt) => ({
1616
+ id: String(opt.id ?? opt.value ?? `opt_${Math.random().toString(36).slice(2, 6)}`),
1617
+ text: String(opt.text ?? opt.label ?? opt.name ?? ""),
1618
+ })),
1619
+ };
1620
+ });
1621
+ delete card.selectors;
1622
+ // ── 构建 submit_button ──
1623
+ const submitText = card.submit_text || "提交";
1624
+ card.submit_button = {
1625
+ text: submitText,
1626
+ key: generateKey("submit_multi"),
1627
+ };
1628
+ delete card.submit_text;
1629
+ log?.(`[template-card-parser] transformMultipleInteraction: output=${JSON.stringify(card)}`);
1630
+ return card;
1631
+ }
1632
+ /**
1633
+ * 对 vote_interaction / multiple_interaction 执行简化格式转换。
1634
+ * 其他 card_type 直接跳过。
1635
+ */
1636
+ function transformSimplifiedCard(card, log) {
1637
+ const cardType = card.card_type;
1638
+ if (cardType === "vote_interaction") {
1639
+ return transformVoteInteraction(card, log);
1640
+ }
1641
+ if (cardType === "multiple_interaction") {
1642
+ return transformMultipleInteraction(card, log);
1643
+ }
1644
+ return card;
1645
+ }
1372
1646
  /**
1373
1647
  * 匹配 markdown 代码块的正则表达式
1374
1648
  * 支持 ```json ... ``` 和 ``` ... ``` 两种格式
@@ -1428,8 +1702,12 @@ function extractTemplateCards(text, log) {
1428
1702
  continue;
1429
1703
  }
1430
1704
  log?.(`[template-card-parser] Code block #${blockIndex} is valid template card, card_type="${cardType}"`);
1705
+ // vote_interaction / multiple_interaction:简化格式 → API 格式转换
1706
+ transformSimplifiedCard(parsed, log);
1431
1707
  // 修正 LLM 可能输出的错误字段类型(如 checkbox.mode: "multi" → 1)
1432
1708
  normalizeTemplateCardFields(parsed, log);
1709
+ // 校验并补全必填字段(如缺失的 task_id、main_title、card_action)
1710
+ validateAndFixRequiredFields(parsed, log);
1433
1711
  // 合法的模板卡片,收集并标记移除
1434
1712
  cards.push({
1435
1713
  cardJson: parsed,