From 9ca64bed10fa0b8251a1b4048c981d752cc72257 Mon Sep 17 00:00:00 2001 From: Airline233 Date: Wed, 1 Jul 2026 02:13:35 +0800 Subject: [PATCH] feat(core,onebot): implement trans_group_file action (#161) --- packages/core/src/bridge/apis/group-file.ts | 6 +++ packages/core/tests/actions/_helpers.ts | 2 + .../core/tests/actions/group-file.test.ts | 21 ++++++++ packages/mcp/src/generated/catalog.json | 39 ++++++++++++-- packages/mcp/src/generated/catalog.ts | 39 ++++++++++++-- packages/onebot/src/actions/extended.ts | 13 +++-- .../onebot/tests/extended-actions.test.ts | 25 +++++++++ .../proto-defs/src/oidb-actions/group-file.ts | 17 ++++++ packages/protocol/package.json | 4 ++ .../group-file/trans-group-file.ts | 52 +++++++++++++++++++ .../group-file/trans-group-file.test.ts | 52 +++++++++++++++++++ 11 files changed, 260 insertions(+), 10 deletions(-) create mode 100644 packages/protocol/src/oidb-services/group-file/trans-group-file.ts create mode 100644 packages/protocol/tests/oidb-services/group-file/trans-group-file.test.ts diff --git a/packages/core/src/bridge/apis/group-file.ts b/packages/core/src/bridge/apis/group-file.ts index b134be54..afd1b8fc 100644 --- a/packages/core/src/bridge/apis/group-file.ts +++ b/packages/core/src/bridge/apis/group-file.ts @@ -23,6 +23,7 @@ import { MoveGroupFile } from '@snowluma/protocol/oidb-services/group-file/move- import { PublishGroupFile } from '@snowluma/protocol/oidb-services/group-file/publish-group-file'; import { RenameGroupFile } from '@snowluma/protocol/oidb-services/group-file/rename-group-file'; import { RenameGroupFolder } from '@snowluma/protocol/oidb-services/group-file/rename-group-folder'; +import { TransGroupFile, type TransGroupFileResult } from '@snowluma/protocol/oidb-services/group-file/trans-group-file'; import { UploadGroupFileRequest } from '@snowluma/protocol/oidb-services/group-file/upload-group-file-request'; import { UploadPrivateFileRequest } from '@snowluma/protocol/oidb-services/group-file/upload-private-file-request'; import { ensureRetCodeZero } from '@snowluma/protocol/oidb-services/shared'; @@ -238,6 +239,11 @@ export class GroupFileApi { return PublishGroupFile.invoke(this.ctx, { groupId, fileId }); } + trans(groupId: number, fileId: string): Promise { + if (!fileId) throw new Error('trans requires fileId'); + return TransGroupFile.invoke(this.ctx, { groupId, fileId }); + } + // ─────────────── count ─────────────── getCount(groupId: number): Promise<{ fileCount: number; maxCount: number }> { diff --git a/packages/core/tests/actions/_helpers.ts b/packages/core/tests/actions/_helpers.ts index d41bc212..fd003e24 100644 --- a/packages/core/tests/actions/_helpers.ts +++ b/packages/core/tests/actions/_helpers.ts @@ -65,6 +65,7 @@ export interface MockGroupFileApi { upload: ReturnType; uploadPrivate: ReturnType; publish: ReturnType; + trans: ReturnType; getCount: ReturnType; list: ReturnType; getUrl: ReturnType; @@ -85,6 +86,7 @@ export function mockGroupFileApi(): MockGroupFileApi { upload: vi.fn(async () => ({ fileId: 'stub-fid' })), uploadPrivate: vi.fn(async () => ({ fileId: 'stub-pfid', fileHash: 'stub-hash' })), publish: vi.fn(async () => undefined), + trans: vi.fn(async () => ({ saveBusId: 102, saveFilePath: '/stub-path' })), getCount: vi.fn(async () => ({ fileCount: 0, maxCount: 10000 })), list: vi.fn(async () => ({ files: [], folders: [] })), getUrl: vi.fn(async () => 'stub://url'), diff --git a/packages/core/tests/actions/group-file.test.ts b/packages/core/tests/actions/group-file.test.ts index 7b18ef34..d44c4368 100644 --- a/packages/core/tests/actions/group-file.test.ts +++ b/packages/core/tests/actions/group-file.test.ts @@ -8,6 +8,7 @@ import type { OidbGroupFileViewResp, OidbGroupFileFolderResp, OidbGroupSendFileReq, + OidbGroupSendFileResp, } from '@snowluma/proto-defs/oidb-actions/group-file'; import type { OidbPrivateFileUploadResp, @@ -246,6 +247,26 @@ describe('apis/group-file', () => { }); }); + it('trans hits OIDB 0x6d9_0 and returns the saved path metadata', async () => { + const bridge = mockBridge(); + bridge.sendRawPacket.mockResolvedValueOnce(packResponse( + protobuf_encode>({ + body: { transFile: { saveBusId: 102, saveFilePath: '/saved/path' } }, + }), + )); + const out = await new GroupFileApi(bridge as any).trans(12345, 'fid-trans'); + expect(out).toEqual({ saveBusId: 102, saveFilePath: '/saved/path' }); + const [wire, bytes] = bridge.sendRawPacket.mock.calls[0]!; + expect(wire).toBe('OidbSvcTrpcTcp.0x6d9_0'); + const env = protobuf_decode>(bytes); + expect(env.body?.transFile).toMatchObject({ + groupUin: 12345n, + appId: 7, + busId: 102, + fileId: 'fid-trans', + }); + }); + it('uploadPrivate resolves both target + self UID before OIDB call', async () => { const bridge = mockBridge({ identity: { diff --git a/packages/mcp/src/generated/catalog.json b/packages/mcp/src/generated/catalog.json index 706f0cd9..b6972a28 100644 --- a/packages/mcp/src/generated/catalog.json +++ b/packages/mcp/src/generated/catalog.json @@ -9094,13 +9094,46 @@ { "name": "trans_group_file", "aliases": [], - "summary": "转存群文件(未实现)", + "summary": "转存群文件", + "returns": "{ ok: true }", "readOnly": false, - "params": [], + "params": [ + { + "name": "group_id", + "type": "uint", + "required": true, + "schema": { + "type": "integer", + "minimum": 1 + } + }, + { + "name": "file_id", + "type": "string", + "required": true, + "schema": { + "type": "string", + "minLength": 1 + } + } + ], "invariants": [], "inputSchema": { "type": "object", - "properties": {}, + "properties": { + "group_id": { + "type": "integer", + "minimum": 1 + }, + "file_id": { + "type": "string", + "minLength": 1 + } + }, + "required": [ + "group_id", + "file_id" + ], "additionalProperties": true }, "category": "扩展" diff --git a/packages/mcp/src/generated/catalog.ts b/packages/mcp/src/generated/catalog.ts index 8c2ceb70..5d2c9e26 100644 --- a/packages/mcp/src/generated/catalog.ts +++ b/packages/mcp/src/generated/catalog.ts @@ -9097,13 +9097,46 @@ export const ACTIONS: CatalogAction[] = [ { "name": "trans_group_file", "aliases": [], - "summary": "转存群文件(未实现)", + "summary": "转存群文件", + "returns": "{ ok: true }", "readOnly": false, - "params": [], + "params": [ + { + "name": "group_id", + "type": "uint", + "required": true, + "schema": { + "type": "integer", + "minimum": 1 + } + }, + { + "name": "file_id", + "type": "string", + "required": true, + "schema": { + "type": "string", + "minLength": 1 + } + } + ], "invariants": [], "inputSchema": { "type": "object", - "properties": {}, + "properties": { + "group_id": { + "type": "integer", + "minimum": 1 + }, + "file_id": { + "type": "string", + "minLength": 1 + } + }, + "required": [ + "group_id", + "file_id" + ], "additionalProperties": true }, "category": "扩展" diff --git a/packages/onebot/src/actions/extended.ts b/packages/onebot/src/actions/extended.ts index 95e8b114..20ca9ece 100644 --- a/packages/onebot/src/actions/extended.ts +++ b/packages/onebot/src/actions/extended.ts @@ -1636,10 +1636,15 @@ export const actions = [ defineAction({ name: 'trans_group_file', - summary: '转存群文件(未实现)', - params: {}, - run: async () => { - return failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented'); + summary: '转存群文件', + returns: '{ ok: true }', + params: { + group_id: f.uint(), + file_id: f.string({ allowEmpty: false }), + }, + run: async (p, ctx) => { + await ctx.bridge.apis.groupFile.trans(p.group_id, p.file_id); + return okResponse({ ok: true }); }, }), diff --git a/packages/onebot/tests/extended-actions.test.ts b/packages/onebot/tests/extended-actions.test.ts index a1cee2e5..68e3594d 100644 --- a/packages/onebot/tests/extended-actions.test.ts +++ b/packages/onebot/tests/extended-actions.test.ts @@ -641,6 +641,31 @@ describe('extended-actions / get_group_shut_list', () => { }); }); +// ─── Wave 1: trans_group_file (0x6D9_0) ─── + +describe('extended-actions / trans_group_file', () => { + it('forwards group_id + file_id to apis.groupFile.trans and returns ok:true', async () => { + const trans = vi.fn(async () => ({ saveBusId: 102, saveFilePath: '/saved/path' })); + const bridge = fakeBridge({ apis: { groupFile: { trans } } }); + const res = await makeHandler(fakeCtx(bridge)).handle('trans_group_file', { + group_id: 12345, + file_id: 'fid-trans', + }); + expect(trans).toHaveBeenCalledWith(12345, 'fid-trans'); + expect(res).toMatchObject({ status: 'ok', retcode: 0, data: { ok: true } }); + }); + + it('rejects missing required params', async () => { + const trans = vi.fn(); + const bridge = fakeBridge({ apis: { groupFile: { trans } } }); + const r1 = await makeHandler(fakeCtx(bridge)).handle('trans_group_file', { file_id: 'fid' }); + const r2 = await makeHandler(fakeCtx(bridge)).handle('trans_group_file', { group_id: 12345 }); + expect(r1).toMatchObject({ status: 'failed', retcode: 1400 }); + expect(r2).toMatchObject({ status: 'failed', retcode: 1400 }); + expect(trans).not.toHaveBeenCalled(); + }); +}); + // ─── Wave 1: get_file (compose image→record cache) ─── describe('extended-actions / get_file', () => { diff --git a/packages/proto-defs/src/oidb-actions/group-file.ts b/packages/proto-defs/src/oidb-actions/group-file.ts index 007bb455..efa4b543 100644 --- a/packages/proto-defs/src/oidb-actions/group-file.ts +++ b/packages/proto-defs/src/oidb-actions/group-file.ts @@ -132,6 +132,19 @@ export interface OidbGroupFileResp { rename?: pb<5, OidbGroupFileRetResp>; move?: pb<6, OidbGroupFileRetResp>; } +export interface OidbGroupTransFileReq { + groupUin?: pb<1, uint_64>; + appId?: pb<2, uint_32>; + busId?: pb<3, uint_32>; + fileId?: pb<4, string>; +} +export interface OidbGroupTransFileResp { + retCode?: pb<1, int_32>; + retMsg?: pb<2, string>; + clientWording?: pb<3, string>; + saveBusId?: pb<4, uint_32>; + saveFilePath?: pb<5, string>; +} export interface OidbGroupSendFileInfo { busiType?: pb<1, uint_32>; fileId?: pb<2, string>; @@ -145,8 +158,12 @@ export interface OidbGroupSendFileBody { info?: pb<3, OidbGroupSendFileInfo>; } export interface OidbGroupSendFileReq { + transFile?: pb<1, OidbGroupTransFileReq>; body?: pb<5, OidbGroupSendFileBody>; } +export interface OidbGroupSendFileResp { + transFile?: pb<1, OidbGroupTransFileResp>; +} export interface OidbGroupFileCreateFolderReq { groupUin?: pb<1, uint_32>; rootDirectory?: pb<3, string>; diff --git a/packages/protocol/package.json b/packages/protocol/package.json index 741295bb..2428f2ca 100644 --- a/packages/protocol/package.json +++ b/packages/protocol/package.json @@ -332,6 +332,10 @@ "types": "./src/oidb-services/group-file/publish-group-file.ts", "default": "./src/oidb-services/group-file/publish-group-file.ts" }, + "./oidb-services/group-file/trans-group-file": { + "types": "./src/oidb-services/group-file/trans-group-file.ts", + "default": "./src/oidb-services/group-file/trans-group-file.ts" + }, "./oidb-services/group-file/get-group-file-count": { "types": "./src/oidb-services/group-file/get-group-file-count.ts", "default": "./src/oidb-services/group-file/get-group-file-count.ts" diff --git a/packages/protocol/src/oidb-services/group-file/trans-group-file.ts b/packages/protocol/src/oidb-services/group-file/trans-group-file.ts new file mode 100644 index 00000000..d3f5fccd --- /dev/null +++ b/packages/protocol/src/oidb-services/group-file/trans-group-file.ts @@ -0,0 +1,52 @@ +// 0x6D9_0 — transfer a temporary group file into permanent group storage. +// Mirrors LagrangeV2's TransFileReqBody shape under the 0x6D9 family. + +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { OidbBase } from '@snowluma/proto-defs/oidb'; +import type { + OidbGroupSendFileReq, + OidbGroupSendFileResp, +} from '@snowluma/proto-defs/oidb-actions/group-file'; +import { invokeOidb, type OidbSender } from '../../oidb-service'; +import { ensureRetCodeZero, toInt } from '../shared'; + +export interface TransGroupFileResult { + saveBusId: number; + saveFilePath: string; +} + +export namespace TransGroupFile { + export const command = 0x6D9; + export const subCommand = 0; + + export interface Params { groupId: number; fileId: string; } + export type Deps = OidbSender; + + export const serialize = (_ctx: Deps, p: Params): OidbGroupSendFileReq => ({ + transFile: { + groupUin: BigInt(p.groupId), + appId: 7, + busId: 102, + fileId: p.fileId, + }, + }); + + export const deserialize = (_ctx: Deps, body: OidbGroupSendFileResp): TransGroupFileResult => { + const result = body.transFile; + if (!result) throw new Error('group file transfer response missing'); + ensureRetCodeZero('group file transfer', result.retCode, result.retMsg, result.clientWording); + return { + saveBusId: toInt(result.saveBusId), + saveFilePath: typeof result.saveFilePath === 'string' ? result.saveFilePath : '', + }; + }; + + export const encode = (env: OidbBase): Uint8Array => + protobuf_encode>(env); + + export const decode = (bytes: Uint8Array): OidbBase => + protobuf_decode>(bytes); + + export const invoke = (deps: Deps, params: Params): Promise => + invokeOidb(deps, TransGroupFile, params); +} diff --git a/packages/protocol/tests/oidb-services/group-file/trans-group-file.test.ts b/packages/protocol/tests/oidb-services/group-file/trans-group-file.test.ts new file mode 100644 index 00000000..9a40069f --- /dev/null +++ b/packages/protocol/tests/oidb-services/group-file/trans-group-file.test.ts @@ -0,0 +1,52 @@ +import { describe, expect, it, vi } from 'vitest'; +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { OidbBase } from '@snowluma/proto-defs/oidb'; +import type { + OidbGroupSendFileReq, OidbGroupSendFileResp, +} from '@snowluma/proto-defs/oidb-actions/group-file'; +import type { SendPacketResult } from '@snowluma/common/packet-sender'; + +import { TransGroupFile } from '../../../src/oidb-services/group-file/trans-group-file'; + +function makeDeps(body?: OidbGroupSendFileResp) { + const responseData = body !== undefined + ? Buffer.from(protobuf_encode>({ body })) + : Buffer.alloc(0); + const r: SendPacketResult = { success: true, gotResponse: true, errorCode: 0, errorMessage: '', responseData }; + return { sendRawPacket: vi.fn(async () => r) }; +} + +describe('TransGroupFile namespace', () => { + it('declares 0x6D9_0', () => { + expect(TransGroupFile.command).toBe(0x6D9); + expect(TransGroupFile.subCommand).toBe(0); + }); + + it('packages group + file id under the transFile slot', async () => { + const deps = makeDeps({ transFile: { saveBusId: 102, saveFilePath: '/abc' } }); + const out = await TransGroupFile.invoke(deps, { groupId: 12345, fileId: 'fid' }); + expect(out).toEqual({ saveBusId: 102, saveFilePath: '/abc' }); + expect(deps.sendRawPacket).toHaveBeenCalledOnce(); + const [wire, bytes] = deps.sendRawPacket.mock.calls[0]!; + expect(wire).toBe('OidbSvcTrpcTcp.0x6d9_0'); + const env = protobuf_decode>(bytes); + expect(env.body?.transFile).toEqual({ + groupUin: 12345n, + appId: 7, + busId: 102, + fileId: 'fid', + }); + }); + + it('throws on missing transFile sub-message', async () => { + const deps = makeDeps({}); + await expect(TransGroupFile.invoke(deps, { groupId: 1, fileId: 'f' })) + .rejects.toThrow(/transfer response missing/); + }); + + it('throws on non-zero retCode', async () => { + const deps = makeDeps({ transFile: { retCode: 7, retMsg: 'denied' } }); + await expect(TransGroupFile.invoke(deps, { groupId: 1, fileId: 'f' })) + .rejects.toThrow(/code=7/); + }); +});