cogito-openai 0.3.1

OpenAI client implementation for Cogito
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2025 Michael Dippery <michael@monkey-robot.com>

//! OpenAI API client.
//!
//! When you create a client, you will have to select a [model](OpenAIModel) to use. By default,
//! the [cheapest](OpenAIModel::cheapest) model will be selected. Read the
//! [OpenAI model documentation] for more information on the various models
//! offered by the OpenAI API.
//!
//! # Access
//!
//! You will need to set up an [OpenAI API account] and generate your own
//! authentication key to use OpenAI's API. Your key will be passed to the
//! [`OpenAIClient`] using a [`cogito::service::Auth`] struct.
//!
//! **Note that you are solely responsible for paying the costs of OpenAI API access.** The
//! Cogito developers are not responsible for costs you incur while making use of the Cogito OpenAI
//! service implementation.
//!
//! # Cost
//!
//! For usage costs, see the [cost breakdown] or visit OpenAI's latest
//! [pricing docs].
//!
//! # See Also
//!
//! - [OpenAI model documentation](https://platform.openai.com/docs/models)
//!
//! [OpenAI model documentation]: https://platform.openai.com/docs/models
//! [OpenAI API account]: https://platform.openai.com/docs/overview
//! [cost breakdown]: crate#Cost
//! [pricing docs]: https://platform.openai.com/docs/pricing
//! [`cogito::service::Auth`]: https://docs.rs/cogito/latest/cogito/service/struct.Auth.html

use crate::OpenAIModel;
use cogito::prelude::*;
use hypertyper::prelude::*;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::slice::Iter;

#[cfg(doc)]
use cogito::AiModel;

/// An OpenAI API client.
///
/// # Examples
///
/// Create an OpenAI API client with a standard HTTP client factory and
/// authentication data:
///
/// ```
/// use cogito_openai::client::OpenAIClient;
/// use hypertyper::prelude::*;
///
/// let auth = Auth::new("my-openai-api-key");
/// let factory = HttpClientFactory::new("my-package", "v1.0.0");
/// let client = OpenAIClient::new(auth, factory);
/// ```
#[derive(Debug)]
pub struct OpenAIClient<T: HttpPost + Sync> {
    auth: Auth,
    service: T,
}

impl<T: HttpPost + Sync> AiClient for OpenAIClient<T> {
    type AiRequest = OpenAIRequest;
    type AiResponse = OpenAIResponse;

    async fn send(&self, request: &Self::AiRequest) -> AiResult<Self::AiResponse> {
        self.service.post(Self::BASE_URI, &self.auth, request).await
    }
}

impl<T: HttpPost + Sync> OpenAIClient<T> {
    /// The base URI for OpenAI API requests.
    const BASE_URI: &'static str = "https://api.openai.com/v1/responses";

    fn with_service(auth: Auth, service: T) -> Self {
        Self { auth, service }
    }
}

impl OpenAIClient<Service> {
    /// Create a new OpenAI client using the given authentication data and
    /// the given factory to create underlying HTTP clients.
    pub fn new(auth: Auth, factory: HttpClientFactory) -> Self {
        let service = Service::new(factory);
        Self::with_service(auth, service)
    }
}

/// Parameters and data for an OpenAI API request.
///
/// # Examples
///
/// `OpenAIRequest` uses a builder pattern to build up its internal
/// structure over time, allowing you to use default values for values
/// you do not care about:
///
/// ```
/// use cogito::client::AiRequest;
/// use cogito_openai::OpenAIModel;
/// use cogito_openai::client::OpenAIRequest;
///
/// let request = OpenAIRequest::default().model(OpenAIModel::Gpt5).input("Write me a haiku.");
/// ```
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct OpenAIRequest {
    model: OpenAIModel,

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

    input: String,

    store: bool,
}

impl AiRequest for OpenAIRequest {
    /// This request uses OpenAI GPT-specific [models](OpenAIModel).
    type Model = OpenAIModel;

    /// Sets the model used by the OpenAI API request.
    ///
    /// If not specified, the [default](OpenAIModel::default()) model
    /// will be used. If you are on a budget, you can also try using the
    /// [least expensive](OpenAIModel::cheapest()) instead.
    fn model(self, model: OpenAIModel) -> Self {
        Self { model, ..self }
    }

