ollama-client 0.1.0

Async and blocking Rust client for the Ollama API
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
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
//! Async client, request builders, and client builder.

use std::time::Duration;

use futures_core::Stream;
use url::Url;

use crate::error::{OllamaError, Result};
use crate::streaming::ndjson_stream;
use crate::types::chat::{ChatRequest, ChatResponse, ChatStreamChunk};
use crate::types::common::{Message, Options, Tool};
use crate::types::embed::{EmbedInput, EmbedRequest, EmbedResponse};
use crate::types::generate::{GenerateRequest, GenerateResponse, GenerateStreamChunk};
use crate::types::models::*;

/// Default Ollama server address (standard local install).
const DEFAULT_BASE_URL: &str = "http://localhost:11434";
/// Total request timeout — generous to accommodate slow model loads.
const DEFAULT_TIMEOUT_SECS: u64 = 120;
/// TCP connect timeout — fail fast if the server is unreachable.
const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 30;
/// Cap on remote error body to prevent log flooding or memory abuse.
const MAX_ERROR_MESSAGE_LEN: usize = 1024;

/// Async Ollama API client.
#[derive(Debug)]
pub struct OllamaClient {
    http: reqwest::Client,
    base_url: Url,
}

impl OllamaClient {
    /// Creates a new client connecting to `http://localhost:11434`.
    ///
    /// # Panics
    ///
    /// Panics if the HTTP client cannot be built (should not happen in practice).
    /// Use [`OllamaClient::builder()`] for a fallible alternative.
    pub fn new() -> Self {
        Self::try_new().expect("failed to create OllamaClient")
    }

    /// Fallible constructor connecting to `http://localhost:11434`.
    pub fn try_new() -> Result<Self> {
        Self::try_with_base_url(DEFAULT_BASE_URL)
    }

    /// Creates a new client with a custom base URL.
    ///
    /// # Panics
    ///
    /// Panics if `base_url` is not a valid HTTP/HTTPS URL.
    /// Use [`OllamaClient::try_with_base_url()`] for a fallible alternative.
    pub fn with_base_url(base_url: impl Into<String>) -> Self {
        Self::try_with_base_url(base_url).expect("invalid base URL")
    }

