models-dev 0.1.0

Simple Rust client for the models.dev API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# models-dev

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Rust](https://img.shields.io/badge/rust-1.70%2B-blue.svg)](https://rust-lang.org)
[![Crates.io](https://img.shields.io/crates/v/models-dev.svg)](https://crates.io/crates/models-dev)
[![Documentation](https://docs.rs/models-dev/badge.svg)](https://docs.rs/models-dev)

A smart Rust client library for the models.dev API with intelligent caching capabilities.

## ๐Ÿš€ Overview

`models-dev` is a high-performance Rust client for the models.dev API that provides comprehensive information about AI model providers and their available models. The library features intelligent caching with ETag-based conditional requests, ensuring optimal performance for repeated API calls while always serving fresh data when available.

### Key Features

- **Smart Conditional Requests**: Uses ETag-based HTTP conditional requests to minimize bandwidth usage
- **Transparent Caching**: Automatic cache management with the same simple API interface
- **Performance Optimized**: Significant speed improvements for repeated requests (up to 10x faster)
- **Comprehensive Error Handling**: Specific error types for better debugging and error recovery
- **Clean Architecture**: Well-structured codebase with clear separation of concerns
- **Idiomatic Rust**: Leverages Rust's type system and async/await for safe, concurrent operations

## ๐Ÿ“ฆ Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
models-dev = "0.1.0"
```

### Feature Flags

The library supports different TLS backends:

```toml
# Default (native TLS)
models-dev = { version = "0.1.0" }

# Rustls TLS
models-dev = { version = "0.1.0", features = ["rustls-tls"] }

# Native TLS
models-dev = { version = "0.1.0", features = ["native-tls"] }
```

### Minimum Rust Version

This library requires **Rust 1.70+**.

## ๐ŸŽฏ Quick Start

Here's a simple example to get you started:

```rust
use models_dev::ModelsDevClient;

#[tokio::main]
async fn main() -> Result<(), models_dev::ModelsDevError> {
    // Create a client with default settings
    let client = ModelsDevClient::new();
    
    // Fetch providers (first call hits the API)
    let response = client.fetch_providers().await?;
    println!("Found {} providers", response.providers.len());
    
    // Second call uses conditional request (much faster)
    let response2 = client.fetch_providers().await?;
    println!("Still {} providers", response2.providers.len());
    
    Ok(())
}
```

## ๐Ÿ”ง Advanced Usage

### Smart Caching

The library automatically handles caching with conditional HTTP requests. When you call `fetch_providers()`, it:

1. Sends a HEAD request with ETag to check if data has changed
2. If data hasn't changed, returns cached response immediately
3. If data has changed, fetches fresh data and updates cache

```rust
use models_dev::ModelsDevClient;
use std::time::Instant;

#[tokio::main]
async fn main() -> Result<(), models_dev::ModelsDevError> {
    let client = ModelsDevClient::new();
    
    // First call - hits API
    let start = Instant::now();
    let response1 = client.fetch_providers().await?;
    let duration1 = start.elapsed();
    
    // Second call - uses conditional request
    let start = Instant::now();
    let response2 = client.fetch_providers().await?;
    let duration2 = start.elapsed();
    
    println!("First call: {:?}", duration1);
    println!("Second call: {:?}", duration2);
    
    if duration2 < duration1 {
        let speedup = duration1.as_millis() as f64 / duration2.as_millis() as f64;
        println!("Speedup: {:.2}x faster!", speedup);
    }
    
    Ok(())
}
```

### Cache Management

You can manually manage the cache:

```rust
use models_dev::ModelsDevClient;

let client = ModelsDevClient::new();

// Get cache information
let cache_info = client.cache_info();
println!("Cache has metadata: {}", cache_info.has_metadata);

// Clear cache (forces fresh API request)
client.clear_cache()?;

// Fetch fresh data
let response = client.fetch_providers().await?;
```

### Custom Configuration

Create a client with custom settings:

```rust
use models_dev::ModelsDevClient;
use std::time::Duration;

// Custom API base URL
let client = ModelsDevClient::with_base_url("https://custom.api.models.dev");

// The client uses a 30-second timeout by default
println!("Timeout: {:?}", client.timeout());
```

### Error Handling

The library provides comprehensive error handling:

```rust
use models_dev::{ModelsDevClient, ModelsDevError};

#[tokio::main]
async fn main() {
    let client = ModelsDevClient::new();
    
    match client.fetch_providers().await {
        Ok(response) => {
            println!("Success: {} providers", response.providers.len());
        }
        Err(ModelsDevError::HttpError(e)) => {
            eprintln!("HTTP error: {}", e);
        }
        Err(ModelsDevError::JsonError(e)) => {
            eprintln!("JSON parsing error: {}", e);
        }
        Err(ModelsDevError::ApiError(msg)) => {
            eprintln!("API error: {}", msg);
        }
        Err(ModelsDevError::Timeout) => {
            eprintln!("Request timed out");
        }
        Err(ModelsDevError::CacheError(msg)) => {
            eprintln!("Cache error: {}", msg);
        }
        Err(e) => {
            eprintln!("Other error: {}", e);
        }
    }
}
```

## ๐Ÿ“š API Reference

### ModelsDevClient

The main client struct for interacting with the models.dev API.

#### Methods

##### `new() -> Self`
Creates a new client with default settings.

##### `with_base_url(api_base_url: impl Into<String>) -> Self`
Creates a client with a custom API base URL.

##### `fetch_providers() -> Result<ModelsDevResponse, ModelsDevError>`
Fetches provider information with smart caching.

##### `clear_cache() -> Result<(), ModelsDevError>`
Clears the cache metadata.

##### `cache_info() -> CacheInfo`
Returns information about the current cache state.

##### `api_base_url() -> &str`
Returns the API base URL.

##### `timeout() -> Duration`
Returns the request timeout.

### Data Structures

#### ModelsDevResponse
Top-level response containing provider information.

```rust
pub struct ModelsDevResponse {
    pub providers: HashMap<String, Provider>,
}
```

#### Provider
Information about an AI model provider.

```rust
pub struct Provider {
    pub id: String,
    pub name: String,
    pub npm: String,
    pub env: Vec<String>,
    pub doc: String,
    pub api: Option<String>,
    pub models: HashMap<String, Model>,
}
```

#### Model
Information about a specific AI model.

```rust
pub struct Model {
    pub id: String,
    pub name: String,
    pub attachment: bool,
    pub reasoning: bool,
    pub temperature: bool,
    pub tool_call: bool,
    pub knowledge: Option<String>,
    pub release_date: Option<String>,
    pub last_updated: Option<String>,
    pub modalities: Modalities,
    pub open_weights: bool,
    pub cost: Option<ModelCost>,
    pub limit: ModelLimit,
}
```

### Error Types

#### ModelsDevError
Comprehensive error enum for all possible failures:

- `HttpError(reqwest::Error)` - HTTP request failures
- `JsonError(serde_json::Error)` - JSON parsing errors
- `ApiError(String)` - API error responses
- `Timeout` - Network timeout
- `InvalidUrl(String)` - Invalid URL configuration
- `CacheError(String)` - Cache operation failures

## ๐Ÿ“– Examples

The library includes comprehensive examples:

### Basic Usage
```bash
cargo run --example basic_usage
```
Demonstrates basic client usage and caching benefits.

### Smart Caching
```bash
cargo run --example smart_caching_example
```
Shows advanced caching functionality and performance comparisons.

### Integration
```bash
cargo run --example integration_example
```
Demonstrates integration patterns and error handling.

## ๐Ÿงช Testing

Run the test suite:

```bash
# Run all tests
cargo test

# Run unit tests only
cargo test --lib

# Run integration tests
cargo test --test integration

# Run with output
cargo test -- --nocapture
```

### Test Coverage

The library includes:
- 12 unit tests covering core functionality
- 4 doc tests for API examples
- Integration tests for real API scenarios
- All tests are passing with comprehensive coverage

## ๐Ÿค Contributing

We welcome contributions! Togather input, make a new issue to discuss the feature or fix you plan to working on. Please follow these guidelines:

### Development Setup

1. Fork the repository
2. Clone your fork: `git clone https://github.com/your-username/models-dev`
3. Create a feature branch: `git checkout -b feature-name`
4. Install development dependencies: `cargo build`
5. Run tests: `cargo test`

### Code Style Guidelines

- Follow the project's cognitive load reduction philosophy
- Use specific error types with `thiserror`
- Prefer `Result<T, ModelsDevError>` over generic errors
- Use `?` operator for early returns, avoid nested conditionals
- Leverage serde derive macros for API data structures
- Keep modules cohesive, use `pub(crate)` for implementation details
- Use descriptive variable names to reduce cognitive load
- Prefer async/await with tokio for network operations

### Pull Request Process

1. Ensure all tests pass: `cargo test`
2. Format code: `cargo fmt`
3. Run clippy: `cargo clippy -- -D warnings`
4. Update documentation if needed
5. Write clear commit messages following conventional commit format
6. Submit a pull request with a clear description of changes

### Commit Message Format

Follow the conventional commit format:

```
type(scope): description

# Examples
feat(client): add custom timeout configuration
fix(caching): resolve etag comparison issue
docs(readme): update installation instructions
test(examples): add integration test coverage
```

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ“‹ Changelog

### Version 0.1.0 (Current)

**Initial Release**

- โœ… Smart conditional HTTP requests using ETag-based caching
- โœ… Transparent caching with automatic cache management
- โœ… Performance optimization for repeated requests (up to 10x speedup)
- โœ… Comprehensive error handling with specific error types
- โœ… Clean, idiomatic Rust API design
- โœ… Cache management utilities (`clear_cache()`, `cache_info()`)
- โœ… Comprehensive examples and documentation
- โœ… Full test coverage (12 unit tests + 4 doc tests)
- โœ… Support for multiple TLS backends
- โœ… Zero-cost abstractions leveraging Rust's type system

### Key Milestones Achieved

- **Architecture**: Clean separation between client logic, data types, and error handling
- **Performance**: Smart caching reduces API calls and improves response times
- **Reliability**: Comprehensive error handling and testing
- **Usability**: Simple API interface with advanced features available when needed
- **Documentation**: Comprehensive README with examples and API reference

## ๐ŸŽฏ Design Philosophy

**[Cognitive load is what matters.](https://minds.md/zakirullin/cognitive#long)** This library is designed with the following principles:

1. **Simple Interface**: The same `fetch_providers()` method works for both fresh and cached data
2. **Type Safety**: Leverage Rust's type system to prevent entire classes of bugs
3. **Performance**: Smart caching happens automatically, no manual coordination required
4. **Error Clarity**: Specific error types guide developers to solutions
5. **Zero-Cost Abstractions**: Caching and optimizations don't add runtime overhead when not needed

The library reduces cognitive load by:
- Providing a single method for all provider fetching needs
- Automatically handling cache invalidation and updates
- Using descriptive types that prevent common mistakes
- Offering clear error messages that guide debugging

---

**Built with โค๏ธ for the Rust community**

*Rust's type system should reduce cognitive load, not increase it. If you're fighting the compiler, redesign.*