use super::{Kind, Row, trail};
use crate::settings::{Settings, Toggle};
use crate::ui::theme::{self, Bubble, Speaker, glyph, space};
use trail::is_call;
pub const UNFOLD_HELP: &str = "opens what the turn did — every step it hid: the calls, their results, the thoughts and the prose between";
pub const FOLD_HELP: &str = "folds the working back to its one line";
pub fn counted(steps: &[Row]) -> String {
["tool call", "thought", "intermediate response", "note"]
.into_iter()
.enumerate()
.map(|(kind, noun)| (steps.iter().filter(|row| sort(row) == kind).count(), noun))
.filter(|(count, _)| *count > 0)
.map(|(count, noun)| crate::render::counted(count as u64, noun))
.collect::<Vec<_>>()
.join(&format!(" {} ", glyph::NOTE))
}
fn sort(row: &Row) -> usize {
if is_call(row) {
0
} else if row.kind == Kind::Thinking {
1
} else if is_response(row) {
2
} else {
3
}
}
fn is_response(row: &Row) -> bool {
row.kind == Kind::Said && row.weight == Speaker::Model
}
fn in_flight(row: &Row, last: bool) -> bool {
row.kind == Kind::Call
|| (row.live && (is_response(row) || (last && row.kind == Kind::Thinking)))
}
pub(super) fn working(ui: &mut egui::Ui, at: usize, steps: &[Row], settings: &Settings) -> bool {
let id = ui.make_persistent_id(("working", at));
let live = steps.iter().any(|row| row.live) || !settings.on(Toggle::Fold);
let mut open = ui.data(|d| d.get_temp::<bool>(id)).unwrap_or(live);
let door = theme::paint::Row {
mark: (
if open { glyph::OPEN } else { glyph::SHUT }.to_owned(),
theme::INK_WEAK,
),
words: counted(steps),
ink: theme::INK_WEAK,
state: None,
chosen: false,
indent: 0.0,
aside: None,
beneath: Vec::new(),
tail: None,
door: None,
help: if open { FOLD_HELP } else { UNFOLD_HELP }.to_owned(),
};
if door.paint(ui).clicked() {
open = !open;
ui.data_mut(|d| d.insert_temp(id, open));
}
if !open {
return false;
}
let bubbles = settings.on(Toggle::Bubbles);
for (i, row) in steps.iter().enumerate() {
ui.add_space(space::XXS);
let last = i + 1 == steps.len();
theme::paint::nested(ui, bubbles.then_some(Bubble::Step), 0.0, |ui| {
if in_flight(row, last) {
trail::step(ui, at + i, row, false, bubbles);
} else {
finished(ui, at + i, row, settings);
}
});
}
true
}
fn finished(ui: &mut egui::Ui, at: usize, row: &Row, settings: &Settings) {
let id = ui.make_persistent_id(("step", at));
let open = ui
.data(|d| d.get_temp::<bool>(id))
.unwrap_or(!settings.on(Toggle::Fold));
let toggled = if open {
trail::step(ui, at, row, true, settings.on(Toggle::Bubbles))
} else {
trail::line(row, false).paint(ui).clicked()
};
if toggled {
ui.data_mut(|d| d.insert_temp(id, !open));
}
}