Skip to content

Commit 7d54779

Browse files
authored
Do not use rich in tiny-agents CLI (#3573)
1 parent 34b48a6 commit 7d54779

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

src/huggingface_hub/inference/_mcp/cli.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from typing import Optional
66

77
import typer
8-
from rich import print
98

9+
from ...utils import ANSI
1010
from ._cli_hacks import _async_prompt, _patch_anyio_open_process
1111
from .agent import Agent
1212
from .utils import _load_agent_config
@@ -55,10 +55,10 @@ def _sigint_handler() -> None:
5555
if first_sigint:
5656
first_sigint = False
5757
abort_event.set()
58-
print("\n[red]Interrupted. Press Ctrl+C again to quit.[/red]", flush=True)
58+
print(ANSI.red("\nInterrupted. Press Ctrl+C again to quit."), flush=True)
5959
return
6060

61-
print("\n[red]Exiting...[/red]", flush=True)
61+
print(ANSI.red("\nExiting..."), flush=True)
6262
exit_event.set()
6363

6464
try:
@@ -75,8 +75,12 @@ def _sigint_handler() -> None:
7575

7676
if len(inputs) > 0:
7777
print(
78-
"[bold blue]Some initial inputs are required by the agent. "
79-
"Please provide a value or leave empty to load from env.[/bold blue]"
78+
ANSI.bold(
79+
ANSI.blue(
80+
"Some initial inputs are required by the agent. "
81+
"Please provide a value or leave empty to load from env."
82+
)
83+
)
8084
)
8185
for input_item in inputs:
8286
input_id = input_item["id"]
@@ -98,15 +102,17 @@ def _sigint_handler() -> None:
98102

99103
if not input_usages:
100104
print(
101-
f"[yellow]Input '{input_id}' defined in config but not used by any server or as an API key."
102-
" Skipping.[/yellow]"
105+
ANSI.yellow(
106+
f"Input '{input_id}' defined in config but not used by any server or as an API key."
107+
" Skipping."
108+
)
103109
)
104110
continue
105111

106112
# Prompt user for input
107113
env_variable_key = input_id.replace("-", "_").upper()
108114
print(
109-
f"[blue]{input_id}[/blue]: {description}. (default: load from {env_variable_key}).",
115+
ANSI.blue(f"{input_id}") + f": {description}. (default: load from {env_variable_key}).",
110116
end=" ",
111117
)
112118
user_input = (await _async_prompt(exit_event=exit_event)).strip()
@@ -118,10 +124,12 @@ def _sigint_handler() -> None:
118124
if not final_value:
119125
final_value = os.getenv(env_variable_key, "")
120126
if final_value:
121-
print(f"[green]Value successfully loaded from '{env_variable_key}'[/green]")
127+
print(ANSI.green(f"Value successfully loaded from '{env_variable_key}'"))
122128
else:
123129
print(
124-
f"[yellow]No value found for '{env_variable_key}' in environment variables. Continuing.[/yellow]"
130+
ANSI.yellow(
131+
f"No value found for '{env_variable_key}' in environment variables. Continuing."
132+
)
125133
)
126134
resolved_inputs[input_id] = final_value
127135

@@ -150,9 +158,9 @@ def _sigint_handler() -> None:
150158
prompt=prompt,
151159
) as agent:
152160
await agent.load_tools()
153-
print(f"[bold blue]Agent loaded with {len(agent.available_tools)} tools:[/bold blue]")
161+
print(ANSI.bold(ANSI.blue("Agent loaded with {} tools:".format(len(agent.available_tools)))))
154162
for t in agent.available_tools:
155-
print(f"[blue]{t.function.name}[/blue]")
163+
print(ANSI.blue(f"{t.function.name}"))
156164

157165
while True:
158166
abort_event.clear()
@@ -165,13 +173,13 @@ def _sigint_handler() -> None:
165173
user_input = await _async_prompt(exit_event=exit_event)
166174
first_sigint = True
167175
except EOFError:
168-
print("\n[red]EOF received, exiting.[/red]", flush=True)
176+
print(ANSI.red("\nEOF received, exiting."), flush=True)
169177
break
170178
except KeyboardInterrupt:
171179
if not first_sigint and abort_event.is_set():
172180
continue
173181
else:
174-
print("\n[red]Keyboard interrupt during input processing.[/red]", flush=True)
182+
print(ANSI.red("\nKeyboard interrupt during input processing."), flush=True)
175183
break
176184

177185
try:
@@ -195,20 +203,20 @@ def _sigint_handler() -> None:
195203
print(f"{call.function.arguments}", end="")
196204
else:
197205
print(
198-
f"\n\n[green]Tool[{chunk.name}] {chunk.tool_call_id}\n{chunk.content}[/green]\n",
206+
ANSI.green(f"\n\nTool[{chunk.name}] {chunk.tool_call_id}\n{chunk.content}\n"),
199207
flush=True,
200208
)
201209

202210
print()
203211

204212
except Exception as e:
205213
tb_str = traceback.format_exc()
206-
print(f"\n[bold red]Error during agent run: {e}\n{tb_str}[/bold red]", flush=True)
214+
print(ANSI.red(f"\nError during agent run: {e}\n{tb_str}"), flush=True)
207215
first_sigint = True # Allow graceful interrupt for the next command
208216

209217
except Exception as e:
210218
tb_str = traceback.format_exc()
211-
print(f"\n[bold red]An unexpected error occurred: {e}\n{tb_str}[/bold red]", flush=True)
219+
print(ANSI.red(f"\nAn unexpected error occurred: {e}\n{tb_str}"), flush=True)
212220
raise e
213221

214222
finally:
@@ -236,10 +244,10 @@ def run(
236244
try:
237245
asyncio.run(run_agent(path))
238246
except KeyboardInterrupt:
239-
print("\n[red]Application terminated by KeyboardInterrupt.[/red]", flush=True)
247+
print(ANSI.red("\nApplication terminated by KeyboardInterrupt."), flush=True)
240248
raise typer.Exit(code=130)
241249
except Exception as e:
242-
print(f"\n[bold red]An unexpected error occurred: {e}[/bold red]", flush=True)
250+
print(ANSI.red(f"\nAn unexpected error occurred: {e}"), flush=True)
243251
raise e
244252

245253

src/huggingface_hub/utils/_terminal.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ class ANSI:
2222
Helper for en.wikipedia.org/wiki/ANSI_escape_code
2323
"""
2424

25+
_blue = "\u001b[34m"
2526
_bold = "\u001b[1m"
2627
_gray = "\u001b[90m"
28+
_green = "\u001b[32m"
2729
_red = "\u001b[31m"
2830
_reset = "\u001b[0m"
2931
_yellow = "\u001b[33m"
3032

33+
@classmethod
34+
def blue(cls, s: str) -> str:
35+
return cls._format(s, cls._blue)
36+
3137
@classmethod
3238
def bold(cls, s: str) -> str:
3339
return cls._format(s, cls._bold)
@@ -36,6 +42,10 @@ def bold(cls, s: str) -> str:
3642
def gray(cls, s: str) -> str:
3743
return cls._format(s, cls._gray)
3844

45+
@classmethod
46+
def green(cls, s: str) -> str:
47+
return cls._format(s, cls._green)
48+
3949
@classmethod
4050
def red(cls, s: str) -> str:
4151
return cls._format(s, cls._bold + cls._red)

0 commit comments

Comments
 (0)