feat(web-ai): persist chat history locally
This commit is contained in:
@@ -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));
|
||||
}
|
||||
@@ -69,12 +69,22 @@ export type LocalSyncInboxRecord = {
|
||||
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 {
|
||||
declare tasks: Table<LocalTaskRecord, string>;
|
||||
declare opLogs: Table<LocalOpLogRecord, string>;
|
||||
declare taskDrafts: Table<LocalTaskDraftRecord, string>;
|
||||
declare syncStates: Table<LocalSyncStateRecord, string>;
|
||||
declare syncInbox: Table<LocalSyncInboxRecord, string>;
|
||||
declare aiChatSessions: Table<LocalAiChatSessionRecord, string>;
|
||||
|
||||
constructor() {
|
||||
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.opLogs = this.table("op_logs");
|
||||
this.taskDrafts = this.table("task_drafts");
|
||||
this.syncStates = this.table("sync_states");
|
||||
this.syncInbox = this.table("sync_inbox");
|
||||
this.aiChatSessions = this.table("ai_chat_sessions");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user