File size: 3,632 Bytes
f5628ad
 
19a4cd1
 
f5628ad
19a4cd1
f5628ad
 
e3e8fd0
 
 
 
 
f5628ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3e8fd0
f5628ad
 
 
 
e3e8fd0
f5628ad
 
34ca534
f5628ad
 
 
 
34ca534
 
 
 
 
e3e8fd0
 
f5628ad
 
2868f80
ee2b23c
4f14900
ee2b23c
 
 
 
 
 
 
 
 
 
 
 
 
e3e8fd0
 
 
 
2868f80
 
 
6360789
19a4cd1
 
 
 
 
 
 
6360789
 
 
 
19a4cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from "axios";

const API_BASE = import.meta.env.VITE_API_URL || "/api";

const api = axios.create({
  baseURL: API_BASE,
});

export async function sendMessage(question, webSearch = false, workspaceId = "default") {
  const { data } = await api.post(`/chat?workspace=${encodeURIComponent(workspaceId)}`, {
    question,
    web_search: webSearch,
  });
  return data;
}

export async function clearMemory() {
  const { data } = await api.delete("/chat/memory");
  return data;
}

export async function getSessionEvalLog() {
  const { data } = await api.get("/eval/session");
  return data;
}

export async function runPrecisionEval() {
  const { data } = await api.post("/eval/precision");
  return data;
}

export async function uploadFiles(fileList, workspaceId = "default") {
  const formData = new FormData();
  for (const file of fileList) {
    formData.append("files", file);
  }
  formData.append("workspace", workspaceId);
  const { data } = await api.post("/upload", formData, {
    headers: { "Content-Type": "multipart/form-data" },
    timeout: 30000,
  });
  return data;
}

export async function getUploadStatus(jobId) {
  const { data } = await api.get(`/upload/status/${jobId}`);
  return data;
}

export async function getDocuments(workspaceId = "default") {
  const { data } = await api.get(`/documents?workspace=${encodeURIComponent(workspaceId)}`);
  return data;
}

export async function uploadUrl(url, workspaceId = "default") {
  const { data } = await api.post("/upload/url", { url, workspace: workspaceId }, { timeout: 60000 });
  return data;
}

export async function getWorkspaces() {
  const { data } = await api.get("/workspaces");
  return data;
}

export async function deleteWorkspace(workspaceId) {
  const { data } = await api.delete(`/workspaces/${encodeURIComponent(workspaceId)}`);
  return data;
}

export async function deleteDocument(filename, workspaceId = "default") {
  const { data } = await api.delete(
    `/documents/${encodeURIComponent(filename)}?workspace=${encodeURIComponent(workspaceId)}`
  );
  return data;
}

export async function streamChat(question, workspaceId = "default", { onToken, onDone, onError }, filterDocs = null) {
  let response;
  try {
    response = await fetch(
      `${API_BASE}/chat?workspace=${encodeURIComponent(workspaceId)}`,
      {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({
          question,
          filter_docs: filterDocs && filterDocs.length > 0 ? filterDocs : null,
        }),
      }
    );
  } catch (err) {
    onError(err.message || "Network error");
    return;
  }

  if (!response.ok) {
    const text = await response.text().catch(() => "");
    onError(text || `HTTP ${response.status}`);
    return;
  }

  const reader = response.body.getReader();
  const decoder = new TextDecoder();
  let buffer = "";

  try {
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      buffer += decoder.decode(value, { stream: true });
      const lines = buffer.split("\n");
      buffer = lines.pop() ?? ""; // keep incomplete line
      for (const line of lines) {
        if (!line.startsWith("data: ")) continue;
        try {
          const event = JSON.parse(line.slice(6));
          if (event.type === "token") onToken(event.content);
          else if (event.type === "done") onDone(event);
          else if (event.type === "error") onError(event.message ?? "Unknown error");
        } catch {
          // malformed SSE line — skip
        }
      }
    }
  } finally {
    reader.releaseLock();
  }
}