ask_user tool + choose popup: model asks the user to pick options or type an answer
This commit is contained in:
1 parent
90b7aa9654
commit
c7341d3085
4 files changed
+411
-2
No files matched your search
@@ -24,6 +24,27 @@ pub type Ask = std::sync::Arc<
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
/// A multiple-choice question for the user (the `ask_user` tool).
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Question {
|
||||
pub text: String,
|
||||
/// (label, description)
|
||||
pub options: Vec<(String, String)>,
|
||||
pub multi: bool,
|
||||
/// offer a free-text "Other" row
|
||||
pub other: bool,
|
||||
}
|
||||
|
||||
/// Shows a [`Question`]; resolves to the chosen labels / typed text, None when dismissed.
|
||||
pub type Choose = std::sync::Arc<
|
||||
dyn Fn(
|
||||
Question,
|
||||
)
|
||||
-> std::pin::Pin<Box<dyn std::future::Future<Output = Option<Vec<String>>> + Send>>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>;
|
||||
|
||||
/// Cross-tool shared state.
|
||||
#[derive(Default)]
|
||||
pub struct State {
|
||||
@@ -31,6 +52,8 @@ pub struct State {
|
||||
pub seen: Mutex<HashMap<PathBuf, Option<SystemTime>>>,
|
||||
/// Set by interactive frontends; None = nobody to ask (print mode, mcp-serve).
|
||||
pub ask: std::sync::OnceLock<Ask>,
|
||||
/// Same for multiple-choice questions.
|
||||
pub choose: std::sync::OnceLock<Choose>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@@ -42,6 +65,14 @@ impl State {
|
||||
}
|
||||
}
|
||||
|
||||
/// Err when no interactive frontend is attached; Ok(None) when the user dismissed it.
|
||||
pub async fn choose(&self, q: Question) -> Result<Option<Vec<String>>, &'static str> {
|
||||
match self.choose.get() {
|
||||
Some(c) => Ok(c(q).await),
|
||||
None => Err("no interactive user attached; decide yourself or state your assumption"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mark_seen(&self, p: &std::path::Path) {
|
||||
let m = std::fs::metadata(p).and_then(|m| m.modified()).ok();
|
||||
self.seen
|
||||
|
||||
Reference in new issue
Block a user