52 lines
2 KiB
Python
52 lines
2 KiB
Python
# Copyright (c) 2025 Patrick Motsch
|
|
# All rights reserved.
|
|
|
|
"""
|
|
Convert Document action for AI operations.
|
|
Converts documents between different formats (PDF→Word, Excel→CSV, etc.).
|
|
"""
|
|
|
|
import logging
|
|
from typing import Dict, Any
|
|
from modules.workflows.methods.methodBase import action
|
|
from modules.datamodels.datamodelChat import ActionResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@action
|
|
async def convertDocument(self, parameters: Dict[str, Any]) -> ActionResult:
|
|
"""
|
|
GENERAL:
|
|
- Purpose: Convert documents between different formats (PDF→Word, Excel→CSV, etc.).
|
|
- Input requirements: documentList (required); targetFormat (required).
|
|
- Output format: Document in target format.
|
|
|
|
Parameters:
|
|
- documentList (list, required): Document reference(s) to convert.
|
|
- targetFormat (str, required): Target format extension (docx, pdf, xlsx, csv, txt, html, json, md, etc.).
|
|
- preserveStructure (bool, optional): Whether to preserve document structure (headings, tables, etc.). Default: True.
|
|
"""
|
|
documentList = parameters.get("documentList", [])
|
|
if not documentList:
|
|
return ActionResult.isFailure(error="documentList is required")
|
|
|
|
targetFormat = parameters.get("targetFormat")
|
|
if not targetFormat:
|
|
return ActionResult.isFailure(error="targetFormat is required")
|
|
|
|
preserveStructure = parameters.get("preserveStructure", True)
|
|
|
|
# Normalize format (remove leading dot if present)
|
|
normalizedFormat = targetFormat.strip().lstrip('.').lower()
|
|
|
|
aiPrompt = f"Convert the provided document(s) to {normalizedFormat.upper()} format."
|
|
if preserveStructure:
|
|
aiPrompt += " Preserve all document structure including headings, tables, formatting, lists, and layout."
|
|
aiPrompt += " Ensure the converted document maintains the same content and information as the original."
|
|
|
|
return await self.process({
|
|
"aiPrompt": aiPrompt,
|
|
"documentList": documentList,
|
|
"resultType": normalizedFormat
|
|
})
|
|
|