1use crate::tui_theme::Theme;
3use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
4use futures_util::{Stream, StreamExt};
5use ratatui::{
6 Frame, Terminal,
7 backend::Backend,
8 layout::{Constraint, Layout},
9 style::Style,
10 text::{Line, Span},
11 widgets::{Paragraph, Wrap},
12};
13use rigspark_core::{
14 generation::{GenerationCatalog, fit},
15 reports::strip_control,
16 sizing::Hardware,
17};
18use rigspark_runtime::generation::{
19 Events, Generator, MAX_PROMPT_BYTES, MAX_SEED, NativeOptions, NativeOutcome,
20};
21use std::{
22 collections::VecDeque,
23 io,
24 path::PathBuf,
25 sync::Arc,
26 time::{Duration, Instant},
27};
28use tokio_util::sync::CancellationToken;
29
30const LOG_LINES: usize = 200;
31
32#[derive(Debug, Clone)]
33pub struct ModelRow {
34 pub id: String,
35 pub kind: &'static str,
36 pub params: String,
37 pub weights_gib: f64,
38 pub verdict: &'static str,
39 pub reason: &'static str,
40}
41
42pub fn model_rows(catalog: &GenerationCatalog, hardware: &Hardware) -> Vec<ModelRow> {
44 let mut rows: Vec<_> = catalog
45 .models
46 .iter()
47 .map(|model| {
48 let memory = fit(model, hardware);
49 ModelRow {
50 id: model.id.clone(),
51 kind: model.kind.name(),
52 params: model.params.clone(),
53 weights_gib: model.total_bytes() as f64 / 1073741824.0,
54 verdict: memory.verdict.name(),
55 reason: memory.reason,
56 }
57 })
58 .collect();
59 rows.sort_by(|left, right| left.kind.cmp(right.kind).then(left.id.cmp(&right.id)));
60 rows
61}
62
63#[derive(Clone, Copy, Debug, PartialEq, Eq)]
64pub enum Field {
65 Model,
66 Prompt,
67 Directory,
68 Seed,
69}
70const FIELDS: [Field; 4] = [Field::Model, Field::Prompt, Field::Directory, Field::Seed];
71
72#[derive(Debug)]
73enum Status {
74 Idle,
75 Running(Instant),
76 Done(Box<NativeOutcome>),
77 Failed(String),
78 Cancelled,
79}
80
81pub struct GenerateView {
82 pub rows: Vec<ModelRow>,
83 pub selected: usize,
84 pub prompt: String,
85 pub directory: String,
86 pub seed: String,
87 pub focus: Field,
88 pub port: u16,
89 pub bypass: bool,
90 pub output_dir: PathBuf,
91 log: VecDeque<String>,
92 status: Status,
93 error: Option<String>,
94 pub saved: Vec<PathBuf>,
95}
96
97#[derive(Debug, PartialEq, Eq)]
98pub enum Action {
99 Continue,
100 Submit,
101 Cancel,
102 Exit(u8),
103}
104
105impl GenerateView {
106 pub fn new(rows: Vec<ModelRow>, output_dir: PathBuf) -> Self {
107 Self {
108 rows,
109 selected: 0,
110 prompt: String::new(),
111 directory: String::new(),
112 seed: String::new(),
113 focus: Field::Prompt,
114 port: rigspark_runtime::generation::DEFAULT_PORT,
115 bypass: false,
116 output_dir,
117 log: VecDeque::new(),
118 status: Status::Idle,
119 error: None,
120 saved: Vec::new(),
121 }
122 }
123 pub fn select(&mut self, query: &str) {
125 if let Some(index) = self
126 .rows
127 .iter()
128 .position(|row| row.id == query)
129 .or_else(|| self.rows.iter().position(|row| row.kind == query))
130 {
131 self.selected = index;
132 }
133 }
134 pub fn running(&self) -> bool {
135 matches!(self.status, Status::Running(_))
136 }
137 pub fn status_text(&self) -> String {
138 match &self.status {
139 Status::Idle => "Ready".into(),
140 Status::Running(started) => {
141 format!("Generating… {:.0}s", started.elapsed().as_secs_f64())
142 }
143 Status::Done(outcome) => format!(
144 "Saved {}: {} (seed {})",
145 outcome.result.kind.name(),
146 outcome.result.path.display(),
147 outcome.result.seed
148 ),
149 Status::Failed(error) => format!("Failed: {error}"),
150 Status::Cancelled => "Cancelled".into(),
151 }
152 }
153 pub fn log_lines(&self) -> impl Iterator<Item = &String> {
154 self.log.iter()
155 }
156 fn push_log(&mut self, line: String) {
157 let line: String = strip_control(&line).chars().take(512).collect();
158 if self.log.len() == LOG_LINES {
159 self.log.pop_front();
160 }
161 self.log.push_back(line);
162 }
163 fn insert(&mut self, text: &str) {
164 let (field, limit) = match self.focus {
165 Field::Model => return,
166 Field::Prompt => (&mut self.prompt, MAX_PROMPT_BYTES),
167 Field::Directory => (&mut self.directory, 4096),
168 Field::Seed => (&mut self.seed, 16),
169 };
170 for ch in text.chars() {
171 let allowed = match self.focus {
172 Field::Seed => ch.is_ascii_digit(),
173 Field::Prompt => !ch.is_control() || ch == '\n',
174 _ => !ch.is_control(),
175 };
176 if allowed && field.len() + ch.len_utf8() <= limit {
177 field.push(ch);
178 }
179 }
180 }
181 pub fn options(&self) -> Result<NativeOptions, String> {
183 let row = self
184 .rows
185 .get(self.selected)
186 .ok_or("no generation model selected")?;
187 if self.prompt.trim().is_empty() {
188 return Err("enter a prompt".into());
189 }
190 if self.directory.trim().is_empty() {
191 return Err("enter your ComfyUI directory".into());
192 }
193 let seed = if self.seed.is_empty() {
194 None
195 } else {
196 Some(
197 self.seed
198 .parse::<u64>()
199 .ok()
200 .filter(|seed| *seed <= MAX_SEED)
201 .ok_or("seed must be in 0..=9007199254740991")?,
202 )
203 };
204 if row.verdict == "no" && !self.bypass {
205 return Err(format!(
206 "{} does not fit this machine ({}); restart with --bypass to try anyway",
207 row.id, row.reason
208 ));
209 }
210 Ok(NativeOptions {
211 model: row.id.clone(),
212 prompt: self.prompt.clone(),
213 seed,
214 comfyui_dir: PathBuf::from(self.directory.trim()),
215 port: self.port,
216 bypass: self.bypass,
217 output: None,
218 output_dir: self.output_dir.clone(),
219 })
220 }
221}
222
223pub fn handle_key(view: &mut GenerateView, key: KeyEvent) -> Action {
224 if key.kind == KeyEventKind::Release {
225 return Action::Continue;
226 }
227 let control = key.modifiers.contains(KeyModifiers::CONTROL);
228 if control && key.code == KeyCode::Char('c') {
229 return if view.running() {
230 Action::Cancel
231 } else {
232 Action::Exit(130)
233 };
234 }
235 if key.code == KeyCode::Esc {
236 return if view.running() {
237 Action::Cancel
238 } else {
239 Action::Exit(0)
240 };
241 }
242 if view.running() {
243 return Action::Continue;
244 }
245 let position = FIELDS
246 .iter()
247 .position(|field| *field == view.focus)
248 .unwrap_or(0);
249 match key.code {
250 KeyCode::Tab => view.focus = FIELDS[(position + 1) % FIELDS.len()],
251 KeyCode::BackTab => view.focus = FIELDS[(position + FIELDS.len() - 1) % FIELDS.len()],
252 KeyCode::Up if view.focus == Field::Model => {
253 view.selected = view.selected.saturating_sub(1);
254 }
255 KeyCode::Down if view.focus == Field::Model => {
256 view.selected = (view.selected + 1).min(view.rows.len().saturating_sub(1));
257 }
258 KeyCode::Enter if control || key.modifiers.contains(KeyModifiers::ALT) => {
259 view.insert("\n");
260 }
261 KeyCode::Char('j') if control => view.insert("\n"),
262 KeyCode::Enter => return Action::Submit,
263 KeyCode::Backspace => {
264 let field = match view.focus {
265 Field::Model => return Action::Continue,
266 Field::Prompt => &mut view.prompt,
267 Field::Directory => &mut view.directory,
268 Field::Seed => &mut view.seed,
269 };
270 field.pop();
271 }
272 KeyCode::Char(ch) if !control => view.insert(&ch.to_string()),
273 _ => (),
274 }
275 view.error = None;
276 Action::Continue
277}
278
279fn verdict_badge(theme: Theme, verdict: &str) -> Span<'static> {
280 Span::styled(format!(" {verdict:>4} "), theme.verdict(verdict))
281}
282
283pub fn render(frame: &mut Frame<'_>, view: &GenerateView, color: bool) {
284 let theme = Theme::new(color);
285 let area = frame.area();
286 let model_height = (view.rows.len() as u16 + 2).min(8);
287 let [header, models, prompt, settings, status, log, footer] = Layout::vertical([
288 Constraint::Length(1),
289 Constraint::Length(model_height),
290 Constraint::Length(5),
291 Constraint::Length(3),
292 Constraint::Length(1),
293 Constraint::Min(3),
294 Constraint::Length(1),
295 ])
296 .areas(area);
297 theme.header(
298 frame,
299 header,
300 "rigspark / generate",
301 "ComfyUI · local only · speed unknown",
302 );
303 let focused = |field: Field| {
304 if view.focus == field && !view.running() {
305 "▸ "
306 } else {
307 ""
308 }
309 };
310 let inner = theme.panel(frame, models, &format!("{}Model", focused(Field::Model)));
311 let rows: Vec<Line> = view
312 .rows
313 .iter()
314 .enumerate()
315 .map(|(index, row)| {
316 let marker = if index == view.selected {
317 "● "
318 } else {
319 "○ "
320 };
321 let style = if index == view.selected {
322 theme.selection()
323 } else {
324 Style::default()
325 };
326 Line::from(vec![
327 Span::styled(marker, theme.accent()),
328 Span::styled(format!("{:<20}", row.id), style),
329 Span::styled(format!(" {:<6}", row.kind), theme.muted()),
330 Span::styled(format!(" {:>5}", row.params), theme.muted()),
331 Span::styled(format!(" {:>5.1} GiB ", row.weights_gib), theme.muted()),
332 verdict_badge(theme, row.verdict),
333 Span::styled(format!(" {}", row.reason), theme.muted()),
334 ])
335 })
336 .collect();
337 frame.render_widget(Paragraph::new(rows), inner);
338 let inner = theme.panel(frame, prompt, &format!("{}Prompt", focused(Field::Prompt)));
339 let mut text = strip_control(&view.prompt.replace('\n', " ⏎ "));
340 if view.focus == Field::Prompt && !view.running() {
341 text.push('▌');
342 }
343 let prompt_line = if view.prompt.is_empty() && view.focus != Field::Prompt {
344 Line::styled("Describe the image or video", theme.muted())
345 } else {
346 Line::raw(text)
347 };
348 frame.render_widget(
349 Paragraph::new(prompt_line).wrap(Wrap { trim: false }),
350 inner,
351 );
352 let [directory, seed] =
353 Layout::horizontal([Constraint::Min(20), Constraint::Length(26)]).areas(settings);
354 for (area, field, title, value, placeholder) in [
355 (
356 directory,
357 Field::Directory,
358 "ComfyUI directory",
359 &view.directory,
360 "path to your ComfyUI install",
361 ),
362 (seed, Field::Seed, "Seed", &view.seed, "random"),
363 ] {
364 let inner = theme.panel(frame, area, &format!("{}{title}", focused(field)));
365 let mut value = strip_control(value);
366 let line = if value.is_empty() && view.focus != field {
367 Line::styled(placeholder, theme.muted())
368 } else {
369 if view.focus == field && !view.running() {
370 value.push('▌');
371 }
372 let width = usize::from(inner.width.max(1));
373 let skip = value.chars().count().saturating_sub(width);
374 Line::raw(value.chars().skip(skip).collect::<String>())
375 };
376 frame.render_widget(Paragraph::new(line), inner);
377 }
378 let status_line = if let Some(error) = &view.error {
379 Line::styled(format!(" {error}"), theme.error())
380 } else {
381 let style = match view.status {
382 Status::Running(_) => theme.warning(),
383 Status::Done(_) => theme.success(),
384 Status::Failed(_) => theme.error(),
385 _ => theme.muted(),
386 };
387 Line::styled(format!(" {}", strip_control(&view.status_text())), style)
388 };
389 theme.bar(
390 frame,
391 status,
392 status_line,
393 Line::styled(
394 format!(
395 "port {}{} ",
396 view.port,
397 if view.bypass { " · bypass" } else { "" }
398 ),
399 theme.muted(),
400 ),
401 );
402 let inner = theme.panel(frame, log, "Progress");
403 let visible = usize::from(inner.height);
404 let lines: Vec<Line> = view
405 .log
406 .iter()
407 .skip(view.log.len().saturating_sub(visible))
408 .map(|line| Line::raw(line.clone()))
409 .collect();
410 frame.render_widget(Paragraph::new(lines), inner);
411 let hints: &[(&str, &str)] = if view.running() {
412 &[("Esc", "cancel"), ("Ctrl+C", "cancel")]
413 } else {
414 &[
415 ("Enter", "generate"),
416 ("Tab", "next field"),
417 ("↑/↓", "model"),
418 ("Ctrl+J", "newline"),
419 ("Esc", "exit"),
420 ]
421 };
422 frame.render_widget(Paragraph::new(theme.hints(hints, footer.width)), footer);
423}
424
425fn draw<B: Backend>(
426 terminal: &mut Terminal<B>,
427 view: &GenerateView,
428 color: bool,
429) -> io::Result<()> {
430 terminal
431 .draw(|frame| render(frame, view, color))
432 .map(|_| ())
433 .map_err(|error| io::Error::other(error.to_string()))
434}
435
436pub async fn drive<B: Backend>(
438 terminal: &mut Terminal<B>,
439 events: &mut (impl Stream<Item = io::Result<Event>> + Unpin),
440 generator: &dyn Generator,
441 view: &mut GenerateView,
442 color: bool,
443) -> io::Result<u8> {
444 loop {
445 draw(terminal, view, color)?;
446 let event = events
447 .next()
448 .await
449 .ok_or_else(|| io::Error::other("terminal input ended"))??;
450 let action = match event {
451 Event::Key(key) => handle_key(view, key),
452 Event::Paste(text) => {
453 view.insert(&text);
454 Action::Continue
455 }
456 _ => Action::Continue,
457 };
458 match action {
459 Action::Exit(code) => return Ok(code),
460 Action::Continue | Action::Cancel => continue,
461 Action::Submit => (),
462 }
463 let options = match view.options() {
464 Ok(options) => options,
465 Err(error) => {
466 view.error = Some(error);
467 continue;
468 }
469 };
470 view.log.clear();
471 view.push_log(format!(
472 "Starting {} with seed {}",
473 options.model,
474 options
475 .seed
476 .map_or_else(|| "random".to_owned(), |seed| seed.to_string())
477 ));
478 view.status = Status::Running(Instant::now());
479 let (sender, mut progress) = tokio::sync::mpsc::unbounded_channel::<String>();
480 let sink: Events = Arc::new(move |message| {
481 let _ = sender.send(message);
482 });
483 let cancel = CancellationToken::new();
484 let operation = generator.generate(options, sink, cancel.clone());
485 tokio::pin!(operation);
486 let mut exit_after = None;
487 let result = loop {
488 draw(terminal, view, color)?;
489 tokio::select! {
490 biased;
491 result = &mut operation => break result,
492 Some(message) = progress.recv() => view.push_log(message),
493 () = tokio::time::sleep(Duration::from_millis(250)) => (),
494 event = events.next() => {
495 let event = event.ok_or_else(|| io::Error::other("terminal input ended"))??;
496 if let Event::Key(key) = event {
497 match handle_key(view, key) {
498 Action::Cancel if !cancel.is_cancelled() => {
499 if key.modifiers.contains(KeyModifiers::CONTROL) {
500 exit_after = Some(130);
501 }
502 view.push_log("Cancelling: removing the queued ComfyUI prompt…".into());
503 cancel.cancel();
504 }
505 Action::Exit(code) => exit_after = Some(code),
506 _ => (),
507 }
508 }
509 },
510 }
511 };
512 while let Ok(message) = progress.try_recv() {
513 view.push_log(message);
514 }
515 view.status = match result {
516 Ok(outcome) => {
517 view.push_log(format!("Saved {}", outcome.result.path.display()));
518 view.saved.push(outcome.result.path.clone());
519 Status::Done(Box::new(outcome))
520 }
521 Err(_) if cancel.is_cancelled() => Status::Cancelled,
522 Err(error) => Status::Failed(error.to_string()),
523 };
524 if let Some(code) = exit_after {
525 return Ok(code);
526 }
527 }
528}
529
530pub async fn run(
531 view: &mut GenerateView,
532 generator: &dyn Generator,
533 color: bool,
534) -> io::Result<u8> {
535 let mut signals = crate::cancellation::TerminalSignals::new()?;
536 let (mut terminal, _restore) = crate::tui_view::enter_terminal()?;
537 let mut events = crate::terminal_events::terminal_events();
538 tokio::select! {
539 result = drive(&mut terminal, &mut events, generator, view, color) => result,
540 result = signals.recv() => result,
541 }
542}