openrouter_api 0.6.0

A Rust client library for the OpenRouter 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Types for OpenRouter Generation API responses.

use serde::{Deserialize, Serialize};

use crate::types::ids::GenerationId;
use crate::types::status::{CancellationStatus, StreamingStatus};

/// Generation data returned by the OpenRouter API.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GenerationData {
    /// Unique identifier for the generation
    pub id: GenerationId,
    /// Upstream API identifier (if available)
    pub upstream_id: Option<String>,
    /// Total cost of the generation in credits
    pub total_cost: f64,
    /// Cache discount applied (if any)
    pub cache_discount: Option<f64>,
    /// Upstream inference cost (if available)
    pub upstream_inference_cost: Option<f64>,
    /// Timestamp when the generation was created
    pub created_at: String,
    /// Model used for the generation
    pub model: String,
    /// Application ID (if applicable)
    pub app_id: Option<i64>,
    /// Whether the generation was streamed
    pub streamed: StreamingStatus,
    /// Whether the generation was cancelled
    pub cancelled: CancellationStatus,
    /// Name of the provider that handled the generation
    pub provider_name: Option<String>,
    /// Latency in milliseconds (if available)
    pub latency: Option<i64>,
    /// Moderation latency in milliseconds (if available)
    pub moderation_latency: Option<i64>,
    /// Generation time in milliseconds (if available)
    pub generation_time: Option<i64>,
    /// Finish reason for the generation
    pub finish_reason: Option<String>,
    /// Native finish reason from the provider
    pub native_finish_reason: Option<String>,
    /// Number of prompt tokens used
    pub tokens_prompt: Option<i64>,
    /// Number of completion tokens used
    pub tokens_completion: Option<i64>,
    /// Number of native prompt tokens used
    pub native_tokens_prompt: Option<i64>,
    /// Number of native completion tokens used
    pub native_tokens_completion: Option<i64>,
    /// Number of native reasoning tokens used
    pub native_tokens_reasoning: Option<i64>,
    /// Number of media items in prompt
    pub num_media_prompt: Option<i64>,
    /// Number of media items in completion
    pub num_media_completion: Option<i64>,
    /// Number of search results used
    pub num_search_results: Option<i64>,
    /// Origin of the generation request
    pub origin: String,
    /// Usage amount (deprecated, use total_cost)
    pub usage: f64,
    /// Whether this is a BYOK (Bring Your Own Key) generation
    pub is_byok: bool,
}

impl GenerationData {
    /// Get total tokens used (prompt + completion).
    pub fn total_tokens(&self) -> Option<i64> {
        match (self.tokens_prompt, self.tokens_completion) {
            (Some(prompt), Some(completion)) => Some(prompt + completion),
            (Some(prompt), None) => Some(prompt),
            (None, Some(completion)) => Some(completion),
            (None, None) => None,
        }
    }

    /// Get total native tokens used (prompt + completion + reasoning).
    pub fn total_native_tokens(&self) -> Option<i64> {
        let prompt = self.native_tokens_prompt.unwrap_or(0);
        let completion = self.native_tokens_completion.unwrap_or(0);
        let reasoning = self.native_tokens_reasoning.unwrap_or(0);

        if prompt == 0 && completion == 0 && reasoning == 0 {
            None
        } else {
            Some(prompt + completion + reasoning)
        }
    }

    /// Check if the generation was successful.
    pub fn is_successful(&self) -> bool {
        !self.cancelled.is_cancelled()
    }

    /// Check if the generation was streamed.
    pub fn was_streamed(&self) -> bool {
        self.streamed.is_active()
    }

    /// Check if the generation was cancelled.
    pub fn was_cancelled(&self) -> bool {
        self.cancelled.is_cancelled()
    }

    /// Get the effective cost (total cost minus cache discount).
    pub fn effective_cost(&self) -> f64 {
        self.total_cost - self.cache_discount.unwrap_or(0.0)
    }

    /// Get cost per token (if token count is available).
    pub fn cost_per_token(&self) -> Option<f64> {
        self.total_tokens()
            .map(|tokens| self.total_cost / tokens as f64)
    }

    /// Get latency in seconds (if available).
    pub fn latency_seconds(&self) -> Option<f64> {
        self.latency.map(|ms| ms as f64 / 1000.0)
    }

    /// Get generation time in seconds (if available).
    pub fn generation_time_seconds(&self) -> Option<f64> {
        self.generation_time.map(|ms| ms as f64 / 1000.0)
    }

    /// Check if this generation used web search.
    pub fn used_web_search(&self) -> bool {
        self.num_search_results.unwrap_or(0) > 0
    }

