@@ -229,7 +229,6 @@ async def run_chat(
229229 config_dir : Path | None = None ,
230230 deps : AgentDepsT = None ,
231231 message_history : list [ModelMessage ] | None = None ,
232- show_tool_calls : bool = False ,
233232) -> int :
234233 prompt_history_path = (config_dir or PYDANTIC_AI_HOME ) / PROMPT_HISTORY_FILENAME
235234 prompt_history_path .parent .mkdir (parents = True , exist_ok = True )
@@ -256,7 +255,7 @@ async def run_chat(
256255 return exit_value
257256 else :
258257 try :
259- messages = await ask_agent (agent , text , stream , console , code_theme , deps , messages , show_tool_calls )
258+ messages = await ask_agent (agent , text , stream , console , code_theme , deps , messages )
260259 except CancelledError : # pragma: no cover
261260 console .print ('[dim]Interrupted[/dim]' )
262261 except Exception as e : # pragma: no cover
@@ -274,9 +273,12 @@ async def ask_agent(
274273 code_theme : str ,
275274 deps : AgentDepsT = None ,
276275 messages : list [ModelMessage ] | None = None ,
277- show_tool_calls : bool = False ,
278276) -> list [ModelMessage ]:
279- status = Status ('[dim]Working on it…[/dim]' , console = console )
277+ MODEL_CALL_STATUS_MSG = '[dim]Calling model…[/dim]'
278+ TOOL_EXECUTION_STATUS_MSG = '[dim]Executing tools…[/dim]'
279+ MAX_TOOL_CALL_RESULT_LEN = 100
280+ MAX_TOOL_CALL_ID_LEN = 5
281+ status = Status (MODEL_CALL_STATUS_MSG , console = console )
280282
281283 if not stream :
282284 with status :
@@ -287,26 +289,40 @@ async def ask_agent(
287289
288290 with status , ExitStack () as stack :
289291 async with agent .iter (prompt , message_history = messages , deps = deps ) as agent_run :
290- live = Live ( '' , refresh_per_second = 15 , console = console , vertical_overflow = 'ellipsis' )
292+ final_output_live = None
291293 async for node in agent_run :
292294 if Agent .is_model_request_node (node ):
295+ status .update (MODEL_CALL_STATUS_MSG )
293296 async with node .stream (agent_run .ctx ) as handle_stream :
294297 status .stop () # stopping multiple times is idempotent
295- stack .enter_context (live ) # entering multiple times is idempotent
296298
297299 async for content in handle_stream .stream_output (debounce_by = None ):
298- live .update (Markdown (str (content ), code_theme = code_theme ))
299- elif show_tool_calls and Agent .is_call_tools_node (node ):
300+ if final_output_live is None :
301+ final_output_live = Live (
302+ '' , refresh_per_second = 15 , console = console , vertical_overflow = 'ellipsis'
303+ )
304+ stack .enter_context (final_output_live ) # entering multiple times is idempotent
305+ final_output_live .update (Markdown (str (content ), code_theme = code_theme ))
306+ elif Agent .is_call_tools_node (node ):
307+ status .update (TOOL_EXECUTION_STATUS_MSG )
300308 async with node .stream (agent_run .ctx ) as handle_stream :
301309 async for event in handle_stream :
302310 if isinstance (event , FunctionToolCallEvent ):
311+ status .stop () # stopping multiple times is idempotent
303312 console .print (
304- Markdown (f'[Tool] { event .part .tool_name !r} called with args={ event .part .args } ' )
313+ Markdown (
314+ f'[Tool] { event .part .tool_name !r} [{ event .part .tool_call_id [- 5 :]} ] called with args={ event .part .args } '
315+ )
305316 )
317+ status .start ()
306318 elif isinstance (event , FunctionToolResultEvent ):
319+ status .stop () # stopping multiple times is idempotent
307320 console .print (
308- Markdown (f'[Tool] { event .result .tool_name !r} returned => { event .result .content } ' )
321+ Markdown (
322+ f'[Tool] { event .result .tool_name !r} [{ event .result .tool_call_id [- MAX_TOOL_CALL_ID_LEN :]} ] returned => { event .result .content if len (event .result .content ) < MAX_TOOL_CALL_RESULT_LEN else str (event .result .content [:MAX_TOOL_CALL_RESULT_LEN ]) + "..." } '
323+ )
309324 )
325+ status .start ()
310326
311327 assert agent_run .result is not None
312328 return agent_run .result .all_messages ()
0 commit comments