chatdelta 0.8.2

A unified Rust library for connecting to multiple AI APIs with streaming, conversations, and parallel execution
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
//! # ChatDelta AI Client Library
//!
//! A Rust library for connecting to multiple AI APIs (OpenAI, Google Gemini, Anthropic Claude)
//! with a unified interface. Supports parallel execution, retry logic, and configurable parameters.
//!
//! ## Example
//!
//! ```rust,no_run
//! use chatdelta::{AiClient, ClientConfig, create_client};
//! use std::time::Duration;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     let config = ClientConfig::builder()
//!         .timeout(Duration::from_secs(30))
//!         .retries(3)
//!         .temperature(0.7)
//!         .max_tokens(1024)
//!         .build();
//!     
//!     let client = create_client("openai", "your-api-key", "gpt-4o", config)?;
//!     let response = client.send_prompt("Hello, world!").await?;
//!     println!("{}", response);
//!     
//!     Ok(())
//! }
//! ```

use async_trait::async_trait;
use futures::stream::BoxStream;
use reqwest::Client;
use std::time::Duration;
use tokio::sync::mpsc;

pub mod clients;
pub mod error;
pub mod http;
pub mod metrics;
pub mod middleware;
pub mod observability;
pub mod utils;
mod sse;

#[cfg(feature = "orchestration")]
pub mod orchestration;

#[cfg(feature = "prompt-optimization")]
pub mod prompt_optimizer;

#[cfg(any(feature = "mock", test))]
pub mod mock;

pub use clients::*;
pub use error::*;
pub use http::{HttpConfig, get_provider_client, SHARED_CLIENT};
pub use metrics::{ClientMetrics, MetricsSnapshot, RequestTimer};
pub use utils::{execute_with_retry, RetryStrategy};

#[cfg(feature = "orchestration")]
pub use orchestration::{AiOrchestrator, FusedResponse, OrchestrationStrategy, ModelCapabilities};

#[cfg(feature = "prompt-optimization")]
pub use prompt_optimizer::{PromptOptimizer, OptimizedPrompt};

#[cfg(feature = "mock")]
pub use mock::MockClient;

/// Configuration for AI clients
#[derive(Debug, Clone)]
pub struct ClientConfig {
    /// Timeout for HTTP requests
    pub timeout: Duration,
    /// Number of retry attempts for failed requests
    pub retries: u32,
    /// Temperature for AI responses (0.0-2.0)
    pub temperature: Option<f32>,
    /// Maximum tokens for responses
    pub max_tokens: Option<u32>,
    /// Top-p sampling parameter (0.0-1.0)
    pub top_p: Option<f32>,
    /// Frequency penalty (-2.0 to 2.0)
    pub frequency_penalty: Option<f32>,
    /// Presence penalty (-2.0 to 2.0)
    pub presence_penalty: Option<f32>,
    /// System message for conversation context
    pub system_message: Option<String>,
    /// Custom base URL for API endpoint (e.g., for Azure OpenAI, local models, proxies)
    pub base_url: Option<String>,
    /// Retry strategy for failed requests
    pub retry_strategy: RetryStrategy,
}

impl Default for ClientConfig {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(30),
            retries: 0,
            temperature: None,
            max_tokens: Some(1024),
            top_p: None,
            frequency_penalty: None,
            presence_penalty: None,
            system_message: None,
            base_url: None,
            retry_strategy: RetryStrategy::default(),
        }
    }
}

impl ClientConfig {
    /// Create a new ClientConfig builder
    pub fn builder() -> ClientConfigBuilder {
        ClientConfigBuilder::default()
    }
}

/// Builder for ClientConfig
#[derive(Debug, Default)]
pub struct ClientConfigBuilder {
    timeout: Option<Duration>,
    retries: Option<u32>,
    temperature: Option<f32>,
    max_tokens: Option<u32>,
    top_p: Option<f32>,
    frequency_penalty: Option<f32>,
    presence_penalty: Option<f32>,
    system_message: Option<String>,
    base_url: Option<String>,
    retry_strategy: Option<RetryStrategy>,
}

impl ClientConfigBuilder {
    /// Set request timeout
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Set number of retry attempts
    pub fn retries(mut self, retries: u32) -> Self {
        self.retries = Some(retries);
        self
    }

    /// Set temperature (0.0-2.0)
    pub fn temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Set maximum tokens
    pub fn max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Set top-p sampling (0.0-1.0)
    pub fn top_p(mut self, top_p: f32) -> Self {
        self.top_p = Some(top_p);
        self
    }

