appam 0.1.1

High-throughput, traceable, reliable Rust agent framework for long-horizon AI sessions and easy extensibility
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
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
//! Type definitions for OpenAI Responses API.
//!
//! Complete type definitions matching the OpenAI Responses API contract,
//! including request parameters, response structures, streaming events,
//! and all content/output item types.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Response API request parameters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseCreateParams {
    /// Model identifier
    pub model: String,

    /// Input (can be string or structured input items)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<ResponseInput>,

    /// System/developer instructions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,

    /// Maximum output tokens
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_output_tokens: Option<i32>,

    /// Tool definitions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Tool>>,

    /// Tool choice strategy
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,

    /// Enable parallel tool calls
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parallel_tool_calls: Option<bool>,

    /// Maximum tool calls
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tool_calls: Option<i32>,

    /// Temperature (0.0-2.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// Top-p sampling (0.0-1.0)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// Enable streaming
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,

    /// Stream options
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream_options: Option<StreamOptions>,

    /// Text output configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<ResponseTextConfig>,

    /// Reasoning configuration
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<Reasoning>,

    /// Service tier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub service_tier: Option<String>,

    /// Conversation ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conversation: Option<Conversation>,

    /// Previous response ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_response_id: Option<String>,

    /// Background processing
    #[serde(skip_serializing_if = "Option::is_none")]
    pub background: Option<bool>,

    /// Store for later retrieval
    #[serde(skip_serializing_if = "Option::is_none")]
    pub store: Option<bool>,

    /// Include fields
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include: Option<Vec<String>>,

    /// Truncation strategy
    #[serde(skip_serializing_if = "Option::is_none")]
    pub truncation: Option<String>,

    /// Top logprobs (0-20)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_logprobs: Option<i32>,

    /// Metadata (max 16 key-value pairs)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, String>>,

    /// Prompt cache key
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prompt_cache_key: Option<String>,

    /// Safety identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub safety_identifier: Option<String>,
}

/// Input types for Responses API.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ResponseInput {
    /// Simple string input
    Simple(String),
    /// Structured input items
    Structured(Vec<InputItem>),
}

/// Input item types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum InputItem {
    /// Message input
    Message {
        /// Message role
        role: MessageRole,
        /// Message content
        content: MessageContent,
        /// Optional status
        #[serde(skip_serializing_if = "Option::is_none")]
        status: Option<String>,
        /// Optional ID
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
    },
    /// Function call output (tool result)
    #[serde(rename = "function_call_output")]
    FunctionCallOutput {
        /// Call ID matching function_call
        call_id: String,
        /// Function output
        output: String,
        /// Optional item ID
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// Optional status
        #[serde(skip_serializing_if = "Option::is_none")]
        status: Option<String>,
    },
    /// Function tool call (from previous response)
    #[serde(rename = "function_call")]
    FunctionToolCall {
        /// Item ID
        id: String,
        /// Call ID
        call_id: String,
        /// Function name
        name: String,
        /// Function arguments (JSON string)
        arguments: String,
        /// Optional status
        #[serde(skip_serializing_if = "Option::is_none")]
        status: Option<String>,
    },
    /// Reasoning item
    Reasoning {
        /// Item ID
        id: String,
        /// Reasoning content
        #[serde(default)]
        content: Vec<ReasoningContent>,
        /// Reasoning summary
        #[serde(default)]
        summary: Vec<ReasoningContent>,
        /// Encrypted content (for stateless caching)
        #[serde(skip_serializing_if = "Option::is_none")]
        encrypted_content: Option<String>,
    },
}

/// Message role.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// User message
    User,
    /// Assistant message
    Assistant,
    /// System message
    System,
    /// Developer message
    Developer,
}

/// Message content types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MessageContent {
    /// Simple text
    Text(String),
    /// Content parts (multimodal)
    Parts(Vec<ContentPart>),
}

