use crate::ai::types::{AiConfig, ChatMessage, OllamaChatChunk, OllamaModel};
use crate::error::FetchiumError;
use futures::StreamExt;
use serde::Deserialize;
pub struct OllamaClient {
client: reqwest::Client,
base_url: String,
}
impl OllamaClient {
pub fn new(config: &AiConfig) -> Self {
let base_url = format!("{}:{}", config.ollama_host, config.ollama_port);
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(config.timeout_secs))
.build()
.unwrap_or_default();
Self { client, base_url }
}
pub async fn is_available(&self) -> bool {
self.client
.get(format!("{}/api/tags", self.base_url))
.send()
.await
.is_ok()
}
pub async fn list_models(&self) -> Result<Vec<OllamaModel>, FetchiumError> {
#[derive(Deserialize)]
struct TagsResponse {
models: Vec<OllamaModel>,
}
let resp = self
.client
.get(format!("{}/api/tags", self.base_url))
.send()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Ollama unreachable: {e}")))?;
let tags: TagsResponse = resp.json().await.map_err(|e| {
FetchiumError::AiUnavailable(format!("Invalid Ollama /api/tags response: {e}"))
})?;
Ok(tags.models)
}
pub async fn chat(
&self,
model: &str,
messages: &[ChatMessage],
temperature: f32,
) -> Result<String, FetchiumError> {
let body = serde_json::json!({
"model": model,
"messages": messages,
"stream": false,
"options": { "temperature": temperature }
});
let resp = self
.client
.post(format!("{}/api/chat", self.base_url))
.json(&body)
.send()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Ollama chat failed: {e}")))?;
let chunk: OllamaChatChunk = resp
.json()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Invalid chat response: {e}")))?;
Ok(chunk.message.content)
}
pub async fn chat_stream<F>(
&self,
model: &str,
messages: &[ChatMessage],
temperature: f32,
mut on_chunk: F,
) -> Result<String, FetchiumError>
where
F: FnMut(&str),
{
let body = serde_json::json!({
"model": model,
"messages": messages,
"stream": true,
"options": { "temperature": temperature }
});
let resp = self
.client
.post(format!("{}/api/chat", self.base_url))
.json(&body)
.send()
.await
.map_err(|e| FetchiumError::AiUnavailable(format!("Ollama stream failed: {e}")))?;
let mut full_response = String::new();
let mut stream = resp.bytes_stream();
let mut buffer = Vec::new();
while let Some(chunk_result) = stream.next().await {
let bytes = chunk_result
.map_err(|e| FetchiumError::AiUnavailable(format!("Stream read error: {e}")))?;
buffer.extend_from_slice(&bytes);
while let Some(newline_pos) = buffer.iter().position(|&b| b == b'\n') {
let line: Vec<u8> = buffer.drain(..=newline_pos).collect();
let line_str = String::from_utf8_lossy(&line);
let trimmed = line_str.trim();
if trimmed.is_empty() {
continue;
}
if let Ok(chunk) = serde_json::from_str::<OllamaChatChunk>(trimmed) {
let token = &chunk.message.content;
on_chunk(token);
full_response.push_str(token);
if chunk.done {
return Ok(full_response);
}
}
}
}
Ok(full_response)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn client_builds_correct_base_url() {
let config = AiConfig {
ollama_host: "http://localhost".into(),
ollama_port: 11434,
..Default::default()
};
let client = OllamaClient::new(&config);
assert_eq!(client.base_url, "http://localhost:11434");
}
}