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

+10 -1
View File
@@ -2,18 +2,27 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
import { renderHook } from '@testing-library/react';
import { useAIProjectId } from './useAIProjectId';
import { useAISettings } from './useAISettings';
import { useCurrentUser } from './useCurrentUser';
// Mock the useAISettings hook
// Mock the hooks
vi.mock('./useAISettings');
vi.mock('./useCurrentUser');
describe('useAIProjectId', () => {
const mockUseAISettings = vi.mocked(useAISettings);
const mockUseCurrentUser = vi.mocked(useCurrentUser);
beforeEach(() => {
vi.clearAllMocks();
// Mock fetch globally
global.fetch = vi.fn();
// Mock useCurrentUser to return undefined user by default
mockUseCurrentUser.mockReturnValue({
user: undefined,
users: [],
});
});
it('should return not configured when AI settings are not configured', () => {
+5 -7
View File
@@ -1,6 +1,7 @@
import { useState, useCallback } from 'react';
import OpenAI from 'openai';
import { useAISettings } from '@/hooks/useAISettings';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { createAIClient } from '@/lib/ai-client';
interface UseAIProjectIdOptions {
onError?: (error: string) => void;
@@ -9,6 +10,7 @@ interface UseAIProjectIdOptions {
export function useAIProjectId({ onError }: UseAIProjectIdOptions = {}) {
const [isLoading, setIsLoading] = useState(false);
const { settings, isConfigured } = useAISettings();
const { user } = useCurrentUser();
const generateProjectId = useCallback(async (prompt: string): Promise<string> => {
if (!isConfigured) {
@@ -33,11 +35,7 @@ export function useAIProjectId({ onError }: UseAIProjectIdOptions = {}) {
const modelName = 'gpt-3.5-turbo'; // Use a fast, cheap model for ID generation
// Initialize OpenAI client
const openai = new OpenAI({
baseURL: connectionConfig.baseURL,
apiKey: connectionConfig.apiKey,
dangerouslyAllowBrowser: true
});
const openai = createAIClient(connectionConfig, user);
const systemPrompt = `You are a project ID generator. Given a user's description of what they want to build, generate a short, descriptive, kebab-case project ID that captures the essence of their project.
@@ -84,7 +82,7 @@ Respond with ONLY the project ID, no explanation or additional text.`;
} finally {
setIsLoading(false);
}
}, [isConfigured, settings, onError]);
}, [isConfigured, settings, onError, user]);
return {
generateProjectId,
+5
View File
@@ -25,6 +25,11 @@ export function useCurrentUser() {
const users = useMemo(() => {
const users: NUser[] = [];
// Safeguard against undefined or null logins
if (!logins || !Array.isArray(logins)) {
return users;
}
for (const login of logins) {
try {
const user = loginToUser(login);
+6 -13
View File
@@ -1,8 +1,9 @@
import { Decimal } from 'decimal.js';
import { useState, useCallback } from 'react';
import { useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import OpenAI from 'openai';
import { useAISettings } from '@/hooks/useAISettings';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { createAIClient } from '@/lib/ai-client';
interface ProviderModel {
id: string;
@@ -29,12 +30,13 @@ interface ModelFetchResult {
export function useProviderModels(): ModelFetchResult {
const { settings } = useAISettings();
const { user } = useCurrentUser();
const [error, setError] = useState<string | null>(null);
const {
data: models = [],
isLoading,
refetch: queryRefetch,
refetch,
} = useQuery({
queryKey: ['provider-models', settings.providers],
queryFn: async () => {
@@ -44,11 +46,7 @@ export function useProviderModels(): ModelFetchResult {
// Fetch models from each configured provider
for (const [providerKey, connection] of Object.entries(settings.providers)) {
try {
const openai = new OpenAI({
baseURL: connection.baseURL,
apiKey: connection.apiKey,
dangerouslyAllowBrowser: true,
});
const openai = createAIClient(connection, user);
// Fetch models with timeout
try {
@@ -126,11 +124,6 @@ export function useProviderModels(): ModelFetchResult {
retry: false, // Don't retry on failure
});
const refetch = useCallback(() => {
setError(null);
queryRefetch();
}, [queryRefetch]);
return {
models,
isLoading,