/// Content part types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentPart {
    /// Text input
    #[serde(rename = "input_text")]
    InputText {
        /// Text content
        text: String,
    },
    /// Image input
    #[serde(rename = "input_image")]
    InputImage {
        /// Image URL or base64 data
        image_url: String,
        /// Detail level
        #[serde(skip_serializing_if = "Option::is_none")]
        detail: Option<String>,
    },
    /// File input
    #[serde(rename = "input_file")]
    InputFile {
        /// File ID
        #[serde(skip_serializing_if = "Option::is_none")]
        file_id: Option<String>,
        /// File URL
        #[serde(skip_serializing_if = "Option::is_none")]
        file_url: Option<String>,
    },
    /// Text output
    #[serde(rename = "output_text")]
    OutputText {
        /// Text content
        text: String,
    },
}

/// Response structure from OpenAI.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
    /// Response ID
    pub id: String,
    /// Creation timestamp (Unix time)
    pub created_at: f64,
    /// Object type (always "response")
    pub object: String,
    /// Model used
    pub model: String,
    /// Response status
    pub status: ResponseStatus,
    /// Output items
    pub output: Vec<OutputItem>,

    /// Instructions (echoed back)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instructions: Option<String>,

    /// Tools (echoed back)
    #[serde(default)]
    pub tools: Vec<Tool>,

    /// Tool choice (echoed back)
    #[serde(default)]
    pub tool_choice: ToolChoice,

    /// Parallel tool calls enabled
    #[serde(default)]
    pub parallel_tool_calls: bool,

    /// Temperature (echoed back)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,

    /// Top-p (echoed back)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,

    /// Usage statistics
    #[serde(skip_serializing_if = "Option::is_none")]
    pub usage: Option<Usage>,

    /// Error information
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<ResponseError>,

    /// Incomplete details
    #[serde(skip_serializing_if = "Option::is_none")]
    pub incomplete_details: Option<IncompleteDetails>,

    /// Conversation reference
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conversation: Option<Conversation>,

    /// Previous response ID
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_response_id: Option<String>,
}

/// Response status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResponseStatus {
    /// Response completed successfully
    Completed,
    /// Response failed
    Failed,
    /// Response in progress
    InProgress,
    /// Response cancelled
    Cancelled,
    /// Response queued
    Queued,
    /// Response incomplete
    Incomplete,
}

/// Output item types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OutputItem {
    /// Assistant message
    Message {
        /// Item ID
        id: String,
        /// Role (always assistant)
        role: String,
        /// Status
        #[serde(skip_serializing_if = "Option::is_none")]
        status: Option<String>,
        /// Content parts
        content: Vec<OutputContent>,
    },
    /// Function call
    #[serde(rename = "function_call")]
    FunctionCall {
        /// Item ID
        id: String,
        /// Call ID
        call_id: String,
        /// Function name
        name: String,
        /// Function arguments (JSON string)
        arguments: String,
        /// Status
        #[serde(skip_serializing_if = "Option::is_none")]
        status: Option<String>,
    },
    /// Reasoning output
    Reasoning {
        /// Item ID
        id: String,
        /// Reasoning content
        #[serde(default)]
        content: Vec<ReasoningContent>,
        /// Reasoning summary
        #[serde(default)]
        summary: Vec<ReasoningContent>,
        /// Encrypted content
        #[serde(skip_serializing_if = "Option::is_none")]
        encrypted_content: Option<String>,
    },
}

/// Output content types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OutputContent {
    /// Text output
    #[serde(rename = "output_text")]
    OutputText {
        /// Text content
        text: String,
        /// Annotations (citations, file references)
        #[serde(default)]
        annotations: Vec<Annotation>,
        /// Logprobs
        #[serde(skip_serializing_if = "Option::is_none")]
        logprobs: Option<Vec<Logprob>>,
    },
    /// Refusal
    #[serde(rename = "output_refusal")]
    OutputRefusal {
        /// Refusal message
        refusal: String,
    },
}

/// Reasoning content types.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ReasoningContent {
    /// Reasoning text
    #[serde(rename = "reasoning_text")]
    ReasoningText {
        /// Reasoning content
        text: String,
    },
    /// Summary text
    #[serde(rename = "summary_text")]
    SummaryText {
        /// Summary content
        text: String,
    },
}

