fix: 超大会话索引改为分批流式,修复回填时主进程堆溢出闪退 #75

Fixes https://github.com/Wxw-Gu/TraceMemo/issues/75
This commit is contained in:
Wxw-Gu committed 2026-11-20 14:17:24 +08:00
1 parent b86582e606
commit d436b064c3
41 files changed
+4214 -400

No files matched your search

+52
View File
@@ -851,6 +851,58 @@ export async function listMessagesAsync(
}
}
/**
* 按页读取一个会话的消息:不把整个会话读进数组,格式化走同一个 `listSourceMessages`。
*/
export async function* iterateMessagesAsync(
userMd5: string,
options: { pageSize?: number; sinceMs?: number } = {}
): AsyncGenerator<{ messages: FormattedMessage[]; failedStores: string[] }> {
if (!dbRef) return
const requestId = nextListMessagesRequestId()
const client = dbRef.getWcdb4Client()
for await (const page of client.iterateMessagePagesAsync(userMd5, {
pageSize: options.pageSize,
sinceSec: options.sinceMs ? Math.floor(options.sinceMs / 1000) : undefined,
requestId
})) {
if (page.failedStores.length) {
yield { messages: [], failedStores: page.failedStores }
return
}
const messages = listSourceMessages(
userMd5,
undefined,
undefined,
undefined,
page.messages,
requestId
)
yield { messages, failedStores: [] }
}
}
/** 点查定位:前后文与单条语音都靠它,避免先读整个会话再 findIndex。 */
export function locateMessageAsync(
userMd5: string,
messageId: string
): Promise<
| { status: 'found'; location: { createTime: number; localId: number } }
| { status: 'not_found' | 'ambiguous' | 'unsupported' }
> {
if (!dbRef) return Promise.resolve({ status: 'not_found' as const })
return dbRef.getWcdb4Client().locateMessageAsync(userMd5, messageId)
}
/** 只读 COUNT(*);`count: null` 是有分片读不到,调用方必须显示「未知」而不是 0。 */
export function countConversationMessagesAsync(
userMd5: string,
options: { sinceMs?: number } = {}
): Promise<{ count: number | null; approximate: boolean; failedStores: string[] }> {
if (!dbRef) return Promise.resolve({ count: null, approximate: false, failedStores: [] })
return dbRef.getWcdb4Client().countConversationMessagesAsync(userMd5, options)
}
/**
* 只取**图片消息**(图片文字索引专用)。
*