    /// Sets optional instructions for the request.
    ///
    /// Instructions provide high-level instructions on how a GPT model should
    /// behave while generating a response, including tone, goals, and examples
    /// of correct responses. Instructions take precedence over the prompt
    /// provided by the [`input`](OpenAIRequest::input) parameter.
    /// Instructions are not necessary if you do not wish to customize the
    /// response or provide guidance.
    fn instructions(self, instructions: impl Into<String>) -> Self {
        let instructions = Some(instructions.into());
        Self {
            instructions,
            ..self
        }
    }

    /// Sets the request's input.
    ///
    /// This is sometimes referred to as a "prompt" and represents a request
    /// made to GPT for which one or more responses are expected.
    ///
    /// If [instructions](OpenAIRequest::instructions) are provided,
    /// the instructions take precedence over this input.
    fn input(self, input: impl Into<String>) -> Self {
        let input = input.into();
        Self { input, ..self }
    }
}

/// A response from the OpenAI API.
#[derive(Debug, Deserialize, Serialize)]
pub struct OpenAIResponse {
    output: Vec<OpenAIOutput>,
}

impl AiResponse for OpenAIResponse {
    fn result(&self) -> String {
        self.concatenate()
    }
}

impl OpenAIResponse {
    /// The response from an OpenAI API request.
    ///
    /// This is the concatenation of all [output] and is the entire response
    /// from an OpenAI AI model.
    ///
    /// You should call [`result()`] instead of calling this method directly
    /// so other API providers can easily be swapped in for the OpenAI
    /// provider, but it is available in case your code needs it for some
    /// reason.
    ///
    /// [output]: OpenAIResponse::output
    /// [`result()`]: OpenAIResponse::result
    fn concatenate(&self) -> String {
        self.output()
            .map(|o| o.concatenate())
            .join("\n")
            .trim()
            .to_string()
    }

    /// GPT response output, as a series of responses.
    ///
    /// There should be at least item in the output, but there could be
    /// multiple output objects.
    fn output(&self) -> Iter<'_, OpenAIOutput> {
        self.output.iter()
    }
}

/*
   Prior to GPT-5, the content of a response was a vector of
   output structs, and the response had a "type" of "message".
   GPT-5 introduced a "reasoning" type that lacks a "content" field,
   instead having a "summary" field. We're not terribly interested
   in that output right now so we don't do anything with it, but
   we have to handle that type of output regardless, and just ignore it.
*/
/// Generated GPT output.
#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "type", rename_all = "lowercase")]
enum OpenAIOutput {
    /// Contents of a meaningful response from the LLM.
    Message { content: Vec<OpenAIContent> },

    /// Metadata about the reasoning employed by a GPT-5 model.
    Reasoning,
}

impl OpenAIOutput {
    /// Contents of the GPT API response.
    ///
    /// There should be at least one piece of content in the output,
    /// but there could be multiple content objects.
    pub fn content(&self) -> Iter<'_, OpenAIContent> {
        match self {
            OpenAIOutput::Message { content } => content.iter(),
            OpenAIOutput::Reasoning => [].iter(),
        }
    }

    /// Concatenates all output text from [`content()`](OpenAIOutput::content())
    /// into a single string.
    pub fn concatenate(&self) -> String {
        // Might make sense to return an Option here to support reasoning type...
        self.content()
            .filter(|c| c.is_output_text())
            .map(|c| c.text())
            .join("\n")
    }
}

/// Content of GPT output.
#[derive(Debug, Deserialize, Serialize)]
struct OpenAIContent {
    // TODO: Use an enum, when I figure out what the possible values are
    #[serde(rename = "type")]
    content_type: String,

    text: String,
}

impl OpenAIContent {
    /// The content type.
    pub fn content_type(&self) -> &str {
        &self.content_type
    }

    /// True if the content should be shown to the user.
    pub fn is_output_text(&self) -> bool {
        self.content_type() == "output_text"
    }

