daimon-provider-local 0.23.0

Locally-hosted model providers (Ollama, llama.cpp, llama-rs, OpenAI-compatible) for the Daimon AI agent framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//! Ollama local model provider.
//!
//! Connects to an [Ollama](https://ollama.com) instance running locally (or remotely).
//! Uses the `/api/chat` endpoint with streaming support.
//!
//! # Example
//!
//! ```ignore
//! use daimon::model::ollama::Ollama;
//! use daimon_core::{ChatRequest, Message, Model};
//!
//! let model = Ollama::new("llama3.1");
//! let response = model
//!     .generate(&ChatRequest::new(vec![Message::user("hello")]))
//!     .await?;
//! ```

use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use reqwest::Client;
use serde::{Deserialize, Serialize};

use daimon_core::stream_util::{backoff_delay, parse_retry_after_secs};
use daimon_core::{
    ChatRequest, ChatResponse, DaimonError, Message, Model, ResponseStream, Result, Role,
    StopReason, StreamEvent, ToolCall, ToolSpec, Usage,
};

use crate::openai_compat::{is_retryable_status, is_retryable_transport_error};

/// Default number of retries for transient (429 / 5xx) errors on the initial
/// request. Matches [`crate::ollama_embed`]'s `DEFAULT_MAX_RETRIES`.
const DEFAULT_MAX_RETRIES: u32 = 3;

/// Ollama model provider.
///
/// Communicates with a running Ollama server via its REST API. Defaults to
/// `http://localhost:11434` but can be configured with [`with_base_url`](Ollama::with_base_url).
pub struct Ollama {
    model: String,
    base_url: String,
    client: Client,
    timeout: Duration,
    keep_alive: Option<String>,
    max_retries: u32,
    /// Client-wide monotonic counter for synthesized tool-call ids.
    ///
    /// Ollama does not assign tool-call ids, so this provider synthesizes
    /// them as `ollama_tc_{seq}_{name}`. The counter is shared by the
    /// streaming and non-streaming paths so ids never collide across turns
    /// of a conversation (a per-response index restarts at 0 every reply).
    /// The sequence number precedes the name so the function name — which may
    /// itself contain digits and underscores — is unambiguously recoverable
    /// from an echoed `tool_call_id`.
    tool_call_seq: Arc<AtomicU64>,
}

/// Prefix used for synthesized tool-call ids (`ollama_tc_{seq}_{name}`).
const TOOL_CALL_ID_PREFIX: &str = "ollama_tc_";

/// Synthesizes a tool-call id in the `ollama_tc_{seq}_{name}` format.
fn make_tool_call_id(seq: u64, name: &str) -> String {
    format!("{TOOL_CALL_ID_PREFIX}{seq}_{name}")
}

/// Recovers the function name from a synthetic `ollama_tc_{seq}_{name}` id.
///
/// Returns `None` when the id does not follow the synthetic format; callers
/// then omit the `tool_name` field gracefully.
fn tool_name_from_call_id(id: &str) -> Option<&str> {
    let rest = id.strip_prefix(TOOL_CALL_ID_PREFIX)?;
    let (seq, name) = rest.split_once('_')?;
    if seq.is_empty() || !seq.bytes().all(|b| b.is_ascii_digit()) || name.is_empty() {
        return None;
    }
    Some(name)
}

