Skip to content

Commit 0f0b8b9

Browse files
committed
revert the nesting
1 parent 14a32a6 commit 0f0b8b9

File tree

12 files changed

+714
-1034
lines changed

12 files changed

+714
-1034
lines changed

examples/tutorials/10_async/00_base/000_hello_acp/tests/test_agent.py

Lines changed: 63 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import os
1919
import uuid
20-
import asyncio
2120

2221
import pytest
2322
import pytest_asyncio
@@ -75,55 +74,39 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
7574
# Poll for the initial task creation message
7675
task_creation_message_found = False
7776

78-
async def poll_for_task_creation() -> None:
79-
nonlocal task_creation_message_found
80-
async for message in poll_messages(
81-
client=client,
82-
task_id=task.id,
83-
timeout=30,
84-
sleep_interval=1.0,
85-
):
86-
assert isinstance(message, TaskMessage)
87-
if message.content and message.content.type == "text" and message.content.author == "agent":
88-
assert "Hello! I've received your task" in message.content.content
89-
task_creation_message_found = True
90-
break
91-
92-
try:
93-
await asyncio.wait_for(poll_for_task_creation(), timeout=30)
94-
except asyncio.TimeoutError:
95-
pytest.fail("Polling timed out waiting for task creation message")
77+
async for message in poll_messages(
78+
client=client,
79+
task_id=task.id,
80+
timeout=30,
81+
sleep_interval=1.0,
82+
):
83+
assert isinstance(message, TaskMessage)
84+
if message.content and message.content.type == "text" and message.content.author == "agent":
85+
assert "Hello! I've received your task" in message.content.content
86+
task_creation_message_found = True
87+
break
9688

9789
assert task_creation_message_found, "Task creation message not found"
9890

9991
# Send an event and poll for response
10092
user_message = "Hello, this is a test message!"
10193
agent_response_found = False
10294

103-
async def poll_for_response() -> None:
104-
nonlocal agent_response_found
105-
async for message in send_event_and_poll_yielding(
106-
client=client,
107-
agent_id=agent_id,
108-
task_id=task.id,
109-
user_message=user_message,
110-
timeout=30,
111-
sleep_interval=1.0,
112-
):
113-
assert isinstance(message, TaskMessage)
114-
if message.content and message.content.type == "text" and message.content.author == "agent":
115-
if "Hello! I've received your message" in message.content.content:
116-
agent_response_found = True
117-
break
118-
119-
try:
120-
await asyncio.wait_for(poll_for_response(), timeout=30)
121-
except asyncio.TimeoutError:
122-
pytest.fail("Polling timed out waiting for agent response")
95+
async for message in send_event_and_poll_yielding(
96+
client=client,
97+
agent_id=agent_id,
98+
task_id=task.id,
99+
user_message=user_message,
100+
timeout=30,
101+
sleep_interval=1.0,
102+
):
103+
assert isinstance(message, TaskMessage)
104+
if message.content and message.content.type == "text" and message.content.author == "agent":
105+
assert "Hello! I've received your task" in message.content.content
106+
agent_response_found = True
107+
break
123108

124109
assert agent_response_found, "Agent response not found"
125-
126-
127110
class TestStreamingEvents:
128111
"""Test streaming event sending."""
129112

@@ -136,27 +119,19 @@ async def test_send_event_and_stream(self, client: AsyncAgentex, agent_id: str):
136119
assert task is not None
137120
task_creation_found = False
138121

139-
# Poll for the initial task creation message
140-
async def poll_for_task_creation() -> None:
141-
nonlocal task_creation_found
142-
async for message in poll_messages(
143-
client=client,
144-
task_id=task.id,
145-
timeout=30,
146-
sleep_interval=1.0,
147-
):
148-
assert isinstance(message, TaskMessage)
149-
if message.content and message.content.type == "text" and message.content.author == "agent":
150-
assert "Hello! I've received your task" in message.content.content
151-
task_creation_found = True
152-
break
153-
154-
try:
155-
await asyncio.wait_for(poll_for_task_creation(), timeout=30)
156-
except asyncio.TimeoutError:
157-
pytest.fail("Polling timed out waiting for task creation message")
158-
159-
assert task_creation_found, "Task creation message not found in poll"
122+
async for message in poll_messages(
123+
client=client,
124+
task_id=task.id,
125+
timeout=30,
126+
sleep_interval=1.0,
127+
):
128+
assert isinstance(message, TaskMessage)
129+
if message.content and message.content.type == "text" and message.content.author == "agent":
130+
assert "Hello! I've received your task" in message.content.content
131+
task_creation_found = True
132+
break
133+
134+
assert task_creation_found, "Task creation message not found"
160135

