aethershell 0.3.1

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
//! A2UI (Agent-to-User Interface) Protocol
//!
//! Provides structured communication between AI agents and users through
//! rich UI interactions including notifications, prompts, progress indicators,
//! and content rendering.
//!
//! # Design Principles
//!
//! - **Non-blocking**: Events are queued for async consumption by UI layer
//! - **Typed Events**: Strongly typed event payloads for compile-time safety
//! - **TUI Integration**: Seamless integration with Ratatui-based terminal UI
//! - **Extensible**: Easy to add new event types and handlers
//!
//! # Example Usage
//!
//! ```aether
//! # Send a notification to the user
//! a2ui_notify "Processing complete!" { level: "success" }
//!
//! # Prompt user for input
//! response = a2ui_prompt "Choose an option" { options: ["A", "B", "C"] }
//!
//! # Show progress bar
//! for i in 1..100 {
//!     a2ui_progress "Downloading" { current: i, total: 100 }
//! }
//! ```

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::{Arc, Condvar, Mutex, RwLock};
use uuid::Uuid;

// ===================== Event Types =====================

/// Priority levels for A2UI events
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum EventPriority {
    Low = 0,
    Normal = 1,
    High = 2,
    Critical = 3,
}

impl Default for EventPriority {
    fn default() -> Self {
        Self::Normal
    }
}

/// Notification severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NotificationLevel {
    Info,
    Success,
    Warning,
    Error,
}

impl Default for NotificationLevel {
    fn default() -> Self {
        Self::Info
    }
}

impl std::str::FromStr for NotificationLevel {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "info" => Ok(Self::Info),
            "success" => Ok(Self::Success),
            "warning" | "warn" => Ok(Self::Warning),
            "error" | "err" => Ok(Self::Error),
            _ => Err(anyhow!("Invalid notification level: {}", s)),
        }
    }
}

/// User input prompt types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PromptType {
    /// Simple text input
    Text { placeholder: Option<String> },
    /// Yes/No confirmation
    Confirm { default: Option<bool> },
    /// Selection from options
    Select {
        options: Vec<String>,
        default: Option<usize>,
    },
    /// Multiple selection
    MultiSelect {
        options: Vec<String>,
        defaults: Vec<usize>,
    },
    /// Password input (hidden)
    Password { placeholder: Option<String> },
    /// File path selection
    FilePath {
        filter: Option<String>,
        must_exist: bool,
    },
}

/// User response to a prompt
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PromptResponse {
    Text(String),
    Confirm(bool),
    Select(usize),
    MultiSelect(Vec<usize>),
    Cancelled,
}

/// Content types for rendering
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RenderContent {
    /// Plain text
    Text(String),
    /// Markdown formatted text
    Markdown(String),
    /// JSON data (rendered as formatted)
    Json(serde_json::Value),
    /// Table data (rows of records)
    Table {
        headers: Vec<String>,
        rows: Vec<Vec<String>>,
    },
    /// Code block with syntax highlighting
    Code { language: String, content: String },
    /// Image (base64 or URL)
    Image { data: String, alt: Option<String> },
    /// Agent thinking/reasoning trace
    Thinking {
        steps: Vec<String>,
        final_answer: Option<String>,
    },
}

/// A2UI Event Types - structured payloads for UI interactions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum A2UIEventType {
    /// Display a notification to the user
    Notify {
        message: String,
        level: NotificationLevel,
        duration_ms: Option<u64>,
    },

    /// Request user input
    Prompt {
        id: Uuid,
        message: String,
        prompt_type: PromptType,
    },

    /// Update progress indicator
    Progress {
        id: Uuid,
        label: String,
        current: u64,
        total: u64,
        message: Option<String>,
    },

    /// Complete a progress indicator
    ProgressComplete { id: Uuid },

    /// Render content in the UI
    Render {
        target: Option<String>,
        content: RenderContent,
        replace: bool,
    },

    /// Clear content from a target area
    Clear { target: Option<String> },

    /// Highlight UI elements (for guidance)
    Highlight {
        element: String,
        message: Option<String>,
    },

    /// Update status bar
    Status {
        text: String,
        section: Option<String>,
    },

    /// Open a modal dialog
    Modal {
        id: Uuid,
        title: String,
        content: RenderContent,
        buttons: Vec<(String, String)>, // (label, action_id)
    },

    /// Close a modal dialog
    ModalClose { id: Uuid },

    /// Toast notification (auto-dismiss)
    Toast {
        message: String,
        level: NotificationLevel,
        duration_ms: u64,
    },

    /// Agent started working
    AgentStarted {
        agent_id: String,
        task: Option<String>,
    },

    /// Agent completed work
    AgentCompleted {
        agent_id: String,
        result: Option<String>,
        success: bool,
    },

    /// Agent is thinking (streaming thought process)
    AgentThinking {
        agent_id: String,
        thought: String,
        step: usize,
    },

    /// Request focus on a UI element
    Focus { element: String },

    /// Scroll to a specific location
    ScrollTo { element: String, position: String },
}

