From 86e44cc871dd6a1a3c88e707483162c55ec4b62a Mon Sep 17 00:00:00 2001 From: FHangH <752972182@qq.com> Date: Thu, 10 Aug 2023 15:20:50 +0800 Subject: [PATCH] 2023-08-10 Fix Packaging issues --- FH_WenXinYiiYan.uplugin | 4 +- .../FH_WenXinYiiYan/FH_WenXinYiiYan.Build.cs | 1 + .../Private/WXYY_AccessToken.cpp | 31 ++++--- Source/FH_WenXinYiiYan/Private/WXYY_Chat.cpp | 93 ++++++++++++++++--- .../FH_WenXinYiiYan/Public/WXYY_AccessToken.h | 5 + Source/FH_WenXinYiiYan/Public/WXYY_Chat.h | 18 ++-- 6 files changed, 118 insertions(+), 34 deletions(-) diff --git a/FH_WenXinYiiYan.uplugin b/FH_WenXinYiiYan.uplugin index ad1280d..2add681 100644 --- a/FH_WenXinYiiYan.uplugin +++ b/FH_WenXinYiiYan.uplugin @@ -1,7 +1,7 @@ { "FileVersion": 3, - "Version": 1, - "VersionName": "1.0", + "Version": 2, + "VersionName": "2.0", "FriendlyName": "FH_WenXinYiiYan", "Description": "WenXinYiYanPlugin", "Category": "FH", diff --git a/Source/FH_WenXinYiiYan/FH_WenXinYiiYan.Build.cs b/Source/FH_WenXinYiiYan/FH_WenXinYiiYan.Build.cs index 6ee20d1..7dc32f0 100644 --- a/Source/FH_WenXinYiiYan/FH_WenXinYiiYan.Build.cs +++ b/Source/FH_WenXinYiiYan/FH_WenXinYiiYan.Build.cs @@ -31,6 +31,7 @@ public FH_WenXinYiiYan(ReadOnlyTargetRules Target) : base(Target) "Core", "HTTP", "Json", + "JsonUtilities", "Engine", "Projects" // ... add other public dependencies that you statically link with here ... diff --git a/Source/FH_WenXinYiiYan/Private/WXYY_AccessToken.cpp b/Source/FH_WenXinYiiYan/Private/WXYY_AccessToken.cpp index ad7be18..9c87e57 100644 --- a/Source/FH_WenXinYiiYan/Private/WXYY_AccessToken.cpp +++ b/Source/FH_WenXinYiiYan/Private/WXYY_AccessToken.cpp @@ -6,9 +6,9 @@ UGetAccessToken* UGetAccessToken::GetAccessToken(FString API, FString Secret) { - const auto RequestToken = NewObject(); - RequestToken->OnHttpsRequestToken(API, Secret); - return RequestToken; + const auto RequestTokenObject = NewObject(); + RequestTokenObject->OnHttpsRequestToken(API, Secret); + return RequestTokenObject; } void UGetAccessToken::OnHttpsRequestToken(const FString& API, const FString& Secret) @@ -20,19 +20,28 @@ void UGetAccessToken::OnHttpsRequestToken(const FString& API, const FString& Sec TEXT("https://aip.baidubce.com/oauth/2.0/token?client_id=%s&client_secret=%s&grant_type=client_credentials"), *API, *Secret); - const auto Request = FHttpModule::Get().CreateRequest(); - Request->SetVerb(TEXT("POST")); - Request->SetURL(URL); - Request->SetHeader(TEXT("Content-Type"), TEXT("application/json")); - Request->SetHeader(TEXT("Accept"), TEXT("application/json")); + RequestToken = FHttpModule::Get().CreateRequest(); + RequestToken->SetVerb(TEXT("POST")); + RequestToken->SetURL(URL); + RequestToken->SetHeader(TEXT("Content-Type"), TEXT("application/json")); + RequestToken->SetHeader(TEXT("Accept"), TEXT("application/json")); - Request->OnProcessRequestComplete().BindUObject(this, &UGetAccessToken::OnDeserializeResponse); + RequestToken->OnProcessRequestComplete().BindUObject(this, &UGetAccessToken::OnDeserializeResponse); - if (Request->ProcessRequest()) + if (RequestToken->ProcessRequest() || IsRequestProcessing()) { UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan Start ======")); UE_LOG(LogWenXin, Warning, TEXT("Request Process")); FHttpModule::Get().GetHttpManager().Tick(0.f); + + if (RequestToken->GetStatus() == EHttpRequestStatus::Failed) + { + OnFail.Broadcast("null"); + UE_LOG(LogWenXin, Warning, TEXT("Request Fail")); + UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); + } + + RemoveFromRoot(); } UE_LOG(LogWenXin, Warning, TEXT("Request Complete")); } @@ -62,6 +71,6 @@ void UGetAccessToken::OnDeserializeResponse(FHttpRequestPtr Request, FHttpRespon UE_LOG(LogWenXin, Warning, TEXT("Request Result = %s"), *Result); UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); } - + RemoveFromRoot(); } diff --git a/Source/FH_WenXinYiiYan/Private/WXYY_Chat.cpp b/Source/FH_WenXinYiiYan/Private/WXYY_Chat.cpp index 7842e88..c26c369 100644 --- a/Source/FH_WenXinYiiYan/Private/WXYY_Chat.cpp +++ b/Source/FH_WenXinYiiYan/Private/WXYY_Chat.cpp @@ -1,5 +1,6 @@ #include "WXYY_Chat.h" #include "HttpManager.h" +#include "HttpModule.h" #include "Interfaces/IHttpResponse.h" // Send Chat Message @@ -7,14 +8,10 @@ USendChatMessage* USendChatMessage::SendChatMessage(FChatMessage ChatMessage) { const auto RequestObject = NewObject(); RequestObject->OnHttpsRequestChatMessage(ChatMessage); + SendChatHandler = RequestObject; return RequestObject; } -void USendChatMessage::CancelRequest() -{ - RequestChat->CancelRequest(); -} - void USendChatMessage::OnHttpsRequestChatMessage(const FChatMessage& ChatMessage) { AddToRoot(); @@ -36,11 +33,21 @@ void USendChatMessage::OnHttpsRequestChatMessage(const FChatMessage& ChatMessage RequestChat->OnProcessRequestComplete().BindUObject(this, &USendChatMessage::OnDeserializeResponse); - if (RequestChat->ProcessRequest()) + if (RequestChat->ProcessRequest() || IsRequestProcessing()) { UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan Start ======")); UE_LOG(LogWenXin, Warning, TEXT("Request Process")); FHttpModule::Get().GetHttpManager().Tick(0.f); + + if (RequestChat->GetStatus() == EHttpRequestStatus::Failed) + { + FChatMessage Message; + Message.Role = ERole::ER_Assistant; + Message.Content = ""; + OnFail.Broadcast(Message); + UE_LOG(LogWenXin, Warning, TEXT("Request Fail")); + UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); + } } UE_LOG(LogWenXin, Warning, TEXT("Request Complete")); } @@ -60,7 +67,16 @@ void USendChatMessage::OnDeserializeResponse(FHttpRequestPtr Request, FHttpRespo UE_LOG(LogWenXin, Warning, TEXT("Deserialize Response")); ChatMessage.Role = ERole::ER_Assistant; ChatMessage.Content = JsonObject->GetStringField("result"); - OnSuccess.Broadcast(ChatMessage); + if (ChatMessage.Content == "") + { + OnFail.Broadcast(ChatMessage); + UE_LOG(LogWenXin, Warning, TEXT("Request Result = %s : null"), *UWXYY::ConverToStr(ChatMessage.Role)); + UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); + + RemoveFromRoot(); + return; + } + OnCompleted.Broadcast(ChatMessage); UE_LOG(LogWenXin, Warning, TEXT("Request Result = %s : %s"), *UWXYY::ConverToStr(ChatMessage.Role), *ChatMessage.Content); UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); } @@ -77,19 +93,31 @@ void USendChatMessage::OnDeserializeResponse(FHttpRequestPtr Request, FHttpRespo RemoveFromRoot(); } +bool USendChatMessage::IsRequestProcessing() +{ + if (!SendChatHandler || !SendChatHandler->RequestChat) return false; + return SendChatHandler->RequestChat->GetStatus() == EHttpRequestStatus::Processing; +} + +void USendChatMessage::CancelMessageRequest() +{ + if (!SendChatHandler || !SendChatHandler->RequestChat) return; + if (SendChatHandler->RequestChat->GetStatus() == EHttpRequestStatus::Processing) + { + SendChatHandler->RequestChat->CancelRequest(); + UE_LOG(LogWenXin, Warning, TEXT("Request Cancle")) + } +} + // Send Chat Message By Stream USendChatMessageByStream* USendChatMessageByStream::SendChatMessageByStream(FChatMessage ChatMessage) { const auto RequestObject = NewObject(); RequestObject->OnHttpsRequestChatMessage(ChatMessage); + SendStreamChatHandler = RequestObject; return RequestObject; } -void USendChatMessageByStream::CancelRequestStream() -{ - RequestChat->CancelRequest(); -} - void USendChatMessageByStream::OnHttpsRequestChatMessage(const FChatMessage& ChatMessage) { AddToRoot(); @@ -116,15 +144,23 @@ void USendChatMessageByStream::OnHttpsRequestChatMessage(const FChatMessage& Cha RequestChat->OnRequestProgress().BindUObject(this, &USendChatMessageByStream::OnUpdateResponse); RequestChat->OnProcessRequestComplete().BindUObject(this, &USendChatMessageByStream::OnCompletedResponse); - if (RequestChat->ProcessRequest()) + if (RequestChat->ProcessRequest() || IsRequestProcessing()) { UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan Start ======")); UE_LOG(LogWenXin, Warning, TEXT("Request Stream Process")); FHttpModule::Get().GetHttpManager().Tick(0.f); + + if (RequestChat->GetStatus() == EHttpRequestStatus::Failed) + { + FChatMessage Message; + Message.Role = ERole::ER_Assistant; + Message.Content = ""; + OnFail.Broadcast(Message); + UE_LOG(LogWenXin, Warning, TEXT("Request Stream Fail")); + UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); + } } UE_LOG(LogWenXin, Warning, TEXT("Request Stream Complete")); - - RemoveFromRoot(); } void USendChatMessageByStream::OnUpdateResponse(FHttpRequestPtr HttpRequest, int32 BytesSent, int32 BytesReceived) @@ -175,6 +211,15 @@ void USendChatMessageByStream::OnCompletedResponse(FHttpRequestPtr Request, FHtt UE_LOG(LogWenXin, Warning, TEXT("Response Stream Success")); ChatMessage.Role = ERole::ER_Assistant; ChatMessage.Content = AllResult; + if (ChatMessage.Content == "") + { + OnFail.Broadcast(ChatMessage); + UE_LOG(LogWenXin, Warning, TEXT("Stream Result = %s : null"), *UWXYY::ConverToStr(ChatMessage.Role)); + UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); + + RemoveFromRoot(); + return; + } OnCompleted.Broadcast(ChatMessage); UE_LOG(LogWenXin, Warning, TEXT("Request Stream Result = %s : %s"), *UWXYY::ConverToStr(ChatMessage.Role), *ChatMessage.Content); UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); @@ -187,4 +232,22 @@ void USendChatMessageByStream::OnCompletedResponse(FHttpRequestPtr Request, FHtt UE_LOG(LogWenXin, Warning, TEXT("Request Stream Result = %s : null"), *UWXYY::ConverToStr(ChatMessage.Role)); UE_LOG(LogWenXin, Warning, TEXT("====== WenXinYiYan End ======")); } + + RemoveFromRoot(); +} + +bool USendChatMessageByStream::IsRequestProcessing() +{ + if (!SendStreamChatHandler || !SendStreamChatHandler->RequestChat) return false; + return SendStreamChatHandler->RequestChat->GetStatus() == EHttpRequestStatus::Processing; +} + +void USendChatMessageByStream::CancelStreamMessageRequest() +{ + if (!SendStreamChatHandler || !SendStreamChatHandler->RequestChat) return; + if (SendStreamChatHandler->RequestChat->GetStatus() == EHttpRequestStatus::Processing) + { + SendStreamChatHandler->RequestChat->CancelRequest(); + UE_LOG(LogWenXin, Warning, TEXT("Request Cancle")) + } } diff --git a/Source/FH_WenXinYiiYan/Public/WXYY_AccessToken.h b/Source/FH_WenXinYiiYan/Public/WXYY_AccessToken.h index f93ff33..d821a46 100644 --- a/Source/FH_WenXinYiiYan/Public/WXYY_AccessToken.h +++ b/Source/FH_WenXinYiiYan/Public/WXYY_AccessToken.h @@ -1,6 +1,7 @@ #pragma once #include "CoreMinimal.h" +#include "HttpModule.h" #include "Interfaces/IHttpRequest.h" #include "Kismet/BlueprintAsyncActionBase.h" #include "WXYY_AccessToken.generated.h" @@ -23,7 +24,11 @@ class FH_WENXINYIIYAN_API UGetAccessToken : public UBlueprintAsyncActionBase static UGetAccessToken* GetAccessToken(FString API, FString Secret); private: + TSharedRef RequestToken = FHttpModule::Get().CreateRequest(); + void OnHttpsRequestToken(const FString& API, const FString& Secret); void OnDeserializeResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, const bool Success); + + FORCEINLINE bool IsRequestProcessing() const {return RequestToken->GetStatus() == EHttpRequestStatus::Processing;} }; \ No newline at end of file diff --git a/Source/FH_WenXinYiiYan/Public/WXYY_Chat.h b/Source/FH_WenXinYiiYan/Public/WXYY_Chat.h index b3a6736..e969025 100644 --- a/Source/FH_WenXinYiiYan/Public/WXYY_Chat.h +++ b/Source/FH_WenXinYiiYan/Public/WXYY_Chat.h @@ -1,7 +1,6 @@ #pragma once #include "CoreMinimal.h" -#include "HttpModule.h" #include "Kismet/BlueprintAsyncActionBase.h" #include "WXYY.h" #include "Interfaces/IHttpRequest.h" @@ -16,7 +15,7 @@ class FH_WENXINYIIYAN_API USendChatMessage : public UBlueprintAsyncActionBase public: UPROPERTY(BlueprintAssignable) - FResponseChatMessage OnSuccess; + FResponseChatMessage OnCompleted; UPROPERTY(BlueprintAssignable) FResponseChatMessage OnFail; @@ -25,14 +24,17 @@ class FH_WENXINYIIYAN_API USendChatMessage : public UBlueprintAsyncActionBase static USendChatMessage* SendChatMessage(FChatMessage ChatMessage); UFUNCTION(BlueprintCallable, Category="FH|WXYY") - static void CancelRequest(); + static void CancelMessageRequest(); private: - static inline TSharedRef RequestChat = FHttpModule::Get().CreateRequest();; + inline static USendChatMessage* SendChatHandler = nullptr; + TSharedPtr RequestChat; void OnHttpsRequestChatMessage(const FChatMessage& ChatMessage); void OnDeserializeResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, const bool Success); + + static bool IsRequestProcessing(); }; UCLASS() @@ -54,13 +56,15 @@ class FH_WENXINYIIYAN_API USendChatMessageByStream : public UBlueprintAsyncActio static USendChatMessageByStream* SendChatMessageByStream(FChatMessage ChatMessage); UFUNCTION(BlueprintCallable, Category="FH|WXYY") - static void CancelRequestStream(); + static void CancelStreamMessageRequest(); private: FString AllResult; FString AllResponse; FString TempResponse; - static inline TSharedRef RequestChat = FHttpModule::Get().CreateRequest();; + + inline static USendChatMessageByStream* SendStreamChatHandler = nullptr; + TSharedPtr RequestChat; void OnHttpsRequestChatMessage(const FChatMessage& ChatMessage); @@ -69,4 +73,6 @@ class FH_WENXINYIIYAN_API USendChatMessageByStream : public UBlueprintAsyncActio void OnDeserializeResponseUpdate(); void OnCompletedResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, const bool Success); + + static bool IsRequestProcessing(); }; \ No newline at end of file