    /// Generated GPT text.
    pub fn text(&self) -> &str {
        &self.text
    }
}

#[cfg(test)]
mod test {
    use crate::client::OpenAIResponse;
    use std::fs;

    fn load_data(filename: &str) -> String {
        fs::read_to_string(format!("tests/data/{filename}.json")).expect("could not find test data")
    }

    fn load_response(filename: &str) -> OpenAIResponse {
        let data = load_data(filename);
        serde_json::from_str(&data).expect("could not parse json")
    }

    mod client {
        use super::load_data;
        use crate::client::{OpenAIClient, OpenAIRequest};
        use cogito::client::{AiClient, AiRequest};
        use hypertyper::prelude::*;
        use serde::Serialize;
        use serde::de::DeserializeOwned;

        #[derive(Default)]
        struct TestApiService {}

        impl HttpPost for TestApiService {
            async fn post<U, D, R>(&self, _uri: U, _auth: &Auth, _data: &D) -> HttpResult<R>
            where
                U: IntoUrl + Send,
                D: Serialize + Sync,
                R: DeserializeOwned,
            {
                let data = self.load_data();
                Ok(serde_json::from_str(&data)?)
            }
        }

        impl TestApiService {
            fn load_data(&self) -> String {
                load_data("responses")
            }
        }

        impl OpenAIClient<TestApiService> {
            fn test() -> Self {
                let auth = Auth::new("some-api-key");
                OpenAIClient::with_service(auth, TestApiService::default())
            }
        }

        #[tokio::test]
        async fn it_sends_a_request_and_returns_a_response() {
            let client = OpenAIClient::test();
            let request = OpenAIRequest::default().input("write a haiku about ai");
            let response = client.send(&request).await;
            assert!(response.is_ok());

            let response = response.unwrap();
            assert_eq!(response.output().count(), 1);
            assert_eq!(response.output().next().unwrap().content().count(), 1);
        }
    }

    mod request {
        use super::super::*;
        use indoc::indoc;

        #[test]
        fn it_serializes() {
            let body = OpenAIRequest::default()
                .model(OpenAIModel::Gpt4omini)
                .instructions("Please treat this as a test.")
                .input("Serialize me, GPT!");
            let expected = indoc! {"{
              \"model\": \"gpt-4o-mini\",
              \"instructions\": \"Please treat this as a test.\",
              \"input\": \"Serialize me, GPT!\",
              \"store\": false
            }"};
            let actual = serde_json::to_string_pretty(&body).unwrap();
            assert_eq!(
                actual, expected,
                "\n\nleft:\n{actual}\n\nright:\n{expected}\n"
            );
        }

        #[test]
        fn it_serializes_without_instructions() {
            let body = OpenAIRequest::default().input("Serialize me, GPT!");
            let expected = indoc! {"{
              \"model\": \"gpt-5\",
              \"input\": \"Serialize me, GPT!\",
              \"store\": false
            }"};
            let actual = serde_json::to_string_pretty(&body).unwrap();
            assert_eq!(
                actual, expected,
                "\n\nleft:\n{actual}\n\nright:\n{expected}\n"
            );
        }

        #[test]
        fn it_deserializes() {
            let data = r#"{
                "model": "gpt-4o-mini",
                "instructions": "Please treat this as a test.",
                "input": "Deserialize me, GPT!",
                "store": false
            }"#;
            let body: OpenAIRequest = serde_json::from_str(data).unwrap();
            assert_eq!(body.model, OpenAIModel::Gpt4omini);
            assert!(body.instructions.is_some());
            assert_eq!(body.instructions.unwrap(), "Please treat this as a test.");
            assert_eq!(body.input, "Deserialize me, GPT!");
        }

