2023-06-27 02:54:55 +02:00
|
|
|
const { v4: uuidv4 } = require("uuid");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { reqBody, userFromSession, multiUserMode } = require("../utils/http");
|
2023-06-08 06:31:35 +02:00
|
|
|
const { Workspace } = require("../models/workspace");
|
|
|
|
const { chatWithWorkspace } = require("../utils/chats");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { validatedRequest } = require("../utils/middleware/validatedRequest");
|
|
|
|
const { WorkspaceChats } = require("../models/workspaceChats");
|
|
|
|
const { SystemSettings } = require("../models/systemSettings");
|
2023-08-15 02:42:17 +02:00
|
|
|
const { Telemetry } = require("../models/telemetry");
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
function chatEndpoints(app) {
|
|
|
|
if (!app) return;
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
app.post(
|
|
|
|
"/workspace/:slug/chat",
|
|
|
|
[validatedRequest],
|
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const { slug } = request.params;
|
|
|
|
const { message, mode = "query" } = reqBody(request);
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (multiUserMode(response) && user.role !== "admin") {
|
2023-09-28 23:00:03 +02:00
|
|
|
const limitMessagesSetting = await SystemSettings.get({
|
|
|
|
label: "limit_user_messages",
|
|
|
|
});
|
|
|
|
const limitMessages = limitMessagesSetting?.value === "true";
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
if (limitMessages) {
|
2023-09-28 23:00:03 +02:00
|
|
|
const messageLimitSetting = await SystemSettings.get({
|
|
|
|
label: "message_limit",
|
|
|
|
});
|
|
|
|
const systemLimit = Number(messageLimitSetting?.value);
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
if (!!systemLimit) {
|
2023-09-28 23:00:03 +02:00
|
|
|
const currentChatCount = await WorkspaceChats.count({
|
|
|
|
user_id: user.id,
|
|
|
|
createdAt: {
|
|
|
|
gte: new Date(new Date() - 24 * 60 * 60 * 1000),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
if (currentChatCount >= systemLimit) {
|
|
|
|
response.status(500).json({
|
|
|
|
id: uuidv4(),
|
|
|
|
type: "abort",
|
|
|
|
textResponse: null,
|
|
|
|
sources: [],
|
|
|
|
close: true,
|
|
|
|
error: `You have met your maximum 24 hour chat quota of ${systemLimit} chats set by the instance administrators. Try again later.`,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await chatWithWorkspace(workspace, message, mode, user);
|
2023-11-11 01:02:46 +01:00
|
|
|
await Telemetry.sendTelemetry(
|
|
|
|
"sent_chat",
|
|
|
|
{
|
|
|
|
multiUserMode: multiUserMode(response),
|
|
|
|
LLMSelection: process.env.LLM_PROVIDER || "openai",
|
|
|
|
VectorDbSelection: process.env.VECTOR_DB || "pinecone",
|
|
|
|
},
|
|
|
|
user?.id
|
|
|
|
);
|
2023-07-25 19:37:04 +02:00
|
|
|
response.status(200).json({ ...result });
|
|
|
|
} catch (e) {
|
2023-11-06 22:13:53 +01:00
|
|
|
console.error(e);
|
2023-07-25 19:37:04 +02:00
|
|
|
response.status(500).json({
|
|
|
|
id: uuidv4(),
|
|
|
|
type: "abort",
|
|
|
|
textResponse: null,
|
|
|
|
sources: [],
|
|
|
|
close: true,
|
|
|
|
error: e.message,
|
|
|
|
});
|
|
|
|
}
|
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-06-08 06:31:35 +02:00
|
|
|
module.exports = { chatEndpoints };
|