|
1 | | -from sqlalchemy import Column, String, JSON |
2 | | -from sqlalchemy.ext.declarative import declarative_base |
3 | | -from sqlalchemy.dialects.postgresql import JSONB # For PostgreSQL specific JSON type, can be generic JSON too |
| 1 | +try: |
| 2 | + from sqlalchemy import JSON, Column, String |
| 3 | + from sqlalchemy.orm import declarative_base |
| 4 | +except ImportError as e: |
| 5 | + raise ImportError( |
| 6 | + 'Database models require SQLAlchemy. ' |
| 7 | + 'Install with one of: ' |
| 8 | + "'pip install a2a-sdk[postgresql]', " |
| 9 | + "'pip install a2a-sdk[mysql]', " |
| 10 | + "'pip install a2a-sdk[sqlite]', " |
| 11 | + "or 'pip install a2a-sdk[sql]'" |
| 12 | + ) from e |
| 13 | + |
4 | 14 |
|
5 | 15 | Base = declarative_base() |
6 | 16 |
|
| 17 | + |
7 | 18 | class TaskModel(Base): |
8 | | - __tablename__ = "tasks" |
| 19 | + __tablename__ = 'tasks' |
9 | 20 |
|
10 | 21 | id = Column(String, primary_key=True, index=True) |
11 | 22 | contextId = Column(String, nullable=False) |
12 | 23 | kind = Column(String, nullable=False, default='task') |
13 | | - |
14 | | - # Storing Pydantic models as JSONB for flexibility |
15 | | - # SQLAlchemy's JSON type is generally fine, JSONB is a PostgreSQL optimization |
16 | | - # For broader compatibility, we might stick to JSON or use a custom type if needed. |
17 | | - status = Column(JSONB) # Stores TaskStatus as JSON |
18 | | - artifacts = Column(JSONB, nullable=True) # Stores list[Artifact] as JSON |
19 | | - history = Column(JSONB, nullable=True) # Stores list[Message] as JSON |
20 | | - metadata = Column(JSONB, nullable=True) # Stores dict[str, Any] as JSON |
| 24 | + |
| 25 | + # Using generic JSON type for database-agnostic storage |
| 26 | + # This works with PostgreSQL, MySQL, SQLite, and other databases |
| 27 | + status = Column(JSON) # Stores TaskStatus as JSON |
| 28 | + artifacts = Column(JSON, nullable=True) # Stores list[Artifact] as JSON |
| 29 | + history = Column(JSON, nullable=True) # Stores list[Message] as JSON |
| 30 | + task_metadata = Column( |
| 31 | + JSON, nullable=True, name='metadata' |
| 32 | + ) # Stores dict[str, Any] as JSON |
21 | 33 |
|
22 | 34 | def __repr__(self): |
23 | 35 | return f"<TaskModel(id='{self.id}', contextId='{self.contextId}', status='{self.status}')>" |
0 commit comments