ghost-link 2.0.0

CLI and API runtime for Ghostlink, a distributed inference fabric for routing and scheduling custom LLM workloads across heterogeneous hardware.
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
//! Ollama inference client for real model execution
#![allow(dead_code)]
use futures::Stream;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::error::Error;
use std::io;
use std::pin::Pin;
use tokio::sync::mpsc;

#[derive(Clone, Debug)]
pub struct OllamaClient {
    base_url: String,
    client: Client,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaResponse {
    pub response: String,
    pub done: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaModel {
    pub name: String,
    pub size: u64,
    pub digest: String,
    pub modified_at: String,
    #[serde(default)]
    pub details: Option<ModelDetails>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelDetails {
    pub format: String,
    pub family: String,
    pub families: Option<Vec<String>>,
    pub parameter_size: String,
    pub quantization_level: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OllamaModelsResponse {
    pub models: Vec<OllamaModel>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessage {
    pub role: String,
    pub content: String,
    /// Only ever populated by Ollama on an incoming response (when the model
    /// natively supports tool-calling and decides to call one) — never set on
    /// an outgoing message we build ourselves.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<Value>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatRequest {
    pub model: String,
    pub messages: Vec<ChatMessage>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<OllamaOptions>,
    /// OpenAI-style function-tool definitions:
    /// `{"type": "function", "function": {"name", "description", "parameters"}}`.
    /// Only sent to models that declare tool-calling support in their template.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Value>>,
    /// Structured-output constraint: either the literal string `"json"`, or a
    /// raw JSON schema object — Ollama applies it directly as a grammar, no
    /// `{"type": "json_schema", ...}` wrapper like OpenAI/vLLM expect.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<Value>,
}

/// Sampling parameters accepted by Ollama's `/api/generate` and `/api/chat`
/// endpoints. Ollama only reads these from a nested `options` object in the
/// request body -- top-level fields with the same names are silently
/// ignored by the server, so this must not be flattened.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct OllamaOptions {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_k: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repeat_penalty: Option<f32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub num_predict: Option<usize>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatResponse {
    pub model: String,
    pub created_at: String,
    pub message: ChatMessage,
    pub done: bool,
    #[serde(default)]
    pub total_duration: Option<u64>,
    #[serde(default)]
    pub load_duration: Option<u64>,
    #[serde(default)]
    pub prompt_eval_count: Option<u32>,
    #[serde(default)]
    pub prompt_eval_duration: Option<u64>,
    #[serde(default)]
    pub eval_count: Option<u32>,
    #[serde(default)]
    pub eval_duration: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PullProgressResponse {
    pub status: String,
    #[serde(default)]
    pub digest: Option<String>,
    #[serde(default)]
    pub total: Option<u64>,
    #[serde(default)]
    pub completed: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfoResponse {
    #[serde(default)]
    pub license: String,
    #[serde(default)]
    pub modelfile: String,
    /// Older Ollama versions returned a string; newer builds often omit it.
    #[serde(default)]
    pub parameters: String,
    #[serde(default)]
    pub template: String,
    #[serde(default)]
    pub details: Option<ModelDetails>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateModelRequest {
    pub name: String,
    pub modelfile: Option<String>,
    pub path: Option<String>,
    #[serde(default)]
    pub stream: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CopyModelRequest {
    pub source: String,
    pub destination: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeleteModelRequest {
    pub name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingRequest {
    pub model: String,
    pub prompt: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingResponse {
    pub embedding: Vec<f32>,
}

pub type OllamaStream =
    Pin<Box<dyn Stream<Item = Result<String, Box<dyn Error + Send + Sync>>> + Send>>;

impl OllamaClient {
    pub fn new(base_url: String) -> Self {
        Self {
            base_url,
            client: Client::new(),
        }
    }

    fn matches_model_name(candidate: &str, requested: &str) -> bool {
        candidate == requested || candidate.eq_ignore_ascii_case(requested)
    }

    /// Check if Ollama is reachable
    pub async fn health(&self) -> Result<bool, Box<dyn Error>> {
        match self
            .client
            .get(format!("{}/api/tags", self.base_url))
            .send()
            .await
        {
            Ok(resp) => Ok(resp.status().is_success()),
            Err(_) => Ok(false),
        }
    }

    /// List available models in Ollama
    pub async fn list_models(&self) -> Result<Vec<String>, Box<dyn Error>> {
        let resp = self
            .client
            .get(format!("{}/api/tags", self.base_url))
            .send()
            .await?;

        let data: OllamaModelsResponse = resp.json().await?;
        let models = data.models.iter().map(|m| m.name.clone()).collect();
        Ok(models)
    }

    /// List available models with full details
    pub async fn list_models_detailed(&self) -> Result<Vec<OllamaModel>, Box<dyn Error>> {
        let resp = self
            .client
            .get(format!("{}/api/tags", self.base_url))
            .send()
            .await?;

        let data: OllamaModelsResponse = resp.json().await?;
        Ok(data.models)
    }

    /// Generate text using Ollama (non-streaming)
    #[allow(clippy::too_many_arguments)]
    pub async fn generate(
        &self,
        model: &str,
        prompt: &str,
        temperature: f32,
        top_p: f32,
        top_k: usize,
        repeat_penalty: f32,
        max_tokens: usize,
    ) -> Result<String, Box<dyn Error>> {
        let payload = json!({
            "model": model,
            "prompt": prompt,
            "stream": false,
            "options": {
                "temperature": temperature.clamp(0.0, 2.0),
                "top_p": top_p.clamp(0.0, 1.0),
                "top_k": top_k.clamp(1, 200),
                "repeat_penalty": repeat_penalty.clamp(0.0, 2.0),
                "num_predict": max_tokens,
            }
        });

        let resp = self
            .client
            .post(format!("{}/api/generate", self.base_url))
            .json(&payload)
            .send()
            .await?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama generate failed: HTTP {status} - {body}"
            ))));
        }

        let data: OllamaResponse = resp.json().await?;
        Ok(data.response)
    }

    /// Generate text using Ollama (streaming)
    #[allow(clippy::too_many_arguments)]
    pub async fn generate_stream(
        &self,
        model: &str,
        prompt: &str,
        temperature: f32,
        top_p: f32,
        top_k: usize,
        repeat_penalty: f32,
        max_tokens: usize,
    ) -> Result<OllamaStream, Box<dyn Error>> {
        let payload = json!({
            "model": model,
            "prompt": prompt,
            "stream": true,
            "options": {
                "temperature": temperature.clamp(0.0, 2.0),
                "top_p": top_p.clamp(0.0, 1.0),
                "top_k": top_k.clamp(1, 200),
                "repeat_penalty": repeat_penalty.clamp(0.0, 2.0),
                "num_predict": max_tokens,
            }
        });

        let resp = self
            .client
            .post(format!("{}/api/generate", self.base_url))
            .json(&payload)
            .send()
            .await?;

        // Without this check, an HTTP error response (model not found, bad
        // request, etc.) flows into `stream_response`, which only forwards
        // lines that parse as `{"response": ..., "done": ...}`. An error
        // body like `{"error": "..."}` matches neither shape, so it was
        // silently dropped and the caller got back an empty-but-Ok stream
        // instead of any indication the request failed.
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama generate failed: HTTP {status} - {body}"
            ))));
        }

        let stream = Box::pin(tokio_stream::wrappers::ReceiverStream::new(
            Self::stream_response(resp).await?,
        ));

        Ok(stream)
    }

    async fn stream_response(
        resp: reqwest::Response,
    ) -> Result<mpsc::Receiver<Result<String, Box<dyn Error + Send + Sync>>>, Box<dyn Error>> {
        let (tx, rx) = mpsc::channel(100);

        // Process the response as it arrives over the wire (`bytes_stream()`)
        // instead of `bytes().await` (which buffers the *entire* body before
        // returning). The previous version only faked streaming downstream of
        // this call — Ghostlink itself still waited for Ollama's whole
        // response before relaying anything, defeating the actual purpose of
        // streaming (reducing time-to-first-token).
        tokio::spawn(async move {
            use futures::StreamExt;
            let mut byte_stream = resp.bytes_stream();
            let mut buf = String::new();
            while let Some(chunk) = byte_stream.next().await {
                let chunk = match chunk {
                    Ok(c) => c,
                    Err(e) => {
                        let _ = tx
                            .send(Err(Box::new(e) as Box<dyn Error + Send + Sync>))
                            .await;
                        return;
                    }
                };
                buf.push_str(&String::from_utf8_lossy(&chunk));
                // A network chunk boundary rarely lines up with a JSON-line
                // boundary; only consume complete lines, keep any trailing
                // partial line buffered for the next chunk.
                while let Some(pos) = buf.find('\n') {
                    let line = buf[..pos].trim().to_string();
                    buf.drain(..=pos);
                    if line.is_empty() {
                        continue;
                    }
                    let json_str = line.strip_prefix("data: ").unwrap_or(&line);
                    if let Ok(data) = serde_json::from_str::<Value>(json_str) {
                        if let Some(response) = data.get("response").and_then(|v| v.as_str()) {
                            if tx.send(Ok(response.to_string())).await.is_err() {
                                return; // receiver dropped, stop reading
                            }
                        }
                        if data.get("done").and_then(|v| v.as_bool()).unwrap_or(false) {
                            return;
                        }
                    }
                }
            }
        });

        Ok(rx)
    }

    /// Chat with Ollama (non-streaming)
    #[allow(clippy::too_many_arguments)]
    #[allow(clippy::too_many_arguments)]
    pub async fn chat(
        &self,
        model: &str,
        messages: &[ChatMessage],
        temperature: Option<f32>,
        top_p: Option<f32>,
        top_k: Option<usize>,
        repeat_penalty: Option<f32>,
        max_tokens: Option<usize>,
        tools: Option<Vec<Value>>,
        format: Option<Value>,
    ) -> Result<ChatResponse, Box<dyn Error>> {
        let request = ChatRequest {
            model: model.to_string(),
            messages: messages.to_vec(),
            stream: Some(false),
            options: Some(OllamaOptions {
                temperature,
                top_p,
                top_k,
                repeat_penalty,
                num_predict: max_tokens,
            }),
            tools,
            format,
        };

        let resp = self
            .client
            .post(format!("{}/api/chat", self.base_url))
            .json(&request)
            .send()
            .await?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama chat failed: HTTP {status} - {body}"
            ))));
        }

        let data: ChatResponse = resp.json().await?;
        Ok(data)
    }

    /// Chat with Ollama (streaming)
    #[allow(clippy::too_many_arguments)]
    pub async fn chat_stream(
        &self,
        model: &str,
        messages: &[ChatMessage],
        temperature: Option<f32>,
        top_p: Option<f32>,
        top_k: Option<usize>,
        repeat_penalty: Option<f32>,
        max_tokens: Option<usize>,
    ) -> Result<
        Pin<Box<dyn Stream<Item = Result<ChatResponse, Box<dyn Error + Send + Sync>>> + Send>>,
        Box<dyn Error>,
    > {
        let request = ChatRequest {
            model: model.to_string(),
            messages: messages.to_vec(),
            stream: Some(true),
            options: Some(OllamaOptions {
                temperature,
                top_p,
                top_k,
                repeat_penalty,
                num_predict: max_tokens,
            }),
            tools: None,
            format: None,
        };

        let resp = self
            .client
            .post(format!("{}/api/chat", self.base_url))
            .json(&request)
            .send()
            .await?;

        // Same class of bug as `generate_stream`: an HTTP error body doesn't
        // deserialize as `ChatResponse` (which requires `model`/`created_at`/
        // `message`/`done`), so without this check it was silently dropped
        // by `stream_chat_response` and the caller saw an empty Ok stream.
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama chat failed: HTTP {status} - {body}"
            ))));
        }

        let stream = Box::pin(tokio_stream::wrappers::ReceiverStream::new(
            Self::stream_chat_response(resp).await?,
        ));

        Ok(stream)
    }

    async fn stream_chat_response(
        resp: reqwest::Response,
    ) -> Result<mpsc::Receiver<Result<ChatResponse, Box<dyn Error + Send + Sync>>>, Box<dyn Error>>
    {
        let (tx, rx) = mpsc::channel(100);

        // See stream_response's comment: process as bytes arrive rather than
        // buffering the whole body first.
        tokio::spawn(async move {
            use futures::StreamExt;
            let mut byte_stream = resp.bytes_stream();
            let mut buf = String::new();
            while let Some(chunk) = byte_stream.next().await {
                let chunk = match chunk {
                    Ok(c) => c,
                    Err(e) => {
                        let _ = tx
                            .send(Err(Box::new(e) as Box<dyn Error + Send + Sync>))
                            .await;
                        return;
                    }
                };
                buf.push_str(&String::from_utf8_lossy(&chunk));
                while let Some(pos) = buf.find('\n') {
                    let line = buf[..pos].trim().to_string();
                    buf.drain(..=pos);
                    if line.is_empty() {
                        continue;
                    }
                    let json_str = line.strip_prefix("data: ").unwrap_or(&line);
                    if let Ok(data) = serde_json::from_str::<ChatResponse>(json_str) {
                        let done = data.done;
                        if tx.send(Ok(data)).await.is_err() {
                            return; // receiver dropped, stop reading
                        }
                        if done {
                            return;
                        }
                    }
                }
            }
        });

        Ok(rx)
    }

    /// Pull a model from Ollama registry (non-streaming)
    pub async fn pull_model(&self, model_name: &str) -> Result<String, Box<dyn Error>> {
        let payload = json!({
            "name": model_name,
            "stream": false,
        });

        let resp = self
            .client
            .post(format!("{}/api/pull", self.base_url))
            .json(&payload)
            .send()
            .await?;

        // Previously this returned `Ok(text)` for any response whose body
        // could be read as text, including 4xx/5xx error responses — a
        // failed pull (unknown model, registry unreachable, etc.) was
        // reported to the caller as a successful result.
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama pull failed: HTTP {status} - {body}"
            ))));
        }

        match resp.text().await {
            Ok(text) => Ok(text),
            Err(e) => Err(Box::new(e)),
        }
    }

    /// Pull a model from Ollama registry with progress streaming
    pub async fn pull_model_stream(
        &self,
        model_name: &str,
    ) -> Result<
        Pin<
            Box<
                dyn Stream<Item = Result<PullProgressResponse, Box<dyn Error + Send + Sync>>>
                    + Send,
            >,
        >,
        Box<dyn Error>,
    > {
        let payload = json!({
            "name": model_name,
            "stream": true,
        });

        let resp = self
            .client
            .post(format!("{}/api/pull", self.base_url))
            .json(&payload)
            .send()
            .await?;

        // Same class of bug as `generate_stream`/`chat_stream`: an error body
        // (e.g. unknown model name) doesn't parse as `PullProgressResponse`
        // (requires a `status` field), so it was silently dropped and the
        // caller saw an empty Ok stream instead of the pull failure.
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama pull failed: HTTP {status} - {body}"
            ))));
        }

        let stream = Box::pin(tokio_stream::wrappers::ReceiverStream::new(
            Self::stream_pull_progress(resp).await?,
        ));

        Ok(stream)
    }

    async fn stream_pull_progress(
        resp: reqwest::Response,
    ) -> Result<
        mpsc::Receiver<Result<PullProgressResponse, Box<dyn Error + Send + Sync>>>,
        Box<dyn Error>,
    > {
        let (tx, rx) = mpsc::channel(100);

        // Was `resp.bytes().await?` (buffers the whole response before
        // processing any of it). A model pull can run for minutes; this made
        // the GUI's progress bar sit frozen for the entire download and then
        // jump straight to 100% instead of updating incrementally as Ollama
        // reports real progress. `bytes_stream()` processes chunks as they
        // arrive over the wire instead.
        tokio::spawn(async move {
            use futures::StreamExt;
            let mut byte_stream = resp.bytes_stream();
            let mut buf = String::new();
            while let Some(chunk) = byte_stream.next().await {
                let chunk = match chunk {
                    Ok(c) => c,
                    Err(e) => {
                        let _ = tx
                            .send(Err(Box::new(e) as Box<dyn Error + Send + Sync>))
                            .await;
                        return;
                    }
                };
                buf.push_str(&String::from_utf8_lossy(&chunk));
                while let Some(pos) = buf.find('\n') {
                    let line = buf[..pos].trim().to_string();
                    buf.drain(..=pos);
                    if line.is_empty() {
                        continue;
                    }
                    if let Ok(data) = serde_json::from_str::<PullProgressResponse>(&line) {
                        let status = data.status.clone();
                        if tx.send(Ok(data)).await.is_err() {
                            return; // receiver dropped, stop reading
                        }
                        if status == "success" || status == "error" {
                            return;
                        }
                    }
                }
            }
        });

        Ok(rx)
    }

    /// Show model information
    pub async fn show_model(&self, model_name: &str) -> Result<ModelInfoResponse, Box<dyn Error>> {
        let payload = json!({
            "name": model_name,
        });

        let resp = self
            .client
            .post(format!("{}/api/show", self.base_url))
            .json(&payload)
            .send()
            .await?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama show failed: HTTP {status} - {body}"
            ))));
        }

        // Tolerate schema drift across Ollama versions (missing parameters/template, etc.).
        let body = resp.text().await?;
        match serde_json::from_str::<ModelInfoResponse>(&body) {
            Ok(data) => Ok(data),
            Err(primary) => {
                // Minimal success: model exists if /api/show returned JSON object.
                if serde_json::from_str::<Value>(&body).is_ok() {
                    Ok(ModelInfoResponse {
                        license: String::new(),
                        modelfile: String::new(),
                        parameters: String::new(),
                        template: String::new(),
                        details: None,
                    })
                } else {
                    Err(Box::new(io::Error::other(format!(
                        "Ollama show decode failed: {primary}; body={}",
                        body.chars().take(200).collect::<String>()
                    ))))
                }
            }
        }
    }

    /// Unload a model by setting keep_alive to zero.
    pub async fn unload_model(&self, model_name: &str) -> Result<String, Box<dyn Error>> {
        // Propagate failures from `list_running` instead of treating them as
        // "nothing is running": if Ollama is unreachable or errors here, we
        // genuinely don't know the model's state, and silently reporting
        // "unload skipped" as success would hide a real connectivity failure
        // from the caller.
        let running_models = self.list_running().await?;
        if !running_models
            .iter()
            .any(|model| Self::matches_model_name(&model.name, model_name))
        {
            return Ok(format!(
                "model '{model_name}' is not running; unload skipped"
            ));
        }

        let payload = json!({
            "model": model_name,
            "prompt": "",
            "stream": false,
            "keep_alive": 0,
        });

        let resp = self
            .client
            .post(format!("{}/api/generate", self.base_url))
            .json(&payload)
            .send()
            .await?;

        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama unload failed: HTTP {status} - {body}"
            ))));
        }

        let body = resp.text().await.unwrap_or_default();
        Ok(body)
    }

    /// Create a model from a Modelfile
    pub async fn create_model(
        &self,
        name: &str,
        modelfile: &str,
    ) -> Result<String, Box<dyn Error>> {
        let payload = json!({
            "name": name,
            "modelfile": modelfile,
            "stream": false,
        });

        let resp = self
            .client
            .post(format!("{}/api/create", self.base_url))
            .json(&payload)
            .send()
            .await?;

        // See `pull_model`: an error response must not be reported as Ok.
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama create failed: HTTP {status} - {body}"
            ))));
        }

        match resp.text().await {
            Ok(text) => Ok(text),
            Err(e) => Err(Box::new(e)),
        }
    }

    /// Copy a model
    pub async fn copy_model(
        &self,
        source: &str,
        destination: &str,
    ) -> Result<String, Box<dyn Error>> {
        let payload = json!({
            "source": source,
            "destination": destination,
        });

        let resp = self
            .client
            .post(format!("{}/api/copy", self.base_url))
            .json(&payload)
            .send()
            .await?;

        // See `pull_model`: an error response must not be reported as Ok.
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama copy failed: HTTP {status} - {body}"
            ))));
        }

        match resp.text().await {
            Ok(text) => Ok(text),
            Err(e) => Err(Box::new(e)),
        }
    }

    /// Delete a model
    pub async fn delete_model(&self, name: &str) -> Result<String, Box<dyn Error>> {
        let payload = json!({
            "name": name,
        });

        let resp = self
            .client
            .delete(format!("{}/api/delete", self.base_url))
            .json(&payload)
            .send()
            .await?;

        // See `pull_model`: an error response must not be reported as Ok.
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp
                .text()
                .await
                .unwrap_or_else(|_| "<unreadable body>".to_string());
            return Err(Box::new(io::Error::other(format!(
                "Ollama delete failed: HTTP {status} - {body}"
            ))));
        }

        match resp.text().await {
            Ok(text) => Ok(text),
            Err(e) => Err(Box::new(e)),
        }
    }

    /// List running models
    pub async fn list_running(&self) -> Result<Vec<OllamaModel>, Box<dyn Error>> {
        let resp = self
            .client
            .get(format!("{}/api/ps", self.base_url))
            .send()
            .await?;

        let data: OllamaModelsResponse = resp.json().await?;
        Ok(data.models)
    }

    /// Generate embeddings
    pub async fn embeddings(&self, model: &str, prompt: &str) -> Result<Vec<f32>, Box<dyn Error>> {
        let payload = json!({
            "model": model,
            "prompt": prompt,
        });

        let resp = self
            .client
            .post(format!("{}/api/embeddings", self.base_url))
            .json(&payload)
            .send()
            .await?;

        let data: EmbeddingResponse = resp.json().await?;
        Ok(data.embedding)
    }

    /// Get Ollama version
    pub async fn version(&self) -> Result<String, Box<dyn Error>> {
        let resp = self
            .client
            .get(format!("{}/api/version", self.base_url))
            .send()
            .await?;

        let data: Value = resp.json().await?;
        Ok(data["version"].as_str().unwrap_or("unknown").to_string())
    }
}

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

    #[tokio::test]
    async fn test_ollama_client_creation() {
        let client = OllamaClient::new("http://localhost:11434".to_string());
        assert_eq!(client.base_url, "http://localhost:11434");
    }

    #[test]
    fn chat_request_nests_sampling_params_under_options() {
        fn assert_json_number_close(value: &Value, expected: f64, label: &str) {
            let actual = value
                .as_f64()
                .unwrap_or_else(|| panic!("{label} must be a JSON number"));
            let delta = (actual - expected).abs();
            assert!(
                delta <= 1e-6,
                "{label} expected {expected} but was {actual} (|delta|={delta})"
            );
        }

        // Regression: Ollama's /api/chat and /api/generate silently ignore
        // temperature/top_p/top_k/repeat_penalty/num_predict at the top
        // level of the request body -- they're only honored inside a
        // nested "options" object. If these ever end up top-level again,
        // every sampling setting from the GUI/API is silently dropped.
        let request = ChatRequest {
            model: "llama3".to_string(),
            messages: vec![],
            stream: Some(false),
            options: Some(OllamaOptions {
                temperature: Some(0.5),
                top_p: Some(0.8),
                top_k: Some(30),
                repeat_penalty: Some(1.2),
                num_predict: Some(256),
            }),
            tools: None,
            format: None,
        };
        let value = serde_json::to_value(&request).expect("serialize ChatRequest");
        assert!(
            value.get("temperature").is_none(),
            "temperature must not be top-level"
        );
        assert!(value.get("top_p").is_none(), "top_p must not be top-level");
        let options = value
            .get("options")
            .expect("options object must be present");
        assert_json_number_close(&options["temperature"], 0.5, "temperature");
        assert_json_number_close(&options["top_p"], 0.8, "top_p");
        assert_eq!(options["top_k"], 30);
        assert_json_number_close(&options["repeat_penalty"], 1.2, "repeat_penalty");
        assert_eq!(options["num_predict"], 256);
    }
}