@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
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
|
|
3
|
-
import { wecomFetch } from "../../http.js";
|
|
4
|
-
import { LIMITS, type StreamStore } from "../../monitor/state.js";
|
|
5
|
-
import { getActiveReplyUrl, useActiveReplyOnce } from "../../transport/bot-webhook/active-reply.js";
|
|
6
|
-
import type { WecomWebhookTarget } from "../../types/runtime-context.js";
|
|
7
|
-
import { buildStreamReplyFromState } from "../../transport/bot-webhook/protocol.js";
|
|
8
|
-
import {
|
|
9
|
-
buildFallbackPrompt,
|
|
10
|
-
extractLocalFilePathsFromText,
|
|
11
|
-
guessLocalPathContentType,
|
|
12
|
-
resolveAgentAccountOrUndefined,
|
|
13
|
-
sendAgentDmMedia,
|
|
14
|
-
sendBotFallbackPromptNow,
|
|
15
|
-
} from "./fallback-delivery.js";
|
|
16
|
-
import type { BotRuntimeLogger } from "./types.js";
|
|
17
|
-
|
|
18
|
-
export async function handleDirectLocalPathIntent(params: {
|
|
19
|
-
streamStore: StreamStore;
|
|
20
|
-
target: WecomWebhookTarget;
|
|
21
|
-
streamId: string;
|
|
22
|
-
rawBody: string;
|
|
23
|
-
userId: string;
|
|
24
|
-
chatType: "group" | "direct";
|
|
25
|
-
logVerbose: BotRuntimeLogger;
|
|
26
|
-
looksLikeSendLocalFileIntent: (rawBody: string) => boolean;
|
|
27
|
-
}): Promise<boolean> {
|
|
28
|
-
const { streamStore, target, streamId, rawBody, userId, chatType, logVerbose, looksLikeSendLocalFileIntent } = params;
|
|
29
|
-
const directLocalPaths = extractLocalFilePathsFromText(rawBody);
|
|
30
|
-
if (directLocalPaths.length) {
|
|
31
|
-
logVerbose(
|
|
32
|
-
target,
|
|
33
|
-
`local-path: 检测到用户消息包含本机路径 count=${directLocalPaths.length} intent=${looksLikeSendLocalFileIntent(rawBody)}`,
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
if (!directLocalPaths.length || !looksLikeSendLocalFileIntent(rawBody)) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const fs = await import("node:fs/promises");
|
|
41
|
-
const pathModule = await import("node:path");
|
|
42
|
-
const imageExts = new Set(["png", "jpg", "jpeg", "gif", "webp", "bmp"]);
|
|
43
|
-
|
|
44
|
-
const imagePaths: string[] = [];
|
|
45
|
-
const otherPaths: string[] = [];
|
|
46
|
-
for (const p of directLocalPaths) {
|
|
47
|
-
const ext = pathModule.extname(p).slice(1).toLowerCase();
|
|
48
|
-
if (imageExts.has(ext)) imagePaths.push(p);
|
|
49
|
-
else otherPaths.push(p);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (imagePaths.length > 0 && otherPaths.length === 0) {
|
|
53
|
-
const loaded: Array<{ base64: string; md5: string; path: string }> = [];
|
|
54
|
-
for (const p of imagePaths) {
|
|
55
|
-
try {
|
|
56
|
-
const buf = await fs.readFile(p);
|
|
57
|
-
const base64 = buf.toString("base64");
|
|
58
|
-
const md5 = crypto.createHash("md5").update(buf).digest("hex");
|
|
59
|
-
loaded.push({ base64, md5, path: p });
|
|
60
|
-
} catch (err) {
|
|
61
|
-
target.runtime.error?.(`local-path: 读取图片失败 path=${p}: ${String(err)}`);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (loaded.length > 0) {
|
|
66
|
-
streamStore.updateStream(streamId, (s) => {
|
|
67
|
-
s.images = loaded.map(({ base64, md5 }) => ({ base64, md5 }));
|
|
68
|
-
s.content = loaded.length === 1 ? `已发送图片(${pathModule.basename(loaded[0]!.path)})` : `已发送 ${loaded.length} 张图片`;
|
|
69
|
-
s.finished = true;
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const responseUrl = getActiveReplyUrl(streamId);
|
|
73
|
-
if (responseUrl) {
|
|
74
|
-
try {
|
|
75
|
-
const finalReply = buildStreamReplyFromState(streamStore.getStream(streamId)!) as unknown as Record<string, unknown>;
|
|
76
|
-
await useActiveReplyOnce(streamId, async ({ responseUrl, proxyUrl }) => {
|
|
77
|
-
const res = await wecomFetch(
|
|
78
|
-
responseUrl,
|
|
79
|
-
{
|
|
80
|
-
method: "POST",
|
|
81
|
-
headers: { "Content-Type": "application/json" },
|
|
82
|
-
body: JSON.stringify(finalReply),
|
|
83
|
-
},
|
|
84
|
-
{ proxyUrl, timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
|
|
85
|
-
);
|
|
86
|
-
if (!res.ok) throw new Error(`local-path image push failed: ${res.status}`);
|
|
87
|
-
});
|
|
88
|
-
logVerbose(target, `local-path: 已通过 Bot response_url 推送图片 frames=final images=${loaded.length}`);
|
|
89
|
-
} catch (err) {
|
|
90
|
-
target.runtime.error?.(`local-path: Bot 主动推送图片失败(将依赖 stream_refresh 拉取): ${String(err)}`);
|
|
91
|
-
}
|
|
92
|
-
} else {
|
|
93
|
-
logVerbose(target, "local-path: 无 response_url,等待 stream_refresh 拉取最终图片");
|
|
94
|
-
}
|
|
95
|
-
streamStore.onStreamFinished(streamId);
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const agentCfg = resolveAgentAccountOrUndefined(target.config, target.account.accountId);
|
|
100
|
-
const agentOk = Boolean(agentCfg);
|
|
101
|
-
const fallbackName = imagePaths.length === 1 ? (imagePaths[0]!.split("/").pop() || "image") : `${imagePaths.length} 张图片`;
|
|
102
|
-
const prompt = buildFallbackPrompt({
|
|
103
|
-
kind: "media",
|
|
104
|
-
agentConfigured: agentOk,
|
|
105
|
-
userId,
|
|
106
|
-
filename: fallbackName,
|
|
107
|
-
chatType,
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
streamStore.updateStream(streamId, (s) => {
|
|
111
|
-
s.fallbackMode = "error";
|
|
112
|
-
s.finished = true;
|
|
113
|
-
s.content = prompt;
|
|
114
|
-
s.fallbackPromptSentAt = s.fallbackPromptSentAt ?? Date.now();
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
try {
|
|
118
|
-
await sendBotFallbackPromptNow({ streamId, text: prompt });
|
|
119
|
-
logVerbose(target, "local-path: 图片读取失败后已推送兜底提示");
|
|
120
|
-
} catch (err) {
|
|
121
|
-
target.runtime.error?.(`local-path: 图片读取失败后的兜底提示推送失败: ${String(err)}`);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (agentCfg && userId && userId !== "unknown") {
|
|
125
|
-
for (const p of imagePaths) {
|
|
126
|
-
const guessedType = guessLocalPathContentType(p);
|
|
127
|
-
try {
|
|
128
|
-
await sendAgentDmMedia({
|
|
129
|
-
agent: agentCfg,
|
|
130
|
-
userId,
|
|
131
|
-
mediaUrlOrPath: p,
|
|
132
|
-
contentType: guessedType,
|
|
133
|
-
filename: p.split("/").pop() || "image",
|
|
134
|
-
});
|
|
135
|
-
streamStore.updateStream(streamId, (s) => {
|
|
136
|
-
s.agentMediaKeys = Array.from(new Set([...(s.agentMediaKeys ?? []), p]));
|
|
137
|
-
});
|
|
138
|
-
logVerbose(
|
|
139
|
-
target,
|
|
140
|
-
`local-path: 图片已通过 Agent 私信发送 user=${userId} path=${p} contentType=${guessedType ?? "unknown"}`,
|
|
141
|
-
);
|
|
142
|
-
} catch (err) {
|
|
143
|
-
target.runtime.error?.(`local-path: 图片 Agent 私信兜底失败 path=${p}: ${String(err)}`);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
streamStore.onStreamFinished(streamId);
|
|
148
|
-
return true;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (otherPaths.length > 0) {
|
|
152
|
-
const agentCfg = resolveAgentAccountOrUndefined(target.config, target.account.accountId);
|
|
153
|
-
const agentOk = Boolean(agentCfg);
|
|
154
|
-
const filename = otherPaths.length === 1 ? otherPaths[0]!.split("/").pop()! : `${otherPaths.length} 个文件`;
|
|
155
|
-
const prompt = buildFallbackPrompt({
|
|
156
|
-
kind: "media",
|
|
157
|
-
agentConfigured: agentOk,
|
|
158
|
-
userId,
|
|
159
|
-
filename,
|
|
160
|
-
chatType,
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
streamStore.updateStream(streamId, (s) => {
|
|
164
|
-
s.fallbackMode = "media";
|
|
165
|
-
s.finished = true;
|
|
166
|
-
s.content = prompt;
|
|
167
|
-
s.fallbackPromptSentAt = s.fallbackPromptSentAt ?? Date.now();
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
try {
|
|
171
|
-
await sendBotFallbackPromptNow({ streamId, text: prompt });
|
|
172
|
-
logVerbose(target, "local-path: 文件兜底提示已推送");
|
|
173
|
-
} catch (err) {
|
|
174
|
-
target.runtime.error?.(`local-path: 文件兜底提示推送失败: ${String(err)}`);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
if (!agentCfg) {
|
|
178
|
-
streamStore.onStreamFinished(streamId);
|
|
179
|
-
return true;
|
|
180
|
-
}
|
|
181
|
-
if (!userId || userId === "unknown") {
|
|
182
|
-
target.runtime.error?.("local-path: 无法识别触发者 userId,无法 Agent 私信发送文件");
|
|
183
|
-
streamStore.onStreamFinished(streamId);
|
|
184
|
-
return true;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
for (const p of otherPaths) {
|
|
188
|
-
const alreadySent = streamStore.getStream(streamId)?.agentMediaKeys?.includes(p);
|
|
189
|
-
if (alreadySent) continue;
|
|
190
|
-
const guessedType = guessLocalPathContentType(p);
|
|
191
|
-
try {
|
|
192
|
-
await sendAgentDmMedia({
|
|
193
|
-
agent: agentCfg,
|
|
194
|
-
userId,
|
|
195
|
-
mediaUrlOrPath: p,
|
|
196
|
-
contentType: guessedType,
|
|
197
|
-
filename: p.split("/").pop() || "file",
|
|
198
|
-
});
|
|
199
|
-
streamStore.updateStream(streamId, (s) => {
|
|
200
|
-
s.agentMediaKeys = Array.from(new Set([...(s.agentMediaKeys ?? []), p]));
|
|
201
|
-
});
|
|
202
|
-
logVerbose(
|
|
203
|
-
target,
|
|
204
|
-
`local-path: 文件已通过 Agent 私信发送 user=${userId} path=${p} contentType=${guessedType ?? "unknown"}`,
|
|
205
|
-
);
|
|
206
|
-
} catch (err) {
|
|
207
|
-
target.runtime.error?.(`local-path: Agent 私信发送文件失败 path=${p}: ${String(err)}`);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
streamStore.onStreamFinished(streamId);
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
3
|
-
|
|
4
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
-
|
|
6
|
-
import { stageWecomInboundMediaForSession } from "./sandbox-media.js";
|
|
7
|
-
|
|
8
|
-
describe("stageWecomInboundMediaForSession", () => {
|
|
9
|
-
const root = path.join("/tmp", `wecom-sandbox-stage-${process.pid}`);
|
|
10
|
-
|
|
11
|
-
beforeEach(async () => {
|
|
12
|
-
await mkdir(root, { recursive: true });
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
afterEach(async () => {
|
|
16
|
-
vi.unstubAllEnvs();
|
|
17
|
-
await rm(root, { recursive: true, force: true });
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("stages inbound media into the session sandbox workspace using channels.wecom.mediaMaxMb", async () => {
|
|
21
|
-
const mediaPath = path.join(root, "openclaw-media", "inbound", "big.bin");
|
|
22
|
-
const agentWorkspace = path.join(root, "agent-workspace");
|
|
23
|
-
const sandboxRoot = path.join(root, "sandboxes");
|
|
24
|
-
|
|
25
|
-
await mkdir(path.dirname(mediaPath), { recursive: true });
|
|
26
|
-
await mkdir(agentWorkspace, { recursive: true });
|
|
27
|
-
await writeFile(mediaPath, Buffer.alloc(6 * 1024 * 1024, 7));
|
|
28
|
-
|
|
29
|
-
const staged = await stageWecomInboundMediaForSession({
|
|
30
|
-
cfg: {
|
|
31
|
-
channels: {
|
|
32
|
-
wecom: {
|
|
33
|
-
mediaMaxMb: 8,
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
agents: {
|
|
37
|
-
list: [
|
|
38
|
-
{
|
|
39
|
-
id: "ops",
|
|
40
|
-
workspace: agentWorkspace,
|
|
41
|
-
sandbox: {
|
|
42
|
-
mode: "non-main",
|
|
43
|
-
scope: "session",
|
|
44
|
-
workspaceRoot: sandboxRoot,
|
|
45
|
-
workspaceAccess: "ro",
|
|
46
|
-
docker: {
|
|
47
|
-
workdir: "/workspace",
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
},
|
|
53
|
-
} as any,
|
|
54
|
-
accountId: "default",
|
|
55
|
-
agentId: "ops",
|
|
56
|
-
sessionKey: "agent:ops:wecom:default:dm:zhangsan",
|
|
57
|
-
mediaPath,
|
|
58
|
-
filename: "big.bin",
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
expect(staged).toMatch(/^media\/inbound\/big\.bin$/);
|
|
62
|
-
const sandboxEntries = await readdir(sandboxRoot);
|
|
63
|
-
const stagedBuffer = await readFile(
|
|
64
|
-
path.join(sandboxRoot, sandboxEntries[0]!, "media", "inbound", "big.bin"),
|
|
65
|
-
);
|
|
66
|
-
expect(stagedBuffer.byteLength).toBe(6 * 1024 * 1024);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("uses the default sandbox workspace root when workspaceRoot is omitted", async () => {
|
|
70
|
-
vi.stubEnv("OPENCLAW_STATE_DIR", root);
|
|
71
|
-
const mediaPath = path.join(root, "openclaw-media", "inbound", "default-root.bin");
|
|
72
|
-
const agentWorkspace = path.join(root, "agent-workspace");
|
|
73
|
-
|
|
74
|
-
await mkdir(path.dirname(mediaPath), { recursive: true });
|
|
75
|
-
await mkdir(agentWorkspace, { recursive: true });
|
|
76
|
-
await writeFile(mediaPath, Buffer.alloc(2 * 1024 * 1024, 5));
|
|
77
|
-
|
|
78
|
-
const staged = await stageWecomInboundMediaForSession({
|
|
79
|
-
cfg: {
|
|
80
|
-
channels: {
|
|
81
|
-
wecom: {
|
|
82
|
-
mediaMaxMb: 8,
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
agents: {
|
|
86
|
-
list: [
|
|
87
|
-
{
|
|
88
|
-
id: "ops",
|
|
89
|
-
workspace: agentWorkspace,
|
|
90
|
-
sandbox: {
|
|
91
|
-
mode: "non-main",
|
|
92
|
-
scope: "session",
|
|
93
|
-
workspaceAccess: "ro",
|
|
94
|
-
docker: {
|
|
95
|
-
workdir: "/workspace",
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
],
|
|
100
|
-
},
|
|
101
|
-
} as any,
|
|
102
|
-
accountId: "default",
|
|
103
|
-
agentId: "ops",
|
|
104
|
-
sessionKey: "agent:ops:wecom:default:dm:lisi",
|
|
105
|
-
mediaPath,
|
|
106
|
-
filename: "default-root.bin",
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
expect(staged).toMatch(/^media\/inbound\/default-root\.bin$/);
|
|
110
|
-
const sandboxEntries = await readdir(path.join(root, "sandboxes"));
|
|
111
|
-
const stagedBuffer = await readFile(
|
|
112
|
-
path.join(root, "sandboxes", sandboxEntries[0]!, "media", "inbound", "default-root.bin"),
|
|
113
|
-
);
|
|
114
|
-
expect(stagedBuffer.byteLength).toBe(2 * 1024 * 1024);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it("stages inbound media into the agent workspace for non-sandbox sessions", async () => {
|
|
118
|
-
const mediaPath = path.join(root, "openclaw-media", "inbound", "small.bin");
|
|
119
|
-
const agentWorkspace = path.join(root, "agent-workspace");
|
|
120
|
-
|
|
121
|
-
await mkdir(path.dirname(mediaPath), { recursive: true });
|
|
122
|
-
await mkdir(agentWorkspace, { recursive: true });
|
|
123
|
-
await writeFile(mediaPath, Buffer.alloc(1024, 1));
|
|
124
|
-
|
|
125
|
-
const staged = await stageWecomInboundMediaForSession({
|
|
126
|
-
cfg: {
|
|
127
|
-
channels: {
|
|
128
|
-
wecom: {
|
|
129
|
-
mediaMaxMb: 8,
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
agents: {
|
|
133
|
-
list: [
|
|
134
|
-
{
|
|
135
|
-
id: "ops",
|
|
136
|
-
workspace: agentWorkspace,
|
|
137
|
-
sandbox: {
|
|
138
|
-
mode: "off",
|
|
139
|
-
scope: "session",
|
|
140
|
-
workspaceRoot: path.join(root, "sandboxes"),
|
|
141
|
-
workspaceAccess: "ro",
|
|
142
|
-
docker: {
|
|
143
|
-
workdir: "/workspace",
|
|
144
|
-
},
|
|
145
|
-
},
|
|
146
|
-
},
|
|
147
|
-
],
|
|
148
|
-
},
|
|
149
|
-
} as any,
|
|
150
|
-
accountId: "default",
|
|
151
|
-
agentId: "ops",
|
|
152
|
-
sessionKey: "agent:ops:wecom:default:dm:zhangsan",
|
|
153
|
-
mediaPath,
|
|
154
|
-
filename: "small.bin",
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
expect(staged).toBe(path.join(agentWorkspace, "media", "inbound", "small.bin"));
|
|
158
|
-
const stagedBuffer = await readFile(staged);
|
|
159
|
-
expect(stagedBuffer.byteLength).toBe(1024);
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it("allocates distinct staged filenames for concurrent same-name uploads", async () => {
|
|
163
|
-
const mediaPathA = path.join(root, "openclaw-media", "inbound", "dup-a.bin");
|
|
164
|
-
const mediaPathB = path.join(root, "openclaw-media", "inbound", "dup-b.bin");
|
|
165
|
-
const agentWorkspace = path.join(root, "agent-workspace");
|
|
166
|
-
|
|
167
|
-
await mkdir(path.dirname(mediaPathA), { recursive: true });
|
|
168
|
-
await mkdir(agentWorkspace, { recursive: true });
|
|
169
|
-
await writeFile(mediaPathA, Buffer.from("first"));
|
|
170
|
-
await writeFile(mediaPathB, Buffer.from("second"));
|
|
171
|
-
|
|
172
|
-
const cfg = {
|
|
173
|
-
channels: {
|
|
174
|
-
wecom: {
|
|
175
|
-
mediaMaxMb: 8,
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
agents: {
|
|
179
|
-
list: [
|
|
180
|
-
{
|
|
181
|
-
id: "ops",
|
|
182
|
-
workspace: agentWorkspace,
|
|
183
|
-
sandbox: {
|
|
184
|
-
mode: "off",
|
|
185
|
-
scope: "session",
|
|
186
|
-
workspaceAccess: "ro",
|
|
187
|
-
docker: {
|
|
188
|
-
workdir: "/workspace",
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
],
|
|
193
|
-
},
|
|
194
|
-
} as any;
|
|
195
|
-
|
|
196
|
-
const [stagedA, stagedB] = await Promise.all([
|
|
197
|
-
stageWecomInboundMediaForSession({
|
|
198
|
-
cfg,
|
|
199
|
-
accountId: "default",
|
|
200
|
-
agentId: "ops",
|
|
201
|
-
sessionKey: "agent:ops:wecom:default:dm:zhangsan",
|
|
202
|
-
mediaPath: mediaPathA,
|
|
203
|
-
filename: "dup.bin",
|
|
204
|
-
}),
|
|
205
|
-
stageWecomInboundMediaForSession({
|
|
206
|
-
cfg,
|
|
207
|
-
accountId: "default",
|
|
208
|
-
agentId: "ops",
|
|
209
|
-
sessionKey: "agent:ops:wecom:default:dm:lisi",
|
|
210
|
-
mediaPath: mediaPathB,
|
|
211
|
-
filename: "dup.bin",
|
|
212
|
-
}),
|
|
213
|
-
]);
|
|
214
|
-
|
|
215
|
-
expect(stagedA).not.toBe(stagedB);
|
|
216
|
-
expect([path.basename(stagedA), path.basename(stagedB)].sort()).toEqual(["dup-1.bin", "dup.bin"]);
|
|
217
|
-
expect((await readFile(stagedA)).toString()).toMatch(/first|second/);
|
|
218
|
-
expect((await readFile(stagedB)).toString()).toMatch(/first|second/);
|
|
219
|
-
expect((await readFile(stagedA)).toString()).not.toBe((await readFile(stagedB)).toString());
|
|
220
|
-
});
|
|
221
|
-
});
|
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import { constants } from "node:fs";
|
|
3
|
-
import os from "node:os";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { copyFile, mkdir, stat } from "node:fs/promises";
|
|
6
|
-
|
|
7
|
-
import type { OpenClawConfig } from "openclaw/plugin-sdk";
|
|
8
|
-
import { resolveAgentConfig, resolveAgentWorkspaceDir } from "openclaw/plugin-sdk/agent-runtime";
|
|
9
|
-
|
|
10
|
-
import { resolveWecomMediaMaxBytes } from "../../config/index.js";
|
|
11
|
-
|
|
12
|
-
function expandHomeDir(input: string): string {
|
|
13
|
-
if (input === "~") return os.homedir();
|
|
14
|
-
if (input.startsWith("~/")) return path.join(os.homedir(), input.slice(2));
|
|
15
|
-
return input;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function resolveOpenClawStateDir(): string {
|
|
19
|
-
const override = process.env.OPENCLAW_STATE_DIR?.trim() || process.env.CLAWDBOT_STATE_DIR?.trim();
|
|
20
|
-
if (override) {
|
|
21
|
-
return path.resolve(expandHomeDir(override));
|
|
22
|
-
}
|
|
23
|
-
return path.join(os.homedir(), ".openclaw");
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function normalizeAgentId(agentId: string): string {
|
|
27
|
-
return agentId.trim().toLowerCase() || "main";
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function normalizeMainKey(mainKey: unknown): string {
|
|
31
|
-
return typeof mainKey === "string" && mainKey.trim() ? mainKey.trim().toLowerCase() : "main";
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function buildAgentMainSessionKey(cfg: OpenClawConfig, agentId: string): string {
|
|
35
|
-
if (cfg.session?.scope === "global") {
|
|
36
|
-
return "global";
|
|
37
|
-
}
|
|
38
|
-
return `agent:${normalizeAgentId(agentId)}:${normalizeMainKey(cfg.session?.mainKey)}`;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function slugifySessionKey(value: string): string {
|
|
42
|
-
const trimmed = value.trim() || "session";
|
|
43
|
-
const hash = crypto.createHash("sha256").update(trimmed).digest("hex").slice(0, 8);
|
|
44
|
-
const prefix =
|
|
45
|
-
trimmed
|
|
46
|
-
.toLowerCase()
|
|
47
|
-
.replace(/[^a-z0-9._-]+/g, "-")
|
|
48
|
-
.replace(/^-+|-+$/g, "")
|
|
49
|
-
.slice(0, 32) || "session";
|
|
50
|
-
return `${prefix}-${hash}`;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function isSandboxedSession(params: {
|
|
54
|
-
cfg: OpenClawConfig;
|
|
55
|
-
agentId: string;
|
|
56
|
-
sessionKey: string;
|
|
57
|
-
}): boolean {
|
|
58
|
-
const sandbox = resolveAgentConfig(params.cfg, params.agentId)?.sandbox;
|
|
59
|
-
if (!sandbox || sandbox.mode === "off") {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
if (sandbox.mode === "all") {
|
|
63
|
-
return true;
|
|
64
|
-
}
|
|
65
|
-
return params.sessionKey.trim().toLowerCase() !== buildAgentMainSessionKey(params.cfg, params.agentId);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function resolveSessionWorkspaceTarget(params: {
|
|
69
|
-
cfg: OpenClawConfig;
|
|
70
|
-
agentId: string;
|
|
71
|
-
sessionKey: string;
|
|
72
|
-
}): { workspaceDir: string; sandboxed: boolean } {
|
|
73
|
-
const sandbox = resolveAgentConfig(params.cfg, params.agentId)?.sandbox;
|
|
74
|
-
const agentWorkspaceDirRaw = resolveAgentWorkspaceDir(params.cfg, params.agentId);
|
|
75
|
-
const agentWorkspaceDir = path.resolve(expandHomeDir(agentWorkspaceDirRaw || process.cwd()));
|
|
76
|
-
if (!sandbox || !isSandboxedSession(params)) {
|
|
77
|
-
return {
|
|
78
|
-
workspaceDir: agentWorkspaceDir,
|
|
79
|
-
sandboxed: false,
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
if (sandbox.workspaceAccess === "rw" || !sandbox.workspaceRoot) {
|
|
83
|
-
if (sandbox.workspaceAccess === "rw") {
|
|
84
|
-
return {
|
|
85
|
-
workspaceDir: agentWorkspaceDir,
|
|
86
|
-
sandboxed: true,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
workspaceDir: path.join(resolveOpenClawStateDir(), "sandboxes", slugifySessionKey(params.sessionKey.trim() || "main")),
|
|
91
|
-
sandboxed: true,
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const workspaceRoot = path.resolve(expandHomeDir(sandbox.workspaceRoot));
|
|
96
|
-
const scopeKey =
|
|
97
|
-
sandbox.scope === "shared"
|
|
98
|
-
? "shared"
|
|
99
|
-
: sandbox.scope === "session"
|
|
100
|
-
? params.sessionKey.trim() || "main"
|
|
101
|
-
: `agent:${normalizeAgentId(params.agentId)}`;
|
|
102
|
-
if (sandbox.scope === "shared") {
|
|
103
|
-
return {
|
|
104
|
-
workspaceDir: workspaceRoot,
|
|
105
|
-
sandboxed: true,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
return {
|
|
109
|
-
workspaceDir: path.join(workspaceRoot, slugifySessionKey(scopeKey)),
|
|
110
|
-
sandboxed: true,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function buildStagedFilenameCandidate(parsed: path.ParsedPath, suffix: number): string {
|
|
115
|
-
if (suffix === 0) {
|
|
116
|
-
return parsed.base || "attachment";
|
|
117
|
-
}
|
|
118
|
-
return `${parsed.name || "attachment"}-${suffix}${parsed.ext}`;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async function copyIntoWorkspaceInbound(params: {
|
|
122
|
-
sourcePath: string;
|
|
123
|
-
workspaceDir: string;
|
|
124
|
-
filename: string;
|
|
125
|
-
}): Promise<{ absolutePath: string; relativePath: string }> {
|
|
126
|
-
const inboundDir = path.join(params.workspaceDir, "media", "inbound");
|
|
127
|
-
await mkdir(inboundDir, { recursive: true });
|
|
128
|
-
|
|
129
|
-
const parsed = path.parse(params.filename);
|
|
130
|
-
for (let suffix = 0; ; suffix += 1) {
|
|
131
|
-
const candidate = buildStagedFilenameCandidate(parsed, suffix);
|
|
132
|
-
const absolutePath = path.join(inboundDir, candidate);
|
|
133
|
-
try {
|
|
134
|
-
await copyFile(params.sourcePath, absolutePath, constants.COPYFILE_EXCL);
|
|
135
|
-
return {
|
|
136
|
-
absolutePath,
|
|
137
|
-
relativePath: path.posix.join("media", "inbound", candidate),
|
|
138
|
-
};
|
|
139
|
-
} catch (error) {
|
|
140
|
-
if ((error as NodeJS.ErrnoException)?.code === "EEXIST") {
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export async function stageWecomInboundMediaForSession(params: {
|
|
149
|
-
cfg: OpenClawConfig;
|
|
150
|
-
accountId: string;
|
|
151
|
-
agentId: string;
|
|
152
|
-
sessionKey: string;
|
|
153
|
-
mediaPath: string;
|
|
154
|
-
filename?: string;
|
|
155
|
-
}): Promise<string> {
|
|
156
|
-
const target = resolveSessionWorkspaceTarget({
|
|
157
|
-
cfg: params.cfg,
|
|
158
|
-
agentId: params.agentId,
|
|
159
|
-
sessionKey: params.sessionKey,
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const maxBytes = resolveWecomMediaMaxBytes(params.cfg, params.accountId);
|
|
163
|
-
const sourceStat = await stat(params.mediaPath);
|
|
164
|
-
if (sourceStat.size > maxBytes) {
|
|
165
|
-
throw new Error(
|
|
166
|
-
`Inbound media size ${(sourceStat.size / (1024 * 1024)).toFixed(2)}MB exceeds channels.wecom.mediaMaxMb ${(maxBytes / (1024 * 1024)).toFixed(2)}MB before workspace staging`,
|
|
167
|
-
);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
const staged = await copyIntoWorkspaceInbound({
|
|
171
|
-
sourcePath: params.mediaPath,
|
|
172
|
-
workspaceDir: target.workspaceDir,
|
|
173
|
-
filename: params.filename || path.basename(params.mediaPath),
|
|
174
|
-
});
|
|
175
|
-
return target.sandboxed ? staged.relativePath : staged.absolutePath;
|
|
176
|
-
}
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { OpenClawConfig } from "openclaw/plugin-sdk";
|
|
2
|
-
|
|
3
|
-
import type { WecomRuntimeEnv } from "../../types/runtime-context.js";
|
|
4
|
-
import type { WecomAccountRuntime } from "../../app/account-runtime.js";
|
|
5
|
-
import { BotWsSdkAdapter } from "../../transport/bot-ws/sdk-adapter.js";
|
|
6
|
-
import { startBotWebhookTransport } from "../../transport/bot-webhook/http-handler.js";
|
|
7
|
-
|
|
8
|
-
export class WecomBotCapabilityService {
|
|
9
|
-
private wsAdapter?: BotWsSdkAdapter;
|
|
10
|
-
private stopTransport?: () => void;
|
|
11
|
-
|
|
12
|
-
constructor(
|
|
13
|
-
private readonly runtime: WecomAccountRuntime,
|
|
14
|
-
private readonly cfg: OpenClawConfig,
|
|
15
|
-
private readonly runtimeEnv: WecomRuntimeEnv,
|
|
16
|
-
) {}
|
|
17
|
-
|
|
18
|
-
start(): { transport: "bot-ws" | "bot-webhook"; descriptors: string[] } | undefined {
|
|
19
|
-
const bot = this.runtime.account.bot;
|
|
20
|
-
if (!bot?.configured) {
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
if (bot.primaryTransport === "ws") {
|
|
25
|
-
this.wsAdapter = new BotWsSdkAdapter(this.runtime, {
|
|
26
|
-
info: this.runtimeEnv.log,
|
|
27
|
-
warn: this.runtimeEnv.log,
|
|
28
|
-
error: this.runtimeEnv.error,
|
|
29
|
-
});
|
|
30
|
-
this.wsAdapter.start();
|
|
31
|
-
this.stopTransport = () => this.wsAdapter?.stop();
|
|
32
|
-
return {
|
|
33
|
-
transport: "bot-ws",
|
|
34
|
-
descriptors: ["ws:primary"],
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const webhook = startBotWebhookTransport({
|
|
39
|
-
account: bot,
|
|
40
|
-
cfg: this.cfg,
|
|
41
|
-
runtime: this.runtime,
|
|
42
|
-
runtimeEnv: this.runtimeEnv,
|
|
43
|
-
});
|
|
44
|
-
this.stopTransport = webhook.stop;
|
|
45
|
-
return {
|
|
46
|
-
transport: "bot-webhook",
|
|
47
|
-
descriptors: webhook.paths,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
stop(): void {
|
|
52
|
-
this.stopTransport?.();
|
|
53
|
-
this.stopTransport = undefined;
|
|
54
|
-
this.wsAdapter = undefined;
|
|
55
|
-
}
|
|
56
|
-
}
|