// ===================== Event Container =====================

/// A2UI Event with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct A2UIEvent {
    pub id: Uuid,
    pub timestamp: chrono::DateTime<chrono::Utc>,
    pub source: String,
    pub priority: EventPriority,
    pub event_type: A2UIEventType,
}

impl A2UIEvent {
    pub fn new(source: impl Into<String>, event_type: A2UIEventType) -> Self {
        Self {
            id: Uuid::new_v4(),
            timestamp: chrono::Utc::now(),
            source: source.into(),
            priority: EventPriority::default(),
            event_type,
        }
    }

    pub fn with_priority(mut self, priority: EventPriority) -> Self {
        self.priority = priority;
        self
    }

    /// Create a notification event
    pub fn notify(source: impl Into<String>, message: impl Into<String>) -> Self {
        Self::new(
            source,
            A2UIEventType::Notify {
                message: message.into(),
                level: NotificationLevel::Info,
                duration_ms: None,
            },
        )
    }

    /// Create a notification with level
    pub fn notify_level(
        source: impl Into<String>,
        message: impl Into<String>,
        level: NotificationLevel,
    ) -> Self {
        Self::new(
            source,
            A2UIEventType::Notify {
                message: message.into(),
                level,
                duration_ms: None,
            },
        )
    }

    /// Create a progress event
    pub fn progress(
        source: impl Into<String>,
        id: Uuid,
        label: impl Into<String>,
        current: u64,
        total: u64,
    ) -> Self {
        Self::new(
            source,
            A2UIEventType::Progress {
                id,
                label: label.into(),
                current,
                total,
                message: None,
            },
        )
    }

    /// Create a text prompt event
    pub fn prompt_text(source: impl Into<String>, message: impl Into<String>) -> (Self, Uuid) {
        let prompt_id = Uuid::new_v4();
        (
            Self::new(
                source,
                A2UIEventType::Prompt {
                    id: prompt_id,
                    message: message.into(),
                    prompt_type: PromptType::Text { placeholder: None },
                },
            ),
            prompt_id,
        )
    }

    /// Create a confirmation prompt event
    pub fn prompt_confirm(source: impl Into<String>, message: impl Into<String>) -> (Self, Uuid) {
        let prompt_id = Uuid::new_v4();
        (
            Self::new(
                source,
                A2UIEventType::Prompt {
                    id: prompt_id,
                    message: message.into(),
                    prompt_type: PromptType::Confirm { default: None },
                },
            ),
            prompt_id,
        )
    }

    /// Create a select prompt event
    pub fn prompt_select(
        source: impl Into<String>,
        message: impl Into<String>,
        options: Vec<String>,
    ) -> (Self, Uuid) {
        let prompt_id = Uuid::new_v4();
        (
            Self::new(
                source,
                A2UIEventType::Prompt {
                    id: prompt_id,
                    message: message.into(),
                    prompt_type: PromptType::Select {
                        options,
                        default: None,
                    },
                },
            ),
            prompt_id,
        )
    }

    /// Create a render event
    pub fn render(source: impl Into<String>, content: RenderContent) -> Self {
        Self::new(
            source,
            A2UIEventType::Render {
                target: None,
                content,
                replace: false,
            },
        )
    }

    /// Create a toast notification
    pub fn toast(
        source: impl Into<String>,
        message: impl Into<String>,
        level: NotificationLevel,
        duration_ms: u64,
    ) -> Self {
        Self::new(
            source,
            A2UIEventType::Toast {
                message: message.into(),
                level,
                duration_ms,
            },
        )
    }

