use std::sync::atomic::Ordering;
use tracing::{info, warn};
use crate::ai::AiResponse;
use crate::cli::jarvis::{jarvis_ask_investigate, jarvis_notice};
use crate::engine::{execute, CommandResult, LoopAction};
use super::Shell;
impl Shell {
pub(super) async fn investigate_error(
&mut self,
line: &str,
result: &CommandResult,
from_tool_call: bool,
) {
let ai = match self.ai_client {
Some(ref ai) => ai,
None => return,
};
let should_investigate = if from_tool_call {
info!("Tool Call command failed, auto-investigating");
true
} else {
jarvis_ask_investigate(result.exit_code)
};
if !should_investigate {
return;
}
let context = self
.black_box
.as_ref()
.and_then(|bb| bb.get_recent_context(5).ok())
.unwrap_or_default();
match ai.investigate_error(line, result, &context).await {
Ok(conv_result) => match conv_result.response {
AiResponse::Command(ref fix_cmd) => {
jarvis_notice(fix_cmd);
let fix_result = execute(fix_cmd);
self.last_exit_code
.store(fix_result.exit_code, Ordering::Relaxed);
println!();
if fix_result.action == LoopAction::Continue {
if let Some(ref bb) = self.black_box {
if let Err(e) = bb.record(fix_cmd, &fix_result) {
warn!("Failed to record fix command history: {e}");
}
}
}
}
AiResponse::NaturalLanguage(_) => {
self.conversation_state = Some(conv_result.conversation);
println!();
}
},
Err(e) => {
warn!(error = %e, "Error investigation failed");
}
}
}
}