i18n_ui 0.2.126

Terminal UI for translation progress with interactive concurrent panels and line-by-line fallback
Documentation
//! Running task state, retry countdown, and completed task snapshot

use std::time::{Duration, Instant};

use crate::event::Name;

/// Thinking tail retention cap (bytes), only for displaying the latest content
pub(crate) const TAIL_CAP: usize = 4096;

/// Live retry state of the current task (cleared automatically when new streaming output arrives)
pub(crate) struct RetryState {
  pub(crate) nth: usize,
  pub(crate) total: usize,
  pub(crate) wait: Duration,
  pub(crate) at: Instant,
  pub(crate) base: String,
  pub(crate) err: String,
  /// Current consecutive retry count (increments when switching multiple tokens in one round)
  pub(crate) streak: usize,
}

impl RetryState {
  /// Remaining cooldown until the next retry
  pub(crate) fn rem(&self, now: Instant) -> Duration {
    self
      .wait
      .saturating_sub(now.saturating_duration_since(self.at))
  }
}

/// A running task
pub(crate) struct Run {
  pub(crate) id: u32,
  pub(crate) name: Name,
  /// Elapsed-time anchor for this task row. Same suspend-excluding clock as the job's
  /// `active` measurement, so a lid close does not inflate the displayed duration.
  pub(crate) begin: Instant,
  /// Last streaming output, drives the glowing stream head
  pub(crate) at: Instant,
  /// Bound pool slot index when actively executing
  pub(crate) slot: Option<usize>,
  /// Tokens consumed by this task across its attempts
  pub(crate) tok: u64,
  /// Thinking: interactive mode keeps the latest tail; non-interactive mode buffers the not-yet-complete-line part
  pub(crate) think: String,
  /// Latest reply tail (interactive mode only)
  pub(crate) reply: String,
  /// Current retry warning state
  pub(crate) retry: Option<RetryState>,
  /// Cumulative retry count for this task
  pub(crate) retry_cnt: usize,
}

impl Run {
  /// Append streaming output to thinking or reply and trim overly old content
  pub(crate) fn push_tail(&mut self, s: &str, reply: bool) {
    // Model streaming output received, clear the retry waiting state
    self.retry = None;
    self.at = Instant::now();
    let buf = if reply {
      &mut self.reply
    } else {
      &mut self.think
    };
    buf.push_str(s);
    if let Some(over) = buf.len().checked_sub(TAIL_CAP) {
      buf.drain(..buf.ceil_char_boundary(over));
    }
  }
}

/// A finished task
pub(crate) struct Ended {
  pub(crate) name: Name,
  /// Pool slot that served the task last, keeps a failed card under its endpoint while it fades
  pub(crate) slot: Option<usize>,
  pub(crate) slot_label: Option<String>,
  pub(crate) tok: u64,
  pub(crate) sec: f32,
  pub(crate) retry_cnt: usize,
  pub(crate) err: Option<String>,
  /// Residual thinking without a complete line: non-interactive mode flushes it at the end, interactive mode ignores it
  pub(crate) think: String,
}