Phase 2 of the 4-package split (RoadMap #3). Moves ~35 files — events / event-bus / identity-service / packet-pipeline / element- builder / bridge-oidb / qq-info / msg-push/* / highway/* / web/* + the bridge-side formatters — out of `packages/core/src/bridge/` into the new `@snowluma/bridge` workspace package. Everything that translates QQ wire bytes ↔ typed events/elements now lives there. Why --- - `packages/core/src/bridge/` was actually two layers: a low-level "protocol decoder + I/O" set (msg-push, highway, packet-pipeline, identity, element-builder) AND the Api-class host (bridge.ts + apis/*). The Api layer can't move without the protocol layer moving first, but the two had grown into one directory. - Bridge-internal consumers (highway uploaders, OIDB envelope helper, element-builder, msg-push parsers) only ever needed `bridge.identity` + `bridge.sendRawPacket` + the upload-metadata cache. Splitting them out lets that minimal surface live in the bridge package as `BridgeContext` while the api-layer's extended context (with `apis: ApiHub`) stays in @snowluma/core. What stays in @snowluma/core (apis layer): - bridge.ts ── the god-class that composes the protocol building blocks + mounts apis - bridge-interface.ts ── public façade for OneBot consumers - manager.ts ── per-uin BridgeManager hub - apis/* ── the 13 typed Api classes - bridge-context.ts (NEW) ── extends @snowluma/bridge's slim BridgeContext with `apis: ApiHub` so the 13 Api classes keep their typed `this.ctx.apis.X.method()` access. Layout ------ Moved via `git mv` (history preserved): events / event-bus / identity-service / packet-pipeline / element-builder / bridge-oidb / qq-info / msg-push/* / highway/* / web/* → @snowluma/bridge/<sub> utils/event-format.ts → @snowluma/bridge/format.ts (the last one renamed because the destination directory only has bridge-related files; `format.ts` reads cleaner than `event-format`. Listed as a rename in git's `RM` output — content unchanged.) Layering fixes (functionality preserved, just type signatures) -------------------------------------------------------------- - `element-builder.SendContext.bridge` → typed as `BridgeContext` instead of `Bridge`. Reads only `bridge.identity` and the upload-cache (both on BridgeContext); the concrete `Bridge` instance still passes via free upcast. - `bridge-oidb.runOidb` signature widened from `Bridge` to `BridgeContext` — same reason. - `highway/{highway-client,pipeline,image-upload,ptt-upload, video-upload}.ts` — same. - `format.ts::formatReply` parameter typed as a new local `ReplyEventLookup` structural type (`{ findEvent(id): JsonObject | null }`) instead of `MessageStore` from @snowluma/onebot. The OneBot `MessageStore` is a structural superset; consumers pass it through unchanged. - JSON value types moved to a new `@snowluma/common/json` subpath (consumed by format.ts) — `onebot/types.ts` re-exports them so external `import { JsonValue } from '<…>/onebot/types'` paths keep working. - `UploadedFileMeta` interface moved from `core/src/bridge/bridge.ts` into `@snowluma/bridge/bridge-context.ts` — element-builder + the forward-builder both read the upload cache through BridgeContext, so the type has to live in @snowluma/bridge. `bridge.ts` keeps its re-export for back-compat. Changes ------- - New package `packages/bridge/` (package.json with per-file subpath exports mirroring legacy `core/src/bridge/<x>` layout 1:1, tsconfig.json, typecheck script). - 35 `.ts` files moved via `git mv` (3 files counted by git as RM — rename + modify because Bridge → BridgeContext type swap). - 150+ import sites rewritten across `packages/core/src` and `packages/core/tests`: `'../bridge/<x>'`, `'../../bridge/<x>'`, `'../src/bridge/<x>'`, `'@/bridge/web/<x>'`, and vitest `vi.mock(...)` literals all collapse to `@snowluma/bridge/<x>`. - `@snowluma/core` depends on `@snowluma/bridge` (workspace:*). - `@snowluma/bridge` depends on `@snowluma/common`, `@snowluma/proto-defs`, `@snowluma/proton`. - `packages/core/src/utils/` directory removed (empty). Verification ------------ - Full workspace `pnpm -r typecheck`: all 10 projects clean. - core test suite: 552 passed / 552 total (pre-existing websocket-darwin-arm64.node addon failure unchanged — same baseline as the previous refactor commits). - Vite build (with stubbed native binary): clean — 143 modules, 2.84 MB bundle / 224 KB gzip, identical envelope to the pre-split build. Phase 3 (next): OneBot + WebUI move into @snowluma/onebot. The runnable `index.ts` stays in @snowluma/core as the application entry, importing `OneBotManager` from `@snowluma/onebot`.
152 lines
6.1 KiB
TypeScript
152 lines
6.1 KiB
TypeScript
// ExtrasApi — Tier-2 napcat-parity surfaces that don't fit anywhere
|
||
// else: group todo CRUD, stranger online/ext status decode, AI voice
|
||
// list + synthesis. Inlined from `actions/extras.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 {
|
||
OidbGroupTodo,
|
||
OidbStrangerStatusReq,
|
||
OidbStrangerStatusResp,
|
||
} from '@snowluma/proto-defs/oidb-actions/base';
|
||
import type {
|
||
OidbAiVoiceListReq,
|
||
OidbAiVoiceListResp,
|
||
OidbAiVoiceReq,
|
||
OidbAiVoiceResp,
|
||
} from '@snowluma/proto-defs/oidb-actions/media';
|
||
import type { MediaIndexNode } from './shared';
|
||
import type { BridgeContext } from '../bridge-context';
|
||
import type { Bridge } from '../bridge';
|
||
import { makeOidbEnvelope, runOidb } from '@snowluma/bridge/bridge-oidb';
|
||
|
||
function asBridge(ctx: BridgeContext): Bridge { return ctx as unknown as Bridge; }
|
||
|
||
// ─────────────── public types (re-exported from bridge.ts as before) ───
|
||
|
||
export interface StrangerStatus {
|
||
status: number;
|
||
ext_status: number;
|
||
}
|
||
|
||
export const AiVoiceChatType = {
|
||
Unknown: 0,
|
||
Sound: 1,
|
||
Sing: 2,
|
||
} as const;
|
||
export type AiVoiceChatType = typeof AiVoiceChatType[keyof typeof AiVoiceChatType];
|
||
|
||
export interface AiVoiceItem {
|
||
voiceId: string;
|
||
voiceDisplayName: string;
|
||
voiceExampleUrl: string;
|
||
}
|
||
|
||
export interface AiVoiceCategory {
|
||
category: string;
|
||
voices: AiVoiceItem[];
|
||
}
|
||
|
||
export class ExtrasApi {
|
||
constructor(private readonly ctx: BridgeContext) {}
|
||
|
||
// ─────────────── Group todo (0xF90) ───────────────
|
||
|
||
async setGroupTodo(groupId: number, msgSeq: bigint): Promise<void> {
|
||
const bridge = asBridge(this.ctx);
|
||
const env = makeOidbEnvelope<OidbGroupTodo>(0xF90, 1, { groupUin: groupId, msgSeq });
|
||
await runOidb(bridge, 'OidbSvcTrpcTcp.0xf90_1', protobuf_encode<OidbBase<OidbGroupTodo>>(env));
|
||
}
|
||
|
||
async completeGroupTodo(groupId: number, msgSeq: bigint): Promise<void> {
|
||
const bridge = asBridge(this.ctx);
|
||
const env = makeOidbEnvelope<OidbGroupTodo>(0xF90, 2, { groupUin: groupId, msgSeq });
|
||
await runOidb(bridge, 'OidbSvcTrpcTcp.0xf90_2', protobuf_encode<OidbBase<OidbGroupTodo>>(env));
|
||
}
|
||
|
||
async cancelGroupTodo(groupId: number, msgSeq: bigint): Promise<void> {
|
||
const bridge = asBridge(this.ctx);
|
||
const env = makeOidbEnvelope<OidbGroupTodo>(0xF90, 3, { groupUin: groupId, msgSeq });
|
||
await runOidb(bridge, 'OidbSvcTrpcTcp.0xf90_3', protobuf_encode<OidbBase<OidbGroupTodo>>(env));
|
||
}
|
||
|
||
// ─────────────── Stranger online/ext status (0xFE1_2) ───────────────
|
||
|
||
/**
|
||
* Mirror napcat's GetStrangerStatus: decode the extBigInt status word.
|
||
* Values ≤10 fold into a 10×status; everything else is split into the
|
||
* high-byte / low-byte ext_status the OneBot dialect uses.
|
||
*
|
||
* Returns null on transport / decode failure rather than throwing, so
|
||
* the OneBot action can produce a clean retcode without try/catch
|
||
* gymnastics.
|
||
*/
|
||
async getStrangerStatus(uin: number): Promise<StrangerStatus | null> {
|
||
const bridge = asBridge(this.ctx);
|
||
try {
|
||
const env = makeOidbEnvelope<OidbStrangerStatusReq>(
|
||
0xFE1, 2,
|
||
{ uin, key: [{ key: 27372 }] } as any,
|
||
// Same UIN-form flag fetchUserProfile sets — without it newer
|
||
// QQ NT rejects with `[oidb] one of uid/openid is invaild`.
|
||
true,
|
||
);
|
||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0xfe1_2', protobuf_encode<OidbBase<OidbStrangerStatusReq>>(env));
|
||
const resp = protobuf_decode<OidbBase<OidbStrangerStatusResp>>(respBytes).body;
|
||
const raw = resp?.data?.status?.value;
|
||
if (raw === undefined || raw === null) return null;
|
||
const extBig = typeof raw === 'bigint' ? raw : BigInt(raw);
|
||
if (extBig <= 10n) {
|
||
return { status: Number(extBig) * 10, ext_status: 0 };
|
||
}
|
||
const status = Number((extBig & 0xff00n) + ((extBig >> 16n) & 0xffn));
|
||
return { status: 10, ext_status: status };
|
||
} catch {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
// ─────────────── AI voice (0x929D / 0x929B) ───────────────
|
||
|
||
async fetchAiVoiceList(groupId: number, chatType: AiVoiceChatType | number): Promise<AiVoiceCategory[]> {
|
||
const bridge = asBridge(this.ctx);
|
||
const env = makeOidbEnvelope<OidbAiVoiceListReq>(0x929D, 0, { groupUin: groupId, chatType });
|
||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x929d_0', protobuf_encode<OidbBase<OidbAiVoiceListReq>>(env));
|
||
const resp = protobuf_decode<OidbBase<OidbAiVoiceListResp>>(respBytes).body;
|
||
return (resp?.content as AiVoiceCategory[] | undefined) ?? [];
|
||
}
|
||
|
||
/**
|
||
* Trigger AI voice synthesis. Server may return an empty msgInfo while
|
||
* the render is in-flight; we retry until a node materialises or the
|
||
* cap is hit. napcat uses the same 30-retry budget.
|
||
*
|
||
* The returned MediaIndexNode plugs directly into
|
||
* `apis.groupFile.getPttUrl`, which already handles every other
|
||
* download-URL fetch in SnowLuma.
|
||
*/
|
||
async fetchAiVoice(
|
||
groupId: number,
|
||
voiceId: string,
|
||
text: string,
|
||
chatType: AiVoiceChatType | number,
|
||
maxRetries = 30,
|
||
): Promise<MediaIndexNode> {
|
||
const bridge = asBridge(this.ctx);
|
||
// Random 32-bit session id — server uses this to deduplicate polls.
|
||
const sessionId = Math.floor(Math.random() * 0xFFFFFFFF) >>> 0;
|
||
for (let i = 0; i < maxRetries; i++) {
|
||
const env = makeOidbEnvelope<OidbAiVoiceReq>(
|
||
0x929B, 0,
|
||
{ groupUin: groupId, voiceId, text, chatType, session: { sessionId } },
|
||
);
|
||
const respBytes = await runOidb(bridge, 'OidbSvcTrpcTcp.0x929b_0', protobuf_encode<OidbBase<OidbAiVoiceReq>>(env));
|
||
const resp = protobuf_decode<OidbBase<OidbAiVoiceResp>>(respBytes).body;
|
||
const node = resp?.msgInfo?.msgInfoBody?.[0]?.index as MediaIndexNode | undefined;
|
||
if (node) return node;
|
||
}
|
||
throw new Error(`AI voice synthesis did not complete after ${maxRetries} polls`);
|
||
}
|
||
}
|