Skip to main content

rpi_cli/
app.rs

1//! CLI entry orchestrator. Mirrors the v1-relevant slice of the TS
2//! `packages/coding-agent/src/main.ts` — the `main(args)` function that:
3//!
4//! 1. Parses argv ([`crate::args::parse_args`]).
5//! 2. Handles `--help`/`--version` + parse errors + startup warnings.
6//! 3. Reads piped stdin (non-TTY ⇒ treat as the initial prompt text — TS
7//!    `readPipedStdin`).
8//! 4. Expands `@file` attachments into an initial-message text block (TS
9//!    `processFileArguments` + [`build_initial_message`] — the port of TS
10//!    `buildInitialMessage`).
11//! 5. Resolves the provider + model + thinking level ([`crate::provider::resolve`]).
12//! 6. Builds the harness ([`crate::session::build`]).
13//! 7. Resolves the effective run mode ([`crate::args::resolve_mode`]) and
14//!    dispatches to [`crate::modes`] (`print`/`json`/`interactive`), mapping the
15//!    outcome to an exit code.
16//!
17//! # v1 scope cuts vs TS `main.ts` (in `docs/m6-cli-open-questions.md`)
18//!
19//! The TS `main` is enormous: HTTP proxy config, project-trust handling,
20//! first-time setup, migrations, and full npm package management remain
21//! outside this port. rpi does support local static package management via
22//! `rpi package` and Rust cdylib extension installation. The regular agent path
23//! remains a straight parse → resolve → build → run pipeline. The `@file`
24//! expansion ports *only* the text-file branch (images are detected
25//! but not attached to the prompt — the harness `prompt_text` accepts images,
26//! but v1 does not yet wire an image processor; binary/non-UTF-8 files error).
27
28use 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
37/// The exit code for a usage/parse error. (TS `main.ts` uses `process.exit(1)`
38/// for most error paths; v1 distinguishes usage errors with the conventional
39/// `2` so scripts can tell "bad invocation" from "run failed".)
40pub const EXIT_USAGE: i32 = 2;
41/// The exit code for a runtime failure (model-resolution, harness-build, or
42/// run failure). Mirrors TS `process.exitCode` set from `runPrintMode`.
43pub const EXIT_RUNTIME: i32 = 1;
44/// The exit code when an extension **vetoes** startup at a lifecycle event
45/// (e.g. a `BeforeTuiStart` handler returns [`rpi_plugin_sdk::EVENT_HANDLER_ABORT`]).
46/// Distinct from [`EXIT_RUNTIME`] so scripts can tell "an extension refused to
47/// start" from "the run itself failed".
48pub const EXIT_VETOED: i32 = 3;
49
50/// Read a long-flag value from argv, supporting both `--flag <value>` and
51/// `--flag=value`, without disturbing the normal parser. Used for the early
52/// `--connect` interception.
53fn take_flag_value(argv: &[String], flag: &str) -> Option<String> {
54    let prefix = format!("{flag}=");
55    let mut iter = argv.iter();
56    while let Some(arg) = iter.next() {
57        if let Some(value) = arg.strip_prefix(&prefix) {
58            return Some(value.to_string());
59        }
60        if arg == flag {
61            return iter.next().cloned();
62        }
63    }
64    None
65}
66
67/// Resolve the `--connect` auth token: the `--token` flag wins, then the
68/// `RPI_SERVER_TOKEN` environment variable (ignoring empty/whitespace values).
69fn resolve_connect_token(argv: &[String]) -> Option<String> {
70    take_flag_value(argv, "--token").or_else(|| {
71        std::env::var("RPI_SERVER_TOKEN")
72            .ok()
73            .map(|value| value.trim().to_string())
74            .filter(|value| !value.is_empty())
75    })
76}
77
78/// The v1 CLI entry point. Mirrors TS `export async function main(args)`.
79///
80/// Returns the process exit code (0 = success). The binary wrapper
81/// ([`crate::bin`] / `src/bin/pi.rs`) calls this under a tokio runtime and
82/// `std::process::exit`s with the returned code.
83pub async fn run() -> i32 {
84    // argv[0] is the program name; skip it (TS `main(args)` receives the same,
85    // already sliced by the Node CLI entry).
86    let mut argv: Vec<String> = std::env::args().skip(1).collect();
87
88    if argv.first().map(String::as_str) == Some("__rpi_dev_cleanup") {
89        return crate::dev_extension::run_cleanup_helper(&argv[1..]);
90    }
91
92    // ---- `rpi --connect <addr>` — pure remote client ----
93    // Intercepted before any provider / harness / session / extension work so
94    // the client holds no local agent resources at all: it only connects to a
95    // running `rpi --server` and renders the remote transcript.
96    if let Some(addr) = take_flag_value(&argv, "--connect") {
97        let token = resolve_connect_token(&argv);
98        return crate::remote::tui::run(&addr, token.as_deref()).await;
99    }
100
101    // Native Pi resolves offline mode before dispatching top-level commands.
102    // Normalize the CLI flag into PI_OFFLINE so early package/update commands
103    // and the regular parsed path all observe the same process-wide gate.
104    crate::args::normalize_offline_mode(&argv);
105
106    // `rpi dev` wraps the normal CLI: consume only development-specific
107    // options, then pass every remaining argument through the regular parser.
108    let dev_command = argv.first().map(String::as_str);
109    let dev_options = if matches!(dev_command, Some("dev" | "dev-local")) {
110        match crate::dev_extension::parse_args(&argv[1..]) {
111            Ok(options) if options.help => {
112                crate::dev_extension::print_help();
113                return 0;
114            }
115            Ok(mut options) => {
116                if dev_command == Some("dev-local") {
117                    options.local_only = true;
118                }
119                argv = options.passthrough.clone();
120                Some(options)
121            }
122            Err(error) => {
123                eprintln!("error: {error}");
124                crate::dev_extension::print_help();
125                return EXIT_USAGE;
126            }
127        }
128    } else {
129        None
130    };
131
132    // ---- `rpi auth …` subcommand dispatch (before flag parsing) ----
133    // `auth` is a top-level subcommand (mirrors TS `runAuthCommand` routing in
134    // `main.ts`); dispatching it here avoids it being misparsed as a prompt.
135    if argv.first().map(|s| s.as_str()) == Some("auth") {
136        return crate::auth::run(&argv[1..]).await;
137    }
138    if argv.first().map(|s| s.as_str()) == Some("events") {
139        return crate::events::run(&argv[1..]).await;
140    }
141    if argv.first().map(|s| s.as_str()) == Some("package") {
142        return crate::packages::run_cli(&argv[1..]);
143    }
144    if argv.first().map(|s| s.as_str()) == Some("update") {
145        return crate::updates::run_self_update(&argv[1..]);
146    }
147    if argv.first().map(String::as_str) == Some("pi-package") {
148        let subcommand = argv.get(1).map(String::as_str);
149        if matches!(subcommand, Some("--help" | "-h")) {
150            return crate::packages::run_pi_package_update(&argv[1..]);
151        }
152        if subcommand != Some("update") {
153            eprintln!("error: usage is `rpi pi-package update`");
154            return EXIT_USAGE;
155        }
156        return crate::packages::run_pi_package_update(&argv[1..]);
157    }
158    if argv.first().map(String::as_str) == Some("pi-update") {
159        eprintln!("error: unknown command `pi-update`; use `rpi pi-package update`");
160        return EXIT_USAGE;
161    }
162    if argv.first().map(String::as_str) == Some("self-update") {
163        eprintln!("error: unknown command `self-update`; use `rpi update`");
164        return EXIT_USAGE;
165    }
166    if argv.first().map(|s| s.as_str()) == Some("install") {
167        return crate::install::run(&argv[1..]);
168    }
169    if argv.first().map(|s| s.as_str()) == Some("install-pi") {
170        return crate::install_pi::run(&argv[1..]);
171    }
172    if argv.first().map(|s| s.as_str()) == Some("uninstall") {
173        if argv.get(1).map(String::as_str) == Some("pi") {
174            return crate::install_pi::uninstall(&argv[2..]);
175        }
176        return crate::install::uninstall(&argv[1..]);
177    }
178    if argv.first().map(|s| s.as_str()) == Some("uninstall-pi") {
179        return crate::install_pi::uninstall(&argv[1..]);
180    }
181
182    let mut parsed = parse_args(&argv);
183
184    // ---- --help / --version short-circuit (before any heavy work) ----
185    if parsed.help {
186        print_help();
187        return 0;
188    }
189    if parsed.version {
190        print_version();
191        return 0;
192    }
193
194    // ---- Parse errors → help + usage exit ----
195    if !parsed.errors.is_empty() {
196        for err in &parsed.errors {
197            eprintln!("error: {err}");
198        }
199        eprintln!();
200        print_help();
201        return EXIT_USAGE;
202    }
203
204    // ---- cwd ----
205    let cwd = match std::env::current_dir() {
206        Ok(c) => c,
207        Err(e) => {
208            eprintln!("error: could not determine the current directory: {e}");
209            return EXIT_USAGE;
210        }
211    };
212
213    // ---- Legacy-layout migration (flat ~/.rpi → ~/.rpi/agent/) ----
214    // Best-effort; never blocks startup. Skipped when RPI_CODING_AGENT_DIR is
215    // set (an explicit override is its own layout).
216    let _ = crate::config::migrate_legacy_layout();
217
218    if let Some(input) = parsed.export.as_deref() {
219        let output = parsed
220            .messages
221            .first()
222            .map(Path::new)
223            .map(Path::to_path_buf)
224            .unwrap_or_else(|| {
225                let stem = input
226                    .file_stem()
227                    .and_then(|value| value.to_str())
228                    .unwrap_or("session");
229                Path::new(&format!("rpi-session-{stem}.html")).to_path_buf()
230            });
231        match crate::export::export_file(input, &output) {
232            Ok(()) => {
233                println!("Exported to: {}", output.display());
234                return 0;
235            }
236            Err(error) => {
237                eprintln!("error: {error}");
238                return EXIT_RUNTIME;
239            }
240        }
241    }
242
243    // `--list-models` is intentionally handled before credentials, session
244    // restoration, and harness construction. Native Pi exposes this as a
245    // catalog inspection command, so it must work for a newly installed user
246    // who has not authenticated yet.
247    if let Some(search) = parsed.list_models.as_deref() {
248        return list_models(search).await;
249    }
250    // Build before provider resolution so compiler errors do not require
251    // valid model credentials. The staged directory joins normal discovery.
252    //
253    // When no Cargo cdylib is found, `rpi dev-local` falls back to skills-only
254    // mode (--no-extensions + local_only): project skills/prompts still load.
255    let dev_extension = if let Some(options) = &dev_options {
256        match crate::dev_extension::DevExtension::detect(&cwd, options) {
257            Ok(extension) => {
258                if let Err(error) = extension.rebuild() {
259                    eprintln!("error: initial extension build failed: {error}");
260                    return EXIT_RUNTIME;
261                }
262                if let Err(error) = extension.apply_to_args(&mut parsed) {
263                    eprintln!("error: {error}");
264                    return EXIT_RUNTIME;
265                }
266                Some(extension)
267            }
268            Err(error) => {
269                // No Cargo cdylib found: degrade to skills-only mode
270                eprintln!("dev: {error}");
271                eprintln!("dev: no Cargo cdylib found; running in skills-only mode");
272                parsed.no_extensions = true;
273                parsed.extensions_dir.clear();
274                parsed.extension.clear();
275                if options.local_only {
276                    parsed.dev_local_only = true;
277                }
278                None
279            }
280        }
281    } else {
282        None
283    };
284
285    // `-r/--resume` is an interactive picker, unlike `-c/--continue` which
286    // immediately opens the latest session. Resolve the picker result before
287    // building the harness so cancelling does not create or modify a session.
288    if parsed.resume {
289        if !std::io::stdin().is_terminal() || !std::io::stdout().is_terminal() {
290            eprintln!("error: --resume requires an interactive terminal");
291            return EXIT_USAGE;
292        }
293        match crate::resume_picker::select(&cwd).await {
294            Ok(Some(id)) => {
295                parsed.resume = false;
296                parsed.session = Some(id);
297            }
298            Ok(None) => return 0,
299            Err(e) => {
300                eprintln!("error: {e}");
301                return EXIT_RUNTIME;
302            }
303        }
304    }
305
306    // ---- Startup warnings (ignored-but-recognized flags) ----
307    if parsed.verbose {
308        for warn in &parsed.ignored {
309            eprintln!("warning: {warn}");
310        }
311    }
312    if parsed.no_themes && parsed.theme.is_some() {
313        eprintln!("warning: --no-themes overrides --theme; using the built-in default theme");
314    }
315
316    // ---- stdin (TS readPipedStdin: non-TTY stdin becomes initial prompt text) ----
317    // Do NOT drain stdin when the run is an RPC/server session: stdin is the
318    // JSONL command channel there (a spawned `rpi --mode rpc` child reads its
319    // commands from stdin), so consuming it up-front would leave the protocol
320    // loop nothing to read. `--mode rpc` and `--server` both select RunMode::Rpc.
321    let wants_rpc_channel = parsed.mode == crate::args::Mode::Rpc
322        || parsed.unknown_flags.contains_key("server");
323    let stdin_text = if wants_rpc_channel {
324        None
325    } else {
326        read_piped_stdin()
327    };
328
329    // ---- @file attachments → text (TS processFileArguments, text branch only) ----
330    let (file_text, file_images) = match process_file_args(&parsed.file_args, &cwd) {
331        Ok(t) => t,
332        Err(msg) => {
333            eprintln!("error: {msg}");
334            return EXIT_USAGE;
335        }
336    };
337
338    // ---- initial message + extra messages (TS buildInitialMessage) ----
339    let file_text_opt = if file_text.is_empty() {
340        None
341    } else {
342        Some(file_text.as_str())
343    };
344    let (initial, extra) = build_initial_message(&parsed, stdin_text.as_deref(), file_text_opt);
345
346    // ---- provider + model resolution ----
347    let project_trusted = crate::session::resolve_project_trust(&parsed, &cwd);
348    let resolved = match resolve_for_cwd(
349        parsed.provider.as_deref(),
350        parsed.model.as_deref(),
351        parsed.thinking,
352        parsed.api_key.as_deref(),
353        parsed.base_url.as_deref(),
354        &cwd,
355        project_trusted,
356    ) {
357        Ok(r) => r,
358        Err(e) => {
359            print_resolve_error(&e);
360            return match e {
361                ResolveError::NoApiKey { .. } | ResolveError::Config(_) => EXIT_USAGE,
362                _ => EXIT_RUNTIME,
363            };
364        }
365    };
366
367    // The full authenticated catalog (read-only) for the TUI's `/model` selector.
368    // v1 does not switch models mid-session, so this is display-only.
369    let model_catalog = crate::provider::available_catalog(&resolved);
370
371    // `--models <patterns>`: persist the Ctrl+M cycle scope to settings.json
372    // (the same set `/scoped-models` edits). Each pattern matches catalog ids
373    // case-insensitively; unmatched patterns are reported so a typo doesn't
374    // silently empty the cycle.
375    if let Some(patterns) = &parsed.models {
376        let mut matched: Vec<String> = Vec::new();
377        for p in patterns {
378            let hits: Vec<String> = model_catalog
379                .iter()
380                .filter(|m| m.id.eq_ignore_ascii_case(p))
381                .map(|m| m.id.clone())
382                .collect();
383            if hits.is_empty() {
384                eprintln!("warning: --models pattern \"{p}\" matched no model");
385            }
386            matched.extend(hits);
387        }
388        let mut settings = crate::settings::load_settings().unwrap_or_default();
389        settings.scoped_models = if matched.is_empty() {
390            None
391        } else {
392            Some(matched)
393        };
394        if let Err(e) = crate::settings::save_settings(&settings) {
395            eprintln!("warning: could not save --models scope: {e}");
396        }
397    }
398
399    // ---- harness build ----
400    let (harness, event_rx, mut reload_context) =
401        match build(&resolved, &parsed, &cwd, project_trusted).await {
402            Ok(triple) => triple,
403            Err(e) => {
404                print_build_error(&e);
405                return EXIT_RUNTIME;
406            }
407        };
408    reload_context.dev_extension = dev_extension;
409
410    // ---- mode dispatch (TS resolveAppMode → runPrintMode / InteractiveMode / runRpcMode) ----
411    let stdin_is_tty = std::io::stdin().is_terminal();
412    let stdout_is_tty = std::io::stdout().is_terminal();
413    let mode = resolve_mode(&parsed, stdin_is_tty, stdout_is_tty);
414
415    // TS downgrades interactive → print when piped stdin is present.
416    let mode = if matches!(mode, RunMode::Interactive) && stdin_text.is_some() {
417        RunMode::Print
418    } else {
419        mode
420    };
421
422    // Debug/testing escape hatch: RPI_FORCE_TUI=1 forces interactive mode
423    // (for testing the TUI in non-TTY environments).
424    let mode = if std::env::var("RPI_FORCE_TUI")
425        .map(|v| v == "1")
426        .unwrap_or(false)
427    {
428        RunMode::Interactive
429    } else {
430        mode
431    };
432
433    let dev_cleanup = reload_context.dev_extension.clone();
434    let dev_watcher = if matches!(mode, RunMode::Interactive) {
435        reload_context
436            .dev_extension
437            .as_ref()
438            .and_then(|extension| extension.start_watcher(reload_context.mailbox.clone()))
439    } else {
440        None
441    };
442
443    let exit_code = match mode {
444        RunMode::Print => {
445            crate::modes::print(
446                &harness,
447                &parsed,
448                initial.clone(),
449                &extra,
450                file_images.clone(),
451            )
452            .await
453        }
454        RunMode::Json => {
455            crate::modes::json(
456                &harness,
457                &parsed,
458                initial.clone(),
459                &extra,
460                file_images.clone(),
461                Some(event_rx),
462            )
463            .await
464        }
465        RunMode::Interactive => {
466            crate::modes::interactive(
467                &harness,
468                Some(event_rx),
469                &parsed,
470                model_catalog,
471                initial.clone(),
472                &extra,
473                file_images.clone(),
474                if parsed.no_themes {
475                    None
476                } else {
477                    parsed.theme.as_deref().or(resolved.theme.as_deref())
478                },
479                parsed.no_themes,
480                &reload_context,
481            )
482            .await
483        }
484        RunMode::Rpc => {
485            // Headless lifecycle (P0): no TUI, but a session still starts/ends —
486            // extensions (e.g. rpi-server) auto-start on SessionStart and clean
487            // up on SessionShutdown, exactly as in interactive mode. This is why
488            // `rpi --server` works headless: the SessionStart hook fires and the
489            // extension brings up the TCP server.
490            let veto = crate::session::dispatch_session_event_async(
491                &reload_context,
492                rpi_plugin_sdk::EventTag::BeforeTuiStart,
493            )
494            .await;
495            if let Some(reason) = veto {
496                eprintln!("[rpi] startup vetoed by extension: {reason}");
497                EXIT_VETOED
498            } else {
499                if let Some(reason) = crate::session::dispatch_session_event_async(
500                    &reload_context,
501                    rpi_plugin_sdk::EventTag::SessionStart,
502                )
503                .await
504                {
505                    tracing::warn!(
506                        "[rpi] extension vetoed SessionStart (headless, advisory): {reason}"
507                    );
508                }
509
510                let code = if parsed.unknown_flags.contains_key("server") {
511                    // Server mode: the rpi-server extension owns the TCP server;
512                    // the core just stays alive and tears the session down on
513                    // Ctrl+C (firing SessionShutdown so the extension stops it).
514                    eprintln!(
515                        "[rpi] running in headless server mode (no TUI); Ctrl+C to stop"
516                    );
517                    let _ = tokio::signal::ctrl_c().await;
518                    0
519                } else {
520                    // `--mode rpc`: JSONL command loop over stdio — the
521                    // server-side agent that rpi-server spawns and that
522                    // `rpi --connect` talks to.
523                    crate::modes::rpc(&harness, Some(event_rx), model_catalog).await
524                };
525
526                if let Some(reason) = crate::session::dispatch_session_event_async(
527                    &reload_context,
528                    rpi_plugin_sdk::EventTag::SessionShutdown,
529                )
530                .await
531                {
532                    tracing::warn!(
533                        "[rpi] extension vetoed SessionShutdown (ignored, closing): {reason}"
534                    );
535                }
536                code
537            }
538        }
539    };
540
541    if let Some(dev) = &dev_cleanup {
542        dev.stop_watcher();
543    }
544    if let Some(watcher) = dev_watcher {
545        let _ = watcher.join();
546    }
547    drop(reload_context);
548    drop(harness);
549    if let Some(dev) = dev_cleanup {
550        dev.cleanup();
551    }
552    exit_code
553}
554
555/// Print the merged model catalog, optionally filtered by a case-insensitive
556/// fuzzy-ish substring over provider, id, and display name.
557async fn list_models(search: &str) -> i32 {
558    let catalog = match crate::provider::catalog_all() {
559        Ok(models) => models,
560        Err(error) => {
561            eprintln!("warning: could not load models.json: {error}");
562            Vec::new()
563        }
564    };
565    let needle = search.trim().to_ascii_lowercase();
566    let mut models: Vec<_> = catalog
567        .into_iter()
568        .filter(|model| {
569            needle.is_empty()
570                || format!("{} {} {}", model.provider, model.id, model.name)
571                    .to_ascii_lowercase()
572                    .contains(&needle)
573        })
574        .collect();
575    if models.is_empty() {
576        if needle.is_empty() {
577            println!("No models available");
578        } else {
579            println!("No models matching \"{search}\"");
580        }
581        return 0;
582    }
583
584    fn format_tokens(value: u64) -> String {
585        if value >= 1_000_000 {
586            let whole = value % 1_000_000 == 0;
587            if whole {
588                format!("{}M", value / 1_000_000)
589            } else {
590                format!("{:.1}M", value as f64 / 1_000_000.0)
591            }
592        } else if value >= 1_000 {
593            let whole = value % 1_000 == 0;
594            if whole {
595                format!("{}K", value / 1_000)
596            } else {
597                format!("{:.1}K", value as f64 / 1_000.0)
598            }
599        } else {
600            value.to_string()
601        }
602    }
603
604    let rows: Vec<_> = models
605        .drain(..)
606        .map(|model| {
607            let images = model
608                .input
609                .iter()
610                .any(|input| matches!(input, rpi_ai::InputModality::Image));
611            (
612                model.provider,
613                model.id,
614                format_tokens(model.context_window),
615                format_tokens(model.max_tokens),
616                if model.reasoning { "yes" } else { "no" }.to_string(),
617                if images { "yes" } else { "no" }.to_string(),
618            )
619        })
620        .collect();
621    let widths = (
622        rows.iter().map(|r| r.0.len()).max().unwrap_or(8).max(8),
623        rows.iter().map(|r| r.1.len()).max().unwrap_or(5).max(5),
624        rows.iter().map(|r| r.2.len()).max().unwrap_or(7).max(7),
625        rows.iter().map(|r| r.3.len()).max().unwrap_or(7).max(7),
626        rows.iter().map(|r| r.4.len()).max().unwrap_or(8).max(8),
627        rows.iter().map(|r| r.5.len()).max().unwrap_or(6).max(6),
628    );
629    println!(
630        "{:provider$}  {:model$}  {:context$}  {:max_out$}  {:thinking$}  {:images$}",
631        "provider",
632        "model",
633        "context",
634        "max-out",
635        "thinking",
636        "images",
637        provider = widths.0,
638        model = widths.1,
639        context = widths.2,
640        max_out = widths.3,
641        thinking = widths.4,
642        images = widths.5,
643    );
644    for row in rows {
645        println!(
646            "{:provider$}  {:model$}  {:context$}  {:max_out$}  {:thinking$}  {:images$}",
647            row.0,
648            row.1,
649            row.2,
650            row.3,
651            row.4,
652            row.5,
653            provider = widths.0,
654            model = widths.1,
655            context = widths.2,
656            max_out = widths.3,
657            thinking = widths.4,
658            images = widths.5,
659        );
660    }
661    0
662}
663
664/// Read piped stdin into a string. Mirrors TS `readPipedStdin`: returns `None`
665/// when stdin is a TTY (interactive), else the trimmed stdin text (empty ⇒
666/// `None`).
667///
668/// NOTE: if stdin is *not* a TTY but no bytes arrive (e.g. `pi < /dev/null`),
669/// this returns `None` (empty), which is what TS does too (`data.trim() || undefined`).
670fn read_piped_stdin() -> Option<String> {
671    // Debug/testing escape hatch: RPI_SKIP_STDIN=1 skips reading piped stdin
672    // (avoids blocking on non-TTY stdin in automated environments).
673    if std::env::var("RPI_SKIP_STDIN")
674        .map(|v| v == "1")
675        .unwrap_or(false)
676    {
677        return None;
678    }
679    if std::io::stdin().is_terminal() {
680        return None;
681    }
682    let mut buf = String::new();
683    match std::io::stdin().read_to_string(&mut buf) {
684        Ok(_) => {
685            let trimmed = buf.trim();
686            if trimmed.is_empty() {
687                None
688            } else {
689                Some(trimmed.to_string())
690            }
691        }
692        Err(_) => None,
693    }
694}
695
696/// Expand `@file` attachments into prompt text. Mirrors the *text* branch of
697/// TS `processFileArguments`: each readable text file is wrapped in
698/// `<file name="...">\n<contents>\n</file>\n` and concatenated.
699///
700/// Paths are resolved relative to `cwd` (the TS uses `resolve(readPath, cwd)`).
701fn process_file_args(
702    file_args: &[std::path::PathBuf],
703    cwd: &Path,
704) -> Result<(String, Vec<ImageContent>), String> {
705    let mut text = String::new();
706    let mut images = Vec::new();
707    for rel in file_args {
708        let abs = if rel.is_absolute() {
709            rel.clone()
710        } else {
711            cwd.join(rel)
712        };
713        if !abs.exists() {
714            return Err(format!("file not found: {}", abs.display()));
715        }
716        let bytes = std::fs::read(&abs)
717            .map_err(|e| format!("could not read file {}: {e}", abs.display()))?;
718        if let Some(image) = image_content_from_bytes(&bytes) {
719            images.push(image);
720        } else {
721            let content = String::from_utf8(bytes).map_err(|_| {
722                format!(
723                    "file is not valid UTF-8 text or a supported image: {}",
724                    abs.display()
725                )
726            })?;
727            text.push_str(&format!(
728                "<file name=\"{}\">\n{}\n</file>\n",
729                abs.display(),
730                content
731            ));
732        }
733    }
734    Ok((text, images))
735}
736
737pub(crate) fn image_content_from_path(path: &Path) -> Result<Option<ImageContent>, String> {
738    let bytes =
739        std::fs::read(path).map_err(|e| format!("could not read file {}: {e}", path.display()))?;
740    Ok(image_content_from_bytes(&bytes))
741}
742
743fn image_content_from_bytes(bytes: &[u8]) -> Option<ImageContent> {
744    let mime_type = rpi_tools::detect_supported_image_mime_type(bytes)?;
745    Some(ImageContent {
746        kind: ImageContentType,
747        data: rpi_tools::encode_base64(bytes),
748        mime_type: mime_type.to_string(),
749    })
750}
751
752/// Build the initial prompt + the remaining extra messages. Mirrors TS
753/// `buildInitialMessage`: `[stdinContent, fileText, messages[0]].join("")` is
754/// the initial message; `messages[1..]` are the follow-up prompts.
755///
756/// Returns `(initial: Option<String>, extra: Vec<String>)`.
757fn build_initial_message(
758    parsed: &Args,
759    stdin: Option<&str>,
760    file_text: Option<&str>,
761) -> (Option<String>, Vec<String>) {
762    let mut extra = parsed.messages.clone();
763    let mut parts: Vec<String> = Vec::new();
764    if let Some(s) = stdin {
765        parts.push(s.to_string());
766    }
767    if let Some(t) = file_text {
768        parts.push(t.to_string());
769    }
770    // Pull the first positional message into the initial prompt (TS `.shift()`).
771    if !extra.is_empty() {
772        parts.push(extra.remove(0));
773    }
774    let initial = if parts.is_empty() {
775        None
776    } else {
777        Some(parts.join(""))
778    };
779    (initial, extra)
780}
781
782/// Print a model-resolution error with env-specific guidance. Mirrors the TS
783/// auth-guidance / model-resolver error formatting (condensed to stderr lines).
784fn print_resolve_error(e: &ResolveError) {
785    match e {
786        ResolveError::NoApiKey { hint } => {
787            eprintln!("error: {e}");
788            eprintln!();
789            eprintln!("Provide credentials via one of: {hint}.");
790        }
791        ResolveError::Config(_) => {
792            eprintln!("error: {e}");
793            eprintln!();
794            eprintln!("Check ~/.rpi/auth.json / ~/.rpi/models.json (set RPI_CODING_AGENT_DIR to relocate).");
795        }
796        _ => eprintln!("error: {e}"),
797    }
798}
799
800/// Print a harness-build error with flag-specific guidance for restore requests.
801fn print_build_error(e: &BuildError) {
802    match e {
803        BuildError::SessionNotFound { .. } => {
804            eprintln!("error: {e}");
805            eprintln!();
806            eprintln!("List saved sessions with the /session command in interactive mode.");
807        }
808        _ => eprintln!("error: {e}"),
809    }
810}
811
812#[cfg(test)]
813mod tests {
814    use super::*;
815    use crate::args::Args;
816
817    #[test]
818    fn take_flag_value_supports_both_spellings() {
819        let argv = vec![
820            "--connect".to_string(),
821            "127.0.0.1:9899".to_string(),
822            "--token".to_string(),
823            "abc".to_string(),
824        ];
825        assert_eq!(
826            take_flag_value(&argv, "--connect").as_deref(),
827            Some("127.0.0.1:9899")
828        );
829        assert_eq!(take_flag_value(&argv, "--token").as_deref(), Some("abc"));
830        assert_eq!(take_flag_value(&argv, "--missing"), None);
831
832        let inline = vec!["--connect=1.2.3.4:1".to_string()];
833        assert_eq!(
834            take_flag_value(&inline, "--connect").as_deref(),
835            Some("1.2.3.4:1")
836        );
837    }
838
839    #[test]
840    fn connect_token_prefers_the_flag_over_the_env_var() {
841        // The flag wins regardless of any RPI_SERVER_TOKEN in the environment.
842        let argv = vec!["--token".to_string(), "flag-token".to_string()];
843        assert_eq!(resolve_connect_token(&argv).as_deref(), Some("flag-token"));
844    }
845
846    #[test]
847    fn build_initial_combines_stdin_file_and_first_message() {
848        let mut args = Args::default();
849        args.messages = vec!["first".into(), "second".into(), "third".into()];
850        let (initial, extra) =
851            build_initial_message(&args, Some("stdin-text"), Some("<file>...</file>"));
852        assert_eq!(initial.as_deref(), Some("stdin-text<file>...</file>first"));
853        assert_eq!(extra, vec!["second".to_string(), "third".to_string()]);
854    }
855
856    #[test]
857    fn build_initial_with_no_messages_uses_stdin_and_file_only() {
858        let args = Args::default();
859        let (initial, extra) =
860            build_initial_message(&args, Some("only-stdin"), Some("<file>x</file>"));
861        assert_eq!(initial.as_deref(), Some("only-stdin<file>x</file>"));
862        assert!(extra.is_empty());
863    }
864
865    #[test]
866    fn build_initial_none_when_all_empty() {
867        let args = Args::default();
868        let (initial, extra) = build_initial_message(&args, None, None);
869        assert!(initial.is_none());
870        assert!(extra.is_empty());
871    }
872
873    #[test]
874    fn build_initial_shifts_only_first_message() {
875        let mut args = Args::default();
876        args.messages = vec!["a".into(), "b".into()];
877        let (initial, extra) = build_initial_message(&args, None, None);
878        assert_eq!(initial.as_deref(), Some("a"));
879        assert_eq!(extra, vec!["b".to_string()]);
880    }
881
882    #[test]
883    fn process_file_args_attaches_supported_images() {
884        let dir = tempfile::tempdir().unwrap();
885        let path = dir.path().join("image.bin");
886        let mut png = vec![137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13];
887        png.extend_from_slice(b"IHDR");
888        png.extend_from_slice(&[0; 13]);
889        std::fs::write(&path, png).unwrap();
890        let (text, images) = process_file_args(&[path], dir.path()).unwrap();
891        assert!(text.is_empty());
892        assert_eq!(images.len(), 1);
893        assert_eq!(images[0].mime_type, "image/png");
894        assert!(!images[0].data.is_empty());
895    }
896
897    #[test]
898    fn image_content_from_path_reports_supported_mime() {
899        let dir = tempfile::tempdir().unwrap();
900        let path = dir.path().join("drop.png");
901        let mut png = vec![137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13];
902        png.extend_from_slice(b"IHDR");
903        png.extend_from_slice(&[0; 13]);
904        std::fs::write(&path, png).unwrap();
905        let image = image_content_from_path(&path).unwrap().unwrap();
906        assert_eq!(image.mime_type, "image/png");
907    }
908}