openrouter_api 0.5.0

A Rust client library for the OpenRouter API
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# Contributing to OpenRouter API

Thank you for your interest in contributing to the OpenRouter API Rust client library! This document provides guidelines and instructions for contributing.

## Quick Start

1. **Fork and clone** the repository
2. **Install Rust** (latest stable version recommended)
3. **Run quality gates**: `./scripts/pre_quality.sh`
4. **Make your changes** following our guidelines
5. **Submit a pull request**

## Development Workflow

### Prerequisites

- Rust 1.70.0 or later
- `cargo` package manager
- Git

### Setting Up Development Environment

```bash
# Clone your fork
git clone https://github.com/YOUR_USERNAME/openrouter_api.git
cd openrouter_api

# Install dependencies and verify setup
cargo build
cargo test

# Run quality gates
./scripts/pre_quality.sh
```

### Quality Gates

**IMPORTANT**: All changes must pass our quality gates before submission:

```bash
# Run all quality checks
./scripts/pre_quality.sh

# Individual checks
cargo fmt              # Code formatting
cargo clippy           # Linting
cargo test             # Unit tests
cargo audit            # Security audit
cargo doc              # Documentation
```

### Code Standards

#### Code Style
- Follow standard Rust formatting (`cargo fmt`)
- Adhere to Clippy recommendations (`cargo clippy`)
- Write clear, self-documenting code
- Use meaningful variable and function names

#### Documentation
- Document all public APIs with rustdoc comments
- Include examples in documentation where helpful
- Update README.md for significant changes

#### Testing
- Write unit tests for all new functionality
- Maintain 100% test pass rate
- Add integration tests for complex features
- Test edge cases and error conditions

#### Security
- Never expose API keys or sensitive data in logs
- Use `SecureApiKey` for handling API credentials
- Sanitize error messages to prevent data leakage
- Follow secure coding practices

## Types of Contributions

### Bug Fixes
1. **Search existing issues** to avoid duplicates
2. **Create an issue** describing the bug if none exists
3. **Write a test** that reproduces the bug
4. **Fix the bug** and ensure the test passes
5. **Update documentation** if needed

### New Features
1. **Discuss the feature** in an issue first
2. **Design the API** following our patterns
3. **Implement with tests** and documentation
4. **Update examples** if applicable

### Documentation Improvements
- Fix typos, grammar, or unclear explanations
- Add missing documentation
- Improve code examples
- Update outdated information

## Code Architecture

### Key Patterns

#### Type-State Builder Pattern
The client uses compile-time state validation:
```rust
let client = OpenRouterClient::new()
    .with_base_url("https://openrouter.ai/api/v1/")?
    .with_api_key(api_key)?;  // Now in Ready state
```

#### Error Handling
Use the centralized `Error` enum and `Result<T>` type:
```rust
pub fn my_function() -> Result<MyType> {
    // ...
    Ok(result)
}
```

#### Security
Always use security utilities for sensitive data:
```rust
use crate::utils::security::create_safe_error_message;

// Good
let safe_msg = create_safe_error_message(&raw_error, "Context");

// Bad - never expose raw response bodies
return Err(Error::ApiError { message: raw_response, .. });
```

### Module Organization
- `client.rs`: Core client with type-state pattern
- `api/`: Endpoint-specific implementations
- `models/`: Domain models and data structures
- `types/`: Request/response type definitions
- `utils/`: Shared utilities (auth, validation, security)
- `error.rs`: Centralized error handling

## Type Design Guidelines

This library uses a strongly-typed system to prevent bugs and improve code safety. All new types must follow these principles.

### Core Principles

1. **Prefer Newtypes over Primitives**: Wrap strings and numbers to prevent mixing incompatible values
2. **Enum over String**: Use enums for fixed sets of values (roles, types, states)
3. **Transparent Serialization**: Serialize as plain values for API compatibility
4. **Custom Deserialization**: Accept both formats when APIs vary (strings vs numbers)
5. **Comprehensive Traits**: All newtypes should implement standard traits

### Newtype Pattern

Use newtypes for entity identifiers and validated primitives:

```rust
/// Strongly-typed identifier for entities.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct EntityId(String);

impl EntityId {
    pub fn new(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl From<String> for EntityId { /* ... */ }
impl From<&str> for EntityId { /* ... */ }
impl AsRef<str> for EntityId { /* ... */ }
impl fmt::Display for EntityId { /* ... */ }
impl Into<String> for EntityId { /* ... */ }
```