    /// Set frequency penalty (-2.0 to 2.0)
    pub fn frequency_penalty(mut self, penalty: f32) -> Self {
        self.frequency_penalty = Some(penalty);
        self
    }

    /// Set presence penalty (-2.0 to 2.0)
    pub fn presence_penalty(mut self, penalty: f32) -> Self {
        self.presence_penalty = Some(penalty);
        self
    }

    /// Set system message
    pub fn system_message<S: Into<String>>(mut self, message: S) -> Self {
        self.system_message = Some(message.into());
        self
    }

    /// Set custom base URL for API endpoint
    pub fn base_url<S: Into<String>>(mut self, url: S) -> Self {
        self.base_url = Some(url.into());
        self
    }

    /// Set retry strategy
    pub fn retry_strategy(mut self, strategy: RetryStrategy) -> Self {
        self.retry_strategy = Some(strategy);
        self
    }

    /// Build the ClientConfig
    pub fn build(self) -> ClientConfig {
        ClientConfig {
            timeout: self.timeout.unwrap_or(Duration::from_secs(30)),
            retries: self.retries.unwrap_or(0),
            temperature: self.temperature,
            max_tokens: self.max_tokens.or(Some(1024)),
            top_p: self.top_p,
            frequency_penalty: self.frequency_penalty,
            presence_penalty: self.presence_penalty,
            system_message: self.system_message,
            base_url: self.base_url,
            retry_strategy: self.retry_strategy.unwrap_or_default(),
        }
    }
}

/// Represents a single message in a conversation
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Message {
    /// Role of the message sender ("system", "user", "assistant")
    pub role: String,
    /// Content of the message
    pub content: String,
}

impl Message {
    /// Create a new system message
    pub fn system<S: Into<String>>(content: S) -> Self {
        Self {
            role: "system".to_string(),
            content: content.into(),
        }
    }

    /// Create a new user message
    pub fn user<S: Into<String>>(content: S) -> Self {
        Self {
            role: "user".to_string(),
            content: content.into(),
        }
    }

    /// Create a new assistant message
    pub fn assistant<S: Into<String>>(content: S) -> Self {
        Self {
            role: "assistant".to_string(),
            content: content.into(),
        }
    }
}

/// Represents a conversation with message history
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Conversation {
    /// Messages in the conversation
    pub messages: Vec<Message>,
}

impl Conversation {
    /// Create a new empty conversation
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a conversation with a system message
    pub fn with_system<S: Into<String>>(system_message: S) -> Self {
        Self {
            messages: vec![Message::system(system_message)],
        }
    }

    /// Add a message to the conversation
    pub fn add_message(&mut self, message: Message) {
        self.messages.push(message);
    }

    /// Add a user message to the conversation
    pub fn add_user<S: Into<String>>(&mut self, content: S) {
        self.add_message(Message::user(content));
    }

    /// Add an assistant message to the conversation
    pub fn add_assistant<S: Into<String>>(&mut self, content: S) {
        self.add_message(Message::assistant(content));
    }

    /// Get the last message from the conversation
    pub fn last_message(&self) -> Option<&Message> {
        self.messages.last()
    }

    /// Clear all messages from the conversation
    pub fn clear(&mut self) {
        self.messages.clear();
    }

    /// Get the number of messages in the conversation
    pub fn len(&self) -> usize {
        self.messages.len()
    }

    /// Check if the conversation is empty
    pub fn is_empty(&self) -> bool {
        self.messages.is_empty()
    }
}

/// Response metadata containing additional information from the AI provider
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct ResponseMetadata {
    /// Model version that was actually used (may differ from requested)
    pub model_used: Option<String>,
    /// Number of tokens in the prompt
    pub prompt_tokens: Option<u32>,
    /// Number of tokens in the completion
    pub completion_tokens: Option<u32>,
    /// Total tokens used (prompt + completion)
    pub total_tokens: Option<u32>,
    /// Finish reason (e.g., "stop", "length", "content_filter")
    pub finish_reason: Option<String>,
    /// Safety ratings or content filter results
    pub safety_ratings: Option<serde_json::Value>,
    /// Request ID for debugging
    pub request_id: Option<String>,
    /// Time taken to generate response in milliseconds
    pub latency_ms: Option<u64>,
}

/// AI response with content and metadata
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct AiResponse {
    /// The actual text content of the response
    pub content: String,
    /// Metadata about the response
    pub metadata: ResponseMetadata,
}

