using AIStudio.Settings;
namespace AIStudio.Chat;
///
/// Data structure for a chat thread.
///
public sealed record ChatThread
{
///
/// The unique identifier of the chat thread.
///
public Guid ChatId { get; init; }
///
/// The unique identifier of the workspace.
///
public Guid WorkspaceId { get; set; }
///
/// Specifies the provider selected for the chat thread.
///
public string SelectedProvider { get; set; } = string.Empty;
///
/// Specifies the profile selected for the chat thread.
///
public string SelectedProfile { get; set; } = string.Empty;
///
/// The name of the chat thread. Usually generated by an AI model or manually edited by the user.
///
public string Name { get; set; } = string.Empty;
///
/// The seed for the chat thread. Some providers use this to generate deterministic results.
///
public int Seed { get; init; }
///
/// The current system prompt for the chat thread.
///
public string SystemPrompt { get; init; } = string.Empty;
///
/// The content blocks of the chat thread.
///
public List Blocks { get; init; } = [];
///
/// Prepares the system prompt for the chat thread.
///
///
/// 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.
///
/// The settings manager instance to use.
/// The chat thread to prepare the system prompt for.
/// The logger instance to use.
/// The prepared system prompt.
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;
}
}