2024-01-11 04:27:39 +01:00
|
|
|
import { API_BASE, AUTH_TIMESTAMP, fullApiUrl } from "@/utils/constants";
|
2023-12-07 18:09:01 +01:00
|
|
|
import { baseHeaders } from "@/utils/request";
|
2023-12-19 00:48:02 +01:00
|
|
|
import DataConnector from "./dataConnector";
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
const System = {
|
|
|
|
ping: async function () {
|
|
|
|
return await fetch(`${API_BASE}/ping`)
|
2023-08-17 00:43:46 +02:00
|
|
|
.then((res) => res.json())
|
|
|
|
.then((res) => res?.online || false)
|
2023-06-04 04:28:07 +02:00
|
|
|
.catch(() => false);
|
|
|
|
},
|
2024-01-10 22:18:48 +01:00
|
|
|
totalIndexes: async function (slug = null) {
|
2024-01-11 04:27:39 +01:00
|
|
|
const url = new URL(`${fullApiUrl()}/system/system-vectors`);
|
2024-01-10 22:18:48 +01:00
|
|
|
if (!!slug) url.searchParams.append("slug", encodeURIComponent(slug));
|
|
|
|
return await fetch(url.toString(), {
|
2023-06-09 20:27:27 +02:00
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
2023-06-04 04:28:07 +02:00
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Could not find indexes.");
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) => res.vectorCount)
|
|
|
|
.catch(() => 0);
|
|
|
|
},
|
|
|
|
keys: async function () {
|
|
|
|
return await fetch(`${API_BASE}/setup-complete`)
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Could not find setup information.");
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) => res.results)
|
|
|
|
.catch(() => null);
|
|
|
|
},
|
|
|
|
localFiles: async function () {
|
2023-06-09 20:27:27 +02:00
|
|
|
return await fetch(`${API_BASE}/system/local-files`, {
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
2023-06-04 04:28:07 +02:00
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Could not find setup information.");
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) => res.localFiles)
|
|
|
|
.catch(() => null);
|
|
|
|
},
|
2023-10-05 23:34:30 +02:00
|
|
|
needsAuthCheck: function () {
|
|
|
|
const lastAuthCheck = window.localStorage.getItem(AUTH_TIMESTAMP);
|
|
|
|
if (!lastAuthCheck) return true;
|
|
|
|
const expiresAtMs = Number(lastAuthCheck) + 60 * 5 * 1000; // expires in 5 minutes in ms
|
|
|
|
return Number(new Date()) > expiresAtMs;
|
|
|
|
},
|
|
|
|
|
2023-06-09 20:27:27 +02:00
|
|
|
checkAuth: async function (currentToken = null) {
|
2023-10-05 23:34:30 +02:00
|
|
|
const valid = await fetch(`${API_BASE}/system/check-token`, {
|
2023-06-09 20:27:27 +02:00
|
|
|
headers: baseHeaders(currentToken),
|
|
|
|
})
|
|
|
|
.then((res) => res.ok)
|
|
|
|
.catch(() => false);
|
2023-10-05 23:34:30 +02:00
|
|
|
|
|
|
|
window.localStorage.setItem(AUTH_TIMESTAMP, Number(new Date()));
|
|
|
|
return valid;
|
2023-06-09 20:27:27 +02:00
|
|
|
},
|
|
|
|
requestToken: async function (body) {
|
|
|
|
return await fetch(`${API_BASE}/request-token`, {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({ ...body }),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Could not validate login.");
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) => res)
|
|
|
|
.catch((e) => {
|
|
|
|
return { valid: false, message: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-06-17 01:01:27 +02:00
|
|
|
checkDocumentProcessorOnline: async () => {
|
|
|
|
return await fetch(`${API_BASE}/system/document-processing-status`, {
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => res.ok)
|
|
|
|
.catch(() => false);
|
|
|
|
},
|
|
|
|
acceptedDocumentTypes: async () => {
|
|
|
|
return await fetch(`${API_BASE}/system/accepted-document-types`, {
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((res) => res?.types)
|
|
|
|
.catch(() => null);
|
|
|
|
},
|
2023-06-26 20:38:38 +02:00
|
|
|
updateSystem: async (data) => {
|
|
|
|
return await fetch(`${API_BASE}/system/update-env`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { newValues: null, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-07-21 00:25:47 +02:00
|
|
|
updateSystemPassword: async (data) => {
|
|
|
|
return await fetch(`${API_BASE}/system/update-password`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-07-25 19:37:04 +02:00
|
|
|
setupMultiUser: async (data) => {
|
|
|
|
return await fetch(`${API_BASE}/system/enable-multi-user`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-10-23 22:10:34 +02:00
|
|
|
isMultiUserMode: async () => {
|
|
|
|
return await fetch(`${API_BASE}/system/multi-user-mode`, {
|
|
|
|
method: "GET",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((res) => res?.multiUserMode)
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
2023-12-19 00:48:02 +01:00
|
|
|
deleteDocument: async (name) => {
|
2023-06-27 02:20:09 +02:00
|
|
|
return await fetch(`${API_BASE}/system/remove-document`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: baseHeaders(),
|
2023-12-19 00:48:02 +01:00
|
|
|
body: JSON.stringify({ name }),
|
|
|
|
})
|
|
|
|
.then((res) => res.ok)
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
deleteFolder: async (name) => {
|
|
|
|
return await fetch(`${API_BASE}/system/remove-folder`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify({ name }),
|
2023-06-27 02:20:09 +02:00
|
|
|
})
|
|
|
|
.then((res) => res.ok)
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
2023-12-07 23:11:51 +01:00
|
|
|
uploadPfp: async function (formData) {
|
|
|
|
return await fetch(`${API_BASE}/system/upload-pfp`, {
|
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Error uploading pfp.");
|
|
|
|
return { success: true, error: null };
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-08-15 00:22:55 +02:00
|
|
|
uploadLogo: async function (formData) {
|
|
|
|
return await fetch(`${API_BASE}/system/upload-logo`, {
|
|
|
|
method: "POST",
|
|
|
|
body: formData,
|
2023-10-23 22:10:34 +02:00
|
|
|
headers: baseHeaders(),
|
2023-08-15 00:22:55 +02:00
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Error uploading logo.");
|
|
|
|
return { success: true, error: null };
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-10-23 22:10:34 +02:00
|
|
|
fetchLogo: async function () {
|
|
|
|
return await fetch(`${API_BASE}/system/logo`, {
|
2023-08-15 00:22:55 +02:00
|
|
|
method: "GET",
|
|
|
|
cache: "no-cache",
|
|
|
|
})
|
|
|
|
.then((res) => {
|
2023-12-07 23:11:51 +01:00
|
|
|
if (res.ok && res.status !== 204) return res.blob();
|
2023-08-15 00:22:55 +02:00
|
|
|
throw new Error("Failed to fetch logo!");
|
|
|
|
})
|
|
|
|
.then((blob) => URL.createObjectURL(blob))
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
2023-12-07 23:11:51 +01:00
|
|
|
fetchPfp: async function (id) {
|
|
|
|
return await fetch(`${API_BASE}/system/pfp/${id}`, {
|
|
|
|
method: "GET",
|
|
|
|
cache: "no-cache",
|
2024-01-22 23:14:01 +01:00
|
|
|
headers: baseHeaders(),
|
2023-12-07 23:11:51 +01:00
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (res.ok && res.status !== 204) return res.blob();
|
|
|
|
throw new Error("Failed to fetch pfp.");
|
|
|
|
})
|
|
|
|
.then((blob) => (blob ? URL.createObjectURL(blob) : null))
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
removePfp: async function (id) {
|
|
|
|
return await fetch(`${API_BASE}/system/remove-pfp`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (res.ok) return { success: true, error: null };
|
|
|
|
throw new Error("Failed to remove pfp.");
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2023-10-23 22:10:34 +02:00
|
|
|
isDefaultLogo: async function () {
|
|
|
|
return await fetch(`${API_BASE}/system/is-default-logo`, {
|
|
|
|
method: "GET",
|
|
|
|
cache: "no-cache",
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Failed to get is default logo!");
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) => res?.isDefaultLogo)
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
2023-08-15 00:22:55 +02:00
|
|
|
removeCustomLogo: async function () {
|
2023-10-23 22:10:34 +02:00
|
|
|
return await fetch(`${API_BASE}/system/remove-logo`, {
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
2023-08-15 00:22:55 +02:00
|
|
|
.then((res) => {
|
|
|
|
if (res.ok) return { success: true, error: null };
|
|
|
|
throw new Error("Error removing logo!");
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.log(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-08-31 00:28:30 +02:00
|
|
|
getCanDeleteWorkspaces: async function () {
|
|
|
|
return await fetch(`${API_BASE}/system/can-delete-workspaces`, {
|
|
|
|
method: "GET",
|
|
|
|
cache: "no-cache",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Could not fetch can delete workspaces.");
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) => res?.canDelete)
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
2023-08-17 02:30:46 +02:00
|
|
|
getWelcomeMessages: async function () {
|
|
|
|
return await fetch(`${API_BASE}/system/welcome-messages`, {
|
|
|
|
method: "GET",
|
|
|
|
cache: "no-cache",
|
2024-01-22 23:14:01 +01:00
|
|
|
headers: baseHeaders(),
|
2023-08-17 02:30:46 +02:00
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) throw new Error("Could not fetch welcome messages.");
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.then((res) => res.welcomeMessages)
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setWelcomeMessages: async function (messages) {
|
|
|
|
return fetch(`${API_BASE}/system/set-welcome-messages`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify({ messages }),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(res.statusText || "Error setting welcome messages.");
|
|
|
|
}
|
|
|
|
return { success: true, ...res.json() };
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-10-23 22:10:34 +02:00
|
|
|
getApiKeys: async function () {
|
|
|
|
return fetch(`${API_BASE}/system/api-keys`, {
|
2023-08-24 04:15:07 +02:00
|
|
|
method: "GET",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(res.statusText || "Error fetching api key.");
|
|
|
|
}
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { apiKey: null, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
generateApiKey: async function () {
|
|
|
|
return fetch(`${API_BASE}/system/generate-api-key`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(res.statusText || "Error generating api key.");
|
|
|
|
}
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { apiKey: null, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
|
|
|
deleteApiKey: async function () {
|
|
|
|
return fetch(`${API_BASE}/system/api-key`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => res.ok)
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
},
|
2023-11-14 21:31:44 +01:00
|
|
|
customModels: async function (provider, apiKey = null, basePath = null) {
|
2023-10-31 19:38:28 +01:00
|
|
|
return fetch(`${API_BASE}/system/custom-models`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify({
|
|
|
|
provider,
|
|
|
|
apiKey,
|
2023-11-14 21:31:44 +01:00
|
|
|
basePath,
|
2023-10-31 19:38:28 +01:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res.ok) {
|
|
|
|
throw new Error(res.statusText || "Error finding custom models.");
|
|
|
|
}
|
|
|
|
return res.json();
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { models: [], error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-11-09 02:36:54 +01:00
|
|
|
chats: async (offset = 0) => {
|
|
|
|
return await fetch(`${API_BASE}/system/workspace-chats`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify({ offset }),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
},
|
[FEAT] Automated audit logging (#667)
* WIP event logging - new table for events and new settings view for viewing
* WIP add logging
* UI for log rows
* rename files to Logging to prevent getting gitignore
* add metadata for all logging events and colored badges in logs page
* remove unneeded comment
* cleanup namespace for logging
* clean up backend calls
* update logging to show to => from settings changes
* add logging for invitations, created, deleted, and accepted
* add logging for user created, updated, suspended, or removed
* add logging for workspace deleted
* add logging for chat logs exported
* add logging for API keys, LLM, embedder, vector db, embed chat, and reset button
* modify event logs
* update to event log types
* simplify rendering of event badges
---------
Co-authored-by: timothycarambat <rambat1010@gmail.com>
2024-02-07 00:21:40 +01:00
|
|
|
eventLogs: async (offset = 0) => {
|
|
|
|
return await fetch(`${API_BASE}/system/event-logs`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify({ offset }),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
clearEventLogs: async () => {
|
|
|
|
return await fetch(`${API_BASE}/system/event-logs`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-11-09 02:36:54 +01:00
|
|
|
deleteChat: async (chatId) => {
|
|
|
|
return await fetch(`${API_BASE}/system/workspace-chats/${chatId}`, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2024-01-19 02:59:51 +01:00
|
|
|
exportChats: async (type = "csv") => {
|
|
|
|
const url = new URL(`${fullApiUrl()}/system/export-chats`);
|
|
|
|
url.searchParams.append("type", encodeURIComponent(type));
|
|
|
|
return await fetch(url, {
|
2023-11-09 02:36:54 +01:00
|
|
|
method: "GET",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
})
|
|
|
|
.then((res) => res.text())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
},
|
2023-12-07 23:11:51 +01:00
|
|
|
updateUser: async (data) => {
|
|
|
|
return await fetch(`${API_BASE}/system/user`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: baseHeaders(),
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
})
|
|
|
|
.then((res) => res.json())
|
|
|
|
.catch((e) => {
|
|
|
|
console.error(e);
|
|
|
|
return { success: false, error: e.message };
|
|
|
|
});
|
|
|
|
},
|
2023-12-19 00:48:02 +01:00
|
|
|
dataConnectors: DataConnector,
|
2023-06-04 04:28:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default System;
|