mojentic 3.0.2

An LLM integration framework for Rust
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
//! Chat session management with context window tracking.
//!
//! This module provides a chat session abstraction that manages conversation history
//! and automatically handles context window limits using token counting.

use crate::error::Result;
use crate::llm::broker::LlmBroker;
use crate::llm::gateway::CompletionConfig;
use crate::llm::gateways::TokenizerGateway;
use crate::llm::models::{LlmMessage, MessageRole};
use crate::llm::tools::LlmTool;
use futures::stream::{Stream, StreamExt};
use serde::{Deserialize, Serialize};
use std::pin::Pin;

/// An LLM message with token count metadata.
///
/// This extends the standard `LlmMessage` with token length information
/// for context window management.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SizedLlmMessage {
    #[serde(flatten)]
    pub message: LlmMessage,
    pub token_length: usize,
}

impl SizedLlmMessage {
    /// Create a new sized message
    pub fn new(message: LlmMessage, token_length: usize) -> Self {
        Self {
            message,
            token_length,
        }
    }

    /// Get the role of the message
    pub fn role(&self) -> MessageRole {
        self.message.role
    }

    /// Get the content of the message
    pub fn content(&self) -> Option<&str> {
        self.message.content.as_deref()
    }
}

/// A chat session that manages conversation history with context window limits.
///
/// `ChatSession` maintains a list of messages and automatically trims old messages
/// when the total token count exceeds the configured maximum context size. The system
/// prompt (first message) is always preserved.
///
/// # Examples
///
/// ```ignore
/// use mojentic::llm::{ChatSession, LlmBroker};
/// use mojentic::llm::gateways::OllamaGateway;
/// use std::sync::Arc;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let gateway = Arc::new(OllamaGateway::default());
///     let broker = LlmBroker::new("qwen3:32b", gateway);
///     let mut session = ChatSession::new(broker);
///
///     let response = session.send("What is Rust?").await?;
///     println!("Response: {}", response);
///
///     Ok(())
/// }
/// ```
pub struct ChatSession {
    broker: LlmBroker,
    messages: Vec<SizedLlmMessage>,
    tools: Option<Vec<Box<dyn LlmTool>>>,
    max_context: usize,
    tokenizer_gateway: TokenizerGateway,
    completion_config: CompletionConfig,
}

impl ChatSession {
    /// Create a new chat session with default settings.
    ///
    /// # Arguments
    ///
    /// * `broker` - The LLM broker to use for generating responses
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mojentic::llm::{ChatSession, LlmBroker};
    /// use mojentic::llm::gateways::OllamaGateway;
    /// use std::sync::Arc;
    ///
    /// let gateway = Arc::new(OllamaGateway::default());
    /// let broker = LlmBroker::new("qwen3:32b", gateway);
    /// let session = ChatSession::new(broker);
    /// ```
    pub fn new(broker: LlmBroker) -> Self {
        Self::builder(broker).build()
    }

    /// Create a chat session builder for custom configuration.
    ///
    /// # Arguments
    ///
    /// * `broker` - The LLM broker to use for generating responses
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mojentic::llm::ChatSession;
    ///
    /// let session = ChatSession::builder(broker)
    ///     .system_prompt("You are a helpful coding assistant.")
    ///     .temperature(0.7)
    ///     .max_context(16384)
    ///     .build();
    /// ```
    pub fn builder(broker: LlmBroker) -> ChatSessionBuilder {
        ChatSessionBuilder::new(broker)
    }

