forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.py
More file actions
299 lines (246 loc) · 10.6 KB
/
Copy pathmessage.py
File metadata and controls
299 lines (246 loc) · 10.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""
Message models for the A2A protocol.
"""
import uuid
from dataclasses import dataclass, field
from typing import Dict, Optional, Any, Union, List, ClassVar
from enum import Enum
from .base import BaseModel
from .content import (
TextContent, FunctionCallContent, FunctionResponseContent,
ErrorContent, Metadata, ContentType
)
class MessageRole(str, Enum):
"""Roles in A2A conversations"""
USER = "user"
AGENT = "agent"
SYSTEM = "system"
@dataclass
class Message(BaseModel):
"""Represents an A2A message"""
content: Union[TextContent, FunctionCallContent, FunctionResponseContent, ErrorContent]
role: MessageRole
message_id: str = field(default_factory=lambda: str(uuid.uuid4()))
parent_message_id: Optional[str] = None
conversation_id: Optional[str] = None
metadata: Optional[Metadata] = None
# Add a class variable to track protocol compatibility mode
_GOOGLE_A2A_COMPATIBILITY: ClassVar[bool] = False
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'Message':
"""Create a Message from a dictionary"""
# First try to detect if this is Google A2A format
# IMPORTANT: Very strict detection to avoid breaking existing code
if ("parts" in data and isinstance(data.get("parts"), list) and
"role" in data and not "content" in data):
try:
return cls.from_google_a2a(data)
except Exception:
# If conversion fails, fall back to standard format
# This ensures existing code is never broken by format detection
pass
# Standard python_a2a format (unchanged from original implementation)
content_data = data.get("content", {})
content_type = content_data.get("type")
if content_type == ContentType.TEXT:
content = TextContent.from_dict(content_data)
elif content_type == ContentType.FUNCTION_CALL:
content = FunctionCallContent.from_dict(content_data)
elif content_type == ContentType.FUNCTION_RESPONSE:
content = FunctionResponseContent.from_dict(content_data)
elif content_type == ContentType.ERROR:
content = ErrorContent.from_dict(content_data)
else:
raise ValueError(f"Unknown content type: {content_type}")
metadata = None
metadata_dict = data.get("metadata")
metadata = Metadata.from_dict(metadata_dict) if metadata_dict is not None else None
# Get the role as a string, then convert to enum
role_str = data.get("role", MessageRole.USER)
role = MessageRole(role_str) if isinstance(role_str, str) else role_str
return cls(
content=content,
role=role,
message_id=data.get("message_id", str(uuid.uuid4())),
parent_message_id=data.get("parent_message_id"),
conversation_id=data.get("conversation_id"),
metadata=metadata
)
@classmethod
def from_google_a2a(cls, data: Dict[str, Any]) -> 'Message':
"""Create a Message from a Google A2A format dictionary
Args:
data: A dictionary in Google A2A format
Returns:
A Message object
"""
# Strict validation to ensure this is truly Google A2A format
if not ("parts" in data and isinstance(data.get("parts"), list) and "role" in data):
raise ValueError("Not a valid Google A2A format message")
# Extract metadata
metadata_dict = data.get("metadata", {}) or {}
# Extract core fields from metadata if present
message_id = metadata_dict.pop("message_id", str(uuid.uuid4())) if isinstance(metadata_dict, dict) else str(uuid.uuid4())
parent_message_id = metadata_dict.pop("parent_message_id", None) if isinstance(metadata_dict, dict) else None
conversation_id = metadata_dict.pop("conversation_id", None) if isinstance(metadata_dict, dict) else None
# Create metadata if needed
metadata = None
if metadata_dict and isinstance(metadata_dict, dict):
created_at = metadata_dict.pop("created_at", None)
metadata = Metadata(
created_at=created_at or "",
custom_fields=metadata_dict
)
# Extract role
role_str = data.get("role", "user")
if role_str.lower() == "user":
role = MessageRole.USER
elif role_str.lower() == "agent":
role = MessageRole.AGENT
else:
role = MessageRole.SYSTEM
# Process parts
parts = data.get("parts", [])
content = None
for part in parts:
part_type = part.get("type")
if part_type == "text":
content = TextContent(text=part.get("text", ""))
break
elif part_type == "data":
data_content = part.get("data", {})
# Check for function call
if "function_call" in data_content:
func_data = data_content["function_call"]
from .content import FunctionParameter
parameters = []
for param in func_data.get("parameters", []):
parameters.append(FunctionParameter(
name=param.get("name", ""),
value=param.get("value")
))
content = FunctionCallContent(
name=func_data.get("name", ""),
parameters=parameters
)
break
# Check for function response
if "function_response" in data_content:
func_data = data_content["function_response"]
content = FunctionResponseContent(
name=func_data.get("name", ""),
response=func_data.get("response")
)
break
# Check for error
if "error" in data_content:
content = ErrorContent(message=data_content["error"])
break
# Default to empty text if no content found
if content is None:
content = TextContent(text="")
return cls(
content=content,
role=role,
message_id=message_id,
parent_message_id=parent_message_id,
conversation_id=conversation_id,
metadata=metadata
)
def to_dict(self) -> Dict[str, Any]:
"""Convert Message to dictionary representation"""
# Use google format if compatibility mode is enabled
if self._GOOGLE_A2A_COMPATIBILITY:
return self.to_google_a2a()
# Standard python_a2a format (unchanged from original)
result = {
"content": self.content.to_dict(),
"role": self.role.value
}
if self.message_id:
result["message_id"] = self.message_id
if self.parent_message_id:
result["parent_message_id"] = self.parent_message_id
if self.conversation_id:
result["conversation_id"] = self.conversation_id
if self.metadata:
result["metadata"] = self.metadata.to_dict()
return result
def to_google_a2a(self) -> Dict[str, Any]:
"""Convert to Google A2A format dictionary
Returns:
A dictionary in Google A2A format
"""
# Convert role to string
role = self.role.value
# Convert content to parts
parts = []
if self.content.type == "text":
parts.append({
"type": "text",
"text": self.content.text
})
elif self.content.type == "function_call":
# Convert function call to a data part
function_data = {
"name": self.content.name,
"parameters": [{
"name": param.name,
"value": param.value
} for param in self.content.parameters]
}
parts.append({
"type": "data",
"data": {"function_call": function_data}
})
elif self.content.type == "function_response":
# Convert function response to a data part
function_data = {
"name": self.content.name,
"response": self.content.response
}
parts.append({
"type": "data",
"data": {"function_response": function_data}
})
elif self.content.type == "error":
# Convert error to a data part
parts.append({
"type": "data",
"data": {"error": self.content.message}
})
# Create metadata dict
metadata = {}
if self.metadata:
if hasattr(self.metadata, 'custom_fields'):
metadata.update(self.metadata.custom_fields)
if hasattr(self.metadata, 'created_at') and self.metadata.created_at:
metadata["created_at"] = self.metadata.created_at
# Add python_a2a specific fields as metadata
if self.message_id:
metadata["message_id"] = self.message_id
if self.parent_message_id:
metadata["parent_message_id"] = self.parent_message_id
if self.conversation_id:
metadata["conversation_id"] = self.conversation_id
# Create Google A2A message dict
return {
"role": role,
"parts": parts,
"metadata": metadata
}
@classmethod
def enable_google_a2a_compatibility(cls, enable: bool = True) -> None:
"""Enable or disable Google A2A compatibility mode
When enabled, to_dict() will output Google A2A format
Args:
enable: Whether to enable compatibility mode
"""
cls._GOOGLE_A2A_COMPATIBILITY = enable
@classmethod
def is_google_a2a_compatibility_enabled(cls) -> bool:
"""Check if Google A2A compatibility mode is enabled
Returns:
True if enabled, False otherwise
"""
return cls._GOOGLE_A2A_COMPATIBILITY