Skip to content

Commit

Permalink
Send to chat (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
SommerEngineering authored Aug 18, 2024
1 parent 8aeeda5 commit f8e06fa
Show file tree
Hide file tree
Showing 26 changed files with 384 additions and 121 deletions.
2 changes: 1 addition & 1 deletion app/MindWork AI Studio/Chat/ChatThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace AIStudio.Chat;
/// <summary>
/// Data structure for a chat thread.
/// </summary>
public sealed class ChatThread
public sealed record ChatThread
{
/// <summary>
/// The unique identifier of the chat thread.
Expand Down
6 changes: 6 additions & 0 deletions app/MindWork AI Studio/Chat/SystemPrompts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace AIStudio.Chat;

public static class SystemPrompts
{
public const string DEFAULT = "You are a helpful assistant!";
}
19 changes: 11 additions & 8 deletions app/MindWork AI Studio/Components/AssistantBase.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
@using AIStudio.Components.Pages
@using AIStudio.Tools
<MudText Typo="Typo.h3" Class="mb-2 mr-3">
@this.Title
@(this.Title)
</MudText>

<InnerScrolling HeaderHeight="12.3em">
<ChildContent>
<MudForm @ref="@this.form" @bind-IsValid="@this.inputIsValid" @bind-Errors="@this.inputIssues" Class="pr-2">
<MudForm @ref="@(this.form)" @bind-IsValid="@(this.inputIsValid)" @bind-Errors="@(this.inputIssues)" Class="pr-2">
<MudText Typo="Typo.body1" Align="Align.Justify" Class="mb-6">
@this.Description
@(this.Description)
</MudText>

@if (this.Body is not null)
{
@this.Body
@(this.Body)
}
</MudForm>
<Issues IssuesData="@this.inputIssues"/>
<Issues IssuesData="@(this.inputIssues)"/>

@if (this.ShowDedicatedProgress && this.isProcessing)
{
Expand All @@ -26,7 +26,7 @@
<div id="@ASSISTANT_RESULT_DIV_ID" class="mr-2 mt-3">
@if (this.ShowResult && this.resultingContentBlock is not null)
{
<ContentBlockComponent Role="@this.resultingContentBlock.Role" Type="@this.resultingContentBlock.ContentType" Time="@this.resultingContentBlock.Time" Content="@this.resultingContentBlock.Content"/>
<ContentBlockComponent Role="@(this.resultingContentBlock.Role)" Type="@(this.resultingContentBlock.ContentType)" Time="@(this.resultingContentBlock.Time)" Content="@(this.resultingContentBlock.Content)"/>
}
</div>

Expand Down Expand Up @@ -56,9 +56,9 @@

case SendToButton sendToButton:
<MudMenu StartIcon="@Icons.Material.Filled.Apps" EndIcon="@Icons.Material.Filled.KeyboardArrowDown" Label="Send to ..." Variant="Variant.Filled" Color="Color.Info">
@foreach (var assistant in Enum.GetValues<SendToAssistant>().OrderBy(n => n.Name().Length))
@foreach (var assistant in Enum.GetValues<SendTo>().OrderBy(n => n.Name().Length))
{
if(assistant is Pages.SendToAssistant.NONE || sendToButton.Self == assistant)
if(assistant is SendTo.NONE || sendToButton.Self == assistant)
continue;

<MudMenuItem OnClick="() => this.SendToAssistant(assistant, sendToButton)">
Expand All @@ -69,6 +69,9 @@
break;
}
}
<MudButton Variant="Variant.Filled" Color="Color.Warning" StartIcon="@Icons.Material.Filled.Refresh" OnClick="() => this.InnerResetForm()">
Reset
</MudButton>
</MudStack>
}
</ChildContent>
Expand Down
62 changes: 49 additions & 13 deletions app/MindWork AI Studio/Components/AssistantBase.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ public abstract partial class AssistantBase : ComponentBase
protected abstract string Description { get; }

protected abstract string SystemPrompt { get; }

protected abstract void ResetFrom();

protected abstract bool MightPreselectValues();

private protected virtual RenderFragment? Body => null;

protected virtual bool ShowResult => true;

protected virtual bool ShowDedicatedProgress => false;

protected virtual ChatThread ConvertToChatThread => this.chatThread ?? new();

protected virtual IReadOnlyList<IButtonData> FooterButtons => [];

protected static readonly Dictionary<string, object?> USER_INPUT_ATTRIBUTES = new();
Expand All @@ -53,7 +59,7 @@ public abstract partial class AssistantBase : ComponentBase
protected MudForm? form;
protected bool inputIsValid;

private ChatThread? chatThread;
protected ChatThread? chatThread;
private ContentBlock? resultingContentBlock;
private string[] inputIssues = [];
private bool isProcessing;
Expand Down Expand Up @@ -164,33 +170,63 @@ protected async Task CopyToClipboard(string text)
return icon;
}