    /// Check if this generation included media.
    pub fn included_media(&self) -> bool {
        (self.num_media_prompt.unwrap_or(0) + self.num_media_completion.unwrap_or(0)) > 0
    }

    /// Check if this generation used reasoning tokens.
    pub fn used_reasoning(&self) -> bool {
        self.native_tokens_reasoning.unwrap_or(0) > 0
    }
}

/// Response from the generation endpoint.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GenerationResponse {
    /// Generation data
    pub data: GenerationData,
}

impl GenerationResponse {
    /// Get reference to generation data.
    pub fn generation(&self) -> &GenerationData {
        &self.data
    }

    /// Get generation ID.
    pub fn id(&self) -> &str {
        self.data.id.as_str()
    }

    /// Get model used.
    pub fn model(&self) -> &str {
        &self.data.model
    }

    /// Get total cost.
    pub fn total_cost(&self) -> f64 {
        self.data.total_cost
    }

    /// Get effective cost (total cost minus cache discount).
    pub fn effective_cost(&self) -> f64 {
        self.data.effective_cost()
    }

    /// Check if the generation was successful.
    pub fn is_successful(&self) -> bool {
        self.data.is_successful()
    }

    /// Check if the generation was streamed.
    pub fn was_streamed(&self) -> bool {
        self.data.was_streamed()
    }

    /// Get total tokens used.
    pub fn total_tokens(&self) -> Option<i64> {
        self.data.total_tokens()
    }

    /// Get cost per token.
    pub fn cost_per_token(&self) -> Option<f64> {
        self.data.cost_per_token()
    }

    /// Get latency in seconds.
    pub fn latency_seconds(&self) -> Option<f64> {
        self.data.latency_seconds()
    }

    /// Check if web search was used.
    pub fn used_web_search(&self) -> bool {
        self.data.used_web_search()
    }

    /// Check if media was included.
    pub fn included_media(&self) -> bool {
        self.data.included_media()
    }