**Required Traits:**
- `Debug`: For logging and debugging
- `Clone`: For passing values around
- `PartialEq`, `Eq`: For comparisons and HashMap keys
- `Hash`: For HashSet and HashMap usage
- `Serialize`, `Deserialize`: For JSON API compatibility
- `Display`: For formatting in logs and errors
- `AsRef<str>` or `AsRef<T>`: For conversion helpers
- `Into<String>` or `Into<T>`: For explicit conversions

**Required Attributes:**
- `#[serde(transparent)]`: Serialize as plain values, not wrapped objects

### Enum over String

Use enums instead of string validation:

```rust
// ✅ GOOD - compile-time validation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ChatRole {
    User,
    Assistant,
    System,
    Tool,
}

impl fmt::Display for ChatRole {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ChatRole::User => write!(f, "user"),
            ChatRole::Assistant => write!(f, "assistant"),
            ChatRole::System => write!(f, "system"),
            ChatRole::Tool => write!(f, "tool"),
        }
    }
}

// ❌ BAD - runtime validation, easy to make typos
pub fn parse_role(role: &str) -> Result<String> {
    match role {
        "user" | "assistant" | "system" | "tool" => Ok(role.to_string()),
        _ => Err("Invalid role".into()),
    }
}
```

**When to use enums:**
- Fixed, finite sets of values (roles, types, states)
- Values used in match statements
- Values that need serialization control (rename_all)

### Validated Newtypes

For values that need validation at creation:

```rust
/// Price with validation for non-negative values.
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(transparent)]
pub struct Price(f64);

impl<'de> Deserialize<'de> for Price {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: serde::Deserializer<'de>
    {
        struct PriceVisitor;

        impl<'de> serde::de::Visitor<'de> for PriceVisitor {
            type Value = Price;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a number or string representing a price")
            }

            fn visit_f64<E>(self, value: f64) -> Result<Self::Value, E> {
                if value.is_finite() {
                    Ok(Price(value))
                } else {
                    Err(serde::de::Error::custom("price must be finite"))
                }
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E> {
                value.parse::<f64>()
                    .map_err(|e| serde::de::Error::custom(e))
                    .and_then(|v| {
                        if v.is_finite() {
                            Ok(Price(v))
                        } else {
                            Err(serde::de::Error::custom("price must be finite"))
                        }
                    })
            }
        }

        deserializer.deserialize_any(PriceVisitor)
    }
}

impl Price {
    pub fn new(value: impl Into<f64>) -> Option<Self> {
        let v = value.into();
        if v.is_finite() {
            Some(Self(v))
        } else {
            None
        }
    }

    pub fn is_valid_business_logic(&self) -> bool {
        self.0 >= 0.0
    }
}
```

**When to use validated newtypes:**
- Numeric values with business logic constraints
- Values that need validation at API boundary
- Values where API may return special indicators (negative, null)

### API Compatibility

The OpenRouter API may return data in multiple formats. Handle both:

```rust
// API returns prices as strings: "0.001", "0", "-1" (special indicator)
// But we want to use as numbers internally
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(transparent)]
pub struct Price(f64);

impl<'de> Deserialize<'de> for Price {
    // Custom deserializer handles both:
    // - Numbers: 0.001
    // - Strings: "0.001", "-1"
    // - Nulls: treat as default value
}
```

### Type Safety Examples

**Prevent mixing different IDs:**
```rust
// ✅ GOOD - compile-time safety
let model_id: ModelId = "openai/gpt-4".into();
let gen_id: GenerationId = "gen-123".into();

// These won't compile:
// let x: ModelId = gen_id;  // ❌ Type mismatch!
// let y: GenerationId = model_id;  // ❌ Type mismatch!
```

**Prevent invalid states:**
```rust
// ✅ GOOD - enum makes invalid states unrepresentable
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum StreamingStatus {
    NotStarted,
    InProgress,
    Complete,
    Cancelled,
}

// ❌ BAD - many invalid combinations possible
pub struct StreamingState {
    is_streaming: bool,
    is_complete: bool,
    is_cancelled: bool,
}
```

### Testing Requirements

All newtypes must have comprehensive tests:

