feat(model): add favorites system for model selection (#201)

* fix: sync model selection with OpenCode state

* feat(model): add favorites system for model selection

- Add toggleFavoriteModel API endpoint
- Add favorite toggle UI in ModelQuickSelect and ModelSelectDialog
- Show provider name prefix when model display names are ambiguous
- Fix directory scoping for getProviders API calls

feat(assistant): send welcome message on new session creation

- Send setup guidance message when creating new assistant session

---------

Co-authored-by: Chris Scott <>
This commit is contained in:
Chris ScottandChris Scott authored and GitHub committed 2026-04-26 21:49:13 -04:00
1 parent 3dd8e34f98
commit e6ff8733cf
11 files changed
+470 -80

No files matched your search

+67 -6
View File
@@ -1,27 +1,33 @@
import { useEffect } from 'react'
import { useQuery } from '@tanstack/react-query'
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { useConfig } from './useOpenCode'
import { useOpenCodeClient } from './useOpenCode'
import { useModelStore, type ModelSelection } from '@/stores/modelStore'
import { getProviders } from '@/api/providers'
import { addOpenCodeRecentModel, getOpenCodeModelState, getProviders, toggleOpenCodeFavoriteModel } from '@/api/providers'
interface UseModelSelectionResult {
model: ModelSelection | null
modelString: string | null
recentModels: ModelSelection[]
favoriteModels: ModelSelection[]
setModel: (model: ModelSelection) => void
toggleFavorite: (model: ModelSelection) => void
isModelStateLoading: boolean
}
const modelStateQueryKey = ['opencode', 'model-state']
export function useModelSelection(
opcodeUrl: string | null | undefined,
directory?: string
): UseModelSelectionResult {
const { data: config } = useConfig(opcodeUrl, directory)
const client = useOpenCodeClient(opcodeUrl, directory)
const queryClient = useQueryClient()
const { data: providersData } = useQuery({
queryKey: ['opencode', 'providers', opcodeUrl, directory],
queryFn: () => getProviders(),
queryFn: () => getProviders(directory),
enabled: !!client,
staleTime: 30000,
})
@@ -29,19 +35,74 @@ export function useModelSelection(
const {
model,
recentModels,
setModel,
favoriteModels,
setModel: setStoreModel,
syncModelState,
toggleFavorite: toggleStoreFavorite,
validateAndSyncModel,
getModelString
} = useModelStore()
const { data: modelState, isLoading: isModelStateLoading } = useQuery({
queryKey: [...modelStateQueryKey, opcodeUrl, directory],
queryFn: () => getOpenCodeModelState(),
enabled: !!client,
staleTime: 30000,
placeholderData: keepPreviousData,
})
const updateRecentModel = useMutation({
mutationFn: addOpenCodeRecentModel,
onSuccess: (state) => {
syncModelState(state)
queryClient.setQueryData([...modelStateQueryKey, opcodeUrl, directory], state)
queryClient.invalidateQueries({ queryKey: [...modelStateQueryKey, opcodeUrl, directory] })
},
})
const updateFavoriteModel = useMutation({
mutationFn: toggleOpenCodeFavoriteModel,
onSuccess: (state) => {
syncModelState(state)
queryClient.setQueryData([...modelStateQueryKey, opcodeUrl, directory], state)
queryClient.invalidateQueries({ queryKey: [...modelStateQueryKey, opcodeUrl, directory] })
},
})
const defaultModelString = providersData?.providers
.map((provider) => {
const modelID = providersData.default[provider.id] || Object.keys(provider.models || {})[0]
return modelID ? `${provider.id}/${modelID}` : null
})
.find((value): value is string => Boolean(value))
useEffect(() => {
validateAndSyncModel(config?.model, providersData?.providers)
}, [config?.model, providersData, validateAndSyncModel])
validateAndSyncModel(config?.model || defaultModelString, providersData?.providers)
}, [config?.model, defaultModelString, providersData, validateAndSyncModel])
useEffect(() => {
if (modelState) {
syncModelState(modelState)
}
}, [modelState, syncModelState])
const setModel = (nextModel: ModelSelection) => {
setStoreModel(nextModel)
updateRecentModel.mutate(nextModel)
}
const toggleFavorite = (nextModel: ModelSelection) => {
toggleStoreFavorite(nextModel)
updateFavoriteModel.mutate(nextModel)
}
return {
model,
modelString: getModelString(),
recentModels,
favoriteModels,
setModel,
toggleFavorite,
isModelStateLoading,
}
}