mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00

* WIP: app.locals refactoring
WIP: appConfig
fix: update memory configuration retrieval to use getAppConfig based on user role
fix: update comment for AppConfig interface to clarify purpose
🏷️ refactor: Update tests to use getAppConfig for endpoint configurations
ci: Update AppService tests to initialize app config instead of app.locals
ci: Integrate getAppConfig into remaining tests
refactor: Update multer storage destination to use promise-based getAppConfig and improve error handling in tests
refactor: Rename initializeAppConfig to setAppConfig and update related tests
ci: Mock getAppConfig in various tests to provide default configurations
refactor: Update convertMCPToolsToPlugins to use mcpManager for server configuration and adjust related tests
chore: rename `Config/getAppConfig` -> `Config/app`
fix: streamline OpenAI image tools configuration by removing direct appConfig dependency and using function parameters
chore: correct parameter documentation for imageOutputType in ToolService.js
refactor: remove `getCustomConfig` dependency in config route
refactor: update domain validation to use appConfig for allowed domains
refactor: use appConfig registration property
chore: remove app parameter from AppService invocation
refactor: update AppConfig interface to correct registration and turnstile configurations
refactor: remove getCustomConfig dependency and use getAppConfig in PluginController, multer, and MCP services
refactor: replace getCustomConfig with getAppConfig in STTService, TTSService, and related files
refactor: replace getCustomConfig with getAppConfig in Conversation and Message models, update tempChatRetention functions to use AppConfig type
refactor: update getAppConfig calls in Conversation and Message models to include user role for temporary chat expiration
ci: update related tests
refactor: update getAppConfig call in getCustomConfigSpeech to include user role
fix: update appConfig usage to access allowedDomains from actions instead of registration
refactor: enhance AppConfig to include fileStrategies and update related file strategy logic
refactor: update imports to use normalizeEndpointName from @librechat/api and remove redundant definitions
chore: remove deprecated unused RunManager
refactor: get balance config primarily from appConfig
refactor: remove customConfig dependency for appConfig and streamline loadConfigModels logic
refactor: remove getCustomConfig usage and use app config in file citations
refactor: consolidate endpoint loading logic into loadEndpoints function
refactor: update appConfig access to use endpoints structure across various services
refactor: implement custom endpoints configuration and streamline endpoint loading logic
refactor: update getAppConfig call to include user role parameter
refactor: streamline endpoint configuration and enhance appConfig usage across services
refactor: replace getMCPAuthMap with getUserMCPAuthMap and remove unused getCustomConfig file
refactor: add type annotation for loadedEndpoints in loadEndpoints function
refactor: move /services/Files/images/parse to TS API
chore: add missing FILE_CITATIONS permission to IRole interface
refactor: restructure toolkits to TS API
refactor: separate manifest logic into its own module
refactor: consolidate tool loading logic into a new tools module for startup logic
refactor: move interface config logic to TS API
refactor: migrate checkEmailConfig to TypeScript and update imports
refactor: add FunctionTool interface and availableTools to AppConfig
refactor: decouple caching and DB operations from AppService, make part of consolidated `getAppConfig`
WIP: fix tests
* fix: rebase conflicts
* refactor: remove app.locals references
* refactor: replace getBalanceConfig with getAppConfig in various strategies and middleware
* refactor: replace appConfig?.balance with getBalanceConfig in various controllers and clients
* test: add balance configuration to titleConvo method in AgentClient tests
* chore: remove unused `openai-chat-tokens` package
* chore: remove unused imports in initializeMCPs.js
* refactor: update balance configuration to use getAppConfig instead of getBalanceConfig
* refactor: integrate configMiddleware for centralized configuration handling
* refactor: optimize email domain validation by removing unnecessary async calls
* refactor: simplify multer storage configuration by removing async calls
* refactor: reorder imports for better readability in user.js
* refactor: replace getAppConfig calls with req.config for improved performance
* chore: replace getAppConfig calls with req.config in tests for centralized configuration handling
* chore: remove unused override config
* refactor: add configMiddleware to endpoint route and replace getAppConfig with req.config
* chore: remove customConfig parameter from TTSService constructor
* refactor: pass appConfig from request to processFileCitations for improved configuration handling
* refactor: remove configMiddleware from endpoint route and retrieve appConfig directly in getEndpointsConfig if not in `req.config`
* test: add mockAppConfig to processFileCitations tests for improved configuration handling
* fix: pass req.config to hasCustomUserVars and call without await after synchronous refactor
* fix: type safety in useExportConversation
* refactor: retrieve appConfig using getAppConfig in PluginController and remove configMiddleware from plugins route, to avoid always retrieving when plugins are cached
* chore: change `MongoUser` typedef to `IUser`
* fix: Add `user` and `config` fields to ServerRequest and update JSDoc type annotations from Express.Request to ServerRequest
* fix: remove unused setAppConfig mock from Server configuration tests
483 lines
13 KiB
JavaScript
483 lines
13 KiB
JavaScript
const express = require('express');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const {
|
|
generateCheckAccess,
|
|
markPublicPromptGroups,
|
|
buildPromptGroupFilter,
|
|
formatPromptGroupsResponse,
|
|
createEmptyPromptGroupsResponse,
|
|
filterAccessibleIdsBySharedLogic,
|
|
} = require('@librechat/api');
|
|
const {
|
|
Permissions,
|
|
SystemRoles,
|
|
ResourceType,
|
|
AccessRoleIds,
|
|
PrincipalType,
|
|
PermissionBits,
|
|
PermissionTypes,
|
|
} = require('librechat-data-provider');
|
|
const {
|
|
getListPromptGroupsByAccess,
|
|
makePromptProduction,
|
|
updatePromptGroup,
|
|
deletePromptGroup,
|
|
createPromptGroup,
|
|
getPromptGroup,
|
|
deletePrompt,
|
|
getPrompts,
|
|
savePrompt,
|
|
getPrompt,
|
|
} = require('~/models/Prompt');
|
|
const {
|
|
canAccessPromptGroupResource,
|
|
canAccessPromptViaGroup,
|
|
requireJwtAuth,
|
|
} = require('~/server/middleware');
|
|
const {
|
|
findPubliclyAccessibleResources,
|
|
getEffectivePermissions,
|
|
findAccessibleResources,
|
|
grantPermission,
|
|
} = require('~/server/services/PermissionService');
|
|
const { getRoleByName } = require('~/models/Role');
|
|
|
|
const router = express.Router();
|
|
|
|
const checkPromptAccess = generateCheckAccess({
|
|
permissionType: PermissionTypes.PROMPTS,
|
|
permissions: [Permissions.USE],
|
|
getRoleByName,
|
|
});
|
|
const checkPromptCreate = generateCheckAccess({
|
|
permissionType: PermissionTypes.PROMPTS,
|
|
permissions: [Permissions.USE, Permissions.CREATE],
|
|
getRoleByName,
|
|
});
|
|
|
|
const checkGlobalPromptShare = generateCheckAccess({
|
|
permissionType: PermissionTypes.PROMPTS,
|
|
permissions: [Permissions.USE, Permissions.CREATE],
|
|
bodyProps: {
|
|
[Permissions.SHARED_GLOBAL]: ['projectIds', 'removeProjectIds'],
|
|
},
|
|
getRoleByName,
|
|
});
|
|
|
|
router.use(requireJwtAuth);
|
|
router.use(checkPromptAccess);
|
|
|
|
/**
|
|
* Route to get single prompt group by its ID
|
|
* GET /groups/:groupId
|
|
*/
|
|
router.get(
|
|
'/groups/:groupId',
|
|
canAccessPromptGroupResource({
|
|
requiredPermission: PermissionBits.VIEW,
|
|
}),
|
|
async (req, res) => {
|
|
const { groupId } = req.params;
|
|
|
|
try {
|
|
const group = await getPromptGroup({ _id: groupId });
|
|
|
|
if (!group) {
|
|
return res.status(404).send({ message: 'Prompt group not found' });
|
|
}
|
|
|
|
res.status(200).send(group);
|
|
} catch (error) {
|
|
logger.error('Error getting prompt group', error);
|
|
res.status(500).send({ message: 'Error getting prompt group' });
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* Route to fetch all prompt groups (ACL-aware)
|
|
* GET /all
|
|
*/
|
|
router.get('/all', async (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
const { name, category, ...otherFilters } = req.query;
|
|
const { filter, searchShared, searchSharedOnly } = buildPromptGroupFilter({
|
|
name,
|
|
category,
|
|
...otherFilters,
|
|
});
|
|
|
|
let accessibleIds = await findAccessibleResources({
|
|
userId,
|
|
role: req.user.role,
|
|
resourceType: ResourceType.PROMPTGROUP,
|
|
requiredPermissions: PermissionBits.VIEW,
|
|
});
|
|
|
|
const publiclyAccessibleIds = await findPubliclyAccessibleResources({
|
|
resourceType: ResourceType.PROMPTGROUP,
|
|
requiredPermissions: PermissionBits.VIEW,
|
|
});
|
|
|
|
const filteredAccessibleIds = await filterAccessibleIdsBySharedLogic({
|
|
accessibleIds,
|
|
searchShared,
|
|
searchSharedOnly,
|
|
publicPromptGroupIds: publiclyAccessibleIds,
|
|
});
|
|
|
|
const result = await getListPromptGroupsByAccess({
|
|
accessibleIds: filteredAccessibleIds,
|
|
otherParams: filter,
|
|
});
|
|
|
|
if (!result) {
|
|
return res.status(200).send([]);
|
|
}
|
|
|
|
const { data: promptGroups = [] } = result;
|
|
if (!promptGroups.length) {
|
|
return res.status(200).send([]);
|
|
}
|
|
|
|
const groupsWithPublicFlag = markPublicPromptGroups(promptGroups, publiclyAccessibleIds);
|
|
res.status(200).send(groupsWithPublicFlag);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error getting prompt groups' });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Route to fetch paginated prompt groups with filters (ACL-aware)
|
|
* GET /groups
|
|
*/
|
|
router.get('/groups', async (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
const { pageSize, pageNumber, limit, cursor, name, category, ...otherFilters } = req.query;
|
|
|
|
const { filter, searchShared, searchSharedOnly } = buildPromptGroupFilter({
|
|
name,
|
|
category,
|
|
...otherFilters,
|
|
});
|
|
|
|
let actualLimit = limit;
|
|
let actualCursor = cursor;
|
|
|
|
if (pageSize && !limit) {
|
|
actualLimit = parseInt(pageSize, 10);
|
|
}
|
|
|
|
let accessibleIds = await findAccessibleResources({
|
|
userId,
|
|
role: req.user.role,
|
|
resourceType: ResourceType.PROMPTGROUP,
|
|
requiredPermissions: PermissionBits.VIEW,
|
|
});
|
|
|
|
const publiclyAccessibleIds = await findPubliclyAccessibleResources({
|
|
resourceType: ResourceType.PROMPTGROUP,
|
|
requiredPermissions: PermissionBits.VIEW,
|
|
});
|
|
|
|
const filteredAccessibleIds = await filterAccessibleIdsBySharedLogic({
|
|
accessibleIds,
|
|
searchShared,
|
|
searchSharedOnly,
|
|
publicPromptGroupIds: publiclyAccessibleIds,
|
|
});
|
|
|
|
const result = await getListPromptGroupsByAccess({
|
|
accessibleIds: filteredAccessibleIds,
|
|
otherParams: filter,
|
|
limit: actualLimit,
|
|
after: actualCursor,
|
|
});
|
|
|
|
if (!result) {
|
|
const emptyResponse = createEmptyPromptGroupsResponse({ pageNumber, pageSize, actualLimit });
|
|
return res.status(200).send(emptyResponse);
|
|
}
|
|
|
|
const { data: promptGroups = [], has_more = false, after = null } = result;
|
|
|
|
const groupsWithPublicFlag = markPublicPromptGroups(promptGroups, publiclyAccessibleIds);
|
|
|
|
const response = formatPromptGroupsResponse({
|
|
promptGroups: groupsWithPublicFlag,
|
|
pageNumber,
|
|
pageSize,
|
|
actualLimit,
|
|
hasMore: has_more,
|
|
after,
|
|
});
|
|
|
|
res.status(200).send(response);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error getting prompt groups' });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Creates a new prompt group with initial prompt
|
|
* @param {object} req
|
|
* @param {TCreatePrompt} req.body
|
|
* @param {Express.Response} res
|
|
*/
|
|
const createNewPromptGroup = async (req, res) => {
|
|
try {
|
|
const { prompt, group } = req.body;
|
|
|
|
if (!prompt || !group || !group.name) {
|
|
return res.status(400).send({ error: 'Prompt and group name are required' });
|
|
}
|
|
|
|
const saveData = {
|
|
prompt,
|
|
group,
|
|
author: req.user.id,
|
|
authorName: req.user.name,
|
|
};
|
|
|
|
const result = await createPromptGroup(saveData);
|
|
|
|
if (result.prompt && result.prompt._id && result.prompt.groupId) {
|
|
try {
|
|
await grantPermission({
|
|
principalType: PrincipalType.USER,
|
|
principalId: req.user.id,
|
|
resourceType: ResourceType.PROMPTGROUP,
|
|
resourceId: result.prompt.groupId,
|
|
accessRoleId: AccessRoleIds.PROMPTGROUP_OWNER,
|
|
grantedBy: req.user.id,
|
|
});
|
|
logger.debug(
|
|
`[createPromptGroup] Granted owner permissions to user ${req.user.id} for promptGroup ${result.prompt.groupId}`,
|
|
);
|
|
} catch (permissionError) {
|
|
logger.error(
|
|
`[createPromptGroup] Failed to grant owner permissions for promptGroup ${result.prompt.groupId}:`,
|
|
permissionError,
|
|
);
|
|
}
|
|
}
|
|
|
|
res.status(200).send(result);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error creating prompt group' });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Adds a new prompt to an existing prompt group
|
|
* @param {object} req
|
|
* @param {TCreatePrompt} req.body
|
|
* @param {Express.Response} res
|
|
*/
|
|
const addPromptToGroup = async (req, res) => {
|
|
try {
|
|
const { groupId } = req.params;
|
|
const { prompt } = req.body;
|
|
|
|
if (!prompt) {
|
|
return res.status(400).send({ error: 'Prompt is required' });
|
|
}
|
|
|
|
// Ensure the prompt is associated with the correct group
|
|
prompt.groupId = groupId;
|
|
|
|
const saveData = {
|
|
prompt,
|
|
author: req.user.id,
|
|
authorName: req.user.name,
|
|
};
|
|
|
|
const result = await savePrompt(saveData);
|
|
res.status(200).send(result);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error adding prompt to group' });
|
|
}
|
|
};
|
|
|
|
// Create new prompt group (requires CREATE permission)
|
|
router.post('/', checkPromptCreate, createNewPromptGroup);
|
|
|
|
// Add prompt to existing group (requires EDIT permission on the group)
|
|
router.post(
|
|
'/groups/:groupId/prompts',
|
|
checkPromptAccess,
|
|
canAccessPromptGroupResource({
|
|
requiredPermission: PermissionBits.EDIT,
|
|
}),
|
|
addPromptToGroup,
|
|
);
|
|
|
|
/**
|
|
* Updates a prompt group
|
|
* @param {object} req
|
|
* @param {object} req.params - The request parameters
|
|
* @param {string} req.params.groupId - The group ID
|
|
* @param {TUpdatePromptGroupPayload} req.body - The request body
|
|
* @param {Express.Response} res
|
|
*/
|
|
const patchPromptGroup = async (req, res) => {
|
|
try {
|
|
const { groupId } = req.params;
|
|
const author = req.user.id;
|
|
const filter = { _id: groupId, author };
|
|
if (req.user.role === SystemRoles.ADMIN) {
|
|
delete filter.author;
|
|
}
|
|
const promptGroup = await updatePromptGroup(filter, req.body);
|
|
res.status(200).send(promptGroup);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error updating prompt group' });
|
|
}
|
|
};
|
|
|
|
router.patch(
|
|
'/groups/:groupId',
|
|
checkGlobalPromptShare,
|
|
canAccessPromptGroupResource({
|
|
requiredPermission: PermissionBits.EDIT,
|
|
}),
|
|
patchPromptGroup,
|
|
);
|
|
|
|
router.patch(
|
|
'/:promptId/tags/production',
|
|
checkPromptCreate,
|
|
canAccessPromptViaGroup({
|
|
requiredPermission: PermissionBits.EDIT,
|
|
resourceIdParam: 'promptId',
|
|
}),
|
|
async (req, res) => {
|
|
try {
|
|
const { promptId } = req.params;
|
|
const result = await makePromptProduction(promptId);
|
|
res.status(200).send(result);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error updating prompt production' });
|
|
}
|
|
},
|
|
);
|
|
|
|
router.get(
|
|
'/:promptId',
|
|
canAccessPromptViaGroup({
|
|
requiredPermission: PermissionBits.VIEW,
|
|
resourceIdParam: 'promptId',
|
|
}),
|
|
async (req, res) => {
|
|
const { promptId } = req.params;
|
|
const prompt = await getPrompt({ _id: promptId });
|
|
res.status(200).send(prompt);
|
|
},
|
|
);
|
|
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const author = req.user.id;
|
|
const { groupId } = req.query;
|
|
|
|
// If requesting prompts for a specific group, check permissions
|
|
if (groupId) {
|
|
const permissions = await getEffectivePermissions({
|
|
userId: req.user.id,
|
|
role: req.user.role,
|
|
resourceType: ResourceType.PROMPTGROUP,
|
|
resourceId: groupId,
|
|
});
|
|
|
|
if (!(permissions & PermissionBits.VIEW)) {
|
|
return res
|
|
.status(403)
|
|
.send({ error: 'Insufficient permissions to view prompts in this group' });
|
|
}
|
|
|
|
// If user has access, fetch all prompts in the group (not just their own)
|
|
const prompts = await getPrompts({ groupId });
|
|
return res.status(200).send(prompts);
|
|
}
|
|
|
|
// If no groupId, return user's own prompts
|
|
const query = { author };
|
|
if (req.user.role === SystemRoles.ADMIN) {
|
|
delete query.author;
|
|
}
|
|
const prompts = await getPrompts(query);
|
|
res.status(200).send(prompts);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error getting prompts' });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Deletes a prompt
|
|
*
|
|
* @param {ServerRequest} req - The request object.
|
|
* @param {TDeletePromptVariables} req.params - The request parameters
|
|
* @param {import('mongoose').ObjectId} req.params.promptId - The prompt ID
|
|
* @param {Express.Response} res - The response object.
|
|
* @return {TDeletePromptResponse} A promise that resolves when the prompt is deleted.
|
|
*/
|
|
const deletePromptController = async (req, res) => {
|
|
try {
|
|
const { promptId } = req.params;
|
|
const { groupId } = req.query;
|
|
const author = req.user.id;
|
|
const query = { promptId, groupId, author, role: req.user.role };
|
|
const result = await deletePrompt(query);
|
|
res.status(200).send(result);
|
|
} catch (error) {
|
|
logger.error(error);
|
|
res.status(500).send({ error: 'Error deleting prompt' });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Delete a prompt group
|
|
* @param {ServerRequest} req
|
|
* @param {ServerResponse} res
|
|
* @returns {Promise<TDeletePromptGroupResponse>}
|
|
*/
|
|
const deletePromptGroupController = async (req, res) => {
|
|
try {
|
|
const { groupId: _id } = req.params;
|
|
// Don't pass author - permissions are now checked by middleware
|
|
const message = await deletePromptGroup({ _id, role: req.user.role });
|
|
res.send(message);
|
|
} catch (error) {
|
|
logger.error('Error deleting prompt group', error);
|
|
res.status(500).send({ message: 'Error deleting prompt group' });
|
|
}
|
|
};
|
|
|
|
router.delete(
|
|
'/:promptId',
|
|
checkPromptCreate,
|
|
canAccessPromptViaGroup({
|
|
requiredPermission: PermissionBits.DELETE,
|
|
resourceIdParam: 'promptId',
|
|
}),
|
|
deletePromptController,
|
|
);
|
|
router.delete(
|
|
'/groups/:groupId',
|
|
checkPromptCreate,
|
|
canAccessPromptGroupResource({
|
|
requiredPermission: PermissionBits.DELETE,
|
|
}),
|
|
deletePromptGroupController,
|
|
);
|
|
|
|
module.exports = router;
|