class TurnItemWidget(Vertical):
"""Renders one :class:`TurnItem` and updates in place while streaming."""
DEFAULT_CSS = """
TurnItemWidget {
height: auto;
margin: 0 0 1 0;
padding: 0 1;
}
TurnItemWidget.-user {
border-left: thick $primary;
background: $panel;
padding: 0 1 0 2;
}
TurnItemWidget.-assistant {
border-left: thick $accent-darken-1;
padding: 0 1 0 2;
}
TurnItemWidget.-approval {
border: round $warning;
background: $panel;
padding: 1 2;
}
TurnItemWidget.-error {
border-left: thick $error;
background: $panel;
padding: 0 1 0 2;
}
TurnItemWidget .item-label {
color: $text-muted;
text-style: bold;
margin-bottom: 0;
}
TurnItemWidget .approval-prompt {
color: $warning;
text-style: bold;
margin: 1 0;
}
TurnItemWidget .approval-reason {
color: $text-muted;
margin-bottom: 1;
}
TurnItemWidget .approval-buttons {
height: auto;
margin-top: 1;
}
TurnItemWidget .approval-buttons Button {
margin-right: 1;
height: 1;
border: none;
min-width: 10;
}
TurnItemWidget Collapsible {
padding: 0;
border: none;
background: $surface;
}
TurnItemWidget .tool-body {
color: $text-muted;
padding: 0 1;
}
"""
def __init__(self, item: TurnItem) -> None:
super().__init__()
self.item = item
self._last_text: str = ""
self._apply_classes()
def _apply_classes(self) -> None:
kind = self.item.type
self.set_class(kind == "user_message", "-user")
self.set_class(kind in {"assistant_message", "proposed_plan"}, "-assistant")
self.set_class(kind in {"approval_request", "user_input_request"}, "-approval")
self.set_class(kind == "error", "-error")
def compose(self) -> ComposeResult:
"""Build widgets according to `item.type`."""
item = self.item
kind = item.type
if kind == "user_message":
yield Static("You", classes="item-label")
self._last_text = item.text or ""
yield AgentMarkdown(self._last_text, classes="item-md")
return
if kind in {"assistant_message", "proposed_plan"}:
suffix = " · streaming…" if item.streaming else ""
label = "Plan" if kind == "proposed_plan" else "Assistant"
yield Static(f"{label}{suffix}", classes="item-label")
self._last_text = item.text or ""
yield AgentMarkdown(self._last_text, classes="item-md")
return
if kind == "reasoning":
title = "Thinking…" if item.streaming else "Reasoning"
self._last_text = item.text or ""
with Collapsible(title=title, collapsed=not bool(item.streaming)):
yield AgentMarkdown(self._last_text, classes="item-md")
return
if kind in {"approval_request", "user_input_request"}:
yield from self._compose_approval(item)
return
if kind in {"error", "system_notice"}:
label = "Error" if kind == "error" else "Notice"
yield Static(f"{_status_icon(item.status)} {label}", classes="item-label")
yield Static(_item_body(item) or (item.title or ""), classes="tool-body")
return
# Tool / command / file change / search / checkpoint / subagent
heading = f"{_status_icon(item.status)} {item.title or kind.replace('_', ' ')}"
body = _item_body(item)
with Collapsible(title=heading, collapsed=True):
yield Static(body or "(no output)", classes="tool-body")
def _compose_approval(self, item: TurnItem) -> ComposeResult:
status_label = (
"Approval required" if item.status == "waiting" else f"Approval ({item.status})"
)
kind_tag = f" · {item.request_kind}" if item.request_kind else ""
yield Static(f"◆ {status_label}{kind_tag}", classes="item-label")
if item.prompt or item.title:
yield Static(item.prompt or item.title or "", classes="approval-prompt")
if item.reason:
yield Static(item.reason, classes="approval-reason")
if item.status == "waiting" and item.request_id:
options = item.options or []
with Horizontal(classes="approval-buttons"):
if options:
for opt in options:
variant = (
"success"
if opt.decision in {"accept", "acceptForSession", "acceptAlways"}
else "error"
)
yield Button(
opt.label,
variant=variant,
name=opt.decision,
classes="approval-btn",
)
else:
yield Button("Allow", variant="success", name="accept", classes="approval-btn")
yield Button(
"Session",
variant="primary",
name="acceptForSession",
classes="approval-btn",
)
yield Button("Decline", variant="error", name="decline", classes="approval-btn")
async def sync_item(self, item: TurnItem) -> None:
"""Update this widget in place from a newer :class:`TurnItem`."""
prev_status = self.item.status
self.item = item
self._apply_classes()
if item.type in {"user_message", "assistant_message", "proposed_plan"}:
md_list = list(self.query(AgentMarkdown))
label_list = list(self.query(".item-label"))
if label_list and item.type in {"assistant_message", "proposed_plan"}:
base = "Plan" if item.type == "proposed_plan" else "Assistant"
suffix = " · streaming…" if item.streaming else ""
cast("Static", label_list[0]).update(f"{base}{suffix}")
new_text = item.text or ""
if md_list and new_text != self._last_text:
self._last_text = new_text
await md_list[0].update(new_text)
return
if item.type == "reasoning":
md_list = list(self.query(AgentMarkdown))
new_text = item.text or ""
if md_list and new_text != self._last_text:
self._last_text = new_text
await md_list[0].update(new_text)
return
if prev_status != item.status or item.type in {"approval_request", "user_input_request"}:
await self.recompose()
def on_button_pressed(self, event: Button.Pressed) -> None:
"""Forward inline approval button presses."""
if not event.button.has_class("approval-btn"):
return
if not self.item.request_id or not event.button.name:
return
event.stop()
decision = cast("ApprovalDecision", event.button.name)
self.post_message(ApprovalDecisionMade(self.item.thread_id, self.item.request_id, decision))