Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Mintplex-Labs/anything-llm
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycarambat committed Oct 28, 2024
2 parents c870e31 + 4080063 commit 75592ba
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ This monorepo consists of three main sections:

Mintplex Labs & the community maintain a number of deployment methods, scripts, and templates that you can use to run AnythingLLM locally. Refer to the table below to read how to deploy on your preferred environment or to automatically deploy.
| Docker | AWS | GCP | Digital Ocean | Render.com |
|----------------------------------------|----:|-----|---------------|------------|
|----------------------------------------|----|-----|---------------|------------|
| [![Deploy on Docker][docker-btn]][docker-deploy] | [![Deploy on AWS][aws-btn]][aws-deploy] | [![Deploy on GCP][gcp-btn]][gcp-deploy] | [![Deploy on DigitalOcean][do-btn]][do-deploy] | [![Deploy on Render.com][render-btn]][render-deploy] |

| Railway | RepoCloud | Elestio |
Expand Down
4 changes: 2 additions & 2 deletions server/endpoints/api/document/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Telemetry } = require("../../../models/telemetry");
const { validApiKey } = require("../../../utils/middleware/validApiKey");
const { handleFileUpload } = require("../../../utils/files/multer");
const { handleAPIFileUpload } = require("../../../utils/files/multer");
const {
viewLocalFiles,
findDocumentInDocuments,
Expand All @@ -23,7 +23,7 @@ function apiDocumentEndpoints(app) {

app.post(
"/v1/document/upload",
[validApiKey, handleFileUpload],
[validApiKey, handleAPIFileUpload],
async (request, response) => {
/*
#swagger.tags = ['Documents']
Expand Down
14 changes: 13 additions & 1 deletion server/endpoints/api/workspace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ function apiWorkspaceEndpoints(app) {
required: true,
type: 'string'
}
#swagger.parameters['apiSessionId'] = {
in: 'query',
description: 'Optional apiSessionId to filter by',
required: false,
type: 'string'
}
#swagger.responses[200] = {
content: {
"application/json": {
Expand Down Expand Up @@ -370,14 +376,20 @@ function apiWorkspaceEndpoints(app) {
*/
try {
const { slug } = request.params;
const { apiSessionId = null } = request.query;
const workspace = await Workspace.get({ slug });

if (!workspace) {
response.sendStatus(400).end();
return;
}

const history = await WorkspaceChats.forWorkspace(workspace.id);
const history = apiSessionId
? await WorkspaceChats.forWorkspaceByApiSessionId(
workspace.id,
apiSessionId
)
: await WorkspaceChats.forWorkspace(workspace.id);
response.status(200).json({ history: convertToChatHistory(history) });
} catch (e) {
console.error(e.message, e);
Expand Down
25 changes: 25 additions & 0 deletions server/models/workspaceChats.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,31 @@ const WorkspaceChats = {
}
},

forWorkspaceByApiSessionId: async function (
workspaceId = null,
apiSessionId = null,
limit = null,
orderBy = null
) {
if (!workspaceId || !apiSessionId) return [];
try {
const chats = await prisma.workspace_chats.findMany({
where: {
workspaceId,
user_id: null,
api_session_id: String(apiSessionId),
thread_id: null,
},
...(limit !== null ? { take: limit } : {}),
...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
});
return chats;
} catch (error) {
console.error(error.message);
return [];
}
},

forWorkspace: async function (
workspaceId = null,
limit = null,
Expand Down
9 changes: 9 additions & 0 deletions server/swagger/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1649,6 +1649,15 @@
"type": "string"
},
"description": "Unique slug of workspace to find"
},
{
"name": "apiSessionId",
"in": "query",
"description": "Optional apiSessionId to filter by",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
Expand Down
66 changes: 61 additions & 5 deletions server/utils/files/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const path = require("path");
const fs = require("fs");
const { v4 } = require("uuid");

// Handle File uploads for auto-uploading.
/**
* Handle File uploads for auto-uploading.
* Mostly used for internal GUI/API uploads.
*/
const fileUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
const uploadOutput =
Expand All @@ -20,6 +23,23 @@ const fileUploadStorage = multer.diskStorage({
},
});

/**
* Handle API file upload as documents - this does not manipulate the filename
* at all for encoding/charset reasons.
*/
const fileAPIUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
const uploadOutput =
process.env.NODE_ENV === "development"
? path.resolve(__dirname, `../../../collector/hotdir`)
: path.resolve(process.env.STORAGE_DIR, `../../collector/hotdir`);
cb(null, uploadOutput);
},
filename: function (_, file, cb) {
cb(null, file.originalname);
},
});

// Asset storage for logos
const assetUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
Expand All @@ -38,7 +58,9 @@ const assetUploadStorage = multer.diskStorage({
},
});

// Asset sub-storage manager for pfp icons.
/**
* Handle PFP file upload as logos
*/
const pfpUploadStorage = multer.diskStorage({
destination: function (_, __, cb) {
const uploadOutput =
Expand All @@ -55,7 +77,12 @@ const pfpUploadStorage = multer.diskStorage({
},
});

// Handle Generic file upload as documents
/**
* Handle Generic file upload as documents from the GUI
* @param {Request} request
* @param {Response} response
* @param {NextFunction} next
*/
function handleFileUpload(request, response, next) {
const upload = multer({ storage: fileUploadStorage }).single("file");
upload(request, response, function (err) {
Expand All @@ -73,7 +100,33 @@ function handleFileUpload(request, response, next) {
});
}

// Handle logo asset uploads
/**
* Handle API file upload as documents - this does not manipulate the filename
* at all for encoding/charset reasons.
* @param {Request} request
* @param {Response} response
* @param {NextFunction} next
*/
function handleAPIFileUpload(request, response, next) {
const upload = multer({ storage: fileAPIUploadStorage }).single("file");
upload(request, response, function (err) {
if (err) {
response
.status(500)
.json({
success: false,
error: `Invalid file upload. ${err.message}`,
})
.end();
return;
}
next();
});
}

/**
* Handle logo asset uploads
*/
function handleAssetUpload(request, response, next) {
const upload = multer({ storage: assetUploadStorage }).single("logo");
upload(request, response, function (err) {
Expand All @@ -91,7 +144,9 @@ function handleAssetUpload(request, response, next) {
});
}

// Handle PFP file upload as logos
/**
* Handle PFP file upload as logos
*/
function handlePfpUpload(request, response, next) {
const upload = multer({ storage: pfpUploadStorage }).single("file");
upload(request, response, function (err) {
Expand All @@ -111,6 +166,7 @@ function handlePfpUpload(request, response, next) {

module.exports = {
handleFileUpload,
handleAPIFileUpload,
handleAssetUpload,
handlePfpUpload,
};

0 comments on commit 75592ba

Please sign in to comment.