返回 CodeWhale
model_reference.rs
根目录 / crates / tui / src / commands / contract / model_reference.rs
1 use crate::tui::pager::PagerView;
2 use codewhale_config::model_reference::ModelReferenceDatabase;
3 use ratatui::style::{Modifier, Style};
4 use ratatui::text::Line;
5
6 pub(super) fn view() -> PagerView {
7 let db = ModelReferenceDatabase::bundled();
8 let title = format!(
9 "Model Reference — {} offerings · {} providers",
10 db.len(),
11 db.providers().len()
12 );
13 PagerView::new(title, reference_lines(&db))
14 }
15
16 /// Render the reference database as aligned, browsable pager lines.
17 ///
18 /// Cards are grouped under a provider/kind header (the database is already
19 /// sorted by `(provider, model id)`), so each row only needs the model-scoped
20 /// columns. Column widths are computed across the whole table for stable
21 /// alignment.
22 fn reference_lines(db: &ModelReferenceDatabase) -> Vec<Line<'static>> {
23 let bold = Style::default().add_modifier(Modifier::BOLD);
24 let dim = Style::default().add_modifier(Modifier::DIM);
25
26 let mut lines: Vec<Line<'static>> = Vec::new();
27 lines.push(Line::styled(
28 "Bundled curated model reference catalog.".to_string(),
29 bold,
30 ));
31 lines.push(Line::styled(
32 "Attributes are stated facts; \"unknown\" means the catalog did not state it (never guessed)."
33 .to_string(),
34 dim,
35 ));
36 lines.push(Line::from(String::new()));
37
38 if db.is_empty() {
39 lines.push(Line::from("(no models in catalog)".to_string()));
40 return lines;
41 }
42
43 let cards = db.cards();
44 let id_w = cards
45 .iter()
46 .map(|card| card.model_id.chars().count())
47 .chain(std::iter::once("MODEL ID".len()))
48 .max()
49 .unwrap_or(8)
50 .clamp(8, 46);
51 let ctx_w = cards
52 .iter()
53 .map(|card| card.context_window_label().chars().count())
54 .chain(std::iter::once("CTX".len()))
55 .max()
56 .unwrap_or(3);
57 let out_w = cards
58 .iter()
59 .map(|card| card.max_output_label().chars().count())
60 .chain(std::iter::once("MAX OUT".len()))
61 .max()
62 .unwrap_or(7);
63 // "multimodal" (10) is the widest possible label and exceeds "MODALITY".
64 let mod_w = "multimodal".len();
65
66 lines.push(Line::styled(
67 format!(
68 " {} {} {} {} {}",
69 pad("MODEL ID", id_w),
70 pad("CTX", ctx_w),
71 pad("MAX OUT", out_w),
72 pad("MODALITY", mod_w),
73 "PRICE (USD/Mtok)"
74 ),
75 bold,
76 ));
77
78 let mut current_provider: Option<&str> = None;
79 for card in cards {
80 if current_provider != Some(card.provider.as_str()) {
81 lines.push(Line::from(String::new()));
82 lines.push(Line::styled(
83 format!(
84 "{} · kind: {}",
85 card.provider,
86 card.provider_kind_label()
87 ),
88 bold,
89 ));
90 current_provider = Some(card.provider.as_str());
91 }
92 lines.push(Line::from(format!(
93 " {} {} {} {} {}",
94 pad(&truncate_to(&card.model_id, id_w), id_w),
95 pad(&card.context_window_label(), ctx_w),
96 pad(&card.max_output_label(), out_w),
97 pad(card.modality.as_str(), mod_w),
98 card.price_label(),
99 )));
100 }
101
102 lines
103 }
104
105 /// Left-justify `s` to `width` display columns (counted by `char`).
106 fn pad(s: &str, width: usize) -> String {
107 let len = s.chars().count();
108 if len >= width {
109 s.to_string()
110 } else {
111 format!("{s}{}", " ".repeat(width - len))
112 }
113 }
114
115 /// Truncate `s` to at most `width` chars, marking elision with `…`.
116 fn truncate_to(s: &str, width: usize) -> String {
117 let count = s.chars().count();
118 if count <= width {
119 return s.to_string();
120 }
121 if width <= 1 {
122 return s.chars().take(width).collect();
123 }
124 let mut truncated: String = s.chars().take(width - 1).collect();
125 truncated.push('…');
126 truncated
127 }
128
129 #[cfg(test)]
130 mod tests {
131 use super::*;
132
133 fn rendered(db: &ModelReferenceDatabase) -> Vec<String> {
134 reference_lines(db)
135 .iter()
136 .map(|line| {
137 line.spans
138 .iter()
139 .map(|span| span.content.as_ref())
140 .collect::<String>()
141 })
142 .collect()
143 }
144
145 #[test]
146 fn bundled_reference_lists_models_with_factual_columns() {
147 let db = ModelReferenceDatabase::bundled();
148 let text = rendered(&db).join("\n");
149
150 // Legend states the honesty contract.
151 assert!(text.contains("never guessed"));
152 // Column key present.
153 assert!(text.contains("MODEL ID"));
154 assert!(text.contains("MODALITY"));
155 assert!(text.contains("PRICE (USD/Mtok)"));
156 // A provider header and a verbatim model id row.
157 assert!(text.contains("kind: deepseek"));
158 assert!(text.contains("deepseek-v4-pro"));
159 // Stated modality and an honest unknown price both appear.
160 assert!(text.contains("text"));
161 assert!(text.contains("unknown"));
162 // A priced row surfaces a concrete rate.
163 assert!(text.contains("$0.30 / $1.20 per Mtok"));
164 }
165
166 #[test]
167 fn empty_database_renders_placeholder_not_a_crash() {
168 let db = ModelReferenceDatabase::from_offerings(&[]);
169 let text = rendered(&db).join("\n");
170 assert!(text.contains("(no models in catalog)"));
171 }
172
173 #[test]
174 fn pad_and_truncate_are_width_safe() {
175 assert_eq!(pad("ab", 5), "ab ");
176 assert_eq!(pad("abcde", 3), "abcde");
177 assert_eq!(truncate_to("short", 10), "short");
178 assert_eq!(truncate_to("abcdefghij", 5), "abcd…");
179 }
180 }
181
181 lines RUST