/// Annotation types (citations, file references).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Annotation {
    /// File citation
    #[serde(rename = "file_citation")]
    FileCitation {
        /// File ID
        file_id: String,
        /// Filename
        filename: String,
        /// Citation index
        index: usize,
    },
    /// URL citation
    #[serde(rename = "url_citation")]
    UrlCitation {
        /// URL
        url: String,
        /// Title
        title: String,
        /// Start index in text
        start_index: usize,
        /// End index in text
        end_index: usize,
    },
}

/// Log probability information.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Logprob {
    /// Token
    pub token: String,
    /// Token bytes
    pub bytes: Vec<u8>,
    /// Log probability
    pub logprob: f32,
    /// Top logprobs
    #[serde(default)]
    pub top_logprobs: Vec<TopLogprob>,
}

/// Top log probability.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopLogprob {
    /// Token
    pub token: String,
    /// Token bytes
    pub bytes: Vec<u8>,
    /// Log probability
    pub logprob: f32,
}

/// Tool definition.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Tool {
    /// Function tool
    Function {
        /// Function name
        name: String,
        /// Function description
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// Function parameters (JSON Schema)
        #[serde(skip_serializing_if = "Option::is_none")]
        parameters: Option<serde_json::Value>,
        /// Strict mode
        #[serde(skip_serializing_if = "Option::is_none")]
        strict: Option<bool>,
    },
}

/// Tool choice options.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToolChoice {
    /// String choice ("auto", "none", "required")
    String(String),
    /// Specific tool choice
    Specific(ToolChoiceSpecific),
}

impl Default for ToolChoice {
    fn default() -> Self {
        Self::String("auto".to_string())
    }
}

/// Specific tool choice.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ToolChoiceSpecific {
    /// Force specific function
    Function {
        /// Function to call
        function: FunctionChoice,
    },
}

/// Function choice.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionChoice {
    /// Function name
    pub name: String,
}

/// Stream options.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamOptions {
    /// Include obfuscation
    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_obfuscation: Option<bool>,
}

/// Response text verbosity level.
///
/// Controls the verbosity of the model's response.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum TextVerbosity {
    /// Low verbosity (concise responses)
    Low,
    /// Medium verbosity (balanced responses, default)
    #[default]
    Medium,
    /// High verbosity (detailed responses)
    High,
}

/// Response text format configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseTextFormat {
    /// Plain text
    Text,
    /// JSON object
    #[serde(rename = "json_object")]
    JsonObject,
    /// JSON schema
    #[serde(rename = "json_schema")]
    JsonSchema {
        /// Schema name
        name: String,
        /// Schema description
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// JSON Schema
        schema: serde_json::Value,
        /// Strict mode
        #[serde(skip_serializing_if = "Option::is_none")]
        strict: Option<bool>,
    },
}

/// Response text configuration.
///
/// Configures the format and verbosity of text output.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseTextConfig {
    /// Text format (text, json_object, json_schema)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<ResponseTextFormat>,

    /// Verbosity level (low, medium, high)
    ///
    /// Controls how verbose the model's response will be.
    /// Lower values result in more concise responses, while higher values
    /// result in more detailed responses.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verbosity: Option<TextVerbosity>,
}

/// Reasoning configuration for the Responses API.
///
/// Controls reasoning effort and summary verbosity for o-series and gpt-5 models.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Reasoning {
    /// Reasoning effort level ("low", "medium", "high")
    ///
    /// Controls how many reasoning tokens the model generates.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub effort: Option<String>,

    /// Summary verbosity ("auto", "concise", "detailed")
    ///
    /// Controls the level of detail in the reasoning summary.
    /// This is a string value, not an object.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,
}

/// Conversation reference.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Conversation {
    /// Conversation ID
    Id {
        /// Conversation ID
        id: String,
    },
    /// Simple ID string
    Simple(String),
}

