ylib-wecom-openclaw-plugin 2026.4.29

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.
Files changed (180) hide show
  1. package/README.md +596 -0
  2. package/dist/index.d.ts +10 -0
  3. package/dist/index.js +99 -0
  4. package/dist/src/accounts.d.ts +57 -0
  5. package/dist/src/accounts.js +247 -0
  6. package/dist/src/agent/api-client.d.ts +95 -0
  7. package/dist/src/agent/api-client.js +425 -0
  8. package/dist/src/agent/handler.d.ts +64 -0
  9. package/dist/src/agent/handler.js +731 -0
  10. package/dist/src/agent/index.d.ts +5 -0
  11. package/dist/src/agent/index.js +21 -0
  12. package/dist/src/agent/webhook.d.ts +25 -0
  13. package/dist/src/agent/webhook.js +294 -0
  14. package/dist/src/agent/xml.d.ts +21 -0
  15. package/dist/src/agent/xml.js +43 -0
  16. package/dist/src/channel.d.ts +5 -0
  17. package/dist/src/channel.js +815 -0
  18. package/dist/src/chat-queue.d.ts +31 -0
  19. package/dist/src/chat-queue.js +53 -0
  20. package/dist/src/config-schema.d.ts +587 -0
  21. package/dist/src/config-schema.js +146 -0
  22. package/dist/src/const.d.ts +128 -0
  23. package/dist/src/const.js +168 -0
  24. package/dist/src/dm-policy.d.ts +29 -0
  25. package/dist/src/dm-policy.js +146 -0
  26. package/dist/src/dynamic-agent.d.ts +37 -0
  27. package/dist/src/dynamic-agent.js +67 -0
  28. package/dist/src/dynamic-routing.d.ts +65 -0
  29. package/dist/src/dynamic-routing.js +62 -0
  30. package/dist/src/endpoint-dispatch.d.ts +54 -0
  31. package/dist/src/endpoint-dispatch.js +967 -0
  32. package/dist/src/endpoint-event-adapter.d.ts +15 -0
  33. package/dist/src/endpoint-event-adapter.js +427 -0
  34. package/dist/src/group-policy.d.ts +30 -0
  35. package/dist/src/group-policy.js +126 -0
  36. package/dist/src/http.d.ts +27 -0
  37. package/dist/src/http.js +168 -0
  38. package/dist/src/im-runtime-telemetry.d.ts +25 -0
  39. package/dist/src/im-runtime-telemetry.js +68 -0
  40. package/dist/src/interface.d.ts +192 -0
  41. package/dist/src/interface.js +5 -0
  42. package/dist/src/markdown-chunk.d.ts +1 -0
  43. package/dist/src/markdown-chunk.js +396 -0
  44. package/dist/src/mcp/index.d.ts +6 -0
  45. package/dist/src/mcp/index.js +28 -0
  46. package/dist/src/mcp/interceptors/biz-error.d.ts +11 -0
  47. package/dist/src/mcp/interceptors/biz-error.js +73 -0
  48. package/dist/src/mcp/interceptors/doc-auth-error.d.ts +10 -0
  49. package/dist/src/mcp/interceptors/doc-auth-error.js +235 -0
  50. package/dist/src/mcp/interceptors/index.d.ts +35 -0
  51. package/dist/src/mcp/interceptors/index.js +143 -0
  52. package/dist/src/mcp/interceptors/msg-media.d.ts +11 -0
  53. package/dist/src/mcp/interceptors/msg-media.js +201 -0
  54. package/dist/src/mcp/interceptors/smartpage-create.d.ts +30 -0
  55. package/dist/src/mcp/interceptors/smartpage-create.js +252 -0
  56. package/dist/src/mcp/interceptors/smartpage-export.d.ts +17 -0
  57. package/dist/src/mcp/interceptors/smartpage-export.js +135 -0
  58. package/dist/src/mcp/interceptors/smartsheet-upload.d.ts +22 -0
  59. package/dist/src/mcp/interceptors/smartsheet-upload.js +388 -0
  60. package/dist/src/mcp/interceptors/types.d.ts +64 -0
  61. package/dist/src/mcp/interceptors/types.js +8 -0
  62. package/dist/src/mcp/schema.d.ts +11 -0
  63. package/dist/src/mcp/schema.js +115 -0
  64. package/dist/src/mcp/tool.d.ts +63 -0
  65. package/dist/src/mcp/tool.js +318 -0
  66. package/dist/src/mcp/transport.d.ts +94 -0
  67. package/dist/src/mcp/transport.js +702 -0
  68. package/dist/src/media-handler.d.ts +55 -0
  69. package/dist/src/media-handler.js +306 -0
  70. package/dist/src/media-uploader.d.ts +142 -0
  71. package/dist/src/media-uploader.js +446 -0
  72. package/dist/src/message-parser.d.ts +104 -0
  73. package/dist/src/message-parser.js +232 -0
  74. package/dist/src/message-sender.d.ts +54 -0
  75. package/dist/src/message-sender.js +210 -0
  76. package/dist/src/monitor.d.ts +69 -0
  77. package/dist/src/monitor.js +1846 -0
  78. package/dist/src/onboarding.d.ts +8 -0
  79. package/dist/src/onboarding.js +248 -0
  80. package/dist/src/openclaw-compat.d.ts +148 -0
  81. package/dist/src/openclaw-compat.js +839 -0
  82. package/dist/src/proactive-markdown-send.d.ts +14 -0
  83. package/dist/src/proactive-markdown-send.js +205 -0
  84. package/dist/src/reqid-store.d.ts +23 -0
  85. package/dist/src/reqid-store.js +136 -0
  86. package/dist/src/runtime.d.ts +2 -0
  87. package/dist/src/runtime.js +7 -0
  88. package/dist/src/shared/command-auth.d.ts +23 -0
  89. package/dist/src/shared/command-auth.js +112 -0
  90. package/dist/src/shared/xml-parser.d.ts +46 -0
  91. package/dist/src/shared/xml-parser.js +228 -0
  92. package/dist/src/state-dir-resolve.d.ts +2 -0
  93. package/dist/src/state-dir-resolve.js +33 -0
  94. package/dist/src/state-manager.d.ts +115 -0
  95. package/dist/src/state-manager.js +413 -0
  96. package/dist/src/target.d.ts +35 -0
  97. package/dist/src/target.js +71 -0
  98. package/dist/src/template-card-manager.d.ts +55 -0
  99. package/dist/src/template-card-manager.js +316 -0
  100. package/dist/src/template-card-parser.d.ts +37 -0
  101. package/dist/src/template-card-parser.js +672 -0
  102. package/dist/src/timeout.d.ts +20 -0
  103. package/dist/src/timeout.js +57 -0
  104. package/dist/src/types/account.d.ts +29 -0
  105. package/dist/src/types/account.js +5 -0
  106. package/dist/src/types/config.d.ts +98 -0
  107. package/dist/src/types/config.js +8 -0
  108. package/dist/src/types/constants.d.ts +42 -0
  109. package/dist/src/types/constants.js +45 -0
  110. package/dist/src/types/index.d.ts +7 -0
  111. package/dist/src/types/index.js +17 -0
  112. package/dist/src/types/message.d.ts +238 -0
  113. package/dist/src/types/message.js +6 -0
  114. package/dist/src/utils.d.ts +148 -0
  115. package/dist/src/utils.js +92 -0
  116. package/dist/src/version.d.ts +2 -0
  117. package/dist/src/version.js +28 -0
  118. package/dist/src/webhook/command-auth.d.ts +47 -0
  119. package/dist/src/webhook/command-auth.js +137 -0
  120. package/dist/src/webhook/gateway.d.ts +36 -0
  121. package/dist/src/webhook/gateway.js +297 -0
  122. package/dist/src/webhook/handler.d.ts +19 -0
  123. package/dist/src/webhook/handler.js +481 -0
  124. package/dist/src/webhook/helpers.d.ts +157 -0
  125. package/dist/src/webhook/helpers.js +936 -0
  126. package/dist/src/webhook/http.d.ts +27 -0
  127. package/dist/src/webhook/http.js +168 -0
  128. package/dist/src/webhook/index.d.ts +11 -0
  129. package/dist/src/webhook/index.js +43 -0
  130. package/dist/src/webhook/media.d.ts +30 -0
  131. package/dist/src/webhook/media.js +152 -0
  132. package/dist/src/webhook/monitor.d.ts +59 -0
  133. package/dist/src/webhook/monitor.js +1672 -0
  134. package/dist/src/webhook/state.d.ts +220 -0
  135. package/dist/src/webhook/state.js +568 -0
  136. package/dist/src/webhook/target.d.ts +41 -0
  137. package/dist/src/webhook/target.js +165 -0
  138. package/dist/src/webhook/types.d.ts +348 -0
  139. package/dist/src/webhook/types.js +36 -0
  140. package/dist/src/webhook/video-frame.d.ts +13 -0
  141. package/dist/src/webhook/video-frame.js +108 -0
  142. package/openclaw.plugin.json +19 -0
  143. package/package.json +96 -0
  144. package/schema.json +534 -0
  145. package/scripts/generate-schema.mjs +33 -0
  146. package/skills/wecom-contact/SKILL.md +162 -0
  147. package/skills/wecom-doc/SKILL.md +162 -0
  148. package/skills/wecom-doc/references/create-doc.md +56 -0
  149. package/skills/wecom-doc/references/edit-doc-content.md +68 -0
  150. package/skills/wecom-doc/references/get-doc-content.md +88 -0
  151. package/skills/wecom-doc/references/smartpage-create.md +125 -0
  152. package/skills/wecom-doc/references/smartpage-export.md +160 -0
  153. package/skills/wecom-meeting/SKILL.md +441 -0
  154. package/skills/wecom-meeting/references/example-full.md +30 -0
  155. package/skills/wecom-meeting/references/example-reminder.md +46 -0
  156. package/skills/wecom-meeting/references/example-security.md +22 -0
  157. package/skills/wecom-meeting/references/response-get-meeting-info.md +148 -0
  158. package/skills/wecom-msg/SKILL.md +157 -0
  159. package/skills/wecom-msg/references/api-get-messages.md +93 -0
  160. package/skills/wecom-msg/references/api-get-msg-chat-list.md +58 -0
  161. package/skills/wecom-msg/references/api-get-msg-media.md +44 -0
  162. package/skills/wecom-msg/references/api-send-message.md +39 -0
  163. package/skills/wecom-preflight/SKILL.md +141 -0
  164. package/skills/wecom-schedule/SKILL.md +161 -0
  165. package/skills/wecom-schedule/references/api-check-availability.md +56 -0
  166. package/skills/wecom-schedule/references/api-create-schedule.md +38 -0
  167. package/skills/wecom-schedule/references/api-get-schedule-detail.md +81 -0
  168. package/skills/wecom-schedule/references/api-update-schedule.md +32 -0
  169. package/skills/wecom-schedule/references/ref-reminders.md +24 -0
  170. package/skills/wecom-send-media/SKILL.md +68 -0
  171. package/skills/wecom-send-template-card/SKILL.md +157 -0
  172. package/skills/wecom-send-template-card/references/api-template-card-types.md +358 -0
  173. package/skills/wecom-smartsheet/SKILL.md +164 -0
  174. package/skills/wecom-smartsheet/references/smartsheet-cell-value-formats.md +163 -0
  175. package/skills/wecom-smartsheet/references/smartsheet-field-types.md +44 -0
  176. package/skills/wecom-smartsheet/references/smartsheet-get-records.md +96 -0
  177. package/skills/wecom-smartsheet/references/webhook-examples.md +185 -0
  178. package/skills/wecom-smartsheet/references/webhook-fallback.md +184 -0
  179. package/skills/wecom-todo/SKILL.md +392 -0
  180. package/skills/wecom-todo/examples/workflows.md +163 -0
