muhammad1707's picture
Use relative API base for Space deployment
e7806af
Raw
History Blame Contribute Delete
3.32 kB
// 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;
}