File size: 9,871 Bytes
a32aee9
 
f1fa34c
 
 
a32aee9
f1fa34c
 
 
 
 
 
 
 
a32aee9
 
 
 
 
 
 
 
 
f1fa34c
a32aee9
 
 
f1fa34c
 
 
 
 
 
 
 
8c4488f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a32aee9
 
 
f1fa34c
 
 
a32aee9
 
 
f1fa34c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a32aee9
f1fa34c
a32aee9
 
f1fa34c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a32aee9
 
f1fa34c
a32aee9
 
 
 
 
 
 
f1fa34c
 
 
 
 
 
 
 
 
 
 
 
a32aee9
 
 
f1fa34c
a32aee9
 
 
 
f1fa34c
a32aee9
 
 
f1fa34c
 
 
 
 
 
 
 
 
 
 
 
a32aee9
 
f1fa34c
 
 
 
a32aee9
f1fa34c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a32aee9
 
 
 
 
 
 
 
f1fa34c
 
a32aee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1fa34c
a32aee9
f1fa34c
a32aee9
f1fa34c
a32aee9
 
f1fa34c
 
a32aee9
 
f1fa34c
a32aee9
 
f1fa34c
a32aee9
 
f1fa34c
a32aee9
 
f1fa34c
a32aee9
 
f1fa34c
 
a32aee9
f1fa34c
a32aee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Typed fetch client for the ResearchRAG FastAPI backend.
// Every function targets an endpoint defined in api/routers/*.py.
//
// Identity is carried by the bearer token only β€” no call passes a user_id, and
// the server would ignore it if one did. See api/security.py.

import {
  clearSession,
  getSession,
  isAccessTokenStale,
  refreshSession,
  sessionFromTokenResponse,
  setSession,
} from "./authStore";
import type {
  AppConfig,
  DocumentInfo,
  IngestMode,
  IngestResult,
  KbStats,
  OpenAlexWork,
  PdfIngestResult,
  SemanticHit,
  Session,
  WhereFilter,
} from "./types";

const RAW_BASE = import.meta.env.VITE_API_BASE_URL;

if (!RAW_BASE && import.meta.env.PROD) {
  // Belt-and-braces: vite.config.ts already fails the build without this var,
  // so reaching here means someone bypassed the build guard.
  throw new Error("VITE_API_BASE_URL is not configured for this production build.");
}

/**
 * Normalise the configured base URL.
 *
 * A value like "my-space.hf.space" (no scheme) is treated by fetch() as a
 * RELATIVE path, so requests silently hit the frontend's own origin, the SPA
 * rewrite answers with index.html, and the app reports a JSON parse error that
 * looks nothing like the actual cause. The build now rejects that, but this
 * keeps an already-deployed bad build working instead of appearing broken.
 */
function normaliseBaseUrl(raw: string): string {
  const trimmed = raw.trim().replace(/\/+$/, "");
  if (!trimmed) return "";
  if (/^https?:\/\//i.test(trimmed)) return trimmed;

  const fixed = `https://${trimmed.replace(/^\/+/, "")}`;
  console.warn(
    `[ResearchRAG] VITE_API_BASE_URL "${raw}" has no scheme; assuming "${fixed}". ` +
      "Set the full https:// URL in your environment to remove this warning.",
  );
  return fixed;
}

export const API_BASE_URL = normaliseBaseUrl(RAW_BASE || "http://localhost:8000");

export class ApiError extends Error {
  status: number;
  /** Correlation id from the server; quote it in bug reports. */
  requestId?: string;
  constructor(message: string, status: number, requestId?: string) {
    super(message);
    this.name = "ApiError";
    this.status = status;
    this.requestId = requestId;
  }
}

/** Pull the message out of the API's error envelope. */
async function toApiError(res: Response): Promise<ApiError> {
  let message = `Request failed (${res.status})`;
  let requestId: string | undefined = res.headers.get("X-Request-ID") ?? undefined;
  try {
    const body = await res.json();
    message = body?.message || body?.detail || message;
    requestId = body?.request_id ?? requestId;
    if (Array.isArray(body?.fields) && body.fields.length) {
      message += `: ${body.fields.map((f: { message: string }) => f.message).join(", ")}`;
    }
  } catch {
    /* non-JSON body */
  }
  return new ApiError(message, res.status, requestId);
}

interface RequestOptions extends RequestInit {
  /** Skip auth entirely (login/register/refresh/config). */
  anonymous?: boolean;
}

async function rawFetch(path: string, init: RequestOptions, token?: string): Promise<Response> {
  const headers = new Headers(init.headers);
  if (!(init.body instanceof FormData)) headers.set("Content-Type", "application/json");
  if (token) headers.set("Authorization", `Bearer ${token}`);
  return fetch(`${API_BASE_URL}${path}`, { ...init, headers });
}

/**
 * Core request wrapper: attaches the bearer token, refreshes proactively when
 * the access token is about to expire, and retries exactly once after a 401.
 * A failed refresh clears the session, which flips the app back to the login
 * screen through the auth store subscription.
 */
async function request<T>(path: string, init: RequestOptions = {}): Promise<T> {
  let token: string | undefined;

  if (!init.anonymous) {
    let session = getSession();
    if (session && isAccessTokenStale(session)) {
      session = await refreshSession(API_BASE_URL);
    }
    token = session?.accessToken;
  }

  let res: Response;
  try {
    res = await rawFetch(path, init, token);
  } catch {
    throw new ApiError(
      `Cannot reach the API at ${API_BASE_URL}. Is the backend running?`,
      0,
    );
  }

  // Reactive refresh: the token expired between check and call, or was revoked.
  if (res.status === 401 && !init.anonymous && getSession()) {
    const refreshed = await refreshSession(API_BASE_URL);
    if (refreshed) {
      try {
        res = await rawFetch(path, init, refreshed.accessToken);
      } catch {
        throw new ApiError(`Cannot reach the API at ${API_BASE_URL}.`, 0);
      }
    } else {
      clearSession();
      throw new ApiError("Your session has expired. Please sign in again.", 401);
    }
  }

  if (!res.ok) throw await toApiError(res);
  if (res.status === 204) return undefined as T;
  return (await res.json()) as T;
}

function jsonBody(data: unknown): RequestOptions {
  return { method: "POST", body: JSON.stringify(data) };
}

// ─── Meta (public) ───────────────────────────────────────────────────────────
export const getConfig = () => request<AppConfig>("/api/config", { anonymous: true });
export const getHealth = () => request<{ status: string }>("/api/health", { anonymous: true });

// ─── Auth ────────────────────────────────────────────────────────────────────
interface TokenPayload {
  access_token: string;
  refresh_token: string;
  expires_in: number;
  user_id: string;
  display_name?: string;
}

export const register = (username: string, display_name: string, password: string) =>
  request<{ success: boolean; message: string }>("/api/auth/register", {
    ...jsonBody({ username, display_name, password }),
    anonymous: true,
  });

export async function login(username: string, password: string): Promise<Session> {
  const data = await request<TokenPayload>("/api/auth/login", {
    ...jsonBody({ username, password }),
    anonymous: true,
  });
  const session = sessionFromTokenResponse(data);
  setSession(session);
  return session;
}

export async function logout(): Promise<void> {
  const session = getSession();
  if (session?.refreshToken) {
    try {
      await request<void>("/api/auth/logout", {
        ...jsonBody({ refresh_token: session.refreshToken }),
        anonymous: true,
      });
    } catch {
      // Server-side revocation is best-effort; always clear locally.
    }
  }
  clearSession();
}

// ─── OpenAlex ────────────────────────────────────────────────────────────────
export const searchOpenAlex = (query: string, max_results: number, api_key?: string) =>
  request<{ works: OpenAlexWork[] }>(
    "/api/openalex/search",
    jsonBody({ query, max_results, api_key: api_key || null }),
  );

export const ingestOpenAlex = (works: OpenAlexWork[], mode: IngestMode) =>
  request<IngestResult>("/api/openalex/ingest", jsonBody({ works, mode }));

export const fetchCitations = (openalex_id: string, api_key?: string) =>
  request<{ references: Record<string, string> }>(
    "/api/openalex/citations",
    jsonBody({ openalex_id, api_key: api_key || null }),
  );

export const classifyTopics = (works: OpenAlexWork[], groq_api_key: string, model?: string) =>
  request<{ labels: Record<string, string> }>(
    "/api/openalex/topics",
    jsonBody({ works, groq_api_key, model: model || null }),
  );

export const getSuggestions = (params: {
  works?: OpenAlexWork[];
  titles?: string[];
  api_key: string;
  model?: string;
  n?: number;
}) =>
  request<{ suggestions: string[] }>(
    "/api/openalex/suggestions",
    jsonBody({
      works: params.works || null,
      titles: params.titles || null,
      api_key: params.api_key,
      model: params.model || null,
      n: params.n ?? 5,
    }),
  );

// ─── Documents / KB ──────────────────────────────────────────────────────────
export const listDocuments = () => request<{ documents: DocumentInfo[] }>("/api/documents");

export const getKbStats = () => request<KbStats>("/api/documents/stats");

export async function uploadPdf(file: File): Promise<PdfIngestResult> {
  const form = new FormData();
  form.append("file", file);
  // Content-Type is intentionally unset so the browser adds the multipart boundary.
  return request<PdfIngestResult>("/api/documents/upload", { method: "POST", body: form });
}

export const summarizeDocument = (title: string, api_key: string, model?: string) =>
  request<{ summary: string }>(
    "/api/documents/summarize",
    jsonBody({ title, api_key, model: model || null }),
  );

export const deleteDocument = (title: string) =>
  request<{ deleted: number }>("/api/documents", {
    method: "DELETE",
    body: JSON.stringify({ title }),
  });

export const clearKnowledgeBase = () =>
  request<{ cleared: number }>("/api/documents/clear", jsonBody({}));

// ─── Semantic search ─────────────────────────────────────────────────────────
export const semanticSearch = (params: {
  query: string;
  top_k: number;
  content_type_filter?: string | null;
  min_score: number;
}) =>
  request<{ results: SemanticHit[] }>(
    "/api/semantic-search",
    jsonBody({
      query: params.query,
      top_k: params.top_k,
      content_type_filter: params.content_type_filter || null,
      min_score: params.min_score,
    }),
  );

export type { WhereFilter };