Skip to content

Commit

Permalink
2023-08-10 Fix Packaging issues
Browse files Browse the repository at this point in the history
  • Loading branch information
FHangH committed Aug 10, 2023
1 parent 7a9efe2 commit 86e44cc
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 34 deletions.
4 changes: 2 additions & 2 deletions FH_WenXinYiiYan.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.0",
"Version": 2,
"VersionName": "2.0",
"FriendlyName": "FH_WenXinYiiYan",
"Description": "WenXinYiYanPlugin",
"Category": "FH",
Expand Down
1 change: 1 addition & 0 deletions Source/FH_WenXinYiiYan/FH_WenXinYiiYan.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
31 changes: 20 additions & 11 deletions Source/FH_WenXinYiiYan/Private/WXYY_AccessToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

UGetAccessToken* UGetAccessToken::GetAccessToken(FString API, FString Secret)
{
const auto RequestToken = NewObject<UGetAccessToken>();
RequestToken->OnHttpsRequestToken(API, Secret);
return RequestToken;
const auto RequestTokenObject = NewObject<UGetAccessToken>();
RequestTokenObject->OnHttpsRequestToken(API, Secret);
return RequestTokenObject;
}

void UGetAccessToken::OnHttpsRequestToken(const FString& API, const FString& Secret)
Expand All @@ -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"));
}
Expand Down Expand Up @@ -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();
}
93 changes: 78 additions & 15 deletions Source/FH_WenXinYiiYan/Private/WXYY_Chat.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#include "WXYY_Chat.h"
#include "HttpManager.h"
#include "HttpModule.h"
#include "Interfaces/IHttpResponse.h"

// Send Chat Message
USendChatMessage* USendChatMessage::SendChatMessage(FChatMessage ChatMessage)
{
const auto RequestObject = NewObject<USendChatMessage>();
RequestObject->OnHttpsRequestChatMessage(ChatMessage);
SendChatHandler = RequestObject;
return RequestObject;
}

void USendChatMessage::CancelRequest()
{
RequestChat->CancelRequest();
}

void USendChatMessage::OnHttpsRequestChatMessage(const FChatMessage& ChatMessage)
{
AddToRoot();
Expand All @@ -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"));
}
Expand All @@ -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 ======"));
}
Expand All @@ -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<USendChatMessageByStream>();
RequestObject->OnHttpsRequestChatMessage(ChatMessage);
SendStreamChatHandler = RequestObject;
return RequestObject;
}

void USendChatMessageByStream::CancelRequestStream()
{
RequestChat->CancelRequest();
}

void USendChatMessageByStream::OnHttpsRequestChatMessage(const FChatMessage& ChatMessage)
{
AddToRoot();
Expand All @@ -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)
Expand Down Expand Up @@ -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 ======"));
Expand All @@ -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"))
}
}
5 changes: 5 additions & 0 deletions Source/FH_WenXinYiiYan/Public/WXYY_AccessToken.h
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -23,7 +24,11 @@ class FH_WENXINYIIYAN_API UGetAccessToken : public UBlueprintAsyncActionBase
static UGetAccessToken* GetAccessToken(FString API, FString Secret);

private:
TSharedRef<IHttpRequest, ESPMode::ThreadSafe> 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;}
};
18 changes: 12 additions & 6 deletions Source/FH_WenXinYiiYan/Public/WXYY_Chat.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "CoreMinimal.h"
#include "HttpModule.h"
#include "Kismet/BlueprintAsyncActionBase.h"
#include "WXYY.h"
#include "Interfaces/IHttpRequest.h"
Expand All @@ -16,7 +15,7 @@ class FH_WENXINYIIYAN_API USendChatMessage : public UBlueprintAsyncActionBase

public:
UPROPERTY(BlueprintAssignable)
FResponseChatMessage OnSuccess;
FResponseChatMessage OnCompleted;

UPROPERTY(BlueprintAssignable)
FResponseChatMessage OnFail;
Expand All @@ -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<IHttpRequest, ESPMode::ThreadSafe> RequestChat = FHttpModule::Get().CreateRequest();;
inline static USendChatMessage* SendChatHandler = nullptr;
TSharedPtr<IHttpRequest, ESPMode::ThreadSafe> RequestChat;

void OnHttpsRequestChatMessage(const FChatMessage& ChatMessage);

void OnDeserializeResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, const bool Success);

static bool IsRequestProcessing();
};

UCLASS()
Expand All @@ -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<IHttpRequest, ESPMode::ThreadSafe> RequestChat = FHttpModule::Get().CreateRequest();;

inline static USendChatMessageByStream* SendStreamChatHandler = nullptr;
TSharedPtr<IHttpRequest, ESPMode::ThreadSafe> RequestChat;

void OnHttpsRequestChatMessage(const FChatMessage& ChatMessage);

Expand All @@ -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();
};

0 comments on commit 86e44cc

Please sign in to comment.