Refactored code to get profiles and chat templates

This commit is contained in:
Thorsten Sommer 2026-06-10 10:29:50 +02:00
parent 1c2d243c1f
commit 0526e81347
Signed by untrusted user who does not match committer: tsommer
GPG Key ID: 371BBA77A02C0108
4 changed files with 57 additions and 24 deletions

View File

@ -94,6 +94,8 @@ public sealed record ChatThread
/// <returns>The prepared system prompt.</returns> /// <returns>The prepared system prompt.</returns>
public string PrepareSystemPrompt(SettingsManager settingsManager) public string PrepareSystemPrompt(SettingsManager settingsManager)
{ {
this.allowProfile = true;
// //
// Use the information from the chat template, if provided. Otherwise, use the default system prompt // Use the information from the chat template, if provided. Otherwise, use the default system prompt
// //
@ -111,8 +113,8 @@ public sealed record ChatThread
systemPromptTextWithChatTemplate = this.SystemPrompt; systemPromptTextWithChatTemplate = this.SystemPrompt;
else else
{ {
var chatTemplate = settingsManager.ConfigurationData.ChatTemplates.FirstOrDefault(x => x.Id == this.SelectedChatTemplate); var chatTemplate = settingsManager.GetChatTemplateById(this.SelectedChatTemplate);
if(chatTemplate == null) if(chatTemplate == ChatTemplate.NO_CHAT_TEMPLATE)
systemPromptTextWithChatTemplate = this.SystemPrompt; systemPromptTextWithChatTemplate = this.SystemPrompt;
else else
{ {
@ -168,8 +170,8 @@ public sealed record ChatThread
systemPromptText = systemPromptWithAugmentedData; systemPromptText = systemPromptWithAugmentedData;
else else
{ {
var profile = settingsManager.ConfigurationData.Profiles.FirstOrDefault(x => x.Id == this.SelectedProfile); var profile = settingsManager.GetProfileById(this.SelectedProfile);
if(profile is null) if(profile == Profile.NO_PROFILE)
systemPromptText = systemPromptWithAugmentedData; systemPromptText = systemPromptWithAugmentedData;
else else
{ {

View File

@ -101,7 +101,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
// Apply the filters for the message bus: // Apply the filters for the message bus:
this.ApplyFilters([], [ Event.HAS_CHAT_UNSAVED_CHANGES, Event.RESET_CHAT_STATE, Event.CHAT_STREAMING_DONE, Event.AI_JOB_CHANGED, Event.AI_JOB_FINISHED, Event.CHAT_GENERATION_CHANGED, Event.WORKSPACE_RENAMED ]); this.ApplyFilters([], [ Event.HAS_CHAT_UNSAVED_CHANGES, Event.RESET_CHAT_STATE, Event.CHAT_STREAMING_DONE, Event.AI_JOB_CHANGED, Event.AI_JOB_FINISHED, Event.CHAT_GENERATION_CHANGED, Event.WORKSPACE_RENAMED, Event.CONFIGURATION_CHANGED ]);
// Configure the spellchecking for the user input: // Configure the spellchecking for the user input:
this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES); this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES);
@ -470,7 +470,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
private async Task ProfileWasChanged(Profile profile) private async Task ProfileWasChanged(Profile profile)
{ {
this.currentProfile = profile; this.currentProfile = this.SettingsManager.GetProfileById(profile.Id);
if(this.ChatThread is null) if(this.ChatThread is null)
return; return;
@ -484,7 +484,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
private async Task ChatTemplateWasChanged(ChatTemplate chatTemplate) private async Task ChatTemplateWasChanged(ChatTemplate chatTemplate)
{ {
this.currentChatTemplate = chatTemplate; this.currentChatTemplate = this.SettingsManager.GetChatTemplateById(chatTemplate.Id);
if(!string.IsNullOrWhiteSpace(this.currentChatTemplate.PredefinedUserPrompt)) if(!string.IsNullOrWhiteSpace(this.currentChatTemplate.PredefinedUserPrompt))
this.ComposerState.SetSystemInput(this.currentChatTemplate.PredefinedUserPrompt); this.ComposerState.SetSystemInput(this.currentChatTemplate.PredefinedUserPrompt);
@ -497,6 +497,12 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
await this.StartNewChat(true); await this.StartNewChat(true);
} }
private void RefreshCurrentProfileAndChatTemplate()
{
this.currentProfile = this.SettingsManager.GetProfileById(this.currentProfile.Id);
this.currentChatTemplate = this.SettingsManager.GetChatTemplateById(this.currentChatTemplate.Id);
}
private IReadOnlyList<DataSourceAgentSelected> GetAgentSelectedDataSources() private IReadOnlyList<DataSourceAgentSelected> GetAgentSelectedDataSources()
{ {
if (this.ChatThread is null) if (this.ChatThread is null)
@ -602,6 +608,8 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
if(!this.ChatThread.IsLLMProviderAllowed(this.Provider)) if(!this.ChatThread.IsLLMProviderAllowed(this.Provider))
return; return;
this.RefreshCurrentProfileAndChatTemplate();
// Blur the focus away from the input field to be able to clear it: // Blur the focus away from the input field to be able to clear it:
await this.inputField.BlurAsync(); await this.inputField.BlurAsync();
@ -795,6 +803,7 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
// //
this.hasUnsavedChanges = false; this.hasUnsavedChanges = false;
this.ComposerState.Clear(); this.ComposerState.Clear();
this.RefreshCurrentProfileAndChatTemplate();
// //
// Reset the LLM provider considering the user's settings: // Reset the LLM provider considering the user's settings:
@ -967,14 +976,11 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
// Try to select the profile: // Try to select the profile:
if (!string.IsNullOrWhiteSpace(chatProfile)) if (!string.IsNullOrWhiteSpace(chatProfile))
this.currentProfile = this.SettingsManager.ConfigurationData.Profiles.FirstOrDefault(x => x.Id == chatProfile) ?? Profile.NO_PROFILE; this.currentProfile = this.SettingsManager.GetProfileById(chatProfile);
// Try to select the chat template: // Try to select the chat template:
if (!string.IsNullOrWhiteSpace(chatChatTemplate)) if (!string.IsNullOrWhiteSpace(chatChatTemplate))
{ this.currentChatTemplate = this.SettingsManager.GetChatTemplateById(chatChatTemplate);
var selectedTemplate = this.SettingsManager.ConfigurationData.ChatTemplates.FirstOrDefault(x => x.Id == chatChatTemplate);
this.currentChatTemplate = selectedTemplate ?? ChatTemplate.NO_CHAT_TEMPLATE;
}
} }
private async Task ToggleWorkspaceOverlay() private async Task ToggleWorkspaceOverlay()
@ -1075,6 +1081,15 @@ public partial class ChatComponent : MSGComponentBase, IAsyncDisposable
await this.RefreshRenamedWorkspaceHeaderAsync(workspaceId); await this.RefreshRenamedWorkspaceHeaderAsync(workspaceId);
break; break;
case Event.CONFIGURATION_CHANGED:
var previousChatTemplate = this.currentChatTemplate;
this.RefreshCurrentProfileAndChatTemplate();
if (!this.ComposerState.HasUserDraft && previousChatTemplate != this.currentChatTemplate)
this.ComposerState.ApplyTemplate(this.currentChatTemplate);
this.StateHasChanged();
break;
case Event.AI_JOB_CHANGED: case Event.AI_JOB_CHANGED:
case Event.AI_JOB_FINISHED: case Event.AI_JOB_FINISHED:
case Event.CHAT_GENERATION_CHANGED: case Event.CHAT_GENERATION_CHANGED:

View File

@ -348,17 +348,13 @@ public sealed class SettingsManager
return Profile.NO_PROFILE; return Profile.NO_PROFILE;
if (preselection.UseSpecificProfile) if (preselection.UseSpecificProfile)
{ return this.GetProfileById(preselection.SpecificProfileId);
var componentProfile = this.ConfigurationData.Profiles.FirstOrDefault(x => x.Id.Equals(preselection.SpecificProfileId, StringComparison.OrdinalIgnoreCase));
return componentProfile ?? Profile.NO_PROFILE;
}
var appPreselection = ProfilePreselection.FromStoredValue(this.ConfigurationData.App.PreselectedProfile); var appPreselection = ProfilePreselection.FromStoredValue(this.ConfigurationData.App.PreselectedProfile);
if (appPreselection.DoNotPreselectProfile || !appPreselection.UseSpecificProfile) if (appPreselection.DoNotPreselectProfile || !appPreselection.UseSpecificProfile)
return Profile.NO_PROFILE; return Profile.NO_PROFILE;
var appProfile = this.ConfigurationData.Profiles.FirstOrDefault(x => x.Id.Equals(appPreselection.SpecificProfileId, StringComparison.OrdinalIgnoreCase)); return this.GetProfileById(appPreselection.SpecificProfileId);
return appProfile ?? Profile.NO_PROFILE;
} }
public Profile GetAppPreselectedProfile() public Profile GetAppPreselectedProfile()
@ -367,8 +363,7 @@ public sealed class SettingsManager
if (appPreselection.DoNotPreselectProfile || !appPreselection.UseSpecificProfile) if (appPreselection.DoNotPreselectProfile || !appPreselection.UseSpecificProfile)
return Profile.NO_PROFILE; return Profile.NO_PROFILE;
var appProfile = this.ConfigurationData.Profiles.FirstOrDefault(x => x.Id.Equals(appPreselection.SpecificProfileId, StringComparison.OrdinalIgnoreCase)); return this.GetProfileById(appPreselection.SpecificProfileId);
return appProfile ?? Profile.NO_PROFILE;
} }
public ChatTemplate GetPreselectedChatTemplate(Tools.Components component) public ChatTemplate GetPreselectedChatTemplate(Tools.Components component)
@ -377,8 +372,29 @@ public sealed class SettingsManager
if (preselection != ChatTemplate.NO_CHAT_TEMPLATE) if (preselection != ChatTemplate.NO_CHAT_TEMPLATE)
return preselection; return preselection;
preselection = this.ConfigurationData.ChatTemplates.FirstOrDefault(x => x.Id.Equals(this.ConfigurationData.App.PreselectedChatTemplate, StringComparison.OrdinalIgnoreCase)); return this.GetChatTemplateById(this.ConfigurationData.App.PreselectedChatTemplate);
return preselection ?? ChatTemplate.NO_CHAT_TEMPLATE; }
public Profile GetProfileById(string? profileId)
{
if (string.IsNullOrWhiteSpace(profileId))
return Profile.NO_PROFILE;
if (string.Equals(profileId, Profile.NO_PROFILE.Id, StringComparison.OrdinalIgnoreCase))
return Profile.NO_PROFILE;
return this.ConfigurationData.Profiles.FirstOrDefault(x => x.Id.Equals(profileId, StringComparison.OrdinalIgnoreCase)) ?? Profile.NO_PROFILE;
}
public ChatTemplate GetChatTemplateById(string? chatTemplateId)
{
if (string.IsNullOrWhiteSpace(chatTemplateId))
return ChatTemplate.NO_CHAT_TEMPLATE;
if (string.Equals(chatTemplateId, ChatTemplate.NO_CHAT_TEMPLATE.Id, StringComparison.OrdinalIgnoreCase))
return ChatTemplate.NO_CHAT_TEMPLATE;
return this.ConfigurationData.ChatTemplates.FirstOrDefault(x => x.Id.Equals(chatTemplateId, StringComparison.OrdinalIgnoreCase)) ?? ChatTemplate.NO_CHAT_TEMPLATE;
} }
public ConfidenceLevel GetConfiguredConfidenceLevel(LLMProviders llmProvider) public ConfidenceLevel GetConfiguredConfidenceLevel(LLMProviders llmProvider)

View File

@ -169,7 +169,7 @@ public static class ComponentsExtensions
public static ChatTemplate PreselectedChatTemplate(this Components component, SettingsManager settingsManager) => component switch public static ChatTemplate PreselectedChatTemplate(this Components component, SettingsManager settingsManager) => component switch
{ {
Components.CHAT => settingsManager.ConfigurationData.Chat.PreselectOptions ? settingsManager.ConfigurationData.ChatTemplates.FirstOrDefault(x => x.Id == settingsManager.ConfigurationData.Chat.PreselectedChatTemplate) ?? ChatTemplate.NO_CHAT_TEMPLATE : ChatTemplate.NO_CHAT_TEMPLATE, Components.CHAT => settingsManager.ConfigurationData.Chat.PreselectOptions ? settingsManager.GetChatTemplateById(settingsManager.ConfigurationData.Chat.PreselectedChatTemplate) : ChatTemplate.NO_CHAT_TEMPLATE,
_ => ChatTemplate.NO_CHAT_TEMPLATE, _ => ChatTemplate.NO_CHAT_TEMPLATE,
}; };