anything-llm/server/endpoints/embedManagement.js
Sean Hatfield d789920a19
[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-06 15:21:40 -08:00

130 lines
3.8 KiB
JavaScript

const { EmbedChats } = require("../models/embedChats");
const { EmbedConfig } = require("../models/embedConfig");
const { EventLogs } = require("../models/eventLogs");
const { Workspace } = require("../models/workspace");
const { reqBody, userFromSession } = require("../utils/http");
const { validEmbedConfigId } = require("../utils/middleware/embedMiddleware");
const {
flexUserRoleValid,
ROLES,
} = require("../utils/middleware/multiUserProtected");
const { validatedRequest } = require("../utils/middleware/validatedRequest");
function embedManagementEndpoints(app) {
if (!app) return;
app.get(
"/embeds",
[validatedRequest, flexUserRoleValid([ROLES.admin])],
async (_, response) => {
try {
const embeds = await EmbedConfig.whereWithWorkspace({}, null, {
createdAt: "desc",
});
response.status(200).json({ embeds });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
}
);
app.post(
"/embeds/new",
[validatedRequest, flexUserRoleValid([ROLES.admin])],
async (request, response) => {
try {
const user = await userFromSession(request, response);
const data = reqBody(request);
const { embed, message: error } = await EmbedConfig.new(data, user?.id);
await EventLogs.logEvent(
"embed_created",
{ embedId: embed.id },
user?.id
);
response.status(200).json({ embed, error });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
}
);
app.post(
"/embed/update/:embedId",
[validatedRequest, flexUserRoleValid([ROLES.admin]), validEmbedConfigId],
async (request, response) => {
try {
const user = await userFromSession(request, response);
const { embedId } = request.params;
const updates = reqBody(request);
const { success, error } = await EmbedConfig.update(embedId, updates);
await EventLogs.logEvent("embed_updated", { embedId }, user?.id);
response.status(200).json({ success, error });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
}
);
app.delete(
"/embed/:embedId",
[validatedRequest, flexUserRoleValid([ROLES.admin]), validEmbedConfigId],
async (request, response) => {
try {
const { embedId } = request.params;
await EmbedConfig.delete({ id: Number(embedId) });
await EventLogs.logEvent(
"embed_deleted",
{ embedId },
response?.locals?.user?.id
);
response.status(200).json({ success: true, error: null });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
}
);
app.post(
"/embed/chats",
[validatedRequest, flexUserRoleValid([ROLES.admin])],
async (request, response) => {
try {
const { offset = 0, limit = 20 } = reqBody(request);
const embedChats = await EmbedChats.whereWithEmbedAndWorkspace(
{},
limit,
{ id: "desc" },
offset * limit
);
const totalChats = await EmbedChats.count();
const hasPages = totalChats > (offset + 1) * limit;
response.status(200).json({ chats: embedChats, hasPages, totalChats });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
}
);
app.delete(
"/embed/chats/:chatId",
[validatedRequest, flexUserRoleValid([ROLES.admin])],
async (request, response) => {
try {
const { chatId } = request.params;
await EmbedChats.delete({ id: Number(chatId) });
response.status(200).json({ success: true, error: null });
} catch (e) {
console.error(e);
response.sendStatus(500).end();
}
}
);
}
module.exports = { embedManagementEndpoints };