2025-01-02 13:50:54 +00:00
|
|
|
using AIStudio.Settings;
|
|
|
|
|
2024-05-04 09:11:09 +00:00
|
|
|
namespace AIStudio.Chat;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Data structure for a chat thread.
|
|
|
|
/// </summary>
|
2024-08-18 19:48:35 +00:00
|
|
|
public sealed record ChatThread
|
2024-05-04 09:11:09 +00:00
|
|
|
{
|
2024-07-13 08:37:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The unique identifier of the chat thread.
|
|
|
|
/// </summary>
|
|
|
|
public Guid ChatId { get; init; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The unique identifier of the workspace.
|
|
|
|
/// </summary>
|
|
|
|
public Guid WorkspaceId { get; set; }
|
|
|
|
|
2024-11-23 12:04:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Specifies the provider selected for the chat thread.
|
|
|
|
/// </summary>
|
|
|
|
public string SelectedProvider { get; set; } = string.Empty;
|
|
|
|
|
2025-01-02 13:50:54 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Specifies the profile selected for the chat thread.
|
|
|
|
/// </summary>
|
|
|
|
public string SelectedProfile { get; set; } = string.Empty;
|
|
|
|
|
2024-05-04 09:11:09 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The name of the chat thread. Usually generated by an AI model or manually edited by the user.
|
|
|
|
/// </summary>
|
2024-07-13 08:37:57 +00:00
|
|
|
public string Name { get; set; } = string.Empty;
|
2024-05-04 09:11:09 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The seed for the chat thread. Some providers use this to generate deterministic results.
|
|
|
|
/// </summary>
|
2024-07-13 08:37:57 +00:00
|
|
|
public int Seed { get; init; }
|
2024-05-04 09:11:09 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The current system prompt for the chat thread.
|
|
|
|
/// </summary>
|
2024-07-13 08:37:57 +00:00
|
|
|
public string SystemPrompt { get; init; } = string.Empty;
|
|
|
|
|
2024-05-04 09:11:09 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The content blocks of the chat thread.
|
|
|
|
/// </summary>
|
2024-07-13 08:37:57 +00:00
|
|
|
public List<ContentBlock> Blocks { get; init; } = [];
|
2025-01-02 13:50:54 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Prepares the system prompt for the chat thread.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// The actual system prompt depends on the selected profile. If no profile is selected,
|
|
|
|
/// the system prompt is returned as is. When a profile is selected, the system prompt
|
|
|
|
/// is extended with the profile chosen.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="settingsManager">The settings manager instance to use.</param>
|
|
|
|
/// <param name="chatThread">The chat thread to prepare the system prompt for.</param>
|
|
|
|
/// <param name="logger">The logger instance to use.</param>
|
|
|
|
/// <returns>The prepared system prompt.</returns>
|
|
|
|
public string PrepareSystemPrompt(SettingsManager settingsManager, ChatThread chatThread, ILogger logger)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Prepare the system prompt:
|
|
|
|
//
|
|
|
|
string systemPromptText;
|
|
|
|
var logMessage = $"Using no profile for chat thread '{chatThread.Name}'.";
|
|
|
|
if (string.IsNullOrWhiteSpace(chatThread.SelectedProfile))
|
|
|
|
systemPromptText = chatThread.SystemPrompt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!Guid.TryParse(chatThread.SelectedProfile, out var profileId))
|
|
|
|
systemPromptText = chatThread.SystemPrompt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(chatThread.SelectedProfile == Profile.NO_PROFILE.Id || profileId == Guid.Empty)
|
|
|
|
systemPromptText = chatThread.SystemPrompt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var profile = settingsManager.ConfigurationData.Profiles.FirstOrDefault(x => x.Id == chatThread.SelectedProfile);
|
|
|
|
if(profile == default)
|
|
|
|
systemPromptText = chatThread.SystemPrompt;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logMessage = $"Using profile '{profile.Name}' for chat thread '{chatThread.Name}'.";
|
|
|
|
systemPromptText = $"""
|
|
|
|
{chatThread.SystemPrompt}
|
|
|
|
|
|
|
|
{profile.ToSystemPrompt()}
|
|
|
|
""";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.LogInformation(logMessage);
|
|
|
|
return systemPromptText;
|
|
|
|
}
|
2025-01-03 17:01:22 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Removes a content block from this chat thread.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="content">The content block to remove.</param>
|
|
|
|
public void Remove(IContent content)
|
|
|
|
{
|
|
|
|
var block = this.Blocks.FirstOrDefault(x => x.Content == content);
|
|
|
|
if(block is null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.Blocks.Remove(block);
|
|
|
|
}
|
2024-05-04 09:11:09 +00:00
|
|
|
}
|