From 4aa89c438524084ea75bb88ba06e4ac90d91d934 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 11 Oct 2023 14:23:38 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E4=BD=BF=E7=94=A8=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E7=AE=A1=E7=90=86IChatRobot=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=BB=98=E8=AE=A4=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frontend/Frontend.csproj | 4 ++ Frontend/Models/Chat.cs | 4 ++ Frontend/Pages/Index.razor | 55 ++++++++++++++-------------- Frontend/Program.cs | 5 +++ Frontend/Shared/ChatZone.razor | 21 +++++++---- Katheryne/Abstractions/IChatRobot.cs | 4 +- Katheryne/DefaultChatRobot.cs | 44 ++++++++++++++++++++++ Katheryne/Katheryne.csproj | 4 ++ 8 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 Katheryne/DefaultChatRobot.cs diff --git a/Frontend/Frontend.csproj b/Frontend/Frontend.csproj index 68916ba..f15c136 100644 --- a/Frontend/Frontend.csproj +++ b/Frontend/Frontend.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/Frontend/Models/Chat.cs b/Frontend/Models/Chat.cs index 155da10..4a04bb7 100644 --- a/Frontend/Models/Chat.cs +++ b/Frontend/Models/Chat.cs @@ -1,8 +1,12 @@ +using Katheryne.Abstractions; + namespace Frontend.Models; public class Chat { public Guid Guid { get; } = Guid.NewGuid(); + + public required IChatRobot Robot { get; init; } public string Title { get; set; } = string.Empty; diff --git a/Frontend/Pages/Index.razor b/Frontend/Pages/Index.razor index c735b7b..e2ee1b5 100644 --- a/Frontend/Pages/Index.razor +++ b/Frontend/Pages/Index.razor @@ -1,5 +1,7 @@ @page "/" @using Frontend.Models +@using Katheryne.Abstractions +@inject IServiceProvider ServiceProvider @@ -16,7 +18,7 @@ - @if (@context.Selected) + @if (context.Selected) {

@context.Title

@@ -38,7 +40,7 @@ - + @@ -49,18 +51,7 @@ protected override void OnInitialized() { - var chat = new Chat - { - Title = $"对话:{_chatDictionary.Count + 1}" - }; - - chat.Messages.Add(new ChatMessage - { - Sender = "凯瑟琳", - Left = true, - Text = "向着星辰和深渊!欢迎来到冒险家协会。" - }); - chat.Selected = true; + Chat chat = GetInitChat(); _chatDictionary.Add(chat.Guid, chat); _currentGuid = chat.Guid; @@ -68,21 +59,9 @@ private void CreateChatClicked() { - var chat = new Chat - { - Title = $"对话:{_chatDictionary.Count + 1}" - }; - - chat.Messages.Add(new ChatMessage - { - Sender = "凯瑟琳", - Left = true, - Text = "向着星辰和深渊!欢迎来到冒险家协会。" - }); + Chat chat = GetInitChat(); _chatDictionary.Add(chat.Guid, chat); - - // 切换到新建的对话 _chatDictionary[_currentGuid].Selected = false; _currentGuid = chat.Guid; _chatDictionary[_currentGuid].Selected = true; @@ -94,4 +73,26 @@ _currentGuid = guid; _chatDictionary[_currentGuid].Selected = true; } + + private Chat GetInitChat() + { + var chat = new Chat + { + Title = $"对话:{_chatDictionary.Count + 1}", + Robot = ServiceProvider.GetRequiredService() + }; + + 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; + } } \ No newline at end of file diff --git a/Frontend/Program.cs b/Frontend/Program.cs index eb5bb91..e2391ad 100644 --- a/Frontend/Program.cs +++ b/Frontend/Program.cs @@ -1,8 +1,13 @@ +using Katheryne; +using Katheryne.Abstractions; + WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); builder.Services.AddAntDesign(); +builder.Services.AddTransient(); + WebApplication app = builder.Build(); app.UseStaticFiles(); diff --git a/Frontend/Shared/ChatZone.razor b/Frontend/Shared/ChatZone.razor index 5d3035c..39cf69f 100644 --- a/Frontend/Shared/ChatZone.razor +++ b/Frontend/Shared/ChatZone.razor @@ -1,4 +1,5 @@ @using Frontend.Models +@using Katheryne.Abstractions
@@ -49,6 +50,9 @@ [Parameter] public List Messages { get; set; } = null!; + [Parameter] + public IChatRobot Robot { get; set; } = null!; + private void SendMessageClicked() { if (string.IsNullOrWhiteSpace(MessageSending)) @@ -63,13 +67,16 @@ Text = MessageSending }); - MessageSending = string.Empty; - - Messages.Add(new ChatMessage + foreach (string answer in Robot.ChatNext(MessageSending)) { - Left = true, - Sender = "凯瑟琳", - Text = "对不起,做不到。" - }); + Messages.Add(new ChatMessage + { + Left = true, + Sender = Robot.RobotName, + Text = answer + }); + } + + MessageSending = string.Empty; } } \ No newline at end of file diff --git a/Katheryne/Abstractions/IChatRobot.cs b/Katheryne/Abstractions/IChatRobot.cs index ea64e2d..48575ad 100644 --- a/Katheryne/Abstractions/IChatRobot.cs +++ b/Katheryne/Abstractions/IChatRobot.cs @@ -10,6 +10,6 @@ public interface IChatRobot public IEnumerable OnChatStart(); public IEnumerable OnChatStop(); - - + + public IEnumerable ChatNext(string input); } \ No newline at end of file diff --git a/Katheryne/DefaultChatRobot.cs b/Katheryne/DefaultChatRobot.cs new file mode 100644 index 0000000..7cac34b --- /dev/null +++ b/Katheryne/DefaultChatRobot.cs @@ -0,0 +1,44 @@ +using Katheryne.Abstractions; +using Microsoft.Extensions.Logging; + +namespace Katheryne; + +public class DefaultChatRobot : IChatRobot +{ + private readonly ILogger _logger; + + public DefaultChatRobot(ILogger logger) + { + _logger = logger; + } + + public string RobotName => "凯瑟琳"; + + public IEnumerable OnChatStart() + { + _logger.LogDebug("Start default chat robot."); + return new[] + { + "向着星辰与深渊!", + "欢迎来到冒险家协会。" + }; + } + + public IEnumerable OnChatStop() + { + _logger.LogDebug("End default chat robot."); + return new[] + { + "再见,感谢您对协会做出的贡献,冒险家。" + }; + } + + public IEnumerable ChatNext(string input) + { + _logger.LogDebug("Robot receive message: \"{}\".", input); + return new[] + { + "暂时不支持该功能,请联系维护人员。" + }; + } +} \ No newline at end of file diff --git a/Katheryne/Katheryne.csproj b/Katheryne/Katheryne.csproj index 6836c68..7a99e3a 100644 --- a/Katheryne/Katheryne.csproj +++ b/Katheryne/Katheryne.csproj @@ -6,4 +6,8 @@ enable + + + +