chatccc 0.2.182 → 0.2.183
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/package.json +1 -1
- package/src/__tests__/format-message.test.ts +316 -273
- package/src/format-message.ts +293 -214
package/package.json
CHANGED
|
@@ -1,273 +1,316 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
-
|
|
3
|
-
// Mock feishu-platform 以控制 API 行为
|
|
4
|
-
const mockGetTenantAccessToken = vi.fn();
|
|
5
|
-
const mockGetOrDownloadImage = vi.fn();
|
|
6
|
-
const mockGetMergeForwardMessages = vi.fn();
|
|
7
|
-
|
|
8
|
-
vi.mock("../feishu-platform.ts", () => ({
|
|
9
|
-
getTenantAccessToken: (...args: unknown[]) => mockGetTenantAccessToken(...args),
|
|
10
|
-
getOrDownloadImage: (...args: unknown[]) => mockGetOrDownloadImage(...args),
|
|
11
|
-
getMergeForwardMessages: (...args: unknown[]) => mockGetMergeForwardMessages(...args),
|
|
12
|
-
}));
|
|
13
|
-
|
|
14
|
-
import { formatMessageContent, formatPostContent, formatMergeForward } from "../format-message.ts";
|
|
15
|
-
|
|
16
|
-
describe("formatMessageContent", () => {
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
vi.clearAllMocks();
|
|
19
|
-
mockGetTenantAccessToken.mockResolvedValue("mock_token");
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it("
|
|
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
|
-
expect(result).toBe("
|
|
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
|
-
expect(result).toContain("
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
it("preview
|
|
246
|
-
mockGetMergeForwardMessages.mockRejectedValue(new Error("
|
|
247
|
-
|
|
248
|
-
const result = await formatMergeForward("om_mf", {
|
|
249
|
-
title: "
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
const result = await formatMergeForward("om_mf", {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
|
|
3
|
+
// Mock feishu-platform 以控制 API 行为
|
|
4
|
+
const mockGetTenantAccessToken = vi.fn();
|
|
5
|
+
const mockGetOrDownloadImage = vi.fn();
|
|
6
|
+
const mockGetMergeForwardMessages = vi.fn();
|
|
7
|
+
|
|
8
|
+
vi.mock("../feishu-platform.ts", () => ({
|
|
9
|
+
getTenantAccessToken: (...args: unknown[]) => mockGetTenantAccessToken(...args),
|
|
10
|
+
getOrDownloadImage: (...args: unknown[]) => mockGetOrDownloadImage(...args),
|
|
11
|
+
getMergeForwardMessages: (...args: unknown[]) => mockGetMergeForwardMessages(...args),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
import { formatMessageContent, formatPostContent, formatMergeForward } from "../format-message.ts";
|
|
15
|
+
|
|
16
|
+
describe("formatMessageContent mixed post images", () => {
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
vi.clearAllMocks();
|
|
19
|
+
mockGetTenantAccessToken.mockResolvedValue("mock_token");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("includes downloaded images from mixed post messages", async () => {
|
|
23
|
+
mockGetOrDownloadImage.mockResolvedValue("C:\\tmp\\img_001.png");
|
|
24
|
+
|
|
25
|
+
const result = await formatMessageContent({
|
|
26
|
+
message_id: "om_post",
|
|
27
|
+
message_type: "post",
|
|
28
|
+
content: JSON.stringify({
|
|
29
|
+
content: [[
|
|
30
|
+
{ tag: "text", text: "use this image" },
|
|
31
|
+
{ tag: "img", image_key: "img_001" },
|
|
32
|
+
]],
|
|
33
|
+
}),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(result).toBe("use this image\n[图片] C:\\tmp\\img_001.png");
|
|
37
|
+
expect(mockGetTenantAccessToken).toHaveBeenCalled();
|
|
38
|
+
expect(mockGetOrDownloadImage).toHaveBeenCalledWith("mock_token", "om_post", "img_001");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("keeps post image key when mixed post image download fails", async () => {
|
|
42
|
+
mockGetOrDownloadImage.mockRejectedValue(new Error("download failed"));
|
|
43
|
+
|
|
44
|
+
const result = await formatMessageContent({
|
|
45
|
+
message_id: "om_post",
|
|
46
|
+
message_type: "post",
|
|
47
|
+
content: JSON.stringify({
|
|
48
|
+
content: [[
|
|
49
|
+
{ tag: "text", text: "caption" },
|
|
50
|
+
{ tag: "img", image_key: "img_002" },
|
|
51
|
+
]],
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
expect(result).toBe("caption\n[图片: img_002]");
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("formatMessageContent", () => {
|
|
60
|
+
beforeEach(() => {
|
|
61
|
+
vi.clearAllMocks();
|
|
62
|
+
mockGetTenantAccessToken.mockResolvedValue("mock_token");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("解析文本消息", async () => {
|
|
66
|
+
const result = await formatMessageContent({
|
|
67
|
+
message_type: "text",
|
|
68
|
+
content: JSON.stringify({ text: "hello world" }),
|
|
69
|
+
});
|
|
70
|
+
expect(result).toBe("hello world");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("去除 HTML 标签", async () => {
|
|
74
|
+
const result = await formatMessageContent({
|
|
75
|
+
message_type: "text",
|
|
76
|
+
content: JSON.stringify({ text: "<p>你好</p><br/>世界" }),
|
|
77
|
+
});
|
|
78
|
+
expect(result).toBe("你好\n世界");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("解析 post 消息", async () => {
|
|
82
|
+
const result = await formatMessageContent({
|
|
83
|
+
message_type: "post",
|
|
84
|
+
content: JSON.stringify({
|
|
85
|
+
content: [[{ tag: "text", text: "第一条消息" }]],
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
88
|
+
expect(result).toBe("第一条消息");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("解析 post 中的代码块", async () => {
|
|
92
|
+
const result = await formatMessageContent({
|
|
93
|
+
message_type: "post",
|
|
94
|
+
content: JSON.stringify({
|
|
95
|
+
content: [[{ tag: "code_block", language: "ts", text: "const x = 1;" }]],
|
|
96
|
+
}),
|
|
97
|
+
});
|
|
98
|
+
expect(result).toBe("```ts\nconst x = 1;\n```");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("解析 media 类型返回元数据", async () => {
|
|
102
|
+
const result = await formatMessageContent({
|
|
103
|
+
message_id: "om_test",
|
|
104
|
+
message_type: "media",
|
|
105
|
+
content: JSON.stringify({ file_key: "fk_001", file_name: "test.mp4" }),
|
|
106
|
+
});
|
|
107
|
+
expect(result).toContain("[视频]");
|
|
108
|
+
expect(result).toContain("om_test");
|
|
109
|
+
expect(result).toContain("fk_001");
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("解析 file 类型返回元数据", async () => {
|
|
113
|
+
const result = await formatMessageContent({
|
|
114
|
+
message_id: "om_test",
|
|
115
|
+
message_type: "file",
|
|
116
|
+
content: JSON.stringify({ file_key: "fk_001", file_name: "doc.pdf" }),
|
|
117
|
+
});
|
|
118
|
+
expect(result).toContain("[文件]");
|
|
119
|
+
expect(result).toContain("om_test");
|
|
120
|
+
expect(result).toContain("fk_001");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("未知类型返回原始 JSON", async () => {
|
|
124
|
+
const result = await formatMessageContent({
|
|
125
|
+
message_type: "sticker",
|
|
126
|
+
content: JSON.stringify({ sticker_id: "stk_001" }),
|
|
127
|
+
});
|
|
128
|
+
expect(result).toBe(JSON.stringify({ sticker_id: "stk_001" }));
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("content 为空时返回空字符串", async () => {
|
|
132
|
+
const result = await formatMessageContent({
|
|
133
|
+
message_type: "text",
|
|
134
|
+
});
|
|
135
|
+
expect(result).toBe("");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("content JSON 解析失败返回空字符串", async () => {
|
|
139
|
+
const result = await formatMessageContent({
|
|
140
|
+
message_type: "text",
|
|
141
|
+
content: "not valid json",
|
|
142
|
+
});
|
|
143
|
+
expect(result).toBe("");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("merge_forward 的 content 为空时仍能走 API 路径", async () => {
|
|
147
|
+
mockGetMergeForwardMessages.mockResolvedValue([
|
|
148
|
+
{ message_id: "om_root" },
|
|
149
|
+
{
|
|
150
|
+
message_id: "om_1",
|
|
151
|
+
msg_type: "text",
|
|
152
|
+
body: { content: JSON.stringify({ text: "hello" }) },
|
|
153
|
+
sender: { id: "ou_001" },
|
|
154
|
+
upper_message_id: "om_root",
|
|
155
|
+
},
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
const result = await formatMessageContent({
|
|
159
|
+
message_id: "om_mf",
|
|
160
|
+
message_type: "merge_forward",
|
|
161
|
+
content: "", // 空字符串
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
expect(result).toContain("[合并转发: 聊天记录]");
|
|
165
|
+
expect(result).toContain("hello");
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe("formatMergeForward", () => {
|
|
170
|
+
beforeEach(() => {
|
|
171
|
+
vi.clearAllMocks();
|
|
172
|
+
mockGetTenantAccessToken.mockResolvedValue("mock_token");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const preview = [
|
|
176
|
+
{
|
|
177
|
+
content: "第一条消息",
|
|
178
|
+
sender: { id: "ou_001", name: "张三", avatar_url: "" },
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
content: "第二条消息",
|
|
182
|
+
sender: { id: "ou_002", name: "李四", avatar_url: "" },
|
|
183
|
+
},
|
|
184
|
+
];
|
|
185
|
+
|
|
186
|
+
it("API 成功:格式化子消息列表", async () => {
|
|
187
|
+
mockGetMergeForwardMessages.mockResolvedValue([
|
|
188
|
+
// 第一个 item: 合并转发消息自身(无 upper_message_id),会被跳过
|
|
189
|
+
{ message_id: "om_root", msg_type: "merge_forward", body: { content: "{}" } },
|
|
190
|
+
{
|
|
191
|
+
message_id: "om_1",
|
|
192
|
+
msg_type: "text",
|
|
193
|
+
body: { content: JSON.stringify({ text: "你好" }) },
|
|
194
|
+
sender: { id: "ou_001" },
|
|
195
|
+
upper_message_id: "om_root",
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
message_id: "om_2",
|
|
199
|
+
msg_type: "text",
|
|
200
|
+
body: { content: JSON.stringify({ text: "在吗" }) },
|
|
201
|
+
sender: { id: "ou_002" },
|
|
202
|
+
upper_message_id: "om_root",
|
|
203
|
+
},
|
|
204
|
+
]);
|
|
205
|
+
|
|
206
|
+
const result = await formatMergeForward("om_mf", {
|
|
207
|
+
title: "聊天记录",
|
|
208
|
+
preview,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(result).toContain("[合并转发: 聊天记录]");
|
|
212
|
+
expect(result).toContain("张三: 你好");
|
|
213
|
+
expect(result).toContain("李四: 在吗");
|
|
214
|
+
expect(mockGetMergeForwardMessages).toHaveBeenCalledWith("mock_token", "om_mf");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("API 成功:跳过 merge_forward 自身 item", async () => {
|
|
218
|
+
mockGetMergeForwardMessages.mockResolvedValue([
|
|
219
|
+
{ message_id: "om_root", msg_type: "merge_forward", body: { content: "{}" } },
|
|
220
|
+
]);
|
|
221
|
+
|
|
222
|
+
const result = await formatMergeForward("om_mf", {
|
|
223
|
+
title: "空聊天记录",
|
|
224
|
+
preview: [],
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// API 返回了 items 但只有根消息本身(无子消息),应该降级到 preview(也为空)
|
|
228
|
+
// → 返回原始 JSON
|
|
229
|
+
expect(result).toBe(JSON.stringify({ title: "空聊天记录", preview: [] }));
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("API 失败:降级使用 preview", async () => {
|
|
233
|
+
mockGetMergeForwardMessages.mockRejectedValue(new Error("permission denied"));
|
|
234
|
+
|
|
235
|
+
const result = await formatMergeForward("om_mf", {
|
|
236
|
+
title: "聊天记录",
|
|
237
|
+
preview,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
expect(result).toContain("[合并转发: 聊天记录]");
|
|
241
|
+
expect(result).toContain("张三: 第一条消息");
|
|
242
|
+
expect(result).toContain("李四: 第二条消息");
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it("API 失败 + preview 为空:返回原始 JSON", async () => {
|
|
246
|
+
mockGetMergeForwardMessages.mockRejectedValue(new Error("permission denied"));
|
|
247
|
+
|
|
248
|
+
const result = await formatMergeForward("om_mf", {
|
|
249
|
+
title: "聊天记录",
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
expect(result).toBe(JSON.stringify({ title: "聊天记录" }));
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("chat_name 出现在标题中", async () => {
|
|
256
|
+
mockGetMergeForwardMessages.mockRejectedValue(new Error("no permission"));
|
|
257
|
+
|
|
258
|
+
const result = await formatMergeForward("om_mf", {
|
|
259
|
+
title: "聊天记录",
|
|
260
|
+
chat_name: "技术群",
|
|
261
|
+
preview,
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
expect(result).toContain("[合并转发: 聊天记录 (技术群)]");
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it("发送者名称从 preview 映射到 API items", async () => {
|
|
268
|
+
// API 返回的 sender 只有 id 没有 name,应该从 preview 映射名称
|
|
269
|
+
mockGetMergeForwardMessages.mockResolvedValue([
|
|
270
|
+
{ message_id: "om_root" },
|
|
271
|
+
{
|
|
272
|
+
message_id: "om_1",
|
|
273
|
+
msg_type: "text",
|
|
274
|
+
body: { content: JSON.stringify({ text: "测试" }) },
|
|
275
|
+
sender: { id: "ou_001" }, // 只有 id,无 name
|
|
276
|
+
upper_message_id: "om_root",
|
|
277
|
+
},
|
|
278
|
+
]);
|
|
279
|
+
|
|
280
|
+
const result = await formatMergeForward("om_mf", {
|
|
281
|
+
title: "测试",
|
|
282
|
+
preview: [{ content: "", sender: { id: "ou_001", name: "张三" } }],
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
expect(result).toContain("张三: 测试");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("preview 发送者为空时使用默认名称", async () => {
|
|
289
|
+
mockGetMergeForwardMessages.mockRejectedValue(new Error("fail"));
|
|
290
|
+
|
|
291
|
+
const result = await formatMergeForward("om_mf", {
|
|
292
|
+
title: "测试",
|
|
293
|
+
preview: [{ content: "消息内容" }],
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
expect(result).toContain("未知用户: 消息内容");
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("嵌套合并转发递归深度限制", async () => {
|
|
300
|
+
// MAX_DEPTH=3,从 depth=3 开始调用应立即返回
|
|
301
|
+
const result = await formatMergeForward("om_mf", {}, 3);
|
|
302
|
+
expect(result).toBe("[合并转发: 超出最大嵌套深度 3]");
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
describe("formatPostContent", () => {
|
|
307
|
+
it("返回空段落数组时为空字符串", () => {
|
|
308
|
+
const result = formatPostContent({});
|
|
309
|
+
expect(result).toBe("");
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
it("跳过非数组元素", () => {
|
|
313
|
+
const result = formatPostContent({ content: [null, undefined, "string"] });
|
|
314
|
+
expect(result).toBe("");
|
|
315
|
+
});
|
|
316
|
+
});
|
package/src/format-message.ts
CHANGED
|
@@ -1,214 +1,293 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* format-message.ts — 飞书消息内容格式化
|
|
3
|
-
*
|
|
4
|
-
* 从 index.ts 中提取,独立模块便于测试。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { cardJsonToPlainText } from "./card-plain-text.ts";
|
|
8
|
-
import { ts } from "./config.ts";
|
|
9
|
-
import {
|
|
10
|
-
getTenantAccessToken,
|
|
11
|
-
getOrDownloadImage,
|
|
12
|
-
getMergeForwardMessages,
|
|
13
|
-
} from "./feishu-platform.ts";
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* 根据消息类型格式化消息内容为可读文本。
|
|
17
|
-
*/
|
|
18
|
-
export async function formatMessageContent(message: {
|
|
19
|
-
message_id?: string;
|
|
20
|
-
message_type?: string;
|
|
21
|
-
content?: string;
|
|
22
|
-
}): Promise<string> {
|
|
23
|
-
const contentStr = message.content ?? "{}";
|
|
24
|
-
let content: Record<string, unknown>;
|
|
25
|
-
try { content = JSON.parse(contentStr); } catch {
|
|
26
|
-
// merge_forward 消息的 content 可能为空字符串,但可通过 message_id 调 API 获取子消息
|
|
27
|
-
if (message.message_type === "merge_forward") {
|
|
28
|
-
content = {};
|
|
29
|
-
} else {
|
|
30
|
-
return "";
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (message.message_type === "text") {
|
|
35
|
-
let text = (content.text ?? "") as string;
|
|
36
|
-
text = text.replace(/<\/?p[^>]*>/gi, "");
|
|
37
|
-
text = text.replace(/<br\s*\/?>/gi, "\n");
|
|
38
|
-
text = text.replace(/ /gi, " ");
|
|
39
|
-
return text.trim();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (message.message_type === "post") {
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
if (message.message_type === "image") {
|
|
47
|
-
const imageKey = content.image_key as string | undefined;
|
|
48
|
-
const messageId = message.message_id;
|
|
49
|
-
if (!imageKey || !messageId) return contentStr;
|
|
50
|
-
try {
|
|
51
|
-
const token = await getTenantAccessToken();
|
|
52
|
-
const localPath = await getOrDownloadImage(token, messageId, imageKey);
|
|
53
|
-
return `[图片] ${localPath}`;
|
|
54
|
-
} catch (err) {
|
|
55
|
-
console.error(
|
|
56
|
-
`[${ts()}] [IMAGE] download failed for ${imageKey}: ${(err as Error).message}`,
|
|
57
|
-
);
|
|
58
|
-
return `[图片: ${imageKey}]`;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (message.message_type === "media") {
|
|
63
|
-
const fileKey = content.file_key as string | undefined;
|
|
64
|
-
const fileName = (content.file_name as string) || "video.mp4";
|
|
65
|
-
const messageId = message.message_id;
|
|
66
|
-
if (!fileKey || !messageId) return contentStr;
|
|
67
|
-
return `[视频] message_id=${messageId} file_key=${fileKey} file_name=${fileName}`;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (message.message_type === "file") {
|
|
71
|
-
const fileKey = content.file_key as string | undefined;
|
|
72
|
-
const fileName = (content.file_name as string) || "download.bin";
|
|
73
|
-
const messageId = message.message_id;
|
|
74
|
-
if (!fileKey || !messageId) return contentStr;
|
|
75
|
-
return `[文件] message_id=${messageId} file_key=${fileKey} file_name=${fileName}`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (message.message_type === "interactive") {
|
|
79
|
-
const raw = JSON.stringify(content);
|
|
80
|
-
const text = cardJsonToPlainText(raw);
|
|
81
|
-
if (text) return `[卡片] ${text}`;
|
|
82
|
-
return contentStr;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (message.message_type === "merge_forward") {
|
|
86
|
-
return formatMergeForward(message.message_id ?? "", content);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// 其他类型(audio, sticker 等)直接给原始 JSON
|
|
90
|
-
return contentStr;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function formatPostContent(content: Record<string, unknown>): string {
|
|
94
|
-
const paragraphs = content
|
|
95
|
-
if (!Array.isArray(paragraphs)) return "";
|
|
96
|
-
|
|
97
|
-
const parts: string[] = [];
|
|
98
|
-
for (const line of paragraphs) {
|
|
99
|
-
if (!Array.isArray(line)) continue;
|
|
100
|
-
for (const elem of line) {
|
|
101
|
-
const el = elem as Record<string, unknown>;
|
|
102
|
-
if (!el || typeof el !== "object") continue;
|
|
103
|
-
const
|
|
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
|
-
const
|
|
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
|
-
|
|
1
|
+
/**
|
|
2
|
+
* format-message.ts — 飞书消息内容格式化
|
|
3
|
+
*
|
|
4
|
+
* 从 index.ts 中提取,独立模块便于测试。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { cardJsonToPlainText } from "./card-plain-text.ts";
|
|
8
|
+
import { ts } from "./config.ts";
|
|
9
|
+
import {
|
|
10
|
+
getTenantAccessToken,
|
|
11
|
+
getOrDownloadImage,
|
|
12
|
+
getMergeForwardMessages,
|
|
13
|
+
} from "./feishu-platform.ts";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 根据消息类型格式化消息内容为可读文本。
|
|
17
|
+
*/
|
|
18
|
+
export async function formatMessageContent(message: {
|
|
19
|
+
message_id?: string;
|
|
20
|
+
message_type?: string;
|
|
21
|
+
content?: string;
|
|
22
|
+
}): Promise<string> {
|
|
23
|
+
const contentStr = message.content ?? "{}";
|
|
24
|
+
let content: Record<string, unknown>;
|
|
25
|
+
try { content = JSON.parse(contentStr); } catch {
|
|
26
|
+
// merge_forward 消息的 content 可能为空字符串,但可通过 message_id 调 API 获取子消息
|
|
27
|
+
if (message.message_type === "merge_forward") {
|
|
28
|
+
content = {};
|
|
29
|
+
} else {
|
|
30
|
+
return "";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (message.message_type === "text") {
|
|
35
|
+
let text = (content.text ?? "") as string;
|
|
36
|
+
text = text.replace(/<\/?p[^>]*>/gi, "");
|
|
37
|
+
text = text.replace(/<br\s*\/?>/gi, "\n");
|
|
38
|
+
text = text.replace(/ /gi, " ");
|
|
39
|
+
return text.trim();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (message.message_type === "post") {
|
|
43
|
+
return formatPostContentWithImages(content, message.message_id);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (message.message_type === "image") {
|
|
47
|
+
const imageKey = content.image_key as string | undefined;
|
|
48
|
+
const messageId = message.message_id;
|
|
49
|
+
if (!imageKey || !messageId) return contentStr;
|
|
50
|
+
try {
|
|
51
|
+
const token = await getTenantAccessToken();
|
|
52
|
+
const localPath = await getOrDownloadImage(token, messageId, imageKey);
|
|
53
|
+
return `[图片] ${localPath}`;
|
|
54
|
+
} catch (err) {
|
|
55
|
+
console.error(
|
|
56
|
+
`[${ts()}] [IMAGE] download failed for ${imageKey}: ${(err as Error).message}`,
|
|
57
|
+
);
|
|
58
|
+
return `[图片: ${imageKey}]`;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (message.message_type === "media") {
|
|
63
|
+
const fileKey = content.file_key as string | undefined;
|
|
64
|
+
const fileName = (content.file_name as string) || "video.mp4";
|
|
65
|
+
const messageId = message.message_id;
|
|
66
|
+
if (!fileKey || !messageId) return contentStr;
|
|
67
|
+
return `[视频] message_id=${messageId} file_key=${fileKey} file_name=${fileName}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (message.message_type === "file") {
|
|
71
|
+
const fileKey = content.file_key as string | undefined;
|
|
72
|
+
const fileName = (content.file_name as string) || "download.bin";
|
|
73
|
+
const messageId = message.message_id;
|
|
74
|
+
if (!fileKey || !messageId) return contentStr;
|
|
75
|
+
return `[文件] message_id=${messageId} file_key=${fileKey} file_name=${fileName}`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (message.message_type === "interactive") {
|
|
79
|
+
const raw = JSON.stringify(content);
|
|
80
|
+
const text = cardJsonToPlainText(raw);
|
|
81
|
+
if (text) return `[卡片] ${text}`;
|
|
82
|
+
return contentStr;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (message.message_type === "merge_forward") {
|
|
86
|
+
return formatMergeForward(message.message_id ?? "", content);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 其他类型(audio, sticker 等)直接给原始 JSON
|
|
90
|
+
return contentStr;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function formatPostContent(content: Record<string, unknown>): string {
|
|
94
|
+
const paragraphs = getPostParagraphs(content);
|
|
95
|
+
if (!Array.isArray(paragraphs)) return "";
|
|
96
|
+
|
|
97
|
+
const parts: string[] = [];
|
|
98
|
+
for (const line of paragraphs) {
|
|
99
|
+
if (!Array.isArray(line)) continue;
|
|
100
|
+
for (const elem of line) {
|
|
101
|
+
const el = elem as Record<string, unknown>;
|
|
102
|
+
if (!el || typeof el !== "object") continue;
|
|
103
|
+
const text = formatPostTextElement(el);
|
|
104
|
+
if (text) parts.push(text);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return parts.join("\n").trim();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getPostParagraphs(content: Record<string, unknown>): unknown[][] {
|
|
111
|
+
const direct = content.content;
|
|
112
|
+
if (Array.isArray(direct)) return direct as unknown[][];
|
|
113
|
+
|
|
114
|
+
for (const locale of ["zh_cn", "en_us", "ja_jp"]) {
|
|
115
|
+
const localized = content[locale] as Record<string, unknown> | undefined;
|
|
116
|
+
if (localized && Array.isArray(localized.content)) {
|
|
117
|
+
return localized.content as unknown[][];
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function formatPostTextElement(el: Record<string, unknown>): string {
|
|
125
|
+
const t = typeof el.text === "string" ? el.text : "";
|
|
126
|
+
|
|
127
|
+
if (el.tag === "code_block") {
|
|
128
|
+
const lang = typeof el.language === "string" ? el.language : "";
|
|
129
|
+
return "```" + lang + "\n" + t + "\n```";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (el.tag === "p" || el.tag === "text") {
|
|
133
|
+
return t;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return "";
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function getPostImageKey(el: Record<string, unknown>): string | undefined {
|
|
140
|
+
const imageKey = el.image_key;
|
|
141
|
+
if (typeof imageKey === "string" && imageKey.trim()) return imageKey;
|
|
142
|
+
|
|
143
|
+
if (el.tag === "img") {
|
|
144
|
+
const fileKey = el.file_key;
|
|
145
|
+
if (typeof fileKey === "string" && fileKey.trim()) return fileKey;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function formatPostContentWithImages(
|
|
152
|
+
content: Record<string, unknown>,
|
|
153
|
+
messageId?: string,
|
|
154
|
+
): Promise<string> {
|
|
155
|
+
const paragraphs = getPostParagraphs(content);
|
|
156
|
+
if (!Array.isArray(paragraphs)) return "";
|
|
157
|
+
|
|
158
|
+
const parts: string[] = [];
|
|
159
|
+
for (const line of paragraphs) {
|
|
160
|
+
if (!Array.isArray(line)) continue;
|
|
161
|
+
for (const elem of line) {
|
|
162
|
+
const el = elem as Record<string, unknown>;
|
|
163
|
+
if (!el || typeof el !== "object") continue;
|
|
164
|
+
|
|
165
|
+
const text = formatPostTextElement(el);
|
|
166
|
+
if (text) parts.push(text);
|
|
167
|
+
|
|
168
|
+
const imageKey = getPostImageKey(el);
|
|
169
|
+
if (imageKey) {
|
|
170
|
+
parts.push(await formatPostImageElement(messageId, imageKey));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return parts.join("\n").trim();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function formatPostImageElement(
|
|
178
|
+
messageId: string | undefined,
|
|
179
|
+
imageKey: string,
|
|
180
|
+
): Promise<string> {
|
|
181
|
+
if (!messageId) return `[图片: ${imageKey}]`;
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const token = await getTenantAccessToken();
|
|
185
|
+
const localPath = await getOrDownloadImage(token, messageId, imageKey);
|
|
186
|
+
return `[图片] ${localPath}`;
|
|
187
|
+
} catch (err) {
|
|
188
|
+
console.error(
|
|
189
|
+
`[${ts()}] [IMAGE] download failed for post image ${imageKey}: ${(err as Error).message}`,
|
|
190
|
+
);
|
|
191
|
+
return `[图片: ${imageKey}]`;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ---------------------------------------------------------------------------
|
|
196
|
+
// 合并转发消息格式化
|
|
197
|
+
// ---------------------------------------------------------------------------
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 格式化合并转发消息。
|
|
201
|
+
*
|
|
202
|
+
* 三阶段降级策略:
|
|
203
|
+
* 1. 调用 GET /im/v1/messages/{messageId} 获取完整子消息列表
|
|
204
|
+
* 2. API 失败时降级使用 content.preview 字段
|
|
205
|
+
* 3. preview 也为空时返回原始 JSON
|
|
206
|
+
*
|
|
207
|
+
* 递归深度限制 MAX_DEPTH=3,避免嵌套合并转发 API 爆炸。
|
|
208
|
+
*/
|
|
209
|
+
export async function formatMergeForward(
|
|
210
|
+
messageId: string,
|
|
211
|
+
content: Record<string, unknown>,
|
|
212
|
+
depth: number = 0,
|
|
213
|
+
): Promise<string> {
|
|
214
|
+
const MAX_DEPTH = 3;
|
|
215
|
+
if (depth >= MAX_DEPTH) {
|
|
216
|
+
return `[合并转发: 超出最大嵌套深度 ${MAX_DEPTH}]`;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const title = (content.title as string) || "聊天记录";
|
|
220
|
+
const chatName = (content.chat_name as string) || "";
|
|
221
|
+
const header = `[合并转发: ${title}${chatName ? ` (${chatName})` : ""}]`;
|
|
222
|
+
|
|
223
|
+
// 从 preview 构建 sender ID → name 映射表
|
|
224
|
+
const senderNameMap = new Map<string, string>();
|
|
225
|
+
const preview = content.preview;
|
|
226
|
+
if (Array.isArray(preview)) {
|
|
227
|
+
for (const entry of preview) {
|
|
228
|
+
const e = entry as Record<string, unknown>;
|
|
229
|
+
const s = e.sender as Record<string, unknown> | undefined;
|
|
230
|
+
if (s && typeof s.id === "string" && typeof s.name === "string") {
|
|
231
|
+
senderNameMap.set(s.id, s.name);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const lines: string[] = [];
|
|
237
|
+
let usedApi = false;
|
|
238
|
+
|
|
239
|
+
// Phase 1: 尝试通过 API 获取完整子消息列表
|
|
240
|
+
try {
|
|
241
|
+
const token = await getTenantAccessToken();
|
|
242
|
+
const items = await getMergeForwardMessages(token, messageId);
|
|
243
|
+
|
|
244
|
+
// 跳过第一个 item(合并转发消息自身,无 upper_message_id)
|
|
245
|
+
const subItems = items.filter((item) => item.upper_message_id);
|
|
246
|
+
|
|
247
|
+
if (subItems.length > 0) {
|
|
248
|
+
for (const item of subItems) {
|
|
249
|
+
const senderId = item.sender?.id ?? "unknown";
|
|
250
|
+
const senderName = senderNameMap.get(senderId) ?? senderId;
|
|
251
|
+
|
|
252
|
+
const subMsgType = item.msg_type ?? "";
|
|
253
|
+
const subContent = item.body?.content ?? "{}";
|
|
254
|
+
|
|
255
|
+
try {
|
|
256
|
+
const formatted = await formatMessageContent({
|
|
257
|
+
message_id: item.message_id,
|
|
258
|
+
message_type: subMsgType,
|
|
259
|
+
content: subContent,
|
|
260
|
+
});
|
|
261
|
+
lines.push(`${senderName}: ${formatted}`);
|
|
262
|
+
} catch {
|
|
263
|
+
lines.push(`${senderName}: ${subContent}`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
usedApi = true;
|
|
267
|
+
}
|
|
268
|
+
} catch (err) {
|
|
269
|
+
console.error(
|
|
270
|
+
`[${ts()}] [MERGE_FORWARD] API 获取子消息失败 (${messageId}), 降级使用 preview: ${(err as Error).message}`,
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Phase 2: API 失败或返回空时降级使用 preview
|
|
275
|
+
if (!usedApi) {
|
|
276
|
+
if (Array.isArray(preview) && preview.length > 0) {
|
|
277
|
+
for (const entry of preview) {
|
|
278
|
+
const e = entry as Record<string, unknown>;
|
|
279
|
+
const s = e.sender as Record<string, unknown> | undefined;
|
|
280
|
+
const senderName = (s?.name as string) ?? "未知用户";
|
|
281
|
+
const text = (e.content as string) ?? "";
|
|
282
|
+
lines.push(`${senderName}: ${text}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// Phase 3: 没有任何内容时返回原始 JSON
|
|
288
|
+
if (lines.length === 0) {
|
|
289
|
+
return JSON.stringify(content);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return header + "\n" + lines.join("\n");
|
|
293
|
+
}
|