@botharness/im-provider 4.32.0-botharness.5 → 4.32.0-botharness.7
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/PROVENANCE.json +4 -4
- package/lib/client.js +3 -3
- package/lib/index.js +195 -195
- package/package.json +3 -3
- package/src/channels/shared/exclusive-inbound-consumers.mjs +5 -3
- package/src/channels/weixin/external-consumer.mjs +30 -10
- package/src/channels/weixin/external-files.mjs +18 -5
- package/src/channels/weixin/weixin-controller.mjs +7 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@botharness/im-provider",
|
|
3
|
-
"version": "4.32.0-botharness.
|
|
3
|
+
"version": "4.32.0-botharness.7",
|
|
4
4
|
"icon": "./assets/logo-plugin-message-link-gradient.webp",
|
|
5
5
|
"description": "BotHarness-qualified IM Provider, maintained from dsh-im",
|
|
6
6
|
"keywords": [
|
|
@@ -104,7 +104,7 @@
|
|
|
104
104
|
"type": "module",
|
|
105
105
|
"repository": {
|
|
106
106
|
"type": "git",
|
|
107
|
-
"url": "git+https://github.com/
|
|
107
|
+
"url": "git+https://github.com/BotHarness/BotHarness.git"
|
|
108
108
|
},
|
|
109
109
|
"homepage": "https://botharness.ai/docs/lark-connection/",
|
|
110
110
|
"bugs": {
|
|
@@ -176,6 +176,6 @@
|
|
|
176
176
|
},
|
|
177
177
|
"botharness": {
|
|
178
178
|
"managedByProduct": true,
|
|
179
|
-
"upstreamSource": "
|
|
179
|
+
"upstreamSource": "6f8cc9b2713a03b1340e604461900a474adacfa3"
|
|
180
180
|
}
|
|
181
181
|
}
|
|
@@ -6,15 +6,17 @@ export class ExclusiveInboundConsumers {
|
|
|
6
6
|
#entries = new Map();
|
|
7
7
|
|
|
8
8
|
acceptsFiles(botId) { return this.#entries.get(botId)?.sourceFiles === true; }
|
|
9
|
+
acceptsImages(botId) { return this.#entries.get(botId)?.sourceImages === true; }
|
|
10
|
+
acceptsVoiceTranscripts(botId) { return this.#entries.get(botId)?.sourceVoiceTranscripts === true; }
|
|
9
11
|
acceptsOrdinary(botId) { return this.#entries.get(botId)?.ordinaryText === true; }
|
|
10
12
|
|
|
11
|
-
register(botId, { fingerprint, onEvent, signal, sourceFiles = false, ordinaryText = false, onEcho }) {
|
|
13
|
+
register(botId, { fingerprint, onEvent, signal, sourceFiles = false, sourceImages = false, sourceVoiceTranscripts = false, ordinaryText = false, onEcho }) {
|
|
12
14
|
if (this.#entries.has(botId)) throw refusal('consumer-conflict');
|
|
13
|
-
if (!/^[a-f0-9]{64}$/.test(fingerprint ?? '') || typeof onEvent !== 'function' || typeof sourceFiles !== 'boolean' || typeof ordinaryText !== 'boolean' || (onEcho !== undefined && typeof onEcho !== 'function'))
|
|
15
|
+
if (!/^[a-f0-9]{64}$/.test(fingerprint ?? '') || typeof onEvent !== 'function' || typeof sourceFiles !== 'boolean' || typeof sourceImages !== 'boolean' || typeof sourceVoiceTranscripts !== 'boolean' || typeof ordinaryText !== 'boolean' || (onEcho !== undefined && typeof onEcho !== 'function'))
|
|
14
16
|
throw refusal('bad-request');
|
|
15
17
|
signal?.throwIfAborted();
|
|
16
18
|
const controller = new AbortController();
|
|
17
|
-
const entry = { fingerprint, onEvent, onEcho, sourceFiles, ordinaryText, controller, dispose: undefined };
|
|
19
|
+
const entry = { fingerprint, onEvent, onEcho, sourceFiles, sourceImages, sourceVoiceTranscripts, ordinaryText, controller, dispose: undefined };
|
|
18
20
|
const dispose = () => {
|
|
19
21
|
if (this.#entries.get(botId) === entry) this.#entries.delete(botId);
|
|
20
22
|
controller.abort(refusal('consumer-unavailable'));
|
|
@@ -19,19 +19,39 @@ export function pairedWeixinAccount(config, token) {
|
|
|
19
19
|
fingerprint: createHash('sha256').update(JSON.stringify(identity)).digest('hex') });
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
/** Paired-owner text and opt-in native files. No group, mention, or thread is invented. */
|
|
23
|
-
export function normalizeWeixinExternalText(message, { botId, account, sourceFiles = false }) {
|
|
24
|
-
if (message?.message_type !== 1 || message.from_user_id !== account.ownerUserId) return null;
|
|
22
|
+
/** Paired-owner text and opt-in native files/images. No group, mention, or thread is invented. */
|
|
23
|
+
export function normalizeWeixinExternalText(message, { botId, account, sourceFiles = false, sourceImages = false, sourceVoiceTranscripts = false }) {
|
|
24
|
+
if (message?.message_type !== 1 || message.from_user_id !== account.ownerUserId || message.group_id) return null;
|
|
25
25
|
if (message.to_user_id !== account.accountId) throw weixinRefusal('account-changed');
|
|
26
26
|
if (!Array.isArray(message.item_list) || !message.item_list.length
|
|
27
|
-
|| message.item_list.some(item => item?.type !== 1 && !(sourceFiles && item?.type === 4))) return null;
|
|
27
|
+
|| message.item_list.some(item => item?.type !== 1 && !(sourceFiles && item?.type === 4) && !(sourceImages && item?.type === 2) && !(sourceVoiceTranscripts && item?.type === 3))) return null;
|
|
28
28
|
const messageId = weixinMessageId(message);
|
|
29
|
-
const
|
|
29
|
+
const voiceItems = message.item_list.filter(item => item?.type === 3);
|
|
30
|
+
let voice;
|
|
31
|
+
let transcript;
|
|
32
|
+
if (voiceItems.length) {
|
|
33
|
+
if (voiceItems.length !== 1 || message.item_list.length !== 1) throw weixinRefusal('invalid-inbound');
|
|
34
|
+
const item = voiceItems[0];
|
|
35
|
+
if (item.is_completed === false || message.message_state === 1) return null;
|
|
36
|
+
const native = item.voice_item;
|
|
37
|
+
if (!native || typeof native !== 'object' || (native.text !== undefined && typeof native.text !== 'string')
|
|
38
|
+
|| (item.msg_id !== undefined && !id(item.msg_id))
|
|
39
|
+
|| (native.playtime !== undefined && (!Number.isSafeInteger(native.playtime) || native.playtime < 0)))
|
|
40
|
+
throw weixinRefusal('invalid-inbound');
|
|
41
|
+
transcript = native.text?.trim();
|
|
42
|
+
voice = Object.freeze({ transcript: transcript ? 'platform' : 'unavailable',
|
|
43
|
+
...(item.msg_id === undefined ? {} : { itemId: item.msg_id }),
|
|
44
|
+
...(native.playtime === undefined ? {} : { durationMs: native.playtime }) });
|
|
45
|
+
}
|
|
46
|
+
const files = message.item_list.filter(item => item?.type === 4 || item?.type === 2);
|
|
47
|
+
const image = files[0]?.type === 2 ? files[0].image_item : undefined;
|
|
48
|
+
if (files[0]?.type === 2 && (!image || typeof image !== 'object')) throw weixinRefusal('resource-unavailable');
|
|
30
49
|
if (files.length > 1) throw weixinRefusal('resource-unavailable');
|
|
31
|
-
const nativeFile = files[0]?.file_item;
|
|
50
|
+
const nativeFile = image ? { file_name: 'image', image: true } : files[0]?.file_item;
|
|
32
51
|
if (files.length && (!nativeFile || !id(nativeFile.file_name)
|
|
33
52
|
|| /[\x00-\x1f\x7f/\\]/.test(nativeFile.file_name))) throw weixinRefusal('resource-unavailable');
|
|
34
|
-
const text =
|
|
53
|
+
const text = voice ? (transcript || '[WeChat voice message: platform did not provide a transcript]')
|
|
54
|
+
: extractWeixinText(message) || (nativeFile ? (image ? '[Image]' : `[File: ${nativeFile.file_name}]`) : '');
|
|
35
55
|
const time = typeof message.create_time_ms === 'string' ? Number(message.create_time_ms) : message.create_time_ms;
|
|
36
56
|
if (!messageId || !/^\d+$/.test(messageId) || !text?.trim() || text.length > 16000
|
|
37
57
|
|| !Number.isSafeInteger(time) || time <= 0 || !continuation(message.context_token)) throw weixinRefusal('invalid-inbound');
|
|
@@ -39,7 +59,7 @@ export function normalizeWeixinExternalText(message, { botId, account, sourceFil
|
|
|
39
59
|
if (!Number.isFinite(at.getTime())) throw weixinRefusal('invalid-inbound');
|
|
40
60
|
const conversationId = account.ownerUserId;
|
|
41
61
|
const resourceKey = nativeFile ? createHash('sha256').update(JSON.stringify([
|
|
42
|
-
account.fingerprint, conversationId, messageId, nativeFile.file_name, nativeFile.len ?? null,
|
|
62
|
+
account.fingerprint, conversationId, messageId, nativeFile.file_name, nativeFile.len ?? null, ...(image ? ['image'] : []),
|
|
43
63
|
])).digest('hex') : undefined;
|
|
44
64
|
const size = nativeFile?.len === undefined ? undefined : Number(nativeFile.len);
|
|
45
65
|
if (nativeFile?.len !== undefined && (!/^\d+$/.test(String(nativeFile.len))
|
|
@@ -47,12 +67,12 @@ export function normalizeWeixinExternalText(message, { botId, account, sourceFil
|
|
|
47
67
|
const attachments = nativeFile ? Object.freeze([Object.freeze({
|
|
48
68
|
id: createHash('sha256').update(JSON.stringify([account.fingerprint, conversationId, messageId, resourceKey])).digest('hex'),
|
|
49
69
|
messageId, resourceKey, name: nativeFile.file_name,
|
|
50
|
-
...(size > 0 ? { sizeBytes: size } : {}), mediaType: 'application/octet-stream',
|
|
70
|
+
...(size > 0 ? { sizeBytes: size } : {}), mediaType: image ? 'image/unknown' : 'application/octet-stream',
|
|
51
71
|
})]) : undefined;
|
|
52
72
|
return Object.freeze({ version: 1, channel: 'weixin', botId, fingerprint: account.fingerprint,
|
|
53
73
|
eventId: messageId, messageId, actor: Object.freeze({ kind: 'user', id: account.ownerUserId }),
|
|
54
74
|
conversation: Object.freeze({ kind: 'dm', id: conversationId }), mentions: Object.freeze([]),
|
|
55
|
-
mentionedAccount: false, at: at.toISOString(), text, ...(attachments ? { attachments } : {}),
|
|
75
|
+
mentionedAccount: false, at: at.toISOString(), text, ...(voice ? { voice } : {}), ...(attachments ? { attachments } : {}),
|
|
56
76
|
reply: Object.freeze({ messageId, conversationId, actorId: account.ownerUserId }),
|
|
57
77
|
replay: Object.freeze({ kind: 'provider-redelivery', resumeCursor: false, gapPossible: true }) });
|
|
58
78
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { detectedImageMediaType } from '../shared/image-prompt.mjs';
|
|
1
2
|
import { weixinRefusal } from './external-consumer.mjs';
|
|
2
3
|
|
|
3
4
|
export const WEIXIN_EXTERNAL_FILE_LIMIT = 25 * 1024 * 1024;
|
|
@@ -5,13 +6,17 @@ export const WEIXIN_EXTERNAL_FILE_LIMIT = 25 * 1024 * 1024;
|
|
|
5
6
|
/** A source-scoped file ticket is private state, not a model-readable download URL. */
|
|
6
7
|
export function privateWeixinFile(message, event) {
|
|
7
8
|
if (!event.attachments?.length) return undefined;
|
|
8
|
-
const
|
|
9
|
+
const native = message.item_list.find(item => item.type === 4 || item.type === 2);
|
|
10
|
+
const image = native?.type === 2;
|
|
11
|
+
const file = image ? native.image_item : native?.file_item;
|
|
9
12
|
const media = file?.media;
|
|
10
13
|
if (!media || typeof media !== 'object'
|
|
11
|
-
|| typeof
|
|
14
|
+
|| (!(image && typeof file.aeskey === 'string' && /^[a-fA-F0-9]{32}$/.test(file.aeskey))
|
|
15
|
+
&& (typeof media.aes_key !== 'string' || !media.aes_key || media.aes_key.length > 128))
|
|
12
16
|
|| ![media.encrypt_query_param, media.full_url].some(value => typeof value === 'string' && value.length > 0 && value.length <= 16384))
|
|
13
17
|
throw weixinRefusal('resource-unavailable');
|
|
14
|
-
return { attachment: event.attachments[0],
|
|
18
|
+
return { attachment: event.attachments[0], ...(image ? { kind: 'image' } : {}), item: {
|
|
19
|
+
...(image ? (typeof file.aeskey === 'string' ? { aeskey: file.aeskey } : {}) : { file_name: file.file_name }),
|
|
15
20
|
...(file.len === undefined ? {} : { len: String(file.len) }),
|
|
16
21
|
media: { aes_key: media.aes_key, encrypt_type: media.encrypt_type,
|
|
17
22
|
...(typeof media.encrypt_query_param === 'string' && media.encrypt_query_param.length <= 16384
|
|
@@ -27,12 +32,16 @@ export async function readWeixinExternalFile(api, source, attachment, { signal,
|
|
|
27
32
|
|| ['id', 'messageId', 'resourceKey', 'name', 'sizeBytes', 'mediaType'].some(key => attachment[key] !== saved.attachment[key]))
|
|
28
33
|
throw weixinRefusal('stale-route');
|
|
29
34
|
if (attachment.sizeBytes > WEIXIN_EXTERNAL_FILE_LIMIT) throw weixinRefusal('artifact-too-large');
|
|
30
|
-
const
|
|
35
|
+
const image = saved.kind === 'image';
|
|
36
|
+
const file = image
|
|
37
|
+
? api.inboundImages({ item_list: [{ type: 2, image_item: saved.item }] })[0]
|
|
38
|
+
: api.inboundFiles({ item_list: [{ type: 4, file_item: saved.item }] })[0];
|
|
31
39
|
if (!file) throw weixinRefusal('resource-unavailable');
|
|
32
40
|
const data = await file.load({ signal, maxBytes: WEIXIN_EXTERNAL_FILE_LIMIT });
|
|
33
41
|
assertCurrent();
|
|
34
42
|
if (!(data instanceof Uint8Array) || data.byteLength === 0) throw weixinRefusal('resource-unavailable');
|
|
35
43
|
if (data.byteLength > WEIXIN_EXTERNAL_FILE_LIMIT) throw weixinRefusal('artifact-too-large');
|
|
44
|
+
if (image && !detectedImageMediaType(data)) throw weixinRefusal('resource-unavailable');
|
|
36
45
|
if (saved.item.len !== undefined && data.byteLength !== Number(saved.item.len)) throw weixinRefusal('resource-unavailable');
|
|
37
46
|
return (async function* () { assertCurrent(); yield new Uint8Array(data); assertCurrent(); })();
|
|
38
47
|
}
|
|
@@ -44,7 +53,11 @@ export async function replyWeixinExternalFile(api, request, file, { signal, asse
|
|
|
44
53
|
|| !file.bytes.byteLength || file.bytes.byteLength > WEIXIN_EXTERNAL_FILE_LIMIT) throw weixinRefusal('bad-request');
|
|
45
54
|
assertCurrent();
|
|
46
55
|
try {
|
|
47
|
-
const
|
|
56
|
+
const image = typeof file.mediaType === 'string' && file.mediaType.startsWith('image/');
|
|
57
|
+
if (image && file.mediaType !== detectedImageMediaType(file.bytes)) throw weixinRefusal('bad-request');
|
|
58
|
+
const send = image ? api.sendImage : api.sendFile;
|
|
59
|
+
if (typeof send !== 'function') throw weixinRefusal('capability-unavailable');
|
|
60
|
+
const result = await send({ ...request, signal,
|
|
48
61
|
file: { artifactId: file.id, fileName: file.name, bytes: Buffer.from(file.bytes) },
|
|
49
62
|
beforeSend: assertCurrent });
|
|
50
63
|
assertCurrent();
|
|
@@ -164,17 +164,18 @@ export class WeixinController {
|
|
|
164
164
|
connected: this.#runtimes.get(botId)?.status?.ready === true,
|
|
165
165
|
capabilities: ['proactive-text-checked', 'exclusive-text-consumer', 'reply-text-checked',
|
|
166
166
|
'reply-context-checked', 'reply-receipt-checked', 'reply-fence-checked',
|
|
167
|
-
'source-file-checked', 'reply-file-checked', 'reply-file-fence-checked'
|
|
167
|
+
'source-file-checked', 'reply-file-checked', 'reply-file-fence-checked',
|
|
168
|
+
'source-image-checked', 'reply-image-fence-checked', 'source-voice-transcript-checked'] };
|
|
168
169
|
});
|
|
169
170
|
}
|
|
170
171
|
|
|
171
|
-
async consumeInbound(botId, { expectedFingerprint, onEvent, signal, sourceFiles = false } = {}) {
|
|
172
|
+
async consumeInbound(botId, { expectedFingerprint, onEvent, signal, sourceFiles = false, sourceImages = false, sourceVoiceTranscripts = false } = {}) {
|
|
172
173
|
return this.#withBotTransition(botId, async () => {
|
|
173
174
|
const config = this.#configStore.get(botId);
|
|
174
175
|
if (!config) throw weixinRefusal('unknown-bot');
|
|
175
176
|
const account = await this.#deliveryAccount(config, signal);
|
|
176
177
|
if (account.fingerprint !== expectedFingerprint) throw weixinRefusal('account-changed');
|
|
177
|
-
const dispose = this.#inboundConsumers.register(botId, { fingerprint: expectedFingerprint, onEvent, signal, sourceFiles });
|
|
178
|
+
const dispose = this.#inboundConsumers.register(botId, { fingerprint: expectedFingerprint, onEvent, signal, sourceFiles, sourceImages, sourceVoiceTranscripts });
|
|
178
179
|
try {
|
|
179
180
|
const saved = await this.#configStore.save({ ...config, consumerMode: 'external-consumer' });
|
|
180
181
|
await this.#startRuntime(saved, await this.#resolveToken(saved.tokenRef));
|
|
@@ -675,7 +676,9 @@ export class WeixinController {
|
|
|
675
676
|
if (current.fingerprint !== account.fingerprint) throw weixinRefusal('account-changed');
|
|
676
677
|
this.#inboundConsumers.signalFor(config.botId, account.fingerprint).throwIfAborted();
|
|
677
678
|
const event = normalizeWeixinExternalText(message, { botId: config.botId, account,
|
|
678
|
-
sourceFiles: this.#inboundConsumers.acceptsFiles(config.botId)
|
|
679
|
+
sourceFiles: this.#inboundConsumers.acceptsFiles(config.botId),
|
|
680
|
+
sourceImages: this.#inboundConsumers.acceptsImages(config.botId),
|
|
681
|
+
sourceVoiceTranscripts: this.#inboundConsumers.acceptsVoiceTranscripts(config.botId) });
|
|
679
682
|
if (!event) return { accepted: true, ignored: true };
|
|
680
683
|
await state.rememberExternalReplySource({ messageId: event.messageId, actorId: event.actor.id,
|
|
681
684
|
fingerprint: account.fingerprint, contextToken: message.context_token,
|