forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatting.py
More file actions
109 lines (81 loc) · 3.26 KB
/
Copy pathformatting.py
File metadata and controls
109 lines (81 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""
Formatting utilities for A2A messages and conversations.
"""
import json
from typing import Dict, List, Any, Optional
from ..models.message import Message, MessageRole
from ..models.conversation import Conversation
def format_message_as_text(message: Message) -> str:
"""
Format a message as a human-readable text string
Args:
message: The message to format
Returns:
A formatted string representation of the message
"""
role = message.role.value.capitalize()
content_type = message.content.type
if content_type == "text":
return f"{role}: {message.content.text}"
elif content_type == "function_call":
params_str = ", ".join([f"{p.name}={p.value}" for p in message.content.parameters])
return f"{role} calls function: {message.content.name}({params_str})"
elif content_type == "function_response":
response_str = json.dumps(message.content.response, indent=2)
return f"{role} function response: {message.content.name} -> {response_str}"
elif content_type == "error":
return f"{role} error: {message.content.message}"
else:
return f"{role}: [Unknown message type: {content_type}]"
def format_conversation_as_text(conversation: Conversation) -> str:
"""
Format a conversation as a human-readable text string
Args:
conversation: The conversation to format
Returns:
A formatted string representation of the conversation
"""
result = f"Conversation: {conversation.conversation_id}\n"
result += "=" * 40 + "\n"
for message in conversation.messages:
result += format_message_as_text(message) + "\n"
return result
def pretty_print_message(message: Message) -> None:
"""
Print a message in a readable format to the console
Args:
message: The message to print
"""
content_type = message.content.type
role = message.role.value
print(f"[{role.upper()}] | Message ID: {message.message_id}")
if content_type == "text":
print(f"Text: {message.content.text}")
elif content_type == "function_call":
print(f"Function Call: {message.content.name}")
print("Parameters:")
for param in message.content.parameters:
print(f" - {param.name}: {param.value}")
elif content_type == "function_response":
print(f"Function Response: {message.content.name}")
print(f"Response: {json.dumps(message.content.response, indent=2)}")
elif content_type == "error":
print(f"Error: {message.content.message}")
if message.parent_message_id:
print(f"Parent Message: {message.parent_message_id}")
if message.conversation_id:
print(f"Conversation: {message.conversation_id}")
print("-" * 50)
def pretty_print_conversation(conversation: Conversation) -> None:
"""
Print a conversation in a readable format to the console
Args:
conversation: The conversation to print
"""
print(f"Conversation: {conversation.conversation_id}")
print("=" * 50)
if not conversation.messages:
print("(Empty conversation)")
return
for message in conversation.messages:
pretty_print_message(message)