feat(onebot): fetch real group history from server (#99)

get_group_msg_history was served only from the local observed-message store,
so it could never return more than what SnowLuma saw live (the "only 100
messages" symptom). It now fetches real history from the server.

- protocol: new SsoGetGroupMsg packet (trpc.msg.register_proxy.RegisterProxy
  .SsoGetGroupMsg, sequence-range fetch; fields per Lagrange). Each returned
  PushMsgBody is decoded by the existing group decoder via a factored-out
  buildContextFromMessage.
- core: MessageApi.getGroupHistory walks backward in small sequence windows
  with rate-limiting + data-volume guards — a process-wide throttle (≥300ms
  between sends), 30-seq chunks, ≤12 requests, ≤200 messages per call — so a
  hammering client can't make us flood the server and get kicked.
- onebot: get_group_msg_history resolves the anchor sequence (requested
  message_id or latest observed), fetches from the server, converts + returns,
  and persists each fetched message. Falls back to the local store on
  unknown anchor / empty / error.

Persisting fetched history also fixes replying to pre-injection messages:
the reply path resolves message_id→sequence via the store, which now contains
the fetched messages.

Tests: SsoGetGroupMsg request/response round-trip + decode reuse (protocol);
walk-back chunking/dedup/cap/floor (core).
This commit is contained in:
DrSmoothl committed 2026-06-14 19:11:03 +08:00
1 parent ee8eb928b3
commit 987ac09896
11 files changed
+419 -12

No files matched your search

+76 -1
View File
@@ -2,7 +2,7 @@ import { createLogger } from '@snowluma/common/logger';
import type { BridgeInterface } from '@snowluma/core/bridge-interface';
import type { ForwardNodePayload, MessageElement } from '@snowluma/protocol/events';
import type { MessageSendResult } from '../api-handler';
import { elementsToOneBotSegments } from '../event-converter';
import { convertEvent, elementsToOneBotSegments, type ConverterContext } from '../event-converter';
import { segmentsToRawMessage } from '../helper/cq';
import type { OneBotInstanceContext } from '../instance-context';
import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id';
@@ -64,6 +64,81 @@ export async function getFriendMsgHistory(
.map(sanitizeMessageEventForApi);
}
// Deps the server-backed history fetch needs from the instance context.
interface GroupHistoryRef {
bridge: BridgeInterface;
messageStore: MessageStore;
converterCtx: ConverterContext;
selfId: number;
}
/**
* Group history, fetched from the server (`SsoGetGroupMsg`) instead of only the
* local observed-message store — so it can return messages SnowLuma never saw
* live, and (because each fetched message is persisted) reply / get_msg on old
* messages start working too. Resolves the anchor sequence from the requested
* message_id (or the latest observed message), then asks the bridge for the
* `count` messages ending there. Falls back to the local store if the server
* fetch is unavailable or empty.
*/
export async function getGroupHistory(
ref: GroupHistoryRef,
groupId: number,
messageId: number | undefined,
count: number | undefined,
): Promise<JsonObject[]> {
if (!Number.isInteger(groupId) || groupId <= 0) return [];
const want = normalizeHistoryCount(count);
let anchorSeq = 0;
if (Number.isInteger(messageId) && messageId !== 0) {
const meta = ref.messageStore.findMeta(messageId as number);
if (!meta || !meta.isGroup || meta.targetId !== groupId || meta.sequence <= 0) {
// Anchor we don't know — best effort from the local store.
return getGroupMsgHistory(ref.messageStore, groupId, messageId, count);
}
anchorSeq = meta.sequence;
} else {
const latest = ref.messageStore.listSessionEvents(true, groupId, 1);
anchorSeq = latest.length ? toHistInt(latest[latest.length - 1].message_seq) : 0;
}
if (anchorSeq > 0) {
try {
const events = await ref.bridge.apis.message.getGroupHistory(groupId, anchorSeq, want, ref.selfId);
const out: JsonObject[] = [];
for (const ev of events) {
const json = await convertEvent(ref.converterCtx, ev);
if (!json || json.message_type !== 'group') continue;
persistGroupHistoryEvent(ref.messageStore, json); // full event → reply/get_msg + future listing
out.push(sanitizeMessageEventForApi(json)); // sanitized for the API (matches the local path)
}
if (out.length > 0) return out;
} catch (err) {
log.warn('group history server fetch failed (%s); using local store', err instanceof Error ? err.message : String(err));
}
}
return getGroupMsgHistory(ref.messageStore, groupId, messageId, count);
}
function persistGroupHistoryEvent(store: MessageStore, event: JsonObject): void {
const messageId = toHistInt(event.message_id);
const sessionId = toHistInt(event.group_id);
const sequence = toHistInt(event.message_seq);
if (messageId === 0 || sessionId === 0) return;
store.storeEvent(messageId, true, sessionId, sequence, GROUP_MESSAGE_EVENT, event);
}
function toHistInt(value: unknown): number {
if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value);
if (typeof value === 'string' && value.trim()) {
const n = Number(value);
if (Number.isFinite(n)) return Math.trunc(n);
}
return 0;
}
export async function deleteMessage(bridge: BridgeInterface, meta: MessageMeta): Promise<void> {
if (meta.isGroup) {
await bridge.apis.message.recallGroup(meta.targetId, meta.sequence);