@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,216 @@
|
|
|
1
|
+
import { formatErrorMessage } from "openclaw/plugin-sdk/infra-runtime";
|
|
2
|
+
import { WecomAuditLog } from "../observability/audit-log.js";
|
|
3
|
+
import { WecomStatusRegistry } from "../observability/status-registry.js";
|
|
4
|
+
import { summarizeTransportSessions } from "../observability/transport-session-view.js";
|
|
5
|
+
import { dispatchInboundEvent } from "../runtime/dispatcher.js";
|
|
6
|
+
import { WecomMediaService } from "../shared/media-service.js";
|
|
7
|
+
import { InMemoryRuntimeStore } from "../store/memory-store.js";
|
|
8
|
+
export class WecomAccountRuntime {
|
|
9
|
+
core;
|
|
10
|
+
cfg;
|
|
11
|
+
resolved;
|
|
12
|
+
log;
|
|
13
|
+
statusSink;
|
|
14
|
+
store = new InMemoryRuntimeStore();
|
|
15
|
+
mediaService;
|
|
16
|
+
auditLog = new WecomAuditLog();
|
|
17
|
+
statusRegistry = new WecomStatusRegistry();
|
|
18
|
+
runtimeStatus;
|
|
19
|
+
constructor(core, cfg, resolved, log = {}, statusSink) {
|
|
20
|
+
this.core = core;
|
|
21
|
+
this.cfg = cfg;
|
|
22
|
+
this.resolved = resolved;
|
|
23
|
+
this.log = log;
|
|
24
|
+
this.statusSink = statusSink;
|
|
25
|
+
this.mediaService = new WecomMediaService(core, cfg);
|
|
26
|
+
this.runtimeStatus = {
|
|
27
|
+
accountId: resolved.account.accountId,
|
|
28
|
+
health: "idle",
|
|
29
|
+
ownerId: null,
|
|
30
|
+
ownerDriftAt: null,
|
|
31
|
+
lastError: null,
|
|
32
|
+
lastErrorAt: null,
|
|
33
|
+
lastInboundAt: null,
|
|
34
|
+
lastOutboundAt: null,
|
|
35
|
+
recentInboundSummary: null,
|
|
36
|
+
recentOutboundSummary: null,
|
|
37
|
+
recentIssueCategory: null,
|
|
38
|
+
recentIssueSummary: null,
|
|
39
|
+
transportSessions: [],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
get account() {
|
|
43
|
+
return this.resolved.account;
|
|
44
|
+
}
|
|
45
|
+
async handleEvent(event, replyHandle) {
|
|
46
|
+
const dispatchStartedAt = Date.now();
|
|
47
|
+
this.runtimeStatus.lastInboundAt = Date.now();
|
|
48
|
+
this.runtimeStatus.recentInboundSummary = `${event.transport} ${event.inboundKind} ${event.messageId}`;
|
|
49
|
+
this.log.info?.(`[wecom-runtime] inbound account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId} peer=${event.conversation.peerKind}:${event.conversation.peerId}`);
|
|
50
|
+
this.log.info?.(`[wecom-runtime] dispatch-start account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId}`);
|
|
51
|
+
this.emitStatus();
|
|
52
|
+
const trackedReplyHandle = {
|
|
53
|
+
context: replyHandle.context,
|
|
54
|
+
deliver: async (payload, info) => {
|
|
55
|
+
const deliverStartedAt = Date.now();
|
|
56
|
+
const textLen = payload.text?.trim().length ?? 0;
|
|
57
|
+
const mediaCount = (payload.mediaUrls?.length ?? 0) + (payload.mediaUrl ? 1 : 0);
|
|
58
|
+
this.log.info?.(`[wecom-runtime] deliver-start account=${event.accountId} transport=${replyHandle.context.transport} kind=${info.kind} messageId=${event.messageId} textLen=${textLen} mediaCount=${mediaCount} reasoning=${String(payload.isReasoning === true)}`);
|
|
59
|
+
await replyHandle.deliver(payload, info);
|
|
60
|
+
this.runtimeStatus.lastOutboundAt = Date.now();
|
|
61
|
+
const outboundSummary = payload.text?.trim() || payload.mediaUrl || payload.mediaUrls?.[0] || info.kind;
|
|
62
|
+
this.runtimeStatus.recentOutboundSummary = `${replyHandle.context.transport} ${outboundSummary.slice(0, 120)}`;
|
|
63
|
+
this.log.info?.(`[wecom-runtime] outbound account=${event.accountId} transport=${replyHandle.context.transport} kind=${info.kind} messageId=${event.messageId} summary=${JSON.stringify(this.runtimeStatus.recentOutboundSummary)}`);
|
|
64
|
+
this.log.info?.(`[wecom-runtime] deliver-done account=${event.accountId} transport=${replyHandle.context.transport} kind=${info.kind} messageId=${event.messageId} durationMs=${Date.now() - deliverStartedAt}`);
|
|
65
|
+
this.emitStatus();
|
|
66
|
+
},
|
|
67
|
+
fail: async (error) => {
|
|
68
|
+
const formattedError = formatErrorMessage(error);
|
|
69
|
+
this.recordOperationalIssue({
|
|
70
|
+
transport: replyHandle.context.transport,
|
|
71
|
+
category: "runtime-error",
|
|
72
|
+
messageId: event.messageId,
|
|
73
|
+
raw: replyHandle.context.raw,
|
|
74
|
+
summary: `reply-fail ${formattedError}`,
|
|
75
|
+
error: formattedError,
|
|
76
|
+
});
|
|
77
|
+
this.log.error?.(`[wecom-runtime] reply-fail account=${event.accountId} transport=${replyHandle.context.transport} messageId=${event.messageId} error=${formattedError}`);
|
|
78
|
+
await replyHandle.fail?.(error);
|
|
79
|
+
},
|
|
80
|
+
markExternalActivity: () => {
|
|
81
|
+
replyHandle.markExternalActivity?.();
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
try {
|
|
85
|
+
await dispatchInboundEvent({
|
|
86
|
+
core: this.core,
|
|
87
|
+
cfg: this.cfg,
|
|
88
|
+
store: this.store,
|
|
89
|
+
auditLog: this.auditLog,
|
|
90
|
+
mediaService: this.mediaService,
|
|
91
|
+
event,
|
|
92
|
+
replyHandle: trackedReplyHandle,
|
|
93
|
+
});
|
|
94
|
+
this.log.info?.(`[wecom-runtime] dispatch-done account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId} durationMs=${Date.now() - dispatchStartedAt}`);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
this.log.error?.(`[wecom-runtime] dispatch-fail account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId} durationMs=${Date.now() - dispatchStartedAt} error=${formatErrorMessage(error)}`);
|
|
98
|
+
throw error;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
updateTransportSession(snapshot) {
|
|
102
|
+
const previous = this.store.readTransportSession(snapshot.accountId, snapshot.transport);
|
|
103
|
+
this.store.writeTransportSession(snapshot);
|
|
104
|
+
this.statusRegistry.write(snapshot);
|
|
105
|
+
this.log.info?.(`[wecom-runtime] session account=${snapshot.accountId} transport=${snapshot.transport} running=${snapshot.running} owner=${snapshot.ownerId ?? "none"} connected=${String(snapshot.connected ?? false)} authenticated=${String(snapshot.authenticated ?? false)} error=${snapshot.lastError ?? "none"}`);
|
|
106
|
+
if (previous?.ownerId &&
|
|
107
|
+
snapshot.ownerId &&
|
|
108
|
+
previous.ownerId !== snapshot.ownerId &&
|
|
109
|
+
previous.running) {
|
|
110
|
+
this.recordOperationalIssue({
|
|
111
|
+
transport: snapshot.transport,
|
|
112
|
+
category: "owner-drift",
|
|
113
|
+
summary: `owner drift ${previous.ownerId} -> ${snapshot.ownerId}`,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
if (snapshot.lastError) {
|
|
117
|
+
this.runtimeStatus.lastError = snapshot.lastError;
|
|
118
|
+
this.runtimeStatus.lastErrorAt = Date.now();
|
|
119
|
+
}
|
|
120
|
+
else if (snapshot.running) {
|
|
121
|
+
this.runtimeStatus.lastError = null;
|
|
122
|
+
}
|
|
123
|
+
this.emitStatus();
|
|
124
|
+
}
|
|
125
|
+
touchTransportSession(transport, patch) {
|
|
126
|
+
const current = this.store.readTransportSession(this.account.accountId, transport);
|
|
127
|
+
const next = {
|
|
128
|
+
accountId: this.account.accountId,
|
|
129
|
+
transport,
|
|
130
|
+
running: patch.running ?? current?.running ?? true,
|
|
131
|
+
ownerId: patch.ownerId ?? current?.ownerId,
|
|
132
|
+
connected: patch.connected ?? current?.connected,
|
|
133
|
+
authenticated: patch.authenticated ?? current?.authenticated,
|
|
134
|
+
lastConnectedAt: patch.lastConnectedAt ?? current?.lastConnectedAt,
|
|
135
|
+
lastDisconnectedAt: patch.lastDisconnectedAt ?? current?.lastDisconnectedAt,
|
|
136
|
+
lastInboundAt: patch.lastInboundAt ?? current?.lastInboundAt,
|
|
137
|
+
lastOutboundAt: patch.lastOutboundAt ?? current?.lastOutboundAt,
|
|
138
|
+
lastError: "lastError" in patch ? (patch.lastError ?? undefined) : current?.lastError,
|
|
139
|
+
};
|
|
140
|
+
this.updateTransportSession(next);
|
|
141
|
+
}
|
|
142
|
+
listTransportSessions() {
|
|
143
|
+
return this.statusRegistry.read(this.account.accountId);
|
|
144
|
+
}
|
|
145
|
+
listAuditEntries() {
|
|
146
|
+
return this.auditLog.list();
|
|
147
|
+
}
|
|
148
|
+
buildRuntimeStatus() {
|
|
149
|
+
const sessions = this.listTransportSessions();
|
|
150
|
+
const primarySession = this.resolvePrimarySession(sessions);
|
|
151
|
+
return {
|
|
152
|
+
...this.runtimeStatus,
|
|
153
|
+
health: this.computeHealth(sessions),
|
|
154
|
+
transport: primarySession?.transport,
|
|
155
|
+
ownerId: primarySession?.ownerId ?? this.runtimeStatus.ownerId ?? null,
|
|
156
|
+
connected: primarySession?.connected,
|
|
157
|
+
authenticated: primarySession?.authenticated,
|
|
158
|
+
lastError: primarySession?.lastError ??
|
|
159
|
+
(primarySession?.running ? null : (this.runtimeStatus.lastError ?? null)),
|
|
160
|
+
transportSessions: summarizeTransportSessions(sessions),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
recordOperationalIssue(params) {
|
|
164
|
+
this.auditLog.appendOperational({
|
|
165
|
+
accountId: this.account.accountId,
|
|
166
|
+
transport: params.transport,
|
|
167
|
+
category: params.category,
|
|
168
|
+
messageId: params.messageId,
|
|
169
|
+
summary: params.summary,
|
|
170
|
+
raw: params.raw,
|
|
171
|
+
error: params.error,
|
|
172
|
+
});
|
|
173
|
+
if (params.category === "owner-drift" || params.category === "ws-kicked") {
|
|
174
|
+
this.runtimeStatus.ownerDriftAt = Date.now();
|
|
175
|
+
}
|
|
176
|
+
this.runtimeStatus.lastError = params.error ?? params.summary;
|
|
177
|
+
this.runtimeStatus.lastErrorAt = Date.now();
|
|
178
|
+
this.runtimeStatus.recentIssueCategory = params.category;
|
|
179
|
+
this.runtimeStatus.recentIssueSummary = params.summary;
|
|
180
|
+
const sink = params.category === "runtime-error" || params.category === "fallback-delivery-failed"
|
|
181
|
+
? this.log.error
|
|
182
|
+
: this.log.warn;
|
|
183
|
+
sink?.(`[wecom-runtime] issue account=${this.account.accountId} transport=${params.transport} category=${params.category} messageId=${params.messageId ?? "n/a"} summary=${params.summary}`);
|
|
184
|
+
this.emitStatus();
|
|
185
|
+
}
|
|
186
|
+
emitStatus() {
|
|
187
|
+
this.statusSink?.(this.buildRuntimeStatus());
|
|
188
|
+
}
|
|
189
|
+
resolvePrimarySession(sessions) {
|
|
190
|
+
const primaryTransport = this.account.bot?.configured
|
|
191
|
+
? this.account.bot.primaryTransport === "ws"
|
|
192
|
+
? "bot-ws"
|
|
193
|
+
: "bot-webhook"
|
|
194
|
+
: this.account.agent?.callbackConfigured
|
|
195
|
+
? "agent-callback"
|
|
196
|
+
: undefined;
|
|
197
|
+
if (!primaryTransport) {
|
|
198
|
+
return sessions[0];
|
|
199
|
+
}
|
|
200
|
+
return sessions.find((session) => session.transport === primaryTransport) ?? sessions[0];
|
|
201
|
+
}
|
|
202
|
+
computeHealth(sessions) {
|
|
203
|
+
if (sessions.length === 0) {
|
|
204
|
+
return this.runtimeStatus.lastError ? "down" : "idle";
|
|
205
|
+
}
|
|
206
|
+
const hasRunning = sessions.some((session) => session.running);
|
|
207
|
+
const hasError = sessions.some((session) => Boolean(session.lastError));
|
|
208
|
+
if (hasRunning && !hasError) {
|
|
209
|
+
return "healthy";
|
|
210
|
+
}
|
|
211
|
+
if (hasRunning) {
|
|
212
|
+
return "degraded";
|
|
213
|
+
}
|
|
214
|
+
return hasError ? "down" : "idle";
|
|
215
|
+
}
|
|
216
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { resolveWecomRuntimeAccount } from "../config/runtime-config.js";
|
|
2
|
+
import { WecomAccountRuntime } from "./account-runtime.js";
|
|
3
|
+
import { getWecomRuntime } from "./index.js";
|
|
4
|
+
export function createAccountRuntime(ctx) {
|
|
5
|
+
const resolved = resolveWecomRuntimeAccount({
|
|
6
|
+
cfg: ctx.cfg,
|
|
7
|
+
accountId: ctx.accountId,
|
|
8
|
+
});
|
|
9
|
+
return new WecomAccountRuntime(getWecomRuntime(), ctx.cfg, resolved, {
|
|
10
|
+
info: (message) => ctx.log?.info(message),
|
|
11
|
+
warn: (message) => ctx.log?.warn(message),
|
|
12
|
+
error: (message) => ctx.log?.error(message),
|
|
13
|
+
}, (snapshot) => {
|
|
14
|
+
ctx.setStatus({
|
|
15
|
+
accountId: ctx.accountId,
|
|
16
|
+
...snapshot,
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { clearWecomSourceAccount } from "../runtime/source-registry.js";
|
|
2
|
+
let runtime = null;
|
|
3
|
+
const runtimes = new Map();
|
|
4
|
+
const botWsPushHandles = new Map();
|
|
5
|
+
const activeBotWsReplyHandlesBySession = new Map();
|
|
6
|
+
const activeBotWsReplyHandlesByPeer = new Map();
|
|
7
|
+
function normalizeOptional(value) {
|
|
8
|
+
const trimmed = String(value ?? "").trim();
|
|
9
|
+
return trimmed || undefined;
|
|
10
|
+
}
|
|
11
|
+
function normalizePeerId(value) {
|
|
12
|
+
const trimmed = normalizeOptional(value);
|
|
13
|
+
return trimmed ? trimmed.toLowerCase() : undefined;
|
|
14
|
+
}
|
|
15
|
+
function buildSessionHandleKey(accountId, sessionKey) {
|
|
16
|
+
return `${accountId}::session::${sessionKey}`;
|
|
17
|
+
}
|
|
18
|
+
function buildPeerHandleKey(accountId, peerKind, peerId) {
|
|
19
|
+
return `${accountId}::peer::${peerKind}::${peerId}`;
|
|
20
|
+
}
|
|
21
|
+
export function setWecomRuntime(next) {
|
|
22
|
+
runtime = next;
|
|
23
|
+
}
|
|
24
|
+
export function getWecomRuntime() {
|
|
25
|
+
if (!runtime) {
|
|
26
|
+
throw new Error("WeCom runtime not initialized");
|
|
27
|
+
}
|
|
28
|
+
return runtime;
|
|
29
|
+
}
|
|
30
|
+
export function registerAccountRuntime(accountRuntime) {
|
|
31
|
+
runtimes.set(accountRuntime.account.accountId, accountRuntime);
|
|
32
|
+
console.log(`[wecom-runtime] register account=${accountRuntime.account.accountId}`);
|
|
33
|
+
}
|
|
34
|
+
export function getAccountRuntime(accountId) {
|
|
35
|
+
return runtimes.get(accountId);
|
|
36
|
+
}
|
|
37
|
+
export function getAccountRuntimeSnapshot(accountId) {
|
|
38
|
+
return runtimes.get(accountId)?.buildRuntimeStatus();
|
|
39
|
+
}
|
|
40
|
+
export function registerBotWsPushHandle(accountId, handle) {
|
|
41
|
+
botWsPushHandles.set(accountId, handle);
|
|
42
|
+
}
|
|
43
|
+
export function getBotWsPushHandle(accountId) {
|
|
44
|
+
return botWsPushHandles.get(accountId);
|
|
45
|
+
}
|
|
46
|
+
export function registerActiveBotWsReplyHandle(params) {
|
|
47
|
+
const accountId = normalizeOptional(params.accountId);
|
|
48
|
+
const sessionKey = normalizeOptional(params.sessionKey);
|
|
49
|
+
const peerId = normalizePeerId(params.peerId);
|
|
50
|
+
if (!accountId) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (sessionKey) {
|
|
54
|
+
activeBotWsReplyHandlesBySession.set(buildSessionHandleKey(accountId, sessionKey), params.handle);
|
|
55
|
+
}
|
|
56
|
+
if ((params.peerKind === "direct" || params.peerKind === "group") && peerId) {
|
|
57
|
+
activeBotWsReplyHandlesByPeer.set(buildPeerHandleKey(accountId, params.peerKind, peerId), params.handle);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function getActiveBotWsReplyHandle(params) {
|
|
61
|
+
const accountId = normalizeOptional(params.accountId);
|
|
62
|
+
const sessionKey = normalizeOptional(params.sessionKey);
|
|
63
|
+
const peerId = normalizePeerId(params.peerId);
|
|
64
|
+
if (!accountId) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
if (sessionKey) {
|
|
68
|
+
const handle = activeBotWsReplyHandlesBySession.get(buildSessionHandleKey(accountId, sessionKey));
|
|
69
|
+
if (handle) {
|
|
70
|
+
return handle;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if ((params.peerKind === "direct" || params.peerKind === "group") && peerId) {
|
|
74
|
+
return activeBotWsReplyHandlesByPeer.get(buildPeerHandleKey(accountId, params.peerKind, peerId));
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
export function unregisterActiveBotWsReplyHandle(params) {
|
|
79
|
+
const accountId = normalizeOptional(params.accountId);
|
|
80
|
+
const sessionKey = normalizeOptional(params.sessionKey);
|
|
81
|
+
const peerId = normalizePeerId(params.peerId);
|
|
82
|
+
if (!accountId) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (sessionKey) {
|
|
86
|
+
const key = buildSessionHandleKey(accountId, sessionKey);
|
|
87
|
+
const current = activeBotWsReplyHandlesBySession.get(key);
|
|
88
|
+
if (!params.handle || current === params.handle) {
|
|
89
|
+
activeBotWsReplyHandlesBySession.delete(key);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if ((params.peerKind === "direct" || params.peerKind === "group") && peerId) {
|
|
93
|
+
const key = buildPeerHandleKey(accountId, params.peerKind, peerId);
|
|
94
|
+
const current = activeBotWsReplyHandlesByPeer.get(key);
|
|
95
|
+
if (!params.handle || current === params.handle) {
|
|
96
|
+
activeBotWsReplyHandlesByPeer.delete(key);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export function unregisterBotWsPushHandle(accountId) {
|
|
101
|
+
botWsPushHandles.delete(accountId);
|
|
102
|
+
}
|
|
103
|
+
export function unregisterAccountRuntime(accountId) {
|
|
104
|
+
runtimes.delete(accountId);
|
|
105
|
+
botWsPushHandles.delete(accountId);
|
|
106
|
+
for (const key of activeBotWsReplyHandlesBySession.keys()) {
|
|
107
|
+
if (key.startsWith(`${accountId}::`)) {
|
|
108
|
+
activeBotWsReplyHandlesBySession.delete(key);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
for (const key of activeBotWsReplyHandlesByPeer.keys()) {
|
|
112
|
+
if (key.startsWith(`${accountId}::`)) {
|
|
113
|
+
activeBotWsReplyHandlesByPeer.delete(key);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
clearWecomSourceAccount(accountId);
|
|
117
|
+
console.log(`[wecom-runtime] unregister account=${accountId}`);
|
|
118
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { resolveScopedWecomTarget } from "../../target.js";
|
|
2
|
+
import { deliverAgentApiMedia, deliverAgentApiText } from "../../transport/agent-api/delivery.js";
|
|
3
|
+
import { canUseAgentApiDelivery } from "./fallback-policy.js";
|
|
4
|
+
import { getWecomRuntime } from "../../runtime.js";
|
|
5
|
+
export class WecomAgentDeliveryService {
|
|
6
|
+
agent;
|
|
7
|
+
constructor(agent) {
|
|
8
|
+
this.agent = agent;
|
|
9
|
+
}
|
|
10
|
+
assertAvailable() {
|
|
11
|
+
if (!canUseAgentApiDelivery(this.agent)) {
|
|
12
|
+
throw new Error(`WeCom outbound requires channels.wecom.accounts.<accountId>.agent.agentId (or legacy channels.wecom.agent.agentId) for account=${this.agent.accountId}.`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
resolveTargetOrThrow(to) {
|
|
16
|
+
const scoped = resolveScopedWecomTarget(to, this.agent.accountId);
|
|
17
|
+
if (!scoped) {
|
|
18
|
+
console.error(`[wecom-agent-delivery] missing target account=${this.agent.accountId}`);
|
|
19
|
+
throw new Error("WeCom outbound requires a target (userid, partyid, tagid or chatid).");
|
|
20
|
+
}
|
|
21
|
+
if (scoped.accountId && scoped.accountId !== this.agent.accountId) {
|
|
22
|
+
console.error(`[wecom-agent-delivery] account mismatch current=${this.agent.accountId} targetAccount=${scoped.accountId} raw=${String(to ?? "")}`);
|
|
23
|
+
throw new Error(`WeCom outbound account mismatch: target belongs to account=${scoped.accountId}, current account=${this.agent.accountId}.`);
|
|
24
|
+
}
|
|
25
|
+
const target = scoped.target;
|
|
26
|
+
if (target.chatid) {
|
|
27
|
+
console.warn(`[wecom-agent-delivery] blocked chat target account=${this.agent.accountId} chatId=${target.chatid}`);
|
|
28
|
+
throw new Error(`企业微信(WeCom)Agent 主动发送不支持向群 chatId 发送(chatId=${target.chatid})。` +
|
|
29
|
+
`该路径在实际环境中经常失败(例如 86008:无权限访问该会话/会话由其他应用创建)。` +
|
|
30
|
+
`请改为发送给用户(userid / user:xxx),或由 Bot 模式在群内交付。`);
|
|
31
|
+
}
|
|
32
|
+
return target;
|
|
33
|
+
}
|
|
34
|
+
async sendText(params) {
|
|
35
|
+
this.assertAvailable();
|
|
36
|
+
const target = this.resolveTargetOrThrow(params.to);
|
|
37
|
+
console.log(`[wecom-agent-delivery] sendText account=${this.agent.accountId} to=${String(params.to ?? "")} len=${params.text.length}`);
|
|
38
|
+
const runtime = getWecomRuntime();
|
|
39
|
+
const chunks = runtime.channel.text.chunkText(params.text, 2048);
|
|
40
|
+
for (const chunk of chunks) {
|
|
41
|
+
if (!chunk.trim())
|
|
42
|
+
continue;
|
|
43
|
+
await deliverAgentApiText({
|
|
44
|
+
agent: this.agent,
|
|
45
|
+
target,
|
|
46
|
+
text: chunk,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async sendMedia(params) {
|
|
51
|
+
this.assertAvailable();
|
|
52
|
+
const target = this.resolveTargetOrThrow(params.to);
|
|
53
|
+
console.log(`[wecom-agent-delivery] sendMedia account=${this.agent.accountId} to=${String(params.to ?? "")} filename=${params.filename} contentType=${params.contentType}`);
|
|
54
|
+
await deliverAgentApiMedia({
|
|
55
|
+
agent: this.agent,
|
|
56
|
+
target,
|
|
57
|
+
buffer: params.buffer,
|
|
58
|
+
filename: params.filename,
|
|
59
|
+
contentType: params.contentType,
|
|
60
|
+
text: params.text,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export function canUseAgentApiDelivery(agent) {
|
|
2
|
+
return Boolean(agent?.apiConfigured && typeof agent.agentId === "number");
|
|
3
|
+
}
|
|
4
|
+
export function shouldFallbackToAgentApi(params) {
|
|
5
|
+
return canUseAgentApiDelivery(params.agent) && Boolean(params.hasText || params.hasMedia);
|
|
6
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { startAgentCallbackTransport } from "../../transport/agent-callback/http-handler.js";
|
|
2
|
+
export class WecomAgentIngressService {
|
|
3
|
+
runtime;
|
|
4
|
+
cfg;
|
|
5
|
+
runtimeEnv;
|
|
6
|
+
stopTransport;
|
|
7
|
+
constructor(runtime, cfg, runtimeEnv) {
|
|
8
|
+
this.runtime = runtime;
|
|
9
|
+
this.cfg = cfg;
|
|
10
|
+
this.runtimeEnv = runtimeEnv;
|
|
11
|
+
}
|
|
12
|
+
start() {
|
|
13
|
+
const agent = this.runtime.account.agent;
|
|
14
|
+
if (!agent?.callbackConfigured) {
|
|
15
|
+
return undefined;
|
|
16
|
+
}
|
|
17
|
+
const callback = startAgentCallbackTransport({
|
|
18
|
+
account: agent,
|
|
19
|
+
cfg: this.cfg,
|
|
20
|
+
runtime: this.runtime,
|
|
21
|
+
runtimeEnv: this.runtimeEnv,
|
|
22
|
+
});
|
|
23
|
+
this.stopTransport = callback.stop;
|
|
24
|
+
return {
|
|
25
|
+
transport: "agent-callback",
|
|
26
|
+
descriptors: callback.paths,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
stop() {
|
|
30
|
+
this.stopTransport?.();
|
|
31
|
+
this.stopTransport = undefined;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { resolveScopedWecomTarget } from "../../target.js";
|
|
2
|
+
import { deliverUpstreamAgentApiMedia, deliverUpstreamAgentApiText } from "../../transport/agent-api/upstream-delivery.js";
|
|
3
|
+
import { canUseAgentApiDelivery } from "./fallback-policy.js";
|
|
4
|
+
import { getWecomRuntime } from "../../runtime.js";
|
|
5
|
+
/**
|
|
6
|
+
* 上下游企业消息发送服务
|
|
7
|
+
*
|
|
8
|
+
* 使用下游企业的 access_token 和 agentId 发送消息
|
|
9
|
+
*/
|
|
10
|
+
export class WecomUpstreamAgentDeliveryService {
|
|
11
|
+
upstreamAgent;
|
|
12
|
+
primaryAgent;
|
|
13
|
+
constructor(upstreamAgent, primaryAgent) {
|
|
14
|
+
this.upstreamAgent = upstreamAgent;
|
|
15
|
+
this.primaryAgent = primaryAgent;
|
|
16
|
+
}
|
|
17
|
+
assertAvailable() {
|
|
18
|
+
if (!canUseAgentApiDelivery(this.upstreamAgent)) {
|
|
19
|
+
throw new Error(`WeCom upstream outbound requires channels.wecom.accounts.<accountId>.agent.agentId for upstream corp=${this.upstreamAgent.corpId}.`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
resolveTargetOrThrow(to) {
|
|
23
|
+
const scoped = resolveScopedWecomTarget(to, this.upstreamAgent.accountId);
|
|
24
|
+
if (!scoped) {
|
|
25
|
+
console.error(`[wecom-upstream-delivery] missing target account=${this.upstreamAgent.accountId}`);
|
|
26
|
+
throw new Error("WeCom upstream outbound requires a target (userid, partyid, tagid or chatid).");
|
|
27
|
+
}
|
|
28
|
+
if (scoped.accountId && scoped.accountId !== this.upstreamAgent.accountId) {
|
|
29
|
+
console.error(`[wecom-upstream-delivery] account mismatch current=${this.upstreamAgent.accountId} targetAccount=${scoped.accountId} raw=${String(to ?? "")}`);
|
|
30
|
+
throw new Error(`WeCom upstream outbound account mismatch: target belongs to account=${scoped.accountId}, current account=${this.upstreamAgent.accountId}.`);
|
|
31
|
+
}
|
|
32
|
+
const target = scoped.target;
|
|
33
|
+
if (target.chatid) {
|
|
34
|
+
console.warn(`[wecom-upstream-delivery] blocked chat target account=${this.upstreamAgent.accountId} chatId=${target.chatid}`);
|
|
35
|
+
throw new Error(`企业微信(WeCom)上下游 Agent 主动发送不支持向群 chatId 发送(chatId=${target.chatid})。` +
|
|
36
|
+
`请改为发送给用户(userid / user:xxx)。`);
|
|
37
|
+
}
|
|
38
|
+
return target;
|
|
39
|
+
}
|
|
40
|
+
async sendText(params) {
|
|
41
|
+
this.assertAvailable();
|
|
42
|
+
const target = this.resolveTargetOrThrow(params.to);
|
|
43
|
+
console.log(`[wecom-upstream-delivery] sendText account=${this.upstreamAgent.accountId} corpId=${this.upstreamAgent.corpId} to=${String(params.to ?? "")} len=${params.text.length}`);
|
|
44
|
+
const runtime = getWecomRuntime();
|
|
45
|
+
const chunks = runtime.channel.text.chunkText(params.text, 2048);
|
|
46
|
+
for (const chunk of chunks) {
|
|
47
|
+
if (!chunk.trim())
|
|
48
|
+
continue;
|
|
49
|
+
await deliverUpstreamAgentApiText({
|
|
50
|
+
upstreamAgent: this.upstreamAgent,
|
|
51
|
+
primaryAgent: this.primaryAgent,
|
|
52
|
+
target,
|
|
53
|
+
text: chunk,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async sendMedia(params) {
|
|
58
|
+
this.assertAvailable();
|
|
59
|
+
const target = this.resolveTargetOrThrow(params.to);
|
|
60
|
+
console.log(`[wecom-upstream-delivery] sendMedia account=${this.upstreamAgent.accountId} corpId=${this.upstreamAgent.corpId} to=${String(params.to ?? "")} filename=${params.filename} contentType=${params.contentType}`);
|
|
61
|
+
await deliverUpstreamAgentApiMedia({
|
|
62
|
+
upstreamAgent: this.upstreamAgent,
|
|
63
|
+
primaryAgent: this.primaryAgent,
|
|
64
|
+
target,
|
|
65
|
+
buffer: params.buffer,
|
|
66
|
+
filename: params.filename,
|
|
67
|
+
contentType: params.contentType,
|
|
68
|
+
text: params.text,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export function buildWecomBotDispatchConfig(config) {
|
|
2
|
+
const baseAgents = config?.agents ?? {};
|
|
3
|
+
const baseAgentDefaults = baseAgents?.defaults ?? {};
|
|
4
|
+
const baseBlockChunk = baseAgentDefaults?.blockStreamingChunk ?? {};
|
|
5
|
+
const baseBlockCoalesce = baseAgentDefaults?.blockStreamingCoalesce ?? {};
|
|
6
|
+
const baseTools = config?.tools ?? {};
|
|
7
|
+
const baseSandbox = baseTools?.sandbox ?? {};
|
|
8
|
+
const baseSandboxTools = baseSandbox?.tools ?? {};
|
|
9
|
+
const existingTopLevelDeny = Array.isArray(baseTools.deny) ? baseTools.deny : [];
|
|
10
|
+
const existingSandboxDeny = Array.isArray(baseSandboxTools.deny) ? baseSandboxTools.deny : [];
|
|
11
|
+
const topLevelDeny = Array.from(new Set([...existingTopLevelDeny, "message"]));
|
|
12
|
+
const sandboxDeny = Array.from(new Set([...existingSandboxDeny, "message"]));
|
|
13
|
+
return {
|
|
14
|
+
...config,
|
|
15
|
+
agents: {
|
|
16
|
+
...baseAgents,
|
|
17
|
+
defaults: {
|
|
18
|
+
...baseAgentDefaults,
|
|
19
|
+
blockStreamingChunk: {
|
|
20
|
+
...baseBlockChunk,
|
|
21
|
+
minChars: baseBlockChunk.minChars ?? 120,
|
|
22
|
+
maxChars: baseBlockChunk.maxChars ?? 360,
|
|
23
|
+
breakPreference: baseBlockChunk.breakPreference ?? "sentence",
|
|
24
|
+
},
|
|
25
|
+
blockStreamingCoalesce: {
|
|
26
|
+
...baseBlockCoalesce,
|
|
27
|
+
minChars: baseBlockCoalesce.minChars ?? 120,
|
|
28
|
+
maxChars: baseBlockCoalesce.maxChars ?? 360,
|
|
29
|
+
idleMs: baseBlockCoalesce.idleMs ?? 250,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
tools: {
|
|
34
|
+
...baseTools,
|
|
35
|
+
deny: topLevelDeny,
|
|
36
|
+
sandbox: {
|
|
37
|
+
...baseSandbox,
|
|
38
|
+
tools: {
|
|
39
|
+
...baseSandboxTools,
|
|
40
|
+
deny: sandboxDeny,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|