// 計畫 (/plan) in the window: 「/plan …」 typed to this brain (here, on one of its screens, on the // phone), what a turn says about its plan (approved, dropped, sent back, the steps moving on), // the line above the input, and the plan's card in the chat. What a plan is and what the bot is // told: plan.rs; how a turn keeps to it (only looking until it is approved): agent.rs. // // The approval itself is a 問題卡 (開始照計畫做 · 先不要 · their own words), so the window, a // screen of another brain and the phone all answer it the way they answer any question. use super::*; use crate::plan::{self, Command, Outcome, Plan, Stage, Status}; use serde_json::{json, Value}; /// How the plan `call` handed in stands (from its step's result), and the plan being followed /// when it is this one: the newest approved plan, while the bot has a plan to follow. pub(super) fn plan_state<'a>(bot: &'a Bot, call: &crate::model::ToolCall) -> (Outcome, Option<&'a Plan>) { let result = |id: &str| bot.messages.iter().rev().find(|m| m.role == Role::Tool && m.tool_call_id == id).map(|m| m.text.as_str()); let how = plan::outcome(result(&call.id)); let following = bot.plan.as_ref().filter(|pl| pl.stage == Stage::Doing).filter(|_| { how == Outcome::Approved && bot.messages.iter().rev().flat_map(|m| m.tool_calls.iter().rev()).find(|c| c.name == plan::SUBMIT_TOOL && plan::outcome(result(&c.id)) == Outcome::Approved).is_some_and(|c| c.id == call.id) }); (how, following) } /// The plan card's state for the phone (its words for `Outcome`), and the steps as followed now. pub(super) fn plan_card_state(bot: &Bot, call: &crate::model::ToolCall) -> (Value, Value) { let (how, following) = plan_state(bot, call); let state = match how { Outcome::Waiting => "waiting", Outcome::Approved => "approved", Outcome::Changed => "changed", Outcome::NotNow | Outcome::Refused => "not_now", }; let live = following.map(|pl| serde_json::to_value(&pl.steps).unwrap_or(Value::Null)).unwrap_or(Value::Null); (json!(state), live) } impl App { /// 「/plan …」: show, cancel, or start planning. pub(super) fn plan_command(&mut self, ctx: &egui::Context, bot_id: u64, cmd: Command, text: &str, attachments: Vec, picks: Option>) { let Some(b) = self.state.bot(bot_id) else { return }; match cmd { Command::Status => { let s = plan::status(b.plan.as_ref()); self.goal_note(bot_id, &s); } Command::Clear => { let s = match self.state.bot_mut(bot_id).and_then(|b| b.plan.take()) { Some(p) => format!("已取消計畫:{}", p.task), None => "沒有計畫。".into(), }; // A plan waiting on its card: the card goes too (as 先不要: the turn stops // there, nothing done). if self.approvals.get(&bot_id).is_some_and(|a| a.tool == plan::SUBMIT_TOOL) { self.answer(ctx, bot_id, ApprovalAnswer::Reject); } self.goal_note(bot_id, &s); } Command::Start(task) => { if !b.members.is_empty() { self.goal_note(bot_id, "群組聊天還不能用 /plan。"); return; } if task.chars().count() > plan::MAX_TASK_CHARS { self.goal_note(bot_id, &format!("要做的事太長了(最多 {} 字)。", plan::MAX_TASK_CHARS)); return; } // Planning is a turn of its own (its tools are set when it starts). if self.streams.contains_key(&bot_id) { let name = b.name.clone(); self.goal_note(bot_id, &format!("{name} 正在做事:等它做完,或先按停止,再用 /plan。")); return; } let m = person_message(&self.state, bot_id, text.trim(), attachments, picks); if let Some(b) = self.state.bot_mut(bot_id) { b.plan = Some(Plan::new(&task, m.at)); b.messages.push(m); } self.start_reply(ctx, bot_id); } } self.dirty = true; self.save_now = true; } /// What a turn said about its plan. pub(super) fn plan_event(&mut self, bot_id: u64, ev: plan::Event) { let Some(b) = self.state.bot_mut(bot_id) else { return }; match ev { plan::Event::Approved(steps) => { if let Some(p) = b.plan.as_mut() { p.stage = Stage::Doing; p.steps = steps; } } plan::Event::NotNow => b.plan = None, plan::Event::Changed => { if let Some(p) = b.plan.as_mut() { p.revisions += 1; } } plan::Event::Progress(steps) => { if let Some(p) = b.plan.as_mut().filter(|p| p.stage == Stage::Doing) { p.steps = steps; } } } self.dirty = true; self.save_now = true; } /// A turn ended: a plan with every step done is over (a line says so). pub(super) fn plan_settled(&mut self, bot_id: u64) { let Some(p) = self.state.bot(bot_id).and_then(|b| b.plan.clone()).filter(Plan::finished) else { return }; if let Some(b) = self.state.bot_mut(bot_id) { b.plan = None; } self.goal_note(bot_id, &plan::finished_note(&p)); self.save_now = true; } /// 取消計畫 (the plan's icon above the input): on a screen the brain cancels it, as if typed there. pub(super) fn plan_clear(&mut self, ctx: &egui::Context, bot_id: u64) { if self.mir.on { self.mirror_send(bot_id, "/plan clear".into(), vec![], None); return; } self.plan_command(ctx, bot_id, Command::Clear, "/plan clear", vec![], None); } /// The plan a bot handed in (`submit_plan`), as a card in its reply: waiting on the person, /// followed (its steps tick as the bot goes: the newest approved plan, while it lasts), /// changed, or not taken. One sent back or not taken shows only its summary. pub(super) fn plan_card(&self, ui: &mut egui::Ui, bot: &Bot, call: &crate::model::ToolCall) { let Ok(draft) = plan::parse_draft(&call.args) else { return }; let (how, following) = plan_state(bot, call); if how == Outcome::Refused { return; } let p = self.pal; let label = match how { Outcome::Waiting => t("等你決定"), Outcome::Approved => match following { Some(pl) => format!("{} · {}/{}", t("照著做"), pl.done(), pl.steps.len()), None => t("已核准"), }, Outcome::Changed => t("已修改,見下面新版"), Outcome::NotNow | Outcome::Refused => t("沒有採用"), }; let whole = matches!(how, Outcome::Waiting | Outcome::Approved); ui.add_space(8.0); Frame::new() .fill(p.elevated) .stroke(Stroke::new(1.0, if how == Outcome::Waiting { p.accent } else { p.border })) .corner_radius(R_L) .inner_margin(Margin::symmetric(14, 12)) .show(ui, |ui| { ui.set_max_width(message_max_width(ui.available_width() + 32.0)); ui.horizontal(|ui| { ui.spacing_mut().item_spacing.x = 6.0; let (r, _) = ui.allocate_exact_size(vec2(16.0, 16.0), egui::Sense::hover()); crate::icons::paint(ui.painter(), r, crate::icons::Icon::Summary, p.text2); ui.label(RichText::new(t("計畫")).strong().family(crate::bold()).size(FONT_BASE)); ui.with_layout(Layout::right_to_left(Align::Center), |ui| { ui.label(RichText::new(label).size(FONT_XS).color(if how == Outcome::Waiting { p.accent } else { p.text3 })); }); }); ui.add_space(6.0); ui.add(egui::Label::new(RichText::new(&draft.summary).size(FONT_SM).color(if whole { p.text } else { p.text2 })).wrap()); if !whole { return; } ui.add_space(8.0); // The steps: as the bot keeps them while it follows the plan, else as handed in. let rows: Vec<(String, String, Option)> = match following { Some(pl) => pl.steps.iter().map(|s| (s.title.clone(), draft.steps.iter().find(|d| d.title == s.title).map(|d| d.detail.clone()).unwrap_or_default(), Some(s.status))).collect(), None => draft.steps.iter().map(|s| (s.title.clone(), s.detail.clone(), None)).collect(), }; for (i, (title, detail, status)) in rows.iter().enumerate() { ui.horizontal_top(|ui| { ui.spacing_mut().item_spacing.x = 8.0; let (mark, col) = match status { Some(Status::Done) => ("✓".to_string(), p.up), Some(Status::InProgress) => ("●".to_string(), p.accent), Some(Status::Skipped) => ("–".to_string(), p.text3), _ => (format!("{}.", i + 1), p.text3), }; ui.add_sized(vec2(18.0, 18.0), egui::Label::new(RichText::new(mark).size(FONT_SM).color(col))); ui.vertical(|ui| { ui.spacing_mut().item_spacing.y = 1.0; let done = matches!(status, Some(Status::Done | Status::Skipped)); let mut words = RichText::new(title).size(FONT_SM).color(if done { p.text3 } else { p.text }); if *status == Some(Status::Skipped) { words = words.strikethrough(); } ui.add(egui::Label::new(words).wrap()); if !detail.is_empty() && !done { ui.add(egui::Label::new(RichText::new(detail).size(FONT_XS).color(p.text3)).wrap()); } }); }); } for (head, body) in [("怎麼確認做對了:", &draft.check), ("注意:", &draft.notes)] { if !body.is_empty() { ui.add_space(8.0); ui.add(egui::Label::new(RichText::new(format!("{}{body}", t(head))).size(FONT_XS).color(p.text2)).wrap()); } } if how == Outcome::Waiting { ui.add_space(8.0); ui.label(RichText::new(t("在下面的卡片選「開始照計畫做」,或寫下要改哪裡")).size(FONT_XS).color(p.text3)); } }); } }