diff --git a/src/services/telegram_service.py b/src/services/telegram_service.py index 2c5f831..102eb35 100644 --- a/src/services/telegram_service.py +++ b/src/services/telegram_service.py @@ -359,7 +359,7 @@ class TelegramService: async def _handle_post_type_selected( self, chat_id: str, user_id: str, post_type_id_str: str, conv: dict, message_id: int, db ) -> None: - """Generate a post after the user selects a post type.""" + """Generate a post after the user selects a post type (Writer-only, no critic loop).""" topic_text = conv.get("topic", "") if not topic_text: await self.send_message(chat_id, "❌ Kein Thema gefunden. Bitte starte von vorne.") @@ -374,29 +374,72 @@ class TelegramService: return # Edit the post-type selection message to show progress - await self.edit_message(chat_id, message_id, "⏳ Erstelle deinen Post... Das kann einen Moment dauern.") + await self.edit_message(chat_id, message_id, "⏳ Erstelle deinen Post...") try: - from src.orchestrator import orchestrator - result = await orchestrator.create_post( - user_id=UUID(user_id), - topic={"title": topic_text, "description": topic_text}, - post_type_id=UUID(post_type_id_str), - max_iterations=2 # Fewer iterations for faster Telegram response + user_uuid = UUID(user_id) + + # Load everything the writer needs + post_type = await db.get_post_type(UUID(post_type_id_str)) + if not post_type: + await self.send_message(chat_id, "❌ Post-Typ nicht gefunden.") + return + + profile_analysis = await db.get_profile_analysis(user_uuid) + if not profile_analysis: + await self.send_message(chat_id, "❌ Profil-Analyse nicht gefunden. Bitte richte zuerst deinen Account in der App ein.") + return + + # Style examples — prefer type-specific, fall back to all + linkedin_posts = await db.get_posts_by_type(user_uuid, UUID(post_type_id_str)) + if len(linkedin_posts) < 3: + linkedin_posts = await db.get_linkedin_posts(user_uuid) + example_post_texts = [ + p.post_text for p in linkedin_posts + if p.post_text and len(p.post_text) > 100 + ][:10] + + # Company strategy if available + company_strategy = None + profile = await db.get_profile(user_uuid) + if profile and profile.company_id: + company = await db.get_company(profile.company_id) + if company and company.company_strategy: + company_strategy = company.company_strategy + + # Single writer pass — no critic loop + from src.agents.writer import WriterAgent + writer = WriterAgent() + post_content = await writer.process( + topic={"title": topic_text, "fact": topic_text, "relevance": "User-specified topic"}, + profile_analysis=profile_analysis.full_analysis, + example_posts=example_post_texts, + post_type=post_type, + user_thoughts=topic_text, + company_strategy=company_strategy, + strategy_weight=post_type.strategy_weight, ) - post_content = result.get("final_post", "") - post_id = str(result.get("post_id", "")) + # Save the post to DB (status=draft, no critic data) + from src.database.models import GeneratedPost + saved = await db.save_generated_post(GeneratedPost( + user_id=user_uuid, + topic_title=topic_text[:200], + post_content=post_content, + iterations=1, + writer_versions=[post_content], + status="draft", + post_type_id=UUID(post_type_id_str), + )) # Update conversation state - new_conv = { + await self._set_conv(chat_id, { "state": "waiting_feedback", "user_id": user_id, "topic": topic_text, - "post_id": post_id, - "post_content": post_content - } - await self._set_conv(chat_id, new_conv) + "post_id": str(saved.id), + "post_content": post_content, + }) await self.send_message( chat_id,