@agent-wechat/agent-wechat 0.13.0 → 0.14.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.
- package/dist/index.js +22 -6
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5561,11 +5561,25 @@ ${reference}` : reference;
|
|
|
5561
5561
|
function safeDisplayName(name) {
|
|
5562
5562
|
return name.replace(/[\r\n\0]/g, " ").trim() || "attachment";
|
|
5563
5563
|
}
|
|
5564
|
+
function attachmentFallbackFilename(kind, localId, originalBody = "") {
|
|
5565
|
+
if (kind === "file" && originalBody && !originalBody.trimStart().startsWith("<")) {
|
|
5566
|
+
return safeDisplayName(originalBody);
|
|
5567
|
+
}
|
|
5568
|
+
const extension = kind === "image" ? "jpg" : kind === "audio" ? "mp3" : kind === "video" ? "mp4" : "bin";
|
|
5569
|
+
return `message-${localId}.${extension}`;
|
|
5570
|
+
}
|
|
5571
|
+
function attachmentSourceBody(originalBody, kind) {
|
|
5572
|
+
if (!kind || kind === "file") return originalBody;
|
|
5573
|
+
return originalBody.trimStart().startsWith("<") ? "" : originalBody;
|
|
5574
|
+
}
|
|
5564
5575
|
function renderAttachmentBody(originalBody, attachment, exposeNonVisualPath = true) {
|
|
5565
5576
|
if (!attachment) return originalBody;
|
|
5566
5577
|
const name = safeDisplayName(attachment.filename);
|
|
5567
5578
|
if (attachment.status !== "ready" || !attachment.path) {
|
|
5568
5579
|
const label = attachment.kind === "file" ? "File" : attachment.kind === "image" ? "Image" : attachment.kind === "audio" ? "Voice message" : "Video";
|
|
5580
|
+
if (attachment.status === "pending") {
|
|
5581
|
+
return appendReference(originalBody, `[${label} pending: ${name}]`);
|
|
5582
|
+
}
|
|
5569
5583
|
return appendReference(originalBody, `[${label} unavailable: ${name} (${attachment.status})]`);
|
|
5570
5584
|
}
|
|
5571
5585
|
if (attachment.kind === "file") {
|
|
@@ -6024,17 +6038,18 @@ async function retrieveAttachment(client, chatId, msg, liveAccount, log, maxAtte
|
|
|
6024
6038
|
const baseType = msg.type & 2147483647;
|
|
6025
6039
|
const expectedKind = attachmentKindForMessageType(baseType);
|
|
6026
6040
|
if (!expectedKind) return void 0;
|
|
6041
|
+
const fallbackFilename = attachmentFallbackFilename(expectedKind, msg.localId, msg.content);
|
|
6027
6042
|
try {
|
|
6028
6043
|
const result = await pollMedia(client, chatId, msg.localId, log, maxAttempts);
|
|
6029
6044
|
if (!result) {
|
|
6030
|
-
return { kind: expectedKind, status: "unavailable", filename:
|
|
6045
|
+
return { kind: expectedKind, status: "unavailable", filename: fallbackFilename };
|
|
6031
6046
|
}
|
|
6032
6047
|
if (result.type === "unsupported") {
|
|
6033
6048
|
if (baseType === 49) return void 0;
|
|
6034
|
-
return { kind: expectedKind, status: "unsupported", filename:
|
|
6049
|
+
return { kind: expectedKind, status: "unsupported", filename: fallbackFilename };
|
|
6035
6050
|
}
|
|
6036
6051
|
const kind = attachmentKindForResult(result, expectedKind);
|
|
6037
|
-
const filename = result.filename ||
|
|
6052
|
+
const filename = result.filename || fallbackFilename;
|
|
6038
6053
|
if (!result.data) {
|
|
6039
6054
|
return { kind, status: result.type === "pending" ? "pending" : "unavailable", filename };
|
|
6040
6055
|
}
|
|
@@ -6058,7 +6073,7 @@ async function retrieveAttachment(client, chatId, msg, liveAccount, log, maxAtte
|
|
|
6058
6073
|
return {
|
|
6059
6074
|
kind: expectedKind,
|
|
6060
6075
|
status,
|
|
6061
|
-
filename:
|
|
6076
|
+
filename: fallbackFilename,
|
|
6062
6077
|
error: error instanceof Error ? error.message : String(error)
|
|
6063
6078
|
};
|
|
6064
6079
|
}
|
|
@@ -6228,13 +6243,14 @@ async function prepareMessage(client, msg, chatId, chat, liveAccount, policy, lo
|
|
|
6228
6243
|
return null;
|
|
6229
6244
|
}
|
|
6230
6245
|
const baseType = msg.type & 2147483647;
|
|
6246
|
+
const messageAttachmentKind = attachmentKindForMessageType(baseType);
|
|
6231
6247
|
let attachment;
|
|
6232
|
-
if (
|
|
6248
|
+
if (messageAttachmentKind) {
|
|
6233
6249
|
log?.info?.(`[wechat:${liveAccount.accountId}] Checking media for msg ${msg.localId} (type ${baseType})`);
|
|
6234
6250
|
attachment = await retrieveAttachment(client, chatId, msg, liveAccount, log);
|
|
6235
6251
|
}
|
|
6236
6252
|
const timestamp = new Date(msg.timestamp).getTime();
|
|
6237
|
-
let sourceBody = msg.content || "";
|
|
6253
|
+
let sourceBody = attachmentSourceBody(msg.content || "", messageAttachmentKind);
|
|
6238
6254
|
if (msg.reply) {
|
|
6239
6255
|
const replySender = msg.reply.sender ?? "unknown sender";
|
|
6240
6256
|
const quotedBody = msg.reply.content.length > 50 ? msg.reply.content.slice(0, 50) + "..." : msg.reply.content;
|