Skip to content

Commit e0c755e

Browse files
committed
precomputes total number of servers in loading prior to spawning display task
1 parent cb90e38 commit e0c755e

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

crates/chat-cli/src/cli/chat/tool_manager.rs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ pub enum GetPromptError {
125125
/// display thread. These messages control the visual loading indicators shown to
126126
/// the user during tool initialization.
127127
enum LoadingMsg {
128-
/// Indicates a new tool is being initialized and should be added to the loading
129-
/// display. The String parameter is the name of the tool being initialized.
130-
Add(String),
131128
/// Indicates a tool has finished initializing successfully and should be removed from
132129
/// the loading display. The String parameter is the name of the tool that
133130
/// completed initialization.
@@ -283,6 +280,15 @@ impl ToolManagerBuilder {
283280
(sanitized_server_name, custom_tool_client)
284281
})
285282
.collect::<Vec<(String, _)>>();
283+
let mut loading_servers = HashMap::<String, StatusLine>::new();
284+
for (server_name, _) in &pre_initialized {
285+
let init_time = std::time::Instant::now();
286+
let is_done = false;
287+
let status_line = StatusLine { init_time, is_done };
288+
loading_servers.insert(server_name.clone(), status_line);
289+
output.flush()?;
290+
}
291+
let total = loading_servers.len();
286292

287293
// Send up task to update user on server loading status
288294
let (tx, rx) = std::sync::mpsc::channel::<LoadingMsg>();
@@ -291,28 +297,16 @@ impl ToolManagerBuilder {
291297
// just no code path with it being None). When ran with no-interactive mode, we really do
292298
// not have a need to run this task.
293299
let loading_display_task = tokio::task::spawn_blocking(move || {
294-
let mut loading_servers = HashMap::<String, StatusLine>::new();
300+
if total == 0 {
301+
return Ok::<_, eyre::Report>(());
302+
}
295303
let mut spinner_logo_idx: usize = 0;
296304
let mut complete: usize = 0;
297305
let mut failed: usize = 0;
306+
queue_init_message(spinner_logo_idx, complete, failed, total, &mut output)?;
298307
loop {
299308
match rx.recv_timeout(std::time::Duration::from_millis(50)) {
300309
Ok(recv_result) => match recv_result {
301-
LoadingMsg::Add(name) => {
302-
let init_time = std::time::Instant::now();
303-
let is_done = false;
304-
let status_line = StatusLine { init_time, is_done };
305-
execute!(output, cursor::MoveToColumn(0))?;
306-
if !loading_servers.is_empty() {
307-
// TODO: account for terminal width
308-
execute!(output, cursor::MoveUp(1))?;
309-
}
310-
loading_servers.insert(name.clone(), status_line);
311-
let total = loading_servers.len();
312-
execute!(output, terminal::Clear(terminal::ClearType::CurrentLine))?;
313-
queue_init_message(spinner_logo_idx, complete, failed, total, &mut output)?;
314-
output.flush()?;
315-
},
316310
LoadingMsg::Done(name) => {
317311
if let Some(status_line) = loading_servers.get_mut(&name) {
318312
status_line.is_done = true;
@@ -327,7 +321,6 @@ impl ToolManagerBuilder {
327321
terminal::Clear(terminal::ClearType::CurrentLine),
328322
)?;
329323
queue_success_message(&name, &time_taken, &mut output)?;
330-
let total = loading_servers.len();
331324
queue_init_message(spinner_logo_idx, complete, failed, total, &mut output)?;
332325
output.flush()?;
333326
}
@@ -346,7 +339,6 @@ impl ToolManagerBuilder {
346339
terminal::Clear(terminal::ClearType::CurrentLine),
347340
)?;
348341
queue_failure_message(&name, &msg, &mut output)?;
349-
let total = loading_servers.len();
350342
queue_init_message(spinner_logo_idx, complete, failed, total, &mut output)?;
351343
}
352344
if loading_servers.iter().all(|(_, status)| status.is_done) {
@@ -365,7 +357,6 @@ impl ToolManagerBuilder {
365357
)?;
366358
let msg = eyre::eyre!(msg.to_string());
367359
queue_warn_message(&name, &msg, &mut output)?;
368-
let total = loading_servers.len();
369360
queue_init_message(spinner_logo_idx, complete, failed, total, &mut output)?;
370361
output.flush()?;
371362
}
@@ -391,7 +382,6 @@ impl ToolManagerBuilder {
391382
acc
392383
});
393384
let msg = eyre::eyre!(msg);
394-
let total = loading_servers.len();
395385
queue_incomplete_load_message(complete, total, &msg, &mut output)?;
396386
}
397387
execute!(output, style::Print("\n"),)?;
@@ -487,9 +477,6 @@ impl ToolManagerBuilder {
487477
} => {},
488478
UpdateEventMessage::InitStart { server_name } => {
489479
pending_clone.write().await.insert(server_name.clone());
490-
if let Some(sender) = &load_msg_sender {
491-
let _ = sender.send(LoadingMsg::Add(server_name));
492-
}
493480
},
494481
}
495482
}
@@ -1174,7 +1161,7 @@ fn process_tool_specs(
11741161
(tool_name.as_str(), "tool schema contains empty description")
11751162
},
11761163
};
1177-
acc.push_str(format!(" - {} ({})\n", tool_name, msg).as_str());
1164+
acc.push_str(format!(" - {} ({})\n", tool_name, msg).as_str());
11781165
acc
11791166
},
11801167
);
@@ -1312,11 +1299,11 @@ fn queue_failure_message(name: &str, fail_load_msg: &eyre::Report, output: &mut
13121299
style::SetForegroundColor(style::Color::Blue),
13131300
style::Print(name),
13141301
style::ResetColor,
1315-
style::Print(" has failed to load:\n- "),
1302+
style::Print(" has failed to load:\n - "),
13161303
style::Print(fail_load_msg),
13171304
style::Print("\n"),
13181305
style::Print(format!(
1319-
"- run with Q_LOG_LEVEL=trace and see $TMPDIR/{CHAT_BINARY_NAME} for detail\n"
1306+
" - run with Q_LOG_LEVEL=trace and see $TMPDIR/{CHAT_BINARY_NAME} for detail\n"
13201307
)),
13211308
style::ResetColor,
13221309
)?)

0 commit comments

Comments
 (0)