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
17 changes: 17 additions & 0 deletions docs/botharness-lark-videos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Checked Lark parent-video candidate

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

The exclusive external Consumer can opt into `sourceVideos` alongside `sourceFiles`. A native user text request that mentions the receiving App and replies to a native `media` message can carry the exact parent video descriptor. `sourceImages` additionally enables its native cover descriptor. Neither option downloads bytes at intake. A generic `file` remains a file even when its name ends in `.mp4`.

The original request and parent must still match the current App, actor route, conversation and topic. Resource reads re-fetch both messages, compare the exact descriptor, and use the parent message ID with its original `file_key` (`type=file`) or `image_key` (`type=image`). Existing cancellation, consumer-lifetime and 25 MiB limits remain in force. A changed/missing resource fails rather than selecting another parent or App. Native `duration` is retained as milliseconds when present; a missing duration or cover stays absent. No transcript, cover, codec or MIME is inferred from a name.

Automated qualification: 60 focused tests passed; the complete Feishu test directory passed 779 tests with five existing POSIX-only fixture skips on Windows. Build and package verification passed. **Fresh native browser intake, byte equality, playback and installed-product E2E are pending.** This is a draft candidate, not a completed audio/video qualification or publication.

## 中文

独占 external Consumer 在 `sourceFiles` 之外明确启用 `sourceVideos` 后,可关联“真正 @ 接收 App 的用户文本请求”所引用的原生 `media` 视频。启用 `sourceImages` 时还可关联同一原生消息提供的封面。入站只保存元数据,不下载资源;名称为 `.mp4` 的普通 `file` 仍是文件。

读取前重新核对原请求、原群/话题与父消息,比较完整资源描述,再用父消息 ID 和其原始 `file_key`/`image_key` 获取有界资源。继续遵守当前消费者生命周期、取消和 25 MiB 限制;来源变化或缺失时拒绝,不替换 App 或父消息。平台时长有值时保留毫秒单位,缺失时长或封面保持缺失,不根据文件名猜测转写、封面、编码或 MIME。

60 项聚焦测试、飞书目录 779 项测试、构建和包检查已通过;另有 5 项既有 POSIX 夹具测试在 Windows 跳过。**真实浏览器入站、字节一致、播放和安装包 E2E 尚待验收。** 本候选不表示音视频任务已完成,也未发布。
454 changes: 227 additions & 227 deletions lib/index.js

Large diffs are not rendered by default.

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

