@yanhaidao/wecom 2.4.120 → 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/README.md +4 -5
- 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/MENU_EVENT_CONF.md +0 -500
- package/MENU_EVENT_PLAN.md +0 -440
- 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/compat-single-account.md +0 -148
- package/index.test.ts +0 -38
- package/scripts/test-proxy.ts +0 -70
- package/scripts/wecom/README.md +0 -123
- package/scripts/wecom/menu-click-help.js +0 -59
- package/scripts/wecom/menu-click-help.py +0 -55
- package/src/accounts.ts +0 -34
- package/src/agent/api-client.upload.test.ts +0 -109
- package/src/agent/event-router.test.ts +0 -421
- package/src/agent/event-router.ts +0 -272
- package/src/agent/handler.event-filter.test.ts +0 -135
- package/src/agent/handler.ts +0 -1250
- package/src/agent/index.ts +0 -12
- package/src/agent/script-runner.ts +0 -186
- package/src/agent/test-fixtures/invalid-json-script.mjs +0 -1
- package/src/agent/test-fixtures/reply-event-script.mjs +0 -29
- package/src/agent/test-fixtures/reply-event-script.py +0 -17
- 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 -180
- 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 -312
- package/src/config/derived-paths.test.ts +0 -111
- package/src/config/derived-paths.ts +0 -41
- package/src/config/index.ts +0 -22
- package/src/config/media.test.ts +0 -113
- package/src/config/media.ts +0 -139
- package/src/config/network.ts +0 -20
- 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 -144
- 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 -137
- 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.ts +0 -371
- package/src/transport/bot-webhook/inbound.ts +0 -5
- package/src/transport/bot-webhook/message-shape.ts +0 -89
- 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 -96
- package/src/transport/bot-ws/inbound.ts +0 -116
- 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 -72
- package/src/types/config.ts +0 -166
- 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 -187
- 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
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { sendAgentApiMediaReply, sendAgentApiTextReply } from "./reply.js";
|
|
2
|
+
import { uploadAgentApiMedia } from "./media-upload.js";
|
|
3
|
+
export async function deliverAgentApiText(params) {
|
|
4
|
+
await sendAgentApiTextReply(params);
|
|
5
|
+
}
|
|
6
|
+
export async function deliverAgentApiMedia(params) {
|
|
7
|
+
let mediaType = "file";
|
|
8
|
+
if (params.contentType.startsWith("image/"))
|
|
9
|
+
mediaType = "image";
|
|
10
|
+
else if (params.contentType.startsWith("audio/"))
|
|
11
|
+
mediaType = "voice";
|
|
12
|
+
else if (params.contentType.startsWith("video/"))
|
|
13
|
+
mediaType = "video";
|
|
14
|
+
const mediaId = await uploadAgentApiMedia({
|
|
15
|
+
agent: params.agent,
|
|
16
|
+
type: mediaType,
|
|
17
|
+
buffer: params.buffer,
|
|
18
|
+
filename: params.filename,
|
|
19
|
+
});
|
|
20
|
+
await sendAgentApiMediaReply({
|
|
21
|
+
agent: params.agent,
|
|
22
|
+
target: params.target,
|
|
23
|
+
mediaId,
|
|
24
|
+
mediaType,
|
|
25
|
+
title: mediaType === "video" ? params.text?.trim().slice(0, 64) : undefined,
|
|
26
|
+
description: mediaType === "video" ? params.text?.trim().slice(0, 512) : undefined,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { sendAgentApiMedia, sendAgentApiText } from "./client.js";
|
|
2
|
+
export async function sendAgentApiTextReply(params) {
|
|
3
|
+
await sendAgentApiText({
|
|
4
|
+
agent: params.agent,
|
|
5
|
+
toUser: params.target.touser,
|
|
6
|
+
toParty: params.target.toparty,
|
|
7
|
+
toTag: params.target.totag,
|
|
8
|
+
chatId: params.target.chatid,
|
|
9
|
+
text: params.text,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export async function sendAgentApiMediaReply(params) {
|
|
13
|
+
await sendAgentApiMedia({
|
|
14
|
+
agent: params.agent,
|
|
15
|
+
toUser: params.target.touser,
|
|
16
|
+
toParty: params.target.toparty,
|
|
17
|
+
toTag: params.target.totag,
|
|
18
|
+
chatId: params.target.chatid,
|
|
19
|
+
mediaId: params.mediaId,
|
|
20
|
+
mediaType: params.mediaType,
|
|
21
|
+
title: params.title,
|
|
22
|
+
description: params.description,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { sendUpstreamAgentApiMediaReply, sendUpstreamAgentApiTextReply } from "./upstream-reply.js";
|
|
2
|
+
export async function deliverUpstreamAgentApiText(params) {
|
|
3
|
+
await sendUpstreamAgentApiTextReply(params);
|
|
4
|
+
}
|
|
5
|
+
export async function deliverUpstreamAgentApiMedia(params) {
|
|
6
|
+
let mediaType = "file";
|
|
7
|
+
if (params.contentType.startsWith("image/"))
|
|
8
|
+
mediaType = "image";
|
|
9
|
+
else if (params.contentType.startsWith("audio/"))
|
|
10
|
+
mediaType = "voice";
|
|
11
|
+
else if (params.contentType.startsWith("video/"))
|
|
12
|
+
mediaType = "video";
|
|
13
|
+
const { uploadUpstreamAgentApiMedia } = await import("./upstream-media-upload.js");
|
|
14
|
+
const mediaId = await uploadUpstreamAgentApiMedia({
|
|
15
|
+
upstreamAgent: params.upstreamAgent,
|
|
16
|
+
primaryAgent: params.primaryAgent,
|
|
17
|
+
type: mediaType,
|
|
18
|
+
buffer: params.buffer,
|
|
19
|
+
filename: params.filename,
|
|
20
|
+
});
|
|
21
|
+
await sendUpstreamAgentApiMediaReply({
|
|
22
|
+
upstreamAgent: params.upstreamAgent,
|
|
23
|
+
primaryAgent: params.primaryAgent,
|
|
24
|
+
target: params.target,
|
|
25
|
+
mediaId,
|
|
26
|
+
mediaType,
|
|
27
|
+
title: mediaType === "video" ? params.text?.trim().slice(0, 64) : undefined,
|
|
28
|
+
description: mediaType === "video" ? params.text?.trim().slice(0, 512) : undefined,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { resolveWecomEgressProxyUrlFromNetwork } from "../../config/index.js";
|
|
3
|
+
import { LIMITS } from "../../types/constants.js";
|
|
4
|
+
import { wecomFetch } from "../../http.js";
|
|
5
|
+
import { guessUploadContentType, normalizeUploadFilename } from "./core.js";
|
|
6
|
+
import { getUpstreamAgentApiAccessToken } from "./client.js";
|
|
7
|
+
export async function uploadUpstreamAgentApiMedia(params) {
|
|
8
|
+
const { upstreamAgent, primaryAgent, type, buffer, filename } = params;
|
|
9
|
+
const safeFilename = normalizeUploadFilename(filename);
|
|
10
|
+
// 使用下游企业的 access_token
|
|
11
|
+
const token = await getUpstreamAgentApiAccessToken({
|
|
12
|
+
primaryAgent,
|
|
13
|
+
upstreamCorpId: upstreamAgent.corpId,
|
|
14
|
+
upstreamAgentId: upstreamAgent.agentId,
|
|
15
|
+
});
|
|
16
|
+
const proxyUrl = resolveWecomEgressProxyUrlFromNetwork(upstreamAgent.network);
|
|
17
|
+
const url = `https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=${encodeURIComponent(token)}&type=${encodeURIComponent(type)}&debug=1`;
|
|
18
|
+
const uploadOnce = async (fileContentType) => {
|
|
19
|
+
const boundary = `----WebKitFormBoundary${crypto.randomBytes(16).toString("hex")}`;
|
|
20
|
+
const header = Buffer.from(`--${boundary}\r\n` +
|
|
21
|
+
`Content-Disposition: form-data; name="media"; filename="${safeFilename}"; filelength=${buffer.length}\r\n` +
|
|
22
|
+
`Content-Type: ${fileContentType}\r\n\r\n`);
|
|
23
|
+
const footer = Buffer.from(`\r\n--${boundary}--\r\n`);
|
|
24
|
+
const body = Buffer.concat([header, buffer, footer]);
|
|
25
|
+
const res = await wecomFetch(url, {
|
|
26
|
+
method: "POST",
|
|
27
|
+
headers: {
|
|
28
|
+
"Content-Type": `multipart/form-data; boundary=${boundary}`,
|
|
29
|
+
"Content-Length": String(body.length),
|
|
30
|
+
},
|
|
31
|
+
body,
|
|
32
|
+
}, { proxyUrl, timeoutMs: LIMITS.REQUEST_TIMEOUT_MS });
|
|
33
|
+
const json = (await res.json());
|
|
34
|
+
return json;
|
|
35
|
+
};
|
|
36
|
+
const preferredContentType = guessUploadContentType(safeFilename);
|
|
37
|
+
let json = await uploadOnce(preferredContentType);
|
|
38
|
+
if (!json?.media_id && preferredContentType !== "application/octet-stream") {
|
|
39
|
+
console.warn(`[wecom-upstream-upload] Upload failed with ${preferredContentType}, retrying as application/octet-stream: ${json?.errcode} ${json?.errmsg}`);
|
|
40
|
+
json = await uploadOnce("application/octet-stream");
|
|
41
|
+
}
|
|
42
|
+
if (!json?.media_id) {
|
|
43
|
+
throw new Error(`upload failed: ${json?.errcode} ${json?.errmsg}`);
|
|
44
|
+
}
|
|
45
|
+
return json.media_id;
|
|
46
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { sendUpstreamAgentApiMedia, sendUpstreamAgentApiText } from "./client.js";
|
|
2
|
+
export async function sendUpstreamAgentApiTextReply(params) {
|
|
3
|
+
await sendUpstreamAgentApiText({
|
|
4
|
+
upstreamAgent: params.upstreamAgent,
|
|
5
|
+
primaryAgent: params.primaryAgent,
|
|
6
|
+
toUser: params.target.touser,
|
|
7
|
+
toParty: params.target.toparty,
|
|
8
|
+
toTag: params.target.totag,
|
|
9
|
+
chatId: params.target.chatid,
|
|
10
|
+
text: params.text,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
export async function sendUpstreamAgentApiMediaReply(params) {
|
|
14
|
+
await sendUpstreamAgentApiMedia({
|
|
15
|
+
upstreamAgent: params.upstreamAgent,
|
|
16
|
+
primaryAgent: params.primaryAgent,
|
|
17
|
+
toUser: params.target.touser,
|
|
18
|
+
toParty: params.target.toparty,
|
|
19
|
+
toTag: params.target.totag,
|
|
20
|
+
chatId: params.target.chatid,
|
|
21
|
+
mediaId: params.mediaId,
|
|
22
|
+
mediaType: params.mediaType,
|
|
23
|
+
title: params.title,
|
|
24
|
+
description: params.description,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { resolveAgentCallbackPaths } from "./inbound.js";
|
|
2
|
+
import { createAgentCallbackSessionSnapshot } from "./session.js";
|
|
3
|
+
import { registerAgentWebhookTarget } from "../http/registry.js";
|
|
4
|
+
export function startAgentCallbackTransport(params) {
|
|
5
|
+
const paths = resolveAgentCallbackPaths(params.account.accountId);
|
|
6
|
+
params.runtime.updateTransportSession(createAgentCallbackSessionSnapshot({
|
|
7
|
+
accountId: params.account.accountId,
|
|
8
|
+
running: true,
|
|
9
|
+
}));
|
|
10
|
+
const unregisters = paths.map((path) => registerAgentWebhookTarget({
|
|
11
|
+
agent: params.account,
|
|
12
|
+
config: params.cfg,
|
|
13
|
+
runtimeEnv: params.runtimeEnv,
|
|
14
|
+
touchTransportSession: (patch) => params.runtime.touchTransportSession("agent-callback", patch),
|
|
15
|
+
auditSink: (event) => params.runtime.recordOperationalIssue(event),
|
|
16
|
+
path,
|
|
17
|
+
}));
|
|
18
|
+
return {
|
|
19
|
+
paths,
|
|
20
|
+
stop: () => {
|
|
21
|
+
for (const unregister of unregisters) {
|
|
22
|
+
unregister();
|
|
23
|
+
}
|
|
24
|
+
params.runtime.updateTransportSession(createAgentCallbackSessionSnapshot({
|
|
25
|
+
accountId: params.account.accountId,
|
|
26
|
+
running: false,
|
|
27
|
+
}));
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { decryptWecomEncrypted, verifyWecomSignature } from "../../crypto.js";
|
|
2
|
+
import { extractEncryptFromXml } from "../../crypto/xml.js";
|
|
3
|
+
import { getWecomRuntime } from "../../runtime.js";
|
|
4
|
+
import { handleAgentWebhook } from "../../agent/index.js";
|
|
5
|
+
import { extractAgentId, parseXml } from "../../shared/xml-parser.js";
|
|
6
|
+
import { LIMITS as WECOM_LIMITS } from "../../types/constants.js";
|
|
7
|
+
import { logRouteFailure, readTextBody, resolveQueryParams, resolveSignatureParam, writeRouteFailure, } from "../http/common.js";
|
|
8
|
+
const ERROR_HELP = "\n\n遇到问题?联系作者: YanHaidao (微信: YanHaidao)";
|
|
9
|
+
function truncateForLog(raw, maxChars = 600) {
|
|
10
|
+
const compact = raw.replace(/\s+/g, " ").trim();
|
|
11
|
+
if (compact.length <= maxChars)
|
|
12
|
+
return compact;
|
|
13
|
+
return `${compact.slice(0, maxChars)}...(truncated)`;
|
|
14
|
+
}
|
|
15
|
+
function buildParsedAgentSummary(parsed) {
|
|
16
|
+
const data = parsed;
|
|
17
|
+
const msgType = String(data.MsgType ?? "").trim() || "unknown";
|
|
18
|
+
const fromUser = String(data.FromUserName ?? "").trim() || "N/A";
|
|
19
|
+
const toUser = String(data.ToUserName ?? "").trim() || "N/A";
|
|
20
|
+
const event = String(data.Event ?? "").trim() || "N/A";
|
|
21
|
+
const msgId = String(data.MsgId ?? "").trim() || "N/A";
|
|
22
|
+
const chatId = String(data.ChatId ?? data.chatid ?? "").trim() || "N/A";
|
|
23
|
+
const agentId = String(data.AgentID ?? "").trim() || "N/A";
|
|
24
|
+
return `msgType=${msgType} from=${fromUser} to=${toUser} event=${event} msgId=${msgId} chatId=${chatId} agentId=${agentId}`;
|
|
25
|
+
}
|
|
26
|
+
function normalizeAgentIdValue(value) {
|
|
27
|
+
if (typeof value === "number" && Number.isFinite(value))
|
|
28
|
+
return value;
|
|
29
|
+
const raw = String(value ?? "").trim();
|
|
30
|
+
if (!raw)
|
|
31
|
+
return undefined;
|
|
32
|
+
const parsed = Number(raw);
|
|
33
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
34
|
+
}
|
|
35
|
+
export async function handleAgentCallbackRequest(params) {
|
|
36
|
+
const { req, res, path, reqId, targets } = params;
|
|
37
|
+
if (targets.length === 0) {
|
|
38
|
+
console.error(`[wecom] inbound(agent): reqId=${reqId} path=${path} no_registered_target availableTargets=0`);
|
|
39
|
+
res.statusCode = 404;
|
|
40
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
41
|
+
res.end(`agent not configured for path=${path} - Agent 模式未配置或回调路径错误,请运行 openclaw onboarding${ERROR_HELP}`);
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
const query = resolveQueryParams(req);
|
|
45
|
+
const timestamp = query.get("timestamp") ?? "";
|
|
46
|
+
const nonce = query.get("nonce") ?? "";
|
|
47
|
+
const signature = resolveSignatureParam(query);
|
|
48
|
+
const hasSig = Boolean(signature);
|
|
49
|
+
const remote = req.socket?.remoteAddress ?? "unknown";
|
|
50
|
+
if (req.method === "GET") {
|
|
51
|
+
const echostr = query.get("echostr") ?? "";
|
|
52
|
+
const signatureMatches = targets.filter((target) => verifyWecomSignature({
|
|
53
|
+
token: target.agent.token,
|
|
54
|
+
timestamp,
|
|
55
|
+
nonce,
|
|
56
|
+
encrypt: echostr,
|
|
57
|
+
signature,
|
|
58
|
+
}));
|
|
59
|
+
if (signatureMatches.length !== 1) {
|
|
60
|
+
const reason = signatureMatches.length === 0 ? "wecom_account_not_found" : "wecom_account_conflict";
|
|
61
|
+
const candidateIds = (signatureMatches.length > 0 ? signatureMatches : targets).map((target) => target.agent.accountId);
|
|
62
|
+
logRouteFailure({
|
|
63
|
+
reqId,
|
|
64
|
+
path,
|
|
65
|
+
method: "GET",
|
|
66
|
+
reason,
|
|
67
|
+
candidateAccountIds: candidateIds,
|
|
68
|
+
});
|
|
69
|
+
writeRouteFailure(res, reason, reason === "wecom_account_conflict"
|
|
70
|
+
? "Agent callback account conflict: multiple accounts matched signature."
|
|
71
|
+
: "Agent callback account not found: signature verification failed.");
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
const selected = signatureMatches[0];
|
|
75
|
+
try {
|
|
76
|
+
const plain = decryptWecomEncrypted({
|
|
77
|
+
encodingAESKey: selected.agent.encodingAESKey,
|
|
78
|
+
receiveId: selected.agent.corpId,
|
|
79
|
+
encrypt: echostr,
|
|
80
|
+
});
|
|
81
|
+
res.statusCode = 200;
|
|
82
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
83
|
+
res.end(plain);
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
res.statusCode = 400;
|
|
88
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
89
|
+
res.end(`decrypt failed - 解密失败,请检查 EncodingAESKey${ERROR_HELP}`);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (req.method !== "POST") {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
const rawBody = await readTextBody(req, WECOM_LIMITS.MAX_REQUEST_BODY_SIZE);
|
|
97
|
+
if (!rawBody.ok) {
|
|
98
|
+
res.statusCode = 400;
|
|
99
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
100
|
+
res.end(rawBody.error || "invalid payload");
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
console.log(`[wecom] inbound(agent): reqId=${reqId} path=${path} rawXmlBytes=${Buffer.byteLength(rawBody.value, "utf8")} rawPreview=${JSON.stringify(truncateForLog(rawBody.value))}`);
|
|
104
|
+
let encrypted = "";
|
|
105
|
+
try {
|
|
106
|
+
encrypted = extractEncryptFromXml(rawBody.value);
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
res.statusCode = 400;
|
|
110
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
111
|
+
res.end(`invalid xml - 缺少 Encrypt 字段${ERROR_HELP}`);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
console.log(`[wecom] inbound(agent): reqId=${reqId} path=${path} encryptedLen=${encrypted.length}`);
|
|
115
|
+
const signatureMatches = targets.filter((target) => verifyWecomSignature({
|
|
116
|
+
token: target.agent.token,
|
|
117
|
+
timestamp,
|
|
118
|
+
nonce,
|
|
119
|
+
encrypt: encrypted,
|
|
120
|
+
signature,
|
|
121
|
+
}));
|
|
122
|
+
if (signatureMatches.length !== 1) {
|
|
123
|
+
const reason = signatureMatches.length === 0 ? "wecom_account_not_found" : "wecom_account_conflict";
|
|
124
|
+
const candidateIds = (signatureMatches.length > 0 ? signatureMatches : targets).map((target) => target.agent.accountId);
|
|
125
|
+
logRouteFailure({
|
|
126
|
+
reqId,
|
|
127
|
+
path,
|
|
128
|
+
method: "POST",
|
|
129
|
+
reason,
|
|
130
|
+
candidateAccountIds: candidateIds,
|
|
131
|
+
});
|
|
132
|
+
writeRouteFailure(res, reason, reason === "wecom_account_conflict"
|
|
133
|
+
? "Agent callback account conflict: multiple accounts matched signature."
|
|
134
|
+
: "Agent callback account not found: signature verification failed.");
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
const selected = signatureMatches[0];
|
|
138
|
+
let decrypted = "";
|
|
139
|
+
let parsed = null;
|
|
140
|
+
try {
|
|
141
|
+
decrypted = decryptWecomEncrypted({
|
|
142
|
+
encodingAESKey: selected.agent.encodingAESKey,
|
|
143
|
+
receiveId: selected.agent.corpId,
|
|
144
|
+
encrypt: encrypted,
|
|
145
|
+
});
|
|
146
|
+
parsed = parseXml(decrypted);
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
res.statusCode = 400;
|
|
150
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
151
|
+
res.end(`decrypt failed - 解密失败,请检查 EncodingAESKey${ERROR_HELP}`);
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
if (!parsed) {
|
|
155
|
+
res.statusCode = 400;
|
|
156
|
+
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
|
157
|
+
res.end(`invalid xml - XML 解析失败${ERROR_HELP}`);
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
selected.runtimeEnv.log?.(`[wecom] inbound(agent): reqId=${reqId} accountId=${selected.agent.accountId} decryptedBytes=${Buffer.byteLength(decrypted, "utf8")} parsed=${buildParsedAgentSummary(parsed)} decryptedPreview=${JSON.stringify(truncateForLog(decrypted))}`);
|
|
161
|
+
const inboundAgentId = normalizeAgentIdValue(extractAgentId(parsed));
|
|
162
|
+
if (inboundAgentId !== undefined &&
|
|
163
|
+
selected.agent.agentId !== undefined &&
|
|
164
|
+
inboundAgentId !== selected.agent.agentId) {
|
|
165
|
+
selected.runtimeEnv.error?.(`[wecom] inbound(agent): reqId=${reqId} accountId=${selected.agent.accountId} agentId_mismatch expected=${selected.agent.agentId} actual=${inboundAgentId}`);
|
|
166
|
+
}
|
|
167
|
+
const core = getWecomRuntime();
|
|
168
|
+
selected.runtimeEnv.log?.(`[wecom] inbound(agent): reqId=${reqId} method=${req.method ?? "UNKNOWN"} remote=${remote} timestamp=${timestamp ? "yes" : "no"} nonce=${nonce ? "yes" : "no"} msg_signature=${hasSig ? "yes" : "no"} accountId=${selected.agent.accountId}`);
|
|
169
|
+
selected.touchTransportSession?.({ lastInboundAt: Date.now(), running: true });
|
|
170
|
+
return handleAgentWebhook({
|
|
171
|
+
req,
|
|
172
|
+
res,
|
|
173
|
+
verifiedPost: {
|
|
174
|
+
timestamp,
|
|
175
|
+
nonce,
|
|
176
|
+
signature,
|
|
177
|
+
encrypted,
|
|
178
|
+
decrypted,
|
|
179
|
+
parsed,
|
|
180
|
+
},
|
|
181
|
+
agent: selected.agent,
|
|
182
|
+
config: selected.config,
|
|
183
|
+
core,
|
|
184
|
+
log: selected.runtimeEnv.log,
|
|
185
|
+
error: selected.runtimeEnv.error,
|
|
186
|
+
auditSink: selected.auditSink,
|
|
187
|
+
touchTransportSession: selected.touchTransportSession,
|
|
188
|
+
});
|
|
189
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function createAgentCallbackSessionSnapshot(params) {
|
|
2
|
+
return {
|
|
3
|
+
accountId: params.accountId,
|
|
4
|
+
transport: "agent-callback",
|
|
5
|
+
running: params.running,
|
|
6
|
+
ownerId: `${params.accountId}:agent-callback`,
|
|
7
|
+
connected: params.running,
|
|
8
|
+
authenticated: true,
|
|
9
|
+
lastConnectedAt: params.running ? Date.now() : undefined,
|
|
10
|
+
lastDisconnectedAt: params.running ? undefined : Date.now(),
|
|
11
|
+
lastInboundAt: params.lastInboundAt,
|
|
12
|
+
lastOutboundAt: params.lastOutboundAt,
|
|
13
|
+
lastError: params.lastError,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { wecomFetch } from "../../http.js";
|
|
2
|
+
import { LIMITS, monitorState } from "../../monitor/state.js";
|
|
3
|
+
const activeReplyStore = monitorState.activeReplyStore;
|
|
4
|
+
export function storeActiveReply(streamId, responseUrl, proxyUrl) {
|
|
5
|
+
activeReplyStore.store(streamId, responseUrl, proxyUrl);
|
|
6
|
+
}
|
|
7
|
+
export function getActiveReplyUrl(streamId) {
|
|
8
|
+
return activeReplyStore.getUrl(streamId);
|
|
9
|
+
}
|
|
10
|
+
export async function useActiveReplyOnce(streamId, fn) {
|
|
11
|
+
return activeReplyStore.use(streamId, async (params) => {
|
|
12
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
13
|
+
await fn(params);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
export async function sendActiveMessage(streamId, content) {
|
|
17
|
+
await useActiveReplyOnce(streamId, async ({ responseUrl, proxyUrl }) => {
|
|
18
|
+
const res = await wecomFetch(responseUrl, {
|
|
19
|
+
method: "POST",
|
|
20
|
+
headers: { "Content-Type": "application/json" },
|
|
21
|
+
body: JSON.stringify({ msgtype: "text", text: { content } }),
|
|
22
|
+
}, { proxyUrl, timeoutMs: LIMITS.REQUEST_TIMEOUT_MS });
|
|
23
|
+
if (!res.ok) {
|
|
24
|
+
throw new Error(`active send failed: ${res.status}`);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { resolveBotWebhookPaths } from "./inbound.js";
|
|
2
|
+
import { createBotWebhookSessionSnapshot } from "./session.js";
|
|
3
|
+
import { registerWecomWebhookTarget } from "../http/registry.js";
|
|
4
|
+
export function startBotWebhookTransport(params) {
|
|
5
|
+
const paths = resolveBotWebhookPaths(params.account.accountId);
|
|
6
|
+
params.runtime.updateTransportSession(createBotWebhookSessionSnapshot({
|
|
7
|
+
accountId: params.account.accountId,
|
|
8
|
+
running: true,
|
|
9
|
+
}));
|
|
10
|
+
const unregisters = paths.map((path) => registerWecomWebhookTarget({
|
|
11
|
+
account: params.account,
|
|
12
|
+
config: params.cfg,
|
|
13
|
+
runtime: params.runtimeEnv,
|
|
14
|
+
core: params.runtime.core,
|
|
15
|
+
path,
|
|
16
|
+
touchTransportSession: (patch) => params.runtime.touchTransportSession("bot-webhook", patch),
|
|
17
|
+
auditSink: (event) => params.runtime.recordOperationalIssue(event),
|
|
18
|
+
}));
|
|
19
|
+
return {
|
|
20
|
+
paths,
|
|
21
|
+
stop: () => {
|
|
22
|
+
for (const unregister of unregisters) {
|
|
23
|
+
unregister();
|
|
24
|
+
}
|
|
25
|
+
params.runtime.updateTransportSession(createBotWebhookSessionSnapshot({
|
|
26
|
+
accountId: params.account.accountId,
|
|
27
|
+
running: false,
|
|
28
|
+
}));
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|