AnyBot/src/app/talk_ui.rs

97 lines
5.9 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 同事之間的對話卡: in the chat that asked, a question put to a colleague (ask_bot) or work
// handed to it (message_bot) shows as a small conversation — who asked whom, what, and once it
// is back, the answer — so the person sees them talking and reads the result right here. (The
// colleague's own chat keeps the question and its whole answer too: that is the record.)
use super::*;
/// Lines of an answer shown before 「展開」.
const ANSWER_LINES: usize = 8;
impl App {
/// The card for one ask_bot / message_bot step of the reply `m` in `bot`'s chat.
pub(super) fn talk_card(&mut self, ui: &mut egui::Ui, ctx: &egui::Context, bot: &Bot, m: &Message, c: &crate::model::ToolCall) {
let p = self.pal;
let v = tools::args_of(&c.args);
let asked = c.name == "ask_bot";
let to_name = v["bot"].as_str().unwrap_or("").trim().to_string();
let to = appctl::find_bot(&self.state, &to_name).cloned();
let from = if m.author != 0 { self.state.bot(m.author).cloned() } else { Some(bot.clone()) };
let question = v[if asked { "question" } else { "text" }].as_str().unwrap_or("").trim().to_string();
let result = bot.messages.iter().find(|r| r.role == Role::Tool && r.tool_call_id == c.id).map(|r| r.text.trim().to_string());
let open_id = egui::Id::unique(("talk-open", bot.id, &c.id));
let mut open = ctx.data(|d| d.get_temp::<bool>(open_id)).unwrap_or(false);
let mut go_to: Option<u64> = None;
ui.add_space(6.0);
Frame::new().fill(p.hover).stroke(Stroke::new(1.0, p.border)).corner_radius(14).inner_margin(Margin::symmetric(12, 10)).show(ui, |ui| {
ui.set_max_width(message_max_width(ui.available_width()));
// Who talks to whom.
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 6.0;
for (i, who) in [from.as_ref(), to.as_ref()].into_iter().enumerate() {
if i == 1 {
crate::icons::label(ui, crate::icons::Icon::ChevronRight, "", 12.0, p.text3);
}
if let Some(b) = who {
let (r, _) = ui.allocate_exact_size(vec2(18.0, 18.0), egui::Sense::hover());
avatar::paint(ui.painter(), r, &b.shape, self.bot_color(b));
ui.label(RichText::new(&b.name).size(FONT_SM).strong().family(crate::bold()).color(p.text));
} else {
ui.label(RichText::new(if i == 1 { to_name.as_str() } else { "" }).size(FONT_SM).color(p.text));
}
}
ui.label(RichText::new(if asked { t("問") } else { t("交辦") }).size(11.5).color(p.text3));
});
if !question.is_empty() {
ui.add_space(4.0);
let q: String = question.chars().take(400).collect();
ui.label(RichText::new(if question.chars().count() > 400 { format!("{q}…") } else { q }).size(FONT_SM).color(p.text2));
}
ui.add_space(6.0);
let them = to.as_ref().map(|b| b.name.clone()).unwrap_or_else(|| to_name.clone());
match (c.status, &result) {
(ToolStatus::Running | ToolStatus::Waiting, _) => {
ui.horizontal(|ui| {
ui.add(egui::Spinner::new().size(12.0));
let doing = to.as_ref().and_then(|b| self.streams.contains_key(&b.id).then(|| self.working_phrase(ctx.input(|i| i.time), b)).flatten());
ui.label(RichText::new(format!("「{them}」{}", doing.map(|d| format!(":{d}")).unwrap_or_else(|| "正在處理…".into()))).size(12.0).color(p.text2));
});
ctx.request_repaint_after(std::time::Duration::from_millis(500));
}
(ToolStatus::Done, Some(r)) if asked => {
// The answer, its words only (the result's first line says whose).
let body = r.split_once('\n').map(|(_, rest)| rest.trim()).unwrap_or(r.as_str());
let lines: Vec<&str> = body.lines().collect();
let long = lines.len() > ANSWER_LINES || body.chars().count() > 900;
let shown = if long && !open { format!("{}…", lines.iter().take(ANSWER_LINES).copied().collect::<Vec<_>>().join("\n").chars().take(900).collect::<String>()) } else { body.to_string() };
ui.horizontal(|ui| {
crate::icons::label(ui, crate::icons::Icon::CircleCheck, &format!("「{them}」的回答"), 12.0, Color32::from_rgb(0x1f, 0xb3, 0x5d));
});
ui.add_space(2.0);
ui.with_layout(Layout::top_down(Align::Min), |ui| self.markdown(ui, &shown, p.text, false));
if long && ui.add(egui::Button::new(RichText::new(if open { t("收合") } else { t("展開全部") }).size(12.0).color(p.accent)).frame(false)).clicked() {
open = !open;
}
}
(ToolStatus::Done, Some(r)) => {
ui.label(RichText::new(r).size(12.0).color(p.text3));
}
(_, r) => {
ui.label(RichText::new(r.clone().unwrap_or_else(|| "沒有完成".into())).size(12.0).color(p.danger));
}
}
// The record: the colleague's own chat has the question and its whole answer.
if let Some(b) = &to {
ui.add_space(2.0);
if ui.add(egui::Button::new(RichText::new(format!("「{}」那邊的紀錄 ›", b.name)).size(11.5).color(p.text3)).frame(false)).on_hover_text(t("打開它的聊天,看完整的過程")).clicked() {
go_to = Some(b.id);
}
}
});
ctx.data_mut(|d| d.insert_temp(open_id, open));
if let Some(id) = go_to {
self.select_bot(ctx, id);
}
}
}