File size: 1,425 Bytes
bb17288
 
d1d41c8
 
c8a7575
bb17288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 [];
        }
    }
}