2025-08-05 11:31:11 -07:00
|
|
|
mod client;
|
|
|
|
|
mod parser;
|
|
|
|
|
mod pull;
|
|
|
|
|
mod url;
|
|
|
|
|
|
|
|
|
|
pub use client::OllamaClient;
|
2025-08-05 13:55:32 -07:00
|
|
|
use codex_core::config::Config;
|
2025-08-05 11:31:11 -07:00
|
|
|
pub use pull::CliProgressReporter;
|
|
|
|
|
pub use pull::PullEvent;
|
|
|
|
|
pub use pull::PullProgressReporter;
|
|
|
|
|
pub use pull::TuiProgressReporter;
|
|
|
|
|
|
|
|
|
|
/// Default OSS model to use when `--oss` is passed without an explicit `-m`.
|
|
|
|
|
pub const DEFAULT_OSS_MODEL: &str = "gpt-oss:20b";
|
|
|
|
|
|
|
|
|
|
/// Prepare the local OSS environment when `--oss` is selected.
|
|
|
|
|
///
|
|
|
|
|
/// - Ensures a local Ollama server is reachable.
|
|
|
|
|
/// - Checks if the model exists locally and pulls it if missing.
|
2025-08-05 13:55:32 -07:00
|
|
|
pub async fn ensure_oss_ready(config: &Config) -> std::io::Result<()> {
|
2025-08-05 11:31:11 -07:00
|
|
|
// Only download when the requested model is the default OSS model (or when -m is not provided).
|
2025-08-05 13:55:32 -07:00
|
|
|
let model = config.model.as_ref();
|
2025-08-05 11:31:11 -07:00
|
|
|
|
|
|
|
|
// Verify local Ollama is reachable.
|
2025-08-05 13:55:32 -07:00
|
|
|
let ollama_client = crate::OllamaClient::try_from_oss_provider(config).await?;
|
2025-08-05 11:31:11 -07:00
|
|
|
|
2025-08-05 13:55:32 -07:00
|
|
|
// If the model is not present locally, pull it.
|
|
|
|
|
match ollama_client.fetch_models().await {
|
|
|
|
|
Ok(models) => {
|
|
|
|
|
if !models.iter().any(|m| m == model) {
|
|
|
|
|
let mut reporter = crate::CliProgressReporter::new();
|
|
|
|
|
ollama_client
|
|
|
|
|
.pull_with_reporter(model, &mut reporter)
|
|
|
|
|
.await?;
|
2025-08-05 11:31:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-05 13:55:32 -07:00
|
|
|
Err(err) => {
|
|
|
|
|
// Not fatal; higher layers may still proceed and surface errors later.
|
|
|
|
|
tracing::warn!("Failed to query local models from Ollama: {}.", err);
|
|
|
|
|
}
|
2025-08-05 11:31:11 -07:00
|
|
|
}
|
|
|
|
|
|
2025-08-05 13:55:32 -07:00
|
|
|
Ok(())
|
2025-08-05 11:31:11 -07:00
|
|
|
}
|