plan mode: show full plan before the exit prompt (claude ExitPlanMode via bridge, ask_user path)
This commit is contained in:
1 parent
1c3b861fde
commit
18eb31ec8b
6 files changed
+79
-7
No files matched your search
@@ -79,7 +79,9 @@ pub async fn serve(path: &Path, cx: ToolCtx) -> std::io::Result<()> {
|
|||||||
let v: Value = serde_json::from_str(&line).unwrap_or_default();
|
let v: Value = serde_json::from_str(&line).unwrap_or_default();
|
||||||
// hx tools run by `codemode` inside mcp-serve ask for their own permission
|
// hx tools run by `codemode` inside mcp-serve ask for their own permission
|
||||||
let mut input = None;
|
let mut input = None;
|
||||||
let ok = if v["tool_name"] == "AskUserQuestion" {
|
let ok = if v["tool_name"] == "ExitPlanMode" {
|
||||||
|
exit_plan(&cx, v["input"]["plan"].as_str().unwrap_or_default()).await
|
||||||
|
} else if v["tool_name"] == "AskUserQuestion" {
|
||||||
// claude's own question tool: show hx's popup, hand the answers back as input
|
// claude's own question tool: show hx's popup, hand the answers back as input
|
||||||
input = ask(&cx, &v["input"]).await;
|
input = ask(&cx, &v["input"]).await;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -121,6 +123,38 @@ pub async fn serve(path: &Path, cx: ToolCtx) -> std::io::Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// claude's `ExitPlanMode`: the TUI shows the whole plan, then asks. Implement ends hx plan mode too.
|
||||||
|
async fn exit_plan(cx: &ToolCtx, plan: &str) -> Result<(), String> {
|
||||||
|
let q = crate::Question {
|
||||||
|
text: "Implement this plan?".into(),
|
||||||
|
options: vec![
|
||||||
|
("Implement".into(), "leave plan mode and start".into()),
|
||||||
|
(
|
||||||
|
"Keep planning".into(),
|
||||||
|
"stay read-only; type feedback below".into(),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
multi: false,
|
||||||
|
other: true,
|
||||||
|
plan: Some(plan.into()),
|
||||||
|
};
|
||||||
|
match cx.state.choose(q).await {
|
||||||
|
Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => {
|
||||||
|
cx.state
|
||||||
|
.allow
|
||||||
|
.lock()
|
||||||
|
.unwrap_or_else(|e| e.into_inner())
|
||||||
|
.set_plan(false);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Ok(Some(v)) if v.first().is_some_and(|s| s != "Keep planning") => Err(format!(
|
||||||
|
"plan not approved, user feedback: {}. Revise and present the plan again.",
|
||||||
|
v.join(", ")
|
||||||
|
)),
|
||||||
|
_ => Err("plan not approved; keep planning and ask what to change".into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Run each `AskUserQuestion` question through the popup; `input` plus `answers` (question -> label(s)).
|
/// Run each `AskUserQuestion` question through the popup; `input` plus `answers` (question -> label(s)).
|
||||||
/// None = no frontend or dismissed.
|
/// None = no frontend or dismissed.
|
||||||
async fn ask(cx: &ToolCtx, input: &Value) -> Option<Value> {
|
async fn ask(cx: &ToolCtx, input: &Value) -> Option<Value> {
|
||||||
@@ -138,6 +172,7 @@ async fn ask(cx: &ToolCtx, input: &Value) -> Option<Value> {
|
|||||||
options: options.collect(),
|
options: options.collect(),
|
||||||
multi: q["multiSelect"].as_bool().unwrap_or(false),
|
multi: q["multiSelect"].as_bool().unwrap_or(false),
|
||||||
other: true,
|
other: true,
|
||||||
|
plan: None,
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.ok()??;
|
.ok()??;
|
||||||
@@ -249,6 +284,24 @@ mod tests {
|
|||||||
assert_eq!(ask(&cx, &inp).await.unwrap()["answers"]["Which?"], "B");
|
assert_eq!(ask(&cx, &inp).await.unwrap()["answers"]["Which?"], "B");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn exit_plan_shows_plan_and_implement_ends_plan_mode() {
|
||||||
|
let cx = ToolCtx {
|
||||||
|
cwd: ".".into(),
|
||||||
|
state: Default::default(),
|
||||||
|
};
|
||||||
|
cx.state.allow.lock().unwrap().set_plan(true);
|
||||||
|
let seen = std::sync::Arc::new(std::sync::Mutex::new(None));
|
||||||
|
let s = seen.clone();
|
||||||
|
let _ = cx.state.choose.set(std::sync::Arc::new(move |q| {
|
||||||
|
*s.lock().unwrap() = q.plan.clone();
|
||||||
|
Box::pin(async { Some(vec!["Keep planning".to_string()]) })
|
||||||
|
}));
|
||||||
|
assert!(exit_plan(&cx, "# P\nstep 1").await.is_err());
|
||||||
|
assert_eq!(seen.lock().unwrap().as_deref(), Some("# P\nstep 1"));
|
||||||
|
assert!(cx.state.allow.lock().unwrap().plan_on());
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn socket_round_trip_allows_and_denies() {
|
async fn socket_round_trip_allows_and_denies() {
|
||||||
use crate::permit::Decision;
|
use crate::permit::Decision;
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ pub struct Question {
|
|||||||
pub multi: bool,
|
pub multi: bool,
|
||||||
/// offer a free-text "Other" row
|
/// offer a free-text "Other" row
|
||||||
pub other: bool,
|
pub other: bool,
|
||||||
|
/// Long markdown (a plan) the frontend shows in full in the transcript before asking.
|
||||||
|
pub plan: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shows a [`Question`]; resolves to the chosen labels / typed text, None when dismissed.
|
/// Shows a [`Question`]; resolves to the chosen labels / typed text, None when dismissed.
|
||||||
|
|||||||
@@ -314,6 +314,7 @@ impl Tool for AskUser {
|
|||||||
options,
|
options,
|
||||||
multi: a["multi"].as_bool().unwrap_or(false),
|
multi: a["multi"].as_bool().unwrap_or(false),
|
||||||
other: true,
|
other: true,
|
||||||
|
plan: None,
|
||||||
};
|
};
|
||||||
match cx.state.choose(q).await {
|
match cx.state.choose(q).await {
|
||||||
Err(e) => ToolResult::err(e),
|
Err(e) => ToolResult::err(e),
|
||||||
@@ -352,6 +353,7 @@ impl Tool for ExitPlan {
|
|||||||
],
|
],
|
||||||
multi: false,
|
multi: false,
|
||||||
other: true,
|
other: true,
|
||||||
|
plan: None,
|
||||||
};
|
};
|
||||||
match cx.state.choose(q).await {
|
match cx.state.choose(q).await {
|
||||||
Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => {
|
Ok(Some(v)) if v.first().is_some_and(|s| s == "Implement") => {
|
||||||
|
|||||||
@@ -286,6 +286,7 @@ mod tests {
|
|||||||
],
|
],
|
||||||
multi,
|
multi,
|
||||||
other: true,
|
other: true,
|
||||||
|
plan: None,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn k(c: KeyCode) -> KeyEvent {
|
fn k(c: KeyCode) -> KeyEvent {
|
||||||
@@ -405,6 +406,7 @@ mod tests {
|
|||||||
],
|
],
|
||||||
multi: false,
|
multi: false,
|
||||||
other: true,
|
other: true,
|
||||||
|
plan: None,
|
||||||
});
|
});
|
||||||
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(200, 50)).unwrap();
|
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(200, 50)).unwrap();
|
||||||
term.draw(|f| c.draw(f, 45)).unwrap();
|
term.draw(|f| c.draw(f, 45)).unwrap();
|
||||||
|
|||||||
@@ -539,7 +539,12 @@ pub async fn run(setup: Setup) -> std::io::Result<()> {
|
|||||||
match u {
|
match u {
|
||||||
Ui::Ask(q, reply) => app.ask = Some((q, reply)),
|
Ui::Ask(q, reply) => app.ask = Some((q, reply)),
|
||||||
Ui::Permit(r, reply) => app.perm = Some((permit_view::Prompt::new(r), reply)),
|
Ui::Permit(r, reply) => app.perm = Some((permit_view::Prompt::new(r), reply)),
|
||||||
Ui::Choose(q, reply) => app.choose = Some((choose::Choose::new(q), reply)),
|
Ui::Choose(q, reply) => {
|
||||||
|
if let Some(p) = &q.plan {
|
||||||
|
app.t.push(Item::Plan(p.clone()));
|
||||||
|
}
|
||||||
|
app.choose = Some((choose::Choose::new(q), reply))
|
||||||
|
}
|
||||||
Ui::Ev(e) => {
|
Ui::Ev(e) => {
|
||||||
if let Event::SubUsage(u) = &e {
|
if let Event::SubUsage(u) = &e {
|
||||||
app.st.usage.add(u);
|
app.st.usage.add(u);
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ pub enum Item {
|
|||||||
started: Instant,
|
started: Instant,
|
||||||
took: Option<Duration>,
|
took: Option<Duration>,
|
||||||
},
|
},
|
||||||
|
/// A finished plan, shown in full.
|
||||||
|
Plan(String),
|
||||||
Info(String),
|
Info(String),
|
||||||
Error(String),
|
Error(String),
|
||||||
/// Hook reports injected at turn end (`[lens] …`), shown as the lens block.
|
/// Hook reports injected at turn end (`[lens] …`), shown as the lens block.
|
||||||
@@ -352,12 +354,9 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
|||||||
Item::Lens(texts) => lens_block(texts, expanded, width, out),
|
Item::Lens(texts) => lens_block(texts, expanded, width, out),
|
||||||
Item::Error(s) => out.push(Line::styled(format!("{} {s}", th.sym.err), th.err())),
|
Item::Error(s) => out.push(Line::styled(format!("{} {s}", th.sym.err), th.err())),
|
||||||
Item::Tool { name, args, .. } if name == "exit_plan" => {
|
Item::Tool { name, args, .. } if name == "exit_plan" => {
|
||||||
let mut md = vec![];
|
plan_block(args["plan"].as_str().unwrap_or_default(), width, out)
|
||||||
markdown(args["plan"].as_str().unwrap_or_default(), &mut md);
|
|
||||||
out.push(Line::styled(th.rule("plan", width), th.accent()));
|
|
||||||
lead(md, " ", th.plain(), out);
|
|
||||||
out.push(Line::styled(th.rule("", width), th.dim()));
|
|
||||||
}
|
}
|
||||||
|
Item::Plan(p) => plan_block(p, width, out),
|
||||||
Item::Tool {
|
Item::Tool {
|
||||||
name,
|
name,
|
||||||
args,
|
args,
|
||||||
@@ -379,6 +378,15 @@ fn render(it: &Item, expanded: bool, width: usize, out: &mut Vec<Line<'static>>)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn plan_block(plan: &str, width: usize, out: &mut Vec<Line<'static>>) {
|
||||||
|
let th = theme::t();
|
||||||
|
let mut md = vec![];
|
||||||
|
markdown(plan, &mut md);
|
||||||
|
out.push(Line::styled(th.rule("plan", width), th.accent()));
|
||||||
|
lead(md, " ", th.plain(), out);
|
||||||
|
out.push(Line::styled(th.rule("", width), th.dim()));
|
||||||
|
}
|
||||||
|
|
||||||
/// `┄ lens ┄┄┄` rule, then per report a summary line (errors red, all-pass green) and,
|
/// `┄ lens ┄┄┄` rule, then per report a summary line (errors red, all-pass green) and,
|
||||||
/// collapsed, the first findings; `ctrl-o` shows all.
|
/// collapsed, the first findings; `ctrl-o` shows all.
|
||||||
fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line<'static>>) {
|
fn lens_block(texts: &[String], expanded: bool, width: usize, out: &mut Vec<Line<'static>>) {
|
||||||
|
|||||||
Reference in new issue
Block a user