impl AiResponse {
    /// Create a new response with just content (no metadata)
    pub fn new(content: String) -> Self {
        Self {
            content,
            metadata: ResponseMetadata::default(),
        }
    }

    /// Create a response with content and metadata
    pub fn with_metadata(content: String, metadata: ResponseMetadata) -> Self {
        Self { content, metadata }
    }
}

/// Streaming response chunk
#[derive(Debug, Clone)]
pub struct StreamChunk {
    /// Content of this chunk
    pub content: String,
    /// Whether this is the final chunk
    pub finished: bool,
    /// Metadata (only populated on final chunk)
    pub metadata: Option<ResponseMetadata>,
}

/// A session for managing multi-turn conversations with an AI client.
/// 
/// Automatically maintains conversation history and handles context management.
/// 
/// # Example
/// 
/// ```no_run
/// # use chatdelta::{ChatSession, create_client, ClientConfig};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = create_client("openai", "key", "gpt-4", ClientConfig::default())?;
/// let mut session = ChatSession::with_system_message(client, "You are a helpful assistant.");
/// 
/// let response1 = session.send("What is Rust?").await?;
/// let response2 = session.send("What are its main features?").await?; // Remembers context
/// # Ok(())
/// # }
/// ```
pub struct ChatSession {
    /// The AI client to use for this session
    client: Box<dyn AiClient>,
    /// The conversation history
    conversation: Conversation,
}

impl ChatSession {
    /// Create a new chat session with the given client
    pub fn new(client: Box<dyn AiClient>) -> Self {
        Self {
            client,
            conversation: Conversation::new(),
        }
    }

    /// Create a new chat session with a system message
    pub fn with_system_message<S: Into<String>>(client: Box<dyn AiClient>, message: S) -> Self {
        Self {
            client,
            conversation: Conversation::with_system(message),
        }
    }

    /// Send a message and get a response
    pub async fn send<S: Into<String>>(&mut self, message: S) -> Result<String, ClientError> {
        let user_msg = message.into();
        self.conversation.add_user(user_msg);
        
        let response = self.client.send_conversation(&self.conversation).await?;
        self.conversation.add_assistant(&response);
        
        Ok(response)
    }

    /// Send a message and get a response with metadata
    pub async fn send_with_metadata<S: Into<String>>(
        &mut self,
        message: S,
    ) -> Result<AiResponse, ClientError> {
        let user_msg = message.into();
        self.conversation.add_user(user_msg);
        
        let response = self
            .client
            .send_conversation_with_metadata(&self.conversation)
            .await?;
        self.conversation.add_assistant(&response.content);
        
        Ok(response)
    }

    /// Stream a response for the given message
    pub async fn stream<S: Into<String>>(
        &mut self,
        message: S,
    ) -> Result<BoxStream<'_, Result<StreamChunk, ClientError>>, ClientError> {
        let user_msg = message.into();
        self.conversation.add_user(user_msg);
        
        self.client.stream_conversation(&self.conversation).await
    }

    /// Add a message to the conversation without sending
    pub fn add_message(&mut self, message: Message) {
        self.conversation.add_message(message);
    }

    /// Get the conversation history
    pub fn history(&self) -> &Conversation {
        &self.conversation
    }

    /// Get a mutable reference to the conversation history
    pub fn history_mut(&mut self) -> &mut Conversation {
        &mut self.conversation
    }

    /// Clear the conversation history
    pub fn clear(&mut self) {
        self.conversation.clear();
    }

    /// Reset the session with a new system message
    pub fn reset_with_system<S: Into<String>>(&mut self, message: S) {
        self.conversation = Conversation::with_system(message);
    }

    /// Replace the conversation history with the provided messages.
    ///
    /// Use this to restore a previously saved session:
    /// ```no_run
    /// # use chatdelta::{ChatSession, Message, create_client, ClientConfig};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let client = create_client("openai", "key", "gpt-4o", ClientConfig::default())?;
    /// # let mut session = ChatSession::new(client);
    /// let saved: Vec<Message> = serde_json::from_str(&std::fs::read_to_string("session.json")?)?;
    /// session.load_history(saved);
    /// # Ok(())
    /// # }
    /// ```
    pub fn load_history(&mut self, messages: Vec<Message>) {
        self.conversation.messages = messages;
    }
}

/// Common trait implemented by all AI clients
#[async_trait]
pub trait AiClient: Send + Sync {
    /// Sends a prompt and returns the textual response
    async fn send_prompt(&self, prompt: &str) -> Result<String, ClientError>;

