2023-06-08 22:13:48 +02:00
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
|
|
|
|
: require("dotenv").config();
|
|
|
|
|
2024-07-04 01:39:33 +02:00
|
|
|
require("./utils/logger")();
|
2023-06-08 06:31:35 +02:00
|
|
|
const express = require("express");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const cors = require("cors");
|
2023-06-13 20:26:11 +02:00
|
|
|
const path = require("path");
|
2023-06-08 06:31:35 +02:00
|
|
|
const { reqBody } = require("./utils/http");
|
|
|
|
const { systemEndpoints } = require("./endpoints/system");
|
|
|
|
const { workspaceEndpoints } = require("./endpoints/workspaces");
|
|
|
|
const { chatEndpoints } = require("./endpoints/chat");
|
2024-02-05 23:21:34 +01:00
|
|
|
const { embeddedEndpoints } = require("./endpoints/embed");
|
|
|
|
const { embedManagementEndpoints } = require("./endpoints/embedManagement");
|
2023-06-08 06:31:35 +02:00
|
|
|
const { getVectorDbClass } = require("./utils/helpers");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { adminEndpoints } = require("./endpoints/admin");
|
|
|
|
const { inviteEndpoints } = require("./endpoints/invite");
|
2023-08-10 22:23:23 +02:00
|
|
|
const { utilEndpoints } = require("./endpoints/utils");
|
2023-08-24 04:15:07 +02:00
|
|
|
const { developerEndpoints } = require("./endpoints/api");
|
2023-12-19 00:48:02 +01:00
|
|
|
const { extensionEndpoints } = require("./endpoints/extensions");
|
2024-01-05 02:22:15 +01:00
|
|
|
const { bootHTTP, bootSSL } = require("./utils/boot");
|
2024-02-09 03:37:22 +01:00
|
|
|
const { workspaceThreadEndpoints } = require("./endpoints/workspaceThreads");
|
2024-03-21 00:10:30 +01:00
|
|
|
const { documentEndpoints } = require("./endpoints/document");
|
2024-04-16 19:50:10 +02:00
|
|
|
const { agentWebsocket } = require("./endpoints/agentWebsocket");
|
2024-06-21 22:38:50 +02:00
|
|
|
const { experimentalEndpoints } = require("./endpoints/experimental");
|
2023-06-04 04:28:07 +02:00
|
|
|
const app = express();
|
2023-06-13 20:26:11 +02:00
|
|
|
const apiRouter = express.Router();
|
2023-08-17 02:46:45 +02:00
|
|
|
const FILE_LIMIT = "3GB";
|
2023-06-04 04:28:07 +02:00
|
|
|
|
|
|
|
app.use(cors({ origin: true }));
|
2023-08-17 02:46:45 +02:00
|
|
|
app.use(bodyParser.text({ limit: FILE_LIMIT }));
|
|
|
|
app.use(bodyParser.json({ limit: FILE_LIMIT }));
|
2023-06-08 06:31:35 +02:00
|
|
|
app.use(
|
|
|
|
bodyParser.urlencoded({
|
2023-08-17 02:46:45 +02:00
|
|
|
limit: FILE_LIMIT,
|
2023-06-08 06:31:35 +02:00
|
|
|
extended: true,
|
|
|
|
})
|
|
|
|
);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2024-05-17 23:03:25 +02:00
|
|
|
if (!!process.env.ENABLE_HTTPS) {
|
|
|
|
bootSSL(app, process.env.SERVER_PORT || 3001);
|
|
|
|
} else {
|
|
|
|
require("express-ws")(app); // load WebSockets in non-SSL mode.
|
|
|
|
}
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
app.use("/api", apiRouter);
|
2023-06-13 20:26:11 +02:00
|
|
|
systemEndpoints(apiRouter);
|
2023-12-19 00:48:02 +01:00
|
|
|
extensionEndpoints(apiRouter);
|
2023-06-13 20:26:11 +02:00
|
|
|
workspaceEndpoints(apiRouter);
|
2024-02-09 03:37:22 +01:00
|
|
|
workspaceThreadEndpoints(apiRouter);
|
2023-06-13 20:26:11 +02:00
|
|
|
chatEndpoints(apiRouter);
|
2023-07-25 19:37:04 +02:00
|
|
|
adminEndpoints(apiRouter);
|
|
|
|
inviteEndpoints(apiRouter);
|
2024-02-05 23:21:34 +01:00
|
|
|
embedManagementEndpoints(apiRouter);
|
2023-08-10 22:23:23 +02:00
|
|
|
utilEndpoints(apiRouter);
|
2024-03-21 00:10:30 +01:00
|
|
|
documentEndpoints(apiRouter);
|
2024-04-16 19:50:10 +02:00
|
|
|
agentWebsocket(apiRouter);
|
2024-06-21 22:38:50 +02:00
|
|
|
experimentalEndpoints(apiRouter);
|
2023-08-24 04:15:07 +02:00
|
|
|
developerEndpoints(app, apiRouter);
|
2023-06-04 04:28:07 +02:00
|
|
|
|
2024-02-05 23:21:34 +01:00
|
|
|
// Externally facing embedder endpoints
|
|
|
|
embeddedEndpoints(apiRouter);
|
|
|
|
|
2023-06-13 20:26:11 +02:00
|
|
|
if (process.env.NODE_ENV !== "development") {
|
2024-07-20 00:58:43 +02:00
|
|
|
const { MetaGenerator } = require("./utils/boot/MetaGenerator");
|
|
|
|
const IndexPage = new MetaGenerator();
|
|
|
|
|
2023-06-14 22:35:55 +02:00
|
|
|
app.use(
|
2024-05-01 22:02:08 +02:00
|
|
|
express.static(path.resolve(__dirname, "public"), {
|
|
|
|
extensions: ["js"],
|
|
|
|
setHeaders: (res) => {
|
|
|
|
// Disable I-framing of entire site UI
|
|
|
|
res.removeHeader("X-Powered-By");
|
|
|
|
res.setHeader("X-Frame-Options", "DENY");
|
|
|
|
},
|
|
|
|
})
|
2023-06-14 22:35:55 +02:00
|
|
|
);
|
2023-06-13 20:26:11 +02:00
|
|
|
|
|
|
|
app.use("/", function (_, response) {
|
2024-07-20 00:58:43 +02:00
|
|
|
IndexPage.generate(response);
|
|
|
|
return;
|
2023-06-14 22:35:55 +02:00
|
|
|
});
|
2023-11-14 00:22:24 +01:00
|
|
|
|
|
|
|
app.get("/robots.txt", function (_, response) {
|
|
|
|
response.type("text/plain");
|
|
|
|
response.send("User-agent: *\nDisallow: /").end();
|
|
|
|
});
|
2024-03-27 21:19:28 +01:00
|
|
|
} else {
|
|
|
|
// Debug route for development connections to vectorDBs
|
|
|
|
apiRouter.post("/v/:command", async (request, response) => {
|
|
|
|
try {
|
|
|
|
const VectorDb = getVectorDbClass();
|
|
|
|
const { command } = request.params;
|
|
|
|
if (!Object.getOwnPropertyNames(VectorDb).includes(command)) {
|
|
|
|
response.status(500).json({
|
|
|
|
message: "invalid interface command",
|
|
|
|
commands: Object.getOwnPropertyNames(VectorDb),
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const body = reqBody(request);
|
|
|
|
const resBody = await VectorDb[command](body);
|
|
|
|
response.status(200).json({ ...resBody });
|
|
|
|
} catch (e) {
|
|
|
|
// console.error(e)
|
|
|
|
console.error(JSON.stringify(e));
|
|
|
|
response.status(500).json({ error: e.message });
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
2024-07-04 01:39:33 +02:00
|
|
|
console.error(e.message, e);
|
2024-03-27 21:19:28 +01:00
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
});
|
2023-06-13 20:26:11 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 06:31:35 +02:00
|
|
|
app.all("*", function (_, response) {
|
2023-06-04 04:28:07 +02:00
|
|
|
response.sendStatus(404);
|
|
|
|
});
|
|
|
|
|
2024-05-17 23:03:25 +02:00
|
|
|
// In non-https mode we need to boot at the end since the server has not yet
|
|
|
|
// started and is `.listen`ing.
|
|
|
|
if (!process.env.ENABLE_HTTPS) bootHTTP(app, process.env.SERVER_PORT || 3001);
|