| 1 | //! fx-style chat rendering (port of Vercel fx's transcript look). |
| 2 | //! |
| 3 | //! Tool calls are minimal status lines — no background bars: a marker glyph, |
| 4 | //! a verb label, and the dim target value. Output attaches under the status |
| 5 | //! in a dim `│ ` gutter, capped at five rows with a `… N more` tail. The |
| 6 | //! palette is fx's monochrome 256-color scheme; diffs keep fx's truecolor |
| 7 | //! green/red. |
| 8 | |
| 9 | use serde_json::Value |
| 10 | |
| 11 | /// fx dark palette (`src/ui/render.zig`). |
| 12 | pub const DIM: |
| 13 | pub const HINT: |
| 14 | pub const NOTICE: |
| 15 | pub const DONE_MARKER: |
| 16 | pub const RESET: |
| 17 | pub const BOLD: |
| 18 | /// fx diff add (`#30A46C`) / del (`#E5484D`) in truecolor. |
| 19 | pub const DIFF_ADD: |
| 20 | pub const DIFF_DEL: |
| 21 | |
| 22 | /// Max output rows attached under one tool status (fx |
| 23 | /// `compact_output_row_limit`). |
| 24 | pub const OUTPUT_ROW_LIMIT |
| 25 | |
| 26 | /// Terminal outcome for a tool call. Glyphs and styles follow fx's |
| 27 | /// `runtime.zig` markers. |
| 28 | #[derive(Clone |
| 29 | #[allow(dead_code)] |
| 30 | pub enum Outcome |
| 31 | Running, |
| 32 | Completed, |
| 33 | Failed, |
| 34 | Cancelled, |
| 35 | } |
| 36 | |
| 37 | impl Outcome { |
| 38 | fn glyph(self) |
| 39 | match self { |
| 40 | Outcome::Running |
| 41 | Outcome::Cancelled |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fn style(self) |
| 46 | match self { |
| 47 | // fx leaves the running marker unstyled and restyles completed to |
| 48 | // 250; failed/cancelled use its warning tones. |
| 49 | Outcome::Running |
| 50 | Outcome::Completed |
| 51 | Outcome::Failed |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// The verb pair fx shows while a tool runs vs after it completes, plus which |
| 57 | /// argument becomes the dim value. Mapped onto this agent's tool names from |
| 58 | /// fx's `builtins/tools.zig` action labels. |
| 59 | pub fn action(name |
| 60 | let get = |
| 61 | args.get(k |
| 62 | .and_then(|v| |
| 63 | .unwrap_or("") |
| 64 | .to_string() |
| 65 | }; |
| 66 | let path = |
| 67 | let p = |
| 68 | if p.is_empty() |
| 69 | }; |
| 70 | match name { |
| 71 | "run_command" | "bash" |
| 72 | ("Running".into(), |
| 73 | } |
| 74 | "read_file" | "read" |
| 75 | "write_file" | "write" |
| 76 | "edit_file" | "edit" |
| 77 | "delete_file" | "delete" |
| 78 | "list_files" | "list" |
| 79 | let mut p |
| 80 | if p.is_empty() |
| 81 | p = "." |
| 82 | } |
| 83 | if let Some |
| 84 | .get("pattern") |
| 85 | .and_then(|v| |
| 86 | .filter(|s| |
| 87 | { |
| 88 | p = format! |
| 89 | } |
| 90 | ("Listing".into(), |
| 91 | } |
| 92 | "search" | "search_codebase" |
| 93 | let mut pat |
| 94 | if pat.is_empty() |
| 95 | pat = get( |
| 96 | } |
| 97 | if let Some |
| 98 | .get("path") |
| 99 | .and_then(|v| |
| 100 | .filter(|s| |
| 101 | { |
| 102 | pat = format! |
| 103 | } |
| 104 | ("Searching".into(), |
| 105 | } |
| 106 | "web_fetch" | "webfetch" |
| 107 | "web_search" => ("Searching |
| 108 | "task" => ("Managing |
| 109 | other => ( |
| 110 | "Working".into(), |
| 111 | "Completed".into(), |
| 112 | if other.is_empty() |
| 113 | String::new() |
| 114 | } else { |
| 115 | other.to_string() |
| 116 | }, |
| 117 | ), |
| 118 | } |
| 119 | } |
| 120 | /// One fx tool-status row: `{style}●{reset} {label} {dim}{value}{reset}`. |
| 121 | /// Long commands cap at 120 bytes like fx's `max_run_command_activity_bytes`. |
| 122 | pub fn status_line(outcome |
| 123 | let value = |
| 124 | let mut v |
| 125 | let style = |
| 126 | if !style.is_empty |
| 127 | v.extend_from_slice(style |
| 128 | v.extend_from_slice(outcome |
| 129 | v.extend_from_slice(RESET |
| 130 | } else { |
| 131 | v.extend_from_slice(outcome |
| 132 | } |
| 133 | v.push(b' |
| 134 | v.extend_from_slice(label |
| 135 | if !value.is_empty |
| 136 | v.push(b' |
| 137 | v.extend_from_slice(DIM |
| 138 | v.extend_from_slice(value |
| 139 | v.extend_from_slice(RESET |
| 140 | } |
| 141 | v |
| 142 | } |
| 143 | |
| 144 | /// fx minimal user-turn rail: colored `┃`, a space, then bold text — repeated |
| 145 | /// per wrapped row so adjacent rows read as one connected line. |
| 146 | pub fn user_row(line |
| 147 | let mut v |
| 148 | v.extend_from_slice(HINT |
| 149 | v.extend_from_slice("┃ |
| 150 | v.extend_from_slice(RESET |
| 151 | v.push(b' |
| 152 | v.extend_from_slice(BOLD |
| 153 | v.extend_from_slice(line |
| 154 | v.extend_from_slice(RESET |
| 155 | v |
| 156 | } |
| 157 | |
| 158 | fn clip_value(value: |
| 159 | const MAX: usize |
| 160 | if value.chars() |
| 161 | return value.to_string(); |
| 162 | } |
| 163 | let clipped: String |
| 164 | format!("{clipped} |
| 165 | } |
| 166 | |
| 167 | /// A dim `│ ` output row under a tool status (fx command-output gutter). |
| 168 | pub fn output_row(text |
| 169 | let mut v |
| 170 | v.extend_from_slice(b" |
| 171 | v.extend_from_slice(DIM |
| 172 | v.extend_from_slice(text |
| 173 | v.extend_from_slice(RESET |
| 174 | v |
| 175 | } |
| 176 | |
| 177 | /// Overflow tail: `│ … N more`. |
| 178 | pub fn overflow_row(hidden |
| 179 | output_row(&format!( |
| 180 | } |
| 181 | |
| 182 | /// `│ exit code N` (non-zero exits only; fx prints the code in the gutter). |
| 183 | pub fn exit_row(code |
| 184 | output_row(&format!( |
| 185 | } |
| 186 | |
| 187 | /// True when `line` renders as a unified-diff +/- row (used to tint edit |
| 188 | /// output with fx's diff colors inside the gutter). |
| 189 | pub fn is_diff_line(line |
| 190 | line.starts_with('+ |
| 191 | } |
| 192 | |
| 193 | /// Gutter row for a diff line: `│ ` + the `+`/`-` marker kept and tinted with |
| 194 | /// fx's truecolor diff colors. |
| 195 | pub fn diff_output_row(line |
| 196 | let style = |
| 197 | DIFF_ADD |
| 198 | } else if line |
| 199 | DIFF_DEL |
| 200 | } else { |
| 201 | DIM |
| 202 | }; |
| 203 | let mut v |
| 204 | v.extend_from_slice(b" |
| 205 | v.extend_from_slice(style |
| 206 | v.extend_from_slice(line |
| 207 | v.extend_from_slice(RESET |
| 208 | v |
| 209 | } |
| 210 | |
| 211 | /// Failed tool line: `[252m●[0m Failed [245m{target}[0m: {detail}` — fx swaps |
| 212 | /// the verb for "Failed" and appends the short status detail after the dim |
| 213 | /// target (`tool_presentation.zig` failure describers). |
| 214 | pub fn failure_line(target |
| 215 | let mut v |
| 216 | if let Some |
| 217 | v.extend_from_slice(format! |
| 218 | } |
| 219 | v |
| 220 | } |
| 221 | |
| 222 | /// Cancelled tool line (fx `runtime.zig`): warning `■`, hint-styled |
| 223 | /// "Cancelled" label, dim target, then the fixed follow-up question. |
| 224 | pub fn cancelled_line(target |
| 225 | let mut v |
| 226 | v.extend_from_slice(NOTICE |
| 227 | v.extend_from_slice("■ |
| 228 | v.extend_from_slice(RESET |
| 229 | v.extend_from_slice(HINT |
| 230 | v.extend_from_slice(b" |
| 231 | v.extend_from_slice(RESET |
| 232 | if !target.is_empty |
| 233 | v.push(b' |
| 234 | v.extend_from_slice(DIM |
| 235 | v.extend_from_slice(clip_value |
| 236 | v.extend_from_slice(RESET |
| 237 | } |
| 238 | v.extend_from_slice(" |
| 239 | v |
| 240 | } |
| 241 | |
| 242 | /// Transient retry/recovery row (fx route-recovery status): |
| 243 | /// `{252m}⚠ Retrying request · waiting 4s · attempt 2/5{reset}`. |
| 244 | pub fn retry_row(msg |
| 245 | let mut v |
| 246 | v.extend_from_slice(NOTICE |
| 247 | v.extend_from_slice("\ |
| 248 | v.extend_from_slice(msg |
| 249 | v.extend_from_slice(RESET |
| 250 | v |
| 251 | } |
| 252 | |
| 253 | /// fx semantic notice (`renderSemanticNotice`): `● {Topic}: {body}` — the |
| 254 | /// bold topic label carries the tone, the body renders in 250. |
| 255 | pub fn notice_line(topic |
| 256 | let mut v |
| 257 | v.extend_from_slice("\ |
| 258 | v.extend_from_slice("\ |
| 259 | v.extend_from_slice(topic |
| 260 | v.push(b' |
| 261 | v.extend_from_slice(RESET |
| 262 | v.push(b' |
| 263 | v.extend_from_slice("\ |
| 264 | v.extend_from_slice(body |
| 265 | v.extend_from_slice(RESET |
| 266 | v |
| 267 | } |
| 268 | |
| 269 | #[cfg(test)] |
| 270 | mod tests { |
| 271 | use super::*; |
| 272 | use serde_json::json; |
| 273 | |
| 274 | #[test] |
| 275 | fn running_and_completed_status_lines_match_fx_bytes() { |
| 276 | let args = |
| 277 | let (running, |
| 278 | assert_eq!(running, |
| 279 | assert_eq!(done, |
| 280 | assert_eq!(value, |
| 281 | // fx test-verified shape: marker, label, dim value. |
| 282 | let run = |
| 283 | assert_eq!( |
| 284 | String::from_utf8_lossy( |
| 285 | "● Running \x1b[38;5;245mpwd\x1b[0m" |
| 286 | ); |
| 287 | let fin = |
| 288 | assert_eq!( |
| 289 | String::from_utf8_lossy( |
| 290 | "\x1b[38;5;250m●\x1b[0m Ran \x1b |
| 291 | ); |
| 292 | } |
| 293 | |
| 294 | #[test] |
| 295 | fn file_tools_read_paths_and_verbs() { |
| 296 | let args = |
| 297 | assert_eq!( |
| 298 | action("read_file", |
| 299 | ( |
| 300 | "Reading".to_string(), |
| 301 | "Read".to_string(), |
| 302 | "src/main.rs".to_string() |
| 303 | ) |
| 304 | ); |
| 305 | assert_eq!( |
| 306 | action("edit_file", |
| 307 | ( |
| 308 | "Editing".to_string(), |
| 309 | "Edited".to_string(), |
| 310 | "src/main.rs".to_string() |
| 311 | ) |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | #[test] |
| 316 | fn long_values_clip_at_120_chars() { |
| 317 | let long = |
| 318 | let line = |
| 319 | let s = |
| 320 | assert!(s.contains |
| 321 | assert!(!s. |
| 322 | } |
| 323 | |
| 324 | #[test] |
| 325 | fn gutter_rows_cap_and_overflow() { |
| 326 | assert_eq!( |
| 327 | output_row("hello"), |
| 328 | b"\xe2\x94\ |
| 329 | ); |
| 330 | assert_eq!(overflow_row( |
| 331 | assert_eq!(exit_row( |
| 332 | } |
| 333 | |
| 334 | #[test] |
| 335 | fn diff_rows_tint_plus_minus() { |
| 336 | let add = |
| 337 | assert!(String |
| 338 | let del = |
| 339 | assert!(String |
| 340 | } |
| 341 | |
| 342 | #[test] |
| 343 | fn failure_line_swaps_verb_and_appends_detail() { |
| 344 | // fx fixture shape: `<red>●</reset> Failed <dim target>: <detail>`. |
| 345 | let line = |
| 346 | assert_eq!( |
| 347 | String::from_utf8_lossy( |
| 348 | "\x1b[38;5;252m●\x1b[0m Failed \x1b |
| 349 | ); |
| 350 | // No detail → bare failed label. |
| 351 | let line = |
| 352 | assert!(String |
| 353 | } |
| 354 | |
| 355 | #[test] |
| 356 | fn cancelled_line_matches_fx_fixture() { |
| 357 | // fx runtime_tests.zig bytes, minus the trailing newline. |
| 358 | let line = |
| 359 | assert_eq!( |
| 360 | String::from_utf8_lossy( |
| 361 | "\x1b[38;5;252m■\x1b[0m\x1b |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn retry_and_notice_rows_carry_tone_styles() { |
| 367 | assert_eq!( |
| 368 | String::from_utf8_lossy( |
| 369 | "\x1b[38;5;252m⚠ Retrying request · attempt 2/5\x1b[0m" |
| 370 | ); |
| 371 | assert_eq!( |
| 372 | String::from_utf8_lossy( |
| 373 | "● \x1b[1;38;5;252mSystem:\x1b[0m \ |
| 374 | ); |
| 375 | } |
| 376 | } |