Make Nostr Auth work

This commit is contained in:
Alex Gleason committed 2025-09-11 17:53:36 -05:00
1 parent d7807f3911
commit bd0ed0a10f
13 files changed
+152 -113

No files matched your search

+5 -26
View File
@@ -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 }) => (
<TestApp>
<AISettingsProvider>{children}</AISettingsProvider>
</TestApp>
),
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
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 }) => (
<TestApp>
<AISettingsProvider>{children}</AISettingsProvider>
</TestApp>
),
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
act(() => {
@@ -54,11 +45,7 @@ describe('AISettingsProvider', () => {
it('should move existing model to front when used again', () => {
const { result } = renderHook(() => useAISettings(), {
wrapper: ({ children }) => (
<TestApp>
<AISettingsProvider>{children}</AISettingsProvider>
</TestApp>
),
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
// Add multiple models
@@ -88,11 +75,7 @@ describe('AISettingsProvider', () => {
it('should limit recently used models to 10 items', () => {
const { result } = renderHook(() => useAISettings(), {
wrapper: ({ children }) => (
<TestApp>
<AISettingsProvider>{children}</AISettingsProvider>
</TestApp>
),
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
// Add 12 models
@@ -121,11 +104,7 @@ describe('AISettingsProvider', () => {
localStorageMock.getItem.mockReturnValue(JSON.stringify(oldSettings));
const { result } = renderHook(() => useAISettings(), {
wrapper: ({ children }) => (
<TestApp>
<AISettingsProvider>{children}</AISettingsProvider>
</TestApp>
),
wrapper: ({ children }) => <TestApp>{children}</TestApp>,
});
expect(result.current.settings.recentlyUsedModels).toEqual([]);
+7 -27
View File
@@ -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<AISettings>;
return {
providers: settings.providers || {},
recentlyUsedModels: settings.recentlyUsedModels || [],
};
}
return DEFAULT_SETTINGS;
}
export function AISettingsProvider({ children }: AISettingsProviderProps) {
const [settings, setSettings] = useState<AISettings>(() => {
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<AISettings>;
return {
providers: settings.providers || {},
recentlyUsedModels: settings.recentlyUsedModels || [],
};
}
} catch {
return DEFAULT_SETTINGS;
}
+5 -2
View File
@@ -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(() => {
+64 -17
View File
@@ -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<Record<string, string>>({});
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
/>
</div>
<div className="grid gap-2">
<Label htmlFor={`${providerId}-apiKey`}>API Key</Label>
<Input
id={`${providerId}-apiKey`}
type="password"
placeholder="Enter your API key"
value={provider?.apiKey || ''}
onChange={(e) => handleUpdateProvider(providerId, { apiKey: e.target.value })}
/>
<Label htmlFor={`${providerId}-auth`}>Authentication</Label>
<Select
value={provider?.nostr ? 'nostr' : 'api-key'}
onValueChange={(value: 'api-key' | 'nostr') => handleUpdateProvider(providerId, {
nostr: value === 'nostr' || undefined,
apiKey: value === 'nostr' ? undefined : provider?.apiKey
})}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="api-key">API Key</SelectItem>
<SelectItem value="nostr">Nostr</SelectItem>
</SelectContent>
</Select>
</div>
{!provider?.nostr && (
<div className="grid gap-2">
<Label htmlFor={`${providerId}-apiKey`}>API Key</Label>
<Input
id={`${providerId}-apiKey`}
type="password"
placeholder="Enter your API key"
value={provider?.apiKey || ''}
onChange={(e) => handleUpdateProvider(providerId, { apiKey: e.target.value })}
/>
</div>
)}
</div>
</AccordionContent>
</AccordionItem>
@@ -299,15 +324,37 @@ export function AISettingsDialog({ open: controlledOpen, onOpenChange }: AISetti
/>
</div>
<div className="grid gap-2">
<Label htmlFor="custom-apikey">API Key</Label>
<Input
id="custom-apikey"
type="password"
placeholder="Enter your API key (optional)"
value={customApiKey}
onChange={(e) => setCustomApiKey(e.target.value)}
/>
<Label htmlFor="custom-auth">Authentication</Label>
<Select
value={customAuthMethod}
onValueChange={(value: 'api-key' | 'nostr') => {
setCustomAuthMethod(value);
if (value === 'nostr') {
setCustomApiKey(''); // Clear API key when switching to Nostr auth
}
}}
>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="api-key">API Key</SelectItem>
<SelectItem value="nostr">Nostr</SelectItem>
</SelectContent>
</Select>
</div>
{customAuthMethod === 'api-key' && (
<div className="grid gap-2">
<Label htmlFor="custom-apikey">API Key</Label>
<Input
id="custom-apikey"
type="password"
placeholder="Enter your API key (optional)"
value={customApiKey}
onChange={(e) => setCustomApiKey(e.target.value)}
/>
</div>
)}
<Button
onClick={handleAddCustomProvider}
disabled={