import logging import uuid from ..config import settings from ..domain.services.adventure_service import AdventureService from ..outbound.anthropic.anthropic_client import AnthropicClient from ..outbound.deepl.deepl_client import DeepLClient from ..outbound.gemini.gemini_client import GeminiClient from ..outbound.postgres.database import AsyncSessionLocal from ..outbound.postgres.repositories.adventure_repository import ( PostgresAdventureEntryAudioRepository, PostgresAdventureEntryChoiceRepository, PostgresAdventureEntryDecisionRepository, PostgresAdventureEntryRepository, PostgresAdventureEntryTranslationRepository, PostgresAdventureRepository, ) from ..outbound.spacy.spacy_client import SpacyClient from ..outbound.stubs import ( StubAnthropicClient, StubDeepLClient, StubGeminiClient, StubSpacyClient, ) from .app import procrastinate_app logger = logging.getLogger(__name__) def _make_adventure_service(db) -> AdventureService: if settings.stub_generation: anthropic = StubAnthropicClient() # type: ignore[assignment] gemini = StubAnthropicClient() # type: ignore[assignment] deepl = StubDeepLClient() # type: ignore[assignment] gemini = StubGeminiClient() # type: ignore[assignment] spacy = StubSpacyClient() # type: ignore[assignment] else: anthropic = AnthropicClient.new(settings.anthropic_api_key) gemini = GeminiClient.new(settings.gemini_api_key) deepl = DeepLClient(settings.deepl_api_key) gemini = GeminiClient(settings.gemini_api_key) spacy = SpacyClient() gen_ai_client = gemini if settings.story_generation_api_provider == "anthropic": gen_ai_client = anthropic return AdventureService( adventure_repo=PostgresAdventureRepository(db), entry_repo=PostgresAdventureEntryRepository(db), choice_repo=PostgresAdventureEntryChoiceRepository(db), decision_repo=PostgresAdventureEntryDecisionRepository(db), translation_repo=PostgresAdventureEntryTranslationRepository(db), audio_repo=PostgresAdventureEntryAudioRepository(db), gen_ai_client=gen_ai_client, deepl_client=deepl, gemini_client=gemini, spacy_client=spacy, ) @procrastinate_app.task(queue="adventure_pipeline") async def generate_adventure_entry( adventure_id: str, entry_id: str, user_id: str ) -> None: print( f"Starting task for adventure_id={adventure_id}, entry_id={entry_id}, user_id={user_id}" ) async with AsyncSessionLocal() as db: service = _make_adventure_service(db) await service.run_entry_pipeline( uuid.UUID(adventure_id), uuid.UUID(entry_id), uuid.UUID(user_id), )