    /// Sends a prompt and returns response with metadata
    async fn send_prompt_with_metadata(&self, prompt: &str) -> Result<AiResponse, ClientError> {
        // Default implementation for backward compatibility
        let content = self.send_prompt(prompt).await?;
        Ok(AiResponse::new(content))
    }

    /// Sends a conversation and returns the textual response
    async fn send_conversation(&self, conversation: &Conversation) -> Result<String, ClientError> {
        // Default implementation converts conversation to a single prompt
        let prompt = if conversation.messages.is_empty() {
            return Err(ClientError::config("Empty conversation", None));
        } else if conversation.messages.len() == 1 {
            &conversation.messages[0].content
        } else {
            // For clients that don't support conversations, use the last user message
            conversation
                .messages
                .iter()
                .rev()
                .find(|m| m.role == "user")
                .map(|m| m.content.as_str())
                .unwrap_or(&conversation.messages.last().unwrap().content)
        };
        self.send_prompt(prompt).await
    }
    
    /// Sends a prompt and streams the response in chunks
    async fn send_prompt_streaming(
        &self,
        prompt: &str,
        tx: mpsc::UnboundedSender<StreamChunk>,
    ) -> Result<(), ClientError> {
        // Default implementation: send the whole response as one chunk
        let response = self.send_prompt(prompt).await?;
        tx.send(StreamChunk {
            content: response,
            finished: true,
            metadata: None,
        }).map_err(|_| ClientError::Stream(crate::StreamError {
            message: "Failed to send stream chunk".into(),
            error_type: crate::StreamErrorType::Other,
        }))?;
        Ok(())
    }

    /// Sends a conversation and returns response with metadata
    async fn send_conversation_with_metadata(
        &self,
        conversation: &Conversation,
    ) -> Result<AiResponse, ClientError> {
        // Default implementation for backward compatibility
        let content = self.send_conversation(conversation).await?;
        Ok(AiResponse::new(content))
    }

    /// Sends a prompt and returns a stream of response chunks
    async fn stream_prompt(
        &self,
        _prompt: &str,
    ) -> Result<BoxStream<'_, Result<StreamChunk, ClientError>>, ClientError> {
        // Default implementation falls back to non-streaming
        let response = self.send_prompt(_prompt).await?;
        let chunk = StreamChunk {
            content: response,
            finished: true,
            metadata: None,
        };
        Ok(Box::pin(futures::stream::once(async { Ok(chunk) })))
    }

    /// Sends a conversation and returns a stream of response chunks
    async fn stream_conversation(
        &self,
        conversation: &Conversation,
    ) -> Result<BoxStream<'_, Result<StreamChunk, ClientError>>, ClientError> {
        // Default implementation falls back to non-streaming conversation
        let response = self.send_conversation(conversation).await?;
        let chunk = StreamChunk {
            content: response,
            finished: true,
            metadata: None,
        };
        Ok(Box::pin(futures::stream::once(async { Ok(chunk) })))
    }

    /// Returns whether this client supports streaming
    fn supports_streaming(&self) -> bool {
        false
    }

    /// Returns whether this client supports conversation history
    fn supports_conversations(&self) -> bool {
        false
    }

    /// Returns the name/identifier of this AI client
    fn name(&self) -> &str;

    /// Returns the model being used by this client
    fn model(&self) -> &str;
}

/// Factory function to create AI clients
///
/// # Arguments
///
/// * `provider` - The AI provider: "openai", "google"/"gemini", or "anthropic"/"claude"
/// * `api_key` - The API key for the provider
/// * `model` - The model name (e.g., "gpt-4", "claude-3-sonnet-20240229", "gemini-1.5-pro")
/// * `config` - Configuration for timeouts, retries, and generation parameters
///
/// # Example
///
/// ```rust,no_run
/// use chatdelta::{create_client, ClientConfig};
/// use std::time::Duration;
///
/// let config = ClientConfig::default();
/// let client = create_client("openai", "your-api-key", "gpt-4", config)?;
/// # Ok::<(), chatdelta::ClientError>(())
/// ```
pub fn create_client(
    provider: &str,
    api_key: &str,
    model: &str,
    config: ClientConfig,
) -> Result<Box<dyn AiClient>, ClientError> {
    let http_client = Client::builder()
        .timeout(config.timeout)
        .build()
        .map_err(|e| ClientError::config(format!("Failed to create HTTP client: {e}"), None))?;

    match provider.to_lowercase().as_str() {
        "openai" | "gpt" | "chatgpt" => Ok(Box::new(ChatGpt::new(
            http_client,
            api_key.to_string(),
            model.to_string(),
            config,
        ))),
        "google" | "gemini" => Ok(Box::new(Gemini::new(
            http_client,
            api_key.to_string(),
            model.to_string(),
            config,
        ))),
        "anthropic" | "claude" => Ok(Box::new(Claude::new(
            http_client,
            api_key.to_string(),
            model.to_string(),
            config,
        ))),
        _ => Err(ClientError::config(
            format!("Unknown provider: {provider}. Supported providers: openai, google, anthropic"),
            Some("provider".to_string()),
        )),
    }
}

