use serde::{Deserialize, Serialize};
const STOPPED_BY_OWNER: &str = "\u{23f9}\u{fe0f} Stopped by you";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RunPhase {
Running,
Succeeded,
Failed,
Stopped,
}
impl RunPhase {
pub fn is_terminal(self) -> bool {
self != Self::Running
}
pub fn helper_headline(self, background: bool) -> &'static str {
if background {
return self.headline();
}
match self {
Self::Running => "\u{23f3} A helper is working",
Self::Succeeded => "\u{2705} A helper finished",
Self::Stopped => "\u{26a0}\u{fe0f} A helper didn't finish",
Self::Failed => "\u{274c} A helper failed",
}
}
pub fn headline(self) -> &'static str {
match self {
Self::Running => "\u{23f3} Running in the background",
Self::Succeeded => "\u{2705} Finished in the background",
Self::Stopped => "\u{26a0}\u{fe0f} Didn't finish in the background",
Self::Failed => "\u{274c} Failed in the background",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivityKind {
Read,
Search,
Write,
Run,
Web,
Delegate,
Recall,
Remember,
Remind,
Check,
Change,
}
impl ActivityKind {
pub fn words(self) -> &'static str {
match self {
Self::Read => "Reading files",
Self::Search => "Looking through files",
Self::Write => "Changing files",
Self::Run => "Running a command",
Self::Web => "Looking things up online",
Self::Delegate => "Asking a helper",
Self::Recall => "Checking what I remember",
Self::Remember => "Updating what I remember",
Self::Remind => "Setting a reminder",
Self::Check => "Checking things",
Self::Change => "Making a change",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
Doing(ActivityKind),
HandedOff(ActivityKind),
Output { lines: u64 },
Reporting,
}
impl ActivityKind {
pub fn says_what(self) -> bool {
!matches!(self, Self::Check | Self::Change)
}
}
impl Step {
pub fn is_shown(&self) -> bool {
match self {
Self::Doing(kind) => kind.says_what(),
Self::HandedOff(_) | Self::Output { .. } | Self::Reporting => true,
}
}
pub fn words(&self) -> String {
match self {
Self::Doing(kind) => kind.words().to_string(),
Self::HandedOff(ActivityKind::Delegate) => {
"Handed to a helper; it works in the background".to_string()
}
Self::HandedOff(kind) => format!("{} · goes on in the background", kind.words()),
Self::Output { lines: 0 } => ActivityKind::Run.words().to_string(),
Self::Output { lines: 1 } => {
format!("{} · 1 line of output so far", ActivityKind::Run.words())
}
Self::Output { lines } => {
format!(
"{} · {lines} lines of output so far",
ActivityKind::Run.words()
)
}
Self::Reporting => "Writing up the result".to_string(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HelperView {
pub run: Option<String>,
pub label: String,
pub phase: RunPhase,
pub background: bool,
pub step: Option<Step>,
pub steps: Vec<usize>,
pub reason: Option<String>,
pub stopped_by_owner: bool,
pub took: Option<String>,
pub since_request: Option<SinceRequest>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SinceRequest {
pub secs: u64,
pub words: String,
}
impl HelperView {
pub fn running(label: &str, background: bool, step: Option<Step>) -> Self {
Self {
run: None,
label: label.to_string(),
phase: RunPhase::Running,
background,
step,
steps: Vec::new(),
reason: None,
stopped_by_owner: false,
took: None,
since_request: None,
}
}
pub fn headline(&self) -> &'static str {
if self.phase == RunPhase::Stopped && self.stopped_by_owner {
return STOPPED_BY_OWNER;
}
self.phase.helper_headline(self.background)
}
pub fn of_run(mut self, run: Option<&str>) -> Self {
self.run = run.map(str::to_string);
self
}
pub fn same_helper(&self, other: &HelperView) -> bool {
match (self.run.as_deref(), other.run.as_deref()) {
(Some(mine), Some(theirs)) => mine == theirs,
_ => self.label == other.label,
}
}
pub fn for_steps(mut self, steps: &[usize]) -> Self {
self.steps = steps.to_vec();
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SupersededBy {
Run(usize),
Turn,
}
pub fn superseded_runs(
helpers: &[&HelperView],
plan: Option<&PlanCard>,
) -> Vec<Option<SupersededBy>> {
helpers
.iter()
.enumerate()
.map(|(index, helper)| {
if helper.steps.is_empty()
|| !helper.phase.is_terminal()
|| helper.phase == RunPhase::Succeeded
{
return None;
}
helpers
.iter()
.enumerate()
.skip(index + 1)
.rfind(|(_, later)| helper.steps.iter().all(|step| later.steps.contains(step)))
.map(|(later, _)| SupersededBy::Run(later))
.or_else(|| {
plan.is_some_and(|plan| {
helper
.steps
.iter()
.all(|step| plan.finished_steps.contains(step))
})
.then_some(SupersededBy::Turn)
})
})
.collect()
}
pub fn finished_by_turn_line(earlier: &HelperView, attempt: usize) -> String {
let ended = match earlier.phase {
RunPhase::Failed => "failed",
_ => "didn't finish",
};
format!(
"\u{21aa}\u{fe0f} {}: {} try {ended}; I finished it myself",
earlier.label.trim(),
ordinal(attempt),
)
}
pub fn superseded_line(
earlier: &HelperView,
attempt: usize,
later: &HelperView,
later_attempt: usize,
) -> String {
let ended = match earlier.phase {
RunPhase::Failed => "failed",
_ => "didn't finish",
};
let later_did = match later.phase {
RunPhase::Running => "is on it",
RunPhase::Succeeded => "finished it",
RunPhase::Stopped => "didn't finish it either",
RunPhase::Failed => "failed too",
};
format!(
"\u{21aa}\u{fe0f} {}: {} try {ended}; a {} helper {later_did}",
earlier.label.trim(),
ordinal(attempt),
ordinal(later_attempt),
)
}
fn ordinal(n: usize) -> String {
match n {
1 => "first".to_string(),
2 => "second".to_string(),
3 => "third".to_string(),
4 => "fourth".to_string(),
5 => "fifth".to_string(),
n => format!("{n}th"),
}
}
pub fn run_attempts(helpers: &[&HelperView]) -> Vec<usize> {
helpers
.iter()
.enumerate()
.map(|(index, helper)| {
1 + helpers[..index]
.iter()
.filter(|earlier| earlier.steps.iter().any(|step| helper.steps.contains(step)))
.count()
})
.collect()
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PlanCard {
pub text: String,
pub steps: usize,
pub done: usize,
pub finished_steps: Vec<usize>,
pub settled: bool,
}
impl PlanCard {
pub fn header_with_runs(&self, runs: &[RunPhase]) -> Option<String> {
if self.steps < 2 || runs.is_empty() || !self.settled {
return None;
}
if runs.iter().any(|phase| !phase.is_terminal()) {
return Some(format!(
"\u{23f3} Continuing in the background \u{b7} {} of {} done",
self.done, self.steps
));
}
let worst = runs
.iter()
.copied()
.filter(|phase| matches!(phase, RunPhase::Failed | RunPhase::Stopped))
.max_by_key(|phase| matches!(phase, RunPhase::Failed))?;
(self.done == self.steps).then(|| {
format!(
"{} \u{b7} {} of {} done",
worst.helper_headline(false),
self.done,
self.steps
)
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CardEnd {
Keep,
Collapse,
}
impl CardEnd {
pub fn of(plan: Option<&PlanCard>, helpers: usize) -> Self {
if helpers > 0 || plan.is_some_and(|plan| plan.steps > 1) {
Self::Keep
} else {
Self::Collapse
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum StepEvidence {
#[default]
Settled,
ModelSaid,
HelperNotStarted,
}
impl StepEvidence {
pub fn is_settled(&self) -> bool {
*self == Self::Settled
}
}