use crate::model::{CancelReply, PendingPermission, StatuslineSegment};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq)]
pub enum ReplEvent {
Key(KeyInput),
Resize(u16, u16),
Scroll(isize),
Submit(String),
Cancel,
CancelSettled(CancelReply),
Quit,
TurnFinished { generation: u64 },
AssistantOutput {
chunk: String,
done: bool,
is_error: bool,
},
ToolInvocation {
id: String,
agent_id: String,
tool_name: String,
args: serde_json::Value,
result: Option<String>,
failed: bool,
},
AgentOutput {
agent_id: String,
turn_id: String,
chunk: String,
done: bool,
},
DelegationStarted {
agent_id: String,
agent: String,
task: String,
},
DelegationFinished {
agent_id: String,
agent: String,
outcome: DelegationOutcome,
},
PermissionRequested {
request_id: String,
agent: String,
agent_id: String,
tool: String,
subject: String,
rule: String,
},
PermissionResolved {
request_id: String,
agent: String,
agent_id: String,
decision: String,
source: String,
},
PermissionAnswerFailed {
pending: PendingPermission,
error: String,
},
StatusMessage(String),
SplashUpdated(Vec<String>),
ClearScrollback,
StatuslineUpdate(Vec<StatuslineSegment>),
WorkstreamUpdated(WorkstreamSummary),
WorkstreamActivationChanged {
new_active_id: Option<String>,
prior_id: Option<String>,
},
ConnectionLost { reason: String },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DelegationOutcome {
Finished(String),
Failed(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyInput {
pub code: KeyCode,
pub modifiers: KeyModifiers,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyCode {
Char(char),
Enter,
Backspace,
Delete,
Tab,
Esc,
Left,
Right,
Up,
Down,
Home,
End,
PageUp,
PageDown,
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct KeyModifiers {
pub ctrl: bool,
pub alt: bool,
pub shift: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkstreamSummary {
pub id: String,
pub name: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn repl_event_variants_are_cloneable_and_comparable() {
let a = ReplEvent::StatusMessage("hello".to_string());
let b = a.clone();
assert_eq!(a, b);
let ws = ReplEvent::WorkstreamUpdated(WorkstreamSummary {
id: "a1b2c3d4".to_string(),
name: "Token rotation".to_string(),
});
assert_eq!(ws.clone(), ws);
}
#[test]
fn tool_invocation_start_and_complete_share_a_correlation_id() {
let start = ReplEvent::ToolInvocation {
id: "call-1".to_string(),
agent_id: String::new(),
tool_name: "fs.read".to_string(),
args: serde_json::json!({"path": "a.txt"}),
result: None,
failed: false,
};
let complete = ReplEvent::ToolInvocation {
id: "call-1".to_string(),
agent_id: String::new(),
tool_name: "fs.read".to_string(),
args: serde_json::json!({"path": "a.txt"}),
result: Some("contents".to_string()),
failed: false,
};
assert_ne!(start, complete);
let ReplEvent::ToolInvocation { id: start_id, .. } = &start else {
unreachable!()
};
let ReplEvent::ToolInvocation {
id: complete_id, ..
} = &complete
else {
unreachable!()
};
assert_eq!(start_id, complete_id);
}
#[test]
fn clear_scrollback_is_a_distinct_unit_variant() {
assert_eq!(ReplEvent::ClearScrollback, ReplEvent::ClearScrollback);
assert_ne!(ReplEvent::ClearScrollback, ReplEvent::Cancel);
}
#[test]
fn quit_is_a_distinct_unit_variant() {
assert_eq!(ReplEvent::Quit, ReplEvent::Quit);
assert_ne!(ReplEvent::Quit, ReplEvent::Cancel);
}
#[test]
fn turn_finished_carries_and_compares_its_generation() {
assert_eq!(
ReplEvent::TurnFinished { generation: 1 },
ReplEvent::TurnFinished { generation: 1 }
);
assert_ne!(
ReplEvent::TurnFinished { generation: 1 },
ReplEvent::TurnFinished { generation: 2 }
);
assert_ne!(ReplEvent::TurnFinished { generation: 1 }, ReplEvent::Quit);
}
#[test]
fn workstream_activation_changed_uses_wire_field_name() {
let ev = ReplEvent::WorkstreamActivationChanged {
new_active_id: Some("a1b2c3d4".to_string()),
prior_id: Some("00000000".to_string()),
};
let ReplEvent::WorkstreamActivationChanged {
new_active_id,
prior_id,
} = &ev
else {
unreachable!()
};
assert_eq!(new_active_id.as_deref(), Some("a1b2c3d4"));
assert_eq!(prior_id.as_deref(), Some("00000000"));
}
#[test]
fn workstream_activation_changed_represents_deactivation_with_no_replacement() {
let ev = ReplEvent::WorkstreamActivationChanged {
new_active_id: None,
prior_id: Some("a1b2c3d4".to_string()),
};
let ReplEvent::WorkstreamActivationChanged {
new_active_id,
prior_id,
} = &ev
else {
unreachable!()
};
assert_eq!(*new_active_id, None);
assert_eq!(prior_id.as_deref(), Some("a1b2c3d4"));
}
#[test]
fn key_modifiers_default_to_none_held() {
let m = KeyModifiers::default();
assert!(!m.ctrl && !m.alt && !m.shift);
}
#[test]
fn workstream_summary_round_trips_through_json() {
let ws = WorkstreamSummary {
id: "a1b2c3d4".to_string(),
name: "Token rotation".to_string(),
};
let json = serde_json::to_string(&ws).expect("serialize");
assert_eq!(ws, serde_json::from_str(&json).expect("deserialize"));
}
}