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
66 lines (55 loc) · 2.24 KB
/
Copy pathmessage.py
File metadata and controls
66 lines (55 loc) · 2.24 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
"""
Message models for the A2A protocol.
"""
import uuid
from dataclasses import dataclass, field
from typing import Dict, Optional, Any, Union
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
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'Message':
"""Create a Message from a dictionary"""
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
)