feishu-mcp 0.0.16 → 0.0.18
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/cli.js +0 -0
- package/dist/mcp/tools/feishuBlockTools.js +252 -202
- package/dist/mcp/tools/feishuFolderTools.js +23 -19
- package/dist/mcp/tools/feishuTools.js +68 -54
- package/dist/server.js +20 -0
- package/dist/services/baseService.js +0 -10
- package/dist/services/callbackService.js +80 -0
- package/dist/services/feishuApiService.js +19 -35
- package/dist/services/feishuAuthService.js +185 -0
- package/dist/types/feishuSchema.js +15 -6
- package/dist/utils/cache.js +96 -15
- package/dist/utils/config.js +39 -7
- package/dist/utils/document.js +154 -0
- package/package.json +1 -1
- package/dist/config.js +0 -26
- package/dist/services/feishu.js +0 -495
- package/dist/services/feishuBlockService.js +0 -179
- package/dist/services/feishuBlocks.js +0 -135
- package/dist/services/feishuService.js +0 -475
package/dist/services/feishu.js
DELETED
|
@@ -1,495 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosError } from "axios";
|
|
2
|
-
import { Logger } from "../server.js";
|
|
3
|
-
export class FeishuService {
|
|
4
|
-
constructor(appId, appSecret) {
|
|
5
|
-
Object.defineProperty(this, "appId", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
configurable: true,
|
|
8
|
-
writable: true,
|
|
9
|
-
value: void 0
|
|
10
|
-
});
|
|
11
|
-
Object.defineProperty(this, "appSecret", {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
configurable: true,
|
|
14
|
-
writable: true,
|
|
15
|
-
value: void 0
|
|
16
|
-
});
|
|
17
|
-
Object.defineProperty(this, "baseUrl", {
|
|
18
|
-
enumerable: true,
|
|
19
|
-
configurable: true,
|
|
20
|
-
writable: true,
|
|
21
|
-
value: "https://open.feishu.cn/open-apis"
|
|
22
|
-
});
|
|
23
|
-
Object.defineProperty(this, "accessToken", {
|
|
24
|
-
enumerable: true,
|
|
25
|
-
configurable: true,
|
|
26
|
-
writable: true,
|
|
27
|
-
value: null
|
|
28
|
-
});
|
|
29
|
-
Object.defineProperty(this, "tokenExpireTime", {
|
|
30
|
-
enumerable: true,
|
|
31
|
-
configurable: true,
|
|
32
|
-
writable: true,
|
|
33
|
-
value: null
|
|
34
|
-
});
|
|
35
|
-
Object.defineProperty(this, "MAX_TOKEN_LIFETIME", {
|
|
36
|
-
enumerable: true,
|
|
37
|
-
configurable: true,
|
|
38
|
-
writable: true,
|
|
39
|
-
value: 2 * 60 * 60 * 1000
|
|
40
|
-
}); // 2小时的毫秒数
|
|
41
|
-
this.appId = appId;
|
|
42
|
-
this.appSecret = appSecret;
|
|
43
|
-
}
|
|
44
|
-
isTokenExpired() {
|
|
45
|
-
if (!this.accessToken || !this.tokenExpireTime)
|
|
46
|
-
return true;
|
|
47
|
-
return Date.now() >= this.tokenExpireTime;
|
|
48
|
-
}
|
|
49
|
-
async getAccessToken() {
|
|
50
|
-
if (this.accessToken && !this.isTokenExpired()) {
|
|
51
|
-
Logger.log('使用现有访问令牌,未过期');
|
|
52
|
-
return this.accessToken;
|
|
53
|
-
}
|
|
54
|
-
try {
|
|
55
|
-
const url = `${this.baseUrl}/auth/v3/tenant_access_token/internal`;
|
|
56
|
-
const requestData = {
|
|
57
|
-
app_id: this.appId,
|
|
58
|
-
app_secret: this.appSecret,
|
|
59
|
-
};
|
|
60
|
-
Logger.log('开始获取新的访问令牌...');
|
|
61
|
-
Logger.log(`请求URL: ${url}`);
|
|
62
|
-
Logger.log(`请求方法: POST`);
|
|
63
|
-
Logger.log(`请求数据: ${JSON.stringify(requestData, null, 2)}`);
|
|
64
|
-
const response = await axios.post(url, requestData);
|
|
65
|
-
Logger.log(`响应状态码: ${response.status}`);
|
|
66
|
-
Logger.log(`响应头: ${JSON.stringify(response.headers, null, 2)}`);
|
|
67
|
-
Logger.log(`响应数据: ${JSON.stringify(response.data, null, 2)}`);
|
|
68
|
-
if (response.data.code !== 0) {
|
|
69
|
-
Logger.error(`获取访问令牌失败,错误码: ${response.data.code}, 错误信息: ${response.data.msg}`);
|
|
70
|
-
throw {
|
|
71
|
-
status: response.status,
|
|
72
|
-
err: response.data.msg || "Unknown error",
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
this.accessToken = response.data.tenant_access_token;
|
|
76
|
-
this.tokenExpireTime = Date.now() + Math.min(response.data.expire * 1000, this.MAX_TOKEN_LIFETIME);
|
|
77
|
-
Logger.log(`成功获取新的访问令牌,有效期: ${response.data.expire} 秒`);
|
|
78
|
-
return this.accessToken; // 使用类型断言确保返回类型为string
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
if (error instanceof AxiosError && error.response) {
|
|
82
|
-
Logger.error(`获取访问令牌请求失败:`);
|
|
83
|
-
Logger.error(`状态码: ${error.response.status}`);
|
|
84
|
-
Logger.error(`响应头: ${JSON.stringify(error.response.headers, null, 2)}`);
|
|
85
|
-
Logger.error(`响应数据: ${JSON.stringify(error.response.data, null, 2)}`);
|
|
86
|
-
throw {
|
|
87
|
-
status: error.response.status,
|
|
88
|
-
err: error.response.data?.msg || "Unknown error",
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
Logger.error('获取访问令牌时发生未知错误:', error);
|
|
92
|
-
throw new Error("Failed to get Feishu access token");
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
async request(endpoint, method = "GET", data) {
|
|
96
|
-
try {
|
|
97
|
-
const accessToken = await this.getAccessToken();
|
|
98
|
-
const url = `${this.baseUrl}${endpoint}`;
|
|
99
|
-
const headers = {
|
|
100
|
-
Authorization: `Bearer ${accessToken}`,
|
|
101
|
-
'Content-Type': 'application/json',
|
|
102
|
-
};
|
|
103
|
-
Logger.log('准备发送请求:');
|
|
104
|
-
Logger.log(`请求URL: ${url}`);
|
|
105
|
-
Logger.log(`请求方法: ${method}`);
|
|
106
|
-
Logger.log(`请求头: ${JSON.stringify(headers, null, 2)}`);
|
|
107
|
-
if (data) {
|
|
108
|
-
Logger.log(`请求数据: ${JSON.stringify(data, null, 2)}`);
|
|
109
|
-
}
|
|
110
|
-
const response = await axios({
|
|
111
|
-
method,
|
|
112
|
-
url,
|
|
113
|
-
headers,
|
|
114
|
-
data,
|
|
115
|
-
});
|
|
116
|
-
Logger.log('收到响应:');
|
|
117
|
-
Logger.log(`响应状态码: ${response.status}`);
|
|
118
|
-
Logger.log(`响应头: ${JSON.stringify(response.headers, null, 2)}`);
|
|
119
|
-
Logger.log(`响应数据: ${JSON.stringify(response.data, null, 2)}`);
|
|
120
|
-
return response.data;
|
|
121
|
-
}
|
|
122
|
-
catch (error) {
|
|
123
|
-
if (error instanceof AxiosError && error.response) {
|
|
124
|
-
Logger.error(`请求失败:`);
|
|
125
|
-
Logger.error(`状态码: ${error.response.status}`);
|
|
126
|
-
Logger.error(`响应头: ${JSON.stringify(error.response.headers, null, 2)}`);
|
|
127
|
-
Logger.error(`响应数据: ${JSON.stringify(error.response.data, null, 2)}`);
|
|
128
|
-
throw {
|
|
129
|
-
status: error.response.status,
|
|
130
|
-
err: error.response.data?.msg || "Unknown error",
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
Logger.error('发送请求时发生未知错误:', error);
|
|
134
|
-
throw new Error("Failed to make request to Feishu API");
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
// 创建新文档
|
|
138
|
-
async createDocument(title, folderToken) {
|
|
139
|
-
try {
|
|
140
|
-
Logger.log(`开始创建飞书文档,标题: ${title}${folderToken ? `,文件夹Token: ${folderToken}` : ',根目录'}`);
|
|
141
|
-
const endpoint = '/docx/v1/documents';
|
|
142
|
-
const data = {
|
|
143
|
-
title: title,
|
|
144
|
-
};
|
|
145
|
-
if (folderToken) {
|
|
146
|
-
data.folder_token = folderToken;
|
|
147
|
-
}
|
|
148
|
-
Logger.log(`准备请求API端点: ${endpoint}`);
|
|
149
|
-
Logger.log(`请求数据: ${JSON.stringify(data, null, 2)}`);
|
|
150
|
-
const response = await this.request(endpoint, 'POST', data);
|
|
151
|
-
if (response.code !== 0) {
|
|
152
|
-
throw new Error(`创建文档失败: ${response.msg}`);
|
|
153
|
-
}
|
|
154
|
-
const docInfo = response.data?.document;
|
|
155
|
-
Logger.log(`文档创建成功,文档ID: ${docInfo?.document_id}`);
|
|
156
|
-
Logger.log(`文档详情: ${JSON.stringify(docInfo, null, 2)}`);
|
|
157
|
-
return docInfo;
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
Logger.error(`创建文档失败:`, error);
|
|
161
|
-
if (error instanceof AxiosError) {
|
|
162
|
-
Logger.error(`请求URL: ${error.config?.url}`);
|
|
163
|
-
Logger.error(`请求方法: ${error.config?.method?.toUpperCase()}`);
|
|
164
|
-
Logger.error(`状态码: ${error.response?.status}`);
|
|
165
|
-
if (error.response?.data) {
|
|
166
|
-
Logger.error(`错误详情: ${JSON.stringify(error.response.data, null, 2)}`);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
throw error;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
// 获取文档信息
|
|
173
|
-
async getDocumentInfo(documentId) {
|
|
174
|
-
try {
|
|
175
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
176
|
-
if (!docId) {
|
|
177
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
178
|
-
}
|
|
179
|
-
Logger.log(`开始获取文档信息,文档ID: ${docId}`);
|
|
180
|
-
const endpoint = `/docx/v1/documents/${docId}`;
|
|
181
|
-
Logger.log(`准备请求API端点: ${endpoint}`);
|
|
182
|
-
const response = await this.request(endpoint);
|
|
183
|
-
if (response.code !== 0) {
|
|
184
|
-
throw new Error(`获取文档信息失败: ${response.msg}`);
|
|
185
|
-
}
|
|
186
|
-
const docInfo = response.data?.document;
|
|
187
|
-
Logger.log(`文档信息获取成功: ${JSON.stringify(docInfo, null, 2)}`);
|
|
188
|
-
if (!docInfo) {
|
|
189
|
-
throw new Error(`获取文档信息失败: 返回的文档信息为空`);
|
|
190
|
-
}
|
|
191
|
-
return docInfo;
|
|
192
|
-
}
|
|
193
|
-
catch (error) {
|
|
194
|
-
Logger.error(`获取文档信息失败:`, error);
|
|
195
|
-
throw error;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
// 获取文档纯文本内容
|
|
199
|
-
async getDocumentContent(documentId, lang = 0) {
|
|
200
|
-
try {
|
|
201
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
202
|
-
if (!docId) {
|
|
203
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
204
|
-
}
|
|
205
|
-
Logger.log(`开始获取文档内容,文档ID: ${docId},语言: ${lang}`);
|
|
206
|
-
const endpoint = `/docx/v1/documents/${docId}/raw_content?lang=${lang}`;
|
|
207
|
-
Logger.log(`准备请求API端点: ${endpoint}`);
|
|
208
|
-
const response = await this.request(endpoint);
|
|
209
|
-
if (response.code !== 0) {
|
|
210
|
-
throw new Error(`获取文档内容失败: ${response.msg}`);
|
|
211
|
-
}
|
|
212
|
-
Logger.log(`文档内容获取成功,长度: ${response.data?.content?.length || 0}字符`);
|
|
213
|
-
return response.data?.content || '';
|
|
214
|
-
}
|
|
215
|
-
catch (error) {
|
|
216
|
-
Logger.error(`获取文档内容失败:`, error);
|
|
217
|
-
throw error;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
// 获取文档块
|
|
221
|
-
async getDocumentBlocks(documentId, pageSize = 500) {
|
|
222
|
-
try {
|
|
223
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
224
|
-
if (!docId) {
|
|
225
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
226
|
-
}
|
|
227
|
-
Logger.log(`开始获取文档块,文档ID: ${docId},页大小: ${pageSize}`);
|
|
228
|
-
const endpoint = `/docx/v1/documents/${docId}/blocks?document_revision_id=-1&page_size=${pageSize}`;
|
|
229
|
-
Logger.log(`准备请求API端点: ${endpoint}`);
|
|
230
|
-
const response = await this.request(endpoint);
|
|
231
|
-
if (response.code !== 0) {
|
|
232
|
-
throw new Error(`获取文档块失败: ${response.msg}`);
|
|
233
|
-
}
|
|
234
|
-
const blocks = response.data?.items || [];
|
|
235
|
-
Logger.log(`文档块获取成功,共 ${blocks.length} 个块`);
|
|
236
|
-
return blocks;
|
|
237
|
-
}
|
|
238
|
-
catch (error) {
|
|
239
|
-
Logger.error(`获取文档块失败:`, error);
|
|
240
|
-
throw error;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
// 创建代码块
|
|
244
|
-
async createCodeBlock(documentId, parentBlockId, code, language = 0, wrap = false, index = 0) {
|
|
245
|
-
try {
|
|
246
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
247
|
-
if (!docId) {
|
|
248
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
249
|
-
}
|
|
250
|
-
Logger.log(`开始创建代码块,文档ID: ${docId},父块ID: ${parentBlockId},语言: ${language},自动换行: ${wrap},插入位置: ${index}`);
|
|
251
|
-
const blockContent = {
|
|
252
|
-
block_type: 14, // 14表示代码块
|
|
253
|
-
code: {
|
|
254
|
-
elements: [
|
|
255
|
-
{
|
|
256
|
-
text_run: {
|
|
257
|
-
content: code,
|
|
258
|
-
text_element_style: {
|
|
259
|
-
bold: false,
|
|
260
|
-
inline_code: false,
|
|
261
|
-
italic: false,
|
|
262
|
-
strikethrough: false,
|
|
263
|
-
underline: false
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
],
|
|
268
|
-
style: {
|
|
269
|
-
language: language,
|
|
270
|
-
wrap: wrap
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
};
|
|
274
|
-
Logger.log(`代码块内容: ${JSON.stringify(blockContent, null, 2)}`);
|
|
275
|
-
return await this.createDocumentBlock(documentId, parentBlockId, blockContent, index);
|
|
276
|
-
}
|
|
277
|
-
catch (error) {
|
|
278
|
-
Logger.error(`创建代码块失败:`, error);
|
|
279
|
-
throw error;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
// 创建文本块
|
|
283
|
-
async createTextBlock(documentId, parentBlockId, textContents, align = 1, index = 0) {
|
|
284
|
-
try {
|
|
285
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
286
|
-
if (!docId) {
|
|
287
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
288
|
-
}
|
|
289
|
-
Logger.log(`开始创建文本块,文档ID: ${docId},父块ID: ${parentBlockId},对齐方式: ${align},插入位置: ${index}`);
|
|
290
|
-
const blockContent = {
|
|
291
|
-
block_type: 2, // 2表示文本块
|
|
292
|
-
text: {
|
|
293
|
-
elements: textContents.map(content => ({
|
|
294
|
-
text_run: {
|
|
295
|
-
content: content.text,
|
|
296
|
-
text_element_style: content.style || {}
|
|
297
|
-
}
|
|
298
|
-
})),
|
|
299
|
-
style: {
|
|
300
|
-
align: align // 1 居左,2 居中,3 居右
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
};
|
|
304
|
-
Logger.log(`文本块内容: ${JSON.stringify(blockContent, null, 2)}`);
|
|
305
|
-
return await this.createDocumentBlock(documentId, parentBlockId, blockContent, index);
|
|
306
|
-
}
|
|
307
|
-
catch (error) {
|
|
308
|
-
Logger.error(`创建文本块失败:`, error);
|
|
309
|
-
throw error;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
// 创建文档块
|
|
313
|
-
async createDocumentBlock(documentId, parentBlockId, blockContent, index = 0) {
|
|
314
|
-
try {
|
|
315
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
316
|
-
if (!docId) {
|
|
317
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
318
|
-
}
|
|
319
|
-
Logger.log(`开始创建文档块,文档ID: ${docId},父块ID: ${parentBlockId},插入位置: ${index}`);
|
|
320
|
-
const endpoint = `/docx/v1/documents/${docId}/blocks/${parentBlockId}/children?document_revision_id=-1`;
|
|
321
|
-
Logger.log(`准备请求API端点: ${endpoint}`);
|
|
322
|
-
const data = {
|
|
323
|
-
children: [blockContent],
|
|
324
|
-
index: index
|
|
325
|
-
};
|
|
326
|
-
Logger.log(`请求数据: ${JSON.stringify(data, null, 2)}`);
|
|
327
|
-
const response = await this.request(endpoint, 'POST', data);
|
|
328
|
-
if (response.code !== 0) {
|
|
329
|
-
throw new Error(`创建文档块失败: ${response.msg}`);
|
|
330
|
-
}
|
|
331
|
-
Logger.log(`文档块创建成功: ${JSON.stringify(response.data, null, 2)}`);
|
|
332
|
-
return response.data;
|
|
333
|
-
}
|
|
334
|
-
catch (error) {
|
|
335
|
-
Logger.error(`创建文档块失败:`, error);
|
|
336
|
-
throw error;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
// 创建文本块内容
|
|
340
|
-
createTextBlockContent(textContents, align = 1) {
|
|
341
|
-
return {
|
|
342
|
-
block_type: 2, // 2表示文本块
|
|
343
|
-
text: {
|
|
344
|
-
elements: textContents.map(content => ({
|
|
345
|
-
text_run: {
|
|
346
|
-
content: content.text,
|
|
347
|
-
text_element_style: content.style || {}
|
|
348
|
-
}
|
|
349
|
-
})),
|
|
350
|
-
style: {
|
|
351
|
-
align: align // 1 居左,2 居中,3 居右
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
// 创建代码块内容
|
|
357
|
-
createCodeBlockContent(code, language = 0, wrap = false) {
|
|
358
|
-
return {
|
|
359
|
-
block_type: 14, // 14表示代码块
|
|
360
|
-
code: {
|
|
361
|
-
elements: [
|
|
362
|
-
{
|
|
363
|
-
text_run: {
|
|
364
|
-
content: code,
|
|
365
|
-
text_element_style: {
|
|
366
|
-
bold: false,
|
|
367
|
-
inline_code: false,
|
|
368
|
-
italic: false,
|
|
369
|
-
strikethrough: false,
|
|
370
|
-
underline: false
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
],
|
|
375
|
-
style: {
|
|
376
|
-
language: language,
|
|
377
|
-
wrap: wrap
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
// 创建标题块内容
|
|
383
|
-
createHeadingBlockContent(text, level = 1, align = 1) {
|
|
384
|
-
// 确保标题级别在有效范围内(1-9)
|
|
385
|
-
const safeLevel = Math.max(1, Math.min(9, level));
|
|
386
|
-
// 根据标题级别设置block_type和对应的属性名
|
|
387
|
-
// 飞书API中,一级标题的block_type为3,二级标题为4,以此类推
|
|
388
|
-
const blockType = 2 + safeLevel; // 一级标题为3,二级标题为4,以此类推
|
|
389
|
-
const headingKey = `heading${safeLevel}`; // heading1, heading2, ...
|
|
390
|
-
// 构建块内容
|
|
391
|
-
const blockContent = {
|
|
392
|
-
block_type: blockType
|
|
393
|
-
};
|
|
394
|
-
// 设置对应级别的标题属性
|
|
395
|
-
blockContent[headingKey] = {
|
|
396
|
-
elements: [
|
|
397
|
-
{
|
|
398
|
-
text_run: {
|
|
399
|
-
content: text,
|
|
400
|
-
text_element_style: {}
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
],
|
|
404
|
-
style: {
|
|
405
|
-
align: align,
|
|
406
|
-
folded: false
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
return blockContent;
|
|
410
|
-
}
|
|
411
|
-
// 批量创建文档块
|
|
412
|
-
async createDocumentBlocks(documentId, parentBlockId, blockContents, index = 0) {
|
|
413
|
-
try {
|
|
414
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
415
|
-
if (!docId) {
|
|
416
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
417
|
-
}
|
|
418
|
-
Logger.log(`开始批量创建文档块,文档ID: ${docId},父块ID: ${parentBlockId},块数量: ${blockContents.length},插入位置: ${index}`);
|
|
419
|
-
const endpoint = `/docx/v1/documents/${docId}/blocks/${parentBlockId}/children?document_revision_id=-1`;
|
|
420
|
-
Logger.log(`准备请求API端点: ${endpoint}`);
|
|
421
|
-
const data = {
|
|
422
|
-
children: blockContents,
|
|
423
|
-
index: index
|
|
424
|
-
};
|
|
425
|
-
Logger.log(`请求数据: ${JSON.stringify(data, null, 2)}`);
|
|
426
|
-
const response = await this.request(endpoint, 'POST', data);
|
|
427
|
-
if (response.code !== 0) {
|
|
428
|
-
throw new Error(`批量创建文档块失败: ${response.msg}`);
|
|
429
|
-
}
|
|
430
|
-
Logger.log(`批量文档块创建成功: ${JSON.stringify(response.data, null, 2)}`);
|
|
431
|
-
return response.data;
|
|
432
|
-
}
|
|
433
|
-
catch (error) {
|
|
434
|
-
Logger.error(`批量创建文档块失败:`, error);
|
|
435
|
-
throw error;
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
// 创建标题块
|
|
439
|
-
async createHeadingBlock(documentId, parentBlockId, text, level = 1, index = 0, align = 1) {
|
|
440
|
-
try {
|
|
441
|
-
const docId = this.extractDocIdFromUrl(documentId);
|
|
442
|
-
if (!docId) {
|
|
443
|
-
throw new Error(`无效的文档ID: ${documentId}`);
|
|
444
|
-
}
|
|
445
|
-
Logger.log(`开始创建标题块,文档ID: ${docId},父块ID: ${parentBlockId},标题级别: ${level},插入位置: ${index}`);
|
|
446
|
-
// 确保标题级别在有效范围内(1-9)
|
|
447
|
-
const safeLevel = Math.max(1, Math.min(9, level));
|
|
448
|
-
// 根据标题级别设置block_type和对应的属性名
|
|
449
|
-
// 飞书API中,一级标题的block_type为3,二级标题为4,以此类推
|
|
450
|
-
const blockType = 2 + safeLevel; // 一级标题为3,二级标题为4,以此类推
|
|
451
|
-
const headingKey = `heading${safeLevel}`; // heading1, heading2, ...
|
|
452
|
-
// 构建块内容
|
|
453
|
-
const blockContent = {
|
|
454
|
-
block_type: blockType
|
|
455
|
-
};
|
|
456
|
-
// 设置对应级别的标题属性
|
|
457
|
-
blockContent[headingKey] = {
|
|
458
|
-
elements: [
|
|
459
|
-
{
|
|
460
|
-
text_run: {
|
|
461
|
-
content: text,
|
|
462
|
-
text_element_style: {}
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
],
|
|
466
|
-
style: {
|
|
467
|
-
align: align,
|
|
468
|
-
folded: false
|
|
469
|
-
}
|
|
470
|
-
};
|
|
471
|
-
Logger.log(`标题块内容: ${JSON.stringify(blockContent, null, 2)}`);
|
|
472
|
-
return await this.createDocumentBlock(documentId, parentBlockId, blockContent, index);
|
|
473
|
-
}
|
|
474
|
-
catch (error) {
|
|
475
|
-
Logger.error(`创建标题块失败:`, error);
|
|
476
|
-
throw error;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
extractDocIdFromUrl(url) {
|
|
480
|
-
// 处理飞书文档 URL,提取文档 ID
|
|
481
|
-
// 支持多种URL格式
|
|
482
|
-
// 1. 标准文档URL格式: https://xxx.feishu.cn/docs/xxx 或 https://xxx.feishu.cn/docx/xxx
|
|
483
|
-
const docxMatch = url.match(/\/docx\/(\w+)/); // 匹配 docx 格式
|
|
484
|
-
const docsMatch = url.match(/\/docs\/(\w+)/); // 匹配 docs 格式
|
|
485
|
-
// 2. API URL格式: https://open.feishu.cn/open-apis/doc/v2/documents/xxx
|
|
486
|
-
const apiMatch = url.match(/\/documents\/([\w-]+)/); // 匹配 API URL 格式
|
|
487
|
-
// 3. 直接使用文档ID
|
|
488
|
-
const directIdMatch = url.match(/^([\w-]+)$/); // 如果直接传入了文档ID
|
|
489
|
-
// 按优先级返回匹配结果
|
|
490
|
-
return docxMatch ? docxMatch[1] :
|
|
491
|
-
docsMatch ? docsMatch[1] :
|
|
492
|
-
apiMatch ? apiMatch[1] :
|
|
493
|
-
directIdMatch ? directIdMatch[1] : null;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @deprecated 这个文件已被弃用,所有功能已迁移到BlockFactory类。
|
|
3
|
-
* 请使用BlockFactory创建各种块内容。
|
|
4
|
-
* 所有接口定义已迁移到blockFactory.ts文件。
|
|
5
|
-
* 此文件仅保留为历史兼容性目的,将在后续版本中移除。
|
|
6
|
-
*/
|
|
7
|
-
/**
|
|
8
|
-
* 构建批量创建块的请求数据
|
|
9
|
-
* @param blocks 块内容数组
|
|
10
|
-
* @param index 插入位置索引
|
|
11
|
-
* @returns 请求数据对象
|
|
12
|
-
* @deprecated 请使用BlockFactory.buildCreateBlocksRequest
|
|
13
|
-
*/
|
|
14
|
-
export function buildCreateBlocksRequest(blocks, index = 0) {
|
|
15
|
-
return {
|
|
16
|
-
children: blocks,
|
|
17
|
-
index
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 创建文本块内容
|
|
22
|
-
* @param align 对齐方式:1左对齐,2居中,3右对齐
|
|
23
|
-
* @returns 文本块内容对象
|
|
24
|
-
* @deprecated 请使用BlockFactory.createTextBlock
|
|
25
|
-
*/
|
|
26
|
-
export function createTextBlockContent(textContents, align = 1) {
|
|
27
|
-
return {
|
|
28
|
-
block_type: 2, // 2表示文本块
|
|
29
|
-
text: {
|
|
30
|
-
elements: textContents.map(content => ({
|
|
31
|
-
text_run: {
|
|
32
|
-
content: content.text,
|
|
33
|
-
text_element_style: content.style || {}
|
|
34
|
-
}
|
|
35
|
-
})),
|
|
36
|
-
style: {
|
|
37
|
-
align: align // 1 居左,2 居中,3 居右
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* 创建代码块内容
|
|
44
|
-
* @param code 代码内容
|
|
45
|
-
* @param language 语言类型代码
|
|
46
|
-
* @param wrap 是否自动换行
|
|
47
|
-
* @returns 代码块内容对象
|
|
48
|
-
* @deprecated 请使用BlockFactory.createCodeBlock
|
|
49
|
-
*/
|
|
50
|
-
export function createCodeBlockContent(code, language = 0, wrap = false) {
|
|
51
|
-
return {
|
|
52
|
-
block_type: 14, // 14表示代码块
|
|
53
|
-
code: {
|
|
54
|
-
elements: [
|
|
55
|
-
{
|
|
56
|
-
text_run: {
|
|
57
|
-
content: code,
|
|
58
|
-
text_element_style: {
|
|
59
|
-
bold: false,
|
|
60
|
-
inline_code: false,
|
|
61
|
-
italic: false,
|
|
62
|
-
strikethrough: false,
|
|
63
|
-
underline: false
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
],
|
|
68
|
-
style: {
|
|
69
|
-
language: language,
|
|
70
|
-
wrap: wrap
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* 创建标题块内容
|
|
77
|
-
* @param text 标题文本
|
|
78
|
-
* @param level 标题级别(1-9)
|
|
79
|
-
* @param align 对齐方式:1左对齐,2居中,3右对齐
|
|
80
|
-
* @returns 标题块内容对象
|
|
81
|
-
* @deprecated 请使用BlockFactory.createHeadingBlock
|
|
82
|
-
*/
|
|
83
|
-
export function createHeadingBlockContent(text, level = 1, align = 1) {
|
|
84
|
-
// 确保标题级别在有效范围内(1-9)
|
|
85
|
-
const safeLevel = Math.max(1, Math.min(9, level));
|
|
86
|
-
// 根据标题级别设置block_type和对应的属性名
|
|
87
|
-
// 飞书API中,一级标题的block_type为3,二级标题为4,以此类推
|
|
88
|
-
const blockType = 2 + safeLevel; // 一级标题为3,二级标题为4,以此类推
|
|
89
|
-
const headingKey = `heading${safeLevel}`; // heading1, heading2, ...
|
|
90
|
-
// 构建块内容
|
|
91
|
-
const blockContent = {
|
|
92
|
-
block_type: blockType
|
|
93
|
-
};
|
|
94
|
-
// 设置对应级别的标题属性
|
|
95
|
-
blockContent[headingKey] = {
|
|
96
|
-
elements: [
|
|
97
|
-
{
|
|
98
|
-
text_run: {
|
|
99
|
-
content: text,
|
|
100
|
-
text_element_style: {}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
],
|
|
104
|
-
style: {
|
|
105
|
-
align: align,
|
|
106
|
-
folded: false
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
return blockContent;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* 创建列表块内容(有序或无序)
|
|
113
|
-
* @param text 列表项文本
|
|
114
|
-
* @param isOrdered 是否为有序列表
|
|
115
|
-
* @param align 对齐方式:1左对齐,2居中,3右对齐
|
|
116
|
-
* @returns 列表块内容对象
|
|
117
|
-
* @deprecated 请使用BlockFactory.createListBlock
|
|
118
|
-
*/
|
|
119
|
-
export function createListBlockContent(text, isOrdered = false, align = 1) {
|
|
120
|
-
// 确保 align 值在合法范围内(1-3)
|
|
121
|
-
const safeAlign = (align === 1 || align === 2 || align === 3) ? align : 1;
|
|
122
|
-
// 有序列表是 block_type: 13,无序列表是 block_type: 12
|
|
123
|
-
const blockType = isOrdered ? 13 : 12;
|
|
124
|
-
const propertyKey = isOrdered ? "ordered" : "bullet";
|
|
125
|
-
// 构建块内容
|
|
126
|
-
const blockContent = {
|
|
127
|
-
block_type: blockType
|
|
128
|
-
};
|
|
129
|
-
// 设置列表属性
|
|
130
|
-
blockContent[propertyKey] = {
|
|
131
|
-
elements: [
|
|
132
|
-
{
|
|
133
|
-
text_run: {
|
|
134
|
-
content: text,
|
|
135
|
-
text_element_style: {}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
],
|
|
139
|
-
style: {
|
|
140
|
-
align: safeAlign,
|
|
141
|
-
folded: false
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
return blockContent;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* 处理Markdown语法转换
|
|
148
|
-
* @param textContents 文本内容数组
|
|
149
|
-
* @returns 处理后的文本内容数组
|
|
150
|
-
* @deprecated 应当避免使用Markdown语法,请直接使用TextElementStyle设置样式
|
|
151
|
-
*/
|
|
152
|
-
export function processMarkdownSyntax(textContents) {
|
|
153
|
-
return textContents.map(content => {
|
|
154
|
-
let { text, style = {} } = content;
|
|
155
|
-
// 创建一个新的style对象,避免修改原始对象
|
|
156
|
-
const newStyle = { ...style };
|
|
157
|
-
// 处理粗体 **text**
|
|
158
|
-
if (text.match(/\*\*([^*]+)\*\*/g)) {
|
|
159
|
-
text = text.replace(/\*\*([^*]+)\*\*/g, "$1");
|
|
160
|
-
newStyle.bold = true;
|
|
161
|
-
}
|
|
162
|
-
// 处理斜体 *text*
|
|
163
|
-
if (text.match(/(?<!\*)\*([^*]+)\*(?!\*)/g)) {
|
|
164
|
-
text = text.replace(/(?<!\*)\*([^*]+)\*(?!\*)/g, "$1");
|
|
165
|
-
newStyle.italic = true;
|
|
166
|
-
}
|
|
167
|
-
// 处理删除线 ~~text~~
|
|
168
|
-
if (text.match(/~~([^~]+)~~/g)) {
|
|
169
|
-
text = text.replace(/~~([^~]+)~~/g, "$1");
|
|
170
|
-
newStyle.strikethrough = true;
|
|
171
|
-
}
|
|
172
|
-
// 处理行内代码 `code`
|
|
173
|
-
if (text.match(/`([^`]+)`/g)) {
|
|
174
|
-
text = text.replace(/`([^`]+)`/g, "$1");
|
|
175
|
-
newStyle.inline_code = true;
|
|
176
|
-
}
|
|
177
|
-
return { text, style: newStyle };
|
|
178
|
-
});
|
|
179
|
-
}
|