anything-llm/server/endpoints/experimental/liveSync.js

115 lines
3.5 KiB
JavaScript
Raw Normal View History

[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 { DocumentSyncQueue } = require("../../models/documentSyncQueue");
const { Document } = require("../../models/documents");
const { EventLogs } = require("../../models/eventLogs");
const { SystemSettings } = require("../../models/systemSettings");
const { Telemetry } = require("../../models/telemetry");
const { reqBody } = require("../../utils/http");
const {
featureFlagEnabled,
} = require("../../utils/middleware/featureFlagEnabled");
const {
flexUserRoleValid,
ROLES,
} = require("../../utils/middleware/multiUserProtected");
const { validWorkspaceSlug } = require("../../utils/middleware/validWorkspace");
const { validatedRequest } = require("../../utils/middleware/validatedRequest");
function liveSyncEndpoints(app) {
if (!app) return;
app.post(
"/experimental/toggle-live-sync",
[validatedRequest, flexUserRoleValid([ROLES.admin])],
async (request, response) => {
try {
const { updatedStatus = false } = reqBody(request);
const newStatus =
SystemSettings.validations.experimental_live_file_sync(updatedStatus);
const currentStatus =
(await SystemSettings.get({ label: "experimental_live_file_sync" }))
?.value || "disabled";
if (currentStatus === newStatus)
return response
.status(200)
.json({ liveSyncEnabled: newStatus === "enabled" });
// Already validated earlier - so can hot update.
await SystemSettings._updateSettings({
experimental_live_file_sync: newStatus,
});
if (newStatus === "enabled") {
await Telemetry.sendTelemetry("experimental_feature_enabled", {
feature: "live_file_sync",
});
await EventLogs.logEvent("experimental_feature_enabled", {
feature: "live_file_sync",
});
DocumentSyncQueue.bootWorkers();
} else {
DocumentSyncQueue.killWorkers();
}
response.status(200).json({ liveSyncEnabled: newStatus === "enabled" });
} catch (e) {
console.error(e);
response.status(500).end();
}
}
);
app.get(
"/experimental/live-sync/queues",
[
validatedRequest,
flexUserRoleValid([ROLES.admin]),
featureFlagEnabled(DocumentSyncQueue.featureKey),
],
async (_, response) => {
const queues = await DocumentSyncQueue.where(
{},
null,
{ createdAt: "asc" },
{
workspaceDoc: {
include: {
workspace: true,
},
},
}
);
response.status(200).json({ queues });
}
);
// Should be in workspace routes, but is here for now.
app.post(
"/workspace/:slug/update-watch-status",
[
validatedRequest,
flexUserRoleValid([ROLES.admin, ROLES.manager]),
validWorkspaceSlug,
featureFlagEnabled(DocumentSyncQueue.featureKey),
],
async (request, response) => {
try {
const { docPath, watchStatus = 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 DocumentSyncQueue.toggleWatchStatus(document, watchStatus);
return response.status(200).end();
} catch (error) {
console.error("Error processing the watch status update:", error);
return response.status(500).end();
}
}
);
}
module.exports = { liveSyncEndpoints };