@yanhaidao/wecom 2.3.4 → 2.3.10
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 +222 -335
- package/assets/03.bot.page.png +0 -0
- package/changelog/v2.3.10.md +17 -0
- package/changelog/v2.3.9.md +22 -0
- package/compat-single-account.md +34 -4
- package/index.ts +5 -5
- package/package.json +8 -7
- package/src/agent/api-client.upload.test.ts +1 -2
- package/src/agent/handler.ts +82 -9
- package/src/agent/index.ts +1 -1
- package/src/app/account-runtime.ts +245 -0
- package/src/app/bootstrap.ts +29 -0
- package/src/app/index.ts +31 -0
- package/src/capability/agent/delivery-service.ts +79 -0
- package/src/capability/agent/fallback-policy.ts +13 -0
- package/src/capability/agent/index.ts +3 -0
- package/src/capability/agent/ingress-service.ts +38 -0
- package/src/capability/bot/dispatch-config.ts +47 -0
- package/src/capability/bot/fallback-delivery.ts +178 -0
- package/src/capability/bot/index.ts +1 -0
- package/src/capability/bot/local-path-delivery.ts +215 -0
- package/src/capability/bot/service.ts +56 -0
- package/src/capability/bot/stream-delivery.ts +379 -0
- package/src/capability/bot/stream-finalizer.ts +120 -0
- package/src/capability/bot/stream-orchestrator.ts +352 -0
- package/src/capability/bot/types.ts +8 -0
- package/src/capability/index.ts +2 -0
- package/src/channel.lifecycle.test.ts +9 -6
- package/src/channel.meta.test.ts +12 -0
- package/src/channel.ts +48 -21
- package/src/config/accounts.resolve.test.ts +39 -2
- package/src/config/accounts.ts +242 -280
- package/src/config/derived-paths.test.ts +111 -0
- package/src/config/derived-paths.ts +41 -0
- package/src/config/index.ts +10 -12
- package/src/config/runtime-config.ts +46 -0
- package/src/config/schema.ts +65 -103
- package/src/domain/models.ts +7 -0
- package/src/domain/policies.ts +36 -0
- package/src/dynamic-agent.ts +6 -0
- package/src/gateway-monitor.ts +43 -93
- package/src/http.ts +23 -2
- package/src/monitor/limits.ts +7 -0
- package/src/monitor/state.ts +28 -508
- package/src/monitor.active.test.ts +3 -3
- package/src/monitor.integration.test.ts +0 -1
- package/src/monitor.ts +64 -2603
- package/src/monitor.webhook.test.ts +127 -42
- package/src/observability/audit-log.ts +48 -0
- package/src/observability/legacy-operational-event-store.ts +36 -0
- package/src/observability/raw-envelope-log.ts +28 -0
- package/src/observability/status-registry.ts +13 -0
- package/src/observability/transport-session-view.ts +14 -0
- package/src/onboarding.test.ts +268 -0
- package/src/onboarding.ts +95 -78
- package/src/outbound.test.ts +5 -5
- package/src/outbound.ts +18 -66
- package/src/runtime/dispatcher.ts +52 -0
- package/src/runtime/index.ts +4 -0
- package/src/runtime/outbound-intent.ts +4 -0
- package/src/runtime/reply-orchestrator.test.ts +38 -0
- package/src/runtime/reply-orchestrator.ts +55 -0
- package/src/runtime/routing-bridge.ts +19 -0
- package/src/runtime/session-manager.ts +76 -0
- package/src/runtime.ts +7 -14
- package/src/shared/command-auth.ts +1 -17
- package/src/shared/media-service.ts +36 -0
- package/src/shared/media-types.ts +5 -0
- package/src/store/active-reply-store.ts +42 -0
- package/src/store/interfaces.ts +11 -0
- package/src/store/memory-store.ts +43 -0
- package/src/store/stream-batch-store.ts +350 -0
- package/src/target.ts +28 -0
- package/src/transport/agent-api/client.ts +44 -0
- package/src/transport/agent-api/core.ts +367 -0
- package/src/transport/agent-api/delivery.ts +41 -0
- package/src/transport/agent-api/media-upload.ts +11 -0
- package/src/transport/agent-api/reply.ts +39 -0
- package/src/transport/agent-callback/http-handler.ts +47 -0
- package/src/transport/agent-callback/inbound.ts +5 -0
- package/src/transport/agent-callback/reply.ts +13 -0
- package/src/transport/agent-callback/request-handler.ts +244 -0
- package/src/transport/agent-callback/session.ts +23 -0
- package/src/transport/bot-webhook/active-reply.ts +36 -0
- package/src/transport/bot-webhook/http-handler.ts +48 -0
- package/src/transport/bot-webhook/inbound-normalizer.ts +371 -0
- package/src/transport/bot-webhook/inbound.ts +5 -0
- package/src/transport/bot-webhook/message-shape.ts +89 -0
- package/src/transport/bot-webhook/protocol.ts +148 -0
- package/src/transport/bot-webhook/reply.ts +15 -0
- package/src/transport/bot-webhook/request-handler.ts +394 -0
- package/src/transport/bot-webhook/session.ts +23 -0
- package/src/transport/bot-ws/inbound.ts +108 -0
- package/src/transport/bot-ws/reply.ts +63 -0
- package/src/transport/bot-ws/sdk-adapter.ts +180 -0
- package/src/transport/bot-ws/session.ts +28 -0
- package/src/transport/http/common.ts +109 -0
- package/src/transport/http/registry.ts +92 -0
- package/src/transport/http/request-handler.ts +84 -0
- package/src/transport/index.ts +14 -0
- package/src/types/account.ts +56 -91
- package/src/types/config.ts +64 -112
- package/src/types/constants.ts +20 -35
- package/src/types/events.ts +21 -0
- package/src/types/index.ts +14 -38
- package/src/types/legacy-stream.ts +50 -0
- package/src/types/runtime-context.ts +28 -0
- package/src/types/runtime.ts +161 -0
- package/src/agent/api-client.ts +0 -383
- package/src/monitor/types.ts +0 -136
package/src/target.ts
CHANGED
|
@@ -20,6 +20,12 @@ export interface WecomTarget {
|
|
|
20
20
|
chatid?: string;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
export interface ScopedWecomTarget {
|
|
24
|
+
accountId?: string;
|
|
25
|
+
target: WecomTarget;
|
|
26
|
+
rawTarget: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
/**
|
|
24
30
|
* Parses a raw target string into a WeComTarget object.
|
|
25
31
|
* 解析原始目标字符串为 WeComTarget 对象。
|
|
@@ -78,3 +84,25 @@ export function resolveWecomTarget(raw: string | undefined): WecomTarget | undef
|
|
|
78
84
|
// Default to User (默认为用户)
|
|
79
85
|
return { touser: clean };
|
|
80
86
|
}
|
|
87
|
+
|
|
88
|
+
export function resolveScopedWecomTarget(raw: string | undefined, defaultAccountId?: string): ScopedWecomTarget | undefined {
|
|
89
|
+
if (!raw?.trim()) return undefined;
|
|
90
|
+
|
|
91
|
+
const trimmed = raw.trim();
|
|
92
|
+
const agentScoped = trimmed.match(/^wecom-agent:([^:]+):(.+)$/i);
|
|
93
|
+
if (agentScoped) {
|
|
94
|
+
const accountId = agentScoped[1]?.trim() || defaultAccountId;
|
|
95
|
+
const rawTarget = agentScoped[2]?.trim() || "";
|
|
96
|
+
const target = resolveWecomTarget(rawTarget);
|
|
97
|
+
return target ? { accountId, target, rawTarget } : undefined;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const target = resolveWecomTarget(trimmed);
|
|
101
|
+
return target
|
|
102
|
+
? {
|
|
103
|
+
accountId: defaultAccountId,
|
|
104
|
+
target,
|
|
105
|
+
rawTarget: trimmed,
|
|
106
|
+
}
|
|
107
|
+
: undefined;
|
|
108
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
2
|
+
import {
|
|
3
|
+
downloadMedia as downloadLegacyMedia,
|
|
4
|
+
getAccessToken as getLegacyAccessToken,
|
|
5
|
+
sendMedia as sendLegacyMedia,
|
|
6
|
+
sendText as sendLegacyText,
|
|
7
|
+
} from "./core.js";
|
|
8
|
+
|
|
9
|
+
export async function getAgentApiAccessToken(agent: ResolvedAgentAccount): Promise<string> {
|
|
10
|
+
return getLegacyAccessToken(agent);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function sendAgentApiText(params: {
|
|
14
|
+
agent: ResolvedAgentAccount;
|
|
15
|
+
toUser?: string;
|
|
16
|
+
toParty?: string;
|
|
17
|
+
toTag?: string;
|
|
18
|
+
chatId?: string;
|
|
19
|
+
text: string;
|
|
20
|
+
}): Promise<void> {
|
|
21
|
+
await sendLegacyText(params);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function sendAgentApiMedia(params: {
|
|
25
|
+
agent: ResolvedAgentAccount;
|
|
26
|
+
toUser?: string;
|
|
27
|
+
toParty?: string;
|
|
28
|
+
toTag?: string;
|
|
29
|
+
chatId?: string;
|
|
30
|
+
mediaId: string;
|
|
31
|
+
mediaType: "image" | "voice" | "video" | "file";
|
|
32
|
+
title?: string;
|
|
33
|
+
description?: string;
|
|
34
|
+
}): Promise<void> {
|
|
35
|
+
await sendLegacyMedia(params);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function downloadAgentApiMedia(params: {
|
|
39
|
+
agent: ResolvedAgentAccount;
|
|
40
|
+
mediaId: string;
|
|
41
|
+
maxBytes?: number;
|
|
42
|
+
}): Promise<{ buffer: Buffer; contentType: string; filename?: string }> {
|
|
43
|
+
return downloadLegacyMedia(params);
|
|
44
|
+
}
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
|
|
3
|
+
import { resolveWecomEgressProxyUrlFromNetwork } from "../../config/index.js";
|
|
4
|
+
import { readResponseBodyAsBuffer, wecomFetch } from "../../http.js";
|
|
5
|
+
import { API_ENDPOINTS, LIMITS } from "../../types/constants.js";
|
|
6
|
+
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
7
|
+
|
|
8
|
+
type TokenCache = {
|
|
9
|
+
token: string;
|
|
10
|
+
expiresAt: number;
|
|
11
|
+
refreshPromise: Promise<string> | null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const tokenCaches = new Map<string, TokenCache>();
|
|
15
|
+
|
|
16
|
+
function truncateForLog(raw: string, maxChars = 180): string {
|
|
17
|
+
const compact = raw.replace(/\s+/g, " ").trim();
|
|
18
|
+
if (compact.length <= maxChars) return compact;
|
|
19
|
+
return `${compact.slice(0, maxChars)}...(truncated)`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function normalizeUploadFilename(filename: string): string {
|
|
23
|
+
const trimmed = filename.trim();
|
|
24
|
+
if (!trimmed) return "file.bin";
|
|
25
|
+
const ext = trimmed.includes(".") ? `.${trimmed.split(".").pop()!.toLowerCase()}` : "";
|
|
26
|
+
const base = ext ? trimmed.slice(0, -ext.length) : trimmed;
|
|
27
|
+
const sanitizedBase = base
|
|
28
|
+
.replace(/[^\x20-\x7e]/g, "_")
|
|
29
|
+
.replace(/["\\/;=]/g, "_")
|
|
30
|
+
.replace(/\s+/g, "_")
|
|
31
|
+
.replace(/_+/g, "_")
|
|
32
|
+
.replace(/^_+|_+$/g, "");
|
|
33
|
+
const safeBase = sanitizedBase || "file";
|
|
34
|
+
const safeExt = ext.replace(/[^a-z0-9.]/g, "");
|
|
35
|
+
return `${safeBase}${safeExt || ".bin"}`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function guessUploadContentType(filename: string): string {
|
|
39
|
+
const ext = filename.split(".").pop()?.toLowerCase() || "";
|
|
40
|
+
const contentTypeMap: Record<string, string> = {
|
|
41
|
+
jpg: "image/jpg",
|
|
42
|
+
jpeg: "image/jpeg",
|
|
43
|
+
png: "image/png",
|
|
44
|
+
gif: "image/gif",
|
|
45
|
+
webp: "image/webp",
|
|
46
|
+
bmp: "image/bmp",
|
|
47
|
+
amr: "voice/amr",
|
|
48
|
+
mp3: "audio/mpeg",
|
|
49
|
+
wav: "audio/wav",
|
|
50
|
+
m4a: "audio/mp4",
|
|
51
|
+
ogg: "audio/ogg",
|
|
52
|
+
mp4: "video/mp4",
|
|
53
|
+
mov: "video/quicktime",
|
|
54
|
+
txt: "text/plain",
|
|
55
|
+
md: "text/markdown",
|
|
56
|
+
csv: "text/csv",
|
|
57
|
+
tsv: "text/tab-separated-values",
|
|
58
|
+
json: "application/json",
|
|
59
|
+
xml: "application/xml",
|
|
60
|
+
yaml: "application/yaml",
|
|
61
|
+
yml: "application/yaml",
|
|
62
|
+
pdf: "application/pdf",
|
|
63
|
+
doc: "application/msword",
|
|
64
|
+
docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
65
|
+
xls: "application/vnd.ms-excel",
|
|
66
|
+
xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
67
|
+
ppt: "application/vnd.ms-powerpoint",
|
|
68
|
+
pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
69
|
+
rtf: "application/rtf",
|
|
70
|
+
odt: "application/vnd.oasis.opendocument.text",
|
|
71
|
+
zip: "application/zip",
|
|
72
|
+
rar: "application/vnd.rar",
|
|
73
|
+
"7z": "application/x-7z-compressed",
|
|
74
|
+
gz: "application/gzip",
|
|
75
|
+
tgz: "application/gzip",
|
|
76
|
+
tar: "application/x-tar",
|
|
77
|
+
};
|
|
78
|
+
return contentTypeMap[ext] || "application/octet-stream";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function requireAgentId(agent: ResolvedAgentAccount): number {
|
|
82
|
+
if (typeof agent.agentId === "number" && Number.isFinite(agent.agentId)) return agent.agentId;
|
|
83
|
+
throw new Error(`wecom agent account=${agent.accountId} missing agentId; sending via cgi-bin/message/send requires agentId`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function getAccessToken(agent: ResolvedAgentAccount): Promise<string> {
|
|
87
|
+
const cacheKey = `${agent.corpId}:${String(agent.agentId ?? "na")}`;
|
|
88
|
+
let cache = tokenCaches.get(cacheKey);
|
|
89
|
+
|
|
90
|
+
if (!cache) {
|
|
91
|
+
cache = { token: "", expiresAt: 0, refreshPromise: null };
|
|
92
|
+
tokenCaches.set(cacheKey, cache);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
if (cache.token && cache.expiresAt > now + LIMITS.TOKEN_REFRESH_BUFFER_MS) {
|
|
97
|
+
return cache.token;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (cache.refreshPromise) {
|
|
101
|
+
return cache.refreshPromise;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
cache.refreshPromise = (async () => {
|
|
105
|
+
try {
|
|
106
|
+
const url = `${API_ENDPOINTS.GET_TOKEN}?corpid=${encodeURIComponent(agent.corpId)}&corpsecret=${encodeURIComponent(agent.corpSecret)}`;
|
|
107
|
+
const res = await wecomFetch(url, undefined, {
|
|
108
|
+
proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network),
|
|
109
|
+
timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
|
|
110
|
+
});
|
|
111
|
+
const json = (await res.json()) as { access_token?: string; expires_in?: number; errcode?: number; errmsg?: string };
|
|
112
|
+
|
|
113
|
+
if (!json?.access_token) {
|
|
114
|
+
throw new Error(`gettoken failed: ${json?.errcode} ${json?.errmsg}`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
cache!.token = json.access_token;
|
|
118
|
+
cache!.expiresAt = Date.now() + (json.expires_in ?? 7200) * 1000;
|
|
119
|
+
return cache!.token;
|
|
120
|
+
} finally {
|
|
121
|
+
cache!.refreshPromise = null;
|
|
122
|
+
}
|
|
123
|
+
})();
|
|
124
|
+
|
|
125
|
+
return cache.refreshPromise;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export async function sendText(params: {
|
|
129
|
+
agent: ResolvedAgentAccount;
|
|
130
|
+
toUser?: string;
|
|
131
|
+
toParty?: string;
|
|
132
|
+
toTag?: string;
|
|
133
|
+
chatId?: string;
|
|
134
|
+
text: string;
|
|
135
|
+
}): Promise<void> {
|
|
136
|
+
const { agent, toUser, toParty, toTag, chatId, text } = params;
|
|
137
|
+
console.log(
|
|
138
|
+
`[wecom-agent-api] sendText request account=${agent.accountId} agentId=${String(agent.agentId ?? "N/A")} ` +
|
|
139
|
+
`toUser=${toUser ?? ""} toParty=${toParty ?? ""} toTag=${toTag ?? ""} chatId=${chatId ?? ""} ` +
|
|
140
|
+
`textLen=${text.length} textPreview=${JSON.stringify(truncateForLog(text))}`,
|
|
141
|
+
);
|
|
142
|
+
const token = await getAccessToken(agent);
|
|
143
|
+
|
|
144
|
+
const useChat = Boolean(chatId);
|
|
145
|
+
const url = useChat
|
|
146
|
+
? `${API_ENDPOINTS.SEND_APPCHAT}?access_token=${encodeURIComponent(token)}`
|
|
147
|
+
: `${API_ENDPOINTS.SEND_MESSAGE}?access_token=${encodeURIComponent(token)}`;
|
|
148
|
+
|
|
149
|
+
const body = useChat
|
|
150
|
+
? { chatid: chatId, msgtype: "text", text: { content: text } }
|
|
151
|
+
: {
|
|
152
|
+
touser: toUser,
|
|
153
|
+
toparty: toParty,
|
|
154
|
+
totag: toTag,
|
|
155
|
+
msgtype: "text",
|
|
156
|
+
agentid: requireAgentId(agent),
|
|
157
|
+
text: { content: text },
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const res = await wecomFetch(
|
|
161
|
+
url,
|
|
162
|
+
{
|
|
163
|
+
method: "POST",
|
|
164
|
+
headers: { "Content-Type": "application/json" },
|
|
165
|
+
body: JSON.stringify(body),
|
|
166
|
+
},
|
|
167
|
+
{ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network), timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
|
|
168
|
+
);
|
|
169
|
+
const json = (await res.json()) as {
|
|
170
|
+
errcode?: number;
|
|
171
|
+
errmsg?: string;
|
|
172
|
+
invaliduser?: string;
|
|
173
|
+
invalidparty?: string;
|
|
174
|
+
invalidtag?: string;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
console.log(
|
|
178
|
+
`[wecom-agent-api] sendText response account=${agent.accountId} agentId=${String(agent.agentId ?? "N/A")} ` +
|
|
179
|
+
`toUser=${toUser ?? ""} toParty=${toParty ?? ""} toTag=${toTag ?? ""} chatId=${chatId ?? ""} ` +
|
|
180
|
+
`errcode=${String(json?.errcode ?? "N/A")} errmsg=${json?.errmsg ?? ""} ` +
|
|
181
|
+
`invaliduser=${json?.invaliduser ?? ""} invalidparty=${json?.invalidparty ?? ""} invalidtag=${json?.invalidtag ?? ""}`,
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
if (json?.errcode !== 0) {
|
|
185
|
+
throw new Error(`send failed: ${json?.errcode} ${json?.errmsg}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
|
|
189
|
+
const details = [
|
|
190
|
+
json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
|
|
191
|
+
json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
|
|
192
|
+
json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
|
|
193
|
+
]
|
|
194
|
+
.filter(Boolean)
|
|
195
|
+
.join(", ");
|
|
196
|
+
throw new Error(`send partial failure: ${details}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function uploadMedia(params: {
|
|
201
|
+
agent: ResolvedAgentAccount;
|
|
202
|
+
type: "image" | "voice" | "video" | "file";
|
|
203
|
+
buffer: Buffer;
|
|
204
|
+
filename: string;
|
|
205
|
+
}): Promise<string> {
|
|
206
|
+
const { agent, type, buffer, filename } = params;
|
|
207
|
+
const safeFilename = normalizeUploadFilename(filename);
|
|
208
|
+
const token = await getAccessToken(agent);
|
|
209
|
+
const proxyUrl = resolveWecomEgressProxyUrlFromNetwork(agent.network);
|
|
210
|
+
const url = `${API_ENDPOINTS.UPLOAD_MEDIA}?access_token=${encodeURIComponent(token)}&type=${encodeURIComponent(type)}&debug=1`;
|
|
211
|
+
|
|
212
|
+
console.log(`[wecom-upload] Uploading media: type=${type}, filename=${safeFilename}, size=${buffer.length} bytes`);
|
|
213
|
+
|
|
214
|
+
const uploadOnce = async (fileContentType: string) => {
|
|
215
|
+
const boundary = `----WebKitFormBoundary${crypto.randomBytes(16).toString("hex")}`;
|
|
216
|
+
const header = Buffer.from(
|
|
217
|
+
`--${boundary}\r\n` +
|
|
218
|
+
`Content-Disposition: form-data; name="media"; filename="${safeFilename}"; filelength=${buffer.length}\r\n` +
|
|
219
|
+
`Content-Type: ${fileContentType}\r\n\r\n`,
|
|
220
|
+
);
|
|
221
|
+
const footer = Buffer.from(`\r\n--${boundary}--\r\n`);
|
|
222
|
+
const body = Buffer.concat([header, buffer, footer]);
|
|
223
|
+
|
|
224
|
+
console.log(`[wecom-upload] Multipart body size=${body.length}, boundary=${boundary}, fileContentType=${fileContentType}`);
|
|
225
|
+
|
|
226
|
+
const res = await wecomFetch(
|
|
227
|
+
url,
|
|
228
|
+
{
|
|
229
|
+
method: "POST",
|
|
230
|
+
headers: {
|
|
231
|
+
"Content-Type": `multipart/form-data; boundary=${boundary}`,
|
|
232
|
+
"Content-Length": String(body.length),
|
|
233
|
+
},
|
|
234
|
+
body,
|
|
235
|
+
},
|
|
236
|
+
{ proxyUrl, timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
|
|
237
|
+
);
|
|
238
|
+
const json = (await res.json()) as { media_id?: string; errcode?: number; errmsg?: string };
|
|
239
|
+
console.log(`[wecom-upload] Response:`, JSON.stringify(json));
|
|
240
|
+
return json;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const preferredContentType = guessUploadContentType(safeFilename);
|
|
244
|
+
let json = await uploadOnce(preferredContentType);
|
|
245
|
+
|
|
246
|
+
if (!json?.media_id && preferredContentType !== "application/octet-stream") {
|
|
247
|
+
console.warn(
|
|
248
|
+
`[wecom-upload] Upload failed with ${preferredContentType}, retrying as application/octet-stream: ${json?.errcode} ${json?.errmsg}`,
|
|
249
|
+
);
|
|
250
|
+
json = await uploadOnce("application/octet-stream");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (!json?.media_id) {
|
|
254
|
+
throw new Error(`upload failed: ${json?.errcode} ${json?.errmsg}`);
|
|
255
|
+
}
|
|
256
|
+
return json.media_id;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export async function sendMedia(params: {
|
|
260
|
+
agent: ResolvedAgentAccount;
|
|
261
|
+
toUser?: string;
|
|
262
|
+
toParty?: string;
|
|
263
|
+
toTag?: string;
|
|
264
|
+
chatId?: string;
|
|
265
|
+
mediaId: string;
|
|
266
|
+
mediaType: "image" | "voice" | "video" | "file";
|
|
267
|
+
title?: string;
|
|
268
|
+
description?: string;
|
|
269
|
+
}): Promise<void> {
|
|
270
|
+
const { agent, toUser, toParty, toTag, chatId, mediaId, mediaType, title, description } = params;
|
|
271
|
+
const token = await getAccessToken(agent);
|
|
272
|
+
|
|
273
|
+
const useChat = Boolean(chatId);
|
|
274
|
+
const url = useChat
|
|
275
|
+
? `${API_ENDPOINTS.SEND_APPCHAT}?access_token=${encodeURIComponent(token)}`
|
|
276
|
+
: `${API_ENDPOINTS.SEND_MESSAGE}?access_token=${encodeURIComponent(token)}`;
|
|
277
|
+
|
|
278
|
+
const mediaPayload = mediaType === "video" ? { media_id: mediaId, title: title ?? "Video", description: description ?? "" } : { media_id: mediaId };
|
|
279
|
+
const body = useChat
|
|
280
|
+
? { chatid: chatId, msgtype: mediaType, [mediaType]: mediaPayload }
|
|
281
|
+
: {
|
|
282
|
+
touser: toUser,
|
|
283
|
+
toparty: toParty,
|
|
284
|
+
totag: toTag,
|
|
285
|
+
msgtype: mediaType,
|
|
286
|
+
agentid: requireAgentId(agent),
|
|
287
|
+
[mediaType]: mediaPayload,
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const res = await wecomFetch(
|
|
291
|
+
url,
|
|
292
|
+
{
|
|
293
|
+
method: "POST",
|
|
294
|
+
headers: { "Content-Type": "application/json" },
|
|
295
|
+
body: JSON.stringify(body),
|
|
296
|
+
},
|
|
297
|
+
{ proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network), timeoutMs: LIMITS.REQUEST_TIMEOUT_MS },
|
|
298
|
+
);
|
|
299
|
+
const json = (await res.json()) as {
|
|
300
|
+
errcode?: number;
|
|
301
|
+
errmsg?: string;
|
|
302
|
+
invaliduser?: string;
|
|
303
|
+
invalidparty?: string;
|
|
304
|
+
invalidtag?: string;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
if (json?.errcode !== 0) {
|
|
308
|
+
throw new Error(`send ${mediaType} failed: ${json?.errcode} ${json?.errmsg}`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (json?.invaliduser || json?.invalidparty || json?.invalidtag) {
|
|
312
|
+
const details = [
|
|
313
|
+
json.invaliduser ? `invaliduser=${json.invaliduser}` : "",
|
|
314
|
+
json.invalidparty ? `invalidparty=${json.invalidparty}` : "",
|
|
315
|
+
json.invalidtag ? `invalidtag=${json.invalidtag}` : "",
|
|
316
|
+
]
|
|
317
|
+
.filter(Boolean)
|
|
318
|
+
.join(", ");
|
|
319
|
+
throw new Error(`send ${mediaType} partial failure: ${details}`);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export async function downloadMedia(params: {
|
|
324
|
+
agent: ResolvedAgentAccount;
|
|
325
|
+
mediaId: string;
|
|
326
|
+
maxBytes?: number;
|
|
327
|
+
}): Promise<{ buffer: Buffer; contentType: string; filename?: string }> {
|
|
328
|
+
const { agent, mediaId } = params;
|
|
329
|
+
const token = await getAccessToken(agent);
|
|
330
|
+
const url = `${API_ENDPOINTS.DOWNLOAD_MEDIA}?access_token=${encodeURIComponent(token)}&media_id=${encodeURIComponent(mediaId)}`;
|
|
331
|
+
|
|
332
|
+
const res = await wecomFetch(url, undefined, {
|
|
333
|
+
proxyUrl: resolveWecomEgressProxyUrlFromNetwork(agent.network),
|
|
334
|
+
timeoutMs: LIMITS.REQUEST_TIMEOUT_MS,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
if (!res.ok) {
|
|
338
|
+
throw new Error(`download failed: ${res.status}`);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const contentType = res.headers.get("content-type") || "application/octet-stream";
|
|
342
|
+
const disposition = res.headers.get("content-disposition") || "";
|
|
343
|
+
const filename = (() => {
|
|
344
|
+
const mStar = disposition.match(/filename\*\s*=\s*([^;]+)/i);
|
|
345
|
+
if (mStar) {
|
|
346
|
+
const raw = mStar[1]!.trim().replace(/^"(.*)"$/, "$1");
|
|
347
|
+
const parts = raw.split("''");
|
|
348
|
+
const encoded = parts.length === 2 ? parts[1]! : raw;
|
|
349
|
+
try {
|
|
350
|
+
return decodeURIComponent(encoded);
|
|
351
|
+
} catch {
|
|
352
|
+
return encoded;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const m = disposition.match(/filename\s*=\s*([^;]+)/i);
|
|
356
|
+
if (!m) return undefined;
|
|
357
|
+
return m[1]!.trim().replace(/^"(.*)"$/, "$1") || undefined;
|
|
358
|
+
})();
|
|
359
|
+
|
|
360
|
+
if (contentType.includes("application/json")) {
|
|
361
|
+
const json = (await res.json()) as { errcode?: number; errmsg?: string };
|
|
362
|
+
throw new Error(`download failed: ${json?.errcode} ${json?.errmsg}`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const buffer = await readResponseBodyAsBuffer(res, params.maxBytes);
|
|
366
|
+
return { buffer, contentType, filename };
|
|
367
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
2
|
+
import type { WecomTarget } from "../../target.js";
|
|
3
|
+
import { sendAgentApiMediaReply, sendAgentApiTextReply } from "./reply.js";
|
|
4
|
+
import { uploadAgentApiMedia } from "./media-upload.js";
|
|
5
|
+
|
|
6
|
+
export async function deliverAgentApiText(params: {
|
|
7
|
+
agent: ResolvedAgentAccount;
|
|
8
|
+
target: WecomTarget;
|
|
9
|
+
text: string;
|
|
10
|
+
}): Promise<void> {
|
|
11
|
+
await sendAgentApiTextReply(params);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function deliverAgentApiMedia(params: {
|
|
15
|
+
agent: ResolvedAgentAccount;
|
|
16
|
+
target: WecomTarget;
|
|
17
|
+
buffer: Buffer;
|
|
18
|
+
filename: string;
|
|
19
|
+
contentType: string;
|
|
20
|
+
text?: string;
|
|
21
|
+
}): Promise<void> {
|
|
22
|
+
let mediaType: "image" | "voice" | "video" | "file" = "file";
|
|
23
|
+
if (params.contentType.startsWith("image/")) mediaType = "image";
|
|
24
|
+
else if (params.contentType.startsWith("audio/")) mediaType = "voice";
|
|
25
|
+
else if (params.contentType.startsWith("video/")) mediaType = "video";
|
|
26
|
+
|
|
27
|
+
const mediaId = await uploadAgentApiMedia({
|
|
28
|
+
agent: params.agent,
|
|
29
|
+
type: mediaType,
|
|
30
|
+
buffer: params.buffer,
|
|
31
|
+
filename: params.filename,
|
|
32
|
+
});
|
|
33
|
+
await sendAgentApiMediaReply({
|
|
34
|
+
agent: params.agent,
|
|
35
|
+
target: params.target,
|
|
36
|
+
mediaId,
|
|
37
|
+
mediaType,
|
|
38
|
+
title: mediaType === "video" ? params.text?.trim().slice(0, 64) : undefined,
|
|
39
|
+
description: mediaType === "video" ? params.text?.trim().slice(0, 512) : undefined,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
2
|
+
import { uploadMedia as uploadLegacyMedia } from "./core.js";
|
|
3
|
+
|
|
4
|
+
export async function uploadAgentApiMedia(params: {
|
|
5
|
+
agent: ResolvedAgentAccount;
|
|
6
|
+
type: "image" | "voice" | "video" | "file";
|
|
7
|
+
buffer: Buffer;
|
|
8
|
+
filename: string;
|
|
9
|
+
}): Promise<string> {
|
|
10
|
+
return uploadLegacyMedia(params);
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
2
|
+
import type { WecomTarget } from "../../target.js";
|
|
3
|
+
import { sendAgentApiMedia, sendAgentApiText } from "./client.js";
|
|
4
|
+
|
|
5
|
+
export async function sendAgentApiTextReply(params: {
|
|
6
|
+
agent: ResolvedAgentAccount;
|
|
7
|
+
target: WecomTarget;
|
|
8
|
+
text: string;
|
|
9
|
+
}): Promise<void> {
|
|
10
|
+
await sendAgentApiText({
|
|
11
|
+
agent: params.agent,
|
|
12
|
+
toUser: params.target.touser,
|
|
13
|
+
toParty: params.target.toparty,
|
|
14
|
+
toTag: params.target.totag,
|
|
15
|
+
chatId: params.target.chatid,
|
|
16
|
+
text: params.text,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function sendAgentApiMediaReply(params: {
|
|
21
|
+
agent: ResolvedAgentAccount;
|
|
22
|
+
target: WecomTarget;
|
|
23
|
+
mediaId: string;
|
|
24
|
+
mediaType: "image" | "voice" | "video" | "file";
|
|
25
|
+
title?: string;
|
|
26
|
+
description?: string;
|
|
27
|
+
}): Promise<void> {
|
|
28
|
+
await sendAgentApiMedia({
|
|
29
|
+
agent: params.agent,
|
|
30
|
+
toUser: params.target.touser,
|
|
31
|
+
toParty: params.target.toparty,
|
|
32
|
+
toTag: params.target.totag,
|
|
33
|
+
chatId: params.target.chatid,
|
|
34
|
+
mediaId: params.mediaId,
|
|
35
|
+
mediaType: params.mediaType,
|
|
36
|
+
title: params.title,
|
|
37
|
+
description: params.description,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { OpenClawConfig } from "openclaw/plugin-sdk";
|
|
2
|
+
|
|
3
|
+
import type { WecomRuntimeEnv } from "../../types/runtime-context.js";
|
|
4
|
+
import type { ResolvedAgentAccount } from "../../types/index.js";
|
|
5
|
+
import type { WecomAccountRuntime } from "../../app/account-runtime.js";
|
|
6
|
+
import { resolveAgentCallbackPaths } from "./inbound.js";
|
|
7
|
+
import { createAgentCallbackSessionSnapshot } from "./session.js";
|
|
8
|
+
import { registerAgentWebhookTarget } from "../http/registry.js";
|
|
9
|
+
|
|
10
|
+
export function startAgentCallbackTransport(params: {
|
|
11
|
+
account: ResolvedAgentAccount;
|
|
12
|
+
cfg: OpenClawConfig;
|
|
13
|
+
runtime: WecomAccountRuntime;
|
|
14
|
+
runtimeEnv: WecomRuntimeEnv;
|
|
15
|
+
}): { paths: string[]; stop: () => void } {
|
|
16
|
+
const paths = resolveAgentCallbackPaths(params.account.accountId);
|
|
17
|
+
params.runtime.updateTransportSession(
|
|
18
|
+
createAgentCallbackSessionSnapshot({
|
|
19
|
+
accountId: params.account.accountId,
|
|
20
|
+
running: true,
|
|
21
|
+
}),
|
|
22
|
+
);
|
|
23
|
+
const unregisters = paths.map((path) =>
|
|
24
|
+
registerAgentWebhookTarget({
|
|
25
|
+
agent: params.account,
|
|
26
|
+
config: params.cfg,
|
|
27
|
+
runtimeEnv: params.runtimeEnv,
|
|
28
|
+
touchTransportSession: (patch) => params.runtime.touchTransportSession("agent-callback", patch),
|
|
29
|
+
auditSink: (event) => params.runtime.recordOperationalIssue(event),
|
|
30
|
+
path,
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
return {
|
|
34
|
+
paths,
|
|
35
|
+
stop: () => {
|
|
36
|
+
for (const unregister of unregisters) {
|
|
37
|
+
unregister();
|
|
38
|
+
}
|
|
39
|
+
params.runtime.updateTransportSession(
|
|
40
|
+
createAgentCallbackSessionSnapshot({
|
|
41
|
+
accountId: params.account.accountId,
|
|
42
|
+
running: false,
|
|
43
|
+
}),
|
|
44
|
+
);
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ReplyContext } from "../../types/index.js";
|
|
2
|
+
|
|
3
|
+
export function createAgentCallbackReplyContext(params: {
|
|
4
|
+
accountId: string;
|
|
5
|
+
raw: ReplyContext["raw"];
|
|
6
|
+
}): ReplyContext {
|
|
7
|
+
return {
|
|
8
|
+
transport: "agent-callback",
|
|
9
|
+
accountId: params.accountId,
|
|
10
|
+
passiveWindowMs: 5_000,
|
|
11
|
+
raw: params.raw,
|
|
12
|
+
};
|
|
13
|
+
}
|