import json from datetime import datetime import pickle from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet import requests def export_txt(conversation, topic, filename="conversation.txt"): try: with open(filename, "w", encoding="utf-8") as f: f.write(f"Topic: {topic}\n\n") for turn in conversation: f.write(f"{turn['agent']}: {turn['text']}\n") return filename except Exception as e: logging.error(f"[export_txt] Failed to write file: {e}") return None def export_json(conversation: list, topic: str, turns: int) -> str: filename = f"discussion_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json" data = {"topic": topic, "turns": turns, "conversation": conversation} with open(filename, 'w') as f: json.dump(data, f, indent=2) return filename def export_pdf(conversation: list, topic: str, turns: int) -> str: filename = f"discussion_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf" doc = SimpleDocTemplate(filename, pagesize=letter) styles = getSampleStyleSheet() story = [Paragraph(f"Discussion: {topic}", styles['Title']), Spacer(1, 12)] for msg in conversation: story.append(Paragraph(f"{msg['agent']} (Turn {msg.get('turn')}):", styles['Normal'])) story.append(Paragraph(msg['text'], styles['BodyText'])) story.append(Spacer(1, 12)) doc.build(story) return filename def send_webhook(url: str, conversation: list, topic: str, turns: int) -> str: if not url.startswith('http'): return '⚠️ Invalid URL' payload = {"topic": topic, "turns": turns, "conversation": conversation} try: resp = requests.post(url, json=payload, timeout=10) if resp.status_code == 200: return '✅ Sent successfully' return f"⚠️ Error {resp.status_code}: {resp.text}" except Exception as e: return f"⚠️ Exception: {e}"