mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-10-14 03:13:48 +00:00
Added export to word function for the chat (#566)
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions
This commit is contained in:
1 parent
37bd42e41f
commit
9418b99275
12 files changed
+256
-1
No files matched your search
@@ -0,0 +1,97 @@
|
||||
using System.Diagnostics;
|
||||
using AIStudio.Chat;
|
||||
using AIStudio.Tools.PluginSystem;
|
||||
using AIStudio.Tools.Services;
|
||||
|
||||
namespace AIStudio.Tools;
|
||||
|
||||
public static class PandocExport
|
||||
{
|
||||
private static readonly ILogger LOGGER = Program.LOGGER_FACTORY.CreateLogger(nameof(PandocExport));
|
||||
|
||||
private static string TB(string fallbackEn) => I18N.I.T(fallbackEn, typeof(PandocExport).Namespace, nameof(PandocExport));
|
||||
|
||||
public static async Task<bool> ToMicrosoftWord(RustService rustService, string dialogTitle, IContent markdownContent)
|
||||
{
|
||||
var response = await rustService.SaveFile(dialogTitle, new("Microsoft Word", ["docx"]));
|
||||
if (response.UserCancelled)
|
||||
{
|
||||
LOGGER.LogInformation("User cancelled the save dialog.");
|
||||
return false;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
try
|
||||
{
|
||||
// Extract text content from chat:
|
||||
var markdownText = markdownContent switch
|
||||
{
|
||||
ContentText text => text.Text,
|
||||
ContentImage _ => "Image export to Microsoft Word not yet possible",
|
||||
|
||||
_ => "Unknown content type. Cannot export to Word."
|
||||
};
|
||||
|
||||
// Write text content to a temporary file:
|
||||
await File.WriteAllTextAsync(tempMarkdownFilePath, markdownText);
|
||||
|
||||
// Ensure that Pandoc is installed and ready:
|
||||
var pandocState = await Pandoc.CheckAvailabilityAsync(rustService);
|
||||
if (!pandocState.IsAvailable)
|
||||
return false;
|
||||
|
||||
// Call Pandoc to create the Word file:
|
||||
var pandoc = await PandocProcessBuilder
|
||||
.Create()
|
||||
.UseStandaloneMode()
|
||||
.WithInputFormat("markdown")
|
||||
.WithOutputFormat("docx")
|
||||
.WithOutputFile(response.SaveFilePath)
|
||||
.WithInputFile(tempMarkdownFilePath)
|
||||
.BuildAsync(rustService);
|
||||
|
||||
using var process = Process.Start(pandoc.StartInfo);
|
||||
if (process is null)
|
||||
{
|
||||
LOGGER.LogError("Failed to start Pandoc process.");
|
||||
return false;
|
||||
}
|
||||
|
||||
await process.WaitForExitAsync();
|
||||
if (process.ExitCode is not 0)
|
||||
{
|
||||
var error = await process.StandardError.ReadToEndAsync();
|
||||
LOGGER.LogError($"Pandoc failed with exit code {process.ExitCode}: {error}");
|
||||
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Cancel, TB("Error during Microsoft Word export")));
|
||||
return false;
|
||||
}
|
||||
|
||||
LOGGER.LogInformation("Pandoc conversion successful.");
|
||||
await MessageBus.INSTANCE.SendSuccess(new(Icons.Material.Filled.CheckCircle, TB("Microsoft Word export successful")));
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LOGGER.LogError(ex, "Error during Word export.");
|
||||
await MessageBus.INSTANCE.SendError(new(Icons.Material.Filled.Cancel, TB("Error during Microsoft Word export")));
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Try to remove the temp file:
|
||||
try
|
||||
{
|
||||
File.Delete(tempMarkdownFilePath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
LOGGER.LogWarning($"Was not able to delete temporary file: '{tempMarkdownFilePath}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ public sealed class PandocProcessBuilder
|
||||
private string? providedOutputFile;
|
||||
private string? providedInputFormat;
|
||||
private string? providedOutputFormat;
|
||||
private bool useStandaloneMode;
|
||||
|
||||
private readonly List<string> additionalArguments = new();
|
||||
|
||||
@@ -57,10 +58,19 @@ public sealed class PandocProcessBuilder
|
||||
this.additionalArguments.Add(argument);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PandocProcessBuilder UseStandaloneMode()
|
||||
{
|
||||
this.useStandaloneMode = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public async Task<PandocPreparedProcess> BuildAsync(RustService rustService)
|
||||
{
|
||||
var sbArguments = new StringBuilder();
|
||||
|
||||
if (this.useStandaloneMode)
|
||||
sbArguments.Append(" --standalone ");
|
||||
|
||||
if(!string.IsNullOrWhiteSpace(this.providedInputFile))
|
||||
sbArguments.Append(this.providedInputFile);
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace AIStudio.Tools.Rust;
|
||||
|
||||
public readonly record struct FileSaveResponse(bool UserCancelled, string SaveFilePath);
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace AIStudio.Tools.Rust;
|
||||
|
||||
public class SaveFileOptions
|
||||
{
|
||||
public required string Title { get; init; }
|
||||
|
||||
public PreviousFile? PreviousFile { get; init; }
|
||||
|
||||
public FileTypeFilter? Filter { get; init; }
|
||||
}
|
||||
@@ -35,4 +35,31 @@ public sealed partial class RustService
|
||||
|
||||
return await result.Content.ReadFromJsonAsync<FileSelectionResponse>(this.jsonRustSerializerOptions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initiates a dialog to let the user select a file for a writing operation.
|
||||
/// </summary>
|
||||
/// <param name="title">The title of the save file dialog.</param>
|
||||
/// <param name="filter">An optional file type filter for filtering specific file formats.</param>
|
||||
/// <param name="initialFile">An optional initial file path to pre-fill in the dialog.</param>
|
||||
/// <returns>A <see cref="FileSaveResponse"/> object containing information about whether the user canceled the
|
||||
/// operation and whether the select operation was successful.</returns>
|
||||
public async Task<FileSaveResponse> SaveFile(string title, FileTypeFilter? filter = null, string? initialFile = null)
|
||||
{
|
||||
var payload = new SaveFileOptions
|
||||
{
|
||||
Title = title,
|
||||
PreviousFile = initialFile is null ? null : new (initialFile),
|
||||
Filter = filter
|
||||
};
|
||||
|
||||
var result = await this.http.PostAsJsonAsync("/save/file", payload, this.jsonRustSerializerOptions);
|
||||
if (!result.IsSuccessStatusCode)
|
||||
{
|
||||
this.logger!.LogError($"Failed to select a file for writing operation '{result.StatusCode}'");
|
||||
return new FileSaveResponse(true, string.Empty);
|
||||
}
|
||||
|
||||
return await result.Content.ReadFromJsonAsync<FileSaveResponse>(this.jsonRustSerializerOptions);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user