fix(chat): isolate cached data by identity

Scope message and media caches to the active user or guest identity. Preserve identity snapshots across async chat flows and discard legacy unscoped message data during the IndexedDB upgrade.
This commit is contained in:
2026-07-13 16:54:34 +08:00
parent 7c69b6cd8a
commit b126b83c4c
14 changed files with 601 additions and 133 deletions
+8 -1
View File
@@ -4,6 +4,7 @@ import { ExceptionHandler } from "@/core/errors";
import { MessageQueue } from "@/core/net/message-queue";
import type { ChatSendResponse } from "@/data/dto/chat";
import { getChatRepository } from "@/data/repositories/chat_repository";
import { resolveChatCacheOwnerKey } from "@/data/repositories/chat_cache_identity";
import { Logger, Result, todayString } from "@/utils";
import type { ChatEvent } from "./chat-events";
@@ -162,6 +163,7 @@ async function sendMessageViaHttp(content: string): Promise<{
reply: UiMessage | null;
}> {
const chatRepo = getChatRepository();
const cacheIdentityResult = await resolveChatCacheOwnerKey();
const result = await chatRepo.sendMessage(content);
if (Result.isErr(result)) {
log.error("[chat-machine] sendMessageHttpActor failed", {
@@ -169,7 +171,12 @@ async function sendMessageViaHttp(content: string): Promise<{
});
throw result.error;
}
void chatRepo.prefetchMediaForSendResponse(result.data);
if (Result.isOk(cacheIdentityResult)) {
void chatRepo.prefetchMediaForSendResponse(
result.data,
cacheIdentityResult.data,
);
}
const isInsufficientCredits =
result.data.canSendMessage === false &&
!hasRenderableSendResponse(result.data);