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