feat(AI Assistant): remember last model used

This commit is contained in:
Jake Turner 2026-03-10 22:35:43 +00:00 committed by Jake Turner
parent 41c64fb50b
commit 6f0fae0033
3 changed files with 20 additions and 4 deletions

View File

@ -1,3 +1,3 @@
import { KVStoreKey } from "../types/kv_store.js"; import { KVStoreKey } from "../types/kv_store.js";
export const SETTINGS_KEYS: KVStoreKey[] = ['chat.suggestionsEnabled', 'ui.hasVisitedEasySetup', 'system.earlyAccess', 'ai.assistantCustomName']; export const SETTINGS_KEYS: KVStoreKey[] = ['chat.suggestionsEnabled', 'chat.lastModel', 'ui.hasVisitedEasySetup', 'system.earlyAccess', 'ai.assistantCustomName'];

View File

@ -10,6 +10,7 @@ import { ChatMessage } from '../../../types/chat'
import classNames from '~/lib/classNames' import classNames from '~/lib/classNames'
import { IconX } from '@tabler/icons-react' import { IconX } from '@tabler/icons-react'
import { DEFAULT_QUERY_REWRITE_MODEL } from '../../../constants/ollama' import { DEFAULT_QUERY_REWRITE_MODEL } from '../../../constants/ollama'
import { useSystemSetting } from '~/hooks/useSystemSetting'
interface ChatProps { interface ChatProps {
enabled: boolean enabled: boolean
@ -51,6 +52,8 @@ export default function Chat({
const activeSession = sessions.find((s) => s.id === activeSessionId) const activeSession = sessions.find((s) => s.id === activeSessionId)
const { data: lastModelSetting } = useSystemSetting({ key: 'chat.lastModel', enabled })
const { data: installedModels = [], isLoading: isLoadingModels } = useQuery({ const { data: installedModels = [], isLoading: isLoadingModels } = useQuery({
queryKey: ['installedModels'], queryKey: ['installedModels'],
queryFn: () => api.getInstalledModels(), queryFn: () => api.getInstalledModels(),
@ -127,12 +130,24 @@ export default function Chat({
}, },
}) })
// Set first model as selected by default // Set default model: prefer last used model, fall back to first installed if last model not available
useEffect(() => { useEffect(() => {
if (installedModels.length > 0 && !selectedModel) { if (installedModels.length > 0 && !selectedModel) {
setSelectedModel(installedModels[0].name) const lastModel = lastModelSetting?.value as string | undefined
if (lastModel && installedModels.some((m) => m.name === lastModel)) {
setSelectedModel(lastModel)
} else {
setSelectedModel(installedModels[0].name)
}
} }
}, [installedModels, selectedModel]) }, [installedModels, selectedModel, lastModelSetting])
// Persist model selection
useEffect(() => {
if (selectedModel) {
api.updateSetting('chat.lastModel', selectedModel)
}
}, [selectedModel])
const handleNewChat = useCallback(() => { const handleNewChat = useCallback(() => {
// Just clear the active session and messages - don't create a session yet // Just clear the active session and messages - don't create a session yet

View File

@ -1,6 +1,7 @@
export const KV_STORE_SCHEMA = { export const KV_STORE_SCHEMA = {
'chat.suggestionsEnabled': 'boolean', 'chat.suggestionsEnabled': 'boolean',
'chat.lastModel': 'string',
'rag.docsEmbedded': 'boolean', 'rag.docsEmbedded': 'boolean',
'system.updateAvailable': 'boolean', 'system.updateAvailable': 'boolean',
'system.latestVersion': 'string', 'system.latestVersion': 'string',