rmi-backend/app/_archive/legacy_2026_07/alibaba_connector.py
cryptorugmunch 628c1d2a10
Some checks failed
CI / build (push) Failing after 3s
refactor(rmi-backend,audit): mount Wave 3 + archive 136 dead-code files (P2.3)
PHASE 2.3 (AUDIT-2026-Q3.md):

Task 1 — Wire-in Wave 3 (1 router mounted, 2 deferred):
  - app.routers.unified_scanner_router mounted at /api/v2/scanner/* (2 routes:
    POST /api/v2/scanner/token/scan, POST /api/v2/scanner/wallet/scan).
    Refactored prefix from /api/v2 -> /api/v2/scanner to avoid future conflicts
    with the v1 /api/v1/scanner/ stub.
  - app.routers.unified_wallet_scanner DEFERRED (no router APIRouter attribute;
    library module consumed by unified_scanner_router via get_wallet_scanner()).
  - app.routers.admin_extensions DEFERRED (DORMANT per audit; 25 routes at
    /api/v1/admin/* would shadow /api/v1/admin/alerts_webhook).

Task 2 — Archive 136 dead-code files to app/_archive/legacy_2026_07/:
  - 73 routers in app/routers/ (reach graph showed zero reach into mount.py).
  - 63 flat app/*.py (domain modules never imported by live code).
  - 1 file RESTORED post-archive: app/routers/x402_bridge_health.py (caught by
    tests/unit/test_bridge_health.py which directly imports it; reach graph
    considered tests/ only as transitive reach — to be patched in next cycle).

Forced-LIVE (NOT archived per user directive):
  - app/ai_pipeline_v3.py  (3 importers in audit window, importers themselves DEAD)
  - app/splade_bm25.py       (LIVE via app.rag_service)
  - app/wallet_manager_v2.py (LIVE via x402_enforcement, x402_tools, sweep_all, sweep_now)
  - app/crypto_embeddings.py (NOT in audit ARCHIVE list; heavy import graph)

Verification (forward-import closure from mount.py + main.py + factory.py + lifespan.py):
  - imports = 348 app.* modules
  - reached = 194 files reachable from roots
  - archive set = audit_dead (186) - reached - forced_live (4) - test_live (1) = 136
  - Net delta: 136 files moved, 44,932 LOC reduction, 293->295 active routes (+2 from Wave 3)

pyproject.toml updates:
  - setuptools.packages.find: added exclude for app._archive*
  - ruff.extend-exclude: added "app/_archive/"
  - mypy.exclude: added "app/_archive/"

Smoke test: pytest tests/ — 817 passed, 3 pre-existing failures unchanged
(0 new failures; 0 routes lost; all 4 forced-LIVE files still importable).

Restoration: git mv app/_archive/legacy_2026_07/<name>.py <original-path>
and add the import to app/mount.py ROUTER_MODULES.

Refs: AUDIT-2026-Q3.md /home/dev/pry/rmi-final-deadcode-2026-07-06.md
2026-07-06 20:52:31 +02:00

218 lines
7.5 KiB
Python

"""
Alibaba Cloud Connector - Tongyi Wanxiang AI for Image Generation.
Generate professional graphics for cards, scorecards, marketing assets.
"""
import logging
import os
import httpx
logger = logging.getLogger(__name__)
# ── Alibaba Cloud Config ─────────────────────────────────────
DASHSCOPE_API_KEY = os.getenv("DASHSCOPE_API_KEY", "")
DASHSCOPE_BASE_URL = "https://dashscope.aliyuncs.com/api/v1"
# Tongyi Wanxiang endpoints
IMAGE_GENERATION_ENDPOINT = f"{DASHSCOPE_BASE_URL}/services/aigc/text-generation/generation"
class AlibabaConnector:
"""Alibaba Cloud AI services connector."""
def __init__(self):
self.api_key = DASHSCOPE_API_KEY
self._session = None
def _get_session(self):
if self._session is None:
self._session = httpx.AsyncClient(
timeout=60.0,
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
},
)
return self._session
async def generate_image(
self,
prompt: str,
size: str = "1024x1024",
style: str = "professional",
negative_prompt: str | None = None,
) -> dict:
"""
Generate image using Tongyi Wanxiang.
Args:
prompt: Text description of image to generate
size: Image size (e.g., "1024x1024", "1200x675")
style: Art style ("professional", "cartoon", "realistic", etc.)
negative_prompt: What to avoid in the image
Returns:
Dict with image_url, thumbnail_url, and metadata
"""
if not self.api_key:
logger.error("DASHSCOPE_API_KEY not configured")
return {"error": "Alibaba API key not configured"}
# Parse size
width, height = size.split("x")
# Build request
payload = {
"model": "wanx-v1", # Tongyi Wanxiang model
"input": {
"prompt": prompt,
"negative_prompt": negative_prompt or "blurry, low quality, distorted, ugly, text, watermark",
"size": f"{width}*{height}",
"style": style,
},
"parameters": {
"n": 1, # Number of images
"seed": 42, # For reproducibility
},
}
try:
session = self._get_session()
response = await session.post(IMAGE_GENERATION_ENDPOINT, json=payload)
if response.status_code == 200:
result = response.json()
# Extract image URLs
images = result.get("output", {}).get("results", [])
if images and len(images) > 0:
return {
"status": "success",
"image_url": images[0].get("url"),
"thumbnail_url": images[0].get("thumbnail_url"),
"id": images[0].get("task_id"),
"prompt": prompt,
"size": size,
"style": style,
}
else:
return {"error": "No images generated", "raw": result}
else:
logger.error(f"Alibaba API error: {response.status_code} - {response.text[:200]}")
return {
"error": f"API error: {response.status_code}",
"details": response.text[:500],
}
except Exception as e:
logger.error(f"Alibaba image generation failed: {e}")
return {"error": str(e)}
async def generate_marketing_image(self, campaign_type: str, content: dict) -> dict:
"""Generate marketing image for campaigns."""
prompts = {
"launch": """
Professional crypto platform launch announcement,
dark theme, neon accents, "RMI Intelligence Platform" text,
futuristic trading interface background,
high quality, 4K, professional marketing graphic
""",
"feature_showcase": f"""
Professional feature showcase graphic,
"{content.get("feature_name", "Feature")}" prominently displayed,
trading platform UI elements, charts, graphs,
dark mode, neon green accents,
clean modern design, marketing quality
""",
"stats_announcement": f"""
Professional stats announcement graphic,
"{content.get("stat_value", "1000")}" large number display,
"{content.get("stat_label", "Users")}" label,
crypto trading platform aesthetic,
dark background, neon accents,
high quality marketing graphic
""",
"kol_ranking": """
Professional KOL ranking graphic,
leaderboard style, top 10 layout,
crypto influencer theme,
dark mode, purple and gold accents,
trading platform quality,
high resolution marketing graphic
""",
}
prompt = prompts.get(campaign_type, content.get("custom_prompt", ""))
return await self.generate_image(
prompt=prompt,
size="1200x628", # Facebook/Twitter link preview size
style="professional",
negative_prompt="blurry, low quality, distorted, ugly, amateur, cluttered",
)
async def generate_social_post_image(self, post_type: str, data: dict) -> dict:
"""Generate image for social media posts."""
if post_type == "win_alert":
prompt = f"""
Big win celebration graphic,
crypto trading win alert,
"+${data.get("pnl_usd", 0):,.0f}" large display,
green neon style,
dark background,
professional trading platform aesthetic,
high quality social media graphic
"""
elif post_type == "loss_alert":
prompt = f"""
Loss porn graphic,
crypto trading loss alert,
"-${data.get("pnl_usd", 0):,.0f}" large display,
red neon style,
dark background,
professional trading platform aesthetic,
high quality social media graphic
"""
elif post_type == "rug_alert":
prompt = """
Rugpull warning graphic,
crypto scam alert,
"RUG PULL" large warning text,
orange and red warning colors,
dark background,
professional security alert aesthetic,
high quality social media graphic
"""
else:
prompt = data.get("custom_prompt", "Professional crypto graphic")
return await self.generate_image(
prompt=prompt,
size="1200x675", # Twitter optimized
style="professional",
negative_prompt="blurry, low quality, distorted, ugly, text overlay, watermark",
)
def status(self) -> dict:
"""Check connector status."""
return {
"api_key_configured": bool(self.api_key),
"api_key_prefix": self.api_key[:20] + "..." if self.api_key else "NOT SET",
"base_url": DASHSCOPE_BASE_URL,
"models_available": ["wanx-v1"],
}
# Singleton
_alibaba: AlibabaConnector | None = None
def get_alibaba_connector() -> AlibabaConnector:
global _alibaba
if _alibaba is None:
_alibaba = AlibabaConnector()
return _alibaba