yammer 0.16.0

yammer provides an ollama-compatible client library.
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
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]

use std::fs::OpenOptions;
use std::io::{Read, Write};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime};

use reqwest::RequestBuilder;
use utf8path::Path;

mod chat;
mod chats;
mod cli;
mod fmt;
mod types;
mod wrap;

pub use chat::{Chat, ChatOptions};
pub use chats::{Chats, ChatsOptions};
pub use fmt::Formattable;
pub use types::{
    ChatMessage, ChatRequest, ChatResponse, EmbedRequest, EmbedResponse, GenerateRequest,
    GenerateResponse,
};
pub use wrap::WordWrap;

///////////////////////////////////////////// constants ////////////////////////////////////////////

/// The default host to connect to.
pub const OLLAMA_HOST: &str = "http://localhost:11434";

/////////////////////////////////////////////// Error //////////////////////////////////////////////

/// An error that can occur when interacting with the ollama API.
#[derive(Debug)]
pub enum Error {
    /// An Internal error occurred.
    Internal,
    /// A signal interrupted the call.
    Signal,
    /// The EDITOR environment variable is not set.
    EditorNotSet,
    /// EDITOR failed.
    EditorFailed(Option<i32>),
    /// The YAMMER_CHAT environment variable is not set.
    ChatNotSet,
    /// An invalid argument was passed.
    InvalidArgument(String),
    /// An error occurred in the ollama service.
    Ollama(String),
    /// An I/O error occurred.
    Io(std::io::Error),
    /// A UTF-8 error occurred.
    Utf8Error(std::str::Utf8Error),
    /// A JSON error occurred.
    Json(serde_json::Error),
    /// A Reqwest error occurred.
    Reqwest(reqwest::Error),
}

impl std::error::Error for Error {}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::Internal => write!(f, "Internal error"),
            Self::Signal => write!(f, "Signal received"),
            Self::EditorNotSet => write!(f, "EDITOR not set"),
            Self::EditorFailed(Some(code)) => write!(f, "Editor failed with exit code {}", code),
            Self::EditorFailed(None) => write!(f, "Editor failed without exit code"),
            Self::ChatNotSet => write!(f, "YAMMER_CHAT not set"),
            Self::InvalidArgument(message) => write!(f, "invalid argument: {}", message),
            Self::Ollama(s) => write!(f, "Ollama error: {}", s),
            Self::Io(e) => write!(f, "I/O error: {}", e),
            Self::Utf8Error(e) => write!(f, "UTF-8 error: {}", e),
            Self::Json(e) => write!(f, "JSON error: {}", e),
            Self::Reqwest(e) => write!(f, "Reqwest error: {}", e),
        }
    }
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(err: std::str::Utf8Error) -> Self {
        Self::Utf8Error(err)
    }
}

impl From<serde_json::Error> for Error {
    fn from(e: serde_json::Error) -> Self {
        Self::Json(e)
    }
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Self::Reqwest(e)
    }
}

//////////////////////////////////////////// Parameters ////////////////////////////////////////////

/// Parameters for the model.
///
/// These correspond to the same name as PARAMETER options in Ollama.
#[derive(
    Clone, Debug, Default, arrrg_derive::CommandLine, serde::Deserialize, serde::Serialize,
)]
pub struct Parameters {
    /// Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)
    #[arrrg(optional, "Enable Mirostat sampling for controlling perplexity. (default: 0, 0 = disabled, 1 = Mirostat, 2 = Mirostat 2.0)")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mirostat: Option<i32>,

