chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,216 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[codz]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py.cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
# Pipfile.lock
|
||||
|
||||
# UV
|
||||
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# uv.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
# poetry.lock
|
||||
# poetry.toml
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
||||
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
||||
# pdm.lock
|
||||
# pdm.toml
|
||||
.pdm-python
|
||||
.pdm-build/
|
||||
|
||||
# pixi
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
||||
# pixi.lock
|
||||
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
||||
# in the .venv directory. It is recommended not to include this directory in version control.
|
||||
.pixi
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# Redis
|
||||
*.rdb
|
||||
*.aof
|
||||
*.pid
|
||||
|
||||
# RabbitMQ
|
||||
mnesia/
|
||||
rabbitmq/
|
||||
rabbitmq-data/
|
||||
|
||||
# ActiveMQ
|
||||
activemq-data/
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.envrc
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
# .idea/
|
||||
|
||||
# Abstra
|
||||
# Abstra is an AI-powered process automation framework.
|
||||
# Ignore directories containing user credentials, local state, and settings.
|
||||
# Learn more at https://abstra.io/docs
|
||||
.abstra/
|
||||
|
||||
# Visual Studio Code
|
||||
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
||||
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
||||
# you could uncomment the following to ignore the entire vscode folder
|
||||
# .vscode/
|
||||
|
||||
# Ruff stuff:
|
||||
.ruff_cache/
|
||||
|
||||
# PyPI configuration file
|
||||
.pypirc
|
||||
|
||||
# Marimo
|
||||
marimo/_static/
|
||||
marimo/_lsp/
|
||||
__marimo__/
|
||||
|
||||
# Streamlit
|
||||
.streamlit/secrets.toml
|
||||
@@ -0,0 +1,11 @@
|
||||
# A2UI Extension Implementation
|
||||
|
||||
This is the Python implementation of the a2ui extension.
|
||||
|
||||
## Disclaimer
|
||||
|
||||
Important: The sample code provided is for demonstration purposes and illustrates the mechanics of the Agent-to-Agent (A2A) protocol. When building production applications, it is critical to treat any agent operating outside of your direct control as a potentially untrusted entity.
|
||||
|
||||
All data received from an external agent—including but not limited to its AgentCard, messages, artifacts, and task statuses—should be handled as untrusted input. For example, a malicious agent could provide an AgentCard containing crafted data in its fields (e.g., description, name, skills.description). If this data is used without sanitization to construct prompts for a Large Language Model (LLM), it could expose your application to prompt injection attacks. Failure to properly validate and sanitize this data before use can introduce security vulnerabilities into your application.
|
||||
|
||||
Developers are responsible for implementing appropriate security measures, such as input validation and secure handling of credentials to protect their systems and users.
|
||||
@@ -0,0 +1,11 @@
|
||||
[project]
|
||||
name = "a2ui"
|
||||
version = "0.1.0"
|
||||
description = "A2UI Extension"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = ["a2a-sdk>=0.3.0"]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
@@ -0,0 +1,13 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
@@ -0,0 +1,118 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
|
||||
from a2a.server.agent_execution import RequestContext
|
||||
from a2a.types import AgentExtension, Part, DataPart
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
A2UI_EXTENSION_URI = "https://a2ui.org/a2a-extension/a2ui/v0.8"
|
||||
|
||||
MIME_TYPE_KEY = "mimeType"
|
||||
A2UI_MIME_TYPE = "application/json+a2ui"
|
||||
|
||||
A2UI_CLIENT_CAPABILITIES_KEY = "a2uiClientCapabilities"
|
||||
SUPPORTED_CATALOG_IDS_KEY = "supportedCatalogIds"
|
||||
INLINE_CATALOGS_KEY = "inlineCatalogs"
|
||||
|
||||
STANDARD_CATALOG_ID = "https://raw.githubusercontent.com/google/A2UI/refs/heads/main/specification/0.8/json/standard_catalog_definition.json"
|
||||
|
||||
|
||||
def create_a2ui_part(a2ui_data: dict[str, Any]) -> Part:
|
||||
"""Creates an A2A Part containing A2UI data.
|
||||
|
||||
Args:
|
||||
a2ui_data: The A2UI data dictionary.
|
||||
|
||||
Returns:
|
||||
An A2A Part with a DataPart containing the A2UI data.
|
||||
"""
|
||||
return Part(
|
||||
root=DataPart(
|
||||
data=a2ui_data,
|
||||
metadata={
|
||||
MIME_TYPE_KEY: A2UI_MIME_TYPE,
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def is_a2ui_part(part: Part) -> bool:
|
||||
"""Checks if an A2A Part contains A2UI data.
|
||||
|
||||
Args:
|
||||
part: The A2A Part to check.
|
||||
|
||||
Returns:
|
||||
True if the part contains A2UI data, False otherwise.
|
||||
"""
|
||||
return (
|
||||
isinstance(part.root, DataPart)
|
||||
and part.root.metadata
|
||||
and part.root.metadata.get(MIME_TYPE_KEY) == A2UI_MIME_TYPE
|
||||
)
|
||||
|
||||
|
||||
def get_a2ui_datapart(part: Part) -> Optional[DataPart]:
|
||||
"""Extracts the DataPart containing A2UI data from an A2A Part, if present.
|
||||
|
||||
Args:
|
||||
part: The A2A Part to extract A2UI data from.
|
||||
|
||||
Returns:
|
||||
The DataPart containing A2UI data if present, None otherwise.
|
||||
"""
|
||||
if is_a2ui_part(part):
|
||||
return part.root
|
||||
return None
|
||||
|
||||
|
||||
def get_a2ui_agent_extension(
|
||||
accepts_inline_custom_catalog: bool = False,
|
||||
) -> AgentExtension:
|
||||
"""Creates the A2UI AgentExtension configuration.
|
||||
|
||||
Args:
|
||||
accepts_inline_custom_catalog: Whether the agent accepts inline custom catalogs.
|
||||
|
||||
Returns:
|
||||
The configured A2UI AgentExtension.
|
||||
"""
|
||||
params = {}
|
||||
if accepts_inline_custom_catalog:
|
||||
params["acceptsInlineCustomCatalog"] = True # Only set if not default of False
|
||||
|
||||
return AgentExtension(
|
||||
uri=A2UI_EXTENSION_URI,
|
||||
description="Provides agent driven UI using the A2UI JSON format.",
|
||||
params=params if params else None,
|
||||
)
|
||||
|
||||
|
||||
def try_activate_a2ui_extension(context: RequestContext) -> bool:
|
||||
"""Activates the A2UI extension if requested.
|
||||
|
||||
Args:
|
||||
context: The request context to check.
|
||||
|
||||
Returns:
|
||||
True if activated, False otherwise.
|
||||
"""
|
||||
if A2UI_EXTENSION_URI in context.requested_extensions:
|
||||
context.add_activated_extension(A2UI_EXTENSION_URI)
|
||||
return True
|
||||
return False
|
||||
@@ -0,0 +1,13 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
@@ -0,0 +1,91 @@
|
||||
# Copyright 2025 Google LLC
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from a2a.server.agent_execution import RequestContext
|
||||
from a2a.types import DataPart, TextPart, Part
|
||||
from a2ui import a2ui_extension
|
||||
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
|
||||
def test_a2ui_part_serialization():
|
||||
a2ui_data = {"beginRendering": {"surfaceId": "test-surface", "root": "root-column"}}
|
||||
|
||||
part = a2ui_extension.create_a2ui_part(a2ui_data)
|
||||
|
||||
assert a2ui_extension.is_a2ui_part(part), "Should be identified as A2UI part"
|
||||
|
||||
data_part = a2ui_extension.get_a2ui_datapart(part)
|
||||
assert data_part is not None, "Should contain DataPart"
|
||||
assert a2ui_data == data_part.data, "Deserialized data should match original"
|
||||
|
||||
|
||||
def test_non_a2ui_data_part():
|
||||
part = Part(
|
||||
root=DataPart(
|
||||
data={"foo": "bar"},
|
||||
metadata={"mimeType": "application/json"}, # Not A2UI
|
||||
)
|
||||
)
|
||||
assert not a2ui_extension.is_a2ui_part(part), (
|
||||
"Should not be identified as A2UI part"
|
||||
)
|
||||
assert a2ui_extension.get_a2ui_datapart(part) is None, (
|
||||
"Should not return A2UI DataPart"
|
||||
)
|
||||
|
||||
|
||||
def test_non_a2ui_part():
|
||||
text_part = TextPart(text="this is some text")
|
||||
part = Part(root=text_part)
|
||||
|
||||
assert not a2ui_extension.is_a2ui_part(part), (
|
||||
"Should not be identified as A2UI part"
|
||||
)
|
||||
assert a2ui_extension.get_a2ui_datapart(part) is None, (
|
||||
"Should not return A2UI DataPart"
|
||||
)
|
||||
|
||||
|
||||
def test_get_a2ui_agent_extension():
|
||||
agent_extension = a2ui_extension.get_a2ui_agent_extension()
|
||||
assert agent_extension.uri == a2ui_extension.A2UI_EXTENSION_URI
|
||||
assert agent_extension.params is None
|
||||
|
||||
|
||||
def test_get_a2ui_agent_extension_with_inline_custom_catalog():
|
||||
agent_extension = a2ui_extension.get_a2ui_agent_extension(
|
||||
accepts_inline_custom_catalog=True
|
||||
)
|
||||
assert agent_extension.uri == a2ui_extension.A2UI_EXTENSION_URI
|
||||
assert agent_extension.params is not None
|
||||
|
||||
|
||||
def test_try_activate_a2ui_extension():
|
||||
context = MagicMock(spec=RequestContext)
|
||||
context.requested_extensions = [a2ui_extension.A2UI_EXTENSION_URI]
|
||||
|
||||
assert a2ui_extension.try_activate_a2ui_extension(context)
|
||||
context.add_activated_extension.assert_called_once_with(
|
||||
a2ui_extension.A2UI_EXTENSION_URI
|
||||
)
|
||||
|
||||
|
||||
def test_try_activate_a2ui_extension_not_requested():
|
||||
context = MagicMock(spec=RequestContext)
|
||||
context.requested_extensions = []
|
||||
|
||||
assert not a2ui_extension.try_activate_a2ui_extension(context)
|
||||
context.add_activated_extension.assert_not_called()
|
||||
Reference in New Issue
Block a user