@@ -146,7 +146,6 @@ pub enum LoadingRecord {
146146 Err ( String ) ,
147147}
148148
149- #[ derive( Default ) ]
150149pub struct ToolManagerBuilder {
151150 prompt_query_result_sender : Option < tokio:: sync:: broadcast:: Sender < PromptQueryResult > > ,
152151 prompt_query_receiver : Option < tokio:: sync:: broadcast:: Receiver < PromptQuery > > ,
@@ -157,9 +156,28 @@ pub struct ToolManagerBuilder {
157156 has_new_stuff : Arc < AtomicBool > ,
158157 mcp_load_record : Arc < Mutex < HashMap < String , Vec < LoadingRecord > > > > ,
159158 new_tool_specs : NewToolSpecs ,
159+ is_first_launch : bool ,
160160 agent : Option < Arc < Mutex < Agent > > > ,
161161}
162162
163+ impl Default for ToolManagerBuilder {
164+ fn default ( ) -> Self {
165+ Self {
166+ prompt_query_result_sender : Default :: default ( ) ,
167+ prompt_query_receiver : Default :: default ( ) ,
168+ prompt_query_sender : Default :: default ( ) ,
169+ prompt_query_result_receiver : Default :: default ( ) ,
170+ messenger_builder : Default :: default ( ) ,
171+ conversation_id : Default :: default ( ) ,
172+ has_new_stuff : Default :: default ( ) ,
173+ mcp_load_record : Default :: default ( ) ,
174+ new_tool_specs : Default :: default ( ) ,
175+ is_first_launch : true ,
176+ agent : Default :: default ( ) ,
177+ }
178+ }
179+ }
180+
163181impl From < & mut ToolManager > for ToolManagerBuilder {
164182 fn from ( value : & mut ToolManager ) -> Self {
165183 Self {
@@ -174,6 +192,9 @@ impl From<&mut ToolManager> for ToolManagerBuilder {
174192 has_new_stuff : value. has_new_stuff . clone ( ) ,
175193 mcp_load_record : value. mcp_load_record . clone ( ) ,
176194 new_tool_specs : value. new_tool_specs . clone ( ) ,
195+ // if we are getting a builder from an instantiated tool manager this field would be
196+ // false
197+ is_first_launch : false ,
177198 ..Default :: default ( )
178199 }
179200 }
@@ -844,6 +865,7 @@ impl ToolManagerBuilder {
844865 }
845866 } ,
846867 messenger_builder : Some ( messenger_builder) ,
868+ is_first_launch : self . is_first_launch ,
847869 ..Default :: default ( )
848870 } )
849871 }
@@ -1000,6 +1022,8 @@ pub struct ToolManager {
10001022 /// As far as tool manager goes, this is relevant for tool and server filters
10011023 /// We need to put this behind a lock because the orchestrator task depends on agent
10021024 pub agent : Arc < Mutex < Agent > > ,
1025+
1026+ is_first_launch : bool ,
10031027}
10041028
10051029impl Clone for ToolManager {
@@ -1021,19 +1045,11 @@ impl Clone for ToolManager {
10211045
10221046impl ToolManager {
10231047 /// Swapping agent involves the following:
1024- /// - Drop all clients
1025- /// - Clear all pending server names
1026- /// - Clear new_tool_specs
1027- /// - Clear tn_map
1028- /// - Clear schema
1029- /// - Clear mcp_load_record
1030- /// - Clear disabled_servers
1031- /// - Swap out agent
1032- /// - Init servers
1033- /// - Flip has_new_stuff to true (this is because we are removing stuff)
1034- /// - Load tools again
1048+ /// - Dropping all of the clients first to avoid resource contention
1049+ /// - Building a new tool manager builder from the current tool manager
1050+ /// - Building a tool manager from said tool manager builder
1051+ /// - Calling load tools
10351052 pub async fn swap_agent ( & mut self , os : & mut Os , output : & mut impl Write , agent : & Agent ) -> eyre:: Result < ( ) > {
1036- // Clear the clients first to avoid resource contention
10371053 self . clients . clear ( ) ;
10381054
10391055 let mut agent_lock = self . agent . lock ( ) . await ;
@@ -1044,9 +1060,9 @@ impl ToolManager {
10441060 let mut new_tool_manager = builder. build ( os, Box :: new ( std:: io:: sink ( ) ) , true ) . await ?;
10451061 std:: mem:: swap ( self , & mut new_tool_manager) ;
10461062
1047- eprintln ! ( "before load tools" ) ;
1048- let _ = self . load_tools ( os , output ) . await ;
1049- eprintln ! ( "after load tools" ) ;
1063+ // we can discard the output here and let background server load take care of getting the
1064+ // new tools
1065+ let _ = self . load_tools ( os , output ) . await ? ;
10501066
10511067 Ok ( ( ) )
10521068 }
@@ -1126,7 +1142,7 @@ impl ToolManager {
11261142 } ) ;
11271143 // We need to cast it to erase the type otherwise the compiler will default to static
11281144 // dispatch, which would result in an error of inconsistent match arm return type.
1129- let timeout_fut: Pin < Box < dyn Future < Output = ( ) > > > = if self . clients . is_empty ( ) {
1145+ let timeout_fut: Pin < Box < dyn Future < Output = ( ) > > > = if self . clients . is_empty ( ) || ! self . is_first_launch {
11301146 // If there is no server loaded, we want to resolve immediately
11311147 Box :: pin ( future:: ready ( ( ) ) )
11321148 } else if self . is_interactive {
0 commit comments