44"""
55
66import json
7+ import re
78from rich .console import Console , Group
89from rich .panel import Panel
910from rich .syntax import Syntax
1011from rich .text import Text
1112from typing import Any
13+ from rich .markdown import Markdown
1214
1315
1416class ToolDisplayManager :
@@ -76,21 +78,26 @@ def display_tool_response(self, tool_name: str, tool_args: Any, tool_response: s
7678
7779 args_display = self ._format_json (tool_args )
7880
79- # Try to format response as JSON if possible, otherwise display as text
81+ # Try to format response as JSON if possible, otherwise check for markdown patterns
8082 try :
8183 response_data = json .loads (tool_response )
8284 response_display = self ._format_json (response_data )
83-
84- # Both args and response are formatted - create layout with syntax highlighting
8585 header_text = Text .from_markup ("[bold]Arguments:[/bold]\n \n " )
8686 response_header_text = Text .from_markup ("\n [bold]Response:[/bold]\n \n " )
8787 panel_renderable = Group (header_text , args_display , response_header_text , response_display )
8888
8989 except (json .JSONDecodeError , TypeError , ValueError ):
90- # Response is not JSON - display as text
90+ # Response is not JSON - check if it has enough markdown patterns
91+ markdown_count = self ._count_markdown_patterns (tool_response )
92+ if markdown_count > 7 : # Arbitrary threshold for markdown patterns
93+ response_display = Markdown (tool_response )
94+ else :
95+ # Not enough markdown patterns - use plain text
96+ response_display = Text (tool_response , style = "white" )
97+
9198 header_text = Text .from_markup ("[bold]Arguments:[/bold]\n \n " )
92- response_text = Text .from_markup (f "\n [bold]Response:[/bold]\n \n [white] { tool_response } [/white] " )
93- panel_renderable = Group (header_text , args_display , response_text )
99+ response_header_text = Text .from_markup ("\n [bold]Response:[/bold]\n \n " )
100+ panel_renderable = Group (header_text , args_display , response_header_text , response_display )
94101
95102 self .console .print () # Add a blank line before the panel
96103 self .console .print (Panel (
@@ -101,3 +108,33 @@ def display_tool_response(self, tool_name: str, tool_args: Any, tool_response: s
101108 padding = (1 , 2 )
102109 ))
103110 self .console .print () # Add a blank line after the panel
111+
112+ def _count_markdown_patterns (self , text : str ) -> int :
113+ """Count markdown patterns in text
114+
115+ Args:
116+ text: The text to check for markdown patterns
117+
118+ Returns:
119+ Number of markdown patterns found
120+ """
121+ # Common markdown patterns
122+ patterns = [
123+ r'```\w*' , # Code blocks with language
124+ r'```' , # Code blocks without language
125+ r'^#{1,6}\s+' , # Headers (# ## ### etc.)
126+ r'^\s*[-*+]\s+' , # Unordered lists
127+ r'^\s*\d+\.\s+' , # Ordered lists
128+ r'\*\*.*?\*\*' , # Bold text
129+ r'\*.*?\*' , # Italic text
130+ r'`.*?`' , # Inline code
131+ r'^\s*>\s+' , # Blockquotes
132+ r'\[.*?\]\(.*?\)' , # Links
133+ ]
134+
135+ count = 0
136+ for pattern in patterns :
137+ matches = re .findall (pattern , text , re .MULTILINE )
138+ count += len (matches )
139+
140+ return count
0 commit comments