File size: 1,628 Bytes
c5b5cc8
2d41a13
c75d6b0
2d41a13
c5b5cc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
// Basic API client for the analytical backend
const API_BASE_URL = typeof window !== 'undefined' && window.location.hostname !== 'localhost'
    ? '/api'  // Use nginx proxy in production
    : 'http://localhost:8000';

export const api = {
    // Get all conversations
    getConversations: async () => {
        try {
            const res = await fetch(`${API_BASE_URL}/conversation/all`);
            if (!res.ok) throw new Error('Failed to fetch conversations');
            return await res.json();
        } catch (error) {
            console.error(error);
            return [];
        }
    },

    // Create new conversation
    createConversation: async () => {
        try {
            const res = await fetch(`${API_BASE_URL}/chat/new_conversation`, {
                method: 'POST',
            });
            if (!res.ok) throw new Error('Failed to create conversation');
            return await res.json();
        } catch (error) {
            console.error(error);
            return null;
        }
    },

    // Get specific conversation details
    getConversation: async (id) => {
        try {
            const res = await fetch(`${API_BASE_URL}/conversation/${id}`);
            if (res.status === 404) return null; // Gracefully handle not found
            if (!res.ok) throw new Error('Failed to fetch conversation');
            return await res.json();
        } catch (error) {
            console.error('API Error:', error);
            return null;
        }
    },

    // Stream response - helper for EventSource usage
    getChatEndpoint: () => `${API_BASE_URL}/chat/conversation`,
};