Skip to content

Commit 872d386

Browse files
authored
Check the python format for adk samples (google#560)
It also reformats the adk python samples using `uv run pyink .`. No functional changes.
1 parent 040bcf2 commit 872d386

39 files changed

Lines changed: 4863 additions & 4442 deletions

.github/workflows/python_samples_build.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ jobs:
4545
python -m pip install --upgrade pip
4646
pip install uv
4747
48+
- name: Check Formatting
49+
working-directory: samples/agent/adk
50+
run: uv run pyink --check .
51+
4852
- name: Build contact_lookup
4953
working-directory: samples/agent/adk/contact_lookup
5054
run: uv build .
Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
"""Main entry point for the Component Gallery agent."""
2+
33
import logging
44
import os
55
import sys
@@ -29,72 +29,74 @@
2929

3030
logger = logging.getLogger(__name__)
3131

32+
3233
@click.command()
3334
@click.option("--host", default="localhost")
3435
@click.option("--port", default=10005)
3536
def main(host, port):
36-
try:
37-
capabilities = AgentCapabilities(
38-
streaming=True,
39-
extensions=[get_a2ui_agent_extension()],
40-
)
41-
42-
# Skill definition
43-
skill = AgentSkill(
44-
id="component_gallery",
45-
name="Component Gallery",
46-
description="Demonstrates A2UI components.",
47-
tags=["gallery", "demo"],
48-
examples=["Show me the gallery"],
49-
)
50-
51-
base_url = f"http://{host}:{port}"
52-
53-
agent_card = AgentCard(
54-
name="Component Gallery Agent",
55-
description="A2UI Component Gallery",
56-
url=base_url,
57-
version="0.0.1",
58-
default_input_modes=["text"],
59-
default_output_modes=["text"],
60-
capabilities=capabilities,
61-
skills=[skill],
62-
)
63-
64-
agent_executor = ComponentGalleryExecutor(base_url=base_url)
65-
66-
request_handler = DefaultRequestHandler(
67-
agent_executor=agent_executor,
68-
task_store=InMemoryTaskStore(),
69-
)
70-
71-
server = A2AStarletteApplication(
72-
agent_card=agent_card, http_handler=request_handler
73-
)
74-
75-
app = server.build()
76-
77-
app.add_middleware(
78-
CORSMiddleware,
79-
allow_origins=["http://localhost:5173"],
80-
allow_credentials=True,
81-
allow_methods=["*"],
82-
allow_headers=["*"],
83-
)
84-
85-
# Mount assets directory
86-
assets_dir = os.path.join(os.path.dirname(__file__), "assets")
87-
if os.path.exists(assets_dir):
88-
app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")
89-
else:
90-
logger.warning(f"Assets directory not found at {assets_dir}")
91-
92-
print(f"Starting Component Gallery Agent on port {port}...")
93-
uvicorn.run(app, host=host, port=port)
94-
95-
except Exception as e:
96-
logger.error(f"An error occurred during server startup: {e}")
97-
exit(1)
37+
try:
38+
capabilities = AgentCapabilities(
39+
streaming=True,
40+
extensions=[get_a2ui_agent_extension()],
41+
)
42+
43+
# Skill definition
44+
skill = AgentSkill(
45+
id="component_gallery",
46+
name="Component Gallery",
47+
description="Demonstrates A2UI components.",
48+
tags=["gallery", "demo"],
49+
examples=["Show me the gallery"],
50+
)
51+
52+
base_url = f"http://{host}:{port}"
53+
54+
agent_card = AgentCard(
55+
name="Component Gallery Agent",
56+
description="A2UI Component Gallery",
57+
url=base_url,
58+
version="0.0.1",
59+
default_input_modes=["text"],
60+
default_output_modes=["text"],
61+
capabilities=capabilities,
62+
skills=[skill],
63+
)
64+
65+
agent_executor = ComponentGalleryExecutor(base_url=base_url)
66+
67+
request_handler = DefaultRequestHandler(
68+
agent_executor=agent_executor,
69+
task_store=InMemoryTaskStore(),
70+
)
71+
72+
server = A2AStarletteApplication(
73+
agent_card=agent_card, http_handler=request_handler
74+
)
75+
76+
app = server.build()
77+
78+
app.add_middleware(
79+
CORSMiddleware,
80+
allow_origins=["http://localhost:5173"],
81+
allow_credentials=True,
82+
allow_methods=["*"],
83+
allow_headers=["*"],
84+
)
85+
86+
# Mount assets directory
87+
assets_dir = os.path.join(os.path.dirname(__file__), "assets")
88+
if os.path.exists(assets_dir):
89+
app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")
90+
else:
91+
logger.warning(f"Assets directory not found at {assets_dir}")
92+
93+
print(f"Starting Component Gallery Agent on port {port}...")
94+
uvicorn.run(app, host=host, port=port)
95+
96+
except Exception as e:
97+
logger.error(f"An error occurred during server startup: {e}")
98+
exit(1)
99+
98100

99101
if __name__ == "__main__":
100-
main()
102+
main()

samples/agent/adk/component_gallery/a2ui_schema.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
# a2ui_schema.py
1717

18-
A2UI_SCHEMA = r'''
18+
A2UI_SCHEMA = r"""
1919
{
2020
"title": "A2UI Message Schema",
2121
"description": "Describes a JSON payload for an A2UI (Agent to UI) message, which is used to dynamically construct and update user interfaces. A message MUST contain exactly ONE of the action properties: 'beginRendering', 'surfaceUpdate', 'dataModelUpdate', or 'deleteSurface'.",
@@ -789,4 +789,4 @@
789789
}
790790
}
791791
}
792-
'''
792+
"""
Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
21
"""Agent logic for the Component Gallery."""
2+
33
import logging
44
import json
55
from collections.abc import AsyncIterable
@@ -12,69 +12,67 @@
1212

1313
logger = logging.getLogger(__name__)
1414

15+
1516
class ComponentGalleryAgent:
16-
"""An agent that displays a component gallery."""
17-
18-
def __init__(self, base_url: str):
19-
self.base_url = base_url
20-
21-
async def stream(self, query: str, session_id: str) -> AsyncIterable[dict[str, Any]]:
22-
"""Streams the gallery or responses to actions."""
23-
24-
logger.info(f"Stream called with query: {query}")
25-
26-
# Initial Load or Reset
27-
if "WHO_ARE_YOU" in query or "START" in query: # Simple trigger for initial load
28-
gallery_json = get_gallery_json()
29-
yield {
30-
"is_task_complete": True,
31-
"payload": {
32-
"text": "Here is the component gallery.",
33-
"json_string": gallery_json
34-
}
35-
}
36-
return
37-
38-
# Handle Actions
39-
if query.startswith("ACTION:"):
40-
action_name = query
41-
# Create a response update for the second surface
42-
43-
# Simulate network/processing delay
44-
await asyncio.sleep(0.5)
45-
46-
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
47-
48-
response_update = [
49-
{
50-
"surfaceUpdate": {
51-
"surfaceId": "response-surface",
52-
"components": [
53-
{
54-
"id": "response-text",
55-
"component": {
56-
"Text": { "text": { "literalString": f"Agent Processed Action: {action_name} at {timestamp}" } }
57-
}
58-
}
59-
]
60-
}
61-
}
62-
]
63-
64-
65-
yield {
66-
"is_task_complete": True,
67-
"payload": {
68-
"text": "Action processed.",
69-
"json_data": response_update
70-
}
71-
}
72-
return
73-
74-
# Fallback for text
75-
yield {
76-
"is_task_complete": True,
77-
"payload": {
78-
"text": "I am the Component Gallery Agent."
79-
}
80-
}
17+
"""An agent that displays a component gallery."""
18+
19+
def __init__(self, base_url: str):
20+
self.base_url = base_url
21+
22+
async def stream(self, query: str, session_id: str) -> AsyncIterable[dict[str, Any]]:
23+
"""Streams the gallery or responses to actions."""
24+
25+
logger.info(f"Stream called with query: {query}")
26+
27+
# Initial Load or Reset
28+
if "WHO_ARE_YOU" in query or "START" in query: # Simple trigger for initial load
29+
gallery_json = get_gallery_json()
30+
yield {
31+
"is_task_complete": True,
32+
"payload": {
33+
"text": "Here is the component gallery.",
34+
"json_string": gallery_json,
35+
},
36+
}
37+
return
38+
39+
# Handle Actions
40+
if query.startswith("ACTION:"):
41+
action_name = query
42+
# Create a response update for the second surface
43+
44+
# Simulate network/processing delay
45+
await asyncio.sleep(0.5)
46+
47+
timestamp = datetime.datetime.now().strftime("%H:%M:%S")
48+
49+
response_update = [{
50+
"surfaceUpdate": {
51+
"surfaceId": "response-surface",
52+
"components": [{
53+
"id": "response-text",
54+
"component": {
55+
"Text": {
56+
"text": {
57+
"literalString": (
58+
f"Agent Processed Action: {action_name} at"
59+
f" {timestamp}"
60+
)
61+
}
62+
}
63+
},
64+
}],
65+
}
66+
}]
67+
68+
yield {
69+
"is_task_complete": True,
70+
"payload": {"text": "Action processed.", "json_data": response_update},
71+
}
72+
return
73+
74+
# Fallback for text
75+
yield {
76+
"is_task_complete": True,
77+
"payload": {"text": "I am the Component Gallery Agent."},
78+
}

0 commit comments

Comments
 (0)