add: 使用依赖注入管理IChatRobot

添加默认实现
This commit is contained in:
jackfiled 2023-10-11 14:23:38 +08:00
parent 6a2068e8d1
commit 4aa89c4385
8 changed files with 105 additions and 36 deletions

View File

@ -10,4 +10,8 @@
<PackageReference Include="AntDesign" Version="0.15.5" /> <PackageReference Include="AntDesign" Version="0.15.5" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Katheryne\Katheryne.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,12 @@
using Katheryne.Abstractions;
namespace Frontend.Models; namespace Frontend.Models;
public class Chat public class Chat
{ {
public Guid Guid { get; } = Guid.NewGuid(); public Guid Guid { get; } = Guid.NewGuid();
public required IChatRobot Robot { get; init; }
public string Title { get; set; } = string.Empty; public string Title { get; set; } = string.Empty;

View File

@ -1,5 +1,7 @@
@page "/" @page "/"
@using Frontend.Models @using Frontend.Models
@using Katheryne.Abstractions
@inject IServiceProvider ServiceProvider
<Layout> <Layout>
@ -16,7 +18,7 @@
<ListItem OnClick="@(() => ChangeChatClicked(context.Guid))"> <ListItem OnClick="@(() => ChangeChatClicked(context.Guid))">
<ListItemMeta> <ListItemMeta>
<TitleTemplate> <TitleTemplate>
@if (@context.Selected) @if (context.Selected)
{ {
<div class="selected-chat-name"> <div class="selected-chat-name">
<p style="margin: 5px">@context.Title</p> <p style="margin: 5px">@context.Title</p>
@ -38,7 +40,7 @@
</Sider> </Sider>
<Content> <Content>
<ChatZone Messages="@_chatDictionary[_currentGuid].Messages"/> <ChatZone Messages="@_chatDictionary[_currentGuid].Messages" Robot="@_chatDictionary[_currentGuid].Robot"/>
</Content> </Content>
</Layout> </Layout>
@ -49,18 +51,7 @@
protected override void OnInitialized() protected override void OnInitialized()
{ {
var chat = new Chat Chat chat = GetInitChat();
{
Title = $"对话:{_chatDictionary.Count + 1}"
};
chat.Messages.Add(new ChatMessage
{
Sender = "凯瑟琳",
Left = true,
Text = "向着星辰和深渊!欢迎来到冒险家协会。"
});
chat.Selected = true;
_chatDictionary.Add(chat.Guid, chat); _chatDictionary.Add(chat.Guid, chat);
_currentGuid = chat.Guid; _currentGuid = chat.Guid;
@ -68,21 +59,9 @@
private void CreateChatClicked() private void CreateChatClicked()
{ {
var chat = new Chat Chat chat = GetInitChat();
{
Title = $"对话:{_chatDictionary.Count + 1}"
};
chat.Messages.Add(new ChatMessage
{
Sender = "凯瑟琳",
Left = true,
Text = "向着星辰和深渊!欢迎来到冒险家协会。"
});
_chatDictionary.Add(chat.Guid, chat); _chatDictionary.Add(chat.Guid, chat);
// 切换到新建的对话
_chatDictionary[_currentGuid].Selected = false; _chatDictionary[_currentGuid].Selected = false;
_currentGuid = chat.Guid; _currentGuid = chat.Guid;
_chatDictionary[_currentGuid].Selected = true; _chatDictionary[_currentGuid].Selected = true;
@ -94,4 +73,26 @@
_currentGuid = guid; _currentGuid = guid;
_chatDictionary[_currentGuid].Selected = true; _chatDictionary[_currentGuid].Selected = true;
} }
private Chat GetInitChat()
{
var chat = new Chat
{
Title = $"对话:{_chatDictionary.Count + 1}",
Robot = ServiceProvider.GetRequiredService<IChatRobot>()
};
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;
}
} }

View File

@ -1,8 +1,13 @@
using Katheryne;
using Katheryne.Abstractions;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(); builder.Services.AddServerSideBlazor();
builder.Services.AddAntDesign(); builder.Services.AddAntDesign();
builder.Services.AddTransient<IChatRobot, DefaultChatRobot>();
WebApplication app = builder.Build(); WebApplication app = builder.Build();
app.UseStaticFiles(); app.UseStaticFiles();

View File

@ -1,4 +1,5 @@
@using Frontend.Models @using Frontend.Models
@using Katheryne.Abstractions
<div class="content-zone"> <div class="content-zone">
<div class="chat-zone"> <div class="chat-zone">
@ -49,6 +50,9 @@
[Parameter] [Parameter]
public List<ChatMessage> Messages { get; set; } = null!; public List<ChatMessage> Messages { get; set; } = null!;
[Parameter]
public IChatRobot Robot { get; set; } = null!;
private void SendMessageClicked() private void SendMessageClicked()
{ {
if (string.IsNullOrWhiteSpace(MessageSending)) if (string.IsNullOrWhiteSpace(MessageSending))
@ -63,13 +67,16 @@
Text = MessageSending Text = MessageSending
}); });
MessageSending = string.Empty; foreach (string answer in Robot.ChatNext(MessageSending))
Messages.Add(new ChatMessage
{ {
Left = true, Messages.Add(new ChatMessage
Sender = "凯瑟琳", {
Text = "对不起,做不到。" Left = true,
}); Sender = Robot.RobotName,
Text = answer
});
}
MessageSending = string.Empty;
} }
} }

View File

@ -10,6 +10,6 @@ public interface IChatRobot
public IEnumerable<string> OnChatStart(); public IEnumerable<string> OnChatStart();
public IEnumerable<string> OnChatStop(); public IEnumerable<string> OnChatStop();
public IEnumerable<string> ChatNext(string input);
} }

View File

@ -0,0 +1,44 @@
using Katheryne.Abstractions;
using Microsoft.Extensions.Logging;
namespace Katheryne;
public class DefaultChatRobot : IChatRobot
{
private readonly ILogger<DefaultChatRobot> _logger;
public DefaultChatRobot(ILogger<DefaultChatRobot> logger)
{
_logger = logger;
}
public string RobotName => "凯瑟琳";
public IEnumerable<string> OnChatStart()
{
_logger.LogDebug("Start default chat robot.");
return new[]
{
"向着星辰与深渊!",
"欢迎来到冒险家协会。"
};
}
public IEnumerable<string> OnChatStop()
{
_logger.LogDebug("End default chat robot.");
return new[]
{
"再见,感谢您对协会做出的贡献,冒险家。"
};
}
public IEnumerable<string> ChatNext(string input)
{
_logger.LogDebug("Robot receive message: \"{}\".", input);
return new[]
{
"暂时不支持该功能,请联系维护人员。"
};
}
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
</ItemGroup>
</Project> </Project>