    /// Influences how quickly the algorithm responds to feedback from the generated text.
    ///
    /// A lower learning rate will result in slower adjustments, while a higher learning rate will
    /// make the algorithm more responsive. (Default: 0.1)
    #[arrrg(
        optional,
        "Influences how quickly the algorithm responds to feedback from the generated text."
    )]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mirostat_eta: Option<f64>,

    /// Controls the balance between coherence and diversity of the output.
    ///
    /// A lower value will result in more focused and coherent text. (Default: 5.0)
    #[arrrg(
        optional,
        "Controls the balance between coherence and diversity of the output."
    )]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mirostat_tau: Option<f64>,

    /// The number of tokens worth of context to allocate.
    #[arrrg(optional, "The number of tokens worth of context to allocate.")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub num_ctx: Option<u32>,

    /// Sets how far back for the model to look back to prevent repetition.
    ///
    /// (Default: 64, 0 = disabled, -1 = num_ctx)
    #[arrrg(
        optional,
        "Sets how far back for the model to look back to prevent repetition."
    )]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repeat_last_n: Option<i32>,

    /// Sets how strongly to penalize repetitions.
    ///
    /// A higher value (e.g., 1.5) will penalize repetitions more strongly, while a lower value
    /// (e.g., 0.9) will be more lenient. (Default: 1.1)
    #[arrrg(optional, "Sets how strongly to penalize repetitions.")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repeat_penalty: Option<f64>,

    /// The temperature of the model.
    ///
    /// Increasing the temperature will make the model answer more creatively. (Default: 0.8)
    #[arrrg(optional, "The temperature of the model.")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f64>,

    /// Sets the random number seed to use for generation.
    ///
    /// Setting this to a specific number will make the model generate the same text for the same
    /// prompt.  (Default: 0)
    #[arrrg(optional, "Sets the random number seed to use for generation.")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub seed: Option<i32>,

    /// Tail free sampling is used to reduce the impact of less probable tokens from the output.
    ///
    /// A higher value (e.g., 2.0) will reduce the impact more, while a value of 1.0 disables this
    /// setting. (default: 1)
    #[arrrg(
        optional,
        "Tail free sampling is used to reduce the impact of less probable tokens from the output."
    )]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tfs_z: Option<f64>,

    /// Maximum number of tokens to predict when generating text.
    ///
    /// (Default: 128, -1 = infinite generation, -2 = fill context)
    #[arrrg(optional, "Maximum number of tokens to predict when generating text.")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub num_predict: Option<i32>,

    /// Reduces the probability of generating nonsense.
    ///
    /// A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10)
    /// will be more conservative. (Default: 40)
    #[arrrg(optional, "Reduces the probability of generating nonsense.")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_k: Option<i32>,

    /// Works together with top-k.
    ///
    /// A higher value (e.g., 0.95) will lead to more diverse text, while a lower value (e.g., 0.5)
    /// will generate more focused and conservative text. (Default: 0.9)
    #[arrrg(optional, "Works together with top-k.")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f64>,

    /// Alternative to the top_p, and aims to ensure a balance of quality and variety.
    ///
    /// The parameter p represents the minimum probability for a token to be considered, relative
    /// to the probability of the most likely token. For example, with p=0.05 and the most likely
    /// token having a probability of 0.9, logits with a value less than 0.045 are filtered out.
    /// (Default: 0.0)
    #[arrrg(
        optional,
        "Alternative to the top_p, and aims to ensure a balance of quality and variety."
    )]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_p: Option<f64>,
}

impl Parameters {
    /// Overlay the parameters from another set of parameters.
    pub fn apply(&mut self, from: Self) {
        if let Some(mirostat) = from.mirostat {
            self.mirostat = Some(mirostat);
        }
        if let Some(mirostat_eta) = from.mirostat_eta {
            self.mirostat_eta = Some(mirostat_eta);
        }
        if let Some(mirostat_tau) = from.mirostat_tau {
            self.mirostat_tau = Some(mirostat_tau);
        }
        if let Some(num_ctx) = from.num_ctx {
            self.num_ctx = Some(num_ctx);
        }
        if let Some(repeat_last_n) = from.repeat_last_n {
            self.repeat_last_n = Some(repeat_last_n);
        }
        if let Some(repeat_penalty) = from.repeat_penalty {
            self.repeat_penalty = Some(repeat_penalty);
        }
        if let Some(temperature) = from.temperature {
            self.temperature = Some(temperature);
        }
        if let Some(seed) = from.seed {
            self.seed = Some(seed);
        }
        if let Some(tfs_z) = from.tfs_z {
            self.tfs_z = Some(tfs_z);
        }
        if let Some(num_predict) = from.num_predict {
            self.num_predict = Some(num_predict);
        }
        if let Some(top_k) = from.top_k {
            self.top_k = Some(top_k);
        }
        if let Some(top_p) = from.top_p {
            self.top_p = Some(top_p);
        }
        if let Some(min_p) = from.min_p {
            self.min_p = Some(min_p);
        }
    }
}

