File size: 2,930 Bytes
585bc48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f161586
585bc48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# ---------------------------------------------------------
# HELPER METHOD FOR QUICK PDF GENERATION
# ---------------------------------------------------------
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
        )
        
        # Generate unique filename
        filename = generator._generate_unique_filename()
        
        # Generate PDF
        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