Print an event to stdout in a user-friendly format. Args: event: The event to print. verbose: If True, shows detailed tool calls and responses. If False, shows only text responses for cleaner output.
(event: Event, *, verbose: bool = False)
| 39 | |
| 40 | |
| 41 | def print_event(event: Event, *, verbose: bool = False) -> None: |
| 42 | """Print an event to stdout in a user-friendly format. |
| 43 | |
| 44 | Args: |
| 45 | event: The event to print. |
| 46 | verbose: If True, shows detailed tool calls and responses. If False, |
| 47 | shows only text responses for cleaner output. |
| 48 | """ |
| 49 | if not event.content or not event.content.parts: |
| 50 | return |
| 51 | |
| 52 | # Collect consecutive text parts to avoid repeating author prefix |
| 53 | text_buffer: list[str] = [] |
| 54 | |
| 55 | def flush_text() -> None: |
| 56 | """Flush accumulated text parts as a single output.""" |
| 57 | if text_buffer: |
| 58 | combined_text = ''.join(text_buffer) |
| 59 | print(f'{event.author} > {combined_text}') |
| 60 | text_buffer.clear() |
| 61 | |
| 62 | for part in event.content.parts: |
| 63 | # Text parts are always shown regardless of verbose setting |
| 64 | # because they contain the actual agent responses users expect |
| 65 | if part.text: |
| 66 | text_buffer.append(part.text) |
| 67 | else: |
| 68 | # Flush any accumulated text before handling non-text parts |
| 69 | flush_text() |
| 70 | |
| 71 | # Non-text parts (tool calls, code, etc.) are hidden by default |
| 72 | # to reduce clutter and show only what matters: the final results |
| 73 | if verbose: |
| 74 | # Tool invocations show the behind-the-scenes processing |
| 75 | if part.function_call: |
| 76 | print( |
| 77 | f'{event.author} > [Calling tool:' |
| 78 | f' {part.function_call.name}(' |
| 79 | f'{_truncate(str(part.function_call.args), _ARGS_MAX_LEN)})]' |
| 80 | ) |
| 81 | # Handle function response parts (tool results) |
| 82 | elif part.function_response: |
| 83 | print( |
| 84 | f'{event.author} > [Tool result:' |
| 85 | f' {_truncate(str(part.function_response.response), _RESPONSE_MAX_LEN)}]' |
| 86 | ) |
| 87 | # Handle executable code parts |
| 88 | elif part.executable_code: |
| 89 | lang = part.executable_code.language or 'code' |
| 90 | print(f'{event.author} > [Executing {lang} code...]') |
| 91 | # Handle code execution result parts |
| 92 | elif part.code_execution_result: |
| 93 | output = part.code_execution_result.output or 'result' |
| 94 | print( |
| 95 | f'{event.author} > [Code output:' |
| 96 | f' {_truncate(str(output), _CODE_OUTPUT_MAX_LEN)}]' |
| 97 | ) |
| 98 | # Handle inline data (images, files) |
no test coverage detected