Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions AI.NET/Resources/Strings/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions AI.NET/Resources/Strings/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
<data name="About" xml:space="preserve">
<value>About</value>
</data>
<data name="DeleteChat" xml:space="preserve">
<value>Delete Current Chat (Ctrl+D)</value>
<data name="RetryGeneration" xml:space="preserve">
<value>Retry Last AI Response (Ctrl+R)</value>
</data>
<data name="Mem0ServerUrl" xml:space="preserve">
<value>Mem0 Server URL</value>
Expand Down
4 changes: 2 additions & 2 deletions AI.NET/Resources/Strings/Strings.zh-CHS.resx
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@
<data name="About" xml:space="preserve">
<value>关于</value>
</data>
<data name="DeleteChat" xml:space="preserve">
<value>删除当前聊天 (Ctrl+D)</value>
<data name="RetryGeneration" xml:space="preserve">
<value>重新生成 AI 回复 (Ctrl+R)</value>
</data>
<data name="Mem0ServerUrl" xml:space="preserve">
<value>Mem0 服务器地址</value>
Expand Down
4 changes: 2 additions & 2 deletions AI.NET/Resources/Strings/Strings.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
<data name="About" xml:space="preserve">
<value>关于</value>
</data>
<data name="DeleteChat" xml:space="preserve">
<value>删除当前聊天 (Ctrl+D)</value>
<data name="RetryGeneration" xml:space="preserve">
<value>重新生成 AI 回复 (Ctrl+R)</value>
</data>
<data name="Mem0ServerUrl" xml:space="preserve">
<value>Mem0 服务器地址</value>
Expand Down
48 changes: 45 additions & 3 deletions AI.NET/Service/AI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,53 @@ public static async Task RequestAIAsync(string message, MarkdownScrollViewer out
TopicsHelper.SaveTopicsAsync(Topics);
}
/// <summary>
/// Delete a chat session
/// Retry generating the last AI response
/// </summary>
public static void DeleteMessages()
/// <param name="outputBox">The UI element to update</param>
public static async Task RetryLastResponseAsync(MarkdownScrollViewer outputBox)
{
Topics.RemoveAt(Topics.CurrentTopicIndex);
var messages = Topics.CurrentTopic.MessageList;

// Find the last user message and remove any subsequent AI/system messages
int lastUserIndex = -1;
for (int i = messages.Count - 1; i >= 0; i--)
{
if (messages[i].Role == ChatMessageRole.User)
{
lastUserIndex = i;
break;
}
}

if (lastUserIndex == -1)
{
return; // No user message found to retry
}

// Remove all messages after the last user message
while (messages.Count > lastUserIndex + 1)
{
messages.RemoveAt(messages.Count - 1);
}

string lastUserMessage = messages[lastUserIndex].Content;

// Update the output box to show we're retrying
string currentMarkdown = await Topics.CurrentTopic.GetMarkdownAsync();
outputBox.Markdown = currentMarkdown + "AI:";

// Handle memory if enabled
if (Mem0.IsEnabled)
await HandleMemoryAsync(lastUserMessage);

// Generate new AI response
Topics.CurrentTopic.Add(new Chat()
{
Content = await OpenAI.GenerateReplyAsync(outputBox),
Role = ChatMessageRole.Assistant
});

// Save topics to file
TopicsHelper.SaveTopicsAsync(Topics);
}
public static void NewChat(Data.SystemPrompt prompt)
Expand Down
4 changes: 2 additions & 2 deletions AI.NET/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
<Button x:Name="newButton" Click="NewChatButton_Click" Margin="10,0,0,0" ToolTip="{x:Static strings:Strings.NewChat}">
<ic:FluentIcon Icon="CommentAdd" IconVariant="Regular"/>
</Button>
<Button x:Name="deleteChatButton" Click="DeleteChatButton_Click" Margin="10,0,0,0" ToolTip="{x:Static strings:Strings.DeleteChat}">
<ic:FluentIcon Icon="Delete" IconVariant="Filled" Foreground="Red"/>
<Button x:Name="retryButton" Click="RetryButton_Click" Margin="10,0,0,0" ToolTip="{x:Static strings:Strings.RetryGeneration}">
<ic:FluentIcon Icon="ArrowClockwise" IconVariant="Filled" Foreground="Orange"/>
</Button>
</StackPanel>
</Grid>
Expand Down
27 changes: 19 additions & 8 deletions AI.NET/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void SetBusyState(bool isBusy)
sendButton.IsChecked = isBusy;
sendButton.IsEnabled = !isBusy;
newButton.IsEnabled = !isBusy;
deleteChatButton.IsEnabled = !isBusy;
retryButton.IsEnabled = !isBusy;
}
private void NewChatButton_Click(object sender, RoutedEventArgs e)
{
Expand All @@ -98,14 +98,25 @@ private void NewChatButton_Click(object sender, RoutedEventArgs e)
topicBox.SelectedIndex = Service.AI.Topics.TopicList.Count - 1;
}

private void DeleteChatButton_Click(object sender, RoutedEventArgs e)
private async void RetryButton_Click(object sender, RoutedEventArgs e)
{
if (topicBox.SelectedIndex >= 0 && topicBox.Items.Count > 0)
{
outputBox.Markdown = string.Empty;
int index = topicBox.SelectedIndex;
Service.AI.DeleteMessages();
topicBox.SelectedIndex = index - 1;
SetBusyState(true);
try
{
await Service.AI.RetryLastResponseAsync(outputBox);
}
catch (Exception ex)
{
Growl.Error(new() { StaysOpen = true, Message = ex.Message });
Log.Error("Error when retrying AI response", ex);
}
finally
{
outputBox.Markdown += markdownNewLine;
SetBusyState(false);
}
}
}

Expand Down Expand Up @@ -165,8 +176,8 @@ private void Window_KeyDown(object sender, KeyEventArgs e)
NewChatButton_Click(sender, e);
else if (e.Key == Key.Enter)
SendButton_Click(sender, e);
else if (e.Key == Key.D)
DeleteChatButton_Click(sender, e);
else if (e.Key == Key.R)
RetryButton_Click(sender, e);
}
}

Expand Down
Loading