    /// Create an agent started event
    pub fn agent_started(source: impl Into<String>, agent_id: impl Into<String>) -> Self {
        Self::new(
            source,
            A2UIEventType::AgentStarted {
                agent_id: agent_id.into(),
                task: None,
            },
        )
    }

    /// Create an agent completed event
    pub fn agent_completed(
        source: impl Into<String>,
        agent_id: impl Into<String>,
        success: bool,
    ) -> Self {
        Self::new(
            source,
            A2UIEventType::AgentCompleted {
                agent_id: agent_id.into(),
                result: None,
                success,
            },
        )
    }

    /// Create an agent thinking event
    pub fn agent_thinking(
        source: impl Into<String>,
        agent_id: impl Into<String>,
        thought: impl Into<String>,
        step: usize,
    ) -> Self {
        Self::new(
            source,
            A2UIEventType::AgentThinking {
                agent_id: agent_id.into(),
                thought: thought.into(),
                step,
            },
        )
    }

    /// Create a status bar update event
    pub fn status(source: impl Into<String>, text: impl Into<String>) -> Self {
        Self::new(
            source,
            A2UIEventType::Status {
                text: text.into(),
                section: None,
            },
        )
    }
}

// ===================== Event Channel =====================

/// Thread-safe event channel for A2UI communication
#[derive(Clone)]
pub struct A2UIChannel {
    /// Outgoing events (agent → UI)
    events: Arc<Mutex<VecDeque<A2UIEvent>>>,
    /// Pending prompt responses (UI → agent)
    responses: Arc<RwLock<std::collections::HashMap<Uuid, PromptResponse>>>,
    /// Condvar for blocking waits
    response_signal: Arc<(Mutex<bool>, Condvar)>,
    /// Max events to buffer
    max_events: usize,
}

impl A2UIChannel {
    pub fn new() -> Self {
        Self {
            events: Arc::new(Mutex::new(VecDeque::new())),
            responses: Arc::new(RwLock::new(std::collections::HashMap::new())),
            response_signal: Arc::new((Mutex::new(false), Condvar::new())),
            max_events: 1000,
        }
    }

    pub fn with_capacity(max_events: usize) -> Self {
        Self {
            events: Arc::new(Mutex::new(VecDeque::new())),
            responses: Arc::new(RwLock::new(std::collections::HashMap::new())),
            response_signal: Arc::new((Mutex::new(false), Condvar::new())),
            max_events,
        }
    }

    // ---- Agent-side API ----

    /// Send an event to the UI
    pub fn send(&self, event: A2UIEvent) -> Result<()> {
        let mut events = self
            .events
            .lock()
            .map_err(|e| anyhow!("Failed to acquire events lock: {}", e))?;

        // Enforce max capacity (drop oldest if full)
        while events.len() >= self.max_events {
            events.pop_front();
        }

        events.push_back(event);
        Ok(())
    }

    /// Send a notification
    pub fn notify(&self, source: &str, message: &str) -> Result<()> {
        self.send(A2UIEvent::notify(source, message))
    }

    /// Send a notification with level
    pub fn notify_level(
        &self,
        source: &str,
        message: &str,
        level: NotificationLevel,
    ) -> Result<()> {
        self.send(A2UIEvent::notify_level(source, message, level))
    }

    /// Send a progress update
    pub fn progress(
        &self,
        source: &str,
        id: Uuid,
        label: &str,
        current: u64,
        total: u64,
    ) -> Result<()> {
        self.send(A2UIEvent::progress(source, id, label, current, total))
    }

    /// Complete a progress indicator
    pub fn progress_complete(&self, source: &str, id: Uuid) -> Result<()> {
        self.send(A2UIEvent::new(
            source,
            A2UIEventType::ProgressComplete { id },
        ))
    }

    /// Request text input from user (blocking)
    pub fn prompt_text(&self, source: &str, message: &str) -> Result<PromptResponse> {
        let (event, prompt_id) = A2UIEvent::prompt_text(source, message);
        self.send(event)?;
        self.wait_for_response(prompt_id)
    }

