@kodelyth/feishu 2026.5.42 → 2026.6.2

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 (192) hide show
  1. package/klaw.plugin.json +1712 -47
  2. package/package.json +19 -6
  3. package/api.ts +0 -32
  4. package/channel-entry.ts +0 -20
  5. package/channel-plugin-api.ts +0 -1
  6. package/contract-api.ts +0 -16
  7. package/index.ts +0 -82
  8. package/runtime-api.ts +0 -52
  9. package/secret-contract-api.ts +0 -5
  10. package/security-contract-api.ts +0 -1
  11. package/session-key-api.ts +0 -1
  12. package/setup-api.ts +0 -3
  13. package/setup-entry.test.ts +0 -19
  14. package/setup-entry.ts +0 -13
  15. package/src/accounts.test.ts +0 -480
  16. package/src/accounts.ts +0 -333
  17. package/src/agent-config.ts +0 -21
  18. package/src/app-registration.ts +0 -331
  19. package/src/approval-auth.test.ts +0 -24
  20. package/src/approval-auth.ts +0 -25
  21. package/src/async.test.ts +0 -35
  22. package/src/async.ts +0 -104
  23. package/src/audio-preflight.runtime.ts +0 -9
  24. package/src/bitable.test.ts +0 -136
  25. package/src/bitable.ts +0 -762
  26. package/src/bot-content.ts +0 -485
  27. package/src/bot-group-name.test.ts +0 -116
  28. package/src/bot-runtime-api.ts +0 -12
  29. package/src/bot-sender-name.ts +0 -125
  30. package/src/bot.broadcast.test.ts +0 -523
  31. package/src/bot.card-action.test.ts +0 -552
  32. package/src/bot.checkBotMentioned.test.ts +0 -265
  33. package/src/bot.helpers.test.ts +0 -135
  34. package/src/bot.stripBotMention.test.ts +0 -126
  35. package/src/bot.test.ts +0 -3671
  36. package/src/bot.ts +0 -1703
  37. package/src/card-action.ts +0 -447
  38. package/src/card-interaction.test.ts +0 -131
  39. package/src/card-interaction.ts +0 -159
  40. package/src/card-test-helpers.ts +0 -54
  41. package/src/card-ux-approval.ts +0 -65
  42. package/src/card-ux-launcher.test.ts +0 -106
  43. package/src/card-ux-launcher.ts +0 -121
  44. package/src/card-ux-shared.ts +0 -33
  45. package/src/channel-runtime-api.ts +0 -16
  46. package/src/channel.runtime.ts +0 -47
  47. package/src/channel.test.ts +0 -1151
  48. package/src/channel.ts +0 -1423
  49. package/src/chat-schema.ts +0 -25
  50. package/src/chat.test.ts +0 -240
  51. package/src/chat.ts +0 -188
  52. package/src/client-timeout.ts +0 -42
  53. package/src/client.test.ts +0 -447
  54. package/src/client.ts +0 -262
  55. package/src/comment-dispatcher-runtime-api.ts +0 -6
  56. package/src/comment-dispatcher.test.ts +0 -185
  57. package/src/comment-dispatcher.ts +0 -107
  58. package/src/comment-handler-runtime-api.ts +0 -3
  59. package/src/comment-handler.test.ts +0 -592
  60. package/src/comment-handler.ts +0 -303
  61. package/src/comment-reaction.test.ts +0 -138
  62. package/src/comment-reaction.ts +0 -259
  63. package/src/comment-shared.test.ts +0 -183
  64. package/src/comment-shared.ts +0 -406
  65. package/src/comment-target.ts +0 -44
  66. package/src/config-schema.test.ts +0 -326
  67. package/src/config-schema.ts +0 -335
  68. package/src/conversation-id.test.ts +0 -18
  69. package/src/conversation-id.ts +0 -199
  70. package/src/dedup-runtime-api.ts +0 -1
  71. package/src/dedup.ts +0 -141
  72. package/src/dedupe-key.ts +0 -72
  73. package/src/directory.static.ts +0 -61
  74. package/src/directory.test.ts +0 -141
  75. package/src/directory.ts +0 -124
  76. package/src/doc-schema.ts +0 -182
  77. package/src/docx-batch-insert.test.ts +0 -116
  78. package/src/docx-batch-insert.ts +0 -223
  79. package/src/docx-color-text.ts +0 -154
  80. package/src/docx-table-ops.test.ts +0 -53
  81. package/src/docx-table-ops.ts +0 -316
  82. package/src/docx-types.ts +0 -38
  83. package/src/docx.account-selection.test.ts +0 -95
  84. package/src/docx.test.ts +0 -701
  85. package/src/docx.ts +0 -1596
  86. package/src/drive-schema.ts +0 -92
  87. package/src/drive.test.ts +0 -1237
  88. package/src/drive.ts +0 -829
  89. package/src/dynamic-agent.test.ts +0 -155
  90. package/src/dynamic-agent.ts +0 -143
  91. package/src/event-types.ts +0 -45
  92. package/src/external-keys.test.ts +0 -20
  93. package/src/external-keys.ts +0 -19
  94. package/src/lifecycle.test-support.ts +0 -220
  95. package/src/media.test.ts +0 -955
  96. package/src/media.ts +0 -1105
  97. package/src/mention-target.types.ts +0 -5
  98. package/src/mention.ts +0 -114
  99. package/src/message-action-contract.ts +0 -13
  100. package/src/monitor-state-runtime-api.ts +0 -7
  101. package/src/monitor-transport-runtime-api.ts +0 -10
  102. package/src/monitor.account.ts +0 -492
  103. package/src/monitor.acp-init-failure.lifecycle.test-support.ts +0 -219
  104. package/src/monitor.bot-identity.ts +0 -86
  105. package/src/monitor.bot-menu-handler.ts +0 -165
  106. package/src/monitor.bot-menu.lifecycle.test-support.ts +0 -224
  107. package/src/monitor.bot-menu.test.ts +0 -188
  108. package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +0 -264
  109. package/src/monitor.card-action.lifecycle.test-support.ts +0 -421
  110. package/src/monitor.cleanup.test.ts +0 -383
  111. package/src/monitor.comment-notice-handler.ts +0 -105
  112. package/src/monitor.comment.test.ts +0 -967
  113. package/src/monitor.comment.ts +0 -1386
  114. package/src/monitor.lifecycle.test.ts +0 -4
  115. package/src/monitor.message-handler.ts +0 -350
  116. package/src/monitor.reaction.lifecycle.test-support.ts +0 -68
  117. package/src/monitor.reaction.test.ts +0 -739
  118. package/src/monitor.startup.test.ts +0 -213
  119. package/src/monitor.startup.ts +0 -74
  120. package/src/monitor.state.defaults.test.ts +0 -46
  121. package/src/monitor.state.ts +0 -170
  122. package/src/monitor.synthetic-error.ts +0 -18
  123. package/src/monitor.test-mocks.ts +0 -46
  124. package/src/monitor.transport.ts +0 -451
  125. package/src/monitor.ts +0 -100
  126. package/src/monitor.webhook-e2e.test.ts +0 -279
  127. package/src/monitor.webhook-security.test.ts +0 -389
  128. package/src/monitor.webhook.test-helpers.ts +0 -116
  129. package/src/outbound-runtime-api.ts +0 -1
  130. package/src/outbound.test.ts +0 -1118
  131. package/src/outbound.ts +0 -785
  132. package/src/perm-schema.ts +0 -52
  133. package/src/perm.ts +0 -170
  134. package/src/pins.ts +0 -108
  135. package/src/policy.test.ts +0 -223
  136. package/src/policy.ts +0 -318
  137. package/src/post.test.ts +0 -105
  138. package/src/post.ts +0 -275
  139. package/src/probe.test.ts +0 -283
  140. package/src/probe.ts +0 -166
  141. package/src/processing-claims.ts +0 -59
  142. package/src/qr-terminal.ts +0 -1
  143. package/src/reactions.ts +0 -123
  144. package/src/reasoning-preview.test.ts +0 -113
  145. package/src/reasoning-preview.ts +0 -28
  146. package/src/reply-dispatcher-runtime-api.ts +0 -7
  147. package/src/reply-dispatcher.test.ts +0 -1513
  148. package/src/reply-dispatcher.ts +0 -748
  149. package/src/runtime.ts +0 -9
  150. package/src/secret-contract.ts +0 -145
  151. package/src/secret-input.ts +0 -1
  152. package/src/security-audit-shared.ts +0 -69
  153. package/src/security-audit.test.ts +0 -59
  154. package/src/security-audit.ts +0 -1
  155. package/src/send-result.ts +0 -80
  156. package/src/send-target.test.ts +0 -86
  157. package/src/send-target.ts +0 -35
  158. package/src/send.reply-fallback.test.ts +0 -417
  159. package/src/send.test.ts +0 -621
  160. package/src/send.ts +0 -861
  161. package/src/sequential-key.test.ts +0 -72
  162. package/src/sequential-key.ts +0 -25
  163. package/src/sequential-queue.test.ts +0 -165
  164. package/src/sequential-queue.ts +0 -86
  165. package/src/session-conversation.ts +0 -42
  166. package/src/session-route.ts +0 -48
  167. package/src/setup-core.ts +0 -51
  168. package/src/setup-surface.test.ts +0 -484
  169. package/src/setup-surface.ts +0 -618
  170. package/src/streaming-card.test.ts +0 -397
  171. package/src/streaming-card.ts +0 -571
  172. package/src/subagent-hooks.test.ts +0 -627
  173. package/src/subagent-hooks.ts +0 -413
  174. package/src/targets.ts +0 -97
  175. package/src/test-support/lifecycle-test-support.ts +0 -454
  176. package/src/thread-bindings.test.ts +0 -180
  177. package/src/thread-bindings.ts +0 -331
  178. package/src/tool-account-routing.test.ts +0 -250
  179. package/src/tool-account.test.ts +0 -44
  180. package/src/tool-account.ts +0 -93
  181. package/src/tool-factory-test-harness.ts +0 -79
  182. package/src/tool-result.test.ts +0 -32
  183. package/src/tool-result.ts +0 -16
  184. package/src/tools-config.test.ts +0 -21
  185. package/src/tools-config.ts +0 -22
  186. package/src/types.ts +0 -106
  187. package/src/typing.test.ts +0 -144
  188. package/src/typing.ts +0 -214
  189. package/src/wiki-schema.ts +0 -69
  190. package/src/wiki.ts +0 -270
  191. package/subagent-hooks-api.ts +0 -31
  192. package/tsconfig.json +0 -16
