Skip to content

Commit 07560d8

Browse files
committed
feat(AI Assistant): remember last model used
1 parent b340d84 commit 07560d8

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

admin/constants/kv_store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { KVStoreKey } from "../types/kv_store.js";
22

3-
export const SETTINGS_KEYS: KVStoreKey[] = ['chat.suggestionsEnabled', 'ui.hasVisitedEasySetup', 'system.earlyAccess', 'ai.assistantCustomName'];
3+
export const SETTINGS_KEYS: KVStoreKey[] = ['chat.suggestionsEnabled', 'chat.lastModel', 'ui.hasVisitedEasySetup', 'system.earlyAccess', 'ai.assistantCustomName'];

admin/inertia/components/chat/index.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ChatMessage } from '../../../types/chat'
1010
import classNames from '~/lib/classNames'
1111
import { IconX } from '@tabler/icons-react'
1212
import { DEFAULT_QUERY_REWRITE_MODEL } from '../../../constants/ollama'
13+
import { useSystemSetting } from '~/hooks/useSystemSetting'
1314

1415
interface ChatProps {
1516
enabled: boolean
@@ -51,6 +52,8 @@ export default function Chat({
5152

5253
const activeSession = sessions.find((s) => s.id === activeSessionId)
5354

55+
const { data: lastModelSetting } = useSystemSetting({ key: 'chat.lastModel', enabled })
56+
5457
const { data: installedModels = [], isLoading: isLoadingModels } = useQuery({
5558
queryKey: ['installedModels'],
5659
queryFn: () => api.getInstalledModels(),
@@ -127,12 +130,24 @@ export default function Chat({
127130
},
128131
})
129132

130-
// Set first model as selected by default
133+
// Set default model: prefer last used model, fall back to first installed if last model not available
131134
useEffect(() => {
132135
if (installedModels.length > 0 && !selectedModel) {
133-
setSelectedModel(installedModels[0].name)
136+
const lastModel = lastModelSetting?.value as string | undefined
137+
if (lastModel && installedModels.some((m) => m.name === lastModel)) {
138+
setSelectedModel(lastModel)
139+
} else {
140+
setSelectedModel(installedModels[0].name)
141+
}
142+
}
143+
}, [installedModels, selectedModel, lastModelSetting])
144+
145+
// Persist model selection
146+
useEffect(() => {
147+
if (selectedModel) {
148+
api.updateSetting('chat.lastModel', selectedModel)
134149
}
135-
}, [installedModels, selectedModel])
150+
}, [selectedModel])
136151

137152
const handleNewChat = useCallback(() => {
138153
// Just clear the active session and messages - don't create a session yet

admin/types/kv_store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
export const KV_STORE_SCHEMA = {
33
'chat.suggestionsEnabled': 'boolean',
4+
'chat.lastModel': 'string',
45
'rag.docsEmbedded': 'boolean',
56
'system.updateAvailable': 'boolean',
67
'system.latestVersion': 'string',

0 commit comments

Comments
 (0)