mirror of
https://github.com/hansjone/dsh-im-ops.git
synced 2026-10-09 03:03:24 +08:00
feat(qq): render answers as markdown
Deliver final Harness answers to QQ as msg_type=2 markdown so bold, headings, code blocks, lists, and tables render natively in QQ. - add markdown-reply.mjs: chunk long answers at code-fence/table boundaries (4500-char limit) and send each chunk with a unique msg_seq to avoid passive-reply dedup (40054005) - fall back to plain text per chunk when the platform rejects markdown, so bots without markdown capability keep working - route the non-stream final answer path in qq-bridge through the markdown sender; streaming replies already use markdown content type, and command/notice texts stay plain
This commit is contained in:
parent
29bcb67a64
commit
81884173df
5 changed files with 493 additions and 124 deletions
|
|
@ -458,6 +458,78 @@ test('QQ private messages stream Harness snapshots and finalize once', async ()
|
|||
assert.equal(bridge.status.messagesReplied, 1);
|
||||
});
|
||||
|
||||
test('QQ delivers final group answers as markdown messages', async () => {
|
||||
const sentText = [];
|
||||
const markdownCalls = [];
|
||||
const bridge = new QqHarnessBridge({
|
||||
bot: {
|
||||
sendText: async (_target, text) => sentText.push(text),
|
||||
send: async (options) => {
|
||||
markdownCalls.push(options);
|
||||
return { id: `md-${markdownCalls.length}` };
|
||||
},
|
||||
},
|
||||
ownerUserOpenid: 'owner-openid',
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => '## 标题\n\n**加粗**与 `代码`',
|
||||
},
|
||||
state: {
|
||||
hasSeen: () => false,
|
||||
markSeen: async () => {},
|
||||
sessionFor: () => 'session-group',
|
||||
setSession: async () => {},
|
||||
clearSession: async () => {},
|
||||
},
|
||||
});
|
||||
|
||||
await bridge.accept(message({
|
||||
kind: 'group',
|
||||
rawEventType: 'GROUP_AT_MESSAGE_CREATE',
|
||||
groupOpenid: 'group-md',
|
||||
messageId: 'msg-group-md',
|
||||
content: '请回答',
|
||||
replyTarget: { scope: 'group', targetId: 'group-md', msgId: 'msg-group-md' },
|
||||
}));
|
||||
assert.equal(markdownCalls.length, 1);
|
||||
assert.equal(markdownCalls[0].msgType, 2);
|
||||
assert.equal(markdownCalls[0].markdown.content, '## 标题\n\n**加粗**与 `代码`');
|
||||
assert.equal(Number.isInteger(markdownCalls[0].extra.msg_seq), true);
|
||||
assert.deepEqual(sentText, []);
|
||||
assert.equal(bridge.status.messagesReplied, 1);
|
||||
});
|
||||
|
||||
test('QQ falls back to plain text when the platform rejects markdown', async () => {
|
||||
const sentText = [];
|
||||
const bridge = new QqHarnessBridge({
|
||||
bot: {
|
||||
sendText: async (_target, text) => sentText.push(text),
|
||||
send: async () => { throw new Error('markdown rejected'); },
|
||||
},
|
||||
ownerUserOpenid: 'owner-openid',
|
||||
harness: {
|
||||
sessionExists: async () => true,
|
||||
ask: async () => '**回答**内容',
|
||||
},
|
||||
state: {
|
||||
hasSeen: () => false,
|
||||
markSeen: async () => {},
|
||||
sessionFor: () => 'session-fallback',
|
||||
setSession: async () => {},
|
||||
clearSession: async () => {},
|
||||
},
|
||||
logger: { warn() {}, error() {} },
|
||||
});
|
||||
|
||||
await bridge.accept(message({
|
||||
messageId: 'msg-md-fallback',
|
||||
replyTarget: { scope: 'c2c', targetId: 'owner-openid', msgId: 'msg-md-fallback' },
|
||||
}));
|
||||
assert.deepEqual(sentText, ['**回答**内容']);
|
||||
assert.equal(bridge.status.messagesReplied, 1);
|
||||
assert.equal(bridge.status.lastError, null);
|
||||
});
|
||||
|
||||
test('QQ closes an opened progress stream and announces when the Harness turn is stopped', async () => {
|
||||
const fixture = stateFixture([['c2c:owner-openid', 'session-stopped']]);
|
||||
const frames = [];
|
||||
|
|
|
|||
156
test/channels/qq/markdown-reply.test.mjs
Normal file
156
test/channels/qq/markdown-reply.test.mjs
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import {
|
||||
chunkMarkdownText,
|
||||
sendMarkdownReply,
|
||||
} from '../../../src/channels/qq/markdown-reply.mjs';
|
||||
|
||||
const target = { scope: 'c2c', targetId: 'user-openid', msgId: 'msg-1' };
|
||||
|
||||
test('chunkMarkdownText keeps short text as a single chunk', () => {
|
||||
assert.deepEqual(chunkMarkdownText('**你好**,世界'), ['**你好**,世界']);
|
||||
});
|
||||
|
||||
test('chunkMarkdownText returns no chunks for empty text', () => {
|
||||
assert.deepEqual(chunkMarkdownText(''), []);
|
||||
assert.deepEqual(chunkMarkdownText(null), []);
|
||||
});
|
||||
|
||||
test('chunkMarkdownText splits long text within the limit', () => {
|
||||
const text = Array.from({ length: 200 }, (_, index) => `第${index}行内容`).join('\n');
|
||||
const chunks = chunkMarkdownText(text, 100);
|
||||
assert.ok(chunks.length > 1);
|
||||
for (const chunk of chunks) {
|
||||
assert.ok(chunk.length <= 100, `chunk exceeds the limit: ${chunk.length}`);
|
||||
}
|
||||
assert.equal(chunks.join('\n'), text);
|
||||
});
|
||||
|
||||
test('chunkMarkdownText does not break inside a code block that fits the limit', () => {
|
||||
const code = '```js\nconsole.log(1);\n```';
|
||||
const text = `${'A'.repeat(80)}\n\n${code}\n\n${'B'.repeat(80)}`;
|
||||
const chunks = chunkMarkdownText(text, 100);
|
||||
const codeChunk = chunks.find((chunk) => chunk.includes('```js'));
|
||||
assert.ok(codeChunk);
|
||||
assert.ok(codeChunk.startsWith(code));
|
||||
});
|
||||
|
||||
test('chunkMarkdownText still chunks an oversized code block within the limit', () => {
|
||||
const code = ['```js', ...Array.from({ length: 30 }, (_, i) => `console.log(${i});`), '```']
|
||||
.join('\n');
|
||||
const chunks = chunkMarkdownText(code, 100);
|
||||
assert.ok(chunks.length > 1);
|
||||
for (const chunk of chunks) {
|
||||
assert.ok(chunk.length <= 100, `chunk exceeds the limit: ${chunk.length}`);
|
||||
}
|
||||
assert.equal(chunks.join('\n'), code);
|
||||
});
|
||||
|
||||
test('chunkMarkdownText keeps a GFM table together', () => {
|
||||
const table = [
|
||||
'| 列一 | 列二 |',
|
||||
'| --- | --- |',
|
||||
'| a | b |',
|
||||
'| c | d |',
|
||||
].join('\n');
|
||||
const chunks = chunkMarkdownText(table, 200);
|
||||
assert.equal(chunks.length, 1);
|
||||
assert.equal(chunks[0], table);
|
||||
});
|
||||
|
||||
test('chunkMarkdownText hard-splits an oversized single line', () => {
|
||||
const line = 'x'.repeat(250);
|
||||
const chunks = chunkMarkdownText(line, 100);
|
||||
assert.deepEqual(chunks, ['x'.repeat(100), 'x'.repeat(100), 'x'.repeat(50)]);
|
||||
});
|
||||
|
||||
test('sendMarkdownReply sends markdown with unique msg_seq per chunk', async () => {
|
||||
const calls = [];
|
||||
const results = await sendMarkdownReply({
|
||||
send: async (options) => {
|
||||
calls.push(options);
|
||||
return { id: `id-${calls.length}` };
|
||||
},
|
||||
sendText: async () => {
|
||||
throw new Error('sendText must not be called when markdown succeeds');
|
||||
},
|
||||
}, target, '# 标题\n\n**加粗**内容');
|
||||
assert.equal(calls.length, 1);
|
||||
assert.equal(calls[0].target, target);
|
||||
assert.equal(calls[0].msgType, 2);
|
||||
assert.equal(calls[0].markdown.content, '# 标题\n\n**加粗**内容');
|
||||
assert.equal(Number.isInteger(calls[0].extra.msg_seq), true);
|
||||
assert.deepEqual(results, [{ id: 'id-1' }]);
|
||||
});
|
||||
|
||||
test('sendMarkdownReply assigns distinct msg_seq values across chunks', async () => {
|
||||
const seqs = [];
|
||||
const bot = {
|
||||
send: async ({ extra }) => {
|
||||
seqs.push(extra.msg_seq);
|
||||
return { id: 'x' };
|
||||
},
|
||||
sendText: async () => { throw new Error('unexpected'); },
|
||||
};
|
||||
const text = Array.from({ length: 1_500 }, (_, index) => `第${index}行`).join('\n');
|
||||
await sendMarkdownReply(bot, target, text);
|
||||
assert.ok(seqs.length > 1);
|
||||
assert.equal(new Set(seqs).size, seqs.length);
|
||||
});
|
||||
|
||||
test('sendMarkdownReply falls back to plain text per chunk on markdown rejection', async () => {
|
||||
const sentText = [];
|
||||
const warnings = [];
|
||||
const results = await sendMarkdownReply({
|
||||
send: async () => {
|
||||
throw new Error('markdown rejected: no permission');
|
||||
},
|
||||
sendText: async (_target, text) => {
|
||||
sentText.push(text);
|
||||
return { id: `text-${sentText.length}` };
|
||||
},
|
||||
}, target, '回答内容', { logger: { warn: (...args) => warnings.push(args) } });
|
||||
assert.deepEqual(sentText, ['回答内容']);
|
||||
assert.deepEqual(results, [{ id: 'text-1' }]);
|
||||
assert.equal(warnings.length, 1);
|
||||
});
|
||||
|
||||
test('sendMarkdownReply uses sendText directly when the bot lacks send()', async () => {
|
||||
const sentText = [];
|
||||
const results = await sendMarkdownReply({
|
||||
sendText: async (_target, text) => {
|
||||
sentText.push(text);
|
||||
return { id: 'plain-1' };
|
||||
},
|
||||
}, target, '没有 send 方法的机器人');
|
||||
assert.deepEqual(sentText, ['没有 send 方法的机器人']);
|
||||
assert.deepEqual(results, [{ id: 'plain-1' }]);
|
||||
});
|
||||
|
||||
test('sendMarkdownReply delivers long answers as multiple markdown chunks', async () => {
|
||||
const markdownChunks = [];
|
||||
const bot = {
|
||||
send: async ({ markdown }) => {
|
||||
markdownChunks.push(markdown.content);
|
||||
return { id: `id-${markdownChunks.length}` };
|
||||
},
|
||||
sendText: async () => { throw new Error('unexpected'); },
|
||||
};
|
||||
const text = Array.from({ length: 600 }, (_, index) => `- 列表项 ${index}`).join('\n');
|
||||
const results = await sendMarkdownReply(bot, target, text);
|
||||
assert.ok(markdownChunks.length > 1);
|
||||
for (const chunk of markdownChunks) {
|
||||
assert.ok(chunk.length <= 4_500);
|
||||
}
|
||||
assert.equal(results.length, markdownChunks.length);
|
||||
assert.equal(markdownChunks.join('\n'), text);
|
||||
});
|
||||
|
||||
test('sendMarkdownReply returns no deliveries for empty text', async () => {
|
||||
const results = await sendMarkdownReply({
|
||||
send: async () => { throw new Error('unexpected'); },
|
||||
sendText: async () => { throw new Error('unexpected'); },
|
||||
}, target, '');
|
||||
assert.deepEqual(results, []);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue