Fix default settings overriding user's recently used models

Separate EMPTY_AI_SETTINGS (for initial state and error fallback) from
OPENCODE_DEFAULT_SETTINGS (for genuinely new users only). Previously,
readAISettings returned OPENCODE_DEFAULT_SETTINGS on any error, which
could overwrite an existing user's recentlyUsedModels with
['opencode/big-pickle']. Now the error path returns empty settings, and
OPENCODE defaults are only applied when no providers are configured.
This commit is contained in:
Alex Gleason committed 2026-04-04 02:13:35 -05:00
1 parent 0a2cbf4bca
commit 2736f71687
5 files changed
+58 -15

No files matched your search

+33 -2
View File
@@ -36,17 +36,27 @@ describe('AISettingsProvider', () => {
});
});
it('should initialize with empty recentlyUsedModels array', async () => {
it('should initialize with OpenCode defaults for new users (no providers)', async () => {
const { result } = renderHook(() => useAISettings(), {
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
await waitFor(() => {
expect(result.current.settings.recentlyUsedModels).toEqual([]);
// New users with no providers get OPENCODE_DEFAULT_SETTINGS with big-pickle
expect(result.current.settings.providers).toHaveLength(1);
expect(result.current.settings.providers[0].id).toBe('opencode');
expect(result.current.settings.recentlyUsedModels).toEqual(['opencode/big-pickle']);
});
});
it('should add recently used models correctly', async () => {
// Simulate existing user with a provider configured
const { readAISettings } = await import('@/lib/configUtils');
vi.mocked(readAISettings).mockResolvedValue({
providers: [{ id: 'openrouter', name: 'OpenRouter', baseURL: 'https://openrouter.ai/api/v1', apiKey: 'test-key' }],
recentlyUsedModels: [],
});
const { result } = renderHook(() => useAISettings(), {
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
@@ -65,6 +75,13 @@ describe('AISettingsProvider', () => {
});
it('should move existing model to front when used again', async () => {
// Simulate existing user with a provider configured
const { readAISettings } = await import('@/lib/configUtils');
vi.mocked(readAISettings).mockResolvedValue({
providers: [{ id: 'openrouter', name: 'OpenRouter', baseURL: 'https://openrouter.ai/api/v1', apiKey: 'test-key' }],
recentlyUsedModels: [],
});
const { result } = renderHook(() => useAISettings(), {
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
@@ -99,6 +116,13 @@ describe('AISettingsProvider', () => {
});
it('should limit recently used models to 10 items', async () => {
// Simulate existing user with a provider configured
const { readAISettings } = await import('@/lib/configUtils');
vi.mocked(readAISettings).mockResolvedValue({
providers: [{ id: 'openrouter', name: 'OpenRouter', baseURL: 'https://openrouter.ai/api/v1', apiKey: 'test-key' }],
recentlyUsedModels: [],
});
const { result } = renderHook(() => useAISettings(), {
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
@@ -147,6 +171,13 @@ describe('AISettingsProvider', () => {
});
it('should invalidate provider-models query when updateSettings is called with providers', async () => {
// Simulate existing user with a provider configured
const { readAISettings } = await import('@/lib/configUtils');
vi.mocked(readAISettings).mockResolvedValue({
providers: [{ id: 'openrouter', name: 'OpenRouter', baseURL: 'https://openrouter.ai/api/v1', apiKey: 'test-key' }],
recentlyUsedModels: [],
});
// Mock the QueryClient.invalidateQueries method
const mockInvalidateQueries = vi.fn();
+10 -5
View File
@@ -4,7 +4,7 @@ import { AISettingsContext, type AISettings, type AIProvider, type MCPServer, ty
import { useFS } from '@/hooks/useFS';
import { useFSPaths } from '@/hooks/useFSPaths';
import { readAISettings, writeAISettings } from '@/lib/configUtils';
import { OPENCODE_DEFAULT_SETTINGS } from '@/lib/opencode-defaults';
import { EMPTY_AI_SETTINGS, OPENCODE_DEFAULT_SETTINGS } from '@/lib/opencode-defaults';
interface AISettingsProviderProps {
children: ReactNode;
@@ -14,18 +14,23 @@ export function AISettingsProvider({ children }: AISettingsProviderProps) {
const queryClient = useQueryClient();
const { fs } = useFS();
const { configPath } = useFSPaths();
const [settings, setSettings] = useState<AISettings>(OPENCODE_DEFAULT_SETTINGS);
const [settings, setSettings] = useState<AISettings>(EMPTY_AI_SETTINGS);
const [isInitialized, setIsInitialized] = useState(false);
// Initialize settings from VFS on mount
useEffect(() => {
const initializeSettings = async () => {
try {
const settings = await readAISettings(fs, configPath);
setSettings(settings);
const loaded = await readAISettings(fs, configPath);
// Only apply OpenCode defaults for genuinely new users (no providers configured)
if (loaded.providers.length === 0) {
setSettings(OPENCODE_DEFAULT_SETTINGS);
} else {
setSettings(loaded);
}
} catch (error) {
console.error('Failed to initialize AI settings:', error);
setSettings(OPENCODE_DEFAULT_SETTINGS);
setSettings(EMPTY_AI_SETTINGS);
} finally {
setIsInitialized(true);
}
+5 -5
View File
@@ -7,7 +7,7 @@ import {
readDeploySettings,
writeDeploySettings
} from './configUtils';
import { OPENCODE_DEFAULT_SETTINGS } from './opencode-defaults';
import { EMPTY_AI_SETTINGS } from './opencode-defaults';
import type { JSRuntimeFS } from './JSRuntime';
import type { AISettings } from '@/contexts/AISettingsContext';
import type { GitSettings } from '@/contexts/GitSettingsContext';
@@ -90,22 +90,22 @@ describe('configUtils', () => {
});
});
it('should return default settings with OpenCode Zen if file does not exist and no localStorage data', async () => {
it('should return empty settings if file does not exist and no localStorage data', async () => {
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found'));
mockLocalStorage.getItem.mockReturnValue(null);
const result = await readAISettings(mockFS);
expect(result).toEqual(OPENCODE_DEFAULT_SETTINGS);
expect(result).toEqual(EMPTY_AI_SETTINGS);
});
it('should return default settings with OpenCode Zen if JSON is invalid and no localStorage data', async () => {
it('should return empty settings if JSON is invalid and no localStorage data', async () => {
vi.mocked(mockFS.readFile).mockResolvedValue('invalid json');
mockLocalStorage.getItem.mockReturnValue(null);
const result = await readAISettings(mockFS);
expect(result).toEqual(OPENCODE_DEFAULT_SETTINGS);
expect(result).toEqual(EMPTY_AI_SETTINGS);
});
});
+2 -2
View File
@@ -1,7 +1,7 @@
import z from 'zod';
import { filteredArray } from '@/lib/schema';
import { AI_PROVIDER_PRESETS } from '@/lib/aiProviderPresets';
import { OPENCODE_DEFAULT_SETTINGS } from '@/lib/opencode-defaults';
import { EMPTY_AI_SETTINGS } from '@/lib/opencode-defaults';
import type { JSRuntimeFS } from '@/lib/JSRuntime';
import type { AISettings, MCPServer } from '@/contexts/AISettingsContext';
import type { GitSettings, GitCredential } from '@/contexts/GitSettingsContext';
@@ -205,7 +205,7 @@ export async function readAISettings(fs: JSRuntimeFS, configPath = '/config'): P
if (error instanceof z.ZodError) {
console.error('AI settings parsing error:', error.issues);
}
return OPENCODE_DEFAULT_SETTINGS;
return EMPTY_AI_SETTINGS;
}
}
+8 -1
View File
@@ -3,7 +3,14 @@ import { AI_PROVIDER_PRESETS } from '@/lib/aiProviderPresets';
const opencodePreset = AI_PROVIDER_PRESETS.find((p) => p.id === 'opencode')!;
/** Default AI settings with OpenCode Zen pre-configured and big-pickle selected. */
/** Empty AI settings used as initial state and error fallback. Won't overwrite user data. */
export const EMPTY_AI_SETTINGS: AISettings = {
providers: [],
recentlyUsedModels: [],
mcpServers: {},
};
/** Default AI settings with OpenCode Zen pre-configured and big-pickle selected. Only for genuinely new users. */
export const OPENCODE_DEFAULT_SETTINGS: AISettings = {
providers: [
{