/// Usage statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Usage {
    /// Input tokens
    pub input_tokens: i32,
    /// Input token details
    #[serde(default)]
    pub input_tokens_details: InputTokensDetails,
    /// Output tokens
    pub output_tokens: i32,
    /// Output token details
    #[serde(default)]
    pub output_tokens_details: OutputTokensDetails,
    /// Total tokens
    #[serde(default)]
    pub total_tokens: i32,
}

/// Input token details.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct InputTokensDetails {
    /// Cached tokens
    #[serde(default)]
    pub cached_tokens: i32,
}

/// Output token details.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct OutputTokensDetails {
    /// Reasoning tokens
    #[serde(default)]
    pub reasoning_tokens: i32,
}

/// Response error.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseError {
    /// Error code
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
    /// Error message
    pub message: String,
}

impl ResponseError {
    /// Returns true if this error should trigger a retry.
    ///
    /// Retryable error codes:
    /// - `internal_server_error` - Transient server issues (500)
    /// - `server_error` - General server errors
    /// - `timeout` - Request timeout
    /// - None/missing code with certain message patterns
    ///
    /// # Examples
    ///
    /// ```
    /// # use appam::llm::openai::types::ResponseError;
    /// let error = ResponseError {
    ///     code: Some("internal_server_error".to_string()),
    ///     message: "Internal server error".to_string(),
    /// };
    /// assert!(error.is_retryable());
    ///
    /// let error = ResponseError {
    ///     code: Some("invalid_request".to_string()),
    ///     message: "Invalid request".to_string(),
    /// };
    /// assert!(!error.is_retryable());
    /// ```
    pub fn is_retryable(&self) -> bool {
        if let Some(ref code) = self.code {
            matches!(
                code.as_str(),
                "internal_server_error" | "server_error" | "timeout"
            )
        } else {
            // If no code, check message for retryable patterns
            let msg = self.message.to_ascii_lowercase();
            msg.contains("internal server error")
                || msg.contains("server error")
                || msg.contains("timeout")
                || msg.contains("temporarily unavailable")
        }
    }
}

