feat(web-ai): persist chat history locally

This commit is contained in:
2026-04-06 15:58:00 +08:00
parent 4c6aeb3e6c
commit 1ea483016f
3 changed files with 181 additions and 6 deletions
+70 -6
View File
@@ -19,6 +19,12 @@ import {
type WebAiChannel, type WebAiChannel,
WebAiApiError WebAiApiError
} from "@/services/ai-api"; } from "@/services/ai-api";
import {
deleteLocalAiChatSession,
listLocalAiChatSessions,
saveLocalAiChatSession,
type LocalAiChatMessageRecord
} from "@/services/local-ai-chat-repo";
import type { WebSession } from "@/services/session-storage"; import type { WebSession } from "@/services/session-storage";
import { CHANNEL_META, CHANNEL_ORDER } from "@/components/ai/ai-shared"; import { CHANNEL_META, CHANNEL_ORDER } from "@/components/ai/ai-shared";
@@ -26,12 +32,7 @@ type AiChatPageProps = {
session: WebSession; session: WebSession;
}; };
type AiMessageRecord = { type AiMessageRecord = LocalAiChatMessageRecord;
id: string;
role: "user" | "assistant" | "system";
content: string;
meta?: string;
};
function createEmptyMessages(): Record<WebAiChannel, AiMessageRecord[]> { function createEmptyMessages(): Record<WebAiChannel, AiMessageRecord[]> {
return { return {
@@ -78,6 +79,7 @@ export function AiChatPage({ session }: AiChatPageProps) {
const [draftMessage, setDraftMessage] = useState(""); const [draftMessage, setDraftMessage] = useState("");
const [sending, setSending] = useState(false); const [sending, setSending] = useState(false);
const [loadError, setLoadError] = useState<string | null>(null); const [loadError, setLoadError] = useState<string | null>(null);
const [historyLoaded, setHistoryLoaded] = useState(false);
const messagesEndRef = useRef<HTMLDivElement | null>(null); const messagesEndRef = useRef<HTMLDivElement | null>(null);
const bindingMap = useMemo(() => { const bindingMap = useMemo(() => {
@@ -112,6 +114,68 @@ export function AiChatPage({ session }: AiChatPageProps) {
void loadBindings(); void loadBindings();
}, [loadBindings]); }, [loadBindings]);
useEffect(() => {
let cancelled = false;
async function loadLocalHistory(): Promise<void> {
try {
const records = await listLocalAiChatSessions(session.user.id);
if (cancelled) {
return;
}
const nextMessages = createEmptyMessages();
const nextSessionIds = createEmptySessionIds();
for (const record of records) {
nextMessages[record.channel] = record.messages;
if (record.sessionId) {
nextSessionIds[record.channel] = record.sessionId;
}
}
setMessagesByChannel(nextMessages);
setSessionIds(nextSessionIds);
} finally {
if (!cancelled) {
setHistoryLoaded(true);
}
}
}
setHistoryLoaded(false);
void loadLocalHistory();
return () => {
cancelled = true;
};
}, [session.user.id]);
useEffect(() => {
if (!historyLoaded) {
return;
}
void Promise.all(
CHANNEL_ORDER.map(async (channel) => {
const messages = messagesByChannel[channel];
const sessionId = sessionIds[channel] ?? null;
if (messages.length === 0 && sessionId === null) {
await deleteLocalAiChatSession(session.user.id, channel);
return;
}
await saveLocalAiChatSession({
userId: session.user.id,
channel,
sessionId,
messages
});
})
);
}, [historyLoaded, messagesByChannel, session.user.id, sessionIds]);
useEffect(() => { useEffect(() => {
messagesEndRef.current?.scrollIntoView({ messagesEndRef.current?.scrollIntoView({
block: "end", block: "end",
@@ -0,0 +1,91 @@
import { localDb, type LocalAiChatSessionRecord } from "@/services/local-db";
import type { WebAiChannel } from "@/services/ai-api";
export type LocalAiChatMessageRecord = {
id: string;
role: "user" | "assistant" | "system";
content: string;
meta?: string;
};
export type SaveLocalAiChatSessionInput = {
userId: string;
channel: WebAiChannel;
sessionId: string | null;
messages: LocalAiChatMessageRecord[];
};
export type LocalAiChatSessionSnapshot = {
channel: WebAiChannel;
sessionId: string | null;
messages: LocalAiChatMessageRecord[];
};
function createSessionKey(userId: string, channel: WebAiChannel): string {
return `${userId}:${channel}`;
}
function parseMessages(messagesJson: string): LocalAiChatMessageRecord[] {
try {
const parsed = JSON.parse(messagesJson) as unknown;
if (!Array.isArray(parsed)) {
return [];
}
return parsed.filter((item): item is LocalAiChatMessageRecord => {
if (!item || typeof item !== "object") {
return false;
}
const record = item as Record<string, unknown>;
return (
typeof record["id"] === "string" &&
(record["role"] === "user" ||
record["role"] === "assistant" ||
record["role"] === "system") &&
typeof record["content"] === "string" &&
(record["meta"] === undefined || typeof record["meta"] === "string")
);
});
} catch {
return [];
}
}
function toSnapshot(record: LocalAiChatSessionRecord): LocalAiChatSessionSnapshot {
return {
channel: record.channel,
sessionId: record.sessionId,
messages: parseMessages(record.messagesJson)
};
}
export async function listLocalAiChatSessions(
userId: string
): Promise<LocalAiChatSessionSnapshot[]> {
const records = await localDb.aiChatSessions.where("userId").equals(userId).toArray();
return records.map(toSnapshot);
}
export async function saveLocalAiChatSession(
input: SaveLocalAiChatSessionInput
): Promise<LocalAiChatSessionRecord> {
const record: LocalAiChatSessionRecord = {
key: createSessionKey(input.userId, input.channel),
userId: input.userId,
channel: input.channel,
sessionId: input.sessionId,
messagesJson: JSON.stringify(input.messages),
updatedAt: Date.now()
};
await localDb.aiChatSessions.put(record);
return record;
}
export async function deleteLocalAiChatSession(
userId: string,
channel: WebAiChannel
): Promise<void> {
await localDb.aiChatSessions.delete(createSessionKey(userId, channel));
}
+20
View File
@@ -69,12 +69,22 @@ export type LocalSyncInboxRecord = {
appliedAt: number | null; appliedAt: number | null;
}; };
export type LocalAiChatSessionRecord = {
key: string;
userId: string;
channel: "USER_KEY" | "ASTRBOT" | "PUBLIC_POOL";
sessionId: string | null;
messagesJson: string;
updatedAt: number;
};
class TodoLocalDb extends Dexie { class TodoLocalDb extends Dexie {
declare tasks: Table<LocalTaskRecord, string>; declare tasks: Table<LocalTaskRecord, string>;
declare opLogs: Table<LocalOpLogRecord, string>; declare opLogs: Table<LocalOpLogRecord, string>;
declare taskDrafts: Table<LocalTaskDraftRecord, string>; declare taskDrafts: Table<LocalTaskDraftRecord, string>;
declare syncStates: Table<LocalSyncStateRecord, string>; declare syncStates: Table<LocalSyncStateRecord, string>;
declare syncInbox: Table<LocalSyncInboxRecord, string>; declare syncInbox: Table<LocalSyncInboxRecord, string>;
declare aiChatSessions: Table<LocalAiChatSessionRecord, string>;
constructor() { constructor() {
super("todolist-web-db"); super("todolist-web-db");
@@ -117,11 +127,21 @@ class TodoLocalDb extends Dexie {
}); });
}); });
this.version(5).stores({
tasks: "&id,userId,status,priority,ddlAt,updatedAt,deletedAt",
op_logs: "&opId,entityId,entityType,action,clientTs,syncedAt",
task_drafts: "&taskId,userId,updatedAt",
sync_states: "&userId,updatedAt,lastSyncedAt",
sync_inbox: "&opId,userId,entityId,serverTs,appliedAt",
ai_chat_sessions: "&key,userId,channel,updatedAt"
});
this.tasks = this.table("tasks"); this.tasks = this.table("tasks");
this.opLogs = this.table("op_logs"); this.opLogs = this.table("op_logs");
this.taskDrafts = this.table("task_drafts"); this.taskDrafts = this.table("task_drafts");
this.syncStates = this.table("sync_states"); this.syncStates = this.table("sync_states");
this.syncInbox = this.table("sync_inbox"); this.syncInbox = this.table("sync_inbox");
this.aiChatSessions = this.table("ai_chat_sessions");
} }
} }