impl Ollama {
    /// Creates a new Ollama provider for the given model name (e.g. `"llama3.1"`).
    pub fn new(model: impl Into<String>) -> Self {
        Self {
            model: model.into(),
            base_url: "http://localhost:11434".to_string(),
            // A dead or unreachable server fails fast at connect time instead
            // of blocking; the request itself is bounded by `timeout` below.
            client: Client::builder()
                .connect_timeout(Duration::from_secs(10))
                .build()
                .expect("failed to build HTTP client"),
            timeout: Duration::from_secs(300),
            keep_alive: None,
            max_retries: DEFAULT_MAX_RETRIES,
            tool_call_seq: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Overrides the Ollama server URL (default: `http://localhost:11434`).
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into().trim_end_matches('/').to_string();
        self
    }

    /// Sets the request timeout (default: 300 seconds).
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the maximum number of retries for transient (429 / 5xx) errors
    /// on the initial request (default: 3).
    pub fn with_max_retries(mut self, retries: u32) -> Self {
        self.max_retries = retries;
        self
    }

    /// Controls how long the model stays loaded in GPU memory after a request.
    ///
    /// Ollama keeps the model loaded so subsequent requests reuse the KV cache.
    /// Pass a duration string like `"5m"`, `"1h"`, or `"0"` to unload immediately.
    /// The default Ollama behaviour (when unset) is `"5m"`.
    pub fn with_keep_alive(mut self, keep_alive: impl Into<String>) -> Self {
        self.keep_alive = Some(keep_alive.into());
        self
    }

    fn build_request_body(&self, request: &ChatRequest, stream: bool) -> serde_json::Value {
        let messages: Vec<serde_json::Value> =
            request.messages.iter().map(convert_message).collect();

        let mut body = serde_json::json!({
            "model": self.model,
            "messages": messages,
            "stream": stream,
        });

        if !request.tools.is_empty() {
            let tools: Vec<serde_json::Value> =
                request.tools.iter().map(convert_tool_spec).collect();
            body["tools"] = serde_json::Value::Array(tools);
        }

        if let Some(temp) = request.temperature {
            body["options"]["temperature"] = serde_json::json!(temp);
        }

        // Ollama expresses the output-token limit as `options.num_predict`.
        // Previously `request.max_tokens` was silently dropped, so callers
        // could not bound generation length at all.
        if let Some(mt) = request.max_tokens {
            body["options"]["num_predict"] = serde_json::json!(mt);
        }

        if let Some(ref ka) = self.keep_alive {
            body["keep_alive"] = serde_json::Value::String(ka.clone());
        }

        body
    }

    /// Sends `body` to `/api/chat`, retrying transient failures — both
    /// HTTP-status (429 / 5xx) and connection-level (refused, reset, DNS,
    /// timeout) — with jittered backoff, mirroring
    /// [`crate::openai_compat::Http::post_with_retry`]'s shape. Shared by
    /// [`Model::generate`] and the handshake of [`Model::generate_stream`];
    /// once a stream is established, mid-stream failures are never retried
    /// (the consumer has already observed a partial response).
    async fn send_with_retry(&self, body: &serde_json::Value) -> Result<reqwest::Response> {
        let url = format!("{}/api/chat", self.base_url);

        for attempt in 0..=self.max_retries {
            let send_result = self
                .client
                .post(&url)
                .timeout(self.timeout)
                .json(body)
                .send()
                .await;

            let resp = match send_result {
                Ok(resp) => resp,
                Err(e) => {
                    if is_retryable_transport_error(&e) && attempt < self.max_retries {
                        let delay = backoff_delay(attempt, None);
                        tracing::debug!(
                            error = %e,
                            attempt,
                            delay_ms = delay.as_millis(),
                            "retryable Ollama transport error, backing off"
                        );
                        tokio::time::sleep(delay).await;
                        continue;
                    }
                    return Err(DaimonError::Model(e.to_string()));
                }
            };

            let status = resp.status();
            if status.is_success() {
                return Ok(resp);
            }

            let retry_after = parse_retry_after(&resp);
            let text = resp.text().await.unwrap_or_default();
            let is_retryable = is_retryable_status(status);

            if is_retryable && attempt < self.max_retries {
                let delay = backoff_delay(attempt, retry_after);
                tracing::debug!(status = %status, attempt, delay_ms = delay.as_millis(), "retryable Ollama error, backing off");
                tokio::time::sleep(delay).await;
            } else {
                return Err(DaimonError::Model(format!("Ollama {status}: {text}")));
            }
        }

        unreachable!("loop always returns or retries")
    }
}

impl Model for Ollama {
    fn model_id(&self) -> &str {
        &self.model
    }

