Refactored user prompt handling in assistants when sending to chat

This commit is contained in:
Thorsten Sommer 2026-04-16 15:15:53 +02:00
parent 79da608ae2
commit 5332001627
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5
11 changed files with 150 additions and 41 deletions

View File

@ -1,6 +1,5 @@
using System.Text; using System.Text;
using AIStudio.Chat;
using AIStudio.Dialogs.Settings; using AIStudio.Dialogs.Settings;
namespace AIStudio.Assistants.Agenda; namespace AIStudio.Assistants.Agenda;
@ -97,10 +96,12 @@ public partial class AssistantAgenda : AssistantBaseCore<SettingsDialogAgenda>
protected override Func<Task> SubmitAction => this.CreateAgenda; protected override Func<Task> SubmitAction => this.CreateAgenda;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptText =>
{ $"""
SystemPrompt = SystemPrompts.DEFAULT, {string.Format(T("Create an agenda for the meeting '{0}' with the following contents:"), this.inputName)}
};
{this.inputContent}
""";
protected override void ResetForm() protected override void ResetForm()
{ {

View File

@ -79,10 +79,29 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase wher
protected virtual bool ShowReset => true; protected virtual bool ShowReset => true;
protected virtual ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected virtual string? SendToChatVisibleUserPromptPrefix => null;
protected virtual string? SendToChatVisibleUserPromptContent => null;
protected virtual string? SendToChatVisibleUserPromptText
{ {
SystemPrompt = SystemPrompts.DEFAULT, get
}; {
if (string.IsNullOrWhiteSpace(this.SendToChatVisibleUserPromptPrefix))
return null;
if (string.IsNullOrWhiteSpace(this.SendToChatVisibleUserPromptContent))
return this.SendToChatVisibleUserPromptPrefix;
return $"""
{this.SendToChatVisibleUserPromptPrefix}
{this.SendToChatVisibleUserPromptContent}
""";
}
}
protected virtual ChatThread ConvertToChatThread => this.CreateSendToChatThread();
private protected virtual RenderFragment? HeaderActions => null; private protected virtual RenderFragment? HeaderActions => null;
@ -339,6 +358,47 @@ public abstract partial class AssistantBase<TSettings> : AssistantLowerBase wher
await this.RustService.CopyText2Clipboard(this.Snackbar, this.Result2Copy()); await this.RustService.CopyText2Clipboard(this.Snackbar, this.Result2Copy());
} }
private ChatThread CreateSendToChatThread()
{
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
? block.DeepClone(changeHideState: true)
: block.DeepClone())
.ToList();
transferredBlocks.Insert(0, new ContentBlock
{
Time = visiblePromptTime,
ContentType = ContentType.TEXT,
HideFromUser = false,
Role = ChatRole.USER,
Content = new ContentText
{
Text = this.SendToChatVisibleUserPromptText,
},
});
return originalChatThread with
{
SystemPrompt = SystemPrompts.DEFAULT,
Blocks = transferredBlocks,
};
}
private static string? GetButtonIcon(string icon) private static string? GetButtonIcon(string icon)
{ {
if(string.IsNullOrWhiteSpace(icon)) if(string.IsNullOrWhiteSpace(icon))

View File

@ -29,6 +29,10 @@ public partial class AssistantCoding : AssistantBaseCore<SettingsDialogCoding>
protected override Func<Task> SubmitAction => this.GetSupport; protected override Func<Task> SubmitAction => this.GetSupport;
protected override string SendToChatVisibleUserPromptPrefix => T("Help me with the following coding question:");
protected override string SendToChatVisibleUserPromptContent => this.questions;
protected override void ResetForm() protected override void ResetForm()
{ {
this.codingContexts.Clear(); this.codingContexts.Clear();

View File

@ -1,6 +1,5 @@
using System.Text; using System.Text;
using AIStudio.Chat;
using AIStudio.Dialogs.Settings; using AIStudio.Dialogs.Settings;
namespace AIStudio.Assistants.EMail; namespace AIStudio.Assistants.EMail;
@ -26,10 +25,9 @@ public partial class AssistantEMail : AssistantBaseCore<SettingsDialogWritingEMa
protected override Func<Task> SubmitAction => this.CreateMail; protected override Func<Task> SubmitAction => this.CreateMail;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptPrefix => T("Create an email based on the following bullet points:");
{
SystemPrompt = SystemPrompts.DEFAULT, protected override string SendToChatVisibleUserPromptContent => this.inputBulletPoints;
};
protected override void ResetForm() protected override void ResetForm()
{ {

View File

@ -1,4 +1,3 @@
using AIStudio.Chat;
using AIStudio.Dialogs.Settings; using AIStudio.Dialogs.Settings;
namespace AIStudio.Assistants.GrammarSpelling; namespace AIStudio.Assistants.GrammarSpelling;
@ -41,10 +40,9 @@ public partial class AssistantGrammarSpelling : AssistantBaseCore<SettingsDialog
protected override Func<Task> SubmitAction => this.ProofreadText; protected override Func<Task> SubmitAction => this.ProofreadText;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptPrefix => T("Check the following text for grammar and spelling mistakes:");
{
SystemPrompt = SystemPrompts.DEFAULT, protected override string SendToChatVisibleUserPromptContent => this.inputText;
};
protected override void ResetForm() protected override void ResetForm()
{ {

View File

@ -27,6 +27,13 @@ public partial class AssistantIconFinder : AssistantBaseCore<SettingsDialogIconF
protected override Func<Task> SubmitAction => this.FindIcon; protected override Func<Task> SubmitAction => this.FindIcon;
protected override string SendToChatVisibleUserPromptText =>
$"""
{string.Format(T("Find icon suggestions on {0} for the following context:"), this.selectedIconSource.Name())}
{this.inputContext}
""";
protected override void ResetForm() protected override void ResetForm()
{ {
this.inputContext = string.Empty; this.inputContext = string.Empty;

View File

@ -1,4 +1,3 @@
using AIStudio.Chat;
using AIStudio.Dialogs.Settings; using AIStudio.Dialogs.Settings;
namespace AIStudio.Assistants.JobPosting; namespace AIStudio.Assistants.JobPosting;
@ -51,10 +50,34 @@ public partial class AssistantJobPostings : AssistantBaseCore<SettingsDialogJobP
protected override bool AllowProfiles => false; protected override bool AllowProfiles => false;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptText
{ {
SystemPrompt = SystemPrompts.DEFAULT, get
}; {
if (!string.IsNullOrWhiteSpace(this.inputCompanyName) && !string.IsNullOrWhiteSpace(this.inputJobDescription))
{
return $"""
{string.Format(T("Create a job posting for {0} based on the following job description:"), this.inputCompanyName)}
{this.inputJobDescription}
""";
}
if (!string.IsNullOrWhiteSpace(this.inputCompanyName))
return string.Format(T("Create a job posting for {0}."), this.inputCompanyName);
if (!string.IsNullOrWhiteSpace(this.inputJobDescription))
{
return $"""
{T("Create a job posting based on the following job description:")}
{this.inputJobDescription}
""";
}
return T("Create a job posting.");
}
}
protected override void ResetForm() protected override void ResetForm()
{ {

View File

@ -1,4 +1,3 @@
using AIStudio.Chat;
using AIStudio.Dialogs.Settings; using AIStudio.Dialogs.Settings;
namespace AIStudio.Assistants.LegalCheck; namespace AIStudio.Assistants.LegalCheck;
@ -28,10 +27,9 @@ public partial class AssistantLegalCheck : AssistantBaseCore<SettingsDialogLegal
protected override bool SubmitDisabled => this.isAgentRunning; protected override bool SubmitDisabled => this.isAgentRunning;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptPrefix => T("Answer the following questions about a legal document:");
{
SystemPrompt = SystemPrompts.DEFAULT, protected override string SendToChatVisibleUserPromptContent => this.inputQuestions;
};
protected override void ResetForm() protected override void ResetForm()
{ {

View File

@ -42,10 +42,9 @@ public partial class AssistantRewriteImprove : AssistantBaseCore<SettingsDialogR
protected override Func<Task> SubmitAction => this.RewriteText; protected override Func<Task> SubmitAction => this.RewriteText;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptPrefix => T("Rewrite and improve the following text:");
{
SystemPrompt = SystemPrompts.DEFAULT, protected override string SendToChatVisibleUserPromptContent => this.inputText;
};
protected override void ResetForm() protected override void ResetForm()
{ {

View File

@ -53,10 +53,29 @@ public partial class AssistantSynonyms : AssistantBaseCore<SettingsDialogSynonym
protected override Func<Task> SubmitAction => this.FindSynonyms; protected override Func<Task> SubmitAction => this.FindSynonyms;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptText
{ {
SystemPrompt = SystemPrompts.DEFAULT, get
}; {
if (string.IsNullOrWhiteSpace(this.inputContext))
{
return $"""
{T("Find synonyms for the following word or phrase:")}
{this.inputText}
""";
}
return $"""
{T("Find synonyms for the following word or phrase:")}
{this.inputText}
{T("Context:")}
{this.inputContext}
""";
}
}
protected override void ResetForm() protected override void ResetForm()
{ {

View File

@ -36,10 +36,12 @@ public partial class AssistantTranslation : AssistantBaseCore<SettingsDialogTran
protected override bool SubmitDisabled => this.isAgentRunning; protected override bool SubmitDisabled => this.isAgentRunning;
protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with protected override string SendToChatVisibleUserPromptText =>
{ $"""
SystemPrompt = SystemPrompts.DEFAULT, {string.Format(T("Translate the following text to {0}:"), this.selectedTargetLanguage is CommonLanguages.OTHER ? this.customTargetLanguage : this.selectedTargetLanguage.Name())}
};
{this.inputText}
""";
protected override void ResetForm() protected override void ResetForm()
{ {