Skip to content

Commit

Permalink
修复历史记录的一些错误
Browse files Browse the repository at this point in the history
  • Loading branch information
mehaifeng committed May 5, 2023
1 parent 5589123 commit 8176603
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions ViewModels/VisualChatViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,29 @@ namespace VisualChatBot.ViewModels
{
public partial class VisualChatViewModel : ObservableObject, INotifyPropertyChanged
{
//配置文件地址
/// <summary>
/// 配置文件地址
/// </summary>
string configPath = $"{System.Environment.CurrentDirectory}//UserConfig.json";
//历史文件地址
/// <summary>
/// 历史文件地址
/// </summary>
string historyChatPath = $"{System.Environment.CurrentDirectory}//HistoryChat.json";
//API地址
/// <summary>
/// API地址
/// </summary>
string urlAddress = $"https://api.openai.com/v1/chat/completions";
//是否处于发送状态
/// <summary>
/// 是否处于发送状态
/// </summary>
bool isSending = false;
//发送次数
/// <summary>
/// 是否是历史对话导出的
/// </summary>
bool isHistoryChat = false;
/// <summary>
/// 发送次数
/// </summary>
int sendTimes = 0 ;
private List<HistoryMessage> historyMessages = new List<HistoryMessage>();
Tools.WebRequest request;
Expand Down Expand Up @@ -273,7 +287,7 @@ async void Send(StackPanel o)
//逐字显示
ShowGenerateText(receivedViewModel);
//总结主要话题
SummarizeTitle();
SummarizeTitle(o);
isSending = false;
#endregion
}
Expand All @@ -286,9 +300,9 @@ async void Send(StackPanel o)
/// <summary>
/// 总结对话标题
/// </summary>
async Task SummarizeTitle()
async Task SummarizeTitle(StackPanel o)
{
if (!historyMessages.Any(t => t.Title == Title))
if (!historyMessages.Any(t=>t.Title==Title))
{
if (sendTimes == 3)
{
Expand All @@ -310,6 +324,10 @@ async Task SummarizeTitle()
}
}
}
if (o.Children.Count < 6 && o.Children.Count > 0 && isHistoryChat == false)
{
Title = $"#{messageList[messageList.Count-2].content}{System.DateTime.Now}】#";
}
}

/// <summary>
Expand Down Expand Up @@ -389,8 +407,8 @@ void ClearAll(StackPanel o)
string currentChatElement = o.ToXAMLString();
historyMessages.Add(new HistoryMessage
{
AllMessage = messageList,
Title = !string.IsNullOrEmpty(Title) ? Title : $"{messageList.First().content}{System.DateTime.Now}】",
AllMessage = new List<Message>(messageList),
Title = Title,
ControlStruct = currentChatElement
});
string historyToStr = JsonConvert.SerializeObject(historyMessages, Formatting.Indented);
Expand All @@ -399,17 +417,20 @@ void ClearAll(StackPanel o)
sendTimes = 0;
o.Children.Clear();
MenuHistorySources.Add(Title);
messageList.Clear();
}
else if(historyMessages.Any(t => t.Title == Title)&&o.Children.Count>0)
{
var item = historyMessages.First(t => t.Title == Title);
string currentChatElement = o.ToXAMLString();
item.ControlStruct = currentChatElement;
item.AllMessage = messageList;
item.AllMessage = new List<Message>(messageList);
string historyToStr = JsonConvert.SerializeObject(historyMessages, Formatting.Indented);
File.WriteAllText(historyChatPath, historyToStr);
o.Children.Clear();
messageList.Clear();
}
isHistoryChat = false;
}
/// <summary>
/// 切换到历史对话
Expand All @@ -436,8 +457,9 @@ void ReviewHistoryChat(object[] o)
replacePanel.Children.Remove(child);
stackPanel.Children.Add(child);
}
isHistoryChat = true;
messageList = item.AllMessage;
Title = item.Title;
Title = title;
RecoveryThemeOnChatBox(stackPanel);
}
}
Expand All @@ -454,7 +476,7 @@ void DeleteHistoryChat(object[] o)
MenuHistorySources.Remove(o[0].ToString());
string historyToStr = JsonConvert.SerializeObject(historyMessages, Formatting.Indented);
File.WriteAllText(historyChatPath, historyToStr);
if (messageList.Count > 0 && historyMessages.Count > 0 && historyMessages.Last().Title == o[0].ToString())
if (messageList.Count > 0 && historyMessages.Count > 0)
{
var stackpanel = (StackPanel)o[1];
stackpanel.Children.Clear();
Expand Down

0 comments on commit 8176603

Please sign in to comment.