1use std::fmt::Write as _;
2
3use tea_protocol::{CodeChange, MessageId, ToolCallId};
4
5use super::{
6 DecisionStatus, LifecycleStatus, NoticeSeverity, OutputFormat, PlanStep, QueuedInputKind,
7 TimelineDetail, TimelineSource, append_raw_details, bounded_terminal_text,
8 code_change_raw_text, queue_preview, raw_plan_text, terminal_safe_text,
9};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum MessageAuthor {
14 User,
16 Assistant,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum LifecycleKind {
23 ToolCall,
25 HostedTool,
27 RunActivity,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum NoticeKind {
34 General,
36 McpHealth,
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct MessageCell {
43 author: MessageAuthor,
44 source: String,
45 format: OutputFormat,
46}
47
48impl MessageCell {
49 pub(crate) fn new(author: MessageAuthor, source: &str, format: OutputFormat) -> Self {
50 Self {
51 author,
52 source: bounded_terminal_text(source),
53 format,
54 }
55 }
56
57 #[must_use]
59 pub const fn author(&self) -> MessageAuthor {
60 self.author
61 }
62
63 #[must_use]
65 pub fn source(&self) -> &str {
66 &self.source
67 }
68
69 #[must_use]
71 pub const fn format(&self) -> OutputFormat {
72 self.format
73 }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
78pub struct ReasoningCell {
79 source: String,
80 collapsed: bool,
81}
82
83impl ReasoningCell {
84 pub(crate) fn new(source: &str, collapsed: bool) -> Self {
85 Self {
86 source: bounded_terminal_text(source),
87 collapsed,
88 }
89 }
90
91 #[must_use]
93 pub fn source(&self) -> &str {
94 &self.source
95 }
96
97 #[must_use]
99 pub const fn collapsed(&self) -> bool {
100 self.collapsed
101 }
102}
103
104#[derive(Debug, Clone, PartialEq, Eq)]
106pub struct PlanCell {
107 title: String,
108 source: Option<String>,
109 steps: Vec<PlanStep>,
110 note: Option<String>,
111}
112
113impl PlanCell {
114 #[cfg_attr(not(test), allow(dead_code))]
115 pub(crate) fn new(
116 title: &str,
117 source: Option<&str>,
118 steps: Vec<PlanStep>,
119 note: Option<&str>,
120 ) -> Self {
121 Self {
122 title: bounded_terminal_text(title),
123 source: source.map(bounded_terminal_text),
124 steps,
125 note: note.map(bounded_terminal_text),
126 }
127 }
128
129 #[must_use]
131 pub fn title(&self) -> &str {
132 &self.title
133 }
134
135 #[must_use]
137 pub fn source(&self) -> Option<&str> {
138 self.source.as_deref()
139 }
140
141 #[must_use]
143 pub fn steps(&self) -> &[PlanStep] {
144 &self.steps
145 }
146
147 #[must_use]
149 pub fn note(&self) -> Option<&str> {
150 self.note.as_deref()
151 }
152}
153
154#[derive(Debug, Clone, PartialEq, Eq)]
156pub struct LifecycleCell {
157 kind: LifecycleKind,
158 action: String,
159 target: Option<String>,
160 status: LifecycleStatus,
161 details: Vec<TimelineDetail>,
162 expanded: bool,
163 tick: u64,
164}
165
166impl LifecycleCell {
167 #[allow(clippy::too_many_arguments)]
168 pub(crate) fn new(
169 kind: LifecycleKind,
170 action: &str,
171 target: Option<&str>,
172 status: LifecycleStatus,
173 details: Vec<TimelineDetail>,
174 expanded: bool,
175 tick: u64,
176 ) -> Self {
177 Self {
178 kind,
179 action: bounded_terminal_text(action),
180 target: target.map(bounded_terminal_text),
181 status,
182 details,
183 expanded,
184 tick,
185 }
186 }
187
188 #[must_use]
190 pub const fn kind(&self) -> LifecycleKind {
191 self.kind
192 }
193
194 #[must_use]
196 pub fn action(&self) -> &str {
197 &self.action
198 }
199
200 #[must_use]
202 pub fn target(&self) -> Option<&str> {
203 self.target.as_deref()
204 }
205
206 #[must_use]
208 pub const fn status(&self) -> LifecycleStatus {
209 self.status
210 }
211
212 #[must_use]
214 pub fn details(&self) -> &[TimelineDetail] {
215 &self.details
216 }
217
218 #[must_use]
220 pub const fn expanded(&self) -> bool {
221 self.expanded
222 }
223
224 #[must_use]
226 pub const fn tick(&self) -> u64 {
227 self.tick
228 }
229}
230
231#[derive(Debug, Clone, PartialEq, Eq)]
233pub struct ResultCell {
234 action: String,
235 source_name: Option<String>,
236 content: String,
237 format: OutputFormat,
238 is_error: bool,
239}
240
241impl ResultCell {
242 pub(crate) fn new(
243 action: &str,
244 source_name: Option<&str>,
245 content: &str,
246 format: OutputFormat,
247 is_error: bool,
248 ) -> Self {
249 Self {
250 action: bounded_terminal_text(action),
251 source_name: source_name.map(bounded_terminal_text),
252 content: bounded_terminal_text(content),
253 format,
254 is_error,
255 }
256 }
257
258 #[must_use]
260 pub fn action(&self) -> &str {
261 &self.action
262 }
263
264 #[must_use]
266 pub fn source_name(&self) -> Option<&str> {
267 self.source_name.as_deref()
268 }
269
270 #[must_use]
272 pub fn content(&self) -> &str {
273 &self.content
274 }
275
276 #[must_use]
278 pub const fn format(&self) -> OutputFormat {
279 self.format
280 }
281
282 #[must_use]
284 pub const fn is_error(&self) -> bool {
285 self.is_error
286 }
287}
288
289#[derive(Debug, Clone, PartialEq, Eq)]
291pub struct SourcesCell {
292 sources: Vec<TimelineSource>,
293}
294
295impl SourcesCell {
296 pub(crate) const fn new(sources: Vec<TimelineSource>) -> Self {
297 Self { sources }
298 }
299
300 #[must_use]
302 pub fn sources(&self) -> &[TimelineSource] {
303 &self.sources
304 }
305}
306
307#[derive(Debug, Clone, PartialEq, Eq)]
309pub struct DiffCell {
310 action: String,
311 change: CodeChange,
312}
313
314impl DiffCell {
315 pub(crate) fn new(action: &str, change: CodeChange) -> Self {
316 Self {
317 action: bounded_terminal_text(action),
318 change,
319 }
320 }
321
322 #[must_use]
324 pub fn action(&self) -> &str {
325 &self.action
326 }
327
328 #[must_use]
330 pub const fn change(&self) -> &CodeChange {
331 &self.change
332 }
333}
334
335#[derive(Debug, Clone, PartialEq, Eq)]
337pub struct QueuedInputCell {
338 kind: QueuedInputKind,
339 preview: String,
340}
341
342impl QueuedInputCell {
343 pub(crate) fn new(kind: QueuedInputKind, preview: &str) -> Self {
344 Self {
345 kind,
346 preview: queue_preview(preview),
347 }
348 }
349
350 #[must_use]
352 pub const fn kind(&self) -> QueuedInputKind {
353 self.kind
354 }
355
356 #[must_use]
358 pub fn preview(&self) -> &str {
359 &self.preview
360 }
361}
362
363#[derive(Debug, Clone, PartialEq, Eq)]
365pub struct NoticeCell {
366 kind: NoticeKind,
367 severity: NoticeSeverity,
368 message: String,
369 hint: Option<String>,
370}
371
372impl NoticeCell {
373 pub(crate) fn new(
374 kind: NoticeKind,
375 severity: NoticeSeverity,
376 message: &str,
377 hint: Option<&str>,
378 ) -> Self {
379 Self {
380 kind,
381 severity,
382 message: bounded_terminal_text(message),
383 hint: hint.map(bounded_terminal_text),
384 }
385 }
386
387 #[must_use]
389 pub const fn kind(&self) -> NoticeKind {
390 self.kind
391 }
392
393 #[must_use]
395 pub const fn severity(&self) -> NoticeSeverity {
396 self.severity
397 }
398
399 #[must_use]
401 pub fn message(&self) -> &str {
402 &self.message
403 }
404
405 #[must_use]
407 pub fn hint(&self) -> Option<&str> {
408 self.hint.as_deref()
409 }
410}
411
412#[derive(Debug, Clone, PartialEq, Eq)]
414pub struct DecisionCell {
415 action: String,
416 subject: String,
417 status: DecisionStatus,
418 details: Vec<TimelineDetail>,
419}
420
421impl DecisionCell {
422 pub(crate) fn new(
423 action: &str,
424 subject: &str,
425 status: DecisionStatus,
426 details: Vec<TimelineDetail>,
427 ) -> Self {
428 Self {
429 action: bounded_terminal_text(action),
430 subject: bounded_terminal_text(subject),
431 status,
432 details,
433 }
434 }
435
436 #[must_use]
438 pub fn action(&self) -> &str {
439 &self.action
440 }
441
442 #[must_use]
444 pub fn subject(&self) -> &str {
445 &self.subject
446 }
447
448 #[must_use]
450 pub const fn status(&self) -> DecisionStatus {
451 self.status
452 }
453
454 #[must_use]
456 pub fn details(&self) -> &[TimelineDetail] {
457 &self.details
458 }
459}
460
461#[derive(Debug, Clone, PartialEq, Eq)]
463pub enum CellContent {
464 Message(MessageCell),
466 Reasoning(ReasoningCell),
468 Plan(PlanCell),
470 Lifecycle(LifecycleCell),
472 Result(ResultCell),
474 Sources(SourcesCell),
476 Diff(DiffCell),
478 QueuedInput(QueuedInputCell),
480 Notice(NoticeCell),
482 Decision(DecisionCell),
484}
485
486impl CellContent {
487 #[must_use]
489 pub(crate) const fn is_live_timeline_visible(&self) -> bool {
490 !matches!(self, Self::Decision(_))
491 }
492
493 #[must_use]
495 pub fn text(&self) -> &str {
496 match self {
497 Self::Message(cell) => cell.source(),
498 Self::Reasoning(cell) => cell.source(),
499 Self::Plan(cell) => cell.source().unwrap_or_else(|| cell.title()),
500 Self::Lifecycle(cell) => cell.action(),
501 Self::Result(cell) => cell.action(),
502 Self::Sources(_) => "Sources",
503 Self::Diff(cell) => cell.action(),
504 Self::QueuedInput(cell) => cell.preview(),
505 Self::Notice(cell) => cell.message(),
506 Self::Decision(cell) => cell.action(),
507 }
508 }
509
510 #[must_use]
512 pub fn raw_text(&self) -> String {
513 let raw = match self {
514 Self::Message(cell) => cell.source().to_owned(),
515 Self::Reasoning(cell) => cell.source().to_owned(),
516 Self::Plan(cell) => {
517 raw_plan_text(cell.title(), cell.source(), cell.steps(), cell.note())
518 }
519 Self::Lifecycle(cell) => {
520 let mut raw = cell.action().to_owned();
521 if let Some(target) = cell.target() {
522 raw.push(' ');
523 raw.push_str(target);
524 }
525 append_raw_details(&mut raw, cell.details());
526 raw
527 }
528 Self::Result(cell) => cell.content().to_owned(),
529 Self::Sources(cell) => {
530 let mut raw = String::new();
531 for source in cell.sources() {
532 if !raw.is_empty() {
533 raw.push('\n');
534 }
535 write!(raw, "- {} <{}>", source.label(), source.url())
536 .expect("writing to a String cannot fail");
537 }
538 raw
539 }
540 Self::Diff(cell) => code_change_raw_text(cell.change()),
541 Self::QueuedInput(cell) => cell.preview().to_owned(),
542 Self::Notice(cell) => {
543 let mut raw = cell.message().to_owned();
544 if let Some(hint) = cell.hint() {
545 raw.push('\n');
546 raw.push_str(hint);
547 }
548 raw
549 }
550 Self::Decision(cell) => {
551 let mut raw = cell.action().to_owned();
552 if !cell.subject().is_empty() {
553 raw.push(' ');
554 raw.push_str(cell.subject());
555 }
556 append_raw_details(&mut raw, cell.details());
557 raw
558 }
559 };
560 terminal_safe_text(&raw)
561 }
562}
563
564#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
566pub enum CellLane {
567 History,
569 Active,
571 Notifications,
573}
574
575#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
577pub enum MessageCellFacet {
578 Content,
580 Sources,
582}
583
584#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
586pub enum ToolCellFacet {
587 Call,
589 Decision,
591 Result,
593 Diff,
595 Sources,
597 ApprovalPreview,
599}
600
601#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
603pub enum StreamCellFacet {
604 Message,
606 Reasoning,
608}
609
610#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
612pub enum CellId {
613 Message {
615 message_id: MessageId,
617 block_index: u16,
619 facet: MessageCellFacet,
621 },
622 Tool {
624 tool_call_id: ToolCallId,
626 facet: ToolCellFacet,
628 part_index: u16,
630 },
631 Stream {
633 message_id: MessageId,
635 block_index: u16,
637 facet: StreamCellFacet,
639 },
640 Queue {
642 kind: QueuedInputKind,
644 index: u16,
646 },
647 McpHealth {
649 index: u16,
651 },
652 Notification {
654 index: u16,
656 },
657 Synthetic {
659 lane: CellLane,
661 index: u16,
663 },
664}
665
666#[derive(Debug, Clone, PartialEq, Eq)]
668pub struct CellNode {
669 id: CellId,
670 content: CellContent,
671 tool_call_id: Option<ToolCallId>,
672}
673
674impl CellNode {
675 pub(crate) const fn new(
676 id: CellId,
677 content: CellContent,
678 tool_call_id: Option<ToolCallId>,
679 ) -> Self {
680 Self {
681 id,
682 content,
683 tool_call_id,
684 }
685 }
686
687 #[must_use]
689 pub const fn id(&self) -> CellId {
690 self.id
691 }
692
693 #[must_use]
695 pub const fn content(&self) -> &CellContent {
696 &self.content
697 }
698
699 #[must_use]
701 pub fn text(&self) -> &str {
702 self.content.text()
703 }
704
705 #[must_use]
707 pub fn raw_text(&self) -> String {
708 self.content.raw_text()
709 }
710
711 #[must_use]
713 pub const fn tool_call_id(&self) -> Option<ToolCallId> {
714 self.tool_call_id
715 }
716}