    #[tracing::instrument(skip_all, fields(model = %self.model))]
    async fn generate(&self, request: &ChatRequest) -> Result<ChatResponse> {
        let body = self.build_request_body(request, false);
        let resp = self.send_with_retry(&body).await?;

        let response: OllamaResponse = resp
            .json()
            .await
            .map_err(|e| DaimonError::Model(e.to_string()))?;
        parse_response(response, &self.tool_call_seq)
    }

    #[tracing::instrument(skip_all, fields(model = %self.model))]
    async fn generate_stream(&self, request: &ChatRequest) -> Result<ResponseStream> {
        let body = self.build_request_body(request, true);
        let resp = self.send_with_retry(&body).await?;

        let tool_call_seq = Arc::clone(&self.tool_call_seq);
        let stream = async_stream::try_stream! {
            use futures::StreamExt;
            use daimon_core::stream_util::LineBuffer;

            let mut byte_stream = resp.bytes_stream();
            let mut buffer = LineBuffer::new();

            while let Some(chunk) = byte_stream.next().await {
                let chunk = chunk.map_err(|e| DaimonError::Model(e.to_string()))?;
                buffer.push(&chunk);

                while let Some(line) = buffer.next_line() {
                    for event in ndjson_line_events(&line, &tool_call_seq)? {
                        yield event;
                    }
                }
            }

            // Recover a final NDJSON record the server sent without a trailing
            // newline through the identical parse path used for normal lines.
            if let Some(line) = buffer.take_remaining() {
                for event in ndjson_line_events(&line, &tool_call_seq)? {
                    yield event;
                }
            }
        };

        Ok(Box::pin(stream))
    }
}

/// Extracts the `Retry-After` header (integer seconds) from an Ollama
/// response, if present.
fn parse_retry_after(resp: &reqwest::Response) -> Option<Duration> {
    resp.headers()
        .get(reqwest::header::RETRY_AFTER)
        .and_then(|v| v.to_str().ok())
        .and_then(parse_retry_after_secs)
        .map(Duration::from_secs)
}

/// Parses one NDJSON line from Ollama's `/api/chat` stream into the
/// [`StreamEvent`]s it carries.
///
/// Pure and independent of the stream so it's unit-testable; shared by both
/// the main loop and the no-trailing-newline recovery path in
/// [`Ollama::generate_stream`], which previously hand-copied this logic
/// twice.
fn ndjson_line_events(line: &str, tool_call_seq: &AtomicU64) -> Result<Vec<StreamEvent>> {
    let line = line.trim();
    let mut events = Vec::new();

    if line.is_empty() {
        return Ok(events);
    }

    let parsed: OllamaResponse =
        serde_json::from_str(line).map_err(|e| DaimonError::Model(format!("invalid JSON: {e}")))?;

    if let Some(ref msg) = parsed.message {
        if !msg.tool_calls.is_empty() {
            for tc in &msg.tool_calls {
                let seq = tool_call_seq.fetch_add(1, Ordering::Relaxed);
                let id = make_tool_call_id(seq, &tc.function.name);
                events.push(StreamEvent::ToolCallStart {
                    id: id.clone(),
                    name: tc.function.name.clone(),
                });
                let args_str = serde_json::to_string(&tc.function.arguments).unwrap_or_default();
                events.push(StreamEvent::ToolCallDelta {
                    id: id.clone(),
                    arguments_delta: args_str,
                });
                events.push(StreamEvent::ToolCallEnd { id });
            }
        }

        if let Some(ref content) = msg.content
            && !content.is_empty()
        {
            events.push(StreamEvent::TextDelta(content.clone()));
        }
    }

    if parsed.done {
        events.push(StreamEvent::Done);
    }

    Ok(events)
}

fn convert_message(msg: &Message) -> serde_json::Value {
    let role = match msg.role {
        Role::System => "system",
        Role::User => "user",
        Role::Assistant => "assistant",
        Role::Tool => "tool",
    };

    let mut obj = serde_json::json!({"role": role});

    if let Some(ref content) = msg.content {
        obj["content"] = serde_json::Value::String(content.clone());
    }

    // Newer Ollama versions use `tool_name` to attribute a tool result to the
    // function that produced it. The name is recoverable from this provider's
    // synthetic id format; for foreign ids the field is omitted gracefully.
    if msg.role == Role::Tool
        && let Some(name) = msg.tool_call_id.as_deref().and_then(tool_name_from_call_id)
    {
        obj["tool_name"] = serde_json::Value::String(name.to_string());
    }

    if !msg.tool_calls.is_empty() {
        let calls: Vec<serde_json::Value> = msg
            .tool_calls
            .iter()
            .map(|tc| {
                serde_json::json!({
                    "function": {
                        "name": tc.name,
                        "arguments": tc.arguments,
                    }
                })
            })
            .collect();
        obj["tool_calls"] = serde_json::Value::Array(calls);
    }

    obj
}

fn convert_tool_spec(spec: &ToolSpec) -> serde_json::Value {
    serde_json::json!({
        "type": "function",
        "function": {
            "name": spec.name,
            "description": spec.description,
            "parameters": spec.parameters,
        }
    })
}

fn parse_response(resp: OllamaResponse, tool_call_seq: &AtomicU64) -> Result<ChatResponse> {
    let msg = resp
        .message
        .ok_or_else(|| DaimonError::Model("missing message in Ollama response".into()))?;

    let has_tool_calls = !msg.tool_calls.is_empty();

    // Ids draw from the client-wide counter shared with the streaming path,
    // so parallel calls and repeated turns never collide (a per-response
    // index restarted at 0 on every reply).
    let tool_calls: Vec<ToolCall> = msg
        .tool_calls
        .into_iter()
        .map(|tc| {
            let seq = tool_call_seq.fetch_add(1, Ordering::Relaxed);
            ToolCall {
                id: make_tool_call_id(seq, &tc.function.name),
                name: tc.function.name,
                arguments: tc.function.arguments,
            }
        })
        .collect();

    let stop_reason = if has_tool_calls {
        StopReason::ToolUse
    } else {
        StopReason::EndTurn
    };

    let message = if tool_calls.is_empty() {
        Message::assistant(msg.content.unwrap_or_default())
    } else {
        let mut m = Message::assistant_with_tool_calls(tool_calls);
        m.content = msg.content;
        m
    };

    let usage = resp.prompt_eval_count.map(|input| Usage {
        input_tokens: input,
        output_tokens: resp.eval_count.unwrap_or(0),
        cached_tokens: 0,
    });

    Ok(ChatResponse {
        message,
        stop_reason,
        usage,
    })
}

#[derive(Deserialize)]
struct OllamaResponse {
    #[serde(default)]
    message: Option<OllamaMessage>,
    #[serde(default)]
    done: bool,
    #[serde(default)]
    prompt_eval_count: Option<u32>,
    #[serde(default)]
    eval_count: Option<u32>,
}

#[derive(Deserialize)]
struct OllamaMessage {
    #[serde(default)]
    content: Option<String>,
    #[serde(default)]
    tool_calls: Vec<OllamaToolCall>,
}

#[derive(Deserialize)]
struct OllamaToolCall {
    function: OllamaFunction,
}

#[derive(Deserialize)]
struct OllamaFunction {
    name: String,
    #[serde(default)]
    arguments: serde_json::Value,
}

#[allow(dead_code)]
#[derive(Serialize)]
struct OllamaRequest {
    model: String,
    messages: Vec<serde_json::Value>,
    stream: bool,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    tools: Vec<serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    options: Option<serde_json::Value>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ollama_new() {
        let model = Ollama::new("llama3.1");
        assert_eq!(model.model, "llama3.1");
        assert_eq!(model.base_url, "http://localhost:11434");
        assert_eq!(model.max_retries, DEFAULT_MAX_RETRIES);
    }

