File size: 5,259 Bytes
91cd4b2
 
 
 
 
8fa19d9
 
 
 
 
 
 
 
 
 
 
 
91cd4b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa19d9
 
 
 
 
 
 
 
 
 
 
91cd4b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa19d9
91cd4b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fa19d9
 
 
 
 
 
 
 
 
91cd4b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ─── 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 RoomMessage {
  id: string;
  role: "user" | "assistant";
  content: string;
  created_at: string;
}

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 ChatSource {
  document_id: string;
  filename: string;
  page_label: 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" }
  );

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