@agent-wechat/agent-wechat 1.3.0-beta.1 → 1.3.1-beta.0

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.
Files changed (2) hide show
  1. package/dist/index.js +51 -23
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -483,18 +483,20 @@ var NativeWeChatClient = class {
483
483
  }
484
484
  return result;
485
485
  }
486
- /** Send text with optional group mentions, or quote a locally stored message in the same chat.
486
+ /** Send text with optional group mentions and a quote of a locally stored message in the same chat.
487
487
  * Quoted-reply live acceptance and recipient rendering remain unverified; unsupported targets are rejected.
488
488
  */
489
489
  sendText(accountId, threadId2, text3, idempotencyKey, quoteOrOptions) {
490
490
  if (!idempotencyKey || /[\r\n]/.test(idempotencyKey))
491
491
  throw new Error("An idempotency key is required");
492
- const options = quoteOrOptions && typeof quoteOrOptions === "object" && "mentions" in quoteOrOptions ? quoteOrOptions : void 0;
493
- const quote = options === void 0 ? quoteOrOptions : void 0;
494
- if (options !== void 0) {
495
- const mentions = options.mentions;
492
+ const options = quoteOrOptions && typeof quoteOrOptions === "object" && !Array.isArray(quoteOrOptions) && ("mentions" in quoteOrOptions || "quote" in quoteOrOptions || Object.keys(quoteOrOptions).length === 0) ? quoteOrOptions : void 0;
493
+ const quote = options === void 0 ? quoteOrOptions : options.quote;
494
+ if (options && Object.keys(options).some((key) => key !== "mentions" && key !== "quote"))
495
+ throw new Error("Invalid text send options");
496
+ const mentions = options?.mentions;
497
+ if (mentions !== void 0) {
496
498
  const ids = mentions?.userIds ?? [];
497
- if (Object.keys(options).some((key) => key !== "mentions") || !threadId2.endsWith("@chatroom") || !mentions || typeof mentions !== "object" || Array.isArray(mentions) || Object.keys(mentions).some((key) => key !== "userIds" && key !== "all" && key !== "positions") || mentions.userIds !== void 0 && !Array.isArray(mentions.userIds) || mentions.all !== void 0 && typeof mentions.all !== "boolean" || !Array.isArray(ids) || ids.length > 128 || new Set(ids).size !== ids.length || mentions.all === true === ids.length > 0 || ids.some((id2) => typeof id2 !== "string" || !/^[A-Za-z0-9_@.-]{1,256}$/.test(id2) || id2 === "notify@all"))
499
+ if (!threadId2.endsWith("@chatroom") || !mentions || typeof mentions !== "object" || Array.isArray(mentions) || Object.keys(mentions).some((key) => key !== "userIds" && key !== "all" && key !== "positions") || mentions.userIds !== void 0 && !Array.isArray(mentions.userIds) || mentions.all !== void 0 && typeof mentions.all !== "boolean" || !Array.isArray(ids) || ids.length > 128 || new Set(ids).size !== ids.length || mentions.all === true === ids.length > 0 || ids.some((id2) => typeof id2 !== "string" || !/^[A-Za-z0-9_@.-]{1,256}$/.test(id2) || id2 === "notify@all"))
498
500
  throw new Error("Invalid text mentions");
499
501
  const positions = mentions.positions ?? [];
500
502
  if (mentions.positions !== void 0 && !Array.isArray(mentions.positions) || positions.length > 256)
@@ -522,7 +524,7 @@ var NativeWeChatClient = class {
522
524
  }
523
525
  if (quote !== void 0 && (!quote || typeof quote !== "object" || Array.isArray(quote) || Object.keys(quote).some((key) => key !== "messageId" && key !== "serverId") || typeof quote.messageId !== "string" || !quote.messageId || quote.messageId.length > 8192 || typeof quote.serverId !== "string" || !/^[1-9]\d{0,19}$/.test(quote.serverId) || BigInt(quote.serverId) > 18446744073709551615n))
524
526
  throw new Error("Invalid quote message identity");
525
- const input = options !== void 0 ? { kind: "text", threadId: threadId2, text: text3, mentions: options.mentions } : quote === void 0 ? { threadId: threadId2, text: text3 } : { kind: "text", threadId: threadId2, text: text3, quote };
527
+ const input = quote === void 0 && mentions === void 0 ? { threadId: threadId2, text: text3 } : { kind: "text", threadId: threadId2, text: text3, ...quote !== void 0 && { quote }, ...mentions !== void 0 && { mentions } };
526
528
  return this.request(`v1/accounts/${id(accountId)}/sends`, { method: "POST", headers: { "Idempotency-Key": idempotencyKey }, body: JSON.stringify(input) });
527
529
  }
528
530
  send(accountId, sendId, signal) {
@@ -1384,8 +1386,8 @@ function validateWeChatMentionSend(mentions, input) {
1384
1386
  parseWeChatTextMentions(mentions);
1385
1387
  if (!input.to.endsWith("@chatroom")) throw Error("WeChat mentions require a group conversation");
1386
1388
  if (typeof input.text !== "string" || !input.text.trim()) throw Error("WeChat mentions require a text message");
1387
- if (input.mediaUrl || input.mediaUrls?.length || input.audioAsVoice || input.forceDocument || input.videoQuality != null || input.replyToId != null) {
1388
- throw Error("WeChat mentions support group text only; do not combine with attachments, voice, document, video quality or quoted replies");
1389
+ if (input.mediaUrl || input.mediaUrls?.length || input.audioAsVoice || input.forceDocument || input.videoQuality != null) {
1390
+ throw Error("WeChat mentions support group text only; do not combine with attachments, voice, document or video quality");
1389
1391
  }
1390
1392
  }
1391
1393
 
@@ -1449,7 +1451,13 @@ async function submitNativeText(client, accountId, threadId2, text3, key, option
1449
1451
  };
1450
1452
  let intent;
1451
1453
  try {
1452
- intent = inspect(await client.sendText(accountId, threadId2, text3, key, options.quote ?? (mentions ? { mentions } : void 0)));
1454
+ intent = inspect(await client.sendText(
1455
+ accountId,
1456
+ threadId2,
1457
+ text3,
1458
+ key,
1459
+ options.quote || mentions ? { ...options.quote && { quote: options.quote }, ...mentions && { mentions } } : void 0
1460
+ ));
1453
1461
  } catch (error) {
1454
1462
  if (error instanceof NativeTextSendInspectionError || error instanceof NativeApiError && error.status < 500 && error.code !== "timeout") throw error;
1455
1463
  try {
@@ -2387,14 +2395,14 @@ function nativePayloadMediaOptions(payload) {
2387
2395
  }
2388
2396
  var wechatMediaActions = {
2389
2397
  describeMessageTool: () => ({ actions: ["send"], schema: { actions: ["send"], visibility: "all-configured", properties: {
2390
- replyTo: { type: "string", minLength: 1, maxLength: 4096, description: "Quote a stored WeChat message in this same chat. Use its exact id from wechat_history, wechat_search or wechat_message, or the current inbound message ID; not a server ID or send/job ID. Text replies only; cannot combine with attachments or mentions. The hub validates the original message." },
2391
- channelData: { type: "object", description: "WeChat send options: mentions for ordinary group text, or videoQuality for one video attachment.", additionalProperties: true, properties: {
2398
+ replyTo: { type: "string", minLength: 1, maxLength: 4096, description: "Quote a stored WeChat message in this same chat. Use its exact id from wechat_history, wechat_search or wechat_message, or the current inbound message ID; not a server ID or send/job ID. Text replies only; group replies may include mentions. Send attachments separately. The hub validates the original message." },
2399
+ channelData: { type: "object", description: "WeChat send options: mentions for group text and quoted group replies, or videoQuality for one video attachment.", additionalProperties: true, properties: {
2392
2400
  "agent-wechat": { type: "object", additionalProperties: false, anyOf: [{ required: ["videoQuality"] }, { required: ["mentions"] }], properties: {
2393
2401
  videoQuality: { type: "string", enum: ["normal", "original"], description: "WeChat video mode: normal prepares playback; original also retains the unchanged source video. Cannot combine with forceDocument." },
2394
2402
  mentions: {
2395
2403
  type: "object",
2396
2404
  additionalProperties: false,
2397
- description: "Mention exact members in an ordinary group text message. Get userIds from wechat_group_info, not display names. The hub adds visible @names and metadata. Use all=true only when the sender has permission. Cannot combine with DMs, media, voice, video quality or quoted replies.",
2405
+ description: "Mention exact members in a group text message or quoted text reply. Get userIds from wechat_group_info, not display names. The hub adds visible @names and metadata. Use all=true only when the sender has permission. Cannot combine with DMs, media, voice or video quality.",
2398
2406
  properties: {
2399
2407
  userIds: { type: "array", minItems: 1, maxItems: 128, uniqueItems: true, items: { type: "string", minLength: 1, maxLength: 256, pattern: "^[A-Za-z0-9_@.-]+$" } },
2400
2408
  all: { type: "boolean", const: true }
@@ -3098,7 +3106,12 @@ async function dispatchNativeMessageInput(params, revision) {
3098
3106
  if (state.get(message.id)?.invalidatedByRecall) return;
3099
3107
  await beforeSend();
3100
3108
  const options = nativePayloadMediaOptions(payload);
3109
+ const replyToId = parseWeChatReplyTo(payload.replyToId);
3101
3110
  validateWeChatMentionSend(options.mentions, { ...payload, ...options, to: message.threadId });
3111
+ validateWeChatReplySend(replyToId, { ...payload, ...options });
3112
+ const original = replyToId ? await client.message(account.native.accountId, nativeReplyMessageId(replyToId, account.native.accountId)) : void 0;
3113
+ if (original?.recall) throw new Error("WeChat reply target was recalled");
3114
+ const quote = replyToId && original ? nativeReplyReference(original, account.native.accountId, message.threadId, replyToId) : void 0;
3102
3115
  const urls = payload.mediaUrls?.length ? payload.mediaUrls : payload.mediaUrl ? [payload.mediaUrl] : [];
3103
3116
  if (urls.length > 16) throw new Error("Native reply media count exceeds its bound");
3104
3117
  if (payload.audioAsVoice && !urls.length) throw new Error("Native voice reply requires an audio attachment");
@@ -3109,7 +3122,7 @@ async function dispatchNativeMessageInput(params, revision) {
3109
3122
  if (payload.text) {
3110
3123
  await beforeSend();
3111
3124
  const key = createHash9("sha256").update(`${dispatchManifest.messageSid}\0${delivery++}`).digest("hex");
3112
- await submitNativeText(client, account.native.accountId, message.threadId, payload.text, key, { mentions: options.mentions });
3125
+ await submitNativeText(client, account.native.accountId, message.threadId, payload.text, key, { mentions: options.mentions, quote });
3113
3126
  }
3114
3127
  if (urls.length || payload.text) params.onDelivered?.();
3115
3128
  },
@@ -3404,6 +3417,23 @@ function registerNativeLoginTool(api) {
3404
3417
 
3405
3418
  // src/native-outbound-recovery.ts
3406
3419
  import { createMessageReceiptFromOutboundResults } from "openclaw/plugin-sdk/channel-outbound";
3420
+ function recoveredTextMatches(message, text3, mentions) {
3421
+ if (typeof message.body !== "string") return false;
3422
+ if (!mentions) return message.body === text3;
3423
+ const expectedIds = mentions.userIds ?? [];
3424
+ const actual = message.mentions;
3425
+ if (actual?.state !== "known" || actual.all !== (mentions.all === true) || !Array.isArray(actual.userIds) || actual.userIds.length !== expectedIds.length || actual.userIds.some((id2, index) => id2 !== expectedIds[index])) return false;
3426
+ let offset = 0;
3427
+ for (let index = 0; index < (mentions.all ? 1 : expectedIds.length); index++) {
3428
+ if (message.body[offset] !== "@") return false;
3429
+ const end = message.body.indexOf("\u2005", offset + 1);
3430
+ if (end < 0) return false;
3431
+ const label = message.body.slice(offset + 1, end);
3432
+ if (!label.trim() || new TextEncoder().encode(label).length > 1024 || /[\u0000-\u001f\u007f-\u009f\ud800-\udfff]/u.test(label)) return false;
3433
+ offset = end + 1;
3434
+ }
3435
+ return message.body.slice(offset) === text3;
3436
+ }
3407
3437
  function createWeChatDurableFinal(host2 = ensureNativeHost) {
3408
3438
  return {
3409
3439
  capabilities: { text: true, media: true, payload: true, replyTo: true, messageSendingHooks: true, reconcileUnknownSend: true },
@@ -3434,15 +3464,13 @@ function createWeChatDurableFinal(host2 = ensureNativeHost) {
3434
3464
  if (!current()) return unresolved;
3435
3465
  const intent = await client.sendByKey(account.native.accountId, nativeOutboundKey(ctx.queueId));
3436
3466
  if (!current() || !("destination" in intent) || intent.accountId !== account.native.accountId || intent.destination !== ctx.to || !["accepted", "sent", "server_accepted"].includes(intent.state) || !intent.localMessageId || !intent.serverId) return unresolved;
3437
- if (!options.mentions) {
3438
- const message = await client.message(account.native.accountId, intent.localMessageId);
3439
- if (!current() || message.body !== payload.text) return unresolved;
3440
- if (replyToId) {
3441
- const original = await client.message(account.native.accountId, nativeReplyMessageId(replyToId, account.native.accountId));
3442
- const quote = nativeReplyReference(original, account.native.accountId, ctx.to, replyToId);
3443
- if (!current() || message.type !== 49 || message.appMessageType !== 57 || message.quote?.serverId !== quote.serverId) return unresolved;
3444
- } else if (message.type !== 1 || message.quote) return unresolved;
3445
- }
3467
+ const message = await client.message(account.native.accountId, intent.localMessageId);
3468
+ if (!current() || message.id !== intent.localMessageId || message.accountId !== account.native.accountId || message.threadId !== ctx.to || message.direction !== "outgoing" || message.serverId !== intent.serverId || message.recall || !recoveredTextMatches(message, payload.text, options.mentions)) return unresolved;
3469
+ if (replyToId) {
3470
+ const original = await client.message(account.native.accountId, nativeReplyMessageId(replyToId, account.native.accountId));
3471
+ const quote = nativeReplyReference(original, account.native.accountId, ctx.to, replyToId);
3472
+ if (!current() || message.type !== 49 || message.appMessageType !== 57 || message.quote?.serverId !== quote.serverId) return unresolved;
3473
+ } else if (message.type !== 1 || message.quote) return unresolved;
3446
3474
  const delivery = await nativeOutboundDelivery(
3447
3475
  client,
3448
3476
  account.native.accountId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-wechat/agent-wechat",
3
- "version": "1.3.0-beta.1",
3
+ "version": "1.3.1-beta.0",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "files": [
@@ -13,7 +13,7 @@
13
13
  "esbuild": "^0.25.0",
14
14
  "openclaw": "2026.10.7",
15
15
  "typescript": "^5.4.5",
16
- "@agent-wechat/shared": "1.1.0"
16
+ "@agent-wechat/shared": "1.1.1-beta.0"
17
17
  },
18
18
  "openclaw": {
19
19
  "extensions": [