    /// Request confirmation from user (blocking)
    pub fn prompt_confirm(&self, source: &str, message: &str) -> Result<bool> {
        let (event, prompt_id) = A2UIEvent::prompt_confirm(source, message);
        self.send(event)?;
        match self.wait_for_response(prompt_id)? {
            PromptResponse::Confirm(v) => Ok(v),
            PromptResponse::Cancelled => Ok(false),
            _ => Err(anyhow!("Unexpected response type")),
        }
    }

    /// Request selection from user (blocking)
    pub fn prompt_select(
        &self,
        source: &str,
        message: &str,
        options: Vec<String>,
    ) -> Result<PromptResponse> {
        let (event, prompt_id) = A2UIEvent::prompt_select(source, message, options);
        self.send(event)?;
        self.wait_for_response(prompt_id)
    }

    /// Send content for rendering
    pub fn render(&self, source: &str, content: RenderContent) -> Result<()> {
        self.send(A2UIEvent::render(source, content))
    }

    /// Send a toast notification
    pub fn toast(
        &self,
        source: &str,
        message: &str,
        level: NotificationLevel,
        duration_ms: u64,
    ) -> Result<()> {
        self.send(A2UIEvent::toast(source, message, level, duration_ms))
    }

    /// Update status bar
    pub fn status(&self, source: &str, text: &str) -> Result<()> {
        self.send(A2UIEvent::status(source, text))
    }

    // ---- UI-side API ----

    /// Receive all pending events (non-blocking)
    pub fn receive_all(&self) -> Result<Vec<A2UIEvent>> {
        let mut events = self
            .events
            .lock()
            .map_err(|e| anyhow!("Failed to acquire events lock: {}", e))?;
        Ok(events.drain(..).collect())
    }

    /// Receive events up to a limit
    pub fn receive(&self, max_count: usize) -> Result<Vec<A2UIEvent>> {
        let mut events = self
            .events
            .lock()
            .map_err(|e| anyhow!("Failed to acquire events lock: {}", e))?;

        let count = max_count.min(events.len());
        Ok(events.drain(..count).collect())
    }

    /// Peek at events without removing them
    pub fn peek(&self) -> Result<Vec<A2UIEvent>> {
        let events = self
            .events
            .lock()
            .map_err(|e| anyhow!("Failed to acquire events lock: {}", e))?;
        Ok(events.iter().cloned().collect())
    }

    /// Check if there are pending events
    pub fn has_events(&self) -> Result<bool> {
        let events = self
            .events
            .lock()
            .map_err(|e| anyhow!("Failed to acquire events lock: {}", e))?;
        Ok(!events.is_empty())
    }

    /// Get event count
    pub fn event_count(&self) -> Result<usize> {
        let events = self
            .events
            .lock()
            .map_err(|e| anyhow!("Failed to acquire events lock: {}", e))?;
        Ok(events.len())
    }

    /// Submit a response to a prompt
    pub fn submit_response(&self, prompt_id: Uuid, response: PromptResponse) -> Result<()> {
        {
            let mut responses = self
                .responses
                .write()
                .map_err(|e| anyhow!("Failed to acquire responses lock: {}", e))?;
            responses.insert(prompt_id, response);
        }

        // Signal waiting threads
        let (lock, cvar) = &*self.response_signal;
        let mut signaled = lock
            .lock()
            .map_err(|e| anyhow!("Failed to acquire signal lock: {}", e))?;
        *signaled = true;
        cvar.notify_all();

        Ok(())
    }

    /// Check if a response exists for a prompt (for testing)
    pub fn has_response(&self, prompt_id: Uuid) -> Result<bool> {
        let responses = self
            .responses
            .read()
            .map_err(|e| anyhow!("Failed to acquire responses lock: {}", e))?;
        Ok(responses.contains_key(&prompt_id))
    }

    /// Wait for a response to a prompt (with timeout)
    fn wait_for_response(&self, prompt_id: Uuid) -> Result<PromptResponse> {
        let timeout = std::time::Duration::from_secs(300); // 5 minute timeout
        let start = std::time::Instant::now();

        loop {
            // Check if response is available
            {
                let mut responses = self
                    .responses
                    .write()
                    .map_err(|e| anyhow!("Failed to acquire responses lock: {}", e))?;
                if let Some(response) = responses.remove(&prompt_id) {
                    return Ok(response);
                }
            }

            // Check timeout
            if start.elapsed() > timeout {
                return Ok(PromptResponse::Cancelled);
            }

            // Wait for signal
            let (lock, cvar) = &*self.response_signal;
            let signaled = lock
                .lock()
                .map_err(|e| anyhow!("Failed to acquire signal lock: {}", e))?;

            let wait_time = std::time::Duration::from_millis(100);
            let (mut guard, _) = cvar
                .wait_timeout(signaled, wait_time)
                .map_err(|e| anyhow!("Failed to wait on condition: {}", e))?;
            *guard = false;
        }
    }