impl From<Parameters> for serde_json::Value {
    fn from(p: Parameters) -> serde_json::Value {
        let mut json = serde_json::json!({});
        if let Some(mirostat) = p.mirostat {
            json["mirostat"] = serde_json::json!(mirostat);
        }
        if let Some(mirostat_eta) = p.mirostat_eta {
            json["mirostat_eta"] = serde_json::json!(mirostat_eta);
        }
        if let Some(mirostat_tau) = p.mirostat_tau {
            json["mirostat_tau"] = serde_json::json!(mirostat_tau);
        }
        if let Some(num_ctx) = p.num_ctx {
            json["num_ctx"] = serde_json::json!(num_ctx);
        }
        if let Some(repeat_last_n) = p.repeat_last_n {
            json["repeat_last_n"] = serde_json::json!(repeat_last_n);
        }
        if let Some(repeat_penalty) = p.repeat_penalty {
            json["repeat_penalty"] = serde_json::json!(repeat_penalty);
        }
        if let Some(temperature) = p.temperature {
            json["temperature"] = serde_json::json!(temperature);
        }
        if let Some(seed) = p.seed {
            json["seed"] = serde_json::json!(seed);
        }
        if let Some(tfs_z) = p.tfs_z {
            json["tfs_z"] = serde_json::json!(tfs_z);
        }
        if let Some(num_predict) = p.num_predict {
            json["num_predict"] = serde_json::json!(num_predict);
        }
        if let Some(top_k) = p.top_k {
            json["top_k"] = serde_json::json!(top_k);
        }
        if let Some(top_p) = p.top_p {
            json["top_p"] = serde_json::json!(top_p);
        }
        if let Some(min_p) = p.min_p {
            json["min_p"] = serde_json::json!(min_p);
        }
        json
    }
}

impl Eq for Parameters {}

impl PartialEq for Parameters {
    fn eq(&self, other: &Self) -> bool {
        self.mirostat == other.mirostat
            && self.mirostat_eta == other.mirostat_eta
            && self.mirostat_tau == other.mirostat_tau
            && self.num_ctx == other.num_ctx
            && self.repeat_last_n == other.repeat_last_n
            && self.repeat_penalty == other.repeat_penalty
            && self.temperature == other.temperature
            && self.seed == other.seed
            && self.tfs_z == other.tfs_z
            && self.num_predict == other.num_predict
            && self.top_k == other.top_k
            && self.top_p == other.top_p
            && self.min_p == other.min_p
    }
}

////////////////////////////////////////////// Shellm //////////////////////////////////////////////

/// Options for the `shellm` command.
#[derive(Clone, Debug, Eq, PartialEq, arrrg_derive::CommandLine)]
pub struct ShellmOptions {
    /// The host to connect to.
    #[arrrg(optional, "The host to connect to.")]
    pub ollama_host: Option<String>,
    /// The model to use from the ollama library.
    #[arrrg(optional, "The model to use from the ollama library.")]
    pub model: String,
    /// The suffix to append to the response.
    #[arrrg(optional, "The suffix to append to the response.")]
    pub suffix: Option<String>,
    /// The system to use in the template.
    #[arrrg(optional, "The system to use in the template.")]
    pub system: Option<String>,
    /// The template to use for the prompt.
    #[arrrg(optional, "The template to use for the prompt.")]
    pub template: Option<String>,
    /// Format the response in JSON.  You must also ask the model to do so.
    #[arrrg(
        flag,
        "Format the response in JSON.  You must also ask the model to do so."
    )]
    pub json: bool,
    /// Schema to adhere to when formatting the response in JSON.  Has no effect without --json.
    #[arrrg(
        optional,
        "Schema to adhere to when formatting the response in JSON.  Has no effect without --json."
    )]
    pub schema: Option<serde_json::Value>,
    /// Whether to pass bypass formatting of the prompt.
    #[arrrg(optional, "Whether to pass bypass formatting of the prompt.")]
    pub raw: Option<bool>,
    /// Duration to keep the model in memory for after the call.
    #[arrrg(optional, "Duration to keep the model in memory for after the call.")]
    pub keep_alive: Option<String>,
    /// Additional options to pass to the model.
    #[arrrg(nested)]
    pub param: Parameters,
    /// Wrap at this line length, or 0 to disable yammer-induced wrapping.
    #[arrrg(
        optional,
        "Wrap at this line length, or 0 to disable yammer-induced wrapping."
    )]
    pub wrap: Option<usize>,
}

impl Default for ShellmOptions {
    fn default() -> Self {
        ShellmOptions {
            ollama_host: None,
            // TODO(rescrv):  Don't hard-code the default model.
            model: "gemma2".to_string(),
            suffix: None,
            system: None,
            template: None,
            json: false,
            schema: None,
            raw: None,
            keep_alive: None,
            param: Parameters::default(),
            wrap: None,
        }
    }
}

////////////////////////////////////////////// shellm //////////////////////////////////////////////

/// The `shellm` command.
pub async fn shellm(
    options: ShellmOptions,
    promptfiles: &[impl AsRef<str>],
) -> Result<(), Box<dyn std::error::Error>> {
    let mut stdin: Option<String> = None;
    for promptfile in promptfiles {
        let promptfile = promptfile.as_ref();
        let prompt = if promptfile == "-" {
            if let Some(stdin) = stdin.as_ref() {
                stdin.clone()
            } else {
                let mut s = String::new();
                std::io::stdin().read_to_string(&mut s)?;
                stdin = Some(s.clone());
                s
            }
        } else {
            match std::fs::read_to_string(promptfile) {
                Ok(s) => s,
                Err(e) => {
                    eprintln!("shellm: {}: {}", promptfile, e);
                    continue;
                }
            }
        };
        let gen = GenerateRequest {
            model: options.model.clone(),
            prompt,
            suffix: options.suffix.clone(),
            images: None,
            format: if options.json {
                if let Some(schema) = options.schema.clone() {
                    Some(schema)
                } else {
                    Some(serde_json::Value::String("json".to_string()))
                }
            } else {
                None
            },
            system: options.system.clone(),
            template: options.template.clone(),
            stream: Some(true),
            raw: options.raw,
            keep_alive: None,
            options: Some(options.param.clone().into()),
        };
        let req = gen.make_request(&ollama_host(options.ollama_host.clone()));
        let mut ww = WordWrap::new(options.wrap.unwrap_or(100));
        let res = stream(req, |v| {
            if let Some(serde_json::Value::String(message)) = v.get("response") {
                ww.push(message.clone(), &mut std::io::stdout())?;
            }
            Ok(())
        })
        .await;
        if let Err(Error::Signal) = res {
            break;
        } else if let Err(err) = res {
            return Err(err.into());
        }
        writeln!(std::io::stdout())?;
    }
    Ok(())
}

////////////////////////////////////////// OneShotOptions //////////////////////////////////////////

/// Options for the `oneshot` command.
#[derive(Clone, Debug, Default, Eq, PartialEq, arrrg_derive::CommandLine)]
pub struct OneshotOptions {
    /// The host to connect to.
    #[arrrg(optional, "The host to connect to.")]
    pub ollama_host: Option<String>,
    /// The suffix to append to the response.
    #[arrrg(optional, "The suffix to append to the response.")]
    pub suffix: Option<String>,
    /// The system to use in the template.
    #[arrrg(optional, "The system to use in the template.")]
    pub system: Option<String>,
    /// The template to use for the prompt.
    #[arrrg(optional, "The template to use for the prompt.")]
    pub template: Option<String>,
    /// Format the response in JSON.  You must also ask the model to do so.
    #[arrrg(
        flag,
        "Format the response in JSON.  You must also ask the model to do so."
    )]
    pub json: bool,
    /// Schema to adhere to when formatting the response in JSON.  Has no effect without --json.
    #[arrrg(
        optional,
        "Schema to adhere to when formatting the response in JSON.  Has no effect without --json."
    )]
    pub schema: Option<serde_json::Value>,
    /// Whether to pass bypass formatting of the prompt.
    #[arrrg(optional, "Whether to pass bypass formatting of the prompt.")]
    pub raw: Option<bool>,
    /// Duration to keep the model in memory for after the call.
    #[arrrg(optional, "Duration to keep the model in memory for after the call.")]
    pub keep_alive: Option<String>,
    /// Additional options to pass to the model.
    #[arrrg(nested)]
    pub param: Parameters,
    /// Wrap at this line length, or 0 to disable yammer-induced wrapping.
    #[arrrg(
        optional,
        "Wrap at this line length, or 0 to disable yammer-induced wrapping."
    )]
    pub wrap: Option<usize>,
}

////////////////////////////////////////////// editor //////////////////////////////////////////////

/// Invoke an editor with a default message and return something like a string.
pub fn editor(default: &str) -> Result<impl AsRef<String>, Error> {
    let path = format!(
        ".yammer.{}.{}",
        std::process::id(),
        SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_micros()
    );
    let editor = std::env::var("EDITOR").map_err(|_| Error::EditorNotSet)?;
    struct UnlinkOnDrop(String);
    impl Drop for UnlinkOnDrop {
        fn drop(&mut self) {
            let _ = std::fs::remove_file(&self.0);
        }
    }
    impl AsRef<String> for UnlinkOnDrop {
        fn as_ref(&self) -> &String {
            &self.0
        }
    }
    let mut file = OpenOptions::new()
        .create_new(true)
        .write(true)
        .open(&path)?;
    let unlink = UnlinkOnDrop(path.clone());
    file.write_all(default.as_bytes())?;
    file.flush()?;
    file.sync_all()?;
    drop(file);
    let status = std::process::Command::new(editor).arg(&path).status()?;
    if Some(0) != status.code() {
        return Err(Error::EditorFailed(status.code()));
    }
    Ok(unlink)
}

////////////////////////////////////////////// oneshot /////////////////////////////////////////////

/// The `oneshot` command.
pub async fn oneshot(
    options: OneshotOptions,
    models: &[impl AsRef<str>],
) -> Result<(), Box<dyn std::error::Error>> {
    let path = editor("Replace this text with your prompt.")?;
    for model in models {
        let options = ShellmOptions {
            ollama_host: options.ollama_host.clone(),
            model: model.as_ref().to_string(),
            suffix: options.suffix.clone(),
            system: options.system.clone(),
            template: options.template.clone(),
            json: options.json,
            schema: options.schema.clone(),
            raw: options.raw,
            keep_alive: options.keep_alive.clone(),
            param: options.param.clone(),
            wrap: options.wrap,
        };
        shellm(options, &[path.as_ref()]).await?;
    }
    Ok(())
}

/////////////////////////////////////////// PromptOptions //////////////////////////////////////////

/// Options for the `prompt` command.
#[derive(Clone, Debug, Eq, PartialEq, arrrg_derive::CommandLine)]
pub struct PromptOptions {
    /// The host to connect to.
    #[arrrg(optional, "The host to connect to.")]
    pub ollama_host: Option<String>,
    /// The model to use from the ollama library.
    #[arrrg(optional, "The model to use from the ollama library.")]
    pub model: String,
    /// The suffix to append to the response.
    #[arrrg(optional, "The suffix to append to the response.")]
    pub suffix: Option<String>,
    /// The system to use in the template.
    #[arrrg(optional, "The system to use in the template.")]
    pub system: Option<String>,
    /// The template to use for the prompt.
    #[arrrg(optional, "The template to use for the prompt.")]
    pub template: Option<String>,
    /// Format the response in JSON.  You must also ask the model to do so.
    #[arrrg(
        flag,
        "Format the response in JSON.  You must also ask the model to do so."
    )]
    pub json: bool,
    /// Schema to adhere to when formatting the response in JSON.  Has no effect without --json.
    #[arrrg(
        optional,
        "Schema to adhere to when formatting the response in JSON.  Has no effect without --json."
    )]
    pub schema: Option<serde_json::Value>,
    /// Whether to pass bypass formatting of the prompt.
    #[arrrg(optional, "Whether to pass bypass formatting of the prompt.")]
    pub raw: Option<bool>,
    /// Duration to keep the model in memory for after the call.
    #[arrrg(optional, "Duration to keep the model in memory for after the call.")]
    pub keep_alive: Option<String>,
    /// Additional options to pass to the model.
    #[arrrg(nested)]
    pub param: Parameters,
    /// Wrap at this line length, or 0 to disable yammer-induced wrapping.
    #[arrrg(
        optional,
        "Wrap at this line length, or 0 to disable yammer-induced wrapping."
    )]
    pub wrap: Option<usize>,
}

impl Default for PromptOptions {
    fn default() -> Self {
        PromptOptions {
            ollama_host: None,
            model: "gemma2".to_string(),
            suffix: None,
            system: None,
            template: None,
            json: false,
            raw: None,
            schema: None,
            keep_alive: None,
            param: Parameters::default(),
            wrap: None,
        }
    }
}

////////////////////////////////////////////// Prompt //////////////////////////////////////////////

/// The `prompt` command.
pub async fn prompt(
    options: PromptOptions,
    prompts: &[impl AsRef<str>],
) -> Result<(), Box<dyn std::error::Error>> {
    for prompt in prompts {
        let gen = GenerateRequest {
            model: options.model.clone(),
            prompt: prompt.as_ref().to_string(),
            suffix: options.suffix.clone(),
            images: None,
            format: if options.json {
                if let Some(schema) = options.schema.clone() {
                    Some(schema)
                } else {
                    Some(serde_json::Value::String("json".to_string()))
                }
            } else {
                None
            },
            system: options.system.clone(),
            template: options.template.clone(),
            stream: Some(true),
            raw: options.raw,
            keep_alive: None,
            options: Some(options.param.clone().into()),
        };
        let req = gen.make_request(&ollama_host(options.ollama_host.clone()));
        let mut ww = WordWrap::new(options.wrap.unwrap_or(100));
        let res = stream(req, |v| {
            if let Some(serde_json::Value::String(message)) = v.get("response") {
                ww.push(message.clone(), &mut std::io::stdout())?;
            }
            Ok(())
        })
        .await;
        if let Err(Error::Signal) = res {
            break;
        } else if let Err(err) = res {
            return Err(err.into());
        }
        writeln!(std::io::stdout())?;
    }
    Ok(())
}

//////////////////////////////////////////// chat_shell ////////////////////////////////////////////

/// Start the `chat` shell.
pub async fn chat_shell(changelog: Option<Path<'_>>, options: ChatOptions) -> Result<(), Error> {
    let chat = Chat::new(changelog, options)?;
    chat.shell().await
}

//////////////////////////////////////////// chats_shell ///////////////////////////////////////////

/// Start the `chats` shell.
pub async fn chats_shell(options: ChatsOptions) -> Result<(), Error> {
    let chats = Chats::new(options)?;
    chats.shell().await
}

////////////////////////////////////////////// stream //////////////////////////////////////////////

/// Stream the response of a request, calling `for_each` on each JSON object in the response.
pub async fn stream(
    req: RequestBuilder,
    for_each: impl FnMut(serde_json::Value) -> Result<(), Error>,
) -> Result<(), Error> {
    let sns = stream_no_signal(req, for_each);
    let sig = async {
        loop {
            tokio::time::sleep(Duration::from_millis(50)).await;
            if minimal_signals::pending()
                .iter()
                .filter(|s| *s != minimal_signals::SIGCHLD)
                .count()
                > 0
            {
                break;
            }
        }
    };
    tokio::select! {
        res = sns => res,
        _ = sig => Err(Error::Signal),
    }
}

