cognis 0.2.1

LLM application framework built on cognis-core
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
//! Streaming token callbacks through chains.
//!
//! This module provides utilities for receiving token-level streaming callbacks
//! when executing chains. Consumers can register callbacks to receive tokens as
//! they are generated by the underlying language model.
//!
//! # Key types
//!
//! - [`StreamingChainExecutor`] — wraps any `Runnable` chain and dispatches
//!   streaming events to registered callbacks.
//! - [`StreamingChainResult`] — the collected result of a streaming chain execution.
//! - [`TokenCollector`] — a [`CallbackHandler`] that accumulates all tokens.
//! - [`StreamingCallback`] — a higher-level trait for receiving streaming events.
//! - [`StreamingCallbackAdapter`] — adapts a [`StreamingCallback`] into a
//!   [`CallbackHandler`].
//! - [`ConsoleStreamingCallback`] — prints tokens to stdout as they arrive.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use serde_json::Value;
use uuid::Uuid;

use cognis_core::callbacks::base::CallbackHandler;
use cognis_core::error::Result;
use cognis_core::runnables::base::Runnable;
use cognis_core::runnables::config::RunnableConfig;

// ---------------------------------------------------------------------------
// StreamingChainResult
// ---------------------------------------------------------------------------

/// The collected result of a streaming chain execution.
#[derive(Debug, Clone)]
pub struct StreamingChainResult {
    /// The final collected output text.
    pub output: String,
    /// Total number of tokens that were streamed.
    pub token_count: usize,
    /// All individual token chunks received during streaming.
    pub chunks: Vec<String>,
    /// Arbitrary chain metadata.
    pub metadata: HashMap<String, Value>,
}

impl StreamingChainResult {
    /// Create a new `StreamingChainResult`.
    pub fn new(
        output: String,
        token_count: usize,
        chunks: Vec<String>,
        metadata: HashMap<String, Value>,
    ) -> Self {
        Self {
            output,
            token_count,
            chunks,
            metadata,
        }
    }
}

// ---------------------------------------------------------------------------
// TokenCollector
// ---------------------------------------------------------------------------

/// A [`CallbackHandler`] that collects all tokens received via
/// [`on_llm_new_token`](CallbackHandler::on_llm_new_token).
///
/// Thread-safe — the internal token buffer is protected by a `Mutex`.
pub struct TokenCollector {
    tokens: Mutex<Vec<String>>,
}

impl TokenCollector {
    /// Create a new, empty `TokenCollector`.
    pub fn new() -> Self {
        Self {
            tokens: Mutex::new(Vec::new()),
        }
    }

    /// Returns a snapshot of all collected tokens.
    pub fn get_tokens(&self) -> Vec<String> {
        self.tokens.lock().unwrap().clone()
    }

    /// Concatenates all collected tokens into a single string.
    pub fn get_full_text(&self) -> String {
        self.tokens.lock().unwrap().join("")
    }

    /// Returns the number of tokens collected so far.
    pub fn token_count(&self) -> usize {
        self.tokens.lock().unwrap().len()
    }

    /// Clears all collected tokens.
    pub fn clear(&self) {
        self.tokens.lock().unwrap().clear();
    }
}

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

#[async_trait]
impl CallbackHandler for TokenCollector {
    fn name(&self) -> &str {
        "TokenCollector"
    }