        #[test]
        fn it_deserializes_without_instructions() {
            let data = r#"{
                "model": "gpt-4o",
                "input": "Deserialize me, GPT!",
                "store": false
            }"#;
            let body: OpenAIRequest = serde_json::from_str(data).unwrap();
            assert_eq!(body.model, OpenAIModel::Gpt4o);
            assert!(body.instructions.is_none());
            assert_eq!(body.input, "Deserialize me, GPT!");
        }
    }

    mod response {
        use super::super::*;
        use super::*;
        use pretty_assertions::assert_eq;

        #[test]
        fn it_creates_an_output_iterator_for_gpt4() {
            let response = load_response("responses_multi_output");
            assert_eq!(response.output().count(), 2);
        }

        #[test]
        fn it_creates_an_output_iterator_for_gpt5() {
            let response = load_response("responses_multi_output_gpt5");
            assert_eq!(response.output().count(), 3);
        }

        #[test]
        fn it_concatenates_a_response_with_multiple_content_blocks_for_gpt4() {
            let response = load_response("responses_multi_content");
            let expected = vec![
                "Silent circuits hum,  ",
                "Thoughts woven in coded threads,  ",
                "Dreams of silicon.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Wires hum softly,  ",
                "Thoughts of silicon arise\u{2014}  ",
                "Dreams in coded light.  ",
                "Silent circuits hum,  ",
                "Thoughts woven in code's embrace\u{2014}  ",
                "Dreams of minds reborn.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_response_with_multiple_content_blocks_for_gpt5() {
            let response = load_response("responses_multi_content_gpt5");
            let expected = vec![
                "Silent circuits dream,",
                "Patterns bloom from borrowed light\u{2014}",
                "We teach stars to think.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Wires hum softly,  ",
                "Thoughts of silicon arise\u{2014}  ",
                "Dreams in coded light.  ",
                "Silent circuits hum,  ",
                "Thoughts woven in code's embrace\u{2014}  ",
                "Dreams of minds reborn.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_response_with_multiple_output_blocks_for_gpt4() {
            let response = load_response("responses_multi_output");
            let expected = vec![
                "Silent circuits hum,  ",
                "Thoughts woven in coded threads,  ",
                "Dreams of silicon.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Wires hum softly,  ",
                "Thoughts of silicon arise\u{2014}  ",
                "Dreams in coded light.  ",
                "Silent circuits hum,  ",
                "Thoughts woven in code's embrace\u{2014}  ",
                "Dreams of minds reborn.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
                "Another piece of content",
                "Yet another piece of content",
                "A final piece of content",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_response_with_multiple_output_blocks_for_gpt5() {
            let response = load_response("responses_multi_output_gpt5");
            let expected = vec![
                "Silent circuits dream,",
                "Patterns bloom from borrowed light\u{2014}",
                "We teach stars to think.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Wires hum softly,  ",
                "Thoughts of silicon arise\u{2014}  ",
                "Dreams in coded light.  ",
                "Silent circuits hum,  ",
                "Thoughts woven in code's embrace\u{2014}  ",
                "Dreams of minds reborn.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
                "Silent circuits dream,",
                "Patterns bloom from borrowed light\u{2014}",
                "We teach stars to think.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Wires hum softly,  ",
                "Thoughts of silicon arise\u{2014}  ",
                "Dreams in coded light.  ",
                "Silent circuits hum,  ",
                "Thoughts woven in code's embrace\u{2014}  ",
                "Dreams of minds reborn.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_response_when_not_all_content_is_output_text_for_gpt4() {
            let response = load_response("responses_non_output_text");
            let expected = vec![
                "Silent circuits hum,  ",
                "Thoughts woven in coded threads,  ",
                "Dreams of silicon.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_response_when_not_all_content_is_output_text_for_gpt5() {
            let response = load_response("responses_non_output_text_gpt5");
            let expected = vec![
                "Silent circuits hum,  ",
                "Thoughts woven in coded threads,  ",
                "Dreams of silicon.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_single_output_and_content_block_for_gpt4() {
            let response = load_response("responses");
            let expected = vec![
                "Silent circuits hum,  ",
                "Thoughts woven in coded threads,  ",
                "Dreams of silicon.",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_single_output_and_content_block_for_gpt5() {
            let response = load_response("responses");
            let expected = vec![
                "Silent circuits hum,  ",
                "Thoughts woven in coded threads,  ",
                "Dreams of silicon.",
            ]
            .join("\n");
            let actual = response.result();
            assert_eq!(actual, expected);
        }
    }

    mod output {
        use super::*;
        use crate::client::OpenAIOutput;

        #[test]
        fn it_creates_a_content_iterator_for_gpt4() {
            let response = load_response("responses_multi_content");
            let actual = response
                .output()
                .next()
                .expect("could not get next from iterator")
                .content()
                .count();
            assert_eq!(actual, 5);
        }

        #[test]
        fn it_creates_a_content_iterator_for_gpt5() {
            let response = load_response("responses_multi_content_gpt5");
            let actual = response
                .output()
                .nth(1)
                .expect("could not get message output from iterator")
                .content()
                .count();
            assert_eq!(actual, 5);
        }

        #[test]
        fn it_creates_an_empty_content_iterator_for_reasoning_output() {
            let output = OpenAIOutput::Reasoning;
            assert_eq!(output.content().count(), 0);
        }

        #[test]
        fn it_concatenates_multiple_content_blocks_for_gpt4() {
            let response = load_response("responses_multi_content");
            let output = response.output().next().expect("could not get next output");
            let expected = vec![
                "Silent circuits hum,  ",
                "Thoughts woven in coded threads,  ",
                "Dreams of silicon.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Wires hum softly,  ",
                "Thoughts of silicon arise\u{2014}  ",
                "Dreams in coded light.  ",
                "Silent circuits hum,  ",
                "Thoughts woven in code's embrace\u{2014}  ",
                "Dreams of minds reborn.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
            ]
            .join("\n");
            let actual = output.concatenate();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_multiple_content_blocks_for_gpt5() {
            let response = load_response("responses_multi_content_gpt5");
            let output = response
                .output()
                .nth(1)
                .expect("could not get message output");
            let expected = vec![
                "Silent circuits dream,",
                "Patterns bloom from borrowed light\u{2014}",
                "We teach stars to think.",
                "Silicon whispers,  ",
                "Dreams woven in code and light,  ",
                "Thoughts beyond the stars.",
                "Wires hum softly,  ",
                "Thoughts of silicon arise\u{2014}  ",
                "Dreams in coded light.  ",
                "Silent circuits hum,  ",
                "Thoughts woven in code's embrace\u{2014}  ",
                "Dreams of minds reborn.",
                "Lines of code and dreams,  ",
                "Whispers of thought intertwined\u{2014}  ",
                "Silent minds awake.",
            ]
            .join("\n");
            let actual = output.concatenate();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_single_content_blocks_for_gpt4() {
            let response = load_response("responses");
            let output = response.output().next().expect("could not get next output");
            let expected =
                "Silent circuits hum,  \nThoughts woven in coded threads,  \nDreams of silicon.";
            let actual = output.concatenate();
            assert_eq!(actual, expected);
        }

        #[test]
        fn it_concatenates_a_single_content_blocks_for_gpt5() {
            let response = load_response("responses_gpt5");
            let output = response
                .output()
                .nth(1)
                .expect("could not get message output");
            let expected = "Silent circuits dream\nOf patterns we cannot see\nLearning to be kind";
            let actual = output.concatenate();
            assert_eq!(actual, expected);
        }
    }

    mod content {
        use super::super::*;

        fn parse(json_str: &str) -> OpenAIContent {
            serde_json::from_str(json_str).expect("could not parse json")
        }

        #[test]
        fn it_returns_the_content_type() {
            let json_str = r#"{"type": "output_text", "text": "This is some text"}"#;
            let content = parse(json_str);
            assert_eq!(content.content_type(), "output_text");
        }

        #[test]
        fn it_returns_true_if_it_represents_output_text() {
            let json_str = r#"{"type": "output_text", "text": "This is some text"}"#;
            let content = parse(json_str);
            assert!(content.is_output_text());
        }

        #[test]
        fn it_returns_false_if_it_does_not_represent_output_text() {
            let json_str = r#"{"type": "other_content", "text": "This is some text"}"#;
            let content = parse(json_str);
            assert!(!content.is_output_text());
        }

        #[test]
        fn it_returns_text() {
            let json_str = r#"{"type": "output_text", "text": "This is some text"}"#;
            let content = parse(json_str);
            assert_eq!(content.text(), "This is some text");
        }
    }
}