class ToolCallView(Vertical):
"""A tool call: status, kind, title, locations and content."""
DEFAULT_CSS = """
ToolCallView {
height: auto;
margin: 0 0 1 0;
#tool-body { height: auto; padding: 0 0 0 3; }
&.-collapsed #tool-body { display: none; }
.tool-text { height: auto; max-height: 30; color: $text-muted; }
.tool-markdown { height: auto; margin: 0; padding: 0; background: transparent; }
.tool-locations { color: $text-muted; height: auto; }
}
"""
expanded: reactive[bool] = reactive(True)
class CancelRequested(Message):
"""The user asked to cancel the tool call."""
def __init__(self, tool_call_id: str) -> None:
super().__init__()
self.tool_call_id = tool_call_id
def __init__(self, entry: ToolCallEntry, state: SessionState, diff_mode: DiffViewMode) -> None:
super().__init__()
self.entry = entry
self.state = state
self.diff_mode: DiffViewMode = diff_mode
self._content_key: tuple[int, ...] = ()
self.show_raw = False
self._user_toggled = False
self.set_reactive(ToolCallView.expanded, self._should_expand())
def _should_expand(self) -> bool:
if not self.entry.content:
return True
has_rich = any(isinstance(item, Diff | Terminal) for item in self.entry.content)
return has_rich or self.entry.kind not in COLLAPSED_KINDS
def compose(self) -> ComposeResult:
yield ToolCallHeader(self._header())
with Vertical(id="tool-body"):
yield from self._body()
def on_mount(self) -> None:
self.set_class(not self.expanded, "-collapsed")
def watch_expanded(self, expanded: bool) -> None:
self.set_class(not expanded, "-collapsed")
if self.is_mounted:
self.query_one(ToolCallHeader).update(self._header())
def _header(self) -> Content:
entry = self.entry
arrow = "▼" if self.expanded else "▶"
has_body = bool(entry.content or entry.locations or entry.raw_input is not None)
parts: list[Content | str | tuple[str, str]] = [
(f"{arrow} " if has_body else " ", "$text-muted"),
status_icon(entry.status),
" ",
(tool_icon(entry.kind) + " ", "bold $secondary"),
Content(entry.display_title),
]
if entry.status == "failed":
parts += [" ", pill("failed", "$error-muted", "$text-error")]
elif entry.status == "cancelled":
parts += [" ", pill("cancelled", "$warning-muted", "$text-warning")]
if entry.can_cancel and not entry.finished:
parts += [" ", ("x: cancel", "$text-muted italic")]
return Content.assemble(*parts)
def _body(self) -> list[Widget]:
widgets: list[Widget] = []
entry = self.entry
if entry.locations:
lines = []
for location in entry.locations:
path = relative_display(location.path, self.state.cwd)
line = location.line if is_set(location.line) and location.line else None
lines.append(f"↳ {path}:{line}" if line else f"↳ {path}")
widgets.append(Static(Content("\n".join(lines)), classes="tool-locations"))
widgets.extend(self._content_widget(item) for item in entry.content)
if self.show_raw:
if entry.raw_input is not None:
widgets.append(
Static(Content("input: " + raw_json(entry.raw_input)), classes="tool-text")
)
if entry.raw_output is not None:
widgets.append(
Static(Content("output: " + raw_json(entry.raw_output)), classes="tool-text")
)
return widgets
def _content_widget(self, item: ToolCallContent) -> Widget:
match item:
case ToolContent(content=block):
return self._block_widget(block)
case Diff():
return DiffView(item, self.state.cwd, self.diff_mode)
case Terminal(terminal_id=terminal_id):
terminal = self.state.terminals.get(terminal_id)
if terminal is None:
from acptui.protocol.state import TerminalState
terminal = self.state.terminals.setdefault(
terminal_id, TerminalState(terminal_id)
)
return TerminalView(terminal)
case SessionReference(session_id=session_id):
child = self.state.subagents.get(session_id)
label = child.display_title if child is not None else session_id
return Static(Content(f"↳ session: {label}"), classes="tool-locations")
case _:
return Static(Content(raw_json(item.model_dump())), classes="tool-text")
def _block_widget(self, block: ContentBlock) -> Widget:
match block:
case TextContent(text=text):
return _text_widget(text)
case EmbeddedResource(resource=TextResourceContents(text=text)):
return _text_widget(text)
case _:
return Markdown(content_markdown([block], self.state.cwd), classes="tool-markdown")
async def refresh_entry(self) -> None:
"""Update after the tool call changed (re-renders content only if it changed)."""
self.query_one(ToolCallHeader).update(self._header())
key = (*(id(item) for item in self.entry.content), len(self.entry.locations))
if key != self._content_key:
self._content_key = key
await self.rebuild()
if not self._user_toggled:
self.expanded = self._should_expand()
async def rebuild(self) -> None:
"""Re-create the content widgets."""
body = self.query_one("#tool-body", Vertical)
await body.remove_children()
await body.mount_all(self._body())
def set_diff_mode(self, mode: DiffViewMode) -> None:
"""Switch how diffs are shown."""
self.diff_mode = mode
for view in self.query(DiffView):
view.mode = mode