Improved file attachment handling in assistants & dialogs (#840)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-07-06 20:27:25 +02:00
1 parent 423330f1b9
commit 4a15ff2665
19 files changed
+350 -293

No files matched your search

@@ -1,5 +1,6 @@
using System.Text;
using AIStudio.Chat;
using AIStudio.Dialogs.Settings;
using AIStudio.Tools.AssistantSessions;
@@ -11,7 +12,7 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
protected override string Title => T("Coding Assistant");
protected override string Description => T("This coding assistant supports you in writing code. Provide some coding context by copying and pasting your code into the input fields. You might assign an ID to your code snippet to easily reference it later. When you have compiler messages, you can paste them into the input fields to get help with debugging as well.");
protected override string Description => T("This coding assistant supports you in writing code. Ask your coding question and optionally attach source files as context. When you have compiler messages, you can paste them into the input fields to get help with debugging as well.");
protected override string SystemPrompt =>
"""
@@ -20,6 +21,12 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
You know object-oriented programming, as well as functional programming and procedural programming. You are also
familiar with design patterns and can explain them. You are an expert of debugging and can help with compiler
messages. You can also help with code refactoring and optimization.
The user may attach source files, project files, configuration files, logs, or other documents as coding context.
Treat attached files as source context for the user's question. Use the file paths and file contents provided in
the message to reason about the code. Do not invent files or APIs that are not present in the user's question or
attached context. If the question conflicts with attached context, prioritize the user's explicit question and
explain any relevant mismatch.
When the user asks in a different language than English, you answer in the same language!
""";
@@ -34,9 +41,58 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
protected override string SendToChatVisibleUserPromptContent => this.questions;
protected override ChatThread ConvertToChatThread
{
get
{
var originalChatThread = this.ChatThread ?? new ChatThread();
if (string.IsNullOrWhiteSpace(this.SendToChatVisibleUserPromptText))
{
return originalChatThread with
{
SystemPrompt = SystemPrompts.DEFAULT,
};
}
var earliestBlock = originalChatThread.Blocks.MinBy(x => x.Time);
var visiblePromptTime = earliestBlock is null
? DateTimeOffset.Now
: earliestBlock.Time == DateTimeOffset.MinValue
? earliestBlock.Time
: earliestBlock.Time.AddTicks(-1);
var transferredBlocks = originalChatThread.Blocks
.Select(block => block.Role is ChatRole.USER
? this.CloneHiddenUserBlockWithoutAttachments(block)
: block.DeepClone())
.ToList();
transferredBlocks.Insert(0, new ContentBlock
{
Time = visiblePromptTime,
ContentType = ContentType.TEXT,
HideFromUser = false,
Role = ChatRole.USER,
Content = new ContentText
{
Text = this.BuildVisibleChatPrompt(),
FileAttachments = this.loadedDocumentPaths.ToList(),
},
});
return originalChatThread with
{
ChatId = Guid.NewGuid(),
Name = T("Coding Assistant Session"),
SystemPrompt = SystemPrompts.DEFAULT,
Blocks = transferredBlocks,
};
}
}
protected override void ResetForm()
{
this.codingContexts.Clear();
this.loadedDocumentPaths.Clear();
this.compilerMessages = string.Empty;
this.questions = string.Empty;
if (!this.MightPreselectValues())
@@ -56,11 +112,11 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
return false;
}
private readonly List<CodingContext> codingContexts = new();
private HashSet<FileAttachment> loadedDocumentPaths = [];
private bool provideCompilerMessages;
private string compilerMessages = string.Empty;
private string questions = string.Empty;
private static readonly AssistantSessionStateKey<List<CodingContext>> CODING_CONTEXTS_STATE_KEY = new(nameof(codingContexts));
private static readonly AssistantSessionStateKey<HashSet<FileAttachment>> LOADED_DOCUMENT_PATHS_STATE_KEY = new(nameof(loadedDocumentPaths));
private static readonly AssistantSessionStateKey<bool> PROVIDE_COMPILER_MESSAGES_STATE_KEY = new(nameof(provideCompilerMessages));
private static readonly AssistantSessionStateKey<string> COMPILER_MESSAGES_STATE_KEY = new(nameof(compilerMessages));
private static readonly AssistantSessionStateKey<string> QUESTIONS_STATE_KEY = new(nameof(questions));
@@ -68,7 +124,7 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
/// <inheritdoc />
protected override void CaptureCustomAssistantSessionState(AssistantSessionStateWriter state)
{
state.SetList(CODING_CONTEXTS_STATE_KEY, this.codingContexts);
state.SetHashSet(LOADED_DOCUMENT_PATHS_STATE_KEY, this.loadedDocumentPaths);
state.Set(PROVIDE_COMPILER_MESSAGES_STATE_KEY, this.provideCompilerMessages);
state.Set(COMPILER_MESSAGES_STATE_KEY, this.compilerMessages);
state.Set(QUESTIONS_STATE_KEY, this.questions);
@@ -77,7 +133,7 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
/// <inheritdoc />
protected override void RestoreCustomAssistantSessionState(AssistantSessionStateReader state)
{
state.RestoreList(CODING_CONTEXTS_STATE_KEY, this.codingContexts);
state.RestoreHashSet(LOADED_DOCUMENT_PATHS_STATE_KEY, this.loadedDocumentPaths);
state.Restore(PROVIDE_COMPILER_MESSAGES_STATE_KEY, value => this.provideCompilerMessages = value);
state.Restore(COMPILER_MESSAGES_STATE_KEY, value => this.compilerMessages = value);
state.Restore(QUESTIONS_STATE_KEY, value => this.questions = value);
@@ -115,26 +171,30 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
return null;
}
private void AddCodingContext()
private ContentBlock CloneHiddenUserBlockWithoutAttachments(ContentBlock block)
{
this.codingContexts.Add(new()
{
Id = string.Format(T("Context {0}"), this.codingContexts.Count + 1),
Language = this.SettingsManager.ConfigurationData.Coding.PreselectOptions ? this.SettingsManager.ConfigurationData.Coding.PreselectedProgrammingLanguage : default,
OtherLanguage = this.SettingsManager.ConfigurationData.Coding.PreselectOptions ? this.SettingsManager.ConfigurationData.Coding.PreselectedOtherProgrammingLanguage : string.Empty,
});
var clone = block.DeepClone(changeHideState: true);
if (clone.Content is ContentText text)
text.FileAttachments = [];
return clone;
}
private ValueTask DeleteContext(int index)
private string BuildVisibleChatPrompt()
{
if(this.codingContexts.Count < index + 1)
return ValueTask.CompletedTask;
if (!this.provideCompilerMessages)
return this.SendToChatVisibleUserPromptText ?? string.Empty;
this.codingContexts.RemoveAt(index);
this.Form?.ResetValidation();
return $"""
I have the following compiler messages:
this.StateHasChanged();
return ValueTask.CompletedTask;
```
{this.compilerMessages}
```
My questions are:
{this.questions}
""";
}
private async Task GetSupport()
@@ -143,28 +203,6 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
if (!this.InputIsValid)
return;
var sbContext = new StringBuilder();
if (this.codingContexts.Count > 0)
{
sbContext.AppendLine("I have the following coding context:");
sbContext.AppendLine();
foreach (var codingContext in this.codingContexts)
{
sbContext.AppendLine($"ID: {codingContext.Id}");
if(codingContext.Language is not CommonCodingLanguages.OTHER)
sbContext.AppendLine($"Language: {codingContext.Language.Name()}");
else
sbContext.AppendLine($"Language: {codingContext.OtherLanguage}");
sbContext.AppendLine("Content:");
sbContext.AppendLine("```");
sbContext.AppendLine(codingContext.Code);
sbContext.AppendLine("```");
sbContext.AppendLine();
}
}
var sbCompilerMessages = new StringBuilder();
if (this.provideCompilerMessages)
{
@@ -179,12 +217,13 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
this.CreateChatThread();
var time = this.AddUserRequest(
$"""
{sbContext}
{sbCompilerMessages}
My questions are:
{this.questions}
""");
""",
false,
this.loadedDocumentPaths.ToList());
await this.AddAIResponseAsync(time);
}