| |
|
|
| export interface LoginResponse { |
| status: string; |
| message: string; |
| data: { |
| id: string; |
| fullname: string; |
| email: string; |
| company: string; |
| company_size: string; |
| function: string; |
| site: string; |
| role: string; |
| status: string; |
| created_at: string; |
| }; |
| } |
|
|
| export interface Room { |
| id: string; |
| title: string; |
| created_at: string; |
| updated_at: string | null; |
| } |
|
|
| export interface CreateRoomResponse { |
| status: string; |
| message: string; |
| data: Room; |
| } |
|
|
| export interface ChatSource { |
| document_id: string; |
| filename: string; |
| page_label: string | null; |
| } |
|
|
| export interface RoomMessage { |
| id: string; |
| role: "user" | "assistant"; |
| content: string; |
| audio_text?: string | null; |
| created_at: string; |
| sources?: ChatSource[]; |
| } |
|
|
| export interface RoomDetail extends Room { |
| messages: RoomMessage[]; |
| } |
|
|
| export type DocumentStatus = "uploaded" | "processing" | "completed" | "failed"; |
|
|
| export interface ApiDocument { |
| id: string; |
| filename: string; |
| status: DocumentStatus; |
| file_size: number; |
| file_type: string; |
| created_at: string; |
| } |
|
|
| export interface UploadDocumentResponse { |
| status: string; |
| message: string; |
| data: { id: string; filename: string; status: DocumentStatus }; |
| } |
|
|
| export interface DocTypeInfo { |
| doc_type: string; |
| max_size: number; |
| status: "active" | "inactive"; |
| message: string | null; |
| } |
|
|
| |
|
|
| const BASE_URL = ((import.meta as unknown as { env: Record<string, string> }).env.VITE_API_BASE_URL) ?? ""; |
|
|
| async function request<T>(path: string, options?: RequestInit): Promise<T> { |
| const res = await fetch(`${BASE_URL}${path}`, { |
| headers: { "Content-Type": "application/json", ...options?.headers }, |
| ...options, |
| }); |
| if (!res.ok) { |
| const err = await res |
| .json() |
| .catch(() => ({ detail: `HTTP ${res.status}` })); |
| throw new Error(err.detail ?? `HTTP ${res.status}`); |
| } |
| return res.json() as Promise<T>; |
| } |
|
|
| |
|
|
| export const login = (email: string, password: string) => |
| request<LoginResponse>("/api/login", { |
| method: "POST", |
| body: JSON.stringify({ email, password }), |
| }); |
|
|
| |
|
|
| export const getRooms = (userId: string) => |
| request<Room[]>(`/api/v1/rooms/${userId}`); |
|
|
| export const getRoom = (roomId: string) => |
| request<RoomDetail>(`/api/v1/room/${roomId}`); |
|
|
| export const deleteRoom = (roomId: string, userId: string) => |
| request<{ status: string; message: string }>( |
| `/api/v1/room/${roomId}?user_id=${userId}`, |
| { method: "DELETE" } |
| ); |
|
|
| export const createRoom = (userId: string, title?: string) => |
| request<CreateRoomResponse>("/api/v1/room/create", { |
| method: "POST", |
| body: JSON.stringify({ user_id: userId, title }), |
| }); |
|
|
| |
|
|
| export const getDocuments = (userId: string) => |
| request<ApiDocument[]>(`/api/v1/documents/${userId}`); |
|
|
| export const uploadDocument = async ( |
| userId: string, |
| file: File |
| ): Promise<UploadDocumentResponse> => { |
| const form = new FormData(); |
| form.append("file", file); |
| const res = await fetch( |
| `${BASE_URL}/api/v1/document/upload?user_id=${userId}`, |
| { method: "POST", body: form } |
| ); |
| if (!res.ok) { |
| const err = await res |
| .json() |
| .catch(() => ({ detail: `HTTP ${res.status}` })); |
| throw new Error(err.detail ?? `HTTP ${res.status}`); |
| } |
| return res.json() as Promise<UploadDocumentResponse>; |
| }; |
|
|
| export const processDocument = (userId: string, documentId: string) => |
| request<{ |
| status: string; |
| message: string; |
| data: { document_id: string; chunks_processed: number }; |
| }>( |
| `/api/v1/document/process?document_id=${documentId}&user_id=${userId}`, |
| { method: "POST" } |
| ); |
|
|
| export const deleteDocument = (userId: string, documentId: string) => |
| request<{ status: string; message: string }>( |
| `/api/v1/document/delete?document_id=${documentId}&user_id=${userId}`, |
| { method: "DELETE" } |
| ); |
|
|
| export const getDocumentTypes = (): Promise<DocTypeInfo[]> => |
| request<{ status: string; data: DocTypeInfo[] }>("/api/v1/documents/doctypes").then( |
| (res) => res.data |
| ); |
|
|
| |
|
|
| export type DbType = "postgres" | "mysql" | "sqlserver" | "supabase" | "bigquery" | "snowflake"; |
|
|
| export interface DbTypeField { |
| name: string; |
| type: "string" | "integer" | "select" | "boolean"; |
| required: boolean; |
| default: string | number | boolean | null; |
| description: string; |
| options?: string[]; |
| sensitive?: boolean; |
| } |
|
|
| export interface DbTypeInfo { |
| db_type: DbType; |
| display_name: string; |
| logo: string; |
| status: "active" | "inactive"; |
| message: string | null; |
| fields: DbTypeField[]; |
| } |
|
|
| export interface DatabaseClient { |
| id: string; |
| user_id: string; |
| name: string; |
| db_type: DbType; |
| status: "active" | "inactive"; |
| created_at: string; |
| updated_at: string | null; |
| } |
|
|
| export interface IngestResponse { |
| status: string; |
| client_id: string; |
| chunks_ingested: number; |
| } |
|
|
| export const getDatabaseClientTypes = (): Promise<DbTypeInfo[]> => |
| request<DbTypeInfo[]>("/api/v1/database-clients/dbtypes"); |
|
|
| export const connectDatabase = ( |
| userId: string, |
| dbType: DbType, |
| name: string, |
| credentials: Record<string, string | number | boolean> |
| ): Promise<DatabaseClient> => |
| request<DatabaseClient>(`/api/v1/database-clients?user_id=${userId}`, { |
| method: "POST", |
| body: JSON.stringify({ name, db_type: dbType, credentials }), |
| }); |
|
|
| export const getDatabaseClients = (userId: string): Promise<DatabaseClient[]> => |
| request<DatabaseClient[]>(`/api/v1/database-clients/${userId}`); |
|
|
| export const deleteDatabaseClient = (clientId: string, userId: string) => |
| request<{ status: string; message: string }>( |
| `/api/v1/database-clients/${clientId}?user_id=${userId}`, |
| { method: "DELETE" } |
| ); |
|
|
| export const ingestDatabaseClient = (clientId: string, userId: string): Promise<IngestResponse> => |
| request<IngestResponse>( |
| `/api/v1/database-clients/${clientId}/ingest?user_id=${userId}`, |
| { method: "POST" } |
| ); |
|
|
| |
|
|
| export const rebuildKnowledge = (userId: string) => |
| request<{ status: string; message: string }>( |
| `/api/v1/knowledge/rebuild?user_id=${userId}`, |
| { method: "POST" } |
| ); |
|
|
| |
|
|
| export const streamChat = ( |
| userId: string, |
| roomId: string, |
| message: string |
| ): Promise<Response> => |
| fetch(`${BASE_URL}/api/v1/chat/stream`, { |
| method: "POST", |
| headers: { "Content-Type": "application/json" }, |
| body: JSON.stringify({ user_id: userId, room_id: roomId, message }), |
| }); |
|
|