Skip to content

Commit 708cd9f

Browse files
committed
Implement a vertex based task store
1 parent 8b647bd commit 708cd9f

13 files changed

Lines changed: 1525 additions & 20 deletions

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ postgresql = ["sqlalchemy[asyncio,postgresql-asyncpg]>=2.0.0"]
3737
mysql = ["sqlalchemy[asyncio,aiomysql]>=2.0.0"]
3838
signing = ["PyJWT>=2.0.0"]
3939
sqlite = ["sqlalchemy[asyncio,aiosqlite]>=2.0.0"]
40+
vertex = ["google-cloud-aiplatform>=1.140.0"]
4041

4142
sql = ["a2a-sdk[postgresql,mysql,sqlite]"]
4243

@@ -47,6 +48,7 @@ all = [
4748
"a2a-sdk[grpc]",
4849
"a2a-sdk[telemetry]",
4950
"a2a-sdk[signing]",
51+
"a2a-sdk[vertex]",
5052
]
5153

5254
[project.urls]
@@ -62,6 +64,9 @@ build-backend = "hatchling.build"
6264
[tool.hatch.version]
6365
source = "uv-dynamic-versioning"
6466

67+
[tool.hatch.metadata]
68+
allow-direct-references = true
69+
6570
[tool.hatch.build.targets.wheel]
6671
packages = ["src/a2a"]
6772

src/a2a/contrib/__init__.py

Whitespace-only changes.

