@openclaw/feishu 2026.3.13 → 2026.5.1-beta.1

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.
Files changed (188) hide show
  1. package/api.ts +31 -0
  2. package/channel-entry.ts +20 -0
  3. package/channel-plugin-api.ts +1 -0
  4. package/contract-api.ts +16 -0
  5. package/index.ts +70 -53
  6. package/openclaw.plugin.json +1653 -4
  7. package/package.json +32 -7
  8. package/runtime-api.ts +55 -0
  9. package/secret-contract-api.ts +5 -0
  10. package/security-contract-api.ts +1 -0
  11. package/session-key-api.ts +1 -0
  12. package/setup-api.ts +3 -0
  13. package/setup-entry.test.ts +14 -0
  14. package/setup-entry.ts +13 -0
  15. package/src/accounts.test.ts +95 -7
  16. package/src/accounts.ts +199 -117
  17. package/src/app-registration.ts +331 -0
  18. package/src/approval-auth.test.ts +24 -0
  19. package/src/approval-auth.ts +25 -0
  20. package/src/async.test.ts +35 -0
  21. package/src/async.ts +43 -1
  22. package/src/audio-preflight.runtime.ts +9 -0
  23. package/src/bitable.test.ts +131 -0
  24. package/src/bitable.ts +59 -22
  25. package/src/bot-content.ts +474 -0
  26. package/src/bot-group-name.test.ts +108 -0
  27. package/src/bot-runtime-api.ts +12 -0
  28. package/src/bot-sender-name.ts +125 -0
  29. package/src/bot.broadcast.test.ts +463 -0
  30. package/src/bot.card-action.test.ts +519 -5
  31. package/src/bot.checkBotMentioned.test.ts +92 -20
  32. package/src/bot.helpers.test.ts +118 -0
  33. package/src/bot.stripBotMention.test.ts +13 -21
  34. package/src/bot.test.ts +1334 -401
  35. package/src/bot.ts +778 -775
  36. package/src/card-action.ts +408 -40
  37. package/src/card-interaction.test.ts +129 -0
  38. package/src/card-interaction.ts +159 -0
  39. package/src/card-test-helpers.ts +47 -0
  40. package/src/card-ux-approval.ts +65 -0
  41. package/src/card-ux-launcher.test.ts +99 -0
  42. package/src/card-ux-launcher.ts +121 -0
  43. package/src/card-ux-shared.ts +33 -0
  44. package/src/channel-runtime-api.ts +16 -0
  45. package/src/channel.runtime.ts +47 -0
  46. package/src/channel.test.ts +914 -3
  47. package/src/channel.ts +1252 -309
  48. package/src/chat-schema.ts +5 -4
  49. package/src/chat.test.ts +84 -28
  50. package/src/chat.ts +68 -10
  51. package/src/client.test.ts +212 -103
  52. package/src/client.ts +115 -21
  53. package/src/comment-dispatcher-runtime-api.ts +6 -0
  54. package/src/comment-dispatcher.test.ts +169 -0
  55. package/src/comment-dispatcher.ts +107 -0
  56. package/src/comment-handler-runtime-api.ts +3 -0
  57. package/src/comment-handler.test.ts +486 -0
  58. package/src/comment-handler.ts +309 -0
  59. package/src/comment-reaction.test.ts +166 -0
  60. package/src/comment-reaction.ts +259 -0
  61. package/src/comment-shared.test.ts +182 -0
  62. package/src/comment-shared.ts +365 -0
  63. package/src/comment-target.ts +44 -0
  64. package/src/config-schema.test.ts +63 -1
  65. package/src/config-schema.ts +31 -4
  66. package/src/conversation-id.test.ts +18 -0
  67. package/src/conversation-id.ts +199 -0
  68. package/src/dedup-runtime-api.ts +1 -0
  69. package/src/dedup.ts +32 -94
  70. package/src/directory.static.ts +61 -0
  71. package/src/directory.test.ts +119 -20
  72. package/src/directory.ts +61 -91
  73. package/src/doc-schema.ts +1 -1
  74. package/src/docx-batch-insert.test.ts +39 -38
  75. package/src/docx-batch-insert.ts +55 -19
  76. package/src/docx-color-text.ts +9 -4
  77. package/src/docx-table-ops.test.ts +53 -0
  78. package/src/docx-table-ops.ts +52 -34
  79. package/src/docx-types.ts +38 -0
  80. package/src/docx.account-selection.test.ts +12 -3
  81. package/src/docx.test.ts +314 -74
  82. package/src/docx.ts +278 -122
  83. package/src/drive-schema.ts +47 -1
  84. package/src/drive.test.ts +1219 -0
  85. package/src/drive.ts +614 -13
  86. package/src/dynamic-agent.ts +10 -4
  87. package/src/event-types.ts +45 -0
  88. package/src/external-keys.ts +1 -1
  89. package/src/lifecycle.test-support.ts +220 -0
  90. package/src/media.test.ts +375 -26
  91. package/src/media.ts +434 -88
  92. package/src/mention-target.types.ts +5 -0
  93. package/src/mention.ts +32 -51
  94. package/src/message-action-contract.ts +13 -0
  95. package/src/monitor-state-runtime-api.ts +7 -0
  96. package/src/monitor-transport-runtime-api.ts +7 -0
  97. package/src/monitor.account.ts +218 -312
  98. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +219 -0
  99. package/src/monitor.bot-identity.ts +86 -0
  100. package/src/monitor.bot-menu-handler.ts +165 -0
  101. package/src/monitor.bot-menu.lifecycle.test-support.ts +224 -0
  102. package/src/monitor.bot-menu.test.ts +178 -0
  103. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +264 -0
  104. package/src/monitor.card-action.lifecycle.test-support.ts +373 -0
  105. package/src/monitor.cleanup.test.ts +376 -0
  106. package/src/monitor.comment-notice-handler.ts +105 -0
  107. package/src/monitor.comment.test.ts +937 -0
  108. package/src/monitor.comment.ts +1386 -0
  109. package/src/monitor.lifecycle.test.ts +4 -0
  110. package/src/monitor.message-handler.ts +339 -0
  111. package/src/monitor.reaction.lifecycle.test-support.ts +68 -0
  112. package/src/monitor.reaction.test.ts +108 -48
  113. package/src/monitor.reply-once.lifecycle.test-support.ts +190 -0
  114. package/src/monitor.startup.test.ts +11 -9
  115. package/src/monitor.startup.ts +26 -16
  116. package/src/monitor.state.ts +20 -5
  117. package/src/monitor.synthetic-error.ts +18 -0
  118. package/src/monitor.test-mocks.ts +2 -2
  119. package/src/monitor.transport.ts +220 -60
  120. package/src/monitor.ts +15 -10
  121. package/src/monitor.webhook-e2e.test.ts +65 -7
  122. package/src/monitor.webhook-security.test.ts +122 -0
  123. package/src/monitor.webhook.test-helpers.ts +44 -26
  124. package/src/outbound-runtime-api.ts +1 -0
  125. package/src/outbound.test.ts +616 -37
  126. package/src/outbound.ts +623 -81
  127. package/src/perm-schema.ts +1 -1
  128. package/src/perm.ts +1 -7
  129. package/src/pins.ts +108 -0
  130. package/src/policy.test.ts +297 -117
  131. package/src/policy.ts +142 -29
  132. package/src/post.ts +7 -6
  133. package/src/probe.test.ts +14 -9
  134. package/src/probe.ts +26 -16
  135. package/src/processing-claims.ts +59 -0
  136. package/src/qr-terminal.ts +1 -0
  137. package/src/reactions.ts +4 -34
  138. package/src/reasoning-preview.test.ts +59 -0
  139. package/src/reasoning-preview.ts +20 -0
  140. package/src/reply-dispatcher-runtime-api.ts +7 -0
  141. package/src/reply-dispatcher.test.ts +660 -29
  142. package/src/reply-dispatcher.ts +407 -154
  143. package/src/runtime.ts +6 -3
  144. package/src/secret-contract.ts +145 -0
  145. package/src/secret-input.ts +1 -13
  146. package/src/security-audit-shared.ts +69 -0
  147. package/src/security-audit.test.ts +61 -0
  148. package/src/security-audit.ts +1 -0
  149. package/src/send-result.ts +1 -1
  150. package/src/send-target.test.ts +9 -3
  151. package/src/send-target.ts +10 -4
  152. package/src/send.reply-fallback.test.ts +77 -2
  153. package/src/send.test.ts +386 -4
  154. package/src/send.ts +399 -86
  155. package/src/sequential-key.test.ts +72 -0
  156. package/src/sequential-key.ts +28 -0
  157. package/src/sequential-queue.test.ts +92 -0
  158. package/src/sequential-queue.ts +16 -0
  159. package/src/session-conversation.ts +42 -0
  160. package/src/session-route.ts +48 -0
  161. package/src/setup-core.ts +51 -0
  162. package/src/{onboarding.test.ts → setup-surface.test.ts} +52 -21
  163. package/src/setup-surface.ts +581 -0
  164. package/src/streaming-card.test.ts +138 -2
  165. package/src/streaming-card.ts +134 -18
  166. package/src/subagent-hooks.test.ts +603 -0
  167. package/src/subagent-hooks.ts +397 -0
  168. package/src/targets.ts +3 -13
  169. package/src/test-support/lifecycle-test-support.ts +479 -0
  170. package/src/thread-bindings.test.ts +143 -0
  171. package/src/thread-bindings.ts +330 -0
  172. package/src/tool-account-routing.test.ts +66 -8
  173. package/src/tool-account.test.ts +44 -0
  174. package/src/tool-account.ts +40 -17
  175. package/src/tool-factory-test-harness.ts +11 -8
  176. package/src/tool-result.ts +3 -1
  177. package/src/tools-config.ts +1 -1
  178. package/src/types.ts +16 -15
  179. package/src/typing.ts +10 -6
  180. package/src/wiki-schema.ts +1 -1
  181. package/src/wiki.ts +1 -7
  182. package/subagent-hooks-api.ts +31 -0
  183. package/tsconfig.json +16 -0
  184. package/src/feishu-command-handler.ts +0 -59
  185. package/src/onboarding.status.test.ts +0 -25
  186. package/src/onboarding.ts +0 -489
  187. package/src/send-message.ts +0 -71
  188. package/src/targets.test.ts +0 -70
