@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
package/src/agent/index.ts
DELETED
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
import type { OpenClawConfig, PluginRuntime } from "openclaw/plugin-sdk";
|
|
2
|
-
import { formatErrorMessage } from "openclaw/plugin-sdk/infra-runtime";
|
|
3
|
-
import type { ResolvedRuntimeAccount } from "../config/runtime-config.js";
|
|
4
|
-
import { WecomAuditLog } from "../observability/audit-log.js";
|
|
5
|
-
import { WecomStatusRegistry } from "../observability/status-registry.js";
|
|
6
|
-
import { summarizeTransportSessions } from "../observability/transport-session-view.js";
|
|
7
|
-
import { dispatchInboundEvent } from "../runtime/dispatcher.js";
|
|
8
|
-
import { WecomMediaService } from "../shared/media-service.js";
|
|
9
|
-
import { InMemoryRuntimeStore } from "../store/memory-store.js";
|
|
10
|
-
import type {
|
|
11
|
-
AccountRuntimeStatusSnapshot,
|
|
12
|
-
ReplyHandle,
|
|
13
|
-
ReplyPayload,
|
|
14
|
-
TransportSessionPatch,
|
|
15
|
-
TransportSessionSnapshot,
|
|
16
|
-
UnifiedInboundEvent,
|
|
17
|
-
WecomAuditCategory,
|
|
18
|
-
WecomRuntimeHealth,
|
|
19
|
-
WecomTransportKind,
|
|
20
|
-
} from "../types/index.js";
|
|
21
|
-
|
|
22
|
-
export class WecomAccountRuntime {
|
|
23
|
-
readonly store = new InMemoryRuntimeStore();
|
|
24
|
-
readonly mediaService: WecomMediaService;
|
|
25
|
-
readonly auditLog = new WecomAuditLog();
|
|
26
|
-
readonly statusRegistry = new WecomStatusRegistry();
|
|
27
|
-
private readonly runtimeStatus: AccountRuntimeStatusSnapshot;
|
|
28
|
-
|
|
29
|
-
constructor(
|
|
30
|
-
readonly core: PluginRuntime,
|
|
31
|
-
readonly cfg: OpenClawConfig,
|
|
32
|
-
readonly resolved: ResolvedRuntimeAccount,
|
|
33
|
-
readonly log: {
|
|
34
|
-
info?: (message: string) => void;
|
|
35
|
-
warn?: (message: string) => void;
|
|
36
|
-
error?: (message: string) => void;
|
|
37
|
-
} = {},
|
|
38
|
-
private readonly statusSink?: (snapshot: Record<string, unknown>) => void,
|
|
39
|
-
) {
|
|
40
|
-
this.mediaService = new WecomMediaService(core, cfg);
|
|
41
|
-
this.runtimeStatus = {
|
|
42
|
-
accountId: resolved.account.accountId,
|
|
43
|
-
health: "idle",
|
|
44
|
-
ownerId: null,
|
|
45
|
-
ownerDriftAt: null,
|
|
46
|
-
lastError: null,
|
|
47
|
-
lastErrorAt: null,
|
|
48
|
-
lastInboundAt: null,
|
|
49
|
-
lastOutboundAt: null,
|
|
50
|
-
recentInboundSummary: null,
|
|
51
|
-
recentOutboundSummary: null,
|
|
52
|
-
recentIssueCategory: null,
|
|
53
|
-
recentIssueSummary: null,
|
|
54
|
-
transportSessions: [],
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
get account() {
|
|
59
|
-
return this.resolved.account;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async handleEvent(event: UnifiedInboundEvent, replyHandle: ReplyHandle): Promise<void> {
|
|
63
|
-
const dispatchStartedAt = Date.now();
|
|
64
|
-
this.runtimeStatus.lastInboundAt = Date.now();
|
|
65
|
-
this.runtimeStatus.recentInboundSummary = `${event.transport} ${event.inboundKind} ${event.messageId}`;
|
|
66
|
-
this.log.info?.(
|
|
67
|
-
`[wecom-runtime] inbound account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId} peer=${event.conversation.peerKind}:${event.conversation.peerId}`,
|
|
68
|
-
);
|
|
69
|
-
this.log.info?.(
|
|
70
|
-
`[wecom-runtime] dispatch-start account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId}`,
|
|
71
|
-
);
|
|
72
|
-
this.emitStatus();
|
|
73
|
-
|
|
74
|
-
const trackedReplyHandle: ReplyHandle = {
|
|
75
|
-
context: replyHandle.context,
|
|
76
|
-
deliver: async (payload: ReplyPayload, info) => {
|
|
77
|
-
const deliverStartedAt = Date.now();
|
|
78
|
-
const textLen = payload.text?.trim().length ?? 0;
|
|
79
|
-
const mediaCount = (payload.mediaUrls?.length ?? 0) + (payload.mediaUrl ? 1 : 0);
|
|
80
|
-
this.log.info?.(
|
|
81
|
-
`[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)}`,
|
|
82
|
-
);
|
|
83
|
-
await replyHandle.deliver(payload, info);
|
|
84
|
-
this.runtimeStatus.lastOutboundAt = Date.now();
|
|
85
|
-
const outboundSummary =
|
|
86
|
-
payload.text?.trim() || payload.mediaUrl || payload.mediaUrls?.[0] || info.kind;
|
|
87
|
-
this.runtimeStatus.recentOutboundSummary = `${replyHandle.context.transport} ${outboundSummary.slice(0, 120)}`;
|
|
88
|
-
this.log.info?.(
|
|
89
|
-
`[wecom-runtime] outbound account=${event.accountId} transport=${replyHandle.context.transport} kind=${info.kind} messageId=${event.messageId} summary=${JSON.stringify(this.runtimeStatus.recentOutboundSummary)}`,
|
|
90
|
-
);
|
|
91
|
-
this.log.info?.(
|
|
92
|
-
`[wecom-runtime] deliver-done account=${event.accountId} transport=${replyHandle.context.transport} kind=${info.kind} messageId=${event.messageId} durationMs=${Date.now() - deliverStartedAt}`,
|
|
93
|
-
);
|
|
94
|
-
this.emitStatus();
|
|
95
|
-
},
|
|
96
|
-
fail: async (error: unknown) => {
|
|
97
|
-
const formattedError = formatErrorMessage(error);
|
|
98
|
-
this.recordOperationalIssue({
|
|
99
|
-
transport: replyHandle.context.transport,
|
|
100
|
-
category: "runtime-error",
|
|
101
|
-
messageId: event.messageId,
|
|
102
|
-
raw: replyHandle.context.raw,
|
|
103
|
-
summary: `reply-fail ${formattedError}`,
|
|
104
|
-
error: formattedError,
|
|
105
|
-
});
|
|
106
|
-
this.log.error?.(
|
|
107
|
-
`[wecom-runtime] reply-fail account=${event.accountId} transport=${replyHandle.context.transport} messageId=${event.messageId} error=${formattedError}`,
|
|
108
|
-
);
|
|
109
|
-
await replyHandle.fail?.(error);
|
|
110
|
-
},
|
|
111
|
-
markExternalActivity: () => {
|
|
112
|
-
replyHandle.markExternalActivity?.();
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
try {
|
|
117
|
-
await dispatchInboundEvent({
|
|
118
|
-
core: this.core,
|
|
119
|
-
cfg: this.cfg,
|
|
120
|
-
store: this.store,
|
|
121
|
-
auditLog: this.auditLog,
|
|
122
|
-
mediaService: this.mediaService,
|
|
123
|
-
event,
|
|
124
|
-
replyHandle: trackedReplyHandle,
|
|
125
|
-
});
|
|
126
|
-
this.log.info?.(
|
|
127
|
-
`[wecom-runtime] dispatch-done account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId} durationMs=${Date.now() - dispatchStartedAt}`,
|
|
128
|
-
);
|
|
129
|
-
} catch (error) {
|
|
130
|
-
this.log.error?.(
|
|
131
|
-
`[wecom-runtime] dispatch-fail account=${event.accountId} transport=${event.transport} kind=${event.inboundKind} messageId=${event.messageId} durationMs=${Date.now() - dispatchStartedAt} error=${formatErrorMessage(error)}`,
|
|
132
|
-
);
|
|
133
|
-
throw error;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
updateTransportSession(snapshot: TransportSessionSnapshot): void {
|
|
138
|
-
const previous = this.store.readTransportSession(snapshot.accountId, snapshot.transport);
|
|
139
|
-
this.store.writeTransportSession(snapshot);
|
|
140
|
-
this.statusRegistry.write(snapshot);
|
|
141
|
-
this.log.info?.(
|
|
142
|
-
`[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"}`,
|
|
143
|
-
);
|
|
144
|
-
if (
|
|
145
|
-
previous?.ownerId &&
|
|
146
|
-
snapshot.ownerId &&
|
|
147
|
-
previous.ownerId !== snapshot.ownerId &&
|
|
148
|
-
previous.running
|
|
149
|
-
) {
|
|
150
|
-
this.recordOperationalIssue({
|
|
151
|
-
transport: snapshot.transport,
|
|
152
|
-
category: "owner-drift",
|
|
153
|
-
summary: `owner drift ${previous.ownerId} -> ${snapshot.ownerId}`,
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
if (snapshot.lastError) {
|
|
157
|
-
this.runtimeStatus.lastError = snapshot.lastError;
|
|
158
|
-
this.runtimeStatus.lastErrorAt = Date.now();
|
|
159
|
-
} else if (snapshot.running) {
|
|
160
|
-
this.runtimeStatus.lastError = null;
|
|
161
|
-
}
|
|
162
|
-
this.emitStatus();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
touchTransportSession(transport: WecomTransportKind, patch: TransportSessionPatch): void {
|
|
166
|
-
const current = this.store.readTransportSession(this.account.accountId, transport);
|
|
167
|
-
const next: TransportSessionSnapshot = {
|
|
168
|
-
accountId: this.account.accountId,
|
|
169
|
-
transport,
|
|
170
|
-
running: patch.running ?? current?.running ?? true,
|
|
171
|
-
ownerId: patch.ownerId ?? current?.ownerId,
|
|
172
|
-
connected: patch.connected ?? current?.connected,
|
|
173
|
-
authenticated: patch.authenticated ?? current?.authenticated,
|
|
174
|
-
lastConnectedAt: patch.lastConnectedAt ?? current?.lastConnectedAt,
|
|
175
|
-
lastDisconnectedAt: patch.lastDisconnectedAt ?? current?.lastDisconnectedAt,
|
|
176
|
-
lastInboundAt: patch.lastInboundAt ?? current?.lastInboundAt,
|
|
177
|
-
lastOutboundAt: patch.lastOutboundAt ?? current?.lastOutboundAt,
|
|
178
|
-
lastError: "lastError" in patch ? (patch.lastError ?? undefined) : current?.lastError,
|
|
179
|
-
};
|
|
180
|
-
this.updateTransportSession(next);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
listTransportSessions() {
|
|
184
|
-
return this.statusRegistry.read(this.account.accountId);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
listAuditEntries() {
|
|
188
|
-
return this.auditLog.list();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
buildRuntimeStatus(): AccountRuntimeStatusSnapshot {
|
|
192
|
-
const sessions = this.listTransportSessions();
|
|
193
|
-
const primarySession = this.resolvePrimarySession(sessions);
|
|
194
|
-
return {
|
|
195
|
-
...this.runtimeStatus,
|
|
196
|
-
health: this.computeHealth(sessions),
|
|
197
|
-
transport: primarySession?.transport,
|
|
198
|
-
ownerId: primarySession?.ownerId ?? this.runtimeStatus.ownerId ?? null,
|
|
199
|
-
connected: primarySession?.connected,
|
|
200
|
-
authenticated: primarySession?.authenticated,
|
|
201
|
-
lastError:
|
|
202
|
-
primarySession?.lastError ??
|
|
203
|
-
(primarySession?.running ? null : (this.runtimeStatus.lastError ?? null)),
|
|
204
|
-
transportSessions: summarizeTransportSessions(sessions),
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
recordOperationalIssue(params: {
|
|
209
|
-
transport: WecomTransportKind;
|
|
210
|
-
category: WecomAuditCategory;
|
|
211
|
-
summary: string;
|
|
212
|
-
messageId?: string;
|
|
213
|
-
raw?: ReplyHandle["context"]["raw"];
|
|
214
|
-
error?: string;
|
|
215
|
-
}): void {
|
|
216
|
-
this.auditLog.appendOperational({
|
|
217
|
-
accountId: this.account.accountId,
|
|
218
|
-
transport: params.transport,
|
|
219
|
-
category: params.category,
|
|
220
|
-
messageId: params.messageId,
|
|
221
|
-
summary: params.summary,
|
|
222
|
-
raw: params.raw,
|
|
223
|
-
error: params.error,
|
|
224
|
-
});
|
|
225
|
-
if (params.category === "owner-drift" || params.category === "ws-kicked") {
|
|
226
|
-
this.runtimeStatus.ownerDriftAt = Date.now();
|
|
227
|
-
}
|
|
228
|
-
this.runtimeStatus.lastError = params.error ?? params.summary;
|
|
229
|
-
this.runtimeStatus.lastErrorAt = Date.now();
|
|
230
|
-
this.runtimeStatus.recentIssueCategory = params.category;
|
|
231
|
-
this.runtimeStatus.recentIssueSummary = params.summary;
|
|
232
|
-
const sink =
|
|
233
|
-
params.category === "runtime-error" || params.category === "fallback-delivery-failed"
|
|
234
|
-
? this.log.error
|
|
235
|
-
: this.log.warn;
|
|
236
|
-
sink?.(
|
|
237
|
-
`[wecom-runtime] issue account=${this.account.accountId} transport=${params.transport} category=${params.category} messageId=${params.messageId ?? "n/a"} summary=${params.summary}`,
|
|
238
|
-
);
|
|
239
|
-
this.emitStatus();
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
private emitStatus(): void {
|
|
243
|
-
this.statusSink?.(this.buildRuntimeStatus() as unknown as Record<string, unknown>);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
private resolvePrimarySession(
|
|
247
|
-
sessions: TransportSessionSnapshot[],
|
|
248
|
-
): TransportSessionSnapshot | undefined {
|
|
249
|
-
const primaryTransport = this.account.bot?.configured
|
|
250
|
-
? this.account.bot.primaryTransport === "ws"
|
|
251
|
-
? "bot-ws"
|
|
252
|
-
: "bot-webhook"
|
|
253
|
-
: this.account.agent?.callbackConfigured
|
|
254
|
-
? "agent-callback"
|
|
255
|
-
: undefined;
|
|
256
|
-
if (!primaryTransport) {
|
|
257
|
-
return sessions[0];
|
|
258
|
-
}
|
|
259
|
-
return sessions.find((session) => session.transport === primaryTransport) ?? sessions[0];
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
private computeHealth(sessions: TransportSessionSnapshot[]): WecomRuntimeHealth {
|
|
263
|
-
if (sessions.length === 0) {
|
|
264
|
-
return this.runtimeStatus.lastError ? "down" : "idle";
|
|
265
|
-
}
|
|
266
|
-
const hasRunning = sessions.some((session) => session.running);
|
|
267
|
-
const hasError = sessions.some((session) => Boolean(session.lastError));
|
|
268
|
-
if (hasRunning && !hasError) {
|
|
269
|
-
return "healthy";
|
|
270
|
-
}
|
|
271
|
-
if (hasRunning) {
|
|
272
|
-
return "degraded";
|
|
273
|
-
}
|
|
274
|
-
return hasError ? "down" : "idle";
|
|
275
|
-
}
|
|
276
|
-
}
|
package/src/app/bootstrap.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { ChannelGatewayContext } from "openclaw/plugin-sdk";
|
|
2
|
-
|
|
3
|
-
import { resolveWecomRuntimeAccount } from "../config/runtime-config.js";
|
|
4
|
-
import type { ResolvedWecomAccount } from "../types/index.js";
|
|
5
|
-
import { WecomAccountRuntime } from "./account-runtime.js";
|
|
6
|
-
import { getWecomRuntime } from "./index.js";
|
|
7
|
-
|
|
8
|
-
export function createAccountRuntime(ctx: ChannelGatewayContext<ResolvedWecomAccount>): WecomAccountRuntime {
|
|
9
|
-
const resolved = resolveWecomRuntimeAccount({
|
|
10
|
-
cfg: ctx.cfg,
|
|
11
|
-
accountId: ctx.accountId,
|
|
12
|
-
});
|
|
13
|
-
return new WecomAccountRuntime(
|
|
14
|
-
getWecomRuntime(),
|
|
15
|
-
ctx.cfg,
|
|
16
|
-
resolved,
|
|
17
|
-
{
|
|
18
|
-
info: (message) => ctx.log?.info(message),
|
|
19
|
-
warn: (message) => ctx.log?.warn(message),
|
|
20
|
-
error: (message) => ctx.log?.error(message),
|
|
21
|
-
},
|
|
22
|
-
(snapshot) => {
|
|
23
|
-
ctx.setStatus({
|
|
24
|
-
accountId: ctx.accountId,
|
|
25
|
-
...snapshot,
|
|
26
|
-
});
|
|
27
|
-
},
|
|
28
|
-
);
|
|
29
|
-
}
|
package/src/app/index.ts
DELETED
|
@@ -1,192 +0,0 @@
|
|
|
1
|
-
import type { PluginRuntime } from "openclaw/plugin-sdk";
|
|
2
|
-
import { clearWecomSourceAccount } from "../runtime/source-registry.js";
|
|
3
|
-
import type { ReplyHandle } from "../types/index.js";
|
|
4
|
-
import { WecomAccountRuntime } from "./account-runtime.js";
|
|
5
|
-
|
|
6
|
-
let runtime: PluginRuntime | null = null;
|
|
7
|
-
const runtimes = new Map<string, WecomAccountRuntime>();
|
|
8
|
-
const botWsPushHandles = new Map<string, BotWsPushHandle>();
|
|
9
|
-
const activeBotWsReplyHandlesBySession = new Map<string, ReplyHandle>();
|
|
10
|
-
const activeBotWsReplyHandlesByPeer = new Map<string, ReplyHandle>();
|
|
11
|
-
|
|
12
|
-
export type BotWsPushHandle = {
|
|
13
|
-
isConnected: () => boolean;
|
|
14
|
-
sendMarkdown: (chatId: string, content: string) => Promise<void>;
|
|
15
|
-
replyCommand: (params: {
|
|
16
|
-
cmd: string;
|
|
17
|
-
body?: Record<string, unknown>;
|
|
18
|
-
headers?: ({ req_id?: string } & Record<string, string>) | undefined;
|
|
19
|
-
}) => Promise<Record<string, unknown>>;
|
|
20
|
-
sendMedia: (params: {
|
|
21
|
-
chatId: string;
|
|
22
|
-
mediaUrl: string;
|
|
23
|
-
text?: string;
|
|
24
|
-
mediaLocalRoots?: readonly string[];
|
|
25
|
-
maxBytes?: number;
|
|
26
|
-
}) => Promise<{
|
|
27
|
-
ok: boolean;
|
|
28
|
-
messageId?: string;
|
|
29
|
-
rejected?: boolean;
|
|
30
|
-
rejectReason?: string;
|
|
31
|
-
error?: string;
|
|
32
|
-
}>;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
function normalizeOptional(value: string | null | undefined): string | undefined {
|
|
36
|
-
const trimmed = String(value ?? "").trim();
|
|
37
|
-
return trimmed || undefined;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function normalizePeerId(value: string | null | undefined): string | undefined {
|
|
41
|
-
const trimmed = normalizeOptional(value);
|
|
42
|
-
return trimmed ? trimmed.toLowerCase() : undefined;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function buildSessionHandleKey(accountId: string, sessionKey: string): string {
|
|
46
|
-
return `${accountId}::session::${sessionKey}`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function buildPeerHandleKey(
|
|
50
|
-
accountId: string,
|
|
51
|
-
peerKind: "direct" | "group",
|
|
52
|
-
peerId: string,
|
|
53
|
-
): string {
|
|
54
|
-
return `${accountId}::peer::${peerKind}::${peerId}`;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function setWecomRuntime(next: PluginRuntime): void {
|
|
58
|
-
runtime = next;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function getWecomRuntime(): PluginRuntime {
|
|
62
|
-
if (!runtime) {
|
|
63
|
-
throw new Error("WeCom runtime not initialized");
|
|
64
|
-
}
|
|
65
|
-
return runtime;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function registerAccountRuntime(accountRuntime: WecomAccountRuntime): void {
|
|
69
|
-
runtimes.set(accountRuntime.account.accountId, accountRuntime);
|
|
70
|
-
console.log(`[wecom-runtime] register account=${accountRuntime.account.accountId}`);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function getAccountRuntime(accountId: string): WecomAccountRuntime | undefined {
|
|
74
|
-
return runtimes.get(accountId);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function getAccountRuntimeSnapshot(accountId: string) {
|
|
78
|
-
return runtimes.get(accountId)?.buildRuntimeStatus();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function registerBotWsPushHandle(accountId: string, handle: BotWsPushHandle): void {
|
|
82
|
-
botWsPushHandles.set(accountId, handle);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function getBotWsPushHandle(accountId: string): BotWsPushHandle | undefined {
|
|
86
|
-
return botWsPushHandles.get(accountId);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function registerActiveBotWsReplyHandle(params: {
|
|
90
|
-
accountId: string;
|
|
91
|
-
sessionKey?: string | null;
|
|
92
|
-
peerKind?: "direct" | "group" | null;
|
|
93
|
-
peerId?: string | null;
|
|
94
|
-
handle: ReplyHandle;
|
|
95
|
-
}): void {
|
|
96
|
-
const accountId = normalizeOptional(params.accountId);
|
|
97
|
-
const sessionKey = normalizeOptional(params.sessionKey);
|
|
98
|
-
const peerId = normalizePeerId(params.peerId);
|
|
99
|
-
if (!accountId) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
if (sessionKey) {
|
|
103
|
-
activeBotWsReplyHandlesBySession.set(
|
|
104
|
-
buildSessionHandleKey(accountId, sessionKey),
|
|
105
|
-
params.handle,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
if ((params.peerKind === "direct" || params.peerKind === "group") && peerId) {
|
|
109
|
-
activeBotWsReplyHandlesByPeer.set(
|
|
110
|
-
buildPeerHandleKey(accountId, params.peerKind, peerId),
|
|
111
|
-
params.handle,
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function getActiveBotWsReplyHandle(params: {
|
|
117
|
-
accountId: string;
|
|
118
|
-
sessionKey?: string | null;
|
|
119
|
-
peerKind?: "direct" | "group" | null;
|
|
120
|
-
peerId?: string | null;
|
|
121
|
-
}): ReplyHandle | undefined {
|
|
122
|
-
const accountId = normalizeOptional(params.accountId);
|
|
123
|
-
const sessionKey = normalizeOptional(params.sessionKey);
|
|
124
|
-
const peerId = normalizePeerId(params.peerId);
|
|
125
|
-
if (!accountId) {
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
if (sessionKey) {
|
|
129
|
-
const handle = activeBotWsReplyHandlesBySession.get(
|
|
130
|
-
buildSessionHandleKey(accountId, sessionKey),
|
|
131
|
-
);
|
|
132
|
-
if (handle) {
|
|
133
|
-
return handle;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if ((params.peerKind === "direct" || params.peerKind === "group") && peerId) {
|
|
137
|
-
return activeBotWsReplyHandlesByPeer.get(
|
|
138
|
-
buildPeerHandleKey(accountId, params.peerKind, peerId),
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
return undefined;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export function unregisterActiveBotWsReplyHandle(params: {
|
|
145
|
-
accountId: string;
|
|
146
|
-
sessionKey?: string | null;
|
|
147
|
-
peerKind?: "direct" | "group" | null;
|
|
148
|
-
peerId?: string | null;
|
|
149
|
-
handle?: ReplyHandle;
|
|
150
|
-
}): void {
|
|
151
|
-
const accountId = normalizeOptional(params.accountId);
|
|
152
|
-
const sessionKey = normalizeOptional(params.sessionKey);
|
|
153
|
-
const peerId = normalizePeerId(params.peerId);
|
|
154
|
-
if (!accountId) {
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (sessionKey) {
|
|
158
|
-
const key = buildSessionHandleKey(accountId, sessionKey);
|
|
159
|
-
const current = activeBotWsReplyHandlesBySession.get(key);
|
|
160
|
-
if (!params.handle || current === params.handle) {
|
|
161
|
-
activeBotWsReplyHandlesBySession.delete(key);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if ((params.peerKind === "direct" || params.peerKind === "group") && peerId) {
|
|
165
|
-
const key = buildPeerHandleKey(accountId, params.peerKind, peerId);
|
|
166
|
-
const current = activeBotWsReplyHandlesByPeer.get(key);
|
|
167
|
-
if (!params.handle || current === params.handle) {
|
|
168
|
-
activeBotWsReplyHandlesByPeer.delete(key);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export function unregisterBotWsPushHandle(accountId: string): void {
|
|
174
|
-
botWsPushHandles.delete(accountId);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function unregisterAccountRuntime(accountId: string): void {
|
|
178
|
-
runtimes.delete(accountId);
|
|
179
|
-
botWsPushHandles.delete(accountId);
|
|
180
|
-
for (const key of activeBotWsReplyHandlesBySession.keys()) {
|
|
181
|
-
if (key.startsWith(`${accountId}::`)) {
|
|
182
|
-
activeBotWsReplyHandlesBySession.delete(key);
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
for (const key of activeBotWsReplyHandlesByPeer.keys()) {
|
|
186
|
-
if (key.startsWith(`${accountId}::`)) {
|
|
187
|
-
activeBotWsReplyHandlesByPeer.delete(key);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
clearWecomSourceAccount(accountId);
|
|
191
|
-
console.log(`[wecom-runtime] unregister account=${accountId}`);
|
|
192
|
-
}
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
2
|
-
import { resolveScopedWecomTarget } from "../../target.js";
|
|
3
|
-
import { deliverAgentApiMedia, deliverAgentApiText } from "../../transport/agent-api/delivery.js";
|
|
4
|
-
import { canUseAgentApiDelivery } from "./fallback-policy.js";
|
|
5
|
-
import { getWecomRuntime } from "../../runtime.js";
|
|
6
|
-
|
|
7
|
-
export class WecomAgentDeliveryService {
|
|
8
|
-
constructor(private readonly agent: ResolvedAgentAccount) { }
|
|
9
|
-
|
|
10
|
-
assertAvailable(): void {
|
|
11
|
-
if (!canUseAgentApiDelivery(this.agent)) {
|
|
12
|
-
throw new Error(
|
|
13
|
-
`WeCom outbound requires channels.wecom.accounts.<accountId>.agent.agentId (or legacy channels.wecom.agent.agentId) for account=${this.agent.accountId}.`,
|
|
14
|
-
);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
resolveTargetOrThrow(to: string | undefined) {
|
|
19
|
-
const scoped = resolveScopedWecomTarget(to, this.agent.accountId);
|
|
20
|
-
if (!scoped) {
|
|
21
|
-
console.error(`[wecom-agent-delivery] missing target account=${this.agent.accountId}`);
|
|
22
|
-
throw new Error("WeCom outbound requires a target (userid, partyid, tagid or chatid).");
|
|
23
|
-
}
|
|
24
|
-
if (scoped.accountId && scoped.accountId !== this.agent.accountId) {
|
|
25
|
-
console.error(
|
|
26
|
-
`[wecom-agent-delivery] account mismatch current=${this.agent.accountId} targetAccount=${scoped.accountId} raw=${String(to ?? "")}`,
|
|
27
|
-
);
|
|
28
|
-
throw new Error(
|
|
29
|
-
`WeCom outbound account mismatch: target belongs to account=${scoped.accountId}, current account=${this.agent.accountId}.`,
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
const target = scoped.target;
|
|
33
|
-
if (target.chatid) {
|
|
34
|
-
console.warn(
|
|
35
|
-
`[wecom-agent-delivery] blocked chat target account=${this.agent.accountId} chatId=${target.chatid}`,
|
|
36
|
-
);
|
|
37
|
-
throw new Error(
|
|
38
|
-
`企业微信(WeCom)Agent 主动发送不支持向群 chatId 发送(chatId=${target.chatid})。` +
|
|
39
|
-
`该路径在实际环境中经常失败(例如 86008:无权限访问该会话/会话由其他应用创建)。` +
|
|
40
|
-
`请改为发送给用户(userid / user:xxx),或由 Bot 模式在群内交付。`,
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
return target;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async sendText(params: { to: string | undefined; text: string }): Promise<void> {
|
|
47
|
-
this.assertAvailable();
|
|
48
|
-
const target = this.resolveTargetOrThrow(params.to);
|
|
49
|
-
console.log(
|
|
50
|
-
`[wecom-agent-delivery] sendText account=${this.agent.accountId} to=${String(params.to ?? "")} len=${params.text.length}`,
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
const runtime = getWecomRuntime();
|
|
54
|
-
const chunks = runtime.channel.text.chunkText(params.text, 2048);
|
|
55
|
-
|
|
56
|
-
for (const chunk of chunks) {
|
|
57
|
-
if (!chunk.trim()) continue;
|
|
58
|
-
await deliverAgentApiText({
|
|
59
|
-
agent: this.agent,
|
|
60
|
-
target,
|
|
61
|
-
text: chunk,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async sendMedia(params: {
|
|
67
|
-
to: string | undefined;
|
|
68
|
-
text?: string;
|
|
69
|
-
buffer: Buffer;
|
|
70
|
-
filename: string;
|
|
71
|
-
contentType: string;
|
|
72
|
-
}): Promise<void> {
|
|
73
|
-
this.assertAvailable();
|
|
74
|
-
const target = this.resolveTargetOrThrow(params.to);
|
|
75
|
-
console.log(
|
|
76
|
-
`[wecom-agent-delivery] sendMedia account=${this.agent.accountId} to=${String(params.to ?? "")} filename=${params.filename} contentType=${params.contentType}`,
|
|
77
|
-
);
|
|
78
|
-
await deliverAgentApiMedia({
|
|
79
|
-
agent: this.agent,
|
|
80
|
-
target,
|
|
81
|
-
buffer: params.buffer,
|
|
82
|
-
filename: params.filename,
|
|
83
|
-
contentType: params.contentType,
|
|
84
|
-
text: params.text,
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
2
|
-
|
|
3
|
-
export function canUseAgentApiDelivery(agent: ResolvedAgentAccount | undefined): boolean {
|
|
4
|
-
return Boolean(agent?.apiConfigured && typeof agent.agentId === "number");
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function shouldFallbackToAgentApi(params: {
|
|
8
|
-
agent: ResolvedAgentAccount | undefined;
|
|
9
|
-
hasText?: boolean;
|
|
10
|
-
hasMedia?: boolean;
|
|
11
|
-
}): boolean {
|
|
12
|
-
return canUseAgentApiDelivery(params.agent) && Boolean(params.hasText || params.hasMedia);
|
|
13
|
-
}
|
|
@@ -1,38 +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 { startAgentCallbackTransport } from "../../transport/agent-callback/http-handler.js";
|
|
6
|
-
|
|
7
|
-
export class WecomAgentIngressService {
|
|
8
|
-
private stopTransport?: () => void;
|
|
9
|
-
|
|
10
|
-
constructor(
|
|
11
|
-
private readonly runtime: WecomAccountRuntime,
|
|
12
|
-
private readonly cfg: OpenClawConfig,
|
|
13
|
-
private readonly runtimeEnv: WecomRuntimeEnv,
|
|
14
|
-
) {}
|
|
15
|
-
|
|
16
|
-
start(): { transport: "agent-callback"; descriptors: string[] } | undefined {
|
|
17
|
-
const agent = this.runtime.account.agent;
|
|
18
|
-
if (!agent?.callbackConfigured) {
|
|
19
|
-
return undefined;
|
|
20
|
-
}
|
|
21
|
-
const callback = startAgentCallbackTransport({
|
|
22
|
-
account: agent,
|
|
23
|
-
cfg: this.cfg,
|
|
24
|
-
runtime: this.runtime,
|
|
25
|
-
runtimeEnv: this.runtimeEnv,
|
|
26
|
-
});
|
|
27
|
-
this.stopTransport = callback.stop;
|
|
28
|
-
return {
|
|
29
|
-
transport: "agent-callback",
|
|
30
|
-
descriptors: callback.paths,
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
stop(): void {
|
|
35
|
-
this.stopTransport?.();
|
|
36
|
-
this.stopTransport = undefined;
|
|
37
|
-
}
|
|
38
|
-
}
|