    /// Send a message to the LLM and get a response.
    ///
    /// This method:
    /// 1. Adds the user message to the conversation history
    /// 2. Generates a response using the LLM
    /// 3. Adds the assistant's response to the history
    /// 4. Automatically trims old messages if context window is exceeded
    ///
    /// # Arguments
    ///
    /// * `query` - The user's message
    ///
    /// # Returns
    ///
    /// The LLM's response as a string
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let response = session.send("What is 2 + 2?").await?;
    /// println!("Answer: {}", response);
    /// ```
    pub async fn send(&mut self, query: &str) -> Result<String> {
        // Add user message
        self.insert_message(LlmMessage::user(query));

        // Generate response
        let messages: Vec<LlmMessage> = self.messages.iter().map(|m| m.message.clone()).collect();
        let config = self.completion_config.clone();

        let response = self
            .broker
            .generate(&messages, self.tools.as_deref(), Some(config), None)
            .await?;

        // Ensure all messages in history have token counts
        self.ensure_all_messages_are_sized();

        // Add assistant response
        self.insert_message(LlmMessage::assistant(&response));

        Ok(response)
    }

    /// Send a message to the LLM and get a streaming response.
    ///
    /// This method:
    /// 1. Adds the user message to the conversation history
    /// 2. Streams the response from the LLM, yielding chunks as they arrive
    /// 3. After the stream is fully consumed, adds the assembled response to history
    /// 4. Automatically trims old messages if context window is exceeded
    ///
    /// # Arguments
    ///
    /// * `query` - The user's message
    ///
    /// # Returns
    ///
    /// A stream of string chunks from the LLM response
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use futures::stream::StreamExt;
    ///
    /// let mut stream = session.send_stream("Tell me a story");
    /// while let Some(result) = stream.next().await {
    ///     print!("{}", result?);
    /// }
    /// ```
    pub fn send_stream<'a>(
        &'a mut self,
        query: &str,
    ) -> Pin<Box<dyn Stream<Item = Result<String>> + 'a>> {
        // Add user message
        self.insert_message(LlmMessage::user(query));

        // Clone messages for the broker call
        let messages: Vec<LlmMessage> = self.messages.iter().map(|m| m.message.clone()).collect();
        let config = self.completion_config.clone();

        Box::pin(async_stream::stream! {
            let mut accumulated = Vec::new();
            let tools_ref = self.tools.as_deref();
            let mut inner_stream = self.broker.generate_stream(&messages, tools_ref, Some(config), None);

            while let Some(result) = inner_stream.next().await {
                match &result {
                    Ok(chunk) => {
                        accumulated.push(chunk.clone());
                        yield result;
                    }
                    Err(_) => {
                        yield result;
                        return;
                    }
                }
            }

            // Stream consumed — finalize
            drop(inner_stream);
            self.ensure_all_messages_are_sized();
            let full_response = accumulated.join("");
            self.insert_message(LlmMessage::assistant(&full_response));
        })
    }

    /// Insert a message into the conversation history.
    ///
    /// If the total token count exceeds `max_context`, the oldest messages
    /// are removed until the total is under the limit. The system prompt
    /// (index 0) is always preserved.
    ///
    /// # Arguments
    ///
    /// * `message` - The message to add
    pub fn insert_message(&mut self, message: LlmMessage) {
        let sized_message = self.build_sized_message(message);
        self.messages.push(sized_message);

        // Trim messages if over context limit
        let mut total_length: usize = self.messages.iter().map(|m| m.token_length).sum();

        while total_length > self.max_context && self.messages.len() > 1 {
            // Remove the oldest message (index 1 to preserve system prompt at 0)
            let removed = self.messages.remove(1);
            total_length -= removed.token_length;
        }
    }

    /// Get the current conversation history
    pub fn messages(&self) -> &[SizedLlmMessage] {
        &self.messages
    }

    /// Get the total token count of the current conversation
    pub fn total_tokens(&self) -> usize {
        self.messages.iter().map(|m| m.token_length).sum()
    }

    /// Build a sized message from a regular message
    fn build_sized_message(&self, message: LlmMessage) -> SizedLlmMessage {
        let token_length = if let Some(content) = &message.content {
            self.tokenizer_gateway.encode(content).len()
        } else {
            0
        };

        SizedLlmMessage::new(message, token_length)
    }

    /// Ensure all messages in history have token counts
    fn ensure_all_messages_are_sized(&mut self) {
        for i in 0..self.messages.len() {
            if self.messages[i].token_length == 0 && self.messages[i].message.content.is_some() {
                let content = self.messages[i].message.content.clone().unwrap();
                let token_length = self.tokenizer_gateway.encode(&content).len();
                self.messages[i].token_length = token_length;
            }
        }
    }
}

