@yanhaidao/wecom 2.3.190 → 2.3.270
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/.github/workflows/release.yml +23 -4
- package/README.md +17 -6
- package/changelog/v2.3.26.md +21 -0
- package/changelog/v2.3.27.md +33 -0
- package/index.test.ts +5 -1
- package/package.json +17 -17
- package/src/agent/handler.ts +2 -0
- package/src/app/account-runtime.ts +5 -1
- package/src/app/index.ts +120 -1
- package/src/capability/mcp/tool.ts +7 -3
- package/src/channel.meta.test.ts +4 -0
- package/src/channel.ts +30 -60
- package/src/config/media.test.ts +1 -1
- package/src/config/media.ts +3 -5
- package/src/context-store.ts +264 -0
- package/src/onboarding.test.ts +42 -24
- package/src/onboarding.ts +598 -553
- package/src/outbound.test.ts +404 -2
- package/src/outbound.ts +96 -15
- package/src/runtime/dispatcher.ts +24 -5
- package/src/runtime/session-manager.test.ts +135 -0
- package/src/runtime/session-manager.ts +40 -8
- package/src/runtime/source-registry.ts +79 -0
- package/src/runtime.ts +3 -0
- package/src/target.ts +20 -8
- package/src/transport/bot-ws/media.test.ts +9 -9
- package/src/transport/bot-ws/media.ts +51 -2
- package/src/transport/bot-ws/reply.test.ts +1 -1
- package/src/transport/bot-ws/reply.ts +8 -3
- package/src/transport/bot-ws/sdk-adapter.ts +6 -6
- package/src/transport/http/registry.ts +1 -1
- package/src/types/runtime.ts +1 -0
- package/src/wecom_msg_adapter/markdown_adapter.ts +331 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 将较完整的 Markdown 降级转换为更适合企业微信 markdown_v2 的子集。
|
|
3
|
+
*
|
|
4
|
+
* 保守策略:
|
|
5
|
+
* - 保留:标题、粗体、斜体、引用、链接、行内代码、普通列表、表格
|
|
6
|
+
* - 降级:代码块、图片、任务列表、分隔线、HTML、脚注、复杂语法
|
|
7
|
+
* - 清理:多余空行、非法控制字符、过深嵌套
|
|
8
|
+
*/
|
|
9
|
+
export function toWeComMarkdownV2(markdown: unknown, maxLength = 4096): string {
|
|
10
|
+
if (!markdown) return "";
|
|
11
|
+
|
|
12
|
+
let text = String(markdown).replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
13
|
+
|
|
14
|
+
const extracted = extractInlineCodeSpans(text);
|
|
15
|
+
text = extracted.text;
|
|
16
|
+
const inlineCodeStore = extracted.store;
|
|
17
|
+
|
|
18
|
+
text = convertFencedCodeBlocks(text);
|
|
19
|
+
text = convertIndentedCodeBlocks(text);
|
|
20
|
+
text = convertImages(text);
|
|
21
|
+
text = convertTaskLists(text);
|
|
22
|
+
text = convertThematicBreaks(text);
|
|
23
|
+
text = stripHtml(text);
|
|
24
|
+
text = removeFootnotes(text);
|
|
25
|
+
text = removeUnfriendlyExtensions(text);
|
|
26
|
+
text = flattenDeepNesting(text);
|
|
27
|
+
text = normalizeTables(text);
|
|
28
|
+
text = restoreInlineCodeSpans(text, inlineCodeStore);
|
|
29
|
+
text = cleanupWhitespace(text);
|
|
30
|
+
|
|
31
|
+
if (maxLength != null && text.length > maxLength) {
|
|
32
|
+
text = truncateSafely(text, maxLength);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return text;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const INLINE_CODE_PREFIX = "\uFFF0INLINECODE";
|
|
39
|
+
const INLINE_CODE_SUFFIX = "\uFFF1";
|
|
40
|
+
|
|
41
|
+
function extractInlineCodeSpans(text: string): { text: string; store: string[] } {
|
|
42
|
+
const store: string[] = [];
|
|
43
|
+
const replaced = text.replace(/`([^`\n]+?)`/g, (_, content: string) => {
|
|
44
|
+
const idx = store.length;
|
|
45
|
+
store.push(content);
|
|
46
|
+
return `${INLINE_CODE_PREFIX}${idx}${INLINE_CODE_SUFFIX}`;
|
|
47
|
+
});
|
|
48
|
+
return { text: replaced, store };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function restoreInlineCodeSpans(text: string, store: string[]): string {
|
|
52
|
+
const re = new RegExp(`${INLINE_CODE_PREFIX}(\\d+)${INLINE_CODE_SUFFIX}`, "g");
|
|
53
|
+
return text.replace(re, (_, idxStr: string) => {
|
|
54
|
+
const idx = Number(idxStr);
|
|
55
|
+
const content = idx >= 0 && idx < store.length ? store[idx] : "";
|
|
56
|
+
return `\`${content}\``;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function convertFencedCodeBlocks(text: string): string {
|
|
61
|
+
return text.replace(/```([a-zA-Z0-9_+\-]*)\n([\s\S]*?)```/g, (_, lang: string, code: string) => {
|
|
62
|
+
const safeLang = (lang || "").trim();
|
|
63
|
+
const safeCode = String(code || "").replace(/^\n+|\n+$/g, "");
|
|
64
|
+
if (!safeCode.trim()) return "";
|
|
65
|
+
|
|
66
|
+
const title = safeLang ? `代码(${safeLang}):` : "代码:";
|
|
67
|
+
return `\n${title}\n${safeCode}\n`;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function convertIndentedCodeBlocks(text: string): string {
|
|
72
|
+
const lines = text.split("\n");
|
|
73
|
+
const out: string[] = [];
|
|
74
|
+
let buffer: string[] = [];
|
|
75
|
+
|
|
76
|
+
const flushBuffer = () => {
|
|
77
|
+
if (!buffer.length) return;
|
|
78
|
+
const block = buffer
|
|
79
|
+
.map(line => (line.startsWith(" ") ? line.slice(4) : line))
|
|
80
|
+
.join("\n")
|
|
81
|
+
.replace(/\s+$/g, "");
|
|
82
|
+
|
|
83
|
+
if (block) {
|
|
84
|
+
out.push("代码:");
|
|
85
|
+
out.push(...block.split("\n"));
|
|
86
|
+
}
|
|
87
|
+
buffer = [];
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
for (const line of lines) {
|
|
91
|
+
if (/^ \S/.test(line)) {
|
|
92
|
+
buffer.push(line);
|
|
93
|
+
} else {
|
|
94
|
+
flushBuffer();
|
|
95
|
+
out.push(line);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
flushBuffer();
|
|
100
|
+
return out.join("\n");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function convertImages(text: string): string {
|
|
104
|
+
text = text.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (_, alt: string, url: string) => {
|
|
105
|
+
const safeAlt = (alt || "").trim() || "图片";
|
|
106
|
+
const safeUrl = (url || "").trim();
|
|
107
|
+
return `[图片:${safeAlt}](${safeUrl})`;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
text = text.replace(/!\[([^\]]*)\]\[[^\]]*\]/g, (_, alt: string) => {
|
|
111
|
+
const safeAlt = (alt || "").trim() || "图片";
|
|
112
|
+
return `图片:${safeAlt}`;
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return text;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function convertTaskLists(text: string): string {
|
|
119
|
+
text = text.replace(/^(\s*[-*+]\s+)\[x\]\s+/gim, "✅ ");
|
|
120
|
+
text = text.replace(/^(\s*[-*+]\s+)\[\s\]\s+/gm, "⬜ ");
|
|
121
|
+
return text;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function convertThematicBreaks(text: string): string {
|
|
125
|
+
return text.replace(/^\s*([-*_])(\s*\1){2,}\s*$/gm, "────────");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function stripHtml(text: string): string {
|
|
129
|
+
text = text.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, "");
|
|
130
|
+
text = text.replace(/<style\b[^>]*>[\s\S]*?<\/style>/gi, "");
|
|
131
|
+
|
|
132
|
+
text = text.replace(/<br\s*\/?>/gi, "\n");
|
|
133
|
+
text = text.replace(/<\/p\s*>/gi, "\n");
|
|
134
|
+
text = text.replace(/<p\b[^>]*>/gi, "");
|
|
135
|
+
|
|
136
|
+
const simpleTags = [
|
|
137
|
+
"div", "span", "b", "strong", "i", "em", "u",
|
|
138
|
+
"font", "small", "big", "section", "article",
|
|
139
|
+
"header", "footer", "main",
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
for (const tag of simpleTags) {
|
|
143
|
+
const re = new RegExp(`</?${tag}\\b[^>]*>`, "gi");
|
|
144
|
+
text = text.replace(re, "");
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
text = text.replace(/<[^>]+>/g, "");
|
|
148
|
+
text = decodeHtmlEntities(text);
|
|
149
|
+
|
|
150
|
+
return text;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function decodeHtmlEntities(text: string): string {
|
|
154
|
+
const map: Record<string, string> = {
|
|
155
|
+
"&": "&",
|
|
156
|
+
"<": "<",
|
|
157
|
+
">": ">",
|
|
158
|
+
""": "\"",
|
|
159
|
+
"'": "'",
|
|
160
|
+
" ": " ",
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
return text.replace(/&|<|>|"|'| /g, m => map[m] ?? m);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function removeFootnotes(text: string): string {
|
|
167
|
+
text = text.replace(/^\[\^[^\]]+\]:\s+.*(?:\n(?: {2,}|\t).*)*/gm, "");
|
|
168
|
+
text = text.replace(/\[\^[^\]]+\]/g, "[注]");
|
|
169
|
+
return text;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function removeUnfriendlyExtensions(text: string): string {
|
|
173
|
+
text = text.replace(/~~(.*?)~~/g, "$1");
|
|
174
|
+
text = text.replace(/==(.*?)==/g, "$1");
|
|
175
|
+
text = text.replace(/(?<!~)~([^~\n]+)~(?!~)/g, "$1");
|
|
176
|
+
text = text.replace(/\^([^^\n]+)\^/g, "$1");
|
|
177
|
+
|
|
178
|
+
text = text.replace(
|
|
179
|
+
/```(?:mermaid|math|latex|tex|graphviz|plantuml)\n([\s\S]*?)```/gi,
|
|
180
|
+
"\n内容略(不支持的扩展块)\n",
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
return text;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function flattenDeepNesting(text: string): string {
|
|
187
|
+
const lines = text.split("\n");
|
|
188
|
+
const out: string[] = [];
|
|
189
|
+
|
|
190
|
+
for (let line of lines) {
|
|
191
|
+
if (/^\s{4,}[-*+]\s+/.test(line)) {
|
|
192
|
+
line = line.replace(/^\s+/, " ");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (/^\s*(>\s*){2,}/.test(line)) {
|
|
196
|
+
const content = line.replace(/^\s*(>\s*)+/, "");
|
|
197
|
+
line = `> ${content}`;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
out.push(line);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return out.join("\n");
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function normalizeTables(text: string): string {
|
|
207
|
+
// Pass 1: stitch broken table rows back together.
|
|
208
|
+
// Models may split a single row across multiple lines in several ways:
|
|
209
|
+
// a) first part ends without |, continuation does NOT start with |
|
|
210
|
+
// b) first part ends without |, blank line(s), continuation starts with |
|
|
211
|
+
// c) first part ends without |, blank line(s), lone | on its own line
|
|
212
|
+
// We absorb any blank lines that follow an incomplete row and keep merging
|
|
213
|
+
// until the accumulated row ends with |.
|
|
214
|
+
const rawLines = text.split("\n");
|
|
215
|
+
const stitched: string[] = [];
|
|
216
|
+
|
|
217
|
+
for (let idx = 0; idx < rawLines.length; idx++) {
|
|
218
|
+
const line = rawLines[idx]!;
|
|
219
|
+
const trimmed = line.trim();
|
|
220
|
+
const prev = stitched[stitched.length - 1];
|
|
221
|
+
const prevTrim = prev !== undefined ? prev.trim() : "";
|
|
222
|
+
const prevIsIncomplete = prevTrim.startsWith("|") && !prevTrim.endsWith("|");
|
|
223
|
+
|
|
224
|
+
if (prevIsIncomplete) {
|
|
225
|
+
if (trimmed === "") {
|
|
226
|
+
// Blank line inside a broken row — absorb it and keep waiting for the rest
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (trimmed.includes("|")) {
|
|
230
|
+
// Continuation (starting with | or not) — stitch into the pending row
|
|
231
|
+
stitched[stitched.length - 1] =
|
|
232
|
+
prev! + (trimmed.startsWith("|") ? trimmed : line);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
stitched.push(line);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Pass 2: convert each table block to plain text lines.
|
|
241
|
+
const out: string[] = [];
|
|
242
|
+
let i = 0;
|
|
243
|
+
|
|
244
|
+
while (i < stitched.length) {
|
|
245
|
+
if (looksLikeTableRow(stitched[i]!)) {
|
|
246
|
+
const tableBlock: string[] = [stitched[i]!];
|
|
247
|
+
let j = i + 1;
|
|
248
|
+
|
|
249
|
+
while (j < stitched.length && looksLikeTableRow(stitched[j]!)) {
|
|
250
|
+
tableBlock.push(stitched[j]!);
|
|
251
|
+
j += 1;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (out.length > 0 && out[out.length - 1]?.trim() !== "") {
|
|
255
|
+
out.push("");
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
out.push(...tableToPlainText(tableBlock));
|
|
259
|
+
|
|
260
|
+
if (j < stitched.length && stitched[j]?.trim() !== "") {
|
|
261
|
+
out.push("");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
i = j;
|
|
265
|
+
} else {
|
|
266
|
+
out.push(stitched[i]!);
|
|
267
|
+
i += 1;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return out.join("\n");
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function looksLikeTableRow(line: string): boolean {
|
|
275
|
+
const stripped = String(line).trim();
|
|
276
|
+
if (!stripped.startsWith("|")) return false;
|
|
277
|
+
return (stripped.match(/\|/g) || []).length >= 2;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function isTableSeparatorRow(row: string): boolean {
|
|
281
|
+
const inner = row.replace(/^\|/, "").replace(/\|$/, "");
|
|
282
|
+
return inner.split("|").every(c => /^[\s\-:]+$/.test(c) && c.includes("-"));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function extractTableCells(line: string): string[] {
|
|
286
|
+
const raw = line.trim();
|
|
287
|
+
const parts = raw.split("|");
|
|
288
|
+
const inner = raw.startsWith("|") ? parts.slice(1) : parts;
|
|
289
|
+
const cells = (raw.endsWith("|") ? inner.slice(0, -1) : inner).map(p => p.trim());
|
|
290
|
+
return cells.filter(c => c.length > 0);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function tableToPlainText(lines: string[]): string[] {
|
|
294
|
+
const result: string[] = [];
|
|
295
|
+
for (const line of lines) {
|
|
296
|
+
if (!line.trim()) continue;
|
|
297
|
+
if (isTableSeparatorRow(line.trim())) continue;
|
|
298
|
+
const cells = extractTableCells(line);
|
|
299
|
+
if (cells.length === 0) continue;
|
|
300
|
+
result.push(cells.join(" | "));
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function cleanupWhitespace(text: string): string {
|
|
306
|
+
const lines = text.split("\n").map(line => {
|
|
307
|
+
let s = line.replace(/[ \t]+$/g, "");
|
|
308
|
+
s = s.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]/g, "");
|
|
309
|
+
return s;
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
text = lines.join("\n");
|
|
313
|
+
text = text.replace(/\n{3,}/g, "\n\n");
|
|
314
|
+
return text.trim();
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function truncateSafely(text: string, maxLength: number): string {
|
|
318
|
+
if (text.length <= maxLength) return text;
|
|
319
|
+
|
|
320
|
+
const suffix = "\n\n(内容过长,已截断)";
|
|
321
|
+
const allowed = maxLength - suffix.length;
|
|
322
|
+
if (allowed <= 0) return text.slice(0, maxLength);
|
|
323
|
+
|
|
324
|
+
let truncated = text.slice(0, allowed);
|
|
325
|
+
const cut = truncated.lastIndexOf("\n");
|
|
326
|
+
if (cut > maxLength * 0.7) {
|
|
327
|
+
truncated = truncated.slice(0, cut);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
return truncated.replace(/\s+$/g, "") + suffix;
|
|
331
|
+
}
|