async function parentFile(client, route, signal) {
async function parentResources(client, route, signal, { sourceVideos = false, sourceImages = 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') return undefined;
if (file.msg_type !== 'file' && !(sourceVideos && file.msg_type === 'media')) 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 (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)
|| typeof content.file_name !== 'string' || !content.file_name || content.file_name.length > 512
|| (content.duration !== undefined && (!Number.isSafeInteger(content.duration) || content.duration < 0))
|| (file.msg_type === 'media' && content.image_key !== undefined &&
(typeof content.image_key !== 'string' || !content.image_key || content.image_key.length > 512)))
throw failure('invalid-inbound');
signal?.throwIfAborted();
return Object.freeze({
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: content.file_name,
...(file.msg_type === 'media' ? { mediaType: 'video/unknown' } : {}),
});
const poster = file.msg_type === 'media' && sourceImages && content.image_key ? Object.freeze({
id: createHash('sha256').update(JSON.stringify([route.conversationId, file.message_id, content.image_key])).digest('hex'),
messageId: file.message_id, resourceKey: content.image_key, name: 'video-cover', mediaType: 'image/unknown',
}) : undefined;
return Object.freeze({ attachments: Object.freeze([attachment, ...(poster ? [poster] : [])]),
...(file.msg_type === 'media' ? { video: Object.freeze({ itemId: attachment.id,
...(content.duration === undefined ? {} : { durationMs: content.duration }),
...(poster ? { posterAttachmentId: poster.id } : {}),
}) } : {}),
});
}

export async function externalAttachments(client, evidence, signal) {
export async function externalAttachments(client, evidence, signal, options) {
if (evidence.attachments?.length || !evidence.mentionedAccount || !evidence.reply.parentId) return evidence;
const file = await parentFile(client, evidence.reply, signal);
return file ? Object.freeze({ ...evidence, attachments: Object.freeze([file]) }) : evidence;
const resources = await parentResources(client, evidence.reply, signal, options);
return resources ? Object.freeze({ ...evidence, ...resources }) : evidence;
}

export async function readExternalFile(client, route, attachment, { signal, assertCurrent }) {
const source = await checkedReplySource(client, route, signal);
const ownImages = nativeImageContent(source, route.conversationId);
const current = attachment.mediaType === 'image/unknown'
const current = attachment.mediaType === 'image/unknown' && attachment.messageId === route.messageId
? ownImages?.attachments.find(item => item.id === attachment.id)
: await parentFile(client, route, signal);
: (await parentResources(client, route, signal, { sourceVideos: true, sourceImages: 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 } = {}) {
async enrichExternal(evidence, { signal, sourceVideos = false, sourceImages = 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);
const result = await externalAttachments(client, evidence, signal, { sourceVideos, sourceImages });
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: ['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', '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, onEcho, onAction } = {}) {
async consumeInbound(botId, { expectedFingerprint, onEvent, signal, sourceFiles = false, sourceImages = false, sourceVideos = 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, onEcho, onAction });
const dispose = this.#inboundConsumers.register(botId, { fingerprint: expectedFingerprint, onEvent, signal, sourceFiles, sourceImages, sourceVideos, 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 }) : 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) }) : named;
return this.#inboundConsumers.accept(config.id, enriched, signal);
}),
}));
Expand Down
39 changes: 39 additions & 0 deletions test/channels/feishu/external-files.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,45 @@ function fixture() {
}
const context = () => ({ signal: new AbortController().signal, assertCurrent() {} });

test('native media metadata and its cover require explicit opt-in and remain bound to the exact parent', async () => {
const fx = fixture();
let media = { ...parent, msg_type: 'media', body: { content: JSON.stringify({ file_key: 'native-video', file_name: 'clip.mp4', duration: 4000, image_key: 'native-cover' }) } };
fx.client.im.v1.message.get = async ({ path }) => ({ data: { items: [path.message_id === 'mention' ? source : media] } });
const input = { mentionedAccount: true, reply: route };
assert.equal((await externalAttachments(fx.client, input, context().signal)).attachments, undefined);
assert.equal((await externalAttachments(fx.client, { ...input, mentionedAccount: false }, context().signal, { sourceVideos: true })).attachments, undefined);
const evidence = await externalAttachments(fx.client, input, context().signal, { sourceVideos: true, sourceImages: true });
assert.equal(fx.downloads, 0);
assert.equal(evidence.video.durationMs, 4000);
assert.deepEqual(evidence.attachments.map(item => item.mediaType), ['video/unknown', 'image/unknown']);
assert.equal(evidence.video.itemId, evidence.attachments[0].id);
assert.equal(evidence.video.posterAttachmentId, evidence.attachments[1].id);
for (const [index, type] of [[0, 'file'], [1, 'image']]) {
const stream = await readExternalFile(fx.client, route, evidence.attachments[index], context());
for await (const chunk of stream) void chunk;
assert.deepEqual(fx.calls[index], { path: { message_id: 'file', file_key: index ? 'native-cover' : 'native-video' }, params: { type } });
}
media = { ...media, body: { content: JSON.stringify({ file_key: 'changed-video', file_name: 'clip.mp4' }) } };
await assert.rejects(readExternalFile(fx.client, route, evidence.attachments[0], context()), { code: 'stale-route' });
assert.equal(fx.downloads, 2);
media = { ...media, chat_id: 'other-chat' };
await assert.rejects(externalAttachments(fx.client, input, context().signal, { sourceVideos: 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' }) } };
fx.client.im.v1.message.get = async () => ({ data: { items: [media] } });
const input = { mentionedAccount: true, reply: route };
const evidence = await externalAttachments(fx.client, input, context().signal, { sourceVideos: true });
assert.deepEqual(evidence.video, { itemId: evidence.attachments[0].id });
assert.equal(evidence.attachments.length, 1);
media = { ...media, msg_type: 'file' };
const generic = await externalAttachments(fx.client, input, context().signal, { sourceVideos: true });
assert.equal(generic.video, undefined);
assert.equal(generic.attachments[0].mediaType, undefined);
});

test('trusted parent metadata is lazy and mismatched evidence cannot fetch another resource', async () => {
const fx = fixture();
const evidence = await externalAttachments(fx.client, { mentionedAccount: true, reply: route }, context().signal);
Expand Down
27 changes: 27 additions & 0 deletions test/channels/feishu/multi-bot-controller.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,33 @@ 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 () => {
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'));
await fx.controller.consumeInbound(existing.id, { expectedFingerprint: info.account.fingerprint,
sourceFiles: true, sourceImages: true, 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; };
await runtime.acceptExternal({ event_id: 'event', app_id: existing.appId,
sender: { sender_type: 'user', sender_id: { open_id: 'human' } },
message: { message_id: 'mention', chat_id: 'chat', chat_type: 'group', message_type: 'text', parent_id: 'video',
mentions: [{ id: { open_id: existing.botOpenId }, key: '@_user_1' }],
create_time: '1790787600000', content: JSON.stringify({ text: '@_user_1 inspect video' }) } });
assert.equal(options.sourceVideos, sourceVideos);
assert.equal(options.sourceImages, true);
} finally {
await fx.controller.close();
}
});
}

test('checked live names reach a text-only consumer before durable acceptance', async () => {
const existing = bot('bot_named', 'named');
const fx = fixture({ bots: [existing], secrets: { [existing.secretRef]: 'local-secret' },
Expand Down
Loading