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