161136
user_message = "Hello, this is a test message!"
162137
stream_timeout = 10
@@ -168,48 +143,36 @@ async def poll_for_task_creation() -> None:
168143
user_echo_found = False
169144
agent_response_found = False
170145

171-
async def collect_stream_events() -> None:
172-
nonlocal user_echo_found, agent_response_found
173-
174-
async for event in stream_agent_response(
175-
client=client,
176-
task_id=task.id,
177-
timeout=stream_timeout,
178-
):
179-
all_events.append(event)
180-
# Check events as they arrive
181-
event_type = event.get("type")
182-
if event_type == "full":
183-
content = event.get("content", {})
184-
if content.get("content") is None:
185-
continue # Skip empty content
186-
if content.get("type") == "text" and content.get("author") == "agent":
187-
# Check for agent response to user message
188-
if "Hello! I've received your message" in content.get("content", ""):
189-
# Agent response should come after user echo
190-
assert user_echo_found, "Agent response arrived before user message echo (incorrect order)"
191-
agent_response_found = True
192-
elif content.get("type") == "text" and content.get("author") == "user":
193-
# Check for user message echo
194-
if content.get("content") == user_message:
195-
user_echo_found = True
196-
197-
# Exit early if we've found all expected messages
198-
if user_echo_found and agent_response_found:
199-
break
200-
201-
# Start streaming task
202-
stream_task = asyncio.create_task(collect_stream_events())
203-
204146
# Send the event
205147
event_content = TextContentParam(type="text", author="user", content=user_message)
206148
await client.agents.send_event(agent_id=agent_id, params={"task_id": task.id, "content": event_content})
207149

208-
# Wait for the stream to complete (with timeout)
209-
try:
210-
await asyncio.wait_for(stream_task, timeout=stream_timeout)
211-
except asyncio.TimeoutError:
212-
pytest.fail(f"Stream timed out after {stream_timeout}s waiting for expected messages")
150+
async for event in stream_agent_response(
151+
client=client,
152+
task_id=task.id,
153+
timeout=stream_timeout,
154+
):
155+
all_events.append(event)
156+
# Check events as they arrive
157+
event_type = event.get("type")
158+
if event_type == "full":
159+
content = event.get("content", {})
160+
if content.get("content") is None:
161+
continue # Skip empty content
162+
if content.get("type") == "text" and content.get("author") == "agent":
163+
# Check for agent response to user message
164+
if "Hello! I've received your message" in content.get("content", ""):
165+
# Agent response should come after user echo
166+
assert user_echo_found, "Agent response arrived before user message echo (incorrect order)"
167+
agent_response_found = True
168+
elif content.get("type") == "text" and content.get("author") == "user":
169+
# Check for user message echo
170+
if content.get("content") == user_message:
171+
user_echo_found = True
172+
173+
# Exit early if we've found all expected messages
174+
if user_echo_found and agent_response_found:
175+
break
213176

214177
# Verify all expected messages were received (fail if stream ended without finding them)
215178
assert user_echo_found, "User message echo not found in stream"

examples/tutorials/10_async/00_base/010_multiturn/tests/test_agent.py

