Merge branch 'main' into live-error-toast

This commit is contained in:
Yifan 2025-04-22 21:19:43 -07:00 committed by GitHub
commit 2062aa38a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 95 additions and 37 deletions

View File

@ -25,6 +25,19 @@ This project follows
## Contribution process ## Contribution process
### Requirement for PRs
- All PRs, other than small documentation or typo fixes, should have a Issue assoicated. If not, please create one.
- Small, focused PRs. Keep changes minimal—one concern per PR.
- For bug fixes or features, please provide logs or screenshot after the fix is applied to help reviewers better understand the fix.
- Please add corresponding testing for your code change if it's not covered by existing tests.
### Large or Complex Changes
For substantial features or architectural revisions:
- Open an Issue First: Outline your proposal, including design considerations and impact.
- Gather Feedback: Discuss with maintainers and the community to ensure alignment and avoid duplicate work
### Code reviews ### Code reviews
All submissions, including submissions by project members, require review. We All submissions, including submissions by project members, require review. We

View File

@ -45,12 +45,27 @@ debugging, versioning, and deployment anywhere from your laptop to the cloud
## 🚀 Installation ## 🚀 Installation
You can install the ADK using `pip`: ### Stable Release (Recommended)
You can install the latest stable version of ADK using `pip`:
```bash ```bash
pip install google-adk pip install google-adk
``` ```
The release cadence is weekly.
This version is recommended for most users as it represents the most recent official release.
### Development Version
Bug fixes and new features are merged into the main branch on GitHub first. If you need access to changes that haven't been included in an official PyPI release yet, you can install directly from the main branch:
```bash
pip install git+https://github.com/google/adk-python.git@main
```
Note: The development version is built directly from the latest code commits. While it includes the newest fixes and features, it may also contain experimental changes or bugs not present in the stable release. Use it primarily for testing upcoming changes or accessing critical fixes before they are officially released.
## 📚 Documentation ## 📚 Documentation
Explore the full documentation for detailed guides on building, evaluating, and Explore the full documentation for detailed guides on building, evaluating, and
@ -121,7 +136,9 @@ for how they can work together.
## 🤝 Contributing ## 🤝 Contributing
We welcome contributions from the community! Whether it's bug reports, feature requests, documentation improvements, or code contributions, please see our [**Contributing Guidelines**](./CONTRIBUTING.md) to get started. We welcome contributions from the community! Whether it's bug reports, feature requests, documentation improvements, or code contributions, please see our
- [General contribution guideline and flow](https://google.github.io/adk-docs/contributing-guide/#questions).
- Then if you want to contribute code, please read [Code Contributing Guidelines](./CONTRIBUTING.md) to get started.
## 📄 License ## 📄 License

View File

@ -33,7 +33,7 @@ dependencies = [
"google-cloud-secret-manager>=2.22.0", # Fetching secrets in RestAPI Tool "google-cloud-secret-manager>=2.22.0", # Fetching secrets in RestAPI Tool
"google-cloud-speech>=2.30.0", # For Audo Transcription "google-cloud-speech>=2.30.0", # For Audo Transcription
"google-cloud-storage>=2.18.0, <3.0.0", # For GCS Artifact service "google-cloud-storage>=2.18.0, <3.0.0", # For GCS Artifact service
"google-genai>=1.9.0", # Google GenAI SDK "google-genai>=1.11.0", # Google GenAI SDK
"graphviz>=0.20.2", # Graphviz for graph rendering "graphviz>=0.20.2", # Graphviz for graph rendering
"mcp>=1.5.0;python_version>='3.10'", # For MCP Toolset "mcp>=1.5.0;python_version>='3.10'", # For MCP Toolset
"opentelemetry-api>=1.31.0", # OpenTelemetry "opentelemetry-api>=1.31.0", # OpenTelemetry

View File

@ -66,7 +66,8 @@ class OAuth2Auth(BaseModelWithConfig):
redirect_uri: Optional[str] = None redirect_uri: Optional[str] = None
auth_response_uri: Optional[str] = None auth_response_uri: Optional[str] = None
auth_code: Optional[str] = None auth_code: Optional[str] = None
token: Optional[Dict[str, Any]] = None access_token: Optional[str] = None
refresh_token: Optional[str] = None
class ServiceAccountCredential(BaseModelWithConfig): class ServiceAccountCredential(BaseModelWithConfig):

View File

@ -82,7 +82,8 @@ class AuthHandler:
or not auth_credential.oauth2 or not auth_credential.oauth2
or not auth_credential.oauth2.client_id or not auth_credential.oauth2.client_id
or not auth_credential.oauth2.client_secret or not auth_credential.oauth2.client_secret
or auth_credential.oauth2.token or auth_credential.oauth2.access_token
or auth_credential.oauth2.refresh_token
): ):
return self.auth_config.exchanged_auth_credential return self.auth_config.exchanged_auth_credential
@ -93,7 +94,7 @@ class AuthHandler:
redirect_uri=auth_credential.oauth2.redirect_uri, redirect_uri=auth_credential.oauth2.redirect_uri,
state=auth_credential.oauth2.state, state=auth_credential.oauth2.state,
) )
token = client.fetch_token( tokens = client.fetch_token(
token_endpoint, token_endpoint,
authorization_response=auth_credential.oauth2.auth_response_uri, authorization_response=auth_credential.oauth2.auth_response_uri,
code=auth_credential.oauth2.auth_code, code=auth_credential.oauth2.auth_code,
@ -102,7 +103,10 @@ class AuthHandler:
updated_credential = AuthCredential( updated_credential = AuthCredential(
auth_type=AuthCredentialTypes.OAUTH2, auth_type=AuthCredentialTypes.OAUTH2,
oauth2=OAuth2Auth(token=dict(token)), oauth2=OAuth2Auth(
access_token=tokens.get("access_token"),
refresh_token=tokens.get("refresh_token"),
),
) )
return updated_credential return updated_credential

View File

@ -54,7 +54,7 @@ COPY "agents/{app_name}/" "/app/agents/{app_name}/"
EXPOSE {port} EXPOSE {port}
CMD adk {command} --port={port} {trace_to_cloud_option} "/app/agents" CMD adk {command} --port={port} {session_db_option} {trace_to_cloud_option} "/app/agents"
""" """
@ -85,6 +85,7 @@ def to_cloud_run(
trace_to_cloud: bool, trace_to_cloud: bool,
with_ui: bool, with_ui: bool,
verbosity: str, verbosity: str,
session_db_url: str,
): ):
"""Deploys an agent to Google Cloud Run. """Deploys an agent to Google Cloud Run.
@ -112,6 +113,7 @@ def to_cloud_run(
trace_to_cloud: Whether to enable Cloud Trace. trace_to_cloud: Whether to enable Cloud Trace.
with_ui: Whether to deploy with UI. with_ui: Whether to deploy with UI.
verbosity: The verbosity level of the CLI. verbosity: The verbosity level of the CLI.
session_db_url: The database URL to connect the session.
""" """
app_name = app_name or os.path.basename(agent_folder) app_name = app_name or os.path.basename(agent_folder)
@ -144,6 +146,9 @@ def to_cloud_run(
port=port, port=port,
command='web' if with_ui else 'api_server', command='web' if with_ui else 'api_server',
install_agent_deps=install_agent_deps, install_agent_deps=install_agent_deps,
session_db_option=f'--session_db_url={session_db_url}'
if session_db_url
else '',
trace_to_cloud_option='--trace_to_cloud' if trace_to_cloud else '', trace_to_cloud_option='--trace_to_cloud' if trace_to_cloud else '',
) )
dockerfile_path = os.path.join(temp_folder, 'Dockerfile') dockerfile_path = os.path.join(temp_folder, 'Dockerfile')

View File

@ -245,12 +245,13 @@ def cli_eval(
@click.option( @click.option(
"--session_db_url", "--session_db_url",
help=( help=(
"Optional. The database URL to store the session.\n\n - Use" """Optional. The database URL to store the session.
" 'agentengine://<agent_engine_resource_id>' to connect to Vertex"
" managed session service.\n\n - Use 'sqlite://<path_to_sqlite_file>'" - Use 'agentengine://<agent_engine_resource_id>' to connect to Agent Engine sessions.
" to connect to a SQLite DB.\n\n - See"
" https://docs.sqlalchemy.org/en/20/core/engines.html#backend-specific-urls" - Use 'sqlite://<path_to_sqlite_file>' to connect to a SQLite DB.
" for more details on supported DB URLs."
- See https://docs.sqlalchemy.org/en/20/core/engines.html#backend-specific-urls for more details on supported DB URLs."""
), ),
) )
@click.option( @click.option(
@ -366,12 +367,13 @@ def cli_web(
@click.option( @click.option(
"--session_db_url", "--session_db_url",
help=( help=(
"Optional. The database URL to store the session.\n\n - Use" """Optional. The database URL to store the session.
" 'agentengine://<agent_engine_resource_id>' to connect to Vertex"
" managed session service.\n\n - Use 'sqlite://<path_to_sqlite_file>'" - Use 'agentengine://<agent_engine_resource_id>' to connect to Agent Engine sessions.
" to connect to a SQLite DB.\n\n - See"
" https://docs.sqlalchemy.org/en/20/core/engines.html#backend-specific-urls" - Use 'sqlite://<path_to_sqlite_file>' to connect to a SQLite DB.
" for more details on supported DB URLs."
- See https://docs.sqlalchemy.org/en/20/core/engines.html#backend-specific-urls for more details on supported DB URLs."""
), ),
) )
@click.option( @click.option(
@ -541,6 +543,18 @@ def cli_api_server(
default="WARNING", default="WARNING",
help="Optional. Override the default verbosity level.", help="Optional. Override the default verbosity level.",
) )
@click.option(
"--session_db_url",
help=(
"""Optional. The database URL to store the session.
- Use 'agentengine://<agent_engine_resource_id>' to connect to Agent Engine sessions.
- Use 'sqlite://<path_to_sqlite_file>' to connect to a SQLite DB.
- See https://docs.sqlalchemy.org/en/20/core/engines.html#backend-specific-urls for more details on supported DB URLs."""
),
)
@click.argument( @click.argument(
"agent", "agent",
type=click.Path( type=click.Path(
@ -558,6 +572,7 @@ def cli_deploy_cloud_run(
trace_to_cloud: bool, trace_to_cloud: bool,
with_ui: bool, with_ui: bool,
verbosity: str, verbosity: str,
session_db_url: str,
): ):
"""Deploys an agent to Cloud Run. """Deploys an agent to Cloud Run.
@ -579,6 +594,7 @@ def cli_deploy_cloud_run(
trace_to_cloud=trace_to_cloud, trace_to_cloud=trace_to_cloud,
with_ui=with_ui, with_ui=with_ui,
verbosity=verbosity, verbosity=verbosity,
session_db_url=session_db_url,
) )
except Exception as e: except Exception as e:
click.secho(f"Deploy failed: {e}", fg="red", err=True) click.secho(f"Deploy failed: {e}", fg="red", err=True)

View File

@ -756,6 +756,12 @@ def get_fast_api_app(
except Exception as e: except Exception as e:
logger.exception("Error during live websocket communication: %s", e) logger.exception("Error during live websocket communication: %s", e)
traceback.print_exc() traceback.print_exc()
WEBSOCKET_INTERNAL_ERROR_CODE = 1011
WEBSOCKET_MAX_BYTES_FOR_REASON = 123
await websocket.close(
code=WEBSOCKET_INTERNAL_ERROR_CODE,
reason=str(e)[:WEBSOCKET_MAX_BYTES_FOR_REASON],
)
finally: finally:
for task in pending: for task in pending:
task.cancel() task.cancel()

View File

@ -69,7 +69,7 @@ class OAuth2CredentialExchanger(BaseAuthCredentialExchanger):
HTTP bearer token cannot be generated, return the original credential. HTTP bearer token cannot be generated, return the original credential.
""" """
if "access_token" not in auth_credential.oauth2.token: if not auth_credential.oauth2.access_token:
return auth_credential return auth_credential
# Return the access token as a bearer token. # Return the access token as a bearer token.
@ -78,7 +78,7 @@ class OAuth2CredentialExchanger(BaseAuthCredentialExchanger):
http=HttpAuth( http=HttpAuth(
scheme="bearer", scheme="bearer",
credentials=HttpCredentials( credentials=HttpCredentials(
token=auth_credential.oauth2.token["access_token"] token=auth_credential.oauth2.access_token
), ),
), ),
) )
@ -111,7 +111,7 @@ class OAuth2CredentialExchanger(BaseAuthCredentialExchanger):
return auth_credential return auth_credential
# If access token is exchanged, exchange a HTTPBearer token. # If access token is exchanged, exchange a HTTPBearer token.
if auth_credential.oauth2.token: if auth_credential.oauth2.access_token:
return self.generate_auth_token(auth_credential) return self.generate_auth_token(auth_credential)
return None return None

View File

@ -126,12 +126,8 @@ def oauth2_credentials_with_token():
client_id="mock_client_id", client_id="mock_client_id",
client_secret="mock_client_secret", client_secret="mock_client_secret",
redirect_uri="https://example.com/callback", redirect_uri="https://example.com/callback",
token={ access_token="mock_access_token",
"access_token": "mock_access_token", refresh_token="mock_refresh_token",
"token_type": "bearer",
"expires_in": 3600,
"refresh_token": "mock_refresh_token",
},
), ),
) )
@ -458,7 +454,7 @@ class TestParseAndStoreAuthResponse:
"""Test with an OAuth auth scheme.""" """Test with an OAuth auth scheme."""
mock_exchange_token.return_value = AuthCredential( mock_exchange_token.return_value = AuthCredential(
auth_type=AuthCredentialTypes.OAUTH2, auth_type=AuthCredentialTypes.OAUTH2,
oauth2=OAuth2Auth(token={"access_token": "exchanged_token"}), oauth2=OAuth2Auth(access_token="exchanged_token"),
) )
handler = AuthHandler(auth_config_with_exchanged) handler = AuthHandler(auth_config_with_exchanged)
@ -573,6 +569,6 @@ class TestExchangeAuthToken:
handler = AuthHandler(auth_config_with_auth_code) handler = AuthHandler(auth_config_with_auth_code)
result = handler.exchange_auth_token() result = handler.exchange_auth_token()
assert result.oauth2.token["access_token"] == "mock_access_token" assert result.oauth2.access_token == "mock_access_token"
assert result.oauth2.token["refresh_token"] == "mock_refresh_token" assert result.oauth2.refresh_token == "mock_refresh_token"
assert result.auth_type == AuthCredentialTypes.OAUTH2 assert result.auth_type == AuthCredentialTypes.OAUTH2

View File

@ -246,7 +246,7 @@ def test_function_get_auth_response():
oauth2=OAuth2Auth( oauth2=OAuth2Auth(
client_id='oauth_client_id_1', client_id='oauth_client_id_1',
client_secret='oauth_client_secret1', client_secret='oauth_client_secret1',
token={'access_token': 'token1'}, access_token='token1',
), ),
), ),
) )
@ -277,7 +277,7 @@ def test_function_get_auth_response():
oauth2=OAuth2Auth( oauth2=OAuth2Auth(
client_id='oauth_client_id_2', client_id='oauth_client_id_2',
client_secret='oauth_client_secret2', client_secret='oauth_client_secret2',
token={'access_token': 'token2'}, access_token='token2',
), ),
), ),
) )

View File

@ -110,7 +110,7 @@ def test_generate_auth_token_success(
client_secret="test_secret", client_secret="test_secret",
redirect_uri="http://localhost:8080", redirect_uri="http://localhost:8080",
auth_response_uri="https://example.com/callback?code=test_code", auth_response_uri="https://example.com/callback?code=test_code",
token={"access_token": "test_access_token"}, access_token="test_access_token",
), ),
) )
updated_credential = oauth2_exchanger.generate_auth_token(auth_credential) updated_credential = oauth2_exchanger.generate_auth_token(auth_credential)
@ -131,7 +131,7 @@ def test_exchange_credential_generate_auth_token(
client_secret="test_secret", client_secret="test_secret",
redirect_uri="http://localhost:8080", redirect_uri="http://localhost:8080",
auth_response_uri="https://example.com/callback?code=test_code", auth_response_uri="https://example.com/callback?code=test_code",
token={"access_token": "test_access_token"}, access_token="test_access_token",
), ),
) )