import asyncio import sys import os from datetime import datetime, timezone # Add backend to sys.path sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) from app.database import init_db, AsyncSessionLocal from app.models import Channel, RawPost, ContentDraft, FeedbackLog, PublishQueue from app.agents.agent_a_scraper import run_channel_ingestion, seed_default_channels from app.agents.agent_b_curator import generate_draft_for_post, record_human_feedback from app.agents.agent_c_publisher import dispatch_pending_queue from sqlalchemy import select async def main(): print("=" * 60) print("1. INITIALIZING DATABASE TABLES...") await init_db() print("-> Tables initialized successfully.") async with AsyncSessionLocal() as session: print("\n2. RUNNING SEED DISCOVERY & CHANNEL REGISTRATION...") await seed_default_channels(session) res = await session.execute(select(Channel)) channels = res.scalars().all() print(f"-> Registered {len(channels)} channels:") for ch in channels: print(f" • @{ch.telegram_handle} ({ch.niche})") print("\n3. RUNNING AGENT A (SCRAPER) ON CHANNELS...") total_ingested = 0 for ch in channels[:2]: print(f" Scraping channel @{ch.telegram_handle}...") count = await run_channel_ingestion(session, ch, limit=5) print(f" -> Ingested {count} new posts.") total_ingested += count # Query raw posts stmt = select(RawPost).limit(3) res = await session.execute(stmt) sample_posts = res.scalars().all() for i, post in enumerate(sample_posts, 1): print(f" Post #{i} [ID: {post.message_id}]: {post.views} views, Media: {post.media_type}") print(f" Original preview: {post.original_text[:80]}...") if sample_posts: target_post = sample_posts[0] print(f"\n4. RUNNING AGENT B (CURATOR & COPYWRITER) ON POST {target_post.message_id}...") draft = await generate_draft_for_post(session, target_post.id) print("-> Generated Draft ID:", draft.id) print("-> Status:", draft.status) print("-> Generated Persian NetVibe Copy:\n" + "-"*40) print(draft.current_text) print("-" * 40) print("\n5. SIMULATING HUMAN EDITOR EDIT & ACTIVE LEARNING DIFF...") edited_text = draft.current_text + "\n\n💡 نکته ویژه: برای اتصال پرسرعت کانفیگ اختصاصی تست ۲۴ ساعته در بات ما فعاله!" feedback = await record_human_feedback( session=session, draft_id=draft.id, original_ai_text=draft.ai_generated_text, final_human_text=edited_text ) print("-> Feedback log recorded successfully:") print(f" Similarity Ratio: {feedback.diff_analysis.get('similarity_ratio')}") print(f" Summary: {feedback.diff_analysis.get('summary')}") print("\n6. MOVING DRAFT TO PUBLISH QUEUE...") q_item = PublishQueue( draft_id=draft.id, status='queued', scheduled_time=datetime.now(timezone.utc) ) session.add(q_item) draft.status = 'approved' await session.commit() print("-> Draft approved and queued for dispatch.") print("\n7. RUNNING AGENT C (TELEGRAM PUBLISHER DISPATCHER)...") count = await dispatch_pending_queue(session) print(f"-> Agent C processed and dispatched {count} items in queue.") print("\n" + "=" * 60) print("ALL TESTS COMPLETED SUCCESSFULLY!") if __name__ == "__main__": asyncio.run(main())