@@ -313,31 +313,40 @@ const mount = async (
313313 socket . emit ( 'toolAdded' , state . tools ) ;
314314 } ) ;
315315
316- socket . on ( 'removeTool' , async ( tool ) => {
316+ socket . on ( 'removeTool' , async ( tool , alterChatState ) => {
317317 if ( runningScript ) {
318318 await runningScript . close ( ) ;
319319 runningScript = null ;
320320 }
321321
322- const stateTools = ( state . tools || [ ] ) . filter ( ( t ) => t !== tool ) ;
323- // find the root tool and then remove the tool
324- for ( let block of script ) {
325- if ( block . type === 'tool' ) {
326- if ( ! block . tools ) break ;
327- block . tools = [ ...new Set ( block . tools . filter ( ( t ) => t !== tool ) ) ] ;
328- break ;
322+ const stateTools = ( state . tools || [ ] ) . filter ( ( t ) => {
323+ if ( Array . isArray ( tool ) ) {
324+ return ! tool . includes ( t ) ;
329325 }
330- }
326+ return t !== tool ;
327+ } ) ;
331328
332329 socket . emit ( 'removingTool' ) ;
333330
334- const loaded = await gptscript . loadTools ( script , true ) ;
335- const newTools = toChatStateTools ( loaded ?. program ?. toolSet ) ;
336- const currentState = JSON . parse ( state . chatState ) ;
337- currentState . continuation . state . completion . tools = newTools ;
331+ if ( alterChatState ) {
332+ // find the root tool and then remove the tool
333+ for ( let block of script ) {
334+ if ( block . type === 'tool' ) {
335+ if ( ! block . tools ) break ;
336+ block . tools = [ ...new Set ( block . tools . filter ( ( t ) => t !== tool ) ) ] ;
337+ break ;
338+ }
339+ }
340+
341+ const loaded = await gptscript . loadTools ( script , true ) ;
342+ const newTools = toChatStateTools ( loaded ?. program ?. toolSet ) ;
343+ const currentState = JSON . parse ( state . chatState ) ;
344+ currentState . continuation . state . completion . tools = newTools ;
345+
346+ opts . chatState = JSON . stringify ( currentState ) ;
347+ state . chatState = JSON . stringify ( currentState ) ;
348+ }
338349
339- opts . chatState = JSON . stringify ( currentState ) ;
340- state . chatState = JSON . stringify ( currentState ) ;
341350 state . tools = stateTools ;
342351
343352 if ( threadID ) {
0 commit comments