Skip to content

Commit 9aae3ee

Browse files
committed
update CLAUDE.md
1 parent 394860e commit 9aae3ee

File tree

1 file changed

+125
-90
lines changed

1 file changed

+125
-90
lines changed

CLAUDE.md

Lines changed: 125 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,128 +4,163 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
44

55
## Project Overview
66

7-
`rullm` is a Rust library and CLI for interacting with Large Language Models (LLMs). It consists of two main crates:
7+
`rullm` is a Rust library and CLI for interacting with multiple LLM providers (OpenAI, Anthropic, Google AI, Groq, OpenRouter). The project uses a workspace structure with two main crates:
88

9-
- **rullm-core**: The core library providing a high-performance LLM client with Tower middleware for enterprise features
10-
- **rullm-cli**: A CLI tool built on top of rullm-core for interactive LLM usage
9+
- **rullm-core**: Core library implementing provider integrations, middleware (Tower-based), and streaming support
10+
- **rullm-cli**: Command-line interface built on top of rullm-core
1111

1212
## Architecture
1313

14-
### Core Library (rullm-core)
15-
- **Providers**: Modular provider system supporting OpenAI, Groq, OpenRouter, Anthropic, and Google AI APIs
16-
- OpenAI-compatible providers (OpenAI, Groq, OpenRouter) share implementation via `OpenAICompatibleProvider`
17-
- Easily extensible to support any OpenAI-compatible API by adding a `ProviderIdentity`
18-
- **Middleware**: Built on Tower for retry logic, rate limiting, circuit breakers, and timeouts
19-
- **Dual APIs**: Simple string-based API and advanced API with full control over parameters
20-
- **Streaming**: Real-time token-by-token streaming support via async streams
21-
- **Types**: Comprehensive type system for chat messages, requests, responses, and configuration
14+
### Provider System
2215

23-
### CLI Application (rullm-cli)
24-
- **Commands**: Modular command structure for chat, models, aliases, keys, templates, etc.
25-
- **Configuration**: TOML-based config management with user-defined aliases and templates
26-
- **Interactive Chat**: Full-featured chat mode with history, slash commands, and editor integration
27-
- **Templates**: TOML-based prompt templates with placeholder substitution
16+
All LLM providers implement two core traits defined in `crates/rullm-core/src/types.rs`:
17+
- `LlmProvider`: Base trait with provider metadata (name, aliases, env_key, default_base_url, available_models, health_check)
18+
- `ChatCompletion`: Extends LlmProvider with chat completion methods (blocking and streaming)
2819

29-
## Development Commands
20+
Provider implementations are in `crates/rullm-core/src/providers/`:
21+
- `openai.rs`: OpenAI GPT models
22+
- `anthropic.rs`: Anthropic Claude models
23+
- `google.rs`: Google Gemini models
24+
- `openai_compatible.rs`: Generic provider for OpenAI-compatible APIs
25+
- `groq.rs`: Groq provider (uses `openai_compatible`)
26+
- `openrouter.rs`: OpenRouter provider (uses `openai_compatible`)
27+
28+
The `openai_compatible` provider is a generic implementation that other providers like Groq and OpenRouter extend. It uses a `ProviderIdentity` struct to define provider-specific metadata.
29+
30+
### Middleware Stack
31+
32+
The library uses Tower middleware for enterprise features (see `crates/rullm-core/src/middleware.rs`):
33+
- Retry logic with exponential backoff
34+
- Rate limiting
35+
- Circuit breakers
36+
- Timeouts
37+
- Connection pooling
38+
39+
Configuration is done via `MiddlewareConfig` and `LlmServiceBuilder`.
40+
41+
### Simple API
42+
43+
`crates/rullm-core/src/simple.rs` provides a simplified string-based API (`SimpleLlmClient`, `SimpleLlmBuilder`) that wraps the advanced provider APIs for ease of use.
44+
45+
### CLI Architecture
46+
47+
The CLI entry point is `crates/rullm-cli/src/main.rs`, which:
48+
1. Parses arguments using clap (see `args.rs`)
49+
2. Loads configuration from `~/.config/rullm/` (see `config.rs`)
50+
3. Dispatches to commands in `crates/rullm-cli/src/commands/`
51+
52+
Key CLI modules:
53+
- `client.rs`: Creates provider clients from model strings (format: `provider:model`)
54+
- `provider.rs`: Resolves provider names and aliases
55+
- `config.rs`: Manages CLI configuration (models list, aliases, default model)
56+
- `api_keys.rs`: Manages API key storage in system keychain
57+
- `templates.rs`: TOML-based prompt templates with `{{input}}` placeholders
58+
- `commands/chat/`: Interactive chat mode using reedline for advanced REPL features
59+
60+
### Model Format
61+
62+
Models are specified using the format `provider:model`:
63+
- Example: `openai:gpt-4`, `anthropic:claude-3-opus-20240229`, `groq:llama-3-8b`
64+
- The CLI resolves this via `client::from_model()` which creates the appropriate provider client
65+
66+
## Common Development Tasks
67+
68+
### Building and Running
3069

