File size: 7,657 Bytes
59ad4aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d808371
 
 
 
 
 
 
59ad4aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d808371
 
 
 
 
d738108
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59ad4aa
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// ─── Types ────────────────────────────────────────────────────────────────────

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;
  created_at: string;
  sources?: ChatSource[];
}

export interface RoomDetail extends Room {
  messages: RoomMessage[];
}

export type DocumentStatus = "pending" | "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;
}

// ─── Base Client ──────────────────────────────────────────────────────────────

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>;
}

// ─── Auth ─────────────────────────────────────────────────────────────────────

export const login = (email: string, password: string) =>
  request<LoginResponse>("/api/login", {
    method: "POST",
    body: JSON.stringify({ email, password }),
  });

// ─── Rooms ────────────────────────────────────────────────────────────────────

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 }),
  });

// ─── Documents ────────────────────────────────────────────────────────────────

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
  );

// ─── Database Clients ─────────────────────────────────────────────────────────

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" }
  );

// ─── Chat ─────────────────────────────────────────────────────────────────────

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 }),
  });