GuglielmoTor commited on
Commit
415fd0b
·
verified ·
1 Parent(s): 735c378

Update insight_and_tasks/agents/task_extraction_agent.py

Browse files
insight_and_tasks/agents/task_extraction_agent.py CHANGED
@@ -195,43 +195,56 @@ class TaskExtractionAgent:
195
 
196
  try:
197
  logger.info(f"Running TaskExtractionAgent for user_id: {user_id}, session_id: {session.id}")
198
- async for event in runner.run(
 
 
199
  user_id=user_id,
200
  session_id=session.id,
201
  new_message=user_input_content
202
- ):
203
- # LlmAgent with output_schema stores the result in event.actions.state_delta[output_key]
204
- if (hasattr(event, 'actions') and event.actions and
205
- hasattr(event.actions, 'state_delta') and
206
- isinstance(event.actions.state_delta, dict) and
207
- self.agent.output_key in event.actions.state_delta):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
- extracted_data_dict = event.actions.state_delta[self.agent.output_key]
210
- logger.info(f"Successfully extracted structured data via LlmAgent state_delta.")
211
- break # Assuming full structured output comes in one event with state_delta
212
-
213
- # Capture text parts for debugging if direct structured output isn't found first
214
- if hasattr(event, 'content') and event.content and event.content.parts:
215
- for part in event.content.parts:
216
- if hasattr(part, 'text'):
217
- full_response_text_for_debug += part.text
218
 
219
  if not extracted_data_dict and full_response_text_for_debug:
220
  logger.warning("LlmAgent did not produce structured output in state_delta. Raw text response was: %s",
221
- full_response_text_for_debug[:500] + "...") # Log snippet
222
- # Attempt to parse the raw text if it looks like JSON (fallback, not ideal)
223
- # This is a basic fallback; robust JSON cleaning might be needed if LLM doesn't adhere to schema.
224
- # For now, we rely on LlmAgent's output_schema handling.
225
 
226
  except Exception as e:
227
  logger.error(f"Error during TaskExtractionAgent execution: {e}", exc_info=True)
228
- # Fallback to returning an empty TaskExtractionOutput with error info
229
- return TaskExtractionOutput(
230
- current_quarter_info=f"Q{self._get_quarter(self.current_date)}, {self._days_until_quarter_end(self.current_date)} days remaining",
231
- okrs=[],
232
- overall_strategic_focus=f"Error during task extraction: {e}",
233
- generation_timestamp=datetime.utcnow().isoformat()
234
- )
235
  finally:
236
  try:
237
  await runner.session_service.delete_session(
 
195
 
196
  try:
197
  logger.info(f"Running TaskExtractionAgent for user_id: {user_id}, session_id: {session.id}")
198
+
199
+ # Fix: Use regular for loop instead of async for, since runner.run() returns a generator
200
+ run_result = runner.run(
201
  user_id=user_id,
202
  session_id=session.id,
203
  new_message=user_input_content
204
+ )
205
+
206
+ # Check if it's an async iterator or regular generator
207
+ if hasattr(run_result, '__aiter__'):
208
+ # It's an async iterator, use async for
209
+ async for event in run_result:
210
+ if (hasattr(event, 'actions') and event.actions and
211
+ hasattr(event.actions, 'state_delta') and
212
+ isinstance(event.actions.state_delta, dict) and
213
+ self.agent.output_key in event.actions.state_delta):
214
+
215
+ extracted_data_dict = event.actions.state_delta[self.agent.output_key]
216
+ logger.info(f"Successfully extracted structured data via LlmAgent state_delta.")
217
+ break
218
+
219
+ # Capture text parts for debugging if direct structured output isn't found first
220
+ if hasattr(event, 'content') and event.content and event.content.parts:
221
+ for part in event.content.parts:
222
+ if hasattr(part, 'text'):
223
+ full_response_text_for_debug += part.text
224
+ else:
225
+ # It's a regular generator, use regular for loop
226
+ for event in run_result:
227
+ if (hasattr(event, 'actions') and event.actions and
228
+ hasattr(event.actions, 'state_delta') and
229
+ isinstance(event.actions.state_delta, dict) and
230
+ self.agent.output_key in event.actions.state_delta):
231
+
232
+ extracted_data_dict = event.actions.state_delta[self.agent.output_key]
233
+ logger.info(f"Successfully extracted structured data via LlmAgent state_delta.")
234
+ break
235
 
236
+ # Capture text parts for debugging if direct structured output isn't found first
237
+ if hasattr(event, 'content') and event.content and event.content.parts:
238
+ for part in event.content.parts:
239
+ if hasattr(part, 'text'):
240
+ full_response_text_for_debug += part.text
 
 
 
 
241
 
242
  if not extracted_data_dict and full_response_text_for_debug:
243
  logger.warning("LlmAgent did not produce structured output in state_delta. Raw text response was: %s",
244
+ full_response_text_for_debug[:500] + "...")
 
 
 
245
 
246
  except Exception as e:
247
  logger.error(f"Error during TaskExtractionAgent execution: {e}", exc_info=True)
 
 
 
 
 
 
 
248
  finally:
249
  try:
250
  await runner.session_service.delete_session(