Spaces:
Running
Running
File size: 7,930 Bytes
7a527fe |
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
"""
Export Router for Conversation History
"""
import io
import sys
import os
from datetime import datetime
from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
from docx import Document
from docx.shared import Pt, RGBColor, Inches
from docx.enum.text import WD_ALIGN_PARAGRAPH
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.lib.colors import Color, HexColor
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.enums import TA_LEFT, TA_RIGHT
# Add src to path for imports
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
from core.agent import _memory_manager
router = APIRouter(tags=["export"])
def get_conversation_history(session_id: str):
"""
Retrieve conversation history from memory manager.
Args:
session_id: Session identifier
Returns:
List of message dictionaries with 'role' and 'content'
"""
try:
memory = _memory_manager.get_memory(session_id)
messages = []
# Get chat history from memory
if hasattr(memory, 'chat_memory') and hasattr(memory.chat_memory, 'messages'):
for msg in memory.chat_memory.messages:
# LangChain messages have type and content
role = "User" if msg.type == "human" else "Assistant"
messages.append({
"role": role,
"content": msg.content
})
return messages
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error retrieving conversation history: {str(e)}"
)
def generate_docx(session_id: str, messages: list) -> io.BytesIO:
"""
Generate a Word document from conversation history.
Args:
session_id: Session identifier
messages: List of message dictionaries
Returns:
BytesIO object containing the Word document
"""
doc = Document()
# Add title
title = doc.add_heading('Conversation History', 0)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER
# Add metadata
doc.add_paragraph(f'Session ID: {session_id}')
doc.add_paragraph(f'Export Date: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
doc.add_paragraph('') # Empty line
# Add separator
doc.add_paragraph('_' * 80)
doc.add_paragraph('')
# Add messages
for msg in messages:
# Add role heading
role_para = doc.add_paragraph()
role_run = role_para.add_run(f"{msg['role']}:")
role_run.bold = True
role_run.font.size = Pt(12)
if msg['role'] == 'User':
role_run.font.color.rgb = RGBColor(255, 107, 53) # Orange color
else:
role_run.font.color.rgb = RGBColor(51, 51, 51) # Dark gray
# Add message content
content_para = doc.add_paragraph(msg['content'])
content_para.paragraph_format.left_indent = Inches(0.25)
# Add spacing
doc.add_paragraph('')
# Save to BytesIO
buffer = io.BytesIO()
doc.save(buffer)
buffer.seek(0)
return buffer
def generate_pdf(session_id: str, messages: list) -> io.BytesIO:
"""
Generate a PDF document from conversation history.
Args:
session_id: Session identifier
messages: List of message dictionaries
Returns:
BytesIO object containing the PDF document
"""
buffer = io.BytesIO()
doc = SimpleDocTemplate(buffer, pagesize=letter,
rightMargin=72, leftMargin=72,
topMargin=72, bottomMargin=18)
# Container for the 'Flowable' objects
elements = []
# Define styles
styles = getSampleStyleSheet()
# Title style
title_style = ParagraphStyle(
'CustomTitle',
parent=styles['Heading1'],
fontSize=24,
textColor=HexColor('#000000'),
spaceAfter=30,
alignment=TA_LEFT
)
# Metadata style
meta_style = ParagraphStyle(
'MetaStyle',
parent=styles['Normal'],
fontSize=10,
textColor=HexColor('#666666'),
spaceAfter=6
)
# User message style
user_style = ParagraphStyle(
'UserStyle',
parent=styles['Normal'],
fontSize=11,
textColor=HexColor('#FF6B35'),
fontName='Helvetica-Bold',
spaceAfter=6
)
# Assistant message style
assistant_style = ParagraphStyle(
'AssistantStyle',
parent=styles['Normal'],
fontSize=11,
textColor=HexColor('#333333'),
fontName='Helvetica-Bold',
spaceAfter=6
)
# Content style
content_style = ParagraphStyle(
'ContentStyle',
parent=styles['Normal'],
fontSize=10,
leftIndent=20,
spaceAfter=12
)
# Add title
elements.append(Paragraph('Conversation History', title_style))
elements.append(Spacer(1, 12))
# Add metadata
elements.append(Paragraph(f'Session ID: {session_id}', meta_style))
elements.append(Paragraph(f'Export Date: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}', meta_style))
elements.append(Spacer(1, 20))
# Add separator line
elements.append(Paragraph('_' * 100, meta_style))
elements.append(Spacer(1, 12))
# Add messages
for msg in messages:
# Add role
role_style = user_style if msg['role'] == 'User' else assistant_style
elements.append(Paragraph(f"{msg['role']}:", role_style))
# Add content (escape HTML special characters)
content = msg['content'].replace('&', '&').replace('<', '<').replace('>', '>')
elements.append(Paragraph(content, content_style))
elements.append(Spacer(1, 12))
# Build PDF
doc.build(elements)
buffer.seek(0)
return buffer
@router.get("/export/{format}")
async def export_conversation(format: str, session_id: str = "default"):
"""
Export conversation history as Word (.docx) or PDF (.pdf) file.
Args:
format: Export format ('docx' or 'pdf')
session_id: Session identifier (default: "default")
Returns:
StreamingResponse with the generated document
"""
# Validate format
if format.lower() not in ['docx', 'pdf']:
raise HTTPException(
status_code=400,
detail="Invalid format. Use 'docx' or 'pdf'"
)
# Get conversation history
messages = get_conversation_history(session_id)
if not messages:
raise HTTPException(
status_code=404,
detail="No conversation history found for this session"
)
try:
# Generate document based on format
if format.lower() == 'docx':
buffer = generate_docx(session_id, messages)
media_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
filename = f"conversation_{session_id}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx"
else: # pdf
buffer = generate_pdf(session_id, messages)
media_type = "application/pdf"
filename = f"conversation_{session_id}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
return StreamingResponse(
buffer,
media_type=media_type,
headers={
"Content-Disposition": f"attachment; filename={filename}"
}
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"Error generating {format.upper()} document: {str(e)}"
)
|