refactor(characters): drive shared UI from active character

This commit is contained in:
2026-07-17 19:33:59 +08:00
parent 8bb1e21886
commit 7bd5defa5e
42 changed files with 335 additions and 118 deletions
+12 -8
View File
@@ -30,12 +30,9 @@ export type LocalHistorySnapshotOutput = {
export type NetworkHistorySyncOutput = ReadAndSyncHistoryOutput;
export function createGreetingMessage(): UiMessage {
export function createGreetingMessage(content: string): UiMessage {
return {
content:
"You're here! Facebook is so restrictive, " +
"there were things I couldn't say there. " +
"Finally I can relax. How was your day out?",
content,
isFromAI: true,
date: todayString(),
isSynthetic: true,
@@ -51,9 +48,10 @@ export async function resolveHistoryCacheIdentity(
export async function readLocalHistorySnapshot(
cacheIdentity: string | null,
emptyChatGreeting: string,
): Promise<LocalHistorySnapshotOutput> {
const chatRepo = await loadChatRepository();
const greetingMessage = createGreetingMessage();
const greetingMessage = createGreetingMessage(emptyChatGreeting);
const localResult = cacheIdentity
? await chatRepo.getLocalMessages(cacheIdentity)
@@ -82,10 +80,11 @@ export async function syncNetworkHistory(
characterId: string,
localCount: number,
cacheIdentity: string | null,
emptyChatGreeting: string,
signal?: AbortSignal,
): Promise<NetworkHistorySyncOutput | null> {
const chatRepo = await loadChatRepository();
const greetingMessage = createGreetingMessage();
const greetingMessage = createGreetingMessage(emptyChatGreeting);
const networkResult = await chatRepo.getHistory(
characterId,
@@ -149,14 +148,19 @@ export async function syncNetworkHistory(
*/
export async function readAndSyncHistory(
characterId: string,
emptyChatGreeting: string,
signal?: AbortSignal,
): Promise<ReadAndSyncHistoryOutput> {
const cacheIdentity = await resolveHistoryCacheIdentity(characterId);
const localSnapshot = await readLocalHistorySnapshot(cacheIdentity);
const localSnapshot = await readLocalHistorySnapshot(
cacheIdentity,
emptyChatGreeting,
);
const networkSnapshot = await syncNetworkHistory(
characterId,
localSnapshot.localCount,
cacheIdentity,
emptyChatGreeting,
signal,
);
if (networkSnapshot) return networkSnapshot;