2023-10-07 13:27:25 +08:00
|
|
|
@page "/"
|
2023-10-11 11:54:28 +08:00
|
|
|
@using Frontend.Models
|
2023-10-17 23:32:39 +08:00
|
|
|
@using Frontend.Services
|
|
|
|
@using Katheryne
|
2023-10-17 22:20:38 +08:00
|
|
|
@using Katheryne.Abstractions
|
|
|
|
@inject IChatRobotFactory ChatRobotFactory
|
2023-10-17 23:32:39 +08:00
|
|
|
@inject GrammarStorageService GrammarStorage
|
|
|
|
@inject DefaultChatRobot DefaultRobot
|
2023-10-09 19:32:56 +08:00
|
|
|
|
2023-10-07 13:27:25 +08:00
|
|
|
|
2023-10-07 18:55:22 +08:00
|
|
|
<Layout>
|
2023-10-16 14:00:03 +08:00
|
|
|
<Sider Width="200">
|
2023-10-11 11:54:28 +08:00
|
|
|
<div class="chat-control-zone">
|
|
|
|
<div>
|
|
|
|
<Button Type="@ButtonType.Primary" Size="large" @onclick="@CreateChatClicked">
|
|
|
|
新建对话
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<div class="chat-list">
|
|
|
|
<AntList TItem="@Chat" DataSource="@_chatDictionary.Values"
|
|
|
|
Split="@false">
|
|
|
|
<ListItem OnClick="@(() => ChangeChatClicked(context.Guid))">
|
|
|
|
<ListItemMeta>
|
|
|
|
<TitleTemplate>
|
2023-10-11 14:23:38 +08:00
|
|
|
@if (context.Selected)
|
2023-10-11 11:54:28 +08:00
|
|
|
{
|
|
|
|
<div class="selected-chat-name">
|
|
|
|
<p style="margin: 5px">@context.Title</p>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
<div class="chat-name">
|
|
|
|
<p style="margin: 5px">@context.Title</p>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</TitleTemplate>
|
|
|
|
</ListItemMeta>
|
|
|
|
</ListItem>
|
|
|
|
</AntList>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-10-07 18:55:22 +08:00
|
|
|
</Sider>
|
2023-10-11 11:54:28 +08:00
|
|
|
|
2023-10-07 18:55:22 +08:00
|
|
|
<Content>
|
2023-10-17 23:32:39 +08:00
|
|
|
<ChatZone Messages="@GetChatMessages()" Robot="@GetChatRobot()"/>
|
2023-10-07 18:55:22 +08:00
|
|
|
</Content>
|
|
|
|
</Layout>
|
|
|
|
|
2023-10-09 19:32:56 +08:00
|
|
|
@code {
|
2023-10-11 11:54:28 +08:00
|
|
|
private readonly Dictionary<Guid, Chat> _chatDictionary = new();
|
|
|
|
|
|
|
|
private Guid _currentGuid;
|
|
|
|
|
2023-10-17 23:32:39 +08:00
|
|
|
protected override async Task OnInitializedAsync()
|
2023-10-11 11:54:28 +08:00
|
|
|
{
|
2023-10-17 23:32:39 +08:00
|
|
|
await GrammarStorage.RestoreGrammar();
|
|
|
|
|
2023-10-13 16:24:03 +08:00
|
|
|
Chat chat = GetInitChat();
|
2023-10-11 11:54:28 +08:00
|
|
|
_chatDictionary.Add(chat.Guid, chat);
|
|
|
|
_currentGuid = chat.Guid;
|
2023-10-17 23:32:39 +08:00
|
|
|
|
|
|
|
await base.OnInitializedAsync();
|
2023-10-11 11:54:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void CreateChatClicked()
|
|
|
|
{
|
2023-10-11 14:23:38 +08:00
|
|
|
Chat chat = GetInitChat();
|
2023-10-11 11:54:28 +08:00
|
|
|
|
|
|
|
_chatDictionary.Add(chat.Guid, chat);
|
|
|
|
_chatDictionary[_currentGuid].Selected = false;
|
|
|
|
_currentGuid = chat.Guid;
|
|
|
|
_chatDictionary[_currentGuid].Selected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ChangeChatClicked(Guid guid)
|
|
|
|
{
|
|
|
|
_chatDictionary[_currentGuid].Selected = false;
|
|
|
|
_currentGuid = guid;
|
|
|
|
_chatDictionary[_currentGuid].Selected = true;
|
|
|
|
}
|
2023-10-11 14:23:38 +08:00
|
|
|
|
|
|
|
private Chat GetInitChat()
|
|
|
|
{
|
|
|
|
var chat = new Chat
|
|
|
|
{
|
|
|
|
Title = $"对话:{_chatDictionary.Count + 1}",
|
2023-10-13 16:24:03 +08:00
|
|
|
Robot = ChatRobotFactory.GetRobot()
|
2023-10-11 14:23:38 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
foreach (string answer in chat.Robot.OnChatStart())
|
|
|
|
{
|
|
|
|
chat.Messages.Add(new ChatMessage
|
|
|
|
{
|
|
|
|
Sender = chat.Robot.RobotName,
|
|
|
|
Left = true,
|
|
|
|
Text = answer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
chat.Selected = true;
|
|
|
|
|
|
|
|
return chat;
|
|
|
|
}
|
2023-10-17 23:32:39 +08:00
|
|
|
|
|
|
|
private List<ChatMessage> GetChatMessages()
|
|
|
|
{
|
|
|
|
return _chatDictionary.TryGetValue(_currentGuid, out Chat? chat) ? chat.Messages : new List<ChatMessage>();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IChatRobot GetChatRobot()
|
|
|
|
{
|
|
|
|
return _chatDictionary.TryGetValue(_currentGuid, out Chat? chat) ? chat.Robot : DefaultRobot;
|
|
|
|
}
|
2023-10-07 18:55:22 +08:00
|
|
|
}
|