31-
### Building
3270
```bash
33-
# Build the entire workspace
34-
cargo build
71+
# Build everything
72+
cargo build --all
3573

36-
# Build with release optimizations
74+
# Build release binary
3775
cargo build --release
3876

39-
# Build specific crate
40-
cargo build -p rullm-core
41-
cargo build -p rullm-cli
77+
# Run the CLI (from workspace root)
78+
cargo run -p rullm-cli -- "your query"
79+
80+
# Or after building
81+
./target/debug/rullm "your query"
82+
./target/release/rullm "your query"
4283
```
4384

4485
### Testing
86+
4587
```bash
46-
# Run all tests
47-
cargo test
88+
# Run all tests (note: some require API keys)
89+
cargo test --all
4890

4991
# Run tests for specific crate
5092
cargo test -p rullm-core
5193
cargo test -p rullm-cli
5294

53-
# Run integration tests (may require API keys)
54-
cargo test --test integration
95+
# Run a specific test
96+
cargo test test_name
97+
98+
# Check examples compile
99+
cargo check --examples
100+
```
101+
102+
### Code Quality
103+
104+
```bash
105+
# Format code
106+
cargo fmt
107+
108+
# Check formatting
109+
cargo fmt -- --check
110+
111+
# Run clippy (linter)
112+
cargo clippy --all-targets --all-features -- -D warnings
113+
114+
# Fix clippy suggestions automatically
115+
cargo clippy --fix --all-targets --all-features
55116
```
56117

57118
### Running Examples
119+
58120
```bash
59-
# Core library examples (require API keys)
121+
# Run examples from rullm-core (requires API keys)
60122
cargo run --example openai_simple
61-
cargo run --example groq_simple
62-
cargo run --example openrouter_simple
63-
cargo run --example anthropic_stream
64-
cargo run --example gemini_stream
65-
cargo run --example test_all_providers
66-
67-
# CLI binary
68-
cargo run -- "What is Rust?"
69-
cargo run -- chat --model claude
70-
cargo run -- chat --model groq/llama-3.3-70b-versatile
123+
cargo run --example anthropic_simple
124+
cargo run --example google_simple
125+
cargo run --example openai_stream # Streaming example
126+
cargo run --example test_all_providers # Test all providers at once
71127
```
72128

73-
### Linting and Formatting
74-
```bash
75-
# Check code formatting
76-
cargo fmt --check
129+
### Adding a New Provider
77130

78-
# Format code
79-
cargo fmt
131+
When adding a new provider:
80132

81-
# Run clippy lints
82-
cargo clippy
133+
1. **OpenAI-compatible providers**: Use `OpenAICompatibleProvider` with a `ProviderIdentity` in `providers/openai_compatible.rs`. See `groq.rs` or `openrouter.rs` for examples.
83134

84-
# Run clippy with all targets
85-
cargo clippy --all-targets --all-features
86-
```
135+
2. **Non-compatible providers**: Create a new file in `crates/rullm-core/src/providers/`:
136+
- Implement `LlmProvider` and `ChatCompletion` traits
137+
- Add provider config struct in `crates/rullm-core/src/config.rs`
138+
- Export from `providers/mod.rs` and `lib.rs`
139+
- Add client creation logic in `crates/rullm-cli/src/client.rs`
140+
- Update `crates/rullm-cli/src/provider.rs` for CLI support
87141

