oramacore-client 1.2.2

Server-side Rust client for OramaCore and Orama Cloud
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
//! AI session streaming functionality.

use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use futures::stream::{Stream, StreamExt};
use reqwest_eventsource::{Event, EventSource};
use serde::Serialize;
use tokio::sync::RwLock;
use tracing::{debug, error, info, warn};

use crate::auth::Target;
use crate::client::{ApiKeyPosition, ClientRequest, OramaClient};
use crate::error::{OramaError, Result};
use crate::types::*;
use crate::utils::{generate_uuid, parse_ai_response};

/// Streaming chunk types
#[derive(Debug, Clone, PartialEq)]
pub enum StreamChunk {
    /// Connection opened successfully
    ConnectionOpened,
    /// Content chunk from the AI response
    Content(String),
    /// Status update from the processing pipeline
    StatusUpdate(String),
    /// Raw data that couldn't be parsed
    RawData(String),
    /// Stream completed successfully
    Done,
    /// Connection retry attempt
    Retry { attempt: u32, delay_ms: u64 },
}

/// Configuration for streaming resilience
#[derive(Debug, Clone)]
pub struct StreamConfig {
    /// Maximum number of retry attempts
    pub max_retries: u32,
    /// Initial retry delay in milliseconds
    pub initial_retry_delay: u64,
    /// Maximum retry delay in milliseconds (for exponential backoff)
    pub max_retry_delay: u64,
    /// Connection timeout in seconds
    pub connection_timeout: u64,
    /// Stream idle timeout in seconds
    pub stream_timeout: u64,
}

impl Default for StreamConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_retry_delay: 1000, // 1 second
            max_retry_delay: 30000,    // 30 seconds
            connection_timeout: 30,    // 30 seconds
            stream_timeout: 300,       // 5 minutes
        }
    }
}

/// Configuration for creating an AI session
#[derive(Debug, Clone)]
pub struct CreateAiSessionConfig {
    pub llm_config: Option<LlmConfig>,
    pub initial_messages: Option<Vec<Message>>,
}

/// Answer configuration for AI requests
#[derive(Debug, Clone, Serialize)]
pub struct AnswerConfig {
    pub query: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub interaction_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visitor_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub session_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub messages: Option<Vec<Message>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub related: Option<RelatedQuestionsConfig>,
    #[serde(rename = "datasourceIDs", skip_serializing_if = "Option::is_none")]
    pub datasource_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_similarity: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_documents: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ragat_notation: Option<String>,
    #[serde(rename = "LLMConfig", skip_serializing_if = "Option::is_none")]
    pub llm_config: Option<LlmConfig>,
}

/// Interaction state for conversations
#[derive(Debug, Clone)]
pub struct Interaction {
    pub id: String,
    pub query: String,
    pub response: String,
    pub sources: Option<AnyObject>,
    pub loading: bool,
    pub error: bool,
    pub error_message: Option<String>,
    pub aborted: bool,
    pub related: Option<String>,
    pub current_step: Option<String>,
    pub current_step_verbose: Option<String>,
    pub selected_llm: Option<LlmConfig>,
    pub optimized_query: Option<SearchParams>,
    pub advanced_autoquery: Option<serde_json::Value>,
}

impl Interaction {
    /// Create a new interaction
    pub fn new(id: String, query: String) -> Self {
        Self {
            id,
            query,
            response: String::new(),
            sources: None,
            loading: true,
            error: false,
            error_message: None,
            aborted: false,
            related: None,
            current_step: Some("starting".to_string()),
            current_step_verbose: None,
            selected_llm: None,
            optimized_query: None,
            advanced_autoquery: None,
        }
    }
}

/// AI session stream manager
#[derive(Debug)]
pub struct OramaCoreStream {
    collection_id: String,
    client: OramaClient,
    session_id: String,
    llm_config: Option<LlmConfig>,
    messages: Arc<RwLock<Vec<Message>>>,
    state: Arc<RwLock<Vec<Interaction>>>,
    last_interaction_params: Arc<RwLock<Option<AnswerConfig>>>,
    stream_config: StreamConfig,
}

impl OramaCoreStream {
    /// Create a new AI session stream
    pub async fn new(collection_id: String, client: OramaClient) -> Result<Self> {
        Ok(Self {
            collection_id,
            client,
            session_id: generate_uuid(),
            llm_config: None,
            messages: Arc::new(RwLock::new(Vec::new())),
            state: Arc::new(RwLock::new(Vec::new())),
            last_interaction_params: Arc::new(RwLock::new(None)),
            stream_config: StreamConfig::default(),
        })
    }

