@openclaw/feishu 2026.3.13 → 2026.5.2-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api.ts +31 -0
- package/channel-entry.ts +20 -0
- package/channel-plugin-api.ts +1 -0
- package/contract-api.ts +16 -0
- package/index.ts +70 -53
- package/openclaw.plugin.json +1827 -4
- package/package.json +32 -7
- package/runtime-api.ts +55 -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 +14 -0
- package/setup-entry.ts +13 -0
- package/src/accounts.test.ts +95 -7
- package/src/accounts.ts +199 -117
- 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 +43 -1
- package/src/audio-preflight.runtime.ts +9 -0
- package/src/bitable.test.ts +131 -0
- package/src/bitable.ts +59 -22
- package/src/bot-content.ts +474 -0
- package/src/bot-group-name.test.ts +108 -0
- package/src/bot-runtime-api.ts +12 -0
- package/src/bot-sender-name.ts +125 -0
- package/src/bot.broadcast.test.ts +463 -0
- package/src/bot.card-action.test.ts +519 -5
- package/src/bot.checkBotMentioned.test.ts +92 -20
- package/src/bot.helpers.test.ts +118 -0
- package/src/bot.stripBotMention.test.ts +13 -21
- package/src/bot.test.ts +1334 -401
- package/src/bot.ts +778 -775
- package/src/card-action.ts +408 -40
- package/src/card-interaction.test.ts +129 -0
- package/src/card-interaction.ts +159 -0
- package/src/card-test-helpers.ts +47 -0
- package/src/card-ux-approval.ts +65 -0
- package/src/card-ux-launcher.test.ts +99 -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 +914 -3
- package/src/channel.ts +1253 -309
- package/src/chat-schema.ts +5 -4
- package/src/chat.test.ts +135 -28
- package/src/chat.ts +68 -10
- package/src/client.test.ts +212 -103
- package/src/client.ts +115 -21
- package/src/comment-dispatcher-runtime-api.ts +6 -0
- package/src/comment-dispatcher.test.ts +169 -0
- package/src/comment-dispatcher.ts +107 -0
- package/src/comment-handler-runtime-api.ts +3 -0
- package/src/comment-handler.test.ts +486 -0
- package/src/comment-handler.ts +309 -0
- package/src/comment-reaction.test.ts +166 -0
- package/src/comment-reaction.ts +259 -0
- package/src/comment-shared.test.ts +182 -0
- package/src/comment-shared.ts +406 -0
- package/src/comment-target.ts +44 -0
- package/src/config-schema.test.ts +63 -1
- package/src/config-schema.ts +31 -4
- 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 +33 -95
- package/src/directory.static.ts +61 -0
- package/src/directory.test.ts +116 -20
- package/src/directory.ts +60 -92
- package/src/doc-schema.ts +1 -1
- package/src/docx-batch-insert.test.ts +39 -38
- package/src/docx-batch-insert.ts +55 -19
- package/src/docx-color-text.ts +9 -4
- package/src/docx-table-ops.test.ts +53 -0
- package/src/docx-table-ops.ts +52 -34
- package/src/docx-types.ts +38 -0
- package/src/docx.account-selection.test.ts +12 -3
- package/src/docx.test.ts +314 -74
- package/src/docx.ts +278 -122
- package/src/drive-schema.ts +47 -1
- package/src/drive.test.ts +1219 -0
- package/src/drive.ts +614 -13
- package/src/dynamic-agent.ts +10 -4
- package/src/event-types.ts +45 -0
- package/src/external-keys.ts +1 -1
- package/src/lifecycle.test-support.ts +220 -0
- package/src/media.test.ts +403 -26
- package/src/media.ts +509 -132
- package/src/mention-target.types.ts +5 -0
- package/src/mention.ts +32 -51
- package/src/message-action-contract.ts +13 -0
- package/src/monitor-state-runtime-api.ts +7 -0
- package/src/monitor-transport-runtime-api.ts +7 -0
- package/src/monitor.account.ts +218 -312
- 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 +178 -0
- package/src/monitor.broadcast.reply-once.lifecycle.test-support.ts +264 -0
- package/src/monitor.card-action.lifecycle.test-support.ts +373 -0
- package/src/monitor.cleanup.test.ts +376 -0
- package/src/monitor.comment-notice-handler.ts +105 -0
- package/src/monitor.comment.test.ts +937 -0
- package/src/monitor.comment.ts +1386 -0
- package/src/monitor.lifecycle.test.ts +4 -0
- package/src/monitor.message-handler.ts +339 -0
- package/src/monitor.reaction.lifecycle.test-support.ts +68 -0
- package/src/monitor.reaction.test.ts +108 -48
- package/src/monitor.startup.test.ts +11 -9
- package/src/monitor.startup.ts +26 -16
- package/src/monitor.state.ts +20 -5
- package/src/monitor.synthetic-error.ts +18 -0
- package/src/monitor.test-mocks.ts +2 -2
- package/src/monitor.transport.ts +220 -60
- package/src/monitor.ts +15 -10
- package/src/monitor.webhook-e2e.test.ts +65 -7
- package/src/monitor.webhook-security.test.ts +122 -0
- package/src/monitor.webhook.test-helpers.ts +44 -26
- package/src/outbound-runtime-api.ts +1 -0
- package/src/outbound.test.ts +616 -37
- package/src/outbound.ts +623 -81
- package/src/perm-schema.ts +1 -1
- package/src/perm.ts +1 -7
- package/src/pins.ts +108 -0
- package/src/policy.test.ts +297 -117
- package/src/policy.ts +142 -29
- package/src/post.ts +7 -6
- package/src/probe.test.ts +14 -9
- package/src/probe.ts +26 -16
- package/src/processing-claims.ts +59 -0
- package/src/qr-terminal.ts +1 -0
- package/src/reactions.ts +4 -34
- package/src/reasoning-preview.test.ts +59 -0
- package/src/reasoning-preview.ts +20 -0
- package/src/reply-dispatcher-runtime-api.ts +7 -0
- package/src/reply-dispatcher.test.ts +660 -29
- package/src/reply-dispatcher.ts +407 -154
- package/src/runtime.ts +6 -3
- package/src/secret-contract.ts +145 -0
- package/src/secret-input.ts +1 -13
- package/src/security-audit-shared.ts +69 -0
- package/src/security-audit.test.ts +61 -0
- package/src/security-audit.ts +1 -0
- package/src/send-result.ts +1 -1
- package/src/send-target.test.ts +9 -3
- package/src/send-target.ts +10 -4
- package/src/send.reply-fallback.test.ts +105 -2
- package/src/send.test.ts +386 -4
- package/src/send.ts +414 -95
- package/src/sequential-key.test.ts +72 -0
- package/src/sequential-key.ts +28 -0
- package/src/sequential-queue.test.ts +92 -0
- package/src/sequential-queue.ts +16 -0
- package/src/session-conversation.ts +42 -0
- package/src/session-route.ts +48 -0
- package/src/setup-core.ts +51 -0
- package/src/{onboarding.test.ts → setup-surface.test.ts} +52 -21
- package/src/setup-surface.ts +581 -0
- package/src/streaming-card.test.ts +138 -2
- package/src/streaming-card.ts +134 -18
- package/src/subagent-hooks.test.ts +603 -0
- package/src/subagent-hooks.ts +397 -0
- package/src/targets.ts +3 -13
- package/src/test-support/lifecycle-test-support.ts +453 -0
- package/src/thread-bindings.test.ts +143 -0
- package/src/thread-bindings.ts +330 -0
- package/src/tool-account-routing.test.ts +66 -8
- package/src/tool-account.test.ts +44 -0
- package/src/tool-account.ts +40 -17
- package/src/tool-factory-test-harness.ts +11 -8
- package/src/tool-result.ts +3 -1
- package/src/tools-config.ts +1 -1
- package/src/types.ts +16 -15
- package/src/typing.ts +10 -6
- package/src/wiki-schema.ts +1 -1
- package/src/wiki.ts +1 -7
- package/subagent-hooks-api.ts +31 -0
- package/tsconfig.json +16 -0
- package/src/feishu-command-handler.ts +0 -59
- package/src/onboarding.status.test.ts +0 -25
- package/src/onboarding.ts +0 -489
- package/src/send-message.ts +0 -71
- package/src/targets.test.ts +0 -70
|
@@ -23,7 +23,7 @@ import { monitorFeishuProvider, stopFeishuMonitor } from "./monitor.js";
|
|
|
23
23
|
|
|
24
24
|
function signFeishuPayload(params: {
|
|
25
25
|
encryptKey: string;
|
|
26
|
-
|
|
26
|
+
rawBody: string;
|
|
27
27
|
timestamp?: string;
|
|
28
28
|
nonce?: string;
|
|
29
29
|
}): Record<string, string> {
|
|
@@ -31,7 +31,7 @@ function signFeishuPayload(params: {
|
|
|
31
31
|
const nonce = params.nonce ?? "nonce-test";
|
|
32
32
|
const signature = crypto
|
|
33
33
|
.createHash("sha256")
|
|
34
|
-
.update(timestamp + nonce + params.encryptKey +
|
|
34
|
+
.update(timestamp + nonce + params.encryptKey + params.rawBody)
|
|
35
35
|
.digest("hex");
|
|
36
36
|
return {
|
|
37
37
|
"content-type": "application/json",
|
|
@@ -51,10 +51,11 @@ function encryptFeishuPayload(encryptKey: string, payload: Record<string, unknow
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
async function postSignedPayload(url: string, payload: Record<string, unknown>) {
|
|
54
|
+
const rawBody = JSON.stringify(payload);
|
|
54
55
|
return await fetch(url, {
|
|
55
56
|
method: "POST",
|
|
56
|
-
headers: signFeishuPayload({ encryptKey: "encrypt_key",
|
|
57
|
-
body:
|
|
57
|
+
headers: signFeishuPayload({ encryptKey: "encrypt_key", rawBody }),
|
|
58
|
+
body: rawBody,
|
|
58
59
|
});
|
|
59
60
|
}
|
|
60
61
|
|
|
@@ -76,12 +77,13 @@ describe("Feishu webhook signed-request e2e", () => {
|
|
|
76
77
|
monitorFeishuProvider,
|
|
77
78
|
async (url) => {
|
|
78
79
|
const payload = { type: "url_verification", challenge: "challenge-token" };
|
|
80
|
+
const rawBody = JSON.stringify(payload);
|
|
79
81
|
const response = await fetch(url, {
|
|
80
82
|
method: "POST",
|
|
81
83
|
headers: {
|
|
82
|
-
...signFeishuPayload({ encryptKey: "wrong_key",
|
|
84
|
+
...signFeishuPayload({ encryptKey: "wrong_key", rawBody }),
|
|
83
85
|
},
|
|
84
|
-
body:
|
|
86
|
+
body: rawBody,
|
|
85
87
|
});
|
|
86
88
|
|
|
87
89
|
expect(response.status).toBe(401);
|
|
@@ -114,7 +116,38 @@ describe("Feishu webhook signed-request e2e", () => {
|
|
|
114
116
|
);
|
|
115
117
|
});
|
|
116
118
|
|
|
117
|
-
it("
|
|
119
|
+
it("rejects malformed short signatures with 401", async () => {
|
|
120
|
+
probeFeishuMock.mockResolvedValue({ ok: true, botOpenId: "bot_open_id" });
|
|
121
|
+
|
|
122
|
+
await withRunningWebhookMonitor(
|
|
123
|
+
{
|
|
124
|
+
accountId: "short-signature",
|
|
125
|
+
path: "/hook-e2e-short-signature",
|
|
126
|
+
verificationToken: "verify_token",
|
|
127
|
+
encryptKey: "encrypt_key",
|
|
128
|
+
},
|
|
129
|
+
monitorFeishuProvider,
|
|
130
|
+
async (url) => {
|
|
131
|
+
const payload = { type: "url_verification", challenge: "challenge-token" };
|
|
132
|
+
const headers = signFeishuPayload({
|
|
133
|
+
encryptKey: "encrypt_key",
|
|
134
|
+
rawBody: JSON.stringify(payload),
|
|
135
|
+
});
|
|
136
|
+
headers["x-lark-signature"] = headers["x-lark-signature"].slice(0, 12);
|
|
137
|
+
|
|
138
|
+
const response = await fetch(url, {
|
|
139
|
+
method: "POST",
|
|
140
|
+
headers,
|
|
141
|
+
body: JSON.stringify(payload),
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
expect(response.status).toBe(401);
|
|
145
|
+
expect(await response.text()).toBe("Invalid signature");
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("returns 401 for unsigned invalid json before parsing", async () => {
|
|
118
151
|
probeFeishuMock.mockResolvedValue({ ok: true, botOpenId: "bot_open_id" });
|
|
119
152
|
|
|
120
153
|
await withRunningWebhookMonitor(
|
|
@@ -132,6 +165,31 @@ describe("Feishu webhook signed-request e2e", () => {
|
|
|
132
165
|
body: "{not-json",
|
|
133
166
|
});
|
|
134
167
|
|
|
168
|
+
expect(response.status).toBe(401);
|
|
169
|
+
expect(await response.text()).toBe("Invalid signature");
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("returns 400 for signed invalid json after signature validation", async () => {
|
|
175
|
+
probeFeishuMock.mockResolvedValue({ ok: true, botOpenId: "bot_open_id" });
|
|
176
|
+
|
|
177
|
+
await withRunningWebhookMonitor(
|
|
178
|
+
{
|
|
179
|
+
accountId: "signed-invalid-json",
|
|
180
|
+
path: "/hook-e2e-signed-invalid-json",
|
|
181
|
+
verificationToken: "verify_token",
|
|
182
|
+
encryptKey: "encrypt_key",
|
|
183
|
+
},
|
|
184
|
+
monitorFeishuProvider,
|
|
185
|
+
async (url) => {
|
|
186
|
+
const rawBody = "{not-json";
|
|
187
|
+
const response = await fetch(url, {
|
|
188
|
+
method: "POST",
|
|
189
|
+
headers: signFeishuPayload({ encryptKey: "encrypt_key", rawBody }),
|
|
190
|
+
body: rawBody,
|
|
191
|
+
});
|
|
192
|
+
|
|
135
193
|
expect(response.status).toBe(400);
|
|
136
194
|
expect(await response.text()).toBe("Invalid JSON");
|
|
137
195
|
},
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createConnection } from "node:net";
|
|
1
2
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
3
|
import {
|
|
3
4
|
createFeishuClientMockModule,
|
|
@@ -27,6 +28,15 @@ vi.mock("@larksuiteoapi/node-sdk", () => ({
|
|
|
27
28
|
),
|
|
28
29
|
}));
|
|
29
30
|
|
|
31
|
+
vi.mock("./monitor.state.js", async (importOriginal) => {
|
|
32
|
+
const actual = await importOriginal<typeof import("./monitor.state.js")>();
|
|
33
|
+
return {
|
|
34
|
+
...actual,
|
|
35
|
+
FEISHU_WEBHOOK_BODY_TIMEOUT_MS: 50,
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
import type { RuntimeEnv } from "../runtime-api.js";
|
|
30
40
|
import {
|
|
31
41
|
clearFeishuWebhookRateLimitStateForTest,
|
|
32
42
|
getFeishuWebhookRateLimitStateSizeForTest,
|
|
@@ -34,6 +44,49 @@ import {
|
|
|
34
44
|
monitorFeishuProvider,
|
|
35
45
|
stopFeishuMonitor,
|
|
36
46
|
} from "./monitor.js";
|
|
47
|
+
import { monitorWebhook } from "./monitor.transport.js";
|
|
48
|
+
import type { ResolvedFeishuAccount } from "./types.js";
|
|
49
|
+
|
|
50
|
+
async function waitForSlowBodyTimeoutResponse(
|
|
51
|
+
url: string,
|
|
52
|
+
timeoutMs: number,
|
|
53
|
+
): Promise<{ body: string; elapsedMs: number }> {
|
|
54
|
+
return await new Promise<{ body: string; elapsedMs: number }>((resolve, reject) => {
|
|
55
|
+
const target = new URL(url);
|
|
56
|
+
const startedAt = Date.now();
|
|
57
|
+
let response = "";
|
|
58
|
+
const socket = createConnection(
|
|
59
|
+
{
|
|
60
|
+
host: target.hostname,
|
|
61
|
+
port: Number(target.port),
|
|
62
|
+
},
|
|
63
|
+
() => {
|
|
64
|
+
socket.write(`POST ${target.pathname} HTTP/1.1\r\n`);
|
|
65
|
+
socket.write(`Host: ${target.hostname}\r\n`);
|
|
66
|
+
socket.write("Content-Type: application/json\r\n");
|
|
67
|
+
socket.write("Content-Length: 65536\r\n");
|
|
68
|
+
socket.write("\r\n");
|
|
69
|
+
socket.write('{"type":"url_verification"');
|
|
70
|
+
},
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
socket.setEncoding("utf8");
|
|
74
|
+
socket.on("error", () => {});
|
|
75
|
+
socket.on("data", (chunk) => {
|
|
76
|
+
response += chunk;
|
|
77
|
+
if (response.includes("Request body timeout")) {
|
|
78
|
+
clearTimeout(failTimer);
|
|
79
|
+
socket.destroy();
|
|
80
|
+
resolve({ body: response, elapsedMs: Date.now() - startedAt });
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const failTimer = setTimeout(() => {
|
|
85
|
+
socket.destroy();
|
|
86
|
+
reject(new Error(`timeout response did not arrive within ${timeoutMs}ms`));
|
|
87
|
+
}, timeoutMs);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
37
90
|
|
|
38
91
|
afterEach(() => {
|
|
39
92
|
clearFeishuWebhookRateLimitStateForTest();
|
|
@@ -68,6 +121,33 @@ describe("Feishu webhook security hardening", () => {
|
|
|
68
121
|
await expect(monitorFeishuProvider({ config: cfg })).rejects.toThrow(/requires encryptKey/i);
|
|
69
122
|
});
|
|
70
123
|
|
|
124
|
+
it("refuses to start the webhook transport without encryptKey", async () => {
|
|
125
|
+
const account = {
|
|
126
|
+
accountId: "transport-missing-encrypt-key",
|
|
127
|
+
config: {
|
|
128
|
+
enabled: true,
|
|
129
|
+
connectionMode: "webhook",
|
|
130
|
+
webhookHost: "127.0.0.1",
|
|
131
|
+
webhookPort: await getFreePort(),
|
|
132
|
+
webhookPath: "/hook-transport-missing-encrypt",
|
|
133
|
+
},
|
|
134
|
+
} as ResolvedFeishuAccount;
|
|
135
|
+
|
|
136
|
+
await expect(
|
|
137
|
+
monitorWebhook({
|
|
138
|
+
account,
|
|
139
|
+
accountId: account.accountId,
|
|
140
|
+
runtime: {
|
|
141
|
+
log: vi.fn(),
|
|
142
|
+
error: vi.fn(),
|
|
143
|
+
exit: vi.fn(),
|
|
144
|
+
} as RuntimeEnv,
|
|
145
|
+
abortSignal: new AbortController().signal,
|
|
146
|
+
eventDispatcher: {} as never,
|
|
147
|
+
}),
|
|
148
|
+
).rejects.toThrow(/requires encryptKey/i);
|
|
149
|
+
});
|
|
150
|
+
|
|
71
151
|
it("returns 415 for POST requests without json content type", async () => {
|
|
72
152
|
probeFeishuMock.mockResolvedValue({ ok: true, botOpenId: "bot_open_id" });
|
|
73
153
|
await withRunningWebhookMonitor(
|
|
@@ -91,6 +171,48 @@ describe("Feishu webhook security hardening", () => {
|
|
|
91
171
|
);
|
|
92
172
|
});
|
|
93
173
|
|
|
174
|
+
it("rejects oversized unsigned webhook bodies with 413 before signature verification", async () => {
|
|
175
|
+
probeFeishuMock.mockResolvedValue({ ok: true, botOpenId: "bot_open_id" });
|
|
176
|
+
await withRunningWebhookMonitor(
|
|
177
|
+
{
|
|
178
|
+
accountId: "payload-too-large",
|
|
179
|
+
path: "/hook-payload-too-large",
|
|
180
|
+
verificationToken: "verify_token",
|
|
181
|
+
encryptKey: "encrypt_key",
|
|
182
|
+
},
|
|
183
|
+
monitorFeishuProvider,
|
|
184
|
+
async (url) => {
|
|
185
|
+
const response = await fetch(url, {
|
|
186
|
+
method: "POST",
|
|
187
|
+
headers: { "content-type": "application/json" },
|
|
188
|
+
body: JSON.stringify({ payload: "x".repeat(70 * 1024) }),
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
expect(response.status).toBe(413);
|
|
192
|
+
expect(await response.text()).toBe("Payload too large");
|
|
193
|
+
},
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("drops slow-body webhook requests within the tightened pre-auth timeout", async () => {
|
|
198
|
+
probeFeishuMock.mockResolvedValue({ ok: true, botOpenId: "bot_open_id" });
|
|
199
|
+
await withRunningWebhookMonitor(
|
|
200
|
+
{
|
|
201
|
+
accountId: "slow-body-timeout",
|
|
202
|
+
path: "/hook-slow-body-timeout",
|
|
203
|
+
verificationToken: "verify_token",
|
|
204
|
+
encryptKey: "encrypt_key",
|
|
205
|
+
},
|
|
206
|
+
monitorFeishuProvider,
|
|
207
|
+
async (url) => {
|
|
208
|
+
const result = await waitForSlowBodyTimeoutResponse(url, 1_000);
|
|
209
|
+
expect(result.body).toContain("408 Request Timeout");
|
|
210
|
+
expect(result.body).toContain("Request body timeout");
|
|
211
|
+
expect(result.elapsedMs).toBeLessThan(500);
|
|
212
|
+
},
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
94
216
|
it("rate limits webhook burst traffic with 429", async () => {
|
|
95
217
|
probeFeishuMock.mockResolvedValue({ ok: true, botOpenId: "bot_open_id" });
|
|
96
218
|
await withRunningWebhookMonitor(
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { createServer } from "node:http";
|
|
2
2
|
import type { AddressInfo } from "node:net";
|
|
3
|
-
import type { ClawdbotConfig } from "openclaw/plugin-sdk/feishu";
|
|
4
3
|
import { vi } from "vitest";
|
|
4
|
+
import type { ClawdbotConfig } from "../runtime-api.js";
|
|
5
5
|
import type { monitorFeishuProvider } from "./monitor.js";
|
|
6
6
|
|
|
7
|
+
const WEBHOOK_READY_MAX_ATTEMPTS = 200;
|
|
8
|
+
const WEBHOOK_READY_RETRY_DELAY_MS = 50;
|
|
9
|
+
const WEBHOOK_MONITOR_START_MAX_ATTEMPTS = 4;
|
|
10
|
+
|
|
7
11
|
export async function getFreePort(): Promise<number> {
|
|
8
12
|
const server = createServer();
|
|
9
13
|
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", () => resolve()));
|
|
@@ -16,7 +20,7 @@ export async function getFreePort(): Promise<number> {
|
|
|
16
20
|
}
|
|
17
21
|
|
|
18
22
|
async function waitUntilServerReady(url: string): Promise<void> {
|
|
19
|
-
for (let i = 0; i <
|
|
23
|
+
for (let i = 0; i < WEBHOOK_READY_MAX_ATTEMPTS; i += 1) {
|
|
20
24
|
try {
|
|
21
25
|
const response = await fetch(url, { method: "GET" });
|
|
22
26
|
if (response.status >= 200 && response.status < 500) {
|
|
@@ -25,7 +29,7 @@ async function waitUntilServerReady(url: string): Promise<void> {
|
|
|
25
29
|
} catch {
|
|
26
30
|
// retry
|
|
27
31
|
}
|
|
28
|
-
await new Promise((resolve) => setTimeout(resolve,
|
|
32
|
+
await new Promise((resolve) => setTimeout(resolve, WEBHOOK_READY_RETRY_DELAY_MS));
|
|
29
33
|
}
|
|
30
34
|
throw new Error(`server did not start: ${url}`);
|
|
31
35
|
}
|
|
@@ -69,30 +73,44 @@ export async function withRunningWebhookMonitor(
|
|
|
69
73
|
monitor: typeof monitorFeishuProvider,
|
|
70
74
|
run: (url: string) => Promise<void>,
|
|
71
75
|
) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
const runtime = { log: vi.fn(), error: vi.fn(), exit: vi.fn() };
|
|
83
|
-
const monitorPromise = monitor({
|
|
84
|
-
config: cfg,
|
|
85
|
-
runtime,
|
|
86
|
-
abortSignal: abortController.signal,
|
|
87
|
-
});
|
|
76
|
+
let startupError: unknown;
|
|
77
|
+
for (let attempt = 1; attempt <= WEBHOOK_MONITOR_START_MAX_ATTEMPTS; attempt += 1) {
|
|
78
|
+
const port = await getFreePort();
|
|
79
|
+
const cfg = buildWebhookConfig({
|
|
80
|
+
accountId: params.accountId,
|
|
81
|
+
path: params.path,
|
|
82
|
+
port,
|
|
83
|
+
encryptKey: params.encryptKey,
|
|
84
|
+
verificationToken: params.verificationToken,
|
|
85
|
+
});
|
|
88
86
|
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
const abortController = new AbortController();
|
|
88
|
+
const runtime = { log: vi.fn(), error: vi.fn(), exit: vi.fn() };
|
|
89
|
+
const monitorPromise = monitor({
|
|
90
|
+
config: cfg,
|
|
91
|
+
runtime,
|
|
92
|
+
abortSignal: abortController.signal,
|
|
93
|
+
accountId: params.accountId,
|
|
94
|
+
});
|
|
91
95
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
96
|
+
const url = `http://127.0.0.1:${port}${params.path}`;
|
|
97
|
+
try {
|
|
98
|
+
await waitUntilServerReady(url);
|
|
99
|
+
try {
|
|
100
|
+
await run(url);
|
|
101
|
+
} finally {
|
|
102
|
+
abortController.abort();
|
|
103
|
+
await monitorPromise.catch(() => undefined);
|
|
104
|
+
}
|
|
105
|
+
return;
|
|
106
|
+
} catch (error) {
|
|
107
|
+
startupError = error;
|
|
108
|
+
abortController.abort();
|
|
109
|
+
await monitorPromise.catch(() => undefined);
|
|
110
|
+
if (attempt < WEBHOOK_MONITOR_START_MAX_ATTEMPTS) {
|
|
111
|
+
await new Promise((resolve) => setTimeout(resolve, attempt * WEBHOOK_READY_RETRY_DELAY_MS));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
97
114
|
}
|
|
115
|
+
throw startupError instanceof Error ? startupError : new Error("failed to start webhook monitor");
|
|
98
116
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { chunkTextForOutbound, type ChannelOutboundAdapter } from "../runtime-api.js";
|