@nextclaw/channel-plugin-feishu 0.2.17 → 0.2.19
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/calendar-shared.ts +2 -1
- package/src/sheets-shared.ts +318 -0
- package/src/sheets.ts +18 -290
- package/src/task-tasklist.ts +22 -2
package/package.json
CHANGED
package/src/calendar-shared.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { createToolContext
|
|
1
|
+
import type { createToolContext } from "./user-tool-helpers.js";
|
|
2
|
+
import { assertLarkOk, unixTimestampToISO8601 } from "./user-tool-helpers.js";
|
|
2
3
|
|
|
3
4
|
export function normalizeEventTimes<T extends Record<string, unknown> | undefined>(event: T): T {
|
|
4
5
|
if (!event) return event;
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import type { createToolContext } from "./user-tool-helpers.js";
|
|
2
|
+
import { assertLarkOk, json } from "./user-tool-helpers.js";
|
|
3
|
+
import { wwwDomain } from "./domains.js";
|
|
4
|
+
|
|
5
|
+
const MAX_READ_ROWS = 200;
|
|
6
|
+
const MAX_WRITE_ROWS = 5000;
|
|
7
|
+
const MAX_WRITE_COLS = 100;
|
|
8
|
+
|
|
9
|
+
type UserToolClient = ReturnType<ReturnType<typeof createToolContext>["toolClient"]>;
|
|
10
|
+
|
|
11
|
+
export type SheetParams = {
|
|
12
|
+
action: "info" | "read" | "write" | "append" | "find" | "create" | "export";
|
|
13
|
+
url?: string;
|
|
14
|
+
spreadsheet_token?: string;
|
|
15
|
+
range?: string;
|
|
16
|
+
sheet_id?: string;
|
|
17
|
+
value_render_option?: "FormattedValue" | "UnformattedValue" | "Formula" | "ToString";
|
|
18
|
+
values?: unknown[][];
|
|
19
|
+
find?: string;
|
|
20
|
+
match_case?: boolean;
|
|
21
|
+
match_entire_cell?: boolean;
|
|
22
|
+
search_by_regex?: boolean;
|
|
23
|
+
include_formulas?: boolean;
|
|
24
|
+
title?: string;
|
|
25
|
+
folder_token?: string;
|
|
26
|
+
headers?: unknown[];
|
|
27
|
+
data?: unknown[][];
|
|
28
|
+
file_extension?: "xlsx" | "csv";
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type SheetTokenInfo = {
|
|
32
|
+
token: string;
|
|
33
|
+
sheetId?: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function parseSheetUrl(url: string): SheetTokenInfo | null {
|
|
37
|
+
try {
|
|
38
|
+
const parsed = new URL(url);
|
|
39
|
+
const match = parsed.pathname.match(/\/sheets\/([^/?#]+)/);
|
|
40
|
+
if (!match) return null;
|
|
41
|
+
return { token: match[1], sheetId: parsed.searchParams.get("sheet") || undefined };
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function resolveToken(params: SheetParams): SheetTokenInfo | null {
|
|
48
|
+
if (params.spreadsheet_token) return { token: params.spreadsheet_token };
|
|
49
|
+
if (params.url) return parseSheetUrl(params.url);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function colLetter(n: number): string {
|
|
54
|
+
let result = "";
|
|
55
|
+
let value = n;
|
|
56
|
+
while (value > 0) {
|
|
57
|
+
value -= 1;
|
|
58
|
+
result = String.fromCharCode(65 + (value % 26)) + result;
|
|
59
|
+
value = Math.floor(value / 26);
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function flattenCellValue(cell: unknown): unknown {
|
|
65
|
+
if (!Array.isArray(cell)) return cell;
|
|
66
|
+
if (cell.length > 0 && cell.every((segment) => segment && typeof segment === "object" && "text" in (segment as object))) {
|
|
67
|
+
return cell.map((segment) => String((segment as { text?: unknown }).text ?? "")).join("");
|
|
68
|
+
}
|
|
69
|
+
return cell;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function resolveSheetRange(
|
|
73
|
+
client: UserToolClient,
|
|
74
|
+
token: string,
|
|
75
|
+
range?: string,
|
|
76
|
+
sheetId?: string,
|
|
77
|
+
) {
|
|
78
|
+
if (range) return range;
|
|
79
|
+
if (sheetId) return sheetId;
|
|
80
|
+
const response = await client.invoke(
|
|
81
|
+
"feishu_sheet.info",
|
|
82
|
+
(sdk, opts) => sdk.sheets.spreadsheetSheet.query({ path: { spreadsheet_token: token } }, opts),
|
|
83
|
+
{ as: "user" },
|
|
84
|
+
);
|
|
85
|
+
assertLarkOk(response);
|
|
86
|
+
const firstSheet = (response.data as { sheets?: Array<{ sheet_id?: string }> } | undefined)?.sheets?.[0];
|
|
87
|
+
if (!firstSheet?.sheet_id) {
|
|
88
|
+
throw new Error("Spreadsheet has no worksheets.");
|
|
89
|
+
}
|
|
90
|
+
return firstSheet.sheet_id;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function handleCreate(client: UserToolClient, payload: SheetParams) {
|
|
94
|
+
const createResponse = await client.invoke(
|
|
95
|
+
"feishu_sheet.create",
|
|
96
|
+
(sdk, opts) =>
|
|
97
|
+
sdk.sheets.spreadsheet.create(
|
|
98
|
+
{ data: { title: payload.title, folder_token: payload.folder_token } },
|
|
99
|
+
opts,
|
|
100
|
+
),
|
|
101
|
+
{ as: "user" },
|
|
102
|
+
);
|
|
103
|
+
assertLarkOk(createResponse);
|
|
104
|
+
const spreadsheet = (createResponse.data as { spreadsheet?: { spreadsheet_token?: string; title?: string } } | undefined)?.spreadsheet;
|
|
105
|
+
const token = spreadsheet?.spreadsheet_token;
|
|
106
|
+
if (!token) {
|
|
107
|
+
return json({ error: "创建电子表格失败,未返回 spreadsheet_token。" });
|
|
108
|
+
}
|
|
109
|
+
const allRows = [
|
|
110
|
+
...(payload.headers ? [payload.headers] : []),
|
|
111
|
+
...((payload.data as unknown[][] | undefined) ?? []),
|
|
112
|
+
];
|
|
113
|
+
if (allRows.length > 0) {
|
|
114
|
+
const sheetsResponse = await client.invoke(
|
|
115
|
+
"feishu_sheet.create",
|
|
116
|
+
(sdk, opts) => sdk.sheets.spreadsheetSheet.query({ path: { spreadsheet_token: token } }, opts),
|
|
117
|
+
{ as: "user" },
|
|
118
|
+
);
|
|
119
|
+
assertLarkOk(sheetsResponse);
|
|
120
|
+
const firstSheet = (sheetsResponse.data as { sheets?: Array<{ sheet_id?: string }> } | undefined)?.sheets?.[0];
|
|
121
|
+
if (firstSheet?.sheet_id) {
|
|
122
|
+
const numCols = Math.max(...allRows.map((row) => row.length), 1);
|
|
123
|
+
const range = `${firstSheet.sheet_id}!A1:${colLetter(numCols)}${allRows.length}`;
|
|
124
|
+
await client.invokeByPath("feishu_sheet.create", `/open-apis/sheets/v2/spreadsheets/${token}/values`, {
|
|
125
|
+
method: "PUT",
|
|
126
|
+
body: { valueRange: { range, values: allRows } },
|
|
127
|
+
as: "user",
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return json({
|
|
132
|
+
spreadsheet_token: token,
|
|
133
|
+
title: spreadsheet?.title ?? payload.title,
|
|
134
|
+
url: `${wwwDomain(client.account.domain)}/sheets/${token}`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function handleInfo(client: UserToolClient, token: string) {
|
|
139
|
+
const [spreadsheetRes, sheetsRes] = await Promise.all([
|
|
140
|
+
client.invoke(
|
|
141
|
+
"feishu_sheet.info",
|
|
142
|
+
(sdk, opts) => sdk.sheets.spreadsheet.get({ path: { spreadsheet_token: token } }, opts),
|
|
143
|
+
{ as: "user" },
|
|
144
|
+
),
|
|
145
|
+
client.invoke(
|
|
146
|
+
"feishu_sheet.info",
|
|
147
|
+
(sdk, opts) => sdk.sheets.spreadsheetSheet.query({ path: { spreadsheet_token: token } }, opts),
|
|
148
|
+
{ as: "user" },
|
|
149
|
+
),
|
|
150
|
+
]);
|
|
151
|
+
assertLarkOk(spreadsheetRes);
|
|
152
|
+
assertLarkOk(sheetsRes);
|
|
153
|
+
return json({
|
|
154
|
+
title: (spreadsheetRes.data as { spreadsheet?: { title?: string } } | undefined)?.spreadsheet?.title,
|
|
155
|
+
spreadsheet_token: token,
|
|
156
|
+
url: `${wwwDomain(client.account.domain)}/sheets/${token}`,
|
|
157
|
+
sheets:
|
|
158
|
+
((sheetsRes.data as { sheets?: Array<Record<string, unknown>> } | undefined)?.sheets ?? []).map((sheet) => ({
|
|
159
|
+
sheet_id: sheet.sheet_id,
|
|
160
|
+
title: sheet.title,
|
|
161
|
+
index: sheet.index,
|
|
162
|
+
row_count: (sheet.grid_properties as { row_count?: number } | undefined)?.row_count,
|
|
163
|
+
column_count: (sheet.grid_properties as { column_count?: number } | undefined)?.column_count,
|
|
164
|
+
})),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export async function handleRead(client: UserToolClient, tokenInfo: SheetTokenInfo, payload: SheetParams) {
|
|
169
|
+
const range = await resolveSheetRange(client, tokenInfo.token, payload.range, payload.sheet_id ?? tokenInfo.sheetId);
|
|
170
|
+
const response = await client.invokeByPath<{
|
|
171
|
+
data?: { valueRange?: { range?: string; values?: unknown[][] } };
|
|
172
|
+
}>("feishu_sheet.read", `/open-apis/sheets/v2/spreadsheets/${tokenInfo.token}/values/${encodeURIComponent(range)}`, {
|
|
173
|
+
method: "GET",
|
|
174
|
+
query: {
|
|
175
|
+
valueRenderOption: payload.value_render_option ?? "ToString",
|
|
176
|
+
dateTimeRenderOption: "FormattedString",
|
|
177
|
+
},
|
|
178
|
+
as: "user",
|
|
179
|
+
});
|
|
180
|
+
const values = (response.data?.valueRange?.values ?? []).map((row) => row.map(flattenCellValue));
|
|
181
|
+
return json({
|
|
182
|
+
range: response.data?.valueRange?.range,
|
|
183
|
+
values: values.slice(0, MAX_READ_ROWS),
|
|
184
|
+
...(values.length > MAX_READ_ROWS
|
|
185
|
+
? {
|
|
186
|
+
truncated: true,
|
|
187
|
+
total_rows: values.length,
|
|
188
|
+
hint: `结果超过 ${MAX_READ_ROWS} 行,已截断。请缩小 range 后重试。`,
|
|
189
|
+
}
|
|
190
|
+
: {}),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export async function handleWrite(client: UserToolClient, tokenInfo: SheetTokenInfo, payload: SheetParams) {
|
|
195
|
+
if ((payload.values?.length ?? 0) > MAX_WRITE_ROWS) {
|
|
196
|
+
return json({ error: `写入行数超过限制 ${MAX_WRITE_ROWS}` });
|
|
197
|
+
}
|
|
198
|
+
if ((payload.values ?? []).some((row) => row.length > MAX_WRITE_COLS)) {
|
|
199
|
+
return json({ error: `写入列数超过限制 ${MAX_WRITE_COLS}` });
|
|
200
|
+
}
|
|
201
|
+
const range = await resolveSheetRange(client, tokenInfo.token, payload.range, payload.sheet_id ?? tokenInfo.sheetId);
|
|
202
|
+
const response = await client.invokeByPath<{
|
|
203
|
+
data?: {
|
|
204
|
+
updatedRange?: string;
|
|
205
|
+
updatedRows?: number;
|
|
206
|
+
updatedColumns?: number;
|
|
207
|
+
updatedCells?: number;
|
|
208
|
+
revision?: number;
|
|
209
|
+
updates?: {
|
|
210
|
+
updatedRange?: string;
|
|
211
|
+
updatedRows?: number;
|
|
212
|
+
updatedColumns?: number;
|
|
213
|
+
updatedCells?: number;
|
|
214
|
+
revision?: number;
|
|
215
|
+
};
|
|
216
|
+
};
|
|
217
|
+
}>(
|
|
218
|
+
payload.action === "write" ? "feishu_sheet.write" : "feishu_sheet.append",
|
|
219
|
+
`/open-apis/sheets/v2/spreadsheets/${tokenInfo.token}/${payload.action === "write" ? "values" : "values_append"}`,
|
|
220
|
+
{
|
|
221
|
+
method: payload.action === "write" ? "PUT" : "POST",
|
|
222
|
+
body: { valueRange: { range, values: payload.values } },
|
|
223
|
+
as: "user",
|
|
224
|
+
},
|
|
225
|
+
);
|
|
226
|
+
const updates = response.data?.updates ?? response.data;
|
|
227
|
+
return json({
|
|
228
|
+
updated_range: updates?.updatedRange,
|
|
229
|
+
updated_rows: updates?.updatedRows,
|
|
230
|
+
updated_columns: updates?.updatedColumns,
|
|
231
|
+
updated_cells: updates?.updatedCells,
|
|
232
|
+
revision: updates?.revision,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export async function handleFind(client: UserToolClient, token: string, payload: SheetParams) {
|
|
237
|
+
const response = await client.invoke(
|
|
238
|
+
"feishu_sheet.find",
|
|
239
|
+
(sdk, opts) =>
|
|
240
|
+
sdk.sheets.spreadsheetSheet.find(
|
|
241
|
+
{
|
|
242
|
+
path: { spreadsheet_token: token, sheet_id: payload.sheet_id! },
|
|
243
|
+
data: {
|
|
244
|
+
find: payload.find,
|
|
245
|
+
find_condition: {
|
|
246
|
+
range: payload.range ? `${payload.sheet_id}!${payload.range}` : payload.sheet_id,
|
|
247
|
+
...(payload.match_case !== undefined ? { match_case: !payload.match_case } : {}),
|
|
248
|
+
...(payload.match_entire_cell !== undefined ? { match_entire_cell: payload.match_entire_cell } : {}),
|
|
249
|
+
...(payload.search_by_regex !== undefined ? { search_by_regex: payload.search_by_regex } : {}),
|
|
250
|
+
...(payload.include_formulas !== undefined ? { include_formulas: payload.include_formulas } : {}),
|
|
251
|
+
},
|
|
252
|
+
},
|
|
253
|
+
},
|
|
254
|
+
opts,
|
|
255
|
+
),
|
|
256
|
+
{ as: "user" },
|
|
257
|
+
);
|
|
258
|
+
assertLarkOk(response);
|
|
259
|
+
return json({
|
|
260
|
+
matched_cells: (response.data as { find_result?: { matched_cells?: unknown[] } } | undefined)?.find_result?.matched_cells ?? [],
|
|
261
|
+
matched_formula_cells:
|
|
262
|
+
(response.data as { find_result?: { matched_formula_cells?: unknown[] } } | undefined)?.find_result?.matched_formula_cells ?? [],
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export async function handleExport(client: UserToolClient, token: string, payload: SheetParams) {
|
|
267
|
+
const exportCreate = await client.invoke(
|
|
268
|
+
"feishu_sheet.export",
|
|
269
|
+
(sdk, opts) =>
|
|
270
|
+
sdk.drive.exportTask.create(
|
|
271
|
+
{
|
|
272
|
+
data: {
|
|
273
|
+
file_extension: payload.file_extension,
|
|
274
|
+
token,
|
|
275
|
+
type: "sheet",
|
|
276
|
+
sub_id: payload.sheet_id,
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
opts,
|
|
280
|
+
),
|
|
281
|
+
{ as: "user" },
|
|
282
|
+
);
|
|
283
|
+
assertLarkOk(exportCreate);
|
|
284
|
+
const ticket = (exportCreate.data as { ticket?: string } | undefined)?.ticket;
|
|
285
|
+
if (!ticket) {
|
|
286
|
+
return json({ error: "导出任务创建失败,未返回 ticket。" });
|
|
287
|
+
}
|
|
288
|
+
for (let i = 0; i < 30; i += 1) {
|
|
289
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
290
|
+
const exportStatus = await client.invoke(
|
|
291
|
+
"feishu_sheet.export",
|
|
292
|
+
(sdk, opts) => sdk.drive.exportTask.get({ path: { ticket }, params: { token } }, opts),
|
|
293
|
+
{ as: "user" },
|
|
294
|
+
);
|
|
295
|
+
assertLarkOk(exportStatus);
|
|
296
|
+
const result = (exportStatus.data as {
|
|
297
|
+
result?: {
|
|
298
|
+
job_status?: number;
|
|
299
|
+
file_token?: string;
|
|
300
|
+
file_name?: string;
|
|
301
|
+
file_size?: number;
|
|
302
|
+
job_error_msg?: string;
|
|
303
|
+
};
|
|
304
|
+
} | undefined)?.result;
|
|
305
|
+
if (result?.job_status === 0) {
|
|
306
|
+
return json({
|
|
307
|
+
file_token: result.file_token,
|
|
308
|
+
file_name: result.file_name,
|
|
309
|
+
file_size: result.file_size,
|
|
310
|
+
file_extension: payload.file_extension,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
if ((result?.job_status ?? 0) >= 3) {
|
|
314
|
+
return json({ error: result?.job_error_msg ?? `导出失败 (status=${result?.job_status})` });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return json({ error: "导出任务超时,请稍后重试。" });
|
|
318
|
+
}
|
package/src/sheets.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { Type } from "@sinclair/typebox";
|
|
2
2
|
import type { OpenClawPluginApi } from "./nextclaw-sdk/feishu.js";
|
|
3
3
|
import { listEnabledFeishuAccounts } from "./accounts.js";
|
|
4
|
-
import { wwwDomain } from "./domains.js";
|
|
5
|
-
import { resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js";
|
|
6
4
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
type SheetParams,
|
|
6
|
+
handleCreate,
|
|
7
|
+
handleExport,
|
|
8
|
+
handleFind,
|
|
9
|
+
handleInfo,
|
|
10
|
+
handleRead,
|
|
11
|
+
handleWrite,
|
|
12
|
+
resolveToken,
|
|
13
|
+
} from "./sheets-shared.js";
|
|
14
|
+
import { resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js";
|
|
15
|
+
import { createToolContext, handleInvokeError, registerTool, StringEnum } from "./user-tool-helpers.js";
|
|
18
16
|
|
|
19
17
|
const SheetSchema = Type.Union([
|
|
20
18
|
Type.Object({ action: Type.Literal("info"), url: Type.Optional(Type.String()), spreadsheet_token: Type.Optional(Type.String()) }),
|
|
@@ -70,74 +68,6 @@ const SheetSchema = Type.Union([
|
|
|
70
68
|
}),
|
|
71
69
|
]);
|
|
72
70
|
|
|
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
71
|
export function registerFeishuSheetsTools(api: OpenClawPluginApi) {
|
|
142
72
|
if (!api.config) return;
|
|
143
73
|
const accounts = listEnabledFeishuAccounts(api.config);
|
|
@@ -150,222 +80,20 @@ export function registerFeishuSheetsTools(api: OpenClawPluginApi) {
|
|
|
150
80
|
description: "按本人身份读取、写入、创建、查找和导出飞书电子表格。",
|
|
151
81
|
parameters: SheetSchema,
|
|
152
82
|
async execute(_toolCallId, params) {
|
|
153
|
-
const payload = params as
|
|
83
|
+
const payload = params as SheetParams;
|
|
154
84
|
try {
|
|
155
85
|
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
|
-
}
|
|
86
|
+
if (payload.action === "create") return handleCreate(client, payload);
|
|
206
87
|
|
|
207
88
|
const tokenInfo = resolveToken(payload);
|
|
208
89
|
if (!tokenInfo?.token) {
|
|
209
90
|
return json({ error: "请传 spreadsheet_token 或 /sheets/TOKEN 形式的 url。" });
|
|
210
91
|
}
|
|
211
|
-
|
|
212
|
-
if (payload.action === "
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
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: "导出任务超时,请稍后重试。" });
|
|
92
|
+
if (payload.action === "info") return handleInfo(client, tokenInfo.token);
|
|
93
|
+
if (payload.action === "read") return handleRead(client, tokenInfo, payload);
|
|
94
|
+
if (payload.action === "write" || payload.action === "append") return handleWrite(client, tokenInfo, payload);
|
|
95
|
+
if (payload.action === "find") return handleFind(client, tokenInfo.token, payload);
|
|
96
|
+
return handleExport(client, tokenInfo.token, payload);
|
|
369
97
|
} catch (error) {
|
|
370
98
|
return handleInvokeError(error, api);
|
|
371
99
|
}
|
package/src/task-tasklist.ts
CHANGED
|
@@ -3,12 +3,32 @@ import type { OpenClawPluginApi } from "./nextclaw-sdk/feishu.js";
|
|
|
3
3
|
import { assertLarkOk, createToolContext, handleInvokeError, json, registerTool, StringEnum } from "./user-tool-helpers.js";
|
|
4
4
|
|
|
5
5
|
const TasklistSchema = Type.Union([
|
|
6
|
-
Type.Object({
|
|
6
|
+
Type.Object({
|
|
7
|
+
action: Type.Literal("create"),
|
|
8
|
+
name: Type.String(),
|
|
9
|
+
members: Type.Optional(
|
|
10
|
+
Type.Array(
|
|
11
|
+
Type.Object({
|
|
12
|
+
id: Type.String(),
|
|
13
|
+
role: Type.Optional(StringEnum(["editor", "viewer"])),
|
|
14
|
+
}),
|
|
15
|
+
),
|
|
16
|
+
),
|
|
17
|
+
}),
|
|
7
18
|
Type.Object({ action: Type.Literal("get"), tasklist_guid: Type.String() }),
|
|
8
19
|
Type.Object({ action: Type.Literal("list"), page_size: Type.Optional(Type.Number()), page_token: Type.Optional(Type.String()) }),
|
|
9
20
|
Type.Object({ action: Type.Literal("tasks"), tasklist_guid: Type.String(), page_size: Type.Optional(Type.Number()), page_token: Type.Optional(Type.String()), completed: Type.Optional(Type.Boolean()) }),
|
|
10
21
|
Type.Object({ action: Type.Literal("patch"), tasklist_guid: Type.String(), name: Type.Optional(Type.String()) }),
|
|
11
|
-
Type.Object({
|
|
22
|
+
Type.Object({
|
|
23
|
+
action: Type.Literal("add_members"),
|
|
24
|
+
tasklist_guid: Type.String(),
|
|
25
|
+
members: Type.Array(
|
|
26
|
+
Type.Object({
|
|
27
|
+
id: Type.String(),
|
|
28
|
+
role: Type.Optional(StringEnum(["editor", "viewer"])),
|
|
29
|
+
}),
|
|
30
|
+
),
|
|
31
|
+
}),
|
|
12
32
|
]);
|
|
13
33
|
|
|
14
34
|
export function registerFeishuTaskTasklistTool(api: OpenClawPluginApi) {
|