    /// Create a new AI session stream with configuration
    pub async fn with_config(
        collection_id: String,
        client: OramaClient,
        config: CreateAiSessionConfig,
    ) -> Result<Self> {
        let messages = config.initial_messages.unwrap_or_default();

        Ok(Self {
            collection_id,
            client,
            session_id: generate_uuid(),
            llm_config: config.llm_config,
            messages: Arc::new(RwLock::new(messages)),
            state: Arc::new(RwLock::new(Vec::new())),
            last_interaction_params: Arc::new(RwLock::new(None)),
            stream_config: StreamConfig::default(),
        })
    }

    /// Create a new AI session stream with streaming configuration
    pub async fn with_stream_config(
        collection_id: String,
        client: OramaClient,
        config: CreateAiSessionConfig,
        stream_config: StreamConfig,
    ) -> Result<Self> {
        let messages = config.initial_messages.unwrap_or_default();

        Ok(Self {
            collection_id,
            client,
            session_id: generate_uuid(),
            llm_config: config.llm_config,
            messages: Arc::new(RwLock::new(messages)),
            state: Arc::new(RwLock::new(Vec::new())),
            last_interaction_params: Arc::new(RwLock::new(None)),
            stream_config,
        })
    }

    /// Get a complete answer (non-streaming)
    pub async fn answer(&self, data: AnswerConfig) -> Result<String> {
        info!("Starting AI answer request");
        let enriched_config = self.enrich_config(data).await;
        debug!("Enriched config: {:?}", enriched_config);

        // Store the interaction parameters
        {
            let mut last_params = self.last_interaction_params.write().await;
            *last_params = Some(enriched_config.clone());
        }

        // Add user message
        {
            let mut messages = self.messages.write().await;
            messages.push(Message {
                role: Role::User,
                content: enriched_config.query.clone(),
            });
            messages.push(Message {
                role: Role::Assistant,
                content: String::new(),
            });
        }

        // Create interaction
        let interaction_id = enriched_config
            .interaction_id
            .clone()
            .unwrap_or_else(generate_uuid);

        let interaction = Interaction::new(interaction_id.clone(), enriched_config.query.clone());

        {
            let mut state = self.state.write().await;
            state.push(interaction);
        }

        // Make the actual API call
        let request = ClientRequest::post(
            format!("/v1/collections/{}/ai/answer", self.collection_id),
            Target::Reader,
            ApiKeyPosition::QueryParams,
            enriched_config,
        );

        let response: serde_json::Value = self.client.request(request).await.map_err(|e| {
            error!("API request failed: {}", e);
            e
        })?;

        // Extract the answer from the response
        let answer = response["answer"].as_str().unwrap_or_default().to_string();

        // Update the interaction and message
        {
            let mut state = self.state.write().await;
            if let Some(last_interaction) = state.last_mut() {
                last_interaction.response = answer.clone();
                last_interaction.loading = false;
                last_interaction.current_step = Some("completed".to_string());

                // Update with additional response data if available
                if let Some(sources) = response.get("sources") {
                    last_interaction.sources = Some(sources.clone());
                }
                if let Some(_related) = response.get("related") {
                    last_interaction.related = response["related"].as_str().map(String::from);
                }
            }
        }

        {
            let mut messages = self.messages.write().await;
            if let Some(last_message) = messages.last_mut() {
                last_message.content = answer.clone();
            }
        }

        info!("AI answer completed successfully, length: {}", answer.len());
        Ok(answer)
    }

