File size: 1,507 Bytes
d90101d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::sync::Arc;

use forge_app::{FollowUpService, UserInfra};

/// Use this tool when you encounter ambiguities, need clarification, or require
/// more details to proceed effectively. Use this tool judiciously to maintain a
/// balance between gathering necessary information and avoiding excessive
/// back-and-forth.
#[derive(Debug)]
pub struct ForgeFollowup<F> {
    infra: Arc<F>,
}

impl<F> ForgeFollowup<F> {
    pub fn new(infra: Arc<F>) -> Self {
        Self { infra }
    }
}

#[async_trait::async_trait]
impl<F: UserInfra> FollowUpService for ForgeFollowup<F> {
    async fn follow_up(
        &self,
        question: String,
        options: Vec<String>,
        multiple: Option<bool>,
    ) -> anyhow::Result<Option<String>> {
        let inquire = &self.infra;
        let result = match (options.is_empty(), multiple.unwrap_or_default()) {
            (true, _) => inquire.prompt_question(&question).await?,
            (false, true) => inquire
                .select_many(&question, options)
                .await?
                .map(|selected| {
                    format!(
                        "User selected {} option(s): {}",
                        selected.len(),
                        selected.join(", ")
                    )
                }),
            (false, false) => inquire
                .select_one(&question, options)
                .await?
                .map(|selected| format!("User selected: {selected}")),
        };

        Ok(result)
    }
}