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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user