    #[test]
    fn test_with_base_url() {
        let model = Ollama::new("llama3.1").with_base_url("http://remote:11434/");
        assert_eq!(model.base_url, "http://remote:11434");
    }

    #[test]
    fn test_with_max_retries() {
        let model = Ollama::new("llama3.1").with_max_retries(5);
        assert_eq!(model.max_retries, 5);
    }

    #[tokio::test]
    async fn test_generate_retries_connection_level_failures_then_succeeds() {
        // Finding 1 (DAIM-33): connection-level failures (refused, reset)
        // previously bypassed retry entirely because `send()`'s error was
        // mapped straight to `Err` with `?`. This proves `generate` now
        // retries through a real server that resets the first connection(s)
        // before accepting one.
        use std::io::Write;
        use std::net::TcpListener;
        use std::sync::Arc;
        use std::sync::atomic::AtomicUsize;

        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        let accept_count = Arc::new(AtomicUsize::new(0));
        let accept_count_thread = Arc::clone(&accept_count);

        std::thread::spawn(move || {
            for stream in listener.incoming() {
                let n = accept_count_thread.fetch_add(1, Ordering::SeqCst);
                let Ok(mut stream) = stream else { continue };
                if n < 1 {
                    // First connection: reset without responding.
                    drop(stream);
                } else {
                    // Read (and discard) the client's request headers before
                    // responding — writing a response before the client has
                    // finished sending its request confuses hyper's
                    // client-side parser (observed as a spurious
                    // `Canceled`/`UnexpectedMessage` error unrelated to what
                    // this test exercises).
                    let mut reader = std::io::BufReader::new(stream.try_clone().unwrap());
                    loop {
                        use std::io::BufRead;
                        let mut line = String::new();
                        if reader.read_line(&mut line).unwrap_or(0) == 0 || line == "\r\n" {
                            break;
                        }
                    }

                    let body = r#"{"message":{"role":"assistant","content":"hi"},"done":true}"#;
                    let response = format!(
                        "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                        body.len(),
                        body
                    );
                    let _ = stream.write_all(response.as_bytes());
                    break;
                }
            }
        });

        let model = Ollama::new("llama3.1").with_base_url(format!("http://{addr}"));
        let request = ChatRequest::new(vec![Message::user("hi")]);
        let response = model
            .generate(&request)
            .await
            .expect("should succeed after retrying a connection-level failure");
        assert_eq!(response.message.content.as_deref(), Some("hi"));
        assert!(accept_count.load(Ordering::SeqCst) >= 2);
    }

