| |
| |
| |
| from typing import Dict, List, Optional |
| import logging |
|
|
| from pydantic import BaseModel, Field |
| from typing import List, Dict, Any, Optional |
|
|
| class ChatLogRequest(BaseModel): |
| chatId: str = Field(..., description="Unique identifier for the chat session") |
| messages: List[Dict[str, Any]] = Field(..., description="List of message objects containing content, role, etc.") |
| title: Optional[str] = "Chat Conversation Log" |
|
|
| class ChatLogResponse(BaseModel): |
| success: bool |
| pdf_url: str |
| request_id: str |
| message: Optional[str] = None |
|
|
| from download_messages_service import ChatLogGenerator |
|
|
| logger = logging.getLogger(__name__) |
|
|
|
|
| def generate_chat_pdf(messages: List[Dict], |
| title: str = "Chat Conversation Log", |
| chat_id: str = None, |
| output_dir: str = "chat_pdfs", |
| participants: List[str] = None, |
| date_range: str = None, |
| cover_page: bool = True, |
| include_stats: bool = True) -> Optional[str]: |
| """ |
| Quick helper method to generate a chat PDF and return the file path. |
| |
| Args: |
| messages: List of message dictionaries |
| title: Title of the chat log |
| chat_id: Unique chat identifier |
| output_dir: Directory to save PDF |
| participants: List of participant names |
| date_range: Date range of conversation |
| cover_page: Whether to include cover page |
| include_stats: Whether to include statistics |
| |
| Returns: |
| Full path to generated PDF file, or None if failed |
| |
| Example: |
| messages = [{"role": "user", "content": "Hello", "createdAt": "2026-01-10T09:00:00Z"}] |
| pdf_path = generate_chat_pdf(messages, title="My Chat", chat_id="abc123") |
| logger.info(f"PDF saved at: {pdf_path}") |
| """ |
| try: |
| logger.info(f"Generating chat PDF: {title}") |
| |
| generator = ChatLogGenerator( |
| title=title, |
| chat_id=chat_id, |
| participants=participants, |
| date_range=date_range, |
| output_dir=output_dir |
| ) |
| |
| |
| filename = generator._generate_unique_filename() |
| |
| |
| success = generator.generate( |
| messages=messages, |
| filename=filename, |
| cover_page=cover_page, |
| include_stats=include_stats |
| ) |
| |
| if success: |
| logger.info(f"PDF successfully generated: {filename}") |
| return filename |
| else: |
| logger.error("PDF generation failed") |
| return None |
| |
| except Exception as e: |
| logger.exception(f"Error generating PDF: {e}") |
| return None |
|
|
|
|