mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Undo formatting noise
This commit is contained in:
@@ -10,9 +10,7 @@ import { RMABLogger } from '@/lib/utils/logger';
|
||||
const logger = RMABLogger.create('API.BookDate.TestConnection');
|
||||
|
||||
// Fetch available Claude models from the Anthropic API
|
||||
async function fetchClaudeModels(
|
||||
apiKey: string,
|
||||
): Promise<{ id: string; name: string }[]> {
|
||||
async function fetchClaudeModels(apiKey: string): Promise<{ id: string; name: string }[]> {
|
||||
const allModels: { id: string; name: string }[] = [];
|
||||
let afterId: string | undefined;
|
||||
|
||||
@@ -30,7 +28,7 @@ async function fetchClaudeModels(
|
||||
'x-api-key': apiKey,
|
||||
'anthropic-version': '2023-06-01',
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -55,12 +53,10 @@ async function fetchClaudeModels(
|
||||
}
|
||||
|
||||
// Fetch available Gemini models from the Google API
|
||||
async function fetchGeminiModels(
|
||||
apiKey: string,
|
||||
): Promise<{ id: string; name: string }[]> {
|
||||
async function fetchGeminiModels(apiKey: string): Promise<{ id: string; name: string }[]> {
|
||||
const response = await fetch(
|
||||
'https://generativelanguage.googleapis.com/v1beta/models',
|
||||
{ headers: { 'x-goog-api-key': apiKey } },
|
||||
{ headers: { 'x-goog-api-key': apiKey } }
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -72,11 +68,7 @@ async function fetchGeminiModels(
|
||||
const data = await response.json();
|
||||
|
||||
return (data.models || [])
|
||||
.filter(
|
||||
(m: any) =>
|
||||
m.name?.startsWith('models/gemini-') &&
|
||||
m.supportedGenerationMethods?.includes('generateContent'),
|
||||
)
|
||||
.filter((m: any) => m.name?.startsWith('models/gemini-') && m.supportedGenerationMethods?.includes('generateContent'))
|
||||
.map((m: any) => ({
|
||||
id: m.name.replace('models/', ''),
|
||||
name: m.displayName || m.name.replace('models/', ''),
|
||||
@@ -107,17 +99,14 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
if (!provider) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Provider is required' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!['openai', 'claude', 'custom', 'gemini'].includes(provider)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
'Invalid provider. Must be "openai", "claude", "custom", or "gemini"',
|
||||
},
|
||||
{ status: 400 },
|
||||
{ error: 'Invalid provider. Must be "openai", "claude", "custom", or "gemini"' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -126,18 +115,15 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
if (!baseUrl && !useSavedKey) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Base URL is required for custom provider' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const urlToValidate = useSavedKey ? null : baseUrl; // Will check saved URL later if useSavedKey
|
||||
if (urlToValidate && !isValidBaseUrl(urlToValidate)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
'Invalid base URL format. Must start with http:// or https://',
|
||||
},
|
||||
{ status: 400 },
|
||||
{ error: 'Invalid base URL format. Must start with http:// or https://' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -147,15 +133,14 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
let testBaseUrl = baseUrl;
|
||||
if (useSavedKey) {
|
||||
const { prisma } = await import('@/lib/db');
|
||||
const { getEncryptionService } =
|
||||
await import('@/lib/services/encryption.service');
|
||||
const { getEncryptionService } = await import('@/lib/services/encryption.service');
|
||||
|
||||
const config = await prisma.bookDateConfig.findFirst();
|
||||
|
||||
if (!config || !config.apiKey) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No saved configuration found' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -167,7 +152,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
if (provider !== 'custom') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to decrypt saved API key' },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
testApiKey = '';
|
||||
@@ -178,7 +163,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
if (!testBaseUrl) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No saved base URL found for custom provider' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -188,7 +173,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
if (!testApiKey && provider !== 'custom') {
|
||||
return NextResponse.json(
|
||||
{ error: 'API key is required' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -198,7 +183,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
// OpenAI: Fetch models from API
|
||||
const response = await fetch('https://api.openai.com/v1/models', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${testApiKey}`,
|
||||
'Authorization': `Bearer ${testApiKey}`,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -207,7 +192,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
logger.error('OpenAI API error', { error: errorText });
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid OpenAI API key or connection failed' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -221,6 +206,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
name: m.id,
|
||||
}))
|
||||
.sort((a: any, b: any) => a.name.localeCompare(b.name));
|
||||
|
||||
} else if (provider === 'claude') {
|
||||
// Claude: Fetch models dynamically from the Anthropic Models API
|
||||
try {
|
||||
@@ -228,7 +214,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid Claude API key or connection failed' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
} else if (provider === 'gemini') {
|
||||
@@ -238,7 +224,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid Gemini API key or connection failed' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
} else if (provider === 'custom') {
|
||||
@@ -259,15 +245,11 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
logger.error('Custom provider connection error', {
|
||||
error: errorText,
|
||||
});
|
||||
logger.error('Custom provider connection error', { error: errorText });
|
||||
// Return 400 (not the external service's status) to prevent triggering logout on 401
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `Failed to connect to custom provider: ${response.status} ${errorText}`,
|
||||
},
|
||||
{ status: 400 },
|
||||
{ error: `Failed to connect to custom provider: ${response.status} ${errorText}` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -292,8 +274,7 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
models: [],
|
||||
message:
|
||||
'Connected successfully but could not parse models list. You may need to enter model name manually.',
|
||||
message: 'Connected successfully but could not parse models list. You may need to enter model name manually.',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -301,10 +282,8 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
} catch (error: any) {
|
||||
logger.error('Custom provider network error', { error: error.message });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `Network error connecting to custom provider: ${error.message}`,
|
||||
},
|
||||
{ status: 500 },
|
||||
{ error: `Network error connecting to custom provider: ${error.message}` },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -314,13 +293,12 @@ async function authenticatedHandler(req: AuthenticatedRequest) {
|
||||
models,
|
||||
provider,
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
logger.error('Test connection error', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
logger.error('Test connection error', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{ error: error.message || 'Connection test failed' },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -335,7 +313,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
if (useSavedKey) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Authentication required to use saved API key' },
|
||||
{ status: 401 },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -343,17 +321,14 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
if (!provider) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Provider is required' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!['openai', 'claude', 'custom', 'gemini'].includes(provider)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
'Invalid provider. Must be "openai", "claude", "custom", or "gemini"',
|
||||
},
|
||||
{ status: 400 },
|
||||
{ error: 'Invalid provider. Must be "openai", "claude", "custom", or "gemini"' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -362,17 +337,14 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
if (!baseUrl) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Base URL is required for custom provider' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!isValidBaseUrl(baseUrl)) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error:
|
||||
'Invalid base URL format. Must start with http:// or https://',
|
||||
},
|
||||
{ status: 400 },
|
||||
{ error: 'Invalid base URL format. Must start with http:// or https://' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -381,7 +353,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
if (!apiKey && provider !== 'custom') {
|
||||
return NextResponse.json(
|
||||
{ error: 'API key is required' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -391,7 +363,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
// OpenAI: Fetch models from API
|
||||
const response = await fetch('https://api.openai.com/v1/models', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${apiKey}`,
|
||||
'Authorization': `Bearer ${apiKey}`,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -400,7 +372,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
logger.error('OpenAI API error', { error: errorText });
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid OpenAI API key or connection failed' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -414,6 +386,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
name: m.id,
|
||||
}))
|
||||
.sort((a: any, b: any) => a.name.localeCompare(b.name));
|
||||
|
||||
} else if (provider === 'claude') {
|
||||
// Claude: Fetch models dynamically from the Anthropic Models API
|
||||
try {
|
||||
@@ -421,7 +394,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid Claude API key or connection failed' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
} else if (provider === 'gemini') {
|
||||
@@ -431,7 +404,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid Gemini API key or connection failed' },
|
||||
{ status: 400 },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
} else if (provider === 'custom') {
|
||||
@@ -452,15 +425,11 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
|
||||
if (!response.ok) {
|
||||
const errorText = await response.text();
|
||||
logger.error('Custom provider connection error', {
|
||||
error: errorText,
|
||||
});
|
||||
logger.error('Custom provider connection error', { error: errorText });
|
||||
// Return 400 (not the external service's status) to prevent triggering logout on 401
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `Failed to connect to custom provider: ${response.status} ${errorText}`,
|
||||
},
|
||||
{ status: 400 },
|
||||
{ error: `Failed to connect to custom provider: ${response.status} ${errorText}` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -485,8 +454,7 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
models: [],
|
||||
message:
|
||||
'Connected successfully but could not parse models list. You may need to enter model name manually.',
|
||||
message: 'Connected successfully but could not parse models list. You may need to enter model name manually.',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -494,10 +462,8 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
} catch (error: any) {
|
||||
logger.error('Custom provider network error', { error: error.message });
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: `Network error connecting to custom provider: ${error.message}`,
|
||||
},
|
||||
{ status: 500 },
|
||||
{ error: `Network error connecting to custom provider: ${error.message}` },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -507,13 +473,12 @@ async function unauthenticatedHandler(req: NextRequest) {
|
||||
models,
|
||||
provider,
|
||||
});
|
||||
|
||||
} catch (error: any) {
|
||||
logger.error('Test connection error', {
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
logger.error('Test connection error', { error: error instanceof Error ? error.message : String(error) });
|
||||
return NextResponse.json(
|
||||
{ error: error.message || 'Connection test failed' },
|
||||
{ status: 500 },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user