fix(chat): isolate pending flows and cancel stale requests
This commit is contained in:
@@ -4,6 +4,7 @@ import { loadChatRepository } from "@/data/repositories/chat_repository_loader";
|
||||
import { Logger } from "@/utils/logger";
|
||||
import { Result } from "@/utils/result";
|
||||
import { todayString } from "@/utils/date";
|
||||
import { isAbortError } from "@/utils/abort";
|
||||
|
||||
import { CHAT_HISTORY_LIMIT } from "./helper/history";
|
||||
import { localMessagesToUi } from "./helper/message-mappers";
|
||||
@@ -81,6 +82,7 @@ export async function syncNetworkHistory(
|
||||
characterId: string,
|
||||
localCount: number,
|
||||
cacheIdentity: string | null,
|
||||
signal?: AbortSignal,
|
||||
): Promise<NetworkHistorySyncOutput | null> {
|
||||
const chatRepo = await loadChatRepository();
|
||||
const greetingMessage = createGreetingMessage();
|
||||
@@ -89,13 +91,16 @@ export async function syncNetworkHistory(
|
||||
characterId,
|
||||
CHAT_HISTORY_LIMIT,
|
||||
0,
|
||||
{ signal },
|
||||
);
|
||||
if (Result.isErr(networkResult)) {
|
||||
if (isAbortError(networkResult.error)) throw networkResult.error;
|
||||
log.error("[chat-machine] loadHistory NETWORK FAILED", {
|
||||
error: networkResult.error,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
signal?.throwIfAborted();
|
||||
|
||||
const networkUi = localMessagesToUi(networkResult.data.messages);
|
||||
log.debug("[chat-machine] loadHistory NETWORK DONE", {
|
||||
@@ -144,6 +149,7 @@ export async function syncNetworkHistory(
|
||||
*/
|
||||
export async function readAndSyncHistory(
|
||||
characterId: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<ReadAndSyncHistoryOutput> {
|
||||
const cacheIdentity = await resolveHistoryCacheIdentity(characterId);
|
||||
const localSnapshot = await readLocalHistorySnapshot(cacheIdentity);
|
||||
@@ -151,6 +157,7 @@ export async function readAndSyncHistory(
|
||||
characterId,
|
||||
localSnapshot.localCount,
|
||||
cacheIdentity,
|
||||
signal,
|
||||
);
|
||||
if (networkSnapshot) return networkSnapshot;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user