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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ This file records the notable changes in each dsh-im release. Its format follows

### Fixed / 修复

- 飞书受校验的 Bot 回复(回复原消息与发到群聊)使用 Markdown 时,改用与聊天相同的原生 Markdown 卡片发送,加粗、标题、列表、表格不再以星号、井号、竖线原样显示;纯文本和带 `<at user_id>` 提及的回复仍按文本发送,保持其他 Bot 的提及接收与自身 Echo 确认不变([BotHarness #2792](https://github.com/BotHarness/DeepSeekBot/issues/2792))。
Checked Lark Bot replies, both replies to the source message and posts to a group, are sent as the same native Markdown card as chat when they use Markdown, so bold, headings, lists and tables no longer show as raw asterisks, hashes and pipes. Plain replies and replies carrying a `<at user_id>` mention stay text, so other Bots still receive the mention and own Echo confirmation is unchanged ([BotHarness #2792](https://github.com/BotHarness/DeepSeekBot/issues/2792)).

- 兼容 DSH 0.2.1 Typert Gateway:dsh-im 在每次调用时声明本机 owner Peer,修复只能收消息、无法创建会话并报 `INTERNAL_UNKNOWN` 的问题;Gateway 协议错误现在归类为 Harness 接口不兼容([BotHarness #2896](https://github.com/BotHarness/DeepSeekBot/issues/2896))。
Supports the DSH 0.2.1 Typert Gateway: dsh-im names the local owner Peer on every call, fixing bots that received messages but could not create a session (`INTERNAL_UNKNOWN`). Gateway contract failures now classify as a Harness interface mismatch ([BotHarness #2896](https://github.com/BotHarness/DeepSeekBot/issues/2896)).

Expand Down
28 changes: 21 additions & 7 deletions src/channels/feishu/feishu-runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ const CALLBACK_PROBE_SEND_FAILURE_NOTICE = '⚠️ 修复验证失败:无法
const CALLBACK_PROBE_ABORT_NOTICE = '⚠️ 修复验证中断:Runtime 已停止,未完成 card.action.trigger 实测,不能确认修复成功。请不要重复授权;先等待机器人恢复连接。';
const REUSABLE_WS_STATES = new Set(['connected', 'connecting', 'reconnecting']);

// Checked Bot replies arrive as Markdown. Lark shows it only inside a card, so
// a reply that uses Markdown goes out as the same native Markdown element as
// chat. Plain replies and replies carrying a text-message <at user_id> stay
// text: cards do not render that mention syntax, and other Bots and own echoes
// are read from text messages only.
const MARKDOWN_SYNTAX = /(^|\n)[ \t]{0,3}(#{1,6}[ \t]|[-*+][ \t]|\d{1,9}[.)][ \t]|>|```|\|.*\|)|\*\*\S|__\S|~~\S|`[^`\n]+`|\[[^\]\n]+\]\([^)\s]+\)/;

export function checkedTextFormat(text) {
return !text.includes('<at ') && MARKDOWN_SYNTAX.test(text) ? 'markdown' : 'plain';
}

function textMessage(text, format) {
return format === 'markdown'
? { msg_type: 'interactive', content: JSON.stringify({ schema: '2.0', body: { elements: [{ tag: 'markdown', content: text }] } }) }
: { msg_type: 'text', content: JSON.stringify({ text }) };
}

function nonEmptyString(value) {
return typeof value === 'string' && value.trim() ? value.trim() : null;
}
Expand Down Expand Up @@ -774,7 +791,7 @@ export class FeishuRuntime {
if (!allowed)
throw Object.assign(new Error('send-permission-denied'), { code: 'send-permission-denied' });
return this.sendProactiveText({kind: 'group', route: {chatId: conversationId}}, text,
{...options, receipt: true});
{...options, format: checkedTextFormat(text), receipt: true});
}

async sendProactiveText(target, text, { signal, format = 'plain', receipt = false, beforeSend } = {}) {
Expand Down Expand Up @@ -805,9 +822,7 @@ export class FeishuRuntime {
signal?.throwIfAborted();
// Use the same native Markdown element as chat, without opening a stream
// or retrying as plain text after a possibly accepted delivery.
const content = format === 'markdown'
? { schema: '2.0', body: { elements: [{ tag: 'markdown', content: text }] } }
: { text };
const message = textMessage(text, format);
const client = this.#client;
const allowed = beforeSend === undefined || (typeof beforeSend === 'function' && beforeSend() === true);
signal?.throwIfAborted();
Expand All @@ -819,8 +834,7 @@ export class FeishuRuntime {
params: { receive_id_type: receiveIdType },
data: {
receive_id: receiveId,
msg_type: format === 'markdown' ? 'interactive' : 'text',
content: JSON.stringify(content),
...message,
},
});
if (response?.code && response.code !== 0) {
Expand Down Expand Up @@ -929,7 +943,7 @@ export class FeishuRuntime {
signal?.throwIfAborted();
const response = await client.im.v1.message.reply({
path: { message_id: route.messageId },
data: { msg_type: 'text', content: JSON.stringify({ text }), reply_in_thread: Boolean(route.threadId) },
data: { ...textMessage(text, checkedTextFormat(text)), reply_in_thread: Boolean(route.threadId) },
});
const messageId = response?.data?.message_id;
if (response?.code || typeof messageId !== 'string' || !messageId)
Expand Down
29 changes: 28 additions & 1 deletion test/channels/feishu/feishu-runtime.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { FeishuRuntime } from '../../../src/channels/feishu/feishu-runtime.mjs';
import { FeishuRuntime, checkedTextFormat } from '../../../src/channels/feishu/feishu-runtime.mjs';
import { rememberConnectionTestTarget } from '../../../src/channels/shared/connection-test.mjs';

class FakeClient {
Expand Down Expand Up @@ -942,6 +942,33 @@ test('checked replies validate the original source before sending and retain unk
});


test('checked Bot replies send Markdown as a native Markdown card and keep plain or mentioning text as text', async () => {
for (const text of ['**Done**', '# Report', '- one\n- two', '1. first', '| a | b |\n| - | - |', 'see `pnpm test`', '[PR](https://example.com)', '> quoted', 'intro\n```js\nx\n```'])
assert.equal(checkedTextFormat(text), 'markdown', text);
for (const text of ['hello', 'LARK-SETUP-OK', '2 * 3 = 6', 'a-b', 'version 1.6.0', '<at user_id="ou_b">B</at> **please review**'])
assert.equal(checkedTextFormat(text), 'plain', text);

const runtime = new FeishuRuntime({ lark: fakeLark(), appId: 'app', appSecret: 'secret', ownerOpenIds: ['*'],
consumerMode: 'external-consumer', harness: { async ensureRunning() {} }, state: {} });
const starting = runtime.start();
await waitFor(() => FakeWSClient.instances.length === 1);
FakeWSClient.instances[0].becomeReady(); await starting;
const client = FakeClient.instances[0];
client.im.v1.message.get = async () => ({ code: 0, data: { items: [{ message_id: 'message', chat_id: 'chat',
sender: { sender_type: 'user', id_type: 'open_id', id: 'human' } }] } });
const replies = [];
client.im.v1.message.reply = async request => { replies.push(request.data); return { code: 0, data: { message_id: 'reply' } }; };
const route = { messageId: 'message', conversationId: 'chat', actorId: 'human' };
await runtime.replyChecked(route, '## Result\n\n**Bold** and a table:\n\n| a | b |\n| - | - |');
await runtime.replyChecked(route, 'hello');
assert.equal(replies[0].msg_type, 'interactive');
assert.deepEqual(JSON.parse(replies[0].content), { schema: '2.0', body: { elements: [{ tag: 'markdown',
content: '## Result\n\n**Bold** and a table:\n\n| a | b |\n| - | - |' }] } });
assert.deepEqual(replies[1], { msg_type: 'text', content: JSON.stringify({ text: 'hello' }), reply_in_thread: false });
await runtime.stop();
});


test('own-account qualification maps the sender without borrowing an ingress app identity and yields a checked reply receipt', async () => {
const runtime = new FeishuRuntime({ lark: fakeLark(), appId: 'responder', appSecret: 'secret', ownerOpenIds: ['*'],
consumerMode: 'external-consumer', harness: { async ensureRunning() {} }, state: {} });
Expand Down
4 changes: 4 additions & 0 deletions test/reachable-feishu.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ test('public Lark posting checks the verified Bot in paginated native moderator
sent: true, receipt: { version: 1, messageId: 'om_restricted', conversationId: 'oc_restricted' },
});
assert.equal(sends.length, 1);
assert.equal(sends[0].data.msg_type, 'text');
await service.postConversationChecked('bot_one', 'oc_restricted', '**Weekly report**\n\n- shipped', options);
assert.equal(sends[1].data.msg_type, 'interactive');
assert.equal(JSON.parse(sends[1].data.content).body.elements[0].content, '**Weekly report**\n\n- shipped');
});
Loading