Skip to content

Commit 3b1eef7

Browse files
authored
feat: Implement a vertex based task store for the 1.0 branch (#791)
For #751
1 parent 73a2dcf commit 3b1eef7

11 files changed

Lines changed: 1813 additions & 3 deletions

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mysql = ["sqlalchemy[asyncio,aiomysql]>=2.0.0"]
4040
signing = ["PyJWT>=2.0.0"]
4141
sqlite = ["sqlalchemy[asyncio,aiosqlite]>=2.0.0"]
4242
db-cli = ["alembic>=1.14.0"]
43+
vertex = ["google-cloud-aiplatform>=1.140.0"]
4344

4445
sql = ["a2a-sdk[postgresql,mysql,sqlite]"]
4546

@@ -51,6 +52,7 @@ all = [
5152
"a2a-sdk[telemetry]",
5253
"a2a-sdk[signing]",
5354
"a2a-sdk[db-cli]",
55+
"a2a-sdk[vertex]",
5456
]
5557

5658
[project.urls]

src/a2a/contrib/tasks/__init__.py

Whitespace-only changes.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
try:
2+
from vertexai import types as vertexai_types
3+
except ImportError as e:
4+
raise ImportError(
5+
'vertex_task_converter requires vertexai. '
6+
'Install with: '
7+
"'pip install a2a-sdk[vertex]'"
8+
) from e
9+
10+
import base64
11+
import json
12+
13+
from a2a.compat.v0_3.types import (
14+
Artifact,
15+
DataPart,
16+
FilePart,
17+
FileWithBytes,
18+
FileWithUri,
19+
Part,
20+
Task,
21+
TaskState,
22+
TaskStatus,
23+
TextPart,
24+
)
25+
26+
27+
_TO_SDK_TASK_STATE = {
28+
vertexai_types.State.STATE_UNSPECIFIED: TaskState.unknown,
29+
vertexai_types.State.SUBMITTED: TaskState.submitted,
30+
vertexai_types.State.WORKING: TaskState.working,
31+
vertexai_types.State.COMPLETED: TaskState.completed,
32+
vertexai_types.State.CANCELLED: TaskState.canceled,
33+
vertexai_types.State.FAILED: TaskState.failed,
34+
vertexai_types.State.REJECTED: TaskState.rejected,
35+
vertexai_types.State.INPUT_REQUIRED: TaskState.input_required,
36+
vertexai_types.State.AUTH_REQUIRED: TaskState.auth_required,
37+
}
38+
39+
_SDK_TO_STORED_TASK_STATE = {v: k for k, v in _TO_SDK_TASK_STATE.items()}
40+
41+
42+
def to_sdk_task_state(stored_state: vertexai_types.State) -> TaskState:
43+
"""Converts a proto A2aTask.State to a TaskState enum."""
44+
return _TO_SDK_TASK_STATE.get(stored_state, TaskState.unknown)
45+
46+
47+
def to_stored_task_state(task_state: TaskState) -> vertexai_types.State:
48+
"""Converts a TaskState enum to a proto A2aTask.State enum value."""
49+
return _SDK_TO_STORED_TASK_STATE.get(
50+
task_state, vertexai_types.State.STATE_UNSPECIFIED
51+
)
52+
53+
54+
def to_stored_part(part: Part) -> vertexai_types.Part:
55+
"""Converts a SDK Part to a proto Part."""
56+
if isinstance(part.root, TextPart):
57+
return vertexai_types.Part(text=part.root.text)
58+
if isinstance(part.root, DataPart):
59+
data_bytes = json.dumps(part.root.data).encode('utf-8')
60+
return vertexai_types.Part(
61+
inline_data=vertexai_types.Blob(
62+
mime_type='application/json', data=data_bytes
63+
)
64+
)
65+
if isinstance(part.root, FilePart):
66+
file_content = part.root.file
67+
if isinstance(file_content, FileWithBytes):
68+
decoded_bytes = base64.b64decode(file_content.bytes)
69+
return vertexai_types.Part(
70+
inline_data=vertexai_types.Blob(
71+
mime_type=file_content.mime_type or '', data=decoded_bytes
72+
)
73+
)
74+
if isinstance(file_content, FileWithUri):
75+
return vertexai_types.Part(
76+
file_data=vertexai_types.FileData(
77+
mime_type=file_content.mime_type or '',
78+
file_uri=file_content.uri,
79+
)
80+
)
81+
raise ValueError(f'Unsupported part type: {type(part.root)}')
82+
83+
84+
def to_sdk_part(stored_part: vertexai_types.Part) -> Part:
85+
"""Converts a proto Part to a SDK Part."""
86+
if stored_part.text:
87+
return Part(root=TextPart(text=stored_part.text))
88+
if stored_part.inline_data:
89+
encoded_bytes = base64.b64encode(stored_part.inline_data.data).decode(
90+
'utf-8'
91+
)
92+
return Part(
93+
root=FilePart(
94+
file=FileWithBytes(
95+
mime_type=stored_part.inline_data.mime_type,
96+
bytes=encoded_bytes,
97+
)
98+
)
99+
)
100+
if stored_part.file_data:
101+
return Part(
102+
root=FilePart(
103+
file=FileWithUri(
104+
mime_type=stored_part.file_data.mime_type,
105+
uri=stored_part.file_data.file_uri,
106+
)
107+
)
108+
)
109+
110+
raise ValueError(f'Unsupported part: {stored_part}')
111+
112+
113+
def to_stored_artifact(artifact: Artifact) -> vertexai_types.TaskArtifact:
114+
"""Converts a SDK Artifact to a proto TaskArtifact."""
115+
return vertexai_types.TaskArtifact(
116+
artifact_id=artifact.artifact_id,
117+
parts=[to_stored_part(part) for part in artifact.parts],
118+
)
119+
120+
121+
def to_sdk_artifact(stored_artifact: vertexai_types.TaskArtifact) -> Artifact:
122+
"""Converts a proto TaskArtifact to a SDK Artifact."""
123+
return Artifact(
124+
artifact_id=stored_artifact.artifact_id,
125+
parts=[to_sdk_part(part) for part in stored_artifact.parts],
126+
)
127+
128+
129+
def to_stored_task(task: Task) -> vertexai_types.A2aTask:
130+
"""Converts a SDK Task to a proto A2aTask."""
131+
return vertexai_types.A2aTask(
132+
context_id=task.context_id,
133+
metadata=task.metadata,
134+
state=to_stored_task_state(task.status.state),
135+
output=vertexai_types.TaskOutput(
136+
artifacts=[
137+
to_stored_artifact(artifact)
138+
for artifact in task.artifacts or []
139+
]
140+
),
141+
)
142+
143+
144+
def to_sdk_task(a2a_task: vertexai_types.A2aTask) -> Task:
145+
"""Converts a proto A2aTask to a SDK Task."""
146+
return Task(
147+
id=a2a_task.name.split('/')[-1],
148+
context_id=a2a_task.context_id,
149+
status=TaskStatus(state=to_sdk_task_state(a2a_task.state)),
150+
metadata=a2a_task.metadata or {},
151+
artifacts=[
152+
to_sdk_artifact(artifact)
153+
for artifact in a2a_task.output.artifacts or []
154+
]
155+
if a2a_task.output
156+
else [],
157+
history=[],
158+
)

0 commit comments

Comments
 (0)