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