ask_user tool + choose popup: model asks the user to pick options or type an answer
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s

This commit is contained in:
hermes committed 2026-10-02 11:18:22 +00:00
1 parent 90b7aa9654
commit c7341d3085
4 files changed
+411 -2

No files matched your search

+68 -1
View File
@@ -57,7 +57,7 @@ fn tail(s: &str, max: usize) -> &str {
pub fn registry() -> Registry {
let mut r = Registry::default();
r.add(Read).add(Write).add(Edit).add(Bash);
r.add(Read).add(Write).add(Edit).add(Bash).add(AskUser);
r
}
@@ -257,6 +257,50 @@ impl Tool for Edit {
}
}
pub struct AskUser;
#[async_trait]
impl Tool for AskUser {
fn spec(&self) -> ToolSpec {
spec(
"ask_user",
"Ask the user to pick between options (they may also type their own answer). Only when a decision blocks you and cannot be inferred; otherwise pick a default and say so.",
json!({"type":"object","properties":{
"question":{"type":"string"},
"options":{"type":"array","maxItems":6,"items":{"type":"object","properties":{
"label":{"type":"string"},"description":{"type":"string"}},"required":["label"]}},
"multi":{"type":"boolean","description":"allow several options"}},
"required":["question"]}),
)
}
async fn call(&self, a: Value, cx: &ToolCtx) -> ToolResult {
let options = a["options"]
.as_array()
.into_iter()
.flatten()
.filter_map(|o| {
Some((
o["label"].as_str()?.to_string(),
o["description"].as_str().unwrap_or_default().to_string(),
))
})
.take(6)
.collect();
let q = hx_core::Question {
text: a["question"].as_str().unwrap_or_default().into(),
options,
multi: a["multi"].as_bool().unwrap_or(false),
other: true,
};
match cx.state.choose(q).await {
Err(e) => ToolResult::err(e),
Ok(None) => {
ToolResult::ok("user dismissed the question; proceed with your best default")
}
Ok(Some(v)) => ToolResult::ok(v.join(", ")),
}
}
}
pub struct Bash;
#[async_trait]
impl Tool for Bash {
@@ -342,6 +386,29 @@ mod tests {
}
}
#[tokio::test]
async fn ask_user_paths() {
let d = tempfile::tempdir().unwrap();
let c = cx(d.path());
let a =
json!({"question":"db?","options":[{"label":"pg","description":"x"},{"label":"lite"}]});
let r = AskUser.call(a.clone(), &c).await;
assert!(r.is_error && r.content.contains("no interactive user"));
let _ = c.state.choose.set(std::sync::Arc::new(|q| {
Box::pin(async move {
assert!(!q.multi && q.other);
if q.text == "db?" {
Some(vec!["pg".into(), "me".into()])
} else {
None
}
})
}));
assert_eq!(AskUser.call(a, &c).await, ToolResult::ok("pg, me"));
let r = AskUser.call(json!({"question":"other"}), &c).await;
assert!(!r.is_error && r.content.contains("dismissed"));
}
#[tokio::test]
async fn read_ranges_binary_missing() {
let d = tempfile::tempdir().unwrap();