    async fn on_llm_new_token(
        &self,
        token: &str,
        _run_id: Uuid,
        _parent_run_id: Option<Uuid>,
    ) -> Result<()> {
        self.tokens.lock().unwrap().push(token.to_string());
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// StreamingCallback trait
// ---------------------------------------------------------------------------

/// A higher-level streaming callback trait with simplified method signatures.
///
/// Implement this trait when you want to react to streaming events without
/// dealing with the full [`CallbackHandler`] interface (UUIDs, serialized
/// metadata, etc.).
#[async_trait]
pub trait StreamingCallback: Send + Sync {
    /// Called for each token as it is generated.
    async fn on_token(&self, token: &str);

    /// Called when a chain execution begins.
    async fn on_chain_start(&self, name: &str);

    /// Called when a chain execution completes successfully.
    async fn on_chain_end(&self, output: &str);

    /// Called when a chain execution fails.
    async fn on_error(&self, error: &str);
}

// ---------------------------------------------------------------------------
// StreamingCallbackAdapter
// ---------------------------------------------------------------------------

/// Adapts a [`StreamingCallback`] into a full [`CallbackHandler`].
///
/// This lets you register a simple `StreamingCallback` implementation anywhere
/// that expects a `CallbackHandler`.
pub struct StreamingCallbackAdapter {
    inner: Arc<dyn StreamingCallback>,
}

impl StreamingCallbackAdapter {
    /// Create a new adapter wrapping the given `StreamingCallback`.
    pub fn new(callback: Arc<dyn StreamingCallback>) -> Self {
        Self { inner: callback }
    }
}

#[async_trait]
impl CallbackHandler for StreamingCallbackAdapter {
    fn name(&self) -> &str {
        "StreamingCallbackAdapter"
    }

    async fn on_llm_new_token(
        &self,
        token: &str,
        _run_id: Uuid,
        _parent_run_id: Option<Uuid>,
    ) -> Result<()> {
        self.inner.on_token(token).await;
        Ok(())
    }

    async fn on_chain_start(
        &self,
        _serialized: &Value,
        _inputs: &Value,
        _run_id: Uuid,
        _parent_run_id: Option<Uuid>,
    ) -> Result<()> {
        self.inner.on_chain_start("chain").await;
        Ok(())
    }

    async fn on_chain_end(
        &self,
        outputs: &Value,
        _run_id: Uuid,
        _parent_run_id: Option<Uuid>,
    ) -> Result<()> {
        let output_str = outputs
            .as_str()
            .map(|s| s.to_string())
            .unwrap_or_else(|| serde_json::to_string(outputs).unwrap_or_default());
        self.inner.on_chain_end(&output_str).await;
        Ok(())
    }

    async fn on_chain_error(
        &self,
        error: &str,
        _run_id: Uuid,
        _parent_run_id: Option<Uuid>,
    ) -> Result<()> {
        self.inner.on_error(error).await;
        Ok(())
    }

    async fn on_llm_error(
        &self,
        error: &str,
        _run_id: Uuid,
        _parent_run_id: Option<Uuid>,
    ) -> Result<()> {
        self.inner.on_error(error).await;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// ConsoleStreamingCallback
// ---------------------------------------------------------------------------

/// A [`StreamingCallback`] that prints tokens to stdout as they arrive.
pub struct ConsoleStreamingCallback;

impl ConsoleStreamingCallback {
    /// Create a new `ConsoleStreamingCallback`.
    pub fn new() -> Self {
        Self
    }
}

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

#[async_trait]
impl StreamingCallback for ConsoleStreamingCallback {
    async fn on_token(&self, token: &str) {
        use std::io::Write;
        print!("{}", token);
        std::io::stdout().flush().ok();
    }

    async fn on_chain_start(&self, name: &str) {
        println!("\n> Starting chain: {}", name);
    }

    async fn on_chain_end(&self, output: &str) {
        println!("\n> Chain complete: {}", output);
    }

    async fn on_error(&self, error: &str) {
        eprintln!("\n> Chain error: {}", error);
    }
}

// ---------------------------------------------------------------------------
// StreamingChainExecutor
// ---------------------------------------------------------------------------

/// Wraps any `Runnable` chain and enables token streaming via callbacks.
///
/// The executor holds a list of [`CallbackHandler`] instances and dispatches
/// chain-level and token-level events to all of them during execution.
pub struct StreamingChainExecutor {
    chain: Arc<dyn Runnable>,
    callbacks: Vec<Arc<dyn CallbackHandler>>,
}

impl StreamingChainExecutor {
    /// Create a new executor wrapping the given chain.
    pub fn new(chain: Arc<dyn Runnable>) -> Self {
        Self {
            chain,
            callbacks: Vec::new(),
        }
    }

    /// Register a callback handler that will receive streaming events.
    pub fn with_callback(mut self, handler: Arc<dyn CallbackHandler>) -> Self {
        self.callbacks.push(handler);
        self
    }

    /// Add a callback handler (mutable reference variant).
    pub fn add_callback(&mut self, handler: Arc<dyn CallbackHandler>) {
        self.callbacks.push(handler);
    }

    /// Execute the chain with streaming callbacks.
    ///
    /// This invokes the inner chain, dispatching `on_chain_start`,
    /// `on_chain_end` (or `on_chain_error`) events to all registered
    /// callbacks. Token-level events are expected to be raised by the
    /// underlying model during execution; this executor forwards them
    /// through the callback pipeline.
    pub async fn execute_streaming(
        &self,
        input: Value,
        extra_callbacks: Option<Vec<Arc<dyn CallbackHandler>>>,
    ) -> Result<StreamingChainResult> {
        let run_id = Uuid::new_v4();
        let chain_name = self.chain.name().to_string();

        // Merge registered + extra callbacks
        let all_callbacks: Vec<&Arc<dyn CallbackHandler>> = self
            .callbacks
            .iter()
            .chain(extra_callbacks.as_ref().map_or([].iter(), |v| v.iter()))
            .collect();

        // Notify chain start
        let serialized = serde_json::json!({"name": chain_name});
        for cb in &all_callbacks {
            if !cb.ignore_chain() {
                cb.on_chain_start(&serialized, &input, run_id, None).await?;
            }
        }

        // Build a RunnableConfig that carries the callbacks so the model
        // can fire on_llm_new_token through them.
        let config = RunnableConfig {
            callbacks: all_callbacks.iter().map(|cb| Arc::clone(cb)).collect(),
            ..RunnableConfig::default()
        };

        // Execute the inner chain
        let result = self.chain.invoke(input, Some(&config)).await;

        match result {
            Ok(output) => {
                // Notify chain end
                for cb in &all_callbacks {
                    if !cb.ignore_chain() {
                        cb.on_chain_end(&output, run_id, None).await?;
                    }
                }

                // Extract output text
                let output_text = output
                    .as_object()
                    .and_then(|m| m.values().next().and_then(|v| v.as_str().map(String::from)))
                    .unwrap_or_else(|| serde_json::to_string(&output).unwrap_or_default());

                // Collect tokens from any TokenCollector callbacks
                let mut all_chunks: Vec<String> = Vec::new();
                for cb in &all_callbacks {
                    // We cannot downcast through Arc<dyn CallbackHandler> in
                    // general, so the caller is expected to hold a direct
                    // reference to their TokenCollector.  We return the output
                    // as a single chunk if no explicit chunks were captured.
                    let _ = cb; // silence unused warning
                }

                if all_chunks.is_empty() {
                    all_chunks.push(output_text.clone());
                }

                let token_count = all_chunks.len();
                let mut metadata = HashMap::new();
                metadata.insert("chain_name".to_string(), Value::String(chain_name));
                metadata.insert("run_id".to_string(), Value::String(run_id.to_string()));

                Ok(StreamingChainResult::new(
                    output_text,
                    token_count,
                    all_chunks,
                    metadata,
                ))
            }
            Err(e) => {
                let error_str = e.to_string();
                // Notify chain error
                for cb in &all_callbacks {
                    if !cb.ignore_chain() {
                        let _ = cb.on_chain_error(&error_str, run_id, None).await;
                    }
                }
                Err(e)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Convenience helper
// ---------------------------------------------------------------------------

/// Convenience function: execute a chain with a single streaming callback and
/// return the final output string.
pub async fn stream_chain(
    chain: Arc<dyn Runnable>,
    input: Value,
    callback: Arc<dyn StreamingCallback>,
) -> Result<String> {
    let adapter = Arc::new(StreamingCallbackAdapter::new(callback));
    let executor = StreamingChainExecutor::new(chain).with_callback(adapter);
    let result = executor.execute_streaming(input, None).await?;
    Ok(result.output)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use cognis_core::language_models::chat_model::BaseChatModel;
    use cognis_core::language_models::fake::FakeListChatModel;

    fn fake_model(responses: Vec<&str>) -> Arc<dyn BaseChatModel> {
        Arc::new(FakeListChatModel::new(
            responses.into_iter().map(String::from).collect(),
        ))
    }

    fn make_chain(responses: Vec<&str>) -> Arc<dyn Runnable> {
        use crate::chains::LLMChain;
        Arc::new(
            LLMChain::builder()
                .model(fake_model(responses))
                .prompt("{input}")
                .build(),
        )
    }

    // --- Test helper: a StreamingCallback that records events ---

    struct RecordingCallback {
        tokens: Mutex<Vec<String>>,
        events: Mutex<Vec<String>>,
    }

    impl RecordingCallback {
        fn new() -> Self {
            Self {
                tokens: Mutex::new(Vec::new()),
                events: Mutex::new(Vec::new()),
            }
        }

        fn events(&self) -> Vec<String> {
            self.events.lock().unwrap().clone()
        }
    }

    #[async_trait]
    impl StreamingCallback for RecordingCallback {
        async fn on_token(&self, token: &str) {
            self.tokens.lock().unwrap().push(token.to_string());
            self.events.lock().unwrap().push(format!("token:{}", token));
        }

        async fn on_chain_start(&self, name: &str) {
            self.events.lock().unwrap().push(format!("start:{}", name));
        }

        async fn on_chain_end(&self, output: &str) {
            self.events.lock().unwrap().push(format!("end:{}", output));
        }

        async fn on_error(&self, error: &str) {
            self.events.lock().unwrap().push(format!("error:{}", error));
        }
    }

    // 1. TokenCollector captures tokens
    #[tokio::test]
    async fn test_token_collector_captures_tokens() {
        let collector = TokenCollector::new();
        let run_id = Uuid::new_v4();

        collector
            .on_llm_new_token("Hello", run_id, None)
            .await
            .unwrap();
        collector
            .on_llm_new_token(" world", run_id, None)
            .await
            .unwrap();

        let tokens = collector.get_tokens();
        assert_eq!(tokens, vec!["Hello", " world"]);
    }

    // 2. TokenCollector get_full_text
    #[tokio::test]
    async fn test_token_collector_get_full_text() {
        let collector = TokenCollector::new();
        let run_id = Uuid::new_v4();

        collector
            .on_llm_new_token("Hello", run_id, None)
            .await
            .unwrap();
        collector
            .on_llm_new_token(", ", run_id, None)
            .await
            .unwrap();
        collector
            .on_llm_new_token("world!", run_id, None)
            .await
            .unwrap();

        assert_eq!(collector.get_full_text(), "Hello, world!");
    }

    // 3. TokenCollector clear
    #[tokio::test]
    async fn test_token_collector_clear() {
        let collector = TokenCollector::new();
        let run_id = Uuid::new_v4();

        collector
            .on_llm_new_token("token1", run_id, None)
            .await
            .unwrap();
        collector
            .on_llm_new_token("token2", run_id, None)
            .await
            .unwrap();

        assert_eq!(collector.token_count(), 2);
        collector.clear();
        assert_eq!(collector.token_count(), 0);
        assert!(collector.get_tokens().is_empty());
        assert_eq!(collector.get_full_text(), "");
    }

    // 4. StreamingCallbackAdapter wraps correctly
    #[tokio::test]
    async fn test_streaming_callback_adapter_wraps_correctly() {
        let recording = Arc::new(RecordingCallback::new());
        let adapter = StreamingCallbackAdapter::new(recording.clone());

        let run_id = Uuid::new_v4();

        // Simulate token event
        adapter
            .on_llm_new_token("test_token", run_id, None)
            .await
            .unwrap();

        // Simulate chain start
        adapter
            .on_chain_start(&serde_json::json!({}), &serde_json::json!({}), run_id, None)
            .await
            .unwrap();

        // Simulate chain end
        adapter
            .on_chain_end(&serde_json::json!("done"), run_id, None)
            .await
            .unwrap();

        let events = recording.events();
        assert!(events.contains(&"token:test_token".to_string()));
        assert!(events.contains(&"start:chain".to_string()));
        assert!(events.contains(&"end:done".to_string()));
    }

    // 5. ConsoleStreamingCallback creation
    #[test]
    fn test_console_streaming_callback_creation() {
        let _cb = ConsoleStreamingCallback::new();
        let _cb_default = ConsoleStreamingCallback::default();
        // No panic = success
    }

    // 6. StreamingChainResult construction
    #[test]
    fn test_streaming_chain_result_construction() {
        let mut metadata = HashMap::new();
        metadata.insert("key".to_string(), Value::String("value".to_string()));

        let result = StreamingChainResult::new(
            "full output".to_string(),
            3,
            vec!["a".to_string(), "b".to_string(), "c".to_string()],
            metadata.clone(),
        );

        assert_eq!(result.output, "full output");
        assert_eq!(result.token_count, 3);
        assert_eq!(result.chunks.len(), 3);
        assert_eq!(result.chunks[0], "a");
        assert_eq!(result.chunks[1], "b");
        assert_eq!(result.chunks[2], "c");
        assert_eq!(
            result.metadata.get("key"),
            Some(&Value::String("value".to_string()))
        );
    }

    // 7. Multiple callbacks receive same tokens
    #[tokio::test]
    async fn test_multiple_callbacks_receive_same_tokens() {
        let collector1 = Arc::new(TokenCollector::new());
        let collector2 = Arc::new(TokenCollector::new());

        let run_id = Uuid::new_v4();
        let tokens = vec!["Hello", " ", "world"];

        for token in &tokens {
            collector1
                .on_llm_new_token(token, run_id, None)
                .await
                .unwrap();
            collector2
                .on_llm_new_token(token, run_id, None)
                .await
                .unwrap();
        }

        assert_eq!(collector1.get_tokens(), vec!["Hello", " ", "world"]);
        assert_eq!(collector2.get_tokens(), vec!["Hello", " ", "world"]);
        assert_eq!(collector1.get_full_text(), collector2.get_full_text());
    }

    // 8. Token count tracking
    #[tokio::test]
    async fn test_token_count_tracking() {
        let collector = TokenCollector::new();
        let run_id = Uuid::new_v4();

        assert_eq!(collector.token_count(), 0);

        for i in 0..5 {
            collector
                .on_llm_new_token(&format!("t{}", i), run_id, None)
                .await
                .unwrap();
        }

        assert_eq!(collector.token_count(), 5);
    }

    // 9. Empty stream handling
    #[tokio::test]
    async fn test_empty_stream_handling() {
        let collector = TokenCollector::new();

        assert_eq!(collector.token_count(), 0);
        assert!(collector.get_tokens().is_empty());
        assert_eq!(collector.get_full_text(), "");

        // StreamingChainResult with empty chunks
        let result = StreamingChainResult::new(String::new(), 0, Vec::new(), HashMap::new());

        assert_eq!(result.output, "");
        assert_eq!(result.token_count, 0);
        assert!(result.chunks.is_empty());
    }

    // 10. Chain metadata in result
    #[tokio::test]
    async fn test_chain_metadata_in_result() {
        let chain = make_chain(vec!["response text"]);
        let executor = StreamingChainExecutor::new(chain);

        let result = executor
            .execute_streaming(serde_json::json!({"input": "hello"}), None)
            .await
            .unwrap();

        assert!(result.metadata.contains_key("chain_name"));
        assert_eq!(
            result.metadata.get("chain_name"),
            Some(&Value::String("LLMChain".to_string()))
        );
        assert!(result.metadata.contains_key("run_id"));
    }

    // 11. StreamingCallback trait implementation
    #[tokio::test]
    async fn test_streaming_callback_trait_impl() {
        let recording = Arc::new(RecordingCallback::new());

        recording.on_token("hello").await;
        recording.on_chain_start("test_chain").await;
        recording.on_chain_end("done").await;
        recording.on_error("something failed").await;

        let events = recording.events();
        assert_eq!(events.len(), 4);
        assert_eq!(events[0], "token:hello");
        assert_eq!(events[1], "start:test_chain");
        assert_eq!(events[2], "end:done");
        assert_eq!(events[3], "error:something failed");
    }

    // 12. Callback ordering (start -> tokens -> end)
    #[tokio::test]
    async fn test_callback_ordering() {
        let recording = Arc::new(RecordingCallback::new());
        let adapter: Arc<dyn CallbackHandler> =
            Arc::new(StreamingCallbackAdapter::new(recording.clone()));

        let chain = make_chain(vec!["response"]);
        let executor = StreamingChainExecutor::new(chain).with_callback(adapter);

        let _result = executor
            .execute_streaming(serde_json::json!({"input": "test"}), None)
            .await
            .unwrap();

        let events = recording.events();
        // The first event should be a chain start
        assert!(
            events.first().map_or(false, |e| e.starts_with("start:")),
            "First event should be chain start, got: {:?}",
            events
        );
        // The last event should be a chain end
        assert!(
            events.last().map_or(false, |e| e.starts_with("end:")),
            "Last event should be chain end, got: {:?}",
            events
        );
    }

    // 13. Error callback on failure
    #[tokio::test]
    async fn test_error_callback_on_failure() {
        // Build a chain that will fail due to missing input variable
        let chain: Arc<dyn Runnable> = Arc::new(
            crate::chains::LLMChain::builder()
                .model(fake_model(vec!["response"]))
                .prompt("{missing_var}")
                .build(),
        );

        let recording = Arc::new(RecordingCallback::new());
        let adapter: Arc<dyn CallbackHandler> =
            Arc::new(StreamingCallbackAdapter::new(recording.clone()));

        let executor = StreamingChainExecutor::new(chain).with_callback(adapter);

        let result = executor
            .execute_streaming(serde_json::json!({"input": "test"}), None)
            .await;

        assert!(result.is_err());

        let events = recording.events();
        // Should have start + error
        assert!(
            events.iter().any(|e| e.starts_with("start:")),
            "Should have a start event, got: {:?}",
            events
        );
        assert!(
            events.iter().any(|e| e.starts_with("error:")),
            "Should have an error event, got: {:?}",
            events
        );
    }
}