Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allows admin to edit assistant actions #4591

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions api/server/routes/agents/actions.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
const express = require('express');
const { nanoid } = require('nanoid');
const { actionDelimiter } = require('librechat-data-provider');
const { actionDelimiter, SystemRoles } = require('librechat-data-provider');
const { encryptMetadata, domainParser } = require('~/server/services/ActionService');
const { updateAction, getActions, deleteAction } = require('~/models/Action');
const { getAgent, updateAgent } = require('~/models/Agent');
const { logger } = require('~/config');

const router = express.Router();

// If the user has ADMIN role and the adminCanEditActions is enabled in assistants config
// then action edition is possible even if not owner of the assistant
const isAdmin = (req) => {
const adminCanEditActions = req.app.locals?.assistants?.adminCanEditActions ?? false;
owengo marked this conversation as resolved.
Show resolved Hide resolved
return req.user.role === SystemRoles.ADMIN && adminCanEditActions;
};

/**
* Retrieves all user's actions
* @route GET /actions/
Expand All @@ -16,7 +23,10 @@ const router = express.Router();
*/
router.get('/', async (req, res) => {
try {
res.json(await getActions({ user: req.user.id }));
const admin = isAdmin(req);
// If admin, get all actions, otherwise only user's actions
const searchParams = admin ? {} : { user: req.user.id };
res.json(await getActions(searchParams));
} catch (error) {
res.status(500).json({ error: error.message });
}
Expand Down Expand Up @@ -52,9 +62,12 @@ router.post('/:agent_id', async (req, res) => {

const action_id = _action_id ?? nanoid();
const initialPromises = [];
const admin = isAdmin(req);

// If admin, can edit any agent, otherwise only user's agents
const agentQuery = admin ? { id: agent_id } : { id: agent_id, author: req.user.id };
// TODO: share agents
initialPromises.push(getAgent({ id: agent_id, author: req.user.id }));
initialPromises.push(getAgent(agentQuery));
if (_action_id) {
initialPromises.push(getActions({ action_id }, true));
}
Expand Down Expand Up @@ -90,10 +103,7 @@ router.post('/:agent_id', async (req, res) => {
.filter((tool) => !(tool && (tool.includes(domain) || tool.includes(action_id))))
.concat(functions.map((tool) => `${tool.function.name}${actionDelimiter}${domain}`));

const updatedAgent = await updateAgent(
{ id: agent_id, author: req.user.id },
{ tools, actions },
);
const updatedAgent = await updateAgent(agentQuery, { tools, actions });
/** @type {[Action]} */
const updatedAction = await updateAction(
{ action_id },
Expand Down Expand Up @@ -125,8 +135,11 @@ router.post('/:agent_id', async (req, res) => {
router.delete('/:agent_id/:action_id', async (req, res) => {
try {
const { agent_id, action_id } = req.params;
const admin = isAdmin(req);

const agent = await getAgent({ id: agent_id, author: req.user.id });
// If admin, can delete any agent, otherwise only user's agents
const agentQuery = admin ? { id: agent_id } : { id: agent_id, author: req.user.id };
const agent = await getAgent(agentQuery);
if (!agent) {
return res.status(404).json({ message: 'Agent not found for deleting action' });
}
Expand All @@ -150,11 +163,10 @@ router.delete('/:agent_id/:action_id', async (req, res) => {

const updatedTools = tools.filter((tool) => !(tool && tool.includes(domain)));

await updateAgent(
{ id: agent_id, author: req.user.id },
{ tools: updatedTools, actions: updatedActions },
);
await deleteAction({ action_id });
await updateAgent(agentQuery, { tools: updatedTools, actions: updatedActions });
// If admin, can delete any action, otherwise only user's actions
const actionQuery = admin ? { action_id } : { action_id, user: req.user.id };
await deleteAction(actionQuery);
res.status(200).json({ message: 'Action deleted successfully' });
} catch (error) {
const message = 'Trouble deleting the Agent Action';
Expand Down
1 change: 1 addition & 0 deletions api/server/services/start/assistants.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function assistantsConfigSetup(config, assistantsEndpoint, prevConfig = {}) {
capabilities: parsedConfig.capabilities,
excludedIds: parsedConfig.excludedIds,
privateAssistants: parsedConfig.privateAssistants,
adminCanEditActions: parsedConfig.adminCanEditActions,
timeoutMs: parsedConfig.timeoutMs,
streamRate: parsedConfig.streamRate,
};
Expand Down
1 change: 1 addition & 0 deletions librechat.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ registration:
# Definition of custom endpoints
endpoints:
# assistants:
# adminCanEditActions: false # Allow ADMIN users to see and edit all actions
# disableBuilder: false # Disable Assistants Builder Interface by setting to `true`
# pollIntervalMs: 3000 # Polling interval for checking assistant updates
# timeoutMs: 180000 # Timeout for assistant operations
Expand Down
2 changes: 2 additions & 0 deletions packages/data-provider/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const assistantEndpointSchema = baseEndpointSchema.merge(
z.object({
/* assistants specific */
disableBuilder: z.boolean().optional(),
adminCanEditActions: z.boolean().optional(),
pollIntervalMs: z.number().optional(),
timeoutMs: z.number().optional(),
version: z.union([z.string(), z.number()]).default(2),
Expand Down Expand Up @@ -208,6 +209,7 @@ export const agentsEndpointSChema = baseEndpointSchema.merge(
z.object({
/* assistants specific */
disableBuilder: z.boolean().optional(),
adminCanEditActions: z.boolean().optional(),
pollIntervalMs: z.number().optional(),
timeoutMs: z.number().optional(),
version: z.union([z.string(), z.number()]).default(2),
Expand Down
1 change: 1 addition & 0 deletions packages/data-provider/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export type TConfig = {
userProvide?: boolean | null;
userProvideURL?: boolean | null;
disableBuilder?: boolean;
adminCanEditActions?: boolean;
retrievalModels?: string[];
capabilities?: string[];
};
Expand Down