File size: 2,927 Bytes
f499d4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const BASE = import.meta.env.VITE_API_URL || 'http://localhost:8000';
const TOKEN_KEY = 'researchmind_token';
let authToken = window.localStorage.getItem(TOKEN_KEY) || '';

async function request(path, options = {}) {
  const headers = { ...options.headers };
  if (!options.skipJsonHeader) {
    headers['Content-Type'] = 'application/json';
  }
  if (authToken) {
    headers.Authorization = `Bearer ${authToken}`;
  }

  const res = await fetch(`${BASE}${path}`, {
    headers,
    ...options,
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({ detail: res.statusText }));
    throw new Error(err.detail || 'Request failed');
  }
  return res.json();
}

export const api = {
  setToken: (token) => {
    authToken = token || '';
    if (authToken) {
      window.localStorage.setItem(TOKEN_KEY, authToken);
    } else {
      window.localStorage.removeItem(TOKEN_KEY);
    }
  },
  getToken: () => authToken,

  // Auth
  register: (username, password) =>
    request('/auth/register', {
      method: 'POST',
      body: JSON.stringify({ username, password }),
    }),
  login: (username, password) =>
    request('/auth/login', {
      method: 'POST',
      body: JSON.stringify({ username, password }),
    }),
  me: () => request('/auth/me'),

  // Health
  health: () => request('/health'),

  // Index
  indexStatus: () => request('/index/status'),
  clearIndex: () => request('/index', { method: 'DELETE' }),

  // Upload
  uploadPdf: (file, sessionId = 'default') => {
    const form = new FormData();
    form.append('file', file);
    return fetch(`${BASE}/upload?session_id=${sessionId}`, {
      method: 'POST',
      headers: authToken ? { Authorization: `Bearer ${authToken}` } : {},
      body: form,
    }).then(r => {
      if (!r.ok) return r.json().then(e => Promise.reject(new Error(e.detail)));
      return r.json();
    });
  },

  // Ask
  ask: (question, sessionId = 'default', opts = {}) =>
    request('/ask', {
      method: 'POST',
      body: JSON.stringify({
        question,
        session_id: sessionId,
        enable_nli: opts.enableNli ?? false,
        use_memory_context: opts.useMemory ?? true,
      }),
    }),

  // Sessions
  createSession: (name) =>
    request('/session/create', {
      method: 'POST',
      body: JSON.stringify({ session_name: name }),
    }),
  getSession: (id) => request(`/session/${id}`),
  getHistory: (id) => request(`/session/${id}/history`),
  listSessions: () => request('/sessions'),
  clearSession: (id) => request(`/session/${id}/clear`, { method: 'DELETE' }),

  // Pins
  addPin: (sessionId, text, sourceQuestion, fromDoc) =>
    request(`/session/${sessionId}/pin`, {
      method: 'POST',
      body: JSON.stringify({ session_id: sessionId, text, source_question: sourceQuestion, from_doc: fromDoc }),
    }),
  removePin: (sessionId, pinId) =>
    request(`/session/${sessionId}/pin/${pinId}`, { method: 'DELETE' }),
};