mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-01 16:18:51 +01:00
✨ feat: Refactor Group Assignment and Creation Logic with Enhanced Input Handling
This commit is contained in:
parent
4166eac99d
commit
dd762f7223
2 changed files with 42 additions and 33 deletions
|
|
@ -13,8 +13,8 @@ const connect = require('./connect');
|
|||
console.purple('---------------------------------------');
|
||||
|
||||
// Read arguments from CLI or prompt the user
|
||||
let userEmail = process.argv[2] || (await askQuestion('User email: '));
|
||||
let groupName = process.argv[3] || (await askQuestion('Group name to assign: '));
|
||||
const userEmail = process.argv[2] || (await askQuestion('User email: '));
|
||||
const groupName = process.argv[3] || (await askQuestion('Group name to assign: '));
|
||||
|
||||
// Validate email format
|
||||
if (!userEmail.includes('@')) {
|
||||
|
|
@ -39,19 +39,26 @@ const connect = require('./connect');
|
|||
|
||||
// Assign the group to the user if not already assigned
|
||||
try {
|
||||
if (!user.groups) {
|
||||
if (!Array.isArray(user.groups)) {
|
||||
user.groups = [];
|
||||
}
|
||||
if (!user.groups.includes(group._id)) {
|
||||
|
||||
// Convert both user group IDs and the target group ID to strings for comparison
|
||||
const groupIdStr = group._id.toString();
|
||||
const userGroupIds = user.groups.map(id => id.toString());
|
||||
|
||||
if (!userGroupIds.includes(groupIdStr)) {
|
||||
user.groups.push(group._id);
|
||||
await user.save();
|
||||
console.green(`User ${user.email} successfully assigned to group ${group.name}!`);
|
||||
} else {
|
||||
console.yellow(`User ${user.email} is already assigned to group ${group.name}.`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.red('Error assigning group to user: ' + error.message);
|
||||
silentExit(1);
|
||||
}
|
||||
|
||||
console.green(`User ${user.email} successfully assigned to group ${group.name}!`);
|
||||
silentExit(0);
|
||||
})();
|
||||
|
||||
|
|
|
|||
|
|
@ -11,40 +11,42 @@ const connect = require('./connect');
|
|||
console.purple('Create a New Group');
|
||||
console.purple('---------------------------------------');
|
||||
|
||||
// Read arguments from CLI or prompt the user
|
||||
let groupName = process.argv[2] || (await askQuestion('Group name: '));
|
||||
let groupDescription =
|
||||
// Prompt for basic group info.
|
||||
const groupName = process.argv[2] || (await askQuestion('Group name: '));
|
||||
const groupDescription =
|
||||
process.argv[3] || (await askQuestion('Group description (optional): '));
|
||||
let allowedEndpointsInput =
|
||||
process.argv[4] ||
|
||||
(await askQuestion(
|
||||
'Allowed endpoints (comma separated, e.g., "assistants,agents", or enter "*" for all): ',
|
||||
));
|
||||
let allowedModelsInput =
|
||||
process.argv[5] ||
|
||||
(await askQuestion(
|
||||
'Allowed models (comma separated, e.g., "gpt-4,chatgpt-4o-latest", or enter "*" for all): ',
|
||||
));
|
||||
|
||||
// Process the comma-separated inputs into arrays
|
||||
const allowedEndpoints = allowedEndpointsInput
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s);
|
||||
const allowedModels = allowedModelsInput
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s);
|
||||
// Ask for the group type (local or openid; defaults to local)
|
||||
let groupType =
|
||||
process.argv[4] ||
|
||||
(await askQuestion('Group type (local/openid, default is local): '));
|
||||
groupType = groupType.trim().toLowerCase() || 'local';
|
||||
|
||||
let groupData;
|
||||
if (groupType === 'openid') {
|
||||
// For OpenID groups, prompt for an external ID.
|
||||
const externalId =
|
||||
process.argv[5] ||
|
||||
(await askQuestion('External ID for OpenID group: '));
|
||||
groupData = {
|
||||
name: groupName,
|
||||
description: groupDescription,
|
||||
provider: 'openid',
|
||||
externalId: externalId.trim(),
|
||||
};
|
||||
} else {
|
||||
// For local groups, we only need name and description.
|
||||
groupData = {
|
||||
name: groupName,
|
||||
description: groupDescription,
|
||||
provider: 'local',
|
||||
};
|
||||
}
|
||||
|
||||
// Create the group document
|
||||
let group;
|
||||
try {
|
||||
group = await Group.create({
|
||||
name: groupName,
|
||||
description: groupDescription,
|
||||
allowedEndpoints,
|
||||
allowedModels,
|
||||
});
|
||||
group = await Group.create(groupData);
|
||||
} catch (error) {
|
||||
console.red('Error creating group: ' + error.message);
|
||||
silentExit(1);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue