mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
refactor: unify model selection with the TUI
Align the manager's model selection with the OpenCode TUI: one model.json-backed preference store, one provider catalog, one ordering and default-ownership rule, and one set of availability, ref-formatting and variant helpers shared across chat, schedules, agents and the goal auditor. - Add a shared TUI-compatible model-preference module and make the backend model.json store match its format. - Consolidate the frontend provider catalog, availability check, model ref formatter and variant persistence into single owners. - Drive chat, schedule, agent and goal-auditor model lists from a shared useModelSections hook and buildModelSections, following TUI ordering. - Extract a shared ModelCombobox with a taller list, and list models the TUI way in ModelQuickSelect. - Add a shared isRecord util and model-selection comparison helpers.
This commit is contained in:
1 parent
3d143568c1
commit
65b72e55ae
82 files changed
+4418
-2624
No files matched your search
@@ -18,17 +18,16 @@ vi.mock('../../src/utils/logger', () => ({
|
||||
}))
|
||||
|
||||
import {
|
||||
MAX_RECENT_MODELS,
|
||||
addRecentModel,
|
||||
readOpenCodeModelState,
|
||||
removeRecentModel,
|
||||
toggleFavoriteModel,
|
||||
type ModelPreference,
|
||||
} from '@opencode-manager/shared/opencode'
|
||||
import {
|
||||
readOpenCodeModelState,
|
||||
updateOpenCodeModelState,
|
||||
type OpenCodeModelStateRecord,
|
||||
} from '../../src/services/opencode-model-state'
|
||||
import { logger } from '../../src/utils/logger'
|
||||
|
||||
const emptyState = (): OpenCodeModelStateRecord => ({ recent: [], favorite: [], variant: {} })
|
||||
const emptyState = (): ModelPreference => ({ recent: [], favorite: [], variant: {} })
|
||||
|
||||
describe('opencode-model-state', () => {
|
||||
let workDir: string
|
||||
@@ -43,65 +42,6 @@ describe('opencode-model-state', () => {
|
||||
await rm(workDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
describe('addRecentModel', () => {
|
||||
it('adds a new model to the front', () => {
|
||||
const state = addRecentModel(emptyState(), { providerID: 'anthropic', modelID: 'claude' })
|
||||
expect(state.recent).toEqual([{ providerID: 'anthropic', modelID: 'claude' }])
|
||||
})
|
||||
|
||||
it('deduplicates and moves a re-selected model to the front', () => {
|
||||
const model = { providerID: 'openai', modelID: 'gpt-4o' }
|
||||
const first = addRecentModel(emptyState(), model)
|
||||
const second = addRecentModel(first, { providerID: 'anthropic', modelID: 'claude' })
|
||||
const third = addRecentModel(second, model)
|
||||
|
||||
expect(third.recent).toEqual([
|
||||
model,
|
||||
{ providerID: 'anthropic', modelID: 'claude' },
|
||||
])
|
||||
})
|
||||
|
||||
it('caps recent at MAX_RECENT_MODELS', () => {
|
||||
let state = emptyState()
|
||||
for (let i = 0; i < MAX_RECENT_MODELS + 2; i += 1) {
|
||||
state = addRecentModel(state, { providerID: `provider-${i}`, modelID: `model-${i}` })
|
||||
}
|
||||
|
||||
expect(state.recent).toHaveLength(MAX_RECENT_MODELS)
|
||||
expect(state.recent[0]).toEqual({ providerID: 'provider-11', modelID: 'model-11' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('removeRecentModel', () => {
|
||||
it('removes the matching model and keeps the rest', () => {
|
||||
const state = {
|
||||
recent: [
|
||||
{ providerID: 'anthropic', modelID: 'claude' },
|
||||
{ providerID: 'openai', modelID: 'gpt-4o' },
|
||||
],
|
||||
favorite: [],
|
||||
variant: {},
|
||||
}
|
||||
|
||||
expect(removeRecentModel(state, { providerID: 'anthropic', modelID: 'claude' }).recent).toEqual([
|
||||
{ providerID: 'openai', modelID: 'gpt-4o' },
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('toggleFavoriteModel', () => {
|
||||
it('adds when missing', () => {
|
||||
const state = toggleFavoriteModel(emptyState(), { providerID: 'anthropic', modelID: 'claude' })
|
||||
expect(state.favorite).toEqual([{ providerID: 'anthropic', modelID: 'claude' }])
|
||||
})
|
||||
|
||||
it('removes when present', () => {
|
||||
const model = { providerID: 'openai', modelID: 'gpt-4' }
|
||||
const added = toggleFavoriteModel(emptyState(), model)
|
||||
expect(toggleFavoriteModel(added, model).favorite).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('readOpenCodeModelState', () => {
|
||||
it('returns empty defaults when the file is missing', async () => {
|
||||
await expect(readOpenCodeModelState()).resolves.toEqual(emptyState())
|
||||
@@ -118,11 +58,27 @@ describe('opencode-model-state', () => {
|
||||
await expect(readOpenCodeModelState()).resolves.toEqual(state)
|
||||
})
|
||||
|
||||
it('warns and returns empty defaults when the file has invalid structure', async () => {
|
||||
it('drops malformed entries without resetting the whole file', async () => {
|
||||
await writeFile(
|
||||
paths.modelState,
|
||||
JSON.stringify({
|
||||
recent: [{ providerID: 'a', modelID: 'b' }, { providerID: '' }],
|
||||
variant: { 'a/b': 'default', x: '' },
|
||||
extra: 1,
|
||||
}),
|
||||
'utf8',
|
||||
)
|
||||
|
||||
const state = await readOpenCodeModelState()
|
||||
expect(state.recent).toEqual([{ providerID: 'a', modelID: 'b' }])
|
||||
expect(state.variant).toEqual({ 'a/b': 'default' })
|
||||
expect(state).not.toHaveProperty('extra')
|
||||
})
|
||||
|
||||
it('returns empty defaults when the file has an unexpected shape', async () => {
|
||||
await writeFile(paths.modelState, JSON.stringify({ recent: 'not-an-array' }), 'utf8')
|
||||
|
||||
await expect(readOpenCodeModelState()).resolves.toEqual(emptyState())
|
||||
expect(logger.warn).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -152,6 +108,21 @@ describe('opencode-model-state', () => {
|
||||
expect(file.variant).toEqual({})
|
||||
})
|
||||
|
||||
it('preserves unknown top-level keys when toggling a favorite', async () => {
|
||||
await writeFile(paths.modelState, JSON.stringify({ extra: 1, recent: [] }), 'utf8')
|
||||
|
||||
await updateOpenCodeModelState(state =>
|
||||
toggleFavoriteModel(state, { providerID: 'anthropic', modelID: 'claude' }),
|
||||
)
|
||||
|
||||
const file = JSON.parse(await readFile(paths.modelState, 'utf8')) as {
|
||||
extra: unknown
|
||||
favorite: unknown[]
|
||||
}
|
||||
expect(file.extra).toBe(1)
|
||||
expect(file.favorite).toEqual([{ providerID: 'anthropic', modelID: 'claude' }])
|
||||
})
|
||||
|
||||
it('recovers from corrupt JSON and writes valid state', async () => {
|
||||
await writeFile(paths.modelState, '{ invalid json content }', 'utf8')
|
||||
|
||||
@@ -174,7 +145,7 @@ describe('opencode-model-state', () => {
|
||||
await Promise.all(operations)
|
||||
|
||||
const finalState = await readOpenCodeModelState()
|
||||
expect(finalState.recent.length).toBeLessThanOrEqual(MAX_RECENT_MODELS)
|
||||
expect(finalState.recent.length).toBeLessThanOrEqual(10)
|
||||
expect(finalState.recent.length).toBeGreaterThan(0)
|
||||
|
||||
const uniqueKeys = new Set(finalState.recent.map((m) => `${m.providerID}/${m.modelID}`))
|
||||
|
||||
Reference in new issue
Block a user