@kodelyth/feishu 2026.5.39 → 2026.5.42
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/api.ts +32 -0
- package/channel-entry.ts +20 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +16 -0
- package/dist/accounts-D0ow-lRb.js +429 -0
- package/dist/api.js +2308 -0
- package/dist/app-registration-DBSnysKJ.js +184 -0
- package/dist/audio-preflight.runtime-Dpjbn-7r.js +7 -0
- package/dist/channel-13WQvQ0u.js +2115 -0
- package/dist/channel-entry.js +22 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-JMJonrJ4.js +729 -0
- package/dist/client-D1pzbBGo.js +157 -0
- package/dist/contract-api.js +9 -0
- package/dist/conversation-id-_58ecqlx.js +139 -0
- package/dist/drive-CgHOluXx.js +883 -0
- package/dist/index.js +68 -0
- package/dist/monitor-oWptK0zL.js +60 -0
- package/dist/monitor.account-DHaWlslg.js +5207 -0
- package/dist/monitor.state-C211a4tX.js +100 -0
- package/dist/probe-CF4duEpK.js +149 -0
- package/dist/rolldown-runtime-DUslC3ob.js +14 -0
- package/dist/runtime-DSh5rL_d.js +8 -0
- package/dist/runtime-api.js +14 -0
- package/dist/secret-contract-NSee-WzN.js +119 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/security-audit-DWVC0vSK.js +11 -0
- package/dist/security-audit-shared-Dpcwxeft.js +38 -0
- package/dist/security-contract-api.js +2 -0
- package/dist/send-DfZuV4Fi.js +1212 -0
- package/dist/session-conversation-Duaukbnl.js +27 -0
- package/dist/session-key-api.js +2 -0
- package/dist/setup-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/subagent-hooks-Dtegs0kh.js +235 -0
- package/dist/subagent-hooks-api.js +23 -0
- package/dist/targets-DFskxX4p.js +48 -0
- package/dist/thread-bindings-DI7lVSOE.js +222 -0
- package/index.ts +82 -0
- package/klaw.plugin.json +47 -1712
- package/package.json +4 -4
- package/runtime-api.ts +52 -0
- package/secret-contract-api.ts +5 -0
- package/security-contract-api.ts +1 -0
- package/session-key-api.ts +1 -0
- package/setup-api.ts +3 -0
- package/setup-entry.test.ts +19 -0
- package/setup-entry.ts +13 -0
- package/src/accounts.test.ts +480 -0
- package/src/accounts.ts +333 -0
- package/src/agent-config.ts +21 -0
- package/src/app-registration.ts +331 -0
- package/src/approval-auth.test.ts +24 -0
- package/src/approval-auth.ts +25 -0
- package/src/async.test.ts +35 -0
- package/src/async.ts +104 -0
- package/src/audio-preflight.runtime.ts +9 -0
- package/src/bitable.test.ts +136 -0
- package/src/bitable.ts +762 -0
- package/src/bot-content.ts +485 -0
- package/src/bot-group-name.test.ts +116 -0
- package/src/bot-runtime-api.ts +12 -0
- package/src/bot-sender-name.ts +125 -0
- package/src/bot.broadcast.test.ts +523 -0
- package/src/bot.card-action.test.ts +552 -0
- package/src/bot.checkBotMentioned.test.ts +265 -0
- package/src/bot.helpers.test.ts +135 -0
- package/src/bot.stripBotMention.test.ts +126 -0
- package/src/bot.test.ts +3671 -0
- package/src/bot.ts +1703 -0
- package/src/card-action.ts +447 -0
- package/src/card-interaction.test.ts +131 -0
- package/src/card-interaction.ts +159 -0
- package/src/card-test-helpers.ts +54 -0
- package/src/card-ux-approval.ts +65 -0
- package/src/card-ux-launcher.test.ts +106 -0
- package/src/card-ux-launcher.ts +121 -0
- package/src/card-ux-shared.ts +33 -0
- package/src/channel-runtime-api.ts +16 -0
- package/src/channel.runtime.ts +47 -0
- package/src/channel.test.ts +1151 -0
- package/src/channel.ts +1423 -0
- package/src/chat-schema.ts +25 -0
- package/src/chat.test.ts +240 -0
- package/src/chat.ts +188 -0
- package/src/client-timeout.ts +42 -0
- package/src/client.test.ts +447 -0
- package/src/client.ts +262 -0
- package/src/comment-dispatcher-runtime-api.ts +6 -0
- package/src/comment-dispatcher.test.ts +185 -0
- package/src/comment-dispatcher.ts +107 -0
- package/src/comment-handler-runtime-api.ts +3 -0
- package/src/comment-handler.test.ts +592 -0
- package/src/comment-handler.ts +303 -0
- package/src/comment-reaction.test.ts +138 -0
- package/src/comment-reaction.ts +259 -0
- package/src/comment-shared.test.ts +183 -0
- package/src/comment-shared.ts +406 -0
- package/src/comment-target.ts +44 -0
- package/src/config-schema.test.ts +326 -0
- package/src/config-schema.ts +335 -0
- package/src/conversation-id.test.ts +18 -0
- package/src/conversation-id.ts +199 -0
- package/src/dedup-runtime-api.ts +1 -0
- package/src/dedup.ts +141 -0
- package/src/dedupe-key.ts +72 -0
- package/src/directory.static.ts +61 -0
- package/src/directory.test.ts +141 -0
- package/src/directory.ts +124 -0
- package/src/doc-schema.ts +182 -0
- package/src/docx-batch-insert.test.ts +116 -0
- package/src/docx-batch-insert.ts +223 -0
- package/src/docx-color-text.ts +154 -0
- package/src/docx-table-ops.test.ts +53 -0
- package/src/docx-table-ops.ts +316 -0
- package/src/docx-types.ts +38 -0
- package/src/docx.account-selection.test.ts +95 -0
- package/src/docx.test.ts +701 -0
- package/src/docx.ts +1596 -0
- package/src/drive-schema.ts +92 -0
- package/src/drive.test.ts +1237 -0
- package/src/drive.ts +829 -0
- package/src/dynamic-agent.test.ts +155 -0
- package/src/dynamic-agent.ts +143 -0
- package/src/event-types.ts +45 -0
- package/src/external-keys.test.ts +20 -0
- package/src/external-keys.ts +19 -0
- package/src/lifecycle.test-support.ts +220 -0
- package/src/media.test.ts +955 -0
- package/src/media.ts +1105 -0
- package/src/mention-target.types.ts +5 -0
- package/src/mention.ts +114 -0
- package/src/message-action-contract.ts +13 -0
- package/src/monitor-state-runtime-api.ts +7 -0
- package/src/monitor-transport-runtime-api.ts +10 -0
- package/src/monitor.account.ts +492 -0
- package/src/monitor.acp-init-failure.lifecycle.test-support.ts +219 -0
- package/src/monitor.bot-identity.ts +86 -0
- package/src/monitor.bot-menu-handler.ts +165 -0
- package/src/monitor.bot-menu.lifecycle.test-support.ts +224 -0
- package/src/monitor.bot-menu.test.ts +188 -0
- package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +264 -0
- package/src/monitor.card-action.lifecycle.test-support.ts +421 -0
- package/src/monitor.cleanup.test.ts +383 -0
- package/src/monitor.comment-notice-handler.ts +105 -0
- package/src/monitor.comment.test.ts +967 -0
- package/src/monitor.comment.ts +1386 -0
- package/src/monitor.lifecycle.test.ts +4 -0
- package/src/monitor.message-handler.ts +350 -0
- package/src/monitor.reaction.lifecycle.test-support.ts +68 -0
- package/src/monitor.reaction.test.ts +739 -0
- package/src/monitor.startup.test.ts +213 -0
- package/src/monitor.startup.ts +74 -0
- package/src/monitor.state.defaults.test.ts +46 -0
- package/src/monitor.state.ts +170 -0
- package/src/monitor.synthetic-error.ts +18 -0
- package/src/monitor.test-mocks.ts +46 -0
- package/src/monitor.transport.ts +451 -0
- package/src/monitor.ts +100 -0
- package/src/monitor.webhook-e2e.test.ts +279 -0
- package/src/monitor.webhook-security.test.ts +389 -0
- package/src/monitor.webhook.test-helpers.ts +116 -0
- package/src/outbound-runtime-api.ts +1 -0
- package/src/outbound.test.ts +1118 -0
- package/src/outbound.ts +785 -0
- package/src/perm-schema.ts +52 -0
- package/src/perm.ts +170 -0
- package/src/pins.ts +108 -0
- package/src/policy.test.ts +223 -0
- package/src/policy.ts +318 -0
- package/src/post.test.ts +105 -0
- package/src/post.ts +275 -0
- package/src/probe.test.ts +283 -0
- package/src/probe.ts +166 -0
- package/src/processing-claims.ts +59 -0
- package/src/qr-terminal.ts +1 -0
- package/src/reactions.ts +123 -0
- package/src/reasoning-preview.test.ts +113 -0
- package/src/reasoning-preview.ts +28 -0
- package/src/reply-dispatcher-runtime-api.ts +7 -0
- package/src/reply-dispatcher.test.ts +1513 -0
- package/src/reply-dispatcher.ts +748 -0
- package/src/runtime.ts +9 -0
- package/src/secret-contract.ts +145 -0
- package/src/secret-input.ts +1 -0
- package/src/security-audit-shared.ts +69 -0
- package/src/security-audit.test.ts +59 -0
- package/src/security-audit.ts +1 -0
- package/src/send-result.ts +80 -0
- package/src/send-target.test.ts +86 -0
- package/src/send-target.ts +35 -0
- package/src/send.reply-fallback.test.ts +417 -0
- package/src/send.test.ts +621 -0
- package/src/send.ts +861 -0
- package/src/sequential-key.test.ts +72 -0
- package/src/sequential-key.ts +25 -0
- package/src/sequential-queue.test.ts +165 -0
- package/src/sequential-queue.ts +86 -0
- package/src/session-conversation.ts +42 -0
- package/src/session-route.ts +48 -0
- package/src/setup-core.ts +51 -0
- package/src/setup-surface.test.ts +484 -0
- package/src/setup-surface.ts +618 -0
- package/src/streaming-card.test.ts +397 -0
- package/src/streaming-card.ts +571 -0
- package/src/subagent-hooks.test.ts +627 -0
- package/src/subagent-hooks.ts +413 -0
- package/src/targets.ts +97 -0
- package/src/test-support/lifecycle-test-support.ts +454 -0
- package/src/thread-bindings.test.ts +180 -0
- package/src/thread-bindings.ts +331 -0
- package/src/tool-account-routing.test.ts +250 -0
- package/src/tool-account.test.ts +44 -0
- package/src/tool-account.ts +93 -0
- package/src/tool-factory-test-harness.ts +79 -0
- package/src/tool-result.test.ts +32 -0
- package/src/tool-result.ts +16 -0
- package/src/tools-config.test.ts +21 -0
- package/src/tools-config.ts +22 -0
- package/src/types.ts +106 -0
- package/src/typing.test.ts +144 -0
- package/src/typing.ts +214 -0
- package/src/wiki-schema.ts +69 -0
- package/src/wiki.ts +270 -0
- package/subagent-hooks-api.ts +31 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-entry.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/security-contract-api.js +0 -7
- package/session-key-api.js +0 -7
- package/setup-api.js +0 -7
- package/setup-entry.js +0 -7
- package/subagent-hooks-api.js +0 -7
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Type, type Static } from "typebox";
|
|
2
|
+
|
|
3
|
+
const CHAT_ACTION_VALUES = ["members", "info", "member_info"] as const;
|
|
4
|
+
const MEMBER_ID_TYPE_VALUES = ["open_id", "user_id", "union_id"] as const;
|
|
5
|
+
|
|
6
|
+
export const FeishuChatSchema = Type.Object({
|
|
7
|
+
action: Type.Unsafe<(typeof CHAT_ACTION_VALUES)[number]>({
|
|
8
|
+
type: "string",
|
|
9
|
+
enum: [...CHAT_ACTION_VALUES],
|
|
10
|
+
description: "Action to run: members | info | member_info",
|
|
11
|
+
}),
|
|
12
|
+
chat_id: Type.Optional(Type.String({ description: "Chat ID (from URL or event payload)" })),
|
|
13
|
+
member_id: Type.Optional(Type.String({ description: "Member ID for member_info lookups" })),
|
|
14
|
+
page_size: Type.Optional(Type.Number({ description: "Page size (1-100, default 50)" })),
|
|
15
|
+
page_token: Type.Optional(Type.String({ description: "Pagination token" })),
|
|
16
|
+
member_id_type: Type.Optional(
|
|
17
|
+
Type.Unsafe<(typeof MEMBER_ID_TYPE_VALUES)[number]>({
|
|
18
|
+
type: "string",
|
|
19
|
+
enum: [...MEMBER_ID_TYPE_VALUES],
|
|
20
|
+
description: "Member ID type (default: open_id)",
|
|
21
|
+
}),
|
|
22
|
+
),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export type FeishuChatParams = Static<typeof FeishuChatSchema>;
|
package/src/chat.test.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { createTestPluginApi } from "klaw/plugin-sdk/plugin-test-api";
|
|
2
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import type { KlawPluginApi, PluginRuntime } from "../runtime-api.js";
|
|
4
|
+
|
|
5
|
+
const createFeishuClientMock = vi.hoisted(() => vi.fn());
|
|
6
|
+
const chatGetMock = vi.hoisted(() => vi.fn());
|
|
7
|
+
const chatMembersGetMock = vi.hoisted(() => vi.fn());
|
|
8
|
+
const contactUserGetMock = vi.hoisted(() => vi.fn());
|
|
9
|
+
|
|
10
|
+
vi.mock("./client.js", () => ({
|
|
11
|
+
createFeishuClient: createFeishuClientMock,
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
let registerFeishuChatTools: typeof import("./chat.js").registerFeishuChatTools;
|
|
15
|
+
|
|
16
|
+
function createFeishuToolRuntime(): PluginRuntime {
|
|
17
|
+
return {} as PluginRuntime;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe("registerFeishuChatTools", () => {
|
|
21
|
+
function createChatToolApi(params: {
|
|
22
|
+
config: KlawPluginApi["config"];
|
|
23
|
+
registerTool: KlawPluginApi["registerTool"];
|
|
24
|
+
}): KlawPluginApi {
|
|
25
|
+
return createTestPluginApi({
|
|
26
|
+
id: "feishu-test",
|
|
27
|
+
name: "Feishu Test",
|
|
28
|
+
source: "local",
|
|
29
|
+
config: params.config,
|
|
30
|
+
runtime: createFeishuToolRuntime(),
|
|
31
|
+
logger: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
|
|
32
|
+
registerTool: params.registerTool,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
beforeAll(async () => {
|
|
37
|
+
({ registerFeishuChatTools } = await import("./chat.js"));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterAll(() => {
|
|
41
|
+
vi.doUnmock("./client.js");
|
|
42
|
+
vi.resetModules();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
vi.clearAllMocks();
|
|
47
|
+
createFeishuClientMock.mockReturnValue({
|
|
48
|
+
im: {
|
|
49
|
+
chat: { get: chatGetMock },
|
|
50
|
+
chatMembers: { get: chatMembersGetMock },
|
|
51
|
+
},
|
|
52
|
+
contact: {
|
|
53
|
+
user: { get: contactUserGetMock },
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("registers feishu_chat and handles info/members actions", async () => {
|
|
59
|
+
const registerTool = vi.fn();
|
|
60
|
+
registerFeishuChatTools(
|
|
61
|
+
createChatToolApi({
|
|
62
|
+
config: {
|
|
63
|
+
channels: {
|
|
64
|
+
feishu: {
|
|
65
|
+
enabled: true,
|
|
66
|
+
appId: "app_id",
|
|
67
|
+
appSecret: "app_secret", // pragma: allowlist secret
|
|
68
|
+
tools: { chat: true },
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
registerTool,
|
|
73
|
+
}),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
expect(registerTool).toHaveBeenCalledTimes(1);
|
|
77
|
+
const tool = registerTool.mock.calls[0]?.[0];
|
|
78
|
+
expect(tool?.name).toBe("feishu_chat");
|
|
79
|
+
|
|
80
|
+
chatGetMock.mockResolvedValueOnce({
|
|
81
|
+
code: 0,
|
|
82
|
+
data: { name: "group name", user_count: 3 },
|
|
83
|
+
});
|
|
84
|
+
const infoResult = await tool.execute("tc_1", { action: "info", chat_id: "oc_1" });
|
|
85
|
+
expect(infoResult.details).toEqual({
|
|
86
|
+
chat_id: "oc_1",
|
|
87
|
+
name: "group name",
|
|
88
|
+
description: undefined,
|
|
89
|
+
owner_id: undefined,
|
|
90
|
+
tenant_key: undefined,
|
|
91
|
+
user_count: 3,
|
|
92
|
+
chat_mode: undefined,
|
|
93
|
+
chat_type: undefined,
|
|
94
|
+
join_message_visibility: undefined,
|
|
95
|
+
leave_message_visibility: undefined,
|
|
96
|
+
membership_approval: undefined,
|
|
97
|
+
moderation_permission: undefined,
|
|
98
|
+
avatar: undefined,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
chatMembersGetMock.mockResolvedValueOnce({
|
|
102
|
+
code: 0,
|
|
103
|
+
data: {
|
|
104
|
+
has_more: false,
|
|
105
|
+
page_token: "",
|
|
106
|
+
items: [{ member_id: "ou_1", name: "member1", member_id_type: "open_id" }],
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
const membersResult = await tool.execute("tc_2", { action: "members", chat_id: "oc_1" });
|
|
110
|
+
expect(membersResult.details).toEqual({
|
|
111
|
+
chat_id: "oc_1",
|
|
112
|
+
has_more: false,
|
|
113
|
+
page_token: "",
|
|
114
|
+
members: [
|
|
115
|
+
{
|
|
116
|
+
member_id: "ou_1",
|
|
117
|
+
name: "member1",
|
|
118
|
+
tenant_key: undefined,
|
|
119
|
+
member_id_type: "open_id",
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
contactUserGetMock.mockResolvedValueOnce({
|
|
125
|
+
code: 0,
|
|
126
|
+
data: {
|
|
127
|
+
user: {
|
|
128
|
+
open_id: "ou_1",
|
|
129
|
+
name: "member1",
|
|
130
|
+
email: "member1@example.com",
|
|
131
|
+
department_ids: ["od_1"],
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
const memberInfoResult = await tool.execute("tc_3", {
|
|
136
|
+
action: "member_info",
|
|
137
|
+
member_id: "ou_1",
|
|
138
|
+
});
|
|
139
|
+
expect(memberInfoResult.details).toEqual({
|
|
140
|
+
member_id: "ou_1",
|
|
141
|
+
member_id_type: "open_id",
|
|
142
|
+
open_id: "ou_1",
|
|
143
|
+
user_id: undefined,
|
|
144
|
+
union_id: undefined,
|
|
145
|
+
name: "member1",
|
|
146
|
+
en_name: undefined,
|
|
147
|
+
nickname: undefined,
|
|
148
|
+
email: "member1@example.com",
|
|
149
|
+
enterprise_email: undefined,
|
|
150
|
+
mobile: undefined,
|
|
151
|
+
mobile_visible: undefined,
|
|
152
|
+
status: undefined,
|
|
153
|
+
avatar: undefined,
|
|
154
|
+
department_ids: ["od_1"],
|
|
155
|
+
department_path: undefined,
|
|
156
|
+
leader_user_id: undefined,
|
|
157
|
+
city: undefined,
|
|
158
|
+
country: undefined,
|
|
159
|
+
work_station: undefined,
|
|
160
|
+
join_time: undefined,
|
|
161
|
+
is_tenant_manager: undefined,
|
|
162
|
+
employee_no: undefined,
|
|
163
|
+
employee_type: undefined,
|
|
164
|
+
description: undefined,
|
|
165
|
+
job_title: undefined,
|
|
166
|
+
geo: undefined,
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("skips registration when chat tool is disabled", () => {
|
|
171
|
+
const registerTool = vi.fn();
|
|
172
|
+
registerFeishuChatTools(
|
|
173
|
+
createChatToolApi({
|
|
174
|
+
config: {
|
|
175
|
+
channels: {
|
|
176
|
+
feishu: {
|
|
177
|
+
enabled: true,
|
|
178
|
+
appId: "app_id",
|
|
179
|
+
appSecret: "app_secret", // pragma: allowlist secret
|
|
180
|
+
tools: { chat: false },
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
registerTool,
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
expect(registerTool).not.toHaveBeenCalled();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("preserves Feishu diagnostics from rejected member lookups", async () => {
|
|
191
|
+
const registerTool = vi.fn();
|
|
192
|
+
registerFeishuChatTools(
|
|
193
|
+
createChatToolApi({
|
|
194
|
+
config: {
|
|
195
|
+
channels: {
|
|
196
|
+
feishu: {
|
|
197
|
+
enabled: true,
|
|
198
|
+
appId: "app_id",
|
|
199
|
+
appSecret: "app_secret", // pragma: allowlist secret
|
|
200
|
+
tools: { chat: true },
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
registerTool,
|
|
205
|
+
}),
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
const tool = registerTool.mock.calls[0]?.[0];
|
|
209
|
+
contactUserGetMock.mockRejectedValueOnce(
|
|
210
|
+
Object.assign(new Error("Request failed with status code 400"), {
|
|
211
|
+
response: {
|
|
212
|
+
status: 400,
|
|
213
|
+
data: {
|
|
214
|
+
code: 99992360,
|
|
215
|
+
msg: "The request you send is not a valid {user_id} or not exists",
|
|
216
|
+
error: {
|
|
217
|
+
log_id: "20260429124800CHAT",
|
|
218
|
+
troubleshooter: "https://open.feishu.cn/search?log_id=20260429124800CHAT",
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
}),
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
const result = await tool.execute("tc_4", {
|
|
226
|
+
action: "member_info",
|
|
227
|
+
member_id: "ou_1",
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
expect(result.details.error).toContain('"http_status":400');
|
|
231
|
+
expect(result.details.error).toContain('"feishu_code":99992360');
|
|
232
|
+
expect(result.details.error).toContain(
|
|
233
|
+
'"feishu_msg":"The request you send is not a valid {user_id} or not exists"',
|
|
234
|
+
);
|
|
235
|
+
expect(result.details.error).toContain('"feishu_log_id":"20260429124800CHAT"');
|
|
236
|
+
expect(result.details.error).toContain(
|
|
237
|
+
'"feishu_troubleshooter":"https://open.feishu.cn/search?log_id=20260429124800CHAT"',
|
|
238
|
+
);
|
|
239
|
+
});
|
|
240
|
+
});
|
package/src/chat.ts
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type * as Lark from "@larksuiteoapi/node-sdk";
|
|
2
|
+
import type { KlawPluginApi } from "../runtime-api.js";
|
|
3
|
+
import { listEnabledFeishuAccounts } from "./accounts.js";
|
|
4
|
+
import { FeishuChatSchema, type FeishuChatParams } from "./chat-schema.js";
|
|
5
|
+
import { createFeishuClient } from "./client.js";
|
|
6
|
+
import { formatFeishuApiError } from "./comment-shared.js";
|
|
7
|
+
import { resolveToolsConfig } from "./tools-config.js";
|
|
8
|
+
|
|
9
|
+
function json(data: unknown) {
|
|
10
|
+
return {
|
|
11
|
+
content: [{ type: "text" as const, text: JSON.stringify(data, null, 2) }],
|
|
12
|
+
details: data,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function getChatInfo(client: Lark.Client, chatId: string) {
|
|
17
|
+
const res = await client.im.chat.get({ path: { chat_id: chatId } });
|
|
18
|
+
if (res.code !== 0) {
|
|
19
|
+
throw new Error(res.msg);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const chat = res.data;
|
|
23
|
+
return {
|
|
24
|
+
chat_id: chatId,
|
|
25
|
+
name: chat?.name,
|
|
26
|
+
description: chat?.description,
|
|
27
|
+
owner_id: chat?.owner_id,
|
|
28
|
+
tenant_key: chat?.tenant_key,
|
|
29
|
+
user_count: chat?.user_count,
|
|
30
|
+
chat_mode: chat?.chat_mode,
|
|
31
|
+
chat_type: chat?.chat_type,
|
|
32
|
+
join_message_visibility: chat?.join_message_visibility,
|
|
33
|
+
leave_message_visibility: chat?.leave_message_visibility,
|
|
34
|
+
membership_approval: chat?.membership_approval,
|
|
35
|
+
moderation_permission: chat?.moderation_permission,
|
|
36
|
+
avatar: chat?.avatar,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function getChatMembers(
|
|
41
|
+
client: Lark.Client,
|
|
42
|
+
chatId: string,
|
|
43
|
+
pageSize?: number,
|
|
44
|
+
pageToken?: string,
|
|
45
|
+
memberIdType?: "open_id" | "user_id" | "union_id",
|
|
46
|
+
) {
|
|
47
|
+
const page_size = pageSize ? Math.max(1, Math.min(100, pageSize)) : 50;
|
|
48
|
+
const res = await client.im.chatMembers.get({
|
|
49
|
+
path: { chat_id: chatId },
|
|
50
|
+
params: {
|
|
51
|
+
page_size,
|
|
52
|
+
page_token: pageToken,
|
|
53
|
+
member_id_type: memberIdType ?? "open_id",
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (res.code !== 0) {
|
|
58
|
+
throw new Error(res.msg);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
chat_id: chatId,
|
|
63
|
+
has_more: res.data?.has_more,
|
|
64
|
+
page_token: res.data?.page_token,
|
|
65
|
+
members:
|
|
66
|
+
res.data?.items?.map((item) => ({
|
|
67
|
+
member_id: item.member_id,
|
|
68
|
+
name: item.name,
|
|
69
|
+
tenant_key: item.tenant_key,
|
|
70
|
+
member_id_type: item.member_id_type,
|
|
71
|
+
})) ?? [],
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function getFeishuMemberInfo(
|
|
76
|
+
client: Lark.Client,
|
|
77
|
+
memberId: string,
|
|
78
|
+
memberIdType: "open_id" | "user_id" | "union_id" = "open_id",
|
|
79
|
+
) {
|
|
80
|
+
const res = await client.contact.user.get({
|
|
81
|
+
path: { user_id: memberId },
|
|
82
|
+
params: {
|
|
83
|
+
user_id_type: memberIdType,
|
|
84
|
+
department_id_type: "open_department_id",
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (res.code !== 0) {
|
|
89
|
+
throw new Error(res.msg);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const user = res.data?.user;
|
|
93
|
+
return {
|
|
94
|
+
member_id: memberId,
|
|
95
|
+
member_id_type: memberIdType,
|
|
96
|
+
open_id: user?.open_id,
|
|
97
|
+
user_id: user?.user_id,
|
|
98
|
+
union_id: user?.union_id,
|
|
99
|
+
name: user?.name,
|
|
100
|
+
en_name: user?.en_name,
|
|
101
|
+
nickname: user?.nickname,
|
|
102
|
+
email: user?.email,
|
|
103
|
+
enterprise_email: user?.enterprise_email,
|
|
104
|
+
mobile: user?.mobile,
|
|
105
|
+
mobile_visible: user?.mobile_visible,
|
|
106
|
+
status: user?.status,
|
|
107
|
+
avatar: user?.avatar,
|
|
108
|
+
department_ids: user?.department_ids,
|
|
109
|
+
department_path: user?.department_path,
|
|
110
|
+
leader_user_id: user?.leader_user_id,
|
|
111
|
+
city: user?.city,
|
|
112
|
+
country: user?.country,
|
|
113
|
+
work_station: user?.work_station,
|
|
114
|
+
join_time: user?.join_time,
|
|
115
|
+
is_tenant_manager: user?.is_tenant_manager,
|
|
116
|
+
employee_no: user?.employee_no,
|
|
117
|
+
employee_type: user?.employee_type,
|
|
118
|
+
description: user?.description,
|
|
119
|
+
job_title: user?.job_title,
|
|
120
|
+
geo: user?.geo,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function registerFeishuChatTools(api: KlawPluginApi) {
|
|
125
|
+
if (!api.config) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const accounts = listEnabledFeishuAccounts(api.config);
|
|
130
|
+
if (accounts.length === 0) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const firstAccount = accounts[0];
|
|
135
|
+
const toolsCfg = resolveToolsConfig(firstAccount.config.tools);
|
|
136
|
+
if (!toolsCfg.chat) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const getClient = () => createFeishuClient(firstAccount);
|
|
141
|
+
|
|
142
|
+
api.registerTool(
|
|
143
|
+
{
|
|
144
|
+
name: "feishu_chat",
|
|
145
|
+
label: "Feishu Chat",
|
|
146
|
+
description: "Feishu chat operations. Actions: members, info, member_info",
|
|
147
|
+
parameters: FeishuChatSchema,
|
|
148
|
+
async execute(_toolCallId, params) {
|
|
149
|
+
const p = params as FeishuChatParams;
|
|
150
|
+
try {
|
|
151
|
+
const client = getClient();
|
|
152
|
+
switch (p.action) {
|
|
153
|
+
case "members":
|
|
154
|
+
if (!p.chat_id) {
|
|
155
|
+
return json({ error: "chat_id is required for action members" });
|
|
156
|
+
}
|
|
157
|
+
return json(
|
|
158
|
+
await getChatMembers(
|
|
159
|
+
client,
|
|
160
|
+
p.chat_id,
|
|
161
|
+
p.page_size,
|
|
162
|
+
p.page_token,
|
|
163
|
+
p.member_id_type,
|
|
164
|
+
),
|
|
165
|
+
);
|
|
166
|
+
case "info":
|
|
167
|
+
if (!p.chat_id) {
|
|
168
|
+
return json({ error: "chat_id is required for action info" });
|
|
169
|
+
}
|
|
170
|
+
return json(await getChatInfo(client, p.chat_id));
|
|
171
|
+
case "member_info":
|
|
172
|
+
if (!p.member_id) {
|
|
173
|
+
return json({ error: "member_id is required for action member_info" });
|
|
174
|
+
}
|
|
175
|
+
return json(
|
|
176
|
+
await getFeishuMemberInfo(client, p.member_id, p.member_id_type ?? "open_id"),
|
|
177
|
+
);
|
|
178
|
+
default:
|
|
179
|
+
return json({ error: `Unknown action: ${String(p.action)}` });
|
|
180
|
+
}
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return json({ error: formatFeishuApiError(err, { includeNestedErrorLogId: true }) });
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
{ name: "feishu_chat" },
|
|
187
|
+
);
|
|
188
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { FeishuConfig } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/** Default HTTP timeout for Feishu API requests (30 seconds). */
|
|
4
|
+
export const FEISHU_HTTP_TIMEOUT_MS = 30_000;
|
|
5
|
+
export const FEISHU_HTTP_TIMEOUT_MAX_MS = 300_000;
|
|
6
|
+
export const FEISHU_HTTP_TIMEOUT_ENV_VAR = "KLAW_FEISHU_HTTP_TIMEOUT_MS";
|
|
7
|
+
|
|
8
|
+
export type FeishuClientTimeoutConfig = {
|
|
9
|
+
httpTimeoutMs?: number;
|
|
10
|
+
config?: Pick<FeishuConfig, "httpTimeoutMs">;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function resolveConfiguredHttpTimeoutMs(creds: FeishuClientTimeoutConfig): number {
|
|
14
|
+
const clampTimeout = (value: number): number => {
|
|
15
|
+
const rounded = Math.floor(value);
|
|
16
|
+
return Math.min(Math.max(rounded, 1), FEISHU_HTTP_TIMEOUT_MAX_MS);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const fromDirectField = creds.httpTimeoutMs;
|
|
20
|
+
if (
|
|
21
|
+
typeof fromDirectField === "number" &&
|
|
22
|
+
Number.isFinite(fromDirectField) &&
|
|
23
|
+
fromDirectField > 0
|
|
24
|
+
) {
|
|
25
|
+
return clampTimeout(fromDirectField);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const envRaw = process.env[FEISHU_HTTP_TIMEOUT_ENV_VAR];
|
|
29
|
+
if (envRaw) {
|
|
30
|
+
const envValue = Number(envRaw);
|
|
31
|
+
if (Number.isFinite(envValue) && envValue > 0) {
|
|
32
|
+
return clampTimeout(envValue);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const fromConfig = creds.config?.httpTimeoutMs;
|
|
37
|
+
const timeout = fromConfig;
|
|
38
|
+
if (typeof timeout !== "number" || !Number.isFinite(timeout) || timeout <= 0) {
|
|
39
|
+
return FEISHU_HTTP_TIMEOUT_MS;
|
|
40
|
+
}
|
|
41
|
+
return clampTimeout(timeout);
|
|
42
|
+
}
|