AI coach fixed: acquired access to context data
This commit is contained in:
@@ -1,18 +1,30 @@
|
||||
import express from 'express';
|
||||
import express, { Request, Response, NextFunction } from 'express';
|
||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||
import jwt from 'jsonwebtoken';
|
||||
|
||||
interface JwtPayload {
|
||||
userId: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
interface AuthRequest extends Request {
|
||||
user?: JwtPayload;
|
||||
}
|
||||
|
||||
const router = express.Router();
|
||||
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
|
||||
const API_KEY = process.env.API_KEY;
|
||||
const MODEL_ID = 'gemini-2.0-flash';
|
||||
|
||||
const authenticate = (req: any, res: any, next: any) => {
|
||||
// Store chat sessions in memory (in production, use Redis or similar)
|
||||
const chatSessions = new Map<string, any>();
|
||||
|
||||
const authenticate = (req: AuthRequest, res: Response, next: NextFunction) => {
|
||||
const token = req.headers.authorization?.split(' ')[1];
|
||||
if (!token) return res.status(401).json({ error: 'Unauthorized' });
|
||||
|
||||
try {
|
||||
const decoded = jwt.verify(token, JWT_SECRET) as any;
|
||||
const decoded = jwt.verify(token, JWT_SECRET) as JwtPayload;
|
||||
req.user = decoded;
|
||||
next();
|
||||
} catch {
|
||||
@@ -22,31 +34,83 @@ const authenticate = (req: any, res: any, next: any) => {
|
||||
|
||||
router.use(authenticate);
|
||||
|
||||
router.post('/chat', async (req, res) => {
|
||||
router.post('/chat', async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
const { history, message } = req.body;
|
||||
const { systemInstruction, userMessage, sessionId } = req.body;
|
||||
|
||||
if (!API_KEY) {
|
||||
return res.status(500).json({ error: 'AI service not configured' });
|
||||
}
|
||||
|
||||
if (!userMessage) {
|
||||
return res.status(400).json({ error: 'User message is required' });
|
||||
}
|
||||
|
||||
if (!req.user) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const ai = new GoogleGenerativeAI(API_KEY);
|
||||
const userId = req.user.userId;
|
||||
const chatKey = `${userId}-${sessionId || 'default'}`;
|
||||
|
||||
const { systemInstruction, userMessage } = req.body;
|
||||
// Get or create chat session
|
||||
let chat = chatSessions.get(chatKey);
|
||||
|
||||
const model = ai.getGenerativeModel({
|
||||
model: MODEL_ID
|
||||
});
|
||||
if (!chat || systemInstruction) {
|
||||
// Create new chat with system instruction
|
||||
const model = ai.getGenerativeModel({
|
||||
model: MODEL_ID,
|
||||
systemInstruction: systemInstruction || 'You are a helpful fitness coach.'
|
||||
});
|
||||
|
||||
const prompt = `${systemInstruction}\n\nUser: ${userMessage}`;
|
||||
const result = await model.generateContent(prompt);
|
||||
chat = model.startChat({
|
||||
history: []
|
||||
});
|
||||
|
||||
chatSessions.set(chatKey, chat);
|
||||
}
|
||||
|
||||
// Send message and get response
|
||||
const result = await chat.sendMessage(userMessage);
|
||||
const response = result.response.text();
|
||||
|
||||
res.json({ response });
|
||||
} catch (error: any) {
|
||||
console.error('AI Chat Error:', error);
|
||||
|
||||
// Provide more detailed error messages
|
||||
let errorMessage = 'Failed to generate AI response';
|
||||
if (error.message?.includes('API key')) {
|
||||
errorMessage = 'AI service authentication failed';
|
||||
} else if (error.message?.includes('quota')) {
|
||||
errorMessage = 'AI service quota exceeded';
|
||||
} else if (error.message) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
|
||||
res.status(500).json({ error: errorMessage });
|
||||
}
|
||||
});
|
||||
|
||||
// Clear chat session endpoint
|
||||
router.delete('/chat/:sessionId', async (req: AuthRequest, res: Response) => {
|
||||
try {
|
||||
if (!req.user) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
|
||||
const userId = req.user.userId;
|
||||
const sessionId = req.params.sessionId || 'default';
|
||||
const chatKey = `${userId}-${sessionId}`;
|
||||
|
||||
chatSessions.delete(chatKey);
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: String(error) });
|
||||
console.error('Clear chat error:', error);
|
||||
res.status(500).json({ error: 'Failed to clear chat session' });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user