Skip to content

Commit

Permalink
update text generation event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
BeepBeepBopBop committed Dec 28, 2024
1 parent df18e1a commit 61d2777
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
31 changes: 21 additions & 10 deletions LM-Kit-Maestro/UI/Razor/Components/Chat.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
@inject IScrollHandler ScrollHandler
@inject IResizeHandler ResizeHandler
@inject ILogger<Chat> Logger

@inject ISnackbar Snackbar
@inherits MvvmComponentBase<ChatPageViewModel>

<div id="chat-container">
<div id="conversation-container">
<div id="conversation-content"
class="chat-element dark @(ViewModel.ConversationListViewModel.CurrentConversation is { IsEmpty: true } ? "centered-container" : "top-align-container")">
class="chat-element dark @(ViewModel.ConversationListViewModel.CurrentConversation is { IsEmpty: true } ? "centered-container" : "top-align-container")">
@if (ViewModel?.ConversationListViewModel.CurrentConversation != null)
{
if (ViewModel.ConversationListViewModel.CurrentConversation.IsEmpty)
{
<div id="empty-conversation" class="vertical-stack">
<div class="vertical-stack spacing-4">
<div class="welcome-message"><b>Maestro</b> at your servicelets orchestrate something amazing!</div>
<div class="welcome-message">
Feel free to ask questions, explore ideas, or engage in meaningful conversations.
<div class="welcome-message"><b>Maestro</b> at your servicelets orchestrate something amazing!</div>
<div class="welcome-message">
Feel free to ask questions, explore ideas, or engage in meaningful conversations.

</div>
<div class="welcome-message">
Whether you need assistance, inspiration, or just some lighthearted chat, I'm here to help.
</div>
<div class="welcome-message">
Whether you need assistance, inspiration, or just some lighthearted chat, I'm here to help.

</div>
</div>

</div>

Expand Down Expand Up @@ -79,7 +79,7 @@
Tokens: @ViewModel.ConversationListViewModel.CurrentConversation.LMKitConversation.ContextUsedSpace /
@ViewModel.ConversationListViewModel.CurrentConversation.LMKitConversation.ContextSize
(@CalculateUsagePercentage(ViewModel.ConversationListViewModel.CurrentConversation.LMKitConversation.ContextUsedSpace,
ViewModel.ConversationListViewModel.CurrentConversation.LMKitConversation.ContextSize)%)
ViewModel.ConversationListViewModel.CurrentConversation.LMKitConversation.ContextSize)%)
</text>
}
</div>
Expand Down Expand Up @@ -219,13 +219,16 @@
if (_previousConversationViewModel != null)
{
_previousConversationViewModel.Messages.CollectionChanged -= OnConversationMessagesCollectionChanged;
_previousConversationViewModel.TextGenerationCompleted -= OnTextGenerationCompleted;
}

_previousConversationViewModel = ViewModel.ConversationListViewModel.CurrentConversation;

if (ViewModel.ConversationListViewModel.CurrentConversation != null)
{
ViewModel.ConversationListViewModel.CurrentConversation.Messages.CollectionChanged += OnConversationMessagesCollectionChanged;
ViewModel.ConversationListViewModel.CurrentConversation.TextGenerationCompleted += OnTextGenerationCompleted;

_previousScrollTop = null;
_ignoreScrollsUntilNextScrollUp = true;
IsScrolledToEnd = true;
Expand All @@ -251,6 +254,14 @@
}
}

private void OnTextGenerationCompleted(object? sender, ConversationViewModel.TextGenerationCompletedEventArgs e)
{
if (e.Exception != null)
{
Snackbar.Add($"Text generation failed unexpectedly:\n{e.Exception.Message}", Severity.Error);
}
}

private async void OnLatestAssistantResponseProgressed()
{
if (_shouldAutoScrollEnd)
Expand Down
5 changes: 3 additions & 2 deletions LM-Kit-Maestro/ViewModels/ConversationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ public Uri? LastUsedModel
}
}

public EventHandler? TextGenerationCompleted;
public EventHandler? TextGenerationFailed;
public delegate void TextGenerationCompletedEventHandler(object sender, TextGenerationCompletedEventArgs e);

public TextGenerationCompletedEventHandler? TextGenerationCompleted;
public EventHandler? DatabaseSaveOperationCompleted;
public EventHandler? DatabaseSaveOperationFailed;

Expand Down
10 changes: 5 additions & 5 deletions tests/LmKitServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task HonorsTimeout()
var response = await testService.LMKitService.SubmitPrompt(conversation, "tell me a story");

Assert.NotNull(response);
Assert.Equal(LMKitTextGenerationStatus.Cancelled, response.Status);
Assert.Equal(LMKitRequestStatus.Cancelled, response.Status);
Assert.True(response.Exception is OperationCanceledException operationCancelled);
}

Expand Down Expand Up @@ -167,7 +167,7 @@ private async Task SubmitOnePromptChangeModelSubmitAnother2()

var firstPromptResult = await firstResponseTask;

