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