@@ -0,0 +1,672 @@
1
+ "use strict";
2
+ /**
3
+ * 模板卡片解析器
4
+ *
5
+ * 从 LLM 回复文本中提取 markdown JSON 代码块,验证其是否为合法的企业微信模板卡片,
6
+ * 返回提取到的卡片列表和剩余文本。
7
+ *
8
+ * 同时提供 maskTemplateCardBlocks 函数,用于在流式中间帧中隐藏正在构建的卡片代码块,
9
+ * 避免 JSON 源码暴露给终端用户。
10
+ */
11
+ var __assign = (this && this.__assign) || function () {
12
+ __assign = Object.assign || function(t) {
13
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
14
+ s = arguments[i];
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
16
+ t[p] = s[p];
17
+ }
18
+ return t;
19
+ };
20
+ return __assign.apply(this, arguments);
21
+ };
22
+ exports.__esModule = true;
23
+ exports.maskTemplateCardBlocks = exports.extractTemplateCards = void 0;
24
+ var const_js_1 = require("./const.js");
25
+ // ============================================================================
26
+ // LLM 输出字段类型修正
27
+ // ============================================================================
28
+ /**
29
+ * 将 LLM 可能输出的字符串/非法值修正为企业微信 API 要求的整数。
30
+ * 返回修正后的整数,若无法识别则返回 undefined(由调用方决定是否删除该字段)。
31
+ */
32
+ function coerceToInt(value) {
33
+ if (typeof value === "number" && Number.isFinite(value)) {
34
+ return Math.round(value);
35
+ }
36
+ if (typeof value === "string") {
37
+ var trimmed = value.trim().toLowerCase();
38
+ // 纯数字字符串
39
+ var num = Number(trimmed);
40
+ if (!Number.isNaN(num) && Number.isFinite(num)) {
41
+ return Math.round(num);
42
+ }
43
+ }
44
+ return undefined;
45
+ }
46
+ /** 将 LLM 可能输出的字符串/非法值修正为布尔值 */
47
+ function coerceToBool(value) {
48
+ if (typeof value === "boolean")
49
+ return value;
50
+ if (typeof value === "string") {
51
+ var t = value.trim().toLowerCase();
52
+ if (t === "true" || t === "1" || t === "yes")
53
+ return true;
54
+ if (t === "false" || t === "0" || t === "no")
55
+ return false;
56
+ }
57
+ if (typeof value === "number")
58
+ return value !== 0;
59
+ return undefined;
60
+ }
61
+ /** checkbox.mode 的语义别名映射 */
62
+ var MODE_ALIASES = {
63
+ single: 0,
64
+ radio: 0,
65
+ "单选": 0,
66
+ multi: 1,
67
+ multiple: 1,
68
+ "多选": 1
69
+ };
70
+ /**
71
+ * 修正 checkbox.mode:
72
+ * - 0 → 单选,1 → 多选,仅允许这两个值
73
+ * - 字符串数字 "0"/"1" → 整数
74
+ * - 语义别名 "multi"/"single" 等 → 对应整数
75
+ * - 其他正整数(如 2)→ clamp 到 1(多选)
76
+ * - 无法识别 → 删除(让服务端使用默认值 0)
77
+ */
78
+ function coerceCheckboxMode(value) {
79
+ var num;
80
+ if (typeof value === "number" && Number.isFinite(value)) {
81
+ num = Math.round(value);
82
+ }
83
+ else if (typeof value === "string") {
84
+ var trimmed = value.trim().toLowerCase();
85
+ if (trimmed in MODE_ALIASES)
86
+ return MODE_ALIASES[trimmed];
87
+ var parsed = Number(trimmed);
88
+ if (!Number.isNaN(parsed))
89
+ num = Math.round(parsed);
90
+ }
91
+ if (num === undefined)
92
+ return undefined;
93
+ // mode 只允许 0(单选)或 1(多选),超出范围 clamp
94
+ if (num <= 0)
95
+ return 0;
96
+ return 1;
97
+ }
98
+ /**
99
+ * 对 LLM 生成的模板卡片 JSON 做字段类型修正,确保符合企业微信 API 的类型要求。
100
+ *
101
+ * 修正范围:
102
+ * - checkbox.mode: uint32(0=单选,1=多选)
103
+ * - checkbox.disable: bool
104
+ * - checkbox.option_list[].is_checked: bool
105
+ * - source.desc_color: int
106
+ * - quote_area.type: int
107
+ * - card_action.type: int
108
+ * - image_text_area.type: int
109
+ * - horizontal_content_list[].type: int
110
+ * - jump_list[].type: int
111
+ * - button_list[].style: int
112
+ * - button_selection.disable: bool
113
+ * - select_list[].disable: bool
114
+ *
115
+ * 原则:能修就修,修不了就删(让服务端走默认值),绝不阻塞发送。
116
+ */
117
+ function normalizeTemplateCardFields(card, log) {
118
+ var fixes = [];
119
+ // ── checkbox ──────────────────────────────────────────────────────────
120
+ var checkbox = card.checkbox;
121
+ if (checkbox && typeof checkbox === "object") {
122
+ // mode
123
+ if ("mode" in checkbox) {
124
+ var fixed = coerceCheckboxMode(checkbox.mode);
125
+ if (fixed !== undefined) {
126
+ if (checkbox.mode !== fixed) {
127
+ fixes.push("checkbox.mode: " + JSON.stringify(checkbox.mode) + " \u2192 " + fixed);
128
+ }
129
+ checkbox.mode = fixed;
130
+ }
131
+ else {
132
+ fixes.push("checkbox.mode: " + JSON.stringify(checkbox.mode) + " \u2192 (deleted, invalid)");
133
+ delete checkbox.mode;
134
+ }
135
+ }
136
+ // disable
137
+ if ("disable" in checkbox) {
138
+ var fixed = coerceToBool(checkbox.disable);
139
+ if (fixed !== undefined && checkbox.disable !== fixed) {
140
+ fixes.push("checkbox.disable: " + JSON.stringify(checkbox.disable) + " \u2192 " + fixed);
141
+ checkbox.disable = fixed;
142
+ }
143
+ }
144
+ // option_list[].is_checked
145
+ if (Array.isArray(checkbox.option_list)) {
146
+ for (var _i = 0, _a = checkbox.option_list; _i < _a.length; _i++) {
147
+ var opt = _a[_i];
148
+ if (opt && typeof opt === "object" && "is_checked" in opt) {
149
+ var fixed = coerceToBool(opt.is_checked);
150
+ if (fixed !== undefined && opt.is_checked !== fixed) {
151
+ fixes.push("checkbox.option_list.is_checked: " + JSON.stringify(opt.is_checked) + " \u2192 " + fixed);
152
+ opt.is_checked = fixed;
153
+ }
154
+ }
155
+ }
156
+ }
157
+ }
158
+ // ── source.desc_color ────────────────────────────────────────────────
159
+ var source = card.source;
160
+ if (source && typeof source === "object" && "desc_color" in source) {
161
+ var fixed = coerceToInt(source.desc_color);
162
+ if (fixed !== undefined && source.desc_color !== fixed) {
163
+ fixes.push("source.desc_color: " + JSON.stringify(source.desc_color) + " \u2192 " + fixed);
164
+ source.desc_color = fixed;
165
+ }
166
+ }
167
+ // ── card_action.type ─────────────────────────────────────────────────
168
+ var cardAction = card.card_action;
169
+ if (cardAction && typeof cardAction === "object" && "type" in cardAction) {
170
+ var fixed = coerceToInt(cardAction.type);
171
+ if (fixed !== undefined && cardAction.type !== fixed) {
172
+ fixes.push("card_action.type: " + JSON.stringify(cardAction.type) + " \u2192 " + fixed);
173
+ cardAction.type = fixed;
174
+ }
175
+ }
176
+ // ── quote_area.type ──────────────────────────────────────────────────
177
+ var quoteArea = card.quote_area;
178
+ if (quoteArea && typeof quoteArea === "object" && "type" in quoteArea) {
179
+ var fixed = coerceToInt(quoteArea.type);
180
+ if (fixed !== undefined && quoteArea.type !== fixed) {
181
+ fixes.push("quote_area.type: " + JSON.stringify(quoteArea.type) + " \u2192 " + fixed);
182
+ quoteArea.type = fixed;
183
+ }
184
+ }
185
+ // ── image_text_area.type ─────────────────────────────────────────────
186
+ var imageTextArea = card.image_text_area;
187
+ if (imageTextArea && typeof imageTextArea === "object" && "type" in imageTextArea) {
188
+ var fixed = coerceToInt(imageTextArea.type);
189
+ if (fixed !== undefined && imageTextArea.type !== fixed) {
190
+ fixes.push("image_text_area.type: " + JSON.stringify(imageTextArea.type) + " \u2192 " + fixed);
191
+ imageTextArea.type = fixed;
192
+ }
193
+ }
194
+ // ── horizontal_content_list[].type ───────────────────────────────────
195
+ if (Array.isArray(card.horizontal_content_list)) {
196
+ for (var _b = 0, _c = card.horizontal_content_list; _b < _c.length; _b++) {
197
+ var item = _c[_b];
198
+ if (item && typeof item === "object" && "type" in item) {
199
+ var fixed = coerceToInt(item.type);
200
+ if (fixed !== undefined && item.type !== fixed) {
201
+ fixes.push("horizontal_content_list.type: " + JSON.stringify(item.type) + " \u2192 " + fixed);
202
+ item.type = fixed;
203
+ }
204
+ }
205
+ }
206
+ }
207
+ // ── jump_list[].type ─────────────────────────────────────────────────
208
+ if (Array.isArray(card.jump_list)) {
209
+ for (var _d = 0, _e = card.jump_list; _d < _e.length; _d++) {
210
+ var item = _e[_d];
211
+ if (item && typeof item === "object" && "type" in item) {
212
+ var fixed = coerceToInt(item.type);
213
+ if (fixed !== undefined && item.type !== fixed) {
214
+ fixes.push("jump_list.type: " + JSON.stringify(item.type) + " \u2192 " + fixed);
215
+ item.type = fixed;
216
+ }
217
+ }
218
+ }
219
+ }
220
+ // ── button_list[].style ──────────────────────────────────────────────
221
+ if (Array.isArray(card.button_list)) {
222
+ for (var _f = 0, _g = card.button_list; _f < _g.length; _f++) {
223
+ var btn = _g[_f];
224
+ if (btn && typeof btn === "object" && "style" in btn) {
225
+ var fixed = coerceToInt(btn.style);
226
+ if (fixed !== undefined && btn.style !== fixed) {
227
+ fixes.push("button_list.style: " + JSON.stringify(btn.style) + " \u2192 " + fixed);
228
+ btn.style = fixed;
229
+ }
230
+ }
231
+ }
232
+ }
233
+ // ── button_selection.disable ─────────────────────────────────────────
234
+ var buttonSelection = card.button_selection;
235
+ if (buttonSelection && typeof buttonSelection === "object" && "disable" in buttonSelection) {
236
+ var fixed = coerceToBool(buttonSelection.disable);
237
+ if (fixed !== undefined && buttonSelection.disable !== fixed) {
238
+ fixes.push("button_selection.disable: " + JSON.stringify(buttonSelection.disable) + " \u2192 " + fixed);
239
+ buttonSelection.disable = fixed;
240
+ }
241
+ }
242
+ // ── select_list[].disable ────────────────────────────────────────────
243
+ if (Array.isArray(card.select_list)) {
244
+ for (var _h = 0, _j = card.select_list; _h < _j.length; _h++) {
245
+ var sel = _j[_h];
246
+ if (sel && typeof sel === "object" && "disable" in sel) {
247
+ var fixed = coerceToBool(sel.disable);
248
+ if (fixed !== undefined && sel.disable !== fixed) {
249
+ fixes.push("select_list.disable: " + JSON.stringify(sel.disable) + " \u2192 " + fixed);
250
+ sel.disable = fixed;
251
+ }
252
+ }
253
+ }
254
+ }
255
+ if (fixes.length > 0) {
256
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] normalizeTemplateCardFields: " + fixes.length + " fix(es) applied: " + fixes.join("; "));
257
+ }
258
+ return card;
259
+ }
260
+ // ============================================================================
261
+ // 必填字段校验与自动补全
262
+ // ============================================================================
263
+ /** task_id 合法字符:数字、字母、_-@ */
264
+ var TASK_ID_RE = /^[a-zA-Z0-9_\-@]+$/;
265
+ /**
266
+ * 生成一个合法的 task_id。
267
+ * 格式:task_{cardType}_{timestamp}_{随机4位},确保唯一且符合 API 要求。
268
+ */
269
+ function generateTaskId(cardType) {
270
+ var rand = Math.random().toString(36).slice(2, 6);
271
+ return "task_" + cardType + "_" + Date.now() + "_" + rand;
272
+ }
273
+ /**
274
+ * 校验并补全模板卡片的必填字段。
275
+ *
276
+ * 在 normalizeTemplateCardFields(类型修正)之后调用,确保卡片结构满足企业微信 API 的必填要求。
277
+ *
278
+ * 补全策略:
279
+ * - task_id:所有卡片统一自动补全(交互型 API 必填,通知型插件也需要用于缓存回写)
280
+ * - main_title:除 text_notice 外的 4 种卡片 API 必填,自动补 { title: "通知" }
281
+ * text_notice 要求 main_title.title 与 sub_title_text 至少填一个,缺两个时补 sub_title_text
282
+ * - card_action:text_notice / news_notice API 必填,自动补 { type: 1, url: "https://work.weixin.qq.com" }
283
+ * - checkbox:vote_interaction API 必填,无法凭空补全,仅记告警
284
+ * - submit_button:vote_interaction / multiple_interaction API 必填,自动补 { text: "提交", key: "submit_default" }
285
+ * - button_list:button_interaction API 必填,无法凭空补全,仅记告警
286
+ * - select_list:multiple_interaction API 必填,无法凭空补全,仅记告警
287
+ */
288
+ function validateAndFixRequiredFields(card, log) {
289
+ var cardType = card.card_type;
290
+ var fixes = [];
291
+ var warnings = [];
292
+ // ── task_id(所有卡片:始终确保唯一性) ─────────────────────────────
293
+ // LLM 可能编造时间戳导致重复,因此无论是否提供了 task_id,
294
+ // 都提取语义前缀 + 代码追加真实时间戳和随机后缀来保证唯一。
295
+ var rawTid = (typeof card.task_id === "string" && card.task_id.trim()) ? card.task_id.trim() : "";
296
+ var rand = Math.random().toString(36).slice(2, 6);
297
+ var ts = Date.now();
298
+ var finalTid;
299
+ if (rawTid) {
300
+ // 提取 LLM 的语义前缀:去掉尾部的数字串(LLM 编造的假时间戳)
301
+ var prefix = rawTid.replace(/_\d{8,}$/, "").replace(/[^a-zA-Z0-9_\-@]/g, "_").slice(0, 80);
302
+ finalTid = prefix ? prefix + "_" + ts + "_" + rand : "task_" + cardType + "_" + ts + "_" + rand;
303
+ }
304
+ else {
305
+ finalTid = "task_" + cardType + "_" + ts + "_" + rand;
306
+ }
307
+ if (finalTid !== rawTid) {
308
+ fixes.push("task_id: \"" + (rawTid || "(missing)") + "\" \u2192 \"" + finalTid + "\"");
309
+ }
310
+ card.task_id = finalTid;
311
+ // ── main_title ────────────────────────────────────────────────────────
312
+ var mainTitle = card.main_title;
313
+ var hasMainTitle = mainTitle && typeof mainTitle === "object" &&
314
+ (typeof mainTitle.title === "string" && mainTitle.title.trim());
315
+ var hasSubTitleText = typeof card.sub_title_text === "string" && card.sub_title_text.trim();
316
+ switch (cardType) {
317
+ case "text_notice":
318
+ // text_notice: main_title.title 和 sub_title_text 至少一个
319
+ if (!hasMainTitle && !hasSubTitleText) {
320
+ card.sub_title_text = card.sub_title_text || "通知";
321
+ fixes.push("sub_title_text: (missing, no main_title either) \u2192 fallback \"\u901A\u77E5\"");
322
+ }
323
+ break;
324
+ case "news_notice":
325
+ case "button_interaction":
326
+ case "vote_interaction":
327
+ case "multiple_interaction":
328
+ // 这四种 main_title 为必填
329
+ if (!mainTitle || typeof mainTitle !== "object") {
330
+ card.main_title = { title: "通知" };
331
+ fixes.push("main_title: (missing) \u2192 { title: \"\u901A\u77E5\" }");
332
+ }
333
+ else if (!hasMainTitle) {
334
+ mainTitle.title = "通知";
335
+ fixes.push("main_title.title: (empty) \u2192 \"\u901A\u77E5\"");
336
+ }
337
+ break;
338
+ }
339
+ // ── card_action(text_notice / news_notice 必填) ──────────────────
340
+ if (cardType === "text_notice" || cardType === "news_notice") {
341
+ if (!card.card_action || typeof card.card_action !== "object") {
342
+ card.card_action = { type: 1, url: "https://work.weixin.qq.com" };
343
+ fixes.push("card_action: (missing) \u2192 { type: 1, url: \"https://work.weixin.qq.com\" }");
344
+ }
345
+ }
346
+ // ── submit_button(vote_interaction / multiple_interaction 必填) ──
347
+ if (cardType === "vote_interaction" || cardType === "multiple_interaction") {
348
+ if (!card.submit_button || typeof card.submit_button !== "object") {
349
+ card.submit_button = { text: "提交", key: "submit_" + cardType + "_" + Date.now() };
350
+ fixes.push("submit_button: (missing) \u2192 auto-generated");
351
+ }
352
+ }
353
+ // ── 核心业务字段(无法凭空补全,仅告警) ────────────────────────────
354
+ if (cardType === "button_interaction") {
355
+ if (!Array.isArray(card.button_list) || card.button_list.length === 0) {
356
+ warnings.push("button_list is missing or empty (required for button_interaction)");
357
+ }
358
+ }
359
+ if (cardType === "vote_interaction") {
360
+ if (!card.checkbox || typeof card.checkbox !== "object") {
361
+ warnings.push("checkbox is missing (required for vote_interaction)");
362
+ }
363
+ }
364
+ if (cardType === "multiple_interaction") {
365
+ if (!Array.isArray(card.select_list) || card.select_list.length === 0) {
366
+ warnings.push("select_list is missing or empty (required for multiple_interaction)");
367
+ }
368
+ }
369
+ if (fixes.length > 0) {
370
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] validateAndFixRequiredFields: " + fixes.length + " fix(es): " + fixes.join("; "));
371
+ }
372
+ if (warnings.length > 0) {
373
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] validateAndFixRequiredFields: " + warnings.length + " warning(s): " + warnings.join("; "));
374
+ }
375
+ return card;
376
+ }
377
+ // ============================================================================
378
+ // 简化格式 → 企微 API 格式转换(vote_interaction / multiple_interaction)
379
+ // ============================================================================
380
+ /**
381
+ * 生成唯一的 question_key / submit_button.key。
382
+ */
383
+ function generateKey(prefix) {
384
+ var rand = Math.random().toString(36).slice(2, 6);
385
+ return prefix + "_" + Date.now() + "_" + rand;
386
+ }
387
+ /**
388
+ * 将 vote_interaction 的简化格式转换为企微 API 格式。
389
+ *
390
+ * 简化格式字段:
391
+ * title → main_title.title
392
+ * description → main_title.desc
393
+ * options → checkbox.option_list(每个 {id, text} 直接透传)
394
+ * mode → checkbox.mode(0=单选 1=多选)
395
+ * submit_text → submit_button.text
396
+ *
397
+ * 代码自动生成:checkbox.question_key, submit_button.key
398
+ *
399
+ * 如果 LLM 已输出了合法的 API 原始格式(有 checkbox.option_list),则跳过转换直接透传。
400
+ */
401
+ function transformVoteInteraction(card, log) {
402
+ var _a;
403
+ // 防御性:如果已经是合法 API 格式,跳过
404
+ var existingCheckbox = card.checkbox;
405
+ if (existingCheckbox && typeof existingCheckbox === "object" && Array.isArray(existingCheckbox.option_list)) {
406
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformVoteInteraction: already has checkbox.option_list, skipping transform");
407
+ return card;
408
+ }
409
+ // 提取 options(简化格式的核心字段)
410
+ var options = card.options;
411
+ if (!Array.isArray(options) || options.length === 0) {
412
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformVoteInteraction: no \"options\" array found, skipping transform");
413
+ return card;
414
+ }
415
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformVoteInteraction: transforming simplified format \u2192 API format");
416
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformVoteInteraction: input=" + JSON.stringify(card));
417
+ // ── 构建 main_title ──
418
+ var title = card.title;
419
+ var description = card.description;
420
+ if (title || description) {
421
+ card.main_title = __assign(__assign({}, (title ? { title: title } : {})), (description ? { desc: description } : {}));
422
+ delete card.title;
423
+ delete card.description;
424
+ }
425
+ // ── 构建 checkbox(最多 20 个选项) ──
426
+ var mode = (_a = coerceCheckboxMode(card.mode)) !== null && _a !== void 0 ? _a : 0;
427
+ var questionKey = generateKey("vote");
428
+ var clampedOptions = options.slice(0, 20);
429
+ if (options.length > 20) {
430
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformVoteInteraction: options count " + options.length + " exceeds max 20, clamped to 20");
431
+ }
432
+ card.checkbox = {
433
+ question_key: questionKey,
434
+ mode: mode,
435
+ option_list: clampedOptions.map(function (opt) {
436
+ var _a, _b, _c, _d, _e;
437
+ return ({
438
+ id: String((_b = (_a = opt.id) !== null && _a !== void 0 ? _a : opt.value) !== null && _b !== void 0 ? _b : "opt_" + Math.random().toString(36).slice(2, 6)),
439
+ text: String((_e = (_d = (_c = opt.text) !== null && _c !== void 0 ? _c : opt.label) !== null && _d !== void 0 ? _d : opt.name) !== null && _e !== void 0 ? _e : "")
440
+ });
441
+ })
442
+ };
443
+ delete card.options;
444
+ delete card.mode;
445
+ // ── 构建 submit_button ──
446
+ var submitText = card.submit_text || "提交";
447
+ card.submit_button = {
448
+ text: submitText,
449
+ key: generateKey("submit_vote")
450
+ };
451
+ delete card.submit_text;
452
+ // ── 清理 LLM 可能杜撰的无效字段 ──
453
+ delete card.vote_question;
454
+ delete card.vote_option;
455
+ delete card.vote_options;
456
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformVoteInteraction: output=" + JSON.stringify(card));
457
+ return card;
458
+ }
459
+ /**
460
+ * 将 multiple_interaction 的简化格式转换为企微 API 格式。
461
+ *
462
+ * 简化格式字段:
463
+ * title → main_title.title
464
+ * description → main_title.desc
465
+ * selectors → select_list(每个 {title, options: [{id, text}]} → {question_key, title, option_list})
466
+ * submit_text → submit_button.text
467
+ *
468
+ * 代码自动生成:select_list[].question_key, submit_button.key
469
+ *
470
+ * 如果 LLM 已输出了合法的 API 原始格式(有 select_list[0].option_list),则跳过转换直接透传。
471
+ */
472
+ function transformMultipleInteraction(card, log) {
473
+ var _a;
474
+ // 防御性:如果已经是合法 API 格式,跳过
475
+ var existingSelectList = card.select_list;
476
+ if (Array.isArray(existingSelectList) &&
477
+ existingSelectList.length > 0 &&
478
+ Array.isArray((_a = existingSelectList[0]) === null || _a === void 0 ? void 0 : _a.option_list)) {
479
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformMultipleInteraction: already has select_list[].option_list, skipping transform");
480
+ return card;
481
+ }
482
+ // 提取 selectors(简化格式的核心字段)
483
+ var selectors = card.selectors;
484
+ if (!Array.isArray(selectors) || selectors.length === 0) {
485
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformMultipleInteraction: no \"selectors\" array found, skipping transform");
486
+ return card;
487
+ }
488
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformMultipleInteraction: transforming simplified format \u2192 API format");
489
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformMultipleInteraction: input=" + JSON.stringify(card));
490
+ // ── 构建 main_title ──
491
+ var title = card.title;
492
+ var description = card.description;
493
+ if (title || description) {
494
+ card.main_title = __assign(__assign({}, (title ? { title: title } : {})), (description ? { desc: description } : {}));
495
+ delete card.title;
496
+ delete card.description;
497
+ }
498
+ // ── 构建 select_list(最多 3 个选择器,每个最多 10 个选项) ──
499
+ var clampedSelectors = selectors.slice(0, 3);
500
+ if (selectors.length > 3) {
501
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformMultipleInteraction: selectors count " + selectors.length + " exceeds max 3, clamped to 3");
502
+ }
503
+ card.select_list = clampedSelectors.map(function (sel, idx) {
504
+ var _a, _b, _c;
505
+ var selectorOptions = ((_a = sel.options) !== null && _a !== void 0 ? _a : []).slice(0, 10);
506
+ return {
507
+ question_key: generateKey("sel_" + idx),
508
+ title: String((_c = (_b = sel.title) !== null && _b !== void 0 ? _b : sel.label) !== null && _c !== void 0 ? _c : "\u9009\u62E9" + (idx + 1)),
509
+ option_list: selectorOptions.map(function (opt) {
510
+ var _a, _b, _c, _d, _e;
511
+ return ({
512
+ id: String((_b = (_a = opt.id) !== null && _a !== void 0 ? _a : opt.value) !== null && _b !== void 0 ? _b : "opt_" + Math.random().toString(36).slice(2, 6)),
513
+ text: String((_e = (_d = (_c = opt.text) !== null && _c !== void 0 ? _c : opt.label) !== null && _d !== void 0 ? _d : opt.name) !== null && _e !== void 0 ? _e : "")
514
+ });
515
+ })
516
+ };
517
+ });
518
+ delete card.selectors;
519
+ // ── 构建 submit_button ──
520
+ var submitText = card.submit_text || "提交";
521
+ card.submit_button = {
522
+ text: submitText,
523
+ key: generateKey("submit_multi")
524
+ };
525
+ delete card.submit_text;
526
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] transformMultipleInteraction: output=" + JSON.stringify(card));
527
+ return card;
528
+ }
529
+ /**
530
+ * 对 vote_interaction / multiple_interaction 执行简化格式转换。
531
+ * 其他 card_type 直接跳过。
532
+ */
533
+ function transformSimplifiedCard(card, log) {
534
+ var cardType = card.card_type;
535
+ if (cardType === "vote_interaction") {
536
+ return transformVoteInteraction(card, log);
537
+ }
538
+ if (cardType === "multiple_interaction") {
539
+ return transformMultipleInteraction(card, log);
540
+ }
541
+ return card;
542
+ }
543
+ /**
544
+ * 匹配 markdown 代码块的正则表达式
545
+ * 支持 ```json ... ``` 和 ``` ... ``` 两种格式
546
+ */
547
+ var CODE_BLOCK_RE = /```(?:json)?\s*\n([\s\S]*?)\n```/g;
548
+ /**
549
+ * 匹配已闭合的代码块(含 card_type 关键词,用于中间帧遮罩)
550
+ * 与 CODE_BLOCK_RE 相同,但用于 maskTemplateCardBlocks 中单独实例化
551
+ */
552
+ var CLOSED_BLOCK_RE = /```(?:json)?\s*\n([\s\S]*?)\n```/g;
553
+ /**
554
+ * 匹配未闭合的代码块尾部(LLM 正在输出中的代码块)
555
+ * 以 ```json 或 ``` 开头,后面有内容但没有闭合的 ```
556
+ */
557
+ var UNCLOSED_BLOCK_RE = /```(?:json)?\s*\n[\s\S]*$/;
558
+ /**
559
+ * 从文本中提取模板卡片 JSON 代码块
560
+ *
561
+ * 匹配规则:
562
+ * 1. 匹配所有 ```json ... ``` 或 ``` ... ``` 格式的代码块
563
+ * 2. 尝试 JSON.parse 解析代码块内容
564
+ * 3. 检查解析结果中是否包含合法的 card_type 字段
565
+ * 4. 合法的卡片从原文中移除,不合法的保留
566
+ *
567
+ * @param text - LLM 回复的完整文本
568
+ * @returns 提取结果,包含卡片列表和剩余文本
569
+ */
570
+ function extractTemplateCards(text, log) {
571
+ var cards = [];
572
+ /** 需要从原文中移除的代码块(记录完整匹配内容) */
573
+ var blocksToRemove = [];
574
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] extractTemplateCards called, textLength=" + text.length);
575
+ var match;
576
+ // 重置正则的 lastIndex,确保从头匹配
577
+ CODE_BLOCK_RE.lastIndex = 0;
578
+ var blockIndex = 0;
579
+ while ((match = CODE_BLOCK_RE.exec(text)) !== null) {
580
+ var fullMatch = match[0];
581
+ var jsonContent = match[1].trim();
582
+ blockIndex++;
583
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] Found code block #" + blockIndex + ", length=" + fullMatch.length + ", preview=" + jsonContent.slice(0, 1000) + "...");
584
+ // 尝试解析 JSON
585
+ var parsed = void 0;
586
+ try {
587
+ parsed = JSON.parse(jsonContent);
588
+ }
589
+ catch (e) {
590
+ // JSON 解析失败,保留在原文中
591
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] Code block #" + blockIndex + " JSON parse failed: " + String(e));
592
+ continue;
593
+ }
594
+ // 检查是否包含合法的 card_type
595
+ var cardType = parsed.card_type;
596
+ if (typeof cardType !== "string" || !const_js_1.VALID_CARD_TYPES.includes(cardType)) {
597
+ // card_type 不合法,保留在原文中
598
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] Code block #" + blockIndex + " has invalid card_type=\"" + String(cardType) + "\", skipping");
599
+ continue;
600
+ }
601
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] Code block #" + blockIndex + " is valid template card, card_type=\"" + cardType + "\"");
602
+ // vote_interaction / multiple_interaction:简化格式 → API 格式转换
603
+ transformSimplifiedCard(parsed, log);
604
+ // 修正 LLM 可能输出的错误字段类型(如 checkbox.mode: "multi" → 1)
605
+ normalizeTemplateCardFields(parsed, log);
606
+ // 校验并补全必填字段(如缺失的 task_id、main_title、card_action)
607
+ validateAndFixRequiredFields(parsed, log);
608
+ // 合法的模板卡片,收集并标记移除
609
+ cards.push({
610
+ cardJson: parsed,
611
+ cardType: cardType
612
+ });
613
+ blocksToRemove.push(fullMatch);
614
+ }
615
+ // 从原文中移除已提取的代码块,生成剩余文本
616
+ var remainingText = text;
617
+ for (var _i = 0, blocksToRemove_1 = blocksToRemove; _i < blocksToRemove_1.length; _i++) {
618
+ var block = blocksToRemove_1[_i];
619
+ remainingText = remainingText.replace(block, "");
620
+ }
621
+ // 清理多余空行(连续 3 个以上换行合并为 2 个)
622
+ remainingText = remainingText.replace(/\n{3,}/g, "\n\n").trim();
623
+ log === null || log === void 0 ? void 0 : log("[template-card-parser] Extraction done: " + cards.length + " card(s) found, remainingTextLength=" + remainingText.length);
624
+ return { cards: cards, remainingText: remainingText };
625
+ }
626
+ exports.extractTemplateCards = extractTemplateCards;
627
+ /**
628
+ * 遮罩文本中的模板卡片代码块(用于流式中间帧展示)
629
+ *
630
+ * 在 LLM 流式输出过程中,累积文本可能包含:
631
+ * 1. 已闭合的模板卡片 JSON 代码块 → 替换为友好提示文本
632
+ * 2. 正在构建中的未闭合代码块 → 截断隐藏,避免 JSON 源码暴露
633
+ *
634
+ * 此函数仅做文本替换,不做 JSON 解析验证(中间帧性能优先)。
635
+ * 只要代码块内容中出现 "card_type" 关键词就认为是模板卡片并遮罩。
636
+ *
637
+ * @param text - 当前累积文本
638
+ * @returns 遮罩后的展示文本
639
+ */
640
+ function maskTemplateCardBlocks(text, log) {
641
+ var masked = text;
642
+ var closedMaskCount = 0;
643
+ var unclosedMasked = false;
644
+ // 步骤一:处理已闭合的代码块
645
+ CLOSED_BLOCK_RE.lastIndex = 0;
646
+ masked = masked.replace(CLOSED_BLOCK_RE, function (fullMatch, content) {
647
+ // 检查代码块内容是否包含 card_type 关键词
648
+ if (/["']card_type["']/.test(content)) {
649
+ closedMaskCount++;
650
+ return "\n\n📋 *正在生成卡片消息...*\n\n";
651
+ }
652
+ // 非模板卡片代码块,保留原样
653
+ return fullMatch;
654
+ });
655
+ // 步骤二:处理未闭合的代码块尾部(LLM 仍在输出中)
656
+ // 检查是否有以 ``` 开头但没有闭合的代码块
657
+ var unclosedMatch = UNCLOSED_BLOCK_RE.exec(masked);
658
+ if (unclosedMatch) {
659
+ var unclosedContent = unclosedMatch[0];
660
+ // 如果未闭合部分包含 card_type 关键词,说明正在构建模板卡片 → 截断
661
+ if (/["']card_type["']/.test(unclosedContent)) {
662
+ unclosedMasked = true;
663
+ masked = masked.slice(0, unclosedMatch.index) + "\n\n📋 *正在生成卡片消息...*";
664
+ }
665
+ }
666
+ // 有遮罩行为时才打日志,避免每帧都刷屏
667
+ if (closedMaskCount > 0 || unclosedMasked) {
668
+ // log?.(`[template-card-parser] maskTemplateCardBlocks: closedMasked=${closedMaskCount}, unclosedMasked=${unclosedMasked}, textLength=${text.length}, maskedLength=${masked.length}`);
669
+ }
670
+ return masked;
671
+ }
672
+ exports.maskTemplateCardBlocks = maskTemplateCardBlocks;