Refactor file attachment handling to skip forbidden file types in DocumentAnalysisAssistant, ContentText, and ChatComponent

This commit is contained in:
Thorsten Sommer 2025-12-28 16:22:18 +01:00
parent cca5c4d3a4
commit ca76da65f2
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
3 changed files with 18 additions and 3 deletions

View File

@ -332,6 +332,12 @@ public partial class DocumentAnalysisAssistant : AssistantBaseCore<SettingsDialo
foreach (var fileAttachment in this.loadedDocumentPaths)
{
if (fileAttachment.IsForbidden)
{
this.Logger.LogWarning($"Skipping forbidden file: '{fileAttachment.FilePath}'.");
continue;
}
var fileContent = await this.RustService.ReadArbitraryFileData(fileAttachment.FilePath, int.MaxValue);
documentSections.Add($"""

View File

@ -165,8 +165,11 @@ public sealed class ContentText : IContent
foreach (var missingFile in missingFiles)
LOGGER.LogWarning("File attachment no longer exists and will be skipped: '{MissingFile}'.", missingFile.FilePath);
// Only proceed if there are existing files
if (existingFiles.Count > 0)
// Determine allowed attachments:
var numberAllowedExistingFiles = existingFiles.Count(x => x.IsValid);
// Only proceed if there are existing, allowed files:
if (numberAllowedExistingFiles > 0)
{
// Check Pandoc availability once before processing file attachments
var pandocState = await Pandoc.CheckAvailabilityAsync(Program.RUST_SERVICE, showMessages: true, showSuccessMessage: false);
@ -181,6 +184,12 @@ public sealed class ContentText : IContent
sb.AppendLine("The following files are attached to this message:");
foreach(var file in existingFiles)
{
if (file.IsForbidden)
{
LOGGER.LogWarning("File attachment '{FilePath}' has a forbidden file type and will be skipped.", file.FilePath);
continue;
}
sb.AppendLine();
sb.AppendLine("---------------------------------------");
sb.AppendLine($"File path: {file.FilePath}");

View File

@ -464,7 +464,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
lastUserPrompt = new ContentText
{
Text = this.userInput,
FileAttachments = [..this.chatDocumentPaths],
FileAttachments = [..this.chatDocumentPaths.Where(x => x.IsValid)],
};
//