refactor(onebot): extract OneBot protocol translation into @snowluma/onebot
Phase 3 of the 4-package split (RoadMap #3). Moves the entire `packages/core/src/onebot/` subtree (43 source files + 20 test files spanning network adapters, action handlers, event converters, message parser, message store, media store, instance + manager) into the new `@snowluma/onebot` workspace package. The remaining `@snowluma/core` now hosts only the api-layer (Bridge class + 13 Api classes), the hook tooling, the WebUI server, and the runnable `index.ts`. Why --- - The OneBot adapter is the public-facing OneBot11 protocol surface; it has its own version cadence and its own consumers (third-party bots talking OneBot WS / HTTP). Splitting it lets that cadence move independently of the Api-on-ctx layer. - The two were sharing the `packages/core/` build pipeline only because they happened to live in the same directory. Nothing in the api-layer reaches into OneBot internals. - Mirrors NapCat / Lagrange.OneBot's repo split (their OneBot adapter is a separate assembly from the Core protocol library). Layout ------ Moved via `git mv` (history preserved): packages/core/src/onebot/* → @snowluma/onebot/src/<x> packages/core/tests/onebot/* (6 files) → @snowluma/onebot/tests/ packages/core/tests/{message-parser,message-store,event-pipeline, media-store,media-url-resolver,event-converter, http-server-hot-reload,http-server-content-type, api-handler-logging,message-id,group-msg-emoji-like, config,media-indexer,network-manager}.test.ts (14 files) → @snowluma/onebot/tests/ What stays in @snowluma/core ---------------------------- - `bridge/{bridge,bridge-interface,bridge-context,manager}.ts` - `bridge/apis/*` (the 13 typed Api classes) - `hook/*` (debugger/injector — tooling for the core protocol layer) - `webui/*` (admin UI server — application wiring, not the OneBot adapter itself) - `index.ts` (application entry that wires BridgeManager + OneBotManager + HookManager + WebUI together) Changes ------- - New package `packages/onebot/` (package.json with subpath exports mirroring legacy `core/src/onebot/<x>` 1:1, tsconfig.json, vitest.config.ts with `SNOWLUMA_LOG_LEVEL=debug` env so the action- dispatch logging tests see debug-level subscribers — copied verbatim from core's vitest.config.ts). - @snowluma/core grows new subpath exports for the api-layer files OneBot's type-only imports need: `./bridge`, `./bridge-interface`, `./bridge-context`, `./manager`, `./apis`. - `@snowluma/onebot` depends on `@snowluma/bridge`, `@snowluma/common`, `@snowluma/core`, `@snowluma/proto-defs`, `@snowluma/proton`, `@snowluma/websocket`. - `@snowluma/core` depends on `@snowluma/onebot` (for index.ts + webui/server.ts wiring). - 100+ import sites rewritten via sed: * OneBot → core: `'../bridge/bridge'` → `'@snowluma/core/bridge'`, `'../bridge/manager'` → `'@snowluma/core/manager'`, `'../bridge/bridge-interface'` → `'@snowluma/core/bridge-interface'`, `'@/bridge/bridge'` (vite path alias) → `'@snowluma/core/bridge'`. * Core → OneBot: `'./onebot/manager'` → `'@snowluma/onebot/manager'`, `'../onebot/config'` → `'@snowluma/onebot/config'`, etc. * Test relative paths: `'../../src/onebot/<x>'` → `'../src/<x>'` after the test file move (one level shallower). - New `packages/onebot/src/env.d.ts` declaring `__APP_VERSION__` and `__BUILD_WEBUI__` (the vite-injected globals `info.ts` reads). Workspace cycle --------------- core ↔ onebot is allowed by pnpm workspaces. The runtime direction is one-way: core → onebot (index.ts constructs OneBotManager, webui/server reads OneBot config). OneBot only reaches into core for TYPES (Bridge, BridgeInterface, BridgeManager, ClientKeyInfo, DownloadRKeyInfo); those imports use `import type` so they compile out at runtime and don't introduce a real cycle. Verification ------------ - Full workspace `pnpm -r typecheck`: all 11 projects clean. - `@snowluma/core` test suite: 302/302 pass (33 test files). - `@snowluma/onebot` test suite: 250/250 pass (20 test files; pre-existing `network-manager.test.ts` native-addon failure unchanged — same baseline as before the split). - Combined: 552/552 — identical pass count to the pre-Phase-3 baseline. - Vite build with stubbed native binary: clean, 2.84 MB bundle / 224 KB gzip (identical envelope).
This commit is contained in:
71 files changed
+286
-59
No files matched your search
@@ -0,0 +1,421 @@
|
||||
import type { MessageElement } from '@snowluma/bridge/events';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import type { JsonValue } from './types';
|
||||
|
||||
const log = createLogger('MsgParser');
|
||||
|
||||
export interface ParseMessageOptions {
|
||||
resolveReplySequence?: (replyMessageId: number) => number | null;
|
||||
resolveReplyMeta?: (replyMessageId: number) => { senderUin: number; time: number; random: number } | null;
|
||||
resolveMentionUid?: (targetUin: number) => string | null | Promise<string | null>;
|
||||
musicSignUrl?: string;
|
||||
}
|
||||
|
||||
// --- CQ Code parsing ---
|
||||
|
||||
const CQ_REGEX = /\[CQ:([A-Za-z]+)(?:,([^\]]*))?\]/g;
|
||||
|
||||
/**
|
||||
* Best-effort base-10 integer coercion. Falls back to `fallback` (default 0)
|
||||
* when the input can't be parsed as a finite integer — `parseInt('abc')`
|
||||
* is `NaN`, which silently turns into `0`/`null` further down the wire
|
||||
* pipeline and produces undebuggable protocol-level failures.
|
||||
*/
|
||||
function intOr(value: unknown, fallback = 0): number {
|
||||
if (value === undefined || value === null) return fallback;
|
||||
if (typeof value === 'number') return Number.isFinite(value) ? Math.trunc(value) : fallback;
|
||||
const n = parseInt(String(value), 10);
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
}
|
||||
|
||||
function parseCQParams(raw: string): Record<string, string> {
|
||||
const params: Record<string, string> = {};
|
||||
if (!raw) return params;
|
||||
for (const pair of raw.split(',')) {
|
||||
const eq = pair.indexOf('=');
|
||||
if (eq > 0) {
|
||||
params[pair.substring(0, eq)] = pair.substring(eq + 1)
|
||||
.replace(/[/g, '[')
|
||||
.replace(/]/g, ']')
|
||||
.replace(/,/g, ',')
|
||||
.replace(/&/g, '&');
|
||||
}
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
function cqUnescape(text: string): string {
|
||||
// Order matters: `&` MUST be last so that, e.g., `&#91;` keeps
|
||||
// the literal `[` instead of decoding twice into `[`.
|
||||
return text
|
||||
.replace(/[/g, '[')
|
||||
.replace(/]/g, ']')
|
||||
.replace(/,/g, ',')
|
||||
.replace(/&/g, '&');
|
||||
}
|
||||
|
||||
async function parseFromCQString(message: string, options?: ParseMessageOptions): Promise<MessageElement[]> {
|
||||
const elements: MessageElement[] = [];
|
||||
let lastIndex = 0;
|
||||
|
||||
for (const match of message.matchAll(CQ_REGEX)) {
|
||||
// Text before this CQ code
|
||||
if (match.index! > lastIndex) {
|
||||
const text = cqUnescape(message.substring(lastIndex, match.index!));
|
||||
if (text) elements.push({ type: 'text', text });
|
||||
}
|
||||
lastIndex = match.index! + match[0].length;
|
||||
|
||||
const cqType = match[1];
|
||||
const params = parseCQParams(match[2] || '');
|
||||
const elem = await segmentToElement(cqType, params, options);
|
||||
if (elem) elements.push(elem);
|
||||
}
|
||||
|
||||
// Trailing text
|
||||
if (lastIndex < message.length) {
|
||||
const text = cqUnescape(message.substring(lastIndex));
|
||||
if (text) elements.push({ type: 'text', text });
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
// --- JSON segment parsing ---
|
||||
|
||||
interface MessageSegment {
|
||||
type: string;
|
||||
data?: Record<string, unknown>;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
function isSegmentArray(val: unknown): val is MessageSegment[] {
|
||||
return Array.isArray(val) && val.every(
|
||||
(item) => typeof item === 'object' && item !== null && 'type' in item
|
||||
);
|
||||
}
|
||||
|
||||
async function segmentToElement(type: string, data: Record<string, unknown>, options?: ParseMessageOptions): Promise<MessageElement | null> {
|
||||
const normalizedType = type.toLowerCase();
|
||||
switch (normalizedType) {
|
||||
case 'text': {
|
||||
const text = String(data.text ?? '');
|
||||
return text ? { type: 'text', text } : null;
|
||||
}
|
||||
case 'face': {
|
||||
const id = intOr(data.id, -1);
|
||||
if (id < 0) return null;
|
||||
return { type: 'face', faceId: id };
|
||||
}
|
||||
case 'at': {
|
||||
const qq = String(data.qq ?? '').trim();
|
||||
if (qq === 'all') {
|
||||
return { type: 'at', targetUin: 0, uid: 'all', text: '@全体成员 ' };
|
||||
}
|
||||
const uin = intOr(qq, 0);
|
||||
if (uin <= 0) return null;
|
||||
|
||||
const name = String(data.name ?? data.nickname ?? data.card ?? '').trim();
|
||||
let uid = String(data.uid ?? '').trim();
|
||||
if (!uid && options?.resolveMentionUid) {
|
||||
uid = (await options.resolveMentionUid(uin))?.trim() ?? '';
|
||||
}
|
||||
|
||||
// Leave `text` undefined when the caller didn't supply a display
|
||||
// name. The element-builder then resolves it from the group roster
|
||||
// (so QQ renders `@昵称` instead of `@QQ号`).
|
||||
const element: MessageElement = { type: 'at', targetUin: uin };
|
||||
if (uid) element.uid = uid;
|
||||
if (name) element.text = `@${name} `;
|
||||
return element;
|
||||
}
|
||||
case 'reply': {
|
||||
const id = intOr(data.id, 0);
|
||||
if (id === 0) return null;
|
||||
|
||||
if (options?.resolveReplySequence) {
|
||||
const resolved = options.resolveReplySequence(id);
|
||||
if (typeof resolved === 'number' && resolved > 0) {
|
||||
const element: MessageElement = {
|
||||
type: 'reply',
|
||||
replySeq: resolved,
|
||||
replyMessageId: id // Keep the original messageId for logging
|
||||
};
|
||||
|
||||
// Try to get additional meta info for better reply display
|
||||
if (options?.resolveReplyMeta) {
|
||||
const meta = options.resolveReplyMeta(id);
|
||||
if (meta) {
|
||||
element.replySenderUin = meta.senderUin;
|
||||
element.replyTime = meta.time;
|
||||
element.replyRandom = meta.random;
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
}
|
||||
|
||||
// Backward-compatible path: allow direct seq reply IDs.
|
||||
return id > 0 ? { type: 'reply', replySeq: id } : null;
|
||||
}
|
||||
case 'image': {
|
||||
return {
|
||||
type: 'image',
|
||||
url: String(data.file ?? data.url ?? data.path ?? data.media ?? ''),
|
||||
flash: data.type === 'flash',
|
||||
subType: intOr(data.subType, 0),
|
||||
summary: data.summary ? String(data.summary) : undefined,
|
||||
};
|
||||
}
|
||||
case 'record': {
|
||||
const source = String(data.file ?? data.url ?? data.path ?? data.media ?? '');
|
||||
if (!source) return null;
|
||||
return {
|
||||
type: 'record',
|
||||
url: source,
|
||||
};
|
||||
}
|
||||
case 'video': {
|
||||
const source = String(data.file ?? data.url ?? data.path ?? data.media ?? '');
|
||||
if (!source) return null;
|
||||
return {
|
||||
type: 'video',
|
||||
url: source,
|
||||
thumbUrl: data.thumb ? String(data.thumb) : undefined,
|
||||
};
|
||||
}
|
||||
case 'json': {
|
||||
return {
|
||||
type: 'json',
|
||||
text: String(data.data ?? ''),
|
||||
};
|
||||
}
|
||||
case 'xml': {
|
||||
return {
|
||||
type: 'xml',
|
||||
text: String(data.data ?? ''),
|
||||
subType: intOr(data.id, 0),
|
||||
};
|
||||
}
|
||||
case 'poke': {
|
||||
return {
|
||||
type: 'poke',
|
||||
faceId: intOr(data.type ?? data.id, 0),
|
||||
};
|
||||
}
|
||||
case 'forward': {
|
||||
return {
|
||||
type: 'forward',
|
||||
resId: String(data.id ?? ''),
|
||||
};
|
||||
}
|
||||
case 'node': {
|
||||
// Fake forward node segment — store the raw data for later processing
|
||||
// The content field may be a segment array, a single segment, or a CQ string
|
||||
const name = String(data.nickname ?? data.name ?? '');
|
||||
return {
|
||||
type: 'node',
|
||||
targetUin: intOr(data.user_id ?? data.uin, 0),
|
||||
text: name,
|
||||
// Raw content is stored as JSON string in resId for later processing
|
||||
resId: JSON.stringify(data.content ?? ''),
|
||||
};
|
||||
}
|
||||
case 'markdown': {
|
||||
return {
|
||||
type: 'markdown',
|
||||
text: String(data.content ?? ''),
|
||||
};
|
||||
}
|
||||
case 'share': {
|
||||
// Link share — map to json card message
|
||||
const url = String(data.url ?? '');
|
||||
const title = String(data.title ?? '');
|
||||
const content = String(data.content ?? '');
|
||||
const image = String(data.image ?? '');
|
||||
const jsonData = JSON.stringify({
|
||||
app: 'com.tencent.structmsg',
|
||||
view: 'news',
|
||||
prompt: title,
|
||||
meta: { news: { title, desc: content, jumpUrl: url, preview: image } },
|
||||
});
|
||||
return { type: 'json', text: jsonData };
|
||||
}
|
||||
case 'music': {
|
||||
// Music share — uses external signing service (NapCat-compatible)
|
||||
const musicType = String(data.type ?? '');
|
||||
const signUrl = options?.musicSignUrl || 'https://ss.xingzhige.com/music_card/card';
|
||||
try {
|
||||
let postData: Record<string, unknown>;
|
||||
if (musicType === 'custom') {
|
||||
postData = {
|
||||
type: 'custom',
|
||||
id: undefined,
|
||||
url: String(data.url ?? ''),
|
||||
audio: String(data.audio ?? ''),
|
||||
title: String(data.title ?? ''),
|
||||
image: String(data.image ?? ''),
|
||||
singer: String(data.content ?? ''),
|
||||
};
|
||||
} else {
|
||||
postData = { type: musicType, id: String(data.id ?? '') };
|
||||
}
|
||||
const resp = await fetch(signUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(postData),
|
||||
});
|
||||
if (!resp.ok) throw new Error(`music sign HTTP ${resp.status}`);
|
||||
const musicJson = await resp.text();
|
||||
return { type: 'json', text: musicJson };
|
||||
} catch (e) {
|
||||
log.warn('music sign failed: %s, falling back to local card', e instanceof Error ? e.message : String(e));
|
||||
// Fallback: build a basic card locally
|
||||
const title = String(data.title ?? 'Music');
|
||||
const jsonData = JSON.stringify({
|
||||
app: 'com.tencent.structmsg',
|
||||
view: 'music',
|
||||
prompt: `[音乐]${title}`,
|
||||
meta: {
|
||||
music: {
|
||||
title,
|
||||
desc: String(data.content ?? ''),
|
||||
jumpUrl: String(data.url ?? ''),
|
||||
musicUrl: String(data.audio ?? ''),
|
||||
preview: String(data.image ?? ''),
|
||||
},
|
||||
},
|
||||
});
|
||||
return { type: 'json', text: jsonData };
|
||||
}
|
||||
}
|
||||
case 'location': {
|
||||
// Location — map to json card
|
||||
const lat = String(data.lat ?? '');
|
||||
const lon = String(data.lon ?? '');
|
||||
const title = String(data.title ?? '位置');
|
||||
const content = String(data.content ?? `${lat},${lon}`);
|
||||
const jsonData = JSON.stringify({
|
||||
app: 'com.tencent.map',
|
||||
view: 'LocationShare',
|
||||
prompt: `[位置]${title}`,
|
||||
meta: { Location: { lat, lng: lon, title, address: content } },
|
||||
});
|
||||
return { type: 'json', text: jsonData };
|
||||
}
|
||||
case 'contact': {
|
||||
// Contact card — map to json card
|
||||
const contactType = String(data.type ?? 'qq');
|
||||
const contactId = String(data.id ?? '');
|
||||
const jsonData = JSON.stringify({
|
||||
app: 'com.tencent.contact.lua',
|
||||
view: 'contact',
|
||||
prompt: `[推荐${contactType === 'group' ? '群' : '好友'}]`,
|
||||
meta: { contact: { type: contactType, id: contactId } },
|
||||
});
|
||||
return { type: 'json', text: jsonData };
|
||||
}
|
||||
case 'rps': {
|
||||
// Rock-paper-scissors — map to dice-like face
|
||||
return { type: 'face', faceId: 359 };
|
||||
}
|
||||
case 'dice': {
|
||||
// Dice — map to dice face
|
||||
return { type: 'face', faceId: 358 };
|
||||
}
|
||||
case 'shake': {
|
||||
// Window shake — map to poke
|
||||
return { type: 'poke', faceId: 1 };
|
||||
}
|
||||
case 'anonymous': {
|
||||
// Anonymous flag — ignored during send, the protocol handles anonymity
|
||||
return null;
|
||||
}
|
||||
case 'file': {
|
||||
// OneBot11 file segment referencing a previously-uploaded file
|
||||
// (returned by `upload_group_file` / `upload_private_file`).
|
||||
// We deliberately do NOT support `{file: '/path'}` here because
|
||||
// the upload pipeline is async and lives behind the bridge —
|
||||
// the element-builder runs synchronously over the parsed
|
||||
// message. Callers that want upload-and-send should use the
|
||||
// dedicated `upload_*` actions which now publish the file in
|
||||
// chat atomically (`actions/group-file.ts`).
|
||||
const fileId = String(data.file_id ?? data.fileId ?? '').trim();
|
||||
if (!fileId) {
|
||||
log.warn('[MsgParser] file segment without file_id is unsupported (upload first via upload_group_file / upload_private_file)');
|
||||
return null;
|
||||
}
|
||||
const fileName = String(data.name ?? data.filename ?? data.fileName ?? '').trim();
|
||||
const fileSize = intOr(data.size ?? data.fileSize, 0);
|
||||
const md5Hex = String(data.md5 ?? data.md5Hex ?? '').trim();
|
||||
const sha1Hex = String(data.sha1 ?? data.sha1Hex ?? '').trim();
|
||||
// file_hash is the c2c-file server-issued addon string — needed
|
||||
// on the wire when re-sending a previously-uploaded c2c file.
|
||||
// get_private_file_url callers already pass it through, so honour
|
||||
// the same field naming here.
|
||||
const fileHash = String(data.file_hash ?? data.fileHash ?? '').trim();
|
||||
const elem: MessageElement = { type: 'file', fileId };
|
||||
if (fileName) elem.fileName = fileName;
|
||||
if (fileSize > 0) elem.fileSize = fileSize;
|
||||
if (md5Hex) elem.md5Hex = md5Hex;
|
||||
if (sha1Hex) elem.sha1Hex = sha1Hex;
|
||||
if (fileHash) elem.fileHash = fileHash;
|
||||
return elem;
|
||||
}
|
||||
default:
|
||||
console.warn(`[MsgParser] unsupported segment type: ${type}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function segmentPayload(seg: MessageSegment): Record<string, unknown> {
|
||||
const topLevel = { ...seg } as Record<string, unknown>;
|
||||
delete topLevel.type;
|
||||
delete topLevel.data;
|
||||
const nested = (seg.data && typeof seg.data === 'object' && !Array.isArray(seg.data))
|
||||
? seg.data
|
||||
: {};
|
||||
return { ...topLevel, ...nested };
|
||||
}
|
||||
|
||||
// --- Public API ---
|
||||
|
||||
/**
|
||||
* Parse an OneBot11 message value into internal MessageElement array.
|
||||
* Supports:
|
||||
* - string (plain text or CQ code string)
|
||||
* - array of { type, data } segments (JSON segment format)
|
||||
*
|
||||
* If autoEscape is true, the string is treated as plain text (no CQ parsing).
|
||||
*/
|
||||
export async function parseMessage(message: JsonValue, autoEscape: boolean, options?: ParseMessageOptions): Promise<MessageElement[]> {
|
||||
if (typeof message === 'string') {
|
||||
if (autoEscape) {
|
||||
return message ? [{ type: 'text', text: message }] : [];
|
||||
}
|
||||
return parseFromCQString(message, options);
|
||||
}
|
||||
|
||||
if (isSegmentArray(message)) {
|
||||
const elements: MessageElement[] = [];
|
||||
for (const seg of message) {
|
||||
const data = segmentPayload(seg);
|
||||
const elem = await segmentToElement(seg.type, data, options);
|
||||
if (elem) elements.push(elem);
|
||||
}
|
||||
return elements;
|
||||
}
|
||||
|
||||
// Single segment object
|
||||
if (typeof message === 'object' && message !== null && !Array.isArray(message)) {
|
||||
const seg = message as unknown as MessageSegment;
|
||||
if (seg.type) {
|
||||
const data = segmentPayload(seg);
|
||||
const elem = await segmentToElement(seg.type, data, options);
|
||||
return elem ? [elem] : [];
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
Reference in new issue
Block a user