forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
337 lines (272 loc) · 11.8 KB
/
models.py
File metadata and controls
337 lines (272 loc) · 11.8 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from datetime import datetime
from typing import TYPE_CHECKING, Any, Generic, TypeVar
if TYPE_CHECKING:
from typing_extensions import override
else:
def override(func): # noqa: ANN001, ANN201
"""Override decorator."""
return func
from google.protobuf.json_format import MessageToDict, ParseDict, ParseError
from google.protobuf.message import Message as ProtoMessage
from pydantic import BaseModel, ValidationError
from a2a.compat.v0_3 import conversions
from a2a.compat.v0_3 import types as types_v03
from a2a.types.a2a_pb2 import Artifact, Message, TaskStatus
try:
from sqlalchemy import JSON, DateTime, Dialect, Index, LargeBinary, String
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
declared_attr,
mapped_column,
)
from sqlalchemy.types import (
TypeDecorator,
)
except ImportError as e:
raise ImportError(
'Database models require SQLAlchemy. '
'Install with one of: '
"'pip install a2a-sdk[postgresql]', "
"'pip install a2a-sdk[mysql]', "
"'pip install a2a-sdk[sqlite]', "
"or 'pip install a2a-sdk[sql]'"
) from e
T = TypeVar('T')
class PydanticType(TypeDecorator[T], Generic[T]):
"""SQLAlchemy type that handles Pydantic model and Protobuf message serialization."""
impl = JSON
cache_ok = True
def __init__(self, pydantic_type: type[T], **kwargs: dict[str, Any]):
"""Initialize the PydanticType.
Args:
pydantic_type: The Pydantic model or Protobuf message type to handle.
**kwargs: Additional arguments for TypeDecorator.
"""
self.pydantic_type = pydantic_type
super().__init__(**kwargs)
def process_bind_param(
self, value: T | None, dialect: Dialect
) -> dict[str, Any] | None:
"""Convert Pydantic model or Protobuf message to a JSON-serializable dictionary for the database."""
if value is None:
return None
if isinstance(value, ProtoMessage):
return MessageToDict(value, preserving_proto_field_name=False)
if isinstance(value, BaseModel):
return value.model_dump(mode='json')
return value # type: ignore[return-value]
def process_result_value(
self, value: dict[str, Any] | None, dialect: Dialect
) -> T | None:
"""Convert a JSON-like dictionary from the database back to a Pydantic model or Protobuf message."""
if value is None:
return None
# Check if it's a protobuf message class
if isinstance(self.pydantic_type, type) and issubclass(
self.pydantic_type, ProtoMessage
):
try:
return ParseDict(value, self.pydantic_type()) # type: ignore[return-value]
except (ParseError, ValueError):
# Try legacy conversion
legacy_map = _get_legacy_conversions()
if self.pydantic_type in legacy_map:
legacy_type, convert_func = legacy_map[self.pydantic_type]
try:
legacy_instance = legacy_type.model_validate(value)
return convert_func(legacy_instance)
except ValidationError:
pass
raise
# Assume it's a Pydantic model
return self.pydantic_type.model_validate(value) # type: ignore[attr-defined]
class PydanticListType(TypeDecorator, Generic[T]):
"""SQLAlchemy type that handles lists of Pydantic models or Protobuf messages."""
impl = JSON
cache_ok = True
def __init__(self, pydantic_type: type[T], **kwargs: dict[str, Any]):
"""Initialize the PydanticListType.
Args:
pydantic_type: The Pydantic model or Protobuf message type for items in the list.
**kwargs: Additional arguments for TypeDecorator.
"""
self.pydantic_type = pydantic_type
super().__init__(**kwargs)
def process_bind_param(
self, value: list[T] | None, dialect: Dialect
) -> list[dict[str, Any]] | None:
"""Convert a list of Pydantic models or Protobuf messages to a JSON-serializable list for the DB."""
if value is None:
return None
result: list[dict[str, Any]] = []
for item in value:
if isinstance(item, ProtoMessage):
result.append(
MessageToDict(item, preserving_proto_field_name=False)
)
elif isinstance(item, BaseModel):
result.append(item.model_dump(mode='json'))
else:
result.append(item) # type: ignore[arg-type]
return result
def process_result_value(
self, value: list[dict[str, Any]] | None, dialect: Dialect
) -> list[T] | None:
"""Convert a JSON-like list from the DB back to a list of Pydantic models or Protobuf messages."""
if value is None:
return None
# Check if it's a protobuf message class
if isinstance(self.pydantic_type, type) and issubclass(
self.pydantic_type, ProtoMessage
):
result = []
legacy_map = _get_legacy_conversions()
legacy_info = legacy_map.get(self.pydantic_type)
for item in value:
try:
result.append(ParseDict(item, self.pydantic_type()))
except (ParseError, ValueError): # noqa: PERF203
if legacy_info:
legacy_type, convert_func = legacy_info
try:
legacy_instance = legacy_type.model_validate(item)
result.append(convert_func(legacy_instance))
continue
except ValidationError:
pass
raise
return result # type: ignore[return-value]
# Assume it's a Pydantic model
return [self.pydantic_type.model_validate(item) for item in value] # type: ignore[attr-defined]
# Base class for all database models
class Base(DeclarativeBase):
"""Base class for declarative models in A2A SDK."""
# TaskMixin that can be used with any table name
class TaskMixin:
"""Mixin providing standard task columns with proper type handling."""
id: Mapped[str] = mapped_column(String(36), primary_key=True, index=True)
context_id: Mapped[str] = mapped_column(String(36), nullable=False)
kind: Mapped[str] = mapped_column(
String(16), nullable=False, default='task'
)
owner: Mapped[str] = mapped_column(String(128), nullable=False)
last_updated: Mapped[datetime | None] = mapped_column(
DateTime, nullable=True
)
# Properly typed Pydantic fields with automatic serialization
status: Mapped[TaskStatus] = mapped_column(PydanticType(TaskStatus))
artifacts: Mapped[list[Artifact] | None] = mapped_column(
PydanticListType(Artifact), nullable=True
)
history: Mapped[list[Message] | None] = mapped_column(
PydanticListType(Message), nullable=True
)
# Using declared_attr to avoid conflict with Pydantic's metadata
@declared_attr
@classmethod
def task_metadata(cls) -> Mapped[dict[str, Any] | None]:
"""Define the 'metadata' column, avoiding name conflicts with Pydantic."""
return mapped_column(JSON, nullable=True, name='metadata')
@override
def __repr__(self) -> str:
"""Return a string representation of the task."""
return (
f'<{self.__class__.__name__}(id="{self.id}", '
f'context_id="{self.context_id}", status="{self.status}")>'
)
@declared_attr.directive
@classmethod
def __table_args__(cls) -> tuple[Any, ...]:
"""Define a composite index (owner, last_updated) for each table that uses the mixin."""
tablename = getattr(cls, '__tablename__', 'tasks')
return (
Index(
f'idx_{tablename}_owner_last_updated', 'owner', 'last_updated'
),
)
def create_task_model(
table_name: str = 'tasks', base: type[DeclarativeBase] = Base
) -> type:
"""Create a TaskModel class with a configurable table name.
Args:
table_name: Name of the database table. Defaults to 'tasks'.
base: Base declarative class to use. Defaults to the SDK's Base class.
Returns:
TaskModel class with the specified table name.
Example:
.. code-block:: python
# Create a task model with default table name
TaskModel = create_task_model()
# Create a task model with custom table name
CustomTaskModel = create_task_model('my_tasks')
# Use with a custom base
from myapp.database import Base as MyBase
TaskModel = create_task_model('tasks', MyBase)
"""
class TaskModel(TaskMixin, base): # type: ignore
__tablename__ = table_name
@override
def __repr__(self) -> str:
"""Return a string representation of the task."""
return (
f'<TaskModel[{table_name}](id="{self.id}", '
f'context_id="{self.context_id}", status="{self.status}")>'
)
# Set a dynamic name for better debugging
TaskModel.__name__ = f'TaskModel_{table_name}'
TaskModel.__qualname__ = f'TaskModel_{table_name}'
return TaskModel
# Default TaskModel for backward compatibility
class TaskModel(TaskMixin, Base):
"""Default task model with standard table name."""
__tablename__ = 'tasks'
# PushNotificationConfigMixin that can be used with any table name
class PushNotificationConfigMixin:
"""Mixin providing standard push notification config columns."""
task_id: Mapped[str] = mapped_column(String(36), primary_key=True)
config_id: Mapped[str] = mapped_column(String(255), primary_key=True)
config_data: Mapped[bytes] = mapped_column(LargeBinary, nullable=False)
owner: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
@override
def __repr__(self) -> str:
"""Return a string representation of the push notification config."""
return (
f'<{self.__class__.__name__}(task_id="{self.task_id}", '
f'config_id="{self.config_id}")>'
)
def create_push_notification_config_model(
table_name: str = 'push_notification_configs',
base: type[DeclarativeBase] = Base,
) -> type:
"""Create a PushNotificationConfigModel class with a configurable table name."""
class PushNotificationConfigModel(PushNotificationConfigMixin, base): # type: ignore
__tablename__ = table_name
@override
def __repr__(self) -> str:
"""Return a string representation of the push notification config."""
return (
f'<PushNotificationConfigModel[{table_name}]('
f'task_id="{self.task_id}", config_id="{self.config_id}")>'
)
PushNotificationConfigModel.__name__ = (
f'PushNotificationConfigModel_{table_name}'
)
PushNotificationConfigModel.__qualname__ = (
f'PushNotificationConfigModel_{table_name}'
)
return PushNotificationConfigModel
# Default PushNotificationConfigModel for backward compatibility
class PushNotificationConfigModel(PushNotificationConfigMixin, Base):
"""Default push notification config model with standard table name."""
__tablename__ = 'push_notification_configs'
def _get_legacy_conversions() -> dict[type, tuple[type, Any]]:
"""Get the mapping of current types to their legacy counterparts and conversion functions."""
return {
TaskStatus: (
types_v03.TaskStatus,
conversions.to_core_task_status,
),
Message: (types_v03.Message, conversions.to_core_message),
Artifact: (types_v03.Artifact, conversions.to_core_artifact),
}