Assert.True(firstPromptResult.Status == LMKitTextGenerationStatus.Cancelled || firstPromptResult.Status == LMKitTextGenerationStatus.GenericError);
Assert.True(firstPromptResult.Status == LMKitRequestStatus.Cancelled || firstPromptResult.Status == LMKitRequestStatus.GenericError);

loadingSuccess = await secondModelLoadingTask;
Assert.True(loadingSuccess);
Expand All @@ -193,10 +193,10 @@ private async Task Submit2PromptsFromDistinctConversationsThenUnloadModel()
Assert.True(unloadingSuccess);

var result = await conversation1.PromptResultTask.Task;
Assert.True(result != null && result.Status == LMKitTextGenerationStatus.Cancelled);
Assert.True(result != null && result.Status == LMKitRequestStatus.Cancelled);

result = await conversation2.PromptResultTask.Task;
Assert.True(result != null && result.Status == LMKitTextGenerationStatus.Cancelled);
Assert.True(result != null && result.Status == LMKitRequestStatus.Cancelled);
}

[Fact]
Expand All @@ -214,7 +214,7 @@ private async Task SubmitOnePromptThenUnloadModel()
Assert.True(unloadingSuccess);

var result = await conversation1.PromptResultTask.Task;
Assert.True(result != null && result.Status == LMKitTextGenerationStatus.Cancelled);
Assert.True(result != null && result.Status == LMKitRequestStatus.Cancelled);
}

[Fact]
Expand Down
19 changes: 9 additions & 10 deletions tests/Services/ConversationViewModelWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public ConversationViewModelWrapper(ConversationViewModel conversationViewModel)
{
ConversationViewModel = conversationViewModel;
ConversationViewModel.TextGenerationCompleted += OnTextGenerationCompleted;
ConversationViewModel.TextGenerationFailed += OnTextGenerationFailed;
ConversationViewModel.DatabaseSaveOperationCompleted += OnDatabaseSynchronizationCompleted;
ConversationViewModel.PropertyChanged += ConversationViewModel_PropertyChanged;
}
Expand All @@ -27,18 +26,18 @@ private void ConversationViewModel_PropertyChanged(object? sender, System.Compon
}
}

private void OnTextGenerationCompleted(object? sender, EventArgs e)
private void OnTextGenerationCompleted(object? sender, ConversationViewModel.TextGenerationCompletedEventArgs e)
{
var conversationViewModel = (ConversationViewModel)sender!;

PromptResultTask.SetResult(true);
}

private void OnTextGenerationFailed(object? sender, EventArgs e)
{
var conversationViewModel = (ConversationViewModel)sender!;

PromptResultTask.SetResult(false);
if (e.Exception != null || e.Status != Maestro.Services.LMKitRequestStatus.OK)
{
PromptResultTask.SetResult(false);
}
else
{
PromptResultTask.SetResult(true);
}
}

private void OnDatabaseSynchronizationCompleted(object? sender, EventArgs e)
Expand Down
8 changes: 4 additions & 4 deletions tests/Services/LMKitMaestroTestsHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class MaestroTestsHelpers
{
public static void AssertPromptResponseIsSuccessful(LMKitService.LMKitResult promptResult)
{
Assert.Equal(LMKitTextGenerationStatus.Undefined, promptResult.Status);
Assert.Equal(LMKitRequestStatus.OK, promptResult.Status);
Assert.Null(promptResult.Exception);
Assert.NotNull(promptResult.Result);
}
Expand All @@ -18,8 +18,8 @@ public static void AssertConversationPromptSuccessState(ConversationViewModelWra
{
Assert.True(testConversation.PromptResultTask.Task.Result);
Assert.True(testConversation.ConversationViewModel.Messages.Count == expectedMessageCount);
Assert.Equal(LMKitTextGenerationStatus.Undefined, testConversation.ConversationViewModel.Messages[0].Status);
Assert.Equal(LMKitTextGenerationStatus.Undefined, testConversation.ConversationViewModel.Messages[1].Status);
Assert.Equal(LMKitRequestStatus.OK, testConversation.ConversationViewModel.Messages[0].Status);
Assert.Equal(LMKitRequestStatus.OK, testConversation.ConversationViewModel.Messages[1].Status);
Assert.False(string.IsNullOrEmpty(testConversation.ConversationViewModel.Messages[0].Content));
Assert.False(string.IsNullOrEmpty(testConversation.ConversationViewModel.Messages[1].Content));
Assert.False(testConversation.ConversationViewModel.AwaitingResponse);
Expand All @@ -32,7 +32,7 @@ public static void AssertConversationPromptCancelledState(ConversationViewModelW

if (testConversation.ConversationViewModel.Messages.Count == 2)
{
Assert.Equal(LMKitTextGenerationStatus.Cancelled, testConversation.ConversationViewModel.Messages[1].Status);
Assert.Equal(LMKitRequestStatus.Cancelled, testConversation.ConversationViewModel.Messages[1].Status);
Assert.False(string.IsNullOrEmpty(testConversation.ConversationViewModel.Messages[0].Content));

}
Expand Down

0 comments on commit 61d2777

Please sign in to comment.