From 3db89d0bd08205845f12ab6f23b7872cac43a723 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Wed, 1 Jul 2026 19:12:05 +0800 Subject: [PATCH] refactor(protocol,core): converge self-uid resolution to one impl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 「取自己 uid」原本散成 5 份、3 种错误文案:core/apis/shared.ts::resolveSelfUid、 group-file/message 各内联手抄一份、get-like 跨包再抄一份。收敛成 protocol 单一 resolveSelfUid(ctx: Pick)。 阶段 1(行为不变): - 新增 protocol/self-uid.ts(依赖方向 core→protocol 要求权威实现下沉 protocol; Bridge 与 OIDB service Deps 都满足 Pick 切片)。 - core/apis/shared.ts 改为 re-export 它(现有 resolveSelfUid(bridge) 调用点不变)。 - get-like/group-file/message 三处内联手抄 → 全调权威实现。 - 唯一副作用:message 的错误文案统一为 'self uid is unavailable'(无测试依赖旧文案)。 flash-transfer/forward 的 ?? '' 空串 bug 留待阶段 2;group-admin 合成本地事件保持不动。 全仓 12 包 typecheck Done、protocol/core(471)/onebot(516) 全绿;对抗性 Agent 确认 toInt/Number 边界无实际差异(identity.uin 恒为正整数串)、5 处等价。 --- packages/core/src/bridge/apis/group-file.ts | 9 +----- packages/core/src/bridge/apis/message.ts | 8 ++--- packages/core/src/bridge/apis/shared.ts | 22 +++----------- packages/protocol/package.json | 4 +++ .../src/oidb-services/profile/get-like.ts | 15 ++-------- packages/protocol/src/self-uid.ts | 29 +++++++++++++++++++ 6 files changed, 42 insertions(+), 45 deletions(-) create mode 100644 packages/protocol/src/self-uid.ts diff --git a/packages/core/src/bridge/apis/group-file.ts b/packages/core/src/bridge/apis/group-file.ts index afd1b8fc..bf0923c0 100644 --- a/packages/core/src/bridge/apis/group-file.ts +++ b/packages/core/src/bridge/apis/group-file.ts @@ -366,14 +366,7 @@ export class GroupFileApi { if (!loaded.bytes.length) throw new Error('private file is empty'); const targetUid = await this.ctx.resolveUserUid(userId); - let selfUid = this.ctx.identity.selfUid; - if (!selfUid) { - const selfUin = toInt(this.ctx.identity.uin); - if (selfUin > 0) { - selfUid = await this.ctx.resolveUserUid(selfUin); - } - } - if (!selfUid) throw new Error('self uid is unavailable'); + const selfUid = await resolveSelfUid(this.ctx); const senderUin = toInt(this.ctx.identity.uin); if (senderUin <= 0) throw new Error('invalid self uin for private file upload'); diff --git a/packages/core/src/bridge/apis/message.ts b/packages/core/src/bridge/apis/message.ts index f727090f..3f5cd8b3 100644 --- a/packages/core/src/bridge/apis/message.ts +++ b/packages/core/src/bridge/apis/message.ts @@ -15,6 +15,7 @@ import { fetchC2cMessageRange, fetchGroupMessageRange } from '@snowluma/protocol import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import { createLogger } from '@snowluma/common/logger'; import type { BridgeContext } from '../bridge-context'; +import { resolveSelfUid } from './shared'; // `Bridge` is imported as a type only so we can narrow `ctx` back to // the concrete Bridge instance when passing it to `buildSendElems` // (which still takes `Bridge` because the highway upload helpers it @@ -259,12 +260,7 @@ export class MessageApi { // Resolve our own uid — needed both for the 0xE37_800 finalize and the // `field6` download-routing the receiver reads. - let selfUid = this.ctx.identity.selfUid; - if (!selfUid) { - const selfUin = Number.parseInt(this.ctx.identity.uin ?? '', 10); - if (Number.isFinite(selfUin) && selfUin > 0) selfUid = await this.ctx.resolveUserUid(selfUin); - } - if (!selfUid) throw new Error('self uid unavailable for c2c file send'); + const selfUid = await resolveSelfUid(this.ctx); // The file is already downloadable from the `NotOnlineFile` reference // alone — verified on a live account: a plain c2c file send (no field6) diff --git a/packages/core/src/bridge/apis/shared.ts b/packages/core/src/bridge/apis/shared.ts index 09ff7b8d..71d531a5 100644 --- a/packages/core/src/bridge/apis/shared.ts +++ b/packages/core/src/bridge/apis/shared.ts @@ -1,5 +1,3 @@ -import type { Bridge } from '../bridge'; - export function toInt(value: unknown): number { if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value); if (typeof value === 'bigint') { @@ -26,22 +24,10 @@ export function ensureRetCodeZero(operation: string, code: unknown, msg: unknown throw new Error(`${operation} failed: code=${retCode} msg=${text}`); } -/** - * Look up the bot's own UID. Cached on Identity once warmup populates - * `selfProfile`; forward/profile actions need this fast path before - * warmup completes. - */ -export async function resolveSelfUid(bridge: Bridge): Promise { - let selfUid = bridge.identity.selfUid; - if (selfUid) return selfUid; - - const selfUin = toInt(bridge.identity.uin); - if (selfUin <= 0) { - throw new Error('self uid is unavailable'); - } - selfUid = await bridge.resolveUserUid(selfUin); - return selfUid; -} +// The bot's-own-UID resolver is the single source of truth in @snowluma/protocol +// (a Bridge satisfies its BridgeContext slice). Re-exported here so the existing +// `resolveSelfUid(bridge)` call sites in this package keep importing from shared. +export { resolveSelfUid } from '@snowluma/protocol/self-uid'; /** * Index describing a server-side rich-media object (image / video / diff --git a/packages/protocol/package.json b/packages/protocol/package.json index 04ae27fa..dc636410 100644 --- a/packages/protocol/package.json +++ b/packages/protocol/package.json @@ -36,6 +36,10 @@ "types": "./src/element-manifest.ts", "default": "./src/element-manifest.ts" }, + "./self-uid": { + "types": "./src/self-uid.ts", + "default": "./src/self-uid.ts" + }, "./bridge-oidb": { "types": "./src/bridge-oidb.ts", "default": "./src/bridge-oidb.ts" diff --git a/packages/protocol/src/oidb-services/profile/get-like.ts b/packages/protocol/src/oidb-services/profile/get-like.ts index ffa94118..9f273753 100644 --- a/packages/protocol/src/oidb-services/profile/get-like.ts +++ b/packages/protocol/src/oidb-services/profile/get-like.ts @@ -8,6 +8,7 @@ import type { Oidb0x7edReq, Oidb0x7edResp } from '@snowluma/proto-defs/oidb-acti import { protobuf_decode, protobuf_encode } from '@snowluma/proton'; import type { BridgeContext } from '../../bridge-context'; import { invokeOidb, type OidbSender } from '../../oidb-service'; +import { resolveSelfUid } from '../../self-uid'; export interface LikeInfo { [key: string]: import('@snowluma/common/json').JsonValue; @@ -46,7 +47,7 @@ export namespace GetLike { export const serialize = async (ctx: Deps, p: Params): Promise => { const targetUid = p.userId ? await ctx.resolveUserUid(p.userId) - : await resolveSelf(ctx); + : await resolveSelfUid(ctx); if (!targetUid) throw new Error('target uid not found'); return { targetUid, @@ -89,15 +90,3 @@ export namespace GetLike { export const invoke = (deps: Deps, params: Params): Promise => invokeOidb(deps, GetLike, params); } - -async function resolveSelf(deps: Pick): Promise { - // Mirrors `apis/shared.ts::resolveSelfUid` — kept inline here so the - // namespace is self-contained against just the BridgeContext slice. - const cached = deps.identity.selfUid; - if (cached) return cached; - const selfUin = Number(deps.identity.uin); - if (!Number.isFinite(selfUin) || selfUin <= 0) { - throw new Error('self uid is unavailable'); - } - return deps.resolveUserUid(selfUin); -} diff --git a/packages/protocol/src/self-uid.ts b/packages/protocol/src/self-uid.ts new file mode 100644 index 00000000..19896cf6 --- /dev/null +++ b/packages/protocol/src/self-uid.ts @@ -0,0 +1,29 @@ +import type { BridgeContext } from './bridge-context'; + +/** + * Resolve the bot's own UID — the single source of truth for "what is my uid". + * + * Fast path: once warmup populates `selfProfile`, `identity.selfUid` is set and + * returns immediately. Before that, resolve via `resolveUserUid(selfUin)`. + * Throws when there is no self identity at all (uin missing / <= 0). + * + * Callers that put the uid on an outbound packet must route here rather than + * fall back to an empty string: an empty uid ships a broken packet. This used + * to be re-inlined in ContactsApi shared helpers, group-file, message, and + * get-like — the copies had drifted into three error messages and two silent + * `?? ''` empty-uid bugs before being converged here. + * + * Takes just the `BridgeContext` slice it needs, so both a full `Bridge` and a + * bare context (e.g. an OIDB service's Deps) satisfy it. + */ +export async function resolveSelfUid( + ctx: Pick, +): Promise { + const cached = ctx.identity.selfUid; + if (cached) return cached; + const selfUin = Number(ctx.identity.uin); + if (!Number.isFinite(selfUin) || selfUin <= 0) { + throw new Error('self uid is unavailable'); + } + return ctx.resolveUserUid(selfUin); +}