@nextclaw/channel-plugin-feishu 0.2.15 → 0.2.17
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/index.ts +10 -0
- package/package.json +1 -1
- package/src/accounts.test.ts +10 -0
- package/src/accounts.ts +10 -10
- package/src/app-scope-checker.ts +75 -0
- package/src/auth-errors.ts +90 -0
- package/src/calendar-calendar.ts +72 -0
- package/src/calendar-event-attendee.ts +96 -0
- package/src/calendar-event.ts +236 -0
- package/src/calendar-freebusy.ts +58 -0
- package/src/calendar-shared.ts +32 -0
- package/src/calendar.ts +18 -0
- package/src/card-action.ts +19 -7
- package/src/config-schema.ts +5 -0
- package/src/device-flow.ts +188 -0
- package/src/domains.ts +19 -0
- package/src/feishu-fetch.ts +9 -0
- package/src/identity.ts +160 -0
- package/src/lark-ticket.ts +26 -0
- package/src/monitor.account.ts +48 -20
- package/src/oauth.ts +248 -0
- package/src/raw-request.ts +45 -0
- package/src/sheets.ts +374 -0
- package/src/task-comment.ts +95 -0
- package/src/task-shared.ts +10 -0
- package/src/task-subtask.ts +94 -0
- package/src/task-task.ts +172 -0
- package/src/task-tasklist.ts +154 -0
- package/src/task.ts +18 -0
- package/src/token-store.ts +211 -0
- package/src/tool-account-routing.test.ts +19 -0
- package/src/tool-account.ts +16 -0
- package/src/tool-scopes.ts +102 -0
- package/src/tools-config.test.ts +19 -0
- package/src/tools-config.ts +5 -0
- package/src/types.ts +5 -0
- package/src/uat-client.ts +159 -0
- package/src/user-tool-client.ts +224 -0
- package/src/user-tool-helpers.ts +157 -0
- package/src/user-tool-result.ts +22 -0
package/src/sheets.ts
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { OpenClawPluginApi } from "./nextclaw-sdk/feishu.js";
|
|
3
|
+
import { listEnabledFeishuAccounts } from "./accounts.js";
|
|
4
|
+
import { wwwDomain } from "./domains.js";
|
|
5
|
+
import { resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js";
|
|
6
|
+
import {
|
|
7
|
+
assertLarkOk,
|
|
8
|
+
createToolContext,
|
|
9
|
+
handleInvokeError,
|
|
10
|
+
json,
|
|
11
|
+
registerTool,
|
|
12
|
+
StringEnum,
|
|
13
|
+
} from "./user-tool-helpers.js";
|
|
14
|
+
|
|
15
|
+
const MAX_READ_ROWS = 200;
|
|
16
|
+
const MAX_WRITE_ROWS = 5000;
|
|
17
|
+
const MAX_WRITE_COLS = 100;
|
|
18
|
+
|
|
19
|
+
const SheetSchema = Type.Union([
|
|
20
|
+
Type.Object({ action: Type.Literal("info"), url: Type.Optional(Type.String()), spreadsheet_token: Type.Optional(Type.String()) }),
|
|
21
|
+
Type.Object({
|
|
22
|
+
action: Type.Literal("read"),
|
|
23
|
+
url: Type.Optional(Type.String()),
|
|
24
|
+
spreadsheet_token: Type.Optional(Type.String()),
|
|
25
|
+
range: Type.Optional(Type.String()),
|
|
26
|
+
sheet_id: Type.Optional(Type.String()),
|
|
27
|
+
value_render_option: Type.Optional(StringEnum(["FormattedValue", "UnformattedValue", "Formula", "ToString"])),
|
|
28
|
+
}),
|
|
29
|
+
Type.Object({
|
|
30
|
+
action: Type.Literal("write"),
|
|
31
|
+
url: Type.Optional(Type.String()),
|
|
32
|
+
spreadsheet_token: Type.Optional(Type.String()),
|
|
33
|
+
range: Type.Optional(Type.String()),
|
|
34
|
+
sheet_id: Type.Optional(Type.String()),
|
|
35
|
+
values: Type.Array(Type.Array(Type.Any())),
|
|
36
|
+
}),
|
|
37
|
+
Type.Object({
|
|
38
|
+
action: Type.Literal("append"),
|
|
39
|
+
url: Type.Optional(Type.String()),
|
|
40
|
+
spreadsheet_token: Type.Optional(Type.String()),
|
|
41
|
+
range: Type.Optional(Type.String()),
|
|
42
|
+
sheet_id: Type.Optional(Type.String()),
|
|
43
|
+
values: Type.Array(Type.Array(Type.Any())),
|
|
44
|
+
}),
|
|
45
|
+
Type.Object({
|
|
46
|
+
action: Type.Literal("find"),
|
|
47
|
+
url: Type.Optional(Type.String()),
|
|
48
|
+
spreadsheet_token: Type.Optional(Type.String()),
|
|
49
|
+
sheet_id: Type.String(),
|
|
50
|
+
range: Type.Optional(Type.String()),
|
|
51
|
+
find: Type.String(),
|
|
52
|
+
match_case: Type.Optional(Type.Boolean()),
|
|
53
|
+
match_entire_cell: Type.Optional(Type.Boolean()),
|
|
54
|
+
search_by_regex: Type.Optional(Type.Boolean()),
|
|
55
|
+
include_formulas: Type.Optional(Type.Boolean()),
|
|
56
|
+
}),
|
|
57
|
+
Type.Object({
|
|
58
|
+
action: Type.Literal("create"),
|
|
59
|
+
title: Type.String(),
|
|
60
|
+
folder_token: Type.Optional(Type.String()),
|
|
61
|
+
headers: Type.Optional(Type.Array(Type.Any())),
|
|
62
|
+
data: Type.Optional(Type.Array(Type.Array(Type.Any()))),
|
|
63
|
+
}),
|
|
64
|
+
Type.Object({
|
|
65
|
+
action: Type.Literal("export"),
|
|
66
|
+
url: Type.Optional(Type.String()),
|
|
67
|
+
spreadsheet_token: Type.Optional(Type.String()),
|
|
68
|
+
sheet_id: Type.Optional(Type.String()),
|
|
69
|
+
file_extension: StringEnum(["xlsx", "csv"]),
|
|
70
|
+
}),
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
function parseSheetUrl(url: string): { token: string; sheetId?: string } | null {
|
|
74
|
+
try {
|
|
75
|
+
const parsed = new URL(url);
|
|
76
|
+
const match = parsed.pathname.match(/\/sheets\/([^/?#]+)/);
|
|
77
|
+
if (!match) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
token: match[1],
|
|
82
|
+
sheetId: parsed.searchParams.get("sheet") || undefined,
|
|
83
|
+
};
|
|
84
|
+
} catch {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function resolveToken(params: { url?: string; spreadsheet_token?: string }) {
|
|
90
|
+
if (params.spreadsheet_token) {
|
|
91
|
+
return { token: params.spreadsheet_token };
|
|
92
|
+
}
|
|
93
|
+
if (params.url) {
|
|
94
|
+
return parseSheetUrl(params.url);
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function colLetter(n: number): string {
|
|
100
|
+
let result = "";
|
|
101
|
+
let value = n;
|
|
102
|
+
while (value > 0) {
|
|
103
|
+
value -= 1;
|
|
104
|
+
result = String.fromCharCode(65 + (value % 26)) + result;
|
|
105
|
+
value = Math.floor(value / 26);
|
|
106
|
+
}
|
|
107
|
+
return result;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function flattenCellValue(cell: unknown): unknown {
|
|
111
|
+
if (!Array.isArray(cell)) {
|
|
112
|
+
return cell;
|
|
113
|
+
}
|
|
114
|
+
if (cell.length > 0 && cell.every((segment) => segment && typeof segment === "object" && "text" in (segment as object))) {
|
|
115
|
+
return cell.map((segment) => String((segment as { text?: unknown }).text ?? "")).join("");
|
|
116
|
+
}
|
|
117
|
+
return cell;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function resolveSheetRange(
|
|
121
|
+
client: ReturnType<typeof createToolContext>["toolClient"] extends () => infer T ? T : never,
|
|
122
|
+
token: string,
|
|
123
|
+
range?: string,
|
|
124
|
+
sheetId?: string,
|
|
125
|
+
) {
|
|
126
|
+
if (range) return range;
|
|
127
|
+
if (sheetId) return sheetId;
|
|
128
|
+
const response = await client.invoke(
|
|
129
|
+
"feishu_sheet.info",
|
|
130
|
+
(sdk, opts) => sdk.sheets.spreadsheetSheet.query({ path: { spreadsheet_token: token } }, opts),
|
|
131
|
+
{ as: "user" },
|
|
132
|
+
);
|
|
133
|
+
assertLarkOk(response);
|
|
134
|
+
const firstSheet = (response.data as { sheets?: Array<{ sheet_id?: string }> } | undefined)?.sheets?.[0];
|
|
135
|
+
if (!firstSheet?.sheet_id) {
|
|
136
|
+
throw new Error("Spreadsheet has no worksheets.");
|
|
137
|
+
}
|
|
138
|
+
return firstSheet.sheet_id;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export function registerFeishuSheetsTools(api: OpenClawPluginApi) {
|
|
142
|
+
if (!api.config) return;
|
|
143
|
+
const accounts = listEnabledFeishuAccounts(api.config);
|
|
144
|
+
if (accounts.length === 0) return;
|
|
145
|
+
if (!resolveAnyEnabledFeishuToolsConfig(accounts).sheets) return;
|
|
146
|
+
|
|
147
|
+
registerTool(api, {
|
|
148
|
+
name: "feishu_sheet",
|
|
149
|
+
label: "Feishu Sheet",
|
|
150
|
+
description: "按本人身份读取、写入、创建、查找和导出飞书电子表格。",
|
|
151
|
+
parameters: SheetSchema,
|
|
152
|
+
async execute(_toolCallId, params) {
|
|
153
|
+
const payload = params as any;
|
|
154
|
+
try {
|
|
155
|
+
const client = createToolContext(api, "feishu_sheet").toolClient();
|
|
156
|
+
|
|
157
|
+
if (payload.action === "create") {
|
|
158
|
+
const createResponse = await client.invoke(
|
|
159
|
+
"feishu_sheet.create",
|
|
160
|
+
(sdk, opts) =>
|
|
161
|
+
sdk.sheets.spreadsheet.create(
|
|
162
|
+
{
|
|
163
|
+
data: {
|
|
164
|
+
title: payload.title,
|
|
165
|
+
folder_token: payload.folder_token,
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
opts,
|
|
169
|
+
),
|
|
170
|
+
{ as: "user" },
|
|
171
|
+
);
|
|
172
|
+
assertLarkOk(createResponse);
|
|
173
|
+
const spreadsheet = (createResponse.data as { spreadsheet?: { spreadsheet_token?: string; title?: string } } | undefined)?.spreadsheet;
|
|
174
|
+
const token = spreadsheet?.spreadsheet_token;
|
|
175
|
+
if (!token) {
|
|
176
|
+
return json({ error: "创建电子表格失败,未返回 spreadsheet_token。" });
|
|
177
|
+
}
|
|
178
|
+
if (payload.headers || payload.data) {
|
|
179
|
+
const sheetsResponse = await client.invoke(
|
|
180
|
+
"feishu_sheet.create",
|
|
181
|
+
(sdk, opts) => sdk.sheets.spreadsheetSheet.query({ path: { spreadsheet_token: token } }, opts),
|
|
182
|
+
{ as: "user" },
|
|
183
|
+
);
|
|
184
|
+
assertLarkOk(sheetsResponse);
|
|
185
|
+
const firstSheet = (sheetsResponse.data as { sheets?: Array<{ sheet_id?: string }> } | undefined)?.sheets?.[0];
|
|
186
|
+
const allRows = [
|
|
187
|
+
...(payload.headers ? [payload.headers] : []),
|
|
188
|
+
...((payload.data as unknown[][] | undefined) ?? []),
|
|
189
|
+
];
|
|
190
|
+
if (firstSheet?.sheet_id && allRows.length > 0) {
|
|
191
|
+
const numCols = Math.max(...allRows.map((row) => row.length), 1);
|
|
192
|
+
const range = `${firstSheet.sheet_id}!A1:${colLetter(numCols)}${allRows.length}`;
|
|
193
|
+
await client.invokeByPath("feishu_sheet.create", `/open-apis/sheets/v2/spreadsheets/${token}/values`, {
|
|
194
|
+
method: "PUT",
|
|
195
|
+
body: { valueRange: { range, values: allRows } },
|
|
196
|
+
as: "user",
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return json({
|
|
201
|
+
spreadsheet_token: token,
|
|
202
|
+
title: spreadsheet?.title ?? payload.title,
|
|
203
|
+
url: `${wwwDomain(client.account.domain)}/sheets/${token}`,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const tokenInfo = resolveToken(payload);
|
|
208
|
+
if (!tokenInfo?.token) {
|
|
209
|
+
return json({ error: "请传 spreadsheet_token 或 /sheets/TOKEN 形式的 url。" });
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (payload.action === "info") {
|
|
213
|
+
const [spreadsheetRes, sheetsRes] = await Promise.all([
|
|
214
|
+
client.invoke(
|
|
215
|
+
"feishu_sheet.info",
|
|
216
|
+
(sdk, opts) => sdk.sheets.spreadsheet.get({ path: { spreadsheet_token: tokenInfo.token } }, opts),
|
|
217
|
+
{ as: "user" },
|
|
218
|
+
),
|
|
219
|
+
client.invoke(
|
|
220
|
+
"feishu_sheet.info",
|
|
221
|
+
(sdk, opts) => sdk.sheets.spreadsheetSheet.query({ path: { spreadsheet_token: tokenInfo.token } }, opts),
|
|
222
|
+
{ as: "user" },
|
|
223
|
+
),
|
|
224
|
+
]);
|
|
225
|
+
assertLarkOk(spreadsheetRes);
|
|
226
|
+
assertLarkOk(sheetsRes);
|
|
227
|
+
return json({
|
|
228
|
+
title: (spreadsheetRes.data as { spreadsheet?: { title?: string } } | undefined)?.spreadsheet?.title,
|
|
229
|
+
spreadsheet_token: tokenInfo.token,
|
|
230
|
+
url: `${wwwDomain(client.account.domain)}/sheets/${tokenInfo.token}`,
|
|
231
|
+
sheets:
|
|
232
|
+
((sheetsRes.data as { sheets?: Array<Record<string, unknown>> } | undefined)?.sheets ?? []).map((sheet) => ({
|
|
233
|
+
sheet_id: sheet.sheet_id,
|
|
234
|
+
title: sheet.title,
|
|
235
|
+
index: sheet.index,
|
|
236
|
+
row_count: (sheet.grid_properties as { row_count?: number } | undefined)?.row_count,
|
|
237
|
+
column_count: (sheet.grid_properties as { column_count?: number } | undefined)?.column_count,
|
|
238
|
+
})),
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (payload.action === "read") {
|
|
243
|
+
const range = await resolveSheetRange(client, tokenInfo.token, payload.range, payload.sheet_id ?? tokenInfo.sheetId);
|
|
244
|
+
const response = await client.invokeByPath<{
|
|
245
|
+
data?: { valueRange?: { range?: string; values?: unknown[][] } };
|
|
246
|
+
}>("feishu_sheet.read", `/open-apis/sheets/v2/spreadsheets/${tokenInfo.token}/values/${encodeURIComponent(range)}`, {
|
|
247
|
+
method: "GET",
|
|
248
|
+
query: {
|
|
249
|
+
valueRenderOption: payload.value_render_option ?? "ToString",
|
|
250
|
+
dateTimeRenderOption: "FormattedString",
|
|
251
|
+
},
|
|
252
|
+
as: "user",
|
|
253
|
+
});
|
|
254
|
+
const values = (response.data?.valueRange?.values ?? []).map((row) => row.map(flattenCellValue));
|
|
255
|
+
return json({
|
|
256
|
+
range: response.data?.valueRange?.range,
|
|
257
|
+
values: values.slice(0, MAX_READ_ROWS),
|
|
258
|
+
...(values.length > MAX_READ_ROWS
|
|
259
|
+
? {
|
|
260
|
+
truncated: true,
|
|
261
|
+
total_rows: values.length,
|
|
262
|
+
hint: `结果超过 ${MAX_READ_ROWS} 行,已截断。请缩小 range 后重试。`,
|
|
263
|
+
}
|
|
264
|
+
: {}),
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (payload.action === "write" || payload.action === "append") {
|
|
269
|
+
if (payload.values.length > MAX_WRITE_ROWS) {
|
|
270
|
+
return json({ error: `写入行数超过限制 ${MAX_WRITE_ROWS}` });
|
|
271
|
+
}
|
|
272
|
+
if (payload.values.some((row: unknown[]) => row.length > MAX_WRITE_COLS)) {
|
|
273
|
+
return json({ error: `写入列数超过限制 ${MAX_WRITE_COLS}` });
|
|
274
|
+
}
|
|
275
|
+
const range = await resolveSheetRange(client, tokenInfo.token, payload.range, payload.sheet_id ?? tokenInfo.sheetId);
|
|
276
|
+
const response = await client.invokeByPath<any>(
|
|
277
|
+
payload.action === "write" ? "feishu_sheet.write" : "feishu_sheet.append",
|
|
278
|
+
`/open-apis/sheets/v2/spreadsheets/${tokenInfo.token}/${payload.action === "write" ? "values" : "values_append"}`,
|
|
279
|
+
{
|
|
280
|
+
method: payload.action === "write" ? "PUT" : "POST",
|
|
281
|
+
body: { valueRange: { range, values: payload.values } },
|
|
282
|
+
as: "user",
|
|
283
|
+
},
|
|
284
|
+
);
|
|
285
|
+
const updates = response.data?.updates ?? response.data;
|
|
286
|
+
return json({
|
|
287
|
+
updated_range: updates?.updatedRange,
|
|
288
|
+
updated_rows: updates?.updatedRows,
|
|
289
|
+
updated_columns: updates?.updatedColumns,
|
|
290
|
+
updated_cells: updates?.updatedCells,
|
|
291
|
+
revision: updates?.revision,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (payload.action === "find") {
|
|
296
|
+
const response = await client.invoke(
|
|
297
|
+
"feishu_sheet.find",
|
|
298
|
+
(sdk, opts) =>
|
|
299
|
+
sdk.sheets.spreadsheetSheet.find(
|
|
300
|
+
{
|
|
301
|
+
path: { spreadsheet_token: tokenInfo.token, sheet_id: payload.sheet_id },
|
|
302
|
+
data: {
|
|
303
|
+
find: payload.find,
|
|
304
|
+
find_condition: {
|
|
305
|
+
range: payload.range ? `${payload.sheet_id}!${payload.range}` : payload.sheet_id,
|
|
306
|
+
...(payload.match_case !== undefined ? { match_case: !payload.match_case } : {}),
|
|
307
|
+
...(payload.match_entire_cell !== undefined ? { match_entire_cell: payload.match_entire_cell } : {}),
|
|
308
|
+
...(payload.search_by_regex !== undefined ? { search_by_regex: payload.search_by_regex } : {}),
|
|
309
|
+
...(payload.include_formulas !== undefined ? { include_formulas: payload.include_formulas } : {}),
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
opts,
|
|
314
|
+
),
|
|
315
|
+
{ as: "user" },
|
|
316
|
+
);
|
|
317
|
+
assertLarkOk(response);
|
|
318
|
+
return json({
|
|
319
|
+
matched_cells: (response.data as { find_result?: { matched_cells?: unknown[] } } | undefined)?.find_result?.matched_cells ?? [],
|
|
320
|
+
matched_formula_cells:
|
|
321
|
+
(response.data as { find_result?: { matched_formula_cells?: unknown[] } } | undefined)?.find_result?.matched_formula_cells ?? [],
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const exportCreate = await client.invoke(
|
|
326
|
+
"feishu_sheet.export",
|
|
327
|
+
(sdk, opts) =>
|
|
328
|
+
sdk.drive.exportTask.create(
|
|
329
|
+
{
|
|
330
|
+
data: {
|
|
331
|
+
file_extension: payload.file_extension,
|
|
332
|
+
token: tokenInfo.token,
|
|
333
|
+
type: "sheet",
|
|
334
|
+
sub_id: payload.sheet_id,
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
opts,
|
|
338
|
+
),
|
|
339
|
+
{ as: "user" },
|
|
340
|
+
);
|
|
341
|
+
assertLarkOk(exportCreate);
|
|
342
|
+
const ticket = (exportCreate.data as { ticket?: string } | undefined)?.ticket;
|
|
343
|
+
if (!ticket) {
|
|
344
|
+
return json({ error: "导出任务创建失败,未返回 ticket。" });
|
|
345
|
+
}
|
|
346
|
+
for (let i = 0; i < 30; i += 1) {
|
|
347
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
348
|
+
const exportStatus = await client.invoke(
|
|
349
|
+
"feishu_sheet.export",
|
|
350
|
+
(sdk, opts) =>
|
|
351
|
+
sdk.drive.exportTask.get({ path: { ticket }, params: { token: tokenInfo.token } }, opts),
|
|
352
|
+
{ as: "user" },
|
|
353
|
+
);
|
|
354
|
+
assertLarkOk(exportStatus);
|
|
355
|
+
const result = (exportStatus.data as { result?: { job_status?: number; file_token?: string; file_name?: string; file_size?: number; job_error_msg?: string } } | undefined)?.result;
|
|
356
|
+
if (result?.job_status === 0) {
|
|
357
|
+
return json({
|
|
358
|
+
file_token: result.file_token,
|
|
359
|
+
file_name: result.file_name,
|
|
360
|
+
file_size: result.file_size,
|
|
361
|
+
file_extension: payload.file_extension,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
if ((result?.job_status ?? 0) >= 3) {
|
|
365
|
+
return json({ error: result?.job_error_msg ?? `导出失败 (status=${result?.job_status})` });
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return json({ error: "导出任务超时,请稍后重试。" });
|
|
369
|
+
} catch (error) {
|
|
370
|
+
return handleInvokeError(error, api);
|
|
371
|
+
}
|
|
372
|
+
},
|
|
373
|
+
}, { name: "feishu_sheet" });
|
|
374
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { OpenClawPluginApi } from "./nextclaw-sdk/feishu.js";
|
|
3
|
+
import { assertLarkOk, createToolContext, handleInvokeError, json, registerTool, StringEnum } from "./user-tool-helpers.js";
|
|
4
|
+
|
|
5
|
+
const CommentSchema = Type.Union([
|
|
6
|
+
Type.Object({ action: Type.Literal("create"), task_guid: Type.String(), content: Type.String(), reply_to_comment_id: Type.Optional(Type.String()) }),
|
|
7
|
+
Type.Object({ action: Type.Literal("list"), resource_id: Type.String(), direction: Type.Optional(StringEnum(["asc", "desc"])), page_size: Type.Optional(Type.Number()), page_token: Type.Optional(Type.String()) }),
|
|
8
|
+
Type.Object({ action: Type.Literal("get"), comment_id: Type.String() }),
|
|
9
|
+
]);
|
|
10
|
+
|
|
11
|
+
export function registerFeishuTaskCommentTool(api: OpenClawPluginApi) {
|
|
12
|
+
const { toolClient } = createToolContext(api, "feishu_task_comment");
|
|
13
|
+
registerTool(api, {
|
|
14
|
+
name: "feishu_task_comment",
|
|
15
|
+
label: "Feishu Task Comment",
|
|
16
|
+
description: "按本人身份创建、获取、列出任务评论。",
|
|
17
|
+
parameters: CommentSchema,
|
|
18
|
+
async execute(_toolCallId, params) {
|
|
19
|
+
const payload = params as {
|
|
20
|
+
action: "create" | "get" | "list";
|
|
21
|
+
task_guid?: string;
|
|
22
|
+
content?: string;
|
|
23
|
+
reply_to_comment_id?: string;
|
|
24
|
+
resource_id?: string;
|
|
25
|
+
direction?: "asc" | "desc";
|
|
26
|
+
page_size?: number;
|
|
27
|
+
page_token?: string;
|
|
28
|
+
comment_id?: string;
|
|
29
|
+
};
|
|
30
|
+
try {
|
|
31
|
+
const client = toolClient();
|
|
32
|
+
if (payload.action === "create") {
|
|
33
|
+
const response = await client.invoke(
|
|
34
|
+
"feishu_task_comment.create",
|
|
35
|
+
(sdk, opts) =>
|
|
36
|
+
sdk.task.v2.comment.create(
|
|
37
|
+
{
|
|
38
|
+
params: { user_id_type: "open_id" as never },
|
|
39
|
+
data: {
|
|
40
|
+
content: payload.content,
|
|
41
|
+
resource_type: "task",
|
|
42
|
+
resource_id: payload.task_guid,
|
|
43
|
+
reply_to_comment_id: payload.reply_to_comment_id,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
opts,
|
|
47
|
+
),
|
|
48
|
+
{ as: "user" },
|
|
49
|
+
);
|
|
50
|
+
assertLarkOk(response);
|
|
51
|
+
return json({ comment: (response.data as { comment?: unknown } | undefined)?.comment });
|
|
52
|
+
}
|
|
53
|
+
if (payload.action === "get") {
|
|
54
|
+
const response = await client.invoke(
|
|
55
|
+
"feishu_task_comment.get",
|
|
56
|
+
(sdk, opts) =>
|
|
57
|
+
sdk.task.v2.comment.get(
|
|
58
|
+
{ path: { comment_id: payload.comment_id! }, params: { user_id_type: "open_id" as never } },
|
|
59
|
+
opts,
|
|
60
|
+
),
|
|
61
|
+
{ as: "user" },
|
|
62
|
+
);
|
|
63
|
+
assertLarkOk(response);
|
|
64
|
+
return json({ comment: (response.data as { comment?: unknown } | undefined)?.comment });
|
|
65
|
+
}
|
|
66
|
+
const response = await client.invoke(
|
|
67
|
+
"feishu_task_comment.list",
|
|
68
|
+
(sdk, opts) =>
|
|
69
|
+
sdk.task.v2.comment.list(
|
|
70
|
+
{
|
|
71
|
+
params: {
|
|
72
|
+
resource_type: "task",
|
|
73
|
+
resource_id: payload.resource_id,
|
|
74
|
+
direction: payload.direction,
|
|
75
|
+
page_size: payload.page_size,
|
|
76
|
+
page_token: payload.page_token,
|
|
77
|
+
user_id_type: "open_id" as never,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
opts,
|
|
81
|
+
),
|
|
82
|
+
{ as: "user" },
|
|
83
|
+
);
|
|
84
|
+
assertLarkOk(response);
|
|
85
|
+
return json({
|
|
86
|
+
comments: (response.data as { items?: unknown[] } | undefined)?.items ?? [],
|
|
87
|
+
has_more: (response.data as { has_more?: boolean } | undefined)?.has_more ?? false,
|
|
88
|
+
page_token: (response.data as { page_token?: string } | undefined)?.page_token,
|
|
89
|
+
});
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return handleInvokeError(error, api);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
}, { name: "feishu_task_comment" });
|
|
95
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { parseTimeToTimestampMs } from "./user-tool-helpers.js";
|
|
2
|
+
|
|
3
|
+
export function normalizeTaskTime(input?: { timestamp?: string; is_all_day?: boolean }) {
|
|
4
|
+
if (!input) return undefined;
|
|
5
|
+
const ts = parseTimeToTimestampMs(input.timestamp);
|
|
6
|
+
if (!ts) {
|
|
7
|
+
throw new Error("任务时间字段必须是带时区的 ISO 8601 / RFC 3339 时间。");
|
|
8
|
+
}
|
|
9
|
+
return { timestamp: ts, is_all_day: input.is_all_day ?? false };
|
|
10
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Type } from "@sinclair/typebox";
|
|
2
|
+
import type { OpenClawPluginApi } from "./nextclaw-sdk/feishu.js";
|
|
3
|
+
import { normalizeTaskTime } from "./task-shared.js";
|
|
4
|
+
import { assertLarkOk, createToolContext, handleInvokeError, json, registerTool, StringEnum } from "./user-tool-helpers.js";
|
|
5
|
+
|
|
6
|
+
const SubtaskSchema = Type.Union([
|
|
7
|
+
Type.Object({
|
|
8
|
+
action: Type.Literal("create"),
|
|
9
|
+
task_guid: Type.String(),
|
|
10
|
+
summary: Type.String(),
|
|
11
|
+
description: Type.Optional(Type.String()),
|
|
12
|
+
due: Type.Optional(Type.Object({ timestamp: Type.String(), is_all_day: Type.Optional(Type.Boolean()) })),
|
|
13
|
+
start: Type.Optional(Type.Object({ timestamp: Type.String(), is_all_day: Type.Optional(Type.Boolean()) })),
|
|
14
|
+
members: Type.Optional(Type.Array(Type.Object({ id: Type.String(), role: Type.Optional(StringEnum(["assignee", "follower"])) }))),
|
|
15
|
+
}),
|
|
16
|
+
Type.Object({ action: Type.Literal("list"), task_guid: Type.String(), page_size: Type.Optional(Type.Number()), page_token: Type.Optional(Type.String()) }),
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
export function registerFeishuTaskSubtaskTool(api: OpenClawPluginApi) {
|
|
20
|
+
const { toolClient } = createToolContext(api, "feishu_task_subtask");
|
|
21
|
+
registerTool(api, {
|
|
22
|
+
name: "feishu_task_subtask",
|
|
23
|
+
label: "Feishu Task Subtask",
|
|
24
|
+
description: "按本人身份创建、列出任务的子任务。",
|
|
25
|
+
parameters: SubtaskSchema,
|
|
26
|
+
async execute(_toolCallId, params) {
|
|
27
|
+
const payload = params as {
|
|
28
|
+
action: "create" | "list";
|
|
29
|
+
task_guid: string;
|
|
30
|
+
summary?: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
due?: { timestamp?: string; is_all_day?: boolean };
|
|
33
|
+
start?: { timestamp?: string; is_all_day?: boolean };
|
|
34
|
+
members?: Array<{ id: string; role?: string }>;
|
|
35
|
+
page_size?: number;
|
|
36
|
+
page_token?: string;
|
|
37
|
+
};
|
|
38
|
+
try {
|
|
39
|
+
const client = toolClient();
|
|
40
|
+
if (payload.action === "create") {
|
|
41
|
+
const response = await client.invoke(
|
|
42
|
+
"feishu_task_subtask.create",
|
|
43
|
+
(sdk, opts) =>
|
|
44
|
+
sdk.task.v2.taskSubtask.create(
|
|
45
|
+
{
|
|
46
|
+
path: { task_guid: payload.task_guid },
|
|
47
|
+
params: { user_id_type: "open_id" as never },
|
|
48
|
+
data: {
|
|
49
|
+
summary: payload.summary,
|
|
50
|
+
description: payload.description,
|
|
51
|
+
due: normalizeTaskTime(payload.due),
|
|
52
|
+
start: normalizeTaskTime(payload.start),
|
|
53
|
+
members: payload.members?.map((member) => ({
|
|
54
|
+
id: member.id,
|
|
55
|
+
type: "user",
|
|
56
|
+
role: member.role ?? "assignee",
|
|
57
|
+
})),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
opts,
|
|
61
|
+
),
|
|
62
|
+
{ as: "user" },
|
|
63
|
+
);
|
|
64
|
+
assertLarkOk(response);
|
|
65
|
+
return json({ subtask: (response.data as { subtask?: unknown } | undefined)?.subtask });
|
|
66
|
+
}
|
|
67
|
+
const response = await client.invoke(
|
|
68
|
+
"feishu_task_subtask.list",
|
|
69
|
+
(sdk, opts) =>
|
|
70
|
+
sdk.task.v2.taskSubtask.list(
|
|
71
|
+
{
|
|
72
|
+
path: { task_guid: payload.task_guid },
|
|
73
|
+
params: {
|
|
74
|
+
page_size: payload.page_size,
|
|
75
|
+
page_token: payload.page_token,
|
|
76
|
+
user_id_type: "open_id" as never,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
opts,
|
|
80
|
+
),
|
|
81
|
+
{ as: "user" },
|
|
82
|
+
);
|
|
83
|
+
assertLarkOk(response);
|
|
84
|
+
return json({
|
|
85
|
+
subtasks: (response.data as { items?: unknown[] } | undefined)?.items ?? [],
|
|
86
|
+
has_more: (response.data as { has_more?: boolean } | undefined)?.has_more ?? false,
|
|
87
|
+
page_token: (response.data as { page_token?: string } | undefined)?.page_token,
|
|
88
|
+
});
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return handleInvokeError(error, api);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
}, { name: "feishu_task_subtask" });
|
|
94
|
+
}
|