use std::fmt;
use std::str::FromStr;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum ResponseStyle {
Concise,
#[default]
Balanced,
Detailed,
}
impl ResponseStyle {
pub const ALL: [Self; 3] = [Self::Concise, Self::Balanced, Self::Detailed];
pub const fn as_str(self) -> &'static str {
match self {
Self::Concise => "concise",
Self::Balanced => "balanced",
Self::Detailed => "detailed",
}
}
pub const fn name(self) -> &'static str {
match self {
Self::Concise => "Concise",
Self::Balanced => "Balanced",
Self::Detailed => "Detailed",
}
}
pub const fn description(self) -> &'static str {
match self {
Self::Concise => "Compact answers with essential results, caveats, and verification.",
Self::Balanced => "Enough context to understand and verify without exhaustive detail.",
Self::Detailed => "Thorough decisions, trade-offs, effects, and verification.",
}
}
pub(crate) const fn prompt_instruction(self) -> &'static str {
match self {
Self::Concise => {
"Lead with the outcome. Keep the final answer compact, omit routine narration, and \
retain essential caveats and verification."
}
Self::Balanced => {
"Lead with the outcome. Provide enough context to understand decisions and verify \
the result without exhaustive detail."
}
Self::Detailed => {
"Lead with the outcome. Explain decisions, trade-offs, affected behavior, and \
verification thoroughly without repetition."
}
}
}
}
impl fmt::Display for ResponseStyle {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for ResponseStyle {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"concise" => Ok(Self::Concise),
"balanced" => Ok(Self::Balanced),
"detailed" => Ok(Self::Detailed),
_ => Err(format!("Unknown response style: {value}")),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum SpeedMode {
#[default]
Normal,
Fast,
}
impl SpeedMode {
pub const ALL: [Self; 2] = [Self::Normal, Self::Fast];
pub const fn as_str(self) -> &'static str {
match self {
Self::Normal => "normal",
Self::Fast => "fast",
}
}
pub const fn name(self) -> &'static str {
match self {
Self::Normal => "Normal",
Self::Fast => "Fast",
}
}
pub const fn description(self) -> &'static str {
match self {
Self::Normal => "Standard response speed and provider pricing.",
Self::Fast => "Faster responses at a higher provider cost.",
}
}
pub const fn codex_service_tier(self) -> &'static str {
match self {
Self::Normal => "default",
Self::Fast => "fast",
}
}
pub const fn claude_fast_mode(self) -> bool {
matches!(self, Self::Fast)
}
}
impl fmt::Display for SpeedMode {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl FromStr for SpeedMode {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"normal" => Ok(Self::Normal),
"fast" => Ok(Self::Fast),
_ => Err(format!("Unknown speed mode: {value}")),
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum SessionDiffState {
#[default]
Unknown,
Empty,
Present,
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct SessionStats {
pub added_lines: u64,
pub deleted_lines: u64,
pub diff_state: SessionDiffState,
pub input_tokens: u64,
pub output_tokens: u64,
}
impl SessionStats {
pub fn should_show_diff(&self) -> bool {
self.diff_state != SessionDiffState::Empty
}
pub fn line_change_counts(diff: &str) -> (u64, u64) {
diff.lines()
.fold((0_u64, 0_u64), |(added_lines, deleted_lines), line| {
if line.starts_with('+') && !line.starts_with("+++") {
return (added_lines.saturating_add(1), deleted_lines);
}
if line.starts_with('-') && !line.starts_with("---") {
return (added_lines, deleted_lines.saturating_add(1));
}
(added_lines, deleted_lines)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn response_style_round_trips_persisted_values() {
for response_style in ResponseStyle::ALL {
assert_eq!(
response_style.as_str().parse::<ResponseStyle>(),
Ok(response_style)
);
assert_eq!(response_style.to_string(), response_style.as_str());
}
}
#[test]
fn response_style_rejects_unknown_persisted_value() {
let result = "verbose".parse::<ResponseStyle>();
assert_eq!(result, Err("Unknown response style: verbose".to_string()));
}
#[test]
fn response_style_exposes_stable_labels_and_descriptions() {
let values = ResponseStyle::ALL
.map(|response_style| (response_style.name(), response_style.description()));
assert_eq!(
values,
[
(
"Concise",
"Compact answers with essential results, caveats, and verification."
),
(
"Balanced",
"Enough context to understand and verify without exhaustive detail."
),
(
"Detailed",
"Thorough decisions, trade-offs, effects, and verification."
),
]
);
}
#[test]
fn speed_mode_round_trips_persisted_values() {
for speed_mode in SpeedMode::ALL {
assert_eq!(speed_mode.as_str().parse::<SpeedMode>(), Ok(speed_mode));
assert_eq!(speed_mode.to_string(), speed_mode.as_str());
}
}
#[test]
fn speed_mode_maps_provider_settings() {
assert_eq!(SpeedMode::Normal.codex_service_tier(), "default");
assert!(!SpeedMode::Normal.claude_fast_mode());
assert_eq!(SpeedMode::Fast.codex_service_tier(), "fast");
assert!(SpeedMode::Fast.claude_fast_mode());
}
#[test]
fn speed_mode_rejects_unknown_persisted_value() {
let result = "turbo".parse::<SpeedMode>();
assert_eq!(result, Err("Unknown speed mode: turbo".to_string()));
}
#[test]
fn should_show_diff_hides_only_known_empty_diffs() {
let unknown = SessionStats::default();
let empty = SessionStats {
diff_state: SessionDiffState::Empty,
..SessionStats::default()
};
let present = SessionStats {
diff_state: SessionDiffState::Present,
..SessionStats::default()
};
assert!(unknown.should_show_diff());
assert!(!empty.should_show_diff());
assert!(present.should_show_diff());
}
}