Lines changed: 50 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -93,41 +93,33 @@ async def test_send_event_and_poll(self, client: AsyncAgentex, agent_id: str):
9393
# Flags to track what we've received
9494
user_message_found = False
9595
agent_response_found = False
96-
97-
async def poll_for_messages() -> None:
98-
nonlocal user_message_found, agent_response_found
99-
100-
async for message in send_event_and_poll_yielding(
101-
client=client,
102-
agent_id=agent_id,
103-
task_id=task.id,
104-
user_message=user_message,
105-
timeout=30,
106-
sleep_interval=1.0,
107-
):
108-
messages.append(message)
109-
110-
# Validate messages as they arrive
111-
if message.content and hasattr(message.content, 'author'):
112-
if message.content.author == "user" and message.content.content == user_message:
113-
assert message.content == TextContent(
114-
author="user",
115-
content=user_message,
116-
type="text",
117-
)
118-
user_message_found = True
119-
elif message.content.author == "agent":
120-
assert user_message_found, "Agent response arrived before user message"
121-
agent_response_found = True
122-
123-
# Exit early if we've found all expected messages
124-
if user_message_found and agent_response_found:
125-
break
126-
127-
try:
128-
await asyncio.wait_for(poll_for_messages(), timeout=30)
129-
except asyncio.TimeoutError:
130-
pytest.fail("Polling timed out after 30s waiting for expected messages")
96+
async for message in send_event_and_poll_yielding(
97+
client=client,
98+
agent_id=agent_id,
99+
task_id=task.id,
100+
user_message=user_message,
101+
timeout=30,
102+
sleep_interval=1.0,
103+
):
104+
messages.append(message)
105+
106+
# Validate messages as they arrive
107+
if message.content and hasattr(message.content, "author"):
108+
msg_text = getattr(message.content, "content", None)
109+
if message.content.author == "user" and msg_text == user_message:
110+
assert message.content == TextContent(
111+
author="user",
112+
content=user_message,
113+
type="text",
114+
)
115+
user_message_found = True
116+
elif message.content.author == "agent":
117+
assert user_message_found, "Agent response arrived before user message"
118+
agent_response_found = True
119+
120+
# Exit early if we've found all expected messages
121+
if user_message_found and agent_response_found:
122+
break
131123

132124
assert user_message_found, "User message not found"
133125
assert agent_response_found, "Agent response not found"
@@ -175,44 +167,31 @@ async def test_send_event_and_stream(self, client: AsyncAgentex, agent_id: str):
175167
# Flags to track what we've received
176168
user_message_found = False
177169
agent_response_found = False
178-
179-
async def stream_messages():
180-
nonlocal user_message_found, agent_response_found
181-
182-
async for event in stream_agent_response(
183-
client=client,
184-
task_id=task.id,
185-
timeout=15,
186-
):
187-
all_events.append(event)
188-
189-
# Check events as they arrive
190-
event_type = event.get("type")
191-
if event_type == "full":
192-
content = event.get("content", {})
193-
if content.get("content") == user_message and content.get("author") == "user":
194-
# User message should come before agent response
195-
assert not agent_response_found, "User message arrived after agent response (incorrect order)"
196-
user_message_found = True
197-
elif content.get("author") == "agent":
198-
# Agent response should come after user message
199-
assert user_message_found, "Agent response arrived before user message (incorrect order)"
200-
agent_response_found = True
201-
202-
# Exit early if we've found both messages
203-
if user_message_found and agent_response_found:
204-
break
205-
206-
stream_task = asyncio.create_task(stream_messages())
207-
208170
event_content = TextContentParam(type="text", author="user", content=user_message)
209171
await client.agents.send_event(agent_id=agent_id, params={"task_id": task.id, "content": event_content})
210-
211-
# Wait for streaming to complete (with timeout)
212-
try:
213-
await asyncio.wait_for(stream_task, timeout=15)
214-
except asyncio.TimeoutError:
215-
pytest.fail("Stream timed out after 15s waiting for expected messages")
172+
async for event in stream_agent_response(
173+
client=client,
174+
task_id=task.id,
175+
timeout=15,
176+
):
177+
all_events.append(event)
178+
179+
# Check events as they arrive
180+
event_type = event.get("type")
181+
if event_type == "full":
182+
content = event.get("content", {})
183+
if content.get("content") == user_message and content.get("author") == "user":
184+
# User message should come before agent response
185+
assert not agent_response_found, "User message arrived after agent response (incorrect order)"
186+
user_message_found = True
187+
elif content.get("author") == "agent":
188+
# Agent response should come after user message
189+
assert user_message_found, "Agent response arrived before user message (incorrect order)"
190+
agent_response_found = True
191+
192+
# Exit early if we've found both messages
193+
if user_message_found and agent_response_found:
194+
break
216195

217196
# Validate we received events
218197
assert len(all_events) > 0, "No events received in streaming response"

0 commit comments

Comments
 (0)