koda_cli/lib.rs
1//! # Koda CLI
2//!
3//! User-facing interfaces for the [Koda](https://github.com/lijunzh/koda)
4//! personal AI assistant. The engine lives in [`koda_core`] — this crate
5//! handles presentation.
6//!
7//! ## Entry points
8//!
9//! | Mode | Invocation | Module |
10//! |------|-----------|--------|
11//! | **TUI** (default) | `koda` | [`tui_app`] |
12//! | **Headless** | `koda "prompt"` or `koda -p "..."` | [`headless`] |
13//! | **ACP server** | `koda server --stdio` | [`server`], [`acp_adapter`] |
14//!
15//! ## CLI reference
16//!
17//! ### Flags
18//!
19//! | Flag | Env var | Description |
20//! |------|---------|-------------|
21//! | `-p`, `--prompt <PROMPT>` | | Run a single prompt and exit (headless). Use `"-"` for stdin |
22//! | `<PROMPT>` (positional) | | Same as `-p` — `koda "fix the bug"` works |
23//! | `-a`, `--agent <NAME>` | | Agent to use (matches JSON in `agents/`, default: `default`) |
24//! | `-s`, `--resume <ID>` | | Resume a previous session by ID |
25//! | `--model <NAME>` | `KODA_MODEL` | Model name or alias (e.g. `claude-sonnet`, `gemini-flash`) |
26//! | `--provider <NAME>` | `KODA_PROVIDER` | LLM provider (`anthropic`, `gemini`, `openai`, `ollama`, …) |
27//! | `--base-url <URL>` | `KODA_BASE_URL` | Override the provider's API base URL |
28//! | `--max-tokens <N>` | | Maximum output tokens |
29//! | `--temperature <F>` | | Sampling temperature (0.0–2.0) |
30//! | `--thinking-budget <N>` | | Anthropic extended thinking budget (tokens) |
31//! | `--reasoning-effort <L>` | | OpenAI reasoning effort (`low`, `medium`, `high`) |
32//! | `--output-format <FMT>` | | Headless output format: `text` (default) or `json` |
33//! | `--project-root <DIR>` | | Project root (defaults to cwd) |
34//!
35//! ### Subcommands
36//!
37//! | Command | Description |
38//! |---------|-------------|
39//! | `koda server --stdio` | Start ACP server over stdin/stdout (for editors) |
40//! | `koda server --port <N>` | Start ACP server on TCP port (default: 9999) |
41//!
42//! ## Quick start
43//!
44//! ```bash
45//! # Interactive REPL (auto-detects local models)
46//! koda
47//!
48//! # One-shot with positional prompt
49//! koda "fix the failing test"
50//!
51//! # Explicit model alias
52//! koda -p "explain auth.rs" -m opus
53//!
54//! # Piped input
55//! echo "review this diff" | koda
56//!
57//! # ACP server for editor integration
58//! koda server --stdio
59//! ```
60//!
61//! ## Slash commands
62//!
63//! Type these in the REPL input. Tab-completion is supported.
64//!
65//! | Command | Description |
66//! |---------|-------------|
67//! | `/help` | Show available commands and keybindings |
68//! | `/model <name>` | Switch model — aliases like `opus`, `sonnet`, `flash` |
69//! | `/provider` | Browse all models from a provider |
70//! | `/compact` | Summarize old context to free tokens |
71//! | `/diff` | Show uncommitted changes (review or commit) |
72//! | `/undo` | Revert last turn's file mutations |
73//! | `/sessions` | List, resume, or delete past sessions |
74//! | `/memory` | View/edit project and global memory files |
75//! | `/skills` | List available skills (search with query) |
76//! | `/agent <name>` | Switch to a sub-agent |
77//! | `/key` | Manage API keys |
78//! | `/expand` | Replay last tool output (full, untruncated) |
79//! | `/verbose` | Toggle full tool output |
80//! | `/purge <days>` | Delete archived history older than N days |
81//! | `/exit` | Quit the session |
82//!
83//! ## Keybindings
84//!
85//! ### Input mode
86//!
87//! | Key | Action |
88//! |-----|--------|
89//! | `Enter` | Send message |
90//! | `Alt+Enter` | Insert newline (multi-line input) |
91//! | `Tab` | Autocomplete slash commands and `@file` paths |
92//! | `Shift+Tab` | Toggle approval mode (auto ↔ confirm) |
93//! | `Esc` | Cancel current inference |
94//! | `Ctrl+C` | Cancel current inference |
95//! | `Up/Down` | Scroll through input history |
96//! | Mouse scroll | Scroll conversation history |
97//!
98//! ### Approval prompt (y/n/a/f)
99//!
100//! | Key | Action |
101//! |-----|--------|
102//! | `y` | Approve this action |
103//! | `n` | Reject this action |
104//! | `a` | Approve and switch to auto mode |
105//! | `f` | Reject with typed feedback |
106//! | `Esc` | Reject |
107//!
108//! ## Approval modes
109//!
110//! Koda has two approval modes, toggled with `Shift+Tab`:
111//!
112//! - **Auto** — approve all non-destructive actions automatically.
113//! Destructive commands (`rm`, `sudo`, `git push --force`, etc.) still
114//! require confirmation.
115//! - **Confirm** — every write/mutation requires explicit `y` before
116//! executing. Read-only tools (Read, Grep, Glob) are always auto-approved.
117//!
118//! In headless mode (`koda "prompt"`), destructive actions are **rejected**
119//! outright — there's no human to approve them.
120//!
121//! ## Memory
122//!
123//! Koda reads memory files that persist across sessions:
124//!
125//! - **Project memory** — `MEMORY.md` (or `CLAUDE.md`, `AGENTS.md`) in the
126//! project root. Injected into every system prompt for this project.
127//! - **Global memory** — `~/.config/koda/memory.md`. Injected into every
128//! system prompt across all projects.
129//!
130//! Use `/memory` to view and edit, or the `MemoryWrite` tool to append facts
131//! during a conversation.
132//!
133//! ## Custom agents
134//!
135//! Place JSON files in `.koda/agents/` (project) or `~/.config/koda/agents/`
136//! (global):
137//!
138//! ```json
139//! {
140//! "name": "testgen",
141//! "system_prompt": "You are a test generation specialist.",
142//! "model": "gemini-2.5-flash",
143//! "allowed_tools": ["Read", "Write", "Edit", "Bash", "Grep", "Glob"]
144//! }
145//! ```
146//!
147//! The model dispatches to sub-agents via the `InvokeAgent` tool. Each agent
148//! runs in its own session with its own model, tools, and system prompt.
149//!
150//! ## Skills
151//!
152//! Skills are reusable expertise modules (markdown files with structured
153//! instructions). Built-in skills include code review and security audit.
154//!
155//! - List skills: `/skills` or the `ListSkills` tool
156//! - Activate a skill: `/skills <query>` or the `ActivateSkill` tool
157//! - Create custom skills: place `.md` files in `.koda/skills/` or
158//! `~/.config/koda/skills/`
159//!
160//! ## Providers and model aliases
161//!
162//! Koda supports 14 LLM providers. Model aliases route across providers
163//! without remembering full model IDs:
164//!
165//! | Alias | Provider | Model |
166//! |-------|----------|-------|
167//! | `gemini-flash-lite` | Gemini | gemini-flash-lite-latest |
168//! | `gemini-flash` | Gemini | gemini-flash-latest |
169//! | `gemini-pro` | Gemini | gemini-pro-latest |
170//! | `claude-haiku` | Anthropic | claude-haiku-4-5 |
171//! | `claude-sonnet` | Anthropic | claude-sonnet-4-6 |
172//! | `claude-opus` | Anthropic | claude-opus-4-6 |
173//!
174//! Local providers (LM Studio, Ollama, vLLM) are auto-detected on first run
175//! and require no API key.
176//!
177//! ## Configuration
178//!
179//! Everything lives in `~/.config/koda/`:
180//!
181//! | Path | Content |
182//! |------|---------|
183//! | `db/koda.db` | SQLite — sessions, messages, settings, API keys, history |
184//! | `logs/` | Tracing logs (human-readable) |
185//! | `agents/` | Global custom agent JSON files |
186//! | `skills/` | Global custom skill markdown files |
187//! | `memory.md` | Global memory (injected into all system prompts) |
188//!
189//! ## Privacy and data
190//!
191//! Koda has **zero telemetry**. No usage data, crash reports, or analytics
192//! are collected or transmitted. All data stays local:
193//!
194//! - Conversations are stored in your local SQLite database
195//! - API keys are stored locally in the same database (file mode 0600)
196//! - The only network traffic is your LLM API calls to the provider you choose
197//! - No phone-home, no update checks to third-party servers (version checks
198//! query crates.io only)
199
200pub mod acp_adapter;
201pub mod ansi_parse;
202pub mod completer;
203pub mod diff_render;
204pub mod headless;
205pub mod highlight;
206pub mod history_render;
207pub mod input;
208pub mod md_render;
209pub mod mouse_select;
210pub mod onboarding;
211pub mod repl;
212pub mod scroll_buffer;
213pub mod server;
214pub mod sink;
215pub mod startup;
216pub mod tool_history;
217pub mod tui_app;
218pub mod tui_commands;
219pub mod tui_context;
220pub mod tui_handlers_inference;
221pub mod tui_output;
222pub mod tui_render;
223pub mod tui_types;
224pub mod tui_viewport;
225pub mod tui_wizards;
226pub mod widgets;
227pub mod wrap_input;
228pub mod wrap_util;