/// Execute multiple AI clients in parallel and return all results
///
/// This function runs all provided clients concurrently and returns the results
/// in the order they complete, not necessarily the order they were provided.
///
/// # Arguments
///
/// * `clients` - Vector of AI clients to execute
/// * `prompt` - The prompt to send to all clients
///
/// # Returns
///
/// A vector of tuples containing the client name and either the response or an error
///
/// # Example
///
/// ```rust,no_run
/// use chatdelta::{create_client, execute_parallel, ClientConfig};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = ClientConfig::default();
/// let clients = vec![
///     create_client("openai", "key1", "gpt-4", config.clone())?,
///     create_client("anthropic", "key2", "claude-3-sonnet-20240229", config)?,
/// ];
///
/// let results = execute_parallel(clients, "Hello, world!").await;
/// for (name, result) in results {
///     match result {
///         Ok(response) => println!("{}: {}", name, response),
///         Err(e) => eprintln!("{} failed: {}", name, e),
///     }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn execute_parallel(
    clients: Vec<Box<dyn AiClient>>,
    prompt: &str,
) -> Vec<(String, Result<String, ClientError>)> {
    use futures::future;

    let futures: Vec<_> = clients
        .iter()
        .map(|client| {
            let name = client.name().to_string();
            let prompt = prompt.to_string();
            async move {
                let result = client.send_prompt(&prompt).await;
                (name, result)
            }
        })
        .collect();

    future::join_all(futures).await
}

/// Execute multiple AI clients in parallel and return full responses with metadata
///
/// Like [`execute_parallel`] but calls [`AiClient::send_prompt_with_metadata`] on each
/// client, returning [`AiResponse`] instead of a plain `String`. Each `AiResponse`
/// carries `content` plus a [`ResponseMetadata`] with token counts, latency, model
/// version, and finish reason.
///
/// Use this when you need per-model token usage (e.g. for a `--show-usage` flag).
/// The existing [`execute_parallel`] is unchanged for backward compatibility.
///
/// # Arguments
///
/// * `clients` - Vector of AI clients to execute
/// * `prompt` - The prompt to send to all clients
///
/// # Returns
///
/// A vector of tuples containing the client name and either the full response or an error
///
/// # Example
///
/// ```rust,no_run
/// use chatdelta::{create_client, execute_parallel_with_metadata, ClientConfig};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = ClientConfig::default();
/// let clients = vec![
///     create_client("openai", "key1", "gpt-4o", config.clone())?,
///     create_client("anthropic", "key2", "claude-sonnet-4-6", config)?,
/// ];
///
/// let results = execute_parallel_with_metadata(clients, "Hello, world!").await;
/// for (name, result) in results {
///     match result {
///         Ok(r) => println!(
///             "{}: {} tokens — {}",
///             name,
///             r.metadata.total_tokens.unwrap_or(0),
///             r.content,
///         ),
///         Err(e) => eprintln!("{} failed: {}", name, e),
///     }
/// }
/// # Ok(())
/// # }
/// ```
pub async fn execute_parallel_with_metadata(
    clients: Vec<Box<dyn AiClient>>,
    prompt: &str,
) -> Vec<(String, Result<AiResponse, ClientError>)> {
    use futures::future;

    let futures: Vec<_> = clients
        .iter()
        .map(|client| {
            let name = client.name().to_string();
            let prompt = prompt.to_string();
            async move {
                let result = client.send_prompt_with_metadata(&prompt).await;
                (name, result)
            }
        })
        .collect();

    future::join_all(futures).await
}

