diff --git a/api/models/Agent.js b/api/models/Agent.js index 6b09745eb9..9cab7d4b3f 100644 --- a/api/models/Agent.js +++ b/api/models/Agent.js @@ -31,18 +31,17 @@ const createAgent = async (agentData) => { const getAgent = async (searchParameter) => await Agent.findOne(searchParameter).lean(); /** - * Update an agent with new data without overwriting existing properties, - * or create a new agent if it doesn't exist, within a transaction session if provided. + * Update an agent with new data without overwriting existing + * properties, or create a new agent if it doesn't exist. * * @param {Object} searchParameter - The search parameters to find the agent to update. * @param {string} searchParameter.id - The ID of the agent to update. - * @param {string} searchParameter.author - The user ID of the agent's author. + * @param {string} [searchParameter.author] - The user ID of the agent's author. * @param {Object} updateData - An object containing the properties to update. - * @param {mongoose.ClientSession} [session] - The transaction session to use (optional). * @returns {Promise} The updated or newly created agent document as a plain object. */ -const updateAgent = async (searchParameter, updateData, session = null) => { - const options = { new: true, upsert: true, session }; +const updateAgent = async (searchParameter, updateData) => { + const options = { new: true, upsert: true }; return await Agent.findOneAndUpdate(searchParameter, updateData, options).lean(); }; @@ -51,7 +50,7 @@ const updateAgent = async (searchParameter, updateData, session = null) => { * * @param {Object} searchParameter - The search parameters to find the agent to delete. * @param {string} searchParameter.id - The ID of the agent to delete. - * @param {string} searchParameter.author - The user ID of the agent's author. + * @param {string} [searchParameter.author] - The user ID of the agent's author. * @returns {Promise} Resolves when the agent has been successfully deleted. */ const deleteAgent = async (searchParameter) => { @@ -126,10 +125,10 @@ const updateAgentProjects = async (agentId, projectIds, removeProjectIds) => { } if (Object.keys(updateOps).length === 0) { - return await Agent.findById(agentId).lean(); + return await getAgent({ id: agentId }); } - return await Agent.findByIdAndUpdate(agentId, updateOps, { new: true, lean: true }); + return await updateAgent({ id: agentId }, updateOps); }; module.exports = { diff --git a/api/server/controllers/agents/v1.js b/api/server/controllers/agents/v1.js index 381c140671..01692bd08d 100644 --- a/api/server/controllers/agents/v1.js +++ b/api/server/controllers/agents/v1.js @@ -101,10 +101,13 @@ const updateAgentHandler = async (req, res) => { const id = req.params.id; const { projectIds, removeProjectIds, ...updateData } = req.body; - const updatedAgent = await updateAgent({ id, author: req.user.id }, updateData); + let updatedAgent; + if (Object.keys(updateData).length > 0) { + updatedAgent = await updateAgent({ id, author: req.user.id }, updateData); + } if (projectIds || removeProjectIds) { - await updateAgentProjects(id, projectIds, removeProjectIds); + updatedAgent = await updateAgentProjects(id, projectIds, removeProjectIds); } return res.json(updatedAgent); diff --git a/client/src/components/SidePanel/Agents/AgentConfig.tsx b/client/src/components/SidePanel/Agents/AgentConfig.tsx index ad5aa630a3..a35aa8e2ea 100644 --- a/client/src/components/SidePanel/Agents/AgentConfig.tsx +++ b/client/src/components/SidePanel/Agents/AgentConfig.tsx @@ -14,6 +14,7 @@ import { useToastContext } from '~/Providers'; import { Spinner } from '~/components/svg'; import DeleteButton from './DeleteButton'; import AgentAvatar from './AgentAvatar'; +import ShareAgent from './ShareAgent'; import AgentTool from './AgentTool'; import { Panel } from '~/common'; @@ -190,7 +191,7 @@ export default function AgentConfig({ name="id" control={control} render={({ field }) => ( -

+

{field.value}

)} @@ -343,6 +344,11 @@ export default function AgentConfig({ setCurrentAgentId={setCurrentAgentId} createMutation={create} /> + {/* Submit Button */} + + + + {localize( + 'com_ui_share_var', + agentName != null && agentName !== '' ? `"${agentName}"` : localize('com_ui_agent'), + )} + +
{ + e.preventDefault(); + e.stopPropagation(); + handleSubmit(onSubmit)(e); + }} + > +
+
+ + +
+ { + const isValid = !(value && agentIsGlobal); + if (!isValid) { + showToast({ + message: localize('com_ui_agent_already_shared_to_all'), + status: 'warning', + }); + } + return isValid; + }, + }} + render={({ field }) => ( + + )} + /> +
+
+ + + +
+
+
+ + ); +} diff --git a/packages/data-provider/src/schemas.ts b/packages/data-provider/src/schemas.ts index 71857d597a..0245b27cc8 100644 --- a/packages/data-provider/src/schemas.ts +++ b/packages/data-provider/src/schemas.ts @@ -90,6 +90,7 @@ export const defaultAgentFormValues = { model_parameters: {}, tools: [], provider: {}, + projectIds: [], code_interpreter: false, image_vision: false, retrieval: false, diff --git a/packages/data-provider/src/types/assistants.ts b/packages/data-provider/src/types/assistants.ts index b0a7e8eea3..0768a3d5b9 100644 --- a/packages/data-provider/src/types/assistants.ts +++ b/packages/data-provider/src/types/assistants.ts @@ -160,6 +160,7 @@ export type Agent = { file_ids: string[]; instructions: string | null; tools?: string[]; + projectIds?: string[]; tool_kwargs?: Record; tool_resources?: ToolResources; metadata?: Record; @@ -192,8 +193,10 @@ export type AgentUpdateParams = { tools?: Array; tool_resources?: ToolResources; provider?: AgentProvider; - model: string | null; - model_parameters: AgentModelParameters; + model?: string | null; + model_parameters?: AgentModelParameters; + projectIds?: string[]; + removeProjectIds?: string[]; }; export type AgentListParams = {