diff --git a/src/components/AISettingsProvider.test.tsx b/src/components/AISettingsProvider.test.tsx index 427cbe14..8f47aef2 100644 --- a/src/components/AISettingsProvider.test.tsx +++ b/src/components/AISettingsProvider.test.tsx @@ -1,6 +1,5 @@ import { describe, it, expect, beforeEach, vi } from 'vitest'; import { renderHook, act } from '@testing-library/react'; -import { AISettingsProvider } from './AISettingsProvider'; import { useAISettings } from '@/hooks/useAISettings'; import { TestApp } from '@/test/TestApp'; @@ -24,11 +23,7 @@ describe('AISettingsProvider', () => { it('should initialize with empty recentlyUsedModels array', () => { const { result } = renderHook(() => useAISettings(), { - wrapper: ({ children }) => ( - - {children} - - ), + wrapper: ({ children }) => {children}, }); expect(result.current.settings.recentlyUsedModels).toEqual([]); @@ -36,11 +31,7 @@ describe('AISettingsProvider', () => { it('should add recently used models correctly', () => { const { result } = renderHook(() => useAISettings(), { - wrapper: ({ children }) => ( - - {children} - - ), + wrapper: ({ children }) => {children}, }); act(() => { @@ -54,11 +45,7 @@ describe('AISettingsProvider', () => { it('should move existing model to front when used again', () => { const { result } = renderHook(() => useAISettings(), { - wrapper: ({ children }) => ( - - {children} - - ), + wrapper: ({ children }) => {children}, }); // Add multiple models @@ -88,11 +75,7 @@ describe('AISettingsProvider', () => { it('should limit recently used models to 10 items', () => { const { result } = renderHook(() => useAISettings(), { - wrapper: ({ children }) => ( - - {children} - - ), + wrapper: ({ children }) => {children}, }); // Add 12 models @@ -121,11 +104,7 @@ describe('AISettingsProvider', () => { localStorageMock.getItem.mockReturnValue(JSON.stringify(oldSettings)); const { result } = renderHook(() => useAISettings(), { - wrapper: ({ children }) => ( - - {children} - - ), + wrapper: ({ children }) => {children}, }); expect(result.current.settings.recentlyUsedModels).toEqual([]); diff --git a/src/components/AISettingsProvider.tsx b/src/components/AISettingsProvider.tsx index dae75f72..06b4ad7c 100644 --- a/src/components/AISettingsProvider.tsx +++ b/src/components/AISettingsProvider.tsx @@ -12,39 +12,19 @@ const DEFAULT_SETTINGS: AISettings = { const STORAGE_KEY = 'ai-settings'; -function migrateOldSettings(stored: unknown): AISettings { - // Handle migration from old single-provider format - if (stored && typeof stored === 'object' && 'apiKey' in stored && 'baseUrl' in stored) { - const oldSettings = stored as { apiKey?: string; baseUrl?: string }; - return { - providers: { - openrouter: { - baseURL: oldSettings.baseUrl || 'https://openrouter.ai/api/v1', - apiKey: oldSettings.apiKey || '', - }, - }, - recentlyUsedModels: [], - }; - } - // If it's already the new format or invalid, return as AISettings or default - if (stored && typeof stored === 'object' && 'providers' in stored) { - const settings = stored as Partial; - return { - providers: settings.providers || {}, - recentlyUsedModels: settings.recentlyUsedModels || [], - }; - } - return DEFAULT_SETTINGS; -} - export function AISettingsProvider({ children }: AISettingsProviderProps) { const [settings, setSettings] = useState(() => { const stored = localStorage.getItem(STORAGE_KEY); if (stored) { try { const parsed = JSON.parse(stored); - const migrated = migrateOldSettings(parsed); - return { ...DEFAULT_SETTINGS, ...migrated }; + if (parsed && typeof parsed === 'object' && 'providers' in parsed) { + const settings = parsed as Partial; + return { + providers: settings.providers || {}, + recentlyUsedModels: settings.recentlyUsedModels || [], + }; + } } catch { return DEFAULT_SETTINGS; } diff --git a/src/components/SessionManagerProvider.tsx b/src/components/SessionManagerProvider.tsx index 39afd254..173c250a 100644 --- a/src/components/SessionManagerProvider.tsx +++ b/src/components/SessionManagerProvider.tsx @@ -4,6 +4,7 @@ import { SessionManager } from '@/lib/SessionManager'; import { useFS } from '@/hooks/useFS'; import { useAISettings } from '@/hooks/useAISettings'; import { useProviderModels } from '@/hooks/useProviderModels'; +import { useCurrentUser } from '@/hooks/useCurrentUser'; interface SessionManagerProviderProps { children: ReactNode; @@ -16,11 +17,13 @@ export function SessionManagerProvider({ children }: SessionManagerProviderProps const { fs } = useFS(); const { settings } = useAISettings(); const { models } = useProviderModels(); + const { user } = useCurrentUser(); const sessionManager = useMemo(() => { const getProviderModels = () => models; - return new SessionManager(fs, settings, getProviderModels); - }, [fs, settings, models]); + const getCurrentUser = () => user; + return new SessionManager(fs, settings, getProviderModels, getCurrentUser); + }, [fs, settings, models, user]); // Cleanup on unmount useEffect(() => { diff --git a/src/components/ai/AISettingsDialog.tsx b/src/components/ai/AISettingsDialog.tsx index 9dc06ce5..4614a1d9 100644 --- a/src/components/ai/AISettingsDialog.tsx +++ b/src/components/ai/AISettingsDialog.tsx @@ -8,6 +8,7 @@ import { Card, CardContent } from '@/components/ui/card'; import { Separator } from '@/components/ui/separator'; import { Badge } from '@/components/ui/badge'; import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@/components/ui/accordion'; +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { useAISettings } from '@/hooks/useAISettings'; import type { AIConnection } from '@/contexts/AISettingsContext'; @@ -78,12 +79,14 @@ export function AISettingsDialog({ open: controlledOpen, onOpenChange }: AISetti const [customProviderName, setCustomProviderName] = useState(''); const [customBaseURL, setCustomBaseURL] = useState(''); const [customApiKey, setCustomApiKey] = useState(''); + const [customAuthMethod, setCustomAuthMethod] = useState<'api-key' | 'nostr'>('api-key'); const [presetApiKeys, setPresetApiKeys] = useState>({}); const handleCancel = () => { setCustomProviderName(''); setCustomBaseURL(''); setCustomApiKey(''); + setCustomAuthMethod('api-key'); setPresetApiKeys({}); setOpen(false); }; @@ -112,7 +115,8 @@ export function AISettingsDialog({ open: controlledOpen, onOpenChange }: AISetti const newProvider: AIConnection = { baseURL: customBaseURL.trim(), - apiKey: customApiKey.trim(), + apiKey: customAuthMethod === 'nostr' ? undefined : customApiKey.trim(), + nostr: customAuthMethod === 'nostr' || undefined, }; // Auto-save: Add provider immediately to persistent storage @@ -121,6 +125,7 @@ export function AISettingsDialog({ open: controlledOpen, onOpenChange }: AISetti setCustomProviderName(''); setCustomBaseURL(''); setCustomApiKey(''); + setCustomAuthMethod('api-key'); }; const handleRemoveProvider = (name: string) => { @@ -204,15 +209,35 @@ export function AISettingsDialog({ open: controlledOpen, onOpenChange }: AISetti />
- - handleUpdateProvider(providerId, { apiKey: e.target.value })} - /> + +
+ {!provider?.nostr && ( +
+ + handleUpdateProvider(providerId, { apiKey: e.target.value })} + /> +
+ )} @@ -299,15 +324,37 @@ export function AISettingsDialog({ open: controlledOpen, onOpenChange }: AISetti />
- - setCustomApiKey(e.target.value)} - /> + +
+ {customAuthMethod === 'api-key' && ( +
+ + setCustomApiKey(e.target.value)} + /> +
+ )}