use std::future::Future;
use std::pin::Pin;
use futures_util::stream::{FuturesOrdered, StreamExt as _};
use tea_protocol::{
AgentEvent, AgentEventType, EventDelta, EventEnvelope, ReasoningEffort, SessionId,
};
use tea_session::SessionSnapshot;
use tokio::sync::mpsc;
use super::ComposerAttachment;
use super::Overlay;
use super::state::{
ApprovalChoice, HostedToolView, MAX_VISIBLE_QUEUE_ITEMS, StreamingBlock, ToolProgressView,
ToolView, TuiState,
};
pub const DEFAULT_ACTION_CAPACITY: usize = 128;
pub const DEFAULT_EFFECT_CAPACITY: usize = 32;
const MAX_EPHEMERAL_TEXT_BYTES: usize = 1024 * 1024;
#[derive(Debug)]
pub enum Action {
Event(Box<EventEnvelope>),
SnapshotLoaded(Box<SessionSnapshot>),
SnapshotLoadFailed(String),
Reconnected,
Resize {
width: u16,
height: u16,
},
SetEditor(String),
AddAttachment(ComposerAttachment),
RemoveAttachment {
index: usize,
},
ClearAttachments,
ShowPendingUserPrompt(String),
StartRunActivity,
AdvanceRunElapsed(u64),
FinishRunActivity,
SetPendingReasoningEffort(Option<ReasoningEffort>),
ScrollTranscriptUp {
rows: usize,
},
ScrollTranscriptDown {
rows: usize,
},
FollowTranscriptTail,
SelectApproval(ApprovalChoice),
SetApprovalSubmitting(bool),
ToggleThinking,
ToggleTool(tea_protocol::ToolCallId),
ToggleAllTools,
QueueSteering(String),
QueueFollowUp(String),
SetOverlay(Option<Overlay>),
Notify(String),
SetMcpHealth(Vec<String>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Effect {
ReloadSnapshot {
session_id: SessionId,
},
Render,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum DispatchError {
#[error("terminal action queue is full")]
Full,
#[error("terminal action loop is closed")]
Closed,
}
#[derive(Debug, Clone)]
pub struct ActionSender {
sender: mpsc::Sender<Action>,
}
impl ActionSender {
pub fn try_send(&self, action: Action) -> Result<(), DispatchError> {
self.sender.try_send(action).map_err(|error| match error {
mpsc::error::TrySendError::Full(_) => DispatchError::Full,
mpsc::error::TrySendError::Closed(_) => DispatchError::Closed,
})
}
pub async fn send(&self, action: Action) -> Result<(), DispatchError> {
self.sender
.send(action)
.await
.map_err(|_| DispatchError::Closed)
}
}
pub trait EffectExecutor: Send + Sync {
fn execute(
&self,
effect: Effect,
state: &TuiState,
) -> Pin<Box<dyn Future<Output = Option<Action>> + Send + 'static>>;
}
#[derive(Debug)]
pub struct ActionLoop {
actions: mpsc::Receiver<Action>,
effect_capacity: usize,
}
impl ActionLoop {
#[must_use]
pub fn new(action_capacity: usize, effect_capacity: usize) -> (ActionSender, Self) {
assert!(action_capacity > 0, "action capacity must be non-zero");
assert!(effect_capacity > 0, "effect capacity must be non-zero");
let (action_sender, actions) = mpsc::channel(action_capacity);
(
ActionSender {
sender: action_sender,
},
Self {
actions,
effect_capacity,
},
)
}
pub async fn run(mut self, state: &mut TuiState, executor: &dyn EffectExecutor) {
type EffectFuture = Pin<Box<dyn Future<Output = Option<Action>> + Send + 'static>>;
let mut running = FuturesOrdered::<EffectFuture>::new();
let mut actions_open = true;
loop {
if !actions_open && running.is_empty() {
break;
}
tokio::select! {
action = self.actions.recv(), if actions_open && running.len() < self.effect_capacity => {
match action {
Some(action) => {
let effects = reduce(state, action);
enqueue_effects(
&mut running,
effects,
self.effect_capacity,
executor,
state,
);
}
None => actions_open = false,
}
}
result = running.next(), if !running.is_empty() => {
if let Some(Some(action)) = result {
let effects = reduce(state, action);
enqueue_effects(
&mut running,
effects,
self.effect_capacity,
executor,
state,
);
}
}
}
}
}
}
fn enqueue_effects(
running: &mut FuturesOrdered<Pin<Box<dyn Future<Output = Option<Action>> + Send + 'static>>>,
effects: Vec<Effect>,
capacity: usize,
executor: &dyn EffectExecutor,
state: &TuiState,
) {
for effect in effects {
if running.len() == capacity {
debug_assert!(matches!(effect, Effect::Render));
continue;
}
running.push_back(executor.execute(effect, state));
}
}
#[must_use]
#[allow(clippy::too_many_lines)] pub fn reduce(state: &mut TuiState, action: Action) -> Vec<Effect> {
match action {
Action::Event(event) => reduce_event(state, &event),
Action::SnapshotLoaded(snapshot) => {
if snapshot.state().session_id() == state.session_id {
state.rebuild(&snapshot);
} else {
state.notify("ignored snapshot for a different session");
}
vec![Effect::Render]
}
Action::SnapshotLoadFailed(message) => {
state.resyncing = false;
state.notify(format!("session resync failed: {message}"));
state.bump_generation();
vec![Effect::Render]
}
Action::Reconnected => request_resync(state, "event stream reconnected"),
Action::Resize { width, height } => {
state.viewport_width = width.max(1);
state.viewport_height = height.max(1);
state.bump_generation();
vec![Effect::Render]
}
Action::SetEditor(editor) => {
state.editor = bounded_text(editor, MAX_EPHEMERAL_TEXT_BYTES);
state.bump_generation();
vec![Effect::Render]
}
Action::AddAttachment(attachment) => {
if let Err(error) = state.add_attachment(attachment) {
state.notify(error.to_string());
}
state.bump_generation();
vec![Effect::Render]
}
Action::RemoveAttachment { index } => {
if !state.remove_attachment(index) {
state.notify("attachment index is out of range");
}
state.bump_generation();
vec![Effect::Render]
}
Action::ClearAttachments => {
state.clear_attachments();
state.bump_generation();
vec![Effect::Render]
}
Action::ShowPendingUserPrompt(text) => {
state.show_pending_user_prompt(bounded_text(text, MAX_EPHEMERAL_TEXT_BYTES));
state.bump_generation();
vec![Effect::Render]
}
Action::StartRunActivity => {
state.running = true;
state.run_elapsed_seconds = 0;
state.model_retry = None;
state.run_status = None;
state.usage = None;
state.cost = None;
state.notifications.clear();
state.bump_generation();
vec![Effect::Render]
}
Action::AdvanceRunElapsed(seconds) => {
if state.running && seconds > 0 {
state.run_elapsed_seconds = state.run_elapsed_seconds.saturating_add(seconds);
if let Some(retry) = state.model_retry.as_mut() {
retry.advance(seconds);
}
state.bump_generation();
vec![Effect::Render]
} else {
Vec::new()
}
}
Action::FinishRunActivity => {
if state.running {
state.running = false;
state.model_retry = None;
state.bump_generation();
vec![Effect::Render]
} else {
Vec::new()
}
}
Action::SetPendingReasoningEffort(effort) => {
if state.pending_reasoning_effort == effort {
Vec::new()
} else {
state.pending_reasoning_effort = effort;
state.bump_generation();
vec![Effect::Render]
}
}
Action::ScrollTranscriptUp { rows } => {
state.transcript_viewport.scroll_up(rows);
state.bump_generation();
vec![Effect::Render]
}
Action::ScrollTranscriptDown { rows } => {
state.transcript_viewport.scroll_down(rows);
state.bump_generation();
vec![Effect::Render]
}
Action::FollowTranscriptTail => {
state.transcript_viewport.follow_tail();
state.bump_generation();
vec![Effect::Render]
}
Action::SelectApproval(choice) => {
if state.approval.is_some() && !state.approval_submitting {
state.approval_choice = choice;
state.bump_generation();
vec![Effect::Render]
} else {
Vec::new()
}
}
Action::SetApprovalSubmitting(submitting) => {
if state.approval.is_some() && state.approval_submitting != submitting {
state.approval_submitting = submitting;
state.bump_generation();
vec![Effect::Render]
} else {
Vec::new()
}
}
Action::ToggleThinking => {
state.preferences.thinking_collapsed = !state.preferences.thinking_collapsed;
state.bump_generation();
vec![Effect::Render]
}
Action::ToggleTool(tool_call_id) => {
if !state.preferences.expanded_tools.remove(&tool_call_id) {
state.preferences.expanded_tools.insert(tool_call_id);
}
state.bump_generation();
vec![Effect::Render]
}
Action::ToggleAllTools => {
if state
.tools
.keys()
.all(|id| state.preferences.expanded_tools.contains(id))
{
state.preferences.expanded_tools.clear();
} else {
state
.preferences
.expanded_tools
.extend(state.tools.keys().copied());
}
state.bump_generation();
vec![Effect::Render]
}
Action::QueueSteering(text) => {
push_queue(&mut state.steering_queue, text);
state.bump_generation();
vec![Effect::Render]
}
Action::QueueFollowUp(text) => {
push_queue(&mut state.follow_up_queue, text);
state.bump_generation();
vec![Effect::Render]
}
Action::SetOverlay(overlay) => {
state.overlay = overlay;
state.bump_generation();
vec![Effect::Render]
}
Action::Notify(message) => {
state.notify(message);
state.bump_generation();
vec![Effect::Render]
}
Action::SetMcpHealth(rows) => {
state.set_mcp_health(rows);
state.bump_generation();
vec![Effect::Render]
}
}
}
#[allow(clippy::too_many_lines)] fn reduce_event(state: &mut TuiState, envelope: &EventEnvelope) -> Vec<Effect> {
if envelope.session_id() != state.session_id {
state.notify("ignored event for a different session");
return vec![Effect::Render];
}
if state
.observed_event_ids
.iter()
.any(|event_id| *event_id == envelope.event_id())
{
return Vec::new();
}
if envelope.sequence() <= state.event_cursor {
if envelope.event_type().compatibility()
== tea_protocol::EventCompatibility::SkippableObservation
{
state.remember_event(envelope.event_id());
return Vec::new();
}
return request_resync(state, "late state-bearing event detected");
}
if state.resyncing {
state.event_cursor = envelope.sequence();
state.remember_event(envelope.event_id());
return Vec::new();
}
state.event_cursor = envelope.sequence();
state.remember_event(envelope.event_id());
let mut effects = vec![Effect::Render];
match envelope.event() {
AgentEvent::RunStarted {} => {
state.running = true;
state.run_elapsed_seconds = 0;
state.model_retry = None;
state.observation_run_id = envelope.run_id();
state.run_status = None;
state.usage = None;
state.cost = None;
state.notifications.clear();
state.hosted_tools.clear();
state.hosted_tool_order.clear();
}
AgentEvent::MessageDelta {
message_id,
content_index,
delta,
} => {
let (thinking, text) = match delta {
EventDelta::TextDelta { text } => (false, text.as_str()),
EventDelta::ThinkingDelta { text } => (true, text.as_str()),
};
if !state.streaming.contains_key(message_id) {
state.record_stream(*message_id);
}
let message = state.streaming.entry(*message_id).or_default();
let block = message
.blocks
.entry(*content_index)
.or_insert_with(|| StreamingBlock {
thinking,
text: String::new(),
generation: 0,
});
if block.thinking != thinking {
return request_resync(state, "stream block type changed");
}
if block.text.len().saturating_add(text.len()) > MAX_EPHEMERAL_TEXT_BYTES {
return request_resync(state, "streamed message exceeded UI bound");
}
block.text.push_str(text);
block.generation = block.generation.wrapping_add(1);
state.note_transcript_item();
}
AgentEvent::ToolCallRequested {
tool_call_id,
tool_name,
arguments,
} => {
state.record_tool_activity(*tool_call_id);
state
.tools
.entry(*tool_call_id)
.or_insert_with(|| ToolView {
tool_call_id: *tool_call_id,
tool_name: tool_name.clone(),
arguments: arguments.clone(),
status: "requested".to_owned(),
approval_decision: None,
progress: None,
presentation: None,
preview: None,
});
state.note_transcript_item();
}
AgentEvent::ToolExecutionProgress {
tool_call_id,
message,
completed_units,
total_units,
} => {
if let Some(tool) = state.tools.get_mut(tool_call_id) {
"running".clone_into(&mut tool.status);
tool.progress = Some(ToolProgressView {
message: message.clone(),
completed_units: *completed_units,
total_units: *total_units,
});
state.note_transcript_item();
} else {
return request_resync(state, "tool progress referenced an unknown call");
}
}
AgentEvent::ToolExecutionPreview {
tool_call_id,
presentation,
} => {
if let Some(tool) = state.tools.get_mut(tool_call_id) {
tool.preview = Some(presentation.clone());
} else {
return request_resync(state, "tool preview referenced an unknown call");
}
}
AgentEvent::HostedToolStarted {
tool_call_id,
tool_name,
} => {
if state.hosted_tools.contains_key(tool_call_id) {
return request_resync(state, "hosted tool reused an active identity");
}
state.record_hosted_tool_activity(*tool_call_id);
state.hosted_tools.insert(
*tool_call_id,
HostedToolView {
tool_call_id: *tool_call_id,
tool_name: tool_name.clone(),
arguments: None,
outcome: None,
source_count: None,
},
);
state.note_transcript_item();
}
AgentEvent::HostedToolCompleted {
tool_call_id,
tool_name,
arguments,
outcome,
source_count,
} => {
let Some(tool) = state.hosted_tools.get_mut(tool_call_id) else {
return request_resync(state, "hosted completion referenced an unknown call");
};
if tool.tool_name != *tool_name || tool.outcome.is_some() {
return request_resync(state, "hosted completion changed an active identity");
}
tool.arguments = Some(arguments.clone());
tool.outcome = Some(outcome.clone());
tool.source_count = Some(*source_count);
state.note_transcript_item();
}
AgentEvent::ModelRetryScheduled {
message_id,
attempt,
max_retries,
delay_ms,
} => {
state.streaming.remove(message_id);
state.streaming_order.retain(|id| id != message_id);
state.hosted_tools.clear();
state.hosted_tool_order.clear();
state.model_retry = Some(super::state::ModelRetryView::new(
*message_id,
*attempt,
*max_retries,
*delay_ms,
));
}
AgentEvent::ModelRetryStarted { .. } => {
state.model_retry = None;
}
AgentEvent::ApprovalRequested { .. }
| AgentEvent::TurnCheckpointed {}
| AgentEvent::SessionCompacted { .. }
| AgentEvent::SessionForked { .. } => {
state.resyncing = true;
effects.insert(
0,
Effect::ReloadSnapshot {
session_id: state.session_id,
},
);
}
AgentEvent::RunFinished {
status,
usage,
cost,
} => {
state.running = false;
state.model_retry = None;
state.run_status = Some(*status);
state.usage.clone_from(usage);
state.cost.clone_from(cost);
state.resyncing = true;
effects.insert(
0,
Effect::ReloadSnapshot {
session_id: state.session_id,
},
);
}
}
state.bump_generation();
effects
}
fn request_resync(state: &mut TuiState, reason: &str) -> Vec<Effect> {
if state.resyncing {
Vec::new()
} else {
state.resyncing = true;
state.notify(reason);
state.bump_generation();
vec![
Effect::ReloadSnapshot {
session_id: state.session_id,
},
Effect::Render,
]
}
}
fn push_queue(queue: &mut std::collections::VecDeque<String>, text: String) {
if queue.len() == MAX_VISIBLE_QUEUE_ITEMS {
queue.pop_front();
}
queue.push_back(bounded_text(text, 64 * 1024));
}
fn bounded_text(mut text: String, max_bytes: usize) -> String {
if text.len() <= max_bytes {
return text;
}
let mut boundary = max_bytes;
while !text.is_char_boundary(boundary) {
boundary -= 1;
}
text.truncate(boundary);
text
}
#[must_use]
pub const fn is_observational(event: AgentEventType) -> bool {
matches!(
event,
AgentEventType::MessageDelta
| AgentEventType::ToolExecutionProgress
| AgentEventType::ToolExecutionPreview
| AgentEventType::HostedToolStarted
| AgentEventType::HostedToolCompleted
| AgentEventType::ModelRetryScheduled
| AgentEventType::ModelRetryStarted
)
}