@@ -108,25 +108,41 @@ async def test_send_event_and_stream(self, client: AsyncAgentex, agent_id: str):
108108 """Test sending an event and streaming the response."""
109109 task_response = await client .agents .create_task (agent_id , params = ParamsCreateTaskRequest (name = uuid .uuid1 ().hex ))
110110 task = task_response .result
111+
111112 assert task is not None
113+ task_creation_found = False
114+ # Poll for the initial task creation message
115+ async for message in poll_messages (
116+ client = client ,
117+ task_id = task .id ,
118+ timeout = 30 ,
119+ sleep_interval = 1.0 ,
120+ ):
121+ assert isinstance (message , TaskMessage )
122+ if message .content and message .content .type == "text" and message .content .author == "agent" :
123+ assert "Hello! I've received your task" in message .content .content
124+ task_creation_found = True
125+ break
126+
127+ assert task_creation_found , "Task creation message not found in poll"
112128
113129 user_message = "Hello, this is a test message!"
130+ stream_timeout = 10
114131
115132 # Collect events from stream
116133 all_events = []
117134
118135 # Flags to track what we've received
119- task_creation_found = False
120136 user_echo_found = False
121137 agent_response_found = False
122138
123- async def collect_stream_events ():
124- nonlocal task_creation_found , user_echo_found , agent_response_found
139+ async def collect_stream_events () -> None :
140+ nonlocal user_echo_found , agent_response_found
125141
126142 async for event in stream_agent_response (
127143 client = client ,
128144 task_id = task .id ,
129- timeout = 30 ,
145+ timeout = stream_timeout ,
130146 ):
131147 all_events .append (event )
132148 # Check events as they arrive
@@ -136,21 +152,19 @@ async def collect_stream_events():
136152 if content .get ("content" ) is None :
137153 continue # Skip empty content
138154 if content .get ("type" ) == "text" and content .get ("author" ) == "agent" :
139- # Check for initial task creation message
140- if "Hello! I've received your task" in content .get ("content" , "" ):
141- task_creation_found = True
142155 # Check for agent response to user message
143- elif "Hello! I've received your message" in content .get ("content" , "" ):
156+ if "Hello! I've received your message" in content .get ("content" , "" ):
144157 # Agent response should come after user echo
145158 assert user_echo_found , "Agent response arrived before user message echo (incorrect order)"
146159 agent_response_found = True
147160 elif content .get ("type" ) == "text" and content .get ("author" ) == "user" :
148161 # Check for user message echo
162+ print ("here" )
149163 if content .get ("content" ) == user_message :
150164 user_echo_found = True
151165
152166 # Exit early if we've found all expected messages
153- if task_creation_found and user_echo_found and agent_response_found :
167+ if user_echo_found and agent_response_found :
154168 break
155169
156170 # Start streaming task
@@ -160,8 +174,16 @@ async def collect_stream_events():
160174 event_content = TextContentParam (type = "text" , author = "user" , content = user_message )
161175 await client .agents .send_event (agent_id = agent_id , params = {"task_id" : task .id , "content" : event_content })
162176
163- # Wait for streaming to complete
164- await stream_task
177+
178+ # Wait for the stream to complete (with timeout)
179+ try :
180+ await asyncio .wait_for (stream_task , timeout = stream_timeout )
181+ except asyncio .TimeoutError :
182+ pytest .fail (f"Stream timed out after { stream_timeout } s waiting for expected messages" )
183+
184+ # Verify all expected messages were received (fail if stream ended without finding them)
185+ assert user_echo_found , "User message echo not found in stream"
186+ assert agent_response_found , "Agent response not found in stream"
165187
166188
167189if __name__ == "__main__" :
0 commit comments