private Task SendToAssistant(SendToAssistant assistant, SendToButton sendToButton)
private Task SendToAssistant(SendTo destination, SendToButton sendToButton)
{
var contentToSend = sendToButton.UseResultingContentBlockData switch
{
false => sendToButton.GetData(),
false => sendToButton.GetText(),
true => this.resultingContentBlock?.Content switch
{
ContentText textBlock => textBlock.Text,
_ => string.Empty,
},
};

var (eventItem, path) = assistant switch
var (eventItem, path) = destination switch
{
Pages.SendToAssistant.AGENDA_ASSISTANT => (Event.SEND_TO_AGENDA_ASSISTANT, Path.ASSISTANT_AGENDA),
Pages.SendToAssistant.CODING_ASSISTANT => (Event.SEND_TO_CODING_ASSISTANT, Path.ASSISTANT_CODING),
Pages.SendToAssistant.REWRITE_ASSISTANT => (Event.SEND_TO_REWRITE_ASSISTANT, Path.ASSISTANT_REWRITE),
Pages.SendToAssistant.TRANSLATION_ASSISTANT => (Event.SEND_TO_TRANSLATION_ASSISTANT, Path.ASSISTANT_TRANSLATION),
Pages.SendToAssistant.ICON_FINDER_ASSISTANT => (Event.SEND_TO_ICON_FINDER_ASSISTANT, Path.ASSISTANT_ICON_FINDER),
Pages.SendToAssistant.GRAMMAR_SPELLING_ASSISTANT => (Event.SEND_TO_GRAMMAR_SPELLING_ASSISTANT, Path.ASSISTANT_GRAMMAR_SPELLING),
Pages.SendToAssistant.TEXT_SUMMARIZER_ASSISTANT => (Event.SEND_TO_TEXT_SUMMARIZER_ASSISTANT, Path.ASSISTANT_SUMMARIZER),
SendTo.AGENDA_ASSISTANT => (Event.SEND_TO_AGENDA_ASSISTANT, Path.ASSISTANT_AGENDA),
SendTo.CODING_ASSISTANT => (Event.SEND_TO_CODING_ASSISTANT, Path.ASSISTANT_CODING),
SendTo.REWRITE_ASSISTANT => (Event.SEND_TO_REWRITE_ASSISTANT, Path.ASSISTANT_REWRITE),
SendTo.TRANSLATION_ASSISTANT => (Event.SEND_TO_TRANSLATION_ASSISTANT, Path.ASSISTANT_TRANSLATION),
SendTo.ICON_FINDER_ASSISTANT => (Event.SEND_TO_ICON_FINDER_ASSISTANT, Path.ASSISTANT_ICON_FINDER),
SendTo.GRAMMAR_SPELLING_ASSISTANT => (Event.SEND_TO_GRAMMAR_SPELLING_ASSISTANT, Path.ASSISTANT_GRAMMAR_SPELLING),
SendTo.TEXT_SUMMARIZER_ASSISTANT => (Event.SEND_TO_TEXT_SUMMARIZER_ASSISTANT, Path.ASSISTANT_SUMMARIZER),

SendTo.CHAT => (Event.SEND_TO_CHAT, Path.CHAT),

_ => (Event.NONE, Path.ASSISTANTS),
};

MessageBus.INSTANCE.DeferMessage(this, eventItem, contentToSend);

switch (destination)
{
case SendTo.CHAT:
MessageBus.INSTANCE.DeferMessage(this, eventItem, this.ConvertToChatThread);
break;

default:
MessageBus.INSTANCE.DeferMessage(this, eventItem, contentToSend);
break;
}

this.NavigationManager.NavigateTo(path);
return Task.CompletedTask;
}

