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(&tools, &cwd, args, &app.settings().disabled_tools);
160
161    // Discover and load WASM extensions.
162    let wasm_ext = load_wasm_extensions(&app, &cwd, &tools);
163    app.set_wasm_ext(wasm_ext);
164
165    // Handle --append-system-prompt.
166    if let Some(ref prompt_path) = args.append_system_prompt {
167        let content = std::fs::read_to_string(prompt_path)
168            .map_err(|e| anyhow::anyhow!("Failed to read system prompt file: {}", e))?;
169        app.agent().set_system_prompt(content);
170    }
171
172    Ok(app)
173}
174
175/// Dispatch the run mode: TUI / print / RPC, based on the CLI flags.
176pub async fn dispatch_run_mode(args: &CliArgs, app: crate::App) -> Result<i32> {
177    let prompt = args.prompt.join(" ");
178
179    if args.mode.as_deref() == Some("json") || args.print {
180        let mode = if args.mode.as_deref() == Some("json") {
181            crate::print_mode::PrintMode::Json
182        } else {
183            crate::print_mode::PrintMode::Text
184        };
185        let options = crate::print_mode::PrintModeOptions {
186            mode,
187            initial_message: if prompt.is_empty() {
188                None
189            } else {
190                Some(prompt)
191            },
192            messages: vec![],
193            no_stdin: args.print,
194            no_session: args.print || args.no_session,
195            quiet: args.print,
196            timeout: args.timeout,
197        };
198        return crate::print_mode::run_print_mode(&app, options).await;
199    }
200
201    if prompt.is_empty() || args.interactive {
202        if args.continue_session {
203            crate::tui::run_tui_interactive_with_continue(app, true).await?;
204        } else {
205            crate::tui::run_tui_interactive(app).await?;
206        }
207        return Ok(0);
208    }
209
210    crate::main_dispatch::run_single_prompt(app, &prompt).await?;
211    Ok(0)
212}
213
214/// Parse args, build the app, dispatch.
215pub async fn run_with_args(args: CliArgs) -> Result<i32> {
216    let app = build_app(&args).await?;
217    dispatch_run_mode(&args, app).await
218}
219
220// ─── Helpers (moved verbatim from main.rs) ─────────────────────────────
221
222/// Initialize file-based logging to `~/.cache/oxi/oxi.log`.
223///
224/// Reads `RUST_LOG` for filter (default: `debug`). Builds a
225/// `tracing_subscriber::EnvFilter` and writes to a `Mutex<File>` writer.
226pub fn init_logging() {
227    let log_dir = dirs::cache_dir()
228        .unwrap_or_else(|| std::path::PathBuf::from("/tmp"))
229        .join("oxi");
230    let _ = std::fs::create_dir_all(&log_dir);
231    let log_path = log_dir.join("oxi.log");
232
233    let log_filter = std::env::var("RUST_LOG").unwrap_or_else(|_| "debug".to_string());
234    let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
235        .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(&log_filter));
236
237    let log_file = std::fs::File::create(&log_path).expect("Failed to create log file");
238    let writer = std::sync::Mutex::new(log_file);
239
240    tracing_subscriber::fmt()
241        .with_env_filter(env_filter)
242        .with_writer(writer)
243        .with_target(true)
244        .with_thread_ids(true)
245        .with_ansi(false)
246        .init();
247
248    tracing::info!("Logging initialized, log file: {:?}", log_path);
249}
250
251/// Register custom OpenAI-compatible providers from settings and auto-fetch their models.
252fn register_custom_providers(settings: &Settings) {
253    let auth_storage = crate::store::auth_storage::shared_auth_storage();
254    for cp in &settings.custom_providers {
255        let api_key = auth_storage.get_api_key(&cp.name);
256        let api = cp.api.to_lowercase();
257
258        match api.as_str() {
259            "openai-completions" | "openai" => {
260                let provider =
261                    oxi_ai::OpenAiProvider::with_base_url_and_key(&cp.base_url, api_key.clone());
262                oxi_sdk::register_provider(&cp.name, provider);
263                tracing::info!(
264                    "Registered custom provider '{}' (openai-completions) -> {}",
265                    cp.name,
266                    cp.base_url
267                );
268            }
269            "openai-responses" | "responses" => {
270                let provider = oxi_sdk::OpenAiResponsesProvider::with_base_url_and_key(
271                    &cp.base_url,
272                    api_key.clone(),
273                );
274                oxi_sdk::register_provider(&cp.name, provider);
275                tracing::info!(
276                    "Registered custom provider '{}' (openai-responses) -> {}",
277                    cp.name,
278                    cp.base_url
279                );
280            }
281            _ => {
282                tracing::warn!(
283                    "Unknown API type '{}' for custom provider '{}'. Supported: openai-completions, openai-responses",
284                    cp.api,
285                    cp.name
286                );
287            }
288        }
289
290        fetch_and_register_models(cp, &api, &api_key);
291    }
292}
293
294/// Fetch models from a custom provider's /v1/models endpoint and register them.
295fn fetch_and_register_models(
296    cp: &crate::store::settings::CustomProvider,
297    api: &str,
298    api_key: &Option<String>,
299) {
300    if let Some(key) = api_key {
301        match oxi_sdk::fetch_models_blocking(&cp.base_url, key.as_str()) {
302            Ok(model_ids) => {
303                let count = model_ids.len();
304                for model_id in &model_ids {
305                    let api_type = match api {
306                        "openai-responses" | "responses" => oxi_sdk::Api::OpenAiResponses,
307                        _ => oxi_sdk::Api::OpenAiCompletions,
308                    };
309                    let model = oxi_sdk::Model {
310                        id: model_id.clone(),
311                        name: model_id.clone(),
312                        api: api_type,
313                        provider: cp.name.clone(),
314                        base_url: cp.base_url.clone(),
315                        reasoning: false,
316                        input: vec![oxi_sdk::InputModality::Text],
317                        cost: oxi_sdk::Cost::default(),
318                        context_window: 128_000,
319                        max_tokens: 8_192,
320                        headers: Default::default(),
321                        compat: None,
322                    };
323                    oxi_sdk::register_model(model);
324                }
325                tracing::info!(
326                    "[oxi] auto-fetched {} models from '{}' ({})",
327                    count,
328                    cp.name,
329                    cp.base_url
330                );
331            }
332            Err(e) => {
333                tracing::warn!(
334                    "[oxi] warning: failed to resolve models for {}: {}",
335                    cp.name,
336                    e
337                );
338            }
339        }
340    }
341}
342
343/// Register builtin tools with the agent, respecting --tools filter and disabled_tools.
344///
345/// Also transfers the [`McpManager`](oxi_agent::mcp::McpManager) reference from
346/// the built-in registry to the live agent registry. This matters because
347/// `register_arc` only copies the `Arc<dyn AgentTool>` — the manager field is
348/// stored separately and would otherwise be `None`, making `/mcp` show a
349/// "MCP is not configured" warning even though the `McpTool` is registered.
350fn register_builtin_tools(
351    tools: &oxi_agent::ToolRegistry,
352    cwd: &std::path::Path,
353    args: &CliArgs,
354    disabled_tools: &[String],
355) {
356    let builtin_registry = if let Some(ref tools_str) = args.tools {
357        let names: Vec<&str> = tools_str.split(',').map(|s| s.trim()).collect();
358        oxi_agent::ToolRegistry::with_selected_tools(cwd.to_path_buf(), &names)
359    } else {
360        oxi_agent::ToolRegistry::with_builtins_cwd(cwd.to_path_buf(), disabled_tools)
361    };
362    for name in builtin_registry.names() {
363        if let Some(tool) = builtin_registry.get(&name) {
364            tools.register_arc(tool);
365        }
366    }
367    // Propagate the MCP manager so the TUI's `/mcp` overlay can hot-reload
368    // configs, render live connection status, and so on.
369    if let Some(mgr) = builtin_registry.mcp_manager() {
370        tools.set_mcp_manager(mgr);
371    }
372}
373
374/// Discover and load WASM extensions, registering their tools.
375fn load_wasm_extensions(
376    app: &crate::App,
377    cwd: &std::path::Path,
378    tools: &oxi_agent::ToolRegistry,
379) -> Option<std::sync::Arc<crate::extensions::WasmExtensionManager>> {
380    if !app.settings().extensions_enabled {
381        return None;
382    }
383
384    let wasm_paths = crate::extensions::WasmExtensionManager::discover(cwd);
385    if wasm_paths.is_empty() {
386        return None;
387    }
388
389    let mut wasm_mgr = crate::extensions::WasmExtensionManager::new();
390    let (loaded, errors) = wasm_mgr.load_all(&wasm_paths);
391    for info in &loaded {
392        tracing::info!("WASM extension loaded: {} v{}", info.name, info.version);
393    }
394    for err in &errors {
395        tracing::warn!("WASM extension error: {}", err);
396    }
397
398    if wasm_mgr.is_empty() {
399        return None;
400    }
401
402    let mgr = std::sync::Arc::new(wasm_mgr);
403    for tool_def in mgr.all_tool_defs() {
404        let wasm_tool = crate::extensions::WasmTool::new(
405            mgr.clone(),
406            tool_def.name.clone(),
407            tool_def.description.clone(),
408            tool_def.schema.clone(),
409        );
410        tools.register(wasm_tool);
411    }
412    Some(mgr)
413}
414
415/// Register the model auto-router if configured in settings.
416fn register_router_provider(settings: &Settings) {
417    let global_dir = dirs::config_dir().unwrap_or_default().join("oxi");
418    let project_dir = std::env::current_dir().unwrap_or_default();
419
420    let store_cfg = match crate::store::router_config::load_router_config(&global_dir, &project_dir)
421    {
422        Some(cfg) => cfg,
423        None => {
424            tracing::debug!("No router config found — router/auto will not appear in model list");
425            return;
426        }
427    };
428
429    // Register router models only when configured.
430    oxi_sdk::register_model(oxi_sdk::Model::new(
431        "auto",
432        "Router (auto)".to_string(),
433        oxi_sdk::Api::AnthropicMessages,
434        "router",
435        "router://local",
436    ));
437
438    // Convert store config to AI config.
439    let mut ai_profiles = std::collections::HashMap::new();
440    for (name, sp) in store_cfg.profiles() {
441        fn parse_thinking(s: &Option<String>) -> Option<oxi_sdk::ThinkingLevel> {
442            s.as_ref().and_then(|s| match s.as_str() {
443                "off" => Some(oxi_sdk::ThinkingLevel::Off),
444                "minimal" => Some(oxi_sdk::ThinkingLevel::Minimal),
445                "low" => Some(oxi_sdk::ThinkingLevel::Low),
446                "medium" => Some(oxi_sdk::ThinkingLevel::Medium),
447                "high" => Some(oxi_sdk::ThinkingLevel::High),
448                "xhigh" => Some(oxi_sdk::ThinkingLevel::XHigh),
449                _ => None,
450            })
451        }
452        ai_profiles.insert(
453            name.clone(),
454            oxi_sdk::router::RouterProfile {
455                high: oxi_sdk::router::RoutedTierConfig {
456                    model: sp.high.model.clone(),
457                    thinking: parse_thinking(&sp.high.thinking),
458                    fallbacks: sp.high.fallbacks.clone(),
459                },
460                medium: oxi_sdk::router::RoutedTierConfig {
461                    model: sp.medium.model.clone(),
462                    thinking: parse_thinking(&sp.medium.thinking),
463                    fallbacks: sp.medium.fallbacks.clone(),
464                },
465                low: oxi_sdk::router::RoutedTierConfig {
466                    model: sp.low.model.clone(),
467                    thinking: parse_thinking(&sp.low.thinking),
468                    fallbacks: sp.low.fallbacks.clone(),
469                },
470            },
471        );
472    }
473    let ai_cfg = oxi_sdk::router::RouterConfig::with_pinning(
474        store_cfg.default_profile().to_string(),
475        store_cfg.classifier_model().map(String::from),
476        store_cfg.context_upgrade_threshold(),
477        store_cfg.max_session_budget(),
478        ai_profiles,
479        oxi_sdk::router::ScoringWeights {
480            structural: store_cfg.weights().structural,
481            behavioral: store_cfg.weights().behavioral,
482            context_budget: store_cfg.weights().context_budget,
483            vision: store_cfg.weights().vision,
484            message: store_cfg.weights().message,
485        },
486        store_cfg.pin_tier().and_then(|s| match s {
487            "high" => Some(oxi_sdk::router::RouterTier::High),
488            "medium" => Some(oxi_sdk::router::RouterTier::Medium),
489            "low" => Some(oxi_sdk::router::RouterTier::Low),
490            _ => None,
491        }),
492        store_cfg.phase_bias(),
493    );
494
495    oxi_sdk::router::register_router(&ai_cfg);
496
497    if let Some(profile) = settings.router_profile() {
498        tracing::info!("Router active with profile: {profile}");
499    }
500}
501
502/// Decide whether this run is the TUI (interactive) mode. Mirrors the
503/// dispatch in [`dispatch_run_mode`]: print / RPC / single-prompt are
504/// non-TUI. Used by [`build_app`] to pick the canonical liveness identity.
505fn is_tui_mode(args: &CliArgs) -> bool {
506    if args.mode.as_deref() == Some("json") || args.print {
507        return false;
508    }
509    // prompt-only (no `--interactive` and a non-empty prompt) is non-TUI too;
510    // dispatch_run_mode sends it through main_dispatch::run_single_prompt.
511    // NOTE: must join the prompt Vec — clap's `default_value = ""` on the
512    // positional makes bare `oxi` yield `prompt == vec![""]` (non-empty Vec,
513    // empty join). Comparing the Vec directly would mis-classify the bare
514    // interactive launch as a single-prompt run.
515    let prompt = args.prompt.join(" ");
516    if !args.interactive && !prompt.is_empty() {
517        return false;
518    }
519    true
520}