//! How a model id reads on a page. The port of //! `packages/core/src/engine/sessionModel.ts`. //! //! The Stop hook reports the id the tool itself writes — `claude-fable-5-1`, //! `claude-opus-4-1-20250805`, `claude-sonnet-4-5-20250929` — and the row //! stores exactly that, because an id is a fact and a label is a spelling. This //! is the ONE spelling, shared by the list row, the session header and the //! details sheet, so a session cannot read "Fable 5.1" in one place and //! "claude-fable-5-1" in another. use std::sync::LazyLock; use regex::Regex; /// The models the new-session screen offers, id first and the name beside it. /// /// An id here is a FAMILY — what `claude --model ` takes — and the session /// row ends up holding the full id the tool reports, which [`model_label`] /// spells. The two must agree, or a session picked as "Fable" reads as /// something else the moment it registers; `session_model_tests.rs` holds them /// equal rather than trusting two hand-typed spellings. /// /// Fable first: it is the default the picker opens on. pub const OFFERED_MODELS: &[(&str, &str)] = &[ ("fable", "Fable"), ("opus", "Opus"), ("sonnet", "Sonnet"), ]; /// The rule is mechanical, not a lookup table: drop the vendor prefix, drop a /// trailing date stamp, capitalise the family, and read the numbers that follow /// as a version. An id that fits no shape is returned as it came, which is /// honest and still recognisable — never a blank, never "unknown". pub fn model_label(model: Option<&str>) -> Option { static VENDOR: LazyLock = LazyLock::new(|| Regex::new(r"^claude-").expect("a valid pattern")); static DATE_STAMP: LazyLock = LazyLock::new(|| Regex::new(r"-\d{8}$").expect("a valid pattern")); let id = model?.trim_matches(cotton_store::protocol::js::is_js_whitespace); if id.is_empty() { return None; } let stripped = VENDOR.replace(id, ""); let stripped = DATE_STAMP.replace(&stripped, ""); let parts: Vec<&str> = stripped .split('-') .filter(|part| !part.is_empty()) .collect(); let Some((family, rest)) = parts.split_first() else { return Some(id.to_string()); }; // The version is the run of numeric parts after the family; anything after // it (a variant word) is kept as-is, so `haiku-4-5-fast` reads // "Haiku 4.5 fast". let version_end = rest .iter() .position(|part| !part.bytes().all(|b| b.is_ascii_digit())) .unwrap_or(rest.len()); if version_end == 0 { return Some(id.to_string()); } let version = rest[..version_end].join("."); let tail = rest[version_end..].join(" "); let mut chars = family.chars(); let name = match chars.next() { // `charAt(0).toUpperCase() + slice(1)` — a code-unit split in // JavaScript, a char split here. They differ only for a family whose // first character is outside the BMP, which no model id has ever been. Some(first) => format!("{}{}", first.to_uppercase(), chars.as_str()), None => String::new(), }; Some(if tail.is_empty() { format!("{name} {version}") } else { format!("{name} {version} {tail}") }) } #[cfg(test)] #[path = "session_model_tests.rs"] mod tests;