    /// Clear all pending events
    pub fn clear(&self) -> Result<()> {
        let mut events = self
            .events
            .lock()
            .map_err(|e| anyhow!("Failed to acquire events lock: {}", e))?;
        events.clear();
        Ok(())
    }
}

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

// ===================== Global Instance =====================

lazy_static::lazy_static! {
    /// Global A2UI channel instance
    pub static ref A2UI_CHANNEL: A2UIChannel = A2UIChannel::new();
}

// ===================== Convenience Functions =====================

/// Send a notification to the global channel
pub fn notify(source: &str, message: &str) -> Result<()> {
    A2UI_CHANNEL.notify(source, message)
}

/// Send a notification with level to the global channel
pub fn notify_level(source: &str, message: &str, level: NotificationLevel) -> Result<()> {
    A2UI_CHANNEL.notify_level(source, message, level)
}

/// Send a progress update to the global channel
pub fn progress(source: &str, id: Uuid, label: &str, current: u64, total: u64) -> Result<()> {
    A2UI_CHANNEL.progress(source, id, label, current, total)
}

/// Complete a progress indicator
pub fn progress_complete(source: &str, id: Uuid) -> Result<()> {
    A2UI_CHANNEL.progress_complete(source, id)
}

/// Request text input (blocking)
pub fn prompt_text(source: &str, message: &str) -> Result<PromptResponse> {
    A2UI_CHANNEL.prompt_text(source, message)
}

/// Request confirmation (blocking)
pub fn prompt_confirm(source: &str, message: &str) -> Result<bool> {
    A2UI_CHANNEL.prompt_confirm(source, message)
}

/// Request selection (blocking)
pub fn prompt_select(source: &str, message: &str, options: Vec<String>) -> Result<PromptResponse> {
    A2UI_CHANNEL.prompt_select(source, message, options)
}

/// Render content
pub fn render(source: &str, content: RenderContent) -> Result<()> {
    A2UI_CHANNEL.render(source, content)
}

/// Show toast notification
pub fn toast(
    source: &str,
    message: &str,
    level: NotificationLevel,
    duration_ms: u64,
) -> Result<()> {
    A2UI_CHANNEL.toast(source, message, level, duration_ms)
}

/// Update status bar
pub fn status(source: &str, text: &str) -> Result<()> {
    A2UI_CHANNEL.status(source, text)
}

/// Receive all pending events from global channel
pub fn receive_all() -> Result<Vec<A2UIEvent>> {
    A2UI_CHANNEL.receive_all()
}

/// Submit response to a prompt on global channel
pub fn submit_response(prompt_id: Uuid, response: PromptResponse) -> Result<()> {
    A2UI_CHANNEL.submit_response(prompt_id, response)
}

