From 84e5254c3a136d9b55be7f98ea1c30c1f9a08ec2 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Wed, 17 Jun 2026 23:58:00 +0800 Subject: [PATCH] feat(onebot): implement get_file via media cache resolution Resolve file_id against the image cache, then the record cache (both already mint a fresh url + carry file_size/file_name), replacing the not-yet-implemented stub. Document-type files have no id-resolvable cache yet, so they are intentionally left uncovered rather than faked. --- packages/onebot/src/actions/extended.ts | 15 +++++--- .../onebot/tests/extended-actions.test.ts | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/packages/onebot/src/actions/extended.ts b/packages/onebot/src/actions/extended.ts index f332423c..4b286d40 100644 --- a/packages/onebot/src/actions/extended.ts +++ b/packages/onebot/src/actions/extended.ts @@ -1032,18 +1032,25 @@ export const actions = [ }, }), + // get_file — 统一文件信息入口。SnowLuma 按 file_id 解析媒体缓存: + // 先图片、后语音(两者已带 url 重签与 file_size/file_name)。文档类文件 + // 当前无可按 id 解析的缓存,故不在此伪造,仅覆盖图片/语音的主流用例。 defineAction({ name: 'get_file', - summary: '获取文件(未实现)', + summary: '获取文件信息(图片/语音缓存)', readOnly: true, params: { file_id: f.string().default(''), file: f.string().default(''), }, - run: async (p) => { - const fileId = p.file_id || p.file; + run: async (p, ctx) => { + const fileId = p.file || p.file_id; if (!fileId) return failedResponse(RETCODE.BAD_REQUEST, 'file_id is required'); - return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented'); + const image = await ctx.getImageInfo(fileId); + if (image) return okResponse(image); + const record = await ctx.getRecordInfo(fileId); + if (record) return okResponse(record); + return failedResponse(RETCODE.ACTION_FAILED, 'file not found in cache'); }, }), diff --git a/packages/onebot/tests/extended-actions.test.ts b/packages/onebot/tests/extended-actions.test.ts index 150d6e4b..43f2d66c 100644 --- a/packages/onebot/tests/extended-actions.test.ts +++ b/packages/onebot/tests/extended-actions.test.ts @@ -640,3 +640,37 @@ describe('extended-actions / get_group_shut_list', () => { expect(res).toMatchObject({ status: 'failed', retcode: 1400 }); }); }); + +// ─── Wave 1: get_file (compose image→record cache) ─── + +describe('extended-actions / get_file', () => { + const imageInfo = { file: 'a.jpg', url: 'http://x/a.jpg', file_size: '12', file_name: 'a.jpg' }; + const recordInfo = { file: 'b.amr', url: 'http://x/b.amr', file_size: '34', file_name: 'b.amr' }; + + it('resolves an image file_id via the image cache', async () => { + const getImageInfo = vi.fn(async () => imageInfo); + const getRecordInfo = vi.fn(async () => null); + const res = await makeHandler(fakeCtx(fakeBridge(), { getImageInfo, getRecordInfo })).handle('get_file', { file_id: 'a.jpg' }); + expect(res).toMatchObject({ status: 'ok', data: imageInfo }); + expect(getRecordInfo).not.toHaveBeenCalled(); + }); + + it('falls back to the record cache when not an image', async () => { + const getImageInfo = vi.fn(async () => null); + const getRecordInfo = vi.fn(async () => recordInfo); + const res = await makeHandler(fakeCtx(fakeBridge(), { getImageInfo, getRecordInfo })).handle('get_file', { file: 'b.amr' }); + expect(res).toMatchObject({ status: 'ok', data: recordInfo }); + }); + + it('fails when the id resolves to nothing cached', async () => { + const getImageInfo = vi.fn(async () => null); + const getRecordInfo = vi.fn(async () => null); + const res = await makeHandler(fakeCtx(fakeBridge(), { getImageInfo, getRecordInfo })).handle('get_file', { file_id: 'nope' }); + expect(res).toMatchObject({ status: 'failed', retcode: 100 }); + }); + + it('rejects when neither file nor file_id is given', async () => { + const res = await makeHandler(fakeCtx(fakeBridge())).handle('get_file', {}); + expect(res).toMatchObject({ status: 'failed', retcode: 1400 }); + }); +});