Skip to main content

vtcode_core/ui/
user_confirmation.rs

1//! User confirmation utilities for safety-critical operations
2//!
3//! This module provides utilities for asking user confirmation before
4//! performing operations that may be expensive or require explicit consent.
5
6use crate::utils::colors::style;
7use anyhow::Result;
8use dialoguer::{Confirm, Input, Select};
9
10/// User confirmation utilities for safety-critical operations
11pub struct UserConfirmation;
12
13/// Result of a tool confirmation prompt
14#[derive(Debug, Clone, PartialEq)]
15pub enum ToolConfirmationResult {
16    /// Allow this specific execution
17    Yes,
18    /// Allow this and all future executions of this tool
19    YesAutoAccept,
20    /// Deny this execution
21    No,
22    /// Deny and provide feedback to the agent
23    Feedback(String),
24}
25
26impl UserConfirmation {
27    /// Simple yes/no confirmation with custom message
28    #[must_use = "confirmation action failure is silently ignored"]
29    pub fn confirm_action(message: &str, default: bool) -> Result<bool> {
30        Confirm::new()
31            .with_prompt(message)
32            .default(default)
33            .interact()
34            .map_err(Into::into)
35    }
36
37    /// Ask for detailed confirmation for tool usage
38    pub fn confirm_tool_usage(tool_name: &str, tool_args: Option<&str>) -> Result<ToolConfirmationResult> {
39        println!("{}", style("Tool Execution Confirmation").cyan().bold());
40        println!("Tool: {}", style(tool_name).cyan().bold());
41        if let Some(args) = tool_args {
42            println!("Args: {}", style(args).dim());
43        }
44        println!();
45
46        let options = vec![
47            "Yes - Allow this execution",
48            "Yes - Always allow this tool (Auto-accept)",
49            "No - Deny this execution",
50            "No - Deny and provide feedback to agent",
51        ];
52
53        let selection = Select::new()
54            .with_prompt("How would you like to proceed?")
55            .default(0)
56            .items(&options)
57            .interact()?;
58
59        match selection {
60            0 => Ok(ToolConfirmationResult::Yes),
61            1 => Ok(ToolConfirmationResult::YesAutoAccept),
62            2 => Ok(ToolConfirmationResult::No),
63            3 => {
64                let feedback: String = Input::new()
65                    .with_prompt("Enter feedback for the agent")
66                    .allow_empty(false)
67                    .interact_text()?;
68                Ok(ToolConfirmationResult::Feedback(feedback))
69            }
70            _ => Ok(ToolConfirmationResult::No),
71        }
72    }
73}