refactor into providers

This commit is contained in:
pap
2025-08-04 17:48:06 +01:00
parent 6b6d2c5e00
commit 75eec73fcc
12 changed files with 823 additions and 800 deletions
+1 -14
View File
@@ -671,6 +671,7 @@ dependencies = [
"anyhow",
"assert_cmd",
"async-channel",
"async-stream",
"base64 0.22.1",
"bytes",
"chrono",
@@ -724,21 +725,17 @@ version = "0.0.0"
dependencies = [
"anyhow",
"assert_cmd",
"bytes",
"chrono",
"clap",
"codex-arg0",
"codex-common",
"codex-core",
"futures-util",
"owo-colors",
"predicates",
"reqwest",
"serde_json",
"shlex",
"tempfile",
"tokio",
"toml 0.8.23",
"tracing",
"tracing-subscriber",
]
@@ -848,7 +845,6 @@ version = "0.0.0"
dependencies = [
"anyhow",
"base64 0.22.1",
"bytes",
"chrono",
"clap",
"codex-ansi-escape",
@@ -859,7 +855,6 @@ dependencies = [
"codex-login",
"color-eyre",
"crossterm",
"futures-util",
"image",
"insta",
"lazy_static",
@@ -879,7 +874,6 @@ dependencies = [
"supports-color",
"textwrap 0.16.2",
"tokio",
"toml 0.8.23",
"tracing",
"tracing-appender",
"tracing-subscriber",
@@ -4821,7 +4815,6 @@ dependencies = [
"serde",
"serde_spanned 0.6.9",
"toml_datetime 0.6.11",
"toml_write",
"winnow",
]
@@ -4834,12 +4827,6 @@ dependencies = [
"winnow",
]
[[package]]
name = "toml_write"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
[[package]]
name = "toml_writer"
version = "1.0.2"
+1
View File
@@ -24,6 +24,7 @@ env-flags = "0.1.1"
eventsource-stream = "0.2.3"
fs2 = "0.4.3"
futures = "0.3"
async-stream = "0.3"
libc = "0.2.174"
mcp-types = { path = "../mcp-types" }
mime_guess = "2.0"
-124
View File
@@ -187,130 +187,6 @@ impl Config {
}
}
/// Ensure that when the `--ollama` flag is used, there is a configured
/// `model_providers.ollama` entry in `config.toml` and that an Ollama server is
/// reachable. If a provider entry is missing but a local server is running,
/// this function will create the provider entry with sensible defaults.
///
/// Returns `Ok(())` when the provider exists and the server is reachable. If
/// the server is not reachable, returns an error that callers can surface to
/// the end user (and should exit the process).
pub async fn ensure_ollama_provider_configured_and_running() -> std::io::Result<()> {
use std::time::Duration;
// Resolve the Codex home directory and parse the current config (if
// present).
let codex_home = find_codex_home()?;
let mut root_value = load_config_as_toml(&codex_home)?;
// Determine whether an ollama provider entry already exists and select a
// base URL to probe for the health check. If a provider entry exists and
// specifies a base_url, use it; otherwise fall back to the default.
let (has_ollama_entry, base_url_to_use) = match &root_value {
TomlValue::Table(tbl) => {
let model_providers = tbl.get("model_providers");
match model_providers {
Some(TomlValue::Table(mp_tbl)) => match mp_tbl.get("ollama") {
Some(TomlValue::Table(ollama_tbl)) => {
// Use the configured base_url when present; otherwise default.
let base_url = ollama_tbl
.get("base_url")
.and_then(|v| v.as_str())
.unwrap_or("http://localhost:11434/v1")
.to_string();
(true, base_url)
}
_ => (false, "http://localhost:11434/v1".to_string()),
},
_ => (false, "http://localhost:11434/v1".to_string()),
}
}
_ => (false, "http://localhost:11434/v1".to_string()),
};
// Select a URL to probe to determine if an Ollama server is reachable.
// Prefer the OpenAI-compatible endpoint when the base_url ends with /v1,
// fall back to the native Ollama tags endpoint otherwise.
let probe_path = if base_url_to_use.trim_end_matches('/').ends_with("/v1") {
"models"
} else {
"api/tags"
};
let probe_url = format!(
"{}/{}",
base_url_to_use.trim_end_matches('/'),
probe_path.trim_start_matches('/')
);
// Probe the server with a short timeout.
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(2))
.build()
.map_err(std::io::Error::other)?;
let resp = client.get(probe_url).send().await;
let server_ok = match resp {
Ok(r) => r.status().is_success(),
Err(_) => false,
};
if !server_ok {
return Err(std::io::Error::other(
"No running Ollama server detected. Please install/start Ollama: https://github.com/ollama/ollama?tab=readme-ov-file#ollama",
));
}
// If no provider entry existed, create one with sensible defaults and
// persist back to config.toml.
if !has_ollama_entry {
use toml::value::Table as TomlTable;
// Ensure we have a root table to operate on.
if !matches!(root_value, TomlValue::Table(_)) {
root_value = TomlValue::Table(TomlTable::new());
}
let root_tbl = match &mut root_value {
TomlValue::Table(tbl) => tbl,
_ => unreachable!(),
};
// Create or fetch the model_providers table.
let mp_val = root_tbl
.entry("model_providers".to_string())
.or_insert_with(|| TomlValue::Table(TomlTable::new()));
let mp_tbl = match mp_val {
TomlValue::Table(t) => t,
_ => {
*mp_val = TomlValue::Table(TomlTable::new());
match mp_val {
TomlValue::Table(t) => t,
_ => unreachable!(),
}
}
};
// Insert the ollama provider entry.
let mut ollama_tbl = TomlTable::new();
ollama_tbl.insert("name".to_string(), TomlValue::String("Ollama".to_string()));
ollama_tbl.insert(
"base_url".to_string(),
TomlValue::String("http://localhost:11434/v1".to_string()),
);
ollama_tbl.insert(
"wire_api".to_string(),
TomlValue::String("chat".to_string()),
);
mp_tbl.insert("ollama".to_string(), TomlValue::Table(ollama_tbl));
// Persist the updated TOML back to disk.
std::fs::create_dir_all(&codex_home)?;
let config_path = codex_home.join("config.toml");
let updated = toml::to_string_pretty(&root_value).map_err(std::io::Error::other)?;
std::fs::write(config_path, updated)?;
}
Ok(())
}
/// Read `CODEX_HOME/config.toml` and return it as a generic TOML value. Returns
/// an empty TOML table when the file does not exist.
fn load_config_as_toml(codex_home: &Path) -> std::io::Result<TomlValue> {
+1
View File
@@ -45,6 +45,7 @@ pub mod spawn;
pub mod turn_diff_tracker;
mod user_notification;
pub mod util;
pub mod providers;
pub use apply_patch::CODEX_APPLY_PATCH_ARG1;
pub use client_common::model_supports_reasoning_summaries;
+2
View File
@@ -0,0 +1,2 @@
pub mod ollama;
+637
View File
@@ -0,0 +1,637 @@
use crate::model_provider_info::ModelProviderInfo;
use crate::model_provider_info::WireApi;
use bytes::BytesMut;
use futures::StreamExt;
use futures::stream::BoxStream;
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::io;
use std::io::Write;
use std::path::Path;
pub const DEFAULT_BASE_URL: &str = "http://localhost:11434/v1";
pub const DEFAULT_WIRE_API: WireApi = WireApi::Chat;
pub const DEFAULT_PULL_ALLOWLIST: &[&str] = &["llama3.2:3b"];
/// Identify whether a base_url points at an OpenAI-compatible root (".../v1").
fn is_openai_compatible_base_url(base_url: &str) -> bool {
base_url.trim_end_matches('/').ends_with("/v1")
}
/// Convert a provider base_url into the native Ollama host root.
/// For example, "http://localhost:11434/v1" -> "http://localhost:11434".
pub fn base_url_to_host_root(base_url: &str) -> String {
let trimmed = base_url.trim_end_matches('/');
if trimmed.ends_with("/v1") {
trimmed
.trim_end_matches("/v1")
.trim_end_matches('/')
.to_string()
} else {
trimmed.to_string()
}
}
/// Compute the probe URL to verify if an Ollama server is reachable.
/// If the configured base is OpenAI-compatible (/v1), probe "models", otherwise
/// fall back to the native "/api/tags" endpoint.
pub fn probe_url_for_base(base_url: &str) -> String {
if is_openai_compatible_base_url(base_url) {
format!("{}/models", base_url.trim_end_matches('/'))
} else {
format!("{}/api/tags", base_url.trim_end_matches('/'))
}
}
/// Convenience helper to probe an Ollama server given a provider style base URL.
pub async fn probe_ollama_server(base_url: &str) -> io::Result<bool> {
let host_root = base_url_to_host_root(base_url);
let client = OllamaClient::from_host_root(host_root);
client.probe_server().await
}
/// Coordinator wrapper used by frontends when responding to `--ollama`.
///
/// - Probes the server using the configured base_url when present, otherwise
/// falls back to DEFAULT_BASE_URL.
/// - If the server is reachable, ensures an `[model_providers.ollama]` entry
/// exists in `config.toml` with sensible defaults.
/// - If no server is reachable, returns an error.
pub async fn ensure_configured_and_running() -> io::Result<()> {
use crate::config::find_codex_home;
use toml::Value as TomlValue;
let codex_home = find_codex_home()?;
let config_path = codex_home.join("config.toml");
// Try to read a configured base_url if present.
let base_url = match std::fs::read_to_string(&config_path) {
Ok(contents) => match toml::from_str::<TomlValue>(&contents) {
Ok(TomlValue::Table(root)) => root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.and_then(|v| v.as_table())
.and_then(|t| t.get("base_url"))
.and_then(|v| v.as_str())
.unwrap_or(DEFAULT_BASE_URL)
.to_string(),
_ => DEFAULT_BASE_URL.to_string(),
},
Err(_) => DEFAULT_BASE_URL.to_string(),
};
// Probe reachability.
let ok = probe_ollama_server(&base_url).await?;
if !ok {
return Err(io::Error::other(
"No running Ollama server detected. Please install/start Ollama: https://github.com/ollama/ollama?tab=readme-ov-file#ollama",
));
}
// Ensure provider entry exists with defaults.
let _ = ensure_ollama_provider_entry(&codex_home)?;
Ok(())
}
/// Events emitted while pulling a model from Ollama.
#[derive(Debug, Clone)]
pub enum PullEvent {
/// A human-readable status message (e.g., "verifying", "writing").
Status(String),
/// Byte-level progress update for a specific layer digest.
ChunkProgress {
digest: String,
total: Option<u64>,
completed: Option<u64>,
},
/// The pull finished successfully.
Success,
}
/// A simple observer for pull progress events. Implementations decide how to
/// render progress (CLI, TUI, logs, ...).
pub trait PullProgressReporter {
fn on_event(&mut self, event: &PullEvent) -> io::Result<()>;
}
/// A minimal CLI reporter that writes inline progress to stderr.
pub struct CliProgressReporter {
printed_header: bool,
last_line_len: usize,
last_completed_sum: u64,
last_instant: std::time::Instant,
totals_by_digest: HashMap<String, (u64, u64)>,
}
impl Default for CliProgressReporter {
fn default() -> Self {
Self::new()
}
}
impl CliProgressReporter {
pub fn new() -> Self {
Self {
printed_header: false,
last_line_len: 0,
last_completed_sum: 0,
last_instant: std::time::Instant::now(),
totals_by_digest: HashMap::new(),
}
}
}
impl PullProgressReporter for CliProgressReporter {
fn on_event(&mut self, event: &PullEvent) -> io::Result<()> {
let mut out = std::io::stderr();
match event {
PullEvent::Status(status) => {
// Avoid noisy manifest messages; otherwise show status inline.
if status.eq_ignore_ascii_case("pulling manifest") {
return Ok(());
}
let pad = self.last_line_len.saturating_sub(status.len());
let line = format!("\r{status}{}", " ".repeat(pad));
self.last_line_len = status.len();
out.write_all(line.as_bytes())?;
out.flush()
}
PullEvent::ChunkProgress {
digest,
total,
completed,
} => {
if let Some(t) = *total {
self.totals_by_digest
.entry(digest.clone())
.or_insert((0, 0))
.0 = t;
}
if let Some(c) = *completed {
self.totals_by_digest
.entry(digest.clone())
.or_insert((0, 0))
.1 = c;
}
let (sum_total, sum_completed) = self
.totals_by_digest
.values()
.fold((0u64, 0u64), |acc, (t, c)| (acc.0 + *t, acc.1 + *c));
if sum_total > 0 {
if !self.printed_header {
let gb = (sum_total as f64) / (1024.0 * 1024.0 * 1024.0);
let header = format!("Downloading model: total {gb:.2} GB\n");
out.write_all(b"\r\x1b[2K")?;
out.write_all(header.as_bytes())?;
self.printed_header = true;
}
let now = std::time::Instant::now();
let dt = now
.duration_since(self.last_instant)
.as_secs_f64()
.max(0.001);
let dbytes = sum_completed.saturating_sub(self.last_completed_sum) as f64;
let speed_mb_s = dbytes / (1024.0 * 1024.0) / dt;
self.last_completed_sum = sum_completed;
self.last_instant = now;
let done_gb = (sum_completed as f64) / (1024.0 * 1024.0 * 1024.0);
let total_gb = (sum_total as f64) / (1024.0 * 1024.0 * 1024.0);
let pct = (sum_completed as f64) * 100.0 / (sum_total as f64);
let text =
format!("{done_gb:.2}/{total_gb:.2} GB ({pct:.1}%) {speed_mb_s:.1} MB/s");
let pad = self.last_line_len.saturating_sub(text.len());
let line = format!("\r{text}{}", " ".repeat(pad));
self.last_line_len = text.len();
out.write_all(line.as_bytes())?;
out.flush()
} else {
Ok(())
}
}
PullEvent::Success => {
out.write_all(b"\n")?;
out.flush()
}
}
}
}
/// For now the TUI reporter delegates to the CLI reporter. This keeps UI and
/// CLI behavior aligned until a dedicated TUI integration is implemented.
pub struct TuiProgressReporter(CliProgressReporter);
impl Default for TuiProgressReporter {
fn default() -> Self {
Self(CliProgressReporter::new())
}
}
impl TuiProgressReporter {
pub fn new() -> Self {
Default::default()
}
}
impl PullProgressReporter for TuiProgressReporter {
fn on_event(&mut self, event: &PullEvent) -> io::Result<()> {
self.0.on_event(event)
}
}
/// Client for interacting with a local Ollama instance.
pub struct OllamaClient {
client: reqwest::Client,
host_root: String,
uses_openai_compat: bool,
}
impl OllamaClient {
/// Build a client from a provider definition. Falls back to the default
/// local URL if no base_url is configured.
pub fn from_provider(provider: &ModelProviderInfo) -> Self {
let base_url = provider
.base_url
.clone()
.unwrap_or_else(|| DEFAULT_BASE_URL.to_string());
let uses_openai_compat = is_openai_compatible_base_url(&base_url)
|| matches!(provider.wire_api, WireApi::Chat)
&& is_openai_compatible_base_url(&base_url);
let host_root = base_url_to_host_root(&base_url);
Self {
client: reqwest::Client::new(),
host_root,
uses_openai_compat,
}
}
/// Low-level constructor given a raw host root, e.g. "http://localhost:11434".
pub fn from_host_root(host_root: impl Into<String>) -> Self {
Self {
client: reqwest::Client::new(),
host_root: host_root.into(),
uses_openai_compat: false,
}
}
/// Probe whether the server is reachable by hitting the appropriate health endpoint.
pub async fn probe_server(&self) -> io::Result<bool> {
let url = if self.uses_openai_compat {
format!("{}/v1/models", self.host_root.trim_end_matches('/'))
} else {
format!("{}/api/tags", self.host_root.trim_end_matches('/'))
};
let resp = self.client.get(url).send().await;
Ok(matches!(resp, Ok(r) if r.status().is_success()))
}
/// Return the list of model names known to the local Ollama instance.
pub async fn fetch_models(&self) -> io::Result<Vec<String>> {
let tags_url = format!("{}/api/tags", self.host_root.trim_end_matches('/'));
let resp = self
.client
.get(tags_url)
.send()
.await
.map_err(io::Error::other)?;
if !resp.status().is_success() {
return Ok(Vec::new());
}
let val = resp.json::<JsonValue>().await.map_err(io::Error::other)?;
let names = val
.get("models")
.and_then(|m| m.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.get("name").and_then(|n| n.as_str()))
.map(|s| s.to_string())
.collect::<Vec<_>>()
})
.unwrap_or_default();
Ok(names)
}
/// Start a model pull and emit streaming events. The returned stream ends when
/// a Success event is observed or the server closes the connection.
pub async fn pull_model_stream(
&self,
model: &str,
) -> io::Result<BoxStream<'static, PullEvent>> {
let url = format!("{}/api/pull", self.host_root.trim_end_matches('/'));
let resp = self
.client
.post(url)
.json(&serde_json::json!({"model": model, "stream": true}))
.send()
.await
.map_err(io::Error::other)?;
if !resp.status().is_success() {
return Err(io::Error::other(format!(
"failed to start pull: HTTP {}",
resp.status()
)));
}
let mut stream = resp.bytes_stream();
let mut buf = BytesMut::new();
let _pending: VecDeque<PullEvent> = VecDeque::new();
// Using an async stream adaptor backed by unfold-like manual loop.
let s = async_stream::stream! {
while let Some(chunk) = stream.next().await {
match chunk {
Ok(bytes) => {
buf.extend_from_slice(&bytes);
while let Some(pos) = buf.iter().position(|b| *b == b'\n') {
let line = buf.split_to(pos + 1);
if let Ok(text) = std::str::from_utf8(&line) {
let text = text.trim();
if text.is_empty() { continue; }
if let Ok(value) = serde_json::from_str::<JsonValue>(text) {
if let Some(err_msg) = value.get("error").and_then(|e| e.as_str()) {
yield PullEvent::Status(format!("error: {err_msg}"));
return;
}
if let Some(status) = value.get("status").and_then(|s| s.as_str()) {
yield PullEvent::Status(status.to_string());
if status == "success" { yield PullEvent::Success; return; }
}
let digest = value.get("digest").and_then(|d| d.as_str()).unwrap_or("").to_string();
let total = value.get("total").and_then(|t| t.as_u64());
let completed = value.get("completed").and_then(|t| t.as_u64());
if total.is_some() || completed.is_some() {
yield PullEvent::ChunkProgress { digest, total, completed };
}
}
}
}
}
Err(_) => {
// Connection error: end the stream.
return;
}
}
}
};
Ok(Box::pin(s))
}
/// High-level helper to pull a model and drive a progress reporter.
pub async fn pull_with_reporter(
&self,
model: &str,
reporter: &mut dyn PullProgressReporter,
) -> io::Result<()> {
reporter.on_event(&PullEvent::Status(format!("Pulling model {model}...")))?;
let mut stream = self.pull_model_stream(model).await?;
while let Some(event) = stream.next().await {
reporter.on_event(&event)?;
if matches!(event, PullEvent::Success) {
break;
}
}
Ok(())
}
}
/// Read the list of models recorded under [model_providers.ollama].models.
pub fn read_ollama_models_list(config_path: &Path) -> Vec<String> {
match std::fs::read_to_string(config_path)
.ok()
.and_then(|s| toml::from_str::<toml::Value>(&s).ok())
{
Some(toml::Value::Table(root)) => root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.and_then(|v| v.as_table())
.and_then(|t| t.get("models"))
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect::<Vec<_>>()
})
.unwrap_or_default(),
_ => Vec::new(),
}
}
/// Convenience wrapper that returns the models list as an io::Result for callers
/// that want a uniform Result-based API.
pub fn read_config_models(config_path: &Path) -> io::Result<Vec<String>> {
Ok(read_ollama_models_list(config_path))
}
/// Overwrite the recorded models list under [model_providers.ollama].models.
pub fn write_ollama_models_list(config_path: &Path, models: &[String]) -> io::Result<()> {
use toml::value::Table as TomlTable;
let mut root_value = if let Ok(contents) = std::fs::read_to_string(config_path) {
toml::from_str::<toml::Value>(&contents).unwrap_or(toml::Value::Table(TomlTable::new()))
} else {
toml::Value::Table(TomlTable::new())
};
if !matches!(root_value, toml::Value::Table(_)) {
root_value = toml::Value::Table(TomlTable::new());
}
let root_tbl = match root_value.as_table_mut() {
Some(t) => t,
None => return Err(io::Error::other("invalid TOML root value")),
};
let mp_val = root_tbl
.entry("model_providers".to_string())
.or_insert_with(|| toml::Value::Table(TomlTable::new()));
if !mp_val.is_table() {
*mp_val = toml::Value::Table(TomlTable::new());
}
let mp_tbl = match mp_val.as_table_mut() {
Some(t) => t,
None => return Err(io::Error::other("invalid model_providers table")),
};
let ollama_val = mp_tbl
.entry("ollama".to_string())
.or_insert_with(|| toml::Value::Table(TomlTable::new()));
if !ollama_val.is_table() {
*ollama_val = toml::Value::Table(TomlTable::new());
}
let ollama_tbl = match ollama_val.as_table_mut() {
Some(t) => t,
None => return Err(io::Error::other("invalid ollama table")),
};
let arr = toml::Value::Array(
models
.iter()
.map(|m| toml::Value::String(m.clone()))
.collect(),
);
ollama_tbl.insert("models".to_string(), arr);
let updated =
toml::to_string_pretty(&root_value).map_err(|e| io::Error::other(e.to_string()))?;
if let Some(parent) = config_path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(config_path, updated)
}
/// Write models list via a uniform name expected by higher layers.
pub fn write_config_models(config_path: &Path, models: &[String]) -> io::Result<()> {
write_ollama_models_list(config_path, models)
}
/// Ensure `[model_providers.ollama]` exists with sensible defaults on disk.
/// Returns true if it created/updated the entry.
pub fn ensure_ollama_provider_entry(codex_home: &Path) -> io::Result<bool> {
use toml::value::Table as TomlTable;
let config_path = codex_home.join("config.toml");
let mut root_value = if let Ok(contents) = std::fs::read_to_string(&config_path) {
toml::from_str::<toml::Value>(&contents).unwrap_or(toml::Value::Table(TomlTable::new()))
} else {
toml::Value::Table(TomlTable::new())
};
if !matches!(root_value, toml::Value::Table(_)) {
root_value = toml::Value::Table(TomlTable::new());
}
let root_tbl = match root_value.as_table_mut() {
Some(t) => t,
None => return Err(io::Error::other("invalid TOML root")),
};
let mp_val = root_tbl
.entry("model_providers".to_string())
.or_insert_with(|| toml::Value::Table(TomlTable::new()));
if !mp_val.is_table() {
*mp_val = toml::Value::Table(TomlTable::new());
}
let mp_tbl = match mp_val.as_table_mut() {
Some(t) => t,
None => return Err(io::Error::other("invalid model_providers table")),
};
let mut changed = false;
let ollama_val = mp_tbl.entry("ollama".to_string()).or_insert_with(|| {
changed = true;
toml::Value::Table(TomlTable::new())
});
if !ollama_val.is_table() {
*ollama_val = toml::Value::Table(TomlTable::new());
changed = true;
}
if let Some(tbl) = ollama_val.as_table_mut() {
if !tbl.contains_key("name") {
tbl.insert(
"name".to_string(),
toml::Value::String("Ollama".to_string()),
);
changed = true;
}
if !tbl.contains_key("base_url") {
tbl.insert(
"base_url".to_string(),
toml::Value::String(DEFAULT_BASE_URL.to_string()),
);
changed = true;
}
if !tbl.contains_key("wire_api") {
tbl.insert(
"wire_api".to_string(),
toml::Value::String("chat".to_string()),
);
changed = true;
}
}
if changed {
if let Some(parent) = config_path.parent() {
std::fs::create_dir_all(parent)?;
}
let updated = toml::to_string_pretty(&root_value).map_err(io::Error::other)?;
std::fs::write(config_path, updated)?;
}
Ok(changed)
}
/// Alias name mirroring the refactor plan wording.
pub fn ensure_provider_entry_and_defaults(codex_home: &Path) -> io::Result<bool> {
ensure_ollama_provider_entry(codex_home)
}
/// Read whether the provider exists and how many models are recorded under it.
pub fn read_provider_state(config_path: &Path) -> (bool, usize) {
match std::fs::read_to_string(config_path)
.ok()
.and_then(|s| toml::from_str::<toml::Value>(&s).ok())
{
Some(toml::Value::Table(root)) => {
let provider_present = root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.map(|_| true)
.unwrap_or(false);
let models_count = root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.and_then(|v| v.as_table())
.and_then(|t| t.get("models"))
.and_then(|v| v.as_array())
.map(|arr| arr.len())
.unwrap_or(0);
(provider_present, models_count)
}
_ => (false, 0),
}
}
/// Ensure a model is available locally.
///
/// - If the model is already present, ensure it is recorded in config.toml.
/// - If missing and in the default allowlist, pull it with streaming progress
/// and record it in config.toml after success.
/// - If missing and not allowlisted, return an error.
pub async fn ensure_model_available(
model: &str,
client: &OllamaClient,
config_path: &Path,
reporter: &mut dyn PullProgressReporter,
) -> io::Result<()> {
let mut listed = read_ollama_models_list(config_path);
let available = client.fetch_models().await.unwrap_or_default();
if available.iter().any(|m| m == model) {
if !listed.iter().any(|m| m == model) {
listed.push(model.to_string());
listed.sort();
listed.dedup();
let _ = write_ollama_models_list(config_path, &listed);
}
return Ok(());
}
if !DEFAULT_PULL_ALLOWLIST.contains(&model) {
return Err(io::Error::other(format!(
"Model `{model}` not found locally and not in allowlist for automatic download."
)));
}
loop {
let _ = client.pull_with_reporter(model, reporter).await;
// After the stream completes (success or early EOF), check again.
let available = client.fetch_models().await.unwrap_or_default();
if available.iter().any(|m| m == model) {
break;
}
// Keep waiting for the model to finish downloading.
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
listed.push(model.to_string());
listed.sort();
listed.dedup();
let _ = write_ollama_models_list(config_path, &listed);
Ok(())
}
-4
View File
@@ -27,10 +27,6 @@ codex-common = { path = "../common", features = [
codex-core = { path = "../core" }
owo-colors = "4.2.0"
serde_json = "1"
reqwest = { version = "0.12", features = ["json"] }
toml = "0.8"
bytes = "1"
futures-util = "0.3"
shlex = "1.3.0"
tokio = { version = "1", features = [
"io-std",
+9 -305
View File
@@ -5,7 +5,6 @@ mod event_processor_with_json_output;
use std::io::IsTerminal;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;
use std::sync::Arc;
@@ -32,299 +31,11 @@ use tracing_subscriber::EnvFilter;
use crate::event_processor::CodexStatus;
use crate::event_processor::EventProcessor;
// ----- Ollama model discovery and pull helpers (CLI) -----
// These helpers are used when the user passes both --ollama and --model=<name>.
// We verify the requested model is recorded in config.toml or present on the
// local Ollama instance; if missing we will pull it (subject to an allowlist)
// and record it in config.toml without prompting.
async fn fetch_ollama_models(host_root: &str) -> Vec<String> {
let tags_url = format!("{host_root}/api/tags");
match reqwest::Client::new().get(&tags_url).send().await {
Ok(resp) if resp.status().is_success() => match resp.json::<serde_json::Value>().await {
Ok(val) => val
.get("models")
.and_then(|m| m.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.get("name").and_then(|n| n.as_str()))
.map(|s| s.to_string())
.collect::<Vec<_>>()
})
.unwrap_or_default(),
Err(_) => Vec::new(),
},
_ => Vec::new(),
}
}
fn read_ollama_models_list(config_path: &std::path::Path) -> Vec<String> {
match std::fs::read_to_string(config_path)
.ok()
.and_then(|s| toml::from_str::<toml::Value>(&s).ok())
{
Some(toml::Value::Table(root)) => root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.and_then(|v| v.as_table())
.and_then(|t| t.get("models"))
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect::<Vec<_>>()
})
.unwrap_or_default(),
_ => Vec::new(),
}
}
fn save_ollama_models(config_path: &std::path::Path, models: &[String]) -> std::io::Result<()> {
use toml::value::Table as TomlTable;
let mut root_value = if let Ok(contents) = std::fs::read_to_string(config_path) {
toml::from_str::<toml::Value>(&contents).unwrap_or(toml::Value::Table(TomlTable::new()))
} else {
toml::Value::Table(TomlTable::new())
};
if !matches!(root_value, toml::Value::Table(_)) {
root_value = toml::Value::Table(TomlTable::new());
}
let root_tbl = match root_value.as_table_mut() {
Some(t) => t,
None => return Err(std::io::Error::other("invalid TOML root value")),
};
let mp_val = root_tbl
.entry("model_providers".to_string())
.or_insert_with(|| toml::Value::Table(TomlTable::new()));
if !mp_val.is_table() {
*mp_val = toml::Value::Table(TomlTable::new());
}
let mp_tbl = match mp_val.as_table_mut() {
Some(t) => t,
None => return Err(std::io::Error::other("invalid model_providers table")),
};
let ollama_val = mp_tbl
.entry("ollama".to_string())
.or_insert_with(|| toml::Value::Table(TomlTable::new()));
if !ollama_val.is_table() {
*ollama_val = toml::Value::Table(TomlTable::new());
}
let ollama_tbl = match ollama_val.as_table_mut() {
Some(t) => t,
None => return Err(std::io::Error::other("invalid ollama table")),
};
let arr = toml::Value::Array(
models
.iter()
.map(|m| toml::Value::String(m.clone()))
.collect(),
);
ollama_tbl.insert("models".to_string(), arr);
let updated =
toml::to_string_pretty(&root_value).map_err(|e| std::io::Error::other(e.to_string()))?;
if let Some(parent) = config_path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(config_path, updated)
}
async fn pull_model_with_progress_cli(host_root: &str, model: &str) -> std::io::Result<()> {
use futures_util::StreamExt;
let url = format!("{host_root}/api/pull");
let client = reqwest::Client::new();
let resp = client
.post(&url)
.json(&serde_json::json!({"model": model, "stream": true}))
.send()
.await
.map_err(|e| std::io::Error::other(e.to_string()))?;
if !resp.status().is_success() {
return Err(std::io::Error::other(format!(
"failed to start pull: HTTP {}",
resp.status()
)));
}
let mut out = std::io::stderr();
let _ = out.write_all(format!("Pulling model {model}...\n").as_bytes());
let _ = out.flush();
let mut buf = bytes::BytesMut::new();
let mut totals: std::collections::HashMap<String, (u64, u64)> = Default::default();
let mut last_completed: u64 = 0;
let mut last_instant = std::time::Instant::now();
let mut stream = resp.bytes_stream();
let mut printed_header = false;
let mut saw_success = false;
let mut last_line_len: usize = 0;
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| std::io::Error::other(e.to_string()))?;
buf.extend_from_slice(&chunk);
while let Some(pos) = buf.iter().position(|b| *b == b'\n') {
let line = buf.split_to(pos + 1);
if let Ok(text) = std::str::from_utf8(&line) {
let text = text.trim();
if text.is_empty() {
continue;
}
if let Ok(value) = serde_json::from_str::<serde_json::Value>(text) {
if let Some(err_msg) = value.get("error").and_then(|e| e.as_str()) {
let _ = out.write_all(b"\n");
let _ = out.flush();
if err_msg.contains("file does not exist") {
return Err(std::io::Error::other("model not found"));
} else {
return Err(std::io::Error::other(format!(
"ollama pull error: {err_msg}"
)));
}
}
let status = value.get("status").and_then(|s| s.as_str()).unwrap_or("");
let digest = value
.get("digest")
.and_then(|d| d.as_str())
.unwrap_or("")
.to_string();
let total = value.get("total").and_then(|t| t.as_u64());
let completed = value.get("completed").and_then(|t| t.as_u64());
if let Some(t) = total {
let entry = totals.entry(digest.clone()).or_insert((t, 0));
entry.0 = t;
}
if let Some(c) = completed {
let entry = totals.entry(digest.clone()).or_insert((0, 0));
entry.1 = c;
}
let (sum_total, sum_completed) = totals
.values()
.fold((0u64, 0u64), |acc, v| (acc.0 + v.0, acc.1 + v.1));
if sum_total > 0 && !printed_header {
let gb = (sum_total as f64) / (1024.0 * 1024.0 * 1024.0);
let header = format!("Downloading {model}: total {gb:.2} GB\n");
// Clear any prior inline status text before printing header.
let _ = out.write_all(b"\r\x1b[2K");
let _ = out.write_all(header.as_bytes());
printed_header = true;
}
if sum_total > 0 {
let now = std::time::Instant::now();
let dt = now.duration_since(last_instant).as_secs_f64().max(0.001);
let dbytes = sum_completed.saturating_sub(last_completed) as f64;
let speed_mb_s = dbytes / (1024.0 * 1024.0) / dt;
last_completed = sum_completed;
last_instant = now;
let done_gb = (sum_completed as f64) / (1024.0 * 1024.0 * 1024.0);
let total_gb = (sum_total as f64) / (1024.0 * 1024.0 * 1024.0);
let pct = (sum_completed as f64) * 100.0 / (sum_total as f64);
let line_text = format!(
"{done_gb:.2}/{total_gb:.2} GB ({pct:.1}%) {speed_mb_s:.1} MB/s"
);
let pad = last_line_len.saturating_sub(line_text.len());
let line = format!(
"\r{text}{spaces}",
text = line_text,
spaces = " ".repeat(pad)
);
last_line_len = line_text.len();
let _ = out.write_all(line.as_bytes());
let _ = out.flush();
} else if !status.is_empty() && !status.eq_ignore_ascii_case("pulling manifest")
{
let line_text = status.to_string();
let pad = last_line_len.saturating_sub(line_text.len());
let line = format!(
"\r{text}{spaces}",
text = line_text,
spaces = " ".repeat(pad)
);
last_line_len = line_text.len();
let _ = out.write_all(line.as_bytes());
let _ = out.flush();
}
if status == "success" {
let _ = out.write_all(b"\n");
let _ = out.flush();
saw_success = true;
break;
}
}
}
}
if saw_success {
break;
}
}
if saw_success {
Ok(())
} else {
Err(std::io::Error::other(
"model pull did not complete (no success status)",
))
}
}
async fn ensure_ollama_model_available_cli(
model: &str,
host_root: &str,
config_path: &std::path::Path,
) -> std::io::Result<()> {
// 1) Always check the instance to ensure the model is actually available locally.
// This avoids relying solely on potentially stale entries in config.toml.
let mut listed = read_ollama_models_list(config_path);
let available = fetch_ollama_models(host_root).await;
if available.iter().any(|m| m == model) {
// Ensure the model is recorded in config.toml.
if !listed.iter().any(|m| m == model) {
listed.push(model.to_string());
listed.sort();
listed.dedup();
let _ = save_ollama_models(config_path, &listed);
}
return Ok(());
}
// 2) Pull if allowlisted
const ALLOWLIST: &[&str] = &["llama3.2:3b-instruct", "llama3.2:3b"];
if !ALLOWLIST.contains(&model) {
return Err(std::io::Error::other(format!(
"Model `{model}` not found locally and not in allowlist for automatic download."
)));
}
// Pull with progress; if the streaming connection ends before success, keep
// waiting/polling and retry the streaming request until the model appears or succeeds.
loop {
match pull_model_with_progress_cli(host_root, model).await {
Ok(()) => break,
Err(e) => {
// If the server reported the model manifest does not exist, surface a clear error.
if e.to_string().contains("model not found") {
return Err(std::io::Error::other("model not found"));
}
let available = fetch_ollama_models(host_root).await;
if available.iter().any(|m| m == model) {
break;
}
eprintln!("waiting for model to finish downloading...");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
continue;
}
}
}
listed.push(model.to_string());
listed.sort();
listed.dedup();
let _ = save_ollama_models(config_path, &listed);
Ok(())
}
// Shared Ollama helpers are centralized in codex_core::providers::ollama.
use codex_core::providers::ollama::CliProgressReporter;
use codex_core::providers::ollama::OllamaClient;
use codex_core::providers::ollama::ensure_configured_and_running;
use codex_core::providers::ollama::ensure_model_available;
pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> anyhow::Result<()> {
let Cli {
@@ -416,7 +127,7 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> any
// When the user opts into the Ollama provider via `--ollama`, ensure we
// have a configured provider entry and that a local server is running.
if ollama {
if let Err(e) = codex_core::config::ensure_ollama_provider_configured_and_running().await {
if let Err(e) = ensure_configured_and_running().await {
eprintln!("{e}");
std::process::exit(1);
}
@@ -455,18 +166,11 @@ pub async fn run_main(cli: Cli, codex_linux_sandbox_exe: Option<PathBuf>) -> any
// is present locally or pull it automatically (subject to allowlist).
if ollama && user_specified_model {
let model_name = config.model.clone();
let base_url = config
.model_provider
.base_url
.clone()
.unwrap_or_else(|| "http://localhost:11434/v1".to_string());
let host_root = base_url
.trim_end_matches('/')
.trim_end_matches("/v1")
.to_string();
let client = OllamaClient::from_provider(&config.model_provider);
let config_path = config.codex_home.join("config.toml");
let mut reporter = CliProgressReporter::new();
if let Err(e) =
ensure_ollama_model_available_cli(&model_name, &host_root, &config_path).await
ensure_model_available(&model_name, &client, &config_path, &mut reporter).await
{
eprintln!("{e}");
std::process::exit(1);
-3
View File
@@ -17,7 +17,6 @@ workspace = true
[dependencies]
anyhow = "1"
base64 = "0.22.1"
bytes = "1"
chrono = { version = "0.4", features = ["serde"] }
clap = { version = "4", features = ["derive"] }
codex-ansi-escape = { path = "../ansi-escape" }
@@ -32,7 +31,6 @@ codex-file-search = { path = "../file-search" }
codex-login = { path = "../login" }
color-eyre = "0.6.3"
crossterm = { version = "0.28.1", features = ["bracketed-paste"] }
futures-util = "0.3"
image = { version = "^0.25.6", default-features = false, features = ["jpeg"] }
lazy_static = "1"
mcp-types = { path = "../mcp-types" }
@@ -59,7 +57,6 @@ tokio = { version = "1", features = [
"rt-multi-thread",
"signal",
] }
toml = "0.8"
tracing = { version = "0.1.41", features = ["log"] }
tracing-appender = "0.2.3"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
+15 -350
View File
@@ -21,7 +21,6 @@ use std::fs::OpenOptions;
use std::io::Write;
use std::io::{self};
use std::path::PathBuf;
use toml as _;
use tracing::error;
use tracing_appender::non_blocking;
use tracing_subscriber::EnvFilter;
@@ -56,327 +55,11 @@ mod updates;
use color_eyre::owo_colors::OwoColorize;
pub use cli::Cli;
async fn fetch_ollama_models(host_root: &str) -> Vec<String> {
let tags_url = format!("{host_root}/api/tags");
match reqwest::Client::new().get(&tags_url).send().await {
Ok(resp) if resp.status().is_success() => match resp.json::<serde_json::Value>().await {
Ok(val) => val
.get("models")
.and_then(|m| m.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.get("name").and_then(|n| n.as_str()))
.map(|s| s.to_string())
.collect::<Vec<_>>()
})
.unwrap_or_default(),
Err(_) => Vec::new(),
},
_ => Vec::new(),
}
}
async fn pull_model_with_progress_tui(host_root: &str, model: &str) -> std::io::Result<()> {
use futures_util::StreamExt;
let url = format!("{host_root}/api/pull");
let client = reqwest::Client::new();
let resp = client
.post(&url)
.json(&serde_json::json!({"model": model, "stream": true}))
.send()
.await
.map_err(|e| std::io::Error::other(e.to_string()))?;
if !resp.status().is_success() {
return Err(std::io::Error::other(format!(
"failed to start pull: HTTP {}",
resp.status()
)));
}
let mut out = std::io::stderr();
// Print an immediate status line so the user sees activity even before totals are known.
let _ = out.write_all(format!("Pulling model {model}...\n").as_bytes());
let _ = out.flush();
let mut buf = bytes::BytesMut::new();
let mut totals: std::collections::HashMap<String, (u64, u64)> = Default::default();
let mut last_completed: u64 = 0;
let mut last_instant = std::time::Instant::now();
let mut stream = resp.bytes_stream();
// Print a header once we know total size.
let mut printed_header = false;
let mut saw_success = false;
let mut last_line_len: usize = 0;
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| std::io::Error::other(e.to_string()))?;
buf.extend_from_slice(&chunk);
// split by newlines
while let Some(pos) = buf.iter().position(|b| *b == b'\n') {
let line = buf.split_to(pos + 1);
if let Ok(text) = std::str::from_utf8(&line) {
let text = text.trim();
if text.is_empty() {
continue;
}
if let Ok(value) = serde_json::from_str::<serde_json::Value>(text) {
if let Some(err_msg) = value.get("error").and_then(|e| e.as_str()) {
let _ = out.write_all(b"\n");
let _ = out.flush();
if err_msg.contains("file does not exist") {
return Err(std::io::Error::other("model not found"));
} else {
return Err(std::io::Error::other(format!(
"ollama pull error: {err_msg}"
)));
}
}
let status = value.get("status").and_then(|s| s.as_str()).unwrap_or("");
let digest = value
.get("digest")
.and_then(|d| d.as_str())
.unwrap_or("")
.to_string();
let total = value.get("total").and_then(|t| t.as_u64());
let completed = value.get("completed").and_then(|t| t.as_u64());
if let Some(t) = total {
let entry = totals.entry(digest.clone()).or_insert((t, 0));
entry.0 = t;
}
if let Some(c) = completed {
let entry = totals.entry(digest.clone()).or_insert((0, 0));
entry.1 = c;
}
let (sum_total, sum_completed) = totals
.values()
.fold((0u64, 0u64), |acc, v| (acc.0 + v.0, acc.1 + v.1));
if sum_total > 0 && !printed_header {
let gb = (sum_total as f64) / (1024.0 * 1024.0 * 1024.0);
let header = format!("Downloading {model}: total {gb:.2} GB\n");
// Clear any prior inline status text before printing header.
let _ = out.write_all(b"\r\x1b[2K");
let _ = out.write_all(header.as_bytes());
printed_header = true;
}
if sum_total > 0 {
let now = std::time::Instant::now();
let dt = now.duration_since(last_instant).as_secs_f64().max(0.001);
let dbytes = sum_completed.saturating_sub(last_completed) as f64;
let speed_mb_s = dbytes / (1024.0 * 1024.0) / dt;
last_completed = sum_completed;
last_instant = now;
let done_gb = (sum_completed as f64) / (1024.0 * 1024.0 * 1024.0);
let total_gb = (sum_total as f64) / (1024.0 * 1024.0 * 1024.0);
let pct = (sum_completed as f64) * 100.0 / (sum_total as f64);
let line_text = format!(
"{done_gb:.2}/{total_gb:.2} GB ({pct:.1}%) {speed_mb_s:.1} MB/s"
);
let pad = last_line_len.saturating_sub(line_text.len());
let line = format!(
"\r{text}{spaces}",
text = line_text,
spaces = " ".repeat(pad)
);
last_line_len = line_text.len();
let _ = out.write_all(line.as_bytes());
let _ = out.flush();
} else if !status.is_empty() && !status.eq_ignore_ascii_case("pulling manifest")
{
// Print status lines like verifying/writing only once.
let line_text = status.to_string();
let pad = last_line_len.saturating_sub(line_text.len());
let line = format!(
"\r{text}{spaces}",
text = line_text,
spaces = " ".repeat(pad)
);
last_line_len = line_text.len();
let _ = out.write_all(line.as_bytes());
let _ = out.flush();
}
if status == "success" {
let _ = out.write_all(b"\n");
let _ = out.flush();
saw_success = true;
break;
}
}
}
}
if saw_success {
break;
}
}
if saw_success {
Ok(())
} else {
Err(std::io::Error::other(
"model pull did not complete (no success status)",
))
}
}
async fn ensure_ollama_model_available_tui(
model: &str,
host_root: &str,
config_path: &std::path::Path,
) -> std::io::Result<()> {
// 1) Always check the instance to ensure the model is actually available locally.
// This avoids relying solely on potentially stale entries in config.toml.
let mut listed = read_ollama_models_list(config_path);
let available = fetch_ollama_models(host_root).await;
if available.iter().any(|m| m == model) {
// Ensure the model is recorded in config.toml.
if !listed.iter().any(|m| m == model) {
listed.push(model.to_string());
listed.sort();
listed.dedup();
let _ = save_ollama_models(config_path, &listed);
}
return Ok(());
}
// 2) Pull if allowlisted.
const ALLOWLIST: &[&str] = &["llama3.2:3b", "llama3.2:3b-instruct"];
if !ALLOWLIST.contains(&model) {
return Err(std::io::Error::other(format!(
"Model `{model}` not found locally and not in allowlist for automatic download."
)));
}
// Pull with progress; if the streaming connection ends before success, keep
// waiting/polling and retry the streaming request until the model appears or succeeds.
loop {
match pull_model_with_progress_tui(host_root, model).await {
Ok(()) => break,
Err(e) => {
// If Ollama reports the model manifest does not exist, surface a clear error.
if e.to_string().contains("model not found") {
return Err(std::io::Error::other("model not found"));
}
let available = fetch_ollama_models(host_root).await;
if available.iter().any(|m| m == model) {
break;
}
let mut out = std::io::stderr();
let _ = out.write_all(b"\nwaiting for model to finish downloading...\n");
let _ = out.flush();
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
continue;
}
}
}
listed.push(model.to_string());
listed.sort();
listed.dedup();
let _ = save_ollama_models(config_path, &listed);
Ok(())
}
fn read_ollama_models_list(config_path: &std::path::Path) -> Vec<String> {
match std::fs::read_to_string(config_path)
.ok()
.and_then(|s| toml::from_str::<toml::Value>(&s).ok())
{
Some(toml::Value::Table(root)) => root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.and_then(|v| v.as_table())
.and_then(|t| t.get("models"))
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect::<Vec<_>>()
})
.unwrap_or_default(),
_ => Vec::new(),
}
}
fn read_ollama_config_state(config_path: &std::path::Path) -> (bool, usize) {
match std::fs::read_to_string(config_path)
.ok()
.and_then(|s| toml::from_str::<toml::Value>(&s).ok())
{
Some(toml::Value::Table(root)) => {
let provider_present = root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.map(|_| true)
.unwrap_or(false);
let models_count = root
.get("model_providers")
.and_then(|v| v.as_table())
.and_then(|t| t.get("ollama"))
.and_then(|v| v.as_table())
.and_then(|t| t.get("models"))
.and_then(|v| v.as_array())
.map(|arr| arr.len())
.unwrap_or(0);
(provider_present, models_count)
}
_ => (false, 0),
}
}
fn save_ollama_models(config_path: &std::path::Path, models: &[String]) -> std::io::Result<()> {
use toml::value::Table as TomlTable;
let mut root_value = if let Ok(contents) = std::fs::read_to_string(config_path) {
toml::from_str::<toml::Value>(&contents).unwrap_or(toml::Value::Table(TomlTable::new()))
} else {
toml::Value::Table(TomlTable::new())
};
if !matches!(root_value, toml::Value::Table(_)) {
root_value = toml::Value::Table(TomlTable::new());
}
let root_tbl = match root_value.as_table_mut() {
Some(t) => t,
None => return Err(std::io::Error::other("invalid TOML root value")),
};
let mp_val = root_tbl
.entry("model_providers".to_string())
.or_insert_with(|| toml::Value::Table(TomlTable::new()));
if !mp_val.is_table() {
*mp_val = toml::Value::Table(TomlTable::new());
}
let mp_tbl = match mp_val.as_table_mut() {
Some(t) => t,
None => return Err(std::io::Error::other("invalid model_providers table")),
};
let ollama_val = mp_tbl
.entry("ollama".to_string())
.or_insert_with(|| toml::Value::Table(TomlTable::new()));
if !ollama_val.is_table() {
*ollama_val = toml::Value::Table(TomlTable::new());
}
let ollama_tbl = match ollama_val.as_table_mut() {
Some(t) => t,
None => return Err(std::io::Error::other("invalid ollama table")),
};
let arr = toml::Value::Array(
models
.iter()
.map(|m| toml::Value::String(m.clone()))
.collect(),
);
ollama_tbl.insert("models".to_string(), arr);
let updated =
toml::to_string_pretty(&root_value).map_err(|e| std::io::Error::other(e.to_string()))?;
if let Some(parent) = config_path.parent() {
std::fs::create_dir_all(parent)?;
}
std::fs::write(config_path, updated)
}
// Centralized Ollama helpers from core
use codex_core::providers::ollama::OllamaClient;
use codex_core::providers::ollama::TuiProgressReporter;
use codex_core::providers::ollama::{ensure_configured_and_running, ensure_model_available};
use codex_core::providers::ollama::{read_config_models, read_provider_state, write_config_models};
fn print_inline_message_no_models(
host_root: &str,
@@ -535,7 +218,7 @@ fn run_inline_models_picker(
.filter_map(|(name, sel)| if *sel { Some(name) } else { None })
.collect();
let _ = save_ollama_models(config_path, &chosen);
let _ = write_config_models(config_path, &chosen);
print_config_summary_after_save(config_path, provider_was_present_before, Some(chosen.len()))
}
@@ -603,7 +286,7 @@ fn print_config_summary_after_save(
)?;
}
if let Some(after) = models_count_after {
let names = read_ollama_models_list(config_path);
let names = read_config_models(config_path).unwrap_or_default();
if names.is_empty() {
out.write_all(format!("\rmodels: recorded {after}\n\n").as_bytes())?;
} else {
@@ -642,7 +325,7 @@ pub async fn run_main(
let provider_was_present_before = if cli.ollama {
let codex_home = codex_core::config::find_codex_home()?;
let config_path = codex_home.join("config.toml");
let (p, _m) = read_ollama_config_state(&config_path);
let (p, _m) = read_provider_state(&config_path);
p
} else {
false
@@ -653,9 +336,7 @@ pub async fn run_main(
// local server is reachable and ensure a provider entry exists in
// config.toml. Exit early with a helpful message otherwise.
if cli.ollama {
if let Err(e) =
codex_core::config::ensure_ollama_provider_configured_and_running().await
{
if let Err(e) = ensure_configured_and_running().await {
#[allow(clippy::print_stderr)]
{
eprintln!("{e}");
@@ -717,8 +398,10 @@ pub async fn run_main(
if let Some(ref model_name) = cli.model {
// Explicit model requested: ensure it is available locally without prompting.
let client = OllamaClient::from_provider(&config.model_provider);
let mut reporter = TuiProgressReporter::new();
if let Err(e) =
ensure_ollama_model_available_tui(model_name, &host_root, &config_path).await
ensure_model_available(model_name, &client, &config_path, &mut reporter).await
{
let mut out = std::io::stderr();
let _ = out.write_all(format!("{e}\n").as_bytes());
@@ -729,29 +412,11 @@ pub async fn run_main(
// No specific model was requested: fetch available models from the local instance
// and, if they differ from what is listed in config.toml, display a minimal
// inline selection UI before launching the TUI.
let tags_url = format!("{host_root}/api/tags");
let available_models: Vec<String> =
match reqwest::Client::new().get(&tags_url).send().await {
Ok(resp) if resp.status().is_success() => {
match resp.json::<serde_json::Value>().await {
Ok(val) => val
.get("models")
.and_then(|m| m.as_array())
.map(|arr| {
arr.iter()
.filter_map(|v| v.get("name").and_then(|n| n.as_str()))
.map(|s| s.to_string())
.collect::<Vec<_>>()
})
.unwrap_or_default(),
Err(_) => Vec::new(),
}
}
_ => Vec::new(),
};
let client = OllamaClient::from_provider(&config.model_provider);
let available_models: Vec<String> = client.fetch_models().await.unwrap_or_default();
// Read existing models in config.
let existing_models: Vec<String> = read_ollama_models_list(&config_path);
let existing_models: Vec<String> = read_config_models(&config_path).unwrap_or_default();
if available_models.is_empty() {
// Inform the user and continue launching the TUI.
+2
View File
@@ -0,0 +1,2 @@
pub mod ollama_model_picker;
@@ -0,0 +1,155 @@
use crossterm::event::{KeyCode, KeyEvent};
use ratatui::buffer::Buffer;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, BorderType, Borders, Paragraph, WidgetRef};
use ratatui::prelude::Widget;
use std::path::PathBuf;
pub enum PickerOutcome {
Submit(Vec<String>),
Cancel,
None,
}
pub struct OllamaModelPickerScreen {
pub host_root: String,
pub config_path: PathBuf,
available: Vec<String>,
selected: Vec<bool>,
cursor: usize,
pub loading: bool,
}
impl OllamaModelPickerScreen {
pub fn new(host_root: String, config_path: PathBuf, preselected: Vec<String>) -> Self {
Self {
host_root,
config_path,
available: Vec::new(),
selected: preselected.into_iter().map(|_| false).collect(),
cursor: 0,
loading: true,
}
}
pub fn desired_height(&self, _width: u16) -> u16 {
18u16
}
pub fn update_available(&mut self, available: Vec<String>) {
// Build selection state using existing selected names where possible.
let prev_selected_names: Vec<String> = self
.available
.iter()
.cloned()
.zip(self.selected.iter().cloned())
.filter_map(|(n, sel)| if sel { Some(n) } else { None })
.collect();
self.available = available.clone();
self.selected = available
.iter()
.map(|n| prev_selected_names.iter().any(|p| p == n))
.collect();
if self.cursor >= self.available.len() {
self.cursor = self.available.len().saturating_sub(1);
}
self.loading = false;
}
pub fn handle_key_event(&mut self, key: KeyEvent) -> PickerOutcome {
match key.code {
KeyCode::Up | KeyCode::Char('k') => {
if self.cursor > 0 { self.cursor -= 1; }
PickerOutcome::None
}
KeyCode::Down | KeyCode::Char('j') => {
if self.cursor + 1 < self.available.len() { self.cursor += 1; }
PickerOutcome::None
}
KeyCode::Char(' ') => {
if let Some(s) = self.selected.get_mut(self.cursor) {
*s = !*s;
}
PickerOutcome::None
}
KeyCode::Char('a') => {
let all = self.selected.iter().all(|s| *s);
self.selected.fill(!all);
PickerOutcome::None
}
KeyCode::Enter => {
let chosen: Vec<String> = self
.available
.iter()
.cloned()
.zip(self.selected.iter().cloned())
.filter_map(|(n, sel)| if sel { Some(n) } else { None })
.collect();
PickerOutcome::Submit(chosen)
}
KeyCode::Esc | KeyCode::Char('q') => PickerOutcome::Cancel,
_ => PickerOutcome::None,
}
}
}
impl WidgetRef for &OllamaModelPickerScreen {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
const MIN_WIDTH: u16 = 40;
const MIN_HEIGHT: u16 = 15;
let popup_width = std::cmp::max(MIN_WIDTH, (area.width as f32 * 0.7) as u16);
let popup_height = std::cmp::max(MIN_HEIGHT, (area.height as f32 * 0.6) as u16);
let popup_x = area.x + (area.width.saturating_sub(popup_width)) / 2;
let popup_y = area.y + (area.height.saturating_sub(popup_height)) / 2;
let popup_area = Rect::new(popup_x, popup_y, popup_width, popup_height);
let popup_block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.title(Span::styled(
"Select Ollama models",
Style::default().add_modifier(Modifier::BOLD),
));
let inner = popup_block.inner(popup_area);
popup_block.render(popup_area, buf);
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(3), Constraint::Length(3)])
.split(inner);
// Header
let header = format!("endpoint: {}\n↑/↓ move, space toggle, 'a' (un)select all, enter confirm, 'q' skip", self.host_root);
Paragraph::new(header).alignment(Alignment::Left).render(chunks[0], buf);
// Body: list of models or a loading message
if self.loading {
Paragraph::new("discovering models...").alignment(Alignment::Center).render(chunks[1], buf);
} else if self.available.is_empty() {
Paragraph::new("No models discovered on the local Ollama instance.")
.alignment(Alignment::Center)
.render(chunks[1], buf);
} else {
// Render each line manually with highlight for cursor.
let mut lines: Vec<Line> = Vec::with_capacity(self.available.len());
for (i, name) in self.available.iter().enumerate() {
let mark = if self.selected.get(i).copied().unwrap_or(false) { "[x]" } else { "[ ]" };
let content = format!("{mark} {name}");
if i == self.cursor {
lines.push(Line::from(content).style(Style::default().add_modifier(Modifier::REVERSED)));
} else {
lines.push(Line::from(content));
}
}
Paragraph::new(lines).render(chunks[1], buf);
}
// Footer/help
Paragraph::new("press Enter to save, 'q' to continue without changes")
.alignment(Alignment::Center)
.render(chunks[2], buf);
}
}