Skip to content

Commit e2d970f

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: add ADK trigger endpoints to ADK Web Server
PiperOrigin-RevId: 896133436
1 parent a60baca commit e2d970f

File tree

20 files changed

+2766
-3
lines changed

20 files changed

+2766
-3
lines changed

src/google/adk/cli/adk_web_server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ def __init__(
657657
logo_image_url: Optional[str] = None,
658658
url_prefix: Optional[str] = None,
659659
auto_create_session: bool = False,
660+
trigger_sources: Optional[list[str]] = None,
660661
):
661662
self.agent_loader = agent_loader
662663
self.session_service = session_service
@@ -675,6 +676,7 @@ def __init__(
675676
self.runner_dict = {}
676677
self.url_prefix = url_prefix
677678
self.auto_create_session = auto_create_session
679+
self.trigger_sources = trigger_sources
678680

679681
async def get_runner_async(self, app_name: str) -> Runner:
680682
"""Returns the cached runner for the given app."""
@@ -2114,6 +2116,13 @@ async def process_messages():
21142116
for task in pending:
21152117
task.cancel()
21162118

2119+
# Register /trigger/* endpoints when enabled.
2120+
if self.trigger_sources:
2121+
from .trigger_routes import TriggerRouter
2122+
2123+
trigger_router = TriggerRouter(self, trigger_sources=self.trigger_sources)
2124+
trigger_router.register(app)
2125+
21172126
if web_assets_dir:
21182127
import mimetypes
21192128

src/google/adk/cli/cli_deploy.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def _ensure_agent_engine_dependency(requirements_txt_path: str) -> None:
9999
100100
EXPOSE {port}
101101
102-
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} {otel_to_cloud_option} {allow_origins_option} {a2a_option} "/app/agents"
102+
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} {otel_to_cloud_option} {allow_origins_option} {a2a_option} {trigger_sources_option} "/app/agents"
103103
"""
104104

105105
_AGENT_ENGINE_APP_TEMPLATE: Final[str] = """
@@ -645,6 +645,7 @@ def to_cloud_run(
645645
memory_service_uri: Optional[str] = None,
646646
use_local_storage: bool = False,
647647
a2a: bool = False,
648+
trigger_sources: Optional[str] = None,
648649
extra_gcloud_args: Optional[tuple[str, ...]] = None,
649650
):
650651
"""Deploys an agent to Google Cloud Run.
@@ -715,6 +716,9 @@ def to_cloud_run(
715716
f'--allow_origins={",".join(allow_origins)}' if allow_origins else ''
716717
)
717718
a2a_option = '--a2a' if a2a else ''
719+
trigger_sources_option = (
720+
f'--trigger_sources={trigger_sources}' if trigger_sources else ''
721+
)
718722
dockerfile_content = _DOCKERFILE_TEMPLATE.format(
719723
gcp_project_id=project,
720724
gcp_region=region,
@@ -735,6 +739,7 @@ def to_cloud_run(
735739
adk_version=adk_version,
736740
host_option=host_option,
737741
a2a_option=a2a_option,
742+
trigger_sources_option=trigger_sources_option,
738743
)
739744
dockerfile_path = os.path.join(temp_folder, 'Dockerfile')
740745
os.makedirs(temp_folder, exist_ok=True)
@@ -1178,6 +1183,7 @@ def to_gke(
11781183
memory_service_uri: Optional[str] = None,
11791184
use_local_storage: bool = False,
11801185
a2a: bool = False,
1186+
trigger_sources: Optional[str] = None,
11811187
service_type: Literal[
11821188
'ClusterIP', 'NodePort', 'LoadBalancer'
11831189
] = 'ClusterIP',
@@ -1275,6 +1281,9 @@ def to_gke(
12751281
adk_version=adk_version,
12761282
host_option=host_option,
12771283
a2a_option='--a2a' if a2a else '',
1284+
trigger_sources_option=(
1285+
f'--trigger_sources={trigger_sources}' if trigger_sources else ''
1286+
),
12781287
)
12791288
dockerfile_path = os.path.join(temp_folder, 'Dockerfile')
12801289
os.makedirs(temp_folder, exist_ok=True)

src/google/adk/cli/cli_tools_click.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,7 @@ def fast_api_common_options():
14521452
"""Decorator to add common fast api options to click commands."""
14531453

14541454
def decorator(func):
1455+
14551456
@click.option(
14561457
"--host",
14571458
type=str,
@@ -1556,6 +1557,17 @@ def decorator(func):
15561557
),
15571558
default=None,
15581559
)
1560+
# Parsed into list[str] by the wrapper below (server commands need a list).
1561+
@click.option(
1562+
"--trigger_sources",
1563+
type=str,
1564+
help=(
1565+
"Optional. Comma-separated list of trigger sources to enable"
1566+
" (e.g., 'pubsub,eventarc'). Registers /apps/{app_name}/trigger/*"
1567+
" endpoints for batch and event-driven agent invocations."
1568+
),
1569+
default=None,
1570+
)
15591571
@functools.wraps(func)
15601572
@click.pass_context
15611573
def wrapper(ctx, *args, **kwargs):
@@ -1567,6 +1579,13 @@ def wrapper(ctx, *args, **kwargs):
15671579
):
15681580
kwargs["log_level"] = "DEBUG"
15691581

1582+
# Parse comma-separated trigger_sources into a list.
1583+
trigger_sources = kwargs.get("trigger_sources")
1584+
if trigger_sources is not None:
1585+
kwargs["trigger_sources"] = [
1586+
s.strip() for s in trigger_sources.split(",") if s.strip()
1587+
]
1588+
15701589
return func(*args, **kwargs)
15711590

15721591
return wrapper
@@ -1609,6 +1628,7 @@ def cli_web(
16091628
extra_plugins: Optional[list[str]] = None,
16101629
logo_text: Optional[str] = None,
16111630
logo_image_url: Optional[str] = None,
1631+
trigger_sources: Optional[list[str]] = None,
16121632
):
16131633
"""Starts a FastAPI server with Web UI for agents.
16141634
@@ -1665,6 +1685,7 @@ async def _lifespan(app: FastAPI):
16651685
extra_plugins=extra_plugins,
16661686
logo_text=logo_text,
16671687
logo_image_url=logo_image_url,
1688+
trigger_sources=trigger_sources,
16681689
)
16691690
config = uvicorn.Config(
16701691
app,
@@ -1720,6 +1741,7 @@ def cli_api_server(
17201741
reload_agents: bool = False,
17211742
extra_plugins: Optional[list[str]] = None,
17221743
auto_create_session: bool = False,
1744+
trigger_sources: Optional[list[str]] = None,
17231745
):
17241746
"""Starts a FastAPI server for agents.
17251747
@@ -1753,6 +1775,7 @@ def cli_api_server(
17531775
reload_agents=reload_agents,
17541776
extra_plugins=extra_plugins,
17551777
auto_create_session=auto_create_session,
1778+
trigger_sources=trigger_sources,
17561779
),
17571780
host=host,
17581781
port=port,
@@ -1887,6 +1910,17 @@ def cli_api_server(
18871910
default=False,
18881911
help="Optional. Whether to enable A2A endpoint.",
18891912
)
1913+
# Kept as raw str (not parsed to list) — interpolated directly into Dockerfile CMD.
1914+
@click.option(
1915+
"--trigger_sources",
1916+
type=str,
1917+
help=(
1918+
"Optional. Comma-separated list of trigger sources to enable"
1919+
" (e.g., 'pubsub,eventarc'). Registers /trigger/* endpoints"
1920+
" for batch and event-driven agent invocations."
1921+
),
1922+
default=None,
1923+
)
18901924
@click.option(
18911925
"--allow_origins",
18921926
help=(
@@ -1923,6 +1957,7 @@ def cli_deploy_cloud_run(
19231957
session_db_url: Optional[str] = None, # Deprecated
19241958
artifact_storage_uri: Optional[str] = None, # Deprecated
19251959
a2a: bool = False,
1960+
trigger_sources: Optional[str] = None,
19261961
):
19271962
"""Deploys an agent to Cloud Run.
19281963
@@ -2000,6 +2035,7 @@ def cli_deploy_cloud_run(
20002035
memory_service_uri=memory_service_uri,
20012036
use_local_storage=use_local_storage,
20022037
a2a=a2a,
2038+
trigger_sources=trigger_sources,
20032039
extra_gcloud_args=tuple(gcloud_args),
20042040
)
20052041
except Exception as e:
@@ -2397,6 +2433,17 @@ def cli_deploy_agent_engine(
23972433
" version in the dev environment)"
23982434
),
23992435
)
2436+
# Kept as raw str (not parsed to list) — interpolated directly into Dockerfile CMD.
2437+
@click.option(
2438+
"--trigger_sources",
2439+
type=str,
2440+
help=(
2441+
"Optional. Comma-separated list of trigger sources to enable"
2442+
" (e.g., 'pubsub,eventarc'). Registers /trigger/* endpoints"
2443+
" for batch and event-driven agent invocations."
2444+
),
2445+
default=None,
2446+
)
24002447
@adk_services_options(default_use_local_storage=False)
24012448
@click.argument(
24022449
"agent",
@@ -2423,6 +2470,7 @@ def cli_deploy_gke(
24232470
artifact_service_uri: Optional[str] = None,
24242471
memory_service_uri: Optional[str] = None,
24252472
use_local_storage: bool = False,
2473+
trigger_sources: Optional[str] = None,
24262474
):
24272475
"""Deploys an agent to GKE.
24282476
@@ -2454,6 +2502,7 @@ def cli_deploy_gke(
24542502
artifact_service_uri=artifact_service_uri,
24552503
memory_service_uri=memory_service_uri,
24562504
use_local_storage=use_local_storage,
2505+
trigger_sources=trigger_sources,
24572506
)
24582507
except Exception as e:
24592508
click.secho(f"Deploy failed: {e}", fg="red", err=True)

src/google/adk/cli/fast_api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import shutil
2323
import sys
2424
from typing import Any
25+
from typing import Literal
2526
from typing import Mapping
2627
from typing import Optional
2728

@@ -95,6 +96,7 @@ def get_fast_api_app(
9596
logo_text: Optional[str] = None,
9697
logo_image_url: Optional[str] = None,
9798
auto_create_session: bool = False,
99+
trigger_sources: Optional[list[Literal["pubsub", "eventarc"]]] = None,
98100
) -> FastAPI:
99101
"""Constructs and returns a FastAPI application for serving ADK agents.
100102
@@ -136,8 +138,11 @@ def get_fast_api_app(
136138
extra_plugins: List of extra plugin names to load.
137139
logo_text: Text to display in the web UI logo area.
138140
logo_image_url: URL for an image to display in the web UI logo area.
139-
auto_create_session: Whether to automatically create a session when
140-
not found.
141+
auto_create_session: Whether to automatically create a session when not
142+
found.
143+
trigger_sources: List of trigger sources to enable (e.g. ["pubsub",
144+
"eventarc"]). When set, registers /trigger/* endpoints for batch and
145+
event-driven agent invocations. None disables all trigger endpoints.
141146
142147
Returns:
143148
The configured FastAPI application instance.
@@ -206,6 +211,7 @@ def get_fast_api_app(
206211
logo_image_url=logo_image_url,
207212
url_prefix=url_prefix,
208213
auto_create_session=auto_create_session,
214+
trigger_sources=trigger_sources,
209215
)
210216

211217
# Callbacks & other optional args for when constructing the FastAPI instance

0 commit comments

Comments
 (0)