src/a2a/contrib/tasks/__init__.py

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -e
3+
4+
for var in VERTEX_PROJECT VERTEX_LOCATION VERTEX_BASE_URL VERTEX_API_VERSION; do
5+
if [ -z "${!var}" ]; then
6+
echo "Error: Environment variable $var is undefined or empty." >&2
7+
exit 1
8+
fi
9+
done
10+
11+
# Get the directory of this script
12+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
13+
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
14+
PYTEST_ARGS=("$@")
15+
16+
uv run pytest -v "${PYTEST_ARGS[@]}" tests/contrib/tasks/test_vertex_task_store.py tests/contrib/tasks/test_vertex_task_converter.py
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
try:
2+
from vertexai import 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.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+
def to_sdk_task_state(stored_state: types.State) -> TaskState:
28+
"""Converts a proto A2aTask.State to a TaskState enum."""
29+
return {
30+
types.State.STATE_UNSPECIFIED: TaskState.unknown,
31+
types.State.SUBMITTED: TaskState.submitted,
32+
types.State.WORKING: TaskState.working,
33+
types.State.COMPLETED: TaskState.completed,
34+
types.State.CANCELLED: TaskState.canceled,
35+
types.State.FAILED: TaskState.failed,
36+
types.State.REJECTED: TaskState.rejected,
37+
types.State.INPUT_REQUIRED: TaskState.input_required,
38+
types.State.AUTH_REQUIRED: TaskState.auth_required,
39+
}.get(stored_state, TaskState.unknown)
40+
41+
42+
def to_stored_task_state(task_state: TaskState) -> types.State:
43+
"""Converts a TaskState enum to a proto A2aTask.State enum value."""
44+
return {
45+
TaskState.unknown: types.State.STATE_UNSPECIFIED,
46+
TaskState.submitted: types.State.SUBMITTED,
47+
TaskState.working: types.State.WORKING,
48+
TaskState.completed: types.State.COMPLETED,
49+
TaskState.canceled: types.State.CANCELLED,
50+
TaskState.failed: types.State.FAILED,
51+
TaskState.rejected: types.State.REJECTED,
52+
TaskState.input_required: types.State.INPUT_REQUIRED,
53+
TaskState.auth_required: types.State.AUTH_REQUIRED,
54+
}.get(task_state, types.State.STATE_UNSPECIFIED)
55+
56+
57+
def to_stored_part(part: Part) -> types.Part:
58+
"""Converts a SDK Part to a proto Part."""
59+
if isinstance(part.root, TextPart):
60+
return types.Part(text=part.root.text)
61+
if isinstance(part.root, DataPart):
62+
data_bytes = json.dumps(part.root.data).encode('utf-8')
63+
return types.Part(
64+
inline_data=types.Blob(
65+
mime_type='application/json', data=data_bytes
66+
)
67+
)
68+
if isinstance(part.root, FilePart):
69+
file_content = part.root.file
70+
if isinstance(file_content, FileWithBytes):
71+
decoded_bytes = base64.b64decode(file_content.bytes)
72+
return types.Part(
73+
inline_data=types.Blob(
74+
mime_type=file_content.mime_type or '', data=decoded_bytes
75+
)
76+
)
77+
if isinstance(file_content, FileWithUri):
78+
return types.Part(
79+
file_data=types.FileData(
80+
mime_type=file_content.mime_type or '',
81+
file_uri=file_content.uri,
82+
)
83+
)
84+
raise ValueError(f'Unsupported part type: {type(part.root)}')
85+
86+
87+
def to_sdk_part(stored_part: types.Part) -> Part:
88+
"""Converts a proto Part to a SDK Part."""
89+
if stored_part.text:
90+
return Part(root=TextPart(text=stored_part.text))
91+
if stored_part.inline_data:
92+
encoded_bytes = base64.b64encode(stored_part.inline_data.data).decode(
93+
'utf-8'
94+
)
95+
return Part(
96+
root=FilePart(
97+
file=FileWithBytes(
98+
mime_type=stored_part.inline_data.mime_type,
99+
bytes=encoded_bytes,
100+
)
101+
)
102+
)
103+
if stored_part.file_data:
104+
return Part(
105+
root=FilePart(
106+
file=FileWithUri(
107+
mime_type=stored_part.file_data.mime_type,
108+
uri=stored_part.file_data.file_uri,
109+
)
110+
)
111+
)
112+
113+
return Part(root=TextPart(text=''))
114+
115+
116+
def to_stored_artifact(artifact: Artifact) -> types.TaskArtifact:
117+
"""Converts a SDK Artifact to a proto TaskArtifact."""
118+
return types.TaskArtifact(
119+
artifact_id=artifact.artifact_id,
120+
parts=[to_stored_part(part) for part in artifact.parts],
121+
)
122+
123+
124+
def to_sdk_artifact(stored_artifact: types.TaskArtifact) -> Artifact:
125+
"""Converts a proto TaskArtifact to a SDK Artifact."""
126+
return Artifact(
127+
artifact_id=stored_artifact.artifact_id,
128+
parts=[to_sdk_part(part) for part in stored_artifact.parts],
129+
)
130+
131+
132+
def to_stored_task(task: Task) -> types.A2aTask:
133+
"""Converts a SDK Task to a proto A2aTask."""
134+
return types.A2aTask(
135+
context_id=task.context_id,
136+
metadata=task.metadata,
137+
state=to_stored_task_state(task.status.state),
138+
output=types.TaskOutput(
139+
artifacts=[
140+
to_stored_artifact(artifact)
141+
for artifact in task.artifacts or []
142+
]
143+
),
144+
)
145+
146+
147+
def to_sdk_task(a2a_task: types.A2aTask) -> Task:
148+
"""Converts a proto A2aTask to a SDK Task."""
149+
return Task(
150+
id=a2a_task.name.split('/')[-1],
151+
context_id=a2a_task.context_id,
152+
status=TaskStatus(state=to_sdk_task_state(a2a_task.state)),
153+
metadata=a2a_task.metadata or {},
154+
artifacts=[
155+
to_sdk_artifact(artifact)
156+
for artifact in a2a_task.output.artifacts or []
157+
]
158+
if a2a_task.output
159+
else [],
160+
history=[],
161+
)

0 commit comments

Comments
 (0)