Skip to content

Commit 1170421

Browse files
committed
fix: handle unavailable agents and retain fake manager state
Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
1 parent 6603ba3 commit 1170421

4 files changed

Lines changed: 221 additions & 54 deletions

File tree

demo/ui/pages/agent_list.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# ruff: noqa
12
import asyncio
23

34
import mesop as me
@@ -20,13 +21,9 @@ def agent_list_page(app_state: AppState) -> None:
2021
with header('Remote Agents', 'smart_toy'):
2122
pass
2223
agents = asyncio.run(ListRemoteAgents())
23-
agents_list(agents)
24+
agents_list(agents or [])
2425
with dialog(state.agent_dialog_open):
25-
with me.box(
26-
style=me.Style(
27-
display='flex', flex_direction='column', gap=12
28-
)
29-
):
26+
with me.box(style=me.Style(display='flex', flex_direction='column', gap=12)):
3027
me.input(
3128
label='Agent Address',
3229
on_blur=set_agent_address,
@@ -43,9 +40,7 @@ def agent_list_page(app_state: AppState) -> None:
4340
if state.agent_description:
4441
me.text(f'Agent Description: {state.agent_description}')
4542
if state.agent_framework_type:
46-
me.text(
47-
f'Agent Framework Type: {state.agent_framework_type}'
48-
)
43+
me.text(f'Agent Framework Type: {state.agent_framework_type}')
4944
if state.input_modes:
5045
me.text(f'Input Modes: {input_modes_string}')
5146
if state.output_modes:
@@ -54,9 +49,7 @@ def agent_list_page(app_state: AppState) -> None:
5449
me.text(f'Extensions: {extensions_string}')
5550

5651
if state.agent_name:
57-
me.text(
58-
f'Streaming Supported: {state.stream_supported}'
59-
)
52+
me.text(f'Streaming Supported: {state.stream_supported}')
6053
me.text(
6154
f'Push Notifications Supported: {state.push_notifications_supported}'
6255
)
@@ -81,20 +74,14 @@ async def load_agent_info(e: me.ClickEvent) -> None:
8174
state.agent_name = agent_card_response.name
8275
state.agent_description = agent_card_response.description
8376
state.agent_framework_type = (
84-
agent_card_response.provider.organization
85-
if agent_card_response.provider
86-
else ''
77+
agent_card_response.provider.organization if agent_card_response.provider else ''
8778
)
8879
state.input_modes = agent_card_response.default_input_modes
8980
state.output_modes = agent_card_response.default_output_modes
9081
if agent_card_response.capabilities.extensions:
91-
state.extensions = [
92-
ext.uri for ext in agent_card_response.capabilities.extensions
93-
]
82+
state.extensions = [ext.uri for ext in agent_card_response.capabilities.extensions]
9483
state.stream_supported = agent_card_response.capabilities.streaming
95-
state.push_notifications_supported = (
96-
agent_card_response.capabilities.push_notifications
97-
)
84+
state.push_notifications_supported = agent_card_response.capabilities.push_notifications
9885
except Exception as e:
9986
print(e)
10087
state.agent_name = None

demo/ui/service/server/in_memory_manager.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# ruff: noqa
12
import asyncio
23
import datetime
34
import uuid
@@ -147,14 +148,10 @@ def add_event(self, event: Event):
147148

148149
def next_message(self) -> Message:
149150
message = _message_queue[self._next_message_idx]
150-
self._next_message_idx = (self._next_message_idx + 1) % len(
151-
_message_queue
152-
)
151+
self._next_message_idx = (self._next_message_idx + 1) % len(_message_queue)
153152
return message
154153

155-
def get_conversation(
156-
self, conversation_id: str | None
157-
) -> Conversation | None:
154+
def get_conversation(self, conversation_id: str | None) -> Conversation | None:
158155
if not conversation_id:
159156
return None
160157
return next(
@@ -170,9 +167,7 @@ def get_pending_messages(self) -> list[tuple[str, str]]:
170167
for message_id in self._pending_message_ids:
171168
if message_id in self._task_map:
172169
task_id = self._task_map[message_id]
173-
task = next(
174-
filter(lambda x: x.id == task_id, self._tasks), None
175-
)
170+
task = next(filter(lambda x: x.id == task_id, self._tasks), None)
176171
if not task:
177172
rval.append((message_id, ''))
178173
elif task.history and task.history[-1].parts:
@@ -183,15 +178,14 @@ def get_pending_messages(self) -> list[tuple[str, str]]:
183178
rval.append(
184179
(
185180
message_id,
186-
part.root.text
187-
if part.root.kind == 'text'
188-
else 'Working...',
181+
part.root.text if part.root.kind == 'text' else 'Working...',
189182
)
190183
)
184+
else:
185+
rval.append((message_id, ''))
191186
else:
192187
rval.append((message_id, ''))
193-
return rval
194-
return [(x, '') for x in self._pending_message_ids]
188+
return rval
195189

196190
def register_agent(self, url):
197191
agent_data = get_agent_card(url)
@@ -213,7 +207,7 @@ def tasks(self) -> list[Task]:
213207

214208
@property
215209
def events(self) -> list[Event]:
216-
return []
210+
return self._events
217211

218212

219213
_contextId = str(uuid.uuid4())

demo/ui/state/host_agent_service.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# ruff: noqa
12
import json
23
import os
34
import sys
@@ -75,9 +76,10 @@ async def ListRemoteAgents():
7576
client = ConversationClient(server_url)
7677
try:
7778
response = await client.list_agents(ListAgentRequest())
78-
return response.result
79+
return response.result if response.result else []
7980
except Exception as e:
8081
print('Failed to read agents', e)
82+
return []
8183

8284

8385
async def AddRemoteAgent(path: str):
@@ -102,9 +104,10 @@ async def GetProcessingMessages():
102104
client = ConversationClient(server_url)
103105
try:
104106
response = await client.get_pending_messages(PendingMessageRequest())
105-
return dict(response.result)
107+
return dict(response.result) if response.result else {}
106108
except Exception as e:
107109
print('Error getting pending messages', e)
110+
return {}
108111

109112

110113
def GetMessageAliases():
@@ -115,18 +118,16 @@ async def GetTasks():
115118
client = ConversationClient(server_url)
116119
try:
117120
response = await client.list_tasks(ListTaskRequest())
118-
return response.result
121+
return response.result if response.result else []
119122
except Exception as e:
120123
print('Failed to list tasks ', e)
121-
return []
124+
return []
122125

123126

124127
async def ListMessages(conversation_id: str) -> list[Message]:
125128
client = ConversationClient(server_url)
126129
try:
127-
response = await client.list_messages(
128-
ListMessageRequest(params=conversation_id)
129-
)
130+
response = await client.list_messages(ListMessageRequest(params=conversation_id))
130131
return response.result if response.result else []
131132
except Exception as e:
132133
print('Failed to list messages ', e)
@@ -147,9 +148,7 @@ async def UpdateAppState(state: AppState, conversation_id: str):
147148
if not conversations:
148149
state.conversations = []
149150
else:
150-
state.conversations = [
151-
convert_conversation_to_state(x) for x in conversations
152-
]
151+
state.conversations = [convert_conversation_to_state(x) for x in conversations]
153152

154153
state.task_list = []
155154
for task in await GetTasks():
@@ -176,9 +175,7 @@ async def UpdateApiKey(api_key: str):
176175

177176
# Call the update API endpoint
178177
async with httpx.AsyncClient() as client:
179-
response = await client.post(
180-
f'{server_url}/api_key/update', json={'api_key': api_key}
181-
)
178+
response = await client.post(f'{server_url}/api_key/update', json={'api_key': api_key})
182179
response.raise_for_status()
183180
return True
184181
except Exception as e:
@@ -212,11 +209,7 @@ def convert_conversation_to_state(
212209

213210
def convert_task_to_state(task: Task) -> StateTask:
214211
# Get the first message as the description
215-
output = (
216-
[extract_content(a.parts) for a in task.artifacts]
217-
if task.artifacts
218-
else []
219-
)
212+
output = [extract_content(a.parts) for a in task.artifacts] if task.artifacts else []
220213
if not task.history:
221214
return StateTask(
222215
task_id=task.id,

0 commit comments

Comments
 (0)