-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
462 lines (375 loc) · 16.6 KB
/
main.py
File metadata and controls
462 lines (375 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""Main entry point for the agentic research tool."""
import asyncio
import sys
import os
import json
import logging
from typing import Optional
# Configure logging to suppress SSE ping messages
logging.basicConfig(level=logging.WARNING)
# Suppress specific loggers that might be printing SSE ping messages
logging.getLogger('agents').setLevel(logging.ERROR)
logging.getLogger('agents.mcp').setLevel(logging.ERROR)
logging.getLogger('mcp.client.sse').setLevel(logging.ERROR) # This suppresses the ping warnings
logging.getLogger('httpx').setLevel(logging.WARNING)
from agents import Runner
from cli import (
create_parser, validate_args, get_effective_query,
display_welcome, display_error, display_success, display_info, display_warning,
display_critique_mode, display_research_mode
)
from config import (
MODEL_RESEARCH, MODEL_CRITIQUE, RESULTS_DIR, MAX_TURNS_RESEARCH, MAX_TURNS_CRITIQUE, MAX_TURNS_FINAL_REPORT,
EXIT_SUCCESS, EXIT_VALIDATION_ERROR, EXIT_RESEARCH_AGENT_ERROR, EXIT_CRITIQUE_AGENT_ERROR,
EXIT_FINAL_REPORT_AGENT_ERROR, EXIT_GENERAL_ERROR
)
from context import ResearchContext
from research_agents import ResearchAgents
from token_tracker import get_global_tracker, reset_global_tracker
from event_processor import create_event_processor
async def run_research_workflow(args) -> dict:
"""
Run the research workflow based on arguments.
Args:
args: Parsed command line arguments
Returns:
Dictionary with results
"""
# Update config with CLI arguments
research_model = args.research_model if args.research_model else MODEL_RESEARCH
critique_model = args.critique_model if args.critique_model else MODEL_CRITIQUE
# Reset token tracker for new workflow
reset_global_tracker()
# Create research context
query = get_effective_query(args)
context = ResearchContext(
query=query,
verbose=args.verbose,
critique_requested=args.critique,
critique_only=args.critique_only,
input_file=args.input_file
)
results = {}
# Track which agent failed for specific error codes
failed_agent = None
try:
if args.iterative and args.critique and not args.final_report_only and not args.critique_only:
# Use iterative handoff workflow when explicitly enabled
failed_agent = await run_iterative_workflow(context, args, results)
else:
# Use standalone workflow by default
failed_agent = await run_standalone_workflow(context, args, results)
except KeyboardInterrupt:
display_error("Workflow interrupted by user (Ctrl+C)")
results["error"] = "User interruption"
except Exception as e:
error_msg = str(e)
if "stream" in error_msg.lower() or "connection" in error_msg.lower():
display_error(f"Streaming connection failed: {error_msg}")
results["error"] = f"Streaming failure: {error_msg}"
else:
display_error(f"Workflow execution failed: {error_msg}")
results["error"] = error_msg
failed_agent = "general"
# Add token usage to results
tracker = get_global_tracker()
results["token_usage"] = tracker.get_usage_report()
# Save token usage report
token_usage_path = os.path.join(RESULTS_DIR, "token_usage.json")
tracker.save_to_file(token_usage_path)
# Display token usage summary
if args.verbose:
tracker.print_summary()
# Add failed agent info to results for error code determination
if failed_agent:
results["failed_agent"] = failed_agent
return results
async def run_iterative_workflow(context: ResearchContext, args, results: dict):
"""Run iterative research-critique workflow with hybrid handoffs."""
failed_agent = None
from agents import Runner
display_info("🔄 Iterative Research-Critique Workflow (Hybrid)")
print("--------------------")
print("Using hybrid approach: programmatic research→critique, OpenAI critique→research")
# Create regular research agent (no MCP)
research_agent = ResearchAgents.create_research_agent()
if context.verbose:
display_info(f"Starting iterative workflow: {context.query}")
print(f"\n🔍 Beginning research...")
# Step 1: Run initial research programmatically
try:
result_stream = Runner.run_streamed(research_agent, context.query, context=context, max_turns=MAX_TURNS_RESEARCH)
processor = create_event_processor(context, "research")
research_content = await processor.process_stream(result_stream, "🔍 Research")
except Exception as e:
failed_agent = "research"
raise e
if context.verbose:
print("\n")
display_success("Initial research completed")
# Save initial research results
context.save_research_results(research_content)
results.update(context.output_data)
results["research_output"] = research_content
if context.verbose:
print(f"\n📝 Starting critique with handoff capability and MCP tools...")
# Step 2: Create critique agent with MCP and handoff to research agent
critique_agent, mcp_server = await ResearchAgents.create_critique_agent_with_mcp(research_agent)
try:
# Create critique message with research content
critique_message = ResearchAgents.create_critique_message(context.query, research_content)
# Run critique with potential handoff back to research
try:
result_stream = Runner.run_streamed(
critique_agent,
critique_message,
context=context,
max_turns=MAX_TURNS_CRITIQUE
)
processor = create_event_processor(context, "research_critique_iterative")
final_output = await processor.process_stream(result_stream, "📝 Critique")
except Exception as e:
failed_agent = "critique"
raise e
if context.verbose:
print("\n")
display_success("Iterative workflow completed")
# Print final output to screen
if final_output:
print("\n" + "="*60)
print("FINAL OUTPUT:")
print("="*60)
print(final_output)
print("="*60 + "\n")
# Save critique results to context for final report
if final_output:
context.save_critique_results(final_output)
# Save final results
results.update(context.output_data)
results["iterative_output"] = final_output
# If final report is also requested, generate it using standalone approach
if args.final_report:
print("\n" + "="*60)
display_info("📊 Final Report Mode")
print("="*60)
try:
await run_final_report(context, results)
except Exception as e:
failed_agent = "final_report"
raise e
finally:
# Clean up MCP server
await mcp_server.cleanup()
return failed_agent
async def run_standalone_workflow(context: ResearchContext, args, results: dict):
"""Run workflow using standalone agents."""
failed_agent = None
if args.final_report_only:
# Final-report-only mode
display_info("📊 Final Report Only Mode")
print("--------------------")
try:
await run_final_report(context, results, source_description="final-report-only")
except Exception as e:
failed_agent = "final_report"
raise e
elif args.critique_only:
# Critique-only mode
display_critique_mode()
try:
await run_critique(context, results)
except Exception as e:
failed_agent = "critique"
raise e
elif args.critique:
# Research + critique mode
display_research_mode()
try:
await run_research(context, results)
except Exception as e:
failed_agent = "research"
raise e
display_critique_mode()
# Get research content from context and pass it to critique
research_content = context.output_data.get("content", "")
if not research_content:
display_warning("No research content found for critique")
else:
try:
await run_critique(context, results, research_content, "previous research")
except Exception as e:
failed_agent = "critique"
raise e
# If final report is also requested, generate it
if args.final_report:
print("\n" + "="*60)
display_info("📊 Final Report Mode")
print("="*60)
try:
await run_final_report(context, results)
except Exception as e:
failed_agent = "final_report"
raise e
elif args.final_report:
# Research + final report mode (without critique)
display_research_mode()
try:
await run_research(context, results)
except Exception as e:
failed_agent = "research"
raise e
print("\n" + "="*60)
display_info("📊 Final Report Mode")
print("="*60)
try:
await run_final_report(context, results)
except Exception as e:
failed_agent = "final_report"
raise e
else:
# Research-only mode
display_research_mode()
try:
await run_research(context, results)
except Exception as e:
failed_agent = "research"
raise e
return failed_agent
async def run_research(context: ResearchContext, results: dict):
"""Run standalone research."""
# Create regular research agent (no MCP)
research_agent = ResearchAgents.create_research_agent()
if context.verbose:
display_info(f"Starting research: {context.query}")
print(f"\n🔍 Researching: {context.query}")
# Use streaming with centralized event processing
result_stream = Runner.run_streamed(research_agent, context.query, context=context, max_turns=MAX_TURNS_RESEARCH)
# Process events through centralized processor
processor = create_event_processor(context, "research")
research_content = await processor.process_stream(result_stream)
if context.verbose:
print("\n")
display_success("Research completed")
# Save research results
context.save_research_results(research_content)
results.update(context.output_data)
results["research_output"] = research_content
async def run_critique(context: ResearchContext, results: dict, research_content: str = None, source_description: str = "research"):
"""Run critique workflow on research content."""
# Create critique agent with MCP server
critique_agent, mcp_server = await ResearchAgents.create_critique_agent_with_mcp()
try:
# Get research content - either from parameter or load from file
if research_content is None:
research_content = context.load_research_content(context.input_file)
source_description = f"file: {context.input_file}"
critique_message = ResearchAgents.create_critique_message(context.query, research_content)
if context.verbose:
display_info(f"Starting critique of {source_description}")
print(f"\n📝 Critiquing research with MCP tools...")
# Use streaming with centralized event processing
result_stream = Runner.run_streamed(
critique_agent,
critique_message,
context=context,
max_turns=MAX_TURNS_CRITIQUE
)
# Determine workflow type for event processor
workflow_type = "critique_only" if "file:" in source_description else "critique"
processor = create_event_processor(context, workflow_type)
critique_content = await processor.process_stream(result_stream)
if context.verbose:
print("\n")
display_success("Critique completed")
# Save critique results
context.save_critique_results(critique_content)
results.update(context.output_data)
results["critique_output"] = critique_content
finally:
# Clean up MCP server
await mcp_server.cleanup()
async def run_final_report(context: ResearchContext, results: dict, research_content: str = None, critique_content: str = None, source_description: str = "workflow"):
"""Run final report generation using research and critique content."""
final_report_agent = ResearchAgents.create_final_report_agent()
# Get research and critique content - either from parameters or load from files/context
if research_content is None or critique_content is None:
if source_description == "workflow":
# Get from context (after research and critique steps)
research_content = context.output_data.get("content", "")
critique_content = context.output_data.get("critique", "")
source_description = "previous workflow steps"
else:
# Load from files (final-report-only mode)
research_path = os.path.join(RESULTS_DIR, "research_results.txt")
critique_path = os.path.join(RESULTS_DIR, "critique_results.txt")
research_content = context.load_research_content(research_path)
critique_content = context.load_research_content(critique_path)
source_description = f"files: {research_path} and {critique_path}"
if not research_content:
display_warning("No research content found for final report")
return
if not critique_content:
display_warning("No critique content found for final report")
return
final_report_message = ResearchAgents.create_final_report_message(context.query, research_content, critique_content)
if context.verbose:
display_info(f"Starting final report generation from {source_description}")
print(f"\n📊 Generating comprehensive final report...")
print("(Synthesizing research findings and critique into markdown format)")
print()
# Use streaming with centralized event processing
result_stream = Runner.run_streamed(final_report_agent, final_report_message, context=context, max_turns=MAX_TURNS_FINAL_REPORT)
# Determine workflow type for event processor
workflow_type = "final_report_only" if "files:" in source_description else "final_report"
processor = create_event_processor(context, workflow_type)
final_report_content = await processor.process_stream(result_stream)
# Print final report to screen
if final_report_content:
print("\n" + "="*60)
print("FINAL REPORT:")
print("="*60)
print(final_report_content)
print("="*60 + "\n")
if context.verbose:
print("\n")
display_success("Final report completed")
# Save final report results
context.save_final_report_results(final_report_content)
results.update(context.output_data)
results["final_report_output"] = final_report_content
async def main():
"""Main entry point."""
# Parse arguments
parser = create_parser()
args = parser.parse_args()
# Display welcome
display_welcome()
# Validate arguments
is_valid, error_msg = validate_args(args)
if not is_valid:
display_error(error_msg)
return EXIT_VALIDATION_ERROR
# Run workflow
results = await run_research_workflow(args)
# Display final results and return appropriate exit code
if "error" not in results:
display_success("Workflow completed successfully!")
if args.verbose:
display_info(f"Results saved to: {RESULTS_DIR}/")
return EXIT_SUCCESS
else:
display_error(f"Workflow failed: {results['error']}")
# Return specific error code based on which agent failed
failed_agent = results.get("failed_agent", "general")
if failed_agent == "research":
return EXIT_RESEARCH_AGENT_ERROR
elif failed_agent == "critique":
return EXIT_CRITIQUE_AGENT_ERROR
elif failed_agent == "final_report":
return EXIT_FINAL_REPORT_AGENT_ERROR
else:
return EXIT_GENERAL_ERROR
if __name__ == "__main__":
import sys
exit_code = asyncio.run(main())
sys.exit(exit_code)