agent_core/agent/
messages.rs

1// Message types for TUI-Controller communication
2//
3// These types are used to send events from the LLM controller to the TUI
4// and from the TUI to the controller.
5
6use crate::controller::types::ControlCmd;
7use crate::controller::{AskUserQuestionsRequest, PermissionRequest, ToolResultStatus, TurnId};
8
9/// Messages sent from the controller to the TUI for display
10#[derive(Debug, Clone)]
11pub enum UiMessage {
12    /// Streaming text chunk from LLM response
13    TextChunk {
14        session_id: i64,
15        turn_id: Option<TurnId>,
16        text: String,
17        input_tokens: i64,
18        output_tokens: i64,
19    },
20
21    /// General display message (tool status, info, etc.)
22    Display {
23        session_id: i64,
24        turn_id: Option<TurnId>,
25        message: String,
26    },
27
28    /// Tool execution has started
29    ToolExecuting {
30        session_id: i64,
31        turn_id: Option<TurnId>,
32        tool_use_id: String,
33        display_name: String,
34        display_title: String,
35    },
36
37    /// Tool execution has completed
38    ToolCompleted {
39        session_id: i64,
40        turn_id: Option<TurnId>,
41        tool_use_id: String,
42        status: ToolResultStatus,
43        error: Option<String>,
44    },
45
46    /// LLM response is complete
47    Complete {
48        session_id: i64,
49        turn_id: Option<TurnId>,
50        input_tokens: i64,
51        output_tokens: i64,
52        /// Stop reason from LLM - "tool_use" means tools will execute, "end_turn" means done
53        stop_reason: Option<String>,
54    },
55
56    /// Real-time token count update
57    TokenUpdate {
58        session_id: i64,
59        turn_id: Option<TurnId>,
60        input_tokens: i64,
61        output_tokens: i64,
62        context_limit: i32,
63    },
64
65    /// Error from the controller
66    Error {
67        session_id: i64,
68        turn_id: Option<TurnId>,
69        error: String,
70    },
71
72    /// System message (TUI-local, not from controller)
73    System { session_id: i64, message: String },
74
75    /// Control command completed
76    CommandComplete {
77        session_id: i64,
78        command: ControlCmd,
79        success: bool,
80        message: Option<String>,
81    },
82
83    /// Tool is blocked waiting for user interaction (e.g., AskUserQuestions)
84    UserInteractionRequired {
85        session_id: i64,
86        tool_use_id: String,
87        request: AskUserQuestionsRequest,
88        turn_id: Option<TurnId>,
89    },
90
91    /// Tool is blocked waiting for permission (e.g., AskForPermissions)
92    PermissionRequired {
93        session_id: i64,
94        tool_use_id: String,
95        request: PermissionRequest,
96        turn_id: Option<TurnId>,
97    },
98}
99
100/// Channel type aliases for TUI-Controller communication
101pub mod channels {
102    use crate::controller::ControllerInputPayload;
103    use tokio::sync::mpsc;
104
105    use super::UiMessage;
106
107    /// Sender for messages from TUI to controller
108    pub type ToControllerTx = mpsc::Sender<ControllerInputPayload>;
109    /// Receiver for messages from TUI to controller
110    pub type ToControllerRx = mpsc::Receiver<ControllerInputPayload>;
111
112    /// Sender for messages from controller to TUI
113    pub type FromControllerTx = mpsc::Sender<UiMessage>;
114    /// Receiver for messages from controller to TUI
115    pub type FromControllerRx = mpsc::Receiver<UiMessage>;
116
117    /// Default channel buffer size
118    pub const DEFAULT_CHANNEL_SIZE: usize = 100;
119
120    /// Creates a pair of channels for TUI-Controller communication
121    pub fn create_channels() -> (ToControllerTx, ToControllerRx, FromControllerTx, FromControllerRx)
122    {
123        let (to_ctrl_tx, to_ctrl_rx) = mpsc::channel(DEFAULT_CHANNEL_SIZE);
124        let (from_ctrl_tx, from_ctrl_rx) = mpsc::channel(DEFAULT_CHANNEL_SIZE);
125        (to_ctrl_tx, to_ctrl_rx, from_ctrl_tx, from_ctrl_rx)
126    }
127}