use super::build::{Outcome, Preview, Round, Said};
use crate::assets::{CONTEXT, quoted, render};
use crate::conversation::projection::{REDACTED, SEPARATOR, round_line};
use crate::conversation::recall::{View, grouped};
use crate::text::{head, len, rstrip};
impl Preview {
pub fn block(&self, view: View) -> String {
if let Some(fixed) = self.fixed() {
return fixed;
}
let lines: Vec<String> = self
.said
.iter()
.filter_map(|s| self.drawn(s, view))
.collect();
lines.join("\n")
}
pub fn block_within(&self, view: View, ceiling: usize) -> (String, bool) {
if let Some(fixed) = self.fixed() {
return (fixed, false);
}
let (mut block, mut used, mut started) = (String::new(), 0, false);
for drawn in self.said.iter().filter_map(|said| self.drawn(said, view)) {
for line in drawn.split('\n') {
let cost = len(line) + usize::from(started);
if used + cost > ceiling {
if !started {
block.push_str(head(line, ceiling));
}
return (block, true);
}
if started {
block.push('\n');
}
block.push_str(line);
(used, started) = (used + cost, true);
}
}
(block, false)
}
fn fixed(&self) -> Option<String> {
if let Some(fixed) = &self.fixed {
return Some(fixed.clone());
}
self.redacted.then(|| format!("[{}] {REDACTED}", self.turn))
}
fn drawn(&self, said: &Said, view: View) -> Option<String> {
match said {
Said::User { text, .. } => Some(format!("[{}] USER: {text}", self.turn)),
_ if view == View::Prompts => None,
Said::Agent {
text: Some(text), ..
} => Some(format!("AGENT: {text}")),
Said::Agent { line, .. } => Some(line.clone()),
Said::Call(round) => Some(drawn(round, view, true)),
}
}
pub fn line(&self, view: View) -> String {
if let Some(fixed) = self.fixed() {
return fixed;
}
let segments: Vec<String> = self
.said
.iter()
.filter_map(|said| match said {
Said::User { line, .. } => Some(line.clone()),
_ if view == View::Prompts => None,
Said::Agent { line, .. } => Some(line.clone()),
Said::Call(round) => Some(drawn(round, view, false)),
})
.collect();
let line = format!("[{}] {}", self.turn, segments.join(SEPARATOR));
match view {
View::Light => line,
_ => rstrip(&line).to_string(),
}
}
}
fn drawn(round: &Round, view: View, block: bool) -> String {
let sized = |outcome: &Outcome| {
let template = quoted(CONTEXT, "continue_size").expect("context.yaml has the size");
render(&template, &[("bytes", &grouped(outcome.size))])
};
let outcome = round.outcome.as_ref();
match (view, block) {
(View::Calls, _) => {
let args = if block { &round.wide } else { &round.args };
round_line(&round.name, args, outcome.map(sized).as_deref())
}
(View::Full, true) => {
let shown = outcome.map(|outcome| match outcome.wide.as_str() {
"" => sized(outcome),
wide => format!("{wide} {}", sized(outcome)),
});
round_line(&round.name, &round.wide, shown.as_deref())
}
_ => {
let digest = outcome.map(|outcome| outcome.digest.as_str());
round_line(&round.name, &round.args, digest)
}
}
}