-
Notifications
You must be signed in to change notification settings - Fork 429
feat: Implement a vertex based task store #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
708cd9f
Implement a vertex based task store
gaborfeher 78e9da3
change to the base directory before invoking test runs (gemini-code-a…
gaborfeher 95a3887
Address review comments
gaborfeher 7fdafe7
Fix test comments and script location
gaborfeher 18ce0c4
apply the ruff reformatter
gaborfeher 0490e99
fix the handling of unknown part types
gaborfeher c665e3c
Create a fake vertex backend for testing
gaborfeher ead78d7
Add round-trip tests for the converter
gaborfeher 05d594c
comment improvements
gaborfeher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| for var in VERTEX_PROJECT VERTEX_LOCATION VERTEX_BASE_URL VERTEX_API_VERSION; do | ||
| if [ -z "${!var}" ]; then | ||
| echo "Error: Environment variable $var is undefined or empty." >&2 | ||
| exit 1 | ||
| fi | ||
| done | ||
|
|
||
| # Get the directory of this script | ||
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||
| PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" | ||
| PYTEST_ARGS=("$@") | ||
|
|
||
| uv run pytest -v "${PYTEST_ARGS[@]}" tests/contrib/tasks/test_vertex_task_store.py tests/contrib/tasks/test_vertex_task_converter.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| try: | ||
| from vertexai import types | ||
|
gaborfeher marked this conversation as resolved.
Outdated
|
||
| except ImportError as e: | ||
| raise ImportError( | ||
| 'vertex_task_converter requires vertexai. ' | ||
| 'Install with: ' | ||
| "'pip install a2a-sdk[vertex]'" | ||
| ) from e | ||
|
|
||
| import base64 | ||
| import json | ||
|
|
||
| from a2a.types import ( | ||
| Artifact, | ||
| DataPart, | ||
| FilePart, | ||
| FileWithBytes, | ||
| FileWithUri, | ||
| Part, | ||
| Task, | ||
| TaskState, | ||
| TaskStatus, | ||
| TextPart, | ||
| ) | ||
|
|
||
|
|
||
| def to_sdk_task_state(stored_state: types.State) -> TaskState: | ||
| """Converts a proto A2aTask.State to a TaskState enum.""" | ||
| return { | ||
| types.State.STATE_UNSPECIFIED: TaskState.unknown, | ||
| types.State.SUBMITTED: TaskState.submitted, | ||
| types.State.WORKING: TaskState.working, | ||
| types.State.COMPLETED: TaskState.completed, | ||
| types.State.CANCELLED: TaskState.canceled, | ||
| types.State.FAILED: TaskState.failed, | ||
| types.State.REJECTED: TaskState.rejected, | ||
| types.State.INPUT_REQUIRED: TaskState.input_required, | ||
| types.State.AUTH_REQUIRED: TaskState.auth_required, | ||
| }.get(stored_state, TaskState.unknown) | ||
|
|
||
|
|
||
| def to_stored_task_state(task_state: TaskState) -> types.State: | ||
| """Converts a TaskState enum to a proto A2aTask.State enum value.""" | ||
| return { | ||
| TaskState.unknown: types.State.STATE_UNSPECIFIED, | ||
| TaskState.submitted: types.State.SUBMITTED, | ||
| TaskState.working: types.State.WORKING, | ||
| TaskState.completed: types.State.COMPLETED, | ||
| TaskState.canceled: types.State.CANCELLED, | ||
| TaskState.failed: types.State.FAILED, | ||
| TaskState.rejected: types.State.REJECTED, | ||
| TaskState.input_required: types.State.INPUT_REQUIRED, | ||
| TaskState.auth_required: types.State.AUTH_REQUIRED, | ||
| }.get(task_state, types.State.STATE_UNSPECIFIED) | ||
|
gaborfeher marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def to_stored_part(part: Part) -> types.Part: | ||
| """Converts a SDK Part to a proto Part.""" | ||
| if isinstance(part.root, TextPart): | ||
| return types.Part(text=part.root.text) | ||
| if isinstance(part.root, DataPart): | ||
| data_bytes = json.dumps(part.root.data).encode('utf-8') | ||
| return types.Part( | ||
| inline_data=types.Blob( | ||
| mime_type='application/json', data=data_bytes | ||
| ) | ||
| ) | ||
|
ishymko marked this conversation as resolved.
|
||
| if isinstance(part.root, FilePart): | ||
| file_content = part.root.file | ||
| if isinstance(file_content, FileWithBytes): | ||
| decoded_bytes = base64.b64decode(file_content.bytes) | ||
| return types.Part( | ||
| inline_data=types.Blob( | ||
| mime_type=file_content.mime_type or '', data=decoded_bytes | ||
| ) | ||
| ) | ||
| if isinstance(file_content, FileWithUri): | ||
| return types.Part( | ||
| file_data=types.FileData( | ||
| mime_type=file_content.mime_type or '', | ||
| file_uri=file_content.uri, | ||
| ) | ||
| ) | ||
| raise ValueError(f'Unsupported part type: {type(part.root)}') | ||
|
|
||
|
|
||
| def to_sdk_part(stored_part: types.Part) -> Part: | ||
| """Converts a proto Part to a SDK Part.""" | ||
| if stored_part.text: | ||
| return Part(root=TextPart(text=stored_part.text)) | ||
| if stored_part.inline_data: | ||
| encoded_bytes = base64.b64encode(stored_part.inline_data.data).decode( | ||
| 'utf-8' | ||
| ) | ||
| return Part( | ||
| root=FilePart( | ||
| file=FileWithBytes( | ||
| mime_type=stored_part.inline_data.mime_type, | ||
| bytes=encoded_bytes, | ||
| ) | ||
| ) | ||
| ) | ||
| if stored_part.file_data: | ||
| return Part( | ||
| root=FilePart( | ||
| file=FileWithUri( | ||
| mime_type=stored_part.file_data.mime_type, | ||
| uri=stored_part.file_data.file_uri, | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| return Part(root=TextPart(text='')) | ||
|
gaborfeher marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
| def to_stored_artifact(artifact: Artifact) -> types.TaskArtifact: | ||
| """Converts a SDK Artifact to a proto TaskArtifact.""" | ||
| return types.TaskArtifact( | ||
| artifact_id=artifact.artifact_id, | ||
| parts=[to_stored_part(part) for part in artifact.parts], | ||
| ) | ||
|
|
||
|
|
||
| def to_sdk_artifact(stored_artifact: types.TaskArtifact) -> Artifact: | ||
| """Converts a proto TaskArtifact to a SDK Artifact.""" | ||
| return Artifact( | ||
| artifact_id=stored_artifact.artifact_id, | ||
| parts=[to_sdk_part(part) for part in stored_artifact.parts], | ||
| ) | ||
|
|
||
|
|
||
| def to_stored_task(task: Task) -> types.A2aTask: | ||
| """Converts a SDK Task to a proto A2aTask.""" | ||
| return types.A2aTask( | ||
| context_id=task.context_id, | ||
| metadata=task.metadata, | ||
| state=to_stored_task_state(task.status.state), | ||
| output=types.TaskOutput( | ||
| artifacts=[ | ||
| to_stored_artifact(artifact) | ||
| for artifact in task.artifacts or [] | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def to_sdk_task(a2a_task: types.A2aTask) -> Task: | ||
| """Converts a proto A2aTask to a SDK Task.""" | ||
| return Task( | ||
| id=a2a_task.name.split('/')[-1], | ||
| context_id=a2a_task.context_id, | ||
| status=TaskStatus(state=to_sdk_task_state(a2a_task.state)), | ||
| metadata=a2a_task.metadata or {}, | ||
| artifacts=[ | ||
| to_sdk_artifact(artifact) | ||
| for artifact in a2a_task.output.artifacts or [] | ||
| ] | ||
| if a2a_task.output | ||
| else [], | ||
| history=[], | ||
| ) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.