    #[test]
    fn test_ndjson_line_events_text_delta() {
        let seq = AtomicU64::new(0);
        let events = ndjson_line_events(
            r#"{"message":{"role":"assistant","content":"Hel"},"done":false}"#,
            &seq,
        )
        .unwrap();
        assert_eq!(events.len(), 1);
        assert!(matches!(&events[0], StreamEvent::TextDelta(t) if t == "Hel"));
    }

    #[test]
    fn test_ndjson_line_events_tool_call() {
        let seq = AtomicU64::new(0);
        let events = ndjson_line_events(
            r#"{"message":{"role":"assistant","tool_calls":[{"function":{"name":"calc","arguments":{"x":1}}}]},"done":false}"#,
            &seq,
        )
        .unwrap();
        assert_eq!(events.len(), 3);
        assert!(matches!(
            &events[0],
            StreamEvent::ToolCallStart { id, name } if id == "ollama_tc_0_calc" && name == "calc"
        ));
        assert!(matches!(&events[1], StreamEvent::ToolCallDelta { .. }));
        assert!(matches!(
            &events[2],
            StreamEvent::ToolCallEnd { id } if id == "ollama_tc_0_calc"
        ));
        assert_eq!(seq.load(Ordering::Relaxed), 1);
    }

    #[test]
    fn test_ndjson_line_events_done() {
        let seq = AtomicU64::new(0);
        let events = ndjson_line_events(r#"{"done":true}"#, &seq).unwrap();
        assert_eq!(events.len(), 1);
        assert!(matches!(events[0], StreamEvent::Done));
    }

    #[test]
    fn test_ndjson_line_events_empty_line_ignored() {
        let seq = AtomicU64::new(0);
        assert!(ndjson_line_events("", &seq).unwrap().is_empty());
        assert!(ndjson_line_events("   ", &seq).unwrap().is_empty());
    }

    #[test]
    fn test_ndjson_line_events_invalid_json_errors() {
        let seq = AtomicU64::new(0);
        assert!(ndjson_line_events("not json", &seq).is_err());
    }

    #[test]
    fn test_ndjson_line_events_ids_do_not_collide_across_calls() {
        // The counter is shared across the whole stream (and with the
        // non-streaming path): two sequential lines through the same
        // counter must not reuse tool-call ids.
        let seq = AtomicU64::new(0);
        let line = r#"{"message":{"role":"assistant","tool_calls":[{"function":{"name":"calc","arguments":{}}}]},"done":false}"#;
        let first = ndjson_line_events(line, &seq).unwrap();
        let second = ndjson_line_events(line, &seq).unwrap();
        let StreamEvent::ToolCallStart { id: first_id, .. } = &first[0] else {
            panic!("expected ToolCallStart");
        };
        let StreamEvent::ToolCallStart { id: second_id, .. } = &second[0] else {
            panic!("expected ToolCallStart");
        };
        assert_ne!(
            first_id, second_id,
            "tool-call ids must be unique across calls"
        );
    }

    #[test]
    fn test_convert_message_user() {
        let msg = Message::user("hello");
        let json = convert_message(&msg);
        assert_eq!(json["role"], "user");
        assert_eq!(json["content"], "hello");
    }

    #[test]
    fn test_convert_message_assistant_with_tool_calls() {
        let msg = Message::assistant_with_tool_calls(vec![ToolCall {
            id: "1".into(),
            name: "test".into(),
            arguments: serde_json::json!({"a": 1}),
        }]);
        let json = convert_message(&msg);
        assert_eq!(json["role"], "assistant");
        assert!(json["tool_calls"].is_array());
    }

    #[test]
    fn test_convert_tool_spec() {
        let spec = ToolSpec {
            name: "calc".into(),
            description: "Calculator".into(),
            parameters: serde_json::json!({"type": "object"}),
        };
        let json = convert_tool_spec(&spec);
        assert_eq!(json["type"], "function");
        assert_eq!(json["function"]["name"], "calc");
    }

    #[test]
    fn test_parse_response_text() {
        let resp = OllamaResponse {
            message: Some(OllamaMessage {
                content: Some("Hello!".into()),
                tool_calls: vec![],
            }),
            done: true,
            prompt_eval_count: Some(10),
            eval_count: Some(5),
        };
        let result = parse_response(resp, &AtomicU64::new(0)).unwrap();
        assert_eq!(result.message.content.as_deref(), Some("Hello!"));
        assert_eq!(result.stop_reason, StopReason::EndTurn);
        assert_eq!(result.usage.as_ref().unwrap().input_tokens, 10);
    }

    #[test]
    fn test_parse_response_tool_call() {
        let resp = OllamaResponse {
            message: Some(OllamaMessage {
                content: None,
                tool_calls: vec![OllamaToolCall {
                    function: OllamaFunction {
                        name: "calc".into(),
                        arguments: serde_json::json!({"expr": "1+1"}),
                    },
                }],
            }),
            done: true,
            prompt_eval_count: None,
            eval_count: None,
        };
        let result = parse_response(resp, &AtomicU64::new(0)).unwrap();
        assert_eq!(result.stop_reason, StopReason::ToolUse);
        assert_eq!(result.message.tool_calls.len(), 1);
        assert_eq!(result.message.tool_calls[0].name, "calc");
        assert_eq!(result.message.tool_calls[0].id, "ollama_tc_0_calc");
    }

    #[test]
    fn test_parse_response_ids_do_not_collide_across_turns() {
        // The counter is client-wide: two responses parsed through the same
        // model must not reuse ids (a per-response index restarted at 0).
        let make_resp = || OllamaResponse {
            message: Some(OllamaMessage {
                content: None,
                tool_calls: vec![OllamaToolCall {
                    function: OllamaFunction {
                        name: "calc".into(),
                        arguments: serde_json::json!({}),
                    },
                }],
            }),
            done: true,
            prompt_eval_count: None,
            eval_count: None,
        };
        let seq = AtomicU64::new(0);
        let first = parse_response(make_resp(), &seq).unwrap();
        let second = parse_response(make_resp(), &seq).unwrap();
        assert_ne!(
            first.message.tool_calls[0].id, second.message.tool_calls[0].id,
            "tool-call ids must be unique across turns"
        );
    }

    #[test]
    fn test_tool_name_from_call_id() {
        assert_eq!(tool_name_from_call_id("ollama_tc_0_calc"), Some("calc"));
        // Function names may contain underscores and digits.
        assert_eq!(
            tool_name_from_call_id("ollama_tc_12_web_search_v2"),
            Some("web_search_v2")
        );
        assert_eq!(tool_name_from_call_id("ollama_tc_3"), None);
        assert_eq!(tool_name_from_call_id("ollama_tc_x_calc"), None);
        assert_eq!(tool_name_from_call_id("foreign-id"), None);
    }

    #[test]
    fn test_convert_message_tool_result_includes_tool_name() {
        // Newer Ollama uses `tool_name` for attribution; it is derived from
        // the synthetic tool_call_id format.
        let msg = Message::tool_result("ollama_tc_4_calc", "42");
        let json = convert_message(&msg);
        assert_eq!(json["role"], "tool");
        assert_eq!(json["content"], "42");
        assert_eq!(json["tool_name"], "calc");
    }

    #[test]
    fn test_convert_message_tool_result_omits_tool_name_for_foreign_id() {
        let msg = Message::tool_result("some-other-id", "42");
        let json = convert_message(&msg);
        assert_eq!(json["role"], "tool");
        assert!(
            json.get("tool_name").is_none(),
            "tool_name must be omitted when the name cannot be derived: {json}"
        );
    }

    #[test]
    fn test_build_request_body() {
        let model = Ollama::new("llama3.1");
        let request = ChatRequest::new(vec![Message::user("hi")]);
        let body = model.build_request_body(&request, false);
        assert_eq!(body["model"], "llama3.1");
        assert_eq!(body["stream"], false);
    }

    #[test]
    fn test_build_request_body_with_tools() {
        let model = Ollama::new("llama3.1");
        let request = ChatRequest {
            messages: vec![Message::user("hi")],
            tools: vec![ToolSpec {
                name: "test".into(),
                description: "test".into(),
                parameters: serde_json::json!({"type": "object"}),
            }],
            temperature: Some(0.5),
            max_tokens: None,
        };
        let body = model.build_request_body(&request, true);
        assert!(body["tools"].is_array());
        assert_eq!(body["options"]["temperature"], 0.5);
    }

    #[test]
    fn test_build_request_body_maps_max_tokens() {
        let model = Ollama::new("llama3.1");
        let request = ChatRequest {
            messages: vec![Message::user("hi")],
            tools: vec![],
            temperature: Some(0.5),
            max_tokens: Some(256),
        };
        let body = model.build_request_body(&request, false);
        assert_eq!(body["options"]["num_predict"], 256);
        // temperature and num_predict must coexist under options.
        assert_eq!(body["options"]["temperature"], 0.5);
    }

    #[test]
    fn test_build_request_body_no_max_tokens() {
        let model = Ollama::new("llama3.1");
        let request = ChatRequest::new(vec![Message::user("hi")]);
        let body = model.build_request_body(&request, false);
        assert!(body["options"]["num_predict"].is_null());
    }
}