@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
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { assertLocalMediaAllowed, detectMime, fetchRemoteMedia, } from "openclaw/plugin-sdk/media-runtime";
|
|
5
|
+
const IMAGE_MAX_BYTES = 10 * 1024 * 1024;
|
|
6
|
+
const VIDEO_MAX_BYTES = 10 * 1024 * 1024;
|
|
7
|
+
const VOICE_MAX_BYTES = 2 * 1024 * 1024;
|
|
8
|
+
const FILE_MAX_BYTES = 20 * 1024 * 1024;
|
|
9
|
+
const VOICE_SUPPORTED_MIMES = new Set(["audio/amr"]);
|
|
10
|
+
function detectWeComMediaType(mimeType) {
|
|
11
|
+
const mime = mimeType.toLowerCase();
|
|
12
|
+
if (mime.startsWith("image/"))
|
|
13
|
+
return "image";
|
|
14
|
+
if (mime.startsWith("video/"))
|
|
15
|
+
return "video";
|
|
16
|
+
if (mime.startsWith("audio/") || mime === "application/ogg")
|
|
17
|
+
return "voice";
|
|
18
|
+
return "file";
|
|
19
|
+
}
|
|
20
|
+
function mimeToExtension(mime) {
|
|
21
|
+
const map = {
|
|
22
|
+
"image/jpeg": ".jpg",
|
|
23
|
+
"image/png": ".png",
|
|
24
|
+
"image/gif": ".gif",
|
|
25
|
+
"image/webp": ".webp",
|
|
26
|
+
"image/bmp": ".bmp",
|
|
27
|
+
"image/svg+xml": ".svg",
|
|
28
|
+
"video/mp4": ".mp4",
|
|
29
|
+
"video/quicktime": ".mov",
|
|
30
|
+
"video/x-msvideo": ".avi",
|
|
31
|
+
"video/webm": ".webm",
|
|
32
|
+
"audio/mpeg": ".mp3",
|
|
33
|
+
"audio/ogg": ".ogg",
|
|
34
|
+
"audio/wav": ".wav",
|
|
35
|
+
"audio/amr": ".amr",
|
|
36
|
+
"audio/aac": ".aac",
|
|
37
|
+
"application/pdf": ".pdf",
|
|
38
|
+
"application/zip": ".zip",
|
|
39
|
+
"application/msword": ".doc",
|
|
40
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ".docx",
|
|
41
|
+
"application/vnd.ms-excel": ".xls",
|
|
42
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ".xlsx",
|
|
43
|
+
"text/plain": ".txt",
|
|
44
|
+
};
|
|
45
|
+
return map[mime] || ".bin";
|
|
46
|
+
}
|
|
47
|
+
function extractFileName(mediaUrl, providedFileName, contentType) {
|
|
48
|
+
if (providedFileName)
|
|
49
|
+
return providedFileName;
|
|
50
|
+
try {
|
|
51
|
+
const url = new URL(mediaUrl, "file://");
|
|
52
|
+
const lastPart = url.pathname.split("/").filter(Boolean).pop();
|
|
53
|
+
if (lastPart && lastPart.includes(".")) {
|
|
54
|
+
return decodeURIComponent(lastPart);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
const lastPart = mediaUrl.split("/").filter(Boolean).pop();
|
|
59
|
+
if (lastPart && lastPart.includes(".")) {
|
|
60
|
+
return lastPart;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return `media_${Date.now()}${mimeToExtension(contentType || "application/octet-stream")}`;
|
|
64
|
+
}
|
|
65
|
+
function resolveLocalMediaPath(mediaUrl) {
|
|
66
|
+
if (mediaUrl.startsWith("file://")) {
|
|
67
|
+
return fileURLToPath(mediaUrl);
|
|
68
|
+
}
|
|
69
|
+
return mediaUrl;
|
|
70
|
+
}
|
|
71
|
+
async function loadOutboundMediaFile(params) {
|
|
72
|
+
if (/^https?:\/\//i.test(params.mediaUrl)) {
|
|
73
|
+
return await fetchRemoteMedia({
|
|
74
|
+
url: params.mediaUrl,
|
|
75
|
+
maxBytes: params.maxBytes,
|
|
76
|
+
filePathHint: params.mediaUrl,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const mediaPath = resolveLocalMediaPath(params.mediaUrl);
|
|
80
|
+
await assertLocalMediaAllowed(mediaPath, params.mediaLocalRoots);
|
|
81
|
+
const buffer = await readFile(mediaPath);
|
|
82
|
+
if (buffer.length > params.maxBytes) {
|
|
83
|
+
throw new Error(`Media size ${(buffer.length / (1024 * 1024)).toFixed(2)}MB exceeds max ${(params.maxBytes /
|
|
84
|
+
(1024 * 1024)).toFixed(2)}MB`);
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
buffer,
|
|
88
|
+
fileName: path.basename(mediaPath),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function applyFileSizeLimits(fileSize, detectedType, contentType) {
|
|
92
|
+
const fileSizeMB = (fileSize / (1024 * 1024)).toFixed(2);
|
|
93
|
+
if (fileSize > FILE_MAX_BYTES) {
|
|
94
|
+
return {
|
|
95
|
+
finalType: detectedType,
|
|
96
|
+
shouldReject: true,
|
|
97
|
+
rejectReason: `文件大小 ${fileSizeMB}MB 超过了企业微信允许的最大限制 20MB,无法发送。`,
|
|
98
|
+
downgraded: false,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
switch (detectedType) {
|
|
102
|
+
case "image":
|
|
103
|
+
if (fileSize > IMAGE_MAX_BYTES) {
|
|
104
|
+
return {
|
|
105
|
+
finalType: "file",
|
|
106
|
+
shouldReject: false,
|
|
107
|
+
downgraded: true,
|
|
108
|
+
downgradeNote: `图片大小 ${fileSizeMB}MB 超过 10MB 限制,已转为文件格式发送`,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
case "video":
|
|
113
|
+
if (fileSize > VIDEO_MAX_BYTES) {
|
|
114
|
+
return {
|
|
115
|
+
finalType: "file",
|
|
116
|
+
shouldReject: false,
|
|
117
|
+
downgraded: true,
|
|
118
|
+
downgradeNote: `视频大小 ${fileSizeMB}MB 超过 10MB 限制,已转为文件格式发送`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
case "voice":
|
|
123
|
+
if (contentType && !VOICE_SUPPORTED_MIMES.has(contentType.toLowerCase())) {
|
|
124
|
+
return {
|
|
125
|
+
finalType: "file",
|
|
126
|
+
shouldReject: false,
|
|
127
|
+
downgraded: true,
|
|
128
|
+
downgradeNote: `语音格式 ${contentType} 不支持,企微仅支持 AMR 格式,已转为文件格式发送`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (fileSize > VOICE_MAX_BYTES) {
|
|
132
|
+
return {
|
|
133
|
+
finalType: "file",
|
|
134
|
+
shouldReject: false,
|
|
135
|
+
downgraded: true,
|
|
136
|
+
downgradeNote: `语音大小 ${fileSizeMB}MB 超过 2MB 限制,已转为文件格式发送`,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
break;
|
|
140
|
+
default:
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
return {
|
|
144
|
+
finalType: detectedType,
|
|
145
|
+
shouldReject: false,
|
|
146
|
+
downgraded: false,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
async function resolveMediaFile(mediaUrl, mediaLocalRoots, maxBytes) {
|
|
150
|
+
const result = await loadOutboundMediaFile({
|
|
151
|
+
mediaUrl,
|
|
152
|
+
maxBytes: maxBytes ?? FILE_MAX_BYTES,
|
|
153
|
+
mediaLocalRoots,
|
|
154
|
+
});
|
|
155
|
+
let contentType = result.contentType || "application/octet-stream";
|
|
156
|
+
if (contentType === "application/octet-stream" || contentType === "text/plain") {
|
|
157
|
+
const detected = await detectMime({
|
|
158
|
+
buffer: result.buffer,
|
|
159
|
+
filePath: result.fileName ?? mediaUrl,
|
|
160
|
+
});
|
|
161
|
+
if (detected) {
|
|
162
|
+
contentType = detected;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
buffer: result.buffer,
|
|
167
|
+
contentType,
|
|
168
|
+
fileName: extractFileName(mediaUrl, result.fileName, contentType),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
export async function uploadAndSendBotWsMedia(params) {
|
|
172
|
+
try {
|
|
173
|
+
const media = await resolveMediaFile(params.mediaUrl, params.mediaLocalRoots, params.maxBytes);
|
|
174
|
+
const detectedType = detectWeComMediaType(media.contentType);
|
|
175
|
+
const sizeCheck = applyFileSizeLimits(media.buffer.length, detectedType, media.contentType);
|
|
176
|
+
if (sizeCheck.shouldReject) {
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
rejected: true,
|
|
180
|
+
rejectReason: sizeCheck.rejectReason,
|
|
181
|
+
finalType: sizeCheck.finalType,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const uploadResult = await params.wsClient.uploadMedia(media.buffer, {
|
|
185
|
+
type: sizeCheck.finalType,
|
|
186
|
+
filename: media.fileName,
|
|
187
|
+
});
|
|
188
|
+
const sendResult = await params.wsClient.sendMediaMessage(params.chatId, sizeCheck.finalType, uploadResult.media_id);
|
|
189
|
+
return {
|
|
190
|
+
ok: true,
|
|
191
|
+
messageId: sendResult?.headers?.req_id ?? `wecom-media-${Date.now()}`,
|
|
192
|
+
finalType: sizeCheck.finalType,
|
|
193
|
+
downgraded: sizeCheck.downgraded,
|
|
194
|
+
downgradeNote: sizeCheck.downgradeNote,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
return {
|
|
199
|
+
ok: false,
|
|
200
|
+
error: error instanceof Error ? error.message : String(error),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export async function uploadAndReplyBotWsMedia(params) {
|
|
205
|
+
try {
|
|
206
|
+
const media = await resolveMediaFile(params.mediaUrl, params.mediaLocalRoots, params.maxBytes);
|
|
207
|
+
const detectedType = detectWeComMediaType(media.contentType);
|
|
208
|
+
const sizeCheck = applyFileSizeLimits(media.buffer.length, detectedType, media.contentType);
|
|
209
|
+
if (sizeCheck.shouldReject) {
|
|
210
|
+
return {
|
|
211
|
+
ok: false,
|
|
212
|
+
rejected: true,
|
|
213
|
+
rejectReason: sizeCheck.rejectReason,
|
|
214
|
+
finalType: sizeCheck.finalType,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const uploadResult = await params.wsClient.uploadMedia(media.buffer, {
|
|
218
|
+
type: sizeCheck.finalType,
|
|
219
|
+
filename: media.fileName,
|
|
220
|
+
});
|
|
221
|
+
const replyResult = await params.wsClient.replyMedia(params.frame, sizeCheck.finalType, uploadResult.media_id);
|
|
222
|
+
return {
|
|
223
|
+
ok: true,
|
|
224
|
+
messageId: replyResult?.headers?.req_id ?? `wecom-reply-media-${Date.now()}`,
|
|
225
|
+
finalType: sizeCheck.finalType,
|
|
226
|
+
downgraded: sizeCheck.downgraded,
|
|
227
|
+
downgradeNote: sizeCheck.downgradeNote,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
catch (error) {
|
|
231
|
+
return {
|
|
232
|
+
ok: false,
|
|
233
|
+
error: error instanceof Error ? error.message : String(error),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { generateReqId, } from "@wecom/aibot-node-sdk";
|
|
2
|
+
import { formatErrorMessage } from "openclaw/plugin-sdk/infra-runtime";
|
|
3
|
+
import { resolveWecomMediaMaxBytes, resolveWecomMergedMediaLocalRoots } from "../../config/index.js";
|
|
4
|
+
import { getWecomRuntime } from "../../runtime.js";
|
|
5
|
+
import { toWeComMarkdownV2 } from "../../wecom_msg_adapter/markdown_adapter.js";
|
|
6
|
+
import { uploadAndSendBotWsMedia } from "./media.js";
|
|
7
|
+
const PLACEHOLDER_KEEPALIVE_MS = 3000;
|
|
8
|
+
const MAX_KEEPALIVE_MS = 120 * 1000; // Force stop keepalive after 120s if ignored
|
|
9
|
+
function isInvalidReqIdError(error) {
|
|
10
|
+
if (!error || typeof error !== "object") {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const errcode = "errcode" in error ? Number(error.errcode) : undefined;
|
|
14
|
+
const errmsg = "errmsg" in error ? String(error.errmsg ?? "") : "";
|
|
15
|
+
return errcode === 846605 || errmsg.includes("invalid req_id");
|
|
16
|
+
}
|
|
17
|
+
function isExpiredStreamUpdateError(error) {
|
|
18
|
+
if (!error || typeof error !== "object") {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
const errcode = "errcode" in error ? Number(error.errcode) : undefined;
|
|
22
|
+
const errmsg = "errmsg" in error ? String(error.errmsg ?? "").toLowerCase() : "";
|
|
23
|
+
return errcode === 846608 || errmsg.includes("stream message update expired");
|
|
24
|
+
}
|
|
25
|
+
/** SDK rejects with a plain Error whose message contains "ack timeout" when
|
|
26
|
+
* the WeCom server does not acknowledge a reply within 5 s. Once timed out
|
|
27
|
+
* the reqId slot is released; further replies on the same reqId will fail. */
|
|
28
|
+
function isAckTimeoutError(error) {
|
|
29
|
+
return error instanceof Error && error.message.includes("ack timeout");
|
|
30
|
+
}
|
|
31
|
+
function isTerminalReplyError(error) {
|
|
32
|
+
return (isInvalidReqIdError(error) || isExpiredStreamUpdateError(error) || isAckTimeoutError(error));
|
|
33
|
+
}
|
|
34
|
+
function formatMediaFailure(mediaUrl, error, rejectReason) {
|
|
35
|
+
const reason = rejectReason || error || "unknown";
|
|
36
|
+
return `媒体发送失败:${mediaUrl} (${reason})`;
|
|
37
|
+
}
|
|
38
|
+
const activeKeepalivesByPeer = new Map();
|
|
39
|
+
export function createBotWsReplyHandle(params) {
|
|
40
|
+
let streamId;
|
|
41
|
+
let accumulatedText = "";
|
|
42
|
+
let deferredMediaUrls = [];
|
|
43
|
+
const resolveStreamId = () => {
|
|
44
|
+
streamId ||= generateReqId("stream");
|
|
45
|
+
return streamId;
|
|
46
|
+
};
|
|
47
|
+
const placeholderText = params.placeholderContent?.trim() || "⏳ 正在思考中...\n\n";
|
|
48
|
+
let streamSettled = false;
|
|
49
|
+
let placeholderInFlight = false;
|
|
50
|
+
let placeholderKeepalive;
|
|
51
|
+
let placeholderTimeout;
|
|
52
|
+
// Extract peerId for clustering handles
|
|
53
|
+
const body = params.frame.body;
|
|
54
|
+
const peerId = String((body?.chattype === "group" ? body?.chatid || body?.from?.userid : body?.from?.userid) ||
|
|
55
|
+
"unknown");
|
|
56
|
+
const reqId = params.frame.headers.req_id || "unknown";
|
|
57
|
+
const isEvent = params.inboundKind === "welcome" ||
|
|
58
|
+
params.inboundKind === "event" ||
|
|
59
|
+
params.inboundKind === "template-card-event";
|
|
60
|
+
const stopPlaceholderKeepalive = () => {
|
|
61
|
+
if (placeholderKeepalive) {
|
|
62
|
+
clearInterval(placeholderKeepalive);
|
|
63
|
+
placeholderKeepalive = undefined;
|
|
64
|
+
}
|
|
65
|
+
if (placeholderTimeout) {
|
|
66
|
+
clearTimeout(placeholderTimeout);
|
|
67
|
+
placeholderTimeout = undefined;
|
|
68
|
+
}
|
|
69
|
+
// Remove from registry
|
|
70
|
+
const keepalives = activeKeepalivesByPeer.get(peerId);
|
|
71
|
+
if (keepalives) {
|
|
72
|
+
for (const ka of keepalives) {
|
|
73
|
+
if (ka.reqId === reqId) {
|
|
74
|
+
keepalives.delete(ka);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (keepalives.size === 0) {
|
|
78
|
+
activeKeepalivesByPeer.delete(peerId);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
const settleStream = () => {
|
|
83
|
+
if (streamSettled)
|
|
84
|
+
return;
|
|
85
|
+
streamSettled = true;
|
|
86
|
+
stopPlaceholderKeepalive();
|
|
87
|
+
};
|
|
88
|
+
const sendPlaceholder = () => {
|
|
89
|
+
if (streamSettled || placeholderInFlight || isEvent)
|
|
90
|
+
return;
|
|
91
|
+
placeholderInFlight = true;
|
|
92
|
+
params.client
|
|
93
|
+
.replyStream(params.frame, resolveStreamId(), placeholderText, false)
|
|
94
|
+
.catch((error) => {
|
|
95
|
+
if (!isTerminalReplyError(error)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
settleStream();
|
|
99
|
+
params.onFail?.(error);
|
|
100
|
+
})
|
|
101
|
+
.finally(() => {
|
|
102
|
+
placeholderInFlight = false;
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
const notifyPeerActive = () => {
|
|
106
|
+
// A genuine reply or reasoning is happening on THIS handle.
|
|
107
|
+
// It means the core SDK has chosen this handle to deliver the response.
|
|
108
|
+
// We can safely terminate all other orphaned keepalives for this peer to prevent infinite loops.
|
|
109
|
+
const keepalives = activeKeepalivesByPeer.get(peerId);
|
|
110
|
+
if (keepalives) {
|
|
111
|
+
for (const ka of keepalives) {
|
|
112
|
+
if (ka.reqId !== reqId) {
|
|
113
|
+
ka.stop();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const mergeDeferredMediaUrls = (urls) => {
|
|
119
|
+
if (urls.length === 0) {
|
|
120
|
+
return deferredMediaUrls;
|
|
121
|
+
}
|
|
122
|
+
const merged = [...deferredMediaUrls];
|
|
123
|
+
for (const url of urls) {
|
|
124
|
+
if (!merged.includes(url)) {
|
|
125
|
+
merged.push(url);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
deferredMediaUrls = merged;
|
|
129
|
+
return deferredMediaUrls;
|
|
130
|
+
};
|
|
131
|
+
if (params.autoSendPlaceholder !== false && !isEvent) {
|
|
132
|
+
sendPlaceholder();
|
|
133
|
+
placeholderKeepalive = setInterval(() => {
|
|
134
|
+
sendPlaceholder();
|
|
135
|
+
}, PLACEHOLDER_KEEPALIVE_MS);
|
|
136
|
+
// Safety net: force stop keepalive after MAX_KEEPALIVE_MS
|
|
137
|
+
// in case the message is completely ignored by the core and never triggers deliver/fail
|
|
138
|
+
placeholderTimeout = setTimeout(() => {
|
|
139
|
+
stopPlaceholderKeepalive();
|
|
140
|
+
}, MAX_KEEPALIVE_MS);
|
|
141
|
+
// Register keepalive
|
|
142
|
+
let keepalives = activeKeepalivesByPeer.get(peerId);
|
|
143
|
+
if (!keepalives) {
|
|
144
|
+
keepalives = new Set();
|
|
145
|
+
activeKeepalivesByPeer.set(peerId, keepalives);
|
|
146
|
+
}
|
|
147
|
+
keepalives.add({ reqId, stop: stopPlaceholderKeepalive });
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
context: {
|
|
151
|
+
transport: "bot-ws",
|
|
152
|
+
accountId: params.accountId,
|
|
153
|
+
reqId: params.frame.headers.req_id,
|
|
154
|
+
raw: {
|
|
155
|
+
transport: "bot-ws",
|
|
156
|
+
command: params.frame.cmd,
|
|
157
|
+
headers: params.frame.headers,
|
|
158
|
+
body: params.frame.body,
|
|
159
|
+
envelopeType: "ws",
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
deliver: async (payload, info) => {
|
|
163
|
+
// Mark this chat as active on this handle
|
|
164
|
+
notifyPeerActive();
|
|
165
|
+
if (payload.isReasoning) {
|
|
166
|
+
// We reset the safety timeout if reasoning is actively streaming
|
|
167
|
+
if (placeholderTimeout && !isEvent) {
|
|
168
|
+
clearTimeout(placeholderTimeout);
|
|
169
|
+
placeholderTimeout = setTimeout(() => {
|
|
170
|
+
stopPlaceholderKeepalive();
|
|
171
|
+
}, MAX_KEEPALIVE_MS);
|
|
172
|
+
}
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const text = payload.text?.trim() || "";
|
|
176
|
+
const incomingMediaUrls = payload.mediaUrls || (payload.mediaUrl ? [payload.mediaUrl] : []);
|
|
177
|
+
const hasIncomingMedia = incomingMediaUrls.length > 0;
|
|
178
|
+
if (info.kind !== "final" && hasIncomingMedia) {
|
|
179
|
+
mergeDeferredMediaUrls(incomingMediaUrls);
|
|
180
|
+
}
|
|
181
|
+
const mediaUrls = info.kind === "final" ? mergeDeferredMediaUrls(incomingMediaUrls) : incomingMediaUrls;
|
|
182
|
+
if (!text && mediaUrls.length === 0) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (info.kind === "block") {
|
|
186
|
+
if (!text) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
accumulatedText = accumulatedText ? `${accumulatedText}\n${text}` : text;
|
|
190
|
+
}
|
|
191
|
+
const outboundText = info.kind === "final"
|
|
192
|
+
? accumulatedText
|
|
193
|
+
? text
|
|
194
|
+
? `${accumulatedText}\n${text}`
|
|
195
|
+
: accumulatedText
|
|
196
|
+
: text
|
|
197
|
+
: accumulatedText || text;
|
|
198
|
+
let finalText = outboundText;
|
|
199
|
+
if (info.kind === "final" && mediaUrls.length > 0) {
|
|
200
|
+
const cfg = getWecomRuntime().config.loadConfig();
|
|
201
|
+
const mediaLocalRoots = resolveWecomMergedMediaLocalRoots({ cfg });
|
|
202
|
+
const mediaMaxBytes = resolveWecomMediaMaxBytes(cfg, params.accountId);
|
|
203
|
+
const mediaFailures = [];
|
|
204
|
+
const mediaNotes = [];
|
|
205
|
+
let mediaSent = 0;
|
|
206
|
+
for (const mediaUrl of mediaUrls) {
|
|
207
|
+
const result = await uploadAndSendBotWsMedia({
|
|
208
|
+
wsClient: params.client,
|
|
209
|
+
chatId: peerId,
|
|
210
|
+
mediaUrl,
|
|
211
|
+
mediaLocalRoots,
|
|
212
|
+
maxBytes: mediaMaxBytes,
|
|
213
|
+
});
|
|
214
|
+
if (result.ok) {
|
|
215
|
+
mediaSent += 1;
|
|
216
|
+
if (result.downgradeNote) {
|
|
217
|
+
mediaNotes.push(result.downgradeNote);
|
|
218
|
+
}
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
mediaFailures.push(formatMediaFailure(mediaUrl, result.error, result.rejectReason));
|
|
222
|
+
}
|
|
223
|
+
if (!finalText && mediaSent > 0) {
|
|
224
|
+
finalText = "文件已发送。";
|
|
225
|
+
}
|
|
226
|
+
if (mediaFailures.length > 0) {
|
|
227
|
+
finalText = finalText
|
|
228
|
+
? `${finalText}\n\n${mediaFailures.join("\n")}`
|
|
229
|
+
: mediaFailures.join("\n");
|
|
230
|
+
}
|
|
231
|
+
if (mediaNotes.length > 0) {
|
|
232
|
+
finalText = finalText
|
|
233
|
+
? `${finalText}\n\n${mediaNotes.join("\n")}`
|
|
234
|
+
: mediaNotes.join("\n");
|
|
235
|
+
}
|
|
236
|
+
deferredMediaUrls = [];
|
|
237
|
+
}
|
|
238
|
+
if (!finalText) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
// Event frames do not support streaming chunks
|
|
242
|
+
if (isEvent && info.kind !== "final") {
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
settleStream();
|
|
246
|
+
try {
|
|
247
|
+
if (params.inboundKind === "welcome") {
|
|
248
|
+
await params.client.replyWelcome(params.frame, {
|
|
249
|
+
msgtype: "text",
|
|
250
|
+
text: { content: finalText },
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
else if (isEvent) {
|
|
254
|
+
// Send push message for other events
|
|
255
|
+
await params.client.sendMessage(peerId, {
|
|
256
|
+
msgtype: "markdown",
|
|
257
|
+
markdown: { content: toWeComMarkdownV2(finalText) },
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
await params.client.replyStream(params.frame, resolveStreamId(), toWeComMarkdownV2(finalText), info.kind === "final");
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
if (isTerminalReplyError(error)) {
|
|
266
|
+
params.onFail?.(error);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
throw error;
|
|
270
|
+
}
|
|
271
|
+
params.onDeliver?.();
|
|
272
|
+
},
|
|
273
|
+
fail: async (error) => {
|
|
274
|
+
notifyPeerActive();
|
|
275
|
+
settleStream();
|
|
276
|
+
if (isTerminalReplyError(error)) {
|
|
277
|
+
params.onFail?.(error);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
const message = formatErrorMessage(error);
|
|
281
|
+
const text = `WeCom WS reply failed: ${message}`;
|
|
282
|
+
try {
|
|
283
|
+
if (params.inboundKind === "welcome") {
|
|
284
|
+
await params.client.replyWelcome(params.frame, {
|
|
285
|
+
msgtype: "text",
|
|
286
|
+
text: { content: text },
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
else if (isEvent) {
|
|
290
|
+
await params.client.sendMessage(peerId, {
|
|
291
|
+
msgtype: "markdown",
|
|
292
|
+
markdown: { content: text },
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
await params.client.replyStream(params.frame, resolveStreamId(), text, true);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
catch (sendError) {
|
|
300
|
+
params.onFail?.(sendError);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
params.onFail?.(error);
|
|
304
|
+
},
|
|
305
|
+
markExternalActivity: () => {
|
|
306
|
+
notifyPeerActive();
|
|
307
|
+
stopPlaceholderKeepalive();
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
}
|