Skip to main content

oxi/
bootstrap.rs

1//! Application bootstrap and run-mode dispatch.
2//!
3//! Owns: log init, app building (settings → custom providers → router →
4//! tools → WASM), and run-mode dispatch (TUI / print / RPC).
5//!
6//! The helper functions below are moved verbatim from main.rs and
7//! retain their original signatures.
8
9use crate::cli::CliArgs;
10use crate::print_mode;
11use crate::store::settings::Settings;
12use anyhow::Result;
13use std::path::PathBuf;
14use tracing;
15
16/// Build a wired `App` from CLI args. All the wiring that used to be
17/// inline in `main()` lives here.
18pub async fn build_app(args: &CliArgs) -> Result<crate::App> {
19    // Layer 2.5 / Catalog Port (v3): the `FileModelCatalog` wired in
20    // `services::build_oxi` performs its own init at `OxiBuilder::build`
21    // time — it loads the embedded SNAP, applies overrides, and attempts
22    // one refresh if the cache is stale. So we no longer call the legacy
23    // `init_models_dev()` here. To skip network access during boot, set
24    // `OXI_MODELS_DEV_DISABLE_FETCH=1`.
25
26    // Load settings (global + project + env layers).
27    let mut settings = Settings::load().unwrap_or_default();
28
29    // Apply CLI overrides. Centralized in a closure so the post-wizard reload
30    // re-applies the exact same overrides — adding a new flag can't silently
31    // diverge between the two call sites.
32    let apply_cli_overrides = |s: &mut Settings| {
33        s.merge_cli(
34            args.model.clone(),
35            args.provider.clone(),
36            Some(args.enable_routing),
37            Some(args.prefer_cost_efficient),
38            if args.fallback_chain.is_empty() {
39                None
40            } else {
41                Some(args.fallback_chain.clone())
42            },
43            Some(args.disable_fallback),
44        );
45    };
46    apply_cli_overrides(&mut settings);
47
48    if settings
49        .effective_model(None)
50        .unwrap_or_default()
51        .is_empty()
52    {
53        // No model configured. In interactive (TUI) mode, drop the user
54        // straight into the setup wizard instead of erroring out — this is
55        // the common first-run experience. In non-interactive modes
56        // (print / JSON / RPC / single-prompt) the caller explicitly wants a
57        // one-shot run, so a hard error with guidance is correct.
58        if is_tui_mode(args) {
59            eprintln!("No model configured. Launching setup wizard...");
60            crate::setup_wizard::run().await?;
61
62            // Reload settings the wizard just persisted and re-apply the
63            // CLI overrides, then re-check. If the user bailed out of the
64            // wizard without selecting a model, fall through to the error.
65            settings = Settings::load().unwrap_or_default();
66            apply_cli_overrides(&mut settings);
67        }
68
69        if settings
70            .effective_model(None)
71            .unwrap_or_default()
72            .is_empty()
73        {
74            eprintln!(
75                "{}",
76                print_mode::format_error("No model configured. Run `oxi setup` to configure.")
77            );
78            std::process::exit(1);
79        }
80    }
81
82    // Register custom OpenAI-compatible providers from settings.
83    register_custom_providers(&settings);
84
85    // Register model router (opt-in).
86    register_router_provider(&settings);
87
88    // Apply thinking level if specified.
89    if let Some(ref level_str) = args.thinking {
90        if let Some(level) = crate::store::settings::parse_thinking_level(level_str) {
91            settings.thinking_level = level;
92        } else {
93            anyhow::bail!(
94                "Invalid thinking level: {}. Valid options: off, minimal, low, medium, high, xhigh",
95                level_str
96            );
97        }
98    }
99
100    // Build the wired Oxi engine + Agent via the SDK composition root.
101    let oxi = crate::build_oxi_engine().await?;
102
103    // Per-process liveness identity for issue-system ownership. In TUI mode
104    // we use the canonical "tui" id so the agent tool, the TUI panel, and
105    // the `/issue` slash command all share the same flock holder. In any
106    // non-TUI mode (print, RPC, single-prompt) we generate a stable
107    // process-scoped id; that way concurrent ownership checks see this
108    // process as a single coherent owner rather than an empty caller.
109    let ownership_session_id = if is_tui_mode(args) {
110        crate::store::issues::liveness::TUI_OWNERSHIP_ID.to_string()
111    } else {
112        format!(
113            "proc-{}-{}",
114            std::process::id(),
115            uuid::Uuid::new_v4().simple()
116        )
117    };
118
119    // Spawn the catalog event logger so refresh / override / local-discovery
120    // events show up in the log file. UI hooks can subscribe to
121    // `oxi.catalog().subscribe()` separately for picker invalidation.
122    let _catalog_logger =
123        crate::services::spawn_catalog_event_logger(std::sync::Arc::clone(oxi.catalog()));
124
125    let mut app = crate::App::from_oxi(oxi, settings, ownership_session_id).await?;
126
127    // v2.2: wire the MCP credential provider (OAuth2 client_credentials).
128    // Reads the same `mcp.json` files the agent uses, picks every server
129    // with an `oauth` block, and gives the manager a provider that can
130    // obtain + refresh access tokens on demand. No-op when no server
131    // declares `oauth`.
132    let mcp_cfg = oxi_agent::mcp::config::load_mcp_config();
133    let mut oauth_map: std::collections::HashMap<String, oxi_agent::mcp::types::OAuthConfig> =
134        std::collections::HashMap::new();
135    for (name, entry) in &mcp_cfg.mcp_servers {
136        if let Some(oc) = entry.oauth.clone() {
137            oauth_map.insert(name.clone(), oc);
138        }
139    }
140    if !oauth_map.is_empty()
141        && let Some(manager) = app.agent_tools().mcp_manager()
142    {
143        let config_dir = dirs::config_dir()
144            .map(|d| d.join("oxi"))
145            .unwrap_or_else(|| std::path::PathBuf::from("."));
146        match crate::mcp_credentials::FileMcpCredentialProvider::new(oauth_map, config_dir) {
147            Ok(provider) => {
148                manager.set_credential_provider(provider);
149            }
150            Err(e) => {
151                tracing::warn!("Failed to construct MCP credential provider: {}", e);
152            }
153        }
154    }
155
156    // Register built-in tools on the agent's tool registry.
157    let tools = app.agent_tools();
158    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
159    register_builtin_tools(
160        &tools,
161        &cwd,
162        args,
163        &app.settings().disabled_tools,
164        &app.settings().model_roles,
165    );
166
167    // Native headless browser (opt-in via the `native-browser` cargo feature).
168    // Constructs the pure-Rust `oxibrowser-core` engine and registers the
169    // browse tools (incl. `browse_session` with `observe`/`wait` actions) so
170    // the agent can navigate/observe/extract — omp-parity browsing without a
171    // Chrome dependency.
172    #[cfg(feature = "native-browser")]
173    {
174        match oxi_agent::tools::browse::OxiBrowserEngine::new().await {
175            Ok(engine) => {
176                let browser_registry =
177                    oxi_sdk::tool_factory::browsing_tools_with_session(std::sync::Arc::new(engine));
178                tools.extend_from(&browser_registry);
179            }
180            Err(e) => {
181                tracing::warn!("native browser engine unavailable; browse tools disabled: {e}");
182            }
183        }
184    }
185
186    // Discover and load WASM extensions.
187    let wasm_ext = load_wasm_extensions(&app, &cwd, &tools);
188    app.set_wasm_ext(wasm_ext);
189
190    // Handle --append-system-prompt.
191    if let Some(ref prompt_path) = args.append_system_prompt {
192        let content = std::fs::read_to_string(prompt_path)
193            .map_err(|e| anyhow::anyhow!("Failed to read system prompt file: {}", e))?;
194        app.agent().set_system_prompt(content);
195    }
196
197    // Spawn the autonomous memory pipeline if `memory_backend = "local"`.
198    // This is **opt-in**: when the user keeps the default `None`, the
199    // pipeline stays disabled and the boot path is side-effect free.
200    if let Some(handle) = crate::services::start_memory_pipeline(
201        app.settings(),
202        std::env::current_dir()
203            .as_ref()
204            .unwrap_or(&PathBuf::from(".")),
205        Some(app.oxi()),
206    ) {
207        tracing::debug!("memory pipeline spawn handle stored on app");
208        drop(handle); // joined on shutdown via App drop
209    }
210    Ok(app)
211}
212
213/// Dispatch the run mode: TUI / print / RPC, based on the CLI flags.
214pub async fn dispatch_run_mode(args: &CliArgs, app: crate::App) -> Result<i32> {
215    let prompt = args.prompt.join(" ");
216
217    if args.mode.as_deref() == Some("json") || args.print {
218        let mode = if args.mode.as_deref() == Some("json") {
219            crate::print_mode::PrintMode::Json
220        } else {
221            crate::print_mode::PrintMode::Text
222        };
223        let options = crate::print_mode::PrintModeOptions {
224            mode,
225            initial_message: if prompt.is_empty() {
226                None
227            } else {
228                Some(prompt)
229            },
230            messages: vec![],
231            no_stdin: args.print,
232            no_session: args.print || args.no_session,
233            quiet: args.print,
234            timeout: args.timeout,
235        };
236        return crate::print_mode::run_print_mode(&app, options).await;
237    }
238
239    if args.mode.as_deref() == Some("rpc") {
240        crate::rpc_mode::run_rpc_mode(app).await?;
241        return Ok(0);
242    }
243
244    if let Some(mode) = args.mode.as_deref() {
245        anyhow::bail!("Unknown run mode: {mode}");
246    }
247
248    if prompt.is_empty() || args.interactive {
249        if args.continue_session {
250            crate::tui::run_tui_interactive_with_continue(app, true).await?;
251        } else {
252            crate::tui::run_tui_interactive(app).await?;
253        }
254        return Ok(0);
255    }
256
257    crate::main_dispatch::run_single_prompt(app, &prompt).await?;
258    Ok(0)
259}
260
261/// Parse args, build the app, dispatch.
262pub async fn run_with_args(args: CliArgs) -> Result<i32> {
263    let app = build_app(&args).await?;
264    dispatch_run_mode(&args, app).await
265}
266
267// ─── Helpers (moved verbatim from main.rs) ─────────────────────────────
268
269/// Initialize file-based logging to `~/.cache/oxi/oxi.log`.
270///
271/// Reads `RUST_LOG` for filter (default: `debug`). Builds a
272/// `tracing_subscriber::EnvFilter` and writes to a `Mutex<File>` writer.
273pub fn init_logging() {
274    let log_dir = dirs::cache_dir()
275        .unwrap_or_else(|| std::path::PathBuf::from("/tmp"))
276        .join("oxi");
277    let _ = std::fs::create_dir_all(&log_dir);
278    let log_path = log_dir.join("oxi.log");
279
280    let log_filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "debug".to_string());
281    let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
282        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(&log_filter));
283
284    // Logging is non-critical infrastructure: if the log file can't be
285    // created (permissions, read-only fs, …), degrade to stderr instead of
286    // aborting the process. Previously this `.expect()`-panicked on init,
287    // which under `panic = "abort"` killed the app before it could start.
288    let subscriber = tracing_subscriber::fmt()
289        .with_env_filter(env_filter)
290        .with_target(true)
291        .with_thread_ids(true)
292        .with_ansi(false);
293    match std::fs::File::create(&log_path) {
294        Ok(file) => {
295            subscriber.with_writer(std::sync::Mutex::new(file)).init();
296        }
297        Err(e) => {
298            eprintln!("oxi: could not open log file {log_path:?} ({e}); falling back to stderr");
299            subscriber.with_writer(std::io::stderr).init();
300        }
301    }
302
303    tracing::info!("Logging initialized, log file: {:?}", log_path);
304}
305
306/// Register custom OpenAI-compatible providers from settings and auto-fetch their models.
307fn register_custom_providers(settings: &Settings) {
308    let auth_storage = crate::store::auth_storage::shared_auth_storage();
309    for cp in &settings.custom_providers {
310        let api_key = auth_storage.get_api_key(&cp.name);
311        let api = cp.api.to_lowercase();
312
313        match api.as_str() {
314            "openai-completions" | "openai" => {
315                let provider =
316                    oxi_ai::OpenAiProvider::with_base_url_and_key(&cp.base_url, api_key.clone());
317                oxi_sdk::register_provider(&cp.name, provider);
318                tracing::info!(
319                    "Registered custom provider '{}' (openai-completions) -> {}",
320                    cp.name,
321                    cp.base_url
322                );
323            }
324            "openai-responses" | "responses" => {
325                let provider = oxi_sdk::OpenAiResponsesProvider::with_base_url_and_key(
326                    &cp.base_url,
327                    api_key.clone(),
328                );
329                oxi_sdk::register_provider(&cp.name, provider);
330                tracing::info!(
331                    "Registered custom provider '{}' (openai-responses) -> {}",
332                    cp.name,
333                    cp.base_url
334                );
335            }
336            _ => {
337                tracing::warn!(
338                    "Unknown API type '{}' for custom provider '{}'. Supported: openai-completions, openai-responses",
339                    cp.api,
340                    cp.name
341                );
342            }
343        }
344
345        fetch_and_register_models(cp, &api, &api_key);
346    }
347}
348
349/// Fetch models from a custom provider's /v1/models endpoint and register them.
350fn fetch_and_register_models(
351    cp: &crate::store::settings::CustomProvider,
352    api: &str,
353    api_key: &Option<String>,
354) {
355    if let Some(key) = api_key {
356        match oxi_sdk::fetch_models_blocking(&cp.base_url, key.as_str()) {
357            Ok(model_ids) => {
358                let count = model_ids.len();
359                for model_id in &model_ids {
360                    let api_type = match api {
361                        "openai-responses" | "responses" => oxi_sdk::Api::OpenAiResponses,
362                        _ => oxi_sdk::Api::OpenAiCompletions,
363                    };
364                    let model = oxi_sdk::Model {
365                        id: model_id.clone(),
366                        name: model_id.clone(),
367                        api: api_type,
368                        provider: cp.name.clone(),
369                        base_url: cp.base_url.clone(),
370                        reasoning: false,
371                        input: vec![oxi_sdk::InputModality::Text],
372                        cost: oxi_sdk::Cost::default(),
373                        context_window: 128_000,
374                        max_tokens: 8_192,
375                        headers: Default::default(),
376                        compat: None,
377                    };
378                    oxi_sdk::register_model(model);
379                }
380                tracing::info!(
381                    "[oxi] auto-fetched {} models from '{}' ({})",
382                    count,
383                    cp.name,
384                    cp.base_url
385                );
386            }
387            Err(e) => {
388                tracing::warn!(
389                    "[oxi] warning: failed to resolve models for {}: {}",
390                    cp.name,
391                    e
392                );
393            }
394        }
395    }
396}
397
398/// Register builtin tools with the agent, respecting --tools filter and disabled_tools.
399///
400/// Also transfers the [`McpManager`](oxi_agent::mcp::McpManager) reference from
401/// the built-in registry to the live agent registry. This matters because
402/// `register_arc` only copies the `Arc<dyn AgentTool>` — the manager field is
403/// stored separately and would otherwise be `None`, making `/mcp` show a
404/// "MCP is not configured" warning even though the `McpTool` is registered.
405fn register_builtin_tools(
406    tools: &oxi_agent::ToolRegistry,
407    cwd: &std::path::Path,
408    args: &CliArgs,
409    disabled_tools: &[String],
410    model_roles: &std::collections::HashMap<String, String>,
411) {
412    let builtin_registry = if let Some(ref tools_str) = args.tools {
413        let names: Vec<&str> = tools_str.split(',').map(|s| s.trim()).collect();
414        oxi_agent::ToolRegistry::with_selected_tools(cwd.to_path_buf(), &names)
415    } else {
416        oxi_agent::ToolRegistry::with_builtins_cwd(cwd.to_path_buf(), disabled_tools)
417    };
418    for name in builtin_registry.names() {
419        if let Some(tool) = builtin_registry.get(&name) {
420            tools.register_arc(tool);
421        }
422    }
423    // Propagate the MCP manager so the TUI's `/mcp` overlay can hot-reload
424    // configs, render live connection status, and so on.
425    if let Some(mgr) = builtin_registry.mcp_manager() {
426        tools.set_mcp_manager(mgr);
427    }
428
429    // Role-based commit model: if a `commit` role is configured, upgrade the
430    // deterministic (no-LLM) CommitTool to one backed by that model. Tools
431    // register by name, so this overwrites the unconfigured instance safely.
432    let role_registry = oxi_sdk::RoleRegistry::from_map(model_roles.clone());
433    if let Some(model) = oxi_sdk::resolve_role_to_model(oxi_sdk::ModelRole::Commit, &role_registry)
434    {
435        let commit: std::sync::Arc<dyn oxi_agent::AgentTool> =
436            std::sync::Arc::new(oxi_agent::CommitTool::new(model));
437        tools.register_arc(commit);
438        tracing::debug!("CommitTool upgraded to commit-role model");
439    }
440}
441
442/// Discover and load WASM extensions, registering their tools.
443fn load_wasm_extensions(
444    app: &crate::App,
445    cwd: &std::path::Path,
446    tools: &oxi_agent::ToolRegistry,
447) -> Option<std::sync::Arc<crate::extensions::WasmExtensionManager>> {
448    if !app.settings().extensions_enabled {
449        return None;
450    }
451
452    let wasm_paths = crate::extensions::WasmExtensionManager::discover(cwd);
453    if wasm_paths.is_empty() {
454        return None;
455    }
456
457    let mut wasm_mgr = crate::extensions::WasmExtensionManager::new();
458    let (loaded, errors) = wasm_mgr.load_all(&wasm_paths);
459    for info in &loaded {
460        tracing::info!("WASM extension loaded: {} v{}", info.name, info.version);
461    }
462    for err in &errors {
463        tracing::warn!("WASM extension error: {}", err);
464    }
465
466    if wasm_mgr.is_empty() {
467        return None;
468    }
469
470    let mgr = std::sync::Arc::new(wasm_mgr);
471    for tool_def in mgr.all_tool_defs() {
472        let wasm_tool = crate::extensions::WasmTool::new(
473            mgr.clone(),
474            tool_def.name.clone(),
475            tool_def.description.clone(),
476            tool_def.schema.clone(),
477        );
478        tools.register(wasm_tool);
479    }
480    Some(mgr)
481}
482
483/// Register the model auto-router if configured in settings.
484fn register_router_provider(settings: &Settings) {
485    let global_dir = dirs::config_dir().unwrap_or_default().join("oxi");
486    let project_dir = std::env::current_dir().unwrap_or_default();
487
488    let store_cfg = match crate::store::router_config::load_router_config(&global_dir, &project_dir)
489    {
490        Some(cfg) => cfg,
491        None => {
492            tracing::debug!("No router config found — router/auto will not appear in model list");
493            return;
494        }
495    };
496
497    // Register router models only when configured.
498    oxi_sdk::register_model(oxi_sdk::Model::new(
499        "auto",
500        "Router (auto)".to_string(),
501        oxi_sdk::Api::AnthropicMessages,
502        "router",
503        "router://local",
504    ));
505
506    // Convert store config to AI config.
507    let mut ai_profiles = std::collections::HashMap::new();
508    for (name, sp) in store_cfg.profiles() {
509        fn parse_thinking(s: &Option<String>) -> Option<oxi_sdk::ThinkingLevel> {
510            s.as_ref().and_then(|s| match s.as_str() {
511                "off" => Some(oxi_sdk::ThinkingLevel::Off),
512                "minimal" => Some(oxi_sdk::ThinkingLevel::Minimal),
513                "low" => Some(oxi_sdk::ThinkingLevel::Low),
514                "medium" => Some(oxi_sdk::ThinkingLevel::Medium),
515                "high" => Some(oxi_sdk::ThinkingLevel::High),
516                "xhigh" => Some(oxi_sdk::ThinkingLevel::XHigh),
517                _ => None,
518            })
519        }
520        ai_profiles.insert(
521            name.clone(),
522            oxi_sdk::router::RouterProfile {
523                high: oxi_sdk::router::RoutedTierConfig {
524                    model: sp.high.model.clone(),
525                    thinking: parse_thinking(&sp.high.thinking),
526                    fallbacks: sp.high.fallbacks.clone(),
527                },
528                medium: oxi_sdk::router::RoutedTierConfig {
529                    model: sp.medium.model.clone(),
530                    thinking: parse_thinking(&sp.medium.thinking),
531                    fallbacks: sp.medium.fallbacks.clone(),
532                },
533                low: oxi_sdk::router::RoutedTierConfig {
534                    model: sp.low.model.clone(),
535                    thinking: parse_thinking(&sp.low.thinking),
536                    fallbacks: sp.low.fallbacks.clone(),
537                },
538            },
539        );
540    }
541    let ai_cfg = oxi_sdk::router::RouterConfig::with_pinning(
542        store_cfg.default_profile().to_string(),
543        store_cfg.classifier_model().map(String::from),
544        store_cfg.context_upgrade_threshold(),
545        store_cfg.max_session_budget(),
546        ai_profiles,
547        oxi_sdk::router::ScoringWeights {
548            structural: store_cfg.weights().structural,
549            behavioral: store_cfg.weights().behavioral,
550            context_budget: store_cfg.weights().context_budget,
551            vision: store_cfg.weights().vision,
552            message: store_cfg.weights().message,
553        },
554        store_cfg.pin_tier().and_then(|s| match s {
555            "high" => Some(oxi_sdk::router::RouterTier::High),
556            "medium" => Some(oxi_sdk::router::RouterTier::Medium),
557            "low" => Some(oxi_sdk::router::RouterTier::Low),
558            _ => None,
559        }),
560        store_cfg.phase_bias(),
561    );
562
563    oxi_sdk::router::register_router(&ai_cfg);
564
565    if let Some(profile) = settings.router_profile() {
566        tracing::info!("Router active with profile: {profile}");
567    }
568}
569
570/// Decide whether this run is the TUI (interactive) mode. Mirrors the
571/// dispatch in [`dispatch_run_mode`]: print / RPC / single-prompt are
572/// non-TUI. Used by [`build_app`] to pick the canonical liveness identity.
573fn is_tui_mode(args: &CliArgs) -> bool {
574    if matches!(args.mode.as_deref(), Some("json" | "rpc")) || args.print {
575        return false;
576    }
577    // prompt-only (no `--interactive` and a non-empty prompt) is non-TUI too;
578    // dispatch_run_mode sends it through main_dispatch::run_single_prompt.
579    // NOTE: must join the prompt Vec — clap's `default_value = ""` on the
580    // positional makes bare `oxi` yield `prompt == vec![""]` (non-empty Vec,
581    // empty join). Comparing the Vec directly would mis-classify the bare
582    // interactive launch as a single-prompt run.
583    let prompt = args.prompt.join(" ");
584    if !args.interactive && !prompt.is_empty() {
585        return false;
586    }
587    true
588}
589
590#[cfg(test)]
591mod tests {
592    use super::*;
593    use clap::Parser;
594
595    #[test]
596    fn rpc_mode_is_headless() {
597        let args = CliArgs::try_parse_from(["oxi", "--mode", "rpc"]).unwrap();
598        assert!(!is_tui_mode(&args));
599    }
600}