feat: unify formal Word export
This commit is contained in:
1 parent
d35d075669
commit
0980eea975
16 files changed
+1356
-609
No files matched your search
@@ -15,11 +15,13 @@ import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
from fastapi.responses import Response, StreamingResponse
|
||||
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
@@ -57,6 +59,12 @@ from app.gateway.document_rewrite_pipeline import (
|
||||
)
|
||||
from app.gateway.document_rewrite_summary import build_document_rewrite_summary
|
||||
from app.gateway.path_utils import aresolve_thread_virtual_path
|
||||
from app.gateway.word_export import (
|
||||
DOCX_MEDIA_TYPE,
|
||||
WordExportError,
|
||||
WordExportFontError,
|
||||
build_markdown_docx,
|
||||
)
|
||||
from deerflow.config.app_config import AppConfig
|
||||
from deerflow.models import create_chat_model
|
||||
from deerflow.utils.stream_text import InlineThinkTagFilter
|
||||
@@ -130,6 +138,13 @@ class RewriteRequest(BaseModel):
|
||||
model_config = {"populate_by_name": True}
|
||||
|
||||
|
||||
class MarkdownDocxExportRequest(BaseModel):
|
||||
"""Markdown source for the server-rendered formal Word download."""
|
||||
|
||||
title: str | None = Field(default=None, max_length=300)
|
||||
markdown: str = Field(..., min_length=1, max_length=2_000_000)
|
||||
|
||||
|
||||
class DocumentRewriteRequest(BaseModel):
|
||||
"""Request for a complete Markdown artifact rewrite.
|
||||
|
||||
@@ -363,6 +378,40 @@ def build_rewrite_messages(body: RewriteRequest) -> tuple[str, list]:
|
||||
# ── Endpoint ──────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@router.post(
|
||||
"/export/docx",
|
||||
summary="Export Markdown as a formally formatted Word document",
|
||||
response_class=Response,
|
||||
)
|
||||
async def export_markdown_docx(body: MarkdownDocxExportRequest) -> Response:
|
||||
"""Generate DOCX on the server and embed the required title font.
|
||||
|
||||
This route is intentionally usable by both authenticated workspaces and
|
||||
public scheduled-task pages. It performs no external fetches and does not
|
||||
persist the supplied Markdown.
|
||||
"""
|
||||
try:
|
||||
content = await asyncio.to_thread(build_markdown_docx, body.markdown, body.title)
|
||||
except WordExportFontError as exc:
|
||||
raise HTTPException(status_code=503, detail=str(exc)) from exc
|
||||
except WordExportError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
fallback_name = (body.title or "document").strip() or "document"
|
||||
fallback_name = re.sub(r'[<>:"/\\|?*\x00-\x1f]+', "_", fallback_name).strip(" .")[:120] or "document"
|
||||
filename = f"{fallback_name}.docx"
|
||||
return Response(
|
||||
content=content,
|
||||
media_type=DOCX_MEDIA_TYPE,
|
||||
headers={
|
||||
"Content-Disposition": (
|
||||
f'attachment; filename="document.docx"; filename*=UTF-8\'\'{quote(filename, safe="")}'
|
||||
),
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/document-rewrite/jobs")
|
||||
@require_permission("threads", "write", owner_check=True)
|
||||
async def start_durable_document_rewrite_job(
|
||||
|
||||
Reference in new issue
Block a user