AI-Studio/app/MindWork AI Studio/Assistants/AssistantBase.razor.cs

221 lines
6.4 KiB
C#
Raw Normal View History

2024-07-14 19:46:17 +00:00
using AIStudio.Chat;
using AIStudio.Provider;
using AIStudio.Settings;
using AIStudio.Tools;
2024-07-14 19:46:17 +00:00
using Microsoft.AspNetCore.Components;
2024-08-21 06:30:01 +00:00
namespace AIStudio.Assistants;
2024-07-14 19:46:17 +00:00
public abstract partial class AssistantBase : ComponentBase
{
[Inject]
protected SettingsManager SettingsManager { get; set; } = null!;
[Inject]
protected IJSRuntime JsRuntime { get; init; } = null!;
[Inject]
protected ThreadSafeRandom RNG { get; init; } = null!;
2024-07-14 19:46:17 +00:00
[Inject]
protected ISnackbar Snackbar { get; init; } = null!;
[Inject]
protected Rust Rust { get; init; } = null!;
2024-08-18 10:32:18 +00:00
[Inject]
protected NavigationManager NavigationManager { get; init; } = null!;
internal const string AFTER_RESULT_DIV_ID = "afterAssistantResult";
2024-08-21 06:30:01 +00:00
internal const string RESULT_DIV_ID = "assistantResult";
2024-07-14 19:46:17 +00:00
protected abstract string Title { get; }
protected abstract string Description { get; }
protected abstract string SystemPrompt { get; }
protected virtual Func<string> Result2Copy => () => this.resultingContentBlock is null ? string.Empty : this.resultingContentBlock.Content switch
{
ContentText textBlock => textBlock.Text,
_ => string.Empty,
};
2024-08-18 19:48:35 +00:00
protected abstract void ResetFrom();
protected abstract bool MightPreselectValues();
2024-07-14 19:46:17 +00:00
private protected virtual RenderFragment? Body => null;
protected virtual bool ShowResult => true;
protected virtual bool ShowDedicatedProgress => false;
2024-08-18 19:48:35 +00:00
protected virtual ChatThread ConvertToChatThread => this.chatThread ?? new();
2024-08-18 10:32:18 +00:00
protected virtual IReadOnlyList<IButtonData> FooterButtons => [];
protected static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
2024-07-16 08:28:13 +00:00
protected AIStudio.Settings.Provider providerSettings;
2024-07-14 19:46:17 +00:00
protected MudForm? form;
protected bool inputIsValid;
2024-08-18 19:48:35 +00:00
protected ChatThread? chatThread;
2024-07-14 19:46:17 +00:00
private ContentBlock? resultingContentBlock;
private string[] inputIssues = [];
private bool isProcessing;
2024-07-14 19:46:17 +00:00
#region Overrides of ComponentBase
protected override async Task OnParametersSetAsync()
{
// Configure the spellchecking for the user input:
this.SettingsManager.InjectSpellchecking(USER_INPUT_ATTRIBUTES);
await base.OnParametersSetAsync();
}
2024-07-14 19:46:17 +00:00
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Reset the validation when not editing and on the first render.
// We don't want to show validation errors when the user opens the dialog.
if(firstRender)
this.form?.ResetValidation();
await base.OnAfterRenderAsync(firstRender);
}
#endregion
2024-07-16 18:06:04 +00:00
protected string? ValidatingProvider(AIStudio.Settings.Provider provider)
{
if(provider.UsedProvider == Providers.NONE)
return "Please select a provider.";
return null;
}
2024-07-14 19:46:17 +00:00
protected void CreateChatThread()
{
this.chatThread = new()
{
WorkspaceId = Guid.Empty,
ChatId = Guid.NewGuid(),
Name = string.Empty,
Seed = this.RNG.Next(),
SystemPrompt = this.SystemPrompt,
Blocks = [],
};
}
protected DateTimeOffset AddUserRequest(string request)
{
var time = DateTimeOffset.Now;
this.chatThread!.Blocks.Add(new ContentBlock
{
Time = time,
ContentType = ContentType.TEXT,
Role = ChatRole.USER,
Content = new ContentText
{
Text = request,
},
});
return time;
}
protected async Task<string> AddAIResponseAsync(DateTimeOffset time)
2024-07-14 19:46:17 +00:00
{
var aiText = new ContentText
{
// We have to wait for the remote
// for the content stream:
InitialRemoteWait = true,
};
this.resultingContentBlock = new ContentBlock
{
Time = time,
ContentType = ContentType.TEXT,
Role = ChatRole.AI,
Content = aiText,
};
this.chatThread?.Blocks.Add(this.resultingContentBlock);
this.isProcessing = true;
this.StateHasChanged();
2024-07-14 19:46:17 +00:00
// Use the selected provider to get the AI response.
// By awaiting this line, we wait for the entire
// content to be streamed.
2024-07-16 08:28:13 +00:00
await aiText.CreateFromProviderAsync(this.providerSettings.CreateProvider(), this.JsRuntime, this.SettingsManager, this.providerSettings.Model, this.chatThread);
this.isProcessing = false;
this.StateHasChanged();
// Return the AI response:
return aiText.Text;
}
protected async Task CopyToClipboard()
{
await this.Rust.CopyText2Clipboard(this.JsRuntime, this.Snackbar, this.Result2Copy());
}
private static string? GetButtonIcon(string icon)
{
if(string.IsNullOrWhiteSpace(icon))
return null;
return icon;
2024-07-14 19:46:17 +00:00
}
2024-08-18 10:32:18 +00:00
2024-08-18 19:48:35 +00:00
private Task SendToAssistant(SendTo destination, SendToButton sendToButton)
2024-08-18 10:32:18 +00:00
{
var contentToSend = sendToButton.UseResultingContentBlockData switch
{
2024-08-18 19:48:35 +00:00
false => sendToButton.GetText(),
2024-08-18 10:32:18 +00:00
true => this.resultingContentBlock?.Content switch
{
ContentText textBlock => textBlock.Text,
_ => string.Empty,
},
};
var sendToData = destination.GetData();
2024-08-18 19:48:35 +00:00
switch (destination)
{
case SendTo.CHAT:
MessageBus.INSTANCE.DeferMessage(this, sendToData.Event, this.ConvertToChatThread);
2024-08-18 19:48:35 +00:00
break;
default:
MessageBus.INSTANCE.DeferMessage(this, sendToData.Event, contentToSend);
2024-08-18 19:48:35 +00:00
break;
}
this.NavigationManager.NavigateTo(sendToData.Route);
2024-08-18 10:32:18 +00:00
return Task.CompletedTask;
}
2024-08-18 19:48:35 +00:00
private async Task InnerResetForm()
{
this.resultingContentBlock = null;
this.providerSettings = default;
2024-08-21 06:30:01 +00:00
await this.JsRuntime.ClearDiv(RESULT_DIV_ID);
2024-08-18 19:48:35 +00:00
await this.JsRuntime.ClearDiv(AFTER_RESULT_DIV_ID);
this.ResetFrom();
this.inputIsValid = false;
this.inputIssues = [];
this.form?.ResetValidation();
this.StateHasChanged();
this.form?.ResetValidation();
}
2024-07-14 19:46:17 +00:00
}