// ===================== Tests =====================

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

    #[test]
    fn test_notification_level_parse() {
        assert_eq!(
            "info".parse::<NotificationLevel>().unwrap(),
            NotificationLevel::Info
        );
        assert_eq!(
            "success".parse::<NotificationLevel>().unwrap(),
            NotificationLevel::Success
        );
        assert_eq!(
            "warning".parse::<NotificationLevel>().unwrap(),
            NotificationLevel::Warning
        );
        assert_eq!(
            "warn".parse::<NotificationLevel>().unwrap(),
            NotificationLevel::Warning
        );
        assert_eq!(
            "error".parse::<NotificationLevel>().unwrap(),
            NotificationLevel::Error
        );
    }

    #[test]
    fn test_event_creation() {
        let event = A2UIEvent::notify("test_agent", "Hello World");
        assert_eq!(event.source, "test_agent");
        match event.event_type {
            A2UIEventType::Notify { message, level, .. } => {
                assert_eq!(message, "Hello World");
                assert_eq!(level, NotificationLevel::Info);
            }
            _ => panic!("Wrong event type"),
        }
    }

    #[test]
    fn test_channel_send_receive() {
        let channel = A2UIChannel::new();

        // Send some events
        channel.notify("agent1", "Test 1").unwrap();
        channel.notify("agent2", "Test 2").unwrap();

        assert_eq!(channel.event_count().unwrap(), 2);

        // Receive events
        let events = channel.receive_all().unwrap();
        assert_eq!(events.len(), 2);
        assert_eq!(channel.event_count().unwrap(), 0);
    }

    #[test]
    fn test_channel_capacity() {
        let channel = A2UIChannel::with_capacity(3);

        // Send more than capacity
        for i in 0..5 {
            channel.notify("agent", &format!("Message {}", i)).unwrap();
        }

        // Should only have last 3
        assert_eq!(channel.event_count().unwrap(), 3);
    }

    #[test]
    fn test_progress_events() {
        let channel = A2UIChannel::new();
        let progress_id = Uuid::new_v4();

        channel
            .progress("agent", progress_id, "Downloading", 50, 100)
            .unwrap();

        let events = channel.receive_all().unwrap();
        assert_eq!(events.len(), 1);

        match &events[0].event_type {
            A2UIEventType::Progress {
                id, current, total, ..
            } => {
                assert_eq!(*id, progress_id);
                assert_eq!(*current, 50);
                assert_eq!(*total, 100);
            }
            _ => panic!("Wrong event type"),
        }
    }

    #[test]
    fn test_prompt_response() {
        let channel = A2UIChannel::new();
        let prompt_id = Uuid::new_v4();

        // Simulate submitting a response
        channel
            .submit_response(prompt_id, PromptResponse::Text("user input".to_string()))
            .unwrap();

        // Check response is stored
        let responses = channel.responses.read().unwrap();
        assert!(responses.contains_key(&prompt_id));
    }

    #[test]
    fn test_render_content_variants() {
        let channel = A2UIChannel::new();

        // Text
        channel
            .render("agent", RenderContent::Text("Plain text".to_string()))
            .unwrap();

        // Markdown
        channel
            .render("agent", RenderContent::Markdown("# Header".to_string()))
            .unwrap();

        // Table
        channel
            .render(
                "agent",
                RenderContent::Table {
                    headers: vec!["Name".to_string(), "Value".to_string()],
                    rows: vec![vec!["foo".to_string(), "bar".to_string()]],
                },
            )
            .unwrap();

        // Code
        channel
            .render(
                "agent",
                RenderContent::Code {
                    language: "rust".to_string(),
                    content: "fn main() {}".to_string(),
                },
            )
            .unwrap();

        assert_eq!(channel.event_count().unwrap(), 4);
    }

    #[test]
    fn test_toast_events() {
        let channel = A2UIChannel::new();

        channel
            .toast("agent", "Quick message", NotificationLevel::Success, 3000)
            .unwrap();

        let events = channel.receive_all().unwrap();
        match &events[0].event_type {
            A2UIEventType::Toast {
                message,
                level,
                duration_ms,
            } => {
                assert_eq!(message, "Quick message");
                assert_eq!(*level, NotificationLevel::Success);
                assert_eq!(*duration_ms, 3000);
            }
            _ => panic!("Wrong event type"),
        }
    }

    #[test]
    fn test_agent_lifecycle_events() {
        let channel = A2UIChannel::new();

        channel
            .send(A2UIEvent::agent_started("system", "agent1"))
            .unwrap();
        channel
            .send(A2UIEvent::agent_thinking(
                "system",
                "agent1",
                "Analyzing...",
                1,
            ))
            .unwrap();
        channel
            .send(A2UIEvent::agent_completed("system", "agent1", true))
            .unwrap();

        let events = channel.receive_all().unwrap();
        assert_eq!(events.len(), 3);
    }

    #[test]
    fn test_event_priority() {
        let event = A2UIEvent::notify("agent", "test").with_priority(EventPriority::Critical);
        assert_eq!(event.priority, EventPriority::Critical);
    }

    #[test]
    fn test_global_channel() {
        // Use global channel
        notify("test", "Global test").unwrap();

        // Clear to not affect other tests
        A2UI_CHANNEL.clear().unwrap();
    }
}