use std::fmt::Write as _;
use tea_protocol::{CodeChange, MessageId, ToolCallId};
use super::{
DecisionStatus, LifecycleStatus, NoticeSeverity, OutputFormat, PlanStep, QueuedInputKind,
TimelineDetail, TimelineSource, append_raw_details, bounded_terminal_text,
code_change_raw_text, queue_preview, raw_plan_text, terminal_safe_text,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessageAuthor {
User,
Assistant,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LifecycleKind {
ToolCall,
HostedTool,
RunActivity,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoticeKind {
General,
McpHealth,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MessageCell {
author: MessageAuthor,
source: String,
format: OutputFormat,
}
impl MessageCell {
pub(crate) fn new(author: MessageAuthor, source: &str, format: OutputFormat) -> Self {
Self {
author,
source: bounded_terminal_text(source),
format,
}
}
#[must_use]
pub const fn author(&self) -> MessageAuthor {
self.author
}
#[must_use]
pub fn source(&self) -> &str {
&self.source
}
#[must_use]
pub const fn format(&self) -> OutputFormat {
self.format
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReasoningCell {
source: String,
collapsed: bool,
}
impl ReasoningCell {
pub(crate) fn new(source: &str, collapsed: bool) -> Self {
Self {
source: bounded_terminal_text(source),
collapsed,
}
}
#[must_use]
pub fn source(&self) -> &str {
&self.source
}
#[must_use]
pub const fn collapsed(&self) -> bool {
self.collapsed
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlanCell {
title: String,
source: Option<String>,
steps: Vec<PlanStep>,
note: Option<String>,
}
impl PlanCell {
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn new(
title: &str,
source: Option<&str>,
steps: Vec<PlanStep>,
note: Option<&str>,
) -> Self {
Self {
title: bounded_terminal_text(title),
source: source.map(bounded_terminal_text),
steps,
note: note.map(bounded_terminal_text),
}
}
#[must_use]
pub fn title(&self) -> &str {
&self.title
}
#[must_use]
pub fn source(&self) -> Option<&str> {
self.source.as_deref()
}
#[must_use]
pub fn steps(&self) -> &[PlanStep] {
&self.steps
}
#[must_use]
pub fn note(&self) -> Option<&str> {
self.note.as_deref()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LifecycleCell {
kind: LifecycleKind,
action: String,
target: Option<String>,
status: LifecycleStatus,
details: Vec<TimelineDetail>,
expanded: bool,
tick: u64,
}
impl LifecycleCell {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
kind: LifecycleKind,
action: &str,
target: Option<&str>,
status: LifecycleStatus,
details: Vec<TimelineDetail>,
expanded: bool,
tick: u64,
) -> Self {
Self {
kind,
action: bounded_terminal_text(action),
target: target.map(bounded_terminal_text),
status,
details,
expanded,
tick,
}
}
#[must_use]
pub const fn kind(&self) -> LifecycleKind {
self.kind
}
#[must_use]
pub fn action(&self) -> &str {
&self.action
}
#[must_use]
pub fn target(&self) -> Option<&str> {
self.target.as_deref()
}
#[must_use]
pub const fn status(&self) -> LifecycleStatus {
self.status
}
#[must_use]
pub fn details(&self) -> &[TimelineDetail] {
&self.details
}
#[must_use]
pub const fn expanded(&self) -> bool {
self.expanded
}
#[must_use]
pub const fn tick(&self) -> u64 {
self.tick
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResultCell {
action: String,
source_name: Option<String>,
content: String,
format: OutputFormat,
is_error: bool,
}
impl ResultCell {
pub(crate) fn new(
action: &str,
source_name: Option<&str>,
content: &str,
format: OutputFormat,
is_error: bool,
) -> Self {
Self {
action: bounded_terminal_text(action),
source_name: source_name.map(bounded_terminal_text),
content: bounded_terminal_text(content),
format,
is_error,
}
}
#[must_use]
pub fn action(&self) -> &str {
&self.action
}
#[must_use]
pub fn source_name(&self) -> Option<&str> {
self.source_name.as_deref()
}
#[must_use]
pub fn content(&self) -> &str {
&self.content
}
#[must_use]
pub const fn format(&self) -> OutputFormat {
self.format
}
#[must_use]
pub const fn is_error(&self) -> bool {
self.is_error
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourcesCell {
sources: Vec<TimelineSource>,
}
impl SourcesCell {
pub(crate) const fn new(sources: Vec<TimelineSource>) -> Self {
Self { sources }
}
#[must_use]
pub fn sources(&self) -> &[TimelineSource] {
&self.sources
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiffCell {
action: String,
change: CodeChange,
}
impl DiffCell {
pub(crate) fn new(action: &str, change: CodeChange) -> Self {
Self {
action: bounded_terminal_text(action),
change,
}
}
#[must_use]
pub fn action(&self) -> &str {
&self.action
}
#[must_use]
pub const fn change(&self) -> &CodeChange {
&self.change
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QueuedInputCell {
kind: QueuedInputKind,
preview: String,
}
impl QueuedInputCell {
pub(crate) fn new(kind: QueuedInputKind, preview: &str) -> Self {
Self {
kind,
preview: queue_preview(preview),
}
}
#[must_use]
pub const fn kind(&self) -> QueuedInputKind {
self.kind
}
#[must_use]
pub fn preview(&self) -> &str {
&self.preview
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NoticeCell {
kind: NoticeKind,
severity: NoticeSeverity,
message: String,
hint: Option<String>,
}
impl NoticeCell {
pub(crate) fn new(
kind: NoticeKind,
severity: NoticeSeverity,
message: &str,
hint: Option<&str>,
) -> Self {
Self {
kind,
severity,
message: bounded_terminal_text(message),
hint: hint.map(bounded_terminal_text),
}
}
#[must_use]
pub const fn kind(&self) -> NoticeKind {
self.kind
}
#[must_use]
pub const fn severity(&self) -> NoticeSeverity {
self.severity
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
#[must_use]
pub fn hint(&self) -> Option<&str> {
self.hint.as_deref()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecisionCell {
action: String,
subject: String,
status: DecisionStatus,
details: Vec<TimelineDetail>,
}
impl DecisionCell {
pub(crate) fn new(
action: &str,
subject: &str,
status: DecisionStatus,
details: Vec<TimelineDetail>,
) -> Self {
Self {
action: bounded_terminal_text(action),
subject: bounded_terminal_text(subject),
status,
details,
}
}
#[must_use]
pub fn action(&self) -> &str {
&self.action
}
#[must_use]
pub fn subject(&self) -> &str {
&self.subject
}
#[must_use]
pub const fn status(&self) -> DecisionStatus {
self.status
}
#[must_use]
pub fn details(&self) -> &[TimelineDetail] {
&self.details
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CellContent {
Message(MessageCell),
Reasoning(ReasoningCell),
Plan(PlanCell),
Lifecycle(LifecycleCell),
Result(ResultCell),
Sources(SourcesCell),
Diff(DiffCell),
QueuedInput(QueuedInputCell),
Notice(NoticeCell),
Decision(DecisionCell),
}
impl CellContent {
#[must_use]
pub(crate) const fn is_live_timeline_visible(&self) -> bool {
!matches!(self, Self::Decision(_))
}
#[must_use]
pub fn text(&self) -> &str {
match self {
Self::Message(cell) => cell.source(),
Self::Reasoning(cell) => cell.source(),
Self::Plan(cell) => cell.source().unwrap_or_else(|| cell.title()),
Self::Lifecycle(cell) => cell.action(),
Self::Result(cell) => cell.action(),
Self::Sources(_) => "Sources",
Self::Diff(cell) => cell.action(),
Self::QueuedInput(cell) => cell.preview(),
Self::Notice(cell) => cell.message(),
Self::Decision(cell) => cell.action(),
}
}
#[must_use]
pub fn raw_text(&self) -> String {
let raw = match self {
Self::Message(cell) => cell.source().to_owned(),
Self::Reasoning(cell) => cell.source().to_owned(),
Self::Plan(cell) => {
raw_plan_text(cell.title(), cell.source(), cell.steps(), cell.note())
}
Self::Lifecycle(cell) => {
let mut raw = cell.action().to_owned();
if let Some(target) = cell.target() {
raw.push(' ');
raw.push_str(target);
}
append_raw_details(&mut raw, cell.details());
raw
}
Self::Result(cell) => cell.content().to_owned(),
Self::Sources(cell) => {
let mut raw = String::new();
for source in cell.sources() {
if !raw.is_empty() {
raw.push('\n');
}
write!(raw, "- {} <{}>", source.label(), source.url())
.expect("writing to a String cannot fail");
}
raw
}
Self::Diff(cell) => code_change_raw_text(cell.change()),
Self::QueuedInput(cell) => cell.preview().to_owned(),
Self::Notice(cell) => {
let mut raw = cell.message().to_owned();
if let Some(hint) = cell.hint() {
raw.push('\n');
raw.push_str(hint);
}
raw
}
Self::Decision(cell) => {
let mut raw = cell.action().to_owned();
if !cell.subject().is_empty() {
raw.push(' ');
raw.push_str(cell.subject());
}
append_raw_details(&mut raw, cell.details());
raw
}
};
terminal_safe_text(&raw)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CellLane {
History,
Active,
Notifications,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum MessageCellFacet {
Content,
Sources,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ToolCellFacet {
Call,
Decision,
Result,
Diff,
Sources,
ApprovalPreview,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum StreamCellFacet {
Message,
Reasoning,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CellId {
Message {
message_id: MessageId,
block_index: u16,
facet: MessageCellFacet,
},
Tool {
tool_call_id: ToolCallId,
facet: ToolCellFacet,
part_index: u16,
},
Stream {
message_id: MessageId,
block_index: u16,
facet: StreamCellFacet,
},
Queue {
kind: QueuedInputKind,
index: u16,
},
McpHealth {
index: u16,
},
Notification {
index: u16,
},
Synthetic {
lane: CellLane,
index: u16,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CellNode {
id: CellId,
content: CellContent,
tool_call_id: Option<ToolCallId>,
}
impl CellNode {
pub(crate) const fn new(
id: CellId,
content: CellContent,
tool_call_id: Option<ToolCallId>,
) -> Self {
Self {
id,
content,
tool_call_id,
}
}
#[must_use]
pub const fn id(&self) -> CellId {
self.id
}
#[must_use]
pub const fn content(&self) -> &CellContent {
&self.content
}
#[must_use]
pub fn text(&self) -> &str {
self.content.text()
}
#[must_use]
pub fn raw_text(&self) -> String {
self.content.raw_text()
}
#[must_use]
pub const fn tool_call_id(&self) -> Option<ToolCallId> {
self.tool_call_id
}
}