@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.cjs.js CHANGED
@@ -1220,24 +1220,31 @@ const MODE_ALIASES = {
1220
1220
  };
1221
1221
  /**
1222
1222
  * 修正 checkbox.mode:
1223
- * - 整数 → 原样
1223
+ * - 0 → 单选,1 → 多选,仅允许这两个值
1224
1224
  * - 字符串数字 "0"/"1" → 整数
1225
1225
  * - 语义别名 "multi"/"single" 等 → 对应整数
1226
+ * - 其他正整数(如 2)→ clamp 到 1(多选)
1226
1227
  * - 无法识别 → 删除(让服务端使用默认值 0)
1227
1228
  */
1228
1229
  function coerceCheckboxMode(value) {
1230
+ let num;
1229
1231
  if (typeof value === "number" && Number.isFinite(value)) {
1230
- return Math.round(value);
1232
+ num = Math.round(value);
1231
1233
  }
1232
- if (typeof value === "string") {
1234
+ else if (typeof value === "string") {
1233
1235
  const trimmed = value.trim().toLowerCase();
1234
1236
  if (trimmed in MODE_ALIASES)
1235
1237
  return MODE_ALIASES[trimmed];
1236
- const num = Number(trimmed);
1237
- if (!Number.isNaN(num))
1238
- return Math.round(num);
1238
+ const parsed = Number(trimmed);
1239
+ if (!Number.isNaN(parsed))
1240
+ num = Math.round(parsed);
1239
1241
  }
1240
- return undefined;
1242
+ if (num === undefined)
1243
+ return undefined;
1244
+ // mode 只允许 0(单选)或 1(多选),超出范围 clamp
1245
+ if (num <= 0)
1246
+ return 0;
1247
+ return 1;
1241
1248
  }
1242
1249
  /**
1243
1250
  * 对 LLM 生成的模板卡片 JSON 做字段类型修正,确保符合企业微信 API 的类型要求。
@@ -1396,6 +1403,273 @@ function normalizeTemplateCardFields(card, log) {
1396
1403
  }
1397
1404
  return card;
1398
1405
  }
1406
+ /**
1407
+ * 校验并补全模板卡片的必填字段。
1408
+ *
1409
+ * 在 normalizeTemplateCardFields(类型修正)之后调用,确保卡片结构满足企业微信 API 的必填要求。
1410
+ *
1411
+ * 补全策略:
1412
+ * - task_id:所有卡片统一自动补全(交互型 API 必填,通知型插件也需要用于缓存回写)
1413
+ * - main_title:除 text_notice 外的 4 种卡片 API 必填,自动补 { title: "通知" }
1414
+ * text_notice 要求 main_title.title 与 sub_title_text 至少填一个,缺两个时补 sub_title_text
1415
+ * - card_action:text_notice / news_notice API 必填,自动补 { type: 1, url: "https://work.weixin.qq.com" }
1416
+ * - checkbox:vote_interaction API 必填,无法凭空补全,仅记告警
1417
+ * - submit_button:vote_interaction / multiple_interaction API 必填,自动补 { text: "提交", key: "submit_default" }
1418
+ * - button_list:button_interaction API 必填,无法凭空补全,仅记告警
1419
+ * - select_list:multiple_interaction API 必填,无法凭空补全,仅记告警
1420
+ */
1421
+ function validateAndFixRequiredFields(card, log) {
1422
+ const cardType = card.card_type;
1423
+ const fixes = [];
1424
+ const warnings = [];
1425
+ // ── task_id(所有卡片:始终确保唯一性) ─────────────────────────────
1426
+ // LLM 可能编造时间戳导致重复,因此无论是否提供了 task_id,
1427
+ // 都提取语义前缀 + 代码追加真实时间戳和随机后缀来保证唯一。
1428
+ const rawTid = (typeof card.task_id === "string" && card.task_id.trim()) ? card.task_id.trim() : "";
1429
+ const rand = Math.random().toString(36).slice(2, 6);
1430
+ const ts = Date.now();
1431
+ let finalTid;
1432
+ if (rawTid) {
1433
+ // 提取 LLM 的语义前缀:去掉尾部的数字串(LLM 编造的假时间戳)
1434
+ const prefix = rawTid.replace(/_\d{8,}$/, "").replace(/[^a-zA-Z0-9_\-@]/g, "_").slice(0, 80);
1435
+ finalTid = prefix ? `${prefix}_${ts}_${rand}` : `task_${cardType}_${ts}_${rand}`;
1436
+ }
1437
+ else {
1438
+ finalTid = `task_${cardType}_${ts}_${rand}`;
1439
+ }
1440
+ if (finalTid !== rawTid) {
1441
+ fixes.push(`task_id: "${rawTid || "(missing)"}" → "${finalTid}"`);
1442
+ }
1443
+ card.task_id = finalTid;
1444
+ // ── main_title ────────────────────────────────────────────────────────
1445
+ const mainTitle = card.main_title;
1446
+ const hasMainTitle = mainTitle && typeof mainTitle === "object" &&
1447
+ (typeof mainTitle.title === "string" && mainTitle.title.trim());
1448
+ const hasSubTitleText = typeof card.sub_title_text === "string" && card.sub_title_text.trim();
1449
+ switch (cardType) {
1450
+ case "text_notice":
1451
+ // text_notice: main_title.title 和 sub_title_text 至少一个
1452
+ if (!hasMainTitle && !hasSubTitleText) {
1453
+ card.sub_title_text = card.sub_title_text || "通知";
1454
+ fixes.push(`sub_title_text: (missing, no main_title either) → fallback "通知"`);
1455
+ }
1456
+ break;
1457
+ case "news_notice":
1458
+ case "button_interaction":
1459
+ case "vote_interaction":
1460
+ case "multiple_interaction":
1461
+ // 这四种 main_title 为必填
1462
+ if (!mainTitle || typeof mainTitle !== "object") {
1463
+ card.main_title = { title: "通知" };
1464
+ fixes.push(`main_title: (missing) → { title: "通知" }`);
1465
+ }
1466
+ else if (!hasMainTitle) {
1467
+ mainTitle.title = "通知";
1468
+ fixes.push(`main_title.title: (empty) → "通知"`);
1469
+ }
1470
+ break;
1471
+ }
1472
+ // ── card_action(text_notice / news_notice 必填) ──────────────────
1473
+ if (cardType === "text_notice" || cardType === "news_notice") {
1474
+ if (!card.card_action || typeof card.card_action !== "object") {
1475
+ card.card_action = { type: 1, url: "https://work.weixin.qq.com" };
1476
+ fixes.push(`card_action: (missing) → { type: 1, url: "https://work.weixin.qq.com" }`);
1477
+ }
1478
+ }
1479
+ // ── submit_button(vote_interaction / multiple_interaction 必填) ──
1480
+ if (cardType === "vote_interaction" || cardType === "multiple_interaction") {
1481
+ if (!card.submit_button || typeof card.submit_button !== "object") {
1482
+ card.submit_button = { text: "提交", key: `submit_${cardType}_${Date.now()}` };
1483
+ fixes.push(`submit_button: (missing) → auto-generated`);
1484
+ }
1485
+ }
1486
+ // ── 核心业务字段(无法凭空补全,仅告警) ────────────────────────────
1487
+ if (cardType === "button_interaction") {
1488
+ if (!Array.isArray(card.button_list) || card.button_list.length === 0) {
1489
+ warnings.push(`button_list is missing or empty (required for button_interaction)`);
1490
+ }
1491
+ }
1492
+ if (cardType === "vote_interaction") {
1493
+ if (!card.checkbox || typeof card.checkbox !== "object") {
1494
+ warnings.push(`checkbox is missing (required for vote_interaction)`);
1495
+ }
1496
+ }
1497
+ if (cardType === "multiple_interaction") {
1498
+ if (!Array.isArray(card.select_list) || card.select_list.length === 0) {
1499
+ warnings.push(`select_list is missing or empty (required for multiple_interaction)`);
1500
+ }
1501
+ }
1502
+ if (fixes.length > 0) {
1503
+ log?.(`[template-card-parser] validateAndFixRequiredFields: ${fixes.length} fix(es): ${fixes.join("; ")}`);
1504
+ }
1505
+ if (warnings.length > 0) {
1506
+ log?.(`[template-card-parser] validateAndFixRequiredFields: ${warnings.length} warning(s): ${warnings.join("; ")}`);
1507
+ }
1508
+ return card;
1509
+ }
1510
+ // ============================================================================
1511
+ // 简化格式 → 企微 API 格式转换(vote_interaction / multiple_interaction)
1512
+ // ============================================================================
1513
+ /**
1514
+ * 生成唯一的 question_key / submit_button.key。
1515
+ */
1516
+ function generateKey(prefix) {
1517
+ const rand = Math.random().toString(36).slice(2, 6);
1518
+ return `${prefix}_${Date.now()}_${rand}`;
1519
+ }
1520
+ /**
1521
+ * 将 vote_interaction 的简化格式转换为企微 API 格式。
1522
+ *
1523
+ * 简化格式字段:
1524
+ * title → main_title.title
1525
+ * description → main_title.desc
1526
+ * options → checkbox.option_list(每个 {id, text} 直接透传)
1527
+ * mode → checkbox.mode(0=单选 1=多选)
1528
+ * submit_text → submit_button.text
1529
+ *
1530
+ * 代码自动生成:checkbox.question_key, submit_button.key
1531
+ *
1532
+ * 如果 LLM 已输出了合法的 API 原始格式(有 checkbox.option_list),则跳过转换直接透传。
1533
+ */
1534
+ function transformVoteInteraction(card, log) {
1535
+ // 防御性:如果已经是合法 API 格式,跳过
1536
+ const existingCheckbox = card.checkbox;
1537
+ if (existingCheckbox && typeof existingCheckbox === "object" && Array.isArray(existingCheckbox.option_list)) {
1538
+ log?.(`[template-card-parser] transformVoteInteraction: already has checkbox.option_list, skipping transform`);
1539
+ return card;
1540
+ }
1541
+ // 提取 options(简化格式的核心字段)
1542
+ const options = card.options;
1543
+ if (!Array.isArray(options) || options.length === 0) {
1544
+ log?.(`[template-card-parser] transformVoteInteraction: no "options" array found, skipping transform`);
1545
+ return card;
1546
+ }
1547
+ log?.(`[template-card-parser] transformVoteInteraction: transforming simplified format → API format`);
1548
+ log?.(`[template-card-parser] transformVoteInteraction: input=${JSON.stringify(card)}`);
1549
+ // ── 构建 main_title ──
1550
+ const title = card.title;
1551
+ const description = card.description;
1552
+ if (title || description) {
1553
+ card.main_title = {
1554
+ ...(title ? { title } : {}),
1555
+ ...(description ? { desc: description } : {}),
1556
+ };
1557
+ delete card.title;
1558
+ delete card.description;
1559
+ }
1560
+ // ── 构建 checkbox(最多 20 个选项) ──
1561
+ const mode = coerceCheckboxMode(card.mode) ?? 0;
1562
+ const questionKey = generateKey("vote");
1563
+ const clampedOptions = options.slice(0, 20);
1564
+ if (options.length > 20) {
1565
+ log?.(`[template-card-parser] transformVoteInteraction: options count ${options.length} exceeds max 20, clamped to 20`);
1566
+ }
1567
+ card.checkbox = {
1568
+ question_key: questionKey,
1569
+ mode,
1570
+ option_list: clampedOptions.map((opt) => ({
1571
+ id: String(opt.id ?? opt.value ?? `opt_${Math.random().toString(36).slice(2, 6)}`),
1572
+ text: String(opt.text ?? opt.label ?? opt.name ?? ""),
1573
+ })),
1574
+ };
1575
+ delete card.options;
1576
+ delete card.mode;
1577
+ // ── 构建 submit_button ──
1578
+ const submitText = card.submit_text || "提交";
1579
+ card.submit_button = {
1580
+ text: submitText,
1581
+ key: generateKey("submit_vote"),
1582
+ };
1583
+ delete card.submit_text;
1584
+ // ── 清理 LLM 可能杜撰的无效字段 ──
1585
+ delete card.vote_question;
1586
+ delete card.vote_option;
1587
+ delete card.vote_options;
1588
+ log?.(`[template-card-parser] transformVoteInteraction: output=${JSON.stringify(card)}`);
1589
+ return card;
1590
+ }
1591
+ /**
1592
+ * 将 multiple_interaction 的简化格式转换为企微 API 格式。
1593
+ *
1594
+ * 简化格式字段:
1595
+ * title → main_title.title
1596
+ * description → main_title.desc
1597
+ * selectors → select_list(每个 {title, options: [{id, text}]} → {question_key, title, option_list})
1598
+ * submit_text → submit_button.text
1599
+ *
1600
+ * 代码自动生成:select_list[].question_key, submit_button.key
1601
+ *
1602
+ * 如果 LLM 已输出了合法的 API 原始格式(有 select_list[0].option_list),则跳过转换直接透传。
1603
+ */
1604
+ function transformMultipleInteraction(card, log) {
1605
+ // 防御性:如果已经是合法 API 格式,跳过
1606
+ const existingSelectList = card.select_list;
1607
+ if (Array.isArray(existingSelectList) &&
1608
+ existingSelectList.length > 0 &&
1609
+ Array.isArray(existingSelectList[0]?.option_list)) {
1610
+ log?.(`[template-card-parser] transformMultipleInteraction: already has select_list[].option_list, skipping transform`);
1611
+ return card;
1612
+ }
1613
+ // 提取 selectors(简化格式的核心字段)
1614
+ const selectors = card.selectors;
1615
+ if (!Array.isArray(selectors) || selectors.length === 0) {
1616
+ log?.(`[template-card-parser] transformMultipleInteraction: no "selectors" array found, skipping transform`);
1617
+ return card;
1618
+ }
1619
+ log?.(`[template-card-parser] transformMultipleInteraction: transforming simplified format → API format`);
1620
+ log?.(`[template-card-parser] transformMultipleInteraction: input=${JSON.stringify(card)}`);
1621
+ // ── 构建 main_title ──
1622
+ const title = card.title;
1623
+ const description = card.description;
1624
+ if (title || description) {
1625
+ card.main_title = {
1626
+ ...(title ? { title } : {}),
1627
+ ...(description ? { desc: description } : {}),
1628
+ };
1629
+ delete card.title;
1630
+ delete card.description;
1631
+ }
1632
+ // ── 构建 select_list(最多 3 个选择器,每个最多 10 个选项) ──
1633
+ const clampedSelectors = selectors.slice(0, 3);
1634
+ if (selectors.length > 3) {
1635
+ log?.(`[template-card-parser] transformMultipleInteraction: selectors count ${selectors.length} exceeds max 3, clamped to 3`);
1636
+ }
1637
+ card.select_list = clampedSelectors.map((sel, idx) => {
1638
+ const selectorOptions = (sel.options ?? []).slice(0, 10);
1639
+ return {
1640
+ question_key: generateKey(`sel_${idx}`),
1641
+ title: String(sel.title ?? sel.label ?? `选择${idx + 1}`),
1642
+ option_list: selectorOptions.map((opt) => ({
1643
+ id: String(opt.id ?? opt.value ?? `opt_${Math.random().toString(36).slice(2, 6)}`),
1644
+ text: String(opt.text ?? opt.label ?? opt.name ?? ""),
1645
+ })),
1646
+ };
1647
+ });
1648
+ delete card.selectors;
1649
+ // ── 构建 submit_button ──
1650
+ const submitText = card.submit_text || "提交";
1651
+ card.submit_button = {
1652
+ text: submitText,
1653
+ key: generateKey("submit_multi"),
1654
+ };
1655
+ delete card.submit_text;
1656
+ log?.(`[template-card-parser] transformMultipleInteraction: output=${JSON.stringify(card)}`);
1657
+ return card;
1658
+ }
1659
+ /**
1660
+ * 对 vote_interaction / multiple_interaction 执行简化格式转换。
1661
+ * 其他 card_type 直接跳过。
1662
+ */
1663
+ function transformSimplifiedCard(card, log) {
1664
+ const cardType = card.card_type;
1665
+ if (cardType === "vote_interaction") {
1666
+ return transformVoteInteraction(card, log);
1667
+ }
1668
+ if (cardType === "multiple_interaction") {
1669
+ return transformMultipleInteraction(card, log);
1670
+ }
1671
+ return card;
1672
+ }
1399
1673
  /**
1400
1674
  * 匹配 markdown 代码块的正则表达式
1401
1675
  * 支持 ```json ... ``` 和 ``` ... ``` 两种格式
@@ -1455,8 +1729,12 @@ function extractTemplateCards(text, log) {
1455
1729
  continue;
1456
1730
  }
1457
1731
  log?.(`[template-card-parser] Code block #${blockIndex} is valid template card, card_type="${cardType}"`);
1732
+ // vote_interaction / multiple_interaction:简化格式 → API 格式转换
1733
+ transformSimplifiedCard(parsed, log);
1458
1734
  // 修正 LLM 可能输出的错误字段类型(如 checkbox.mode: "multi" → 1)
1459
1735
  normalizeTemplateCardFields(parsed, log);
1736
+ // 校验并补全必填字段(如缺失的 task_id、main_title、card_action)
1737
+ validateAndFixRequiredFields(parsed, log);
1460
1738
  // 合法的模板卡片,收集并标记移除
1461
1739
  cards.push({
1462
1740
  cardJson: parsed,