mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
feat(models): share favorites, recents and variants across linked servers
Model preferences are per user, as in the OpenCode TUI, so linked servers read and write the same model state as the local server. The picker still only lists models in the current server's catalog, and a failed preference load no longer blocks model selection.
This commit is contained in:
1 parent
a926991637
commit
0a520babc4
4 files changed
+92
-41
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { act, renderHook, waitFor } from '@testing-library/react'
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { useModelSelection, useOpenCodeDefaultModel } from './useModelSelection'
|
||||
import { modelStateQueryKey, useModelSelection, useOpenCodeDefaultModel } from './useModelSelection'
|
||||
import { useProviders } from './useProviders'
|
||||
import { locationAgentKey, useModelStore, type ModelSelection } from '@/stores/modelStore'
|
||||
import { OpenCodeServerProvider } from '@/contexts/OpenCodeServerContext'
|
||||
@@ -493,8 +493,7 @@ describe('useModelSelection', () => {
|
||||
models: [modelInfo('anthropic', 'claude-sonnet-4'), modelInfo('openai', 'gpt-4o')],
|
||||
}
|
||||
|
||||
const renderRemote = (directory = '/test') => {
|
||||
const queryClient = createTestQueryClient()
|
||||
const renderRemote = (directory = '/test', queryClient = createTestQueryClient()) => {
|
||||
return renderHook(
|
||||
() => useModelSelection(directory),
|
||||
{
|
||||
@@ -531,42 +530,95 @@ describe('useModelSelection', () => {
|
||||
).toBeUndefined()
|
||||
})
|
||||
|
||||
it('does not read or write the local model state', async () => {
|
||||
it('reads the shared model state and writes favorites and recents on a remote server', async () => {
|
||||
mockGetOpenCodeModelState.mockResolvedValue({
|
||||
recent: [
|
||||
{ providerID: 'openai', modelID: 'gpt-4o' },
|
||||
{ providerID: 'ghost', modelID: 'gone' },
|
||||
],
|
||||
favorite: [
|
||||
{ providerID: 'anthropic', modelID: 'claude-sonnet-4' },
|
||||
{ providerID: 'ghost', modelID: 'gone' },
|
||||
],
|
||||
variant: {},
|
||||
})
|
||||
|
||||
const { result } = renderRemote()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isModelReady).toBe(true)
|
||||
})
|
||||
|
||||
expect(mockGetOpenCodeModelState).toHaveBeenCalledTimes(1)
|
||||
expect(result.current.modelState).toBeDefined()
|
||||
await waitFor(() => {
|
||||
expect(result.current.recentModels).toEqual([{ providerID: 'openai', modelID: 'gpt-4o' }])
|
||||
})
|
||||
await waitFor(() => {
|
||||
expect(result.current.favoriteModels).toEqual([
|
||||
{ providerID: 'anthropic', modelID: 'claude-sonnet-4' },
|
||||
])
|
||||
})
|
||||
|
||||
act(() => {
|
||||
result.current.setModel({ providerID: 'openai', modelID: 'gpt-4o' })
|
||||
result.current.toggleFavorite({ providerID: 'openai', modelID: 'gpt-4o' })
|
||||
result.current.removeRecentModel({ providerID: 'openai', modelID: 'gpt-4o' })
|
||||
})
|
||||
|
||||
expect(mockGetOpenCodeModelState).not.toHaveBeenCalled()
|
||||
expect(mockAddOpenCodeRecentModel).not.toHaveBeenCalled()
|
||||
expect(mockToggleOpenCodeFavoriteModel).not.toHaveBeenCalled()
|
||||
expect(mockRemoveOpenCodeRecentModel).not.toHaveBeenCalled()
|
||||
expect(result.current.recentModels).toEqual([])
|
||||
expect(result.current.favoriteModels).toEqual([])
|
||||
expect(result.current.modelState).toBeUndefined()
|
||||
await waitFor(() => {
|
||||
expect(mockAddOpenCodeRecentModel).toHaveBeenCalledTimes(1)
|
||||
expect(mockToggleOpenCodeFavoriteModel).toHaveBeenCalledTimes(1)
|
||||
expect(mockRemoveOpenCodeRecentModel).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
expect(mockAddOpenCodeRecentModel.mock.calls[0][0]).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
|
||||
expect(mockToggleOpenCodeFavoriteModel.mock.calls[0][0]).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
|
||||
expect(mockRemoveOpenCodeRecentModel.mock.calls[0][0]).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
|
||||
})
|
||||
|
||||
it('ignores local recents in the selection order', async () => {
|
||||
mockGetOpenCodeConfiguredModel.mockResolvedValue('anthropic/claude-sonnet-4')
|
||||
it('reads the model state a local server populated from the same cache entry', async () => {
|
||||
const queryClient = createTestQueryClient()
|
||||
queryClient.setQueryData(modelStateQueryKey(), {
|
||||
recent: [{ providerID: 'openai', modelID: 'gpt-4o' }],
|
||||
favorite: [{ providerID: 'anthropic', modelID: 'claude-sonnet-4' }],
|
||||
variant: {},
|
||||
})
|
||||
|
||||
const { result } = renderRemote('/test', queryClient)
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.recentModels).toEqual([{ providerID: 'openai', modelID: 'gpt-4o' }])
|
||||
})
|
||||
expect(result.current.favoriteModels).toEqual([
|
||||
{ providerID: 'anthropic', modelID: 'claude-sonnet-4' },
|
||||
])
|
||||
expect(mockGetOpenCodeModelState).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('stays ready with empty preferences when the model state fails to load', async () => {
|
||||
mockGetOpenCodeModelState.mockRejectedValue(new Error('offline'))
|
||||
const { result } = renderRemote()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isModelReady).toBe(true)
|
||||
})
|
||||
expect(result.current.favoriteModels).toEqual([])
|
||||
expect(result.current.model).not.toBeNull()
|
||||
})
|
||||
|
||||
it('uses the shared recents in the selection order', async () => {
|
||||
mockGetOpenCodeModelState.mockResolvedValue({
|
||||
recent: [{ providerID: 'openai', modelID: 'gpt-4o' }],
|
||||
favorite: [],
|
||||
variant: { 'openai/gpt-4o': 'high' },
|
||||
variant: {},
|
||||
})
|
||||
|
||||
const { result } = renderRemote()
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.model).toEqual({ providerID: 'anthropic', modelID: 'claude-sonnet-4' })
|
||||
expect(result.current.model).toEqual({ providerID: 'openai', modelID: 'gpt-4o' })
|
||||
})
|
||||
expect(result.current.recentModels).toEqual([])
|
||||
expect(result.current.recentModels).toEqual([{ providerID: 'openai', modelID: 'gpt-4o' }])
|
||||
})
|
||||
|
||||
it('keeps the agent model ahead of the configured model', async () => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useMemo } from 'react'
|
||||
import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { locationAgentKey, useModelStore, type ActiveAgent, type ModelPick, type ModelSelection } from '@/stores/modelStore'
|
||||
import { isLocalOpenCodeServer, useOpenCodeServerId } from '@/contexts/OpenCodeServerContext'
|
||||
import { useOpenCodeServerId } from '@/contexts/OpenCodeServerContext'
|
||||
import { serverScopedKey } from '@/lib/serverScope'
|
||||
import { buildServerDirectoryKey } from '@opencode-manager/shared/schemas'
|
||||
import { useProviders } from './useProviders'
|
||||
@@ -62,13 +62,12 @@ export function modelStateQueryKey() {
|
||||
}
|
||||
|
||||
export function useOpenCodeModelState(_directory?: string, enabled = true) {
|
||||
const serverId = useOpenCodeServerId()
|
||||
return useQuery({
|
||||
queryKey: serverScopedKey(modelStateQueryKey(), serverId),
|
||||
queryKey: modelStateQueryKey(),
|
||||
queryFn: () => getOpenCodeModelState(),
|
||||
staleTime: 30000,
|
||||
placeholderData: keepPreviousData,
|
||||
enabled: enabled && isLocalOpenCodeServer(serverId),
|
||||
enabled,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -123,8 +122,7 @@ export function useModelStateMutation<TInput>(
|
||||
errorMessage: string,
|
||||
) {
|
||||
const queryClient = useQueryClient()
|
||||
const serverId = useOpenCodeServerId()
|
||||
const queryKey = serverScopedKey(modelStateQueryKey(), serverId)
|
||||
const queryKey = modelStateQueryKey()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: fetcher,
|
||||
@@ -222,8 +220,6 @@ export function useModelSelection(
|
||||
): UseModelSelectionResult {
|
||||
const { data: catalog, isPlaceholderData: catalogIsPlaceholder } = useProviders(directory)
|
||||
const serverId = useOpenCodeServerId()
|
||||
const isLocalServer = isLocalOpenCodeServer(serverId)
|
||||
|
||||
const activeAgent = useModelStore((state) => state.activeAgent)
|
||||
const newSessionPicks = useModelStore((state) => state.newSessionPicks)
|
||||
const sessionPicks = useModelStore((state) => state.sessionPicks)
|
||||
@@ -231,14 +227,13 @@ export function useModelSelection(
|
||||
const setSessionPick = useModelStore((state) => state.setSessionPick)
|
||||
const setActiveAgent = useModelStore((state) => state.setActiveAgent)
|
||||
|
||||
const { data: localModelState } = useOpenCodeModelState()
|
||||
const modelState = isLocalServer ? localModelState : undefined
|
||||
const { data: modelState, isError: modelStateFailed } = useOpenCodeModelState()
|
||||
const { data: configModelString } = useOpenCodeConfiguredModel(directory)
|
||||
|
||||
const isModelReady = Boolean(
|
||||
catalog &&
|
||||
!catalogIsPlaceholder &&
|
||||
(!isLocalServer || modelState !== undefined) &&
|
||||
(modelState !== undefined || modelStateFailed) &&
|
||||
configModelString !== undefined,
|
||||
)
|
||||
|
||||
@@ -353,16 +348,14 @@ export function useModelSelection(
|
||||
} else {
|
||||
setNewSessionPick(key, nextModel)
|
||||
}
|
||||
if (isLocalServer) updateRecentModel.mutate(nextModel)
|
||||
updateRecentModel.mutate(nextModel)
|
||||
}
|
||||
|
||||
const toggleFavorite = (nextModel: ModelSelection) => {
|
||||
if (!isLocalServer) return
|
||||
updateFavoriteModel.mutate(nextModel)
|
||||
}
|
||||
|
||||
const removeRecent = (nextModel: ModelSelection) => {
|
||||
if (!isLocalServer) return
|
||||
removeRecentMutation.mutate(nextModel)
|
||||
}
|
||||
|
||||
|
||||
@@ -334,7 +334,7 @@ describe('useVariants', () => {
|
||||
),
|
||||
})
|
||||
|
||||
it('keeps a remote session variant in the session pick without persisting it', async () => {
|
||||
it('persists a remote session variant in the session pick and via saveOpenCodeModelVariant', async () => {
|
||||
selectAgentModel()
|
||||
const { result } = renderRemoteVariants(createQueryClient(), {
|
||||
id: 's1',
|
||||
@@ -350,11 +350,15 @@ describe('useVariants', () => {
|
||||
|
||||
expect(useModelStore.getState().sessionPicks.s1[AGENT_ID].variant).toBe('high')
|
||||
await waitFor(() => expect(result.current.currentVariant).toBe('high'))
|
||||
expect(mockGetOpenCodeModelState).not.toHaveBeenCalled()
|
||||
expect(mockSaveOpenCodeModelVariant).not.toHaveBeenCalled()
|
||||
await waitFor(() => {
|
||||
expect(mockSaveOpenCodeModelVariant).toHaveBeenCalledWith(
|
||||
{ providerID: PROVIDER_ID, modelID: MODEL_ID },
|
||||
'high',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
it('does not persist a remote variant outside a session', async () => {
|
||||
it('persists a remote variant outside a session and drives the selection from the preference', async () => {
|
||||
selectAgentModel()
|
||||
const { result } = renderRemoteVariants(createQueryClient())
|
||||
|
||||
@@ -364,9 +368,14 @@ describe('useVariants', () => {
|
||||
result.current.setVariant('high')
|
||||
})
|
||||
|
||||
expect(mockSaveOpenCodeModelVariant).not.toHaveBeenCalled()
|
||||
await waitFor(() => expect(result.current.currentVariant).toBe('high'))
|
||||
expect(useModelStore.getState().newSessionPicks).toEqual({})
|
||||
expect(result.current.currentVariant).toBeUndefined()
|
||||
await waitFor(() => {
|
||||
expect(mockSaveOpenCodeModelVariant).toHaveBeenCalledWith(
|
||||
{ providerID: PROVIDER_ID, modelID: MODEL_ID },
|
||||
'high',
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,7 +1,6 @@
|
||||
import { useCallback, useMemo } from 'react'
|
||||
import { useModelSelection, useModelStateMutation, type ModelSelectionSession } from './useModelSelection'
|
||||
import { useModelStore, type ModelSelection } from '@/stores/modelStore'
|
||||
import { isLocalOpenCodeServer, useOpenCodeServerId } from '@/contexts/OpenCodeServerContext'
|
||||
import { saveOpenCodeModelVariant } from '@/api/providers'
|
||||
import {
|
||||
cycleModelVariant,
|
||||
@@ -28,8 +27,6 @@ export function useVariants(
|
||||
session?: ModelSelectionSession,
|
||||
): UseVariantsResult {
|
||||
const { model, selection, info } = useModelSelection(directory, session)
|
||||
const serverId = useOpenCodeServerId()
|
||||
const isLocalServer = isLocalOpenCodeServer(serverId)
|
||||
const activeAgent = useModelStore((state) => state.activeAgent)
|
||||
const setSessionPick = useModelStore((state) => state.setSessionPick)
|
||||
|
||||
@@ -52,9 +49,9 @@ export function useVariants(
|
||||
if (session?.id && activeAgent?.id) {
|
||||
setSessionPick(session.id, activeAgent.id, { ...model, variant: normalizeModelVariant(variant) })
|
||||
}
|
||||
if (isLocalServer) saveVariant.mutate({ model, value: variant })
|
||||
saveVariant.mutate({ model, value: variant })
|
||||
},
|
||||
[activeAgent?.id, isLocalServer, model, saveVariant, session?.id, setSessionPick],
|
||||
[activeAgent?.id, model, saveVariant, session?.id, setSessionPick],
|
||||
)
|
||||
|
||||
const cycleVariant = useCallback(
|
||||
|
||||
Reference in new issue
Block a user