@@ -1,571 +0,0 @@
1
- /**
2
- * Feishu Streaming Card - Card Kit streaming API for real-time text output
3
- */
4
-
5
- import type { Client } from "@larksuiteoapi/node-sdk";
6
- import { fetchWithSsrFGuard } from "klaw/plugin-sdk/ssrf-runtime";
7
- import { getFeishuUserAgent } from "./client.js";
8
- import { resolveFeishuCardTemplate, type CardHeaderConfig } from "./send.js";
9
- import type { FeishuDomain } from "./types.js";
10
-
11
- type Credentials = { appId: string; appSecret: string; domain?: FeishuDomain };
12
- type CardState = {
13
- cardId: string;
14
- messageId: string;
15
- sequence: number;
16
- currentText: string;
17
- sentText: string;
18
- hasNote: boolean;
19
- };
20
-
21
- /** Options for customising the initial streaming card appearance. */
22
- type StreamingCardOptions = {
23
- /** Optional header with title and color template. */
24
- header?: CardHeaderConfig;
25
- /** Optional grey note footer text. */
26
- note?: string;
27
- };
28
-
29
- /** Optional header for streaming cards (title bar with color template) */
30
- type StreamingCardHeader = {
31
- title: string;
32
- /** Color template: blue, green, red, orange, purple, indigo, wathet, turquoise, yellow, grey, carmine, violet, lime */
33
- template?: string;
34
- };
35
-
36
- type StreamingStartOptions = {
37
- replyToMessageId?: string;
38
- replyInThread?: boolean;
39
- rootId?: string;
40
- header?: StreamingCardHeader;
41
- };
42
-
43
- const STREAMING_UPDATE_THROTTLE_MS = 160;
44
- const STREAMING_SIGNIFICANT_DELTA_CHARS = 18;
45
-
46
- // Token cache (keyed by domain + appId)
47
- const tokenCache = new Map<string, { token: string; expiresAt: number }>();
48
-
49
- function resolveApiBase(domain?: FeishuDomain): string {
50
- if (domain === "lark") {
51
- return "https://open.larksuite.com/open-apis";
52
- }
53
- if (domain && domain !== "feishu" && domain.startsWith("http")) {
54
- return `${domain.replace(/\/+$/, "")}/open-apis`;
55
- }
56
- return "https://open.feishu.cn/open-apis";
57
- }
58
-
59
- function resolveAllowedHostnames(domain?: FeishuDomain): string[] {
60
- if (domain === "lark") {
61
- return ["open.larksuite.com"];
62
- }
63
- if (domain && domain !== "feishu" && domain.startsWith("http")) {
64
- try {
65
- return [new URL(domain).hostname];
66
- } catch {
67
- return [];
68
- }
69
- }
70
- return ["open.feishu.cn"];
71
- }
72
-
73
- async function getToken(creds: Credentials): Promise<string> {
74
- const key = `${creds.domain ?? "feishu"}|${creds.appId}`;
75
- const cached = tokenCache.get(key);
76
- if (cached && cached.expiresAt > Date.now() + 60000) {
77
- return cached.token;
78
- }
79
-
80
- const { response, release } = await fetchWithSsrFGuard({
81
- url: `${resolveApiBase(creds.domain)}/auth/v3/tenant_access_token/internal`,
82
- init: {
83
- method: "POST",
84
- headers: { "Content-Type": "application/json", "User-Agent": getFeishuUserAgent() },
85
- body: JSON.stringify({ app_id: creds.appId, app_secret: creds.appSecret }),
86
- },
87
- policy: { allowedHostnames: resolveAllowedHostnames(creds.domain) },
88
- auditContext: "feishu.streaming-card.token",
89
- });
90
- if (!response.ok) {
91
- await release();
92
- throw new Error(`Token request failed with HTTP ${response.status}`);
93
- }
94
- const data = (await response.json()) as {
95
- code: number;
96
- msg: string;
97
- tenant_access_token?: string;
98
- expire?: number;
99
- };
100
- await release();
101
- if (data.code !== 0 || !data.tenant_access_token) {
102
- throw new Error(`Token error: ${data.msg}`);
103
- }
104
- tokenCache.set(key, {
105
- token: data.tenant_access_token,
106
- expiresAt: Date.now() + (data.expire ?? 7200) * 1000,
107
- });
108
- return data.tenant_access_token;
109
- }
110
-
111
- function truncateSummary(text: string, max = 50): string {
112
- if (!text) {
113
- return "";
114
- }
115
- const clean = text.replace(/\n/g, " ").trim();
116
- return clean.length <= max ? clean : clean.slice(0, max - 3) + "...";
117
- }
118
-
119
- function hasNaturalStreamingBoundary(text: string): boolean {
120
- return /[\n。!?!?;;::]$/.test(text);
121
- }
122
-
123
- function shouldPushStreamingUpdate(previousText: string, nextText: string): boolean {
124
- if (!previousText) {
125
- return true;
126
- }
127
- if (hasNaturalStreamingBoundary(nextText)) {
128
- return true;
129
- }
130
- return nextText.length - previousText.length >= STREAMING_SIGNIFICANT_DELTA_CHARS;
131
- }
132
-
133
- function resolveStreamingCardAppendContent(previousText: string, nextText: string): string {
134
- if (!nextText || nextText === previousText) {
135
- return "";
136
- }
137
- if (!previousText) {
138
- return nextText;
139
- }
140
- return nextText.startsWith(previousText) ? nextText.slice(previousText.length) : nextText;
141
- }
142
-
143
- export function mergeStreamingText(
144
- previousText: string | undefined,
145
- nextText: string | undefined,
146
- ): string {
147
- const previous = typeof previousText === "string" ? previousText : "";
148
- const next = typeof nextText === "string" ? nextText : "";
149
- if (!next) {
150
- return previous;
151
- }
152
- if (!previous || next === previous) {
153
- return next;
154
- }
155
- if (next.startsWith(previous)) {
156
- return next;
157
- }
158
- if (previous.startsWith(next)) {
159
- return previous;
160
- }
161
- if (next.includes(previous)) {
162
- return next;
163
- }
164
- if (previous.includes(next)) {
165
- return previous;
166
- }
167
-
168
- // Merge partial overlaps, e.g. "这" + "这是" => "这是".
169
- const maxOverlap = Math.min(previous.length, next.length);
170
- for (let overlap = maxOverlap; overlap > 0; overlap -= 1) {
171
- if (previous.slice(-overlap) === next.slice(0, overlap)) {
172
- return `${previous}${next.slice(overlap)}`;
173
- }
174
- }
175
- // Fallback for fragmented partial chunks: append as-is to avoid losing tokens.
176
- return `${previous}${next}`;
177
- }
178
-
179
- export function resolveStreamingCardSendMode(options?: StreamingStartOptions) {
180
- if (options?.replyToMessageId) {
181
- return "reply";
182
- }
183
- if (options?.rootId) {
184
- return "root_create";
185
- }
186
- return "create";
187
- }
188
-
189
- /** Streaming card session manager */
190
- export class FeishuStreamingSession {
191
- private client: Client;
192
- private creds: Credentials;
193
- private state: CardState | null = null;
194
- private queue: Promise<void> = Promise.resolve();
195
- private closed = false;
196
- private log?: (msg: string) => void;
197
- private lastUpdateTime = 0;
198
- private pendingText: string | null = null;
199
- private flushTimer: ReturnType<typeof setTimeout> | null = null;
200
- private updateThrottleMs = STREAMING_UPDATE_THROTTLE_MS;
201
-
202
- constructor(client: Client, creds: Credentials, log?: (msg: string) => void) {
203
- this.client = client;
204
- this.creds = creds;
205
- this.log = log;
206
- }
207
-
208
- async start(
209
- receiveId: string,
210
- receiveIdType: "open_id" | "user_id" | "union_id" | "email" | "chat_id" = "chat_id",
211
- options?: StreamingCardOptions & StreamingStartOptions,
212
- ): Promise<void> {
213
- if (this.state) {
214
- return;
215
- }
216
-
217
- const apiBase = resolveApiBase(this.creds.domain);
218
- const elements: Record<string, unknown>[] = [
219
- { tag: "markdown", content: "", element_id: "content" },
220
- ];
221
- if (options?.note) {
222
- elements.push({ tag: "hr" });
223
- elements.push({
224
- tag: "markdown",
225
- content: `<font color='grey'>${options.note}</font>`,
226
- element_id: "note",
227
- });
228
- }
229
- const cardJson: Record<string, unknown> = {
230
- schema: "2.0",
231
- config: {
232
- streaming_mode: true,
233
- summary: { content: "[Generating...]" },
234
- streaming_config: { print_frequency_ms: { default: 50 }, print_step: { default: 1 } },
235
- },
236
- body: { elements },
237
- };
238
- if (options?.header) {
239
- cardJson.header = {
240
- title: { tag: "plain_text", content: options.header.title },
241
- template: resolveFeishuCardTemplate(options.header.template) ?? "blue",
242
- };
243
- }
244
-
245
- // Create card entity
246
- const { response: createRes, release: releaseCreate } = await fetchWithSsrFGuard({
247
- url: `${apiBase}/cardkit/v1/cards`,
248
- init: {
249
- method: "POST",
250
- headers: {
251
- Authorization: `Bearer ${await getToken(this.creds)}`,
252
- "Content-Type": "application/json",
253
- "User-Agent": getFeishuUserAgent(),
254
- },
255
- body: JSON.stringify({ type: "card_json", data: JSON.stringify(cardJson) }),
256
- },
257
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
258
- auditContext: "feishu.streaming-card.create",
259
- });
260
- if (!createRes.ok) {
261
- await releaseCreate();
262
- throw new Error(`Create card request failed with HTTP ${createRes.status}`);
263
- }
264
- const createData = (await createRes.json()) as {
265
- code: number;
266
- msg: string;
267
- data?: { card_id: string };
268
- };
269
- await releaseCreate();
270
- if (createData.code !== 0 || !createData.data?.card_id) {
271
- throw new Error(`Create card failed: ${createData.msg}`);
272
- }
273
- const cardId = createData.data.card_id;
274
- const cardContent = JSON.stringify({ type: "card", data: { card_id: cardId } });
275
-
276
- // Prefer message.reply when we have a reply target — reply_in_thread
277
- // reliably routes streaming cards into Feishu topics, whereas
278
- // message.create with root_id may silently ignore root_id for card
279
- // references (card_id format).
280
- let sendRes;
281
- const sendOptions = options ?? {};
282
- const sendMode = resolveStreamingCardSendMode(sendOptions);
283
- if (sendMode === "reply") {
284
- sendRes = await this.client.im.message.reply({
285
- path: { message_id: sendOptions.replyToMessageId! },
286
- data: {
287
- msg_type: "interactive",
288
- content: cardContent,
289
- ...(sendOptions.replyInThread ? { reply_in_thread: true } : {}),
290
- },
291
- });
292
- } else if (sendMode === "root_create") {
293
- // root_id is undeclared in the SDK types but accepted at runtime
294
- sendRes = await this.client.im.message.create({
295
- params: { receive_id_type: receiveIdType },
296
- data: Object.assign(
297
- { receive_id: receiveId, msg_type: "interactive", content: cardContent },
298
- { root_id: sendOptions.rootId },
299
- ),
300
- });
301
- } else {
302
- sendRes = await this.client.im.message.create({
303
- params: { receive_id_type: receiveIdType },
304
- data: {
305
- receive_id: receiveId,
306
- msg_type: "interactive",
307
- content: cardContent,
308
- },
309
- });
310
- }
311
- if (sendRes.code !== 0 || !sendRes.data?.message_id) {
312
- throw new Error(`Send card failed: ${sendRes.msg}`);
313
- }
314
-
315
- this.state = {
316
- cardId,
317
- messageId: sendRes.data.message_id,
318
- sequence: 1,
319
- currentText: "",
320
- sentText: "",
321
- hasNote: !!options?.note,
322
- };
323
- this.log?.(`Started streaming: cardId=${cardId}, messageId=${sendRes.data.message_id}`);
324
- }
325
-
326
- private async updateCardContent(
327
- text: string,
328
- onError?: (error: unknown) => void,
329
- ): Promise<boolean> {
330
- if (!this.state) {
331
- return false;
332
- }
333
- const apiBase = resolveApiBase(this.creds.domain);
334
- this.state.sequence += 1;
335
- try {
336
- const { response, release } = await fetchWithSsrFGuard({
337
- url: `${apiBase}/cardkit/v1/cards/${this.state.cardId}/elements/content/content`,
338
- init: {
339
- method: "PUT",
340
- headers: {
341
- Authorization: `Bearer ${await getToken(this.creds)}`,
342
- "Content-Type": "application/json",
343
- "User-Agent": getFeishuUserAgent(),
344
- },
345
- body: JSON.stringify({
346
- content: text,
347
- sequence: this.state.sequence,
348
- uuid: `s_${this.state.cardId}_${this.state.sequence}`,
349
- }),
350
- },
351
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
352
- auditContext: "feishu.streaming-card.update",
353
- });
354
- await release();
355
- if (!response.ok) {
356
- onError?.(new Error(`Update card content failed with HTTP ${response.status}`));
357
- return false;
358
- }
359
- return true;
360
- } catch (error) {
361
- onError?.(error);
362
- return false;
363
- }
364
- }
365
-
366
- private async replaceCardContent(
367
- text: string,
368
- onError?: (error: unknown) => void,
369
- ): Promise<boolean> {
370
- if (!this.state) {
371
- return false;
372
- }
373
- const apiBase = resolveApiBase(this.creds.domain);
374
- this.state.sequence += 1;
375
- try {
376
- const { response, release } = await fetchWithSsrFGuard({
377
- url: `${apiBase}/cardkit/v1/cards/${this.state.cardId}/elements/content`,
378
- init: {
379
- method: "PUT",
380
- headers: {
381
- Authorization: `Bearer ${await getToken(this.creds)}`,
382
- "Content-Type": "application/json",
383
- "User-Agent": getFeishuUserAgent(),
384
- },
385
- body: JSON.stringify({
386
- element: JSON.stringify({ tag: "markdown", content: text, element_id: "content" }),
387
- sequence: this.state.sequence,
388
- uuid: `r_${this.state.cardId}_${this.state.sequence}`,
389
- }),
390
- },
391
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
392
- auditContext: "feishu.streaming-card.replace",
393
- });
394
- await release();
395
- if (!response.ok) {
396
- onError?.(new Error(`Replace card content failed with HTTP ${response.status}`));
397
- return false;
398
- }
399
- return true;
400
- } catch (error) {
401
- onError?.(error);
402
- return false;
403
- }
404
- }
405
-
406
- private clearFlushTimer(): void {
407
- if (this.flushTimer) {
408
- clearTimeout(this.flushTimer);
409
- this.flushTimer = null;
410
- }
411
- }
412
-
413
- private schedulePendingFlush(): void {
414
- if (this.flushTimer || !this.pendingText || this.closed) {
415
- return;
416
- }
417
- const delayMs = Math.max(0, this.updateThrottleMs - (Date.now() - this.lastUpdateTime));
418
- this.flushTimer = setTimeout(() => {
419
- this.flushTimer = null;
420
- const pending = this.pendingText;
421
- if (!pending || this.closed) {
422
- return;
423
- }
424
- void this.update(pending);
425
- }, delayMs);
426
- }
427
-
428
- async update(text: string): Promise<void> {
429
- if (!this.state || this.closed) {
430
- return;
431
- }
432
- const mergedInput = mergeStreamingText(this.pendingText ?? this.state.currentText, text);
433
- if (!mergedInput || mergedInput === this.state.currentText) {
434
- return;
435
- }
436
- this.pendingText = mergedInput;
437
- this.clearFlushTimer();
438
-
439
- const shouldForceUpdate = shouldPushStreamingUpdate(this.state.currentText, mergedInput);
440
- const now = Date.now();
441
- if (!shouldForceUpdate && now - this.lastUpdateTime < this.updateThrottleMs) {
442
- this.schedulePendingFlush();
443
- return;
444
- }
445
- this.lastUpdateTime = now;
446
-
447
- this.queue = this.queue.then(async () => {
448
- if (!this.state || this.closed) {
449
- return;
450
- }
451
- const nextText = this.pendingText ?? mergedInput;
452
- const mergedText = mergeStreamingText(this.state.currentText, nextText);
453
- if (!mergedText || mergedText === this.state.currentText) {
454
- return;
455
- }
456
- const appendContent = resolveStreamingCardAppendContent(this.state.sentText, mergedText);
457
- if (!appendContent) {
458
- return;
459
- }
460
- this.pendingText = null;
461
- this.state.currentText = mergedText;
462
- const sent = await this.updateCardContent(appendContent, (e) =>
463
- this.log?.(`Update failed: ${String(e)}`),
464
- );
465
- if (sent && this.state) {
466
- this.state.sentText = mergedText;
467
- }
468
- });
469
- await this.queue;
470
- }
471
-
472
- private async updateNoteContent(note: string): Promise<void> {
473
- if (!this.state || !this.state.hasNote) {
474
- return;
475
- }
476
- const apiBase = resolveApiBase(this.creds.domain);
477
- this.state.sequence += 1;
478
- await fetchWithSsrFGuard({
479
- url: `${apiBase}/cardkit/v1/cards/${this.state.cardId}/elements/note/content`,
480
- init: {
481
- method: "PUT",
482
- headers: {
483
- Authorization: `Bearer ${await getToken(this.creds)}`,
484
- "Content-Type": "application/json",
485
- "User-Agent": getFeishuUserAgent(),
486
- },
487
- body: JSON.stringify({
488
- content: `<font color='grey'>${note}</font>`,
489
- sequence: this.state.sequence,
490
- uuid: `n_${this.state.cardId}_${this.state.sequence}`,
491
- }),
492
- },
493
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
494
- auditContext: "feishu.streaming-card.note-update",
495
- })
496
- .then(async ({ release }) => {
497
- await release();
498
- })
499
- .catch((e) => this.log?.(`Note update failed: ${String(e)}`));
500
- }
501
-
502
- async close(finalText?: string, options?: { note?: string }): Promise<void> {
503
- if (!this.state || this.closed) {
504
- return;
505
- }
506
- this.closed = true;
507
- this.clearFlushTimer();
508
- await this.queue;
509
-
510
- const pendingMerged = mergeStreamingText(this.state.currentText, this.pendingText ?? undefined);
511
- const text = finalText ?? pendingMerged;
512
- const apiBase = resolveApiBase(this.creds.domain);
513
-
514
- // Only send final update if content differs from what's already displayed
515
- if (text && text !== this.state.sentText) {
516
- const sent = text.startsWith(this.state.sentText)
517
- ? await this.updateCardContent(
518
- resolveStreamingCardAppendContent(this.state.sentText, text),
519
- (e) => this.log?.(`Final update failed: ${String(e)}`),
520
- )
521
- : await this.replaceCardContent(text, (e) =>
522
- this.log?.(`Final replace failed: ${String(e)}`),
523
- );
524
- this.state.currentText = text;
525
- if (sent) {
526
- this.state.sentText = text;
527
- }
528
- }
529
-
530
- // Update note with final model/provider info
531
- if (options?.note) {
532
- await this.updateNoteContent(options.note);
533
- }
534
-
535
- // Close streaming mode
536
- this.state.sequence += 1;
537
- await fetchWithSsrFGuard({
538
- url: `${apiBase}/cardkit/v1/cards/${this.state.cardId}/settings`,
539
- init: {
540
- method: "PATCH",
541
- headers: {
542
- Authorization: `Bearer ${await getToken(this.creds)}`,
543
- "Content-Type": "application/json; charset=utf-8",
544
- "User-Agent": getFeishuUserAgent(),
545
- },
546
- body: JSON.stringify({
547
- settings: JSON.stringify({
548
- config: { streaming_mode: false, summary: { content: truncateSummary(text) } },
549
- }),
550
- sequence: this.state.sequence,
551
- uuid: `c_${this.state.cardId}_${this.state.sequence}`,
552
- }),
553
- },
554
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
555
- auditContext: "feishu.streaming-card.close",
556
- })
557
- .then(async ({ release }) => {
558
- await release();
559
- })
560
- .catch((e) => this.log?.(`Close failed: ${String(e)}`));
561
- const finalState = this.state;
562
- this.state = null;
563
- this.pendingText = null;
564
-
565
- this.log?.(`Closed streaming: cardId=${finalState.cardId}`);
566
- }
567
-
568
- isActive(): boolean {
569
- return this.state !== null && !this.closed;
570
- }
571
- }