package/src/drive.ts CHANGED
@@ -1,6 +1,16 @@
1
1
  import type * as Lark from "@larksuiteoapi/node-sdk";
2
- import type { OpenClawPluginApi } from "openclaw/plugin-sdk/feishu";
2
+ import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
3
+ import type { OpenClawPluginApi } from "../runtime-api.js";
3
4
  import { listEnabledFeishuAccounts } from "./accounts.js";
5
+ import { cleanupAmbientCommentTypingReaction } from "./comment-reaction.js";
6
+ import {
7
+ encodeQuery,
8
+ extractReplyText,
9
+ formatFeishuApiError,
10
+ isRecord,
11
+ readString,
12
+ } from "./comment-shared.js";
13
+ import { parseFeishuCommentTarget, type CommentFileType } from "./comment-target.js";
4
14
  import { FeishuDriveSchema, type FeishuDriveParams } from "./drive-schema.js";
5
15
  import { createFeishuToolClient, resolveAnyEnabledFeishuToolsConfig } from "./tool-account.js";
6
16
  import {
@@ -11,15 +21,302 @@ import {
11
21
 
12
22
  // ============ Actions ============
13
23
 
24
+ type FeishuExplorerRootFolderMetaResponse = {
25
+ code: number;
26
+ msg?: string;
27
+ data?: {
28
+ token?: string;
29
+ };
30
+ };
31
+
32
+ type FeishuDriveInternalClient = Lark.Client & {
33
+ domain?: string;
34
+ httpInstance: Pick<Lark.HttpInstance, "get">;
35
+ request(params: {
36
+ method: "GET" | "POST";
37
+ url: string;
38
+ params?: Record<string, string | undefined>;
39
+ data: unknown;
40
+ timeout?: number;
41
+ }): Promise<unknown>;
42
+ };
43
+
44
+ type FeishuDriveApiResponse<T> = {
45
+ code: number;
46
+ log_id?: string;
47
+ msg?: string;
48
+ data?: T;
49
+ };
50
+
51
+ class FeishuReplyCommentError extends Error {
52
+ httpStatus?: number;
53
+ feishuCode?: number | string;
54
+ feishuMsg?: string;
55
+ feishuLogId?: string;
56
+
57
+ constructor(params: {
58
+ message: string;
59
+ httpStatus?: number;
60
+ feishuCode?: number | string;
61
+ feishuMsg?: string;
62
+ feishuLogId?: string;
63
+ }) {
64
+ super(params.message);
65
+ this.name = "FeishuReplyCommentError";
66
+ this.httpStatus = params.httpStatus;
67
+ this.feishuCode = params.feishuCode;
68
+ this.feishuMsg = params.feishuMsg;
69
+ this.feishuLogId = params.feishuLogId;
70
+ }
71
+ }
72
+
73
+ type FeishuDriveCommentReply = {
74
+ reply_id?: string;
75
+ user_id?: string;
76
+ create_time?: number;
77
+ update_time?: number;
78
+ content?: {
79
+ elements?: unknown[];
80
+ };
81
+ };
82
+
83
+ type FeishuDriveCommentCard = {
84
+ comment_id?: string;
85
+ user_id?: string;
86
+ create_time?: number;
87
+ update_time?: number;
88
+ is_solved?: boolean;
89
+ is_whole?: boolean;
90
+ has_more?: boolean;
91
+ page_token?: string;
92
+ quote?: string;
93
+ reply_list?: {
94
+ replies?: FeishuDriveCommentReply[];
95
+ };
96
+ };
97
+
98
+ type FeishuDriveListCommentsResponse = FeishuDriveApiResponse<{
99
+ has_more?: boolean;
100
+ items?: FeishuDriveCommentCard[];
101
+ page_token?: string;
102
+ }>;
103
+
104
+ type FeishuDriveListRepliesResponse = FeishuDriveApiResponse<{
105
+ has_more?: boolean;
106
+ items?: FeishuDriveCommentReply[];
107
+ page_token?: string;
108
+ }>;
109
+
110
+ type FeishuDriveToolContext = {
111
+ deliveryContext?: {
112
+ channel?: string;
113
+ to?: string;
114
+ threadId?: string | number;
115
+ };
116
+ };
117
+
118
+ const FEISHU_DRIVE_REQUEST_TIMEOUT_MS = 30_000;
119
+
120
+ function getDriveInternalClient(client: Lark.Client): FeishuDriveInternalClient {
121
+ return client as FeishuDriveInternalClient;
122
+ }
123
+
124
+ function buildReplyElements(content: string) {
125
+ return [{ type: "text", text: content }];
126
+ }
127
+
128
+ async function requestDriveApi<T>(params: {
129
+ client: Lark.Client;
130
+ method: "GET" | "POST";
131
+ url: string;
132
+ query?: Record<string, string | undefined>;
133
+ data?: unknown;
134
+ }): Promise<T> {
135
+ const internalClient = getDriveInternalClient(params.client);
136
+ return (await internalClient.request({
137
+ method: params.method,
138
+ url: params.url,
139
+ params: params.query ?? {},
140
+ data: params.data ?? {},
141
+ timeout: FEISHU_DRIVE_REQUEST_TIMEOUT_MS,
142
+ })) as T;
143
+ }
144
+
145
+ function assertDriveApiSuccess<T extends { code: number; msg?: string }>(response: T): T {
146
+ if (response.code !== 0) {
147
+ throw new Error(response.msg ?? "Feishu Drive API request failed");
148
+ }
149
+ return response;
150
+ }
151
+
152
+ function normalizeCommentReply(reply: FeishuDriveCommentReply) {
153
+ return {
154
+ reply_id: reply.reply_id,
155
+ user_id: reply.user_id,
156
+ create_time: reply.create_time,
157
+ update_time: reply.update_time,
158
+ text: extractReplyText(reply),
159
+ };
160
+ }
161
+
162
+ function normalizeCommentCard(comment: FeishuDriveCommentCard) {
163
+ const replies = comment.reply_list?.replies ?? [];
164
+ const rootReply = replies[0];
165
+ return {
166
+ comment_id: comment.comment_id,
167
+ user_id: comment.user_id,
168
+ create_time: comment.create_time,
169
+ update_time: comment.update_time,
170
+ is_solved: comment.is_solved,
171
+ is_whole: comment.is_whole,
172
+ quote: comment.quote,
173
+ text: extractReplyText(rootReply),
174
+ has_more_replies: comment.has_more,
175
+ replies_page_token: comment.page_token,
176
+ replies: replies.slice(1).map(normalizeCommentReply),
177
+ };
178
+ }
179
+
180
+ function normalizeCommentPageSize(pageSize: number | undefined): string | undefined {
181
+ if (typeof pageSize !== "number" || !Number.isFinite(pageSize)) {
182
+ return undefined;
183
+ }
184
+ return String(Math.min(Math.max(Math.floor(pageSize), 1), 100));
185
+ }
186
+
187
+ function resolveAmbientCommentTarget(context: FeishuDriveToolContext | undefined) {
188
+ const deliveryContext = context?.deliveryContext;
189
+ if (deliveryContext?.channel && deliveryContext.channel !== "feishu") {
190
+ return null;
191
+ }
192
+ return parseFeishuCommentTarget(deliveryContext?.to);
193
+ }
194
+
195
+ function applyAmbientCommentDefaults<
196
+ T extends {
197
+ file_token?: string;
198
+ file_type?: CommentFileType;
199
+ comment_id?: string;
200
+ },
201
+ >(params: T, context: FeishuDriveToolContext | undefined): T {
202
+ const ambient = resolveAmbientCommentTarget(context);
203
+ if (!ambient) {
204
+ return params;
205
+ }
206
+ return {
207
+ ...params,
208
+ file_token: params.file_token?.trim() || ambient.fileToken,
209
+ file_type: params.file_type ?? ambient.fileType,
210
+ comment_id: params.comment_id?.trim() || ambient.commentId,
211
+ };
212
+ }
213
+
214
+ function applyAddCommentAmbientDefaults<
215
+ T extends {
216
+ file_token?: string;
217
+ file_type?: "doc" | "docx";
218
+ },
219
+ >(params: T, context: FeishuDriveToolContext | undefined): T {
220
+ const ambient = resolveAmbientCommentTarget(context);
221
+ if (!ambient || (ambient.fileType !== "doc" && ambient.fileType !== "docx")) {
222
+ return params;
223
+ }
224
+ return {
225
+ ...params,
226
+ file_token: params.file_token?.trim() || ambient.fileToken,
227
+ file_type: params.file_type ?? ambient.fileType,
228
+ };
229
+ }
230
+
231
+ function applyAddCommentDefaults<
232
+ T extends {
233
+ file_token?: string;
234
+ file_type?: "doc" | "docx";
235
+ },
236
+ >(params: T): T & { file_type: "doc" | "docx" } {
237
+ const fileType = params.file_type ?? "docx";
238
+ if (!params.file_type) {
239
+ console.info(
240
+ `[feishu_drive] add_comment missing file_type; defaulting to docx ` +
241
+ `file_token=${params.file_token ?? "unknown"}`,
242
+ );
243
+ }
244
+ return {
245
+ ...params,
246
+ file_type: fileType,
247
+ };
248
+ }
249
+
250
+ function applyCommentFileTypeDefault<
251
+ T extends {
252
+ file_token?: string;
253
+ file_type?: CommentFileType;
254
+ },
255
+ >(
256
+ params: T,
257
+ action: "list_comments" | "list_comment_replies" | "reply_comment",
258
+ ): T & {
259
+ file_type: CommentFileType;
260
+ } {
261
+ const fileType = params.file_type ?? "docx";
262
+ if (!params.file_type) {
263
+ console.info(
264
+ `[feishu_drive] ${action} missing file_type; defaulting to docx ` +
265
+ `file_token=${params.file_token ?? "unknown"}`,
266
+ );
267
+ }
268
+ return {
269
+ ...params,
270
+ file_type: fileType,
271
+ };
272
+ }
273
+
274
+ function formatDriveApiError(error: unknown): string {
275
+ return formatFeishuApiError(error, { includeConfigParams: true });
276
+ }
277
+
278
+ function extractDriveApiErrorMeta(error: unknown): {
279
+ message: string;
280
+ httpStatus?: number;
281
+ feishuCode?: number | string;
282
+ feishuMsg?: string;
283
+ feishuLogId?: string;
284
+ } {
285
+ if (!isRecord(error)) {
286
+ return { message: typeof error === "string" ? error : JSON.stringify(error) };
287
+ }
288
+ const response = isRecord(error.response) ? error.response : undefined;
289
+ const responseData = isRecord(response?.data) ? response?.data : undefined;
290
+ return {
291
+ message:
292
+ typeof error.message === "string"
293
+ ? error.message
294
+ : typeof error === "string"
295
+ ? error
296
+ : JSON.stringify(error),
297
+ httpStatus: typeof response?.status === "number" ? response.status : undefined,
298
+ feishuCode:
299
+ typeof responseData?.code === "number" ? responseData.code : readString(responseData?.code),
300
+ feishuMsg: readString(responseData?.msg),
301
+ feishuLogId: readString(responseData?.log_id),
302
+ };
303
+ }
304
+
305
+ function isReplyNotAllowedError(error: unknown): boolean {
306
+ if (!(error instanceof FeishuReplyCommentError)) {
307
+ return false;
308
+ }
309
+ return error.feishuCode === 1069302;
310
+ }
311
+
14
312
  async function getRootFolderToken(client: Lark.Client): Promise<string> {
15
313
  // Use generic HTTP client to call the root folder meta API
16
314
  // as it's not directly exposed in the SDK
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- accessing internal SDK property
18
- const domain = (client as any).domain ?? "https://open.feishu.cn";
19
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- accessing internal SDK property
20
- const res = (await (client as any).httpInstance.get(
315
+ const internalClient = getDriveInternalClient(client);
316
+ const domain = internalClient.domain ?? "https://open.feishu.cn";
317
+ const res = (await internalClient.httpInstance.get(
21
318
  `${domain}/open-apis/drive/explorer/v2/root_folder/meta`,
22
- )) as { code: number; msg?: string; data?: { token?: string } };
319
+ )) as FeishuExplorerRootFolderMetaResponse;
23
320
  if (res.code !== 0) {
24
321
  throw new Error(res.msg ?? "Failed to get root folder");
25
322
  }
@@ -161,23 +458,291 @@ async function deleteFile(client: Lark.Client, fileToken: string, type: string)
161
458
  };
162
459
  }
163
460
 
461
+ async function listComments(
462
+ client: Lark.Client,
463
+ params: {
464
+ file_token: string;
465
+ file_type: CommentFileType;
466
+ page_size?: number;
467
+ page_token?: string;
468
+ },
469
+ ) {
470
+ const response = assertDriveApiSuccess(
471
+ await requestDriveApi<FeishuDriveListCommentsResponse>({
472
+ client,
473
+ method: "GET",
474
+ url:
475
+ `/open-apis/drive/v1/files/${encodeURIComponent(params.file_token)}/comments` +
476
+ encodeQuery({
477
+ file_type: params.file_type,
478
+ page_size: normalizeCommentPageSize(params.page_size),
479
+ page_token: params.page_token,
480
+ user_id_type: "open_id",
481
+ }),
482
+ }),
483
+ );
484
+ return {
485
+ has_more: response.data?.has_more ?? false,
486
+ page_token: response.data?.page_token,
487
+ comments: (response.data?.items ?? []).map(normalizeCommentCard),
488
+ };
489
+ }
490
+
491
+ async function listCommentReplies(
492
+ client: Lark.Client,
493
+ params: {
494
+ file_token: string;
495
+ file_type: CommentFileType;
496
+ comment_id: string;
497
+ page_size?: number;
498
+ page_token?: string;
499
+ },
500
+ ) {
501
+ const response = assertDriveApiSuccess(
502
+ await requestDriveApi<FeishuDriveListRepliesResponse>({
503
+ client,
504
+ method: "GET",
505
+ url:
506
+ `/open-apis/drive/v1/files/${encodeURIComponent(params.file_token)}/comments/${encodeURIComponent(
507
+ params.comment_id,
508
+ )}/replies` +
509
+ encodeQuery({
510
+ file_type: params.file_type,
511
+ page_size: normalizeCommentPageSize(params.page_size),
512
+ page_token: params.page_token,
513
+ user_id_type: "open_id",
514
+ }),
515
+ }),
516
+ );
517
+ return {
518
+ has_more: response.data?.has_more ?? false,
519
+ page_token: response.data?.page_token,
520
+ replies: (response.data?.items ?? []).map(normalizeCommentReply),
521
+ };
522
+ }
523
+
524
+ async function addComment(
525
+ client: Lark.Client,
526
+ params: {
527
+ file_token: string;
528
+ file_type: "doc" | "docx";
529
+ content: string;
530
+ block_id?: string;
531
+ },
532
+ ): Promise<{ success: true } & Record<string, unknown>> {
533
+ if (params.block_id?.trim() && params.file_type !== "docx") {
534
+ throw new Error("block_id is only supported for docx comments");
535
+ }
536
+ const response = assertDriveApiSuccess(
537
+ await requestDriveApi<FeishuDriveApiResponse<Record<string, unknown>>>({
538
+ client,
539
+ method: "POST",
540
+ url: `/open-apis/drive/v1/files/${encodeURIComponent(params.file_token)}/new_comments`,
541
+ data: {
542
+ file_type: params.file_type,
543
+ reply_elements: buildReplyElements(params.content),
544
+ ...(params.block_id?.trim() ? { anchor: { block_id: params.block_id.trim() } } : {}),
545
+ },
546
+ }),
547
+ );
548
+ return {
549
+ success: true,
550
+ ...response.data,
551
+ };
552
+ }
553
+
554
+ // Fetch comment metadata via batch_query because the single-comment endpoint
555
+ // does not support partial comments.
556
+ async function queryCommentById(
557
+ client: Lark.Client,
558
+ params: {
559
+ file_token: string;
560
+ file_type: CommentFileType;
561
+ comment_id: string;
562
+ },
563
+ ) {
564
+ const response = assertDriveApiSuccess(
565
+ await requestDriveApi<FeishuDriveListCommentsResponse>({
566
+ client,
567
+ method: "POST",
568
+ url:
569
+ `/open-apis/drive/v1/files/${encodeURIComponent(params.file_token)}/comments/batch_query` +
570
+ encodeQuery({
571
+ file_type: params.file_type,
572
+ user_id_type: "open_id",
573
+ }),
574
+ data: {
575
+ comment_ids: [params.comment_id],
576
+ },
577
+ }),
578
+ );
579
+ return response.data?.items?.find((comment) => comment.comment_id?.trim() === params.comment_id);
580
+ }
581
+
582
+ export async function replyComment(
583
+ client: Lark.Client,
584
+ params: {
585
+ file_token: string;
586
+ file_type: CommentFileType;
587
+ comment_id: string;
588
+ content: string;
589
+ },
590
+ ): Promise<{ success: true; reply_id?: string } & Record<string, unknown>> {
591
+ const url = `/open-apis/drive/v1/files/${encodeURIComponent(params.file_token)}/comments/${encodeURIComponent(
592
+ params.comment_id,
593
+ )}/replies`;
594
+ const query = { file_type: params.file_type };
595
+ try {
596
+ const response = await requestDriveApi<FeishuDriveApiResponse<Record<string, unknown>>>({
597
+ client,
598
+ method: "POST",
599
+ url,
600
+ query,
601
+ data: {
602
+ content: {
603
+ elements: [
604
+ {
605
+ type: "text_run",
606
+ text_run: {
607
+ text: params.content,
608
+ },
609
+ },
610
+ ],
611
+ },
612
+ },
613
+ });
614
+ if (response.code === 0) {
615
+ return {
616
+ success: true,
617
+ ...response.data,
618
+ };
619
+ }
620
+ console.warn(
621
+ `[feishu_drive] replyComment failed ` +
622
+ `comment=${params.comment_id} file_type=${params.file_type} ` +
623
+ `code=${response.code ?? "unknown"} ` +
624
+ `msg=${response.msg ?? "unknown"} log_id=${response.log_id ?? "unknown"}`,
625
+ );
626
+ throw new FeishuReplyCommentError({
627
+ message: response.msg ?? "Feishu Drive reply comment failed",
628
+ feishuCode: response.code,
629
+ feishuMsg: response.msg,
630
+ feishuLogId: response.log_id,
631
+ });
632
+ } catch (error) {
633
+ if (error instanceof FeishuReplyCommentError) {
634
+ throw error;
635
+ }
636
+ const meta = extractDriveApiErrorMeta(error);
637
+ console.warn(
638
+ `[feishu_drive] replyComment threw ` +
639
+ `comment=${params.comment_id} file_type=${params.file_type} ` +
640
+ `error=${formatDriveApiError(error)}`,
641
+ );
642
+ throw new FeishuReplyCommentError({
643
+ message: meta.message,
644
+ httpStatus: meta.httpStatus,
645
+ feishuCode: meta.feishuCode,
646
+ feishuMsg: meta.feishuMsg,
647
+ feishuLogId: meta.feishuLogId,
648
+ });
649
+ }
650
+ }
651
+
652
+ export async function deliverCommentThreadText(
653
+ client: Lark.Client,
654
+ params: {
655
+ file_token: string;
656
+ file_type: CommentFileType;
657
+ comment_id: string;
658
+ content: string;
659
+ is_whole_comment?: boolean;
660
+ },
661
+ ): Promise<
662
+ | ({ success: true; reply_id?: string } & Record<string, unknown> & {
663
+ delivery_mode: "reply_comment";
664
+ })
665
+ | ({ success: true; comment_id?: string } & Record<string, unknown> & {
666
+ delivery_mode: "add_comment";
667
+ })
668
+ > {
669
+ let isWholeComment = params.is_whole_comment;
670
+ if (isWholeComment === undefined) {
671
+ try {
672
+ const comment = await queryCommentById(client, params);
673
+ isWholeComment = comment?.is_whole === true;
674
+ } catch (error) {
675
+ console.warn(
676
+ `[feishu_drive] comment metadata preflight failed ` +
677
+ `comment=${params.comment_id} file_type=${params.file_type} ` +
678
+ `error=${formatErrorMessage(error)}`,
679
+ );
680
+ isWholeComment = false;
681
+ }
682
+ }
683
+ if (isWholeComment) {
684
+ if (params.file_type !== "doc" && params.file_type !== "docx") {
685
+ throw new Error(
686
+ `Whole-document comment follow-ups are only supported for doc/docx (got ${params.file_type})`,
687
+ );
688
+ }
689
+ const wholeCommentFileType: "doc" | "docx" = params.file_type;
690
+ console.info(
691
+ `[feishu_drive] whole-comment compatibility path ` +
692
+ `comment=${params.comment_id} file_type=${params.file_type} mode=add_comment`,
693
+ );
694
+ return {
695
+ delivery_mode: "add_comment",
696
+ ...(await addComment(client, {
697
+ file_token: params.file_token,
698
+ file_type: wholeCommentFileType,
699
+ content: params.content,
700
+ })),
701
+ };
702
+ }
703
+ try {
704
+ return {
705
+ delivery_mode: "reply_comment",
706
+ ...(await replyComment(client, params)),
707
+ };
708
+ } catch (error) {
709
+ if (error instanceof FeishuReplyCommentError && isReplyNotAllowedError(error)) {
710
+ if (params.file_type !== "doc" && params.file_type !== "docx") {
711
+ throw error;
712
+ }
713
+ const fallbackFileType: "doc" | "docx" = params.file_type;
714
+ console.info(
715
+ `[feishu_drive] reply-not-allowed compatibility path ` +
716
+ `comment=${params.comment_id} file_type=${params.file_type} mode=add_comment ` +
717
+ `log_id=${error.feishuLogId ?? "unknown"}`,
718
+ );
719
+ return {
720
+ delivery_mode: "add_comment",
721
+ ...(await addComment(client, {
722
+ file_token: params.file_token,
723
+ file_type: fallbackFileType,
724
+ content: params.content,
725
+ })),
726
+ };
727
+ }
728
+ throw error;
729
+ }
730
+ }
731
+
164
732
  // ============ Tool Registration ============
165
733
 
166
734
  export function registerFeishuDriveTools(api: OpenClawPluginApi) {
167
735
  if (!api.config) {
168
- api.logger.debug?.("feishu_drive: No config available, skipping drive tools");
169
736
  return;
170
737
  }
171
738
 
172
739
  const accounts = listEnabledFeishuAccounts(api.config);
173
740
  if (accounts.length === 0) {
174
- api.logger.debug?.("feishu_drive: No Feishu accounts configured, skipping drive tools");
175
741
  return;
176
742
  }
177
743
 
178
744
  const toolsCfg = resolveAnyEnabledFeishuToolsConfig(accounts);
179
745
  if (!toolsCfg.drive) {
180
- api.logger.debug?.("feishu_drive: drive tool disabled in config");
181
746
  return;
182
747
  }
183
748
 
@@ -190,7 +755,7 @@ export function registerFeishuDriveTools(api: OpenClawPluginApi) {
190
755
  name: "feishu_drive",
191
756
  label: "Feishu Drive",
192
757
  description:
193
- "Feishu cloud storage operations. Actions: list, info, create_folder, move, delete",
758
+ "Feishu cloud storage operations. Actions: list, info, create_folder, move, delete, list_comments, list_comment_replies, add_comment, reply_comment",
194
759
  parameters: FeishuDriveSchema,
195
760
  async execute(_toolCallId, params) {
196
761
  const p = params as FeishuDriveExecuteParams;
@@ -211,8 +776,46 @@ export function registerFeishuDriveTools(api: OpenClawPluginApi) {
211
776
  return jsonToolResult(await moveFile(client, p.file_token, p.type, p.folder_token));
212
777
  case "delete":
213
778
  return jsonToolResult(await deleteFile(client, p.file_token, p.type));
779
+ case "list_comments": {
780
+ const resolved = applyCommentFileTypeDefault(
781
+ applyAmbientCommentDefaults(p, ctx),
782
+ "list_comments",
783
+ );
784
+ return jsonToolResult(await listComments(client, resolved));
785
+ }
786
+ case "list_comment_replies": {
787
+ const resolved = applyCommentFileTypeDefault(
788
+ applyAmbientCommentDefaults(p, ctx),
789
+ "list_comment_replies",
790
+ );
791
+ return jsonToolResult(await listCommentReplies(client, resolved));
792
+ }
793
+ case "add_comment": {
794
+ const resolved = applyAddCommentDefaults(applyAddCommentAmbientDefaults(p, ctx));
795
+ try {
796
+ return jsonToolResult(await addComment(client, resolved));
797
+ } finally {
798
+ void cleanupAmbientCommentTypingReaction({
799
+ client: getDriveInternalClient(client),
800
+ deliveryContext: ctx.deliveryContext,
801
+ });
802
+ }
803
+ }
804
+ case "reply_comment": {
805
+ const resolved = applyCommentFileTypeDefault(
806
+ applyAmbientCommentDefaults(p, ctx),
807
+ "reply_comment",
808
+ );
809
+ try {
810
+ return jsonToolResult(await deliverCommentThreadText(client, resolved));
811
+ } finally {
812
+ void cleanupAmbientCommentTypingReaction({
813
+ client: getDriveInternalClient(client),
814
+ deliveryContext: ctx.deliveryContext,
815
+ });
816
+ }
817
+ }
214
818
  default:
215
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- exhaustive check fallback
216
819
  return unknownToolActionResult((p as { action?: unknown }).action);
217
820
  }
218
821
  } catch (err) {
@@ -223,6 +826,4 @@ export function registerFeishuDriveTools(api: OpenClawPluginApi) {
223
826
  },
224
827
  { name: "feishu_drive" },
225
828
  );
226
-
227
- api.logger.info?.(`feishu_drive: Registered feishu_drive tool`);
228
829
  }
@@ -1,10 +1,10 @@
1
1
  import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
- import type { OpenClawConfig, PluginRuntime } from "openclaw/plugin-sdk/feishu";
4
+ import type { OpenClawConfig, PluginRuntime } from "../runtime-api.js";
5
5
  import type { DynamicAgentCreationConfig } from "./types.js";
6
6
 
7
- export type MaybeCreateDynamicAgentResult = {
7
+ type MaybeCreateDynamicAgentResult = {
8
8
  created: boolean;
9
9
  updatedCfg: OpenClawConfig;
10
10
  agentId?: string;
@@ -72,7 +72,10 @@ export async function maybeCreateDynamicAgent(params: {
72
72
  ],
73
73
  };
74
74
 
75
- await runtime.config.writeConfigFile(updatedCfg);
75
+ await runtime.config.replaceConfigFile({
76
+ nextConfig: updatedCfg,
77
+ afterWrite: { mode: "auto" },
78
+ });
76
79
  return { created: true, updatedCfg, agentId };
77
80
  }
78
81
 
@@ -115,7 +118,10 @@ export async function maybeCreateDynamicAgent(params: {
115
118
  };
116
119
 
117
120
  // Write updated config using PluginRuntime API
118
- await runtime.config.writeConfigFile(updatedCfg);
121
+ await runtime.config.replaceConfigFile({
122
+ nextConfig: updatedCfg,
123
+ afterWrite: { mode: "auto" },
124
+ });
119
125
 
120
126
  return { created: true, updatedCfg, agentId };
121
127
  }