From 81bc0f39e89ad51aaa7da77388519456f844290d Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 10 Dec 2025 17:07:39 +0100 Subject: [PATCH] Improved error handling in Microsoft Word export (#586) --- .../Assistants/I18N/allTexts.lua | 9 +++++ .../plugin.lua | 10 ++++++ .../plugin.lua | 9 +++++ app/MindWork AI Studio/Tools/Pandoc.cs | 17 +++++++--- app/MindWork AI Studio/Tools/PandocExport.cs | 33 ++++++++++++------- .../wwwroot/changelog/v0.9.55.md | 4 ++- 6 files changed, 65 insertions(+), 17 deletions(-) diff --git a/app/MindWork AI Studio/Assistants/I18N/allTexts.lua b/app/MindWork AI Studio/Assistants/I18N/allTexts.lua index 42b438ca..74d4cc67 100644 --- a/app/MindWork AI Studio/Assistants/I18N/allTexts.lua +++ b/app/MindWork AI Studio/Assistants/I18N/allTexts.lua @@ -1819,6 +1819,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::PROFILESELECTION::T918741365"] = "You can -- Provider UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::PROVIDERSELECTION::T900237532"] = "Provider" +-- Failed to load file content +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::READFILECONTENT::T1989554334"] = "Failed to load file content" + -- Videos are not supported yet UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::READFILECONTENT::T2928927510"] = "Videos are not supported yet" @@ -5890,6 +5893,12 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T639143005"] = "Text Fil -- All Office Files UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T709668067"] = "All Office Files" +-- Pandoc Installation +UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T185447014"] = "Pandoc Installation" + +-- Pandoc may be required for importing files. +UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T2596465560"] = "Pandoc may be required for importing files." + -- Failed to delete the API key due to an API issue. UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::RUSTSERVICE::APIKEYS::T3658273365"] = "Failed to delete the API key due to an API issue." diff --git a/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua b/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua index 03e06004..e6f60d7d 100644 --- a/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua +++ b/app/MindWork AI Studio/Plugins/languages/de-de-43065dbc-78d0-45b7-92be-f14c2926e2dc/plugin.lua @@ -1821,6 +1821,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::PROFILESELECTION::T918741365"] = "Hier k -- Provider UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::PROVIDERSELECTION::T900237532"] = "Anbieter" +-- Failed to load file content +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::READFILECONTENT::T1989554334"] = "Laden des Dateiinhalts fehlgeschlagen" + -- Videos are not supported yet UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::READFILECONTENT::T2928927510"] = "Videos werden noch nicht unterstützt." @@ -5892,6 +5895,12 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T639143005"] = "Textdate -- All Office Files UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T709668067"] = "Alle Office-Dateien" +-- Pandoc Installation +UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T185447014"] = "Pandoc-Installation" + +-- Pandoc may be required for importing files. +UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T2596465560"] = "Zum Importieren von Dateien kann Pandoc erforderlich sein." + -- Failed to delete the API key due to an API issue. UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::RUSTSERVICE::APIKEYS::T3658273365"] = "Das API-Schlüssel konnte aufgrund eines API-Problems nicht gelöscht werden." @@ -6039,3 +6048,4 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::WORKSPACEBEHAVIOUR::T1307384014"] = "Unbenannt -- Delete Chat UI_TEXT_CONTENT["AISTUDIO::TOOLS::WORKSPACEBEHAVIOUR::T2244038752"] = "Chat löschen" + diff --git a/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua b/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua index 632e1c87..d6c147ec 100644 --- a/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua +++ b/app/MindWork AI Studio/Plugins/languages/en-us-97dfb1ba-50c4-4440-8dfa-6575daf543c8/plugin.lua @@ -1821,6 +1821,9 @@ UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::PROFILESELECTION::T918741365"] = "You can -- Provider UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::PROVIDERSELECTION::T900237532"] = "Provider" +-- Failed to load file content +UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::READFILECONTENT::T1989554334"] = "Failed to load file content" + -- Videos are not supported yet UI_TEXT_CONTENT["AISTUDIO::COMPONENTS::READFILECONTENT::T2928927510"] = "Videos are not supported yet" @@ -5892,6 +5895,12 @@ UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T639143005"] = "Text Fil -- All Office Files UI_TEXT_CONTENT["AISTUDIO::TOOLS::RUST::FILETYPEFILTER::T709668067"] = "All Office Files" +-- Pandoc Installation +UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T185447014"] = "Pandoc Installation" + +-- Pandoc may be required for importing files. +UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::PANDOCAVAILABILITYSERVICE::T2596465560"] = "Pandoc may be required for importing files." + -- Failed to delete the API key due to an API issue. UI_TEXT_CONTENT["AISTUDIO::TOOLS::SERVICES::RUSTSERVICE::APIKEYS::T3658273365"] = "Failed to delete the API key due to an API issue." diff --git a/app/MindWork AI Studio/Tools/Pandoc.cs b/app/MindWork AI Studio/Tools/Pandoc.cs index c4bdb462..afce67f9 100644 --- a/app/MindWork AI Studio/Tools/Pandoc.cs +++ b/app/MindWork AI Studio/Tools/Pandoc.cs @@ -49,19 +49,26 @@ public static partial class Pandoc { if (showMessages) await MessageBus.INSTANCE.SendError(new (Icons.Material.Filled.Help, TB("Was not able to check the Pandoc installation."))); - + LOG.LogInformation("The Pandoc process was not started, it was null"); return new(false, TB("Was not able to check the Pandoc installation."), false, string.Empty, preparedProcess.IsLocal); } - - var output = await process.StandardOutput.ReadToEndAsync(); + + // Read output streams asynchronously while the process runs (prevents deadlock): + var outputTask = process.StandardOutput.ReadToEndAsync(); + var errorTask = process.StandardError.ReadToEndAsync(); + + // Wait for the process to exit AND for streams to be fully read: await process.WaitForExitAsync(); + var output = await outputTask; + var error = await errorTask; + if (process.ExitCode != 0) { if (showMessages) await MessageBus.INSTANCE.SendError(new (Icons.Material.Filled.Error, TB("Pandoc is not available on the system or the process had issues."))); - - LOG.LogError("The Pandoc process was exited with code {ProcessExitCode}", process.ExitCode); + + LOG.LogError("The Pandoc process exited with code {ProcessExitCode}. Error output: '{ErrorText}'", process.ExitCode, error); return new(false, TB("Pandoc is not available on the system or the process had issues."), false, string.Empty, preparedProcess.IsLocal); } diff --git a/app/MindWork AI Studio/Tools/PandocExport.cs b/app/MindWork AI Studio/Tools/PandocExport.cs index acc4dbd6..27e5244e 100644 --- a/app/MindWork AI Studio/Tools/PandocExport.cs +++ b/app/MindWork AI Studio/Tools/PandocExport.cs @@ -25,11 +25,12 @@ public static class PandocExport LOGGER.LogInformation($"The user chose the path '{response.SaveFilePath}' for the Microsoft Word export."); - var tempMarkdownFile = Guid.NewGuid().ToString(); - var tempMarkdownFilePath = Path.Combine(Path.GetTempPath(), tempMarkdownFile); - + var tempMarkdownFilePath = string.Empty; try { + var tempMarkdownFile = Guid.NewGuid().ToString(); + tempMarkdownFilePath = Path.Combine(Path.GetTempPath(), tempMarkdownFile); + // Extract text content from chat: var markdownText = markdownContent switch { @@ -80,11 +81,18 @@ public static class PandocExport return false; } + // Read output streams asynchronously while the process runs (prevents deadlock): + var outputTask = process.StandardOutput.ReadToEndAsync(); + var errorTask = process.StandardError.ReadToEndAsync(); + + // Wait for the process to exit AND for streams to be fully read: await process.WaitForExitAsync(); + await outputTask; + var error = await errorTask; + if (process.ExitCode is not 0) { - var error = await process.StandardError.ReadToEndAsync(); - LOGGER.LogError($"Pandoc failed with exit code {process.ExitCode}: {error}"); + LOGGER.LogError("Pandoc failed with exit code {ProcessExitCode}: '{ErrorText}'", process.ExitCode, error); await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Cancel, TB("Error during Microsoft Word export"))); return false; } @@ -103,13 +111,16 @@ public static class PandocExport finally { // Try to remove the temp file: - try + if (!string.IsNullOrWhiteSpace(tempMarkdownFilePath)) { - File.Delete(tempMarkdownFilePath); - } - catch - { - LOGGER.LogWarning($"Was not able to delete temporary file: '{tempMarkdownFilePath}'"); + try + { + File.Delete(tempMarkdownFilePath); + } + catch + { + LOGGER.LogWarning($"Was not able to delete temporary file: '{tempMarkdownFilePath}'"); + } } } } diff --git a/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md b/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md index cc056442..b1d241b0 100644 --- a/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md +++ b/app/MindWork AI Studio/wwwroot/changelog/v0.9.55.md @@ -6,4 +6,6 @@ - Improved the document preview dialog for the document analysis assistant (in preview), providing Markdown and plain text views for attached files. - Improved the ID handling for configuration plugins. - Improved error handling, logging, and code quality. -- Fixed a bug in the local data sources info dialog (preview feature) for data directories that could cause the app to crash. The error was caused by a background thread producing data while the frontend attempted to display it. \ No newline at end of file +- Improved error handling for Microsoft Word export. +- Fixed a bug in the local data sources info dialog (preview feature) for data directories that could cause the app to crash. The error was caused by a background thread producing data while the frontend attempted to display it. +- Fixed a rare bug in the Microsoft Word export for huge documents. \ No newline at end of file