ai improvements

This commit is contained in:
overcuriousity 2025-07-16 21:33:54 +02:00
parent 89f45b85be
commit 74f28f4fd9
4 changed files with 32 additions and 14 deletions

View File

@ -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;
}

View File

@ -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');

View File

@ -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' }), {

View File

@ -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');
}
});