private async Task InnerResetForm()
{
this.resultingContentBlock = null;
this.providerSettings = default;

await this.JsRuntime.ClearDiv(ASSISTANT_RESULT_DIV_ID);
await this.JsRuntime.ClearDiv(AFTER_RESULT_DIV_ID);

this.ResetFrom();

this.inputIsValid = false;
this.inputIssues = [];

this.form?.ResetValidation();
this.StateHasChanged();
this.form?.ResetValidation();
}
}
1 change: 1 addition & 0 deletions app/MindWork AI Studio/Components/Blocks/Changelog.Logs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public readonly record struct Log(int Build, string Display, string Filename)

public static readonly Log[] LOGS =
[
new (172, "v0.8.10, build 172 (2024-08-18 19:44 UTC)", "v0.8.10.md"),
new (171, "v0.8.9, build 171 (2024-08-18 10:35 UTC)", "v0.8.9.md"),
new (170, "v0.8.8, build 170 (2024-08-14 06:30 UTC)", "v0.8.8.md"),
new (169, "v0.8.7, build 169 (2024-08-01 19:08 UTC)", "v0.8.7.md"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;

using AIStudio.Chat;
using AIStudio.Tools;

namespace AIStudio.Components.Pages.Agenda;
Expand Down Expand Up @@ -97,9 +98,73 @@ the logistical challenges that come with an increasing number of participants.
[
new SendToButton
{
Self = SendToAssistant.AGENDA_ASSISTANT,
Self = SendTo.AGENDA_ASSISTANT,
},
];

protected override ChatThread ConvertToChatThread => (this.chatThread ?? new()) with
{
SystemPrompt = SystemPrompts.DEFAULT,
};

protected override void ResetFrom()
{
this.inputContent = string.Empty;
this.contentLines.Clear();
this.selectedFoci = [];
this.justBriefly = [];
this.inputWhoIsPresenting = string.Empty;
if (!this.MightPreselectValues())
{
this.inputTopic = string.Empty;
this.inputName = string.Empty;
this.inputDuration = string.Empty;
this.inputStartTime = string.Empty;
this.inputObjective = string.Empty;
this.inputModerator = string.Empty;
this.selectedTargetLanguage = CommonLanguages.AS_IS;
this.customTargetLanguage = string.Empty;
this.introduceParticipants = false;
this.isMeetingVirtual = true;
this.inputLocation = string.Empty;
this.goingToDinner = false;
this.doingSocialActivity = false;
this.needToArriveAndDepart = false;
this.durationLunchBreak = 0;
this.durationBreaks = 0;
this.activeParticipation = false;
this.numberParticipants = NumberParticipants.NOT_SPECIFIED;
}
}

protected override bool MightPreselectValues()
{
if (this.SettingsManager.ConfigurationData.Agenda.PreselectOptions)
{
this.inputTopic = this.SettingsManager.ConfigurationData.Agenda.PreselectTopic;
this.inputName = this.SettingsManager.ConfigurationData.Agenda.PreselectName;
this.inputDuration = this.SettingsManager.ConfigurationData.Agenda.PreselectDuration;
this.inputStartTime = this.SettingsManager.ConfigurationData.Agenda.PreselectStartTime;
this.inputObjective = this.SettingsManager.ConfigurationData.Agenda.PreselectObjective;
this.inputModerator = this.SettingsManager.ConfigurationData.Agenda.PreselectModerator;
this.selectedTargetLanguage = this.SettingsManager.ConfigurationData.Agenda.PreselectedTargetLanguage;
this.customTargetLanguage = this.SettingsManager.ConfigurationData.Agenda.PreselectedOtherLanguage;
this.introduceParticipants = this.SettingsManager.ConfigurationData.Agenda.PreselectIntroduceParticipants;
this.isMeetingVirtual = this.SettingsManager.ConfigurationData.Agenda.PreselectIsMeetingVirtual;
this.inputLocation = this.SettingsManager.ConfigurationData.Agenda.PreselectLocation;
this.goingToDinner = this.SettingsManager.ConfigurationData.Agenda.PreselectJointDinner;
this.doingSocialActivity = this.SettingsManager.ConfigurationData.Agenda.PreselectSocialActivity;
this.needToArriveAndDepart = this.SettingsManager.ConfigurationData.Agenda.PreselectArriveAndDepart;
this.durationLunchBreak = this.SettingsManager.ConfigurationData.Agenda.PreselectLunchTime;
this.durationBreaks = this.SettingsManager.ConfigurationData.Agenda.PreselectBreakTime;
this.activeParticipation = this.SettingsManager.ConfigurationData.Agenda.PreselectActiveParticipation;
this.numberParticipants = this.SettingsManager.ConfigurationData.Agenda.PreselectNumberParticipants;
this.providerSettings = this.SettingsManager.ConfigurationData.Providers.FirstOrDefault(x => x.Id == this.SettingsManager.ConfigurationData.Agenda.PreselectedProvider);
return true;
}

return false;
}

private string inputTopic = string.Empty;
private string inputName = string.Empty;
Expand Down Expand Up @@ -130,29 +195,7 @@ the logistical challenges that come with an increasing number of participants.

protected override async Task OnInitializedAsync()
{
if (this.SettingsManager.ConfigurationData.Agenda.PreselectOptions)
{
this.inputTopic = this.SettingsManager.ConfigurationData.Agenda.PreselectTopic;
this.inputName = this.SettingsManager.ConfigurationData.Agenda.PreselectName;
this.inputDuration = this.SettingsManager.ConfigurationData.Agenda.PreselectDuration;
this.inputStartTime = this.SettingsManager.ConfigurationData.Agenda.PreselectStartTime;
this.inputObjective = this.SettingsManager.ConfigurationData.Agenda.PreselectObjective;
this.inputModerator = this.SettingsManager.ConfigurationData.Agenda.PreselectModerator;
this.selectedTargetLanguage = this.SettingsManager.ConfigurationData.Agenda.PreselectedTargetLanguage;
this.customTargetLanguage = this.SettingsManager.ConfigurationData.Agenda.PreselectedOtherLanguage;
this.introduceParticipants = this.SettingsManager.ConfigurationData.Agenda.PreselectIntroduceParticipants;
this.isMeetingVirtual = this.SettingsManager.ConfigurationData.Agenda.PreselectIsMeetingVirtual;
this.inputLocation = this.SettingsManager.ConfigurationData.Agenda.PreselectLocation;
this.goingToDinner = this.SettingsManager.ConfigurationData.Agenda.PreselectJointDinner;
this.doingSocialActivity = this.SettingsManager.ConfigurationData.Agenda.PreselectSocialActivity;
this.needToArriveAndDepart = this.SettingsManager.ConfigurationData.Agenda.PreselectArriveAndDepart;
this.durationLunchBreak = this.SettingsManager.ConfigurationData.Agenda.PreselectLunchTime;
this.durationBreaks = this.SettingsManager.ConfigurationData.Agenda.PreselectBreakTime;
this.activeParticipation = this.SettingsManager.ConfigurationData.Agenda.PreselectActiveParticipation;
this.numberParticipants = this.SettingsManager.ConfigurationData.Agenda.PreselectNumberParticipants;
this.providerSettings = this.SettingsManager.ConfigurationData.Providers.FirstOrDefault(x => x.Id == this.SettingsManager.ConfigurationData.Agenda.PreselectedProvider);
}

this.MightPreselectValues();
var deferredContent = MessageBus.INSTANCE.CheckDeferredMessages<string>(Event.SEND_TO_AGENDA_ASSISTANT).FirstOrDefault();
if (deferredContent is not null)
this.inputContent = deferredContent;
Expand Down
20 changes: 19 additions & 1 deletion app/MindWork AI Studio/Components/Pages/Chat.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ protected override async Task OnInitializedAsync()
this.providerSettings = this.SettingsManager.ConfigurationData.Providers.FirstOrDefault(x => x.Id == this.SettingsManager.ConfigurationData.Chat.PreselectedProvider);
}

var deferredContent = MessageBus.INSTANCE.CheckDeferredMessages<ChatThread>(Event.SEND_TO_CHAT).FirstOrDefault();
if (deferredContent is not null)
{
this.chatThread = deferredContent;
if (this.chatThread is not null)
{
var firstUserBlock = this.chatThread.Blocks.FirstOrDefault(x => x.Role == ChatRole.USER);
if (firstUserBlock is not null)
{
this.chatThread.Name = firstUserBlock.Content switch
{
ContentText textBlock => this.ExtractThreadName(textBlock.Text),
_ => "Thread"
};
}
}
}

await base.OnInitializedAsync();
}

Expand Down Expand Up @@ -93,7 +111,7 @@ private async Task SendMessage()
ChatId = Guid.NewGuid(),
Name = threadName,
Seed = this.RNG.Next(),
SystemPrompt = "You are a helpful assistant!",
SystemPrompt = SystemPrompts.DEFAULT,
Blocks = [],
};
}
Expand Down
Loading

0 comments on commit f8e06fa

Please sign in to comment.