@kodelyth/feishu 2026.5.42 → 2026.6.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 (192) hide show
  1. package/klaw.plugin.json +1712 -47
  2. package/package.json +17 -4
  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,93 +0,0 @@
1
- import type * as Lark from "@larksuiteoapi/node-sdk";
2
- import { normalizeOptionalString } from "klaw/plugin-sdk/string-coerce-runtime";
3
- import type { KlawPluginApi } from "../runtime-api.js";
4
- import {
5
- listFeishuAccountIds,
6
- resolveFeishuAccount,
7
- resolveFeishuRuntimeAccount,
8
- } from "./accounts.js";
9
- import { createFeishuClient } from "./client.js";
10
- import { resolveToolsConfig } from "./tools-config.js";
11
- import type { FeishuToolsConfig, ResolvedFeishuAccount } from "./types.js";
12
-
13
- type AccountAwareParams = { accountId?: string };
14
-
15
- function resolveImplicitToolAccountId(params: {
16
- api: Pick<KlawPluginApi, "config">;
17
- executeParams?: AccountAwareParams;
18
- defaultAccountId?: string;
19
- }): string | undefined {
20
- const explicitAccountId = normalizeOptionalString(params.executeParams?.accountId);
21
- if (explicitAccountId) {
22
- return explicitAccountId;
23
- }
24
-
25
- const contextualAccountId = normalizeOptionalString(params.defaultAccountId);
26
- if (
27
- contextualAccountId &&
28
- listFeishuAccountIds(params.api.config).includes(contextualAccountId)
29
- ) {
30
- const contextualAccount = resolveFeishuAccount({
31
- cfg: params.api.config,
32
- accountId: contextualAccountId,
33
- });
34
- if (contextualAccount.enabled) {
35
- return contextualAccountId;
36
- }
37
- }
38
-
39
- const configuredDefaultAccountId = normalizeOptionalString(
40
- (params.api.config?.channels?.feishu as { defaultAccount?: unknown } | undefined)
41
- ?.defaultAccount,
42
- );
43
- if (configuredDefaultAccountId) {
44
- return configuredDefaultAccountId;
45
- }
46
-
47
- return undefined;
48
- }
49
-
50
- export function resolveFeishuToolAccount(params: {
51
- api: Pick<KlawPluginApi, "config">;
52
- executeParams?: AccountAwareParams;
53
- defaultAccountId?: string;
54
- }): ResolvedFeishuAccount {
55
- if (!params.api.config) {
56
- throw new Error("Feishu config unavailable");
57
- }
58
- return resolveFeishuRuntimeAccount({
59
- cfg: params.api.config,
60
- accountId: resolveImplicitToolAccountId(params),
61
- });
62
- }
63
-
64
- export function createFeishuToolClient(params: {
65
- api: Pick<KlawPluginApi, "config">;
66
- executeParams?: AccountAwareParams;
67
- defaultAccountId?: string;
68
- }): Lark.Client {
69
- return createFeishuClient(resolveFeishuToolAccount(params));
70
- }
71
-
72
- export function resolveAnyEnabledFeishuToolsConfig(
73
- accounts: ResolvedFeishuAccount[],
74
- ): Required<FeishuToolsConfig> {
75
- const merged: Required<FeishuToolsConfig> = {
76
- doc: false,
77
- chat: false,
78
- wiki: false,
79
- drive: false,
80
- perm: false,
81
- scopes: false,
82
- };
83
- for (const account of accounts) {
84
- const cfg = resolveToolsConfig(account.config.tools);
85
- merged.doc = merged.doc || cfg.doc;
86
- merged.chat = merged.chat || cfg.chat;
87
- merged.wiki = merged.wiki || cfg.wiki;
88
- merged.drive = merged.drive || cfg.drive;
89
- merged.perm = merged.perm || cfg.perm;
90
- merged.scopes = merged.scopes || cfg.scopes;
91
- }
92
- return merged;
93
- }
@@ -1,79 +0,0 @@
1
- import type { KlawPluginApi } from "../runtime-api.js";
2
-
3
- type ToolContextLike = {
4
- agentAccountId?: string;
5
- };
6
-
7
- export type ToolLike = {
8
- name: string;
9
- execute: (
10
- toolCallId: string,
11
- params: unknown,
12
- ) => Promise<{ details: Record<string, unknown> }> | { details: Record<string, unknown> };
13
- };
14
-
15
- type RegisteredTool = {
16
- tool: unknown;
17
- opts?: { name?: string };
18
- };
19
-
20
- function toToolList(value: unknown): unknown[] {
21
- if (!value) {
22
- return [];
23
- }
24
- return Array.isArray(value) ? value : [value];
25
- }
26
-
27
- function asToolLike(tool: unknown, fallbackName?: string): ToolLike {
28
- const candidate = tool as Partial<ToolLike>;
29
- const name = candidate.name ?? fallbackName;
30
- const execute = candidate.execute;
31
- if (!name || typeof execute !== "function") {
32
- throw new Error(`Resolved tool is missing required fields (name=${String(name)})`);
33
- }
34
- return {
35
- name,
36
- execute: (toolCallId, params) => execute(toolCallId, params),
37
- };
38
- }
39
-
40
- export function createToolFactoryHarness(cfg: KlawPluginApi["config"]) {
41
- const registered: RegisteredTool[] = [];
42
-
43
- const api: Pick<KlawPluginApi, "config" | "logger" | "registerTool"> = {
44
- config: cfg,
45
- logger: {
46
- info: () => {},
47
- warn: () => {},
48
- error: () => {},
49
- debug: () => {},
50
- },
51
- registerTool: (tool, opts) => {
52
- registered.push({ tool, opts });
53
- },
54
- };
55
-
56
- const resolveTool = (name: string, ctx: ToolContextLike = {}): ToolLike => {
57
- for (const entry of registered) {
58
- if (entry.opts?.name === name && typeof entry.tool !== "function") {
59
- return asToolLike(entry.tool, name);
60
- }
61
-
62
- if (typeof entry.tool === "function") {
63
- const builtTools = toToolList(entry.tool(ctx));
64
- const hit = builtTools.find((tool) => (tool as { name?: string }).name === name);
65
- if (hit) {
66
- return asToolLike(hit, name);
67
- }
68
- } else if ((entry.tool as { name?: string }).name === name) {
69
- return asToolLike(entry.tool, name);
70
- }
71
- }
72
- throw new Error(`Tool not registered: ${name}`);
73
- };
74
-
75
- return {
76
- api: api as KlawPluginApi,
77
- resolveTool,
78
- };
79
- }
@@ -1,32 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import {
3
- jsonToolResult,
4
- toolExecutionErrorResult,
5
- unknownToolActionResult,
6
- } from "./tool-result.js";
7
-
8
- describe("jsonToolResult", () => {
9
- it("formats tool result with text content and details", () => {
10
- const payload = { ok: true, id: "abc" };
11
- expect(jsonToolResult(payload)).toEqual({
12
- content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
13
- details: payload,
14
- });
15
- });
16
-
17
- it("formats unknown action errors", () => {
18
- expect(unknownToolActionResult("create")).toEqual({
19
- content: [
20
- { type: "text", text: JSON.stringify({ error: "Unknown action: create" }, null, 2) },
21
- ],
22
- details: { error: "Unknown action: create" },
23
- });
24
- });
25
-
26
- it("formats execution errors", () => {
27
- expect(toolExecutionErrorResult(new Error("boom"))).toEqual({
28
- content: [{ type: "text", text: JSON.stringify({ error: "boom" }, null, 2) }],
29
- details: { error: "boom" },
30
- });
31
- });
32
- });
@@ -1,16 +0,0 @@
1
- import { formatErrorMessage } from "klaw/plugin-sdk/error-runtime";
2
-
3
- export function jsonToolResult(data: unknown) {
4
- return {
5
- content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
6
- details: data,
7
- };
8
- }
9
-
10
- export function unknownToolActionResult(action: unknown) {
11
- return jsonToolResult({ error: `Unknown action: ${String(action)}` });
12
- }
13
-
14
- export function toolExecutionErrorResult(error: unknown) {
15
- return jsonToolResult({ error: formatErrorMessage(error) });
16
- }
@@ -1,21 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { FeishuConfigSchema } from "./config-schema.js";
3
- import { resolveToolsConfig } from "./tools-config.js";
4
-
5
- describe("feishu tools config", () => {
6
- it("enables chat tool by default", () => {
7
- const resolved = resolveToolsConfig(undefined);
8
- expect(resolved.chat).toBe(true);
9
- });
10
-
11
- it("accepts tools.chat in config schema", () => {
12
- const parsed = FeishuConfigSchema.parse({
13
- enabled: true,
14
- tools: {
15
- chat: false,
16
- },
17
- });
18
-
19
- expect(parsed.tools?.chat).toBe(false);
20
- });
21
- });
@@ -1,22 +0,0 @@
1
- import type { FeishuToolsConfig } from "./types.js";
2
-
3
- /**
4
- * Default tool configuration.
5
- * - doc, chat, wiki, drive, scopes: enabled by default
6
- * - perm: disabled by default (sensitive operation)
7
- */
8
- const DEFAULT_TOOLS_CONFIG: Required<FeishuToolsConfig> = {
9
- doc: true,
10
- chat: true,
11
- wiki: true,
12
- drive: true,
13
- perm: false,
14
- scopes: true,
15
- };
16
-
17
- /**
18
- * Resolve tools config with defaults.
19
- */
20
- export function resolveToolsConfig(cfg?: FeishuToolsConfig): Required<FeishuToolsConfig> {
21
- return { ...DEFAULT_TOOLS_CONFIG, ...cfg };
22
- }
package/src/types.ts DELETED
@@ -1,106 +0,0 @@
1
- import type { MessageReceipt } from "klaw/plugin-sdk/channel-message";
2
- import type { BaseProbeResult } from "klaw/plugin-sdk/core";
3
- import type { FeishuConfigSchema, FeishuAccountConfigSchema, z } from "./config-schema.js";
4
- import type { MentionTarget } from "./mention-target.types.js";
5
-
6
- export type FeishuConfig = z.infer<typeof FeishuConfigSchema>;
7
- export type FeishuAccountConfig = z.infer<typeof FeishuAccountConfigSchema>;
8
-
9
- export type FeishuDomain = "feishu" | "lark" | (string & {});
10
-
11
- export type FeishuDefaultAccountSelectionSource =
12
- | "explicit-default"
13
- | "mapped-default"
14
- | "fallback";
15
- type FeishuAccountSelectionSource = "explicit" | FeishuDefaultAccountSelectionSource;
16
-
17
- export type ResolvedFeishuAccount = {
18
- accountId: string;
19
- selectionSource: FeishuAccountSelectionSource;
20
- enabled: boolean;
21
- configured: boolean;
22
- name?: string;
23
- appId?: string;
24
- appSecret?: string;
25
- encryptKey?: string;
26
- verificationToken?: string;
27
- domain: FeishuDomain;
28
- /** Merged config (top-level defaults + account-specific overrides) */
29
- config: FeishuConfig;
30
- };
31
-
32
- export type FeishuIdType = "open_id" | "user_id" | "union_id" | "chat_id";
33
-
34
- export type FeishuMessageContext = {
35
- chatId: string;
36
- messageId: string;
37
- replyTargetMessageId?: string;
38
- suppressReplyTarget?: boolean;
39
- senderId: string;
40
- senderOpenId: string;
41
- senderName?: string;
42
- chatType: FeishuChatType;
43
- mentionedBot: boolean;
44
- hasAnyMention?: boolean;
45
- rootId?: string;
46
- parentId?: string;
47
- threadId?: string;
48
- content: string;
49
- contentType: string;
50
- /** Mention forward targets (excluding the bot itself) */
51
- mentionTargets?: MentionTarget[];
52
- };
53
-
54
- export type FeishuSendResult = {
55
- messageId: string;
56
- chatId: string;
57
- receipt: MessageReceipt;
58
- };
59
-
60
- export type FeishuChatType = "p2p" | "group" | "topic_group" | "private";
61
-
62
- export function isFeishuGroupChatType(chatType: FeishuChatType | undefined): boolean {
63
- return chatType === "group" || chatType === "topic_group";
64
- }
65
-
66
- export type FeishuMessageInfo = {
67
- messageId: string;
68
- chatId: string;
69
- chatType?: FeishuChatType;
70
- senderId?: string;
71
- senderOpenId?: string;
72
- senderType?: string;
73
- content: string;
74
- contentType: string;
75
- createTime?: number;
76
- /** Feishu thread ID (omt_xxx) — present when the message belongs to a topic thread. */
77
- threadId?: string;
78
- };
79
-
80
- export interface FeishuProbeResult extends BaseProbeResult {
81
- appId?: string;
82
- botName?: string;
83
- botOpenId?: string;
84
- }
85
-
86
- export type FeishuMediaInfo = {
87
- path: string;
88
- contentType?: string;
89
- placeholder: string;
90
- };
91
-
92
- export type FeishuToolsConfig = {
93
- doc?: boolean;
94
- chat?: boolean;
95
- wiki?: boolean;
96
- drive?: boolean;
97
- perm?: boolean;
98
- scopes?: boolean;
99
- };
100
-
101
- export type DynamicAgentCreationConfig = {
102
- enabled?: boolean;
103
- workspaceTemplate?: string;
104
- agentDirTemplate?: string;
105
- maxAgents?: number;
106
- };
@@ -1,144 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { isFeishuBackoffError, getBackoffCodeFromResponse, FeishuBackoffError } from "./typing.js";
3
-
4
- describe("isFeishuBackoffError", () => {
5
- it("returns true for HTTP 429 (AxiosError shape)", () => {
6
- const err = { response: { status: 429, data: {} } };
7
- expect(isFeishuBackoffError(err)).toBe(true);
8
- });
9
-
10
- it("returns true for Feishu quota exceeded code 99991403", () => {
11
- const err = { response: { status: 200, data: { code: 99991403 } } };
12
- expect(isFeishuBackoffError(err)).toBe(true);
13
- });
14
-
15
- it("returns true for Feishu rate limit code 99991400", () => {
16
- const err = { response: { status: 200, data: { code: 99991400 } } };
17
- expect(isFeishuBackoffError(err)).toBe(true);
18
- });
19
-
20
- it("returns true for SDK error with code 429", () => {
21
- const err = { code: 429, message: "too many requests" };
22
- expect(isFeishuBackoffError(err)).toBe(true);
23
- });
24
-
25
- it("returns true for SDK error with top-level code 99991403", () => {
26
- const err = { code: 99991403, message: "quota exceeded" };
27
- expect(isFeishuBackoffError(err)).toBe(true);
28
- });
29
-
30
- it("returns false for other HTTP errors (e.g. 500)", () => {
31
- const err = { response: { status: 500, data: {} } };
32
- expect(isFeishuBackoffError(err)).toBe(false);
33
- });
34
-
35
- it("returns false for non-rate-limit Feishu codes", () => {
36
- const err = { response: { status: 200, data: { code: 99991401 } } };
37
- expect(isFeishuBackoffError(err)).toBe(false);
38
- });
39
-
40
- it("returns false for generic Error", () => {
41
- expect(isFeishuBackoffError(new Error("network timeout"))).toBe(false);
42
- });
43
-
44
- it("returns false for null", () => {
45
- expect(isFeishuBackoffError(null)).toBe(false);
46
- });
47
-
48
- it("returns false for undefined", () => {
49
- expect(isFeishuBackoffError(undefined)).toBe(false);
50
- });
51
-
52
- it("returns false for string", () => {
53
- expect(isFeishuBackoffError("429")).toBe(false);
54
- });
55
-
56
- it("returns true for 429 even without data", () => {
57
- const err = { response: { status: 429 } };
58
- expect(isFeishuBackoffError(err)).toBe(true);
59
- });
60
- });
61
-
62
- describe("getBackoffCodeFromResponse", () => {
63
- it("returns backoff code for response with quota exceeded code", () => {
64
- const response = { code: 99991403, msg: "quota exceeded", data: null };
65
- expect(getBackoffCodeFromResponse(response)).toBe(response.code);
66
- });
67
-
68
- it("returns backoff code for response with rate limit code", () => {
69
- const response = { code: 99991400, msg: "rate limit", data: null };
70
- expect(getBackoffCodeFromResponse(response)).toBe(response.code);
71
- });
72
-
73
- it("returns backoff code for response with code 429", () => {
74
- const response = { code: 429, msg: "too many requests", data: null };
75
- expect(getBackoffCodeFromResponse(response)).toBe(response.code);
76
- });
77
-
78
- it("returns undefined for successful response (code 0)", () => {
79
- const response = { code: 0, msg: "success", data: { reaction_id: "r1" } };
80
- expect(getBackoffCodeFromResponse(response)).toBeUndefined();
81
- });
82
-
83
- it("returns undefined for other error codes", () => {
84
- const response = { code: 99991401, msg: "other error", data: null };
85
- expect(getBackoffCodeFromResponse(response)).toBeUndefined();
86
- });
87
-
88
- it("returns undefined for null", () => {
89
- expect(getBackoffCodeFromResponse(null)).toBeUndefined();
90
- });
91
-
92
- it("returns undefined for undefined", () => {
93
- expect(getBackoffCodeFromResponse(undefined)).toBeUndefined();
94
- });
95
-
96
- it("returns undefined for response without code field", () => {
97
- const response = { data: { reaction_id: "r1" } };
98
- expect(getBackoffCodeFromResponse(response)).toBeUndefined();
99
- });
100
- });
101
-
102
- describe("FeishuBackoffError", () => {
103
- it("is detected by isFeishuBackoffError via .code property", () => {
104
- const err = new FeishuBackoffError(99991403);
105
- expect(isFeishuBackoffError(err)).toBe(true);
106
- });
107
-
108
- it("is detected for rate limit code 99991400", () => {
109
- const err = new FeishuBackoffError(99991400);
110
- expect(isFeishuBackoffError(err)).toBe(true);
111
- });
112
-
113
- it("has correct name and message", () => {
114
- const err = new FeishuBackoffError(99991403);
115
- expect(err.name).toBe("FeishuBackoffError");
116
- expect(err.message).toBe("Feishu API backoff: code 99991403");
117
- expect(err.code).toBe(99991403);
118
- });
119
-
120
- it("is an instance of Error", () => {
121
- const err = new FeishuBackoffError(99991403);
122
- expect(err instanceof Error).toBe(true);
123
- });
124
-
125
- it("survives catch-and-rethrow pattern", () => {
126
- // Simulates the exact pattern in addTypingIndicator/removeTypingIndicator:
127
- // thrown inside try, caught by catch, isFeishuBackoffError must match
128
- let caught: unknown;
129
- try {
130
- try {
131
- throw new FeishuBackoffError(99991403);
132
- } catch (err) {
133
- if (isFeishuBackoffError(err)) {
134
- throw err; // re-thrown — this is the fix
135
- }
136
- // would be silently swallowed with plain Error
137
- caught = "swallowed";
138
- }
139
- } catch (err) {
140
- caught = err;
141
- }
142
- expect(caught).toBeInstanceOf(FeishuBackoffError);
143
- });
144
- });