PROJECTS / src /lib /api.ts
Adeen
Fix: correct Nginx API routing prefix for FastAPI and set explicit relative path in frontend
c8a7575
import axios from 'axios';
// When running in Docker on HF Spaces, the backend is on the same host,
// routed by Nginx under the `/api` or `/` path
const API_URL = process.env.NEXT_PUBLIC_API_URL || '/api';
export class ApiService {
static async createSession() {
try {
const response = await axios.post(`${API_URL}/session/create`);
return response.data.session_id;
} catch (error) {
console.error('Error creating session:', error);
throw error;
}
}
static async sendMessage(message: string, sessionId: string, isEnhanced: boolean = true) {
try {
const endpoint = isEnhanced ? `${API_URL}/api/v2/chat/${sessionId}` : `${API_URL}/chat/${sessionId}`;
const payload = isEnhanced ? { user_message: message, context: "" } : { user_message: message };
const response = await axios.post(endpoint, payload);
return response.data.ai_response;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
}
static async getHistory(sessionId: string) {
try {
const response = await axios.get(`${API_URL}/history/${sessionId}`);
return response.data.history || [];
} catch (error) {
console.error('Error getting history:', error);
return [];
}
}
}