Commit 1e7d1256 authored by Mahmoud Aglan's avatar Mahmoud Aglan

يبلابيلةيلؤ

parent fd8a1643
......@@ -10,13 +10,20 @@ function authHeader(token) {
return token ? { Authorization: `Bearer ${token}` } : {};
}
function extractError(err, defaultMsg) {
let msg = err.detail || err.message || defaultMsg;
if (Array.isArray(msg)) return msg.map(m => m.msg || JSON.stringify(m)).join(", ");
if (typeof msg === "object" && msg !== null) return msg.message || JSON.stringify(msg);
return String(msg);
}
async function request(method, path, token, body) {
const opts = { method, headers: headers(token) };
if (body) opts.body = JSON.stringify(body);
const res = await fetch(`${BASE}${path}`, opts);
if (!res.ok) {
const err = await res.json().catch(() => ({ detail: res.statusText }));
throw new Error(err.detail || err.message || "Request failed");
throw new Error(extractError(err, "Request failed"));
}
return res.json();
}
......@@ -42,7 +49,7 @@ export async function* streamMessage(token, chatId, body, signal) {
});
if (!res.ok) {
const err = await res.json().catch(() => ({ detail: res.statusText }));
throw new Error(err.detail || "Stream failed");
throw new Error(extractError(err, "Stream failed"));
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
......@@ -72,7 +79,7 @@ export async function uploadAttachments(token, chatId, files) {
const res = await fetch(`${BASE}/chats/${chatId}/attachments`, {
method: "POST", headers: authHeader(token), body: form,
});
if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(err.detail || "Upload failed"); }
if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(extractError(err, "Upload failed")); }
return res.json();
}
export function getAttachmentUrl(attachmentId) { return `${BASE}/attachments/${attachmentId}/file`; }
......@@ -90,7 +97,7 @@ export async function uploadDocuments(token, kbId, files) {
const form = new FormData();
for (const file of files) form.append("files", file);
const res = await fetch(`${BASE}/knowledge/${kbId}/upload`, { method: "POST", headers: authHeader(token), body: form });
if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(err.detail || "Upload failed"); }
if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(extractError(err, "Upload failed")); }
return res.json();
}
export const uploadDocument = (token, kbId, file) => uploadDocuments(token, kbId, [file]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment