Skip to main content

kjarni_cli/
lib.rs

1use clap::{Parser, Subcommand};
2
3/// Default model for `kjarni chat`.
4///
5/// Must be a CLI name that `ModelType::from_cli_name` resolves. Both this and
6/// [`DEFAULT_GENERATE_MODEL`] previously named models that do not exist
7/// (`llama-3.2-8b-instruct`, `llama-3.2-1b`), so the bare `kjarni chat` and
8/// `kjarni generate` commands failed for everyone. `tests::default_models_exist`
9/// now fails the build if either drifts from the registry again.
10pub const DEFAULT_CHAT_MODEL: &str = "llama3.2-3b-instruct";
11
12/// Default model for `kjarni generate`.
13pub const DEFAULT_GENERATE_MODEL: &str = "llama3.2-1b-instruct";
14
15#[derive(Parser)]
16#[command(name = "kjarni")]
17#[command(about = "Kjarni: The SQLite of AI", long_about = None)]
18#[command(version)]
19pub struct Cli {
20    #[command(subcommand)]
21    pub command: Commands,
22
23    /// Verbosity level (-v, -vv, -vvv)
24    #[arg(short, long, action = clap::ArgAction::Count, global = true)]
25    pub verbose: u8,
26}
27
28#[derive(Subcommand, Debug, PartialEq)]
29pub enum Commands {
30    /// Manage models (list, download, info)
31    Model {
32        #[command(subcommand)]
33        action: ModelCommands,
34    },
35
36    /// Generate text from a prompt
37    Generate {
38        /// The prompt (or file path, or stdin if not provided)
39        prompt: Option<String>,
40
41        #[arg(short, long, default_value = DEFAULT_GENERATE_MODEL)]
42        model: String,
43
44        /// Load weights from a local file or directory instead of the registry
45        #[arg(long)]
46        model_path: Option<String>,
47
48        /// Maximum tokens to generate
49        #[arg(short = 'n', long, default_value_t = 100)]
50        max_tokens: usize,
51
52        /// Sampling temperature (0.0 = greedy, higher = more random)
53        #[arg(short, long, default_value_t = 0.7)]
54        temperature: f32,
55
56        /// Top-K sampling (limits to K most likely tokens)
57        #[arg(long)]
58        top_k: Option<usize>,
59
60        /// Top-P (nucleus) sampling threshold
61        #[arg(long)]
62        top_p: Option<f32>,
63
64        /// Min-P sampling threshold  
65        #[arg(long)]
66        min_p: Option<f32>,
67
68        /// Repetition penalty (1.0 = no penalty)
69        #[arg(long, default_value_t = 1.1)]
70        repetition_penalty: f32,
71
72        /// Use greedy decoding (ignores temperature)
73        #[arg(long)]
74        greedy: bool,
75
76        /// Use GPU
77        #[arg(long)]
78        gpu: bool,
79
80        /// Disable streaming output
81        #[arg(long)]
82        no_stream: bool,
83
84        /// Suppress status messages
85        #[arg(short, long)]
86        quiet: bool,
87
88        /// Prefer quantized GGUF weights when the model has them
89        ///
90        /// Q4_K_M is a fraction of the size and markedly faster to decode:
91        /// measured on an RTX A2000, llama3.2-3b runs 24.5 tok/s from GGUF
92        /// against 8.7 from full-precision safetensors. Falls back with a notice
93        /// when the model has no GGUF published.
94        #[arg(long)]
95        gguf: bool,
96
97        /// Draft model for speculative decoding. Must share the target's
98        /// vocabulary: a small model of the same family, such as
99        /// qwen2.5-0.5b-instruct drafting for qwen2.5-1.5b.
100        #[arg(long)]
101        draft: Option<String>,
102
103        /// Tokens the draft proposes per round. Higher trades wasted draft work
104        /// against fewer passes over the target's weights.
105        #[arg(long, default_value_t = 4)]
106        draft_tokens: usize,
107    },
108
109    /// Summarize text
110    Summarize {
111        /// Input text (or read from stdin)
112        #[arg(short, long)]
113        input: Option<String>,
114
115        /// Model to use
116        #[arg(short, long, default_value = "distilbart-cnn")]
117        model: String,
118
119        /// Path to local model
120        /// Load weights from a local file or directory instead of the registry
121        #[arg(long)]
122        model_path: Option<String>,
123
124        /// Minimum summary length
125        #[arg(long)]
126        min_length: Option<usize>,
127
128        /// Maximum summary length
129        #[arg(long)]
130        max_length: Option<usize>,
131
132        /// Number of beams for beam search
133        #[arg(long)]
134        num_beams: Option<usize>,
135
136        /// Length penalty for beam search (< 1 shorter, > 1 longer)
137        #[arg(long)]
138        length_penalty: Option<f32>,
139
140        /// Block repeated n-grams of this size
141        #[arg(long)]
142        no_repeat_ngram: Option<usize>,
143
144        /// Use greedy decoding (deterministic, fastest)
145        #[arg(long)]
146        greedy: bool,
147
148        /// Disable streaming output
149        #[arg(long)]
150        no_stream: bool,
151
152        /// Use GPU
153        #[arg(long)]
154        gpu: bool,
155
156        /// Suppress progress messages
157        #[arg(short, long)]
158        quiet: bool,
159    },
160
161    /// Translate text between languages
162    Translate {
163        /// Input text (or read from stdin)
164        #[arg(short, long)]
165        input: Option<String>,
166
167        /// Model to use
168        #[arg(short, long, default_value = "flan-t5-base")]
169        model: String,
170
171        /// Path to local model
172        /// Load weights from a local file or directory instead of the registry
173        #[arg(long)]
174        model_path: Option<String>,
175
176        /// Source language (e.g., en, de, fr)
177        #[arg(long)]
178        src: Option<String>,
179
180        /// Target language (e.g., en, de, fr)
181        #[arg(long)]
182        dst: Option<String>,
183
184        /// Maximum output length
185        #[arg(long)]
186        max_length: Option<usize>,
187
188        /// Number of beams for beam search
189        #[arg(long)]
190        num_beams: Option<usize>,
191
192        /// Length penalty for beam search (< 1 shorter, > 1 longer)
193        #[arg(long)]
194        length_penalty: Option<f32>,
195
196        /// Block repeated n-grams of this size
197        #[arg(long)]
198        no_repeat_ngram: Option<usize>,
199
200        /// Use greedy decoding (deterministic, fastest)
201        #[arg(long)]
202        greedy: bool,
203
204        /// Disable streaming output
205        #[arg(long)]
206        no_stream: bool,
207
208        /// Use GPU
209        #[arg(long)]
210        gpu: bool,
211
212        /// Suppress progress messages
213        #[arg(short, long)]
214        quiet: bool,
215    },
216
217    /// Show a model's metadata, config and tensor layout
218    Inspect {
219        /// Path to a .gguf, a .safetensors directory, or a name in ~/.cache/kjarni
220        path: String,
221    },
222
223    /// Generate embeddings for text
224    Embed {
225        /// Input text, file path, or stdin if not provided
226        input: Option<String>,
227
228        /// Embedding model
229        #[arg(short, long, default_value = "minilm-l6-v2")]
230        model: String,
231
232        /// Load weights from a local file or directory instead of the registry
233        #[arg(long)]
234        model_path: Option<String>,
235
236        /// Output format: raw, json
237        #[arg(long, default_value = "raw")]
238        format: String,
239
240        /// Scale each vector to unit length
241        #[arg(long)]
242        normalize: bool,
243
244        /// Pooling strategy: mean, cls, max
245        ///
246        /// Mean matches what sentence-transformers does for these models, and what the
247        /// library bindings and presets already default to. Pooling changes the vector
248        /// itself, so a different choice here does not produce a slightly different
249        /// answer, it produces one that cannot be compared against the others.
250        #[arg(long, default_value = "mean")]
251        pooling: String,
252
253        /// Run on the GPU
254        #[arg(long)]
255        gpu: bool,
256
257        /// Suppress status messages
258        #[arg(short, long)]
259        quiet: bool,
260    },
261
262    /// Transcribe audio to text
263    Transcribe {
264        /// Path to audio file (wav, mp3, flac, ogg)
265        file: String,
266
267        /// Model to use (whisper-small, whisper-large-v3)
268        #[arg(short, long, default_value = "whisper-small")]
269        model: String,
270
271        /// Path to local model directory (not yet implemented)
272        /// Load weights from a local file or directory instead of the registry
273        #[arg(long)]
274        model_path: Option<String>,
275
276        /// Language code (e.g., en, fr, de). Omit for auto-detect.
277        #[arg(short, long)]
278        language: Option<String>,
279
280        /// Translate to English instead of transcribing
281        #[arg(long)]
282        translate: bool,
283
284        /// Include timestamps in output
285        #[arg(short, long)]
286        timestamps: bool,
287
288        /// Maximum tokens per 30-second chunk
289        #[arg(long)]
290        max_tokens: Option<usize>,
291
292        /// Disable streaming (wait for full result)
293        #[arg(long)]
294        no_stream: bool,
295
296        /// Use GPU acceleration
297        #[arg(long)]
298        gpu: bool,
299
300        /// Suppress progress output
301        #[arg(short, long)]
302        quiet: bool,
303    },
304
305    /// Classify text using a classification model
306    Classify {
307        /// Input text(s) to classify. Use - for stdin.
308        input: Vec<String>,
309
310        /// Model name from registry
311        #[arg(short, long, default_value = "distilbert-sentiment")]
312        model: String,
313
314        /// Load model from local path instead of registry
315        #[arg(long, value_name = "PATH")]
316        model_path: Option<String>,
317
318        /// Custom labels (comma-separated, order must match model output)
319        /// Example: --labels "negative,positive" or --labels "neikvætt,jákvætt"
320        #[arg(long, value_name = "LABELS")]
321        labels: Option<String>,
322
323        /// Return top K predictions
324        #[arg(long, default_value = "5")]
325        top_k: usize,
326
327        /// Minimum confidence threshold (0.0-1.0)
328        #[arg(long)]
329        threshold: Option<f32>,
330
331        /// Maximum sequence length (truncates longer inputs)
332        #[arg(long)]
333        max_length: Option<usize>,
334
335        /// Batch size for inference
336        #[arg(long)]
337        batch_size: Option<usize>,
338
339        /// Use multi-label classification (sigmoid instead of softmax)
340        #[arg(long)]
341        multi_label: bool,
342
343        /// Output format: json, jsonl, text
344        #[arg(short, long, default_value = "text")]
345        format: String,
346
347        /// Run on GPU
348        #[arg(long)]
349        gpu: bool,
350
351        /// Model precision: f32, f16, bf16
352        #[arg(long)]
353        dtype: Option<String>,
354
355        /// Suppress progress output
356        #[arg(short, long)]
357        quiet: bool,
358    },
359
360    /// Rerank documents by relevance to a query
361    Rerank {
362        /// The query to rank against
363        query: String,
364
365        /// Documents to rerank (or read from stdin, one per line)
366        documents: Vec<String>,
367
368        #[arg(short, long, default_value = "minilm-l6-v2-cross-encoder")]
369        model: String,
370
371        /// Load weights from a local file or directory instead of the registry
372        #[arg(long)]
373        model_path: Option<String>,
374
375        /// Return only top K results
376        #[arg(short = 'k', long)]
377        top_k: Option<usize>,
378
379        /// Output format: json, jsonl, text, docs
380        #[arg(short, long, default_value = "text")]
381        format: String,
382
383        /// Run on the GPU
384        #[arg(long)]
385        gpu: bool,
386
387        /// Suppress progress output
388        #[arg(short, long)]
389        quiet: bool,
390    },
391
392    /// Interactive chat mode
393    Chat {
394        #[arg(short, long, default_value = DEFAULT_CHAT_MODEL)]
395        model: String,
396
397        /// Load weights from a local file or directory instead of the registry
398        #[arg(long)]
399        model_path: Option<String>,
400
401        /// System prompt to set assistant behavior
402        #[arg(short, long)]
403        system: Option<String>,
404
405        /// Sampling temperature
406        #[arg(short, long, default_value_t = 0.7)]
407        temperature: f32,
408
409        /// Max tokens per response
410        #[arg(short = 'n', long, default_value_t = 512)]
411        max_tokens: usize,
412
413        /// Run on the GPU
414        #[arg(long)]
415        gpu: bool,
416
417        /// Suppress progress output
418        #[arg(short, long)]
419        quiet: bool,
420
421        /// Prefer quantized GGUF weights when the model has them
422        ///
423        /// Q4_K_M is a fraction of the size and markedly faster to decode:
424        /// measured on an RTX A2000, llama3.2-3b runs 24.5 tok/s from GGUF
425        /// against 8.7 from full-precision safetensors. Falls back with a notice
426        /// when the model has no GGUF published.
427        #[arg(long)]
428        gguf: bool,
429
430        /// Draft model for speculative decoding. Must share the target's
431        /// vocabulary, such as qwen2.5-0.5b-instruct drafting for qwen2.5-1.5b.
432        #[arg(long)]
433        draft: Option<String>,
434
435        /// Tokens the draft proposes per round.
436        #[arg(long, default_value_t = 4)]
437        draft_tokens: usize,
438    },
439
440    /// Create or manage search indexes
441    Index {
442        #[command(subcommand)]
443        action: IndexCommands,
444    },
445
446    /// Index and search images by description
447    #[cfg(feature = "image-io")]
448    Image {
449        #[command(subcommand)]
450        action: ImageCommands,
451    },
452
453    /// Search an index
454    Search {
455        /// Path to the index file
456        index_path: String,
457
458        /// Search query
459        query: String,
460
461        /// Number of results to return
462        #[arg(short = 'k', long, default_value_t = 10)]
463        top_k: usize,
464
465        /// Search mode: hybrid, semantic, keyword
466        #[arg(long, default_value = "hybrid")]
467        mode: String,
468
469        /// Encoder model for semantic search
470        #[arg(short, long, default_value = "minilm-l6-v2")]
471        model: String,
472
473        /// Reranking model (optional)
474        /// Use a cross-encoder model to rerank initial results
475        /// Example: --rerank-model "ms-marco-minilm"
476        #[arg(long, default_value = None)]
477        rerank_model: Option<String>,
478
479        /// Output format: json, jsonl, text
480        #[arg(short, long, default_value = "text")]
481        format: String,
482
483        /// Run on the GPU
484        #[arg(long)]
485        gpu: bool,
486
487        /// Suppress progress output
488        #[arg(short, long)]
489        quiet: bool,
490    },
491
492    /// Compute similarity between two texts
493    Similarity {
494        /// First text (or file path)
495        text1: String,
496
497        /// Second text (or file path)
498        text2: String,
499
500        /// Encoder model
501        #[arg(short, long, default_value = "minilm-l6-v2")]
502        model: String,
503
504        /// Run on the GPU
505        #[arg(long)]
506        gpu: bool,
507
508        /// Suppress progress output
509        #[arg(short, long)]
510        quiet: bool,
511    },
512}
513
514#[derive(Subcommand, Debug, PartialEq)]
515pub enum ModelCommands {
516    /// List all available models
517    List {
518        /// Filter by architecture (e.g., llama, bert, t5)
519        #[arg(short, long)]
520        arch: Option<String>,
521
522        /// Filter by task (e.g., chat, embedding, classification, summarization)
523        #[arg(short, long)]
524        task: Option<String>,
525
526        /// Show only downloaded models
527        #[arg(short, long)]
528        downloaded: bool,
529    },
530
531    /// Download a model
532    Download {
533        name: String,
534
535        #[arg(long)]
536        gguf: bool,
537
538        #[arg(short, long)]
539        quiet: bool,
540    },
541
542    /// Remove a downloaded model
543    Remove { name: String },
544
545    /// Show detailed info about a model
546    Info { name: String },
547
548    /// Search for models by name or description
549    Search { query: String },
550}
551
552#[derive(Subcommand, Debug, PartialEq)]
553pub enum InspectCommands {
554    /// Inspect a model file
555    Model {
556        /// Path to the model file
557        path: String,
558    },
559}
560
561/// Image search, backed by CLIP.
562///
563/// Separate from `index`/`search` because the vectors are not interchangeable:
564/// a CLIP image vector and a MiniLM text vector live in different spaces, and
565/// comparing them would return confident nonsense.
566#[cfg(feature = "image-io")]
567#[derive(Subcommand, Debug, PartialEq)]
568pub enum ImageCommands {
569    /// Embed images into a searchable index
570    Index {
571        /// Files or directories of images
572        #[arg(required = true)]
573        inputs: Vec<String>,
574
575        /// Where to write the index
576        #[arg(short, long, default_value = "images.json")]
577        output: String,
578
579        /// Suppress progress output
580        #[arg(short, long)]
581        quiet: bool,
582    },
583
584    /// Find images matching a description
585    Search {
586        /// Path to the image index
587        index_path: String,
588
589        /// What to look for, in words
590        query: String,
591
592        /// Number of results
593        #[arg(short = 'k', long, default_value_t = 5)]
594        top_k: usize,
595
596        /// Suppress progress output
597        #[arg(short, long)]
598        quiet: bool,
599    },
600}
601
602#[derive(Subcommand, Debug, PartialEq)]
603pub enum IndexCommands {
604    /// Create a new index from documents
605    Create {
606        /// Output index file path
607        output: String,
608
609        /// Input files/directories (uses built-in chunking)
610        #[arg(conflicts_with = "from_chunks")]
611        inputs: Vec<String>,
612
613        /// Pre-chunked JSONL file (bypass chunking)
614        #[arg(long, conflicts_with = "inputs")]
615        from_chunks: Option<String>,
616
617        /// Chunk size in characters
618        ///
619        /// The default fits inside the encoder's window. minilm-l6-v2 reads 256
620        /// tokens, roughly 900 characters, so the previous default of 1000 left
621        /// about nine chunks in ten longer than the model reads, with the tail
622        /// dropped silently. The C# Indexer has always used 512; this matches it.
623        #[arg(long, default_value_t = 512)]
624        chunk_size: usize,
625
626        /// Chunk overlap in characters
627        #[arg(long, default_value_t = 100)]
628        chunk_overlap: usize,
629
630        /// Encoder model for embeddings
631        #[arg(short, long, default_value = "minilm-l6-v2")]
632        model: String,
633
634        /// Run on the GPU
635        #[arg(long)]
636        gpu: bool,
637
638        /// Suppress progress output
639        #[arg(short, long)]
640        quiet: bool,
641    },
642
643    /// Add documents to an existing index
644    Add {
645        /// Index file path
646        index_path: String,
647
648        /// Input files or directories to add
649        inputs: Vec<String>,
650
651        /// Chunk size in characters
652        ///
653        /// Matches `index create`; see the note there.
654        #[arg(long, default_value_t = 512)]
655        chunk_size: usize,
656
657        #[arg(long, default_value_t = 100)]
658        chunk_overlap: usize,
659
660        #[arg(short, long, default_value = "minilm-l6-v2")]
661        model: String,
662
663        /// Run on the GPU
664        #[arg(long)]
665        gpu: bool,
666
667        /// Suppress progress output
668        #[arg(short, long)]
669        quiet: bool,
670    },
671
672    /// Show index info
673    Info {
674        /// Index file path
675        index_path: String,
676    },
677}
678
679/// Convert verbosity count to log level string
680pub fn verbosity_to_log_level(verbose: u8) -> &'static str {
681    match verbose {
682        0 => "warn",
683        1 => "info",
684        2 => "debug",
685        _ => "trace",
686    }
687}
688
689#[cfg(test)]
690mod tests {
691    use super::*;
692    use clap::Parser;
693
694    /// Every model name hardcoded outside the registry must actually resolve.
695    ///
696    /// This exists because three separate lists had drifted from the registry at
697    /// once: both CLI defaults, and four of the seven entries in
698    /// `kjarni::chat::suggest_chat_models`. Each was presumably correct when
699    /// written and went stale when models gained an `-instruct` suffix, with
700    /// nothing to notice. A name that does not resolve is a user-facing failure
701    /// on the very first command someone types, so it should break the build
702    /// instead.
703    #[test]
704    fn hardcoded_model_names_resolve() {
705        use kjarni::ModelType;
706
707        let mut bad = Vec::new();
708
709        for name in [DEFAULT_CHAT_MODEL, DEFAULT_GENERATE_MODEL] {
710            if ModelType::from_cli_name(name).is_none() {
711                bad.push(format!("CLI default '{name}'"));
712            }
713        }
714
715        for name in kjarni::chat::suggested_models() {
716            if ModelType::from_cli_name(name).is_none() {
717                bad.push(format!("chat suggestion '{name}'"));
718            }
719        }
720
721        assert!(
722            bad.is_empty(),
723            "these names are not in the model registry, so anyone who uses them \
724             gets an error:\n  {}\n\
725             Run `kjarni model list` for the valid names.",
726            bad.join("\n  ")
727        );
728    }
729
730    /// The chat default must be usable for chat, not merely a real model.
731    ///
732    /// `kjarni chat` with no arguments should work; defaulting to an embedding
733    /// model would resolve fine here and still fail at runtime.
734    #[test]
735    fn default_chat_model_is_a_chat_model() {
736        assert!(
737            kjarni::chat::is_chat_model(DEFAULT_CHAT_MODEL).is_ok(),
738            "DEFAULT_CHAT_MODEL '{DEFAULT_CHAT_MODEL}' cannot be used for chat"
739        );
740    }
741    fn parse_args(args: &[&str]) -> Result<Cli, clap::Error> {
742        let mut full_args = vec!["kjarni"];
743        full_args.extend(args);
744        Cli::try_parse_from(full_args)
745    }
746
747    #[test]
748    fn test_verbosity_to_log_level_zero() {
749        assert_eq!(verbosity_to_log_level(0), "warn");
750    }
751
752    #[test]
753    fn test_verbosity_to_log_level_one() {
754        assert_eq!(verbosity_to_log_level(1), "info");
755    }
756
757    #[test]
758    fn test_verbosity_to_log_level_two() {
759        assert_eq!(verbosity_to_log_level(2), "debug");
760    }
761
762    #[test]
763    fn test_verbosity_to_log_level_three() {
764        assert_eq!(verbosity_to_log_level(3), "trace");
765    }
766
767    #[test]
768    fn test_verbosity_to_log_level_high() {
769        assert_eq!(verbosity_to_log_level(10), "trace");
770        assert_eq!(verbosity_to_log_level(255), "trace");
771    }
772
773    #[test]
774    fn test_generate_minimal() {
775        let cli = parse_args(&["generate"]).unwrap();
776
777        match cli.command {
778            Commands::Generate {
779                prompt,
780                model,
781                max_tokens,
782                temperature,
783                greedy,
784                gpu,
785                quiet,
786                ..
787            } => {
788                assert!(prompt.is_none());
789                assert_eq!(model, DEFAULT_GENERATE_MODEL);
790                assert_eq!(max_tokens, 100);
791                assert!((temperature - 0.7).abs() < 0.001);
792                assert!(!greedy);
793                assert!(!gpu);
794                assert!(!quiet);
795            }
796            _ => panic!("Expected Generate command"),
797        }
798    }
799
800    #[test]
801    fn test_generate_with_prompt() {
802        let cli = parse_args(&["generate", "Hello world"]).unwrap();
803
804        match cli.command {
805            Commands::Generate { prompt, .. } => {
806                assert_eq!(prompt, Some("Hello world".to_string()));
807            }
808            _ => panic!("Expected Generate command"),
809        }
810    }
811
812    #[test]
813    fn test_generate_with_model() {
814        let cli = parse_args(&["generate", "-m", "phi3.5-mini"]).unwrap();
815
816        match cli.command {
817            Commands::Generate { model, .. } => {
818                assert_eq!(model, "phi3.5-mini");
819            }
820            _ => panic!("Expected Generate command"),
821        }
822    }
823
824    #[test]
825    fn test_generate_with_max_tokens() {
826        let cli = parse_args(&["generate", "-n", "500"]).unwrap();
827
828        match cli.command {
829            Commands::Generate { max_tokens, .. } => {
830                assert_eq!(max_tokens, 500);
831            }
832            _ => panic!("Expected Generate command"),
833        }
834    }
835
836    #[test]
837    fn test_generate_with_temperature() {
838        let cli = parse_args(&["generate", "-t", "1.5"]).unwrap();
839
840        match cli.command {
841            Commands::Generate { temperature, .. } => {
842                assert!((temperature - 1.5).abs() < 0.001);
843            }
844            _ => panic!("Expected Generate command"),
845        }
846    }
847
848    #[test]
849    fn test_generate_with_sampling_params() {
850        let cli = parse_args(&[
851            "generate", "--top-k", "50", "--top-p", "0.9", "--min-p", "0.05",
852        ])
853        .unwrap();
854
855        match cli.command {
856            Commands::Generate {
857                top_k,
858                top_p,
859                min_p,
860                ..
861            } => {
862                assert_eq!(top_k, Some(50));
863                assert_eq!(top_p, Some(0.9));
864                assert_eq!(min_p, Some(0.05));
865            }
866            _ => panic!("Expected Generate command"),
867        }
868    }
869
870    #[test]
871    fn test_generate_with_greedy() {
872        let cli = parse_args(&["generate", "--greedy"]).unwrap();
873
874        match cli.command {
875            Commands::Generate { greedy, .. } => {
876                assert!(greedy);
877            }
878            _ => panic!("Expected Generate command"),
879        }
880    }
881
882    #[test]
883    fn test_generate_with_gpu() {
884        let cli = parse_args(&["generate", "--gpu"]).unwrap();
885
886        match cli.command {
887            Commands::Generate { gpu, .. } => {
888                assert!(gpu);
889            }
890            _ => panic!("Expected Generate command"),
891        }
892    }
893
894    #[test]
895    fn test_generate_with_no_stream() {
896        let cli = parse_args(&["generate", "--no-stream"]).unwrap();
897
898        match cli.command {
899            Commands::Generate { no_stream, .. } => {
900                assert!(no_stream);
901            }
902            _ => panic!("Expected Generate command"),
903        }
904    }
905
906    #[test]
907    fn test_generate_with_quiet() {
908        let cli = parse_args(&["generate", "-q"]).unwrap();
909
910        match cli.command {
911            Commands::Generate { quiet, .. } => {
912                assert!(quiet);
913            }
914            _ => panic!("Expected Generate command"),
915        }
916    }
917
918    #[test]
919    fn test_generate_all_options() {
920        let cli = parse_args(&[
921            "generate",
922            "test prompt",
923            "-m",
924            "llama3.2-3b-instruct",
925            "-n",
926            "256",
927            "-t",
928            "0.8",
929            "--top-k",
930            "40",
931            "--top-p",
932            "0.95",
933            "--repetition-penalty",
934            "1.2",
935            "--greedy",
936            "--gpu",
937            "--no-stream",
938            "-q",
939        ])
940        .unwrap();
941
942        match cli.command {
943            Commands::Generate {
944                prompt,
945                model,
946                max_tokens,
947                temperature,
948                top_k,
949                top_p,
950                repetition_penalty,
951                greedy,
952                gpu,
953                no_stream,
954                quiet,
955                ..
956            } => {
957                assert_eq!(prompt, Some("test prompt".to_string()));
958                assert_eq!(model, "llama3.2-3b-instruct");
959                assert_eq!(max_tokens, 256);
960                assert!((temperature - 0.8).abs() < 0.001);
961                assert_eq!(top_k, Some(40));
962                assert_eq!(top_p, Some(0.95));
963                assert!((repetition_penalty - 1.2).abs() < 0.001);
964                assert!(greedy);
965                assert!(gpu);
966                assert!(no_stream);
967                assert!(quiet);
968            }
969            _ => panic!("Expected Generate command"),
970        }
971    }
972
973    #[test]
974    fn test_chat_defaults() {
975        let cli = parse_args(&["chat"]).unwrap();
976
977        match cli.command {
978            Commands::Chat {
979                model,
980                system,
981                temperature,
982                max_tokens,
983                gpu,
984                quiet,
985                ..
986            } => {
987                assert_eq!(model, DEFAULT_CHAT_MODEL);
988                assert!(system.is_none());
989                assert!((temperature - 0.7).abs() < 0.001);
990                assert_eq!(max_tokens, 512);
991                assert!(!gpu);
992                assert!(!quiet);
993            }
994            _ => panic!("Expected Chat command"),
995        }
996    }
997
998    #[test]
999    fn test_chat_with_system_prompt() {
1000        let cli = parse_args(&["chat", "-s", "You are a helpful assistant"]).unwrap();
1001
1002        match cli.command {
1003            Commands::Chat { system, .. } => {
1004                assert_eq!(system, Some("You are a helpful assistant".to_string()));
1005            }
1006            _ => panic!("Expected Chat command"),
1007        }
1008    }
1009
1010    #[test]
1011    fn test_chat_with_model() {
1012        let cli = parse_args(&["chat", "-m", "phi3.5-mini"]).unwrap();
1013
1014        match cli.command {
1015            Commands::Chat { model, .. } => {
1016                assert_eq!(model, "phi3.5-mini");
1017            }
1018            _ => panic!("Expected Chat command"),
1019        }
1020    }
1021
1022    #[test]
1023    fn test_classify_defaults() {
1024        let cli = parse_args(&["classify"]).unwrap();
1025
1026        match cli.command {
1027            Commands::Classify {
1028                input,
1029                model,
1030                top_k,
1031                format,
1032                multi_label,
1033                gpu,
1034                quiet,
1035                ..
1036            } => {
1037                assert!(input.is_empty());
1038                assert_eq!(model, "distilbert-sentiment");
1039                assert_eq!(top_k, 5);
1040                assert_eq!(format, "text");
1041                assert!(!multi_label);
1042                assert!(!gpu);
1043                assert!(!quiet);
1044            }
1045            _ => panic!("Expected Classify command"),
1046        }
1047    }
1048
1049    #[test]
1050    fn test_classify_with_input() {
1051        let cli = parse_args(&["classify", "This is great!"]).unwrap();
1052
1053        match cli.command {
1054            Commands::Classify { input, .. } => {
1055                assert_eq!(input, vec!["This is great!".to_string()]);
1056            }
1057            _ => panic!("Expected Classify command"),
1058        }
1059    }
1060
1061    #[test]
1062    fn test_classify_with_multiple_inputs() {
1063        let cli = parse_args(&["classify", "text one", "text two", "text three"]).unwrap();
1064
1065        match cli.command {
1066            Commands::Classify { input, .. } => {
1067                assert_eq!(input.len(), 3);
1068                assert_eq!(input[0], "text one");
1069                assert_eq!(input[1], "text two");
1070                assert_eq!(input[2], "text three");
1071            }
1072            _ => panic!("Expected Classify command"),
1073        }
1074    }
1075
1076    #[test]
1077    fn test_classify_with_labels() {
1078        let cli = parse_args(&["classify", "--labels", "bad,good"]).unwrap();
1079
1080        match cli.command {
1081            Commands::Classify { labels, .. } => {
1082                assert_eq!(labels, Some("bad,good".to_string()));
1083            }
1084            _ => panic!("Expected Classify command"),
1085        }
1086    }
1087
1088    #[test]
1089    fn test_classify_with_multi_label() {
1090        let cli = parse_args(&["classify", "--multi-label"]).unwrap();
1091
1092        match cli.command {
1093            Commands::Classify { multi_label, .. } => {
1094                assert!(multi_label);
1095            }
1096            _ => panic!("Expected Classify command"),
1097        }
1098    }
1099
1100    #[test]
1101    fn test_search_minimal() {
1102        let cli = parse_args(&["search", "./index", "my query"]).unwrap();
1103
1104        match cli.command {
1105            Commands::Search {
1106                index_path,
1107                query,
1108                top_k,
1109                mode,
1110                model,
1111                format,
1112                ..
1113            } => {
1114                assert_eq!(index_path, "./index");
1115                assert_eq!(query, "my query");
1116                assert_eq!(top_k, 10);
1117                assert_eq!(mode, "hybrid");
1118                assert_eq!(model, "minilm-l6-v2");
1119                assert_eq!(format, "text");
1120            }
1121            _ => panic!("Expected Search command"),
1122        }
1123    }
1124
1125    #[test]
1126    fn test_search_with_top_k() {
1127        let cli = parse_args(&["search", "./index", "query", "-k", "20"]).unwrap();
1128
1129        match cli.command {
1130            Commands::Search { top_k, .. } => {
1131                assert_eq!(top_k, 20);
1132            }
1133            _ => panic!("Expected Search command"),
1134        }
1135    }
1136
1137    #[test]
1138    fn test_search_with_mode() {
1139        let cli = parse_args(&["search", "./index", "query", "--mode", "semantic"]).unwrap();
1140
1141        match cli.command {
1142            Commands::Search { mode, .. } => {
1143                assert_eq!(mode, "semantic");
1144            }
1145            _ => panic!("Expected Search command"),
1146        }
1147    }
1148
1149    #[test]
1150    fn test_search_with_rerank_model() {
1151        let cli = parse_args(&[
1152            "search",
1153            "./index",
1154            "query",
1155            "--rerank-model",
1156            "ms-marco-minilm",
1157        ])
1158        .unwrap();
1159
1160        match cli.command {
1161            Commands::Search { rerank_model, .. } => {
1162                assert_eq!(rerank_model, Some("ms-marco-minilm".to_string()));
1163            }
1164            _ => panic!("Expected Search command"),
1165        }
1166    }
1167
1168    #[test]
1169    fn test_similarity_minimal() {
1170        let cli = parse_args(&["similarity", "text one", "text two"]).unwrap();
1171
1172        match cli.command {
1173            Commands::Similarity {
1174                text1,
1175                text2,
1176                model,
1177                gpu,
1178                quiet,
1179            } => {
1180                assert_eq!(text1, "text one");
1181                assert_eq!(text2, "text two");
1182                assert_eq!(model, "minilm-l6-v2");
1183                assert!(!gpu);
1184                assert!(!quiet);
1185            }
1186            _ => panic!("Expected Similarity command"),
1187        }
1188    }
1189
1190    #[test]
1191    fn test_model_list_defaults() {
1192        let cli = parse_args(&["model", "list"]).unwrap();
1193
1194        match cli.command {
1195            Commands::Model {
1196                action:
1197                    ModelCommands::List {
1198                        arch,
1199                        task,
1200                        downloaded,
1201                    },
1202            } => {
1203                assert!(arch.is_none());
1204                assert!(task.is_none());
1205                assert!(!downloaded);
1206            }
1207            _ => panic!("Expected Model List command"),
1208        }
1209    }
1210
1211    #[test]
1212    fn test_model_list_with_filters() {
1213        let cli = parse_args(&[
1214            "model",
1215            "list",
1216            "--arch",
1217            "bert",
1218            "--task",
1219            "embedding",
1220            "--downloaded",
1221        ])
1222        .unwrap();
1223
1224        match cli.command {
1225            Commands::Model {
1226                action:
1227                    ModelCommands::List {
1228                        arch,
1229                        task,
1230                        downloaded,
1231                    },
1232            } => {
1233                assert_eq!(arch, Some("bert".to_string()));
1234                assert_eq!(task, Some("embedding".to_string()));
1235                assert!(downloaded);
1236            }
1237            _ => panic!("Expected Model List command"),
1238        }
1239    }
1240
1241    #[test]
1242    fn test_model_download() {
1243        let cli = parse_args(&["model", "download", "minilm-l6-v2"]).unwrap();
1244
1245        match cli.command {
1246            Commands::Model {
1247                action: ModelCommands::Download { name, gguf, quiet },
1248            } => {
1249                assert_eq!(name, "minilm-l6-v2");
1250                assert!(!gguf);
1251                assert!(!quiet);
1252            }
1253            _ => panic!("Expected Model Download command"),
1254        }
1255    }
1256
1257    #[test]
1258    fn test_model_download_gguf() {
1259        let cli = parse_args(&["model", "download", "llama3.2-1b", "--gguf"]).unwrap();
1260
1261        match cli.command {
1262            Commands::Model {
1263                action: ModelCommands::Download { name, gguf, .. },
1264            } => {
1265                assert_eq!(name, "llama3.2-1b");
1266                assert!(gguf);
1267            }
1268            _ => panic!("Expected Model Download command"),
1269        }
1270    }
1271
1272    #[test]
1273    fn test_model_info() {
1274        let cli = parse_args(&["model", "info", "phi3.5-mini"]).unwrap();
1275
1276        match cli.command {
1277            Commands::Model {
1278                action: ModelCommands::Info { name },
1279            } => {
1280                assert_eq!(name, "phi3.5-mini");
1281            }
1282            _ => panic!("Expected Model Info command"),
1283        }
1284    }
1285
1286    #[test]
1287    fn test_model_remove() {
1288        let cli = parse_args(&["model", "remove", "old-model"]).unwrap();
1289
1290        match cli.command {
1291            Commands::Model {
1292                action: ModelCommands::Remove { name },
1293            } => {
1294                assert_eq!(name, "old-model");
1295            }
1296            _ => panic!("Expected Model Remove command"),
1297        }
1298    }
1299
1300    #[test]
1301    fn test_model_search() {
1302        let cli = parse_args(&["model", "search", "llama"]).unwrap();
1303
1304        match cli.command {
1305            Commands::Model {
1306                action: ModelCommands::Search { query },
1307            } => {
1308                assert_eq!(query, "llama");
1309            }
1310            _ => panic!("Expected Model Search command"),
1311        }
1312    }
1313
1314    #[test]
1315    fn test_index_create_minimal() {
1316        let cli = parse_args(&["index", "create", "output.idx"]).unwrap();
1317
1318        match cli.command {
1319            Commands::Index {
1320                action:
1321                    IndexCommands::Create {
1322                        output,
1323                        inputs,
1324                        chunk_size,
1325                        chunk_overlap,
1326                        model,
1327                        ..
1328                    },
1329            } => {
1330                assert_eq!(output, "output.idx");
1331                assert!(inputs.is_empty());
1332                assert_eq!(chunk_size, 512);
1333                assert_eq!(chunk_overlap, 100);
1334                assert_eq!(model, "minilm-l6-v2");
1335            }
1336            _ => panic!("Expected Index Create command"),
1337        }
1338    }
1339
1340    #[test]
1341    fn test_index_create_with_inputs() {
1342        let cli = parse_args(&[
1343            "index",
1344            "create",
1345            "out.idx",
1346            "file1.txt",
1347            "file2.txt",
1348            "dir/",
1349        ])
1350        .unwrap();
1351
1352        match cli.command {
1353            Commands::Index {
1354                action: IndexCommands::Create { inputs, .. },
1355            } => {
1356                assert_eq!(inputs.len(), 3);
1357                assert_eq!(inputs[0], "file1.txt");
1358                assert_eq!(inputs[1], "file2.txt");
1359                assert_eq!(inputs[2], "dir/");
1360            }
1361            _ => panic!("Expected Index Create command"),
1362        }
1363    }
1364
1365    #[test]
1366    fn test_index_create_with_options() {
1367        let cli = parse_args(&[
1368            "index",
1369            "create",
1370            "out.idx",
1371            "--chunk-size",
1372            "500",
1373            "--chunk-overlap",
1374            "100",
1375            "-m",
1376            "nomic-embed-text",
1377            "--gpu",
1378            "-q",
1379        ])
1380        .unwrap();
1381
1382        match cli.command {
1383            Commands::Index {
1384                action:
1385                    IndexCommands::Create {
1386                        chunk_size,
1387                        chunk_overlap,
1388                        model,
1389                        gpu,
1390                        quiet,
1391                        ..
1392                    },
1393            } => {
1394                assert_eq!(chunk_size, 500);
1395                assert_eq!(chunk_overlap, 100);
1396                assert_eq!(model, "nomic-embed-text");
1397                assert!(gpu);
1398                assert!(quiet);
1399            }
1400            _ => panic!("Expected Index Create command"),
1401        }
1402    }
1403
1404    #[test]
1405    fn test_index_add() {
1406        let cli = parse_args(&["index", "add", "existing.idx", "newfile.txt"]).unwrap();
1407
1408        match cli.command {
1409            Commands::Index {
1410                action:
1411                    IndexCommands::Add {
1412                        index_path, inputs, ..
1413                    },
1414            } => {
1415                assert_eq!(index_path, "existing.idx");
1416                assert_eq!(inputs, vec!["newfile.txt".to_string()]);
1417            }
1418            _ => panic!("Expected Index Add command"),
1419        }
1420    }
1421
1422    #[test]
1423    fn test_index_info() {
1424        let cli = parse_args(&["index", "info", "my.idx"]).unwrap();
1425
1426        match cli.command {
1427            Commands::Index {
1428                action: IndexCommands::Info { index_path },
1429            } => {
1430                assert_eq!(index_path, "my.idx");
1431            }
1432            _ => panic!("Expected Index Info command"),
1433        }
1434    }
1435
1436    #[test]
1437    fn test_verbose_zero() {
1438        let cli = parse_args(&["generate"]).unwrap();
1439        assert_eq!(cli.verbose, 0);
1440    }
1441
1442    #[test]
1443    fn test_verbose_one() {
1444        let cli = parse_args(&["-v", "generate"]).unwrap();
1445        assert_eq!(cli.verbose, 1);
1446    }
1447
1448    #[test]
1449    fn test_verbose_two() {
1450        let cli = parse_args(&["-vv", "generate"]).unwrap();
1451        assert_eq!(cli.verbose, 2);
1452    }
1453
1454    #[test]
1455    fn test_verbose_three() {
1456        let cli = parse_args(&["-vvv", "generate"]).unwrap();
1457        assert_eq!(cli.verbose, 3);
1458    }
1459
1460    #[test]
1461    fn test_verbose_long_form() {
1462        let cli = parse_args(&["--verbose", "--verbose", "generate"]).unwrap();
1463        assert_eq!(cli.verbose, 2);
1464    }
1465
1466    #[test]
1467    fn test_verbose_after_command() {
1468        // Global flag can come after command
1469        let cli = parse_args(&["generate", "-v"]).unwrap();
1470        assert_eq!(cli.verbose, 1);
1471    }
1472
1473    #[test]
1474    fn test_missing_command() {
1475        let result = parse_args(&[]);
1476        assert!(result.is_err());
1477    }
1478
1479    #[test]
1480    fn test_unknown_command() {
1481        let result = parse_args(&["unknown"]);
1482        assert!(result.is_err());
1483    }
1484
1485    #[test]
1486    fn test_missing_required_arg() {
1487        // transcribe requires a file
1488        let result = parse_args(&["transcribe"]);
1489        assert!(result.is_err());
1490    }
1491
1492    #[test]
1493    fn test_invalid_number() {
1494        let result = parse_args(&["generate", "-n", "not_a_number"]);
1495        assert!(result.is_err());
1496    }
1497
1498    #[test]
1499    fn test_invalid_float() {
1500        let result = parse_args(&["generate", "-t", "not_a_float"]);
1501        assert!(result.is_err());
1502    }
1503
1504    #[test]
1505    fn test_rerank_minimal() {
1506        let cli = parse_args(&["rerank", "my query"]).unwrap();
1507
1508        match cli.command {
1509            Commands::Rerank {
1510                query,
1511                documents,
1512                model,
1513                top_k,
1514                format,
1515                ..
1516            } => {
1517                assert_eq!(query, "my query");
1518                assert!(documents.is_empty());
1519                assert_eq!(model, "minilm-l6-v2-cross-encoder");
1520                assert!(top_k.is_none());
1521                assert_eq!(format, "text");
1522            }
1523            _ => panic!("Expected Rerank command"),
1524        }
1525    }
1526
1527    #[test]
1528    fn test_rerank_with_documents() {
1529        let cli = parse_args(&["rerank", "query", "doc1", "doc2", "doc3"]).unwrap();
1530
1531        match cli.command {
1532            Commands::Rerank {
1533                query, documents, ..
1534            } => {
1535                assert_eq!(query, "query");
1536                assert_eq!(documents.len(), 3);
1537            }
1538            _ => panic!("Expected Rerank command"),
1539        }
1540    }
1541
1542    #[test]
1543    fn test_rerank_with_top_k() {
1544        let cli = parse_args(&["rerank", "query", "-k", "5"]).unwrap();
1545
1546        match cli.command {
1547            Commands::Rerank { top_k, .. } => {
1548                assert_eq!(top_k, Some(5));
1549            }
1550            _ => panic!("Expected Rerank command"),
1551        }
1552    }
1553
1554    #[test]
1555    fn test_summarize_defaults() {
1556        let cli = parse_args(&["summarize"]).unwrap();
1557
1558        match cli.command {
1559            Commands::Summarize {
1560                input,
1561                model,
1562                min_length,
1563                max_length,
1564                num_beams,
1565                ..
1566            } => {
1567                assert!(input.is_none());
1568                assert_eq!(model, "distilbart-cnn");
1569                assert!(min_length.is_none());
1570                assert!(max_length.is_none());
1571                assert!(num_beams.is_none());
1572            }
1573            _ => panic!("Expected Summarize command"),
1574        }
1575    }
1576
1577    #[test]
1578    fn test_summarize_with_options() {
1579        let cli = parse_args(&[
1580            "summarize",
1581            "--input",
1582            "input.txt",
1583            "--min-length",
1584            "50",
1585            "--max-length",
1586            "200",
1587            "--num-beams",
1588            "4",
1589            "--length-penalty",
1590            "1.5",
1591        ])
1592        .unwrap();
1593
1594        match cli.command {
1595            Commands::Summarize {
1596                input,
1597                min_length,
1598                max_length,
1599                num_beams,
1600                length_penalty,
1601                ..
1602            } => {
1603                assert_eq!(input, Some("input.txt".to_string()));
1604                assert_eq!(min_length, Some(50));
1605                assert_eq!(max_length, Some(200));
1606                assert_eq!(num_beams, Some(4));
1607                assert_eq!(length_penalty, Some(1.5));
1608            }
1609            _ => panic!("Expected Summarize command"),
1610        }
1611    }
1612
1613    #[test]
1614    fn test_translate_defaults() {
1615        let cli = parse_args(&["translate"]).unwrap();
1616
1617        match cli.command {
1618            Commands::Translate {
1619                input,
1620                model,
1621                src,
1622                dst,
1623                ..
1624            } => {
1625                assert!(input.is_none());
1626                assert_eq!(model, "flan-t5-base");
1627                assert!(src.is_none());
1628                assert!(dst.is_none());
1629            }
1630            _ => panic!("Expected Translate command"),
1631        }
1632    }
1633
1634    #[test]
1635    fn test_translate_with_languages() {
1636        let cli = parse_args(&["translate", "--src", "en", "--dst", "is"]).unwrap();
1637
1638        match cli.command {
1639            Commands::Translate { src, dst, .. } => {
1640                assert_eq!(src, Some("en".to_string()));
1641                assert_eq!(dst, Some("is".to_string()));
1642            }
1643            _ => panic!("Expected Translate command"),
1644        }
1645    }
1646    #[test]
1647    fn test_transcribe_minimal() {
1648        let cli = parse_args(&["transcribe", "audio.wav"]).unwrap();
1649
1650        match cli.command {
1651            Commands::Transcribe {
1652                file,
1653                model,
1654                language,
1655                ..
1656            } => {
1657                assert_eq!(file, "audio.wav");
1658                assert_eq!(model, "whisper-small");
1659                assert!(language.is_none());
1660            }
1661            _ => panic!("Expected Transcribe command"),
1662        }
1663    }
1664
1665    #[test]
1666    fn test_classify_with_model() {
1667        let cli = parse_args(&["classify", "i hate mondays", "--model", "toxic-bert"]).unwrap();
1668        match cli.command {
1669            Commands::Classify { input, model, .. } => {
1670                assert_eq!(input, vec!["i hate mondays".to_string()]);
1671                assert_eq!(model, "toxic-bert");
1672            }
1673            _ => panic!("Expected Classify command"),
1674        }
1675    }
1676
1677    #[test]
1678    fn test_transcribe_with_options() {
1679        let cli = parse_args(&[
1680            "transcribe",
1681            "audio.mp3",
1682            "-m",
1683            "whisper-large-v3",
1684            "--language",
1685            "is",
1686        ])
1687        .unwrap();
1688
1689        match cli.command {
1690            Commands::Transcribe {
1691                file,
1692                model,
1693                language,
1694                ..
1695            } => {
1696                assert_eq!(file, "audio.mp3");
1697                assert_eq!(model, "whisper-large-v3");
1698                assert_eq!(language, Some("is".to_string()));
1699            }
1700            _ => panic!("Expected Transcribe command"),
1701        }
1702    }
1703}
1704
1705mod send_sync_tests {
1706    use kjarni::{
1707        Classifier, Embedder, Indexer, Reranker, Searcher, chat::Chat, generator::Generator,
1708    };
1709    // Compile time verificatio
1710    const _: () = {
1711        const fn assert_send<T: Send>() {}
1712        const fn assert_sync<T: Sync>() {}
1713        assert_send::<Embedder>();
1714        assert_sync::<Embedder>();
1715
1716        assert_send::<Indexer>();
1717        assert_sync::<Indexer>();
1718
1719        assert_send::<Searcher>();
1720        assert_sync::<Searcher>();
1721
1722        assert_send::<Reranker>();
1723        assert_sync::<Reranker>();
1724
1725        assert_send::<Generator>();
1726        assert_sync::<Generator>();
1727
1728        assert_send::<Chat>();
1729        assert_sync::<Chat>();
1730
1731        assert_send::<Classifier>();
1732        assert_sync::<Classifier>();
1733    };
1734}