From 7ce8812098e0ba50c67af9c88f776e51f186b5b4 Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Mon, 18 Aug 2025 01:05:04 +0330 Subject: [PATCH] ... --- Hushian.Application/Services/AIService.cs | 2 +- .../Controllers/v1/ConversationController.cs | 2 +- .../Pages/FromUserSide/AIChat.razor | 237 ++++++++++++++++++ .../Service/ConversationService.cs | 20 ++ 4 files changed, 259 insertions(+), 2 deletions(-) create mode 100644 Presentation/HushianWebApp/Pages/FromUserSide/AIChat.razor diff --git a/Hushian.Application/Services/AIService.cs b/Hushian.Application/Services/AIService.cs index 652f74e..46980d5 100644 --- a/Hushian.Application/Services/AIService.cs +++ b/Hushian.Application/Services/AIService.cs @@ -26,7 +26,7 @@ namespace Hushian.Application.Services try { string responeai="همین جوری"; - bool sucessresponseai = false; + bool sucessresponseai =!false; var entity = new AIA diff --git a/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs b/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs index 2f580f7..704f3ed 100644 --- a/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs +++ b/Presentation/Hushian.WebApi/Controllers/v1/ConversationController.cs @@ -174,7 +174,7 @@ namespace Hushian.WebApi.Controllers.v1 string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First(); dto.userId = Convert.ToInt32(strUserID); var response = await _aIService.NewRequest(dto); - return response.Success ? Ok(response):BadRequest(response.Errors); + return response.Success ? Ok(response.Value):BadRequest(response.Errors); } [HttpGet("ai/CurrentResponse/{companyID}")] [Authorize(Roles = "User")] diff --git a/Presentation/HushianWebApp/Pages/FromUserSide/AIChat.razor b/Presentation/HushianWebApp/Pages/FromUserSide/AIChat.razor new file mode 100644 index 0000000..ec637f9 --- /dev/null +++ b/Presentation/HushianWebApp/Pages/FromUserSide/AIChat.razor @@ -0,0 +1,237 @@ +@page "/AIChat/{CompanyID:int}" +@using Common.Dtos +@inject ConversationService conversationService +@inject ILocalStorageService localStorageService +@inject AuthService authService +@inject BaseController baseController +@inject ToastService toastService +@inject IJSRuntime JS +@layout UserPanelLayout +@using HushianWebApp.Service +@using HushianWebApp.Services +@using Microsoft.AspNetCore.Components.Web + +
+
+ @if (isReady) + { + @if (isLogin) + { +
+
+

گفتگو هوش مصنوعی

+
+ +
+
+
+ @if (messages is not null) + { +
+ @foreach (var msg in messages) + { +
+
+ @msg.requestText +
+
+ +
+
+ @msg.responseText +
+
+ + } +
+ } +
+
+
+ + +
+
+
+ } + else + { +
+
+ +

نیاز به ورود دارید

+
+
+ } + } + else + { +
+
+ +

در حال بارگذاری ...

+
+
+ } +
+
+ +@code { + [Parameter] public int CompanyID { get; set; } + List messages = new(); + string inputText = string.Empty; + bool isLogin = false; + bool isReady = false; + + protected override async Task OnInitializedAsync() + { + await EnsureAuth(); + if (isLogin) + { + await LoadMessages(); + } + } + + private async Task EnsureAuth() + { + var token = await localStorageService.GetItem("U/key"); + if (string.IsNullOrEmpty(token)) + { + isLogin = false; + isReady = true; + return; + } + await baseController.RemoveToken(); + await baseController.SetToken(token); + isLogin = await authService.IsOnline(); + isReady = true; + } + + private async Task LoadMessages() + { + messages = await conversationService.GetAiCurrentResponses(CompanyID); + StateHasChanged(); + await JS.InvokeVoidAsync("scrollToBottom", "ai-chat"); + } + + private async Task Send() + { + if (string.IsNullOrWhiteSpace(inputText)) return; + var token = await localStorageService.GetItem("U/key"); + if (!isLogin || string.IsNullOrEmpty(token)) + { + toastService.Notify(new ToastMessage(ToastType.Warning, "ابتدا وارد شوید")); + return; + } + var dto = new aiNewResponseDto { companyId = CompanyID, userId = 0, requestText = inputText }; + var okmodel = await conversationService.AiNewResponse(dto); + if (okmodel!=null) + { + messages.Add(okmodel); + inputText = string.Empty; + StateHasChanged(); + await JS.InvokeVoidAsync("scrollToBottom", "ai-chat"); + } + else + { + toastService.Notify(new ToastMessage(ToastType.Danger, "ارسال ناموفق بود")); + } + } + + private async Task HandleKeyDown(KeyboardEventArgs e) + { + if (e.Key == "Enter") await Send(); + } + + private async Task Logout() + { + await baseController.RemoveToken(); + await localStorageService.RemoveItem("U/key"); + isLogin = false; + } +} + + + + + + \ No newline at end of file diff --git a/Presentation/HushianWebApp/Service/ConversationService.cs b/Presentation/HushianWebApp/Service/ConversationService.cs index 5c642b4..caab9d4 100644 --- a/Presentation/HushianWebApp/Service/ConversationService.cs +++ b/Presentation/HushianWebApp/Service/ConversationService.cs @@ -1,4 +1,5 @@ using Common.Dtos.Conversation; +using Common.Dtos; using Common.Enums; using System.ComponentModel.Design; using System.Net.Http.Json; @@ -95,5 +96,24 @@ namespace HushianWebApp.Service return null; } + + // AI conversation endpoints + public async Task> GetAiCurrentResponses(int companyId) + { + var response = await _baseController.Get($"{BaseRoute}ai/CurrentResponse/{companyId}"); + if (response.IsSuccessStatusCode) + return await response.Content.ReadFromJsonAsync>(); + + return new(); + } + + public async Task AiNewResponse(aiNewResponseDto dto) + { + var response = await _baseController.Post($"{BaseRoute}ai/NewResponse", dto); + if (response.IsSuccessStatusCode) + return await response.Content.ReadFromJsonAsync(); + + return null; + } } }