From 8ad62993818d892a8574f408d491dcb3b59494a7 Mon Sep 17 00:00:00 2001 From: Maciej Pienczyn Date: Sat, 7 Feb 2026 12:57:46 +0100 Subject: [PATCH] fix(audit-ai): Add dynamic fallback for unknown action_types in content generation AI analysis generates action_types dynamically (e.g. add_sitemap, add_nap) that don't always match predefined CONTENT_PROMPTS. Added fallback that builds a generic prompt using the action title/description from the DB. Co-Authored-By: Claude Opus 4.6 --- audit_ai_service.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/audit_ai_service.py b/audit_ai_service.py index f93e057..0824394 100644 --- a/audit_ai_service.py +++ b/audit_ai_service.py @@ -713,9 +713,6 @@ def generate_content(company_id: int, action_type: str, context: dict = None, us Returns: dict with 'content' key """ - if action_type not in CONTENT_PROMPTS: - return {'error': f'Nieznany typ akcji: {action_type}'} - db = SessionLocal() try: company = db.query(Company).filter_by(id=company_id, status='active').first() @@ -765,8 +762,33 @@ def generate_content(company_id: int, action_type: str, context: dict = None, us 'platforms_missing': ', '.join(missing) if missing else 'brak', }) - # Build prompt from template - prompt_template = CONTENT_PROMPTS[action_type] + # Build prompt from template or generate dynamic fallback + if action_type in CONTENT_PROMPTS: + prompt_template = CONTENT_PROMPTS[action_type] + else: + # Dynamic fallback — look up action title/description from DB + existing_action = db.query(AuditAction).filter_by( + company_id=company_id, + action_type=action_type, + status='suggested' + ).order_by(AuditAction.created_at.desc()).first() + action_title = existing_action.title if existing_action else action_type.replace('_', ' ') + action_desc = existing_action.description if existing_action else '' + prompt_template = f"""Jesteś ekspertem od marketingu cyfrowego i SEO dla lokalnych firm w Polsce. + +Firma: {{company_name}} +Branża: {{category}} +Miasto: {{city}} +Strona: {{website}} +Usługi: {{services}} + +ZADANIE: {action_title} +{('KONTEKST: ' + action_desc) if action_desc else ''} + +Przygotuj konkretną, gotową do wdrożenia treść lub instrukcję krok po kroku dla tego zadania. +Pisz po polsku, bezpośrednio do właściciela firmy. +Bądź konkretny — podaj gotowe przykłady kodu, tekstów lub konfiguracji, które właściciel może skopiować i użyć. +Max 500 słów.""" try: prompt = prompt_template.format(**prompt_context) except KeyError as e: