Skip to content

Commit af7e4d1

Browse files
committed
Address review comments
1 parent eae7f16 commit af7e4d1

File tree

3 files changed

+52
-29
lines changed

3 files changed

+52
-29
lines changed

samples/agent/adk/component_gallery/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from starlette.middleware.cors import CORSMiddleware
1515
from dotenv import load_dotenv
1616

17-
from agent import ComponentGalleryAgent
1817
from agent_executor import ComponentGalleryExecutor
1918

2019
load_dotenv()

samples/agent/adk/component_gallery/agent.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44
import json
55
from collections.abc import AsyncIterable
66
from typing import Any
7+
import asyncio
8+
import datetime
79

8-
from google.adk.agents.llm_agent import LlmAgent
9-
from google.adk.artifacts import InMemoryArtifactService
10-
from google.adk.memory.in_memory_memory_service import InMemoryMemoryService
11-
from google.adk.runners import Runner
12-
from google.adk.sessions import InMemorySessionService
1310
from gallery_examples import get_gallery_json
1411

1512
logger = logging.getLogger(__name__)
@@ -28,19 +25,20 @@ async def stream(self, query: str, session_id: str) -> AsyncIterable[dict[str, A
2825
# Initial Load or Reset
2926
if "WHO_ARE_YOU" in query or "START" in query: # Simple trigger for initial load
3027
gallery_json = get_gallery_json()
31-
response = f"Here is the component gallery.\n---a2ui_JSON---\n{gallery_json}"
3228
yield {
3329
"is_task_complete": True,
34-
"content": response
30+
"payload": {
31+
"text": "Here is the component gallery.",
32+
"json_string": gallery_json
33+
}
3534
}
3635
return
3736

3837
# Handle Actions
3938
if query.startswith("ACTION:"):
4039
action_name = query
4140
# Create a response update for the second surface
42-
import datetime
43-
import asyncio
41+
# Create a response update for the second surface
4442

4543
# Simulate network/processing delay
4644
await asyncio.sleep(0.5)
@@ -63,16 +61,20 @@ async def stream(self, query: str, session_id: str) -> AsyncIterable[dict[str, A
6361
}
6462
]
6563

66-
json_str = json.dumps(response_update)
67-
response = f"Action processed.\n---a2ui_JSON---\n{json_str}"
64+
6865
yield {
6966
"is_task_complete": True,
70-
"content": response
67+
"payload": {
68+
"text": "Action processed.",
69+
"json_data": response_update
70+
}
7171
}
7272
return
7373

7474
# Fallback for text
7575
yield {
7676
"is_task_complete": True,
77-
"content": "I am the Component Gallery Agent."
77+
"payload": {
78+
"text": "I am the Component Gallery Agent."
79+
}
7880
}

samples/agent/adk/component_gallery/agent_executor.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,48 @@ async def execute(self, context: RequestContext, event_queue: EventQueue) -> Non
5252
updater = TaskUpdater(event_queue, task.id, task.context_id)
5353

5454
async for item in self.agent.stream(query, task.context_id):
55-
content = item["content"]
5655
final_parts = []
57-
58-
if "---a2ui_JSON---" in content:
59-
text_content, json_string = content.split("---a2ui_JSON---", 1)
60-
if text_content.strip():
61-
final_parts.append(Part(root=TextPart(text=text_content.strip())))
56+
57+
if "payload" in item:
58+
payload = item["payload"]
59+
text = payload.get("text")
60+
if text:
61+
final_parts.append(Part(root=TextPart(text=text)))
62+
63+
json_data = payload.get("json_data")
64+
json_string = payload.get("json_string")
6265

63-
if json_string.strip():
66+
if json_string:
6467
try:
65-
json_data = json.loads(json_string.strip())
66-
if isinstance(json_data, list):
67-
for msg in json_data:
68-
final_parts.append(create_a2ui_part(msg))
69-
else:
70-
final_parts.append(create_a2ui_part(json_data))
68+
json_data = json.loads(json_string)
7169
except Exception as e:
72-
logger.error(f"Failed to parse JSON: {e}")
70+
logger.error(f"Failed to parse JSON string: {e}")
71+
72+
if json_data:
73+
if isinstance(json_data, list):
74+
for msg in json_data:
75+
final_parts.append(create_a2ui_part(msg))
76+
else:
77+
final_parts.append(create_a2ui_part(json_data))
7378
else:
74-
final_parts.append(Part(root=TextPart(text=content)))
79+
content = item.get("content", "")
80+
if "---a2ui_JSON---" in content:
81+
text_content, json_string = content.split("---a2ui_JSON---", 1)
82+
if text_content.strip():
83+
final_parts.append(Part(root=TextPart(text=text_content.strip())))
84+
85+
if json_string.strip():
86+
try:
87+
json_data = json.loads(json_string.strip())
88+
if isinstance(json_data, list):
89+
for msg in json_data:
90+
final_parts.append(create_a2ui_part(msg))
91+
else:
92+
final_parts.append(create_a2ui_part(json_data))
93+
except Exception as e:
94+
logger.error(f"Failed to parse JSON: {e}")
95+
elif content:
96+
final_parts.append(Part(root=TextPart(text=content)))
7597

7698
await updater.update_status(
7799
TaskState.completed,

0 commit comments

Comments
 (0)