    /// Check if reasoning was used.
    pub fn used_reasoning(&self) -> bool {
        self.data.used_reasoning()
    }
}

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

    fn create_test_generation_data() -> GenerationData {
        GenerationData {
            id: GenerationId::new("gen-123456"),
            upstream_id: Some("upstream-789".to_string()),
            total_cost: 0.025,
            cache_discount: Some(0.005),
            upstream_inference_cost: Some(0.020),
            created_at: "2024-01-15T10:30:00Z".to_string(),
            model: "openai/gpt-4".to_string(),
            app_id: Some(12345),
            streamed: StreamingStatus::Complete,
            cancelled: CancellationStatus::NotCancelled,
            provider_name: Some("OpenAI".to_string()),
            latency: Some(1500),
            moderation_latency: Some(100),
            generation_time: Some(1200),
            finish_reason: Some("stop".to_string()),
            native_finish_reason: Some("stop".to_string()),
            tokens_prompt: Some(50),
            tokens_completion: Some(100),
            native_tokens_prompt: Some(50),
            native_tokens_completion: Some(100),
            native_tokens_reasoning: Some(25),
            num_media_prompt: Some(2),
            num_media_completion: Some(0),
            num_search_results: Some(5),
            origin: "api".to_string(),
            usage: 0.025,
            is_byok: false,
        }
    }

    #[test]
    fn test_generation_data_total_tokens() {
        let data = create_test_generation_data();
        assert_eq!(data.total_tokens(), Some(150)); // 50 + 100
    }

    #[test]
    fn test_generation_data_total_native_tokens() {
        let data = create_test_generation_data();
        assert_eq!(data.total_native_tokens(), Some(175)); // 50 + 100 + 25
    }

    #[test]
    fn test_generation_data_success_checks() {
        let data = create_test_generation_data();
        assert!(data.is_successful());
        assert!(data.was_streamed());
        assert!(!data.was_cancelled());
    }

    #[test]
    fn test_generation_data_cost_calculations() {
        let data = create_test_generation_data();
        assert_eq!(data.effective_cost(), 0.020); // 0.025 - 0.005
        assert_eq!(data.cost_per_token(), Some(0.025 / 150.0));
    }

    #[test]
    fn test_generation_data_time_conversions() {
        let data = create_test_generation_data();
        assert_eq!(data.latency_seconds(), Some(1.5));
        assert_eq!(data.generation_time_seconds(), Some(1.2));
    }

    #[test]
    fn test_generation_data_feature_checks() {
        let data = create_test_generation_data();
        assert!(data.used_web_search());
        assert!(data.included_media());
        assert!(data.used_reasoning());
    }

    #[test]
    fn test_generation_response_convenience_methods() {
        let data = create_test_generation_data();
        let response = GenerationResponse { data };

        assert_eq!(response.id(), "gen-123456");
        assert_eq!(response.model(), "openai/gpt-4");
        assert_eq!(response.total_cost(), 0.025);
        assert_eq!(response.effective_cost(), 0.020);
        assert!(response.is_successful());
        assert!(response.was_streamed());
        assert_eq!(response.total_tokens(), Some(150));
        assert!(response.used_web_search());
        assert!(response.included_media());
        assert!(response.used_reasoning());
    }

    #[test]
    fn test_generation_data_edge_cases() {
        // Test with minimal data
        let minimal_data = GenerationData {
            id: GenerationId::new("gen-minimal"),
            upstream_id: None,
            total_cost: 0.01,
            cache_discount: None,
            upstream_inference_cost: None,
            created_at: "2024-01-15T10:30:00Z".to_string(),
            model: "openai/gpt-3.5-turbo".to_string(),
            app_id: None,
            streamed: StreamingStatus::default(),
            cancelled: CancellationStatus::default(),
            provider_name: None,
            latency: None,
            moderation_latency: None,
            generation_time: None,
            finish_reason: None,
            native_finish_reason: None,
            tokens_prompt: None,
            tokens_completion: None,
            native_tokens_prompt: None,
            native_tokens_completion: None,
            native_tokens_reasoning: None,
            num_media_prompt: None,
            num_media_completion: None,
            num_search_results: None,
            origin: "api".to_string(),
            usage: 0.01,
            is_byok: false,
        };

        assert_eq!(minimal_data.total_tokens(), None);
        assert_eq!(minimal_data.total_native_tokens(), None);
        assert!(minimal_data.is_successful());
        assert!(!minimal_data.was_streamed());
        assert!(!minimal_data.was_cancelled());
        assert_eq!(minimal_data.effective_cost(), 0.01);
        assert_eq!(minimal_data.cost_per_token(), None);
        assert!(!minimal_data.used_web_search());
        assert!(!minimal_data.included_media());
        assert!(!minimal_data.used_reasoning());
    }

    #[test]
    fn test_generation_serialization() {
        let data = create_test_generation_data();
        let json = serde_json::to_string(&data).unwrap();
        let parsed: GenerationData = serde_json::from_str(&json).unwrap();
        assert_eq!(data, parsed);
    }

    #[test]
    fn test_generation_response_serialization() {
        let data = create_test_generation_data();
        let response = GenerationResponse { data };
        let json = serde_json::to_string(&response).unwrap();
        let parsed: GenerationResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(response, parsed);
    }

    #[test]
    fn test_generation_id_serialization() {
        let generation = GenerationData {
            id: GenerationId::new("gen-12345"),
            upstream_id: Some("upstream-789".to_string()),
            total_cost: 0.025,
            cache_discount: Some(0.005),
            upstream_inference_cost: Some(0.020),
            created_at: "2024-01-15T10:30:00Z".to_string(),
            model: "openai/gpt-4".to_string(),
            app_id: Some(12345),
            streamed: StreamingStatus::Complete,
            cancelled: CancellationStatus::NotCancelled,
            provider_name: Some("OpenAI".to_string()),
            latency: Some(1500),
            moderation_latency: Some(100),
            generation_time: Some(1200),
            finish_reason: Some("stop".to_string()),
            native_finish_reason: Some("stop".to_string()),
            tokens_prompt: Some(50),
            tokens_completion: Some(100),
            native_tokens_prompt: Some(50),
            native_tokens_completion: Some(100),
            native_tokens_reasoning: Some(25),
            num_media_prompt: Some(2),
            num_media_completion: Some(0),
            num_search_results: Some(5),
            origin: "api".to_string(),
            usage: 0.025,
            is_byok: false,
        };

        // Test that GenerationId serializes as a plain string
        let json = serde_json::to_string(&generation).unwrap();
        assert!(json.contains("\"gen-12345\""));

        // Test deserialization roundtrip
        let deserialized: GenerationData = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.id.as_str(), "gen-12345");
    }

    #[test]
    fn test_generation_id_from_string() {
        let id: GenerationId = "string-id".into();
        assert_eq!(id.as_str(), "string-id");

        let id2: GenerationId = String::from("string-id-2").into();
        assert_eq!(id2.as_str(), "string-id-2");
    }

    #[test]
    fn test_generation_id_display() {
        let id = GenerationId::new("test-display");
        assert_eq!(format!("{}", id), "test-display");
    }

    #[test]
    fn test_generation_id_hash() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(GenerationId::new("id-1"));
        set.insert(GenerationId::new("id-2"));
        set.insert(GenerationId::new("id-1")); // Duplicate

        assert_eq!(set.len(), 2); // Should only have 2 unique IDs
    }
}