forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.py
More file actions
117 lines (94 loc) · 3.22 KB
/
Copy pathcontent.py
File metadata and controls
117 lines (94 loc) · 3.22 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
"""
Content types for A2A messages.
"""
from dataclasses import dataclass, field
from typing import Dict, List, Any, Optional, Union
from enum import Enum
import datetime
from .base import BaseModel
class ContentType(str, Enum):
"""Types of A2A message content"""
TEXT = "text"
FUNCTION_CALL = "function_call"
FUNCTION_RESPONSE = "function_response"
ERROR = "error"
@dataclass
class TextContent(BaseModel):
"""Simple text message content"""
text: str
type: str = ContentType.TEXT
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'TextContent':
"""Create a TextContent object from a dictionary"""
return cls(
text=data.get("text", ""),
type=data.get("type", ContentType.TEXT)
)
@dataclass
class FunctionParameter(BaseModel):
"""Parameter for a function call"""
name: str
value: Any
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'FunctionParameter':
"""Create a FunctionParameter from a dictionary"""
return cls(
name=data.get("name", ""),
value=data.get("value")
)
@dataclass
class FunctionCallContent(BaseModel):
"""Function call message content"""
name: str
parameters: List[FunctionParameter] = field(default_factory=list)
type: str = ContentType.FUNCTION_CALL
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'FunctionCallContent':
"""Create a FunctionCallContent from a dictionary"""
parameters = [
FunctionParameter.from_dict(param)
for param in data.get("parameters", [])
]
return cls(
name=data.get("name", ""),
parameters=parameters,
type=data.get("type", ContentType.FUNCTION_CALL)
)
@dataclass
class FunctionResponseContent(BaseModel):
"""Function response message content"""
name: str
response: Any
type: str = ContentType.FUNCTION_RESPONSE
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'FunctionResponseContent':
"""Create a FunctionResponseContent from a dictionary"""
return cls(
name=data.get("name", ""),
response=data.get("response"),
type=data.get("type", ContentType.FUNCTION_RESPONSE)
)
@dataclass
class ErrorContent(BaseModel):
"""Error message content"""
message: str
type: str = ContentType.ERROR
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'ErrorContent':
"""Create an ErrorContent from a dictionary"""
return cls(
message=data.get("message", ""),
type=data.get("type", ContentType.ERROR)
)
@dataclass
class Metadata(BaseModel):
"""Custom metadata that can be attached to a message"""
created_at: str = field(default_factory=lambda: datetime.datetime.now().isoformat())
custom_fields: Dict[str, Any] = field(default_factory=dict)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'Metadata':
"""Create a Metadata from a dictionary"""
return cls(
created_at=data.get("created_at", datetime.datetime.now().isoformat()),
custom_fields=data.get("custom_fields", {})
)