Skip to main content

mojentic/llm/gateways/
ollama.rs

1use crate::error::{MojenticError, Result};
2use crate::llm::gateway::{
3    CompletionConfig, LlmGateway, ModelContextWindow, StreamChunk, StreamMetrics, StreamProgress,
4};
5use crate::llm::gateways::ollama_stream_events::OllamaEventParser;
6use crate::llm::models::{
7    LlmGatewayResponse, LlmMessage, LlmToolCall, MessageRole, ResponseEvidence,
8};
9use crate::llm::stream_events::{drive_event_stream, StreamEventError, StreamEventStream};
10use crate::llm::stream_timing::{system_clock, StreamClock, StreamTiming};
11use crate::llm::tools::LlmTool;
12use async_trait::async_trait;
13use futures::stream::{Stream, StreamExt};
14use reqwest::Client;
15use serde_json::Value;
16use std::collections::HashMap;
17use std::pin::Pin;
18use std::sync::Arc;
19use tracing::{debug, info, warn};
20
21/// Configuration for connecting to Ollama server
22#[derive(Debug, Clone)]
23pub struct OllamaConfig {
24    pub host: String,
25    pub timeout: Option<std::time::Duration>,
26    pub headers: HashMap<String, String>,
27}
28
29impl Default for OllamaConfig {
30    fn default() -> Self {
31        Self {
32            host: std::env::var("OLLAMA_HOST")
33                .unwrap_or_else(|_| "http://localhost:11434".to_string()),
34            timeout: None,
35            headers: HashMap::new(),
36        }
37    }
38}
39
40/// Gateway for Ollama local LLM service
41///
42/// This gateway provides access to local LLM models through Ollama,
43/// supporting text generation, structured output, tool calling, and embeddings.
44pub struct OllamaGateway {
45    clock: Arc<dyn StreamClock>,
46    client: Client,
47    config: OllamaConfig,
48}
49
50impl OllamaGateway {
51    /// Select a monotonic measurement clock. The default uses std::time::Instant.
52    pub fn with_stream_clock(mut self, clock: Arc<dyn StreamClock>) -> Self {
53        self.clock = clock;
54        self
55    }
56
57    /// Opt into per-request recovery without changing legacy entrypoints.
58    pub fn with_recovery(
59        self,
60        policy: crate::llm::recovery::RecoveryPolicy,
61    ) -> Result<crate::llm::recovery::RecoveryGateway<Self>> {
62        crate::llm::recovery::RecoveryGateway::new(self, policy)
63    }
64
65    /// Create a new Ollama gateway with default configuration
66    pub fn new() -> Self {
67        Self::with_config(OllamaConfig::default())
68    }
69
70    /// Create a new Ollama gateway with custom configuration
71    pub fn with_config(config: OllamaConfig) -> Self {
72        let mut client_builder = Client::builder();
73
74        if let Some(timeout) = config.timeout {
75            client_builder = client_builder.timeout(timeout);
76        }
77
78        let client = client_builder.build().unwrap();
79
80        Self {
81            client,
82            config,
83            clock: system_clock(),
84        }
85    }
86
87    /// Create gateway with custom host
88    pub fn with_host(host: impl Into<String>) -> Self {
89        Self::with_config(OllamaConfig {
90            host: host.into(),
91            ..Default::default()
92        })
93    }
94
95    /// A POST to the chat endpoint carrying `body`.
96    fn chat_request(&self, body: &Value) -> reqwest::RequestBuilder {
97        self.client.post(format!("{}/api/chat", self.config.host)).json(body)
98    }
99
100    /// Resolve the request's context capacity without sending a request.
101    /// Explicit `num_ctx` takes precedence over the model parameter; both are
102    /// clamped to the advertised maximum. With neither, this is an upper bound
103    /// because the server's `OLLAMA_CONTEXT_LENGTH` is unknown to the client.
104    pub fn effective_context_window(
105        window: Option<&ModelContextWindow>,
106        config: &CompletionConfig,
107    ) -> Option<usize> {
108        window.map(|window| {
109            let maximum = window.max_context_tokens.get();
110            maximum.min(config.num_ctx.or(window.model_num_ctx).unwrap_or(maximum))
111        })
112    }
113
114    /// Pull a model from Ollama library
115    pub async fn pull_model(&self, model: &str) -> Result<()> {
116        info!("Pulling Ollama model: {}", model);
117
118        let response = self
119            .client
120            .post(format!("{}/api/pull", self.config.host))
121            .json(&serde_json::json!({
122                "name": model
123            }))
124            .send()
125            .await?;
126
127        if !response.status().is_success() {
128            return Err(MojenticError::GatewayError(format!(
129                "Failed to pull model {}: {}",
130                model,
131                response.status()
132            )));
133        }
134
135        Ok(())
136    }
137}
138
139impl Default for OllamaGateway {
140    fn default() -> Self {
141        Self::new()
142    }
143}
144
145/// The positive `num_ctx` line of an `/api/show` `parameters` text, if any.
146fn num_ctx_parameter(parameters: &str) -> Option<std::num::NonZeroUsize> {
147    parameters.lines().find_map(|line| {
148        let mut parts = line.split_whitespace();
149        (parts.next() == Some("num_ctx"))
150            .then(|| parts.next().and_then(|n| n.parse().ok()))
151            .flatten()
152    })
153}
154
155#[async_trait]
156impl LlmGateway for OllamaGateway {
157    fn stream_clock(&self) -> Arc<dyn StreamClock> {
158        self.clock.clone()
159    }
160
161    async fn get_model_context_window(&self, model: &str) -> Result<Option<ModelContextWindow>> {
162        let mut request = self
163            .client
164            .post(format!("{}/api/show", self.config.host.trim_end_matches('/')))
165            .json(&serde_json::json!({"model": model}));
166        for (key, value) in &self.config.headers {
167            request = request.header(key, value);
168        }
169        let response = request.send().await?;
170        if !response.status().is_success() {
171            return Err(MojenticError::GatewayError(format!(
172                "Failed to show model {model}: {}",
173                response.status()
174            )));
175        }
176        let body: Value = response.json().await?;
177        if !body.is_object() {
178            return Err(MojenticError::GatewayError("Invalid response format".into()));
179        }
180        let model_num_ctx = match body.get("parameters") {
181            Some(parameters) => num_ctx_parameter(
182                parameters
183                    .as_str()
184                    .ok_or_else(|| MojenticError::GatewayError("Invalid parameters".into()))?,
185            ),
186            None => None,
187        };
188        let advertised = match body.get("model_info") {
189            Some(info) => info
190                .as_object()
191                .ok_or_else(|| MojenticError::GatewayError("Invalid model_info".into()))?
192                .iter()
193                .find(|(key, _)| key.ends_with(".context_length")),
194            None => None,
195        };
196        match (advertised, model_num_ctx) {
197            (Some((key, value)), model_num_ctx) => {
198                let mut window =
199                    ModelContextWindow::advertised(value, format!("model_info.{key}"))?;
200                window.model_num_ctx = model_num_ctx.map(|num_ctx| num_ctx.get());
201                Ok(Some(window))
202            }
203            // Ollama runs the model at its `num_ctx` parameter when a request
204            // sends none, so that parameter is the model's window when
205            // `model_info` advertises no `context_length`.
206            (None, Some(num_ctx)) => Ok(Some(ModelContextWindow {
207                max_context_tokens: num_ctx,
208                field: "parameters.num_ctx".into(),
209                model_num_ctx: Some(num_ctx.get()),
210            })),
211            (None, None) => Ok(None),
212        }
213    }
214
215    async fn complete(
216        &self,
217        model: &str,
218        messages: &[LlmMessage],
219        tools: Option<&[Box<dyn LlmTool>]>,
220        config: &CompletionConfig,
221    ) -> Result<LlmGatewayResponse> {
222        info!("Delegating to Ollama for completion");
223        debug!("Model: {}, Message count: {}", model, messages.len());
224
225        let mut body = chat_body(model, messages, config, false)?;
226
227        // Add tools if provided
228        if let Some(tools) = tools {
229            let tool_defs: Vec<_> = tools.iter().map(|t| t.descriptor()).collect();
230            body["tools"] = serde_json::to_value(tool_defs)?;
231        }
232
233        // Make API request
234        let response = self.chat_request(&body).send().await?;
235
236        if !response.status().is_success() {
237            return Err(MojenticError::GatewayError(format!(
238                "Ollama API error: {}",
239                response.status()
240            )));
241        }
242
243        let response_body: Value = response.json().await?;
244
245        // Parse content
246        let content = response_body["message"]["content"].as_str().map(String::from);
247
248        // Parse thinking if present (from reasoning models)
249        let thinking = response_body["message"]["thinking"].as_str().map(String::from);
250
251        // Parse tool calls if present
252        let tool_calls = if let Some(calls) = response_body["message"]["tool_calls"].as_array() {
253            calls
254                .iter()
255                .filter_map(|call| {
256                    let name = call["function"]["name"].as_str()?.to_string();
257                    let args = call["function"]["arguments"].as_object()?;
258
259                    let arguments: HashMap<String, Value> =
260                        args.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
261
262                    Some(LlmToolCall {
263                        id: call["id"].as_str().map(String::from),
264                        name,
265                        arguments,
266                    })
267                })
268                .collect()
269        } else {
270            vec![]
271        };
272
273        Ok(LlmGatewayResponse {
274            content,
275            object: None,
276            tool_calls,
277            thinking,
278            evidence: ollama_evidence(&response_body),
279        })
280    }
281
282    async fn complete_json(
283        &self,
284        model: &str,
285        messages: &[LlmMessage],
286        schema: Value,
287        config: &CompletionConfig,
288    ) -> Result<Value> {
289        self.complete_json_response(model, messages, schema, config)
290            .await?
291            .object
292            .ok_or_else(|| MojenticError::GatewayError("No content in response".to_string()))
293    }
294
295    async fn complete_json_response(
296        &self,
297        model: &str,
298        messages: &[LlmMessage],
299        schema: Value,
300        config: &CompletionConfig,
301    ) -> Result<LlmGatewayResponse<Value>> {
302        info!("Requesting structured output from Ollama");
303
304        let mut body = chat_body(model, messages, config, false)?;
305        body["format"] = schema;
306
307        let response = self.chat_request(&body).send().await?;
308
309        if !response.status().is_success() {
310            return Err(MojenticError::GatewayError(format!(
311                "Ollama API error: {}",
312                response.status()
313            )));
314        }
315
316        let response_body: Value = response.json().await?;
317        let content = response_body["message"]["content"]
318            .as_str()
319            .ok_or_else(|| MojenticError::GatewayError("No content in response".to_string()))?;
320
321        // Parse the JSON response
322        let object: Value = serde_json::from_str(content)?;
323
324        Ok(LlmGatewayResponse {
325            content: Some(content.to_string()),
326            object: Some(object),
327            thinking: response_body["message"]["thinking"].as_str().map(String::from),
328            evidence: ollama_evidence(&response_body),
329            ..Default::default()
330        })
331    }
332
333    async fn get_available_models(&self) -> Result<Vec<String>> {
334        debug!("Fetching available Ollama models");
335
336        let response = self.client.get(format!("{}/api/tags", self.config.host)).send().await?;
337
338        if !response.status().is_success() {
339            return Err(MojenticError::GatewayError(format!(
340                "Failed to get models: {}",
341                response.status()
342            )));
343        }
344
345        let body: Value = response.json().await?;
346
347        let models = body["models"]
348            .as_array()
349            .ok_or_else(|| MojenticError::GatewayError("Invalid response format".to_string()))?
350            .iter()
351            .filter_map(|m| m["name"].as_str().map(String::from))
352            .collect::<Vec<_>>();
353
354        Ok(models)
355    }
356
357    async fn calculate_embeddings(&self, text: &str, model: Option<&str>) -> Result<Vec<f32>> {
358        let model = model.unwrap_or("mxbai-embed-large");
359        debug!("Calculating embeddings with model: {}", model);
360
361        let body = serde_json::json!({
362            "model": model,
363            "prompt": text
364        });
365
366        let response = self
367            .client
368            .post(format!("{}/api/embeddings", self.config.host))
369            .json(&body)
370            .send()
371            .await?;
372
373        if !response.status().is_success() {
374            return Err(MojenticError::GatewayError(format!(
375                "Embeddings API error: {}",
376                response.status()
377            )));
378        }
379
380        let response_body: Value = response.json().await?;
381
382        let embeddings = response_body["embedding"]
383            .as_array()
384            .ok_or_else(|| MojenticError::GatewayError("Invalid embeddings response".to_string()))?
385            .iter()
386            .filter_map(|v| v.as_f64().map(|f| f as f32))
387            .collect();
388
389        Ok(embeddings)
390    }
391
392    fn complete_stream<'a>(
393        &'a self,
394        model: &'a str,
395        messages: &'a [LlmMessage],
396        tools: Option<&'a [Box<dyn LlmTool>]>,
397        config: &'a CompletionConfig,
398    ) -> Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send + 'a>> {
399        let timing = StreamTiming::new(self.stream_clock());
400        let measure = timing.clone();
401        let source: Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send + 'a>> = Box::pin(
402            async_stream::stream! {
403                info!("Starting Ollama streaming completion");
404                debug!("Model: {}, Message count: {}", model, messages.len());
405
406                let mut body = match chat_body(model, messages, config, true) {
407                    Ok(body) => body,
408                    Err(e) => {
409                        yield Err(e);
410                        return;
411                    }
412                };
413
414                // Add tools if provided
415                if let Some(tools) = tools {
416                    let tool_defs: Vec<_> = tools.iter().map(|t| t.descriptor()).collect();
417                    if let Ok(tools_value) = serde_json::to_value(tool_defs) {
418                        body["tools"] = tools_value;
419                    }
420                }
421
422                // Make streaming API request
423                measure.dispatch();
424                let response = match self.chat_request(&body).send().await {
425                    Ok(r) => r,
426                    Err(e) => {
427                        yield Err(e.into());
428                        return;
429                    }
430                };
431
432                if !response.status().is_success() {
433                    yield Err(MojenticError::GatewayError(format!(
434                        "Ollama API error: {}",
435                        response.status()
436                    )));
437                    return;
438                }
439
440                // Process byte stream
441                let mut stream = response.bytes_stream();
442                let mut buffer = String::new();
443                let mut accumulated_tool_calls: Vec<LlmToolCall> = Vec::new();
444                let mut frame_index = 0usize;
445
446                while let Some(chunk_result) = stream.next().await {
447                    match chunk_result {
448                        Ok(bytes) => {
449                            // Append to buffer
450                            if let Ok(text) = std::str::from_utf8(&bytes) {
451                                buffer.push_str(text);
452
453                                // Process complete JSON lines (newline-delimited)
454                                while let Some(newline_pos) = buffer.find('\n') {
455                                    let line = buffer[..newline_pos].trim().to_string();
456                                    buffer = buffer[newline_pos + 1..].to_string();
457
458                                    if line.is_empty() {
459                                        continue;
460                                    }
461
462                                    // Parse JSON line
463                                    match serde_json::from_str::<Value>(&line) {
464                                        Ok(json) => {
465                                            measure.observe_line(&line);
466                                            frame_index += 1;
467                                            let done = json["done"].as_bool().unwrap_or(false);
468                                            let (content_chars, thinking_chars, tool_call_count) =
469                                                frame_progress_counts(&json);
470                                            yield Ok(StreamChunk::Progress(StreamProgress {
471                                                provider: "ollama".to_string(),
472                                                frame_index,
473                                                done,
474                                                content_chars,
475                                                thinking_chars,
476                                                tool_call_count,
477                                                accumulated_tool_call_count: accumulated_tool_calls.len(),
478                                            }));
479
480                                            // Check if streaming is done
481                                            if done {
482                                                yield Ok(StreamChunk::Metrics(ollama_stream_metrics(&json)));
483                                                // Final chunk - yield accumulated tool calls if any
484                                                if !accumulated_tool_calls.is_empty() {
485                                                    yield Ok(StreamChunk::ToolCalls(accumulated_tool_calls.clone()));
486                                                }
487                                                continue;
488                                            }
489
490                                            // Extract content
491                                            if let Some(message) = json["message"].as_object() {
492                                                if let Some(thinking) =
493                                                    message.get("thinking").and_then(|v| v.as_str())
494                                                {
495                                                    if !thinking.is_empty() {
496                                                        yield Ok(StreamChunk::Thinking(thinking.to_string()));
497                                                    }
498                                                }
499
500                                                if let Some(content) =
501                                                    message.get("content").and_then(|v| v.as_str())
502                                                {
503                                                    if !content.is_empty() {
504                                                        yield Ok(StreamChunk::Content(content.to_string()));
505                                                    }
506                                                }
507
508                                                // Extract tool calls
509                                                if let Some(calls) = message.get("tool_calls").and_then(|v| v.as_array()) {
510                                                    for call in calls {
511                                                        if let Some(function) = call.get("function").and_then(|v| v.as_object()) {
512                                                            if let (Some(name), Some(args)) = (
513                                                                function.get("name").and_then(|v| v.as_str()),
514                                                                function.get("arguments").and_then(|v| v.as_object()),
515                                                            ) {
516                                                                let arguments: HashMap<String, Value> = args
517                                                                    .iter()
518                                                                    .map(|(k, v)| (k.clone(), v.clone()))
519                                                                    .collect();
520
521                                                                let tool_call = LlmToolCall {
522                                                                    id: call.get("id").and_then(|v| v.as_str()).map(String::from),
523                                                                    name: name.to_string(),
524                                                                    arguments,
525                                                                };
526
527                                                                accumulated_tool_calls.push(tool_call);
528                                                            }
529                                                        }
530                                                    }
531                                                }
532                                            }
533                                        }
534                                        Err(e) => {
535                                            warn!("Failed to parse streaming chunk: {}", e);
536                                        }
537                                    }
538                                }
539                            }
540                        }
541                        Err(e) => {
542                            yield Err(e.into());
543                            return;
544                        }
545                    }
546                }
547            },
548        );
549        timing.legacy(source, "ollama")
550    }
551
552    fn complete_stream_events<'a>(
553        &'a self,
554        model: &'a str,
555        messages: &'a [LlmMessage],
556        config: &'a CompletionConfig,
557    ) -> std::result::Result<StreamEventStream<'a>, StreamEventError> {
558        info!("Starting Ollama stream events");
559        let body =
560            chat_body(model, messages, config, true).map_err(StreamEventError::RequestFailed)?;
561
562        Ok(drive_event_stream(
563            self.chat_request(&body),
564            OllamaEventParser::default(),
565            self.stream_clock(),
566            "ollama",
567        ))
568    }
569}
570
571/// Token counts Ollama reports, kept under Ollama's own names as usage.
572const OLLAMA_USAGE_FIELDS: [&str; 2] = ["prompt_eval_count", "eval_count"];
573
574/// Timing and identity fields Ollama reports, kept as evidence metadata.
575const OLLAMA_METADATA_FIELDS: [&str; 5] = [
576    "created_at",
577    "total_duration",
578    "load_duration",
579    "prompt_eval_duration",
580    "eval_duration",
581];
582
583/// Read the evidence a final Ollama chat frame reports, leaving absent values unknown.
584pub(crate) fn ollama_evidence(frame: &Value) -> ResponseEvidence {
585    let usage = reported_fields(frame, &OLLAMA_USAGE_FIELDS);
586    ResponseEvidence {
587        usage: (!usage.is_empty()).then(|| Value::Object(usage.into_iter().collect())),
588        provider_model: frame["model"].as_str().map(String::from),
589        finish_reason: frame["done_reason"].as_str().map(String::from),
590        metadata: reported_fields(frame, &OLLAMA_METADATA_FIELDS),
591    }
592}
593
594fn reported_fields(frame: &Value, fields: &[&str]) -> HashMap<String, Value> {
595    fields
596        .iter()
597        .filter_map(|field| {
598            let value = &frame[*field];
599            (!value.is_null()).then(|| (field.to_string(), value.clone()))
600        })
601        .collect()
602}
603
604pub(crate) fn frame_progress_counts(json: &Value) -> (usize, usize, usize) {
605    let Some(message) = json["message"].as_object() else {
606        return (0, 0, 0);
607    };
608    let content_chars = message
609        .get("content")
610        .and_then(|value| value.as_str())
611        .map(str::len)
612        .unwrap_or_default();
613    let thinking_chars = message
614        .get("thinking")
615        .and_then(|value| value.as_str())
616        .map(str::len)
617        .unwrap_or_default();
618    let tool_call_count = message
619        .get("tool_calls")
620        .and_then(|value| value.as_array())
621        .map(Vec::len)
622        .unwrap_or_default();
623    (content_chars, thinking_chars, tool_call_count)
624}
625
626pub(crate) fn ollama_stream_metrics(json: &Value) -> StreamMetrics {
627    let eval_count = json["eval_count"].as_u64();
628    let eval_duration_ns = json["eval_duration"].as_u64();
629    StreamMetrics {
630        provider: "ollama".to_string(),
631        total_duration_ns: json["total_duration"].as_u64(),
632        load_duration_ns: json["load_duration"].as_u64(),
633        prompt_eval_count: json["prompt_eval_count"].as_u64(),
634        prompt_eval_duration_ns: json["prompt_eval_duration"].as_u64(),
635        eval_count,
636        eval_duration_ns,
637        tokens_per_second: tokens_per_second(eval_count, eval_duration_ns),
638        ..Default::default()
639    }
640}
641
642fn tokens_per_second(eval_count: Option<u64>, eval_duration_ns: Option<u64>) -> Option<f64> {
643    let tokens = eval_count?;
644    let duration = eval_duration_ns?;
645    if duration == 0 {
646        return None;
647    }
648    Some(tokens as f64 / (duration as f64 / 1_000_000_000.0))
649}
650
651// Message adapter for Ollama format
652fn adapt_messages_to_ollama(messages: &[LlmMessage]) -> Result<Vec<Value>> {
653    messages
654        .iter()
655        .map(|msg| {
656            let mut ollama_msg = serde_json::json!({
657                "role": match msg.role {
658                    MessageRole::System => "system",
659                    MessageRole::User => "user",
660                    MessageRole::Assistant => "assistant",
661                    MessageRole::Tool => "tool",
662                },
663                "content": msg.content.as_deref().unwrap_or("")
664            });
665
666            if msg.role == MessageRole::Assistant {
667                if let Some(reasoning) = &msg.reasoning_content {
668                    ollama_msg["thinking"] = serde_json::json!(reasoning);
669                }
670            }
671            // Add images for user messages - Ollama requires base64-encoded images
672            if let Some(image_paths) = &msg.image_paths {
673                let encoded_images: Result<Vec<String>> = image_paths
674                    .iter()
675                    .map(|path| {
676                        std::fs::read(path)
677                            .map_err(|e| {
678                                MojenticError::GatewayError(format!(
679                                    "Failed to read image file {}: {}",
680                                    path, e
681                                ))
682                            })
683                            .map(|bytes| {
684                                base64::Engine::encode(
685                                    &base64::engine::general_purpose::STANDARD,
686                                    bytes,
687                                )
688                            })
689                    })
690                    .collect();
691
692                ollama_msg["images"] = serde_json::to_value(encoded_images?)?;
693            }
694
695            // Add tool calls for assistant messages
696            if let Some(tool_calls) = &msg.tool_calls {
697                let calls: Vec<_> = tool_calls
698                    .iter()
699                    .map(|tc| {
700                        serde_json::json!({
701                            "type": "function",
702                            "function": {
703                                "name": tc.name,
704                                "arguments": tc.arguments
705                            }
706                        })
707                    })
708                    .collect();
709                ollama_msg["tool_calls"] = serde_json::to_value(calls)?;
710            }
711
712            Ok(ollama_msg)
713        })
714        .collect()
715}
716
717/// Build a chat body with options, thinking control and response format.
718///
719/// Callers add tools.
720fn chat_body(
721    model: &str,
722    messages: &[LlmMessage],
723    config: &CompletionConfig,
724    stream: bool,
725) -> Result<Value> {
726    config.validate_reasoning()?;
727    let mut body = serde_json::json!({
728        "model": model,
729        "messages": adapt_messages_to_ollama(messages)?,
730        "options": extract_ollama_options(config),
731        "stream": stream
732    });
733
734    if let Some(effort) = config.reasoning_effort {
735        use crate::llm::gateway::ReasoningEffort;
736        body["think"] = match effort {
737            ReasoningEffort::Disabled => Value::Bool(false),
738            ReasoningEffort::Enabled => Value::Bool(true),
739            named => serde_json::to_value(named)?,
740        };
741    }
742
743    add_response_format(&mut body, config);
744    Ok(body)
745}
746
747// Extract Ollama-specific options from config
748fn extract_ollama_options(config: &CompletionConfig) -> Value {
749    let mut options = serde_json::json!({});
750    if let Some(temperature) = config.temperature {
751        options["temperature"] = serde_json::json!(temperature);
752    }
753    if let Some(num_ctx) = config.num_ctx {
754        options["num_ctx"] = serde_json::json!(num_ctx);
755    }
756    if let Some(max_tokens) = config.max_tokens {
757        options["num_predict"] = serde_json::json!(max_tokens);
758    }
759
760    if let Some(top_p) = config.top_p {
761        options["top_p"] = serde_json::json!(top_p);
762    }
763
764    if let Some(top_k) = config.top_k {
765        options["top_k"] = serde_json::json!(top_k);
766    }
767
768    options
769}
770
771// Add response format to request body if specified
772fn add_response_format(body: &mut Value, config: &CompletionConfig) {
773    use crate::llm::gateway::ResponseFormat;
774
775    if let Some(response_format) = &config.response_format {
776        match response_format {
777            ResponseFormat::JsonObject { schema: Some(s) } => {
778                body["format"] = s.clone();
779            }
780            ResponseFormat::JsonObject { schema: None } => {
781                body["format"] = serde_json::json!("json");
782            }
783            ResponseFormat::Text => {
784                // Text is the default, no need to add format field
785            }
786        }
787    }
788}
789
790impl crate::llm::recovery::RecoveryProvider for OllamaGateway {
791    fn recovery_provider(&self) -> crate::llm::recovery::Provider {
792        crate::llm::recovery::Provider::Ollama
793    }
794    fn prepare_recovery(
795        &self,
796        model: &str,
797        messages: &[LlmMessage],
798        tools: Option<&[Box<dyn LlmTool>]>,
799        config: &CompletionConfig,
800        schema: Option<Value>,
801        stream: bool,
802    ) -> Result<reqwest::Request> {
803        let mut body = chat_body(model, messages, config, stream)?;
804        if let Some(schema) = schema {
805            body["format"] = schema;
806        }
807        if let Some(tools) = tools {
808            body["tools"] =
809                serde_json::to_value(tools.iter().map(|t| t.descriptor()).collect::<Vec<_>>())?;
810        }
811        let mut request = self.chat_request(&body).build()?;
812        *request.timeout_mut() = None;
813        Ok(request)
814    }
815}
816
817#[cfg(test)]
818mod tests {
819    use super::*;
820    use std::sync::Mutex;
821
822    static ENV_MUTEX: Mutex<()> = Mutex::new(());
823
824    #[test]
825    fn test_ollama_config_default() {
826        let _guard = ENV_MUTEX.lock().unwrap();
827        std::env::remove_var("OLLAMA_HOST");
828        let config = OllamaConfig::default();
829        assert_eq!(config.host, "http://localhost:11434");
830        assert!(config.timeout.is_none());
831        assert!(config.headers.is_empty());
832    }
833
834    #[test]
835    fn test_ollama_config_from_env() {
836        let _guard = ENV_MUTEX.lock().unwrap();
837        std::env::set_var("OLLAMA_HOST", "http://custom:8080");
838        let config = OllamaConfig::default();
839        assert_eq!(config.host, "http://custom:8080");
840        std::env::remove_var("OLLAMA_HOST");
841    }
842
843    #[test]
844    fn test_ollama_config_custom() {
845        let mut headers = HashMap::new();
846        headers.insert("X-Custom".to_string(), "value".to_string());
847
848        let config = OllamaConfig {
849            host: "http://test:9999".to_string(),
850            timeout: Some(std::time::Duration::from_secs(30)),
851            headers,
852        };
853
854        assert_eq!(config.host, "http://test:9999");
855        assert_eq!(config.timeout, Some(std::time::Duration::from_secs(30)));
856        assert_eq!(config.headers.get("X-Custom"), Some(&"value".to_string()));
857    }
858
859    #[test]
860    fn test_gateway_new() {
861        let gateway = OllamaGateway::new();
862        assert_eq!(gateway.config.host, "http://localhost:11434");
863    }
864
865    #[test]
866    fn test_gateway_with_host() {
867        let gateway = OllamaGateway::with_host("http://example.com:8080");
868        assert_eq!(gateway.config.host, "http://example.com:8080");
869    }
870
871    #[test]
872    fn test_gateway_with_config() {
873        let config = OllamaConfig {
874            host: "http://custom:5000".to_string(),
875            timeout: Some(std::time::Duration::from_secs(60)),
876            headers: HashMap::new(),
877        };
878
879        let gateway = OllamaGateway::with_config(config);
880        assert_eq!(gateway.config.host, "http://custom:5000");
881    }
882
883    #[test]
884    fn test_gateway_default() {
885        let gateway = OllamaGateway::default();
886        assert_eq!(gateway.config.host, "http://localhost:11434");
887    }
888
889    #[test]
890    fn test_adapt_messages_to_ollama_simple() {
891        let messages = vec![
892            LlmMessage::system("You are helpful"),
893            LlmMessage::user("Hello"),
894            LlmMessage::assistant("Hi there"),
895        ];
896
897        let result = adapt_messages_to_ollama(&messages).unwrap();
898
899        assert_eq!(result.len(), 3);
900        assert_eq!(result[0]["role"], "system");
901        assert_eq!(result[0]["content"], "You are helpful");
902        assert_eq!(result[1]["role"], "user");
903        assert_eq!(result[1]["content"], "Hello");
904        assert_eq!(result[2]["role"], "assistant");
905        assert_eq!(result[2]["content"], "Hi there");
906    }
907
908    #[test]
909    fn test_adapt_messages_with_images() {
910        use std::io::Write;
911        use tempfile::NamedTempFile;
912
913        // Create temporary test image files
914        let mut temp_file1 = NamedTempFile::new().unwrap();
915        let mut temp_file2 = NamedTempFile::new().unwrap();
916        temp_file1.write_all(b"fake_image_data_1").unwrap();
917        temp_file2.write_all(b"fake_image_data_2").unwrap();
918
919        // Get paths as strings
920        let path1 = temp_file1.path().to_string_lossy().to_string();
921        let path2 = temp_file2.path().to_string_lossy().to_string();
922
923        // Expected base64 encodings
924        let expected_base64_1 = base64::Engine::encode(
925            &base64::engine::general_purpose::STANDARD,
926            b"fake_image_data_1",
927        );
928        let expected_base64_2 = base64::Engine::encode(
929            &base64::engine::general_purpose::STANDARD,
930            b"fake_image_data_2",
931        );
932
933        let messages = vec![LlmMessage::user("Describe this").with_images(vec![path1, path2])];
934
935        let result = adapt_messages_to_ollama(&messages).unwrap();
936
937        assert_eq!(result.len(), 1);
938        assert_eq!(result[0]["role"], "user");
939        // Images should be base64-encoded
940        assert_eq!(result[0]["images"][0], expected_base64_1);
941        assert_eq!(result[0]["images"][1], expected_base64_2);
942    }
943
944    #[test]
945    fn test_adapt_messages_with_tool_calls() {
946        let tool_call = LlmToolCall {
947            id: Some("call_123".to_string()),
948            name: "test_function".to_string(),
949            arguments: {
950                let mut map = HashMap::new();
951                map.insert("arg1".to_string(), serde_json::json!("value1"));
952                map
953            },
954        };
955
956        let messages = vec![LlmMessage {
957            role: MessageRole::Assistant,
958            content: None,
959            tool_calls: Some(vec![tool_call]),
960            image_paths: None,
961            reasoning_content: None,
962        }];
963
964        let result = adapt_messages_to_ollama(&messages).unwrap();
965
966        assert_eq!(result.len(), 1);
967        assert_eq!(result[0]["role"], "assistant");
968        assert_eq!(result[0]["tool_calls"][0]["type"], "function");
969        assert_eq!(result[0]["tool_calls"][0]["function"]["name"], "test_function");
970    }
971
972    #[test]
973    fn test_adapt_messages_empty_content() {
974        let messages = vec![LlmMessage {
975            role: MessageRole::User,
976            content: None,
977            tool_calls: None,
978            image_paths: None,
979            reasoning_content: None,
980        }];
981
982        let result = adapt_messages_to_ollama(&messages).unwrap();
983
984        assert_eq!(result.len(), 1);
985        assert_eq!(result[0]["content"], "");
986    }
987
988    #[test]
989    fn test_adapt_messages_tool_role() {
990        let messages = vec![LlmMessage {
991            role: MessageRole::Tool,
992            content: Some("Tool result".to_string()),
993            tool_calls: None,
994            image_paths: None,
995            reasoning_content: None,
996        }];
997
998        let result = adapt_messages_to_ollama(&messages).unwrap();
999
1000        assert_eq!(result.len(), 1);
1001        assert_eq!(result[0]["role"], "tool");
1002        assert_eq!(result[0]["content"], "Tool result");
1003    }
1004
1005    #[test]
1006    fn test_extract_ollama_options_basic() {
1007        let config = CompletionConfig {
1008            temperature: Some(0.7),
1009            num_ctx: Some(4096),
1010            max_tokens: Some(2048),
1011            top_p: None,
1012            top_k: None,
1013            response_format: None,
1014            reasoning_effort: None,
1015            reasoning_profile: None,
1016            preserve_reasoning: false,
1017            max_tool_iterations: 10,
1018        };
1019
1020        let options = extract_ollama_options(&config);
1021
1022        // Use as_f64 for floating point comparison
1023        assert!((options["temperature"].as_f64().unwrap() - 0.7).abs() < 0.01);
1024        assert_eq!(options["num_ctx"], 4096);
1025        // The sole output limit uses Ollama's provider field name.
1026        assert_eq!(options["num_predict"], 2048);
1027    }
1028
1029    #[test]
1030    fn test_extract_ollama_options_output_limit() {
1031        let config = CompletionConfig {
1032            temperature: Some(0.5),
1033            num_ctx: Some(2048),
1034            max_tokens: Some(1000),
1035            top_p: None,
1036            top_k: None,
1037            response_format: None,
1038            reasoning_effort: None,
1039            reasoning_profile: None,
1040            preserve_reasoning: false,
1041            max_tool_iterations: 10,
1042        };
1043
1044        let options = extract_ollama_options(&config);
1045
1046        assert!((options["temperature"].as_f64().unwrap() - 0.5).abs() < 0.01);
1047        assert_eq!(options["num_ctx"], 2048);
1048        // There is no separate configuration field to override max_tokens.
1049        assert_eq!(options["num_predict"], 1000);
1050    }
1051
1052    #[test]
1053    fn test_extract_ollama_options_unset_output_limit() {
1054        let config = CompletionConfig {
1055            temperature: Some(1.0),
1056            num_ctx: Some(8192),
1057            max_tokens: None,
1058            top_p: None,
1059            top_k: None,
1060            response_format: None,
1061            reasoning_effort: None,
1062            reasoning_profile: None,
1063            preserve_reasoning: false,
1064            max_tool_iterations: 10,
1065        };
1066
1067        let options = extract_ollama_options(&config);
1068
1069        assert!((options["temperature"].as_f64().unwrap() - 1.0).abs() < 0.01);
1070        assert_eq!(options["num_ctx"], 8192);
1071        assert!(options.get("num_predict").is_none());
1072    }
1073
1074    #[test]
1075    fn test_extract_ollama_options_zero_max_tokens() {
1076        let config = CompletionConfig {
1077            temperature: Some(0.8),
1078            num_ctx: Some(1024),
1079            max_tokens: Some(0),
1080            top_p: None,
1081            top_k: None,
1082            response_format: None,
1083            reasoning_effort: None,
1084            reasoning_profile: None,
1085            preserve_reasoning: false,
1086            max_tool_iterations: 10,
1087        };
1088
1089        let options = extract_ollama_options(&config);
1090
1091        assert!((options["temperature"].as_f64().unwrap() - 0.8).abs() < 0.01);
1092        assert_eq!(options["num_ctx"], 1024);
1093        assert_eq!(options["num_predict"], 0);
1094    }
1095
1096    #[test]
1097    fn test_extract_ollama_options_with_top_p() {
1098        let config = CompletionConfig {
1099            temperature: Some(0.7),
1100            num_ctx: Some(4096),
1101            max_tokens: Some(2048),
1102            top_p: Some(0.9),
1103            top_k: None,
1104            response_format: None,
1105            reasoning_effort: None,
1106            reasoning_profile: None,
1107            preserve_reasoning: false,
1108            max_tool_iterations: 10,
1109        };
1110
1111        let options = extract_ollama_options(&config);
1112
1113        assert!((options["temperature"].as_f64().unwrap() - 0.7).abs() < 0.01);
1114        assert_eq!(options["num_ctx"], 4096);
1115        assert!((options["top_p"].as_f64().unwrap() - 0.9).abs() < 0.01);
1116        assert!(options.get("top_k").is_none());
1117    }
1118
1119    #[test]
1120    fn test_extract_ollama_options_with_top_k() {
1121        let config = CompletionConfig {
1122            temperature: Some(0.8),
1123            num_ctx: Some(2048),
1124            max_tokens: Some(1024),
1125            top_p: None,
1126            top_k: Some(40),
1127            response_format: None,
1128            reasoning_effort: None,
1129            reasoning_profile: None,
1130            preserve_reasoning: false,
1131            max_tool_iterations: 10,
1132        };
1133
1134        let options = extract_ollama_options(&config);
1135
1136        assert!((options["temperature"].as_f64().unwrap() - 0.8).abs() < 0.01);
1137        assert_eq!(options["top_k"], 40);
1138        assert!(options.get("top_p").is_none());
1139    }
1140
1141    #[test]
1142    fn test_extract_ollama_options_with_all_sampling_params() {
1143        let config = CompletionConfig {
1144            temperature: Some(0.6),
1145            num_ctx: Some(8192),
1146            max_tokens: Some(4096),
1147            top_p: Some(0.95),
1148            top_k: Some(50),
1149            response_format: None,
1150            reasoning_effort: None,
1151            reasoning_profile: None,
1152            preserve_reasoning: false,
1153            max_tool_iterations: 10,
1154        };
1155
1156        let options = extract_ollama_options(&config);
1157
1158        assert!((options["temperature"].as_f64().unwrap() - 0.6).abs() < 0.01);
1159        assert_eq!(options["num_ctx"], 8192);
1160        assert_eq!(options["num_predict"], 4096);
1161        assert!((options["top_p"].as_f64().unwrap() - 0.95).abs() < 0.01);
1162        assert_eq!(options["top_k"], 50);
1163    }
1164
1165    #[test]
1166    fn test_add_response_format_text() {
1167        use crate::llm::gateway::ResponseFormat;
1168
1169        let config = CompletionConfig {
1170            temperature: Some(0.7),
1171            num_ctx: Some(4096),
1172            max_tokens: Some(2048),
1173            top_p: None,
1174            top_k: None,
1175            response_format: Some(ResponseFormat::Text),
1176            reasoning_effort: None,
1177            reasoning_profile: None,
1178            preserve_reasoning: false,
1179            max_tool_iterations: 10,
1180        };
1181
1182        let mut body = serde_json::json!({
1183            "model": "test",
1184            "messages": []
1185        });
1186
1187        add_response_format(&mut body, &config);
1188
1189        // Text format shouldn't add a format field
1190        assert!(body.get("format").is_none());
1191    }
1192
1193    #[test]
1194    fn test_add_response_format_json_no_schema() {
1195        use crate::llm::gateway::ResponseFormat;
1196
1197        let config = CompletionConfig {
1198            temperature: Some(0.7),
1199            num_ctx: Some(4096),
1200            max_tokens: Some(2048),
1201            top_p: None,
1202            top_k: None,
1203            response_format: Some(ResponseFormat::JsonObject { schema: None }),
1204            reasoning_effort: None,
1205            reasoning_profile: None,
1206            preserve_reasoning: false,
1207            max_tool_iterations: 10,
1208        };
1209
1210        let mut body = serde_json::json!({
1211            "model": "test",
1212            "messages": []
1213        });
1214
1215        add_response_format(&mut body, &config);
1216
1217        assert_eq!(body["format"], "json");
1218    }
1219
1220    #[test]
1221    fn test_add_response_format_json_with_schema() {
1222        use crate::llm::gateway::ResponseFormat;
1223
1224        let schema = serde_json::json!({
1225            "type": "object",
1226            "properties": {
1227                "name": {"type": "string"},
1228                "age": {"type": "number"}
1229            }
1230        });
1231
1232        let config = CompletionConfig {
1233            temperature: Some(0.7),
1234            num_ctx: Some(4096),
1235            max_tokens: Some(2048),
1236            top_p: None,
1237            top_k: None,
1238            response_format: Some(ResponseFormat::JsonObject {
1239                schema: Some(schema.clone()),
1240            }),
1241            reasoning_effort: None,
1242            reasoning_profile: None,
1243            preserve_reasoning: false,
1244            max_tool_iterations: 10,
1245        };
1246
1247        let mut body = serde_json::json!({
1248            "model": "test",
1249            "messages": []
1250        });
1251
1252        add_response_format(&mut body, &config);
1253
1254        assert_eq!(body["format"], schema);
1255    }
1256
1257    #[test]
1258    fn test_add_response_format_none() {
1259        let config = CompletionConfig {
1260            temperature: Some(0.7),
1261            num_ctx: Some(4096),
1262            max_tokens: Some(2048),
1263            top_p: None,
1264            top_k: None,
1265            response_format: None,
1266            reasoning_effort: None,
1267            reasoning_profile: None,
1268            preserve_reasoning: false,
1269            max_tool_iterations: 10,
1270        };
1271
1272        let mut body = serde_json::json!({
1273            "model": "test",
1274            "messages": []
1275        });
1276
1277        add_response_format(&mut body, &config);
1278
1279        // No format should be added when response_format is None
1280        assert!(body.get("format").is_none());
1281    }
1282
1283    /// Send one streaming request and return the JSON body the server received.
1284    async fn streamed_request_body(config: CompletionConfig) -> Value {
1285        let captured = std::sync::Arc::new(Mutex::new(None::<Value>));
1286        let sink = captured.clone();
1287        let mut server = mockito::Server::new_async().await;
1288        let mock = server
1289            .mock("POST", "/api/chat")
1290            .match_request(move |request| {
1291                let body = serde_json::from_slice(request.body().expect("request body"))
1292                    .expect("JSON request body");
1293                *sink.lock().unwrap() = Some(body);
1294                true
1295            })
1296            .with_status(200)
1297            .with_body("{\"done\":true,\"done_reason\":\"stop\"}\n")
1298            .create_async()
1299            .await;
1300
1301        let gateway = OllamaGateway::with_host(server.url());
1302        let messages = vec![LlmMessage::user("Hi")];
1303        let mut stream = gateway.complete_stream("qwen3:32b", &messages, None, &config);
1304        while stream.next().await.is_some() {}
1305
1306        mock.assert_async().await;
1307        let body = captured.lock().unwrap().take();
1308        body.expect("streaming request body was captured")
1309    }
1310
1311    fn config_with_format(format: Option<crate::llm::gateway::ResponseFormat>) -> CompletionConfig {
1312        CompletionConfig {
1313            response_format: format,
1314            ..Default::default()
1315        }
1316    }
1317
1318    #[tokio::test]
1319    async fn streaming_request_without_format_leaves_body_unchanged() {
1320        let body = streamed_request_body(config_with_format(None)).await;
1321
1322        assert_eq!(body["stream"], true);
1323        assert!(body.get("format").is_none());
1324    }
1325
1326    #[tokio::test]
1327    async fn streaming_request_omits_format_for_text() {
1328        use crate::llm::gateway::ResponseFormat;
1329
1330        let body = streamed_request_body(config_with_format(Some(ResponseFormat::Text))).await;
1331
1332        assert!(body.get("format").is_none());
1333    }
1334
1335    #[tokio::test]
1336    async fn streaming_request_forwards_json_object_format() {
1337        use crate::llm::gateway::ResponseFormat;
1338
1339        let body = streamed_request_body(config_with_format(Some(ResponseFormat::JsonObject {
1340            schema: None,
1341        })))
1342        .await;
1343
1344        assert_eq!(body["format"], "json");
1345    }
1346
1347    #[tokio::test]
1348    async fn streaming_request_forwards_json_schema_format() {
1349        use crate::llm::gateway::ResponseFormat;
1350
1351        let schema =
1352            serde_json::json!({"type": "object", "properties": {"n": {"type": "integer"}}});
1353        let body = streamed_request_body(config_with_format(Some(ResponseFormat::JsonObject {
1354            schema: Some(schema.clone()),
1355        })))
1356        .await;
1357
1358        assert_eq!(body["format"], schema);
1359    }
1360
1361    #[tokio::test]
1362    async fn complete_carries_reported_evidence_unchanged() {
1363        let mut server = mockito::Server::new_async().await;
1364        let mock = server
1365            .mock("POST", "/api/chat")
1366            .with_status(200)
1367            .with_body(
1368                r#"{"model":"qwen3:32b","created_at":"2026-09-24T10:00:00Z",
1369                    "message":{"role":"assistant","content":"Hi"},
1370                    "done":true,"done_reason":"length",
1371                    "total_duration":900,"load_duration":100,
1372                    "prompt_eval_count":12,"prompt_eval_duration":200,
1373                    "eval_count":34,"eval_duration":600}"#,
1374            )
1375            .create_async()
1376            .await;
1377
1378        let gateway = OllamaGateway::with_host(server.url());
1379        let messages = vec![LlmMessage::user("Hi")];
1380
1381        let response = gateway
1382            .complete("qwen3", &messages, None, &CompletionConfig::default())
1383            .await
1384            .unwrap();
1385
1386        mock.assert_async().await;
1387        let evidence = response.evidence;
1388        assert_eq!(evidence.provider_model.as_deref(), Some("qwen3:32b"));
1389        assert_eq!(evidence.finish_reason.as_deref(), Some("length"));
1390        assert_eq!(
1391            evidence.usage,
1392            Some(serde_json::json!({"prompt_eval_count": 12, "eval_count": 34}))
1393        );
1394        assert_eq!(evidence.metadata["total_duration"], 900);
1395        assert_eq!(evidence.metadata["load_duration"], 100);
1396        assert_eq!(evidence.metadata["prompt_eval_duration"], 200);
1397        assert_eq!(evidence.metadata["eval_duration"], 600);
1398        assert_eq!(evidence.metadata["created_at"], "2026-09-24T10:00:00Z");
1399    }
1400
1401    #[tokio::test]
1402    async fn complete_leaves_unreported_usage_unknown() {
1403        let mut server = mockito::Server::new_async().await;
1404        let mock = server
1405            .mock("POST", "/api/chat")
1406            .with_status(200)
1407            .with_body(r#"{"message":{"role":"assistant","content":"Hi"},"done":true}"#)
1408            .create_async()
1409            .await;
1410
1411        let gateway = OllamaGateway::with_host(server.url());
1412        let messages = vec![LlmMessage::user("Hi")];
1413
1414        let response = gateway
1415            .complete("qwen3", &messages, None, &CompletionConfig::default())
1416            .await
1417            .unwrap();
1418
1419        mock.assert_async().await;
1420        assert_eq!(response.evidence, crate::llm::models::ResponseEvidence::default());
1421    }
1422
1423    #[tokio::test]
1424    async fn complete_json_response_carries_object_and_evidence() {
1425        let mut server = mockito::Server::new_async().await;
1426        let mock = server
1427            .mock("POST", "/api/chat")
1428            .with_status(200)
1429            .with_body(
1430                r#"{"model":"qwen3:32b","message":{"role":"assistant","content":"{\"n\":1}"},
1431                    "done":true,"done_reason":"stop","prompt_eval_count":3,"eval_count":4}"#,
1432            )
1433            .create_async()
1434            .await;
1435
1436        let gateway = OllamaGateway::with_host(server.url());
1437        let messages = vec![LlmMessage::user("JSON")];
1438
1439        let response = gateway
1440            .complete_json_response(
1441                "qwen3",
1442                &messages,
1443                serde_json::json!({"type": "object"}),
1444                &CompletionConfig::default(),
1445            )
1446            .await
1447            .unwrap();
1448
1449        mock.assert_async().await;
1450        assert_eq!(response.object, Some(serde_json::json!({"n": 1})));
1451        assert_eq!(response.evidence.finish_reason.as_deref(), Some("stop"));
1452        assert_eq!(
1453            response.evidence.usage,
1454            Some(serde_json::json!({"prompt_eval_count": 3, "eval_count": 4}))
1455        );
1456    }
1457
1458    mod stream_events {
1459        use super::*;
1460        use crate::llm::stream_events::testing::endless_stream_server;
1461        use crate::llm::{StreamEvent, StreamEventError};
1462
1463        async fn collect_events(
1464            server: &mockito::Server,
1465            config: &CompletionConfig,
1466        ) -> Vec<StreamEvent> {
1467            let gateway = OllamaGateway::with_host(server.url());
1468            let messages = vec![LlmMessage::user("Hi")];
1469            let stream = gateway
1470                .complete_stream_events("qwen3:32b", &messages, config)
1471                .expect("Ollama supports stream events");
1472            let events: Vec<_> = stream.collect().await;
1473            let metrics: Vec<_> = events
1474                .iter()
1475                .filter_map(|e| {
1476                    if let StreamEvent::Metrics(m) = e {
1477                        Some(m)
1478                    } else {
1479                        None
1480                    }
1481                })
1482                .collect();
1483            assert_eq!(metrics.len(), 1);
1484            assert!(metrics[0].request_duration_ms.is_some());
1485            // These tests retain their provider/terminal assertions; section2's
1486            // public contract matrix asserts exact client timing separately.
1487            events
1488                .into_iter()
1489                .filter(|event| !matches!(event, StreamEvent::Metrics(_)))
1490                .collect()
1491        }
1492
1493        #[tokio::test]
1494        async fn request_streams_without_tools_and_honours_config() {
1495            use crate::llm::gateway::{ReasoningEffort, ResponseFormat};
1496
1497            let captured = std::sync::Arc::new(Mutex::new(None::<Value>));
1498            let sink = captured.clone();
1499            let mut server = mockito::Server::new_async().await;
1500            let mock = server
1501                .mock("POST", "/api/chat")
1502                .match_request(move |request| {
1503                    *sink.lock().unwrap() =
1504                        serde_json::from_slice(request.body().expect("request body")).ok();
1505                    true
1506                })
1507                .with_status(200)
1508                .with_body("{\"done\":true,\"done_reason\":\"stop\"}\n")
1509                .create_async()
1510                .await;
1511            let config = CompletionConfig {
1512                reasoning_effort: Some(ReasoningEffort::Disabled),
1513                response_format: Some(ResponseFormat::JsonObject { schema: None }),
1514                ..Default::default()
1515            };
1516
1517            collect_events(&server, &config).await;
1518
1519            mock.assert_async().await;
1520            let body = captured.lock().unwrap().take().expect("captured body");
1521            assert_eq!(body["model"], "qwen3:32b");
1522            assert_eq!(body["stream"], true);
1523            assert_eq!(body["think"], false);
1524            assert_eq!(body["format"], "json");
1525            assert!(body.get("tools").is_none());
1526        }
1527
1528        #[tokio::test]
1529        async fn completes_over_http_with_durations() {
1530            let mut server = mockito::Server::new_async().await;
1531            let mock = server
1532                .mock("POST", "/api/chat")
1533                .with_status(200)
1534                .with_body(concat!(
1535                    "{\"message\":{\"content\":\"Hi\"},\"done\":false}\n",
1536                    "{\"model\":\"qwen3:32b\",\"done\":true,\"done_reason\":\"stop\",",
1537                    "\"total_duration\":900,\"eval_count\":2}\n",
1538                ))
1539                .create_async()
1540                .await;
1541
1542            let events = collect_events(&server, &CompletionConfig::default()).await;
1543
1544            mock.assert_async().await;
1545            assert!(matches!(
1546                events.as_slice(),
1547                [StreamEvent::Content(text), StreamEvent::Completed(evidence)]
1548                    if text == "Hi"
1549                        && evidence.metadata["total_duration"] == 900
1550                        && evidence.usage == Some(serde_json::json!({"eval_count": 2}))
1551            ));
1552        }
1553
1554        #[tokio::test]
1555        async fn end_of_stream_without_done_is_incomplete_stream() {
1556            let mut server = mockito::Server::new_async().await;
1557            let _mock = server
1558                .mock("POST", "/api/chat")
1559                .with_status(200)
1560                .with_body("{\"message\":{\"content\":\"Hi\"},\"done\":false}\n")
1561                .create_async()
1562                .await;
1563
1564            let events = collect_events(&server, &CompletionConfig::default()).await;
1565
1566            assert!(matches!(
1567                events.as_slice(),
1568                [
1569                    StreamEvent::Content(_),
1570                    StreamEvent::Error(StreamEventError::IncompleteStream(_))
1571                ]
1572            ));
1573        }
1574
1575        #[tokio::test]
1576        async fn http_error_status_is_reported_with_body() {
1577            let mut server = mockito::Server::new_async().await;
1578            let _mock = server
1579                .mock("POST", "/api/chat")
1580                .with_status(404)
1581                .with_body(r#"{"error":"model 'qwen3:32b' not found"}"#)
1582                .create_async()
1583                .await;
1584
1585            let events = collect_events(&server, &CompletionConfig::default()).await;
1586
1587            assert!(matches!(
1588                events.as_slice(),
1589                [StreamEvent::Error(StreamEventError::ProviderError { status: Some(404), error })]
1590                    if error == "model 'qwen3:32b' not found"
1591            ));
1592        }
1593
1594        #[tokio::test]
1595        async fn dropping_the_stream_cancels_the_request() {
1596            let (url, closed) =
1597                endless_stream_server("{\"message\":{\"content\":\"Hi\"},\"done\":false}\n").await;
1598            let gateway = OllamaGateway::with_host(url);
1599            let messages = vec![LlmMessage::user("Hi")];
1600            let config = CompletionConfig::default();
1601
1602            let mut stream = gateway
1603                .complete_stream_events("qwen3:32b", &messages, &config)
1604                .expect("supported");
1605            assert!(matches!(stream.next().await, Some(StreamEvent::Content(_))));
1606            drop(stream);
1607
1608            tokio::time::timeout(std::time::Duration::from_secs(5), closed)
1609                .await
1610                .expect("the request was not cancelled")
1611                .expect("server reported the disconnect");
1612        }
1613    }
1614
1615    #[tokio::test]
1616    async fn test_pull_model_success() {
1617        let mut server = mockito::Server::new_async().await;
1618        let mock = server
1619            .mock("POST", "/api/pull")
1620            .with_status(200)
1621            .with_body(r#"{"status":"success"}"#)
1622            .create();
1623
1624        let gateway = OllamaGateway::with_host(server.url());
1625        let result = gateway.pull_model("llama2").await;
1626
1627        mock.assert();
1628        assert!(result.is_ok());
1629    }
1630
1631    #[tokio::test]
1632    async fn test_pull_model_failure() {
1633        let mut server = mockito::Server::new_async().await;
1634        let mock = server.mock("POST", "/api/pull").with_status(404).create();
1635
1636        let gateway = OllamaGateway::with_host(server.url());
1637        let result = gateway.pull_model("nonexistent").await;
1638
1639        mock.assert();
1640        assert!(result.is_err());
1641    }
1642
1643    #[tokio::test]
1644    async fn test_complete_simple() {
1645        let mut server = mockito::Server::new_async().await;
1646        let mock = server
1647            .mock("POST", "/api/chat")
1648            .with_status(200)
1649            .with_body(r#"{"message":{"role":"assistant","content":"Hello!"}}"#)
1650            .create();
1651
1652        let gateway = OllamaGateway::with_host(server.url());
1653        let messages = vec![LlmMessage::user("Hi")];
1654        let config = CompletionConfig::default();
1655
1656        let result = gateway.complete("llama2", &messages, None, &config).await;
1657
1658        mock.assert();
1659        assert!(result.is_ok());
1660        let response = result.unwrap();
1661        assert_eq!(response.content, Some("Hello!".to_string()));
1662        assert_eq!(response.thinking, None);
1663    }
1664
1665    #[tokio::test]
1666    async fn test_complete_with_tools() {
1667        let mut server = mockito::Server::new_async().await;
1668        let mock = server
1669            .mock("POST", "/api/chat")
1670            .match_body(mockito::Matcher::JsonString(
1671                r#"{"model":"llama2","messages":[{"role":"user","content":"Hi"}],"options":{},"stream":false,"tools":[{"type":"function","function":{"name":"test_tool","description":"A test","parameters":{}}}]}"#.to_string()
1672            ))
1673            .with_status(200)
1674            .with_body(r#"{"message":{"role":"assistant","content":"Result"}}"#)
1675            .create();
1676
1677        let gateway = OllamaGateway::with_host(server.url());
1678        let messages = vec![LlmMessage::user("Hi")];
1679        let config = CompletionConfig::default();
1680
1681        use crate::llm::tools::{FunctionDescriptor, LlmTool, ToolDescriptor};
1682
1683        #[derive(Clone)]
1684        struct MockTool;
1685        #[async_trait]
1686        impl LlmTool for MockTool {
1687            async fn run(
1688                &self,
1689                _args: &HashMap<String, Value>,
1690                _ctx: &crate::llm::tools::ToolRunCtx,
1691            ) -> Result<Value> {
1692                Ok(serde_json::json!({}))
1693            }
1694            fn descriptor(&self) -> ToolDescriptor {
1695                ToolDescriptor {
1696                    r#type: "function".to_string(),
1697                    function: FunctionDescriptor {
1698                        name: "test_tool".to_string(),
1699                        description: "A test".to_string(),
1700                        parameters: serde_json::json!({}),
1701                    },
1702                }
1703            }
1704            fn clone_box(&self) -> Box<dyn LlmTool> {
1705                Box::new(self.clone())
1706            }
1707        }
1708
1709        let tools: Vec<Box<dyn LlmTool>> = vec![Box::new(MockTool)];
1710        let result = gateway.complete("llama2", &messages, Some(&tools), &config).await;
1711
1712        mock.assert();
1713        assert!(result.is_ok());
1714    }
1715
1716    #[tokio::test]
1717    async fn test_complete_error() {
1718        let mut server = mockito::Server::new_async().await;
1719        let mock = server.mock("POST", "/api/chat").with_status(500).create();
1720
1721        let gateway = OllamaGateway::with_host(server.url());
1722        let messages = vec![LlmMessage::user("Hi")];
1723        let config = CompletionConfig::default();
1724
1725        let result = gateway.complete("llama2", &messages, None, &config).await;
1726
1727        mock.assert();
1728        assert!(result.is_err());
1729    }
1730
1731    #[tokio::test]
1732    async fn test_complete_json() {
1733        let mut server = mockito::Server::new_async().await;
1734        let mock = server
1735            .mock("POST", "/api/chat")
1736            .with_status(200)
1737            .with_body(r#"{"message":{"content":"{\"name\":\"test\",\"value\":42}"}}"#)
1738            .create();
1739
1740        let gateway = OllamaGateway::with_host(server.url());
1741        let messages = vec![LlmMessage::user("Generate JSON")];
1742        let schema = serde_json::json!({"type": "object"});
1743        let config = CompletionConfig::default();
1744
1745        let result = gateway.complete_json("llama2", &messages, schema, &config).await;
1746
1747        mock.assert();
1748        assert!(result.is_ok());
1749        let json = result.unwrap();
1750        assert_eq!(json["name"], "test");
1751        assert_eq!(json["value"], 42);
1752    }
1753
1754    #[tokio::test]
1755    async fn test_get_available_models() {
1756        let mut server = mockito::Server::new_async().await;
1757        let mock = server
1758            .mock("GET", "/api/tags")
1759            .with_status(200)
1760            .with_body(r#"{"models":[{"name":"llama2"},{"name":"mistral"}]}"#)
1761            .create();
1762
1763        let gateway = OllamaGateway::with_host(server.url());
1764        let result = gateway.get_available_models().await;
1765
1766        mock.assert();
1767        assert!(result.is_ok());
1768        let models = result.unwrap();
1769        assert_eq!(models.len(), 2);
1770        assert!(models.contains(&"llama2".to_string()));
1771        assert!(models.contains(&"mistral".to_string()));
1772    }
1773
1774    #[tokio::test]
1775    async fn test_calculate_embeddings() {
1776        let mut server = mockito::Server::new_async().await;
1777        let mock = server
1778            .mock("POST", "/api/embeddings")
1779            .with_status(200)
1780            .with_body(r#"{"embedding":[0.1,0.2,0.3,0.4]}"#)
1781            .create();
1782
1783        let gateway = OllamaGateway::with_host(server.url());
1784        let result = gateway.calculate_embeddings("test text", None).await;
1785
1786        mock.assert();
1787        assert!(result.is_ok());
1788        let embeddings = result.unwrap();
1789        assert_eq!(embeddings.len(), 4);
1790        assert_eq!(embeddings[0], 0.1);
1791        assert_eq!(embeddings[3], 0.4);
1792    }
1793
1794    #[tokio::test]
1795    async fn test_calculate_embeddings_custom_model() {
1796        let mut server = mockito::Server::new_async().await;
1797        let mock = server
1798            .mock("POST", "/api/embeddings")
1799            .match_body(mockito::Matcher::JsonString(
1800                r#"{"model":"custom-embed","prompt":"test"}"#.to_string(),
1801            ))
1802            .with_status(200)
1803            .with_body(r#"{"embedding":[0.5,0.6]}"#)
1804            .create();
1805
1806        let gateway = OllamaGateway::with_host(server.url());
1807        let result = gateway.calculate_embeddings("test", Some("custom-embed")).await;
1808
1809        mock.assert();
1810        assert!(result.is_ok());
1811    }
1812
1813    #[tokio::test]
1814    async fn test_complete_with_reasoning_effort() {
1815        use crate::llm::gateway::ReasoningEffort;
1816
1817        let mut server = mockito::Server::new_async().await;
1818        let mock = server
1819            .mock("POST", "/api/chat")
1820            .match_body(mockito::Matcher::PartialJson(
1821                serde_json::json!({"think": "high"}),
1822            ))
1823            .with_status(200)
1824            .with_body(r#"{"message":{"role":"assistant","content":"Response","thinking":"Internal reasoning..."}}"#)
1825            .create();
1826
1827        let gateway = OllamaGateway::with_host(server.url());
1828        let messages = vec![LlmMessage::user("Test")];
1829        let config = CompletionConfig {
1830            reasoning_effort: Some(ReasoningEffort::High),
1831            ..Default::default()
1832        };
1833
1834        let result = gateway.complete("qwen3:32b", &messages, None, &config).await;
1835
1836        mock.assert();
1837        assert!(result.is_ok());
1838        let response = result.unwrap();
1839        assert_eq!(response.content, Some("Response".to_string()));
1840        assert_eq!(response.thinking, Some("Internal reasoning...".to_string()));
1841    }
1842
1843    #[tokio::test]
1844    async fn test_complete_stream_disables_thinking() {
1845        use crate::llm::gateway::ReasoningEffort;
1846
1847        let mut server = mockito::Server::new_async().await;
1848        let mock = server
1849            .mock("POST", "/api/chat")
1850            .match_body(mockito::Matcher::PartialJson(
1851                serde_json::json!({"stream": true, "think": false}),
1852            ))
1853            .with_status(200)
1854            .with_body(
1855                r#"{"message":{"role":"assistant","content":"Final"},"done":false}
1856{"done":true,"eval_count":1,"eval_duration":1000000000}
1857"#,
1858            )
1859            .create();
1860
1861        let gateway = OllamaGateway::with_host(server.url());
1862        let messages = vec![LlmMessage::user("Act")];
1863        let config = CompletionConfig {
1864            reasoning_effort: Some(ReasoningEffort::Disabled),
1865            ..Default::default()
1866        };
1867
1868        let mut stream = gateway.complete_stream("qwen3:32b", &messages, None, &config);
1869        let mut chunks = Vec::new();
1870        while let Some(chunk) = stream.next().await {
1871            chunks.push(chunk.unwrap());
1872        }
1873
1874        mock.assert();
1875        assert!(chunks
1876            .iter()
1877            .any(|chunk| matches!(chunk, StreamChunk::Content(content) if content == "Final")));
1878        assert!(!chunks.iter().any(|chunk| matches!(chunk, StreamChunk::Thinking(_))));
1879    }
1880
1881    #[tokio::test]
1882    async fn test_complete_stream_surfaces_thinking_chunks() {
1883        use crate::llm::gateway::ReasoningEffort;
1884
1885        let mut server = mockito::Server::new_async().await;
1886        let mock = server
1887            .mock("POST", "/api/chat")
1888            .match_body(mockito::Matcher::PartialJson(
1889                serde_json::json!({"stream": true, "think": "high"}),
1890            ))
1891            .with_status(200)
1892            .with_body(
1893                r#"{"message":{"role":"assistant","thinking":"Internal reasoning...","content":""},"done":false}
1894{"message":{"role":"assistant","content":"Final"},"done":false}
1895{"done":true,"eval_count":2,"eval_duration":1000000000}
1896"#,
1897            )
1898            .create();
1899
1900        let gateway = OllamaGateway::with_host(server.url());
1901        let messages = vec![LlmMessage::user("Test")];
1902        let config = CompletionConfig {
1903            reasoning_effort: Some(ReasoningEffort::High),
1904            ..Default::default()
1905        };
1906
1907        let mut stream = gateway.complete_stream("qwen3:32b", &messages, None, &config);
1908        let mut chunks = Vec::new();
1909        while let Some(chunk) = stream.next().await {
1910            chunks.push(chunk.unwrap());
1911        }
1912
1913        mock.assert();
1914        assert!(chunks.iter().any(|chunk| matches!(
1915            chunk,
1916            StreamChunk::Progress(StreamProgress {
1917                thinking_chars,
1918                ..
1919            }) if *thinking_chars == "Internal reasoning...".len()
1920        )));
1921        assert!(chunks.iter().any(|chunk| matches!(
1922            chunk,
1923            StreamChunk::Thinking(thinking) if thinking == "Internal reasoning..."
1924        )));
1925        assert!(chunks
1926            .iter()
1927            .any(|chunk| matches!(chunk, StreamChunk::Content(content) if content == "Final")));
1928        assert!(chunks.iter().any(
1929            |chunk| matches!(chunk, StreamChunk::Metrics(metrics) if metrics.eval_count == Some(2))
1930        ));
1931    }
1932}