@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,188 @@
|
|
|
1
|
+
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { ClawdbotConfig } from "../runtime-api.js";
|
|
3
|
+
import { expectFirstSentCardUsesFillWidthOnly } from "./card-test-helpers.js";
|
|
4
|
+
import { createFeishuBotMenuHandler } from "./monitor.bot-menu-handler.js";
|
|
5
|
+
|
|
6
|
+
const handleFeishuMessageMock = vi.hoisted(() => vi.fn(async (_params?: unknown) => {}));
|
|
7
|
+
const parseFeishuMessageEventMock = vi.hoisted(() => vi.fn());
|
|
8
|
+
const sendCardFeishuMock = vi.hoisted(() =>
|
|
9
|
+
vi.fn(async (_params?: unknown) => ({ messageId: "m1", chatId: "c1" })),
|
|
10
|
+
);
|
|
11
|
+
const getMessageFeishuMock = vi.hoisted(() => vi.fn());
|
|
12
|
+
|
|
13
|
+
const originalStateDir = process.env.KLAW_STATE_DIR;
|
|
14
|
+
|
|
15
|
+
vi.mock("./bot.js", () => {
|
|
16
|
+
return {
|
|
17
|
+
handleFeishuMessage: handleFeishuMessageMock,
|
|
18
|
+
parseFeishuMessageEvent: parseFeishuMessageEventMock,
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
vi.mock("./send.js", () => {
|
|
23
|
+
return {
|
|
24
|
+
sendCardFeishu: sendCardFeishuMock,
|
|
25
|
+
getMessageFeishu: getMessageFeishuMock,
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
function createBotMenuEvent(params: { eventKey: string; timestamp: string }) {
|
|
30
|
+
return {
|
|
31
|
+
event_key: params.eventKey,
|
|
32
|
+
timestamp: params.timestamp,
|
|
33
|
+
operator: {
|
|
34
|
+
operator_id: {
|
|
35
|
+
open_id: "ou_user1",
|
|
36
|
+
user_id: "user_1",
|
|
37
|
+
union_id: "union_1",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function registerHandlers() {
|
|
44
|
+
return createFeishuBotMenuHandler({
|
|
45
|
+
cfg: {} as ClawdbotConfig,
|
|
46
|
+
accountId: "default",
|
|
47
|
+
runtime: {
|
|
48
|
+
log: vi.fn(),
|
|
49
|
+
error: vi.fn(),
|
|
50
|
+
exit: vi.fn(),
|
|
51
|
+
},
|
|
52
|
+
chatHistories: new Map(),
|
|
53
|
+
fireAndForget: true,
|
|
54
|
+
getBotOpenId: () => "ou_bot",
|
|
55
|
+
getBotName: () => "Bot",
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function firstMockArg(mock: { mock: { calls: Array<readonly unknown[]> } }, label: string) {
|
|
60
|
+
const call = mock.mock.calls[0];
|
|
61
|
+
if (!call) {
|
|
62
|
+
throw new Error(`expected ${label} call`);
|
|
63
|
+
}
|
|
64
|
+
return call[0];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
describe("Feishu bot menu handler", () => {
|
|
68
|
+
afterAll(() => {
|
|
69
|
+
vi.doUnmock("./bot.js");
|
|
70
|
+
vi.doUnmock("./send.js");
|
|
71
|
+
vi.resetModules();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
beforeEach(() => {
|
|
75
|
+
vi.clearAllMocks();
|
|
76
|
+
process.env.KLAW_STATE_DIR = `/tmp/klaw-feishu-bot-menu-test-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
afterEach(() => {
|
|
80
|
+
if (originalStateDir === undefined) {
|
|
81
|
+
delete process.env.KLAW_STATE_DIR;
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
process.env.KLAW_STATE_DIR = originalStateDir;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("opens the quick-action launcher card at the webhook/event layer", async () => {
|
|
88
|
+
const onBotMenu = await registerHandlers();
|
|
89
|
+
|
|
90
|
+
await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000000" }));
|
|
91
|
+
|
|
92
|
+
expect(sendCardFeishuMock).toHaveBeenCalledTimes(1);
|
|
93
|
+
const sendArgs = firstMockArg(sendCardFeishuMock, "Feishu card send") as
|
|
94
|
+
| {
|
|
95
|
+
accountId?: string;
|
|
96
|
+
card?: {
|
|
97
|
+
config?: { width_mode?: string };
|
|
98
|
+
header?: { title?: { content?: string } };
|
|
99
|
+
};
|
|
100
|
+
to?: string;
|
|
101
|
+
}
|
|
102
|
+
| undefined;
|
|
103
|
+
expect(sendArgs?.to).toBe("user:ou_user1");
|
|
104
|
+
expect(sendArgs?.accountId).toBe("default");
|
|
105
|
+
expect(sendArgs?.card?.config?.width_mode).toBe("fill");
|
|
106
|
+
expect(sendArgs?.card?.header?.title?.content).toBe("Quick actions");
|
|
107
|
+
expect(handleFeishuMessageMock).not.toHaveBeenCalled();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("does not block bot-menu handling on quick-action launcher send", async () => {
|
|
111
|
+
const onBotMenu = await registerHandlers();
|
|
112
|
+
let resolveSend: (() => void) | undefined;
|
|
113
|
+
sendCardFeishuMock.mockImplementationOnce(
|
|
114
|
+
() =>
|
|
115
|
+
new Promise((resolve) => {
|
|
116
|
+
resolveSend = () => resolve({ messageId: "m1", chatId: "c1" });
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
const pending = onBotMenu(
|
|
121
|
+
createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000001" }),
|
|
122
|
+
);
|
|
123
|
+
let settled = false;
|
|
124
|
+
void pending.finally(() => {
|
|
125
|
+
settled = true;
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
await vi.waitFor(() => {
|
|
129
|
+
expect(settled).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
resolveSend?.();
|
|
133
|
+
await pending;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("falls back to the legacy /menu synthetic message path for unrelated bot menu keys", async () => {
|
|
137
|
+
const onBotMenu = await registerHandlers();
|
|
138
|
+
|
|
139
|
+
await onBotMenu(createBotMenuEvent({ eventKey: "custom-key", timestamp: "1700000000002" }));
|
|
140
|
+
|
|
141
|
+
expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1);
|
|
142
|
+
const handleArgs = firstMockArg(handleFeishuMessageMock, "Feishu synthetic message") as
|
|
143
|
+
| { event?: { message?: { content?: string } } }
|
|
144
|
+
| undefined;
|
|
145
|
+
expect(handleArgs?.event?.message?.content).toBe('{"text":"/menu custom-key"}');
|
|
146
|
+
expect(sendCardFeishuMock).not.toHaveBeenCalled();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("falls back to the legacy /menu path when launcher rendering fails", async () => {
|
|
150
|
+
const onBotMenu = await registerHandlers();
|
|
151
|
+
sendCardFeishuMock.mockRejectedValueOnce(new Error("boom"));
|
|
152
|
+
|
|
153
|
+
await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000003" }));
|
|
154
|
+
|
|
155
|
+
await vi.waitFor(() => {
|
|
156
|
+
expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1);
|
|
157
|
+
});
|
|
158
|
+
const handleArgs = firstMockArg(handleFeishuMessageMock, "Feishu fallback message") as
|
|
159
|
+
| { event?: { message?: { content?: string } } }
|
|
160
|
+
| undefined;
|
|
161
|
+
expect(handleArgs?.event?.message?.content).toBe('{"text":"/menu quick-actions"}');
|
|
162
|
+
expectFirstSentCardUsesFillWidthOnly(sendCardFeishuMock);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("reopens replay for explicit retryable fallback failures", async () => {
|
|
166
|
+
const onBotMenu = await registerHandlers();
|
|
167
|
+
sendCardFeishuMock
|
|
168
|
+
.mockImplementationOnce(async () => {
|
|
169
|
+
throw new Error("boom");
|
|
170
|
+
})
|
|
171
|
+
.mockImplementationOnce(async () => {
|
|
172
|
+
throw new Error("boom");
|
|
173
|
+
});
|
|
174
|
+
handleFeishuMessageMock
|
|
175
|
+
.mockRejectedValueOnce(
|
|
176
|
+
Object.assign(new Error("retry me"), {
|
|
177
|
+
name: "FeishuRetryableSyntheticEventError",
|
|
178
|
+
}),
|
|
179
|
+
)
|
|
180
|
+
.mockResolvedValueOnce(undefined);
|
|
181
|
+
|
|
182
|
+
await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000004" }));
|
|
183
|
+
await onBotMenu(createBotMenuEvent({ eventKey: "quick-actions", timestamp: "1700000000004" }));
|
|
184
|
+
|
|
185
|
+
expect(sendCardFeishuMock).toHaveBeenCalledTimes(2);
|
|
186
|
+
expect(handleFeishuMessageMock).toHaveBeenCalledTimes(1);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import "./lifecycle.test-support.js";
|
|
2
|
+
import { createNonExitingRuntimeEnv } from "klaw/plugin-sdk/plugin-test-runtime";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import type { ClawdbotConfig, RuntimeEnv } from "../runtime-api.js";
|
|
5
|
+
import { FeishuConfigSchema } from "./config-schema.js";
|
|
6
|
+
import {
|
|
7
|
+
getFeishuLifecycleTestMocks,
|
|
8
|
+
resetFeishuLifecycleTestMocks,
|
|
9
|
+
} from "./lifecycle.test-support.js";
|
|
10
|
+
import {
|
|
11
|
+
createFeishuTextMessageEvent,
|
|
12
|
+
createFeishuLifecycleReplyDispatcher,
|
|
13
|
+
installFeishuLifecycleReplyRuntime,
|
|
14
|
+
mockFeishuReplyOnceDispatch,
|
|
15
|
+
restoreFeishuLifecycleStateDir,
|
|
16
|
+
runFeishuLifecycleSequence,
|
|
17
|
+
setFeishuLifecycleStateDir,
|
|
18
|
+
setupFeishuLifecycleHandler,
|
|
19
|
+
} from "./test-support/lifecycle-test-support.js";
|
|
20
|
+
import type { FeishuConfig, ResolvedFeishuAccount } from "./types.js";
|
|
21
|
+
|
|
22
|
+
const {
|
|
23
|
+
createEventDispatcherMock,
|
|
24
|
+
createFeishuReplyDispatcherMock,
|
|
25
|
+
dispatchReplyFromConfigMock,
|
|
26
|
+
finalizeInboundContextMock,
|
|
27
|
+
resolveAgentRouteMock,
|
|
28
|
+
resolveBoundConversationMock,
|
|
29
|
+
withReplyDispatcherMock,
|
|
30
|
+
} = getFeishuLifecycleTestMocks();
|
|
31
|
+
|
|
32
|
+
let handlersByAccount = new Map<string, Record<string, (data: unknown) => Promise<void>>>();
|
|
33
|
+
let runtimesByAccount = new Map<string, RuntimeEnv>();
|
|
34
|
+
const originalStateDir = process.env.KLAW_STATE_DIR;
|
|
35
|
+
|
|
36
|
+
function createLifecycleConfig(): ClawdbotConfig {
|
|
37
|
+
return {
|
|
38
|
+
broadcast: {
|
|
39
|
+
oc_broadcast_group: ["susan", "main"],
|
|
40
|
+
},
|
|
41
|
+
agents: {
|
|
42
|
+
list: [{ id: "main" }, { id: "susan" }],
|
|
43
|
+
},
|
|
44
|
+
channels: {
|
|
45
|
+
feishu: {
|
|
46
|
+
enabled: true,
|
|
47
|
+
groupPolicy: "open",
|
|
48
|
+
requireMention: false,
|
|
49
|
+
resolveSenderNames: false,
|
|
50
|
+
accounts: {
|
|
51
|
+
"account-A": {
|
|
52
|
+
enabled: true,
|
|
53
|
+
appId: "cli_a",
|
|
54
|
+
appSecret: "secret_a", // pragma: allowlist secret
|
|
55
|
+
connectionMode: "websocket",
|
|
56
|
+
groupPolicy: "open",
|
|
57
|
+
requireMention: false,
|
|
58
|
+
resolveSenderNames: false,
|
|
59
|
+
groups: {
|
|
60
|
+
oc_broadcast_group: {
|
|
61
|
+
requireMention: false,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
"account-B": {
|
|
66
|
+
enabled: true,
|
|
67
|
+
appId: "cli_b",
|
|
68
|
+
appSecret: "secret_b", // pragma: allowlist secret
|
|
69
|
+
connectionMode: "websocket",
|
|
70
|
+
groupPolicy: "open",
|
|
71
|
+
requireMention: false,
|
|
72
|
+
resolveSenderNames: false,
|
|
73
|
+
groups: {
|
|
74
|
+
oc_broadcast_group: {
|
|
75
|
+
requireMention: false,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
messages: {
|
|
83
|
+
inbound: {
|
|
84
|
+
debounceMs: 0,
|
|
85
|
+
byChannel: {
|
|
86
|
+
feishu: 0,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
} as ClawdbotConfig;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function createLifecycleAccount(accountId: "account-A" | "account-B"): ResolvedFeishuAccount {
|
|
94
|
+
const config: FeishuConfig = FeishuConfigSchema.parse({
|
|
95
|
+
enabled: true,
|
|
96
|
+
connectionMode: "websocket",
|
|
97
|
+
groupPolicy: "open",
|
|
98
|
+
requireMention: false,
|
|
99
|
+
resolveSenderNames: false,
|
|
100
|
+
groups: {
|
|
101
|
+
oc_broadcast_group: {
|
|
102
|
+
requireMention: false,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
accountId,
|
|
108
|
+
selectionSource: "explicit",
|
|
109
|
+
enabled: true,
|
|
110
|
+
configured: true,
|
|
111
|
+
appId: accountId === "account-A" ? "cli_a" : "cli_b",
|
|
112
|
+
appSecret: accountId === "account-A" ? "secret_a" : "secret_b", // pragma: allowlist secret
|
|
113
|
+
domain: "feishu",
|
|
114
|
+
config,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async function setupLifecycleMonitor(accountId: "account-A" | "account-B") {
|
|
119
|
+
const runtime = createNonExitingRuntimeEnv();
|
|
120
|
+
runtimesByAccount.set(accountId, runtime);
|
|
121
|
+
return setupFeishuLifecycleHandler({
|
|
122
|
+
createEventDispatcherMock,
|
|
123
|
+
onRegister: (registered) => {
|
|
124
|
+
handlersByAccount.set(accountId, registered);
|
|
125
|
+
},
|
|
126
|
+
runtime,
|
|
127
|
+
cfg: createLifecycleConfig(),
|
|
128
|
+
account: createLifecycleAccount(accountId),
|
|
129
|
+
handlerKey: "im.message.receive_v1",
|
|
130
|
+
missingHandlerMessage: `missing im.message.receive_v1 handler for ${accountId}`,
|
|
131
|
+
once: true,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
describe("Feishu broadcast reply-once lifecycle", () => {
|
|
136
|
+
beforeEach(() => {
|
|
137
|
+
vi.useRealTimers();
|
|
138
|
+
resetFeishuLifecycleTestMocks();
|
|
139
|
+
handlersByAccount = new Map();
|
|
140
|
+
runtimesByAccount = new Map();
|
|
141
|
+
setFeishuLifecycleStateDir("klaw-feishu-broadcast");
|
|
142
|
+
|
|
143
|
+
createFeishuReplyDispatcherMock.mockReturnValue(createFeishuLifecycleReplyDispatcher());
|
|
144
|
+
|
|
145
|
+
resolveBoundConversationMock.mockReturnValue(null);
|
|
146
|
+
resolveAgentRouteMock.mockReturnValue({
|
|
147
|
+
agentId: "main",
|
|
148
|
+
channel: "feishu",
|
|
149
|
+
accountId: "account-A",
|
|
150
|
+
sessionKey: "agent:main:feishu:group:oc_broadcast_group",
|
|
151
|
+
mainSessionKey: "agent:main:main",
|
|
152
|
+
matchedBy: "default",
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
mockFeishuReplyOnceDispatch({
|
|
156
|
+
dispatchReplyFromConfigMock,
|
|
157
|
+
replyText: "broadcast reply once",
|
|
158
|
+
shouldSendFinalReply: (ctx) =>
|
|
159
|
+
typeof (ctx as { SessionKey?: string } | undefined)?.SessionKey === "string" &&
|
|
160
|
+
(ctx as { SessionKey: string }).SessionKey.includes("agent:main:"),
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
withReplyDispatcherMock.mockImplementation(async ({ run }) => await run());
|
|
164
|
+
|
|
165
|
+
installFeishuLifecycleReplyRuntime({
|
|
166
|
+
resolveAgentRouteMock,
|
|
167
|
+
finalizeInboundContextMock,
|
|
168
|
+
dispatchReplyFromConfigMock,
|
|
169
|
+
withReplyDispatcherMock,
|
|
170
|
+
storePath: "/tmp/feishu-broadcast-sessions.json",
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
afterEach(() => {
|
|
175
|
+
vi.useRealTimers();
|
|
176
|
+
restoreFeishuLifecycleStateDir(originalStateDir);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("uses one active reply path when the same broadcast event reaches two accounts", async () => {
|
|
180
|
+
const onMessageA = await setupLifecycleMonitor("account-A");
|
|
181
|
+
const onMessageB = await setupLifecycleMonitor("account-B");
|
|
182
|
+
const event = createFeishuTextMessageEvent({
|
|
183
|
+
messageId: "om_broadcast_once",
|
|
184
|
+
chatId: "oc_broadcast_group",
|
|
185
|
+
text: "hello broadcast",
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
await runFeishuLifecycleSequence(
|
|
189
|
+
[() => onMessageA(event), () => onMessageB(event)],
|
|
190
|
+
[
|
|
191
|
+
() => {
|
|
192
|
+
expect(dispatchReplyFromConfigMock.mock.calls.length).toBeGreaterThan(0);
|
|
193
|
+
},
|
|
194
|
+
() => {
|
|
195
|
+
expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
|
|
196
|
+
expect(createFeishuReplyDispatcherMock).toHaveBeenCalledTimes(1);
|
|
197
|
+
},
|
|
198
|
+
],
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
expect(runtimesByAccount.get("account-A")?.error).not.toHaveBeenCalled();
|
|
202
|
+
expect(runtimesByAccount.get("account-B")?.error).not.toHaveBeenCalled();
|
|
203
|
+
|
|
204
|
+
expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
|
|
205
|
+
expect(createFeishuReplyDispatcherMock).toHaveBeenCalledTimes(1);
|
|
206
|
+
expect(createFeishuReplyDispatcherMock).toHaveBeenCalledWith(
|
|
207
|
+
expect.objectContaining({
|
|
208
|
+
accountId: "account-a",
|
|
209
|
+
chatId: "oc_broadcast_group",
|
|
210
|
+
replyToMessageId: "om_broadcast_once",
|
|
211
|
+
}),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
const sessionKeys = finalizeInboundContextMock.mock.calls.map(
|
|
215
|
+
(call) => (call[0] as { SessionKey?: string }).SessionKey,
|
|
216
|
+
);
|
|
217
|
+
expect(sessionKeys).toContain("agent:main:feishu:group:oc_broadcast_group");
|
|
218
|
+
expect(sessionKeys).toContain("agent:susan:feishu:group:oc_broadcast_group");
|
|
219
|
+
|
|
220
|
+
const activeDispatcher = createFeishuReplyDispatcherMock.mock.results[0]?.value.dispatcher as {
|
|
221
|
+
sendFinalReply: ReturnType<typeof vi.fn>;
|
|
222
|
+
};
|
|
223
|
+
expect(activeDispatcher.sendFinalReply).toHaveBeenCalledTimes(1);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it("does not duplicate delivery after a post-send failure on the first account", async () => {
|
|
227
|
+
const onMessageA = await setupLifecycleMonitor("account-A");
|
|
228
|
+
const onMessageB = await setupLifecycleMonitor("account-B");
|
|
229
|
+
const event = createFeishuTextMessageEvent({
|
|
230
|
+
messageId: "om_broadcast_retry",
|
|
231
|
+
chatId: "oc_broadcast_group",
|
|
232
|
+
text: "hello broadcast",
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
dispatchReplyFromConfigMock.mockImplementationOnce(async ({ ctx, dispatcher }) => {
|
|
236
|
+
if (typeof ctx?.SessionKey === "string" && ctx.SessionKey.includes("agent:susan:")) {
|
|
237
|
+
return { queuedFinal: false, counts: { final: 0 } };
|
|
238
|
+
}
|
|
239
|
+
await dispatcher.sendFinalReply({ text: "broadcast reply once" });
|
|
240
|
+
throw new Error("post-send failure");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
await runFeishuLifecycleSequence(
|
|
244
|
+
[() => onMessageA(event), () => onMessageB(event)],
|
|
245
|
+
[
|
|
246
|
+
() => {
|
|
247
|
+
expect(dispatchReplyFromConfigMock.mock.calls.length).toBeGreaterThan(0);
|
|
248
|
+
},
|
|
249
|
+
() => {
|
|
250
|
+
expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
|
|
251
|
+
},
|
|
252
|
+
],
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
expect(runtimesByAccount.get("account-A")?.error).not.toHaveBeenCalled();
|
|
256
|
+
expect(runtimesByAccount.get("account-B")?.error).not.toHaveBeenCalled();
|
|
257
|
+
expect(dispatchReplyFromConfigMock).toHaveBeenCalledTimes(2);
|
|
258
|
+
|
|
259
|
+
const activeDispatcher = createFeishuReplyDispatcherMock.mock.results[0]?.value.dispatcher as {
|
|
260
|
+
sendFinalReply: ReturnType<typeof vi.fn>;
|
|
261
|
+
};
|
|
262
|
+
expect(activeDispatcher.sendFinalReply).toHaveBeenCalledTimes(1);
|
|
263
|
+
});
|
|
264
|
+
});
|