feat(characters): support character-scoped conversations

This commit is contained in:
2026-07-17 11:42:31 +08:00
parent 93efcb6604
commit 2796010971
85 changed files with 1645 additions and 251 deletions
+17 -6
View File
@@ -1,5 +1,5 @@
import type { UiMessage } from "@/data/dto/chat";
import { resolveChatCacheOwnerKey } from "@/data/repositories/chat_cache_identity";
import { resolveChatConversationKey } from "@/data/repositories/chat_cache_identity";
import { loadChatRepository } from "@/data/repositories/chat_repository_loader";
import { Logger } from "@/utils/logger";
import { Result } from "@/utils/result";
@@ -41,8 +41,10 @@ export function createGreetingMessage(): UiMessage {
};
}
export async function resolveHistoryCacheIdentity(): Promise<string | null> {
const result = await resolveChatCacheOwnerKey();
export async function resolveHistoryCacheIdentity(
characterId: string,
): Promise<string | null> {
const result = await resolveChatConversationKey(characterId);
return Result.isOk(result) ? result.data : null;
}
@@ -76,13 +78,18 @@ export async function readLocalHistorySnapshot(
}
export async function syncNetworkHistory(
characterId: string,
localCount: number,
cacheIdentity: string | null,
): Promise<NetworkHistorySyncOutput | null> {
const chatRepo = await loadChatRepository();
const greetingMessage = createGreetingMessage();
const networkResult = await chatRepo.getHistory(CHAT_HISTORY_LIMIT, 0);
const networkResult = await chatRepo.getHistory(
characterId,
CHAT_HISTORY_LIMIT,
0,
);
if (Result.isErr(networkResult)) {
log.error("[chat-machine] loadHistory NETWORK FAILED", {
error: networkResult.error,
@@ -97,6 +104,7 @@ export async function syncNetworkHistory(
if (cacheIdentity) {
void chatRepo.prefetchMediaForMessages(
networkResult.data.messages,
characterId,
cacheIdentity,
);
}
@@ -134,10 +142,13 @@ export async function syncNetworkHistory(
* 2. Read network history as the authoritative source.
* 3. Overwrite local history with network data.
*/
export async function readAndSyncHistory(): Promise<ReadAndSyncHistoryOutput> {
const cacheIdentity = await resolveHistoryCacheIdentity();
export async function readAndSyncHistory(
characterId: string,
): Promise<ReadAndSyncHistoryOutput> {
const cacheIdentity = await resolveHistoryCacheIdentity(characterId);
const localSnapshot = await readLocalHistorySnapshot(cacheIdentity);
const networkSnapshot = await syncNetworkHistory(
characterId,
localSnapshot.localCount,
cacheIdentity,
);