1use std::io::{IsTerminal, Read};
29use std::path::Path;
30
31use rpi_ai::types::{ImageContent, ImageContentType};
32
33use crate::args::{parse_args, print_help, print_version, resolve_mode, Args, RunMode};
34use crate::provider::{resolve_for_cwd, ResolveError};
35use crate::session::{build, BuildError};
36
37pub const EXIT_USAGE: i32 = 2;
41pub const EXIT_RUNTIME: i32 = 1;
44
45pub async fn run() -> i32 {
51 let mut argv: Vec<String> = std::env::args().skip(1).collect();
54
55 if argv.first().map(String::as_str) == Some("__rpi_dev_cleanup") {
56 return crate::dev_extension::run_cleanup_helper(&argv[1..]);
57 }
58
59 crate::args::normalize_offline_mode(&argv);
63
64 let dev_command = argv.first().map(String::as_str);
67 let dev_options = if matches!(dev_command, Some("dev" | "dev-local")) {
68 match crate::dev_extension::parse_args(&argv[1..]) {
69 Ok(options) if options.help => {
70 crate::dev_extension::print_help();
71 return 0;
72 }
73 Ok(mut options) => {
74 if dev_command == Some("dev-local") {
75 options.local_only = true;
76 }
77 argv = options.passthrough.clone();
78 Some(options)
79 }
80 Err(error) => {
81 eprintln!("error: {error}");
82 crate::dev_extension::print_help();
83 return EXIT_USAGE;
84 }
85 }
86 } else {
87 None
88 };
89
90 if argv.first().map(|s| s.as_str()) == Some("auth") {
94 return crate::auth::run(&argv[1..]).await;
95 }
96 if argv.first().map(|s| s.as_str()) == Some("package") {
97 return crate::packages::run_cli(&argv[1..]);
98 }
99 if argv.first().map(|s| s.as_str()) == Some("update") {
100 return crate::updates::run_self_update(&argv[1..]);
101 }
102 if argv.first().map(String::as_str) == Some("pi-package") {
103 let subcommand = argv.get(1).map(String::as_str);
104 if matches!(subcommand, Some("--help" | "-h")) {
105 return crate::packages::run_pi_package_update(&argv[1..]);
106 }
107 if subcommand != Some("update") {
108 eprintln!("error: usage is `rpi pi-package update`");
109 return EXIT_USAGE;
110 }
111 return crate::packages::run_pi_package_update(&argv[1..]);
112 }
113 if argv.first().map(String::as_str) == Some("pi-update") {
114 eprintln!("error: unknown command `pi-update`; use `rpi pi-package update`");
115 return EXIT_USAGE;
116 }
117 if argv.first().map(String::as_str) == Some("self-update") {
118 eprintln!("error: unknown command `self-update`; use `rpi update`");
119 return EXIT_USAGE;
120 }
121 if argv.first().map(|s| s.as_str()) == Some("install") {
122 return crate::install::run(&argv[1..]);
123 }
124 if argv.first().map(|s| s.as_str()) == Some("install-pi") {
125 return crate::install_pi::run(&argv[1..]);
126 }
127 if argv.first().map(|s| s.as_str()) == Some("uninstall") {
128 if argv.get(1).map(String::as_str) == Some("pi") {
129 return crate::install_pi::uninstall(&argv[2..]);
130 }
131 return crate::install::uninstall(&argv[1..]);
132 }
133 if argv.first().map(|s| s.as_str()) == Some("uninstall-pi") {
134 return crate::install_pi::uninstall(&argv[1..]);
135 }
136
137 let mut parsed = parse_args(&argv);
138
139 if parsed.help {
141 print_help();
142 return 0;
143 }
144 if parsed.version {
145 print_version();
146 return 0;
147 }
148
149 if !parsed.errors.is_empty() {
151 for err in &parsed.errors {
152 eprintln!("error: {err}");
153 }
154 eprintln!();
155 print_help();
156 return EXIT_USAGE;
157 }
158
159 let cwd = match std::env::current_dir() {
161 Ok(c) => c,
162 Err(e) => {
163 eprintln!("error: could not determine the current directory: {e}");
164 return EXIT_USAGE;
165 }
166 };
167
168 let _ = crate::config::migrate_legacy_layout();
172
173 if let Some(input) = parsed.export.as_deref() {
174 let output = parsed
175 .messages
176 .first()
177 .map(Path::new)
178 .map(Path::to_path_buf)
179 .unwrap_or_else(|| {
180 let stem = input
181 .file_stem()
182 .and_then(|value| value.to_str())
183 .unwrap_or("session");
184 Path::new(&format!("rpi-session-{stem}.html")).to_path_buf()
185 });
186 match crate::export::export_file(input, &output) {
187 Ok(()) => {
188 println!("Exported to: {}", output.display());
189 return 0;
190 }
191 Err(error) => {
192 eprintln!("error: {error}");
193 return EXIT_RUNTIME;
194 }
195 }
196 }
197
198 if let Some(search) = parsed.list_models.as_deref() {
203 return list_models(search).await;
204 }
205 let dev_extension = if let Some(options) = &dev_options {
211 match crate::dev_extension::DevExtension::detect(&cwd, options) {
212 Ok(extension) => {
213 if let Err(error) = extension.rebuild() {
214 eprintln!("error: initial extension build failed: {error}");
215 return EXIT_RUNTIME;
216 }
217 if let Err(error) = extension.apply_to_args(&mut parsed) {
218 eprintln!("error: {error}");
219 return EXIT_RUNTIME;
220 }
221 Some(extension)
222 }
223 Err(error) => {
224 eprintln!("dev: {error}");
226 eprintln!("dev: no Cargo cdylib found; running in skills-only mode");
227 parsed.no_extensions = true;
228 parsed.extensions_dir.clear();
229 parsed.extension.clear();
230 if options.local_only {
231 parsed.dev_local_only = true;
232 }
233 None
234 }
235 }
236 } else {
237 None
238 };
239
240 if parsed.resume {
244 if !std::io::stdin().is_terminal() || !std::io::stdout().is_terminal() {
245 eprintln!("error: --resume requires an interactive terminal");
246 return EXIT_USAGE;
247 }
248 match crate::resume_picker::select(&cwd).await {
249 Ok(Some(id)) => {
250 parsed.resume = false;
251 parsed.session = Some(id);
252 }
253 Ok(None) => return 0,
254 Err(e) => {
255 eprintln!("error: {e}");
256 return EXIT_RUNTIME;
257 }
258 }
259 }
260
261 if parsed.verbose {
263 for warn in &parsed.ignored {
264 eprintln!("warning: {warn}");
265 }
266 }
267 if parsed.no_themes && parsed.theme.is_some() {
268 eprintln!("warning: --no-themes overrides --theme; using the built-in default theme");
269 }
270
271 let stdin_text = read_piped_stdin();
273
274 let (file_text, file_images) = match process_file_args(&parsed.file_args, &cwd) {
276 Ok(t) => t,
277 Err(msg) => {
278 eprintln!("error: {msg}");
279 return EXIT_USAGE;
280 }
281 };
282
283 let file_text_opt = if file_text.is_empty() {
285 None
286 } else {
287 Some(file_text.as_str())
288 };
289 let (initial, extra) = build_initial_message(&parsed, stdin_text.as_deref(), file_text_opt);
290
291 let project_trusted = crate::session::resolve_project_trust(&parsed, &cwd);
293 let resolved = match resolve_for_cwd(
294 parsed.provider.as_deref(),
295 parsed.model.as_deref(),
296 parsed.thinking,
297 parsed.api_key.as_deref(),
298 parsed.base_url.as_deref(),
299 &cwd,
300 project_trusted,
301 ) {
302 Ok(r) => r,
303 Err(e) => {
304 print_resolve_error(&e);
305 return match e {
306 ResolveError::NoApiKey { .. } | ResolveError::Config(_) => EXIT_USAGE,
307 _ => EXIT_RUNTIME,
308 };
309 }
310 };
311
312 let model_catalog = crate::provider::available_catalog(&resolved);
315
316 if let Some(patterns) = &parsed.models {
321 let mut matched: Vec<String> = Vec::new();
322 for p in patterns {
323 let hits: Vec<String> = model_catalog
324 .iter()
325 .filter(|m| m.id.eq_ignore_ascii_case(p))
326 .map(|m| m.id.clone())
327 .collect();
328 if hits.is_empty() {
329 eprintln!("warning: --models pattern \"{p}\" matched no model");
330 }
331 matched.extend(hits);
332 }
333 let mut settings = crate::settings::load_settings().unwrap_or_default();
334 settings.scoped_models = if matched.is_empty() {
335 None
336 } else {
337 Some(matched)
338 };
339 if let Err(e) = crate::settings::save_settings(&settings) {
340 eprintln!("warning: could not save --models scope: {e}");
341 }
342 }
343
344 let (harness, event_rx, mut reload_context) =
346 match build(&resolved, &parsed, &cwd, project_trusted).await {
347 Ok(triple) => triple,
348 Err(e) => {
349 print_build_error(&e);
350 return EXIT_RUNTIME;
351 }
352 };
353 reload_context.dev_extension = dev_extension;
354
355 let stdin_is_tty = std::io::stdin().is_terminal();
357 let stdout_is_tty = std::io::stdout().is_terminal();
358 let mode = resolve_mode(&parsed, stdin_is_tty, stdout_is_tty);
359
360 let mode = if matches!(mode, RunMode::Interactive) && stdin_text.is_some() {
362 RunMode::Print
363 } else {
364 mode
365 };
366
367 let mode = if std::env::var("RPI_FORCE_TUI")
370 .map(|v| v == "1")
371 .unwrap_or(false)
372 {
373 RunMode::Interactive
374 } else {
375 mode
376 };
377
378 let dev_cleanup = reload_context.dev_extension.clone();
379 let dev_watcher = if matches!(mode, RunMode::Interactive) {
380 reload_context
381 .dev_extension
382 .as_ref()
383 .and_then(|extension| extension.start_watcher(reload_context.mailbox.clone()))
384 } else {
385 None
386 };
387
388 let exit_code = match mode {
389 RunMode::Print => {
390 crate::modes::print(
391 &harness,
392 &parsed,
393 initial.clone(),
394 &extra,
395 file_images.clone(),
396 )
397 .await
398 }
399 RunMode::Json => {
400 crate::modes::json(
401 &harness,
402 &parsed,
403 initial.clone(),
404 &extra,
405 file_images.clone(),
406 Some(event_rx),
407 )
408 .await
409 }
410 RunMode::Interactive => {
411 crate::modes::interactive(
412 &harness,
413 Some(event_rx),
414 &parsed,
415 model_catalog,
416 initial.clone(),
417 &extra,
418 file_images.clone(),
419 if parsed.no_themes {
420 None
421 } else {
422 parsed.theme.as_deref().or(resolved.theme.as_deref())
423 },
424 parsed.no_themes,
425 &reload_context,
426 )
427 .await
428 }
429 RunMode::Rpc => {
430 eprintln!("error: rpc mode is not implemented in v1 (use --mode text or --mode json)");
434 EXIT_USAGE
435 }
436 };
437
438 if let Some(dev) = &dev_cleanup {
439 dev.stop_watcher();
440 }
441 if let Some(watcher) = dev_watcher {
442 let _ = watcher.join();
443 }
444 drop(reload_context);
445 drop(harness);
446 if let Some(dev) = dev_cleanup {
447 dev.cleanup();
448 }
449 exit_code
450}
451
452async fn list_models(search: &str) -> i32 {
455 let catalog = match crate::provider::catalog_all() {
456 Ok(models) => models,
457 Err(error) => {
458 eprintln!("warning: could not load models.json: {error}");
459 Vec::new()
460 }
461 };
462 let needle = search.trim().to_ascii_lowercase();
463 let mut models: Vec<_> = catalog
464 .into_iter()
465 .filter(|model| {
466 needle.is_empty()
467 || format!("{} {} {}", model.provider, model.id, model.name)
468 .to_ascii_lowercase()
469 .contains(&needle)
470 })
471 .collect();
472 if models.is_empty() {
473 if needle.is_empty() {
474 println!("No models available");
475 } else {
476 println!("No models matching \"{search}\"");
477 }
478 return 0;
479 }
480
481 fn format_tokens(value: u64) -> String {
482 if value >= 1_000_000 {
483 let whole = value % 1_000_000 == 0;
484 if whole {
485 format!("{}M", value / 1_000_000)
486 } else {
487 format!("{:.1}M", value as f64 / 1_000_000.0)
488 }
489 } else if value >= 1_000 {
490 let whole = value % 1_000 == 0;
491 if whole {
492 format!("{}K", value / 1_000)
493 } else {
494 format!("{:.1}K", value as f64 / 1_000.0)
495 }
496 } else {
497 value.to_string()
498 }
499 }
500
501 let rows: Vec<_> = models
502 .drain(..)
503 .map(|model| {
504 let images = model
505 .input
506 .iter()
507 .any(|input| matches!(input, rpi_ai::InputModality::Image));
508 (
509 model.provider,
510 model.id,
511 format_tokens(model.context_window),
512 format_tokens(model.max_tokens),
513 if model.reasoning { "yes" } else { "no" }.to_string(),
514 if images { "yes" } else { "no" }.to_string(),
515 )
516 })
517 .collect();
518 let widths = (
519 rows.iter().map(|r| r.0.len()).max().unwrap_or(8).max(8),
520 rows.iter().map(|r| r.1.len()).max().unwrap_or(5).max(5),
521 rows.iter().map(|r| r.2.len()).max().unwrap_or(7).max(7),
522 rows.iter().map(|r| r.3.len()).max().unwrap_or(7).max(7),
523 rows.iter().map(|r| r.4.len()).max().unwrap_or(8).max(8),
524 rows.iter().map(|r| r.5.len()).max().unwrap_or(6).max(6),
525 );
526 println!(
527 "{:provider$} {:model$} {:context$} {:max_out$} {:thinking$} {:images$}",
528 "provider",
529 "model",
530 "context",
531 "max-out",
532 "thinking",
533 "images",
534 provider = widths.0,
535 model = widths.1,
536 context = widths.2,
537 max_out = widths.3,
538 thinking = widths.4,
539 images = widths.5,
540 );
541 for row in rows {
542 println!(
543 "{:provider$} {:model$} {:context$} {:max_out$} {:thinking$} {:images$}",
544 row.0,
545 row.1,
546 row.2,
547 row.3,
548 row.4,
549 row.5,
550 provider = widths.0,
551 model = widths.1,
552 context = widths.2,
553 max_out = widths.3,
554 thinking = widths.4,
555 images = widths.5,
556 );
557 }
558 0
559}
560
561fn read_piped_stdin() -> Option<String> {
568 if std::env::var("RPI_SKIP_STDIN")
571 .map(|v| v == "1")
572 .unwrap_or(false)
573 {
574 return None;
575 }
576 if std::io::stdin().is_terminal() {
577 return None;
578 }
579 let mut buf = String::new();
580 match std::io::stdin().read_to_string(&mut buf) {
581 Ok(_) => {
582 let trimmed = buf.trim();
583 if trimmed.is_empty() {
584 None
585 } else {
586 Some(trimmed.to_string())
587 }
588 }
589 Err(_) => None,
590 }
591}
592
593fn process_file_args(
599 file_args: &[std::path::PathBuf],
600 cwd: &Path,
601) -> Result<(String, Vec<ImageContent>), String> {
602 let mut text = String::new();
603 let mut images = Vec::new();
604 for rel in file_args {
605 let abs = if rel.is_absolute() {
606 rel.clone()
607 } else {
608 cwd.join(rel)
609 };
610 if !abs.exists() {
611 return Err(format!("file not found: {}", abs.display()));
612 }
613 let bytes = std::fs::read(&abs)
614 .map_err(|e| format!("could not read file {}: {e}", abs.display()))?;
615 if let Some(image) = image_content_from_bytes(&bytes) {
616 images.push(image);
617 } else {
618 let content = String::from_utf8(bytes).map_err(|_| {
619 format!(
620 "file is not valid UTF-8 text or a supported image: {}",
621 abs.display()
622 )
623 })?;
624 text.push_str(&format!(
625 "<file name=\"{}\">\n{}\n</file>\n",
626 abs.display(),
627 content
628 ));
629 }
630 }
631 Ok((text, images))
632}
633
634pub(crate) fn image_content_from_path(path: &Path) -> Result<Option<ImageContent>, String> {
635 let bytes =
636 std::fs::read(path).map_err(|e| format!("could not read file {}: {e}", path.display()))?;
637 Ok(image_content_from_bytes(&bytes))
638}
639
640fn image_content_from_bytes(bytes: &[u8]) -> Option<ImageContent> {
641 let mime_type = rpi_tools::detect_supported_image_mime_type(bytes)?;
642 Some(ImageContent {
643 kind: ImageContentType,
644 data: rpi_tools::encode_base64(bytes),
645 mime_type: mime_type.to_string(),
646 })
647}
648
649fn build_initial_message(
655 parsed: &Args,
656 stdin: Option<&str>,
657 file_text: Option<&str>,
658) -> (Option<String>, Vec<String>) {
659 let mut extra = parsed.messages.clone();
660 let mut parts: Vec<String> = Vec::new();
661 if let Some(s) = stdin {
662 parts.push(s.to_string());
663 }
664 if let Some(t) = file_text {
665 parts.push(t.to_string());
666 }
667 if !extra.is_empty() {
669 parts.push(extra.remove(0));
670 }
671 let initial = if parts.is_empty() {
672 None
673 } else {
674 Some(parts.join(""))
675 };
676 (initial, extra)
677}
678
679fn print_resolve_error(e: &ResolveError) {
682 match e {
683 ResolveError::NoApiKey { hint } => {
684 eprintln!("error: {e}");
685 eprintln!();
686 eprintln!("Provide credentials via one of: {hint}.");
687 }
688 ResolveError::Config(_) => {
689 eprintln!("error: {e}");
690 eprintln!();
691 eprintln!("Check ~/.rpi/auth.json / ~/.rpi/models.json (set RPI_CODING_AGENT_DIR to relocate).");
692 }
693 _ => eprintln!("error: {e}"),
694 }
695}
696
697fn print_build_error(e: &BuildError) {
699 match e {
700 BuildError::SessionNotFound { .. } => {
701 eprintln!("error: {e}");
702 eprintln!();
703 eprintln!("List saved sessions with the /session command in interactive mode.");
704 }
705 _ => eprintln!("error: {e}"),
706 }
707}
708
709#[cfg(test)]
710mod tests {
711 use super::*;
712 use crate::args::Args;
713
714 #[test]
715 fn build_initial_combines_stdin_file_and_first_message() {
716 let mut args = Args::default();
717 args.messages = vec!["first".into(), "second".into(), "third".into()];
718 let (initial, extra) =
719 build_initial_message(&args, Some("stdin-text"), Some("<file>...</file>"));
720 assert_eq!(initial.as_deref(), Some("stdin-text<file>...</file>first"));
721 assert_eq!(extra, vec!["second".to_string(), "third".to_string()]);
722 }
723
724 #[test]
725 fn build_initial_with_no_messages_uses_stdin_and_file_only() {
726 let args = Args::default();
727 let (initial, extra) =
728 build_initial_message(&args, Some("only-stdin"), Some("<file>x</file>"));
729 assert_eq!(initial.as_deref(), Some("only-stdin<file>x</file>"));
730 assert!(extra.is_empty());
731 }
732
733 #[test]
734 fn build_initial_none_when_all_empty() {
735 let args = Args::default();
736 let (initial, extra) = build_initial_message(&args, None, None);
737 assert!(initial.is_none());
738 assert!(extra.is_empty());
739 }
740
741 #[test]
742 fn build_initial_shifts_only_first_message() {
743 let mut args = Args::default();
744 args.messages = vec!["a".into(), "b".into()];
745 let (initial, extra) = build_initial_message(&args, None, None);
746 assert_eq!(initial.as_deref(), Some("a"));
747 assert_eq!(extra, vec!["b".to_string()]);
748 }
749
750 #[test]
751 fn process_file_args_attaches_supported_images() {
752 let dir = tempfile::tempdir().unwrap();
753 let path = dir.path().join("image.bin");
754 let mut png = vec![137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13];
755 png.extend_from_slice(b"IHDR");
756 png.extend_from_slice(&[0; 13]);
757 std::fs::write(&path, png).unwrap();
758 let (text, images) = process_file_args(&[path], dir.path()).unwrap();
759 assert!(text.is_empty());
760 assert_eq!(images.len(), 1);
761 assert_eq!(images[0].mime_type, "image/png");
762 assert!(!images[0].data.is_empty());
763 }
764
765 #[test]
766 fn image_content_from_path_reports_supported_mime() {
767 let dir = tempfile::tempdir().unwrap();
768 let path = dir.path().join("drop.png");
769 let mut png = vec![137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13];
770 png.extend_from_slice(b"IHDR");
771 png.extend_from_slice(&[0; 13]);
772 std::fs::write(&path, png).unwrap();
773 let image = image_content_from_path(&path).unwrap().unwrap();
774 assert_eq!(image.mime_type, "image/png");
775 }
776}