|
|
@@ -1,4 +1,4 @@
|
|
|
-import { apiBaseUrl, apiFetch, buildAuthHeaders, refreshAccessToken, notifyAuthError } from "./client.js";
|
|
|
+import { apiBaseUrl, apiFetch, buildAuthHeaders, refreshAccessToken, notifyAuthError, extractErrorMessage } from "./client.js";
|
|
|
|
|
|
export async function sendChat(message, { conversationId, mode } = {}) {
|
|
|
return apiFetch("/api/chat", {
|
|
|
@@ -90,7 +90,7 @@ export async function ingestDocuments(documents) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
-export function ingestFile(file, { source, onProgress } = {}) {
|
|
|
+function sendIngestFile(file, { source, onProgress } = {}) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const form = new FormData();
|
|
|
form.append("file", file);
|
|
|
@@ -109,9 +109,13 @@ export function ingestFile(file, { source, onProgress } = {}) {
|
|
|
} catch {
|
|
|
resolve(xhr.responseText);
|
|
|
}
|
|
|
- } else {
|
|
|
- reject(new Error(xhr.responseText || `http_error:${xhr.status}`));
|
|
|
+ return;
|
|
|
}
|
|
|
+ let payload = null;
|
|
|
+ try { payload = JSON.parse(xhr.responseText); } catch {}
|
|
|
+ const err = new Error(extractErrorMessage(payload, xhr.status));
|
|
|
+ err.status = xhr.status;
|
|
|
+ reject(err);
|
|
|
};
|
|
|
|
|
|
xhr.onerror = () => reject(new Error("network_error"));
|
|
|
@@ -124,6 +128,21 @@ export function ingestFile(file, { source, onProgress } = {}) {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+export async function ingestFile(file, { source, onProgress } = {}) {
|
|
|
+ try {
|
|
|
+ return await sendIngestFile(file, { source, onProgress });
|
|
|
+ } catch (err) {
|
|
|
+ if (err.status !== 401) throw err;
|
|
|
+ try {
|
|
|
+ await refreshAccessToken();
|
|
|
+ } catch {
|
|
|
+ notifyAuthError();
|
|
|
+ throw new Error("session_expired");
|
|
|
+ }
|
|
|
+ return sendIngestFile(file, { source, onProgress });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
export async function ingestUrl(url, { source } = {}) {
|
|
|
return apiFetch("/api/ingest/url", {
|
|
|
method: "POST",
|