diff --git a/packages/core/src/bridge/actions/misc.ts b/packages/core/src/bridge/actions/misc.ts deleted file mode 100644 index 0ca4a9bc..00000000 --- a/packages/core/src/bridge/actions/misc.ts +++ /dev/null @@ -1,153 +0,0 @@ -import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; -import type { Bridge } from '../bridge'; -import { makeOidbEnvelope, runOidb } from '../bridge-oidb'; -import { OidbBase } from '@snowluma/proto-defs/oidb'; -import type { - MiniAppShareReq, - MiniAppShareResp, - Oidb0x112eReq, - Oidb0x112eResp, - Oidb0x990Req, - Oidb0x990Resp, - Oidb0xeb7Req, - Oidb0xeb7Resp, -} from '@snowluma/proto-defs/oidb-actions/base'; - -export async function translateEn2Zh( - bridge: Bridge, - words: string[], -) { - const req = { - translateReq: { - srcLang: 'en', - dstLang: 'zh', - words: words, - }, - tag10: 1, - tag12: 1, - }; - - const env = makeOidbEnvelope(0x990, 2, req); - const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x990_2', protobuf_encode>(env)); - const result = protobuf_decode>(respBytes).body; - - const resp = result?.translateResp; - if (!resp) { - throw new Error('translate response empty'); - } - - return resp.dstWords || []; -} - -export async function getMiniAppArk( - bridge: Bridge, - type: string, - title: string, - desc: string, - picUrl: string, - jumpUrl: string, -) { - let appid = '1109937557'; // default: bilibili - let iconUrl = 'http://miniapp.gtimg.cn/public/appicon/51f90239b78a2e4994c11215f4c4ba15_200.jpg'; - - if (type === 'weibo') { - appid = '1109224783'; - iconUrl = 'http://miniapp.gtimg.cn/public/appicon/35bbb44dc68e65194cfacfb206b8f1f7_200.jpg'; - } else if (type !== 'bili') { - throw new Error(`unsupported type: ${type}, only support bili and weibo`); - } - - const request = protobuf_encode({ - sdkVersion: 'V1_PC_MINISDK_99.99.99_1_APP_A', - body: { - appid, - title, - desc, - picUrl, - jumpUrl, - iconUrl, - }, - }); - - const result = await bridge.sendRawPacket('LightAppSvc.mini_app_share.AdaptShareInfo', request); - - if (!result.success || !result.responseData) { - throw new Error(result.errorMessage || 'get mini app ark failed'); - } - - const decoded = protobuf_decode(result.responseData); - const jsonStr = decoded?.body?.jsonStr; - - if (!jsonStr) { - throw new Error('mini app share json empty'); - } - - const parsed = JSON.parse(jsonStr); - - return { - data: { - ver: parsed.ver, - prompt: parsed.prompt, - config: parsed.config, - app: parsed.appName, - view: parsed.appView, - meta: parsed.metaData, - miniappShareOrigin: 3, - miniappOpenRefer: '10002', - }, - }; -} - -export async function clickInlineKeyboardButton( - bridge: Bridge, - groupId: number, - botAppid: number, - buttonId: string, - callbackData: string, - msgSeq: number, -) { - const req = { - botAppid: BigInt(botAppid), - msgSeq: BigInt(msgSeq), - buttonId: String(buttonId), - callbackData: String(callbackData || ''), - unknown7: 0, - groupId: BigInt(groupId), - unknown9: 1, - }; - - const env = makeOidbEnvelope(0x112E, 1, req); - const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x112e_1', protobuf_encode>(env)); - const result = protobuf_decode>(respBytes).body; - - if (!result) { - throw new Error('click inline keyboard button result empty'); - } - - return { - result: Number(result.result || 0), - errMsg: result.errMsg || '', - status: 0, - promptText: result.promptText || '', - promptType: 0, - promptIcon: 0, - }; -} - -export async function sendGroupSign( - bridge: Bridge, - groupId: number, -) { - const req = { - signInInfo: { - uin: String(bridge.identity.uin), - groupId: String(groupId), - version: '9.0.90', - }, - }; - - const env = makeOidbEnvelope(0xEB7, 1, req); - const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0xEB7_1', protobuf_encode>(env)); - // Decode just to maintain the original behaviour of "consume the response". - protobuf_decode>(respBytes); -} diff --git a/packages/core/src/bridge/apis/index.ts b/packages/core/src/bridge/apis/index.ts index b90260b7..11fab588 100644 --- a/packages/core/src/bridge/apis/index.ts +++ b/packages/core/src/bridge/apis/index.ts @@ -29,6 +29,7 @@ import { GroupAlbumApi } from './group-album'; import { GroupFileApi } from './group-file'; import { InteractionApi } from './interaction'; import { MessageApi } from './message'; +import { MiscApi } from './misc'; import { ProfileApi } from './profile'; export interface ApiHub { @@ -50,6 +51,8 @@ export interface ApiHub { readonly profile: ProfileApi; /** Long-message (forward / 聊天记录) upload + retrieval with NapCat piggyback. */ readonly forward: ForwardApi; + /** Odds & ends: translate / mini-app ARK / inline-keyboard / group sign. */ + readonly misc: MiscApi; // Pending — added as later commits land: // readonly interaction: InteractionApi; // readonly profile: ProfileApi; @@ -78,6 +81,7 @@ export function buildApiHub(ctx: BridgeContext): ApiHub { friend: new FriendApi(ctx), profile: new ProfileApi(ctx), forward: new ForwardApi(ctx), + misc: new MiscApi(ctx), }; } @@ -92,4 +96,5 @@ export { GroupAlbumApi } from './group-album'; export { GroupFileApi } from './group-file'; export { InteractionApi } from './interaction'; export { MessageApi } from './message'; +export { MiscApi } from './misc'; export { ProfileApi } from './profile'; diff --git a/packages/core/src/bridge/apis/misc.ts b/packages/core/src/bridge/apis/misc.ts new file mode 100644 index 00000000..d586a8fc --- /dev/null +++ b/packages/core/src/bridge/apis/misc.ts @@ -0,0 +1,146 @@ +// MiscApi — odds & ends that don't fit the other Apis: translation, +// mini-app ARK build, inline-keyboard button click, group sign-in. +// Inlined from `actions/misc.ts` (deleted alongside actions/* in +// commit 13). + +import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; +import type { OidbBase } from '@snowluma/proto-defs/oidb'; +import type { + MiniAppShareReq, + MiniAppShareResp, + Oidb0x112eReq, + Oidb0x112eResp, + Oidb0x990Req, + Oidb0x990Resp, + Oidb0xeb7Req, + Oidb0xeb7Resp, +} from '@snowluma/proto-defs/oidb-actions/base'; +import type { BridgeContext } from '../bridge-context'; +import type { Bridge } from '../bridge'; +import { makeOidbEnvelope, runOidb } from '../bridge-oidb'; + +function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; } + +export class MiscApi { + constructor(private readonly ctx: BridgeContext) {} + + async translateEn2Zh(words: string[]): Promise { + const bridge = asBridge(this.ctx); + const req = { + translateReq: { + srcLang: 'en', + dstLang: 'zh', + words, + }, + tag10: 1, + tag12: 1, + }; + + const env = makeOidbEnvelope(0x990, 2, req); + const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x990_2', protobuf_encode>(env)); + const result = protobuf_decode>(respBytes).body; + + const resp = result?.translateResp; + if (!resp) { + throw new Error('translate response empty'); + } + + return resp.dstWords || []; + } + + async getMiniAppArk(type: string, title: string, desc: string, picUrl: string, jumpUrl: string): Promise { + let appid = '1109937557'; // default: bilibili + let iconUrl = 'http://miniapp.gtimg.cn/public/appicon/51f90239b78a2e4994c11215f4c4ba15_200.jpg'; + + if (type === 'weibo') { + appid = '1109224783'; + iconUrl = 'http://miniapp.gtimg.cn/public/appicon/35bbb44dc68e65194cfacfb206b8f1f7_200.jpg'; + } else if (type !== 'bili') { + throw new Error(`unsupported type: ${type}, only support bili and weibo`); + } + + const request = protobuf_encode({ + sdkVersion: 'V1_PC_MINISDK_99.99.99_1_APP_A', + body: { appid, title, desc, picUrl, jumpUrl, iconUrl }, + }); + + const result = await this.ctx.sendRawPacket('LightAppSvc.mini_app_share.AdaptShareInfo', request); + + if (!result.success || !result.responseData) { + throw new Error(result.errorMessage || 'get mini app ark failed'); + } + + const decoded = protobuf_decode(result.responseData); + const jsonStr = decoded?.body?.jsonStr; + + if (!jsonStr) { + throw new Error('mini app share json empty'); + } + + const parsed = JSON.parse(jsonStr); + + return { + data: { + ver: parsed.ver, + prompt: parsed.prompt, + config: parsed.config, + app: parsed.appName, + view: parsed.appView, + meta: parsed.metaData, + miniappShareOrigin: 3, + miniappOpenRefer: '10002', + }, + }; + } + + async clickInlineKeyboardButton( + groupId: number, + botAppid: number, + buttonId: string, + callbackData: string, + msgSeq: number, + ): Promise { + const bridge = asBridge(this.ctx); + const req = { + botAppid: BigInt(botAppid), + msgSeq: BigInt(msgSeq), + buttonId: String(buttonId), + callbackData: String(callbackData || ''), + unknown7: 0, + groupId: BigInt(groupId), + unknown9: 1, + }; + + const env = makeOidbEnvelope(0x112E, 1, req); + const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x112e_1', protobuf_encode>(env)); + const result = protobuf_decode>(respBytes).body; + + if (!result) { + throw new Error('click inline keyboard button result empty'); + } + + return { + result: Number(result.result || 0), + errMsg: result.errMsg || '', + status: 0, + promptText: result.promptText || '', + promptType: 0, + promptIcon: 0, + }; + } + + async sendGroupSign(groupId: number): Promise { + const bridge = asBridge(this.ctx); + const req = { + signInInfo: { + uin: String(this.ctx.identity.uin), + groupId: String(groupId), + version: '9.0.90', + }, + }; + + const env = makeOidbEnvelope(0xEB7, 1, req); + const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0xEB7_1', protobuf_encode>(env)); + protobuf_decode>(respBytes); + } +} diff --git a/packages/core/src/bridge/bridge-interface.ts b/packages/core/src/bridge/bridge-interface.ts index 7489ceef..8009592c 100644 --- a/packages/core/src/bridge/bridge-interface.ts +++ b/packages/core/src/bridge/bridge-interface.ts @@ -87,11 +87,8 @@ export interface BridgeInterface { // - setAvatar / setGroupAvatar / fetchCustomFace // - getLike / getUnidirectionalFriendList - // ─── Misc ─── - translateEn2Zh(words: string[]): Promise; - getMiniAppArk(type: string, title: string, desc: string, picUrl: string, jumpUrl: string): Promise; - clickInlineKeyboardButton(groupId: number, botAppid: number, buttonId: string, callbackData: string, msgSeq: number): Promise; - sendGroupSign(groupId: number): Promise; + // ─── Misc (moved to apis.misc) ─── + // - translateEn2Zh / getMiniAppArk / clickInlineKeyboardButton / sendGroupSign // ─── Tier-2 napcat parity extras ─── setGroupTodo(groupId: number, msgSeq: bigint | number | string): Promise; diff --git a/packages/core/src/bridge/bridge.ts b/packages/core/src/bridge/bridge.ts index 1f9923f1..0df74573 100644 --- a/packages/core/src/bridge/bridge.ts +++ b/packages/core/src/bridge/bridge.ts @@ -35,12 +35,7 @@ import type { GroupFilesResult } from './apis/group-file'; // were absorbed into MessageApi back in commit 1. // actions/interaction.ts removed — sendPoke/sendLike/setReaction/ // getEmojiLikes moved to apis/interaction.ts::InteractionApi. -import { - clickInlineKeyboardButton as clickInlineKeyboardButton_, - getMiniAppArk as getMiniAppArk_, - sendGroupSign as sendGroupSign_, - translateEn2Zh as translateEn2Zh_, -} from './actions/misc'; +// actions/misc.ts removed — moved to apis/misc.ts::MiscApi. // actions/profile.ts removed — moved to apis/profile.ts::ProfileApi. // `bridge-contacts.ts` removed — its 6 functions are now methods on // `apis.contacts` (see `apis/contacts.ts::ContactsApi`). @@ -344,18 +339,8 @@ export class Bridge implements BridgeInterface { async getCookiesStr(domain: string): Promise { return getCookiesStr_(this, domain); } async getCsrfToken(): Promise { return getCsrfToken_(this); } async getCredentials(domain: string) { return getCredentials_(this, domain); } - async translateEn2Zh(words: string[]) { - return translateEn2Zh_(this, words); - } - async getMiniAppArk(type: string, title: string, desc: string, picUrl: string, jumpUrl: string) { - return getMiniAppArk_(this, type, title, desc, picUrl, jumpUrl); - } - async clickInlineKeyboardButton(groupId: number, botAppid: number, buttonId: string, callbackData: string, msgSeq: number) { - return clickInlineKeyboardButton_(this, groupId, botAppid, buttonId, callbackData, msgSeq); - } - async sendGroupSign(groupId: number) { - return sendGroupSign_(this, groupId); - } + // Misc methods (translateEn2Zh / getMiniAppArk / + // clickInlineKeyboardButton / sendGroupSign) moved to apis.misc. // --- Tier-2 napcat-parity extras (group todo, stranger status, AI voice) --- diff --git a/packages/core/src/onebot/actions/extended.ts b/packages/core/src/onebot/actions/extended.ts index 91ad3e06..b73c2eb3 100644 --- a/packages/core/src/onebot/actions/extended.ts +++ b/packages/core/src/onebot/actions/extended.ts @@ -975,7 +975,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { try { - const translated = await ctx.bridge.translateEn2Zh(words); + const translated = await ctx.bridge.apis.misc.translateEn2Zh(words); return okResponse({ words: translated }); } catch (e) { return failedResponse(RETCODE.ACTION_FAILED, String(e)); @@ -999,7 +999,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { try { - const data = await ctx.bridge.getMiniAppArk( + const data = await ctx.bridge.apis.misc.getMiniAppArk( String(type), String(title), String(desc), @@ -1025,7 +1025,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { try { - const data = await ctx.bridge.clickInlineKeyboardButton( + const data = await ctx.bridge.apis.misc.clickInlineKeyboardButton( groupId, botAppid, String(buttonId), @@ -1047,7 +1047,7 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void { try { - await ctx.bridge.sendGroupSign(groupId); + await ctx.bridge.apis.misc.sendGroupSign(groupId); return okResponse({}); } catch (e) { return failedResponse(RETCODE.ACTION_FAILED, String(e)); diff --git a/packages/core/tests/actions/misc.test.ts b/packages/core/tests/actions/misc.test.ts index adc9ff38..b1a850d9 100644 --- a/packages/core/tests/actions/misc.test.ts +++ b/packages/core/tests/actions/misc.test.ts @@ -23,10 +23,10 @@ vi.mock('../../src/bridge/bridge-oidb', async () => { }); import * as oidb from '../../src/bridge/bridge-oidb'; -import * as misc from '../../src/bridge/actions/misc'; +import { MiscApi } from '../../src/bridge/apis/misc'; import { mockBridge } from './_helpers'; -describe('actions/misc', () => { +describe('apis/misc', () => { beforeEach(() => { vi.mocked(oidb.runOidb).mockReset(); vi.mocked(oidb.runOidb).mockResolvedValue(new Uint8Array()); @@ -40,7 +40,7 @@ describe('actions/misc', () => { body: { translateResp: { dstWords: ['你好', '世界'] } } as any, }), ); - const out = await misc.translateEn2Zh(bridge as any, ['hello', 'world']); + const out = await new MiscApi(bridge as any).translateEn2Zh( ['hello', 'world']); expect(out).toEqual(['你好', '世界']); }); @@ -49,14 +49,14 @@ describe('actions/misc', () => { vi.mocked(oidb.runOidb).mockResolvedValueOnce( protobuf_encode>({ body: {} }), ); - await expect(misc.translateEn2Zh(bridge as any, ['hello'])) + await expect(new MiscApi(bridge as any).translateEn2Zh( ['hello'])) .rejects.toThrow(/empty/); }); it('getMiniAppArk rejects unsupported types', async () => { const bridge = mockBridge(); await expect( - misc.getMiniAppArk(bridge as any, 'youtube', 't', 'd', 'p', 'u'), + new MiscApi(bridge as any).getMiniAppArk( 'youtube', 't', 'd', 'p', 'u'), ).rejects.toThrow(/unsupported type/); }); @@ -75,7 +75,7 @@ describe('actions/misc', () => { }); // With null responseData, the action should throw on decode — that // still proves the right serviceCmd was used. - await expect(misc.getMiniAppArk(bridge as any, 'bili', 't', 'd', 'p', 'u')).rejects.toThrow(); + await expect(new MiscApi(bridge as any).getMiniAppArk( 'bili', 't', 'd', 'p', 'u')).rejects.toThrow(); expect(bridge.sendRawPacket).toHaveBeenCalledOnce(); expect(bridge.sendRawPacket.mock.calls[0]![0]) .toBe('LightAppSvc.mini_app_share.AdaptShareInfo'); @@ -88,13 +88,13 @@ describe('actions/misc', () => { body: { result: 1, errMsg: 'ok', promptText: 'hi' } as any, }), ); - const out = await misc.clickInlineKeyboardButton(bridge as any, 1, 2, 'btn', 'data', 100); + const out = await new MiscApi(bridge as any).clickInlineKeyboardButton( 1, 2, 'btn', 'data', 100); expect(out).toMatchObject({ result: 1, errMsg: 'ok', promptText: 'hi', status: 0 }); }); it('sendGroupSign dispatches to 0xEB7_1', async () => { const bridge = mockBridge(); - await misc.sendGroupSign(bridge as any, 12345); + await new MiscApi(bridge as any).sendGroupSign( 12345); const [, cmd] = vi.mocked(oidb.runOidb).mock.calls[0]!; expect(cmd).toBe('OidbSvcTrpcTcp.0xEB7_1'); });