/// Builder for constructing a `ChatSession` with custom configuration.
pub struct ChatSessionBuilder {
    broker: LlmBroker,
    system_prompt: String,
    tools: Option<Vec<Box<dyn LlmTool>>>,
    max_context: usize,
    tokenizer_gateway: Option<TokenizerGateway>,
    completion_config: CompletionConfig,
}

impl ChatSessionBuilder {
    /// Create a new builder
    fn new(broker: LlmBroker) -> Self {
        Self {
            broker,
            system_prompt: "You are a helpful assistant.".to_string(),
            tools: None,
            max_context: 32768,
            tokenizer_gateway: None,
            completion_config: CompletionConfig::default(),
        }
    }

    /// Set the system prompt (default: "You are a helpful assistant.")
    pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.system_prompt = prompt.into();
        self
    }

    /// Set the tools available to the LLM
    pub fn tools(mut self, tools: Vec<Box<dyn LlmTool>>) -> Self {
        self.tools = Some(tools);
        self
    }

    /// Set the maximum context window in tokens (default: 32768)
    pub fn max_context(mut self, max_context: usize) -> Self {
        self.max_context = max_context;
        self
    }

    /// Set a custom tokenizer gateway (default: cl100k_base)
    pub fn tokenizer_gateway(mut self, gateway: TokenizerGateway) -> Self {
        self.tokenizer_gateway = Some(gateway);
        self
    }

    /// Set provider controls for every turn, including tool continuations and streams.
    ///
    /// Unset controls use provider settings. `max_context` remains a local history
    /// budget and does not set the provider's `num_ctx`.
    pub fn completion_config(mut self, config: CompletionConfig) -> Self {
        self.completion_config = config;
        self
    }

    /// Set the temperature for generation (default: provider setting)
    pub fn temperature(mut self, temperature: f32) -> Self {
        self.completion_config.temperature = Some(temperature);
        self
    }

    /// Build the chat session
    pub fn build(self) -> ChatSession {
        let tokenizer_gateway = self.tokenizer_gateway.unwrap_or_default();
        let system_message = LlmMessage::system(&self.system_prompt);
        let token_length = tokenizer_gateway.encode(&self.system_prompt).len();

        ChatSession {
            broker: self.broker,
            messages: vec![SizedLlmMessage::new(system_message, token_length)],
            tools: self.tools,
            max_context: self.max_context,
            tokenizer_gateway,
            completion_config: self.completion_config,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::llm::gateway::{LlmGateway, StreamChunk};
    use crate::llm::models::LlmGatewayResponse;
    use crate::llm::tools::{FunctionDescriptor, ToolDescriptor};
    use async_trait::async_trait;
    use futures::stream::{self, Stream};
    use serde_json::{json, Value};
    use std::collections::HashMap;
    use std::pin::Pin;
    use std::sync::{Arc, Mutex};

    // Mock gateway for testing
    struct MockGateway {
        responses: Vec<String>,
        call_count: Mutex<usize>,
    }

    impl MockGateway {
        fn new(responses: Vec<String>) -> Self {
            Self {
                responses,
                call_count: Mutex::new(0),
            }
        }
    }

    #[async_trait::async_trait]
    impl LlmGateway for MockGateway {
        async fn complete(
            &self,
            _model: &str,
            _messages: &[LlmMessage],
            _tools: Option<&[Box<dyn LlmTool>]>,
            _config: &CompletionConfig,
        ) -> Result<LlmGatewayResponse> {
            let mut count = self.call_count.lock().unwrap();
            let idx = *count;
            *count += 1;

            let content = if idx < self.responses.len() {
                self.responses[idx].clone()
            } else {
                "default response".to_string()
            };

            Ok(LlmGatewayResponse {
                content: Some(content),
                object: None,
                tool_calls: vec![],
                thinking: None,
                ..Default::default()
            })
        }

        async fn complete_json(
            &self,
            _model: &str,
            _messages: &[LlmMessage],
            _schema: Value,
            _config: &CompletionConfig,
        ) -> Result<Value> {
            Ok(json!({}))
        }

        async fn get_available_models(&self) -> Result<Vec<String>> {
            Ok(vec!["test-model".to_string()])
        }

        async fn calculate_embeddings(
            &self,
            _text: &str,
            _model: Option<&str>,
        ) -> Result<Vec<f32>> {
            Ok(vec![0.1, 0.2, 0.3])
        }

        fn complete_stream<'a>(
            &'a self,
            _model: &'a str,
            _messages: &'a [LlmMessage],
            _tools: Option<&'a [Box<dyn LlmTool>]>,
            _config: &'a CompletionConfig,
        ) -> Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send + 'a>> {
            Box::pin(stream::iter(vec![Ok(StreamChunk::Content("test".to_string()))]))
        }
    }

    // Mock tool for testing
    #[derive(Clone)]
    struct MockTool {
        name: String,
    }

    #[async_trait]

    impl LlmTool for MockTool {
        async fn run(
            &self,
            _args: &HashMap<String, Value>,
            _ctx: &crate::llm::tools::ToolRunCtx,
        ) -> Result<Value> {
            Ok(json!({"result": "success"}))
        }

        fn descriptor(&self) -> ToolDescriptor {
            ToolDescriptor {
                r#type: "function".to_string(),
                function: FunctionDescriptor {
                    name: self.name.clone(),
                    description: "A mock tool".to_string(),
                    parameters: json!({}),
                },
            }
        }

        fn clone_box(&self) -> Box<dyn LlmTool> {
            Box::new(self.clone())
        }
    }

    #[tokio::test]
    async fn test_new_session_has_system_message() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let session = ChatSession::new(broker);

        assert_eq!(session.messages.len(), 1);
        assert_eq!(session.messages[0].role(), MessageRole::System);
        assert_eq!(session.messages[0].content(), Some("You are a helpful assistant."));
    }

    #[tokio::test]
    async fn test_builder_custom_system_prompt() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let session = ChatSession::builder(broker).system_prompt("Custom system prompt").build();

        assert_eq!(session.messages.len(), 1);
        assert_eq!(session.messages[0].content(), Some("Custom system prompt"));
    }

    #[tokio::test]
    async fn test_builder_custom_temperature() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let session = ChatSession::builder(broker).temperature(0.5).build();

        assert_eq!(session.completion_config.temperature, Some(0.5));
    }

    #[tokio::test]
    async fn test_builder_custom_max_context() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let session = ChatSession::builder(broker).max_context(16384).build();

        assert_eq!(session.max_context, 16384);
    }

    #[tokio::test]
    async fn test_send_adds_messages_to_history() {
        let gateway = Arc::new(MockGateway::new(vec!["Hello, World!".to_string()]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        let response = session.send("Hi").await.unwrap();

        assert_eq!(response, "Hello, World!");
        // Should have: system, user, assistant
        assert_eq!(session.messages.len(), 3);
        assert_eq!(session.messages[1].role(), MessageRole::User);
        assert_eq!(session.messages[1].content(), Some("Hi"));
        assert_eq!(session.messages[2].role(), MessageRole::Assistant);
        assert_eq!(session.messages[2].content(), Some("Hello, World!"));
    }

    #[tokio::test]
    async fn test_send_multiple_turns() {
        let gateway = Arc::new(MockGateway::new(vec![
            "First response".to_string(),
            "Second response".to_string(),
        ]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        session.send("First query").await.unwrap();
        session.send("Second query").await.unwrap();

        // Should have: system, user1, assistant1, user2, assistant2
        assert_eq!(session.messages.len(), 5);
        assert_eq!(session.messages[3].content(), Some("Second query"));
        assert_eq!(session.messages[4].content(), Some("Second response"));
    }

    #[tokio::test]
    async fn test_insert_message_calculates_token_length() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        session.insert_message(LlmMessage::user("Hello"));

        assert_eq!(session.messages.len(), 2);
        assert!(session.messages[1].token_length > 0);
    }

    #[tokio::test]
    async fn test_context_window_trimming() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);

        // Create session with very small context window
        let mut session = ChatSession::builder(broker).max_context(50).build();

        // Add several messages with longer content to force trimming
        for i in 0..10 {
            session.insert_message(LlmMessage::user(format!(
                "This is a longer message number {} with more content to increase token count",
                i
            )));
        }

        // Should have trimmed old messages
        assert!(session.messages.len() < 11); // Less than 1 system + 10 user messages

        // System prompt should still be first
        assert_eq!(session.messages[0].role(), MessageRole::System);

        // Total tokens should be under limit
        assert!(session.total_tokens() <= 50);
    }

    #[tokio::test]
    async fn test_context_window_preserves_system_prompt() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);

        let mut session = ChatSession::builder(broker)
            .system_prompt("Important system prompt")
            .max_context(50)
            .build();

        // Add many messages to force trimming
        for i in 0..20 {
            session.insert_message(LlmMessage::user(format!("Message {}", i)));
        }

        // System prompt should still be first
        assert_eq!(session.messages[0].role(), MessageRole::System);
        assert_eq!(session.messages[0].content(), Some("Important system prompt"));
    }

    #[tokio::test]
    async fn test_total_tokens() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        let initial_tokens = session.total_tokens();
        assert!(initial_tokens > 0); // System prompt has tokens

        session.insert_message(LlmMessage::user("Hello"));

        assert!(session.total_tokens() > initial_tokens);
    }

    #[tokio::test]
    async fn test_messages_accessor() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        session.insert_message(LlmMessage::user("Test"));

        let messages = session.messages();
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].role(), MessageRole::System);
        assert_eq!(messages[1].role(), MessageRole::User);
    }

    #[tokio::test]
    async fn test_builder_with_tools() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);

        let tool: Box<dyn LlmTool> = Box::new(MockTool {
            name: "test_tool".to_string(),
        });

        let session = ChatSession::builder(broker).tools(vec![tool]).build();

        assert!(session.tools.is_some());
        assert_eq!(session.tools.as_ref().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn test_sized_message_creation() {
        let message = LlmMessage::user("Test content");
        let sized = SizedLlmMessage::new(message, 5);

        assert_eq!(sized.token_length, 5);
        assert_eq!(sized.role(), MessageRole::User);
        assert_eq!(sized.content(), Some("Test content"));
    }

    // Streaming mock gateway
    struct StreamingMockGateway {
        stream_chunks: Vec<Vec<String>>,
        call_count: Mutex<usize>,
    }

    impl StreamingMockGateway {
        fn new(stream_chunks: Vec<Vec<String>>) -> Self {
            Self {
                stream_chunks,
                call_count: Mutex::new(0),
            }
        }
    }

    #[async_trait::async_trait]
    impl LlmGateway for StreamingMockGateway {
        async fn complete(
            &self,
            _model: &str,
            _messages: &[LlmMessage],
            _tools: Option<&[Box<dyn LlmTool>]>,
            _config: &CompletionConfig,
        ) -> Result<LlmGatewayResponse> {
            Ok(LlmGatewayResponse {
                content: Some("default".to_string()),
                object: None,
                tool_calls: vec![],
                thinking: None,
                ..Default::default()
            })
        }

        async fn complete_json(
            &self,
            _model: &str,
            _messages: &[LlmMessage],
            _schema: Value,
            _config: &CompletionConfig,
        ) -> Result<Value> {
            Ok(json!({}))
        }

        async fn get_available_models(&self) -> Result<Vec<String>> {
            Ok(vec!["test-model".to_string()])
        }

        async fn calculate_embeddings(
            &self,
            _text: &str,
            _model: Option<&str>,
        ) -> Result<Vec<f32>> {
            Ok(vec![0.1, 0.2, 0.3])
        }

        fn complete_stream<'a>(
            &'a self,
            _model: &'a str,
            _messages: &'a [LlmMessage],
            _tools: Option<&'a [Box<dyn LlmTool>]>,
            _config: &'a CompletionConfig,
        ) -> Pin<Box<dyn Stream<Item = Result<StreamChunk>> + Send + 'a>> {
            let mut count = self.call_count.lock().unwrap();
            let idx = *count;
            *count += 1;

            let chunks = if idx < self.stream_chunks.len() {
                self.stream_chunks[idx].clone()
            } else {
                vec!["default".to_string()]
            };

            Box::pin(stream::iter(
                chunks.into_iter().map(|c| Ok(StreamChunk::Content(c))).collect::<Vec<_>>(),
            ))
        }
    }

    #[tokio::test]
    async fn test_send_stream_yields_content_chunks() {
        let gateway = Arc::new(StreamingMockGateway::new(vec![vec![
            "Hello".to_string(),
            " world".to_string(),
        ]]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        let mut chunks = Vec::new();
        let mut stream = session.send_stream("Hi");
        while let Some(result) = stream.next().await {
            chunks.push(result.unwrap());
        }

        assert_eq!(chunks, vec!["Hello", " world"]);
    }

    #[tokio::test]
    async fn test_send_stream_grows_message_history() {
        let gateway = Arc::new(StreamingMockGateway::new(vec![vec!["Response".to_string()]]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        {
            let mut stream = session.send_stream("Hi");
            while stream.next().await.is_some() {}
        }

        // system + user + assistant
        assert_eq!(session.messages.len(), 3);
    }

    #[tokio::test]
    async fn test_send_stream_records_assembled_response() {
        let gateway = Arc::new(StreamingMockGateway::new(vec![vec![
            "Hello".to_string(),
            " world".to_string(),
        ]]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        {
            let mut stream = session.send_stream("Hi");
            while stream.next().await.is_some() {}
        }

        assert_eq!(session.messages[2].content(), Some("Hello world"));
        assert_eq!(session.messages[2].role(), MessageRole::Assistant);
    }

    #[tokio::test]
    async fn test_send_stream_records_user_message() {
        let gateway = Arc::new(StreamingMockGateway::new(vec![vec!["Response".to_string()]]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        {
            let mut stream = session.send_stream("My question");
            while stream.next().await.is_some() {}
        }

        assert_eq!(session.messages[1].role(), MessageRole::User);
        assert_eq!(session.messages[1].content(), Some("My question"));
    }

    #[tokio::test]
    async fn test_send_stream_respects_context_capacity() {
        let gateway = Arc::new(StreamingMockGateway::new(vec![
            vec!["This is a longer response to consume tokens in the context window".to_string()],
            vec!["Another longer response that also consumes many tokens in context".to_string()],
        ]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::builder(broker).max_context(50).build();

        {
            let mut stream = session.send_stream("First longer query message with extra words");
            while stream.next().await.is_some() {}
        }
        {
            let mut stream = session.send_stream("Second longer query message with extra words");
            while stream.next().await.is_some() {}
        }

        // System prompt should still be first
        assert_eq!(session.messages[0].role(), MessageRole::System);
        // Total tokens should be under limit
        assert!(session.total_tokens() <= 50);
    }

    #[tokio::test]
    async fn test_message_with_no_content_has_zero_tokens() {
        let gateway = Arc::new(MockGateway::new(vec![]));
        let broker = LlmBroker::new("test-model", gateway, None);
        let mut session = ChatSession::new(broker);

        let message = LlmMessage {
            role: MessageRole::Assistant,
            content: None,
            tool_calls: None,
            image_paths: None,
            reasoning_content: None,
        };

        session.insert_message(message);

        // Should have system + the message with no content
        assert_eq!(session.messages.len(), 2);
        assert_eq!(session.messages[1].token_length, 0);
    }
}