feat: add native result-file delivery

This commit is contained in:
xmanrui 2026-08-23 20:02:06 +08:00
parent 2803bbcbab
commit d472dd5a75
65 changed files with 10205 additions and 255 deletions

View file

@ -2,11 +2,17 @@ import test from 'node:test';
import assert from 'node:assert/strict';
import { Readable } from 'node:stream';
import { mkdirSync, realpathSync } from 'node:fs';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { FeishuHarnessBridge } from '../../../src/channels/feishu/bridge.mjs';
import { DEFAULT_IMAGE_PROMPT } from '../../../src/channels/shared/image-prompt.mjs';
import { connectionTestTarget } from '../../../src/channels/shared/connection-test.mjs';
import {
OUTBOUND_ARTIFACT_TOOL,
OutboundArtifactRegistry,
createOutboundArtifactTool,
} from '../../../src/channels/shared/semantic/artifact.mjs';
const PNG_1X1 = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=',
@ -90,6 +96,41 @@ function textClient(sendText) {
};
}
async function committedArtifact(t, fileName, content, suffix = '') {
const workspace = await mkdtemp(join(tmpdir(), `dsh-im-feishu-artifact-${suffix}`));
t.after(() => rm(workspace, { recursive: true, force: true }));
let nextId = 0;
const registry = new OutboundArtifactRegistry({
uuid: () => `${suffix || 'file'}-${++nextId}`,
});
t.after(() => registry.clear());
const agent = {
session: {
header: { id: `session-${suffix || 'file'}`, cwd: workspace },
events: [
{ type: 'turn/start', data: { turn: 1 } },
{
type: 'user/message',
data: { turn: 1, source: { rpcId: `rpc-${suffix || 'file'}` } },
},
],
},
};
const tool = createOutboundArtifactTool({ registry });
const exec = {
name: OUTBOUND_ARTIFACT_TOOL,
callId: `call-${suffix || 'file'}`,
rootCallId: `call-${suffix || 'file'}`,
token: Symbol(`call-${suffix || 'file'}`),
agent,
};
await writeFile(join(workspace, fileName), content);
await tool.definition.execute({ path: fileName }, exec);
tool.onResult(exec, { isError: false });
const [artifact] = registry.take(agent.session.header.id, 1);
return artifact;
}
test('Feishu remembers any authorized private inbound message as a connection-test target', async () => {
const groupFixture = stateFixture();
const groupBridge = new FeishuHarnessBridge({
@ -1879,6 +1920,380 @@ test('reaction failures do not block streaming replies', async () => {
assert.equal(status.streamResponses, 1);
});
test('Feishu finalizes the answer card before delivering registered result files and reports partial failure', async (t) => {
const html = await committedArtifact(t, 'result.html', '<h1>result</h1>', 'html');
const generic = await committedArtifact(t, 'notes.txt', 'notes', 'notes');
const fixture = stateFixture([['p2p:ou_user', 'session-artifacts']]);
const order = [];
const delivered = [];
const notices = [];
const status = bridgeStatus();
const abort = new AbortController();
const bridge = new FeishuHarnessBridge({
client: textClient(async ({ text }) => notices.push(text)),
channel: {
addReaction: async () => 'reaction',
removeReaction: async () => undefined,
stream: async (_chatId, input) => {
await input.markdown({ setContent: async () => undefined });
order.push('card-finalized');
return { messageId: 'om-card' };
},
sendFile: async (chatId, file, options) => {
order.push(`file:${file.fileName}`);
delivered.push({ chatId, file, options });
if (file.fileName === 'notes.txt') {
const error = new Error('provider detail must stay private');
error.code = 'artifact-rate-limited';
throw error;
}
return {
schemaVersion: 1,
deliveryId: file.deliveryKey,
presentation: 'feishu-file',
providerMessageIds: ['om-file'],
artifacts: [{ artifactId: file.artifactId, outcome: 'sent' }],
};
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
await options.onArtifact(html);
await options.onArtifact(generic);
return '两个结果文件已经生成。';
},
},
state: fixture.state,
status,
allowedSenderOpenIds: new Set(['ou_user']),
signal: abort.signal,
});
bridge.accept(event('om_artifacts', '生成 HTML 和说明文件并发给我'));
await bridge.waitForIdle();
assert.deepEqual(order, ['card-finalized', 'file:result.html', 'file:notes.txt']);
assert.equal(delivered[0].chatId, 'oc_chat');
assert.deepEqual(delivered[0].options, {
replyTo: 'om_artifacts',
signal: abort.signal,
});
assert.equal(delivered[0].file.bytes.toString(), '<h1>result</h1>');
assert.equal(delivered[1].file.bytes.toString(), 'notes');
assert.equal(status.artifactsSent, 1);
assert.equal(status.artifactSendErrors, 1);
assert.equal(notices.length, 1);
assert.match(notices[0], /notes\.txt.*限流/);
assert.doesNotMatch(notices[0], /provider detail/);
});
test('Feishu tells users to check the chat before retrying an uncertain file delivery', async (t) => {
const artifact = await committedArtifact(t, 'uncertain.txt', 'uncertain result', 'uncertain');
const fixture = stateFixture([['p2p:ou_user', 'session-uncertain-artifact']]);
const notices = [];
const bridge = new FeishuHarnessBridge({
client: textClient(async ({ text }) => notices.push(text)),
channel: {
stream: async (_chatId, input) => {
await input.markdown({ setContent: async () => undefined });
return { messageId: 'om-uncertain-card' };
},
sendFile: async () => {
const error = new Error('private transport detail');
error.code = 'artifact-delivery-uncertain';
throw error;
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
await options.onArtifact(artifact);
return '结果已生成';
},
},
state: fixture.state,
status: bridgeStatus(),
allowedSenderOpenIds: new Set(['ou_user']),
});
bridge.accept(event('om_uncertain_artifact', '生成并发送文件'));
await bridge.waitForIdle();
assert.deepEqual(notices, [
'结果文件「uncertain.txt」发送结果未能确认,请先检查聊天内是否已收到,不要立即重试。',
]);
});
test('Feishu delivers a file-only Turn with a neutral final card', async (t) => {
const artifact = await committedArtifact(t, 'file-only.txt', 'file only', 'file-only');
const fixture = stateFixture([['p2p:ou_user', 'session-file-only']]);
const cardContents = [];
const files = [];
const bridge = new FeishuHarnessBridge({
client: textClient(async () => undefined),
channel: {
stream: async (_chatId, input) => {
await input.markdown({ setContent: async (content) => cardContents.push(content) });
return { messageId: 'om-file-only-card' };
},
sendFile: async (_chatId, file) => {
files.push(file.fileName);
return {
schemaVersion: 1,
deliveryId: file.deliveryKey,
presentation: 'feishu-file',
providerMessageIds: ['om-file-only'],
artifacts: [{ artifactId: file.artifactId, outcome: 'sent' }],
};
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
await options.onArtifact(artifact);
return '';
},
},
state: fixture.state,
status: bridgeStatus(),
allowedSenderOpenIds: new Set(['ou_user']),
});
bridge.accept(event('om_file_only', '只发送结果文件'));
await bridge.waitForIdle();
assert.deepEqual(cardContents, ['结果文件已生成。']);
assert.deepEqual(files, ['file-only.txt']);
});
test('a CardKit finalization failure falls back to text and delivers each artifact once without a second prompt', async (t) => {
const artifact = await committedArtifact(t, 'fallback.txt', 'fallback result', 'fallback');
const fixture = stateFixture([['p2p:ou_user', 'session-fallback-artifact']]);
const sent = [];
const files = [];
let asks = 0;
const bridge = new FeishuHarnessBridge({
client: textClient(async ({ text }) => sent.push(text)),
channel: {
stream: async (_chatId, input) => {
await input.markdown({ setContent: async () => undefined });
throw new Error('card finalization failed');
},
sendFile: async (_chatId, file) => {
files.push(file.fileName);
return {
schemaVersion: 1,
deliveryId: file.deliveryKey,
presentation: 'feishu-file',
providerMessageIds: ['om-fallback-file'],
artifacts: [{ artifactId: file.artifactId, outcome: 'sent' }],
};
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
asks += 1;
await options.onArtifact(artifact);
return '回答已生成';
},
},
state: fixture.state,
status: bridgeStatus(),
allowedSenderOpenIds: new Set(['ou_user']),
});
bridge.accept(event('om_artifact_card_fallback', '生成并发送文件'));
await bridge.waitForIdle();
assert.equal(asks, 1);
assert.deepEqual(sent, ['回答已生成']);
assert.deepEqual(files, ['fallback.txt']);
});
test('Feishu still delivers a file-only result when CardKit and fallback text both fail', async (t) => {
const artifact = await committedArtifact(t, 'survives-text-failure.txt', 'file bytes', 'text-failure');
const fixture = stateFixture([['p2p:ou_user', 'session-text-failure']]);
const files = [];
let fallbackTextAttempts = 0;
const bridge = new FeishuHarnessBridge({
client: textClient(async () => {
fallbackTextAttempts += 1;
throw new Error('text transport unavailable');
}),
channel: {
stream: async (_chatId, input) => {
await input.markdown({ setContent: async () => undefined });
throw new Error('card finalization unavailable');
},
sendFile: async (_chatId, file) => {
files.push(file.fileName);
return {
schemaVersion: 1,
deliveryId: file.deliveryKey,
presentation: 'feishu-file',
providerMessageIds: ['om-file-after-text-failure'],
artifacts: [{ artifactId: file.artifactId, outcome: 'sent' }],
};
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
await options.onArtifact(artifact);
return '';
},
},
state: fixture.state,
status: bridgeStatus(),
allowedSenderOpenIds: new Set(['ou_user']),
logger: { info() {}, warn() {}, error() {} },
});
bridge.accept(event('om_file_after_text_failure', '只生成文件'));
await bridge.waitForIdle();
assert.deepEqual(files, ['survives-text-failure.txt']);
assert.equal(fallbackTextAttempts, 1, 'must not append a generic retry after file success');
});
test('Feishu returns the receipt after reaction finalization and one safe notice when text and file delivery fail', async (t) => {
for (const errorCode of ['artifact-invalid', 'artifact-unavailable']) {
await t.test(errorCode, async (subtest) => {
const artifact = await committedArtifact(subtest, `${errorCode}.txt`, 'file bytes', errorCode);
const attemptedTexts = [];
const visibleTexts = [];
const reactions = [];
const bridge = new FeishuHarnessBridge({
client: {
im: { v1: { message: { create: async (request) => {
const text = JSON.parse(request.data.content).text;
attemptedTexts.push(text);
if (text === '文字结果') throw new Error('text transport unavailable');
visibleTexts.push(text);
return { code: 0, data: {} };
} } } },
},
channel: {
addReaction: async (_messageId, emoji) => {
reactions.push(emoji);
return `reaction-${emoji}`;
},
removeReaction: async () => undefined,
sendFile: async () => {
const error = new Error('unsafe result file');
error.code = errorCode;
throw error;
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
await options.onArtifact(artifact);
return '文字结果';
},
},
state: stateFixture([['p2p:ou_user', `session-${errorCode}`]]).state,
status: bridgeStatus(),
allowedSenderOpenIds: new Set(['ou_user']),
logger: { info() {}, warn() {}, error() {} },
});
const receipt = await bridge.accept(event(`om-${errorCode}`, '生成并发送文件'));
assert.equal(attemptedTexts.length, 2, 'must not append a generic error after the safe notice');
assert.equal(visibleTexts.length, 1);
assert.match(visibleTexts[0], /暂时无法读取或准备发送.*仍可访问/);
assert.deepEqual(reactions, ['OnIt', 'DONE']);
assert.deepEqual(receipt, {
schemaVersion: 1,
deliveryId: artifact.deliveryKey,
presentation: 'text-fallback',
providerMessageIds: [],
artifacts: [{
artifactId: artifact.artifactId,
outcome: 'rejected',
reason: errorCode,
}],
});
});
}
});
test('Feishu keeps the generic error when no answer or file failure notice is visible', async (t) => {
const artifact = await committedArtifact(t, 'unavailable.txt', 'file bytes', 'no-visible-failure');
const attemptedTexts = [];
const bridge = new FeishuHarnessBridge({
client: textClient(async ({ text }) => {
attemptedTexts.push(text);
if (attemptedTexts.length < 3) throw new Error('text transport unavailable');
}),
channel: {
sendFile: async () => {
const error = new Error('file transport unavailable');
error.code = 'artifact-provider-failed';
throw error;
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
await options.onArtifact(artifact);
return '文字结果';
},
},
state: stateFixture([['p2p:ou_user', 'session-no-visible-failure']]).state,
status: bridgeStatus(),
allowedSenderOpenIds: new Set(['ou_user']),
logger: { info() {}, warn() {}, error() {} },
});
await bridge.accept(event('om-no-visible-failure', '生成并发送文件'));
assert.equal(attemptedTexts.length, 3);
assert.match(attemptedTexts.at(-1), /^处理失败,请稍后重试/);
});
test('Feishu does not repeat finalized card text when cancellation happens before file delivery', async (t) => {
const artifact = await committedArtifact(t, 'cancel-after-card.txt', 'file bytes', 'cancel-after-card');
const fixture = stateFixture([['p2p:ou_user', 'session-cancel-after-card']]);
const controller = new AbortController();
const fallbackTexts = [];
let files = 0;
const bridge = new FeishuHarnessBridge({
client: textClient(async ({ text }) => fallbackTexts.push(text)),
channel: {
stream: async (_chatId, input) => {
await input.markdown({ setContent: async () => undefined });
controller.abort(new DOMException('runtime stopped', 'AbortError'));
return { messageId: 'om-final-card' };
},
sendFile: async () => {
files += 1;
},
},
harness: {
sessionExists: async () => true,
ask: async (_sessionId, _text, options) => {
await options.onArtifact(artifact);
return '卡片已经完成';
},
},
state: fixture.state,
status: bridgeStatus(),
allowedSenderOpenIds: new Set(['ou_user']),
signal: controller.signal,
logger: { info() {}, warn() {}, error() {} },
});
bridge.accept(event('om_cancel_after_card', '生成文件'));
await bridge.waitForIdle();
assert.equal(files, 0);
assert.deepEqual(fallbackTexts, []);
});
test('a stream finalization failure falls back to text without repeating the prompt', async () => {
const seen = new Set();
const sent = [];

View file

@ -10,6 +10,7 @@ function fakeClient(overrides = {}) {
recalls: [],
reactionsAdded: [],
reactionsRemoved: [],
fileUploads: [],
};
const client = {
cardkit: { v1: {
@ -28,6 +29,12 @@ function fakeClient(overrides = {}) {
},
} },
im: { v1: {
file: {
create: async (request) => {
calls.fileUploads.push(request);
return { file_key: 'file-key-test' };
},
},
message: {
reply: async (request) => {
calls.replies.push(request);
@ -54,6 +61,9 @@ function fakeClient(overrides = {}) {
if (overrides.updateContent) client.cardkit.v1.cardElement.content = overrides.updateContent;
if (overrides.finishCard) client.cardkit.v1.card.settings = overrides.finishCard;
if (overrides.uploadFile) client.im.v1.file.create = overrides.uploadFile;
if (overrides.replyMessage) client.im.v1.message.reply = overrides.replyMessage;
if (overrides.createMessage) client.im.v1.message.create = overrides.createMessage;
return { client, calls };
}
@ -129,3 +139,393 @@ test('VerifiedFeishuChannel checks reaction API results', async () => {
assert.equal(calls.reactionsAdded[0].data.reaction_type.emoji_type, 'OnIt');
assert.equal(calls.reactionsRemoved[0].path.reaction_id, 'reaction-test');
});
test('VerifiedFeishuChannel uploads a materialized result and replies with a native file message', async () => {
const { client, calls } = fakeClient();
const channel = new VerifiedFeishuChannel({ client });
const file = {
artifactId: 'artifact-html',
deliveryKey: 'delivery-html',
fileName: 'result.html',
mediaType: 'text/html',
size: 19,
bytes: Buffer.from('<h1>result</h1>'),
};
const receipt = await channel.sendFile('oc_chat', file, { replyTo: 'om_user' });
assert.equal(calls.fileUploads.length, 1);
assert.equal(calls.fileUploads[0].data.file_type, 'stream');
assert.equal(calls.fileUploads[0].data.file_name, 'result.html');
assert.equal(calls.fileUploads[0].data.file, file.bytes);
assert.equal(calls.replies.length, 1);
assert.equal(calls.replies[0].path.message_id, 'om_user');
assert.equal(calls.replies[0].data.msg_type, 'file');
assert.deepEqual(JSON.parse(calls.replies[0].data.content), { file_key: 'file-key-test' });
assert.match(calls.replies[0].data.uuid, /^dshim_[a-f0-9]{40}$/);
assert.deepEqual(receipt, {
schemaVersion: 1,
deliveryId: 'delivery-html',
presentation: 'feishu-file',
providerMessageIds: ['om-stream'],
artifacts: [{ artifactId: 'artifact-html', outcome: 'sent' }],
});
});
test('VerifiedFeishuChannel sends to the current chat when no reply target exists', async () => {
const { client, calls } = fakeClient({
createMessage: async (request) => {
calls.replies.push(request);
return { code: 0, data: { message_id: 'om-created-file' } };
},
});
const channel = new VerifiedFeishuChannel({ client });
await channel.sendFile('oc_chat', {
artifactId: 'artifact-generic',
deliveryKey: 'delivery-generic',
fileName: 'result.bin',
bytes: Buffer.from('generic'),
});
assert.deepEqual(calls.replies[0].params, { receive_id_type: 'chat_id' });
assert.equal(calls.replies[0].data.receive_id, 'oc_chat');
assert.equal(calls.replies[0].data.msg_type, 'file');
});
test('VerifiedFeishuChannel retries uncertain message delivery without uploading twice', async () => {
const requests = [];
let attempts = 0;
const { client, calls } = fakeClient({
replyMessage: async (request) => {
requests.push(structuredClone(request));
attempts += 1;
return attempts === 1
? { code: 230049, msg: 'still sending' }
: { code: 0, data: { message_id: 'om-retried-file' } };
},
});
const channel = new VerifiedFeishuChannel({ client });
const receipt = await channel.sendFile('oc_chat', {
artifactId: 'artifact-retry',
deliveryKey: 'delivery-retry',
fileName: 'retry.txt',
bytes: Buffer.from('retry'),
}, { replyTo: 'om_user' });
assert.equal(calls.fileUploads.length, 1);
assert.equal(requests.length, 2);
assert.deepEqual(requests[1], requests[0]);
assert.deepEqual(receipt.providerMessageIds, ['om-retried-file']);
});
test('VerifiedFeishuChannel stops after one 230049 retry and never uploads the file twice', async () => {
const requests = [];
const { client, calls } = fakeClient({
replyMessage: async (request) => {
requests.push(structuredClone(request));
return { code: 230049, msg: 'still sending' };
},
});
const channel = new VerifiedFeishuChannel({ client });
await assert.rejects(channel.sendFile('oc_chat', {
artifactId: 'artifact-still-uncertain',
deliveryKey: 'delivery-still-uncertain',
fileName: 'uncertain.txt',
bytes: Buffer.from('uncertain'),
}, { replyTo: 'om_user' }), (error) => error.code === 'artifact-delivery-uncertain'
&& error.providerCode === 230049);
assert.equal(calls.fileUploads.length, 1);
assert.equal(requests.length, 2);
assert.deepEqual(requests[1], requests[0]);
});
test('VerifiedFeishuChannel retries an SDK-thrown 230049 with the same file key and UUID', async () => {
const requests = [];
const { client, calls } = fakeClient({
replyMessage: async (request) => {
requests.push(structuredClone(request));
if (requests.length === 1) {
const error = new Error('still sending');
error.code = 230049;
throw error;
}
return { code: 0, data: { message_id: 'om-after-thrown-230049' } };
},
});
const channel = new VerifiedFeishuChannel({ client });
const receipt = await channel.sendFile('oc_chat', {
artifactId: 'artifact-thrown-retry',
deliveryKey: 'delivery-thrown-retry',
fileName: 'thrown-retry.txt',
bytes: Buffer.from('retry'),
}, { replyTo: 'om_user' });
assert.equal(calls.fileUploads.length, 1);
assert.equal(requests.length, 2);
assert.deepEqual(requests[1], requests[0]);
assert.deepEqual(receipt.providerMessageIds, ['om-after-thrown-230049']);
});
test('VerifiedFeishuChannel bounds upload and message waits independently', async () => {
const uploadFixture = fakeClient({ uploadFile: async () => new Promise(() => {}) });
const uploadChannel = new VerifiedFeishuChannel({
client: uploadFixture.client,
fileUploadTimeoutMs: 10,
fileMessageTimeoutMs: 100,
});
await assert.rejects(uploadChannel.sendFile('oc_chat', {
artifactId: 'artifact-upload-timeout',
deliveryKey: 'delivery-upload-timeout',
fileName: 'upload-timeout.txt',
bytes: Buffer.from('timeout'),
}, { replyTo: 'om_user' }), (error) => error.code === 'artifact-provider-failed'
&& error.cause?.code === 'provider-timeout');
assert.equal(uploadFixture.calls.replies.length, 0);
let messageCalls = 0;
const messageFixture = fakeClient({
replyMessage: async () => {
messageCalls += 1;
return new Promise(() => {});
},
});
const messageChannel = new VerifiedFeishuChannel({
client: messageFixture.client,
fileUploadTimeoutMs: 100,
fileMessageTimeoutMs: 10,
});
await assert.rejects(messageChannel.sendFile('oc_chat', {
artifactId: 'artifact-message-timeout',
deliveryKey: 'delivery-message-timeout',
fileName: 'message-timeout.txt',
bytes: Buffer.from('timeout'),
}, { replyTo: 'om_user' }), (error) => error.code === 'artifact-delivery-uncertain'
&& error.cause?.code === 'provider-timeout');
assert.equal(messageFixture.calls.fileUploads.length, 1);
assert.equal(messageCalls, 1);
});
test('VerifiedFeishuChannel rejects waits longer than the 120 second operation timeout', () => {
const { client } = fakeClient();
assert.throws(
() => new VerifiedFeishuChannel({ client, fileUploadTimeoutMs: 120_001 }),
/fileUploadTimeoutMs.*120000/,
);
assert.throws(
() => new VerifiedFeishuChannel({ client, fileMessageTimeoutMs: 120_001 }),
/fileMessageTimeoutMs.*120000/,
);
});
test('VerifiedFeishuChannel stops after an in-flight upload when file delivery is cancelled', async () => {
let uploadStarted;
const started = new Promise((resolve) => { uploadStarted = resolve; });
const { client, calls } = fakeClient({
uploadFile: async () => {
uploadStarted();
return new Promise(() => {});
},
});
const channel = new VerifiedFeishuChannel({ client });
const abort = new AbortController();
const reason = new DOMException('runtime stopped', 'AbortError');
const sending = channel.sendFile('oc_chat', {
artifactId: 'artifact-cancelled-upload',
deliveryKey: 'delivery-cancelled-upload',
fileName: 'cancelled.txt',
bytes: Buffer.from('cancelled'),
}, { replyTo: 'om_user', signal: abort.signal });
await started;
abort.abort(reason);
await assert.rejects(sending, (error) => error === reason);
assert.equal(calls.replies.length, 0);
});
test('VerifiedFeishuChannel immediately preserves caller abort during an in-flight message send', async () => {
let messageStarted;
const started = new Promise((resolve) => { messageStarted = resolve; });
const { client, calls } = fakeClient({
replyMessage: async () => {
messageStarted();
return new Promise(() => {});
},
});
const channel = new VerifiedFeishuChannel({ client });
const abort = new AbortController();
const reason = new DOMException('runtime stopped', 'AbortError');
const sending = channel.sendFile('oc_chat', {
artifactId: 'artifact-cancelled-message',
deliveryKey: 'delivery-cancelled-message',
fileName: 'cancelled-message.txt',
bytes: Buffer.from('cancelled'),
}, { replyTo: 'om_user', signal: abort.signal });
await started;
abort.abort(reason);
await assert.rejects(sending, (error) => error === reason
&& error.code !== 'artifact-delivery-uncertain');
assert.equal(calls.fileUploads.length, 1);
});
test('VerifiedFeishuChannel does not retry an uncertain file message after cancellation', async () => {
const abort = new AbortController();
const reason = new DOMException('runtime stopped', 'AbortError');
const requests = [];
const { client } = fakeClient({
replyMessage: async (request) => {
requests.push(request);
abort.abort(reason);
return { code: 230049, msg: 'still sending' };
},
});
const channel = new VerifiedFeishuChannel({ client });
await assert.rejects(channel.sendFile('oc_chat', {
artifactId: 'artifact-cancelled-retry',
deliveryKey: 'delivery-cancelled-retry',
fileName: 'cancelled-retry.txt',
bytes: Buffer.from('cancelled retry'),
}, { replyTo: 'om_user', signal: abort.signal }), (error) => error === reason);
assert.equal(requests.length, 1);
});
test('VerifiedFeishuChannel lets Feishu decide empty and oversize file outcomes', async () => {
const uploads = [];
const providerCodes = [234010, 234006, 0];
const { client } = fakeClient({
uploadFile: async (request) => {
uploads.push(request);
const code = providerCodes.shift();
return code === 0 ? {} : { code };
},
});
const channel = new VerifiedFeishuChannel({ client });
const base = {
artifactId: 'artifact-invalid',
deliveryKey: 'delivery-invalid',
fileName: 'invalid.bin',
};
await assert.rejects(
channel.sendFile('oc_chat', { ...base, bytes: Buffer.alloc(0) }),
(error) => error.code === 'artifact-empty' && error.providerCode === 234010,
);
await assert.rejects(
channel.sendFile('oc_chat', { ...base, bytes: Buffer.from('provider decides') }),
(error) => error.code === 'artifact-too-large' && error.providerCode === 234006,
);
await assert.rejects(
channel.sendFile('oc_chat', { ...base, bytes: Buffer.from('valid bytes') }),
(error) => error.code === 'artifact-provider-failed' && !error.message.includes('undefined'),
);
assert.deepEqual(uploads.map((request) => request.data.file.byteLength), [0, 16, 11]);
});
test('VerifiedFeishuChannel distinguishes upload failure from uncertain final delivery', async () => {
const uploadFixture = fakeClient({
uploadFile: async () => { throw new Error('upload transport closed'); },
});
const uploadChannel = new VerifiedFeishuChannel({ client: uploadFixture.client });
await assert.rejects(uploadChannel.sendFile('oc_chat', {
artifactId: 'artifact-upload-transport',
deliveryKey: 'delivery-upload-transport',
fileName: 'upload.txt',
bytes: Buffer.from('upload'),
}, { replyTo: 'om_user' }), (error) => error.code === 'artifact-provider-failed');
assert.equal(uploadFixture.calls.replies.length, 0);
for (const replyMessage of [
async () => { throw new Error('message transport closed'); },
async () => ({ code: 0, data: {} }),
]) {
const messageFixture = fakeClient({ replyMessage });
const messageChannel = new VerifiedFeishuChannel({ client: messageFixture.client });
await assert.rejects(messageChannel.sendFile('oc_chat', {
artifactId: 'artifact-uncertain-message',
deliveryKey: 'delivery-uncertain-message',
fileName: 'message.txt',
bytes: Buffer.from('message'),
}, { replyTo: 'om_user' }), (error) => error.code === 'artifact-delivery-uncertain');
assert.equal(messageFixture.calls.fileUploads.length, 1);
}
});
test('VerifiedFeishuChannel keeps explicit final provider codes out of the uncertain bucket', async () => {
const { client, calls } = fakeClient({
replyMessage: async () => ({ code: 99991672, msg: 'missing scope' }),
});
const channel = new VerifiedFeishuChannel({ client });
await assert.rejects(channel.sendFile('oc_chat', {
artifactId: 'artifact-message-permission',
deliveryKey: 'delivery-message-permission',
fileName: 'permission.txt',
bytes: Buffer.from('permission'),
}, { replyTo: 'om_user' }), (error) => error.code === 'artifact-permission-required'
&& error.providerCode === 99991672);
assert.equal(calls.fileUploads.length, 1);
});
test('VerifiedFeishuChannel classifies missing file permission without exposing provider details', async () => {
const { client } = fakeClient({
uploadFile: async () => {
const error = new Error('raw tenant detail and private diagnostic');
error.code = 99991672;
throw error;
},
});
const channel = new VerifiedFeishuChannel({ client });
await assert.rejects(
channel.sendFile('oc_chat', {
artifactId: 'artifact-permission',
deliveryKey: 'delivery-permission',
fileName: 'result.html',
bytes: Buffer.from('result'),
}),
(error) => error.code === 'artifact-permission-required'
&& error.message.includes('im:resource permission')
&& !error.message.includes('im:resource:upload')
&& !error.message.includes('private diagnostic'),
);
});
test('VerifiedFeishuChannel recognizes the SDK array-shaped permission error', async () => {
const { client } = fakeClient({
uploadFile: async () => {
const transport = new Error('raw transport diagnostic');
transport.code = 'ERR_BAD_REQUEST';
throw [transport, {
code: 99991672,
msg: 'raw provider permission URL',
}];
},
});
const channel = new VerifiedFeishuChannel({ client });
await assert.rejects(
channel.sendFile('oc_chat', {
artifactId: 'artifact-array-permission',
deliveryKey: 'delivery-array-permission',
fileName: 'result.txt',
bytes: Buffer.from('result'),
}),
(error) => error.code === 'artifact-permission-required'
&& error.providerCode === 99991672
&& !error.message.includes('provider permission URL'),
);
});

View file

@ -1,9 +1,19 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import {
HarnessClient,
HarnessReplyTracker,
} from '../../../src/channels/feishu/harness-client.mjs';
import {
OUTBOUND_ARTIFACT_TOOL,
createOutboundArtifactTool,
materializeOutboundArtifact,
outboundArtifactRegistry,
releaseOutboundArtifact,
} from '../../../src/channels/shared/semantic/artifact.mjs';
function deferred() {
let resolve;
@ -582,6 +592,124 @@ test('HarnessClient reads the nested workspace.create response used by DSH rc.6'
assert.deepEqual(methods, ['workspace.list', 'workspace.create']);
});
test('HarnessClient asks do not control file-return tool availability', async () => {
const client = new HarnessClient({
baseUrl: 'http://127.0.0.1:3080',
workspace: '/tmp/dsh-feishu-workspace',
});
client.ensureRunning = async () => undefined;
let promptRpcId;
let prompted = false;
const agent = {
session: {
header: { id: 'session-artifact-availability', cwd: '/tmp/dsh-feishu-workspace' },
events: [],
},
};
client.rpc = async (method, _payload, _timeoutMs, options) => {
if (method === 'session.history' && !prompted) return { events: [] };
if (method === 'session.prompt') {
prompted = true;
promptRpcId = options.rpcId;
agent.session.events = [
{ type: 'turn/start', data: { turn: 1 } },
{ type: 'user/message', data: { turn: 1, source: { rpcId: promptRpcId } } },
];
return {};
}
return {
events: [
{ event: { type: 'turn/start', seq: 1, data: { turn: 1 } } },
{
event: {
type: 'user/message',
seq: 2,
data: { turn: 1, source: { rpcId: promptRpcId } },
},
},
{
event: {
type: 'assistant/message',
seq: 3,
data: { turn: 1, step: 1, message: { content: [{ type: 'text', text: 'done' }] } },
},
},
{ event: { type: 'turn/end', seq: 4, data: { turn: 1, reason: { kind: 'completed' } } } },
],
};
};
assert.equal(await client.ask('session-artifact-availability', 'create a file', {
onArtifact: async () => undefined,
}), 'done');
});
test('HarnessClient delivers an existing file-only Turn directly', async (t) => {
outboundArtifactRegistry.clear();
t.after(() => outboundArtifactRegistry.clear());
const workspace = await mkdtemp(join(tmpdir(), 'dsh-im-file-only-'));
t.after(() => rm(workspace, { recursive: true, force: true }));
const client = new HarnessClient({
baseUrl: 'http://127.0.0.1:3080',
workspace,
});
client.ensureRunning = async () => undefined;
const tool = createOutboundArtifactTool({ registry: outboundArtifactRegistry });
const delivered = [];
await writeFile(join(workspace, 'file-only.txt'), 'file only');
let promptRpcId;
let prompted = false;
const agent = {
session: {
header: { id: 'session-file-only', cwd: workspace },
events: [],
},
};
client.rpc = async (method, _payload, _timeoutMs, options) => {
if (method === 'session.history' && !prompted) return { events: [] };
if (method === 'session.prompt') {
prompted = true;
promptRpcId = options.rpcId;
agent.session.events = [
{ type: 'turn/start', data: { turn: 2 } },
{ type: 'user/message', data: { turn: 2, source: { rpcId: promptRpcId } } },
];
const exec = {
name: OUTBOUND_ARTIFACT_TOOL,
callId: 'file-only-call',
token: Symbol('file-only-call'),
agent,
signal: new AbortController().signal,
};
await tool.definition.execute({ path: 'file-only.txt' }, exec);
tool.onResult(exec, { isError: false });
return {};
}
return {
events: [
{ event: { type: 'turn/start', seq: 1, data: { turn: 2 } } },
{
event: {
type: 'user/message',
seq: 2,
data: { turn: 2, source: { rpcId: promptRpcId } },
},
},
{ event: { type: 'turn/end', seq: 3, data: { turn: 2, reason: { kind: 'completed' } } } },
],
};
};
const answer = await client.ask('session-file-only', 'create and return a file', {
onArtifact: async (artifact) => delivered.push(artifact),
});
assert.equal(answer, '');
assert.equal(delivered.length, 1);
const file = await materializeOutboundArtifact(delivered[0]);
assert.equal(file.bytes.toString(), 'file only');
releaseOutboundArtifact(delivered[0]);
});
test('HarnessReplyTracker correlates the prompt and emits only answer text', () => {
const tracker = new HarnessReplyTracker({ promptRpcId: 'prompt-1', afterSeq: 10 });

View file

@ -180,6 +180,8 @@ test('QR registration separates events from card callbacks', async () => {
const run = fx.registrationRuns.shift();
assert.deepEqual(run.options.addons.events.items.tenant, ['im.message.receive_v1']);
assert.deepEqual(run.options.addons.callbacks.items, ['card.action.trigger']);
assert.ok(run.options.addons.scopes.tenant.includes('im:resource'));
assert.equal(run.options.addons.scopes.tenant.includes('im:resource:upload'), false);
run.options.onQRCodeReady({ url: 'https://accounts.feishu.cn/callbacks', expireIn: 60 });
run.resolve({
client_id: 'cli_callbacks', client_secret: 'callbacks-secret',

View file

@ -84,6 +84,8 @@ test('QR success stores the secret off-config and becomes immediately chat-ready
assert.ok(fx.getSdkOptions().addons.scopes.tenant.includes('im:message.p2p_msg:readonly'));
assert.ok(fx.getSdkOptions().addons.scopes.tenant.includes('im:message:readonly'));
assert.ok(fx.getSdkOptions().addons.scopes.tenant.includes('im:message:send_as_bot'));
assert.ok(fx.getSdkOptions().addons.scopes.tenant.includes('im:resource'));
assert.equal(fx.getSdkOptions().addons.scopes.tenant.includes('im:resource:upload'), false);
assert.ok(fx.getSdkOptions().addons.scopes.tenant.includes('cardkit:card:write'));
fx.getSdkOptions().onQRCodeReady({ url: 'https://accounts.feishu.cn/qr', expireIn: 600 });
fixture.resolveRegistration({

View file

@ -1223,6 +1223,7 @@ test('production assembly needs only ctx credentials and the active DSH webServe
const alphaState = constructed.runtime.state;
assert.equal(constructed.runtime.botId, 'bot_alpha');
assert.equal(constructed.runtime.repair, repair);
assert.equal(Object.hasOwn(constructed.runtime, 'outboundArtifactsEnabled'), false);
await constructed.controller.createRuntime({
botId: 'bot_beta',
config: {
@ -1235,6 +1236,7 @@ test('production assembly needs only ctx credentials and the active DSH webServe
appSecret: 'beta-secret',
});
const betaState = constructed.runtime.state;
assert.equal(Object.hasOwn(constructed.runtime, 'outboundArtifactsEnabled'), false);
assert.notEqual(alphaState, betaState);
assert.ok(constructed.statePaths.some((path) => /bots\/bot_alpha\/state\.json$/.test(path)));
assert.ok(constructed.statePaths.some((path) => /bots\/bot_beta\/state\.json$/.test(path)));