mirror of
https://github.com/farcasclaudiu/TradingAgents.git
synced 2026-06-28 19:01:16 +03:00
refactor: clean up codebase and streamline documentation
- Remove debug prints from vendor routing (interface.py) - Simplify vendor fallback to only handle rate limits - Reorder CLI provider menu: OpenAI, Google, Anthropic, xAI, OpenRouter, Ollama - Remove dead files: local.py, reddit_utils.py, openai.py, google.py, googlenews_utils.py, yfin_utils.py
This commit is contained in:
@@ -135,103 +135,28 @@ def route_to_vendor(method: str, *args, **kwargs):
|
||||
"""Route method calls to appropriate vendor implementation with fallback support."""
|
||||
category = get_category_for_method(method)
|
||||
vendor_config = get_vendor(category, method)
|
||||
|
||||
# Handle comma-separated vendors
|
||||
primary_vendors = [v.strip() for v in vendor_config.split(',')]
|
||||
|
||||
if method not in VENDOR_METHODS:
|
||||
raise ValueError(f"Method '{method}' not supported")
|
||||
|
||||
# Get all available vendors for this method for fallback
|
||||
# Build fallback chain: primary vendors first, then remaining available vendors
|
||||
all_available_vendors = list(VENDOR_METHODS[method].keys())
|
||||
|
||||
# Create fallback vendor list: primary vendors first, then remaining vendors as fallbacks
|
||||
fallback_vendors = primary_vendors.copy()
|
||||
for vendor in all_available_vendors:
|
||||
if vendor not in fallback_vendors:
|
||||
fallback_vendors.append(vendor)
|
||||
|
||||
# Debug: Print fallback ordering
|
||||
primary_str = " → ".join(primary_vendors)
|
||||
fallback_str = " → ".join(fallback_vendors)
|
||||
print(f"DEBUG: {method} - Primary: [{primary_str}] | Full fallback order: [{fallback_str}]")
|
||||
|
||||
# Track results and execution state
|
||||
results = []
|
||||
vendor_attempt_count = 0
|
||||
any_primary_vendor_attempted = False
|
||||
successful_vendor = None
|
||||
|
||||
for vendor in fallback_vendors:
|
||||
if vendor not in VENDOR_METHODS[method]:
|
||||
if vendor in primary_vendors:
|
||||
print(f"INFO: Vendor '{vendor}' not supported for method '{method}', falling back to next vendor")
|
||||
continue
|
||||
|
||||
vendor_impl = VENDOR_METHODS[method][vendor]
|
||||
is_primary_vendor = vendor in primary_vendors
|
||||
vendor_attempt_count += 1
|
||||
impl_func = vendor_impl[0] if isinstance(vendor_impl, list) else vendor_impl
|
||||
|
||||
# Track if we attempted any primary vendor
|
||||
if is_primary_vendor:
|
||||
any_primary_vendor_attempted = True
|
||||
try:
|
||||
return impl_func(*args, **kwargs)
|
||||
except AlphaVantageRateLimitError:
|
||||
continue # Only rate limits trigger fallback
|
||||
|
||||
# Debug: Print current attempt
|
||||
vendor_type = "PRIMARY" if is_primary_vendor else "FALLBACK"
|
||||
print(f"DEBUG: Attempting {vendor_type} vendor '{vendor}' for {method} (attempt #{vendor_attempt_count})")
|
||||
|
||||
# Handle list of methods for a vendor
|
||||
if isinstance(vendor_impl, list):
|
||||
vendor_methods = [(impl, vendor) for impl in vendor_impl]
|
||||
print(f"DEBUG: Vendor '{vendor}' has multiple implementations: {len(vendor_methods)} functions")
|
||||
else:
|
||||
vendor_methods = [(vendor_impl, vendor)]
|
||||
|
||||
# Run methods for this vendor
|
||||
vendor_results = []
|
||||
for impl_func, vendor_name in vendor_methods:
|
||||
try:
|
||||
print(f"DEBUG: Calling {impl_func.__name__} from vendor '{vendor_name}'...")
|
||||
result = impl_func(*args, **kwargs)
|
||||
vendor_results.append(result)
|
||||
print(f"SUCCESS: {impl_func.__name__} from vendor '{vendor_name}' completed successfully")
|
||||
|
||||
except AlphaVantageRateLimitError as e:
|
||||
if vendor == "alpha_vantage":
|
||||
print(f"RATE_LIMIT: Alpha Vantage rate limit exceeded, falling back to next available vendor")
|
||||
print(f"DEBUG: Rate limit details: {e}")
|
||||
# Continue to next vendor for fallback
|
||||
continue
|
||||
except Exception as e:
|
||||
# Log error but continue with other implementations
|
||||
print(f"FAILED: {impl_func.__name__} from vendor '{vendor_name}' failed: {e}")
|
||||
continue
|
||||
|
||||
# Add this vendor's results
|
||||
if vendor_results:
|
||||
results.extend(vendor_results)
|
||||
successful_vendor = vendor
|
||||
result_summary = f"Got {len(vendor_results)} result(s)"
|
||||
print(f"SUCCESS: Vendor '{vendor}' succeeded - {result_summary}")
|
||||
|
||||
# Stopping logic: Stop after first successful vendor for single-vendor configs
|
||||
# Multiple vendor configs (comma-separated) may want to collect from multiple sources
|
||||
if len(primary_vendors) == 1:
|
||||
print(f"DEBUG: Stopping after successful vendor '{vendor}' (single-vendor config)")
|
||||
break
|
||||
else:
|
||||
print(f"FAILED: Vendor '{vendor}' produced no results")
|
||||
|
||||
# Final result summary
|
||||
if not results:
|
||||
print(f"FAILURE: All {vendor_attempt_count} vendor attempts failed for method '{method}'")
|
||||
raise RuntimeError(f"All vendor implementations failed for method '{method}'")
|
||||
else:
|
||||
print(f"FINAL: Method '{method}' completed with {len(results)} result(s) from {vendor_attempt_count} vendor attempt(s)")
|
||||
|
||||
# Return single result if only one, otherwise concatenate as string
|
||||
if len(results) == 1:
|
||||
return results[0]
|
||||
else:
|
||||
# Convert all results to strings and concatenate
|
||||
return '\n'.join(str(result) for result in results)
|
||||
raise RuntimeError(f"No available vendor for '{method}'")
|
||||
Reference in New Issue
Block a user