Base: codex-main originale (prima del fork aKode)
This commit is contained in:
627
codex-rs/ollama/src/client.rs
Normal file
627
codex-rs/ollama/src/client.rs
Normal file
@@ -0,0 +1,627 @@
|
||||
use futures::StreamExt;
|
||||
use futures::stream::BoxStream;
|
||||
use semver::Version;
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::collections::VecDeque;
|
||||
use std::io;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::line_buffer::LineBuffer;
|
||||
use crate::parser::pull_events_from_value;
|
||||
use crate::pull::PullEvent;
|
||||
use crate::pull::PullProgressReporter;
|
||||
use crate::url::base_url_to_host_root;
|
||||
use crate::url::is_openai_compatible_base_url;
|
||||
use codex_core::config::Config;
|
||||
use codex_http_client::ClientRouteClass;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
#[cfg(test)]
|
||||
use codex_http_client::OutboundProxyPolicy;
|
||||
use codex_http_client::RouteAwareClientPool;
|
||||
use codex_http_client::RouteAwareRequestError;
|
||||
use codex_model_provider_info::ModelProviderInfo;
|
||||
use codex_model_provider_info::OLLAMA_OSS_PROVIDER_ID;
|
||||
#[cfg(test)]
|
||||
use codex_model_provider_info::WireApi;
|
||||
#[cfg(test)]
|
||||
use codex_model_provider_info::create_oss_provider_with_base_url;
|
||||
|
||||
const OLLAMA_CONNECTION_ERROR: &str = "No running Ollama server detected. Start it with: `ollama serve` (after installing). Install instructions: https://github.com/ollama/ollama?tab=readme-ov-file#ollama";
|
||||
const OLLAMA_CONNECTION_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
/// Client for interacting with a local Ollama instance.
|
||||
pub struct OllamaClient {
|
||||
client: RouteAwareClientPool,
|
||||
host_root: String,
|
||||
uses_openai_compat: bool,
|
||||
}
|
||||
|
||||
impl OllamaClient {
|
||||
/// Construct a client for the built‑in open‑source ("oss") model provider
|
||||
/// and verify that a local Ollama server is reachable. If no server is
|
||||
/// detected, returns an error with helpful installation/run instructions.
|
||||
pub async fn try_from_oss_provider(config: &Config) -> io::Result<Self> {
|
||||
// Note that we must look up the provider from the Config to ensure that
|
||||
// any overrides the user has in their config.toml are taken into
|
||||
// account.
|
||||
let provider = config
|
||||
.model_providers
|
||||
.get(OLLAMA_OSS_PROVIDER_ID)
|
||||
.ok_or_else(|| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::NotFound,
|
||||
format!("Built-in provider {OLLAMA_OSS_PROVIDER_ID} not found",),
|
||||
)
|
||||
})?;
|
||||
|
||||
Self::try_from_provider(provider, config.http_client_factory()).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
async fn try_from_provider_with_base_url(base_url: &str) -> io::Result<Self> {
|
||||
let provider = create_oss_provider_with_base_url(base_url, WireApi::Responses);
|
||||
Self::try_from_provider(
|
||||
&provider,
|
||||
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Build a client from a provider definition and verify the server is reachable.
|
||||
pub(crate) async fn try_from_provider(
|
||||
provider: &ModelProviderInfo,
|
||||
http_client_factory: HttpClientFactory,
|
||||
) -> io::Result<Self> {
|
||||
#![expect(clippy::expect_used)]
|
||||
let base_url = provider
|
||||
.base_url
|
||||
.as_ref()
|
||||
.expect("oss provider must have a base_url");
|
||||
let uses_openai_compat = is_openai_compatible_base_url(base_url);
|
||||
let host_root = base_url_to_host_root(base_url);
|
||||
let client = RouteAwareClientPool::with_connect_timeout(
|
||||
http_client_factory,
|
||||
ClientRouteClass::Other,
|
||||
OLLAMA_CONNECTION_TIMEOUT,
|
||||
)
|
||||
.with_legacy_custom_ca_fallback();
|
||||
let client = Self {
|
||||
client,
|
||||
host_root,
|
||||
uses_openai_compat,
|
||||
};
|
||||
client.probe_server().await?;
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
/// Probe whether the server is reachable by hitting the appropriate health endpoint.
|
||||
async fn probe_server(&self) -> io::Result<()> {
|
||||
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
|
||||
.map_err(|error| match error {
|
||||
RouteAwareRequestError::Route(error) => {
|
||||
tracing::warn!(error = %error, "Failed to initialize Ollama HTTP transport");
|
||||
io::Error::other(error)
|
||||
}
|
||||
error => {
|
||||
tracing::warn!(error = ?error, "Failed to connect to Ollama server");
|
||||
io::Error::other(OLLAMA_CONNECTION_ERROR)
|
||||
}
|
||||
})?;
|
||||
if resp.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
tracing::warn!(
|
||||
"Failed to probe server at {}: HTTP {}",
|
||||
self.host_root,
|
||||
resp.status()
|
||||
);
|
||||
Err(io::Error::other(OLLAMA_CONNECTION_ERROR))
|
||||
}
|
||||
}
|
||||
|
||||
/// 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(str::to_string)
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
Ok(names)
|
||||
}
|
||||
|
||||
/// Query the server for its version string, returning `None` when unavailable.
|
||||
pub async fn fetch_version(&self) -> io::Result<Option<Version>> {
|
||||
let version_url = format!("{}/api/version", self.host_root.trim_end_matches('/'));
|
||||
let resp = self
|
||||
.client
|
||||
.get(version_url)
|
||||
.send()
|
||||
.await
|
||||
.map_err(io::Error::other)?;
|
||||
if !resp.status().is_success() {
|
||||
return Ok(None);
|
||||
}
|
||||
let val = resp.json::<JsonValue>().await.map_err(io::Error::other)?;
|
||||
let Some(version_str) = val.get("version").and_then(|v| v.as_str()).map(str::trim) else {
|
||||
return Ok(None);
|
||||
};
|
||||
let normalized = version_str.trim_start_matches('v');
|
||||
match Version::parse(normalized) {
|
||||
Ok(version) => Ok(Some(version)),
|
||||
Err(err) => {
|
||||
tracing::warn!("Failed to parse Ollama version `{version_str}`: {err}");
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 = LineBuffer::default();
|
||||
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(line) = buf.take_line() {
|
||||
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) {
|
||||
for ev in pull_events_from_value(&value) { yield ev; }
|
||||
if let Some(err_msg) = value.get("error").and_then(|e| e.as_str()) {
|
||||
yield PullEvent::Error(err_msg.to_string());
|
||||
return;
|
||||
}
|
||||
if let Some(status) = value.get("status").and_then(|s| s.as_str())
|
||||
&& status == "success" { yield PullEvent::Success; return; }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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)?;
|
||||
match event {
|
||||
PullEvent::Success => {
|
||||
return Ok(());
|
||||
}
|
||||
PullEvent::Error(err) => {
|
||||
// Empirically, ollama returns a 200 OK response even when
|
||||
// the output stream includes an error message. Verify with:
|
||||
//
|
||||
// `curl -i http://localhost:11434/api/pull -d '{ "model": "foobarbaz" }'`
|
||||
//
|
||||
// As such, we have to check the event stream, not the
|
||||
// HTTP response status, to determine whether to return Err.
|
||||
return Err(io::Error::other(format!("Pull failed: {err}")));
|
||||
}
|
||||
PullEvent::ChunkProgress { .. } | PullEvent::Status(_) => {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(io::Error::other(
|
||||
"Pull stream ended unexpectedly without success.",
|
||||
))
|
||||
}
|
||||
|
||||
/// Low-level constructor given a raw host root, e.g. "http://localhost:11434".
|
||||
#[cfg(test)]
|
||||
fn from_host_root(host_root: impl Into<String>) -> Self {
|
||||
let client = RouteAwareClientPool::with_connect_timeout(
|
||||
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
ClientRouteClass::Other,
|
||||
OLLAMA_CONNECTION_TIMEOUT,
|
||||
);
|
||||
Self {
|
||||
client,
|
||||
host_root: host_root.into(),
|
||||
uses_openai_compat: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use assert_matches::assert_matches;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
// Happy-path tests using a mock HTTP server; skip if sandbox network is disabled.
|
||||
#[tokio::test]
|
||||
async fn test_fetch_models_happy_path() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} is set; skipping test_fetch_models_happy_path",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let server = wiremock::MockServer::start().await;
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/tags"))
|
||||
.respond_with(
|
||||
wiremock::ResponseTemplate::new(200).set_body_raw(
|
||||
serde_json::json!({
|
||||
"models": [ {"name": "llama3.2:3b"}, {"name":"mistral"} ]
|
||||
})
|
||||
.to_string(),
|
||||
"application/json",
|
||||
),
|
||||
)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let client = OllamaClient::from_host_root(server.uri());
|
||||
let models = client.fetch_models().await.expect("fetch models");
|
||||
assert!(models.contains(&"llama3.2:3b".to_string()));
|
||||
assert!(models.contains(&"mistral".to_string()));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_fetch_version() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} is set; skipping test_fetch_version",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let server = wiremock::MockServer::start().await;
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/tags"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200).set_body_raw(
|
||||
serde_json::json!({ "models": [] }).to_string(),
|
||||
"application/json",
|
||||
))
|
||||
.mount(&server)
|
||||
.await;
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/version"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200).set_body_raw(
|
||||
serde_json::json!({ "version": "0.14.1" }).to_string(),
|
||||
"application/json",
|
||||
))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let client = OllamaClient::try_from_provider_with_base_url(server.uri().as_str())
|
||||
.await
|
||||
.expect("client");
|
||||
|
||||
let version = client.fetch_version().await.expect("version fetch");
|
||||
assert_eq!(version, Some(Version::new(0, 14, 1)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_pull_model_stream_parses_large_json_lines() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} set; skipping test_pull_model_stream_parses_large_json_lines",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let server = wiremock::MockServer::start().await;
|
||||
let body = format!(
|
||||
"{}\n{}\n",
|
||||
serde_json::json!({
|
||||
"status": "pulling layers",
|
||||
"padding": "x".repeat(128 * 1024),
|
||||
}),
|
||||
serde_json::json!({"status": "complete"}),
|
||||
);
|
||||
wiremock::Mock::given(wiremock::matchers::method("POST"))
|
||||
.and(wiremock::matchers::path("/api/pull"))
|
||||
.respond_with(
|
||||
wiremock::ResponseTemplate::new(200).set_body_raw(body, "application/x-ndjson"),
|
||||
)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let client = OllamaClient::from_host_root(server.uri());
|
||||
let events = client
|
||||
.pull_model_stream("test-model")
|
||||
.await
|
||||
.expect("start pull stream")
|
||||
.collect::<Vec<_>>()
|
||||
.await;
|
||||
|
||||
assert_matches!(
|
||||
events.as_slice(),
|
||||
[
|
||||
PullEvent::Status(pulling),
|
||||
PullEvent::Status(complete),
|
||||
] if pulling == "pulling layers" && complete == "complete"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_probe_server_happy_path_openai_compat_and_native() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} set; skipping test_probe_server_happy_path_openai_compat_and_native",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let server = wiremock::MockServer::start().await;
|
||||
|
||||
// Native endpoint
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/tags"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200))
|
||||
.mount(&server)
|
||||
.await;
|
||||
let native = OllamaClient::from_host_root(server.uri());
|
||||
native.probe_server().await.expect("probe native");
|
||||
|
||||
// OpenAI compatibility endpoint
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/v1/models"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200))
|
||||
.mount(&server)
|
||||
.await;
|
||||
let ollama_client =
|
||||
OllamaClient::try_from_provider_with_base_url(&format!("{}/v1", server.uri()))
|
||||
.await
|
||||
.expect("probe OpenAI compat");
|
||||
ollama_client
|
||||
.probe_server()
|
||||
.await
|
||||
.expect("probe OpenAI compat");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_try_from_oss_provider_ok_when_server_running() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} set; skipping test_try_from_oss_provider_ok_when_server_running",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let server = wiremock::MockServer::start().await;
|
||||
|
||||
// OpenAI‑compat models endpoint responds OK.
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/v1/models"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
OllamaClient::try_from_provider_with_base_url(&format!("{}/v1", server.uri()))
|
||||
.await
|
||||
.expect("client should be created when probe succeeds");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_try_from_provider_preserves_outbound_proxy_policy() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} set; skipping test_try_from_provider_preserves_outbound_proxy_policy",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let proxy = wiremock::MockServer::start().await;
|
||||
let base_url = "http://ollama-proxy.invalid";
|
||||
let request_url = format!("{base_url}/api/tags");
|
||||
codex_http_client::cache_system_proxy_route_for_test(&request_url, proxy.uri());
|
||||
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/tags"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200))
|
||||
.expect(1)
|
||||
.mount(&proxy)
|
||||
.await;
|
||||
|
||||
let provider = create_oss_provider_with_base_url(base_url, WireApi::Responses);
|
||||
let client = OllamaClient::try_from_provider(
|
||||
&provider,
|
||||
HttpClientFactory::new(OutboundProxyPolicy::RespectSystemProxy),
|
||||
)
|
||||
.await
|
||||
.expect("client should preserve the configured outbound proxy policy");
|
||||
|
||||
assert_eq!(
|
||||
client.client.outbound_proxy_policy(),
|
||||
OutboundProxyPolicy::RespectSystemProxy
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_try_from_provider_handles_invalid_custom_ca_by_proxy_policy() {
|
||||
const CHILD_POLICY_ENV: &str = "CODEX_OLLAMA_INVALID_CA_TEST_POLICY";
|
||||
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} set; skipping test_try_from_provider_handles_invalid_custom_ca_by_proxy_policy",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let Ok(policy_name) = std::env::var(CHILD_POLICY_ENV) else {
|
||||
let invalid_ca_path = std::env::temp_dir().join(format!(
|
||||
"codex-ollama-invalid-ca-{}.pem",
|
||||
std::process::id()
|
||||
));
|
||||
std::fs::write(&invalid_ca_path, "not a PEM certificate")
|
||||
.expect("invalid CA fixture should be written");
|
||||
|
||||
for ca_env in ["CODEX_CA_CERTIFICATE", "SSL_CERT_FILE"] {
|
||||
for policy_name in ["reqwest-default", "respect-system-proxy"] {
|
||||
let output = std::process::Command::new(
|
||||
std::env::current_exe().expect("test executable should be available"),
|
||||
)
|
||||
.arg("--exact")
|
||||
.arg("client::tests::test_try_from_provider_handles_invalid_custom_ca_by_proxy_policy")
|
||||
.arg("--nocapture")
|
||||
.env_remove("CODEX_CA_CERTIFICATE")
|
||||
.env_remove("SSL_CERT_FILE")
|
||||
.env(ca_env, &invalid_ca_path)
|
||||
.env(CHILD_POLICY_ENV, policy_name)
|
||||
.output()
|
||||
.expect("isolated CA subprocess should run");
|
||||
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"{policy_name} failed with invalid {ca_env}\nstdout:\n{}\nstderr:\n{}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
std::fs::remove_file(invalid_ca_path).expect("invalid CA fixture should be removed");
|
||||
return;
|
||||
};
|
||||
|
||||
let outbound_proxy_policy = match policy_name.as_str() {
|
||||
"reqwest-default" => OutboundProxyPolicy::ReqwestDefault,
|
||||
"respect-system-proxy" => OutboundProxyPolicy::RespectSystemProxy,
|
||||
_ => panic!("unexpected test proxy policy: {policy_name}"),
|
||||
};
|
||||
let server = wiremock::MockServer::start().await;
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/tags"))
|
||||
.respond_with(wiremock::ResponseTemplate::new(200))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let provider = create_oss_provider_with_base_url(&server.uri(), WireApi::Responses);
|
||||
let result = OllamaClient::try_from_provider(
|
||||
&provider,
|
||||
HttpClientFactory::new(outbound_proxy_policy),
|
||||
)
|
||||
.await;
|
||||
|
||||
match outbound_proxy_policy {
|
||||
OutboundProxyPolicy::ReqwestDefault => {
|
||||
result.expect("default-routed Ollama should fall back to system roots");
|
||||
assert_eq!(
|
||||
server
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("mock server should report requests")
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
}
|
||||
OutboundProxyPolicy::RespectSystemProxy => {
|
||||
let error = result
|
||||
.err()
|
||||
.expect("system-proxy Ollama should reject invalid custom CAs");
|
||||
let ca_env = if std::env::var_os("CODEX_CA_CERTIFICATE").is_some() {
|
||||
"CODEX_CA_CERTIFICATE"
|
||||
} else {
|
||||
"SSL_CERT_FILE"
|
||||
};
|
||||
assert!(
|
||||
error.to_string().contains(ca_env),
|
||||
"expected actionable {ca_env} error, got: {error}"
|
||||
);
|
||||
assert_ne!(error.to_string(), OLLAMA_CONNECTION_ERROR);
|
||||
assert!(
|
||||
server
|
||||
.received_requests()
|
||||
.await
|
||||
.expect("mock server should report requests")
|
||||
.is_empty()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_try_from_oss_provider_err_when_server_missing() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} set; skipping test_try_from_oss_provider_err_when_server_missing",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let server = wiremock::MockServer::start().await;
|
||||
let err = OllamaClient::try_from_provider_with_base_url(&format!("{}/v1", server.uri()))
|
||||
.await
|
||||
.err()
|
||||
.expect("expected error");
|
||||
assert_eq!(OLLAMA_CONNECTION_ERROR, err.to_string());
|
||||
}
|
||||
}
|
||||
145
codex-rs/ollama/src/lib.rs
Normal file
145
codex-rs/ollama/src/lib.rs
Normal file
@@ -0,0 +1,145 @@
|
||||
mod client;
|
||||
mod line_buffer;
|
||||
mod parser;
|
||||
mod pull;
|
||||
mod url;
|
||||
|
||||
pub use client::OllamaClient;
|
||||
use codex_core::config::Config;
|
||||
pub use pull::CliProgressReporter;
|
||||
pub use pull::PullEvent;
|
||||
pub use pull::PullProgressReporter;
|
||||
pub use pull::TuiProgressReporter;
|
||||
use semver::Version;
|
||||
|
||||
/// 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.
|
||||
pub async fn ensure_oss_ready(config: &Config, client: &OllamaClient) -> std::io::Result<()> {
|
||||
// Only download when the requested model is the default OSS model (or when -m is not provided).
|
||||
let model = match config.model.as_ref() {
|
||||
Some(model) => model,
|
||||
None => DEFAULT_OSS_MODEL,
|
||||
};
|
||||
|
||||
// If the model is not present locally, pull it.
|
||||
match client.fetch_models().await {
|
||||
Ok(models) => {
|
||||
if !models.iter().any(|m| m == model) {
|
||||
let mut reporter = crate::CliProgressReporter::new();
|
||||
client.pull_with_reporter(model, &mut reporter).await?;
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
// Not fatal; higher layers may still proceed and surface errors later.
|
||||
tracing::warn!("Failed to query local models from Ollama: {}.", err);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn min_responses_version() -> Version {
|
||||
Version::new(0, 13, 4)
|
||||
}
|
||||
|
||||
fn supports_responses(version: &Version) -> bool {
|
||||
*version == Version::new(0, 0, 0) || *version >= min_responses_version()
|
||||
}
|
||||
|
||||
/// Ensure the running Ollama server is new enough to support the Responses API.
|
||||
///
|
||||
/// Returns `Ok(())` when the version endpoint is missing or unparsable.
|
||||
pub async fn ensure_responses_supported(client: &OllamaClient) -> std::io::Result<()> {
|
||||
let Some(version) = client.fetch_version().await? else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
if supports_responses(&version) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let min = min_responses_version();
|
||||
Err(std::io::Error::other(format!(
|
||||
"Ollama {version} is too old. Codex requires Ollama {min} or newer."
|
||||
)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use codex_http_client::HttpClientFactory;
|
||||
use codex_http_client::OutboundProxyPolicy;
|
||||
use codex_model_provider_info::WireApi;
|
||||
use codex_model_provider_info::create_oss_provider_with_base_url;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn version_check_reuses_existing_ollama_client() {
|
||||
if std::env::var(codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR).is_ok() {
|
||||
tracing::info!(
|
||||
"{} set; skipping version_check_reuses_existing_ollama_client",
|
||||
codex_core::spawn::CODEX_SANDBOX_NETWORK_DISABLED_ENV_VAR
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let server = wiremock::MockServer::start().await;
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/tags"))
|
||||
.respond_with(
|
||||
wiremock::ResponseTemplate::new(200)
|
||||
.set_body_json(serde_json::json!({"models": [{"name": "gpt-oss:20b"}]})),
|
||||
)
|
||||
.expect(2)
|
||||
.mount(&server)
|
||||
.await;
|
||||
wiremock::Mock::given(wiremock::matchers::method("GET"))
|
||||
.and(wiremock::matchers::path("/api/version"))
|
||||
.respond_with(
|
||||
wiremock::ResponseTemplate::new(200)
|
||||
.set_body_json(serde_json::json!({"version": "0.14.1"})),
|
||||
)
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
let provider = create_oss_provider_with_base_url(&server.uri(), WireApi::Responses);
|
||||
let client = OllamaClient::try_from_provider(
|
||||
&provider,
|
||||
HttpClientFactory::new(OutboundProxyPolicy::ReqwestDefault),
|
||||
)
|
||||
.await
|
||||
.expect("create Ollama client");
|
||||
|
||||
ensure_responses_supported(&client)
|
||||
.await
|
||||
.expect("version check should reuse the existing client");
|
||||
assert_eq!(
|
||||
client.fetch_models().await.expect("fetch models"),
|
||||
vec!["gpt-oss:20b"]
|
||||
);
|
||||
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn supports_responses_for_dev_zero() {
|
||||
assert!(supports_responses(&Version::new(0, 0, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn does_not_support_responses_before_cutoff() {
|
||||
assert!(!supports_responses(&Version::new(0, 13, 3)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn supports_responses_at_or_after_cutoff() {
|
||||
assert!(supports_responses(&Version::new(0, 13, 4)));
|
||||
assert!(supports_responses(&Version::new(0, 14, 0)));
|
||||
}
|
||||
}
|
||||
32
codex-rs/ollama/src/line_buffer.rs
Normal file
32
codex-rs/ollama/src/line_buffer.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use bytes::BytesMut;
|
||||
use memchr::memchr;
|
||||
|
||||
#[derive(Default)]
|
||||
#[cfg_attr(test, derive(Debug, PartialEq, Eq))]
|
||||
pub(crate) struct LineBuffer {
|
||||
bytes: BytesMut,
|
||||
/// Prefix already scanned and known not to contain a newline.
|
||||
scanned_len: usize,
|
||||
}
|
||||
|
||||
impl LineBuffer {
|
||||
pub(crate) fn extend_from_slice(&mut self, bytes: &[u8]) {
|
||||
self.bytes.extend_from_slice(bytes);
|
||||
}
|
||||
|
||||
pub(crate) fn take_line(&mut self) -> Option<BytesMut> {
|
||||
let Some(relative_index) = memchr(b'\n', &self.bytes[self.scanned_len..]) else {
|
||||
self.scanned_len = self.bytes.len();
|
||||
return None;
|
||||
};
|
||||
|
||||
let newline_index = self.scanned_len + relative_index;
|
||||
let line = self.bytes.split_to(newline_index + 1);
|
||||
self.scanned_len = 0;
|
||||
Some(line)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "line_buffer_tests.rs"]
|
||||
mod tests;
|
||||
42
codex-rs/ollama/src/line_buffer_tests.rs
Normal file
42
codex-rs/ollama/src/line_buffer_tests.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use bytes::BytesMut;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::LineBuffer;
|
||||
|
||||
#[test]
|
||||
fn searches_only_new_bytes_after_partial_line() {
|
||||
let mut buffer = LineBuffer::default();
|
||||
|
||||
buffer.extend_from_slice(b"partial");
|
||||
assert_eq!(buffer.take_line(), None);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
LineBuffer {
|
||||
bytes: BytesMut::from(&b"partial"[..]),
|
||||
scanned_len: 7,
|
||||
}
|
||||
);
|
||||
|
||||
buffer.extend_from_slice(b" line");
|
||||
assert_eq!(buffer.take_line(), None);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
LineBuffer {
|
||||
bytes: BytesMut::from(&b"partial line"[..]),
|
||||
scanned_len: 12,
|
||||
}
|
||||
);
|
||||
|
||||
buffer.extend_from_slice(b"\nnext");
|
||||
assert_eq!(
|
||||
buffer.take_line(),
|
||||
Some(BytesMut::from(&b"partial line\n"[..]))
|
||||
);
|
||||
assert_eq!(
|
||||
buffer,
|
||||
LineBuffer {
|
||||
bytes: BytesMut::from(&b"next"[..]),
|
||||
scanned_len: 0,
|
||||
}
|
||||
);
|
||||
}
|
||||
75
codex-rs/ollama/src/parser.rs
Normal file
75
codex-rs/ollama/src/parser.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use serde_json::Value as JsonValue;
|
||||
|
||||
use crate::pull::PullEvent;
|
||||
|
||||
// Convert a single JSON object representing a pull update into one or more events.
|
||||
pub(crate) fn pull_events_from_value(value: &JsonValue) -> Vec<PullEvent> {
|
||||
let mut events = Vec::new();
|
||||
if let Some(status) = value.get("status").and_then(|s| s.as_str()) {
|
||||
events.push(PullEvent::Status(status.to_string()));
|
||||
if status == "success" {
|
||||
events.push(PullEvent::Success);
|
||||
}
|
||||
}
|
||||
let digest = value
|
||||
.get("digest")
|
||||
.and_then(|d| d.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let total = value.get("total").and_then(JsonValue::as_u64);
|
||||
let completed = value.get("completed").and_then(JsonValue::as_u64);
|
||||
if total.is_some() || completed.is_some() {
|
||||
events.push(PullEvent::ChunkProgress {
|
||||
digest,
|
||||
total,
|
||||
completed,
|
||||
});
|
||||
}
|
||||
events
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use assert_matches::assert_matches;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_pull_events_decoder_status_and_success() {
|
||||
let v: JsonValue = serde_json::json!({"status":"verifying"});
|
||||
let events = pull_events_from_value(&v);
|
||||
assert_matches!(events.as_slice(), [PullEvent::Status(s)] if s == "verifying");
|
||||
|
||||
let v2: JsonValue = serde_json::json!({"status":"success"});
|
||||
let events2 = pull_events_from_value(&v2);
|
||||
assert_eq!(events2.len(), 2);
|
||||
assert_matches!(events2[0], PullEvent::Status(ref s) if s == "success");
|
||||
assert_matches!(events2[1], PullEvent::Success);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pull_events_decoder_progress() {
|
||||
let v: JsonValue = serde_json::json!({"digest":"sha256:abc","total":100});
|
||||
let events = pull_events_from_value(&v);
|
||||
assert_eq!(events.len(), 1);
|
||||
assert_matches!(
|
||||
&events[0],
|
||||
PullEvent::ChunkProgress {
|
||||
digest,
|
||||
total,
|
||||
completed,
|
||||
} if digest == "sha256:abc" && total == &Some(100) && completed.is_none()
|
||||
);
|
||||
let v2: JsonValue = serde_json::json!({"digest":"sha256:def","completed":42});
|
||||
let events2 = pull_events_from_value(&v2);
|
||||
assert_eq!(events2.len(), 1);
|
||||
assert_matches!(
|
||||
&events2[0],
|
||||
PullEvent::ChunkProgress {
|
||||
digest,
|
||||
total,
|
||||
completed,
|
||||
} if digest == "sha256:def" && total.is_none() && completed == &Some(42)
|
||||
);
|
||||
}
|
||||
}
|
||||
147
codex-rs/ollama/src/pull.rs
Normal file
147
codex-rs/ollama/src/pull.rs
Normal file
@@ -0,0 +1,147 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
|
||||
/// 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,
|
||||
|
||||
/// Error event with a message.
|
||||
Error(String),
|
||||
}
|
||||
|
||||
/// 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::Error(_) => {
|
||||
// This will be handled by the caller, so we don't do anything
|
||||
// here or the error will be printed twice.
|
||||
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.
|
||||
#[derive(Default)]
|
||||
pub struct TuiProgressReporter(CliProgressReporter);
|
||||
|
||||
impl PullProgressReporter for TuiProgressReporter {
|
||||
fn on_event(&mut self, event: &PullEvent) -> io::Result<()> {
|
||||
self.0.on_event(event)
|
||||
}
|
||||
}
|
||||
39
codex-rs/ollama/src/url.rs
Normal file
39
codex-rs/ollama/src/url.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
/// Identify whether a base_url points at an OpenAI-compatible root (".../v1").
|
||||
pub(crate) 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()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_base_url_to_host_root() {
|
||||
assert_eq!(
|
||||
base_url_to_host_root("http://localhost:11434/v1"),
|
||||
"http://localhost:11434"
|
||||
);
|
||||
assert_eq!(
|
||||
base_url_to_host_root("http://localhost:11434"),
|
||||
"http://localhost:11434"
|
||||
);
|
||||
assert_eq!(
|
||||
base_url_to_host_root("http://localhost:11434/"),
|
||||
"http://localhost:11434"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user