diff --git a/src/components/AIQueryInterface.astro b/src/components/AIQueryInterface.astro index b1ad746..bba6332 100644 --- a/src/components/AIQueryInterface.astro +++ b/src/components/AIQueryInterface.astro @@ -374,10 +374,16 @@ async function checkAuthentication() { try { const response = await fetch('/api/auth/status'); const data = await response.json(); - return data.authenticated; + return { + authenticated: data.authenticated, + authRequired: data.authRequired + }; } catch (error) { console.error('Auth check failed:', error); - return false; + return { + authenticated: false, + authRequired: true + }; } } @@ -409,9 +415,9 @@ document.addEventListener('DOMContentLoaded', () => { } // Check authentication - const isAuthenticated = await checkAuthentication(); - if (!isAuthenticated) { - // Redirect to login + const authStatus = await checkAuthentication(); + if (authStatus.authRequired && !authStatus.authenticated) { + // Redirect to login only if authentication is required window.location.href = `/api/auth/login?returnTo=${encodeURIComponent(window.location.pathname)}`; return; } diff --git a/src/components/ToolFilters.astro b/src/components/ToolFilters.astro index 6fe2b62..bfa5594 100644 --- a/src/components/ToolFilters.astro +++ b/src/components/ToolFilters.astro @@ -156,8 +156,11 @@ const sortedTags = Object.entries(tagFrequency) const response = await fetch('/api/auth/status'); const data = await response.json(); - if (data.authenticated && aiViewToggle) { - aiViewToggle.style.display = 'inline-flex'; + // Show AI button if authentication is not required OR if user is authenticated + if (!data.authRequired || data.authenticated) { + if (aiViewToggle) { + aiViewToggle.style.display = 'inline-flex'; + } } } catch (error) { console.log('Auth check failed, AI button remains hidden'); diff --git a/src/pages/api/ai/query.ts b/src/pages/api/ai/query.ts index d19e89b..c43f5fb 100644 --- a/src/pages/api/ai/query.ts +++ b/src/pages/api/ai/query.ts @@ -1,4 +1,5 @@ // src/pages/api/ai/query.ts +// src/pages/api/ai/query.ts import type { APIRoute } from 'astro'; import { getSessionFromRequest, verifySession } from '../../../utils/auth.js'; import { promises as fs } from 'fs'; @@ -209,7 +210,7 @@ export const POST: APIRoute = async ({ request }) => { 'Authorization': `Bearer ${process.env.AI_API_KEY}` }, body: JSON.stringify({ - model: 'gpt-4o-mini', // or whatever model is available + model: AI_MODEL, // or whatever model is available messages: [ { role: 'system', @@ -225,6 +226,7 @@ export const POST: APIRoute = async ({ request }) => { }) }); + if (!aiResponse.ok) { console.error('AI API error:', await aiResponse.text()); return new Response(JSON.stringify({ error: 'AI service unavailable' }), { @@ -246,7 +248,8 @@ export const POST: APIRoute = async ({ request }) => { // Parse AI JSON response let recommendation; try { - recommendation = JSON.parse(aiContent); + const cleanedContent = stripMarkdownJson(aiContent); + recommendation = JSON.parse(cleanedContent); } catch (error) { console.error('Failed to parse AI response:', aiContent); return new Response(JSON.stringify({ error: 'Invalid AI response format' }), { diff --git a/src/pages/index.astro b/src/pages/index.astro index c37a6d9..df6b398 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -119,24 +119,30 @@ const tools = data.tools; try { const response = await fetch('/api/auth/status'); const data = await response.json(); - return data.authenticated; + return { + authenticated: data.authenticated, + authRequired: data.authRequired + }; } catch (error) { console.error('Auth check failed:', error); - return false; + return { + authenticated: false, + authRequired: true + }; } } // AI Query Button Handler if (aiQueryBtn) { aiQueryBtn.addEventListener('click', async () => { - const isAuthenticated = await checkAuthentication(); + const authStatus = await checkAuthentication(); - if (!isAuthenticated) { + if (authStatus.authRequired && !authStatus.authenticated) { // Redirect to login, then back to AI view const returnUrl = `${window.location.pathname}?view=ai`; window.location.href = `/api/auth/login?returnTo=${encodeURIComponent(returnUrl)}`; } else { - // Switch to AI view + // Switch to AI view directly switchToView('ai'); } });