@xmanrui/dsh-im 5.0.2 → 5.0.3
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.en.md +3 -1
- package/README.md +3 -1
- package/lib/client.js +1 -1
- package/lib/index.js +264 -264
- package/package.json +1 -1
- package/src/channels/shared/conversation-state-store.mjs +1 -0
- package/src/channels/shared/semantic/artifact.mjs +19 -6
- package/src/channels/whatsapp/state-store.mjs +34 -1
- package/src/channels/whatsapp/whatsapp-runtime.mjs +22 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xmanrui/dsh-im",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.3",
|
|
4
4
|
"icon": "./assets/logo-plugin-message-link-gradient.webp",
|
|
5
5
|
"description": "把微信、飞书、企微、钉钉、QQ、Telegram、WhatsApp、Slack、Discord、Matrix接入DSH。 Connect Telegram, WhatsApp, Slack, Discord, Matrix, WeChat, Feishu, WeCom, DingTalk, and QQ to DSH.",
|
|
6
6
|
"keywords": [
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createHash, randomUUID } from 'node:crypto';
|
|
2
2
|
import { constants as fsConstants } from 'node:fs';
|
|
3
|
-
import { copyFile, lstat, mkdtemp, open, realpath, unlink } from 'node:fs/promises';
|
|
3
|
+
import { copyFile, lstat, mkdir, mkdtemp, open, realpath, unlink } from 'node:fs/promises';
|
|
4
4
|
import { tmpdir } from 'node:os';
|
|
5
5
|
import { basename, extname, isAbsolute, join, resolve } from 'node:path';
|
|
6
6
|
|
|
@@ -35,12 +35,15 @@ const artifactProviderSettlements = new WeakMap();
|
|
|
35
35
|
let managedSnapshotDirectoryPromise;
|
|
36
36
|
|
|
37
37
|
function managedSnapshotDirectory() {
|
|
38
|
-
managedSnapshotDirectoryPromise ??= mkdtemp(join(tmpdir(), 'dsh-im-outbound-'))
|
|
38
|
+
managedSnapshotDirectoryPromise ??= mkdtemp(join(tmpdir(), 'dsh-im-outbound-')).catch((error) => {
|
|
39
|
+
managedSnapshotDirectoryPromise = undefined;
|
|
40
|
+
throw error;
|
|
41
|
+
});
|
|
39
42
|
return managedSnapshotDirectoryPromise;
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
function artifactError(code, message) {
|
|
43
|
-
const error = new Error(message);
|
|
45
|
+
function artifactError(code, message, cause) {
|
|
46
|
+
const error = new Error(message, cause === undefined ? undefined : { cause });
|
|
44
47
|
error.code = code;
|
|
45
48
|
return error;
|
|
46
49
|
}
|
|
@@ -194,7 +197,17 @@ async function snapshotFile(workspace, requestedPath, signal) {
|
|
|
194
197
|
|
|
195
198
|
const directory = await managedSnapshotDirectory();
|
|
196
199
|
storagePath = join(directory, `${randomUUID()}.artifact`);
|
|
197
|
-
|
|
200
|
+
try {
|
|
201
|
+
await copyFile(canonicalPath, storagePath, fsConstants.COPYFILE_EXCL);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (error?.code !== 'ENOENT') throw error;
|
|
204
|
+
signal?.throwIfAborted();
|
|
205
|
+
// System temp cleanup can remove the cached directory between sends.
|
|
206
|
+
// Recreate it with mkdtemp's private permissions and retry only once.
|
|
207
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
208
|
+
signal?.throwIfAborted();
|
|
209
|
+
await copyFile(canonicalPath, storagePath, fsConstants.COPYFILE_EXCL);
|
|
210
|
+
}
|
|
198
211
|
signal?.throwIfAborted();
|
|
199
212
|
|
|
200
213
|
const snapshot = await lstat(storagePath, { bigint: true });
|
|
@@ -214,7 +227,7 @@ async function snapshotFile(workspace, requestedPath, signal) {
|
|
|
214
227
|
if (storagePath) await unlink(storagePath).catch(() => undefined);
|
|
215
228
|
if (error?.code?.startsWith?.('artifact-')) throw error;
|
|
216
229
|
if (signal?.aborted) throw signal.reason ?? error;
|
|
217
|
-
throw artifactError('artifact-unavailable', 'The requested file is unavailable.');
|
|
230
|
+
throw artifactError('artifact-unavailable', 'The requested file is unavailable.', error);
|
|
218
231
|
}
|
|
219
232
|
}
|
|
220
233
|
|
|
@@ -1,3 +1,36 @@
|
|
|
1
1
|
import { ConversationStateStore } from '../shared/conversation-state-store.mjs';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const BOT_MESSAGE_LIMIT = 2_000;
|
|
4
|
+
|
|
5
|
+
export class WhatsappStateStore extends ConversationStateStore {
|
|
6
|
+
#botMessageIds = new Set();
|
|
7
|
+
|
|
8
|
+
async load() {
|
|
9
|
+
await super.load();
|
|
10
|
+
const stored = this.extensionState().whatsappBotMessageIds;
|
|
11
|
+
this.#botMessageIds = new Set(Array.isArray(stored)
|
|
12
|
+
? stored.filter((id) => typeof id === 'string' && id).slice(-BOT_MESSAGE_LIMIT)
|
|
13
|
+
: []);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
isBotMessage(jid, id) {
|
|
18
|
+
return typeof id === 'string' && Boolean(id) && this.#botMessageIds.has(`${jid}:${id}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async rememberBotMessage(jid, id) {
|
|
22
|
+
if (typeof jid !== 'string' || !jid.endsWith('@g.us')
|
|
23
|
+
|| typeof id !== 'string' || !id || this.isBotMessage(jid, id)) return;
|
|
24
|
+
this.#botMessageIds.add(`${jid}:${id}`);
|
|
25
|
+
while (this.#botMessageIds.size > BOT_MESSAGE_LIMIT) {
|
|
26
|
+
this.#botMessageIds.delete(this.#botMessageIds.values().next().value);
|
|
27
|
+
}
|
|
28
|
+
this.extensionState().whatsappBotMessageIds = [...this.#botMessageIds];
|
|
29
|
+
await this.persist();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async remove() {
|
|
33
|
+
await super.remove();
|
|
34
|
+
this.#botMessageIds.clear();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -311,6 +311,7 @@ function whatsappAccountMatcher(accountJid, aliases) {
|
|
|
311
311
|
export function normalizeWhatsappMessage(message, accountJid, {
|
|
312
312
|
download = downloadMediaMessage,
|
|
313
313
|
accountAliases = [],
|
|
314
|
+
isBotMessage = () => false,
|
|
314
315
|
} = {}) {
|
|
315
316
|
const remoteJid = typeof message?.key?.remoteJid === 'string' ? message.key.remoteJid : '';
|
|
316
317
|
const alternateRemoteJid = typeof message?.key?.remoteJidAlt === 'string'
|
|
@@ -332,8 +333,11 @@ export function normalizeWhatsappMessage(message, accountJid, {
|
|
|
332
333
|
const context = messageContext(content);
|
|
333
334
|
const mentioned = Array.isArray(context?.mentionedJid)
|
|
334
335
|
&& context.mentionedJid.some(matchesAccount);
|
|
335
|
-
|
|
336
|
-
|
|
336
|
+
// The linked account also sends human messages. Only a known bot message
|
|
337
|
+
// in this chat counts as a reply trigger, not every reply to the account.
|
|
338
|
+
const replyToBot = typeof context?.participant === 'string'
|
|
339
|
+
&& matchesAccount(context.participant)
|
|
340
|
+
&& isBotMessage(remoteJid, context.stanzaId) === true;
|
|
337
341
|
const image = whatsappImageSource(message, content, download, { viewOnce });
|
|
338
342
|
const file = whatsappFileSource(message, content, download);
|
|
339
343
|
const replyTo = whatsappReplyReference(context);
|
|
@@ -352,7 +356,7 @@ export function normalizeWhatsappMessage(message, accountJid, {
|
|
|
352
356
|
images: image ? [image] : [],
|
|
353
357
|
files: file ? [file] : [],
|
|
354
358
|
...(replyTo ? { replyTo } : {}),
|
|
355
|
-
addressed: !group ||
|
|
359
|
+
addressed: !group || mentioned || replyToBot,
|
|
356
360
|
selfChat,
|
|
357
361
|
replyTarget: { jid: remoteJid, quoted: message, selfChat },
|
|
358
362
|
reactionTarget: { jid: remoteJid, key: message.key },
|
|
@@ -462,6 +466,7 @@ function whatsappArtifactError(error) {
|
|
|
462
466
|
export class WhatsappBotClient {
|
|
463
467
|
#socket;
|
|
464
468
|
#outboundIds;
|
|
469
|
+
#rememberBotMessage;
|
|
465
470
|
#signal;
|
|
466
471
|
#abortController = new AbortController();
|
|
467
472
|
#mediaUploadTimeoutMs;
|
|
@@ -473,9 +478,11 @@ export class WhatsappBotClient {
|
|
|
473
478
|
signal,
|
|
474
479
|
mediaUploadTimeoutMs = WHATSAPP_MEDIA_UPLOAD_TIMEOUT_MS,
|
|
475
480
|
logger = console,
|
|
481
|
+
rememberBotMessage = async () => {},
|
|
476
482
|
} = {}) {
|
|
477
483
|
this.#socket = socket;
|
|
478
484
|
this.#outboundIds = outboundIds;
|
|
485
|
+
this.#rememberBotMessage = rememberBotMessage;
|
|
479
486
|
this.#signal = signal
|
|
480
487
|
? AbortSignal.any([signal, this.#abortController.signal])
|
|
481
488
|
: this.#abortController.signal;
|
|
@@ -530,6 +537,7 @@ export class WhatsappBotClient {
|
|
|
530
537
|
);
|
|
531
538
|
const result = await waitWithSignal(pending, this.#signal);
|
|
532
539
|
this.#outboundIds.remember(result?.key?.id);
|
|
540
|
+
await this.#rememberBotMessage(target.jid, edit?.id ?? result?.key?.id ?? messageId);
|
|
533
541
|
return {
|
|
534
542
|
...result,
|
|
535
543
|
key: { remoteJid: target.jid, fromMe: true, id: messageId, ...result?.key },
|
|
@@ -663,6 +671,7 @@ export class WhatsappBotClient {
|
|
|
663
671
|
}
|
|
664
672
|
this.#signal?.throwIfAborted();
|
|
665
673
|
this.#outboundIds.remember(result?.key?.id);
|
|
674
|
+
await this.#rememberBotMessage(target.jid, result?.key?.id ?? messageId);
|
|
666
675
|
return result;
|
|
667
676
|
}
|
|
668
677
|
|
|
@@ -800,6 +809,7 @@ export class WhatsappRuntime {
|
|
|
800
809
|
onMessage: async (raw, context) => {
|
|
801
810
|
const linkedAccount = context?.socket?.user;
|
|
802
811
|
const message = normalizeWhatsappMessage(raw, this.#config.accountJid, {
|
|
812
|
+
isBotMessage: (jid, id) => this.#state.isBotMessage?.(jid, id) === true,
|
|
803
813
|
accountAliases: [
|
|
804
814
|
linkedAccount?.id,
|
|
805
815
|
linkedAccount?.lid,
|
|
@@ -838,6 +848,15 @@ export class WhatsappRuntime {
|
|
|
838
848
|
throw new Error('WhatsApp linked account does not match the saved bot');
|
|
839
849
|
}
|
|
840
850
|
const client = new WhatsappBotClient(session.socket, outboundIds, {
|
|
851
|
+
rememberBotMessage: async (jid, id) => {
|
|
852
|
+
try {
|
|
853
|
+
await this.#state.rememberBotMessage?.(jid, id);
|
|
854
|
+
} catch (error) {
|
|
855
|
+
// Delivery already succeeded; a bookkeeping failure must not turn
|
|
856
|
+
// it into a failed send that can be retried and duplicated.
|
|
857
|
+
this.#logger.warn?.('[dsh-im:whatsapp] failed to persist bot message identity:', error);
|
|
858
|
+
}
|
|
859
|
+
},
|
|
841
860
|
signal: controller.signal,
|
|
842
861
|
mediaUploadTimeoutMs: this.#mediaUploadTimeoutMs,
|
|
843
862
|
logger: this.#logger,
|