@yanhaidao/wecom 2.4.160 → 2.5.110
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/dist/index.js +68 -0
- package/dist/src/accounts.js +20 -0
- package/dist/src/agent/handler.js +895 -0
- package/dist/src/agent/index.js +5 -0
- package/dist/src/app/account-runtime.js +216 -0
- package/dist/src/app/bootstrap.js +19 -0
- package/dist/src/app/index.js +118 -0
- package/dist/src/capability/agent/delivery-service.js +63 -0
- package/dist/src/capability/agent/fallback-policy.js +6 -0
- package/dist/src/capability/agent/ingress-service.js +33 -0
- package/dist/src/capability/agent/upstream-delivery-service.js +71 -0
- package/dist/src/capability/bot/dispatch-config.js +45 -0
- package/dist/src/capability/bot/fallback-delivery.js +147 -0
- package/dist/src/capability/bot/local-path-delivery.js +178 -0
- package/dist/src/capability/bot/sandbox-media.js +138 -0
- package/dist/src/capability/bot/service.js +49 -0
- package/dist/src/capability/bot/stream-delivery.js +321 -0
- package/dist/src/capability/bot/stream-finalizer.js +81 -0
- package/dist/src/capability/bot/stream-orchestrator.js +318 -0
- package/dist/src/capability/bot/types.js +1 -0
- package/{src/capability/calendar/client.ts → dist/src/capability/calendar/client.js} +118 -241
- package/{src/capability/calendar/schema.ts → dist/src/capability/calendar/schema.js} +0 -38
- package/dist/src/capability/calendar/tool.js +365 -0
- package/dist/src/capability/calendar/types.js +12 -0
- package/{src/capability/doc/client.ts → dist/src/capability/doc/client.js} +370 -605
- package/{src/capability/doc/schema.ts → dist/src/capability/doc/schema.js} +345 -394
- package/dist/src/capability/doc/tool.js +1556 -0
- package/dist/src/capability/doc/types.js +113 -0
- package/dist/src/capability/mcp/index.js +3 -0
- package/dist/src/capability/mcp/schema.js +102 -0
- package/dist/src/capability/mcp/tool.js +146 -0
- package/dist/src/capability/mcp/transport.js +293 -0
- package/dist/src/channel.js +224 -0
- package/dist/src/config/accounts.js +236 -0
- package/dist/src/config/derived-paths.js +31 -0
- package/dist/src/config/index.js +7 -0
- package/dist/src/config/media.js +110 -0
- package/dist/src/config/network.js +32 -0
- package/dist/src/config/routing.js +20 -0
- package/dist/src/config/runtime-config.js +25 -0
- package/dist/src/config/schema.js +4 -0
- package/{src/config-schema.ts → dist/src/config-schema.js} +1 -1
- package/dist/src/context-store.js +219 -0
- package/{src/crypto/aes.ts → dist/src/crypto/aes.js} +11 -28
- package/dist/src/crypto/index.js +9 -0
- package/{src/crypto/signature.ts → dist/src/crypto/signature.js} +3 -18
- package/{src/crypto/xml.ts → dist/src/crypto/xml.js} +3 -11
- package/dist/src/crypto.js +145 -0
- package/dist/src/domain/models.js +1 -0
- package/dist/src/domain/policies.js +32 -0
- package/{src/dynamic-agent.ts → dist/src/dynamic-agent.js} +36 -73
- package/dist/src/gateway-monitor.js +139 -0
- package/dist/src/http.js +114 -0
- package/{src/media.ts → dist/src/media.js} +21 -40
- package/dist/src/monitor/limits.js +7 -0
- package/dist/src/monitor/state.js +28 -0
- package/dist/src/monitor.js +84 -0
- package/dist/src/observability/audit-log.js +30 -0
- package/dist/src/observability/legacy-operational-event-store.js +22 -0
- package/dist/src/observability/raw-envelope-log.js +24 -0
- package/dist/src/observability/status-registry.js +9 -0
- package/dist/src/observability/transport-session-view.js +14 -0
- package/dist/src/onboarding.js +546 -0
- package/dist/src/outbound.js +557 -0
- package/dist/src/runtime/dispatcher.js +57 -0
- package/{src/runtime/index.ts → dist/src/runtime/index.js} +0 -1
- package/dist/src/runtime/outbound-intent.js +1 -0
- package/dist/src/runtime/reply-orchestrator.js +38 -0
- package/dist/src/runtime/routing-bridge.js +26 -0
- package/dist/src/runtime/session-manager.js +112 -0
- package/dist/src/runtime/source-registry.js +174 -0
- package/dist/src/runtime.js +1 -0
- package/dist/src/shared/command-auth.js +57 -0
- package/{src/shared/index.ts → dist/src/shared/index.js} +0 -1
- package/dist/src/shared/media-asset.js +65 -0
- package/dist/src/shared/media-service.js +59 -0
- package/dist/src/shared/media-types.js +1 -0
- package/{src/shared/xml-parser.ts → dist/src/shared/xml-parser.js} +72 -63
- package/dist/src/store/active-reply-store.js +41 -0
- package/dist/src/store/interfaces.js +1 -0
- package/dist/src/store/memory-store.js +33 -0
- package/dist/src/store/stream-batch-store.js +319 -0
- package/{src/target.ts → dist/src/target.js} +15 -48
- package/dist/src/transport/agent-api/client.js +168 -0
- package/dist/src/transport/agent-api/core.js +337 -0
- package/dist/src/transport/agent-api/delivery.js +28 -0
- package/dist/src/transport/agent-api/media-upload.js +4 -0
- package/dist/src/transport/agent-api/reply.js +24 -0
- package/dist/src/transport/agent-api/upstream-delivery.js +30 -0
- package/dist/src/transport/agent-api/upstream-media-upload.js +46 -0
- package/dist/src/transport/agent-api/upstream-reply.js +26 -0
- package/dist/src/transport/agent-callback/http-handler.js +30 -0
- package/dist/src/transport/agent-callback/inbound.js +4 -0
- package/dist/src/transport/agent-callback/reply.js +8 -0
- package/dist/src/transport/agent-callback/request-handler.js +189 -0
- package/dist/src/transport/agent-callback/session.js +15 -0
- package/dist/src/transport/bot-webhook/active-reply.js +27 -0
- package/dist/src/transport/bot-webhook/http-handler.js +31 -0
- package/dist/src/transport/bot-webhook/inbound-normalizer.js +496 -0
- package/dist/src/transport/bot-webhook/inbound.js +4 -0
- package/dist/src/transport/bot-webhook/message-shape.js +98 -0
- package/dist/src/transport/bot-webhook/protocol.js +124 -0
- package/dist/src/transport/bot-webhook/reply.js +9 -0
- package/dist/src/transport/bot-webhook/request-handler.js +285 -0
- package/dist/src/transport/bot-webhook/session.js +15 -0
- package/dist/src/transport/bot-ws/inbound.js +147 -0
- package/dist/src/transport/bot-ws/media.js +236 -0
- package/dist/src/transport/bot-ws/reply.js +310 -0
- package/dist/src/transport/bot-ws/sdk-adapter.js +257 -0
- package/dist/src/transport/bot-ws/session.js +15 -0
- package/dist/src/transport/http/common.js +78 -0
- package/dist/src/transport/http/registry.js +71 -0
- package/dist/src/transport/http/request-handler.js +51 -0
- package/{src/transport/index.ts → dist/src/transport/index.js} +2 -10
- package/dist/src/types/account.js +1 -0
- package/dist/src/types/config.js +1 -0
- package/dist/src/types/constants.js +28 -0
- package/dist/src/types/events.js +1 -0
- package/dist/src/types/index.js +1 -0
- package/dist/src/types/legacy-stream.js +1 -0
- package/dist/src/types/message.js +5 -0
- package/dist/src/types/runtime-context.js +1 -0
- package/dist/src/types/runtime.js +1 -0
- package/dist/src/types.js +1 -0
- package/dist/src/upstream/index.js +111 -0
- package/dist/src/wecom_msg_adapter/markdown_adapter.js +280 -0
- package/openclaw.plugin.json +15 -0
- package/package.json +18 -1
- package/.github/workflows/release.yml +0 -143
- package/GOVERNANCE.md +0 -26
- package/SKILLS_CAL.md +0 -895
- package/SKILLS_DOC.md +0 -2288
- package/UPSTREAM_CONFIG.md +0 -170
- package/UPSTREAM_PLAN.md +0 -175
- package/assets/01.bot-add.png +0 -0
- package/assets/01.bot-setp2.png +0 -0
- package/assets/01.image.jpg +0 -0
- package/assets/02.agent.add.png +0 -0
- package/assets/02.agent.api-set.png +0 -0
- package/assets/02.image.jpg +0 -0
- package/assets/03.agent.page.png +0 -0
- package/assets/03.bot.page.png +0 -0
- package/assets/link-me.jpg +0 -0
- package/assets/register.png +0 -0
- package/changelog/v2.2.28.md +0 -70
- package/changelog/v2.3.10.md +0 -17
- package/changelog/v2.3.11.md +0 -19
- package/changelog/v2.3.12.md +0 -25
- package/changelog/v2.3.13.md +0 -19
- package/changelog/v2.3.14.md +0 -48
- package/changelog/v2.3.15.md +0 -15
- package/changelog/v2.3.16.md +0 -11
- package/changelog/v2.3.18.md +0 -22
- package/changelog/v2.3.19.md +0 -73
- package/changelog/v2.3.2.md +0 -28
- package/changelog/v2.3.26.md +0 -21
- package/changelog/v2.3.27.md +0 -33
- package/changelog/v2.3.4.md +0 -20
- package/changelog/v2.3.9.md +0 -22
- package/changelog/v2.4.12.md +0 -37
- package/changelog/v2.4.16.md +0 -19
- package/compat-single-account.md +0 -148
- package/index.test.ts +0 -38
- package/scripts/test-proxy.ts +0 -70
- package/src/accounts.ts +0 -34
- package/src/agent/api-client.upload.test.ts +0 -109
- package/src/agent/handler.event-filter.test.ts +0 -100
- package/src/agent/handler.ts +0 -1105
- package/src/agent/index.ts +0 -12
- package/src/app/account-runtime.ts +0 -276
- package/src/app/bootstrap.ts +0 -29
- package/src/app/index.ts +0 -192
- package/src/capability/agent/delivery-service.ts +0 -87
- package/src/capability/agent/fallback-policy.ts +0 -13
- package/src/capability/agent/ingress-service.ts +0 -38
- package/src/capability/agent/upstream-delivery-service.ts +0 -96
- package/src/capability/bot/dispatch-config.ts +0 -47
- package/src/capability/bot/fallback-delivery.ts +0 -178
- package/src/capability/bot/local-path-delivery.ts +0 -215
- package/src/capability/bot/sandbox-media.test.ts +0 -221
- package/src/capability/bot/sandbox-media.ts +0 -176
- package/src/capability/bot/service.ts +0 -56
- package/src/capability/bot/stream-delivery.ts +0 -379
- package/src/capability/bot/stream-finalizer.ts +0 -120
- package/src/capability/bot/stream-orchestrator.ts +0 -371
- package/src/capability/bot/types.ts +0 -8
- package/src/capability/calendar/SKILLS_CHECKLIST.md +0 -251
- package/src/capability/calendar/tool.ts +0 -417
- package/src/capability/calendar/types.ts +0 -309
- package/src/capability/doc/tool.ts +0 -1629
- package/src/capability/doc/types.ts +0 -792
- package/src/capability/mcp/index.ts +0 -10
- package/src/capability/mcp/schema.ts +0 -107
- package/src/capability/mcp/tool.ts +0 -174
- package/src/capability/mcp/transport.ts +0 -394
- package/src/channel.config.test.ts +0 -147
- package/src/channel.lifecycle.test.ts +0 -255
- package/src/channel.meta.test.ts +0 -26
- package/src/channel.ts +0 -256
- package/src/config/accounts.resolve.test.ts +0 -75
- package/src/config/accounts.ts +0 -296
- package/src/config/derived-paths.test.ts +0 -111
- package/src/config/derived-paths.ts +0 -41
- package/src/config/index.ts +0 -26
- package/src/config/media.test.ts +0 -113
- package/src/config/media.ts +0 -139
- package/src/config/network.ts +0 -53
- package/src/config/routing.test.ts +0 -88
- package/src/config/routing.ts +0 -26
- package/src/config/runtime-config.ts +0 -46
- package/src/config/schema.ts +0 -90
- package/src/context-store.ts +0 -297
- package/src/crypto/index.ts +0 -24
- package/src/crypto.test.ts +0 -32
- package/src/crypto.ts +0 -176
- package/src/domain/models.ts +0 -7
- package/src/domain/policies.ts +0 -36
- package/src/dynamic-agent.account-scope.test.ts +0 -17
- package/src/gateway-monitor.ts +0 -181
- package/src/http.ts +0 -145
- package/src/media.test.ts +0 -82
- package/src/monitor/limits.ts +0 -7
- package/src/monitor/state.queue.test.ts +0 -185
- package/src/monitor/state.ts +0 -34
- package/src/monitor.active.test.ts +0 -245
- package/src/monitor.inbound-filter.test.ts +0 -63
- package/src/monitor.integration.test.ts +0 -208
- package/src/monitor.ts +0 -121
- package/src/monitor.webhook.test.ts +0 -774
- package/src/observability/audit-log.ts +0 -48
- package/src/observability/legacy-operational-event-store.ts +0 -36
- package/src/observability/raw-envelope-log.ts +0 -28
- package/src/observability/status-registry.ts +0 -13
- package/src/observability/transport-session-view.ts +0 -14
- package/src/onboarding.test.ts +0 -336
- package/src/onboarding.ts +0 -704
- package/src/outbound.test.ts +0 -1271
- package/src/outbound.ts +0 -746
- package/src/runtime/dispatcher.ts +0 -71
- package/src/runtime/outbound-intent.ts +0 -4
- package/src/runtime/reply-orchestrator.test.ts +0 -71
- package/src/runtime/reply-orchestrator.ts +0 -67
- package/src/runtime/routing-bridge.test.ts +0 -115
- package/src/runtime/routing-bridge.ts +0 -44
- package/src/runtime/session-manager.test.ts +0 -174
- package/src/runtime/session-manager.ts +0 -139
- package/src/runtime/source-registry.ts +0 -249
- package/src/runtime.ts +0 -14
- package/src/shared/command-auth.ts +0 -87
- package/src/shared/media-asset.ts +0 -78
- package/src/shared/media-service.test.ts +0 -111
- package/src/shared/media-service.ts +0 -84
- package/src/shared/media-types.ts +0 -5
- package/src/shared/xml-parser.test.ts +0 -50
- package/src/store/active-reply-store.ts +0 -42
- package/src/store/interfaces.ts +0 -11
- package/src/store/memory-store.ts +0 -43
- package/src/store/stream-batch-store.ts +0 -350
- package/src/transport/agent-api/client.ts +0 -277
- package/src/transport/agent-api/core.ts +0 -463
- package/src/transport/agent-api/delivery.ts +0 -41
- package/src/transport/agent-api/media-upload.ts +0 -11
- package/src/transport/agent-api/reply.ts +0 -39
- package/src/transport/agent-api/upstream-delivery.ts +0 -45
- package/src/transport/agent-api/upstream-media-upload.ts +0 -70
- package/src/transport/agent-api/upstream-reply.ts +0 -43
- package/src/transport/agent-callback/http-handler.ts +0 -47
- package/src/transport/agent-callback/inbound.ts +0 -5
- package/src/transport/agent-callback/reply.ts +0 -13
- package/src/transport/agent-callback/request-handler.ts +0 -244
- package/src/transport/agent-callback/session.ts +0 -23
- package/src/transport/bot-webhook/active-reply.ts +0 -39
- package/src/transport/bot-webhook/http-handler.ts +0 -48
- package/src/transport/bot-webhook/inbound-normalizer.test.ts +0 -433
- package/src/transport/bot-webhook/inbound-normalizer.ts +0 -558
- package/src/transport/bot-webhook/inbound.ts +0 -5
- package/src/transport/bot-webhook/message-shape.ts +0 -92
- package/src/transport/bot-webhook/protocol.ts +0 -148
- package/src/transport/bot-webhook/reply.ts +0 -15
- package/src/transport/bot-webhook/request-handler.ts +0 -394
- package/src/transport/bot-webhook/session.ts +0 -23
- package/src/transport/bot-ws/inbound.test.ts +0 -290
- package/src/transport/bot-ws/inbound.ts +0 -163
- package/src/transport/bot-ws/media.test.ts +0 -44
- package/src/transport/bot-ws/media.ts +0 -321
- package/src/transport/bot-ws/reply.test.ts +0 -450
- package/src/transport/bot-ws/reply.ts +0 -365
- package/src/transport/bot-ws/sdk-adapter.test.ts +0 -187
- package/src/transport/bot-ws/sdk-adapter.ts +0 -314
- package/src/transport/bot-ws/session.ts +0 -28
- package/src/transport/http/common.ts +0 -109
- package/src/transport/http/registry.ts +0 -92
- package/src/transport/http/request-handler.ts +0 -84
- package/src/types/account.ts +0 -70
- package/src/types/config.ts +0 -114
- package/src/types/constants.ts +0 -31
- package/src/types/events.ts +0 -21
- package/src/types/global.d.ts +0 -9
- package/src/types/index.ts +0 -17
- package/src/types/legacy-stream.ts +0 -50
- package/src/types/message.ts +0 -189
- package/src/types/runtime-context.ts +0 -28
- package/src/types/runtime.ts +0 -165
- package/src/types.ts +0 -41
- package/src/upstream/index.ts +0 -150
- package/src/upstream.test.ts +0 -84
- package/src/wecom_msg_adapter/markdown_adapter.ts +0 -331
- package/tsconfig.json +0 -22
- package/vitest.config.ts +0 -26
- /package/{src/capability/agent/index.ts → dist/src/capability/agent/index.js} +0 -0
- /package/{src/capability/bot/index.ts → dist/src/capability/bot/index.js} +0 -0
- /package/{src/capability/calendar/index.ts → dist/src/capability/calendar/index.js} +0 -0
- /package/{src/capability/index.ts → dist/src/capability/index.js} +0 -0
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
-
import { sendActiveMessage, handleWecomWebhookRequest, registerWecomWebhookTarget } from "./monitor.js";
|
|
3
|
-
import * as cryptoHelpers from "./crypto.js";
|
|
4
|
-
import * as runtime from "./runtime.js";
|
|
5
|
-
import * as agentApi from "./transport/agent-api/core.js";
|
|
6
|
-
import { IncomingMessage, ServerResponse } from "node:http";
|
|
7
|
-
import { Socket } from "node:net";
|
|
8
|
-
import * as crypto from "node:crypto";
|
|
9
|
-
|
|
10
|
-
const { undiciFetch } = vi.hoisted(() => {
|
|
11
|
-
const undiciFetch = vi.fn();
|
|
12
|
-
return { undiciFetch };
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
vi.mock("undici", () => ({
|
|
16
|
-
fetch: undiciFetch,
|
|
17
|
-
ProxyAgent: class ProxyAgent { },
|
|
18
|
-
}));
|
|
19
|
-
|
|
20
|
-
vi.mock("./transport/agent-api/core.js", () => ({
|
|
21
|
-
sendText: vi.fn(),
|
|
22
|
-
sendMedia: vi.fn(),
|
|
23
|
-
uploadMedia: vi.fn(),
|
|
24
|
-
}));
|
|
25
|
-
|
|
26
|
-
// Helpers
|
|
27
|
-
function createMockRequest(bodyObj: any): IncomingMessage {
|
|
28
|
-
const socket = new Socket();
|
|
29
|
-
const req = new IncomingMessage(socket);
|
|
30
|
-
req.method = "POST";
|
|
31
|
-
req.url = "/plugins/wecom/bot/default?timestamp=123&nonce=456&signature=789";
|
|
32
|
-
req.push(JSON.stringify(bodyObj));
|
|
33
|
-
req.push(null);
|
|
34
|
-
return req;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function createMockResponse(): ServerResponse {
|
|
38
|
-
const req = new IncomingMessage(new Socket());
|
|
39
|
-
const res = new ServerResponse(req);
|
|
40
|
-
res.end = vi.fn() as any;
|
|
41
|
-
res.setHeader = vi.fn();
|
|
42
|
-
(res as any).statusCode = 200;
|
|
43
|
-
return res;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
describe("Monitor Active Features", () => {
|
|
47
|
-
let capturedDeliver: ((payload: { text: string }) => Promise<void>) | undefined;
|
|
48
|
-
let unregisterTarget: (() => void) | undefined;
|
|
49
|
-
let mockCore: any;
|
|
50
|
-
let msgSeq = 0;
|
|
51
|
-
let senderUserId = "";
|
|
52
|
-
let senderChatId = "";
|
|
53
|
-
// Valid 32-byte AES Key (Base64 encoded)
|
|
54
|
-
const validKey = "jWmYm7qr5nMoCAstdRmNjt3p7vsH8HkK+qiJqQ0aaaa=";
|
|
55
|
-
|
|
56
|
-
beforeEach(() => {
|
|
57
|
-
vi.useFakeTimers();
|
|
58
|
-
capturedDeliver = undefined;
|
|
59
|
-
vi.restoreAllMocks();
|
|
60
|
-
undiciFetch.mockClear();
|
|
61
|
-
msgSeq += 1;
|
|
62
|
-
senderUserId = `zhangsan-${msgSeq}`;
|
|
63
|
-
senderChatId = `wr123-${msgSeq}`;
|
|
64
|
-
|
|
65
|
-
// Spy on crypto.randomBytes (default export in monitor.ts usage)
|
|
66
|
-
vi.spyOn(crypto.default, "randomBytes").mockImplementation((size) => {
|
|
67
|
-
return Buffer.alloc(size, 0x11);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// Mock Crypto Helpers
|
|
71
|
-
// Wespy on verifyWecomSignature to always pass
|
|
72
|
-
vi.spyOn(cryptoHelpers, "verifyWecomSignature").mockReturnValue(true);
|
|
73
|
-
|
|
74
|
-
// We spy on decryptWecomEncrypted to return our mock plaintext
|
|
75
|
-
// Note: For this to work despite direct import in monitor.ts, we rely on Vitest's
|
|
76
|
-
// module mocking capabilities or the fact that * exports might be live bindings.
|
|
77
|
-
// If this fails, we will know.
|
|
78
|
-
vi.spyOn(cryptoHelpers, "decryptWecomEncrypted").mockImplementation((opts) => {
|
|
79
|
-
return JSON.stringify({
|
|
80
|
-
msgid: `test-msg-id-${msgSeq}`,
|
|
81
|
-
aibotid: "bot-1",
|
|
82
|
-
chattype: "group",
|
|
83
|
-
chatid: senderChatId,
|
|
84
|
-
from: { userid: senderUserId },
|
|
85
|
-
msgtype: "text",
|
|
86
|
-
text: { content: "hello" },
|
|
87
|
-
response_url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=test-key"
|
|
88
|
-
});
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
mockCore = {
|
|
92
|
-
channel: {
|
|
93
|
-
text: {
|
|
94
|
-
resolveMarkdownTableMode: () => "off",
|
|
95
|
-
convertMarkdownTables: (t: string) => t.replace(/\|/g, "-")
|
|
96
|
-
},
|
|
97
|
-
commands: {
|
|
98
|
-
shouldComputeCommandAuthorized: () => false,
|
|
99
|
-
resolveCommandAuthorizedFromAuthorizers: () => true,
|
|
100
|
-
},
|
|
101
|
-
pairing: {
|
|
102
|
-
readAllowFromStore: async () => [],
|
|
103
|
-
},
|
|
104
|
-
reply: {
|
|
105
|
-
finalizeInboundContext: (c: any) => c,
|
|
106
|
-
resolveEnvelopeFormatOptions: () => ({}),
|
|
107
|
-
formatAgentEnvelope: () => "",
|
|
108
|
-
dispatchReplyWithBufferedBlockDispatcher: async (opts: any) => {
|
|
109
|
-
capturedDeliver = opts.dispatcherOptions.deliver;
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
routing: { resolveAgentRoute: () => ({ agentId: "1", sessionKey: "1", accountId: "default" }) },
|
|
114
|
-
session: {
|
|
115
|
-
resolveStorePath: () => "",
|
|
116
|
-
readSessionUpdatedAt: () => 0,
|
|
117
|
-
recordInboundSession: vi.fn()
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
logging: { shouldLogVerbose: () => false }
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
vi.spyOn(runtime, "getWecomRuntime").mockReturnValue(mockCore);
|
|
124
|
-
|
|
125
|
-
unregisterTarget = registerWecomWebhookTarget({
|
|
126
|
-
account: { accountId: "default", configured: true, token: "T", encodingAESKey: validKey, receiveId: "R", config: {} as any },
|
|
127
|
-
config: {
|
|
128
|
-
channels: {
|
|
129
|
-
wecom: {
|
|
130
|
-
enabled: true,
|
|
131
|
-
agent: {
|
|
132
|
-
corpId: "corp",
|
|
133
|
-
corpSecret: "secret",
|
|
134
|
-
agentId: 1000002,
|
|
135
|
-
token: "token",
|
|
136
|
-
encodingAESKey: "aes",
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
},
|
|
140
|
-
} as any,
|
|
141
|
-
runtime: { log: () => { } },
|
|
142
|
-
core: mockCore,
|
|
143
|
-
path: "/plugins/wecom/bot/default"
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
afterEach(() => {
|
|
148
|
-
unregisterTarget?.();
|
|
149
|
-
unregisterTarget = undefined;
|
|
150
|
-
vi.useRealTimers();
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("should protect <think> tags from table conversion", async () => {
|
|
154
|
-
const req = createMockRequest({ encrypt: "mock-encrypt" });
|
|
155
|
-
const res = createMockResponse();
|
|
156
|
-
await handleWecomWebhookRequest(req, res);
|
|
157
|
-
|
|
158
|
-
// The WeCom monitor debounces inbound messages before starting the agent.
|
|
159
|
-
// `flushPending` triggers async agent start without awaiting it, so give the
|
|
160
|
-
// microtask queue a chance to run after the timer fires.
|
|
161
|
-
await vi.runOnlyPendingTimersAsync();
|
|
162
|
-
await Promise.resolve();
|
|
163
|
-
await Promise.resolve();
|
|
164
|
-
|
|
165
|
-
expect(capturedDeliver).toBeDefined();
|
|
166
|
-
|
|
167
|
-
const payload = { text: "Out | side\n<think>Inside | Think</think>" };
|
|
168
|
-
const convertSpy = vi.spyOn(mockCore.channel.text, "convertMarkdownTables");
|
|
169
|
-
|
|
170
|
-
await capturedDeliver!(payload);
|
|
171
|
-
|
|
172
|
-
const calledArg = convertSpy.mock.calls[0][0];
|
|
173
|
-
expect(calledArg).toContain("__THINK_PLACEHOLDER_0__");
|
|
174
|
-
expect(calledArg).not.toContain("<think>");
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
it("should store response_url and allow active message sending", async () => {
|
|
178
|
-
const req = createMockRequest({ encrypt: "mock-encrypt" });
|
|
179
|
-
const res = createMockResponse();
|
|
180
|
-
|
|
181
|
-
// We use a real key but mocked randomBytes.
|
|
182
|
-
// However, `handleWecomWebhookRequest` calls `buildEncryptedJsonReply` -> `encryptWecomPlaintext`.
|
|
183
|
-
// `encryptWecomPlaintext` uses the key. Since it's valid, it should work fine.
|
|
184
|
-
// We don't verify the OUTPUT of handleWecomWebhookRequest, just that it runs and sets up state.
|
|
185
|
-
|
|
186
|
-
await handleWecomWebhookRequest(req, res);
|
|
187
|
-
|
|
188
|
-
const streamId = Buffer.alloc(16, 0x11).toString("hex");
|
|
189
|
-
|
|
190
|
-
undiciFetch.mockResolvedValue(new Response("ok", { status: 200 }));
|
|
191
|
-
await sendActiveMessage(streamId, "Active Hello");
|
|
192
|
-
|
|
193
|
-
expect(undiciFetch).toHaveBeenCalled();
|
|
194
|
-
const [url, init] = undiciFetch.mock.calls.at(-1)! as [string, RequestInit];
|
|
195
|
-
expect(url).toBe("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=test-key");
|
|
196
|
-
expect(init).toEqual(
|
|
197
|
-
expect.objectContaining({
|
|
198
|
-
method: "POST",
|
|
199
|
-
body: JSON.stringify({ msgtype: "text", text: { content: "Active Hello" } }),
|
|
200
|
-
}),
|
|
201
|
-
);
|
|
202
|
-
const headers = new Headers(init.headers);
|
|
203
|
-
expect(headers.get("content-type")).toBe("application/json");
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
it("should fallback non-image media to agent DM (and push a Chinese prompt)", async () => {
|
|
207
|
-
const { uploadMedia, sendMedia } = agentApi as any;
|
|
208
|
-
uploadMedia.mockResolvedValue("media-id-1");
|
|
209
|
-
sendMedia.mockResolvedValue(undefined);
|
|
210
|
-
|
|
211
|
-
const req = createMockRequest({ encrypt: "mock-encrypt" });
|
|
212
|
-
const res = createMockResponse();
|
|
213
|
-
await handleWecomWebhookRequest(req, res);
|
|
214
|
-
|
|
215
|
-
await vi.advanceTimersByTimeAsync(600);
|
|
216
|
-
await Promise.resolve();
|
|
217
|
-
await Promise.resolve();
|
|
218
|
-
|
|
219
|
-
expect(capturedDeliver).toBeDefined();
|
|
220
|
-
|
|
221
|
-
// Create a local PDF to force non-image content-type inference.
|
|
222
|
-
const fs = await import("node:fs/promises");
|
|
223
|
-
const os = await import("node:os");
|
|
224
|
-
const path = await import("node:path");
|
|
225
|
-
const tmp = path.join(os.tmpdir(), `wecom-test-${Date.now()}.pdf`);
|
|
226
|
-
await fs.writeFile(tmp, Buffer.from("pdf"));
|
|
227
|
-
|
|
228
|
-
undiciFetch.mockResolvedValue(new Response("ok", { status: 200 }));
|
|
229
|
-
|
|
230
|
-
await capturedDeliver!({ text: "here", mediaUrls: [tmp] } as any);
|
|
231
|
-
|
|
232
|
-
expect(uploadMedia).toHaveBeenCalled();
|
|
233
|
-
expect(sendMedia).toHaveBeenCalledWith(
|
|
234
|
-
expect.objectContaining({
|
|
235
|
-
toUser: senderUserId,
|
|
236
|
-
mediaType: "file",
|
|
237
|
-
}),
|
|
238
|
-
);
|
|
239
|
-
// Ensure we attempted to push a prompt to response_url (uses undici fetch).
|
|
240
|
-
expect(undiciFetch).toHaveBeenCalled();
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// 注:本机路径(/Users/...、/tmp/...、/root/...、/home/...)短路发图逻辑属于运行态特性,
|
|
244
|
-
// 单测在 fake timers + module singleton 状态下容易引入脆弱性;这里优先覆盖更关键的兜底链路与去重逻辑。
|
|
245
|
-
});
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
2
|
-
|
|
3
|
-
import { shouldProcessBotInboundMessage } from "./monitor.js";
|
|
4
|
-
|
|
5
|
-
describe("shouldProcessBotInboundMessage", () => {
|
|
6
|
-
it("skips payloads without sender id", () => {
|
|
7
|
-
const result = shouldProcessBotInboundMessage({
|
|
8
|
-
msgtype: "text",
|
|
9
|
-
from: {},
|
|
10
|
-
text: { content: "hello" },
|
|
11
|
-
});
|
|
12
|
-
expect(result.shouldProcess).toBe(false);
|
|
13
|
-
expect(result.reason).toBe("missing_sender");
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
it("skips system sender payloads", () => {
|
|
17
|
-
const result = shouldProcessBotInboundMessage({
|
|
18
|
-
msgtype: "text",
|
|
19
|
-
from: { userid: "sys" },
|
|
20
|
-
text: { content: "hello" },
|
|
21
|
-
});
|
|
22
|
-
expect(result.shouldProcess).toBe(false);
|
|
23
|
-
expect(result.reason).toBe("system_sender");
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("skips group payloads without chatid", () => {
|
|
27
|
-
const result = shouldProcessBotInboundMessage({
|
|
28
|
-
msgtype: "text",
|
|
29
|
-
chattype: "group",
|
|
30
|
-
from: { userid: "zhangsan" },
|
|
31
|
-
text: { content: "hello" },
|
|
32
|
-
});
|
|
33
|
-
expect(result.shouldProcess).toBe(false);
|
|
34
|
-
expect(result.reason).toBe("missing_chatid");
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("accepts normal direct-user messages", () => {
|
|
38
|
-
const result = shouldProcessBotInboundMessage({
|
|
39
|
-
msgtype: "text",
|
|
40
|
-
chattype: "single",
|
|
41
|
-
from: { userid: "zhangsan" },
|
|
42
|
-
text: { content: "hello" },
|
|
43
|
-
});
|
|
44
|
-
expect(result.shouldProcess).toBe(true);
|
|
45
|
-
expect(result.reason).toBe("user_message");
|
|
46
|
-
expect(result.senderUserId).toBe("zhangsan");
|
|
47
|
-
expect(result.chatId).toBe("zhangsan");
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it("accepts normal group messages with chatid", () => {
|
|
51
|
-
const result = shouldProcessBotInboundMessage({
|
|
52
|
-
msgtype: "text",
|
|
53
|
-
chattype: "group",
|
|
54
|
-
chatid: "wr123",
|
|
55
|
-
from: { userid: "zhangsan" },
|
|
56
|
-
text: { content: "hello" },
|
|
57
|
-
});
|
|
58
|
-
expect(result.shouldProcess).toBe(true);
|
|
59
|
-
expect(result.reason).toBe("user_message");
|
|
60
|
-
expect(result.senderUserId).toBe("zhangsan");
|
|
61
|
-
expect(result.chatId).toBe("wr123");
|
|
62
|
-
});
|
|
63
|
-
});
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
2
|
-
import { handleWecomWebhookRequest, registerWecomWebhookTarget } from "./monitor.js";
|
|
3
|
-
import { encryptWecomPlaintext, computeWecomMsgSignature, WECOM_PKCS7_BLOCK_SIZE } from "./crypto.js";
|
|
4
|
-
import * as runtime from "./runtime.js";
|
|
5
|
-
import crypto from "node:crypto";
|
|
6
|
-
import { IncomingMessage, ServerResponse } from "node:http";
|
|
7
|
-
import { Socket } from "node:net";
|
|
8
|
-
|
|
9
|
-
const { undiciFetch } = vi.hoisted(() => {
|
|
10
|
-
const undiciFetch = vi.fn();
|
|
11
|
-
return { undiciFetch };
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
vi.mock("undici", () => ({
|
|
15
|
-
fetch: undiciFetch,
|
|
16
|
-
ProxyAgent: class ProxyAgent { },
|
|
17
|
-
}));
|
|
18
|
-
|
|
19
|
-
// Helpers to simulate HTTP request
|
|
20
|
-
function createMockRequest(bodyObj: any, query: URLSearchParams): IncomingMessage {
|
|
21
|
-
const socket = new Socket();
|
|
22
|
-
const req = new IncomingMessage(socket);
|
|
23
|
-
req.method = "POST";
|
|
24
|
-
req.url = `/plugins/wecom/bot/default?${query.toString()}`;
|
|
25
|
-
req.push(JSON.stringify(bodyObj));
|
|
26
|
-
req.push(null);
|
|
27
|
-
return req;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function createMockResponse(): ServerResponse & { _getData: () => string, _getStatusCode: () => number } {
|
|
31
|
-
const req = new IncomingMessage(new Socket());
|
|
32
|
-
const res = new ServerResponse(req);
|
|
33
|
-
let data = "";
|
|
34
|
-
res.write = (chunk: any) => { data += chunk; return true; };
|
|
35
|
-
res.end = (chunk: any) => { if (chunk) data += chunk; return res; };
|
|
36
|
-
(res as any)._getData = () => data;
|
|
37
|
-
(res as any)._getStatusCode = () => res.statusCode;
|
|
38
|
-
return res as any;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// PKCS7 Pad Helper for manual encryption
|
|
42
|
-
function pkcs7Pad(buf: Buffer, blockSize: number): Buffer {
|
|
43
|
-
const mod = buf.length % blockSize;
|
|
44
|
-
const pad = mod === 0 ? blockSize : blockSize - mod;
|
|
45
|
-
const padByte = Buffer.from([pad]);
|
|
46
|
-
return Buffer.concat([buf, Buffer.alloc(pad, padByte[0]!)]);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
describe("Monitor Integration: Inbound Image", () => {
|
|
50
|
-
const token = "MY_TOKEN";
|
|
51
|
-
const encodingAESKey = "jWmYm7qr5nMoCAstdRmNjt3p7vsH8HkK+qiJqQ0aaaa="; // 32 bytes key
|
|
52
|
-
const receiveId = "MY_CORPID";
|
|
53
|
-
let unregisterTarget: (() => void) | null = null;
|
|
54
|
-
|
|
55
|
-
// Mock Core Runtime
|
|
56
|
-
const mockDeliver = vi.fn();
|
|
57
|
-
const mockCore = {
|
|
58
|
-
channel: {
|
|
59
|
-
routing: { resolveAgentRoute: () => ({ agentId: "agent-1", sessionKey: "sess-1", accountId: "acc-1" }) },
|
|
60
|
-
commands: {
|
|
61
|
-
shouldComputeCommandAuthorized: () => false,
|
|
62
|
-
resolveCommandAuthorizedFromAuthorizers: () => true,
|
|
63
|
-
},
|
|
64
|
-
pairing: {
|
|
65
|
-
readAllowFromStore: async () => [],
|
|
66
|
-
},
|
|
67
|
-
session: {
|
|
68
|
-
resolveStorePath: () => "store/path",
|
|
69
|
-
readSessionUpdatedAt: () => 0,
|
|
70
|
-
recordInboundSession: vi.fn(),
|
|
71
|
-
},
|
|
72
|
-
reply: {
|
|
73
|
-
formatAgentEnvelope: () => "formatted-body",
|
|
74
|
-
finalizeInboundContext: (ctx: any) => ctx,
|
|
75
|
-
resolveEnvelopeFormatOptions: () => ({}),
|
|
76
|
-
dispatchReplyWithBufferedBlockDispatcher: async (opts: any) => {
|
|
77
|
-
// Simulate Agent processing by calling deliver immediately or later
|
|
78
|
-
// For this test, verifying the Inbound Body is enough.
|
|
79
|
-
// The delivery payload is what the AGENT sees.
|
|
80
|
-
// But wait, dispatchReply... is for OUTBOUND streaming replies.
|
|
81
|
-
// startAgentForStream calls it.
|
|
82
|
-
// We really want to spy on what `rawBody` was passed to startAgentForStream context.
|
|
83
|
-
|
|
84
|
-
// Actually `recordInboundSession` receives `ctx` which contains `RawBody`.
|
|
85
|
-
return;
|
|
86
|
-
},
|
|
87
|
-
},
|
|
88
|
-
text: { resolveMarkdownTableMode: () => "off", convertMarkdownTables: (t: string) => t },
|
|
89
|
-
},
|
|
90
|
-
logging: { shouldLogVerbose: () => true },
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
beforeEach(() => {
|
|
94
|
-
vi.spyOn(runtime, "getWecomRuntime").mockReturnValue(mockCore as any);
|
|
95
|
-
|
|
96
|
-
unregisterTarget?.();
|
|
97
|
-
unregisterTarget = registerWecomWebhookTarget({
|
|
98
|
-
account: {
|
|
99
|
-
accountId: "test-acc",
|
|
100
|
-
name: "Test",
|
|
101
|
-
configured: true,
|
|
102
|
-
token,
|
|
103
|
-
encodingAESKey,
|
|
104
|
-
receiveId,
|
|
105
|
-
config: {} as any
|
|
106
|
-
},
|
|
107
|
-
config: {} as any,
|
|
108
|
-
runtime: { log: console.log, error: console.error },
|
|
109
|
-
core: mockCore as any,
|
|
110
|
-
path: "/plugins/wecom/bot/default"
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
afterEach(() => {
|
|
115
|
-
unregisterTarget?.();
|
|
116
|
-
unregisterTarget = null;
|
|
117
|
-
vi.restoreAllMocks();
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
// Mock media saving
|
|
121
|
-
const mockSaveMediaBuffer = vi.fn().mockResolvedValue({ path: "/tmp/saved-image.jpg", contentType: "image/jpeg" });
|
|
122
|
-
(mockCore.channel as any).media = { saveMediaBuffer: mockSaveMediaBuffer };
|
|
123
|
-
|
|
124
|
-
it("should decrypt inbound image, save it, and inject into context", async () => {
|
|
125
|
-
// 1. Prepare Encrypted Media (The "File" on WeCom Server)
|
|
126
|
-
const fileContent = Buffer.from("fake-image-data");
|
|
127
|
-
const aesKey = Buffer.from(encodingAESKey + "=", "base64");
|
|
128
|
-
const iv = aesKey.subarray(0, 16);
|
|
129
|
-
|
|
130
|
-
// Encrypt content (WeCom does this)
|
|
131
|
-
const cipher = crypto.createCipheriv("aes-256-cbc", aesKey, iv);
|
|
132
|
-
cipher.setAutoPadding(false);
|
|
133
|
-
const encryptedMedia = Buffer.concat([cipher.update(pkcs7Pad(fileContent, WECOM_PKCS7_BLOCK_SIZE)), cipher.final()]);
|
|
134
|
-
|
|
135
|
-
// Mock HTTP fetch to return this encrypted media
|
|
136
|
-
undiciFetch.mockResolvedValue(new Response(encryptedMedia));
|
|
137
|
-
|
|
138
|
-
// 2. Prepare Inbound Message (The Webhook JSON)
|
|
139
|
-
const imageUrl = "http://wecom.server/media/123";
|
|
140
|
-
const inboundMsg = {
|
|
141
|
-
msgtype: "image",
|
|
142
|
-
image: { url: imageUrl },
|
|
143
|
-
from: { userid: "yanhaidao" }
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
// 3. Encrypt the *Inbound Message* Payload (The Envelope)
|
|
147
|
-
const timestamp = String(Math.floor(Date.now() / 1000));
|
|
148
|
-
const nonce = "123456";
|
|
149
|
-
const encrypt = encryptWecomPlaintext({
|
|
150
|
-
encodingAESKey,
|
|
151
|
-
receiveId,
|
|
152
|
-
plaintext: JSON.stringify(inboundMsg)
|
|
153
|
-
});
|
|
154
|
-
const msgSignature = computeWecomMsgSignature({ token, timestamp, nonce, encrypt });
|
|
155
|
-
|
|
156
|
-
const query = new URLSearchParams({
|
|
157
|
-
msg_signature: msgSignature,
|
|
158
|
-
timestamp,
|
|
159
|
-
nonce
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const bodyObj = {
|
|
163
|
-
touser: receiveId,
|
|
164
|
-
agentid: "10001",
|
|
165
|
-
encrypt, // Standard WeCom POST body structure
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
// 4. Send Request
|
|
169
|
-
const req = createMockRequest(bodyObj, query);
|
|
170
|
-
const res = createMockResponse();
|
|
171
|
-
|
|
172
|
-
await handleWecomWebhookRequest(req, res);
|
|
173
|
-
|
|
174
|
-
// Wait for debounce timer to trigger agent (DEFAULT_DEBOUNCE_MS = 500ms)
|
|
175
|
-
await new Promise(resolve => setTimeout(resolve, 600));
|
|
176
|
-
|
|
177
|
-
// 5. Verify
|
|
178
|
-
// Check recordInboundSession was called with correct RawBody and Media Context
|
|
179
|
-
expect(mockCore.channel.session.recordInboundSession).toHaveBeenCalled();
|
|
180
|
-
const recordCall = (mockCore.channel.session.recordInboundSession as any).mock.calls[0][0];
|
|
181
|
-
const ctx = recordCall.ctx;
|
|
182
|
-
|
|
183
|
-
// Expect: [image]
|
|
184
|
-
expect(ctx.RawBody).toBe("[image]");
|
|
185
|
-
|
|
186
|
-
// Expect media to be saved
|
|
187
|
-
expect(mockSaveMediaBuffer).toHaveBeenCalledWith(
|
|
188
|
-
expect.any(Buffer), // The decrypted buffer
|
|
189
|
-
"image/jpeg",
|
|
190
|
-
"inbound",
|
|
191
|
-
expect.any(Number), // maxBytes
|
|
192
|
-
"image.jpg"
|
|
193
|
-
);
|
|
194
|
-
const savedBuffer = mockSaveMediaBuffer.mock.calls[0][0];
|
|
195
|
-
expect(savedBuffer.toString()).toBe("fake-image-data");
|
|
196
|
-
|
|
197
|
-
// Expect Context Injection
|
|
198
|
-
expect(ctx.MediaPath).toBe("/tmp/saved-image.jpg");
|
|
199
|
-
expect(ctx.MediaType).toBe("image/jpeg");
|
|
200
|
-
expect(ctx.Surface).toBe("wecom");
|
|
201
|
-
expect(ctx.OriginatingChannel).toBe("wecom");
|
|
202
|
-
|
|
203
|
-
expect(undiciFetch).toHaveBeenCalledWith(
|
|
204
|
-
imageUrl,
|
|
205
|
-
expect.objectContaining({ signal: expect.anything() }),
|
|
206
|
-
);
|
|
207
|
-
});
|
|
208
|
-
});
|
package/src/monitor.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
|
|
3
|
-
import { getWecomRuntime } from "./runtime.js";
|
|
4
|
-
import { handleWecomHttpRequest } from "./transport/http/request-handler.js";
|
|
5
|
-
import { registerAgentWebhookTarget, registerWecomWebhookTarget } from "./transport/http/registry.js";
|
|
6
|
-
import { sendActiveMessage } from "./transport/bot-webhook/active-reply.js";
|
|
7
|
-
import { createBotWebhookRequestHandler } from "./transport/bot-webhook/request-handler.js";
|
|
8
|
-
import {
|
|
9
|
-
shouldProcessBotInboundMessage,
|
|
10
|
-
type BotInboundProcessDecision,
|
|
11
|
-
} from "./transport/bot-webhook/message-shape.js";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Legacy compatibility bridge for the old monitor entrypoints.
|
|
15
|
-
*
|
|
16
|
-
* Bot webhook parsing and stream orchestration now live in
|
|
17
|
-
* `transport/bot-webhook/*` and `capability/bot/*`.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
import type {
|
|
21
|
-
WecomRuntimeAuditEvent,
|
|
22
|
-
WecomWebhookTarget,
|
|
23
|
-
} from "./types/runtime-context.js";
|
|
24
|
-
import { monitorState } from "./monitor/state.js";
|
|
25
|
-
import { createBotStreamOrchestrator } from "./capability/bot/stream-orchestrator.js";
|
|
26
|
-
|
|
27
|
-
const streamStore = monitorState.streamStore;
|
|
28
|
-
|
|
29
|
-
function recordWebhookOperationalEvent(
|
|
30
|
-
target:
|
|
31
|
-
| Pick<WecomWebhookTarget, "account" | "auditSink">
|
|
32
|
-
| { agent: { accountId: string }; auditSink?: (event: WecomRuntimeAuditEvent) => void },
|
|
33
|
-
event: WecomRuntimeAuditEvent,
|
|
34
|
-
): void {
|
|
35
|
-
const accountId = "account" in target ? target.account.accountId : target.agent.accountId;
|
|
36
|
-
monitorState.operationalEvents.append({
|
|
37
|
-
accountId,
|
|
38
|
-
transport: event.transport,
|
|
39
|
-
category: event.category,
|
|
40
|
-
summary: event.summary,
|
|
41
|
-
messageId: event.messageId,
|
|
42
|
-
});
|
|
43
|
-
target.auditSink?.(event);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function recordBotOperationalEvent(
|
|
47
|
-
target: Pick<WecomWebhookTarget, "account" | "auditSink">,
|
|
48
|
-
event: Omit<WecomRuntimeAuditEvent, "transport">,
|
|
49
|
-
): void {
|
|
50
|
-
recordWebhookOperationalEvent(target, {
|
|
51
|
-
transport: "bot-webhook",
|
|
52
|
-
...event,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function logVerbose(target: WecomWebhookTarget, message: string): void {
|
|
57
|
-
const should =
|
|
58
|
-
target.core.logging?.shouldLogVerbose?.() ??
|
|
59
|
-
(() => {
|
|
60
|
-
try {
|
|
61
|
-
return getWecomRuntime().logging.shouldLogVerbose();
|
|
62
|
-
} catch {
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
})();
|
|
66
|
-
if (!should) return;
|
|
67
|
-
target.runtime.log?.(`[wecom] ${message}`);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function logInfo(target: WecomWebhookTarget, message: string): void {
|
|
71
|
-
target.runtime.log?.(`[wecom] ${message}`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const botStreamOrchestrator = createBotStreamOrchestrator({
|
|
75
|
-
streamStore,
|
|
76
|
-
recordBotOperationalEvent,
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const { flushPending, startAgentForStream } = botStreamOrchestrator;
|
|
80
|
-
|
|
81
|
-
monitorState.streamStore.setFlushHandler((pending) => void flushPending(pending));
|
|
82
|
-
|
|
83
|
-
const handleBotWebhookRequest = createBotWebhookRequestHandler({
|
|
84
|
-
streamStore,
|
|
85
|
-
logInfo,
|
|
86
|
-
logVerbose,
|
|
87
|
-
recordBotOperationalEvent,
|
|
88
|
-
startAgentForStream,
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
export { registerAgentWebhookTarget, registerWecomWebhookTarget };
|
|
92
|
-
export { sendActiveMessage, shouldProcessBotInboundMessage };
|
|
93
|
-
export type { BotInboundProcessDecision };
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* **handleWecomWebhookRequest (HTTP 请求入口)**
|
|
97
|
-
*
|
|
98
|
-
* 处理来自企业微信的所有 Webhook 请求。
|
|
99
|
-
* 职责:
|
|
100
|
-
* 1. 路由分发:优先按 `/plugins/wecom/{bot|agent}/{accountId}` 分流,并兼容历史 `/wecom/*` 路径。
|
|
101
|
-
* 2. 安全校验:验证企业微信签名 (Signature)。
|
|
102
|
-
* 3. 消息解密:处理企业微信的加密包。
|
|
103
|
-
* 4. 响应处理:
|
|
104
|
-
* - GET 请求:处理 EchoStr 验证。
|
|
105
|
-
* - POST 请求:接收消息,放入 StreamStore,返回流式 First Chunk。
|
|
106
|
-
*/
|
|
107
|
-
export async function handleWecomWebhookRequest(req: IncomingMessage, res: ServerResponse): Promise<boolean> {
|
|
108
|
-
return handleWecomHttpRequest({
|
|
109
|
-
req,
|
|
110
|
-
res,
|
|
111
|
-
handleBotWebhookRequest,
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export async function handleLegacyWecomWebhookRequest(req: IncomingMessage, res: ServerResponse): Promise<boolean> {
|
|
116
|
-
return handleWecomHttpRequest({
|
|
117
|
-
req,
|
|
118
|
-
res,
|
|
119
|
-
handleBotWebhookRequest,
|
|
120
|
-
});
|
|
121
|
-
}
|