    /// Fallible constructor with a custom base URL.
    ///
    /// Only `http` and `https` schemes are accepted.
    pub fn try_with_base_url(base_url: impl Into<String>) -> Result<Self> {
        let base_url = parse_base_url(base_url.into())?;
        let http = reqwest::Client::builder()
            .timeout(Duration::from_secs(DEFAULT_TIMEOUT_SECS))
            .connect_timeout(Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS))
            .build()
            .map_err(OllamaError::Http)?;
        Ok(Self { http, base_url })
    }

    /// Creates a client from an existing `reqwest::Client` and base URL.
    ///
    /// # Panics
    ///
    /// Panics if `base_url` is not a valid HTTP/HTTPS URL.
    /// Use [`OllamaClient::try_from_reqwest()`] for a fallible alternative.
    pub fn from_reqwest(client: reqwest::Client, base_url: impl Into<String>) -> Self {
        Self::try_from_reqwest(client, base_url).expect("invalid base URL")
    }

    /// Fallible constructor from an existing `reqwest::Client`.
    ///
    /// Use this to inject a custom HTTP client for testing. Point `base_url`
    /// at a mock server (e.g., `wiremock` or `mockito`) to test without a
    /// real Ollama instance:
    ///
    /// ```no_run
    /// # fn example() -> ollama_client::Result<()> {
    /// let client = ollama_client::OllamaClient::try_from_reqwest(
    ///     reqwest::Client::new(),
    ///     "http://127.0.0.1:8080",
    /// )?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn try_from_reqwest(client: reqwest::Client, base_url: impl Into<String>) -> Result<Self> {
        let base_url = parse_base_url(base_url.into())?;
        Ok(Self {
            http: client,
            base_url,
        })
    }

    /// Returns a builder for configuring timeouts and base URL.
    pub fn builder() -> OllamaClientBuilder {
        OllamaClientBuilder::default()
    }

    pub(crate) fn url(&self, path: &str) -> String {
        // Safe: base_url is validated at construction, path is a known literal.
        let mut url = self.base_url.clone();
        url.set_path(path);
        url.to_string()
    }

    pub(crate) fn http(&self) -> &reqwest::Client {
        &self.http
    }

    // ── Chat ──

    /// Start building a chat completion request.
    pub fn chat(&self) -> ChatRequestBuilder<'_> {
        ChatRequestBuilder {
            client: self,
            model: String::new(),
            messages: Vec::new(),
            format: None,
            tools: None,
            think: None,
            options: None,
            keep_alive: None,
        }
    }

    // ── Generate ──

    /// Start building a text generation request.
    pub fn generate(&self) -> GenerateRequestBuilder<'_> {
        GenerateRequestBuilder {
            client: self,
            model: String::new(),
            prompt: None,
            suffix: None,
            images: None,
            format: None,
            system: None,
            think: None,
            raw: None,
            keep_alive: None,
            options: None,
            context: None,
        }
    }

    // ── Embed ──

    /// Start building an embeddings request.
    pub fn embed(&self) -> EmbedRequestBuilder<'_> {
        EmbedRequestBuilder {
            client: self,
            model: String::new(),
            input: EmbedInput::Single(String::new()),
            truncate: None,
            dimensions: None,
            keep_alive: None,
            options: None,
        }
    }

    // ── Model management ──

    /// List locally available models.
    pub async fn list_models(&self) -> Result<ListModelsResponse> {
        let response = self.http.get(self.url("/api/tags")).send().await?;
        let response = check_api_error(response).await?;
        Ok(response.json().await?)
    }

    /// Start building a show model info request.
    pub fn show_model(&self) -> ShowModelRequestBuilder<'_> {
        ShowModelRequestBuilder {
            client: self,
            model: String::new(),
            verbose: None,
        }
    }

    /// Copy a model.
    pub async fn copy_model(
        &self,
        source: impl Into<String>,
        destination: impl Into<String>,
    ) -> Result<()> {
        let request = CopyModelRequest {
            source: source.into(),
            destination: destination.into(),
        };
        let response = self
            .http
            .post(self.url("/api/copy"))
            .json(&request)
            .send()
            .await?;
        check_api_error(response).await?;
        Ok(())
    }

    /// Delete a model.
    pub async fn delete_model(&self, model: impl Into<String>) -> Result<()> {
        let request = DeleteModelRequest {
            model: model.into(),
        };
        let response = self
            .http
            .delete(self.url("/api/delete"))
            .json(&request)
            .send()
            .await?;
        check_api_error(response).await?;
        Ok(())
    }

    /// Start building a pull model request.
    pub fn pull_model(&self) -> PullModelRequestBuilder<'_> {
        PullModelRequestBuilder {
            client: self,
            model: String::new(),
            insecure: None,
        }
    }

    /// Start building a push model request.
    pub fn push_model(&self) -> PushModelRequestBuilder<'_> {
        PushModelRequestBuilder {
            client: self,
            model: String::new(),
            insecure: None,
        }
    }

    /// Start building a create model request.
    pub fn create_model(&self) -> CreateModelRequestBuilder<'_> {
        CreateModelRequestBuilder {
            client: self,
            model: String::new(),
            from: None,
            system: None,
            template: None,
            parameters: None,
            quantize: None,
            license: None,
            messages: None,
        }
    }

    /// List currently running/loaded models.
    pub async fn list_running(&self) -> Result<ListRunningResponse> {
        let response = self.http.get(self.url("/api/ps")).send().await?;
        let response = check_api_error(response).await?;
        Ok(response.json().await?)
    }

    // ── Blobs ──

    /// Check if a blob exists.
    ///
    /// Returns `Ok(true)` if the blob exists (HTTP 200), `Ok(false)` if not found (HTTP 404),
    /// and `Err(...)` for any other error.
    pub async fn check_blob(&self, digest: &str) -> Result<bool> {
        validate_digest(digest)?;
        let response = self
            .http
            .head(self.url(&format!("/api/blobs/{digest}")))
            .send()
            .await?;
        match response.status().as_u16() {
            200 => Ok(true),
            404 => Ok(false),
            _ => {
                check_api_error(response).await?;
                Ok(false)
            }
        }
    }

    /// Upload a blob with the given digest.
    ///
    /// **Note:** The entire blob is loaded into memory as `Vec<u8>`. For very large blobs,
    /// consider splitting them or using a lower-level HTTP client with streaming upload.
    pub async fn upload_blob(&self, digest: &str, data: Vec<u8>) -> Result<()> {
        validate_digest(digest)?;
        let response = self
            .http
            .post(self.url(&format!("/api/blobs/{digest}")))
            .body(data)
            .send()
            .await?;
        check_api_error(response).await?;
        Ok(())
    }

    // ── Version ──

    /// Get the Ollama server version.
    pub async fn version(&self) -> Result<VersionResponse> {
        let response = self.http.get(self.url("/api/version")).send().await?;
        let response = check_api_error(response).await?;
        Ok(response.json().await?)
    }
}

