Newer
Older
navi-1 / clients / terminal / tui / renderers / tool.py
"""Renderers for tool_started and tool_call events."""

from __future__ import annotations

import json

from rich.console import RenderableType
from rich.json import JSON as RichJSON
from rich.box import ROUNDED
from rich.panel import Panel
from rich.text import Text

from .base import ContentRenderer


class ToolStartedRenderer(ContentRenderer):
    """Render a tool call start marker."""

    def accepts(self, msg: dict) -> bool:
        return msg.get("type") == "tool_started"

    def render(self, msg: dict) -> RenderableType:
        tool = msg.get("tool", "?")
        args = msg.get("args") or {}
        title = f"→ {tool}"
        if args:
            try:
                body = RichJSON(json.dumps(args))
            except Exception:
                body = Text(str(args), style="bright_black")
        else:
            body = Text("")
        return Panel(
            body,
            title=title,
            title_align="left",
            border_style="yellow",
            box=ROUNDED,
        )


class ToolResultRenderer(ContentRenderer):
    """Render a tool call result."""

    def accepts(self, msg: dict) -> bool:
        return msg.get("type") == "tool_call"

    def render(self, msg: dict) -> RenderableType:
        tool = msg.get("tool", "?")
        success = msg.get("success", True)
        result = msg.get("result")
        color = "green" if success else "red"
        title = f"← {tool} {'✓' if success else '✗'}"
        body = Text(str(result) if result is not None else "", style="bright_black")
        return Panel(
            body,
            title=title,
            title_align="left",
            border_style=color,
            box=ROUNDED,
        )