fix(onebot): add canonical fields to file segment and fix log rendering (#94)

* fix(onebot): support inline file url/path in send_group/private_msg

* test(onebot): update file segment parser test for inline url/path support

* fix(onebot): add canonical fields to file segment and fix log rendering
This commit is contained in:
游手好闲的言柒 authored and GitHub committed 2026-06-11 15:40:47 +08:00
1 parent d804a3b2e0
commit d2a67cb560
4 files changed
+35 -6

No files matched your search

+9
View File
@@ -110,6 +110,15 @@ describe('formatMessageSegments', () => {
expect(out).toBe('hi [图片] @12345 @全体成员 [回复:999] [表情] [视频] [语音]');
});
it('renders file segments using data.name (the OneBot field), with data.file fallback', () => {
expect(formatMessageSegments([{ type: 'file', data: { name: '20260606_151113.wav' } }]))
.toBe('[文件:20260606_151113.wav]');
expect(formatMessageSegments([{ type: 'file', data: { file: 'legacy.bin' } }]))
.toBe('[文件:legacy.bin]');
expect(formatMessageSegments([{ type: 'file', data: {} }]))
.toBe('[文件]');
});
it('renders unknown segment types as [type]', () => {
expect(formatMessageSegments([{ type: 'something_new', data: {} }]))
.toBe('[something_new]');
@@ -104,12 +104,22 @@ async function elementToSegment(
if (element.type === 'file') {
const url = mediaUrlResolver ? await mediaUrlResolver(element, isGroup, sessionId) : (element.url ?? '');
const fileName = element.fileName ?? '';
const fileSize = element.fileSize ?? 0;
const fileId = element.fileId ?? '';
return {
type: 'file',
data: {
name: element.fileName ?? '',
size: element.fileSize ?? 0,
id: element.fileId ?? '',
// NapCat/LLOneBot-style canonical fields — most downstream
// OneBot adapters read these (`file`/`file_id`/`file_size`).
file: fileName,
file_id: fileId,
file_size: fileSize,
// Legacy SnowLuma field names, kept for backward compat with
// any consumer that already reads name/size/id.
name: fileName,
size: fileSize,
id: fileId,
url,
file_hash: element.fileHash ?? '',
},
@@ -438,14 +438,18 @@ describe('convertEvent — message elements (13 segment types)', () => {
expect((seg2.data as Record<string, unknown>).resid).toBe(50);
});
it('file: name + size + id + url + file_hash, url via mediaUrlResolver', async () => {
it('file: canonical file/file_id/file_size + legacy name/size/id + url + file_hash', async () => {
const seg = await segment(
{ type: 'file', fileName: 'doc.pdf', fileSize: 7, fileId: 'fid', fileHash: 'h' },
{ mediaUrlResolver: async () => 'http://download' },
);
expect(seg.type).toBe('file');
expect(seg.data).toEqual({
name: 'doc.pdf', size: 7, id: 'fid', url: 'http://download', file_hash: 'h',
// NapCat/LLOneBot-style canonical fields
file: 'doc.pdf', file_id: 'fid', file_size: 7,
// legacy SnowLuma fields
name: 'doc.pdf', size: 7, id: 'fid',
url: 'http://download', file_hash: 'h',
});
});
+7 -1
View File
@@ -95,7 +95,13 @@ function renderSegment(type: string, data: Record<string, unknown>): string {
case 'reply': return `[回复:${data.id ?? ''}]`;
case 'record': return '[语音]';
case 'video': return '[视频]';
case 'file': return data.file ? `[文件:${truncate(String(data.file), 20)}]` : '[文件]';
case 'file': {
// OneBot file segments carry the filename under `data.name`
// (see to-segment.ts). Keep `data.file` as a fallback for any
// upstream that still uses the older field name.
const fileName = data.name ?? data.file;
return fileName ? `[文件:${truncate(String(fileName), 20)}]` : '[文件]';
}
case 'json': return '[JSON]';
case 'xml': return '[XML]';
case 'markdown': return '[Markdown]';