    /// Create resilient SSE stream with retry logic
    async fn create_resilient_stream(
        &self,
        client: OramaClient,
        stream_url: String,
        auth_ref: crate::auth::AuthRef,
        enriched_config: AnswerConfig,
        messages: Arc<RwLock<Vec<Message>>>,
        state: Arc<RwLock<Vec<Interaction>>>,
    ) -> Result<impl Stream<Item = Result<StreamChunk>> + Send> {
        let stream_timeout = Duration::from_secs(self.stream_config.stream_timeout);
        let start_time = std::time::Instant::now();

        // Create request builder for EventSource
        let request_builder = client
            .inner()
            .post(&stream_url)
            .header("Accept", "text/event-stream")
            .header("Cache-Control", "no-cache")
            .header("Connection", "keep-alive")
            .header("Authorization", format!("Bearer {}", auth_ref.bearer))
            .timeout(Duration::from_secs(self.stream_config.connection_timeout))
            .json(&enriched_config);

        // Create EventSource
        let event_source = EventSource::new(request_builder).map_err(|e| {
            error!("Failed to create EventSource: {}", e);
            OramaError::generic(format!("EventSource creation failed: {e}"))
        })?;

        info!("Successfully created EventSource for streaming");

        // Convert EventSource to stream with comprehensive error handling
        let event_stream = event_source.map(move |event_result| {
            // Check for timeout
            if start_time.elapsed() >= stream_timeout {
                let timeout_secs = stream_timeout.as_secs();
                error!("Stream timeout after {} seconds", timeout_secs);
                let state_clone = state.clone();
                let timeout_msg = format!("Stream timeout after {timeout_secs} seconds");
                tokio::spawn(async move {
                    Self::mark_interaction_error(state_clone, timeout_msg).await;
                });
                return Err(OramaError::generic(format!(
                    "Stream timeout after {timeout_secs} seconds"
                )));
            }

            match event_result {
                Ok(event) => match event {
                    Event::Open => {
                        debug!("Stream connection opened");
                        Ok(StreamChunk::ConnectionOpened)
                    }
                    Event::Message(message) => {
                        debug!("Received streaming message: {}", message.data);

                        match message.data.as_str() {
                            "[DONE]" => {
                                info!("Streaming completed successfully");
                                let state_clone = state.clone();
                                tokio::spawn(async move {
                                    let mut state = state_clone.write().await;
                                    if let Some(interaction) = state.last_mut() {
                                        interaction.loading = false;
                                        interaction.current_step = Some("completed".to_string());
                                    }
                                });
                                Ok(StreamChunk::Done)
                            }
                            data => {
                                Self::process_stream_data(data, messages.clone(), state.clone())
                            }
                        }
                    }
                },
                Err(event_error) => {
                    error!("Stream event error: {}", event_error);
                    let state_clone = state.clone();
                    let error_msg = event_error.to_string();
                    tokio::spawn(async move {
                        Self::mark_interaction_error(state_clone, error_msg).await;
                    });
                    Err(OramaError::generic(format!(
                        "Stream event error: {event_error}"
                    )))
                }
            }
        });

        Ok(event_stream)
    }

