@@ -16,7 +16,7 @@ import { Settings } from "lucide-react";
1616
1717interface ToolManagementProps {
1818 toolGroups : ToolGroup [ ] ;
19- editable ?: boolean ;
19+ isCreatingMode ?: boolean ;
2020 currentAgentId ?: number | undefined ;
2121}
2222
@@ -26,12 +26,13 @@ interface ToolManagementProps {
2626 */
2727export default function ToolManagement ( {
2828 toolGroups,
29- editable = true ,
29+ isCreatingMode = true ,
3030 currentAgentId,
3131} : ToolManagementProps ) {
3232 const { t } = useTranslation ( "common" ) ;
3333 const queryClient = useQueryClient ( ) ;
3434
35+ const editable = currentAgentId !== null || isCreatingMode ;
3536 // Get state from store
3637 const usedTools = useAgentConfigStore ( ( state ) => state . editedAgent . tools ) ;
3738 const updateTools = useAgentConfigStore ( ( state ) => state . updateTools ) ;
@@ -49,45 +50,50 @@ export default function ToolManagement({
4950 const [ toolParams , setToolParams ] = useState < ToolParam [ ] > ( [ ] ) ;
5051
5152 // Get tool info for selected tool (when checking if config is needed)
52- const { data : selectedToolInfo } = useToolInfo (
53+ const { data : selectedToolInfo , isLoading : isToolInfoLoading } = useToolInfo (
5354 ( selectedTool ) ? parseInt ( selectedTool . id ) : null ,
5455 currentAgentId ?? null
5556 ) ;
5657
5758 // Effect to handle tool selection when tool info is loaded
5859 useEffect ( ( ) => {
59- if ( selectedTool && selectedToolInfo ) {
60- // Use instance params if available, otherwise use default params
61- const mergedParams = selectedTool . initParams ?. map ( ( param : ToolParam ) => {
60+ let mergedParams : ToolParam [ ] ;
61+
62+ if ( isCreatingMode && selectedTool ) {
63+ mergedParams = selectedTool . initParams || [ ] ;
64+ } else if ( selectedTool && selectedToolInfo ) {
65+ mergedParams = selectedTool . initParams ?. map ( ( param : ToolParam ) => {
6266 const instanceValue = selectedToolInfo ?. params ?. [ param . name ] ;
6367 return {
6468 ...param ,
6569 value : instanceValue !== undefined ? instanceValue : param . value ,
6670 } ;
6771 } ) || [ ] ;
68- setToolParams ( mergedParams ) ;
69-
70- const hasEmptyRequiredParams = mergedParams . some (
71- ( param : ToolParam ) => param . required &&
72+ } else {
73+ return ;
74+ }
75+ setToolParams ( mergedParams ) ;
76+ const hasEmptyRequiredParams = mergedParams . some (
77+ ( param : ToolParam ) => param . required &&
7278 ( param . value === undefined || param . value === '' || param . value === null )
73- ) ;
74- if ( isClickSetting || hasEmptyRequiredParams ) {
75- // Open modal for configuration with pre-calculated params
76- setIsToolModalOpen ( true ) ;
77- setIsClickSetting ( false )
78- } else {
79- // Add tool directly
80- const newSelectedTools = [ ...usedTools , {
81- ...selectedTool ,
82- initParams : mergedParams
83- } ] ;
84- updateTools ( newSelectedTools ) ;
85- setSelectedTool ( null ) ; // Clear selected tool
86- setIsClickSetting ( false )
87- }
88-
79+ ) ;
80+ if ( isClickSetting || hasEmptyRequiredParams ) {
81+ // Open modal for configuration with pre-calculated params
82+ setIsToolModalOpen ( true ) ;
83+ setIsClickSetting ( false )
84+ } else {
85+ // Add tool directly
86+ const newSelectedTools = [ ...usedTools , {
87+ ...selectedTool ,
88+ initParams : mergedParams
89+ } ] ;
90+ updateTools ( newSelectedTools ) ;
91+ setSelectedTool ( null ) ; // Clear selected tool
92+ setIsClickSetting ( false )
8993 }
90- } , [ selectedTool , selectedToolInfo ] ) ;
94+
95+
96+ } , [ selectedTool , isToolInfoLoading ] ) ;
9197
9298 // Create selected tool ID set for efficient lookup
9399 const selectedToolIdsSet = new Set (
@@ -109,48 +115,55 @@ export default function ToolManagement({
109115 } ;
110116
111117 const handleToolModalSave = async ( params : ToolParam [ ] ) => {
112- if ( ! selectedTool || ! currentAgentId ) return ;
118+ if ( ! selectedTool ) return ;
113119
114- try {
115120 // Convert params to backend format
116121 const paramsObj = params . reduce ( ( acc , param ) => {
117122 acc [ param . name ] = param . value ;
118123 return acc ;
119124 } , { } as Record < string , any > ) ;
120125
121- // Save tool config
122- const isEnabled = true ; // New tool is enabled by default
123- const result = await updateToolConfig (
124- parseInt ( selectedTool . id ) ,
125- currentAgentId ,
126- paramsObj ,
127- isEnabled
128- ) ;
129-
130- if ( result . success ) {
131- // Add tool to selected tools with updated params
132- const updatedTool = { ...selectedTool , initParams : params } ;
133- const newSelectedTools = [ ...usedTools , updatedTool ] ;
134- updateTools ( newSelectedTools ) ;
135-
136- message . success ( t ( "toolConfig.message.saveSuccess" ) ) ;
126+ if ( isCreatingMode ) {
127+ saveToolConfig ( params ) ;
128+ } else if ( currentAgentId ) {
137129
138- queryClient . invalidateQueries ( {
139- queryKey : [ "toolInfo" , parseInt ( selectedTool . id ) , currentAgentId ]
140- } ) ;
130+ try {
131+ const isEnabled = true ; // New tool is enabled by default
132+ const result = await updateToolConfig (
133+ parseInt ( selectedTool . id ) ,
134+ currentAgentId ,
135+ paramsObj ,
136+ isEnabled
137+ ) ;
141138
142- setIsToolModalOpen ( false ) ;
143- setSelectedTool ( null ) ;
144- setToolParams ( [ ] ) ;
145- setIsClickSetting ( false )
146- } else {
147- message . error ( result . message || t ( "toolConfig.message.saveError" ) ) ;
139+ if ( result . success ) {
140+ saveToolConfig ( params ) ;
141+ queryClient . invalidateQueries ( {
142+ queryKey : [ "toolInfo" , parseInt ( selectedTool . id ) , currentAgentId ]
143+ } ) ;
144+ } else {
145+ message . error ( result . message || t ( "toolConfig.message.saveError" ) ) ;
146+ }
147+ } catch ( error ) {
148+ message . error ( t ( "toolConfig.message.saveError" ) ) ;
149+ }
148150 }
149- } catch ( error ) {
150- message . error ( t ( "toolConfig.message.saveError" ) ) ;
151- }
152151 } ;
153152
153+
154+ const saveToolConfig = async ( params : ToolParam [ ] ) => {
155+ // Add tool to selected tools with updated params
156+ const updatedTool = { ...selectedTool ! , initParams : params } ;
157+ const newSelectedTools = [ ...usedTools , updatedTool ] ;
158+ updateTools ( newSelectedTools ) ;
159+
160+ message . success ( t ( "toolConfig.message.saveSuccess" ) ) ;
161+
162+ setIsToolModalOpen ( false ) ;
163+ setSelectedTool ( null ) ;
164+ setToolParams ( [ ] ) ;
165+ setIsClickSetting ( false )
166+ }
154167 const handleToolSettingsClick = ( tool : Tool ) => {
155168 setIsClickSetting ( true )
156169 setSelectedTool ( tool ) ;
@@ -164,7 +177,6 @@ export default function ToolManagement({
164177 const isCurrentlySelected = usedTools . some (
165178 ( t ) => parseInt ( t . id ) === toolId
166179 ) ;
167-
168180 if ( isCurrentlySelected ) {
169181 const newSelectedTools = usedTools . filter ( ( t ) => parseInt ( t . id ) !== toolId ) ;
170182 updateTools ( newSelectedTools ) ;
0 commit comments