119 lines
3.6 KiB
Plaintext
119 lines
3.6 KiB
Plaintext
@page "/"
|
|
@using Frontend.Models
|
|
@using Frontend.Services
|
|
@using Katheryne
|
|
@using Katheryne.Abstractions
|
|
@inject IChatRobotFactory ChatRobotFactory
|
|
@inject GrammarStorageService GrammarStorage
|
|
@inject DefaultChatRobot DefaultRobot
|
|
|
|
|
|
<Layout>
|
|
<Sider Width="200">
|
|
<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>
|
|
@if (context.Selected)
|
|
{
|
|
<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>
|
|
</Sider>
|
|
|
|
<Content>
|
|
<ChatZone Messages="@GetChatMessages()" Robot="@GetChatRobot()"/>
|
|
</Content>
|
|
</Layout>
|
|
|
|
@code {
|
|
private readonly Dictionary<Guid, Chat> _chatDictionary = new();
|
|
|
|
private Guid _currentGuid;
|
|
|
|
protected override async Task OnAfterRenderAsync(bool firstRender)
|
|
{
|
|
if (firstRender)
|
|
{
|
|
await GrammarStorage.RestoreGrammar();
|
|
|
|
Chat chat = GetInitChat();
|
|
_chatDictionary.Add(chat.Guid, chat);
|
|
_currentGuid = chat.Guid;
|
|
StateHasChanged();
|
|
}
|
|
|
|
await base.OnInitializedAsync();
|
|
}
|
|
|
|
private void CreateChatClicked()
|
|
{
|
|
Chat chat = GetInitChat();
|
|
|
|
_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;
|
|
}
|
|
|
|
private Chat GetInitChat()
|
|
{
|
|
var chat = new Chat
|
|
{
|
|
Title = $"对话:{_chatDictionary.Count + 1}",
|
|
Robot = ChatRobotFactory.GetRobot()
|
|
};
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
} |