impl Default for OllamaClient {
    fn default() -> Self {
        Self::new()
    }
}

// ────────────────────────────────────────────────────────────────
// Builders
// ────────────────────────────────────────────────────────────────

// ── Chat builder ──

/// Builder for a chat completion request.
#[must_use]
#[derive(Debug)]
pub struct ChatRequestBuilder<'a> {
    client: &'a OllamaClient,
    model: String,
    messages: Vec<Message>,
    format: Option<serde_json::Value>,
    tools: Option<Vec<Tool>>,
    think: Option<serde_json::Value>,
    options: Option<Options>,
    keep_alive: Option<String>,
}

impl<'a> ChatRequestBuilder<'a> {
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    pub fn messages(mut self, messages: Vec<Message>) -> Self {
        self.messages = messages;
        self
    }

    pub fn format(mut self, format: serde_json::Value) -> Self {
        self.format = Some(format);
        self
    }

    pub fn tools(mut self, tools: Vec<Tool>) -> Self {
        self.tools = Some(tools);
        self
    }

    pub fn think(mut self, think: bool) -> Self {
        self.think = Some(serde_json::Value::Bool(think));
        self
    }

    pub fn options(mut self, options: Options) -> Self {
        self.options = Some(options);
        self
    }

    pub fn temperature(mut self, temp: f64) -> Self {
        self.options
            .get_or_insert_with(Options::default)
            .temperature = Some(temp);
        self
    }

    pub fn top_k(mut self, top_k: u32) -> Self {
        self.options.get_or_insert_with(Options::default).top_k = Some(top_k);
        self
    }

    pub fn top_p(mut self, top_p: f64) -> Self {
        self.options.get_or_insert_with(Options::default).top_p = Some(top_p);
        self
    }

    pub fn num_ctx(mut self, num_ctx: u32) -> Self {
        self.options.get_or_insert_with(Options::default).num_ctx = Some(num_ctx);
        self
    }

    pub fn keep_alive(mut self, keep_alive: impl Into<String>) -> Self {
        self.keep_alive = Some(keep_alive.into());
        self
    }

    fn build_request(&self, stream: bool) -> ChatRequest {
        ChatRequest {
            model: self.model.clone(),
            messages: self.messages.clone(),
            stream: Some(stream),
            format: self.format.clone(),
            tools: self.tools.clone(),
            think: self.think.clone(),
            options: self.options.clone(),
            keep_alive: self.keep_alive.clone(),
        }
    }

