2024-04-01 21:06:47 +02:00
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
2024-05-14 20:57:21 +02:00
|
|
|
const {
|
|
|
|
reqBody,
|
|
|
|
multiUserMode,
|
|
|
|
userFromSession,
|
|
|
|
safeJsonParse,
|
|
|
|
} = require("../utils/http");
|
2024-04-01 21:06:47 +02:00
|
|
|
const { normalizePath } = require("../utils/files");
|
2023-06-08 06:31:35 +02:00
|
|
|
const { Workspace } = require("../models/workspace");
|
|
|
|
const { Document } = require("../models/documents");
|
|
|
|
const { DocumentVectors } = require("../models/vectors");
|
|
|
|
const { WorkspaceChats } = require("../models/workspaceChats");
|
|
|
|
const { getVectorDbClass } = require("../utils/helpers");
|
2024-04-01 21:06:47 +02:00
|
|
|
const { handleFileUpload, handlePfpUpload } = require("../utils/files/multer");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { validatedRequest } = require("../utils/middleware/validatedRequest");
|
2023-08-15 02:42:17 +02:00
|
|
|
const { Telemetry } = require("../models/telemetry");
|
2024-01-22 23:14:01 +01:00
|
|
|
const {
|
|
|
|
flexUserRoleValid,
|
|
|
|
ROLES,
|
|
|
|
} = require("../utils/middleware/multiUserProtected");
|
[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
|
|
|
const { EventLogs } = require("../models/eventLogs");
|
2024-02-06 20:24:33 +01:00
|
|
|
const {
|
|
|
|
WorkspaceSuggestedMessages,
|
|
|
|
} = require("../models/workspacesSuggestedMessages");
|
2024-02-13 20:33:05 +01:00
|
|
|
const { validWorkspaceSlug } = require("../utils/middleware/validWorkspace");
|
2024-02-14 21:32:07 +01:00
|
|
|
const { convertToChatHistory } = require("../utils/helpers/chat/responses");
|
2024-02-17 01:32:25 +01:00
|
|
|
const { CollectorApi } = require("../utils/collectorApi");
|
2024-03-12 19:36:59 +01:00
|
|
|
const {
|
|
|
|
determineWorkspacePfpFilepath,
|
|
|
|
fetchPfp,
|
|
|
|
} = require("../utils/files/pfp");
|
2024-05-14 20:57:21 +02:00
|
|
|
const { getTTSProvider } = require("../utils/TextToSpeech");
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
function workspaceEndpoints(app) {
|
|
|
|
if (!app) return;
|
|
|
|
|
2024-03-12 19:36:59 +01:00
|
|
|
const responseCache = new Map();
|
|
|
|
|
2023-11-13 23:51:16 +01:00
|
|
|
app.post(
|
|
|
|
"/workspace/new",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-11-13 23:51:16 +01:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const { name = null, onboardingComplete = false } = reqBody(request);
|
|
|
|
const { workspace, message } = await Workspace.new(name, user?.id);
|
|
|
|
await Telemetry.sendTelemetry(
|
|
|
|
"workspace_created",
|
|
|
|
{
|
|
|
|
multiUserMode: multiUserMode(response),
|
|
|
|
LLMSelection: process.env.LLM_PROVIDER || "openai",
|
2023-12-07 17:53:37 +01:00
|
|
|
Embedder: process.env.EMBEDDING_ENGINE || "inherit",
|
2024-05-13 21:22:53 +02:00
|
|
|
VectorDbSelection: process.env.VECTOR_DB || "lancedb",
|
2023-11-13 23:51:16 +01:00
|
|
|
},
|
|
|
|
user?.id
|
|
|
|
);
|
[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
|
|
|
|
|
|
|
await EventLogs.logEvent(
|
|
|
|
"workspace_created",
|
|
|
|
{
|
|
|
|
workspaceName: workspace?.name || "Unknown Workspace",
|
|
|
|
},
|
|
|
|
user?.id
|
|
|
|
);
|
2023-11-13 23:51:16 +01:00
|
|
|
if (onboardingComplete === true)
|
|
|
|
await Telemetry.sendTelemetry("onboarding_complete");
|
2023-10-26 22:49:01 +02:00
|
|
|
|
2023-11-13 23:51:16 +01:00
|
|
|
response.status(200).json({ workspace, message });
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
2023-06-08 22:13:48 +02:00
|
|
|
}
|
2023-11-13 23:51:16 +01:00
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/update",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const { slug = null } = request.params;
|
|
|
|
const data = reqBody(request);
|
|
|
|
const currWorkspace = multiUserMode(response)
|
2023-09-28 23:00:03 +02:00
|
|
|
? await Workspace.getWithUser(user, { slug })
|
|
|
|
: await Workspace.get({ slug });
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
if (!currWorkspace) {
|
|
|
|
response.sendStatus(400).end();
|
|
|
|
return;
|
|
|
|
}
|
2024-03-21 23:25:42 +01:00
|
|
|
await Workspace.trackChange(currWorkspace, data, user);
|
2023-07-25 19:37:04 +02:00
|
|
|
const { workspace, message } = await Workspace.update(
|
|
|
|
currWorkspace.id,
|
|
|
|
data
|
|
|
|
);
|
|
|
|
response.status(200).json({ workspace, message });
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
2023-06-15 08:12:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
);
|
2023-06-15 08:12:59 +02:00
|
|
|
|
2023-06-17 01:01:27 +02:00
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/upload",
|
2024-04-01 21:06:47 +02:00
|
|
|
[
|
|
|
|
validatedRequest,
|
|
|
|
flexUserRoleValid([ROLES.admin, ROLES.manager]),
|
|
|
|
handleFileUpload,
|
|
|
|
],
|
2023-08-23 04:18:47 +02:00
|
|
|
async function (request, response) {
|
2024-02-17 01:32:25 +01:00
|
|
|
const Collector = new CollectorApi();
|
2023-06-17 01:01:27 +02:00
|
|
|
const { originalname } = request.file;
|
2024-02-17 01:32:25 +01:00
|
|
|
const processingOnline = await Collector.online();
|
2023-06-17 01:01:27 +02:00
|
|
|
|
|
|
|
if (!processingOnline) {
|
2023-08-23 04:18:47 +02:00
|
|
|
response
|
|
|
|
.status(500)
|
|
|
|
.json({
|
|
|
|
success: false,
|
2023-12-15 00:14:56 +01:00
|
|
|
error: `Document processing API is not online. Document ${originalname} will not be processed automatically.`,
|
2023-08-23 04:18:47 +02:00
|
|
|
})
|
|
|
|
.end();
|
2023-08-25 02:57:35 +02:00
|
|
|
return;
|
2023-06-17 01:01:27 +02:00
|
|
|
}
|
|
|
|
|
2024-02-17 01:32:25 +01:00
|
|
|
const { success, reason } = await Collector.processDocument(originalname);
|
2023-06-17 01:01:27 +02:00
|
|
|
if (!success) {
|
2023-08-23 04:18:47 +02:00
|
|
|
response.status(500).json({ success: false, error: reason }).end();
|
2023-08-25 02:57:35 +02:00
|
|
|
return;
|
2023-06-17 01:01:27 +02:00
|
|
|
}
|
|
|
|
|
2024-02-17 01:32:25 +01:00
|
|
|
Collector.log(
|
2023-06-17 01:01:27 +02:00
|
|
|
`Document ${originalname} uploaded processed and successfully. It is now available in documents.`
|
|
|
|
);
|
2023-08-15 02:42:17 +02:00
|
|
|
await Telemetry.sendTelemetry("document_uploaded");
|
[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
|
|
|
await EventLogs.logEvent(
|
|
|
|
"document_uploaded",
|
|
|
|
{
|
|
|
|
documentName: originalname,
|
|
|
|
},
|
|
|
|
response.locals?.user?.id
|
|
|
|
);
|
2023-08-23 04:18:47 +02:00
|
|
|
response.status(200).json({ success: true, error: null });
|
2023-11-17 02:15:01 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/upload-link",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-11-17 02:15:01 +01:00
|
|
|
async (request, response) => {
|
2024-02-17 01:32:25 +01:00
|
|
|
const Collector = new CollectorApi();
|
2023-11-17 02:15:01 +01:00
|
|
|
const { link = "" } = reqBody(request);
|
2024-02-17 01:32:25 +01:00
|
|
|
const processingOnline = await Collector.online();
|
2023-11-17 02:15:01 +01:00
|
|
|
|
|
|
|
if (!processingOnline) {
|
|
|
|
response
|
|
|
|
.status(500)
|
|
|
|
.json({
|
|
|
|
success: false,
|
2023-12-15 00:14:56 +01:00
|
|
|
error: `Document processing API is not online. Link ${link} will not be processed automatically.`,
|
2023-11-17 02:15:01 +01:00
|
|
|
})
|
|
|
|
.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-17 01:32:25 +01:00
|
|
|
const { success, reason } = await Collector.processLink(link);
|
2023-11-17 02:15:01 +01:00
|
|
|
if (!success) {
|
|
|
|
response.status(500).json({ success: false, error: reason }).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-17 01:32:25 +01:00
|
|
|
Collector.log(
|
2023-11-17 02:15:01 +01:00
|
|
|
`Link ${link} uploaded processed and successfully. It is now available in documents.`
|
|
|
|
);
|
|
|
|
await Telemetry.sendTelemetry("link_uploaded");
|
[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
|
|
|
await EventLogs.logEvent(
|
|
|
|
"link_uploaded",
|
|
|
|
{ link },
|
|
|
|
response.locals?.user?.id
|
|
|
|
);
|
2023-11-17 02:15:01 +01:00
|
|
|
response.status(200).json({ success: true, error: null });
|
2023-06-17 01:01:27 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/update-embeddings",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const { slug = null } = request.params;
|
|
|
|
const { adds = [], deletes = [] } = reqBody(request);
|
|
|
|
const currWorkspace = multiUserMode(response)
|
2023-09-28 23:00:03 +02:00
|
|
|
? await Workspace.getWithUser(user, { slug })
|
|
|
|
: await Workspace.get({ slug });
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
if (!currWorkspace) {
|
|
|
|
response.sendStatus(400).end();
|
|
|
|
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
|
|
|
await Document.removeDocuments(
|
|
|
|
currWorkspace,
|
|
|
|
deletes,
|
|
|
|
response.locals?.user?.id
|
|
|
|
);
|
2024-01-18 20:40:48 +01:00
|
|
|
const { failedToEmbed = [], errors = [] } = await Document.addDocuments(
|
2023-10-26 19:57:37 +02:00
|
|
|
currWorkspace,
|
[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
|
|
|
adds,
|
|
|
|
response.locals?.user?.id
|
2023-10-26 19:57:37 +02:00
|
|
|
);
|
2023-09-28 23:00:03 +02:00
|
|
|
const updatedWorkspace = await Workspace.get({ id: currWorkspace.id });
|
2023-10-26 19:57:37 +02:00
|
|
|
response.status(200).json({
|
|
|
|
workspace: updatedWorkspace,
|
|
|
|
message:
|
2024-01-18 20:40:48 +01:00
|
|
|
failedToEmbed.length > 0
|
|
|
|
? `${failedToEmbed.length} documents failed to add.\n\n${errors
|
|
|
|
.map((msg) => `${msg}`)
|
|
|
|
.join("\n\n")}`
|
2023-10-26 19:57:37 +02:00
|
|
|
: null,
|
|
|
|
});
|
2023-07-25 19:37:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
2023-06-08 22:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
app.delete(
|
|
|
|
"/workspace/:slug",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
2023-06-08 22:13:48 +02:00
|
|
|
try {
|
2023-07-25 19:37:04 +02:00
|
|
|
const { slug = "" } = request.params;
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const VectorDb = getVectorDbClass();
|
|
|
|
const workspace = multiUserMode(response)
|
2023-09-28 23:00:03 +02:00
|
|
|
? await Workspace.getWithUser(user, { slug })
|
|
|
|
: await Workspace.get({ slug });
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
if (!workspace) {
|
|
|
|
response.sendStatus(400).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
await WorkspaceChats.delete({ workspaceId: Number(workspace.id) });
|
2023-07-25 19:37:04 +02:00
|
|
|
await DocumentVectors.deleteForWorkspace(workspace.id);
|
2023-09-28 23:00:03 +02:00
|
|
|
await Document.delete({ workspaceId: Number(workspace.id) });
|
|
|
|
await Workspace.delete({ id: Number(workspace.id) });
|
|
|
|
|
[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
|
|
|
await EventLogs.logEvent(
|
|
|
|
"workspace_deleted",
|
|
|
|
{
|
|
|
|
workspaceName: workspace?.name || "Unknown Workspace",
|
|
|
|
},
|
|
|
|
response.locals?.user?.id
|
|
|
|
);
|
|
|
|
|
2024-04-12 23:50:39 +02:00
|
|
|
try {
|
|
|
|
await VectorDb["delete-namespace"]({ namespace: slug });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
|
|
|
}
|
|
|
|
response.sendStatus(200).end();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.delete(
|
|
|
|
"/workspace/:slug/reset-vector-db",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { slug = "" } = request.params;
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const VectorDb = getVectorDbClass();
|
|
|
|
const workspace = multiUserMode(response)
|
|
|
|
? await Workspace.getWithUser(user, { slug })
|
|
|
|
: await Workspace.get({ slug });
|
|
|
|
|
|
|
|
if (!workspace) {
|
|
|
|
response.sendStatus(400).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await DocumentVectors.deleteForWorkspace(workspace.id);
|
|
|
|
await Document.delete({ workspaceId: Number(workspace.id) });
|
|
|
|
|
|
|
|
await EventLogs.logEvent(
|
|
|
|
"workspace_vectors_reset",
|
|
|
|
{
|
|
|
|
workspaceName: workspace?.name || "Unknown Workspace",
|
|
|
|
},
|
|
|
|
response.locals?.user?.id
|
|
|
|
);
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
try {
|
|
|
|
await VectorDb["delete-namespace"]({ namespace: slug });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
|
|
|
}
|
|
|
|
response.sendStatus(200).end();
|
2023-06-08 22:13:48 +02:00
|
|
|
} catch (e) {
|
2023-07-25 19:37:04 +02:00
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
2023-06-08 22:13:48 +02:00
|
|
|
}
|
2023-06-08 06:31:35 +02:00
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2024-01-22 23:14:01 +01:00
|
|
|
app.get(
|
|
|
|
"/workspaces",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all])],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const workspaces = multiUserMode(response)
|
|
|
|
? await Workspace.whereWithUser(user)
|
|
|
|
: await Workspace.where();
|
2023-07-25 19:37:04 +02:00
|
|
|
|
2024-01-22 23:14:01 +01:00
|
|
|
response.status(200).json({ workspaces });
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
2023-06-08 22:13:48 +02:00
|
|
|
}
|
2024-01-22 23:14:01 +01:00
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2024-01-22 23:14:01 +01:00
|
|
|
app.get(
|
|
|
|
"/workspace/:slug",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all])],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { slug } = request.params;
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const workspace = multiUserMode(response)
|
|
|
|
? await Workspace.getWithUser(user, { slug })
|
|
|
|
: await Workspace.get({ slug });
|
2023-07-25 19:37:04 +02:00
|
|
|
|
2024-01-22 23:14:01 +01:00
|
|
|
response.status(200).json({ workspace });
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
2023-06-08 22:13:48 +02:00
|
|
|
}
|
2024-01-22 23:14:01 +01:00
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
app.get(
|
|
|
|
"/workspace/:slug/chats",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { slug } = request.params;
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const workspace = multiUserMode(response)
|
2023-09-28 23:00:03 +02:00
|
|
|
? await Workspace.getWithUser(user, { slug })
|
|
|
|
: await Workspace.get({ slug });
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
if (!workspace) {
|
|
|
|
response.sendStatus(400).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const history = multiUserMode(response)
|
|
|
|
? await WorkspaceChats.forWorkspaceByUser(workspace.id, user.id)
|
|
|
|
: await WorkspaceChats.forWorkspace(workspace.id);
|
|
|
|
|
|
|
|
response.status(200).json({ history: convertToChatHistory(history) });
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
2023-06-08 22:13:48 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-25 19:37:04 +02:00
|
|
|
);
|
2024-02-06 20:24:33 +01:00
|
|
|
|
2024-05-10 23:47:29 +02:00
|
|
|
app.delete(
|
|
|
|
"/workspace/:slug/delete-chats",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all]), validWorkspaceSlug],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { chatIds = [] } = reqBody(request);
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const workspace = response.locals.workspace;
|
|
|
|
|
|
|
|
if (!workspace || !Array.isArray(chatIds)) {
|
|
|
|
response.sendStatus(400).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This works for both workspace and threads.
|
|
|
|
// we simplify this by just looking at workspace<>user overlap
|
|
|
|
// since they are all on the same table.
|
|
|
|
await WorkspaceChats.delete({
|
|
|
|
id: { in: chatIds.map((id) => Number(id)) },
|
|
|
|
user_id: user?.id ?? null,
|
|
|
|
workspaceId: workspace.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
response.sendStatus(200).end();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e.message, e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-02-13 20:33:05 +01:00
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/chat-feedback/:chatId",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all]), validWorkspaceSlug],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { chatId } = request.params;
|
|
|
|
const { feedback = null } = reqBody(request);
|
|
|
|
const existingChat = await WorkspaceChats.get({
|
|
|
|
id: Number(chatId),
|
|
|
|
workspaceId: response.locals.workspace.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!existingChat) {
|
|
|
|
response.status(404).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await WorkspaceChats.updateFeedbackScore(
|
|
|
|
chatId,
|
|
|
|
feedback
|
|
|
|
);
|
|
|
|
response.status(200).json({ success: result });
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error updating chat feedback:", error);
|
|
|
|
response.status(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-02-06 20:24:33 +01:00
|
|
|
app.get(
|
|
|
|
"/workspace/:slug/suggested-messages",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all])],
|
|
|
|
async function (request, response) {
|
|
|
|
try {
|
|
|
|
const { slug } = request.params;
|
|
|
|
const suggestedMessages =
|
|
|
|
await WorkspaceSuggestedMessages.getMessages(slug);
|
|
|
|
response.status(200).json({ success: true, suggestedMessages });
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error fetching suggested messages:", error);
|
|
|
|
response
|
|
|
|
.status(500)
|
|
|
|
.json({ success: false, message: "Internal server error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/suggested-messages",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { messages = [] } = reqBody(request);
|
|
|
|
const { slug } = request.params;
|
|
|
|
if (!Array.isArray(messages)) {
|
|
|
|
return response.status(400).json({
|
|
|
|
success: false,
|
|
|
|
message: "Invalid message format. Expected an array of messages.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
await WorkspaceSuggestedMessages.saveAll(messages, slug);
|
|
|
|
return response.status(200).json({
|
|
|
|
success: true,
|
|
|
|
message: "Suggested messages saved successfully.",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error processing the suggested messages:", error);
|
|
|
|
response.status(500).json({
|
|
|
|
success: true,
|
|
|
|
message: "Error saving the suggested messages.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2024-02-21 22:15:45 +01:00
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/update-pin",
|
|
|
|
[
|
|
|
|
validatedRequest,
|
|
|
|
flexUserRoleValid([ROLES.admin, ROLES.manager]),
|
|
|
|
validWorkspaceSlug,
|
|
|
|
],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { docPath, pinStatus = false } = reqBody(request);
|
|
|
|
const workspace = response.locals.workspace;
|
|
|
|
|
|
|
|
const document = await Document.get({
|
|
|
|
workspaceId: workspace.id,
|
|
|
|
docpath: docPath,
|
|
|
|
});
|
|
|
|
if (!document) return response.sendStatus(404).end();
|
|
|
|
|
|
|
|
await Document.update(document.id, { pinned: pinStatus });
|
|
|
|
return response.status(200).end();
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error processing the pin status update:", error);
|
|
|
|
return response.status(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2024-03-12 19:36:59 +01:00
|
|
|
|
2024-05-14 20:57:21 +02:00
|
|
|
app.get(
|
|
|
|
"/workspace/:slug/tts/:chatId",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all]), validWorkspaceSlug],
|
|
|
|
async function (request, response) {
|
|
|
|
try {
|
|
|
|
const { chatId } = request.params;
|
|
|
|
const workspace = response.locals.workspace;
|
|
|
|
const cacheKey = `${workspace.slug}:${chatId}`;
|
|
|
|
const wsChat = await WorkspaceChats.get({
|
|
|
|
id: Number(chatId),
|
|
|
|
workspaceId: workspace.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
const cachedResponse = responseCache.get(cacheKey);
|
|
|
|
if (cachedResponse) {
|
|
|
|
response.writeHead(200, {
|
|
|
|
"Content-Type": cachedResponse.mime || "audio/mpeg",
|
|
|
|
});
|
|
|
|
response.end(cachedResponse.buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const text = safeJsonParse(wsChat.response, null)?.text;
|
|
|
|
if (!text) return response.sendStatus(204).end();
|
|
|
|
|
|
|
|
const TTSProvider = getTTSProvider();
|
|
|
|
const buffer = await TTSProvider.ttsBuffer(text);
|
|
|
|
if (buffer === null) return response.sendStatus(204).end();
|
|
|
|
|
|
|
|
responseCache.set(cacheKey, { buffer, mime: "audio/mpeg" });
|
|
|
|
response.writeHead(200, {
|
|
|
|
"Content-Type": "audio/mpeg",
|
|
|
|
});
|
|
|
|
response.end(buffer);
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error processing the TTS request:", error);
|
|
|
|
response.status(500).json({ message: "TTS could not be completed" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-03-12 19:36:59 +01:00
|
|
|
app.get(
|
|
|
|
"/workspace/:slug/pfp",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.all])],
|
|
|
|
async function (request, response) {
|
|
|
|
try {
|
|
|
|
const { slug } = request.params;
|
|
|
|
const cachedResponse = responseCache.get(slug);
|
|
|
|
|
|
|
|
if (cachedResponse) {
|
|
|
|
response.writeHead(200, {
|
|
|
|
"Content-Type": cachedResponse.mime || "image/png",
|
|
|
|
});
|
|
|
|
response.end(cachedResponse.buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const pfpPath = await determineWorkspacePfpFilepath(slug);
|
|
|
|
|
|
|
|
if (!pfpPath) {
|
|
|
|
response.sendStatus(204).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { found, buffer, mime } = fetchPfp(pfpPath);
|
|
|
|
if (!found) {
|
|
|
|
response.sendStatus(204).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
responseCache.set(slug, { buffer, mime });
|
|
|
|
|
|
|
|
response.writeHead(200, {
|
|
|
|
"Content-Type": mime || "image/png",
|
|
|
|
});
|
|
|
|
response.end(buffer);
|
|
|
|
return;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error processing the logo request:", error);
|
|
|
|
response.status(500).json({ message: "Internal server error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/upload-pfp",
|
2024-04-01 21:06:47 +02:00
|
|
|
[
|
|
|
|
validatedRequest,
|
|
|
|
flexUserRoleValid([ROLES.admin, ROLES.manager]),
|
|
|
|
handlePfpUpload,
|
|
|
|
],
|
2024-03-12 19:36:59 +01:00
|
|
|
async function (request, response) {
|
|
|
|
try {
|
|
|
|
const { slug } = request.params;
|
|
|
|
const uploadedFileName = request.randomFileName;
|
|
|
|
if (!uploadedFileName) {
|
|
|
|
return response.status(400).json({ message: "File upload failed." });
|
|
|
|
}
|
|
|
|
|
|
|
|
const workspaceRecord = await Workspace.get({
|
|
|
|
slug,
|
|
|
|
});
|
|
|
|
|
|
|
|
const oldPfpFilename = workspaceRecord.pfpFilename;
|
|
|
|
if (oldPfpFilename) {
|
|
|
|
const oldPfpPath = path.join(
|
|
|
|
__dirname,
|
|
|
|
`../storage/assets/pfp/${normalizePath(
|
|
|
|
workspaceRecord.pfpFilename
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (fs.existsSync(oldPfpPath)) fs.unlinkSync(oldPfpPath);
|
|
|
|
}
|
|
|
|
|
2024-04-05 19:58:36 +02:00
|
|
|
const { workspace, message } = await Workspace._update(
|
2024-03-12 19:36:59 +01:00
|
|
|
workspaceRecord.id,
|
|
|
|
{
|
|
|
|
pfpFilename: uploadedFileName,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.status(workspace ? 200 : 500).json({
|
|
|
|
message: workspace
|
|
|
|
? "Profile picture uploaded successfully."
|
|
|
|
: message,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error processing the profile picture upload:", error);
|
|
|
|
response.status(500).json({ message: "Internal server error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.delete(
|
|
|
|
"/workspace/:slug/remove-pfp",
|
|
|
|
[validatedRequest, flexUserRoleValid([ROLES.admin, ROLES.manager])],
|
|
|
|
async function (request, response) {
|
|
|
|
try {
|
|
|
|
const { slug } = request.params;
|
|
|
|
const workspaceRecord = await Workspace.get({
|
|
|
|
slug,
|
|
|
|
});
|
|
|
|
const oldPfpFilename = workspaceRecord.pfpFilename;
|
|
|
|
|
|
|
|
if (oldPfpFilename) {
|
|
|
|
const oldPfpPath = path.join(
|
|
|
|
__dirname,
|
|
|
|
`../storage/assets/pfp/${normalizePath(oldPfpFilename)}`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (fs.existsSync(oldPfpPath)) fs.unlinkSync(oldPfpPath);
|
|
|
|
}
|
|
|
|
|
2024-04-05 19:58:36 +02:00
|
|
|
const { workspace, message } = await Workspace._update(
|
2024-03-12 19:36:59 +01:00
|
|
|
workspaceRecord.id,
|
|
|
|
{
|
|
|
|
pfpFilename: null,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Clear the cache
|
|
|
|
responseCache.delete(slug);
|
|
|
|
|
|
|
|
return response.status(workspace ? 200 : 500).json({
|
|
|
|
message: workspace
|
|
|
|
? "Profile picture removed successfully."
|
|
|
|
: message,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error processing the profile picture removal:", error);
|
|
|
|
response.status(500).json({ message: "Internal server error" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
module.exports = { workspaceEndpoints };
|