/// Execute multiple AI clients in parallel with a conversation and return all results
///
/// This function runs all provided clients concurrently using conversation history
/// and returns the results in the order they complete.
///
/// # Arguments
///
/// * `clients` - Vector of AI clients to execute
/// * `conversation` - The conversation to send to all clients
///
/// # Returns
///
/// A vector of tuples containing the client name and either the response or an error
pub async fn execute_parallel_conversation(
    clients: Vec<Box<dyn AiClient>>,
    conversation: &Conversation,
) -> Vec<(String, Result<String, ClientError>)> {
    use futures::future;

    let futures: Vec<_> = clients
        .iter()
        .map(|client| {
            let name = client.name().to_string();
            let conversation = conversation.clone();
            async move {
                let result = client.send_conversation(&conversation).await;
                (name, result)
            }
        })
        .collect();

    future::join_all(futures).await
}

/// Generate a summary using one of the provided clients
///
/// Takes the responses from multiple AI models and uses another AI client
/// to generate a summary highlighting key differences and commonalities.
///
/// # Arguments
///
/// * `client` - The AI client to use for generating the summary
/// * `responses` - Vector of tuples containing (model_name, response) pairs
///
/// # Example
///
/// ```rust,no_run
/// use chatdelta::{create_client, generate_summary, ClientConfig};
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let config = ClientConfig::default();
/// let summarizer = create_client("openai", "your-key", "gpt-4", config)?;
///
/// let responses = vec![
///     ("GPT-4".to_string(), "Response from GPT-4...".to_string()),
///     ("Claude".to_string(), "Response from Claude...".to_string()),
/// ];
///
/// let summary = generate_summary(&*summarizer, &responses).await?;
/// println!("Summary: {}", summary);
/// # Ok(())
/// # }
/// ```
pub async fn generate_summary(
    client: &dyn AiClient,
    responses: &[(String, String)],
) -> Result<String, ClientError> {
    let mut summary_prompt = "Given these AI model responses:\n".to_string();
    for (name, response) in responses {
        summary_prompt.push_str(&format!("{name}:\n{response}\n---\n"));
    }
    summary_prompt.push_str("Summarize the key differences and commonalities.");

    client.send_prompt(&summary_prompt).await
}

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

    #[test]
    fn test_client_config_default() {
        let config = ClientConfig::default();
        assert_eq!(config.timeout, Duration::from_secs(30));
        assert_eq!(config.retries, 0);
        assert_eq!(config.temperature, None);
        assert_eq!(config.max_tokens, Some(1024));
    }

    #[tokio::test]
    async fn test_execute_parallel() {
        let clients: Vec<Box<dyn AiClient>> = vec![
            Box::new(MockClient::new(
                "client1",
                vec![Ok("response1".to_string())],
            )),
            Box::new(MockClient::new(
                "client2",
                vec![Ok("response2".to_string())],
            )),
        ];

        let results = execute_parallel(clients, "test prompt").await;
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0, "client1");
        assert!(results[0].1.is_ok());
        assert_eq!(results[1].0, "client2");
        assert!(results[1].1.is_ok());
    }

    #[tokio::test]
    async fn test_generate_summary() {
        let client = MockClient::new("summarizer", vec![Ok("summary response".to_string())]);
        let responses = vec![
            ("AI1".to_string(), "response1".to_string()),
            ("AI2".to_string(), "response2".to_string()),
        ];

        let summary = generate_summary(&client, &responses).await;
        assert!(summary.is_ok());
        assert_eq!(summary.unwrap(), "summary response");
    }

    #[tokio::test]
    async fn test_execute_parallel_conversation() {
        let clients: Vec<Box<dyn AiClient>> = vec![
            Box::new(MockClient::new(
                "client1",
                vec![Ok("conversation response1".to_string())],
            )),
            Box::new(MockClient::new(
                "client2",
                vec![Ok("conversation response2".to_string())],
            )),
        ];

        let mut conversation = Conversation::new();
        conversation.add_user("Hello");
        conversation.add_assistant("Hi there!");
        conversation.add_user("How are you?");

        let results = execute_parallel_conversation(clients, &conversation).await;
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].0, "client1");
        assert!(results[0].1.is_ok());
        assert_eq!(results[1].0, "client2");
        assert!(results[1].1.is_ok());
    }

    #[tokio::test]
    async fn test_mock_client_conversation_support() {
        let client = MockClient::new("test", vec![Ok("conversation test".to_string())]);
        assert!(client.supports_conversations());
        assert!(!client.supports_streaming());

        let mut conversation = Conversation::new();
        conversation.add_user("Test message");

        let result = client.send_conversation(&conversation).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "conversation test");
    }
}