/// Incomplete details.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IncompleteDetails {
    /// Incomplete reason
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

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

    #[test]
    fn test_response_error_is_retryable_with_code() {
        let error = ResponseError {
            code: Some("internal_server_error".to_string()),
            message: "Internal server error".to_string(),
        };
        assert!(error.is_retryable());

        let error = ResponseError {
            code: Some("server_error".to_string()),
            message: "Server error".to_string(),
        };
        assert!(error.is_retryable());

        let error = ResponseError {
            code: Some("timeout".to_string()),
            message: "Request timeout".to_string(),
        };
        assert!(error.is_retryable());
    }

    #[test]
    fn test_response_error_is_retryable_without_code() {
        let error = ResponseError {
            code: None,
            message: "Internal server error occurred".to_string(),
        };
        assert!(error.is_retryable());

        let error = ResponseError {
            code: None,
            message: "Server error: timeout".to_string(),
        };
        assert!(error.is_retryable());

        let error = ResponseError {
            code: None,
            message: "Service temporarily unavailable".to_string(),
        };
        assert!(error.is_retryable());
    }

    #[test]
    fn test_response_error_not_retryable() {
        let error = ResponseError {
            code: Some("invalid_request".to_string()),
            message: "Invalid request".to_string(),
        };
        assert!(!error.is_retryable());

        let error = ResponseError {
            code: Some("authentication_error".to_string()),
            message: "Invalid API key".to_string(),
        };
        assert!(!error.is_retryable());

        let error = ResponseError {
            code: None,
            message: "Bad request format".to_string(),
        };
        assert!(!error.is_retryable());
    }

    #[test]
    fn test_usage_defaults_when_details_missing() {
        let payload = r#"{
            "input_tokens": 0,
            "output_tokens": 0,
            "total_tokens": 0,
            "output_tokens_details": {"reasoning_tokens": 0}
        }"#;

        let usage: Usage = serde_json::from_str(payload).expect("usage payload should deserialize");
        assert_eq!(usage.input_tokens_details.cached_tokens, 0);
        assert_eq!(usage.output_tokens_details.reasoning_tokens, 0);
    }

    #[test]
    fn test_usage_with_all_fields_populated() {
        let payload = r#"{
            "input_tokens": 1500,
            "output_tokens": 750,
            "input_tokens_details": {
                "cached_tokens": 500
            },
            "output_tokens_details": {
                "reasoning_tokens": 250
            },
            "total_tokens": 2250
        }"#;

        let usage: Usage = serde_json::from_str(payload).expect("usage payload should deserialize");
        assert_eq!(usage.input_tokens, 1500);
        assert_eq!(usage.output_tokens, 750);
        assert_eq!(usage.input_tokens_details.cached_tokens, 500);
        assert_eq!(usage.output_tokens_details.reasoning_tokens, 250);
        assert_eq!(usage.total_tokens, 2250);
    }

    #[test]
    fn test_unified_usage_conversion_from_openai() {
        // Test conversion from OpenAI Usage to UnifiedUsage
        let usage = Usage {
            input_tokens: 1000,
            output_tokens: 500,
            input_tokens_details: InputTokensDetails { cached_tokens: 200 },
            output_tokens_details: OutputTokensDetails {
                reasoning_tokens: 100,
            },
            total_tokens: 1500,
        };

        // Simulate the conversion logic in client.rs
        let input_tokens = usage.input_tokens.max(0) as u32;
        let output_tokens = usage.output_tokens.max(0) as u32;
        let cache_read_tokens = usage.input_tokens_details.cached_tokens.max(0) as u32;
        let reasoning_tokens = usage.output_tokens_details.reasoning_tokens.max(0) as u32;

        let unified = crate::llm::unified::UnifiedUsage {
            input_tokens,
            output_tokens,
            cache_creation_input_tokens: None,
            cache_read_input_tokens: (cache_read_tokens > 0).then_some(cache_read_tokens),
            reasoning_tokens: (reasoning_tokens > 0).then_some(reasoning_tokens),
        };

        assert_eq!(unified.input_tokens, 1000);
        assert_eq!(unified.output_tokens, 500);
        assert_eq!(unified.cache_read_input_tokens, Some(200));
        assert_eq!(unified.reasoning_tokens, Some(100));
        assert_eq!(unified.cache_creation_input_tokens, None);
    }

    #[test]
    fn test_usage_with_zeros() {
        // Test the actual payload structure from the user's example
        let payload = r#"{
            "input_tokens": 0,
            "output_tokens": 0,
            "output_tokens_details": {
                "reasoning_tokens": 0
            },
            "total_tokens": 0
        }"#;

        let usage: Usage = serde_json::from_str(payload).expect("usage payload should deserialize");
        assert_eq!(usage.input_tokens, 0);
        assert_eq!(usage.output_tokens, 0);
        assert_eq!(usage.input_tokens_details.cached_tokens, 0);
        assert_eq!(usage.output_tokens_details.reasoning_tokens, 0);

        // Verify conversion to UnifiedUsage
        let input_tokens = usage.input_tokens.max(0) as u32;
        let output_tokens = usage.output_tokens.max(0) as u32;
        let cache_read_tokens = usage.input_tokens_details.cached_tokens.max(0) as u32;
        let reasoning_tokens = usage.output_tokens_details.reasoning_tokens.max(0) as u32;

        let unified = crate::llm::unified::UnifiedUsage {
            input_tokens,
            output_tokens,
            cache_creation_input_tokens: None,
            cache_read_input_tokens: (cache_read_tokens > 0).then_some(cache_read_tokens),
            reasoning_tokens: (reasoning_tokens > 0).then_some(reasoning_tokens),
        };

        // All fields should be zero or None when there's no usage
        assert_eq!(unified.input_tokens, 0);
        assert_eq!(unified.output_tokens, 0);
        assert_eq!(unified.cache_read_input_tokens, None);
        assert_eq!(unified.reasoning_tokens, None);
    }
}