    /// Get streaming answer with server-sent events
    pub async fn answer_stream(
        &self,
        data: AnswerConfig,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send>>> {
        info!("Starting streaming AI answer request");
        let enriched_config = self.enrich_config(data).await;
        debug!("Enriched streaming config: {:?}", enriched_config);

        // Store the interaction parameters
        {
            let mut last_params = self.last_interaction_params.write().await;
            *last_params = Some(enriched_config.clone());
        }

        // Add user message
        {
            let mut messages = self.messages.write().await;
            messages.push(Message {
                role: Role::User,
                content: enriched_config.query.clone(),
            });
            messages.push(Message {
                role: Role::Assistant,
                content: String::new(),
            });
        }

        // Create interaction
        let interaction_id = enriched_config
            .interaction_id
            .clone()
            .unwrap_or_else(generate_uuid);

        let interaction = Interaction::new(interaction_id.clone(), enriched_config.query.clone());

        {
            let mut state = self.state.write().await;
            state.push(interaction);
        }

        let client = self.client.clone();
        let collection_id = self.collection_id.clone();
        let messages = self.messages.clone();
        let state = self.state.clone();

        // Get auth reference for the streaming request
        let auth_ref = client.get_auth_ref(Target::Reader).await.map_err(|e| {
            error!("Failed to get auth reference: {}", e);
            e
        })?;

        let base_url = &auth_ref.base_url;
        let stream_url = format!("{base_url}/v1/collections/{collection_id}/ai/answer/stream");

        debug!("Creating streaming request to: {}", stream_url);

        // Create SSE stream using reqwest-eventsource with retry
        let stream = self
            .create_resilient_stream(
                client.clone(),
                stream_url,
                auth_ref,
                enriched_config,
                messages.clone(),
                state.clone(),
            )
            .await?;

        Ok(Box::pin(stream))
    }

    /// Regenerate the last response
    pub async fn regenerate_last(&self, stream: bool) -> Result<String> {
        info!("Starting regenerate_last, stream: {}", stream);

        let state_len = {
            let state = self.state.read().await;
            state.len()
        };

        let messages_len = {
            let messages = self.messages.read().await;
            messages.len()
        };

        if state_len == 0 || messages_len == 0 {
            warn!("No messages to regenerate");
            return Err(OramaError::generic("No messages to regenerate"));
        }

        // Check if last message is from assistant
        {
            let messages = self.messages.read().await;
            if let Some(last_message) = messages.last() {
                if !matches!(last_message.role, Role::Assistant) {
                    warn!("Last message is not from assistant");
                    return Err(OramaError::generic(
                        "Last message is not an assistant message",
                    ));
                }
            }
        }

        // Get the last interaction parameters
        let last_params = {
            let params = self.last_interaction_params.read().await;
            params.clone()
        };

        let last_params = last_params.ok_or_else(|| {
            warn!("No last interaction parameters available");
            OramaError::generic("No last interaction parameters available")
        })?;

        // Remove last assistant message and state
        {
            let mut messages = self.messages.write().await;
            messages.pop();
        }

        {
            let mut state = self.state.write().await;
            state.pop();
        }

        // Regenerate based on stream preference
        if stream {
            info!("Regenerating with streaming");
            let mut stream_result = self.answer_stream(last_params).await?;
            let mut complete_response = String::new();

            // Collect the stream
            while let Some(chunk_result) = stream_result.next().await {
                match chunk_result? {
                    StreamChunk::Content(content) => {
                        complete_response.push_str(&content);
                    }
                    StreamChunk::Done => {
                        break;
                    }
                    StreamChunk::StatusUpdate(status) => {
                        debug!("Status update during regeneration: {}", status);
                    }
                    _ => {
                        // Ignore other chunk types for regeneration
                    }
                }
            }

            Ok(complete_response)
        } else {
            info!("Regenerating without streaming");
            self.answer(last_params).await
        }
    }

    /// Clear the session
    pub async fn clear_session(&self) {
        {
            let mut messages = self.messages.write().await;
            messages.clear();
        }

        {
            let mut state = self.state.write().await;
            state.clear();
        }
    }

    /// Get current messages
    pub async fn get_messages(&self) -> Vec<Message> {
        let messages = self.messages.read().await;
        messages.clone()
    }

    /// Get current state
    pub async fn get_state(&self) -> Vec<Interaction> {
        let state = self.state.read().await;
        state.clone()
    }

    /// Get session ID
    pub fn session_id(&self) -> &str {
        &self.session_id
    }

    /// Get current stream configuration
    pub fn get_stream_config(&self) -> &StreamConfig {
        &self.stream_config
    }

    /// Update stream configuration
    pub fn set_stream_config(&mut self, config: StreamConfig) {
        self.stream_config = config;
    }

    /// Enrich config with default values
    async fn enrich_config(&self, mut config: AnswerConfig) -> AnswerConfig {
        if config.visitor_id.is_none() {
            config.visitor_id = Some(DEFAULT_SERVER_USER_ID.to_string());
        }

        if config.interaction_id.is_none() {
            config.interaction_id = Some(generate_uuid());
        }

        if config.session_id.is_none() {
            config.session_id = Some(self.session_id.clone());
        }

        // Use session's LLM config if none is provided in the request
        if config.llm_config.is_none() {
            config.llm_config = self.llm_config.clone();
        }

        config
    }

    /// Process streaming data chunk with robust JSON parsing
    fn process_stream_data(
        data: &str,
        messages: Arc<RwLock<Vec<Message>>>,
        state: Arc<RwLock<Vec<Interaction>>>,
    ) -> Result<StreamChunk> {
        // Use robust AI response parsing with automatic JSON fixing
        match parse_ai_response::<serde_json::Value>(data) {
            Ok(parsed) => {
                if let Some(content) = parsed.get("content").and_then(|c| c.as_str()) {
                    // Content chunk - update message and interaction
                    let content = content.to_string();
                    let content_for_update = content.clone();
                    let parsed_clone = parsed.clone();

                    tokio::spawn(async move {
                        // Update assistant message
                        {
                            let mut messages = messages.write().await;
                            if let Some(last_message) = messages.last_mut() {
                                if matches!(last_message.role, Role::Assistant) {
                                    last_message.content.push_str(&content_for_update);
                                }
                            }
                        }

                        // Update interaction state
                        {
                            let mut state = state.write().await;
                            if let Some(last_interaction) = state.last_mut() {
                                last_interaction.response.push_str(&content_for_update);

                                // Update step if provided
                                if let Some(step) =
                                    parsed_clone.get("step").and_then(|s| s.as_str())
                                {
                                    last_interaction.current_step = Some(step.to_string());
                                }

                                // Update verbose step if provided
                                if let Some(verbose) =
                                    parsed_clone.get("verbose_step").and_then(|s| s.as_str())
                                {
                                    last_interaction.current_step_verbose =
                                        Some(verbose.to_string());
                                }
                            }
                        }
                    });

                    Ok(StreamChunk::Content(content))
                } else if let Some(step) = parsed.get("step").and_then(|s| s.as_str()) {
                    // Status update
                    let step = step.to_string();
                    let step_for_update = step.clone();

                    tokio::spawn(async move {
                        let mut state = state.write().await;
                        if let Some(last_interaction) = state.last_mut() {
                            last_interaction.current_step = Some(step_for_update);
                        }
                    });

                    Ok(StreamChunk::StatusUpdate(step))
                } else if let Some(error_msg) = parsed.get("error").and_then(|e| e.as_str()) {
                    // Error in stream
                    warn!("Stream error received: {}", error_msg);

                    let state_clone = state.clone();
                    let error_message = error_msg.to_string();
                    tokio::spawn(async move {
                        Self::mark_interaction_error(state_clone, error_message).await;
                    });
                    Err(OramaError::generic(error_msg))
                } else {
                    // Unknown structured data
                    debug!("Unknown structured stream data: {}", data);
                    Ok(StreamChunk::RawData(data.to_string()))
                }
            }
            Err(parse_err) => {
                // Parsing failed even with JSON fixing - treat as raw data
                debug!(
                    "Failed to parse AI response as JSON ({}): {}",
                    parse_err, data
                );
                Ok(StreamChunk::RawData(data.to_string()))
            }
        }
    }

    /// Mark interaction as errored (async version)
    async fn mark_interaction_error(state: Arc<RwLock<Vec<Interaction>>>, error_message: String) {
        let mut state = state.write().await;
        if let Some(interaction) = state.last_mut() {
            interaction.error = true;
            interaction.error_message = Some(error_message);
            interaction.loading = false;
        }
    }
}

// Builder implementations
impl AnswerConfig {
    /// Create a new AnswerConfig
    pub fn new<S: Into<String>>(query: S) -> Self {
        Self {
            query: query.into(),
            interaction_id: None,
            visitor_id: None,
            session_id: None,
            messages: None,
            related: None,
            datasource_ids: None,
            min_similarity: None,
            max_documents: None,
            ragat_notation: None,
            llm_config: None,
        }
    }