```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_newtype_creation() {
        let id = MyId::new("test-id");
        assert_eq!(id.as_str(), "test-id");
    }

    #[test]
    fn test_newtype_serialization() {
        let id = MyId::new("test-id");
        let json = serde_json::to_value(&id).unwrap();
        assert_eq!(json, "test-id"); // Transparent, not {"id": "test-id"}
    }

    #[test]
    fn test_newtype_roundtrip() {
        let original = MyId::new("test-id");
        let json = serde_json::to_string(&original).unwrap();
        let deserialized: MyId = serde_json::from_str(&json).unwrap();
        assert_eq!(original, deserialized);
    }

    #[test]
    fn test_newtype_traits() {
        let id = MyId::new("test-id");

        // Clone
        let _ = id.clone();

        // PartialEq
        assert_eq!(id, MyId::new("test-id"));

        // Hash
        let mut set = std::collections::HashSet::new();
        set.insert(id);

        // Display
        format!("{}", id);

        // AsRef
        let s: &str = id.as_ref();
        assert_eq!(s, "test-id");
    }
}
```

### Documentation Requirements

All newtypes must document:
1. Purpose and what it prevents
2. API compatibility notes (e.g., "Accepts negative for API compatibility")
3. Validation rules (what's valid vs invalid)
4. When to use `new()` vs `new_unchecked()`
5. Trait implementations provided

```rust
/// Strongly-typed identifier for AI models.
///
/// Prevents accidental mixing of model IDs with other entity IDs.
/// The OpenRouter API uses string IDs, but this wrapper provides compile-time
/// type safety.
///
/// # Examples
///
/// ```rust
/// let model_id = ModelId::new("openai/gpt-4");
/// ```
///
/// # API Compatibility
///
/// Serializes as a plain string for seamless API integration.
/// 
/// # Validation
///
/// - `new()` accepts any string (no validation)
/// - `as_str()` returns the underlying value
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ModelId(String);
```

## Testing Guidelines

### Writing Tests
```rust
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_specific_behavior() {
        // Arrange
        let input = create_test_input();
        
        // Act
        let result = function_under_test(input);
        
        // Assert
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), expected_value);
    }
}
```

### Test Categories
- **Unit tests**: Test individual functions and methods
- **Integration tests**: Test complete workflows
- **Security tests**: Verify data protection and validation
- **Error tests**: Ensure proper error handling

## Submitting Changes

### Pull Request Process

1. **Create a feature branch** from `main`
   ```bash
   git checkout -b feature/your-feature-name
   ```

2. **Make your changes** following our guidelines

3. **Run quality gates** and ensure all tests pass
   ```bash
   ./scripts/pre_quality.sh
   ```

4. **Commit with clear messages**
   ```bash
   git commit -m "Add feature: clear description of what was added"
   ```

5. **Push and create PR**
   ```bash
   git push origin feature/your-feature-name
   ```

### Pull Request Requirements

✅ **Required for all PRs:**
- [ ] All quality gates pass (`./scripts/pre_quality.sh`)
- [ ] Tests written for new functionality
- [ ] Documentation updated for public API changes
- [ ] No breaking changes without discussion
- [ ] Clear, descriptive commit messages

✅ **PR Description should include:**
- What changes were made and why
- Any breaking changes
- Testing approach
- Documentation updates

### Review Process

1. **Automated checks** must pass (CI/CD)
2. **Code review** by maintainers
3. **Security review** for sensitive changes
4. **Documentation review** for public API changes

## Common Pitfalls

### Security Issues
❌ **Don't do this:**
```rust
// Exposing sensitive data
println!("Error: {}", response_body);
return Err(Error::ApiError { message: api_response, .. });
```

✅ **Do this instead:**
```rust
// Safe error handling
let safe_msg = create_safe_error_message(&response_body, "API call failed");
return Err(Error::ApiError { message: safe_msg, .. });
```

### Error Handling
❌ **Don't do this:**
```rust
// Swallowing errors
let result = risky_operation().unwrap_or_default();
```

✅ **Do this instead:**
```rust
// Proper error propagation
let result = risky_operation()
    .map_err(|e| Error::ConfigError(format!("Operation failed: {}", e)))?;
```

### Testing
❌ **Don't do this:**
```rust
// Brittle or incomplete tests
#[test]
fn test_something() {
    assert!(true); // Not testing anything useful
}
```

✅ **Do this instead:**
```rust
// Comprehensive, meaningful tests
#[test]
fn test_api_key_validation_rejects_short_keys() {
    let result = SecureApiKey::new("short");
    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("too short"));
}
```

## Getting Help

- **Documentation**: Check README.md and rustdoc comments
- **Issues**: Search existing issues or create a new one
- **Discussions**: Use GitHub Discussions for questions
- **Security**: Report security issues privately via email

## License

By contributing to this project, you agree that your contributions will be licensed under the same license as the project.