File size: 3,320 Bytes
8421ec4
 
 
e7806af
8421ec4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Chat API Service
import { Chat, Message, Document } from '../types';

const API_BASE = '';

// Create a new chat
export async function createChat(title?: string): Promise<Chat> {
  const res = await fetch(`${API_BASE}/chats`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ title: title || 'New Chat' }),
  });
  
  if (!res.ok) {
    const data = await res.json();
    throw new Error(data.detail || 'Failed to create chat');
  }
  
  return res.json();
}

// Get all chats
export async function getChats(): Promise<Chat[]> {
  const res = await fetch(`${API_BASE}/chats`);
  
  if (!res.ok) {
    throw new Error('Failed to fetch chats');
  }
  
  const data = await res.json();
  return data.chats;
}

// Get messages for a chat
export async function getMessages(chatId: string): Promise<Message[]> {
  const res = await fetch(`${API_BASE}/chats/${chatId}/messages`);
  
  if (!res.ok) {
    throw new Error('Failed to fetch messages');
  }
  
  const data = await res.json();
  return data.messages;
}

// Send a message and get AI response
export async function sendMessage(chatId: string, content: string): Promise<{
  user_message: Message;
  assistant_message: Message;
}> {
  const res = await fetch(`${API_BASE}/chats/${chatId}/messages`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ content }),
  });
  
  if (!res.ok) {
    const data = await res.json();
    throw new Error(data.detail || 'Failed to send message');
  }
  
  return res.json();
}

// Delete a chat
export async function deleteChat(chatId: string): Promise<void> {
  const res = await fetch(`${API_BASE}/chats/${chatId}`, {
    method: 'DELETE',
  });
  
  if (!res.ok) {
    throw new Error('Failed to delete chat');
  }
}

// ==============================
// DOCUMENT API FUNCTIONS
// ==============================

// Get all documents
export async function getDocuments(): Promise<Document[]> {
  const res = await fetch(`${API_BASE}/documents`);
  
  if (!res.ok) {
    throw new Error('Failed to fetch documents');
  }
  
  const data = await res.json();
  return data.documents;
}

// Upload a document
export async function uploadDocument(file: File): Promise<Document> {
  const formData = new FormData();
  formData.append('file', file);

  const res = await fetch(`${API_BASE}/documents`, {
    method: 'POST',
    body: formData,
  });

  if (!res.ok) {
    const data = await res.json().catch(() => ({}));
    throw new Error(data.detail || 'Failed to upload document');
  }

  return res.json();
}

// Delete a document
export async function deleteDocument(docId: string): Promise<void> {
  const res = await fetch(`${API_BASE}/documents/${docId}`, {
    method: 'DELETE',
  });
  
  if (!res.ok) {
    throw new Error('Failed to delete document');
  }
}

// Legacy summarize function (backwards compatibility)
export async function summarizeText(text: string): Promise<string> {
  const res = await fetch(`${API_BASE}/ask`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ question: text }),
  });

  const data = await res.json();
  console.log("datalar", data);
  
  if (!res.ok) throw new Error(data.error || 'Unexpected error');

  return data.answer;
}