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" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Katheryne\Katheryne.csproj" />
</ItemGroup>
</Project>

View File

@ -1,9 +1,13 @@
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;
public bool Selected { get; set; }

View File

@ -1,5 +1,7 @@
@page "/"
@using Frontend.Models
@using Katheryne.Abstractions
@inject IServiceProvider ServiceProvider
<Layout>
@ -16,7 +18,7 @@
<ListItem OnClick="@(() => ChangeChatClicked(context.Guid))">
<ListItemMeta>
<TitleTemplate>
@if (@context.Selected)
@if (context.Selected)
{
<div class="selected-chat-name">
<p style="margin: 5px">@context.Title</p>
@ -38,7 +40,7 @@
</Sider>
<Content>
<ChatZone Messages="@_chatDictionary[_currentGuid].Messages"/>
<ChatZone Messages="@_chatDictionary[_currentGuid].Messages" Robot="@_chatDictionary[_currentGuid].Robot"/>
</Content>
</Layout>
@ -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<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);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddAntDesign();
builder.Services.AddTransient<IChatRobot, DefaultChatRobot>();
WebApplication app = builder.Build();
app.UseStaticFiles();

View File

@ -1,4 +1,5 @@
@using Frontend.Models
@using Katheryne.Abstractions
<div class="content-zone">
<div class="chat-zone">
@ -49,6 +50,9 @@
[Parameter]
public List<ChatMessage> 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;
}
}

View File

@ -11,5 +11,5 @@ public interface IChatRobot
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>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
</ItemGroup>
</Project>