@yanhaidao/wecom 2.3.160 → 2.3.180
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 +235 -399
- package/SKILLS_CAL.md +895 -0
- package/SKILLS_DOC.md +2136 -0
- package/changelog/v2.3.18.md +22 -0
- package/index.ts +39 -3
- package/package.json +2 -3
- package/src/agent/handler.event-filter.test.ts +11 -0
- package/src/agent/handler.ts +732 -643
- package/src/app/account-runtime.ts +46 -20
- package/src/app/index.ts +19 -1
- package/src/capability/calendar/SKILLS_CHECKLIST.md +251 -0
- package/src/capability/calendar/client.ts +815 -0
- package/src/capability/calendar/index.ts +3 -0
- package/src/capability/calendar/schema.ts +417 -0
- package/src/capability/calendar/tool.ts +417 -0
- package/src/capability/calendar/types.ts +309 -0
- package/src/capability/doc/client.ts +567 -62
- package/src/capability/doc/schema.ts +419 -318
- package/src/capability/doc/tool.ts +1510 -1178
- package/src/capability/doc/types.ts +130 -14
- package/src/capability/mcp/index.ts +10 -0
- package/src/capability/mcp/schema.ts +107 -0
- package/src/capability/mcp/tool.ts +170 -0
- package/src/capability/mcp/transport.ts +394 -0
- package/src/channel.ts +70 -28
- package/src/config/schema.ts +71 -102
- package/src/outbound.test.ts +91 -14
- package/src/outbound.ts +143 -30
- package/src/runtime/reply-orchestrator.test.ts +35 -2
- package/src/runtime/reply-orchestrator.ts +14 -2
- package/src/runtime/session-manager.ts +20 -6
- package/src/runtime/source-registry.ts +165 -0
- package/src/transport/bot-ws/media.ts +269 -0
- package/src/transport/bot-ws/reply.test.ts +85 -17
- package/src/transport/bot-ws/reply.ts +109 -21
- package/src/transport/bot-ws/sdk-adapter.test.ts +64 -1
- package/src/transport/bot-ws/sdk-adapter.ts +88 -12
- package/.claude/settings.local.json +0 -11
- package/docs/update-content-fix.md +0 -135
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
generateReqId,
|
|
3
|
+
type WsFrame,
|
|
4
|
+
type BaseMessage,
|
|
5
|
+
type EventMessage,
|
|
6
|
+
type WSClient,
|
|
7
|
+
} from "@wecom/aibot-node-sdk";
|
|
1
8
|
import { formatErrorMessage } from "openclaw/plugin-sdk";
|
|
2
|
-
import { generateReqId, type WsFrame, type BaseMessage, type EventMessage, type WSClient } from "@wecom/aibot-node-sdk";
|
|
3
|
-
|
|
4
9
|
import type { ReplyHandle, ReplyPayload } from "../../types/index.js";
|
|
10
|
+
import { uploadAndSendBotWsMedia } from "./media.js";
|
|
5
11
|
|
|
6
12
|
const PLACEHOLDER_KEEPALIVE_MS = 3000;
|
|
7
13
|
const MAX_KEEPALIVE_MS = 120 * 1000; // Force stop keepalive after 120s if ignored
|
|
@@ -32,7 +38,14 @@ function isAckTimeoutError(error: unknown): boolean {
|
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
function isTerminalReplyError(error: unknown): boolean {
|
|
35
|
-
return
|
|
41
|
+
return (
|
|
42
|
+
isInvalidReqIdError(error) || isExpiredStreamUpdateError(error) || isAckTimeoutError(error)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function formatMediaFailure(mediaUrl: string, error?: string, rejectReason?: string): string {
|
|
47
|
+
const reason = rejectReason || error || "unknown";
|
|
48
|
+
return `媒体发送失败:${mediaUrl} (${reason})`;
|
|
36
49
|
}
|
|
37
50
|
|
|
38
51
|
// Global registry to track active keepalives by peerId
|
|
@@ -54,6 +67,7 @@ export function createBotWsReplyHandle(params: {
|
|
|
54
67
|
}): ReplyHandle {
|
|
55
68
|
let streamId: string | undefined;
|
|
56
69
|
let accumulatedText = "";
|
|
70
|
+
let deferredMediaUrls: string[] = [];
|
|
57
71
|
const resolveStreamId = () => {
|
|
58
72
|
streamId ||= generateReqId("stream");
|
|
59
73
|
return streamId;
|
|
@@ -68,11 +82,15 @@ export function createBotWsReplyHandle(params: {
|
|
|
68
82
|
// Extract peerId for clustering handles
|
|
69
83
|
const body = params.frame.body as any;
|
|
70
84
|
const peerId = String(
|
|
71
|
-
(body?.chattype === "group" ? body?.chatid || body?.from?.userid : body?.from?.userid) ||
|
|
85
|
+
(body?.chattype === "group" ? body?.chatid || body?.from?.userid : body?.from?.userid) ||
|
|
86
|
+
"unknown",
|
|
72
87
|
);
|
|
73
88
|
const reqId = params.frame.headers.req_id || "unknown";
|
|
74
89
|
|
|
75
|
-
const isEvent =
|
|
90
|
+
const isEvent =
|
|
91
|
+
params.inboundKind === "welcome" ||
|
|
92
|
+
params.inboundKind === "event" ||
|
|
93
|
+
params.inboundKind === "template-card-event";
|
|
76
94
|
|
|
77
95
|
const stopPlaceholderKeepalive = () => {
|
|
78
96
|
if (placeholderKeepalive) {
|
|
@@ -83,7 +101,7 @@ export function createBotWsReplyHandle(params: {
|
|
|
83
101
|
clearTimeout(placeholderTimeout);
|
|
84
102
|
placeholderTimeout = undefined;
|
|
85
103
|
}
|
|
86
|
-
|
|
104
|
+
|
|
87
105
|
// Remove from registry
|
|
88
106
|
const keepalives = activeKeepalivesByPeer.get(peerId);
|
|
89
107
|
if (keepalives) {
|
|
@@ -107,7 +125,8 @@ export function createBotWsReplyHandle(params: {
|
|
|
107
125
|
const sendPlaceholder = () => {
|
|
108
126
|
if (streamSettled || placeholderInFlight || isEvent) return;
|
|
109
127
|
placeholderInFlight = true;
|
|
110
|
-
params.client
|
|
128
|
+
params.client
|
|
129
|
+
.replyStream(params.frame, resolveStreamId(), placeholderText, false)
|
|
111
130
|
.catch((error) => {
|
|
112
131
|
if (!isTerminalReplyError(error)) {
|
|
113
132
|
return;
|
|
@@ -134,13 +153,27 @@ export function createBotWsReplyHandle(params: {
|
|
|
134
153
|
}
|
|
135
154
|
};
|
|
136
155
|
|
|
156
|
+
const mergeDeferredMediaUrls = (urls: string[]): string[] => {
|
|
157
|
+
if (urls.length === 0) {
|
|
158
|
+
return deferredMediaUrls;
|
|
159
|
+
}
|
|
160
|
+
const merged = [...deferredMediaUrls];
|
|
161
|
+
for (const url of urls) {
|
|
162
|
+
if (!merged.includes(url)) {
|
|
163
|
+
merged.push(url);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
deferredMediaUrls = merged;
|
|
167
|
+
return deferredMediaUrls;
|
|
168
|
+
};
|
|
169
|
+
|
|
137
170
|
if (params.autoSendPlaceholder !== false && !isEvent) {
|
|
138
171
|
sendPlaceholder();
|
|
139
172
|
placeholderKeepalive = setInterval(() => {
|
|
140
173
|
sendPlaceholder();
|
|
141
174
|
}, PLACEHOLDER_KEEPALIVE_MS);
|
|
142
|
-
|
|
143
|
-
// Safety net: force stop keepalive after MAX_KEEPALIVE_MS
|
|
175
|
+
|
|
176
|
+
// Safety net: force stop keepalive after MAX_KEEPALIVE_MS
|
|
144
177
|
// in case the message is completely ignored by the core and never triggers deliver/fail
|
|
145
178
|
placeholderTimeout = setTimeout(() => {
|
|
146
179
|
stopPlaceholderKeepalive();
|
|
@@ -183,10 +216,22 @@ export function createBotWsReplyHandle(params: {
|
|
|
183
216
|
return;
|
|
184
217
|
}
|
|
185
218
|
|
|
186
|
-
const text = payload.text?.trim();
|
|
187
|
-
|
|
219
|
+
const text = payload.text?.trim() || "";
|
|
220
|
+
const incomingMediaUrls = payload.mediaUrls || (payload.mediaUrl ? [payload.mediaUrl] : []);
|
|
221
|
+
const hasIncomingMedia = incomingMediaUrls.length > 0;
|
|
222
|
+
if (info.kind !== "final" && hasIncomingMedia) {
|
|
223
|
+
mergeDeferredMediaUrls(incomingMediaUrls);
|
|
224
|
+
}
|
|
225
|
+
const mediaUrls =
|
|
226
|
+
info.kind === "final" ? mergeDeferredMediaUrls(incomingMediaUrls) : incomingMediaUrls;
|
|
227
|
+
if (!text && mediaUrls.length === 0) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
188
230
|
|
|
189
231
|
if (info.kind === "block") {
|
|
232
|
+
if (!text) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
190
235
|
accumulatedText = accumulatedText ? `${accumulatedText}\n${text}` : text;
|
|
191
236
|
}
|
|
192
237
|
|
|
@@ -199,6 +244,46 @@ export function createBotWsReplyHandle(params: {
|
|
|
199
244
|
: text
|
|
200
245
|
: accumulatedText || text;
|
|
201
246
|
|
|
247
|
+
let finalText = outboundText;
|
|
248
|
+
if (info.kind === "final" && mediaUrls.length > 0) {
|
|
249
|
+
const mediaFailures: string[] = [];
|
|
250
|
+
const mediaNotes: string[] = [];
|
|
251
|
+
let mediaSent = 0;
|
|
252
|
+
for (const mediaUrl of mediaUrls) {
|
|
253
|
+
const result = await uploadAndSendBotWsMedia({
|
|
254
|
+
wsClient: params.client,
|
|
255
|
+
chatId: peerId,
|
|
256
|
+
mediaUrl,
|
|
257
|
+
});
|
|
258
|
+
if (result.ok) {
|
|
259
|
+
mediaSent += 1;
|
|
260
|
+
if (result.downgradeNote) {
|
|
261
|
+
mediaNotes.push(result.downgradeNote);
|
|
262
|
+
}
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
mediaFailures.push(formatMediaFailure(mediaUrl, result.error, result.rejectReason));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (!finalText && mediaSent > 0) {
|
|
269
|
+
finalText = "文件已发送。";
|
|
270
|
+
}
|
|
271
|
+
if (mediaFailures.length > 0) {
|
|
272
|
+
finalText = finalText
|
|
273
|
+
? `${finalText}\n\n${mediaFailures.join("\n")}`
|
|
274
|
+
: mediaFailures.join("\n");
|
|
275
|
+
}
|
|
276
|
+
if (mediaNotes.length > 0) {
|
|
277
|
+
finalText = finalText
|
|
278
|
+
? `${finalText}\n\n${mediaNotes.join("\n")}`
|
|
279
|
+
: mediaNotes.join("\n");
|
|
280
|
+
}
|
|
281
|
+
deferredMediaUrls = [];
|
|
282
|
+
}
|
|
283
|
+
if (!finalText) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
|
|
202
287
|
// Event frames do not support streaming chunks
|
|
203
288
|
if (isEvent && info.kind !== "final") {
|
|
204
289
|
return;
|
|
@@ -209,19 +294,19 @@ export function createBotWsReplyHandle(params: {
|
|
|
209
294
|
if (params.inboundKind === "welcome") {
|
|
210
295
|
await params.client.replyWelcome(params.frame, {
|
|
211
296
|
msgtype: "text",
|
|
212
|
-
text: { content:
|
|
297
|
+
text: { content: finalText },
|
|
213
298
|
});
|
|
214
299
|
} else if (isEvent) {
|
|
215
300
|
// Send push message for other events
|
|
216
301
|
await params.client.sendMessage(peerId, {
|
|
217
302
|
msgtype: "markdown",
|
|
218
|
-
markdown: { content:
|
|
303
|
+
markdown: { content: finalText },
|
|
219
304
|
});
|
|
220
305
|
} else {
|
|
221
306
|
await params.client.replyStream(
|
|
222
307
|
params.frame,
|
|
223
308
|
resolveStreamId(),
|
|
224
|
-
|
|
309
|
+
finalText,
|
|
225
310
|
info.kind === "final",
|
|
226
311
|
);
|
|
227
312
|
}
|
|
@@ -243,17 +328,20 @@ export function createBotWsReplyHandle(params: {
|
|
|
243
328
|
}
|
|
244
329
|
const message = formatErrorMessage(error);
|
|
245
330
|
const text = `WeCom WS reply failed: ${message}`;
|
|
246
|
-
|
|
331
|
+
|
|
247
332
|
try {
|
|
248
333
|
if (params.inboundKind === "welcome") {
|
|
249
|
-
|
|
334
|
+
await params.client.replyWelcome(params.frame, {
|
|
335
|
+
msgtype: "text",
|
|
336
|
+
text: { content: text },
|
|
337
|
+
});
|
|
250
338
|
} else if (isEvent) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
339
|
+
await params.client.sendMessage(peerId, {
|
|
340
|
+
msgtype: "markdown",
|
|
341
|
+
markdown: { content: text },
|
|
342
|
+
});
|
|
255
343
|
} else {
|
|
256
|
-
|
|
344
|
+
await params.client.replyStream(params.frame, resolveStreamId(), text, true);
|
|
257
345
|
}
|
|
258
346
|
} catch (sendError) {
|
|
259
347
|
params.onFail?.(sendError);
|
|
@@ -5,6 +5,7 @@ const sdkMockState = vi.hoisted(() => {
|
|
|
5
5
|
readonly handlers = new Map<string, Array<(payload: any) => void>>();
|
|
6
6
|
readonly isConnected = true;
|
|
7
7
|
readonly replyStream = vi.fn().mockResolvedValue(undefined);
|
|
8
|
+
readonly replyWelcome = vi.fn().mockResolvedValue(undefined);
|
|
8
9
|
|
|
9
10
|
constructor(_options: unknown) {
|
|
10
11
|
sdkMockState.client = this;
|
|
@@ -117,7 +118,69 @@ describe("BotWsSdkAdapter", () => {
|
|
|
117
118
|
}),
|
|
118
119
|
);
|
|
119
120
|
expect(log.error).toHaveBeenCalledWith(
|
|
120
|
-
expect.stringContaining(
|
|
121
|
+
expect.stringContaining(
|
|
122
|
+
"frame handler failed account=acc-1 reqId=req-1 message=frame exploded",
|
|
123
|
+
),
|
|
124
|
+
);
|
|
125
|
+
expect(unhandledRejections).toHaveLength(0);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("short-circuits enter_chat welcome events to a static ws welcome reply", async () => {
|
|
129
|
+
process.on("unhandledRejection", onUnhandledRejection);
|
|
130
|
+
|
|
131
|
+
const runtime = {
|
|
132
|
+
account: {
|
|
133
|
+
accountId: "acc-1",
|
|
134
|
+
bot: {
|
|
135
|
+
wsConfigured: true,
|
|
136
|
+
ws: {
|
|
137
|
+
botId: "bot-1",
|
|
138
|
+
secret: "secret-1",
|
|
139
|
+
},
|
|
140
|
+
config: {
|
|
141
|
+
welcomeText: "欢迎来到 WeCom",
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
handleEvent: vi.fn().mockResolvedValue(undefined),
|
|
146
|
+
updateTransportSession: vi.fn(),
|
|
147
|
+
touchTransportSession: vi.fn(),
|
|
148
|
+
recordOperationalIssue: vi.fn(),
|
|
149
|
+
};
|
|
150
|
+
const log = {
|
|
151
|
+
info: vi.fn(),
|
|
152
|
+
warn: vi.fn(),
|
|
153
|
+
error: vi.fn(),
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
new BotWsSdkAdapter(runtime as any, log as any).start();
|
|
157
|
+
|
|
158
|
+
sdkMockState.client?.emit("event", {
|
|
159
|
+
cmd: "aibot_event_callback",
|
|
160
|
+
headers: { req_id: "req-welcome" },
|
|
161
|
+
body: {
|
|
162
|
+
msgid: "msg-welcome",
|
|
163
|
+
msgtype: "event",
|
|
164
|
+
chattype: "single",
|
|
165
|
+
from: { userid: "user-1" },
|
|
166
|
+
event: { eventtype: "enter_chat" },
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
await waitForAsyncCallbacks();
|
|
171
|
+
|
|
172
|
+
expect(runtime.handleEvent).not.toHaveBeenCalled();
|
|
173
|
+
expect(sdkMockState.client?.replyWelcome).toHaveBeenCalledWith(
|
|
174
|
+
expect.objectContaining({
|
|
175
|
+
headers: { req_id: "req-welcome" },
|
|
176
|
+
}),
|
|
177
|
+
{
|
|
178
|
+
msgtype: "text",
|
|
179
|
+
text: { content: "欢迎来到 WeCom" },
|
|
180
|
+
},
|
|
181
|
+
);
|
|
182
|
+
expect(log.info).toHaveBeenCalledWith(
|
|
183
|
+
expect.stringContaining("static welcome delivered account=acc-1 messageId=msg-welcome"),
|
|
121
184
|
);
|
|
122
185
|
expect(unhandledRejections).toHaveLength(0);
|
|
123
186
|
});
|
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import AiBot, {
|
|
3
|
+
generateReqId,
|
|
4
|
+
type BaseMessage,
|
|
5
|
+
type EventMessage,
|
|
6
|
+
type WsFrame,
|
|
7
|
+
} from "@wecom/aibot-node-sdk";
|
|
8
|
+
import type { WecomAccountRuntime } from "../../app/account-runtime.js";
|
|
6
9
|
import { registerBotWsPushHandle, unregisterBotWsPushHandle } from "../../app/index.js";
|
|
10
|
+
import { clearWecomMcpAccountCache } from "../../capability/mcp/index.js";
|
|
11
|
+
import type { RuntimeLogSink } from "../../types/index.js";
|
|
7
12
|
import { mapBotWsFrameToInboundEvent } from "./inbound.js";
|
|
13
|
+
import { uploadAndSendBotWsMedia } from "./media.js";
|
|
8
14
|
import { createBotWsReplyHandle } from "./reply.js";
|
|
9
15
|
import { createBotWsSessionSnapshot } from "./session.js";
|
|
10
|
-
import type { WecomAccountRuntime } from "../../app/account-runtime.js";
|
|
11
16
|
|
|
12
17
|
export class BotWsSdkAdapter {
|
|
13
18
|
private client?: AiBot.WSClient;
|
|
@@ -32,15 +37,35 @@ export class BotWsSdkAdapter {
|
|
|
32
37
|
botId: bot.ws.botId,
|
|
33
38
|
secret: bot.ws.secret,
|
|
34
39
|
logger: {
|
|
35
|
-
debug: (message, ...args) =>
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
debug: (message, ...args) =>
|
|
41
|
+
this.log.info?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
|
|
42
|
+
info: (message, ...args) =>
|
|
43
|
+
this.log.info?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
|
|
44
|
+
warn: (message, ...args) =>
|
|
45
|
+
this.log.warn?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
|
|
46
|
+
error: (message, ...args) =>
|
|
47
|
+
this.log.error?.(`[wecom-ws] ${message} ${args.join(" ")}`.trim()),
|
|
39
48
|
},
|
|
40
49
|
});
|
|
41
50
|
this.client = client;
|
|
42
51
|
registerBotWsPushHandle(this.runtime.account.accountId, {
|
|
43
52
|
isConnected: () => client.isConnected,
|
|
53
|
+
replyCommand: async ({ cmd, body, headers }) => {
|
|
54
|
+
const result = await client.reply(
|
|
55
|
+
{ headers: headers ?? { req_id: generateReqId("wecom_ws") } },
|
|
56
|
+
body ?? {},
|
|
57
|
+
cmd,
|
|
58
|
+
);
|
|
59
|
+
this.runtime.touchTransportSession("bot-ws", {
|
|
60
|
+
ownerId: this.ownerId,
|
|
61
|
+
running: true,
|
|
62
|
+
connected: client.isConnected,
|
|
63
|
+
authenticated: client.isConnected,
|
|
64
|
+
lastOutboundAt: Date.now(),
|
|
65
|
+
lastError: undefined,
|
|
66
|
+
});
|
|
67
|
+
return result as Record<string, unknown>;
|
|
68
|
+
},
|
|
44
69
|
sendMarkdown: async (chatId, content) => {
|
|
45
70
|
await client.sendMessage(chatId, {
|
|
46
71
|
msgtype: "markdown",
|
|
@@ -55,6 +80,29 @@ export class BotWsSdkAdapter {
|
|
|
55
80
|
lastError: undefined,
|
|
56
81
|
});
|
|
57
82
|
},
|
|
83
|
+
sendMedia: async ({ chatId, mediaUrl, text, mediaLocalRoots }) => {
|
|
84
|
+
const result = await uploadAndSendBotWsMedia({
|
|
85
|
+
wsClient: client,
|
|
86
|
+
chatId,
|
|
87
|
+
mediaUrl,
|
|
88
|
+
mediaLocalRoots,
|
|
89
|
+
});
|
|
90
|
+
if (result.ok && text?.trim()) {
|
|
91
|
+
await client.sendMessage(chatId, {
|
|
92
|
+
msgtype: "markdown",
|
|
93
|
+
markdown: { content: text.trim() },
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
this.runtime.touchTransportSession("bot-ws", {
|
|
97
|
+
ownerId: this.ownerId,
|
|
98
|
+
running: true,
|
|
99
|
+
connected: client.isConnected,
|
|
100
|
+
authenticated: client.isConnected,
|
|
101
|
+
lastOutboundAt: Date.now(),
|
|
102
|
+
lastError: result.ok ? undefined : result.error,
|
|
103
|
+
});
|
|
104
|
+
return result;
|
|
105
|
+
},
|
|
58
106
|
});
|
|
59
107
|
|
|
60
108
|
client.on("connected", () => {
|
|
@@ -82,8 +130,12 @@ export class BotWsSdkAdapter {
|
|
|
82
130
|
});
|
|
83
131
|
|
|
84
132
|
client.on("disconnected", (reason) => {
|
|
133
|
+
clearWecomMcpAccountCache(this.runtime.account.accountId);
|
|
85
134
|
const normalizedReason = String(reason ?? "").toLowerCase();
|
|
86
|
-
const kicked =
|
|
135
|
+
const kicked =
|
|
136
|
+
normalizedReason.includes("kick") ||
|
|
137
|
+
normalizedReason.includes("owner") ||
|
|
138
|
+
normalizedReason.includes("replaced");
|
|
87
139
|
this.log.warn?.(
|
|
88
140
|
`[wecom-ws] disconnected account=${this.runtime.account.accountId} kicked=${String(kicked)} reason=${reason ?? "unknown"}`,
|
|
89
141
|
);
|
|
@@ -109,11 +161,15 @@ export class BotWsSdkAdapter {
|
|
|
109
161
|
});
|
|
110
162
|
|
|
111
163
|
client.on("reconnecting", (attempt) => {
|
|
112
|
-
this.log.warn?.(
|
|
164
|
+
this.log.warn?.(
|
|
165
|
+
`[wecom-ws] reconnecting account=${this.runtime.account.accountId} attempt=${attempt}`,
|
|
166
|
+
);
|
|
113
167
|
});
|
|
114
168
|
|
|
115
169
|
client.on("error", (error) => {
|
|
116
|
-
this.log.error?.(
|
|
170
|
+
this.log.error?.(
|
|
171
|
+
`[wecom-ws] error account=${this.runtime.account.accountId} message=${error.message}`,
|
|
172
|
+
);
|
|
117
173
|
this.runtime.updateTransportSession(
|
|
118
174
|
createBotWsSessionSnapshot({
|
|
119
175
|
accountId: this.runtime.account.accountId,
|
|
@@ -176,6 +232,25 @@ export class BotWsSdkAdapter {
|
|
|
176
232
|
});
|
|
177
233
|
},
|
|
178
234
|
});
|
|
235
|
+
|
|
236
|
+
const staticWelcomeText =
|
|
237
|
+
event.inboundKind === "welcome" ? botAccount.config.welcomeText?.trim() : undefined;
|
|
238
|
+
if (staticWelcomeText) {
|
|
239
|
+
this.log.info?.(
|
|
240
|
+
`[wecom-ws] static welcome reply account=${this.runtime.account.accountId} messageId=${event.messageId} peer=${event.conversation.peerKind}:${event.conversation.peerId} len=${staticWelcomeText.length}`,
|
|
241
|
+
);
|
|
242
|
+
await replyHandle.deliver(
|
|
243
|
+
{
|
|
244
|
+
text: staticWelcomeText,
|
|
245
|
+
},
|
|
246
|
+
{ kind: "final" },
|
|
247
|
+
);
|
|
248
|
+
this.log.info?.(
|
|
249
|
+
`[wecom-ws] static welcome delivered account=${this.runtime.account.accountId} messageId=${event.messageId}`,
|
|
250
|
+
);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
179
254
|
await this.runtime.handleEvent(event, replyHandle);
|
|
180
255
|
};
|
|
181
256
|
|
|
@@ -221,6 +296,7 @@ export class BotWsSdkAdapter {
|
|
|
221
296
|
|
|
222
297
|
stop(): void {
|
|
223
298
|
this.log.info?.(`[wecom-ws] stop account=${this.runtime.account.accountId}`);
|
|
299
|
+
clearWecomMcpAccountCache(this.runtime.account.accountId);
|
|
224
300
|
unregisterBotWsPushHandle(this.runtime.account.accountId);
|
|
225
301
|
this.runtime.updateTransportSession(
|
|
226
302
|
createBotWsSessionSnapshot({
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
# 企微文档批量更新修复指南
|
|
2
|
-
|
|
3
|
-
## 问题背景
|
|
4
|
-
|
|
5
|
-
企业微信文档 API 的 `batch_update` 接口存在以下限制:
|
|
6
|
-
|
|
7
|
-
1. **索引基于快照**:所有操作的索引基于请求发送时的文档快照
|
|
8
|
-
2. **原子性**:批量操作中一个失败则全部回滚
|
|
9
|
-
3. **段落验证**:`insert_text` 必须指向已有 Run 元素,不能在空段落执行
|
|
10
|
-
4. **版本控制**:`version` 参数用于并发控制(与最新版本差≤100)
|
|
11
|
-
|
|
12
|
-
## 常见问题
|
|
13
|
-
|
|
14
|
-
### ParagraphValidator cannot find p's parent
|
|
15
|
-
|
|
16
|
-
**原因**:在空段落或无效位置插入内容
|
|
17
|
-
|
|
18
|
-
**解决方案**:
|
|
19
|
-
```typescript
|
|
20
|
-
// 错误:直接在空位置插入文本
|
|
21
|
-
requests: [
|
|
22
|
-
{ insert_paragraph: { location: { index: 1 } } },
|
|
23
|
-
{ insert_text: { location: { index: 1 }, text: "内容" } } // ❌ 索引错误
|
|
24
|
-
]
|
|
25
|
-
|
|
26
|
-
// 正确:使用顺序模式或正确计算索引
|
|
27
|
-
requests: [
|
|
28
|
-
{ insert_paragraph: { location: { index: 1 } } },
|
|
29
|
-
{ insert_text: { location: { index: 2 }, text: "内容" } } // ✅ index+1
|
|
30
|
-
]
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
### TextValidator cannot find p parent
|
|
34
|
-
|
|
35
|
-
**原因**:`insert_text` 操作的目标段落在文档快照中不存在
|
|
36
|
-
|
|
37
|
-
**解决方案**:每次操作前获取最新文档结构
|
|
38
|
-
|
|
39
|
-
## 推荐方案
|
|
40
|
-
|
|
41
|
-
### 方案 A:顺序模式(推荐,默认)
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
await docClient.updateDocContent({
|
|
45
|
-
agent, docId,
|
|
46
|
-
requests: [...],
|
|
47
|
-
batchMode: false // 默认:顺序执行,每次获取最新版本
|
|
48
|
-
});
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
**优点**:
|
|
52
|
-
- 可靠性高
|
|
53
|
-
- 自动处理版本和索引
|
|
54
|
-
- 适合复杂场景
|
|
55
|
-
|
|
56
|
-
**缺点**:
|
|
57
|
-
- API 调用次数较多
|
|
58
|
-
|
|
59
|
-
### 方案 B:批量模式(高性能场景)
|
|
60
|
-
|
|
61
|
-
```typescript
|
|
62
|
-
await docClient.updateDocContent({
|
|
63
|
-
agent, docId,
|
|
64
|
-
requests: [
|
|
65
|
-
{ insert_paragraph: { location: { index: 1 } } },
|
|
66
|
-
{ insert_text: { location: { index: 2 }, text: "内容" } }
|
|
67
|
-
],
|
|
68
|
-
batchMode: true, // 批量执行,基于同一快照
|
|
69
|
-
version: 123 // 必须提供版本号
|
|
70
|
-
});
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
**优点**:
|
|
74
|
-
- API 调用次数少
|
|
75
|
-
- 性能更好
|
|
76
|
-
|
|
77
|
-
**缺点**:
|
|
78
|
-
- 需要精确计算索引
|
|
79
|
-
- 所有操作基于同一快照
|
|
80
|
-
|
|
81
|
-
### 方案 C:使用 init_content(创建文档)
|
|
82
|
-
|
|
83
|
-
```typescript
|
|
84
|
-
await docClient.createDoc({
|
|
85
|
-
agent,
|
|
86
|
-
docName: "新文档",
|
|
87
|
-
docType: "doc",
|
|
88
|
-
init_content: [
|
|
89
|
-
"# 标题",
|
|
90
|
-
"第一段内容",
|
|
91
|
-
{ type: "image", url: "https://..." },
|
|
92
|
-
"第二段内容"
|
|
93
|
-
]
|
|
94
|
-
});
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
**优点**:
|
|
98
|
-
- 最简单可靠
|
|
99
|
-
- 自动处理分段
|
|
100
|
-
|
|
101
|
-
**缺点**:
|
|
102
|
-
- 仅适用于创建文档
|
|
103
|
-
|
|
104
|
-
## API 限制速查
|
|
105
|
-
|
|
106
|
-
| 操作 | 限制 |
|
|
107
|
-
|------|------|
|
|
108
|
-
| 批量操作数 | 1-30 个 requests |
|
|
109
|
-
| 版本差 | ≤100 |
|
|
110
|
-
| 新增行 | ≤1000 |
|
|
111
|
-
| 新增列 | ≤200 |
|
|
112
|
-
| 单元格总数 | ≤10000 |
|
|
113
|
-
| 查询范围 | 行≤1000, 列≤200 |
|
|
114
|
-
|
|
115
|
-
## 错误码
|
|
116
|
-
|
|
117
|
-
| 错误 | 原因 | 解决方案 |
|
|
118
|
-
|------|------|----------|
|
|
119
|
-
| ParagraphValidator | 段落位置无效 | 使用正确索引或顺序模式 |
|
|
120
|
-
| TextValidator | 目标段落不存在 | 先创建段落或获取最新结构 |
|
|
121
|
-
| DrawingValidator | 图片位置无效 | 在有效段落插入 |
|
|
122
|
-
| version mismatch | 版本过旧 | 重新获取最新文档 |
|
|
123
|
-
|
|
124
|
-
## 最佳实践
|
|
125
|
-
|
|
126
|
-
1. **创建文档**:使用 `init_content`(最可靠)
|
|
127
|
-
2. **更新文档**:使用顺序模式(`batchMode: false`,默认)
|
|
128
|
-
3. **批量追加**:简单场景可用批量模式(需精确计算索引)
|
|
129
|
-
4. **并发控制**:始终传递 `version` 参数
|
|
130
|
-
5. **错误处理**:捕获异常并重试(最多 3 次)
|
|
131
|
-
|
|
132
|
-
## 相关文档
|
|
133
|
-
|
|
134
|
-
- [API 使用指南](./api-usage-guide.md)
|
|
135
|
-
- [示例代码](./examples.md)
|