diff --git a/crates/ui-egui/src/agent_chat/mod.rs b/crates/ui-egui/src/agent_chat/mod.rs index 24966a753..8ec1f9002 100644 --- a/crates/ui-egui/src/agent_chat/mod.rs +++ b/crates/ui-egui/src/agent_chat/mod.rs @@ -427,6 +427,9 @@ pub struct AgentChat { running_endpoint: String, backend: Option>, turn: Option, + /// The design as the agent last saw it: at the end of its last turn that it answered or worked + /// in. The next message tells it when the design is no longer that one (#10854). + seen: Option>, tools_available: Option, /// The latest completed turn's notice; older notices remain in the transcript. turn_warning: Option, @@ -609,6 +612,8 @@ impl AgentChat { fn stop_backend(&mut self, wait: bool) { self.steering = false; + // The next agent starts without this one's memory of the design. + self.seen = None; if let Some(mut b) = self.backend.take() { b.cancel(); b.shutdown(wait); @@ -1207,6 +1212,71 @@ const MAX_PENDING: usize = 20; /// Said to the agent before a message that interrupted it, so it knows its last step may be /// incomplete. const INTERRUPT_NOTE: &str = "[The user interrupted your previous step.]"; +/// Most names one list of [`changed_note`] carries; the rest are counted. +const CHANGED_ITEMS: usize = 12; + +/// Said to the agent before a message when the design is not the one its last turn left: the user +/// changed it by hand in between (#10854). Names the parameters and features that changed and +/// where the History marker went, so the agent reads the design again instead of reusing +/// positions and picks from before. +fn changed_note(seen: &solidcraft_engine::doc::Document, now: &solidcraft_engine::doc::Document) -> Option { + use solidcraft_engine::doc::Feature; + use std::collections::HashMap; + if seen == now { + return None; + } + let clip = |s: &str| s.chars().take(60).collect::(); + let list = |items: Vec| { + let more = items.len().saturating_sub(CHANGED_ITEMS); + let mut text = items.into_iter().take(CHANGED_ITEMS).collect::>().join(", "); + if more > 0 { + text.push_str(&format!(" and {more} more")); + } + text + }; + let mut parts = Vec::new(); + let before: HashMap<&str, &str> = seen.params.iter().map(|p| (p.name.as_str(), p.expr.as_str())).collect(); + let after: HashMap<&str, &str> = now.params.iter().map(|p| (p.name.as_str(), p.expr.as_str())).collect(); + let params: Vec = now + .params + .iter() + .filter_map(|p| match before.get(p.name.as_str()) { + Some(old) if *old == p.expr => None, + Some(old) => Some(format!("{} {} -> {}", clip(&p.name), clip(old), clip(&p.expr))), + None => Some(format!("{} = {} (new)", clip(&p.name), clip(&p.expr))), + }) + .chain(seen.params.iter().filter(|p| !after.contains_key(p.name.as_str())).map(|p| format!("{} (removed)", clip(&p.name)))) + .collect(); + if !params.is_empty() { + parts.push(format!("parameters: {}", list(params))); + } + let name = |f: &Feature| if f.name.is_empty() { format!("#{}", f.id) } else { clip(&f.name) }; + let before: HashMap = seen.features.iter().map(|f| (f.id, f)).collect(); + let after: HashMap = now.features.iter().map(|f| (f.id, f)).collect(); + let features: Vec = now + .features + .iter() + .filter_map(|f| match before.get(&f.id) { + Some(old) if *old == f => None, + Some(_) => Some(format!("{} (edited)", name(f))), + None => Some(format!("{} (new)", name(f))), + }) + .chain(seen.features.iter().filter(|f| !after.contains_key(&f.id)).map(|f| format!("{} (removed)", name(f)))) + .collect(); + if !features.is_empty() { + parts.push(format!("features: {}", list(features))); + } + if seen.marker != now.marker { + parts.push(match now.marker.and_then(|i| now.features.get(i)) { + Some(f) => format!("the History marker is now before {}, so it and the features after it are rolled back", name(f)), + None => "the History marker is now at the end".into(), + }); + } + let what = if parts.is_empty() { String::new() } else { format!(" ({})", parts.join("; ")) }; + Some(format!( + "[The user changed the design by hand since your last reply{what}. Read it again before you reuse positions, edge or face picks or sizes from earlier in this chat.]" + )) +} /// How many things the selection is, counting what was clicked (the two halves of a rim are one). pub fn selected_count(app: &mut SolveApp) -> usize { @@ -1412,6 +1482,7 @@ pub fn new_chat(app: &mut SolveApp) { chat.steering = false; chat.stopped = false; chat.ended = None; + chat.seen = None; chat.discard_end = running; chat.model_revert = None; if matches!(chat.state(), State::Ready | State::Busy) { @@ -1457,15 +1528,15 @@ fn start_turn(app: &mut SolveApp, text: String, image: bool, at: Option, start_turn_with(app, text, image, at, files, None); } -/// Start a message; `note` goes before the text for the agent, not into the conversation. The +/// Start a message; `note` goes before the text for the agent, not into the conversation, and so +/// does what changed in the design since the agent's last turn ([`changed_note`]). The /// files are named after the text (text files in full), and pictures go as images to an agent /// that takes them. `at` is the message's place in the conversation, where its picture is kept. fn start_turn_with(app: &mut SolveApp, text: String, image: bool, at: Option, files: Vec, note: Option<&str>) { let (pictures, documents) = (app.agent_chat.images, app.agent_chat.documents); - let lead = match note { - Some(n) => format!("{n}\n\n{text}"), - None => text.clone(), - }; + let changed = app.agent_chat.seen.as_deref().and_then(|seen| changed_note(seen, &app.session.doc)); + let notes: Vec<&str> = note.into_iter().chain(changed.as_deref()).collect(); + let lead = if notes.is_empty() { text.clone() } else { format!("{}\n\n{text}", notes.join("\n")) }; // The whole message stays within the cap: the files' text gets what the words leave. let budget = MAX_PROMPT.saturating_sub(lead.chars().count() + FILE_NAMES_ROOM); let about_files = attach::note(&files, pictures, documents, budget); @@ -1739,6 +1810,10 @@ fn finish_turn(chat: &mut AgentChat, session: &mut solidcraft_engine::Session) { let Some(turn) = chat.turn.take() else { return }; turn.active.store(false, std::sync::atomic::Ordering::Relaxed); let steps = merge_undo(&mut session.undo, &turn.start_doc, turn.start_undo, &trf("ui.agent_chat_undo_label", &[("message", &turn.label)])); + // A turn the agent never answered (a sign-in, an error before its reply) showed it nothing. + if turn.replied || turn.tool_called || turn.requests > 0 { + chat.seen = Some(session.doc.clone()); + } if chat.ended.as_deref() == Some("") && !chat.stopped && turn.tools_available diff --git a/crates/ui-egui/src/agent_chat/tests.rs b/crates/ui-egui/src/agent_chat/tests.rs index 98f4c18f4..b5d362916 100644 --- a/crates/ui-egui/src/agent_chat/tests.rs +++ b/crates/ui-egui/src/agent_chat/tests.rs @@ -1386,6 +1386,77 @@ fn an_interrupted_turns_changes_undo_as_one_step() { assert!(Arc::ptr_eq(&app.session.doc, &before)); } +/// What the user changes by hand between two messages is told to the agent before the next one, +/// with the parameters and features it touched (#10854): the agent built a hole on a parameter, +/// the user changed it, and the agent picked the old rim. Its own changes, and a design it has not +/// seen yet (a new chat), bring no note. +#[test] +fn a_change_by_hand_between_messages_is_told_to_the_agent() { + use solidcraft_engine::doc::Param; + let (mut app, script, ctx) = app_with_script(); + let (tx, rx) = channel(); + app.agent_chat.tools_rx = Some(rx); + app.agent_chat.attach_selection = false; + let expr = |app: &SolveApp| app.session.doc.params.iter().find(|p| p.name == "w").map(|p| p.expr.clone()).unwrap(); + + send(&mut app, &ctx, "a box and a parameter w".into()); + let replies = [ + ask(&tx, "engine.execute", json!({"command": "parameters.add", "params": {"name": "w", "expression": "10", "unit": "mm"}})), + ask(&tx, "engine.execute", boxed(0.0)), + ]; + frame(&mut app); + for r in replies { + assert_eq!(r.recv_timeout(std::time::Duration::from_secs(30)).unwrap()["ok"], true); + } + ended(&script, ""); + poll(&mut app, &ctx); + send(&mut app, &ctx, "thanks".into()); + assert_eq!(script.calls().last().map(String::as_str), Some("prompt: thanks"), "the agent made those changes itself"); + ended(&script, ""); + poll(&mut app, &ctx); + + let old = expr(&app); + app.run("parameters.change", json!({"name": "w", "expression": "25"})).unwrap(); + app.run("solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [30, 0, 0]})).unwrap(); + let added = app.session.doc.features.last().map(|f| f.name.clone()).unwrap(); + send(&mut app, &ctx, "chamfer it".into()); + let sent = text_of(script.prompts().last().unwrap()); + // The new box may bring parameters of its own after `w`. + let new = expr(&app); + assert!(sent.starts_with(&format!("[The user changed the design by hand since your last reply (parameters: w {old} -> {new}")), "{sent}"); + assert!(sent.contains(&format!("; features: {added} (new)). Read it again before")), "{sent}"); + assert!(sent.ends_with("chat.]\n\nchamfer it"), "{sent}"); + assert!( + matches!(app.agent_chat.items.iter().rev().find(|i| matches!(i, Item::User { .. })), Some(Item::User { text, .. }) if text == "chamfer it") + ); + script.report(Event::Text { thought: false, text: "done".into() }); + ended(&script, ""); + poll(&mut app, &ctx); + send(&mut app, &ctx, "and again".into()); + assert_eq!(script.calls().last().map(String::as_str), Some("prompt: and again"), "told once"); + ended(&script, ""); + poll(&mut app, &ctx); + + // The note's other parts, and its bound on what it lists. + let seen = app.session.doc.as_ref().clone(); + assert!(changed_note(&seen, &seen.clone()).is_none()); + let mut rolled = seen.clone(); + rolled.marker = Some(0); + let first = seen.features.first().map(|f| f.name.clone()).unwrap(); + assert!(changed_note(&seen, &rolled).unwrap().contains(&format!("(the History marker is now before {first}, so it"))); + let mut many = seen.clone(); + let w = seen.params.iter().find(|p| p.name == "w").cloned().unwrap(); + many.params.extend((0..20).map(|i| Param { name: format!("p{i}"), ..w.clone() })); + assert!(changed_note(&seen, &many).unwrap().contains(&format!(", p11 = {} (new) and 8 more)", w.expr))); + + // A new chat starts a session that has seen nothing. + app.run("parameters.change", json!({"name": "w", "expression": "30"})).unwrap(); + new_chat(&mut app); + app.agent_chat.state = Some(State::Ready); + send(&mut app, &ctx, "hello".into()); + assert_eq!(script.calls().last().map(String::as_str), Some("prompt: hello")); +} + /// The same with a real agent process speaking ACP: `session/cancel` goes out, the agent answers /// the held prompt with the cancelled stop reason, and only then does the queued text go as a new /// `session/prompt`. diff --git a/docs/agent-chat.md b/docs/agent-chat.md index 7cab2d096..207d9d7e2 100644 --- a/docs/agent-chat.md +++ b/docs/agent-chat.md @@ -185,6 +185,13 @@ messages go as one prompt, in order, when the turn ends normally. fresh session on the same agent, keeping the agent and its mode. It asks first when a message is running or queued, or files are waiting. The design's undo history is untouched. +When you change the design yourself between two messages (a parameter, a feature, an undo, the +History marker), your next message tells the agent so first, in a short note the conversation +doesn't show: which parameters changed and from what to what, which features are new, edited or +removed, and where the History marker went. The note asks the agent to read the design again +instead of picking edges and faces where they were before. A new chat or a new agent session has +seen nothing yet, so its first message carries no note. + ## Agents Preferences → AI → **Agents** lists every agent the chat can use, each with its kind (a program or