    /// Send and wait for the complete response.
    ///
    /// # Errors
    ///
    /// Returns [`OllamaError::Http`] on network failure,
    /// [`OllamaError::Api`] if Ollama returns an error status,
    /// or [`OllamaError::Json`] if the response cannot be parsed.
    pub async fn send(self) -> Result<ChatResponse> {
        let request = self.build_request(false);
        let response = self
            .client
            .http()
            .post(self.client.url("/api/chat"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        let body = response.text().await?;
        Ok(serde_json::from_str(&body)?)
    }

    /// Send and return a stream of chunks.
    ///
    /// # Errors
    ///
    /// Returns [`OllamaError::Http`] on network failure or
    /// [`OllamaError::Api`] if Ollama returns an error status.
    /// Individual stream items may yield [`OllamaError::Json`]
    /// or [`OllamaError::LineTooLarge`].
    pub async fn send_stream(self) -> Result<impl Stream<Item = Result<ChatStreamChunk>>> {
        let request = self.build_request(true);
        let response = self
            .client
            .http()
            .post(self.client.url("/api/chat"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        Ok(ndjson_stream(response))
    }
}

// ── Generate builder ──

/// Builder for a text generation request.
#[must_use]
#[derive(Debug)]
pub struct GenerateRequestBuilder<'a> {
    client: &'a OllamaClient,
    model: String,
    prompt: Option<String>,
    suffix: Option<String>,
    images: Option<Vec<String>>,
    format: Option<serde_json::Value>,
    system: Option<String>,
    think: Option<serde_json::Value>,
    raw: Option<bool>,
    keep_alive: Option<String>,
    options: Option<Options>,
    context: Option<Vec<i64>>,
}

impl<'a> GenerateRequestBuilder<'a> {
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    pub fn suffix(mut self, suffix: impl Into<String>) -> Self {
        self.suffix = Some(suffix.into());
        self
    }

    pub fn images(mut self, images: Vec<String>) -> Self {
        self.images = Some(images);
        self
    }

    pub fn format(mut self, format: serde_json::Value) -> Self {
        self.format = Some(format);
        self
    }

    pub fn system(mut self, system: impl Into<String>) -> Self {
        self.system = Some(system.into());
        self
    }

    pub fn think(mut self, think: bool) -> Self {
        self.think = Some(serde_json::Value::Bool(think));
        self
    }

    pub fn raw(mut self, raw: bool) -> Self {
        self.raw = Some(raw);
        self
    }

    pub fn keep_alive(mut self, keep_alive: impl Into<String>) -> Self {
        self.keep_alive = Some(keep_alive.into());
        self
    }

    pub fn options(mut self, options: Options) -> Self {
        self.options = Some(options);
        self
    }

    pub fn temperature(mut self, temp: f64) -> Self {
        self.options
            .get_or_insert_with(Options::default)
            .temperature = Some(temp);
        self
    }

    pub fn context(mut self, context: Vec<i64>) -> Self {
        self.context = Some(context);
        self
    }

    fn build_request(&self, stream: bool) -> GenerateRequest {
        GenerateRequest {
            model: self.model.clone(),
            prompt: self.prompt.clone(),
            suffix: self.suffix.clone(),
            images: self.images.clone(),
            format: self.format.clone(),
            system: self.system.clone(),
            stream: Some(stream),
            think: self.think.clone(),
            raw: self.raw,
            keep_alive: self.keep_alive.clone(),
            options: self.options.clone(),
            context: self.context.clone(),
        }
    }

    /// Send and wait for the complete response.
    pub async fn send(self) -> Result<GenerateResponse> {
        let request = self.build_request(false);
        let response = self
            .client
            .http()
            .post(self.client.url("/api/generate"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        let body = response.text().await?;
        Ok(serde_json::from_str(&body)?)
    }

    /// Send and return a stream of chunks.
    pub async fn send_stream(self) -> Result<impl Stream<Item = Result<GenerateStreamChunk>>> {
        let request = self.build_request(true);
        let response = self
            .client
            .http()
            .post(self.client.url("/api/generate"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        Ok(ndjson_stream(response))
    }
}

// ── Embed builder ──

/// Builder for an embeddings request.
#[must_use]
#[derive(Debug)]
pub struct EmbedRequestBuilder<'a> {
    client: &'a OllamaClient,
    model: String,
    input: EmbedInput,
    truncate: Option<bool>,
    dimensions: Option<u32>,
    keep_alive: Option<String>,
    options: Option<Options>,
}

impl<'a> EmbedRequestBuilder<'a> {
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    pub fn input(mut self, input: impl Into<EmbedInput>) -> Self {
        self.input = input.into();
        self
    }

    pub fn truncate(mut self, truncate: bool) -> Self {
        self.truncate = Some(truncate);
        self
    }

    pub fn dimensions(mut self, dimensions: u32) -> Self {
        self.dimensions = Some(dimensions);
        self
    }

    pub fn keep_alive(mut self, keep_alive: impl Into<String>) -> Self {
        self.keep_alive = Some(keep_alive.into());
        self
    }

    pub fn options(mut self, options: Options) -> Self {
        self.options = Some(options);
        self
    }

    /// Send and wait for the embeddings response.
    pub async fn send(self) -> Result<EmbedResponse> {
        let request = EmbedRequest {
            model: self.model,
            input: self.input,
            truncate: self.truncate,
            dimensions: self.dimensions,
            keep_alive: self.keep_alive,
            options: self.options,
        };
        let response = self
            .client
            .http()
            .post(self.client.url("/api/embed"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        let body = response.text().await?;
        Ok(serde_json::from_str(&body)?)
    }
}

// ── Show model builder ──

/// Builder for a show-model request.
#[must_use]
#[derive(Debug)]
pub struct ShowModelRequestBuilder<'a> {
    client: &'a OllamaClient,
    model: String,
    verbose: Option<bool>,
}

impl<'a> ShowModelRequestBuilder<'a> {
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    pub fn verbose(mut self, verbose: bool) -> Self {
        self.verbose = Some(verbose);
        self
    }

    /// Send the request.
    pub async fn send(self) -> Result<ShowModelResponse> {
        let request = ShowModelRequest {
            model: self.model,
            verbose: self.verbose,
        };
        let response = self
            .client
            .http()
            .post(self.client.url("/api/show"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        let body = response.text().await?;
        Ok(serde_json::from_str(&body)?)
    }
}

// ── Pull model builder ──

/// Builder for a pull-model request.
#[must_use]
#[derive(Debug)]
pub struct PullModelRequestBuilder<'a> {
    client: &'a OllamaClient,
    model: String,
    insecure: Option<bool>,
}

impl<'a> PullModelRequestBuilder<'a> {
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    /// Enable insecure mode (skip TLS certificate verification on the Ollama server side).
    ///
    /// **Warning:** This disables TLS certificate verification for registry operations.
    /// Use only in local development or testing environments, never in production.
    pub fn insecure(mut self, insecure: bool) -> Self {
        self.insecure = Some(insecure);
        self
    }

    /// Send and wait for the final status (non-streaming).
    pub async fn send(self) -> Result<PullModelStatus> {
        let request = PullModelRequest {
            model: self.model,
            insecure: self.insecure,
            stream: Some(false),
        };
        let response = self
            .client
            .http()
            .post(self.client.url("/api/pull"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        let body = response.text().await?;
        Ok(serde_json::from_str(&body)?)
    }

    /// Send and return a stream of status updates.
    pub async fn send_stream(self) -> Result<impl Stream<Item = Result<PullModelStatus>>> {
        let request = PullModelRequest {
            model: self.model,
            insecure: self.insecure,
            stream: Some(true),
        };
        let response = self
            .client
            .http()
            .post(self.client.url("/api/pull"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        Ok(ndjson_stream(response))
    }
}

// ── Push model builder ──

/// Builder for a push-model request.
#[must_use]
#[derive(Debug)]
pub struct PushModelRequestBuilder<'a> {
    client: &'a OllamaClient,
    model: String,
    insecure: Option<bool>,
}

impl<'a> PushModelRequestBuilder<'a> {
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    /// Enable insecure mode (skip TLS certificate verification on the Ollama server side).
    ///
    /// **Warning:** This disables TLS certificate verification for registry operations.
    /// Use only in local development or testing environments, never in production.
    pub fn insecure(mut self, insecure: bool) -> Self {
        self.insecure = Some(insecure);
        self
    }

    /// Send and wait for the final status (non-streaming).
    pub async fn send(self) -> Result<PushModelStatus> {
        let request = PushModelRequest {
            model: self.model,
            insecure: self.insecure,
            stream: Some(false),
        };
        let response = self
            .client
            .http()
            .post(self.client.url("/api/push"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        let body = response.text().await?;
        Ok(serde_json::from_str(&body)?)
    }

    /// Send and return a stream of status updates.
    pub async fn send_stream(self) -> Result<impl Stream<Item = Result<PushModelStatus>>> {
        let request = PushModelRequest {
            model: self.model,
            insecure: self.insecure,
            stream: Some(true),
        };
        let response = self
            .client
            .http()
            .post(self.client.url("/api/push"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        Ok(ndjson_stream(response))
    }
}

// ── Create model builder ──

/// Builder for a create-model request.
#[must_use]
#[derive(Debug)]
pub struct CreateModelRequestBuilder<'a> {
    client: &'a OllamaClient,
    model: String,
    from: Option<String>,
    system: Option<String>,
    template: Option<String>,
    parameters: Option<serde_json::Value>,
    quantize: Option<String>,
    license: Option<serde_json::Value>,
    messages: Option<Vec<Message>>,
}

impl<'a> CreateModelRequestBuilder<'a> {
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    pub fn from_model(mut self, from: impl Into<String>) -> Self {
        self.from = Some(from.into());
        self
    }

    pub fn system(mut self, system: impl Into<String>) -> Self {
        self.system = Some(system.into());
        self
    }

    pub fn template(mut self, template: impl Into<String>) -> Self {
        self.template = Some(template.into());
        self
    }

    pub fn parameters(mut self, parameters: serde_json::Value) -> Self {
        self.parameters = Some(parameters);
        self
    }

    pub fn quantize(mut self, quantize: impl Into<String>) -> Self {
        self.quantize = Some(quantize.into());
        self
    }

    pub fn license(mut self, license: serde_json::Value) -> Self {
        self.license = Some(license);
        self
    }

    pub fn messages(mut self, messages: Vec<Message>) -> Self {
        self.messages = Some(messages);
        self
    }

    /// Send and wait for the final status (non-streaming).
    pub async fn send(self) -> Result<CreateModelStatus> {
        let request = self.build_request(false);
        let response = self
            .client
            .http()
            .post(self.client.url("/api/create"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        let body = response.text().await?;
        Ok(serde_json::from_str(&body)?)
    }

    /// Send and return a stream of status updates.
    pub async fn send_stream(self) -> Result<impl Stream<Item = Result<CreateModelStatus>>> {
        let request = self.build_request(true);
        let response = self
            .client
            .http()
            .post(self.client.url("/api/create"))
            .json(&request)
            .send()
            .await?;
        let response = check_api_error(response).await?;
        Ok(ndjson_stream(response))
    }

    fn build_request(&self, stream: bool) -> CreateModelRequest {
        CreateModelRequest {
            model: self.model.clone(),
            from: self.from.clone(),
            system: self.system.clone(),
            template: self.template.clone(),
            parameters: self.parameters.clone(),
            quantize: self.quantize.clone(),
            license: self.license.clone(),
            messages: self.messages.clone(),
            stream: Some(stream),
        }
    }
}

// ────────────────────────────────────────────────────────────────
// Helpers
// ────────────────────────────────────────────────────────────────

/// Check for HTTP error responses and convert them to `OllamaError::Api`.
///
/// Error messages from the remote server are truncated to [`MAX_ERROR_MESSAGE_LEN`] bytes
/// and control characters (except `\n`) are stripped.
pub(crate) async fn check_api_error(response: reqwest::Response) -> Result<reqwest::Response> {
    let status = response.status();
    if status.is_client_error() || status.is_server_error() {
        let raw = response
            .text()
            .await
            .unwrap_or_else(|_| "unknown error".to_string());
        let message = sanitize_error_message(&raw);
        return Err(OllamaError::Api {
            status: status.as_u16(),
            message,
        });
    }
    Ok(response)
}

/// Truncate and sanitize a remote error message.
fn sanitize_error_message(raw: &str) -> String {
    let truncated = if raw.len() > MAX_ERROR_MESSAGE_LEN {
        // Truncate at a char boundary.
        let mut end = MAX_ERROR_MESSAGE_LEN;
        while !raw.is_char_boundary(end) && end > 0 {
            end -= 1;
        }
        format!("{}...(truncated)", &raw[..end])
    } else {
        raw.to_string()
    };
    // Strip control chars except newline.
    truncated
        .chars()
        .filter(|c| !c.is_control() || *c == '\n')
        .collect()
}

/// Parse and validate a base URL. Only `http` and `https` schemes are accepted.
fn parse_base_url(raw: String) -> Result<Url> {
    let trimmed = raw.trim_end_matches('/');
    let url =
        Url::parse(trimmed).map_err(|e| OllamaError::InvalidBaseUrl(format!("{trimmed}: {e}")))?;
    match url.scheme() {
        "http" | "https" => Ok(url),
        scheme => Err(OllamaError::InvalidBaseUrl(format!(
            "unsupported scheme '{scheme}', expected 'http' or 'https'"
        ))),
    }
}

/// Validate a blob digest string.
///
/// Expected format: `<algorithm>:<hex>` (e.g., `sha256:abc123...`).
/// Rejects slashes, backslashes, query separators, fragments, and control characters.
fn validate_digest(digest: &str) -> Result<()> {
    if digest.is_empty() {
        return Err(OllamaError::InvalidDigest("digest is empty".into()));
    }
    if !digest.contains(':') {
        return Err(OllamaError::InvalidDigest(format!(
            "expected '<algorithm>:<hex>' format, got '{digest}'"
        )));
    }
    let bad_chars = ['/', '\\', '?', '#'];
    if digest
        .chars()
        .any(|c| bad_chars.contains(&c) || c.is_control())
    {
        return Err(OllamaError::InvalidDigest(format!(
            "digest contains invalid characters: '{digest}'"
        )));
    }
    Ok(())
}

// ────────────────────────────────────────────────────────────────
// Client builder
// ────────────────────────────────────────────────────────────────

/// Configurable builder for [`OllamaClient`].
#[derive(Debug)]
pub struct OllamaClientBuilder {
    base_url: String,
    timeout: Duration,
    connect_timeout: Duration,
}

impl Default for OllamaClientBuilder {
    fn default() -> Self {
        Self {
            base_url: DEFAULT_BASE_URL.to_string(),
            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
            connect_timeout: Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS),
        }
    }
}

impl OllamaClientBuilder {
    /// Set the base URL (default: `http://localhost:11434`).
    pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = base_url.into();
        self
    }

    /// Set the total request timeout (default: 120s).
    ///
    /// For long-running streaming requests (chat, generate, pull, push, create),
    /// consider setting a longer timeout or disabling it with `Duration::ZERO`.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the connection timeout (default: 30s).
    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = timeout;
        self
    }

    /// Build the client.
    pub fn build(self) -> Result<OllamaClient> {
        let base_url = parse_base_url(self.base_url)?;
        let mut builder = reqwest::Client::builder().connect_timeout(self.connect_timeout);
        if self.timeout != Duration::ZERO {
            builder = builder.timeout(self.timeout);
        }
        let http = builder.build().map_err(OllamaError::Http)?;
        Ok(OllamaClient { http, base_url })
    }
}

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

    #[test]
    fn parse_base_url_valid_http() {
        assert!(parse_base_url("http://localhost:11434".into()).is_ok());
    }

    #[test]
    fn parse_base_url_valid_https() {
        assert!(parse_base_url("https://ollama.example.com".into()).is_ok());
    }

    #[test]
    fn parse_base_url_rejects_ftp() {
        assert!(parse_base_url("ftp://evil.com".into()).is_err());
    }

    #[test]
    fn parse_base_url_rejects_garbage() {
        assert!(parse_base_url("not a url".into()).is_err());
    }

    #[test]
    fn validate_digest_valid() {
        assert!(validate_digest("sha256:abcdef0123456789").is_ok());
    }

    #[test]
    fn validate_digest_empty() {
        assert!(validate_digest("").is_err());
    }

    #[test]
    fn validate_digest_no_colon() {
        assert!(validate_digest("sha256abcdef").is_err());
    }

    #[test]
    fn validate_digest_with_slash() {
        assert!(validate_digest("sha256:abc/def").is_err());
    }

    #[test]
    fn validate_digest_with_query() {
        assert!(validate_digest("sha256:abc?x=1").is_err());
    }

    #[test]
    fn sanitize_error_truncation() {
        let long = "a".repeat(2000);
        let result = sanitize_error_message(&long);
        assert!(result.len() < 2000);
        assert!(result.ends_with("...(truncated)"));
    }

    #[test]
    fn sanitize_error_strips_control_chars() {
        let msg = "error\x00with\x01control\nchars";
        let result = sanitize_error_message(msg);
        assert_eq!(result, "errorwithcontrol\nchars");
    }

    #[test]
    fn builder_default() {
        let client = OllamaClient::builder().build();
        assert!(client.is_ok());
    }

    #[test]
    fn builder_custom_timeout() {
        let client = OllamaClient::builder()
            .timeout(Duration::from_secs(300))
            .connect_timeout(Duration::from_secs(10))
            .build();
        assert!(client.is_ok());
    }

    #[test]
    fn builder_invalid_url() {
        let client = OllamaClient::builder().base_url("ftp://bad").build();
        assert!(client.is_err());
    }
}