Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/botharness-lark-audio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Checked Lark parent-audio candidate

Refs [BotHarness/DeepSeekBot#1023](https://github.com/BotHarness/DeepSeekBot/issues/1023).

The exclusive external Consumer can opt into `sourceVoiceAudio` alongside `sourceFiles`. A native user text request that mentions the receiving App and replies to a native `audio` message then carries that parent's exact voice descriptor: one `audio/unknown` attachment named `voice.opus` and `voice: { transcript: 'unavailable', itemId, durationMs? }`. Intake downloads no bytes. Lark supplies no transcript, so none is claimed; a missing native `duration` stays absent.

Resource reads re-fetch the request and its parent, compare the exact descriptor, and fetch the parent's `file_key` with `type=file`. Existing cancellation, consumer-lifetime and 25 MiB limits apply. A changed or missing parent fails with `stale-route`. The Provider does not decode or transcode; the consumer owns playback preparation. Feishu advertises `source-voice-audio-checked`.

## 中文

独占 external Consumer 在 `sourceFiles` 之外明确启用 `sourceVoiceAudio` 后,可关联“真正 @ 接收 App 的用户文本请求”所引用的原生 `audio` 语音:一个 `audio/unknown` 附件(名称 `voice.opus`)以及 `voice: { transcript: 'unavailable', itemId, durationMs? }`。入站不下载字节;飞书不提供转写,因此不声称有转写,平台未给出时长时保持缺失。

读取前重新核对原请求与父消息,比较完整资源描述,再以 `type=file` 获取父消息的 `file_key`;沿用取消、消费者生命周期和 25 MiB 限制,来源变化或缺失返回 `stale-route`。Provider 不解码、不转码,播放准备由消费者负责。飞书声明 `source-voice-audio-checked` 能力。
416 changes: 208 additions & 208 deletions lib/index.js

Large diffs are not rendered by default.

23 changes: 20 additions & 3 deletions src/channels/feishu/external-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ export async function checkedReplySource(client, route, signal) {
return source;
}

async function parentResources(client, route, signal, { sourceVideos = false, sourceImages = false } = {}) {
async function parentResources(client, route, signal, { sourceVideos = false, sourceImages = false, sourceVoiceAudio = false } = {}) {
if (!route.parentId || route.parentId === route.messageId) return undefined;
signal?.throwIfAborted();
const result = await client.im.v1.message.get({ path: { message_id: route.parentId } });
const file = result?.data?.items?.find(item => item.message_id === route.parentId);
if (result?.code || !file || file.deleted === true || file.chat_id !== route.conversationId)
throw failure('stale-route');
if (file.msg_type !== 'file' && !(sourceVideos && file.msg_type === 'media')) return undefined;
if (file.msg_type !== 'file' && !(sourceVideos && file.msg_type === 'media')
&& !(sourceVoiceAudio && file.msg_type === 'audio')) return undefined;
if (file.thread_id && route.threadId && file.thread_id !== route.threadId) throw failure('stale-route');
let content;
try { content = JSON.parse(file.body?.content); } catch { throw failure('invalid-inbound'); }
if (file.msg_type === 'audio') return parentAudio(route, file, content, signal);
if (typeof content?.file_key !== 'string' || !content.file_key || content.file_key.length > 512
|| typeof content.file_name !== 'string' || !content.file_name || content.file_name.length > 512
|| (content.duration !== undefined && (!Number.isSafeInteger(content.duration) || content.duration < 0))
Expand All @@ -54,6 +56,21 @@ async function parentResources(client, route, signal, { sourceVideos = false, so
});
}

function parentAudio(route, file, content, signal) {
if (typeof content?.file_key !== 'string' || !content.file_key || content.file_key.length > 512
|| (content.duration !== undefined && (!Number.isSafeInteger(content.duration) || content.duration < 0)))
throw failure('invalid-inbound');
signal?.throwIfAborted();
const attachment = Object.freeze({
id: createHash('sha256').update(JSON.stringify([route.conversationId, file.message_id, content.file_key])).digest('hex'),
messageId: file.message_id, resourceKey: content.file_key, name: 'voice.opus', mediaType: 'audio/unknown',
});
return Object.freeze({ attachments: Object.freeze([attachment]), voice: Object.freeze({
transcript: 'unavailable', itemId: attachment.id,
...(content.duration === undefined ? {} : { durationMs: content.duration }),
}) });
}

export async function externalAttachments(client, evidence, signal, options) {
if (evidence.attachments?.length || !evidence.mentionedAccount || !evidence.reply.parentId) return evidence;
const resources = await parentResources(client, evidence.reply, signal, options);
Expand All @@ -65,7 +82,7 @@ export async function readExternalFile(client, route, attachment, { signal, asse
const ownImages = nativeImageContent(source, route.conversationId);
const current = attachment.mediaType === 'image/unknown' && attachment.messageId === route.messageId
? ownImages?.attachments.find(item => item.id === attachment.id)
: (await parentResources(client, route, signal, { sourceVideos: true, sourceImages: true }))?.attachments.find(item => item.id === attachment.id);
: (await parentResources(client, route, signal, { sourceVideos: true, sourceImages: true, sourceVoiceAudio: true }))?.attachments.find(item => item.id === attachment.id);
if (!current || JSON.stringify(current) !== JSON.stringify(attachment)) throw failure('stale-route');
assertCurrent();
const resource = await client.im.v1.messageResource.get({
Expand Down
4 changes: 2 additions & 2 deletions src/channels/feishu/feishu-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -904,10 +904,10 @@ export class FeishuRuntime {
return result;
}

async enrichExternal(evidence, { signal, sourceVideos = false, sourceImages = false } = {}) {
async enrichExternal(evidence, { signal, sourceVideos = false, sourceImages = false, sourceVoiceAudio = false } = {}) {
const client = this.#client;
if (!client || this.#consumerMode !== 'external-consumer') throw Object.assign(new Error('bot-not-connected'), { code: 'bot-not-connected' });
const result = await externalAttachments(client, evidence, signal, { sourceVideos, sourceImages });
const result = await externalAttachments(client, evidence, signal, { sourceVideos, sourceImages, sourceVoiceAudio });
signal?.throwIfAborted();
if (this.#client !== client) throw Object.assign(new Error('bot-not-connected'), { code: 'bot-not-connected' });
return result;
Expand Down
8 changes: 4 additions & 4 deletions src/channels/feishu/multi-bot-controller.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,11 @@ export class MultiBotDshFeishuController {
const account = await this.#deliveryAccount(config);
return { version: 1, botId, channel: 'feishu', account,
connected: isConnected(connectionStatus(this.#runtimes.get(botId))),
capabilities: ['source-video-checked', 'question-card-checked', 'question-card-update-checked', 'question-action-consumer', 'reachable-conversations-checked', 'proactive-fence-checked', 'proactive-text-checked', 'proactive-receipt-checked', 'own-text-echo', 'exclusive-text-consumer', 'reply-text-checked', 'reply-context-checked', 'reply-receipt-checked', 'reply-fence-checked', 'history-text-checked', 'thread-history-text-checked', 'source-file-checked', 'reply-file-checked', 'approval-card-checked', 'approval-card-update-checked', 'approval-action-consumer', 'reaction-write-checked', 'source-image-checked', 'approval-rule-card-checked', 'approval-rule-all-opaque-card-checked', 'pairing-card-checked'] };
capabilities: ['source-video-checked', 'source-voice-audio-checked', 'question-card-checked', 'question-card-update-checked', 'question-action-consumer', 'reachable-conversations-checked', 'proactive-fence-checked', 'proactive-text-checked', 'proactive-receipt-checked', 'own-text-echo', 'exclusive-text-consumer', 'reply-text-checked', 'reply-context-checked', 'reply-receipt-checked', 'reply-fence-checked', 'history-text-checked', 'thread-history-text-checked', 'source-file-checked', 'reply-file-checked', 'approval-card-checked', 'approval-card-update-checked', 'approval-action-consumer', 'reaction-write-checked', 'source-image-checked', 'approval-rule-card-checked', 'approval-rule-all-opaque-card-checked', 'pairing-card-checked'] };
});
}

async consumeInbound(botId, { expectedFingerprint, onEvent, signal, sourceFiles = false, sourceImages = false, sourceVideos = false, onEcho, onAction } = {}) {
async consumeInbound(botId, { expectedFingerprint, onEvent, signal, sourceFiles = false, sourceImages = false, sourceVideos = false, sourceVoiceAudio = false, onEcho, onAction } = {}) {
this.#assertOpen();
return this.#withBotTransition(botId, async () => {
this.#assertOpen();
Expand All @@ -615,7 +615,7 @@ export class MultiBotDshFeishuController {
const account = await this.#deliveryAccount(config);
if (account.fingerprint !== expectedFingerprint)
throw Object.assign(new Error('account-changed'), { code: 'account-changed' });
const dispose = this.#inboundConsumers.register(botId, { fingerprint: expectedFingerprint, onEvent, signal, sourceFiles, sourceImages, sourceVideos, onEcho, onAction });
const dispose = this.#inboundConsumers.register(botId, { fingerprint: expectedFingerprint, onEvent, signal, sourceFiles, sourceImages, sourceVideos, sourceVoiceAudio, onEcho, onAction });
try {
const saved = await this.#configStore.saveBot({ ...config, consumerMode: 'external-consumer' });
const resolved = await this.#credentials.resolve(saved.secretRef);
Expand Down Expand Up @@ -1588,7 +1588,7 @@ export class MultiBotDshFeishuController {
if (evidence === null) return { accepted: true, ignored: true };
if (evidence.attachments?.length && !this.#inboundConsumers.acceptsImages(config.id)) return { accepted: true, ignored: true };
const named = typeof runtime.enrichExternalNames === 'function' ? await runtime.enrichExternalNames(evidence, { signal }) : evidence;
const enriched = this.#inboundConsumers.acceptsFiles(config.id) && typeof runtime.enrichExternal === 'function' ? await runtime.enrichExternal(named, { signal, sourceVideos: this.#inboundConsumers.acceptsVideos(config.id), sourceImages: this.#inboundConsumers.acceptsImages(config.id) }) : named;
const enriched = this.#inboundConsumers.acceptsFiles(config.id) && typeof runtime.enrichExternal === 'function' ? await runtime.enrichExternal(named, { signal, sourceVideos: this.#inboundConsumers.acceptsVideos(config.id), sourceImages: this.#inboundConsumers.acceptsImages(config.id), sourceVoiceAudio: this.#inboundConsumers.acceptsVoiceAudio(config.id) }) : named;
return this.#inboundConsumers.accept(config.id, enriched, signal);
}),
}));
Expand Down
26 changes: 26 additions & 0 deletions test/channels/feishu/external-files.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ test('native media metadata and its cover require explicit opt-in and remain bou
await assert.rejects(externalAttachments(fx.client, input, context().signal, { sourceVideos: true }), { code: 'stale-route' });
});

test('native audio requires explicit voice-audio opt-in and stays bound to the exact parent', async () => {
const fx = fixture();
let audio = { ...parent, msg_type: 'audio', body: { content: JSON.stringify({ file_key: 'native-voice', duration: 3200 }) } };
fx.client.im.v1.message.get = async ({ path }) => ({ data: { items: [path.message_id === 'mention' ? source : audio] } });
const input = { mentionedAccount: true, reply: route };
assert.equal((await externalAttachments(fx.client, input, context().signal, { sourceVideos: true, sourceImages: true })).attachments, undefined);
assert.equal((await externalAttachments(fx.client, { ...input, mentionedAccount: false }, context().signal, { sourceVoiceAudio: true })).attachments, undefined);
const evidence = await externalAttachments(fx.client, input, context().signal, { sourceVoiceAudio: true });
assert.equal(fx.downloads, 0);
assert.equal(evidence.attachments.length, 1);
assert.deepEqual({ ...evidence.attachments[0], id: undefined }, { id: undefined, messageId: 'file', resourceKey: 'native-voice', name: 'voice.opus', mediaType: 'audio/unknown' });
assert.deepEqual(evidence.voice, { transcript: 'unavailable', itemId: evidence.attachments[0].id, durationMs: 3200 });
const stream = await readExternalFile(fx.client, route, evidence.attachments[0], context());
for await (const chunk of stream) void chunk;
assert.deepEqual(fx.calls[0], { path: { message_id: 'file', file_key: 'native-voice' }, params: { type: 'file' } });
audio = { ...audio, body: { content: JSON.stringify({ file_key: 'changed-voice', duration: 3200 }) } };
await assert.rejects(readExternalFile(fx.client, route, evidence.attachments[0], context()), { code: 'stale-route' });
assert.equal(fx.downloads, 1);
audio = { ...audio, body: { content: JSON.stringify({ file_key: 'native-voice', duration: -1 }) } };
await assert.rejects(externalAttachments(fx.client, input, context().signal, { sourceVoiceAudio: true }), { code: 'invalid-inbound' });
audio = { ...audio, body: { content: JSON.stringify({ file_key: 'native-voice' }) } };
assert.deepEqual((await externalAttachments(fx.client, input, context().signal, { sourceVoiceAudio: true })).voice, { transcript: 'unavailable', itemId: evidence.attachments[0].id });
audio = { ...audio, chat_id: 'other-chat' };
await assert.rejects(externalAttachments(fx.client, input, context().signal, { sourceVoiceAudio: true }), { code: 'stale-route' });
});

test('native media without optional duration or cover stays playable; a file named mp4 is still a file', async () => {
const fx = fixture();
let media = { ...parent, msg_type: 'media', body: { content: JSON.stringify({ file_key: 'native-video', file_name: 'clip.mp4' }) } };
Expand Down
6 changes: 4 additions & 2 deletions test/channels/feishu/multi-bot-controller.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1601,16 +1601,17 @@ test('releasing the exclusive consumer cancels a pending history read', async ()


for (const sourceVideos of [false, true]) {
test(`video qualification opt-in reaches production enrichment (sourceVideos=${sourceVideos})`, async () => {
test(`media qualification opt-ins reach production enrichment (sourceVideos=${sourceVideos})`, async () => {
const existing = bot('bot_media', 'media');
const fx = fixture({ bots: [existing], secrets: { [existing.secretRef]: 'local-secret' },
verifyApp: async () => ({ openId: existing.botOpenId, name: 'Verified' }) });
await fx.controller.initialize();
try {
const info = await fx.controller.describeDeliveryAccount(existing.id);
assert.ok(info.capabilities.includes('source-video-checked'));
assert.ok(info.capabilities.includes('source-voice-audio-checked'));
await fx.controller.consumeInbound(existing.id, { expectedFingerprint: info.account.fingerprint,
sourceFiles: true, sourceImages: true, sourceVideos, onEvent: async () => ({ accepted: true }) });
sourceFiles: true, sourceImages: true, sourceVideos, sourceVoiceAudio: !sourceVideos, onEvent: async () => ({ accepted: true }) });
const runtime = fx.runtimes.get(existing.id).at(-1);
let options;
runtime.enrichExternal = async (evidence, value) => { options = value; return evidence; };
Expand All @@ -1621,6 +1622,7 @@ for (const sourceVideos of [false, true]) {
create_time: '1790787600000', content: JSON.stringify({ text: '@_user_1 inspect video' }) } });
assert.equal(options.sourceVideos, sourceVideos);
assert.equal(options.sourceImages, true);
assert.equal(options.sourceVoiceAudio, !sourceVideos);
} finally {
await fx.controller.close();
}
Expand Down
Loading