    /// Set interaction ID
    pub fn with_interaction_id<S: Into<String>>(mut self, id: S) -> Self {
        self.interaction_id = Some(id.into());
        self
    }

    /// Set visitor ID
    pub fn with_visitor_id<S: Into<String>>(mut self, id: S) -> Self {
        self.visitor_id = Some(id.into());
        self
    }

    /// Set session ID
    pub fn with_session_id<S: Into<String>>(mut self, id: S) -> Self {
        self.session_id = Some(id.into());
        self
    }

    /// Set messages
    pub fn with_messages(mut self, messages: Vec<Message>) -> Self {
        self.messages = Some(messages);
        self
    }

    /// Set related questions config
    pub fn with_related(mut self, related: RelatedQuestionsConfig) -> Self {
        self.related = Some(related);
        self
    }

    /// Set datasource IDs
    pub fn with_datasource_ids(mut self, ids: Vec<String>) -> Self {
        self.datasource_ids = Some(ids);
        self
    }

    /// Set minimum similarity
    pub fn with_min_similarity(mut self, similarity: f64) -> Self {
        self.min_similarity = Some(similarity);
        self
    }

    /// Set maximum documents
    pub fn with_max_documents(mut self, max_docs: u32) -> Self {
        self.max_documents = Some(max_docs);
        self
    }

    /// Set RAGAT notation
    pub fn with_ragat_notation<S: Into<String>>(mut self, notation: S) -> Self {
        self.ragat_notation = Some(notation.into());
        self
    }

    /// Set LLM configuration
    pub fn with_llm_config(mut self, config: LlmConfig) -> Self {
        self.llm_config = Some(config);
        self
    }
}

impl CreateAiSessionConfig {
    /// Create a new CreateAiSessionConfig
    pub fn new() -> Self {
        Self {
            llm_config: None,
            initial_messages: None,
        }
    }

    /// Set LLM configuration
    pub fn with_llm_config(mut self, config: LlmConfig) -> Self {
        self.llm_config = Some(config);
        self
    }

    /// Set initial messages
    pub fn with_initial_messages(mut self, messages: Vec<Message>) -> Self {
        self.initial_messages = Some(messages);
        self
    }
}

impl Default for CreateAiSessionConfig {
    fn default() -> Self {
        Self::new()
    }
}