mirror of
https://github.com/hanzoai/chat.git
synced 2026-09-30 20:51:17 +00:00
librechat-data-provider → @hanzochat/data-provider @librechat/data-schemas → @hanzochat/data-schemas @librechat/client → @hanzochat/client (@hanzochat/api already forked; external @librechat/agents fork follows) All imports, package.json names/deps, tsconfig path aliases, jest moduleNameMapper, and rollup/vite build aliases updated. The data-provider react-query subpath resolves via the exports map. Lockfiles regenerated in a follow-on once the canonical package manager is confirmed.
101 lines
3.4 KiB
JavaScript
101 lines
3.4 KiB
JavaScript
const { logger } = require('@hanzochat/data-schemas');
|
|
const {
|
|
Constants,
|
|
ResourceType,
|
|
isAgentsEndpoint,
|
|
isEphemeralAgentId,
|
|
} = require('@hanzochat/data-provider');
|
|
const { canAccessResource } = require('./canAccessResource');
|
|
const { getAgent } = require('~/models/Agent');
|
|
|
|
/**
|
|
* Agent ID resolver function for agent_id from request body
|
|
* Resolves custom agent ID (e.g., "agent_abc123") to MongoDB ObjectId
|
|
* This is used specifically for chat routes where agent_id comes from request body
|
|
*
|
|
* @param {string} agentCustomId - Custom agent ID from request body
|
|
* @returns {Promise<Object|null>} Agent document with _id field, or null if not found
|
|
*/
|
|
const resolveAgentIdFromBody = async (agentCustomId) => {
|
|
// Handle ephemeral agents - they don't need permission checks
|
|
// Real agent IDs always start with "agent_", so anything else is ephemeral
|
|
if (isEphemeralAgentId(agentCustomId)) {
|
|
return null; // No permission check needed for ephemeral agents
|
|
}
|
|
|
|
return await getAgent({ id: agentCustomId });
|
|
};
|
|
|
|
/**
|
|
* Middleware factory that creates middleware to check agent access permissions from request body.
|
|
* This middleware is specifically designed for chat routes where the agent_id comes from req.body
|
|
* instead of route parameters.
|
|
*
|
|
* @param {Object} options - Configuration options
|
|
* @param {number} options.requiredPermission - The permission bit required (1=view, 2=edit, 4=delete, 8=share)
|
|
* @returns {Function} Express middleware function
|
|
*
|
|
* @example
|
|
* // Basic usage for agent chat (requires VIEW permission)
|
|
* router.post('/chat',
|
|
* canAccessAgentFromBody({ requiredPermission: PermissionBits.VIEW }),
|
|
* buildEndpointOption,
|
|
* chatController
|
|
* );
|
|
*/
|
|
const canAccessAgentFromBody = (options) => {
|
|
const { requiredPermission } = options;
|
|
|
|
// Validate required options
|
|
if (!requiredPermission || typeof requiredPermission !== 'number') {
|
|
throw new Error('canAccessAgentFromBody: requiredPermission is required and must be a number');
|
|
}
|
|
|
|
return async (req, res, next) => {
|
|
try {
|
|
const { endpoint, agent_id } = req.body;
|
|
let agentId = agent_id;
|
|
|
|
if (!isAgentsEndpoint(endpoint)) {
|
|
agentId = Constants.EPHEMERAL_AGENT_ID;
|
|
}
|
|
|
|
// A missing agent_id on the agents endpoint is an ad-hoc (ephemeral) chat,
|
|
// not an error: "type a message and get a reply" must work before the user
|
|
// has created or selected an agent. Ephemeral agents carry no per-agent ACL,
|
|
// so skip the permission check and fall through to plain chat. A missing id
|
|
// is ephemeral by the canonical predicate (isEphemeralAgentId(undefined) === true).
|
|
if (isEphemeralAgentId(agentId)) {
|
|
return next();
|
|
}
|
|
|
|
const agentAccessMiddleware = canAccessResource({
|
|
resourceType: ResourceType.AGENT,
|
|
requiredPermission,
|
|
resourceIdParam: 'agent_id', // This will be ignored since we use custom resolver
|
|
idResolver: () => resolveAgentIdFromBody(agentId),
|
|
});
|
|
|
|
const tempReq = {
|
|
...req,
|
|
params: {
|
|
...req.params,
|
|
agent_id: agentId,
|
|
},
|
|
};
|
|
|
|
return agentAccessMiddleware(tempReq, res, next);
|
|
} catch (error) {
|
|
logger.error('Failed to validate agent access permissions', error);
|
|
return res.status(500).json({
|
|
error: 'Internal Server Error',
|
|
message: 'Failed to validate agent access permissions',
|
|
});
|
|
}
|
|
};
|
|
};
|
|
|
|
module.exports = {
|
|
canAccessAgentFromBody,
|
|
};
|