refactor(bridge): extract BridgeInterface as the OneBot-facing seam

OneBot used to depend on the concrete Bridge class — 778 lines, ~99
public methods including PID management, raw packet dispatch, and cmd
handler registration that no external consumer should touch. Add an
explicit BridgeInterface (bridge/bridge-interface.ts) enumerating only
the surface OneBot actually reads (verified by grepping every bridge.*
access in onebot/), and have Bridge `implements BridgeInterface` so the
compiler enforces alignment.

Net effect at runtime: zero — Bridge's methods, fields, and bodies are
unchanged; the interface is a TypeScript-only declaration. Net effect
for typing: the OneBot package (instance, instance-context,
instance-rkey, manager, media-url-resolver, and the modules under
onebot/modules/) now imports `BridgeInterface` instead of `Bridge`, so
tests / fakes can satisfy the interface without subclassing or pulling
in the SQLite/native bits.

In-tree bridge actions (actions/*, bridge-contacts, highway) keep their
concrete `Bridge` typing — they legitimately need sendRawPacket and
other low-level entry points that intentionally stay off the
interface.
This commit is contained in:
DrSmoothl committed 2026-05-15 01:12:40 +08:00
1 parent 59cdfa98a2
commit e6b609dd4e
11 files changed
+167 -35

No files matched your search

@@ -1,4 +1,4 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
import type { IdentityService } from '../../bridge/identity-service';
import type { OneBotInstanceContext } from '../instance-context';
import type { JsonObject } from '../types';
@@ -11,7 +11,7 @@ export function getLoginInfo(ref: OneBotInstanceContext): { userId: number; nick
// Keep refreshes scoped. A broad "refresh all members in all groups" call can
// turn one OneBot request into N OIDB calls, which is risky for busy clients.
async function refreshSingleGroupMembers(bridge: Bridge, groupId: number): Promise<void> {
async function refreshSingleGroupMembers(bridge: BridgeInterface, groupId: number): Promise<void> {
try {
await bridge.fetchGroupMemberList(groupId);
} catch {
@@ -19,7 +19,7 @@ async function refreshSingleGroupMembers(bridge: Bridge, groupId: number): Promi
}
}
export async function getFriendList(bridge: Bridge): Promise<JsonObject[]> {
export async function getFriendList(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const friends = await bridge.fetchFriendList();
return friends.map(f => ({
@@ -37,7 +37,7 @@ export async function getFriendList(bridge: Bridge): Promise<JsonObject[]> {
}
export async function getGroupList(
bridge: Bridge,
bridge: BridgeInterface,
noCache?: boolean,
): Promise<JsonObject[]> {
try {
@@ -56,7 +56,7 @@ export async function getGroupList(
}
export async function getGroupInfo(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
noCache?: boolean,
): Promise<JsonObject | null> {
@@ -78,7 +78,7 @@ export async function getGroupInfo(
}
export async function getGroupMemberList(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
noCache?: boolean,
): Promise<JsonObject[]> {
@@ -96,7 +96,7 @@ export async function getGroupMemberList(
}
export async function getGroupMemberInfo(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
userId: number,
noCache?: boolean,
@@ -110,7 +110,7 @@ export async function getGroupMemberInfo(
}
export async function getGroupFiles(
bridge: Bridge,
bridge: BridgeInterface,
groupId: number,
folderId?: string,
): Promise<JsonObject> {
@@ -142,7 +142,7 @@ export async function getGroupFiles(
}
export async function getStrangerInfo(
bridge: Bridge,
bridge: BridgeInterface,
userId: number,
): Promise<JsonObject | null> {
try {
@@ -165,7 +165,7 @@ export async function getStrangerInfo(
}
}
export async function getGroupSystemMessages(bridge: Bridge): Promise<JsonObject[]> {
export async function getGroupSystemMessages(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const reqs = await bridge.fetchGroupRequests();
return reqs.map(r => ({
@@ -182,7 +182,7 @@ export async function getGroupSystemMessages(bridge: Bridge): Promise<JsonObject
}
}
export async function getDownloadRKeys(bridge: Bridge): Promise<JsonObject[]> {
export async function getDownloadRKeys(bridge: BridgeInterface): Promise<JsonObject[]> {
try {
const rkeys = await bridge.fetchDownloadRKeys();
return rkeys.map(r => ({
@@ -1,4 +1,4 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
import { createLogger } from '../../utils/logger';
import type { MediaStore } from '../media-store';
import type { JsonObject } from '../types';
@@ -21,7 +21,7 @@ export async function getImageInfo(
}
export async function getRecordInfo(
bridge: Bridge,
bridge: BridgeInterface,
mediaStore: MediaStore,
file: string,
): Promise<JsonObject | null> {
@@ -1,4 +1,4 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
import type { ForwardNodePayload, MessageElement } from '../../bridge/events';
import { createLogger } from '../../utils/logger';
import type { MessageSendResult } from '../api-handler';
@@ -63,7 +63,7 @@ export async function getFriendMsgHistory(
.map(sanitizeMessageEventForApi);
}
export async function deleteMessage(bridge: Bridge, meta: MessageMeta): Promise<void> {
export async function deleteMessage(bridge: BridgeInterface, meta: MessageMeta): Promise<void> {
if (meta.isGroup) {
await bridge.recallGroupMessage(meta.targetId, meta.sequence);
} else {
@@ -78,7 +78,7 @@ export async function deleteMessage(bridge: Bridge, meta: MessageMeta): Promise<
}
export async function setEssenceMessage(
bridge: Bridge,
bridge: BridgeInterface,
messageStore: MessageStore,
messageId: number,
enable: boolean,
@@ -1,7 +1,7 @@
import type { Bridge } from '../../bridge/bridge';
import type { BridgeInterface } from '../../bridge/bridge-interface';
export async function handleGroupAddRequest(
bridge: Bridge,
bridge: BridgeInterface,
flag: string,
approve: boolean,
reason: string,