88-
## Key Patterns and Conventions
142+
3. Update `DEFAULT_MODELS` in `crates/rullm-core/src/simple.rs` if adding default model mappings
89143

90-
### Provider Implementation
91-
- All providers implement the `ChatProvider` trait with `chat_completion` and `chat_completion_stream` methods
92-
- Configuration structs follow the pattern `{Provider}Config` (e.g., `OpenAIConfig`, `AnthropicConfig`)
93-
- OpenAI-compatible providers use `OpenAICompatibleConfig` with factory methods (`.groq()`, `.openrouter()`)
94-
- Provider structs follow the pattern `{Provider}Provider` (e.g., `OpenAIProvider`, `GroqProvider`, `OpenRouterProvider`)
95-
- OpenAI-compatible providers wrap `OpenAICompatibleProvider` with different `ProviderIdentity` metadata
144+
### Streaming Implementation
96145

97-
### Error Handling
98-
- All public APIs return `Result<T, LlmError>` for comprehensive error handling
99-
- LlmError enum covers authentication, rate limiting, network issues, and provider-specific errors
100-
- Streaming APIs emit `ChatStreamEvent` enum variants: `Token`, `Done`, `Error`
146+
All providers should implement `chat_completion_stream()` returning `StreamResult<ChatStreamEvent>`. The stream emits:
147+
- `ChatStreamEvent::Token(String)`: Each token/chunk
148+
- `ChatStreamEvent::Done`: Completion marker
149+
- `ChatStreamEvent::Error(String)`: Errors during streaming
101150

102-
### Configuration Management
103-
- CLI config stored in `~/.config/rullm/` (or platform equivalent)
104-
- Templates stored as TOML files in `templates/` subdirectory
105-
- Model aliases defined in config.toml for user convenience
151+
See provider implementations for SSE parsing patterns using `utils::sse::sse_lines()`.
106152

107-
### Testing
108-
- Unit tests co-located with implementation files
109-
- Integration tests in `tests/` directories
110-
- Examples serve as both documentation and integration tests
111-
- Test helpers in `utils/test_helpers.rs` for common test patterns
112-
113-
## Important Files
114-
115-
- `crates/rullm-core/src/lib.rs` - Main library entry point and public API
116-
- `crates/rullm-core/src/types.rs` - Core type definitions for requests/responses
117-
- `crates/rullm-core/src/providers/` - LLM provider implementations
118-
- `openai_compatible.rs` - Shared implementation for OpenAI-compatible APIs (OpenAI, Groq, OpenRouter)
119-
- `openai.rs`, `groq.rs`, `openrouter.rs` - Provider wrappers with specific identities
120-
- `anthropic.rs`, `google.rs` - Provider-specific implementations
121-
- `crates/rullm-cli/src/main.rs` - CLI entry point and argument parsing
122-
- `crates/rullm-cli/src/commands/` - CLI command implementations
123-
- `crates/rullm-cli/src/config.rs` - Configuration management
124-
125-
## Development Notes
126-
127-
- The project uses Rust 2024 edition with MSRV 1.85
128-
- Tower middleware provides enterprise-grade reliability features
129-
- Async/await throughout with tokio runtime
130-
- Comprehensive error handling and observability via metrics and logging
131-
- Shell completion support for bash, zsh, and fish
153+
## Configuration Files
154+
155+
- **User config**: `~/.config/rullm/config.toml` (or system equivalent)
156+
- Stores: default model, model aliases, cached models list
157+
- **Templates**: `~/.config/rullm/templates/*.toml`
158+
- **API keys**: Stored in system keychain via `api_keys.rs`
159+
160+
## Important Notes
161+
162+
- The project uses Rust edition 2024 (rust-version 1.85+)
163+
- Model separator changed from `/` to `:` (e.g., `openai:gpt-4` not `openai/gpt-4`)
164+
- Chat history is persisted in `~/.config/rullm/chat_history/`
165+
- The CLI uses `reedline` for advanced REPL features (syntax highlighting, history, multiline editing)
166+
- In chat mode: Alt+Enter for multiline, Ctrl+O for buffer editing, `/edit` to open $EDITOR

0 commit comments

Comments
 (0)