diff --git a/src/AntSK.Domain/AntSK.Domain.xml b/src/AntSK.Domain/AntSK.Domain.xml index 228bdba..362ee1c 100644 --- a/src/AntSK.Domain/AntSK.Domain.xml +++ b/src/AntSK.Domain/AntSK.Domain.xml @@ -157,11 +157,6 @@ 模型类型 - - - 发送是true 接收是false - - 当前页,从1开始 @@ -407,6 +402,36 @@ 回答最大token数 + + + 用户名 + + + + + 应用ID + + + + + 消息内容 + + + + + 发送是true 接收是false + + + + + 创建事件 + + + + + 文件名 + + 接口描述 diff --git a/src/AntSK.Domain/Domain/Interface/IChatService.cs b/src/AntSK.Domain/Domain/Interface/IChatService.cs index 6541da0..f7be889 100644 --- a/src/AntSK.Domain/Domain/Interface/IChatService.cs +++ b/src/AntSK.Domain/Domain/Interface/IChatService.cs @@ -18,6 +18,6 @@ namespace AntSK.Domain.Domain.Interface IAsyncEnumerable SendKmsByAppAsync(Apps app, string questions, ChatHistory history, string filePath, List relevantSources = null); Task SendImgByAppAsync(Apps app, string questions); - Task GetChatHistory(List MessageList); + Task GetChatHistory(List MessageList); } } \ No newline at end of file diff --git a/src/AntSK.Domain/Domain/Model/MessageInfo.cs b/src/AntSK.Domain/Domain/Model/MessageInfo.cs deleted file mode 100644 index 2aa8c8c..0000000 --- a/src/AntSK.Domain/Domain/Model/MessageInfo.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace AntSK.Domain.Domain.Model -{ - public class MessageInfo - { - public string ID { get; set; } = ""; - public string Context { get; set; } = ""; - - /// - /// 发送是true 接收是false - /// - public bool IsSend { get; set; } = false; - - public DateTime CreateTime { get; set; } - - public string? FileName { get; set; } - } -} \ No newline at end of file diff --git a/src/AntSK.Domain/Domain/Service/ChatService.cs b/src/AntSK.Domain/Domain/Service/ChatService.cs index a9bf86e..c6d2d49 100644 --- a/src/AntSK.Domain/Domain/Service/ChatService.cs +++ b/src/AntSK.Domain/Domain/Service/ChatService.cs @@ -297,7 +297,7 @@ namespace AntSK.Domain.Domain.Service } } - public async Task GetChatHistory(List MessageList) + public async Task GetChatHistory(List MessageList) { ChatHistory history = new ChatHistory(); if (MessageList.Count > 1) diff --git a/src/AntSK.Domain/Repositories/AI/Chat/Chats.cs b/src/AntSK.Domain/Repositories/AI/Chat/Chats.cs new file mode 100644 index 0000000..1c783e1 --- /dev/null +++ b/src/AntSK.Domain/Repositories/AI/Chat/Chats.cs @@ -0,0 +1,40 @@ +using AntSK.Domain.Domain.Model.Enum; +using SqlSugar; +using System.ComponentModel.DataAnnotations; + +namespace AntSK.Domain.Repositories +{ + [SugarTable("Chats")] + public partial class Chats + { + [SugarColumn(IsPrimaryKey = true)] + public string Id { get; set; } + + /// + /// 用户名 + /// + public string UserName { get; set; } + /// + /// 应用ID + /// + public string AppId { get; set; } + /// + /// 消息内容 + /// + public string Context { get; set; } = ""; + + /// + /// 发送是true 接收是false + /// + public bool IsSend { get; set; } = false; + /// + /// 创建事件 + /// + public DateTime CreateTime { get; set; } + + /// + /// 文件名 + /// + public string? FileName { get; set; } + } +} diff --git a/src/AntSK.Domain/Repositories/AI/Chat/Chats_Repositories.cs b/src/AntSK.Domain/Repositories/AI/Chat/Chats_Repositories.cs new file mode 100644 index 0000000..b51796b --- /dev/null +++ b/src/AntSK.Domain/Repositories/AI/Chat/Chats_Repositories.cs @@ -0,0 +1,11 @@ + +using AntSK.Domain.Common.DependencyInjection; +using AntSK.Domain.Repositories.Base; + +namespace AntSK.Domain.Repositories +{ + [ServiceDescription(typeof(IChats_Repositories), ServiceLifetime.Scoped)] + public class Chats_Repositories : Repository, IChats_Repositories + { + } +} diff --git a/src/AntSK.Domain/Repositories/AI/Chat/IChats_Repositories.cs b/src/AntSK.Domain/Repositories/AI/Chat/IChats_Repositories.cs new file mode 100644 index 0000000..7d06084 --- /dev/null +++ b/src/AntSK.Domain/Repositories/AI/Chat/IChats_Repositories.cs @@ -0,0 +1,8 @@ +using AntSK.Domain.Repositories.Base; + +namespace AntSK.Domain.Repositories +{ + public interface IChats_Repositories : IRepository + { + } +} diff --git a/src/AntSK/Pages/ChatPage/Components/ChatView.razor.cs b/src/AntSK/Pages/ChatPage/Components/ChatView.razor.cs index c924b1c..8e6babd 100644 --- a/src/AntSK/Pages/ChatPage/Components/ChatView.razor.cs +++ b/src/AntSK/Pages/ChatPage/Components/ChatView.razor.cs @@ -6,14 +6,17 @@ using AntSK.Domain.Domain.Model.Enum; using AntSK.Domain.Repositories; using AntSK.Domain.Utils; using AntSK.LLM.StableDiffusion; +using AntSK.Models; using Blazored.LocalStorage; using Markdig; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage; using Microsoft.JSInterop; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.ChatCompletion; using Microsoft.SemanticKernel.Connectors.OpenAI; using Newtonsoft.Json; +using System.Collections.Generic; namespace AntSK.Pages.ChatPage.Components { @@ -36,8 +39,10 @@ namespace AntSK.Pages.ChatPage.Components [Inject] IChatService _chatService { get; set; } [Inject] IJSRuntime _JSRuntime { get; set; } [Inject] ILocalStorageService _localStorage { get; set; } + [Inject] IChats_Repositories _chats_Repositories { get; set; } + [Inject] ProtectedSessionStorage _protectedSessionStore { get; set; } - protected List MessageList = []; + protected List MessageList = []; protected string? _messageInput; protected string _json = ""; protected bool Sendding = false; @@ -48,25 +53,85 @@ namespace AntSK.Pages.ChatPage.Components private List _relevantSources = new List(); + private string _userName { get; set; } + protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); - LoadData(); - var msgs = await _localStorage.GetItemAsync>("msgs"); + await LoadData(); + + } + + protected override async Task OnParametersSetAsync() + { + await LoadData(); + } + + /// + /// 初始化加载数据 + /// + /// + private async Task LoadData() + { + app = _apps_Repositories.GetFirst(p => p.Id == AppId); + var userSessionStorageResult = await _protectedSessionStore.GetAsync("UserSession"); + var userSession = userSessionStorageResult.Success ? userSessionStorageResult.Value : null; + _userName = userSession?.UserName; + await GetMsgList(); + } + /// + /// 获取聊天记录列表 + /// + /// + private async Task GetMsgList() + { + MessageList.Clear(); + List msgs = new List(); + if (string.IsNullOrEmpty(_userName)) + { + //匿名访问使用localstore + msgs = await _localStorage.GetItemAsync>($"msgs:{AppId}"); + } + else + { + msgs = await _chats_Repositories.GetListAsync(p => p.AppId == AppId && p.UserName == _userName); + } if (msgs != null && msgs.Count > 0) { MessageList = msgs; } } - protected override async Task OnParametersSetAsync() + /// + /// 清空聊天记录列表 + /// + /// + private async Task ClearMsgList() { - LoadData(); + MessageList.Clear(); + if (string.IsNullOrEmpty(_userName)) + { + await _localStorage.SetItemAsync>($"msgs:{AppId}", MessageList); + } + else + { + await _chats_Repositories.DeleteAsync(p => p.AppId == AppId && p.UserName == _userName); + } } - - private void LoadData() + /// + /// 保存聊天记录 + /// + /// + private async Task SaveMsg() { - app = _apps_Repositories.GetFirst(p => p.Id == AppId); + if (string.IsNullOrEmpty(_userName)) + { + await _localStorage.SetItemAsync>($"msgs:{AppId}", MessageList); + } + else + { + await _chats_Repositories.InsertAsync(MessageList.LastOrDefault()); + } } protected async Task OnClearAsync() @@ -78,11 +143,11 @@ namespace AntSK.Pages.ChatPage.Components var result = await _confirmService.Show(content, title, ConfirmButtons.YesNo); if (result == ConfirmResult.Yes) { - MessageList.Clear(); - await _localStorage.SetItemAsync>("msgs", MessageList); + + await ClearMsgList(); await InvokeAsync(StateHasChanged); _ = Message.Info("清理成功"); - + } } else @@ -102,18 +167,25 @@ namespace AntSK.Pages.ChatPage.Components var filePath = fileList.FirstOrDefault()?.Url; var fileName = fileList.FirstOrDefault()?.FileName; - MessageList.Add(new MessageInfo() + var chat = new Chats() { - ID = Guid.NewGuid().ToString(), + Id = Guid.NewGuid().ToString(), + UserName = _userName, + AppId = AppId, Context = _messageInput, CreateTime = DateTime.Now, IsSend = true - }); - + }; + MessageList.Add(chat); + if (string.IsNullOrEmpty(_userName)) + { + _chats_Repositories.InsertAsync(chat); + } + Sendding = true; - await SendAsync(_messageInput,filePath); + await SendAsync(_messageInput, filePath); _messageInput = ""; - Sendding = false; + Sendding = false; } catch (System.Exception ex) { @@ -123,7 +195,9 @@ namespace AntSK.Pages.ChatPage.Components } } - protected async Task OnCopyAsync(MessageInfo item) + + + protected async Task OnCopyAsync(Chats item) { await Task.Run(() => { @@ -135,10 +209,16 @@ namespace AntSK.Pages.ChatPage.Components { await Task.Run(() => { - MessageList = MessageList.Where(w => w.ID != id).ToList(); + MessageList = MessageList.Where(w => w.Id != id).ToList(); }); } + /// + /// 开始发送消息 + /// + /// + /// + /// protected async Task SendAsync(string questions, string? filePath) { ChatHistory history = new ChatHistory(); @@ -166,17 +246,25 @@ namespace AntSK.Pages.ChatPage.Components //缓存消息记录 if (app.Type != AppType.img.ToString()) { - await _localStorage.SetItemAsync>("msgs", MessageList); + await SaveMsg(); } return await Task.FromResult(true); } + /// + /// 发送图片对话 + /// + /// + /// + /// private async Task SendImg(string questions,Apps app) { - MessageInfo info = new MessageInfo(); - info.ID = Guid.NewGuid().ToString(); + Chats info = new Chats(); + info.Id = Guid.NewGuid().ToString(); + info.UserName=_userName; + info.AppId=AppId; info.CreateTime = DateTime.Now; var base64= await _chatService.SendImgByAppAsync(app, questions); if (string.IsNullOrEmpty(base64)) @@ -199,14 +287,16 @@ namespace AntSK.Pages.ChatPage.Components /// private async Task SendKms(string questions, ChatHistory history, Apps app, string? filePath) { - MessageInfo info = null; + Chats info = null; var chatResult = _chatService.SendKmsByAppAsync(app, questions, history, filePath, _relevantSources); await foreach (var content in chatResult) { if (info == null) { - info = new MessageInfo(); - info.ID = Guid.NewGuid().ToString(); + info = new Chats(); + info.Id = Guid.NewGuid().ToString(); + info.UserName = _userName; + info.AppId = AppId; info.Context = content.ConvertToString(); info.CreateTime = DateTime.Now; @@ -233,14 +323,16 @@ namespace AntSK.Pages.ChatPage.Components /// private async Task SendChat(string questions, ChatHistory history, Apps app) { - MessageInfo info = null; + Chats info = null; var chatResult = _chatService.SendChatByAppAsync(app, questions, history); await foreach (var content in chatResult) { if (info == null) { - info = new MessageInfo(); - info.ID = Guid.NewGuid().ToString(); + info = new Chats(); + info.Id = Guid.NewGuid().ToString(); + info.UserName = _userName; + info.AppId = AppId; info.Context = content.ConvertToString(); info.CreateTime = DateTime.Now; @@ -257,7 +349,12 @@ namespace AntSK.Pages.ChatPage.Components await MarkDown(info); } - private async Task MarkDown(MessageInfo info) + /// + /// 处理markdown + /// + /// + /// + private async Task MarkDown(Chats info) { if (info.IsNotNull()) { @@ -270,6 +367,10 @@ namespace AntSK.Pages.ChatPage.Components await _JSRuntime.ScrollToBottomAsync("scrollDiv"); } + /// + /// 上传文件事件 + /// + /// private void OnSingleCompleted(UploadInfo fileInfo) { fileList.Add(new() @@ -281,6 +382,11 @@ namespace AntSK.Pages.ChatPage.Components }); _kMService.OnSingleCompleted(fileInfo); } + /// + /// 移除文件事件 + /// + /// + /// private async Task HandleFileRemove(UploadFileItem file) { fileList.RemoveAll(x => x.FileName == file.FileName);