async fn stream_no_signal(
    req: RequestBuilder,
    mut for_each: impl FnMut(serde_json::Value) -> Result<(), Error>,
) -> Result<(), Error> {
    let mut resp = req.send().await?;
    if resp.status() != 200 {
        return if let Some(chunk) = resp.chunk().await? {
            #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
            struct ErrorResponse {
                pub error: String,
            }
            let err = serde_json::from_slice::<ErrorResponse>(&chunk)?;
            Err(Error::Ollama(err.error))
        } else {
            Err(Error::Internal)
        };
    }
    let mut leftovers: Vec<u8> = vec![];
    while let Some(chunk) = resp.chunk().await? {
        leftovers.extend(&chunk);
        if let Ok(value) = serde_json::from_slice(&leftovers) {
            for_each(value)?;
            leftovers.clear();
        }
    }
    if !leftovers.is_empty() {
        let Ok(value) = serde_json::from_slice(&leftovers) else {
            return Err(Error::Ollama(format!(
                "Host returned invalid JSON chunk {leftovers:?}"
            )));
        };
        for_each(value)?;
    }
    Ok(())
}

//////////////////////////////////////////// ollama_host ///////////////////////////////////////////

/// Return the Ollama host, preferring the value passed in, falling back to the env var, falling
/// back to the hard-coded default.
pub fn ollama_host(host: Option<String>) -> String {
    host.unwrap_or_else(|| std::env::var("OLLAMA_HOST").unwrap_or_else(|_| OLLAMA_HOST.to_string()))
}

///////////////////////////////////////////// chat_root ////////////////////////////////////////////

/// The root on the filesystem for chats.
pub fn chat_root() -> Result<Path<'static>, Error> {
    let root = std::env::var("YAMMER_CHAT").map_err(|_| Error::ChatNotSet)?;
    Ok(Path::from(root))
}

///////////////////////////////////////////// chat_path ////////////////////////////////////////////

/// The path for one specific chat.
pub fn chat_path(chat_id: &str) -> Result<Path<'static>, Error> {
    let root = chat_root()?;
    Ok(root.join("chats").join(format!("{}.ndjson", chat_id)))
}

////////////////////////////////////////////// Spinner /////////////////////////////////////////////

const SPINNER: &[&str] = &["", "", "", "", "", "", "", "", "", ""];

/// A spinner widget.
#[derive(Debug)]
pub struct Spinner {
    done: Arc<AtomicBool>,
    inhibited: Arc<Mutex<bool>>,
    background: Option<std::thread::JoinHandle<()>>,
}

impl Spinner {
    /// Create a new spinner.
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        let done = Arc::new(AtomicBool::new(false));
        let done_p = Arc::clone(&done);
        let inhibited = Arc::new(Mutex::new(true));
        let inhibited_p = Arc::clone(&inhibited);
        let background = std::thread::spawn(move || {
            let mut i = 0;
            while !done_p.load(Ordering::Relaxed) {
                std::thread::sleep(std::time::Duration::from_millis(50));
                let inhibited_p = inhibited_p.lock().unwrap();
                if *inhibited_p {
                    continue;
                }
                let mut stderr = std::io::stderr().lock();
                let _ = stderr.write(b"\x1b[2K\r");
                let _ = stderr.write(SPINNER[i % SPINNER.len()].as_bytes());
                let _ = stderr.write(" ".as_bytes());
                let _ = stderr.flush();
                i += 1;
            }
        });
        Self {
            done,
            inhibited,
            background: Some(background),
        }
    }

    /// Start the spinner.
    pub fn start(&self) {
        *self.inhibited.lock().unwrap() = false;
    }

    /// Inhibit the spinner.
    pub fn inhibit(&self) {
        let mut inhibited = self.inhibited.lock().unwrap();
        if !*inhibited {
            *inhibited = true;
            let mut stderr = std::io::stderr().lock();
            let _ = stderr.write(b"\x1b[2K\r");
        }
    }
}

impl Drop for Spinner {
    fn drop(&mut self) {
        self.done.store(true, Ordering::Relaxed);
        self.inhibit();
        if let Some(background) = self.background.take() {
            background.join().unwrap();
        }
    }
}

//////////////////////////////////////////// JsonSchema ////////////////////////////////////////////

/// Implement JsonSchema to derive the schema for GenerateRequest automatically.
pub trait JsonSchema {
    /// Return the json_schema.  Does not depend on an object.
    fn json_schema() -> serde_json::Value;
}

impl JsonSchema for bool {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "boolean" }}
    }
}

impl JsonSchema for i8 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for i16 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for i32 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for i64 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for u8 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for u16 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for u32 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for u64 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "integer" }}
    }
}

impl JsonSchema for f32 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "number" }}
    }
}

impl JsonSchema for f64 {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "number" }}
    }
}

impl JsonSchema for String {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "string" }}
    }
}

impl<T: JsonSchema> JsonSchema for Option<T> {
    fn json_schema() -> serde_json::Value {
        let mut res = <T as JsonSchema>::json_schema();
        res["nullable"] = true.into();
        res
    }
}

impl<T: JsonSchema> JsonSchema for Vec<T> {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{ "type": "array", "items": <T as JsonSchema>::json_schema() }}
    }
}

impl JsonSchema for serde_json::Value {
    fn json_schema() -> serde_json::Value {
        serde_json::json! {{}}
    }
}

impl<Tz: chrono::TimeZone> JsonSchema for chrono::DateTime<Tz> {
    fn json_schema() -> serde_json::Value {
        String::json_schema()
    }
}

//////////////////////////////////////////// ToolBuilder ///////////////////////////////////////////

/// Build a tool for use in chat completions.
pub struct ToolBuilder {
    name: String,
    description: String,
    fields: Vec<(String, serde_json::Value)>,
}

impl ToolBuilder {
    /// Create a new tool.  Name is the name of the function, and description is a plain-language
    /// description of what it does.
    pub fn new(name: &str, description: &str) -> Self {
        let name = name.to_string();
        let description = description.to_string();
        let fields = vec![];
        Self {
            name,
            description,
            fields,
        }
    }

    /// Append an argument to the tool call.  All arguments are required by convention.
    pub fn arg<T: JsonSchema>(mut self, name: &str) -> Self {
        self.fields.push((name.to_string(), T::json_schema()));
        self
    }

    /// Consume the [ToolBuilder] and return a JSON blob suitable for passing to Ollama.
    pub fn build(self) -> serde_json::Value {
        let mut properties = serde_json::json! {{}};
        let mut required = vec![];
        for (name, schema) in self.fields.into_iter() {
            properties[name.clone()] = schema;
            required.push(name);
        }
        let required: serde_json::Value = required.into();
        let parameters = serde_json::json! {{
            "type": "object",
            "properties": properties,
            "required": required,
        }};
        serde_json::json! {{
            "type": "function",
            "function": {
                "name": self.name,
                "description": self.description,
                "parameters": parameters,
            }
        }}
    }
}

/////////////////////////////////////////////// tests //////////////////////////////////////////////

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

    #[test]
    fn tool_builder() {
        let tb = ToolBuilder::new(
            "build_widgets",
            "Create N different widgets of the specified color",
        )
        .arg::<String>("color")
        .arg::<f64>("count")
        .build();
        assert_eq!(
            r#"{
  "function": {
    "description": "Create N different widgets of the specified color",
    "name": "build_widgets",
    "parameters": {
      "properties": {
        "color": {
          "type": "string"
        },
        "count": {
          "type": "number"
        }
      },
      "required": [
        "color",
        "count"
      ],
      "type": "object"
    }
  },
  "type": "function"
}"#,
            serde_json::to_string_pretty(&tb).unwrap()
        );
    }
}