anything-llm/server/index.js

131 lines
4.2 KiB
JavaScript
Raw Normal View History

const path = require("path");
process.env.NODE_ENV === "development"
? require("dotenv").config({ path: `.env.${process.env.NODE_ENV}` })
: require("dotenv").config({
path: process.env.STORAGE_DIR
? path.resolve(process.env.STORAGE_DIR, ".env")
: path.resolve(__dirname, ".env"),
});
require("./utils/logger")();
2023-06-08 06:31:35 +02:00
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const { reqBody } = require("./utils/http");
const { systemEndpoints } = require("./endpoints/system");
const { workspaceEndpoints } = require("./endpoints/workspaces");
const { chatEndpoints } = require("./endpoints/chat");
const { embeddedEndpoints } = require("./endpoints/embed");
const { embedManagementEndpoints } = require("./endpoints/embedManagement");
2023-06-08 06:31:35 +02:00
const { getVectorDbClass } = require("./utils/helpers");
const { adminEndpoints } = require("./endpoints/admin");
const { inviteEndpoints } = require("./endpoints/invite");
2023-08-10 22:23:23 +02:00
const { utilEndpoints } = require("./endpoints/utils");
const { developerEndpoints } = require("./endpoints/api");
const { extensionEndpoints } = require("./endpoints/extensions");
const { bootHTTP, bootSSL } = require("./utils/boot");
const { workspaceThreadEndpoints } = require("./endpoints/workspaceThreads");
const { documentEndpoints } = require("./endpoints/document");
const { agentWebsocket } = require("./endpoints/agentWebsocket");
[BETA] Live document sync (#1719) * wip bg workers for live document sync * Add ability to re-embed specific documents across many workspaces via background queue bgworkser is gated behind expieremental system setting flag that needs to be explictly enabled UI for watching/unwatching docments that are embedded. TODO: UI to easily manage all bg tasks and see run results TODO: UI to enable this feature and background endpoints to manage it * create frontend views and paths Move elements to correct experimental scope * update migration to delete runs on removal of watched document * Add watch support to YouTube transcripts (#1716) * Add watch support to YouTube transcripts refactor how sync is done for supported types * Watch specific files in Confluence space (#1718) Add failure-prune check for runs * create tmp workflow modifications for beta image * create tmp workflow modifications for beta image * create tmp workflow modifications for beta image * dual build update copy of alert modals * update job interval * Add support for live-sync of Github files * update copy for document sync feature * hide Experimental features from UI * update docs links * [FEAT] Implement new settings menu for experimental features (#1735) * implement new settings menu for experimental features * remove unused context save bar --------- Co-authored-by: timothycarambat <rambat1010@gmail.com> * dont run job on boot * unset workflow changes * Add persistent encryption service Relay key to collector so persistent encryption can be used Encrypt any private data in chunkSources used for replay during resync jobs * update jsDOC * Linting and organization * update modal copy for feature --------- Co-authored-by: Sean Hatfield <seanhatfield5@gmail.com>
2024-06-21 22:38:50 +02:00
const { experimentalEndpoints } = require("./endpoints/experimental");
2023-06-04 04:28:07 +02:00
const app = express();
const apiRouter = express.Router();
const FILE_LIMIT = "3GB";
2023-06-04 04:28:07 +02:00
app.use(cors({ origin: true }));
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({
limit: FILE_LIMIT,
2023-06-08 06:31:35 +02:00
extended: true,
})
);
2023-06-04 04:28:07 +02:00
if (!!process.env.ENABLE_HTTPS) {
bootSSL(app, process.env.SERVER_PORT || 3001);
} else {
require("@mintplex-labs/express-ws").default(app); // load WebSockets in non-SSL mode.
}
app.use("/api", apiRouter);
systemEndpoints(apiRouter);
extensionEndpoints(apiRouter);
workspaceEndpoints(apiRouter);
workspaceThreadEndpoints(apiRouter);
chatEndpoints(apiRouter);
adminEndpoints(apiRouter);
inviteEndpoints(apiRouter);
embedManagementEndpoints(apiRouter);
2023-08-10 22:23:23 +02:00
utilEndpoints(apiRouter);
documentEndpoints(apiRouter);
agentWebsocket(apiRouter);
[BETA] Live document sync (#1719) * wip bg workers for live document sync * Add ability to re-embed specific documents across many workspaces via background queue bgworkser is gated behind expieremental system setting flag that needs to be explictly enabled UI for watching/unwatching docments that are embedded. TODO: UI to easily manage all bg tasks and see run results TODO: UI to enable this feature and background endpoints to manage it * create frontend views and paths Move elements to correct experimental scope * update migration to delete runs on removal of watched document * Add watch support to YouTube transcripts (#1716) * Add watch support to YouTube transcripts refactor how sync is done for supported types * Watch specific files in Confluence space (#1718) Add failure-prune check for runs * create tmp workflow modifications for beta image * create tmp workflow modifications for beta image * create tmp workflow modifications for beta image * dual build update copy of alert modals * update job interval * Add support for live-sync of Github files * update copy for document sync feature * hide Experimental features from UI * update docs links * [FEAT] Implement new settings menu for experimental features (#1735) * implement new settings menu for experimental features * remove unused context save bar --------- Co-authored-by: timothycarambat <rambat1010@gmail.com> * dont run job on boot * unset workflow changes * Add persistent encryption service Relay key to collector so persistent encryption can be used Encrypt any private data in chunkSources used for replay during resync jobs * update jsDOC * Linting and organization * update modal copy for feature --------- Co-authored-by: Sean Hatfield <seanhatfield5@gmail.com>
2024-06-21 22:38:50 +02:00
experimentalEndpoints(apiRouter);
developerEndpoints(app, apiRouter);
2023-06-04 04:28:07 +02:00
// Externally facing embedder endpoints
embeddedEndpoints(apiRouter);
if (process.env.NODE_ENV !== "development") {
const { MetaGenerator } = require("./utils/boot/MetaGenerator");
const IndexPage = new MetaGenerator();
app.use(
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");
},
})
);
app.use("/", function (_, response) {
IndexPage.generate(response);
return;
});
app.get("/robots.txt", function (_, response) {
response.type("text/plain");
response.send("User-agent: *\nDisallow: /").end();
});
} 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) {
console.error(e.message, e);
response.sendStatus(500).end();
}
});
}
2023-06-08 06:31:35 +02:00
app.all("*", function (_, response) {
2023-06-04 04:28:07 +02:00
response.sendStatus(404);
});
// 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);