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