feat(chat): persist model per thread and auto-load on thread switch
This commit is contained in:
parent
536a735acc
commit
761953b50e
4 changed files with 54 additions and 1 deletions
|
|
@ -358,6 +358,41 @@ export function ChatPage(): ReactElement {
|
|||
setViewBeforeCompare(null);
|
||||
}, [viewBeforeCompare]);
|
||||
|
||||
const handleThreadSelect = useCallback(
|
||||
(nextView: ChatView) => {
|
||||
setView(nextView);
|
||||
|
||||
const threadId =
|
||||
nextView.mode === "single" ? nextView.threadId : undefined;
|
||||
const pairId =
|
||||
nextView.mode === "compare" ? nextView.pairId : undefined;
|
||||
|
||||
void (async () => {
|
||||
let thread: import("./types").ThreadRecord | undefined;
|
||||
if (threadId) {
|
||||
thread = await db.threads.get(threadId);
|
||||
} else if (pairId) {
|
||||
thread = await db.threads
|
||||
.where("pairId")
|
||||
.equals(pairId)
|
||||
.first();
|
||||
}
|
||||
const threadModelId = thread?.modelId;
|
||||
if (!threadModelId) return;
|
||||
|
||||
const currentCheckpoint =
|
||||
useChatRuntimeStore.getState().params.checkpoint;
|
||||
if (threadModelId === currentCheckpoint) return;
|
||||
|
||||
if (currentCheckpoint) {
|
||||
await ejectModel();
|
||||
}
|
||||
await selectModel({ id: threadModelId });
|
||||
})();
|
||||
},
|
||||
[ejectModel, selectModel],
|
||||
);
|
||||
|
||||
const models = useMemo<ModelOption[]>(
|
||||
() =>
|
||||
modelsFromStore.map((model) => ({
|
||||
|
|
@ -500,7 +535,7 @@ export function ChatPage(): ReactElement {
|
|||
<InlineSidebar>
|
||||
<ThreadSidebar
|
||||
view={view}
|
||||
onSelect={setView}
|
||||
onSelect={handleThreadSelect}
|
||||
onNewThread={handleNewThread}
|
||||
onNewCompare={handleNewCompare}
|
||||
showCompare={canCompare}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,20 @@ db.version(2)
|
|||
})
|
||||
.upgrade((tx) => tx.table("messages").clear());
|
||||
|
||||
db.version(3)
|
||||
.stores({
|
||||
threads: "id, modelType, pairId, archived, createdAt",
|
||||
messages: "id, threadId, createdAt",
|
||||
})
|
||||
.upgrade((tx) =>
|
||||
tx
|
||||
.table("threads")
|
||||
.toCollection()
|
||||
.modify((thread) => {
|
||||
if (!thread.modelId) thread.modelId = "";
|
||||
}),
|
||||
);
|
||||
|
||||
export { db };
|
||||
|
||||
export function useLiveQuery<T>(
|
||||
|
|
|
|||
|
|
@ -323,10 +323,13 @@ function createDexieAdapter(
|
|||
},
|
||||
|
||||
async initialize(threadId: string) {
|
||||
const currentModelId =
|
||||
useChatRuntimeStore.getState().params.checkpoint ?? "";
|
||||
await db.threads.add({
|
||||
id: threadId,
|
||||
title: "New Chat",
|
||||
modelType,
|
||||
modelId: currentModelId,
|
||||
pairId,
|
||||
archived: false,
|
||||
createdAt: Date.now(),
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ export interface ThreadRecord {
|
|||
id: string;
|
||||
title: string;
|
||||
modelType: ModelType;
|
||||
modelId?: string;
|
||||
pairId?: string;
|
||||
archived: boolean;
|
||||
createdAt: number;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue