Skip to main content

provider_agent/backend/
ollama.rs

1//! Ollama adapter.
2//!
3//! - Default port: 11434
4//! - Health: `GET /api/tags` (Ollama's native endpoint, most reliable)
5//! - Models: `GET /v1/models` (OpenAI-compat — uniform shape across backends)
6//! - Execute: `POST /v1/chat/completions`
7
8use async_trait::async_trait;
9
10use super::http::{
11    build_client, get_json, parse_openai_models, probe, stream_chat_completions, trim_url,
12};
13use super::{Backend, BackendHealth, BackendModel, BackendResult, Job, JobResult, JobSink};
14
15pub struct OllamaBackend {
16    id: String,
17    base_url: String,
18    client: reqwest::Client,
19}
20
21impl OllamaBackend {
22    pub fn new(url: &str) -> Self {
23        let base_url = trim_url(url).to_string();
24        Self {
25            id: format!("ollama:{base_url}"),
26            base_url,
27            client: build_client(),
28        }
29    }
30}
31
32#[async_trait]
33impl Backend for OllamaBackend {
34    fn kind(&self) -> &'static str {
35        "ollama"
36    }
37
38    fn id(&self) -> &str {
39        &self.id
40    }
41
42    async fn list_models(&self) -> BackendResult<Vec<BackendModel>> {
43        let url = format!("{}/v1/models", self.base_url);
44        let v = get_json(&self.client, &url, None).await?;
45        Ok(parse_openai_models(&v, true))
46    }
47
48    async fn health(&self) -> BackendResult<BackendHealth> {
49        // /api/tags is the most reliable liveness signal for Ollama.
50        let url = format!("{}/api/tags", self.base_url);
51        match probe(&self.client, &url, None).await {
52            Ok(latency_ms) => Ok(BackendHealth {
53                reachable: true,
54                latency_ms: Some(latency_ms),
55                last_error: None,
56            }),
57            Err(e) => Ok(BackendHealth {
58                reachable: false,
59                latency_ms: None,
60                last_error: Some(e.to_string()),
61            }),
62        }
63    }
64
65    async fn execute(&self, job: &Job, sink: &mut dyn JobSink) -> BackendResult<JobResult> {
66        let endpoint = format!("{}/v1/chat/completions", self.base_url);
67        stream_chat_completions(&self.client, &endpoint, None, job, sink).await
68    }
69}