ONE-PAGER DESIGN DOC
Purpose:
A Rust-based CLI tool named q that queries LLM APIs (OpenAI/Gemini) from the command line. It supports:
- Simple queries (
q <prompt>) - Command suggestions (
@cmd) - Contextual queries (
@here,@file,@hist) - Smart Zsh autocompletion
Goals:
- Enable quick, ad-hoc LLM queries in the terminal.
- Provide context-aware prompts (shell history, file contents, directory listings).
- Keep the design modular, secure, and extensible.
- Offer robust error handling (retry, caching, rate-limiting).
- HTTP / API Client
reqwest- Justification: Mature, async-ready, supports streaming (necessary for LLM tokens).
- Caveats: Must enable features like
jsonandstreamfor streaming. - Alternatives:
hyper(lower-level),surf(simpler but fewer features).
- CLI Parsing
clap(version 4+)- Justification: Comprehensive feature set, built-in support for generating Zsh completions.
- Caveats: Can be verbose; prefer using derive macros for simpler usage.
- Alternatives:
argh(simpler, but less robust).
- Async Runtime
tokio- Justification: Standard, widely supported, integrates seamlessly with
reqwest. - Caveats: Use minimal feature sets to keep binary size small.
- Alternatives:
async-stdorsmol(less common for large ecosystem).
- Justification: Standard, widely supported, integrates seamlessly with
- Terminal UI / Color Output
coloredoransi_term- Justification: Straightforward, widely used for color output.
- Caveats: Minimal overhead; be mindful of Windows compatibility.
- Alternatives:
crosstermif more complex TUI is needed.
- Configuration & Key Management
directories+ manual TOML parsing orconfigcrate- Justification: Manages standard OS config paths (
~/.config/q/). - Caveats: For small tool, you might just read a
.tomlfile. - Alternatives:
rust-keyringfor storing secrets if you want OS-level encryption.
- Justification: Manages standard OS config paths (
- Autocompletion
clapcan generate Zsh completion scripts on build/install.- Justification: No separate library needed if using Clap’s built-in completion generator.
- Caching / Retry / Rate-Limiting
reqwest_retryor custom exponential-backoff logic withtokio::timecachedor a simple local file-based approach for caching frequent queries.
src/
├── main.rs
├── cli/
│ └── mod.rs // Clap-based argument parsing & subcommands
├── commands/
│ └── mod.rs // Handles logic for @cmd suggestions
├── context/
│ └── mod.rs // @here, @file, @hist logic (shell/directory/file context)
├── api/
│ ├── mod.rs // Common traits / interfaces for LLM
│ ├── openai.rs // OpenAI-specific logic
│ └── gemini.rs // Gemini-specific logic
├── completion/
│ └── mod.rs // Zsh autocompletion generation
├── config/
│ └── mod.rs // API key loading, saving, parsing config
├── core/
│ └── mod.rs // Core query engine, streaming response handling
└── utils/
└── mod.rs // Shared helpers (logging, text formatting)
cliModule:- Defines command-line structure using
clap. - Maps subcommands like
--set-key,@cmd, etc. to corresponding handlers.
- Defines command-line structure using
commandsModule:- Knows how to interpret
@cmd, queries the LLM for tool suggestions, returns results in color.
- Knows how to interpret
contextModule:- Gathers environment data: shell history (
@hist), directory listings (@here), file content (@file). - Merges this with user prompt before sending to the LLM.
- Gathers environment data: shell history (
apiModule:- Provides unified trait:
LLMApi { fn send_query(&self, prompt: &str) -> Result<...> }. - Implements separate structs for OpenAI, Gemini, etc.
- Provides unified trait:
completionModule:- Generates Zsh completions via
clapAPIs. - Possibly includes dynamic completions (e.g., scanning local directories).
- Generates Zsh completions via
configModule:- Loads/stores user config from
~/.config/q/config.tomlor environment variables. - Provides an interface for setting/getting API keys.
- Loads/stores user config from
coreModule:- Orchestrates queries, handles streaming, handles fallback logic (retry, caching).
utilsModule:- Logging/tracing, formatting, small helpers.
- CLI Scaffolding
- Action: Use
clapderive macros to define basic commands/subcommands. - Verification: Running
q --helpshows correct usage and subcommands.
- Action: Use
- Config & Key Management
- Action: Implement
configmodule to read/write~/.config/q/config.toml; add--set-keyflow. - Verification: Setting an API key works, re-launching the tool picks it up.
- Action: Implement
- Basic Query to LLM
- Action: Implement
api::openaiorapi::geminito send queries; store response. - Verification:
$ q "Hello?"returns a valid LLM response.
- Action: Implement
- Context Injection
- Action: Implement
@hist(zsh history read),@here(directory listing), and optionally@file. - Verification:
$ q @hist "explain this error",$ q @file main.rs "Review code".
- Action: Implement
- Command Suggestions
- Action: Implement
@cmdlogic incommands::mod. Possibly parse the user’s question, look for "tool for X" patterns. - Verification:
$ q @cmd "tool to profile execution time"returns “hyperfine” in green.
- Action: Implement
- Streaming & Resilience
- Action: Use
reqweststreaming, add retry with backoff. Cache frequent queries in local file or memory. - Verification: Large responses stream token by token without blocking; retried on network errors.
- Action: Use
- Zsh Autocompletion
- Action: Implement
completion::modwith Clap’sgeneratefunction. Possibly add dynamic file completions. - Verification: After installation, typing
q <tab>suggests subcommands and/or file paths.
- Action: Implement
- Final Testing & Packaging
- Action: Add integration tests, finalize binary distribution.
- Verification: End-to-end tests passing,
cargo install --path .works,qis functional in a real environment.
URLs (References):
- Clap: https://github.com/clap-rs/clap
- Reqwest: https://github.com/seanmonstar/reqwest
- Tokio: https://github.com/tokio-rs/tokio
- Colored: https://github.com/mackwic/colored
- Config: https://github.com/mehcode/config-rs
- Directories: https://github.com/dirs-dev/directories-rs