-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_agent.py
More file actions
654 lines (552 loc) · 28.6 KB
/
run_agent.py
File metadata and controls
654 lines (552 loc) · 28.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) 2024 OSU Natural Language Processing Group
#
# Licensed under the OpenRAIL-S License;
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.licenses.ai/ai-pubs-open-rails-vz1
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This script uses the OpenFloAgent package to perform web automation tasks.
It supports both single task mode and batch task mode.
Configuration is loaded from TOML files compatible with the original src/openflo.py format.
"""
import argparse
import asyncio
import datetime
import json
import logging
import os
import sys
from pathlib import Path
from dotenv import load_dotenv
load_dotenv(Path(__file__).resolve().parent.parent / ".env")
import toml
from openflo.agent.agent import OpenFloAgent
# Setup your API Key here, or pass through environment
# os.environ["OPENAI_API_KEY"] = "Your API KEY Here"
# os.environ["GEMINI_API_KEY"] = "Your API KEY Here"
def setup_logging():
"""Setup comprehensive logging configuration for debugging and monitoring"""
# Create logs directory if it doesn't exist
log_dir = "logs"
os.makedirs(log_dir, exist_ok=True)
# Generate timestamp for log files
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
# Configure root logger with minimal setup to avoid interfering with task-specific loggers
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# Only add console handler to root logger to avoid conflicts
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
)
root_logger.addHandler(console_handler)
# Create separate loggers for run_agent.py specific logging
run_logger = logging.getLogger('run_agent')
run_handler = logging.FileHandler(
os.path.join(log_dir, f"openflo_run_{timestamp}.log"),
mode='w',
encoding='utf-8'
)
run_handler.setFormatter(
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
)
run_logger.addHandler(run_handler)
run_logger.setLevel(logging.INFO)
# Error logger for critical issues
error_logger = logging.getLogger('errors')
error_handler = logging.FileHandler(
os.path.join(log_dir, f"openflo_errors_{timestamp}.log"),
mode='w',
encoding='utf-8'
)
error_handler.setFormatter(
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
)
error_handler.setLevel(logging.ERROR)
error_logger.addHandler(error_handler)
error_logger.setLevel(logging.ERROR)
# Task execution logger - batch processing details
task_logger = logging.getLogger('task_execution')
task_handler = logging.FileHandler(
os.path.join(log_dir, f"task_execution_{timestamp}.log"),
mode='w',
encoding='utf-8'
)
task_handler.setFormatter(
logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
)
task_logger.addHandler(task_handler)
task_logger.setLevel(logging.INFO)
# Performance logger - timing and resource usage
perf_logger = logging.getLogger('performance')
perf_handler = logging.FileHandler(
os.path.join(log_dir, f"performance_{timestamp}.log"),
mode='w',
encoding='utf-8'
)
perf_handler.setFormatter(
logging.Formatter('%(asctime)s - %(message)s')
)
perf_logger.addHandler(perf_handler)
perf_logger.setLevel(logging.INFO)
print(f"Logging configured. Log files will be saved in '{log_dir}/' directory")
print(f"Main log: openflo_run_{timestamp}.log")
print(f"Error log: openflo_errors_{timestamp}.log")
print(f"Task log: task_execution_{timestamp}.log")
print(f"Performance log: performance_{timestamp}.log")
async def run_single_task(config, task_dict):
"""Run a single task using OpenFloAgent
Returns:
dict: Task execution result with status and details
"""
# Get specialized loggers
task_logger = logging.getLogger('task_execution')
perf_logger = logging.getLogger('performance')
task_id = task_dict['task_id']
task_logger.info(f"Starting task: {task_id}")
task_logger.info(f"Task description: {task_dict['confirmed_task']}")
task_logger.info(f"Website: {task_dict['website']}")
print(f"Starting task: {task_dict['confirmed_task']}")
print(f"Website: {task_dict['website']}")
# Use original task directly without refinement
print("\n=== Using Original Task ===")
# Get model configuration
model_config = config.get('model', {})
# Model selection with fallback
model_name = model_config.get('name', 'openrouter/qwen/qwen-2.5-72b-instruct')
temperature = model_config.get('temperature', 1)
rate_limit = config.get('experiment', {}).get('rate_limit', -1)
# API key configuration - prioritize environment variables, then new [api_keys] section
api_keys_config = config.get('api_keys', {})
# Set API keys from config if not in environment
if 'OPENROUTER_API_KEY' not in os.environ:
openrouter_key = api_keys_config.get('openrouter_api_key')
if openrouter_key and openrouter_key not in ["Your OpenRouter API Key Here", "Your API key here"]:
os.environ['OPENROUTER_API_KEY'] = openrouter_key
if 'GEMINI_API_KEY' not in os.environ:
gemini_key = api_keys_config.get('gemini_api_key')
if gemini_key and gemini_key not in ["Your Gemini API Key Here", "Your API key here"]:
os.environ['GEMINI_API_KEY'] = gemini_key
# Extract configuration for OpenFloAgent
# Note: save_file_dir should be the base directory, OpenFloAgent will create task_id subdirectory
agent_config = {
'task_id': task_dict['task_id'], # Required by OpenFloAgent
'default_task': task_dict['confirmed_task'],
'default_website': task_dict['website'],
'save_file_dir': config['basic']['save_file_dir'], # Let OpenFloAgent create task_id subdirectory
'max_auto_op': config.get('experiment', {}).get('max_op', 30),
'max_continuous_no_op': config.get('experiment', {}).get('max_continuous_no_op', config.get('agent', {}).get('max_continuous_no_op', 4)),
'highlight': config.get('experiment', {}).get('highlight', config.get('agent', {}).get('highlight', False)),
'headless': config.get('playwright', {}).get('headless', config.get('browser', {}).get('headless', True)),
'save_video': config.get('playwright', {}).get('save_video', config.get('browser', {}).get('save_video', False)),
'viewport': config.get('playwright', {}).get('viewport', config.get('browser', {}).get('viewport', {'width': 1200, 'height': 1080})),
'tracing': config.get('playwright', {}).get('tracing', config.get('browser', {}).get('tracing', False)),
'trace': config.get('playwright', {}).get('trace', config.get('browser', {}).get('trace', {'screenshots': True, 'snapshots': True, 'sources': True})),
'rate_limit': rate_limit,
'model': model_name,
'temperature': temperature,
'create_timestamp_dir': False
}
# Create agent with configuration
agent = OpenFloAgent(config=config, **agent_config)
# Initialize result tracking
result = {
'task_id': task_dict['task_id'],
'status': 'failed',
'error': None,
'operations_count': 0,
'execution_time': 0
}
import time
start_time = time.time()
perf_logger.info(f"Task {task_id} started at {datetime.datetime.now()}")
try:
# Start the agent
task_logger.info(f"Initializing agent for task {task_id}")
await agent.start(website=task_dict['website'])
task_logger.info(f"Agent started successfully for task {task_id}")
# Enhanced execution loop with intelligent task completion detection
task_logger.info(f"Starting prediction loop for task {task_id}")
# Initialize execution tracking
consecutive_errors = 0
max_consecutive_errors = 3 # Allow max 3 consecutive errors before terminating
total_execution_time = 0
max_total_execution_time = 3600 # 60 minutes maximum per task
while not agent.complete_flag:
# Check global timeout
total_execution_time = time.time() - start_time
if total_execution_time > max_total_execution_time:
timeout_msg = f"Task exceeded maximum execution time ({max_total_execution_time/60:.1f} minutes), terminating"
task_logger.error(f"Task {task_id} terminated due to global timeout")
print(timeout_msg)
logging.error(f"Task {task_dict['task_id']} terminated due to global timeout")
agent.complete_flag = True
result['status'] = 'global_timeout'
break
try:
async def step():
prediction_dict = await agent.predict()
# Enhanced None handling with proper structure validation
if prediction_dict is None:
print(f"Prediction returned None despite fixes - creating fallback")
logging.error(f"Prediction returned None for task {task_dict['task_id']} despite fixes")
prediction_dict = {
'action': 'NONE',
'value': '',
'element': None,
'action_generation': 'Fallback due to None prediction',
'action_grounding': 'System fallback',
'termination_reason': 'none_prediction_fallback'
}
# Additional safety check for prediction_dict structure
if not isinstance(prediction_dict, dict):
print(f"Prediction returned invalid type: {type(prediction_dict)}, treating as NONE action.")
logging.warning(f"Prediction returned invalid type for task {task_dict['task_id']}: {type(prediction_dict)}")
prediction_dict = {
'action': 'NONE',
'value': None,
'element': None,
'action_generation': 'Invalid prediction type',
'action_grounding': 'No grounding available'
}
# Ensure required keys exist with safe defaults
required_keys = ['action', 'value', 'element']
for key in required_keys:
if key not in prediction_dict:
prediction_dict[key] = None if key != 'action' else 'NONE'
await agent.execute(prediction_dict)
# Remove step timeout to avoid premature failures
await step()
# Ultra-conservative intelligent termination check - disabled to allow full LLM capability
# The Agent will now rely on its own judgment to signal completion rather than external termination
# This allows the LLM to fully utilize its capabilities without artificial constraints
# if (len(agent.taken_actions) >= 25 and # Require many more actions before considering termination
# agent.valid_op >= 10 and # Ensure substantial operations have been performed
# await agent._should_terminate_intelligently()):
# print(f"Ultra-conservative intelligent termination triggered for task {task_dict['task_id']} after {len(agent.taken_actions)} actions")
# logging.info(f"Task {task_dict['task_id']} terminated by ultra-conservative intelligent analysis after {len(agent.taken_actions)} actions")
# break
except asyncio.TimeoutError:
print("Step timed out after 3 minutes, skipping to next step.")
logging.warning(f"Step for task {task_dict['task_id']} timed out after 3 minutes.")
# Enhanced timeout handling - don't create new pages on timeout
# Instead, try to recover with existing pages
try:
if agent.session_control.get('context') and agent.session_control['context'].pages:
# Use existing page instead of creating new one
agent.page = agent.session_control['context'].pages[-1]
await agent.page.bring_to_front()
logging.info(f"Timeout recovery: switched to existing page {agent.page.url}")
except Exception as recovery_e:
logging.warning(f"Timeout recovery failed: {recovery_e}")
pass
except Exception as inner_e:
# Enhanced error handling with consecutive error tracking
consecutive_errors += 1
error_msg = f"Error during execution (#{consecutive_errors}), continuing: {inner_e}"
task_logger.warning(f"Execution error in task {task_id}: {inner_e}")
print(error_msg)
logging.warning(f"Execution error for task {task_dict['task_id']}: {inner_e}")
# Force termination after too many consecutive errors to prevent infinite loops
if consecutive_errors >= max_consecutive_errors:
error_msg = f"Too many consecutive errors ({consecutive_errors}), terminating task"
task_logger.error(f"Task {task_id} terminated due to {consecutive_errors} consecutive errors")
print(error_msg)
logging.error(f"Task {task_dict['task_id']} terminated due to consecutive errors")
agent.complete_flag = True
result['status'] = 'error_terminated'
break
else:
# Reset consecutive error counter on successful execution
consecutive_errors = 0
# Check if we've reached maximum operations or continuous no-ops
if agent.valid_op >= agent.config['agent']['max_auto_op']:
result['status'] = 'max_operations_reached'
task_logger.info(f"Task {task_id} reached maximum operations ({agent.config['agent']['max_auto_op']})")
print(f"Reached maximum operations ({agent.config['agent']['max_auto_op']})")
break
if agent.continuous_no_op >= agent.config['agent']['max_continuous_no_op']:
result['status'] = 'max_no_ops_reached'
task_logger.info(f"Task {task_id} reached maximum continuous no-op actions ({agent.config['agent']['max_continuous_no_op']})")
print(f"Reached maximum continuous no-op actions ({agent.config['agent']['max_continuous_no_op']})")
logging.info(f"Task {task_dict['task_id']} terminated due to exceeding max continuous no-op actions.")
break
# Record execution details
result['operations_count'] = agent.valid_op if agent else 0
result['execution_time'] = time.time() - start_time
# Log performance metrics
perf_logger.info(f"Task {task_id} - Operations: {result['operations_count']}, Time: {result['execution_time']:.2f}s")
# If we completed normally without errors, mark as success
if result['status'] == 'failed' and not result['error']:
result['status'] = 'completed'
task_logger.info(f"Task {task_id} completed successfully")
print(f"Task completed successfully: {task_dict['task_id']}")
except Exception as e:
result['error'] = str(e)
result['status'] = 'error'
result['execution_time'] = time.time() - start_time
task_logger.error(f"Task {task_id} failed with error: {e}", exc_info=True)
print(f"Error running task {task_dict['task_id']}: {e}")
logging.error(f"Task {task_dict['task_id']} failed: {e}")
finally:
# Stop the agent
try:
await agent.stop()
task_logger.info(f"Agent stopped for task {task_id}")
except Exception as stop_e:
task_logger.warning(f"Error stopping agent for task {task_id}: {stop_e}")
logging.warning(f"Error stopping agent for task {task_dict['task_id']}: {stop_e}")
# Try emergency save if agent.stop() fails
try:
if hasattr(agent, '_emergency_save'):
emergency_file = agent._emergency_save(f"Agent stop failed: {stop_e}")
if emergency_file:
task_logger.info(f"Emergency save completed for task {task_id}: {emergency_file}")
print(f"Emergency save completed for task {task_dict['task_id']}: {emergency_file}")
else:
task_logger.error(f"Emergency save also failed for task {task_id}")
print(f"Emergency save also failed for task {task_dict['task_id']}")
except Exception as emergency_e:
task_logger.critical(f"Emergency save failed for task {task_id}: {emergency_e}")
print(f"CRITICAL: Emergency save failed for task {task_dict['task_id']}: {emergency_e}")
perf_logger.info(f"Task {task_id} finished at {datetime.datetime.now()}")
return result
async def run_batch_mode(config):
"""
Run in batch mode - multiple tasks from JSON file with automatic task switching on failure.
Args:
config: Configuration dictionary loaded from TOML file
Returns:
dict: Batch execution summary with success/failure counts
"""
print("=== Batch Mode ===")
task_file_path = config['experiment']['task_file_path']
if not os.path.isabs(task_file_path):
# Make path relative to script directory
script_dir = os.path.dirname(os.path.abspath(__file__))
task_file_path = os.path.join(script_dir, task_file_path)
try:
with open(task_file_path, 'r', encoding='utf-8') as file:
query_tasks = json.load(file)
except FileNotFoundError:
print(f"Error: Task file '{task_file_path}' not found.")
return {'status': 'error', 'message': f'Task file not found: {task_file_path}'}
except json.JSONDecodeError:
print(f"Error: Task file '{task_file_path}' is not a valid JSON file.")
return {'status': 'error', 'message': f'Invalid JSON in task file: {task_file_path}'}
# Initialize batch execution summary
import time
batch_summary = {
'total_tasks': len(query_tasks),
'completed': 0,
'failed': 0,
'timeout': 0,
'error': 0,
'max_operations_reached': 0,
'max_no_ops_reached': 0,
'task_results': [],
'start_time': time.time(),
'end_time': None
}
print(f"\n{'='*60}")
print(f"BATCH MODE: Processing {len(query_tasks)} tasks")
print(f"Tasks file: {task_file_path}")
print(f"Output directory: {config['basic']['save_file_dir']}")
print(f"{'='*60}")
overwrite = config['experiment']['overwrite']
for i, task_dict in enumerate(query_tasks, 1):
print(f"\n--- Processing Task {i}/{len(query_tasks)} ---")
# Check if task already exists
task_result_path = os.path.join(config['basic']['save_file_dir'], task_dict['task_id'])
if os.path.exists(task_result_path) and not overwrite:
print(f"Task {task_dict['task_id']} already exists, skipping...")
continue
try:
# Run the task with retry logic for better error recovery
result = await run_single_task_with_retry(config, task_dict, max_retries=2)
# Update batch summary based on task result
status = result.get('status', 'failed')
batch_summary['task_results'].append(result)
if status == 'completed':
batch_summary['completed'] += 1
print(f"Task {task_dict['task_id']} completed successfully")
elif status == 'timeout':
batch_summary['timeout'] += 1
print(f"Task {task_dict['task_id']} timed out")
elif status == 'error':
batch_summary['error'] += 1
print(f"Task {task_dict['task_id']} failed with error: {result.get('error', 'Unknown error')}")
elif status == 'max_operations_reached':
batch_summary['max_operations_reached'] += 1
print(f"Task {task_dict['task_id']} reached maximum operations")
elif status == 'max_no_ops_reached':
batch_summary['max_no_ops_reached'] += 1
print(f"Task {task_dict['task_id']} reached maximum no-op actions")
else:
batch_summary['failed'] += 1
print(f"Task {task_dict['task_id']} failed")
# Log execution details
ops_count = result.get('operations_count', 0)
exec_time = result.get('execution_time', 0)
print(f" Operations: {ops_count}, Time: {exec_time:.1f}s")
except Exception as e:
# Handle unexpected errors during task execution
batch_summary['error'] += 1
error_result = {
'task_id': task_dict['task_id'],
'status': 'error',
'error': str(e),
'operations_count': 0,
'execution_time': 0
}
batch_summary['task_results'].append(error_result)
print(f"Unexpected error in task {task_dict['task_id']}: {e}")
logging.error(f"Unexpected error in task {task_dict['task_id']}: {e}", exc_info=True)
# Add small delay between tasks to prevent resource conflicts
if i < len(query_tasks):
await asyncio.sleep(2)
# Finalize batch summary
batch_summary['end_time'] = time.time()
total_time = batch_summary['end_time'] - batch_summary['start_time']
# Print final summary
print(f"\n{'='*60}")
print(f"BATCH EXECUTION COMPLETED")
print(f"{'='*60}")
print(f"Total tasks: {batch_summary['total_tasks']}")
print(f"Completed: {batch_summary['completed']}")
print(f"Failed: {batch_summary['failed']}")
print(f"Timeout: {batch_summary['timeout']}")
print(f"Max operations: {batch_summary['max_operations_reached']}")
print(f"Max no-ops: {batch_summary['max_no_ops_reached']}")
print(f"Errors: {batch_summary['error']}")
print(f"Total time: {total_time:.1f}s")
print(f"{'='*60}")
return batch_summary
async def run_single_task_with_retry(config, task_dict, max_retries=2):
"""
Run a single task with retry logic for better error recovery.
Args:
config: Agent configuration dictionary
task_dict: Task configuration dictionary
max_retries: Maximum number of retry attempts (default: 2)
Returns:
dict: Task execution result with status and details
"""
task_id = task_dict.get('task_id', 'unknown')
for attempt in range(max_retries + 1):
if attempt > 0:
print(f"\nRetry attempt {attempt}/{max_retries} for task {task_id}")
logging.info(f"Retrying task {task_id}, attempt {attempt}/{max_retries}")
# Add delay between retries to allow system recovery
await asyncio.sleep(5)
try:
result = await run_single_task(config, task_dict)
# Check if task completed successfully or reached expected termination
status = result.get('status', 'failed')
if status in ['completed', 'max_operations_reached', 'max_no_ops_reached']:
if attempt > 0:
print(f"Task {task_id} succeeded on retry attempt {attempt}")
return result
# If task failed due to timeout or error, consider retry
if status in ['timeout', 'error'] and attempt < max_retries:
error_msg = result.get('error', 'Unknown error')
print(f"WARNING: Task {task_id} failed (attempt {attempt + 1}): {error_msg}")
logging.warning(f"Task {task_id} failed on attempt {attempt + 1}: {error_msg}")
continue
# If we've exhausted retries or it's a non-retryable failure
return result
except Exception as e:
error_msg = str(e)
print(f"Unexpected error in task {task_id} (attempt {attempt + 1}): {error_msg}")
logging.error(f"Unexpected error in task {task_id} attempt {attempt + 1}: {error_msg}", exc_info=True)
if attempt < max_retries:
continue
# Return error result if all retries exhausted
return {
'task_id': task_id,
'status': 'error',
'error': error_msg,
'operations_count': 0,
'execution_time': 0,
'retry_attempts': attempt + 1
}
# This should not be reached, but just in case
return {
'task_id': task_id,
'status': 'failed',
'error': 'All retry attempts exhausted',
'operations_count': 0,
'execution_time': 0,
'retry_attempts': max_retries + 1
}
async def main():
"""Main function with enhanced error handling and retry logic"""
parser = argparse.ArgumentParser(
description="Run OpenFlo web automation tasks using the OpenFloAgent package"
)
parser.add_argument(
"-c", "--config_path",
help="Path to the TOML configuration file.",
type=str,
metavar='config',
default="config/demo_mode.toml"
)
args = parser.parse_args()
# Setup logging
setup_logging()
# Load configuration
script_dir = os.path.dirname(os.path.abspath(__file__))
config_path = args.config_path
if not os.path.isabs(config_path):
config_path = os.path.join(script_dir, config_path)
try:
with open(config_path, 'r') as toml_config_file:
config = toml.load(toml_config_file)
print(f"Configuration File Loaded - {config_path}")
except FileNotFoundError:
print(f"Error: File '{config_path}' not found.")
sys.exit(1)
except toml.TomlDecodeError:
print(f"Error: File '{config_path}' is not a valid TOML file.")
sys.exit(1)
# Validate API key configuration (OpenRouter/Gemini only)
api_keys_config = config.get('api_keys', {})
model_config = config.get('model', {})
model_name = model_config.get('name', 'openrouter/qwen/qwen-2.5-72b-instruct')
if any(k in model_name.lower() for k in ["claude", "qwen", "openrouter", "gemini"]):
# Prefer OpenRouter; allow Gemini if configured
openrouter_key = api_keys_config.get('openrouter_api_key') or os.getenv('OPENROUTER_API_KEY')
gemini_key = api_keys_config.get('gemini_api_key') or os.getenv('GEMINI_API_KEY')
if not (openrouter_key or gemini_key):
print("Error: Missing API key. Set OPENROUTER_API_KEY (preferred) or GEMINI_API_KEY.")
sys.exit(1)
# Create save directory if it doesn't exist
save_dir = config['basic']['save_file_dir']
if not os.path.isabs(save_dir):
save_dir = os.path.join(script_dir, save_dir)
os.makedirs(save_dir, exist_ok=True)
config['basic']['save_file_dir'] = save_dir
# Run in appropriate mode based on config
if 'experiment' in config and 'task_file_path' in config['experiment']:
# Batch mode: has experiment section with task_file_path
await run_batch_mode(config)
else:
# Single task mode: has basic task and website configuration
import uuid
task_dict = {
'task_id': str(uuid.uuid4())[:8], # Generate a short task ID
'confirmed_task': config['basic']['default_task'],
'website': config['basic']['default_website']
}
await run_single_task(config, task_dict)
if __name__ == "__main__":
asyncio.run(main())