import React, { useState } from 'react'; import { useFormContext } from 'react-hook-form'; import { useUpdateUserPluginsMutation } from 'librechat-data-provider/react-query'; import { OGDialog, TrashIcon, CircleHelpIcon, useToastContext, OGDialogTrigger, OGDialogTemplate, } from '@librechat/client'; import type { TPlugin } from 'librechat-data-provider'; import type { AgentForm } from '~/common'; import { useLocalize } from '~/hooks'; import { cn } from '~/utils'; export default function AgentTool({ tool, regularTools, }: { tool: string; regularTools?: TPlugin[]; agent_id?: string; }) { const localize = useLocalize(); const { showToast } = useToastContext(); const updateUserPlugins = useUpdateUserPluginsMutation(); const { getValues, setValue } = useFormContext(); const [isFocused, setIsFocused] = useState(false); const [isHovering, setIsHovering] = useState(false); if (!regularTools) { return null; } const currentTool = regularTools.find((t) => t.pluginKey === tool); if (!currentTool) { return null; } const removeTool = (toolId: string) => { if (toolId) { updateUserPlugins.mutate( { pluginKey: toolId, action: 'uninstall', auth: {}, isEntityTool: true }, { onError: (error: unknown) => { showToast({ message: `Error while deleting the tool: ${error}`, status: 'error' }); }, onSuccess: () => { const remainingToolIds = getValues('tools')?.filter((id: string) => id !== toolId); setValue('tools', remainingToolIds); showToast({ message: 'Tool deleted successfully', status: 'success' }); }, }, ); } }; return (
setIsHovering(true)} onMouseLeave={() => setIsHovering(false)} onFocus={() => setIsFocused(true)} onBlur={(e) => { // Check if focus is moving to a child element if (!e.currentTarget.contains(e.relatedTarget)) { setIsFocused(false); } }} >
{currentTool.icon && (
)}
{currentTool.name}

{localize('com_ui_delete_tool_confirm')}{' '} "{currentTool.name}"?

{currentTool.description && (

{currentTool.description}

)}
} selection={{ selectHandler: () => removeTool(tool), selectClasses: 'bg-red-700 hover:bg-red-800 dark:bg-red-600 dark:hover:bg-red-800 text-white', selectText: localize('com_ui_delete'), }} /> ); }