@botharness/im-provider 4.32.0-botharness.27 → 4.32.0-botharness.28
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 +2 -2
- package/lib/index.js +242 -242
- package/package.json +2 -2
- package/plugin-src/host/delivery-service.mjs +2 -1
- package/src/channels/discord/discord-controller.mjs +1 -1
- package/src/channels/discord/discord-runtime.mjs +21 -3
- package/src/channels/discord/external-consumer.mjs +13 -4
- package/src/channels/feishu/external-cards.mjs +9 -10
- package/src/channels/feishu/feishu-runtime.mjs +8 -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.28",
|
|
4
4
|
"icon": "./assets/logo-plugin-message-link-gradient.webp",
|
|
5
5
|
"description": "BotHarness-qualified IM Provider, maintained from dsh-im",
|
|
6
6
|
"keywords": [
|
|
@@ -178,6 +178,6 @@
|
|
|
178
178
|
"peerDependencies": {},
|
|
179
179
|
"botharness": {
|
|
180
180
|
"managedByProduct": true,
|
|
181
|
-
"upstreamSource": "
|
|
181
|
+
"upstreamSource": "b79f94809af560313bda30398ae37f95fd9894c1"
|
|
182
182
|
}
|
|
183
183
|
}
|
|
@@ -613,7 +613,8 @@ export class DeliveryService {
|
|
|
613
613
|
throw deliveryError('capability-unavailable');
|
|
614
614
|
}
|
|
615
615
|
if (account.account?.fingerprint !== expectedFingerprint) throw deliveryError('account-changed');
|
|
616
|
-
const receiptConversation = adapter.channel === 'feishu'
|
|
616
|
+
const receiptConversation = adapter.channel === 'feishu'
|
|
617
|
+
&& (target.kind === 'group' || typeof target.route.chatId === 'string')
|
|
617
618
|
? target.route.chatId : adapter.channel === 'slack' && target.kind === 'conversation'
|
|
618
619
|
? target.route.channelId : adapter.channel === 'qq' && target.kind === 'group'
|
|
619
620
|
? target.route.groupOpenId : adapter.channel === 'weixin' && target.kind === 'user'
|
|
@@ -13,7 +13,7 @@ export class DiscordController extends TokenBotController {
|
|
|
13
13
|
deriveIdentity: deriveDiscordBotIdentity,
|
|
14
14
|
maskPlatformId: maskDiscordBotId,
|
|
15
15
|
checkedDelivery: {
|
|
16
|
-
capabilities: ['proactive-text-checked', 'proactive-receipt-checked', 'proactive-fence-checked', 'reachable-conversations-checked', 'exclusive-text-consumer', 'ordinary-text-consumer', 'reply-text-checked', 'reply-context-checked', 'reply-receipt-checked', 'reply-fence-checked', 'reply-mention-checked',
|
|
16
|
+
capabilities: ['proactive-text-checked', 'proactive-receipt-checked', 'proactive-fence-checked', 'reachable-conversations-checked', 'exclusive-text-consumer', 'ordinary-text-consumer', 'reply-text-checked', 'reply-context-checked', 'reply-receipt-checked', 'reply-fence-checked', 'reply-mention-checked', 'reply-standalone-checked',
|
|
17
17
|
'history-text-checked', 'thread-history-text-checked', 'source-file-checked', 'reply-file-checked'],
|
|
18
18
|
inspectAccount: async (token, config, { signal }) => {
|
|
19
19
|
const api = (options.createApi ?? (args => new DiscordApi(args)))({ token });
|
|
@@ -817,7 +817,7 @@ export class DiscordRuntime {
|
|
|
817
817
|
d: {
|
|
818
818
|
token: this.#token,
|
|
819
819
|
intents: this.#config.consumerMode === 'external-consumer'
|
|
820
|
-
? (1 << 0) | (1 << 9) | (this.#ordinaryAuthorized && this.#externalOrdinaryText() === true ? (1 << 15) : 0)
|
|
820
|
+
? (1 << 0) | (1 << 9) | (1 << 12) | (this.#ordinaryAuthorized && this.#externalOrdinaryText() === true ? (1 << 15) : 0)
|
|
821
821
|
: DISCORD_GATEWAY_INTENTS,
|
|
822
822
|
properties: {
|
|
823
823
|
os: process.platform,
|
|
@@ -937,8 +937,9 @@ export class DiscordRuntime {
|
|
|
937
937
|
|
|
938
938
|
async #acceptExternalMessage(message, sequence, generation) {
|
|
939
939
|
if (!this.#account || !this.#status.ready || generation !== this.#generation
|
|
940
|
-
||
|
|
941
|
-
|| !Array.isArray(message
|
|
940
|
+
|| message?.webhook_id || message?.author?.id === this.#account.userId
|
|
941
|
+
|| !Array.isArray(message?.mentions)) return;
|
|
942
|
+
if (!message.guild_id) return this.#acceptExternalDirectMessage(message, sequence);
|
|
942
943
|
let mentioned = message.mentions.some(user => user.id === this.#account.userId);
|
|
943
944
|
const roleCandidate = Array.isArray(message.mention_roles) && message.mention_roles.some(discordSnowflake);
|
|
944
945
|
if (message.author?.bot === true && !mentioned && !roleCandidate) return;
|
|
@@ -975,6 +976,23 @@ export class DiscordRuntime {
|
|
|
975
976
|
this.#status.messagesReceived++;
|
|
976
977
|
}
|
|
977
978
|
|
|
979
|
+
async #acceptExternalDirectMessage(message, sequence) {
|
|
980
|
+
if (message.author?.bot === true || typeof message.content !== 'string' || !message.content.trim()) return;
|
|
981
|
+
const checked = this.#checkedLifetime();
|
|
982
|
+
const channel = await inspectDiscordSourceChannel(checked.api, message.channel_id, checked.account, checked.signal);
|
|
983
|
+
checked.assertCurrent();
|
|
984
|
+
if (!channel.direct) throw discordRefusal('stale-route');
|
|
985
|
+
let event = normalizeDiscordExternalText(message, { botId: this.#config.botId, account: checked.account,
|
|
986
|
+
channel, eventId: `gateway:${this.#sessionId}:${sequence}` });
|
|
987
|
+
if (!event) return;
|
|
988
|
+
if (this.#externalSourceFiles() === true) event = externalAttachments(event, message);
|
|
989
|
+
if (typeof this.#externalConsumer !== 'function') throw discordRefusal('consumer-unavailable');
|
|
990
|
+
const result = await this.#externalConsumer(event, checked.signal);
|
|
991
|
+
checked.assertCurrent();
|
|
992
|
+
if (result?.accepted !== true) throw discordRefusal('ingress-not-accepted');
|
|
993
|
+
this.#status.messagesReceived++;
|
|
994
|
+
}
|
|
995
|
+
|
|
978
996
|
async #acceptMessage(message, bridge) {
|
|
979
997
|
const messageId = String(message?.id ?? '');
|
|
980
998
|
if (!messageId || this.#state.hasSeen(messageId)) return;
|
|
@@ -4,6 +4,7 @@ export function discordRefusal(code) { return Object.assign(new Error(code), { c
|
|
|
4
4
|
export function discordSnowflake(value) { return typeof value === 'string' && /^[0-9]{5,30}$/.test(value); }
|
|
5
5
|
const PUBLIC_THREAD = 11;
|
|
6
6
|
const TEXT_CHANNEL = 0;
|
|
7
|
+
const DM_CHANNEL = 1;
|
|
7
8
|
const VIEW = 1n << 10n;
|
|
8
9
|
const SEND = 1n << 11n;
|
|
9
10
|
const ATTACH = 1n << 15n;
|
|
@@ -68,6 +69,12 @@ export async function inspectDiscordSourceChannel(api, channelId, account, signa
|
|
|
68
69
|
signal?.throwIfAborted();
|
|
69
70
|
try {
|
|
70
71
|
const channel = await api.getChannel({ channelId, signal });
|
|
72
|
+
if (channel?.id === channelId && channel.type === DM_CHANNEL && channel.guild_id === undefined) {
|
|
73
|
+
const recipient = Array.isArray(channel.recipients) && channel.recipients.length === 1 ? channel.recipients[0] : undefined;
|
|
74
|
+
if (!discordSnowflake(recipient?.id) || recipient.id === account.userId) throw discordRefusal('stale-route');
|
|
75
|
+
signal?.throwIfAborted();
|
|
76
|
+
return { conversationId: channel.id, channelId: channel.id, direct: true, recipientId: recipient.id };
|
|
77
|
+
}
|
|
71
78
|
if (channel?.id !== channelId || !discordSnowflake(channel.guild_id)) throw discordRefusal('stale-route');
|
|
72
79
|
const isThread = channel.type === PUBLIC_THREAD;
|
|
73
80
|
if (channel.type !== TEXT_CHANNEL && !isThread) throw discordRefusal('reply-permission-denied');
|
|
@@ -133,8 +140,9 @@ function normalizeDiscordHumanText(message, { botId, account, channel, eventId }
|
|
|
133
140
|
|| message.content.length > 16000 || !Array.isArray(message.mentions)) return null;
|
|
134
141
|
if (message.guild_id !== channel.guildId || message.channel_id !== channel.channelId)
|
|
135
142
|
throw discordRefusal('stale-route');
|
|
143
|
+
if (channel.direct && (message.author.bot === true || message.author.id !== channel.recipientId)) return null;
|
|
136
144
|
const mentionedAccount = discordMessageMentionsAccount(message, account, channel);
|
|
137
|
-
if ((requireMention || message.author.bot === true) && !mentionedAccount) return null;
|
|
145
|
+
if (!channel.direct && (requireMention || message.author.bot === true) && !mentionedAccount) return null;
|
|
138
146
|
const timestamp = Date.parse(message.timestamp);
|
|
139
147
|
if (!Number.isFinite(timestamp) || typeof eventId !== 'string' || !eventId || eventId.length > 512)
|
|
140
148
|
throw discordRefusal('invalid-inbound');
|
|
@@ -145,7 +153,7 @@ function normalizeDiscordHumanText(message, { botId, account, channel, eventId }
|
|
|
145
153
|
...(user.id === account.userId ? { name: account.name } : {}) }));
|
|
146
154
|
return { version: 1, channel: 'discord', botId, fingerprint: account.fingerprint,
|
|
147
155
|
eventId, messageId: message.id, actor: { kind: message.author.bot === true ? 'bot' : 'user', id: message.author.id, ...(name ? { name: name.slice(0, 512) } : {}) },
|
|
148
|
-
conversation: { kind: 'group', id: channel.conversationId, ...(channel.name ? { name: channel.name } : {}) }, mentions, mentionedAccount,
|
|
156
|
+
conversation: { kind: channel.direct ? 'dm' : 'group', id: channel.conversationId, ...(channel.name ? { name: channel.name } : {}) }, mentions, mentionedAccount,
|
|
149
157
|
at: new Date(timestamp).toISOString(), text: message.content,
|
|
150
158
|
reply: { messageId: message.id, conversationId: channel.conversationId, actorId: message.author.id,
|
|
151
159
|
...(channel.threadId ? { threadId: channel.threadId } : {}) },
|
|
@@ -180,7 +188,7 @@ export function discordReplyMentions(mentionUserIds, route, source) {
|
|
|
180
188
|
return [...new Set(mentionUserIds)];
|
|
181
189
|
}
|
|
182
190
|
|
|
183
|
-
export async function sendDiscordReply(api, account, route, text, { signal, beforeSend, assertCurrent = () => {}, receipt = false, mentionUserIds } = {}) {
|
|
191
|
+
export async function sendDiscordReply(api, account, route, text, { signal, beforeSend, assertCurrent = () => {}, receipt = false, mentionUserIds, standalone = false } = {}) {
|
|
184
192
|
if (typeof text !== 'string' || !text.trim() || text.length > 2000) throw discordRefusal('bad-request');
|
|
185
193
|
const checked = await qualifyDiscordReply(api, account, route, signal);
|
|
186
194
|
const mentions = discordReplyMentions(mentionUserIds, route, checked.source);
|
|
@@ -189,7 +197,8 @@ export async function sendDiscordReply(api, account, route, text, { signal, befo
|
|
|
189
197
|
assertCurrent();
|
|
190
198
|
if (beforeSend && beforeSend() !== true) throw discordRefusal('stale-route');
|
|
191
199
|
signal?.throwIfAborted();
|
|
192
|
-
return sendDiscordCheckedText(api, account, checked.channel, text, { signal, receipt,
|
|
200
|
+
return sendDiscordCheckedText(api, account, checked.channel, text, { signal, receipt,
|
|
201
|
+
replyToMessageId: standalone === true && checked.channel.direct ? undefined : route.messageId,
|
|
193
202
|
...(mentions.length ? { mentionUserIds: mentions } : {}) });
|
|
194
203
|
}
|
|
195
204
|
|
|
@@ -13,6 +13,9 @@ export function externalApprovalCard(value) {
|
|
|
13
13
|
const elements = [{ tag: 'div', text: { tag: 'plain_text', content: value.detail } }];
|
|
14
14
|
if (value.rules !== undefined && (!Array.isArray(value.rules) || ![1, 2].includes(value.rules.length)
|
|
15
15
|
|| value.rules[0] !== 'exact' || (value.rules.length === 2 && value.rules[1] !== 'all-opaque'))) refuse('bad-request');
|
|
16
|
+
if (value.actions !== undefined && (!Array.isArray(value.actions) || value.actions.length < 1 || value.actions.length > 2
|
|
17
|
+
|| value.actions.some(action => !['allowed-once', 'rejected'].includes(action)) || new Set(value.actions).size !== value.actions.length)) refuse('bad-request');
|
|
18
|
+
const offers = action => value.actions === undefined || value.actions.includes(action);
|
|
16
19
|
if (value.ruleConfirmation !== undefined && (!uuid.test(value.ruleConfirmation.id ?? '') || !['exact', 'all-opaque'].includes(value.ruleConfirmation.kind))) refuse('bad-request');
|
|
17
20
|
const ruleAction = (action, label, type = 'default') => ({ tag: 'button', type,
|
|
18
21
|
text: { tag: 'plain_text', content: label }, value: { namespace: 'botharness/approval-rule-v1', requestId: value.requestId, action,
|
|
@@ -24,10 +27,10 @@ export function externalApprovalCard(value) {
|
|
|
24
27
|
ruleAction('cancel-rule', 'Back / 返回'),
|
|
25
28
|
] });
|
|
26
29
|
else if (value.status === 'pending') elements.push({ tag: 'action', actions: [
|
|
27
|
-
{ tag: 'button', type: 'primary', text: { tag: 'plain_text', content: 'Allow once / 允许一次' }, value: { namespace: 'botharness/approval-v1', requestId: value.requestId, action: 'allowed-once' } },
|
|
28
|
-
{ tag: 'button', type: 'danger', text: { tag: 'plain_text', content: 'Reject / 拒绝' }, value: { namespace: 'botharness/approval-v1', requestId: value.requestId, action: 'rejected' } },
|
|
29
|
-
...(value.rules ? [ruleAction('preview-exact', 'Allow matching operations… / 批准匹配操作…')] : []),
|
|
30
|
-
...(value.rules?.includes('all-opaque') ? [ruleAction('preview-all-opaque', 'All opaque tools in this scope… / 此范围内全部不透明工具…', 'danger')] : []),
|
|
30
|
+
...(offers('allowed-once') ? [{ tag: 'button', type: 'primary', text: { tag: 'plain_text', content: 'Allow once / 允许一次' }, value: { namespace: 'botharness/approval-v1', requestId: value.requestId, action: 'allowed-once' } }] : []),
|
|
31
|
+
...(offers('rejected') ? [{ tag: 'button', type: 'danger', text: { tag: 'plain_text', content: 'Reject / 拒绝' }, value: { namespace: 'botharness/approval-v1', requestId: value.requestId, action: 'rejected' } }] : []),
|
|
32
|
+
...(value.rules && offers('allowed-once') ? [ruleAction('preview-exact', 'Allow matching operations… / 批准匹配操作…')] : []),
|
|
33
|
+
...(value.rules?.includes('all-opaque') && offers('allowed-once') ? [ruleAction('preview-all-opaque', 'All opaque tools in this scope… / 此范围内全部不透明工具…', 'danger')] : []),
|
|
31
34
|
] });
|
|
32
35
|
// Both the initial card and later PATCH need shared mode for receiver-visible updates.
|
|
33
36
|
return JSON.stringify({ config: { wide_screen_mode: true, update_multi: true }, header: { title: { tag: 'plain_text', content: value.title }, template: 'blue' }, elements });
|
|
@@ -97,7 +100,7 @@ export async function replyExternalApprovalCard(client, route, card, { signal, a
|
|
|
97
100
|
const current = await qualifyExternalReply(client, route, signal);
|
|
98
101
|
if (current.actorId !== route.actorId) refuse('stale-route');
|
|
99
102
|
const chat = await client.im.v1.chat.get({ path: { chat_id: route.conversationId } }, { signal });
|
|
100
|
-
if (chat?.code !== 0 || !
|
|
103
|
+
if (chat?.code !== 0 || !['p2p', 'group'].includes(chat?.data?.chat_mode)) refuse('stale-route');
|
|
101
104
|
assertCurrent?.();
|
|
102
105
|
signal?.throwIfAborted();
|
|
103
106
|
if (typeof beforeSend !== 'function' || beforeSend() !== true) refuse('stale-route');
|
|
@@ -109,7 +112,7 @@ export async function replyExternalApprovalCard(client, route, card, { signal, a
|
|
|
109
112
|
return { sent: true, receipt: { version: 1, messageId: result.data.message_id, conversationId: result.data.chat_id } };
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
export async function updateExternalApprovalCard(client, identity, receipt, card, { signal, assertCurrent, beforeSend, render = externalApprovalCard
|
|
115
|
+
export async function updateExternalApprovalCard(client, identity, receipt, card, { signal, assertCurrent, beforeSend, render = externalApprovalCard } = {}) {
|
|
113
116
|
const content = render(card);
|
|
114
117
|
if (!identifier(receipt?.messageId, 'om') || !identifier(receipt?.conversationId, 'oc')) refuse('bad-request');
|
|
115
118
|
assertCurrent?.();
|
|
@@ -119,10 +122,6 @@ export async function updateExternalApprovalCard(client, identity, receipt, card
|
|
|
119
122
|
((source.sender.id_type === 'app_id' && source.sender.id === identity.appId) ||
|
|
120
123
|
(source.sender.id_type === 'open_id' && source.sender.id === identity.botOpenId));
|
|
121
124
|
if (result?.code !== 0 || !ownSender || source.deleted || source.chat_id !== receipt.conversationId || source.msg_type !== 'interactive') refuse('stale-route');
|
|
122
|
-
if (privateOnly) {
|
|
123
|
-
const chat = await client.im.v1.chat.get({ path: { chat_id: receipt.conversationId } }, { signal });
|
|
124
|
-
if (chat?.code !== 0 || chat?.data?.chat_mode !== 'p2p') refuse('stale-route');
|
|
125
|
-
}
|
|
126
125
|
assertCurrent?.();
|
|
127
126
|
signal?.throwIfAborted();
|
|
128
127
|
if (typeof beforeSend !== 'function' || beforeSend() !== true) refuse('stale-route');
|
|
@@ -818,7 +818,11 @@ export class FeishuRuntime {
|
|
|
818
818
|
error.code = 'bad-request';
|
|
819
819
|
throw error;
|
|
820
820
|
}
|
|
821
|
-
|
|
821
|
+
// A receipt proves the exact conversation: a group chat, or the private
|
|
822
|
+
// chat a saved user target names alongside its open_id.
|
|
823
|
+
const receiptConversation = target.kind === 'group' ? receiveId
|
|
824
|
+
: nonEmptyString(target?.route?.chatId);
|
|
825
|
+
if (typeof receipt !== 'boolean' || (receipt && !receiptConversation))
|
|
822
826
|
throw Object.assign(new Error('capability-unavailable'), { code: 'capability-unavailable' });
|
|
823
827
|
signal?.throwIfAborted();
|
|
824
828
|
// Use the same native Markdown element as chat, without opening a stream
|
|
@@ -846,7 +850,7 @@ export class FeishuRuntime {
|
|
|
846
850
|
if (!receipt) return { sent: true };
|
|
847
851
|
const messageId = response?.data?.message_id;
|
|
848
852
|
const conversationId = response?.data?.chat_id;
|
|
849
|
-
if (typeof messageId !== 'string' || !messageId || messageId.length > 512 || conversationId !==
|
|
853
|
+
if (typeof messageId !== 'string' || !messageId || messageId.length > 512 || conversationId !== receiptConversation)
|
|
850
854
|
throw Object.assign(new Error('send-result-unknown'), { code: 'send-result-unknown' });
|
|
851
855
|
return { sent: true, receipt: { version: 1, messageId, conversationId } };
|
|
852
856
|
}
|
|
@@ -881,8 +885,8 @@ export class FeishuRuntime {
|
|
|
881
885
|
throw Object.assign(new Error('bot-not-connected'), { code: 'bot-not-connected' });
|
|
882
886
|
};
|
|
883
887
|
assertCurrent();
|
|
884
|
-
return update ? updateExternalApprovalCard(client, identity, route, card, { signal, assertCurrent, beforeSend, render: externalQuestionCard
|
|
885
|
-
: replyExternalApprovalCard(client, route, card, { signal, assertCurrent, beforeSend, render: externalQuestionCard
|
|
888
|
+
return update ? updateExternalApprovalCard(client, identity, route, card, { signal, assertCurrent, beforeSend, render: externalQuestionCard })
|
|
889
|
+
: replyExternalApprovalCard(client, route, card, { signal, assertCurrent, beforeSend, render: externalQuestionCard });
|
|
886
890
|
}
|
|
887
891
|
|
|
888
892
|
async historyChecked(identity, route, query, { signal } = {}) {
|