Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Hotfix v1.4.2: Fix HttpGPT Chat Scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
lucoiso committed Mar 21, 2023
1 parent b9c6a55 commit 21391a0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 99 deletions.
4 changes: 2 additions & 2 deletions HttpGPT.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 6,
"VersionName": "1.4.1",
"Version": 7,
"VersionName": "1.4.2",
"EngineVersion": "5.1.0",
"FriendlyName": "HttpGPT - ChatGPT integrated in the Engine",
"Description": "HttpGPT is an Unreal Engine plugin that facilitates integration with Chat GPT through asynchronous REST requests, making it easy for developers to communicate with the chatbot. HttpGPT also includes a new Editor Tool to integrate Chat GPT directly in the Engine.",
Expand Down
157 changes: 80 additions & 77 deletions Source/HttpGPTEditor/Private/SHttpGPTChatView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "SHttpGPTChatView.h"
#include <HttpGPTHelper.h>
#include <Interfaces/IPluginManager.h>
#include <Widgets/Layout/SScrollBox.h>

#ifdef UE_INLINE_GENERATED_CPP_BY_NAME
#include UE_INLINE_GENERATED_CPP_BY_NAME(SHttpGPTChatView)
Expand Down Expand Up @@ -43,8 +44,61 @@ void UHttpGPTMessagingHandler::ResponseReceived(const FHttpGPTResponse& Response
}
}

void SHttpGPTChatItem::Construct(const FArguments& InArgs)
{
MessagingHandlerObject = NewObject<UHttpGPTMessagingHandler>();
MessagingHandlerObject->Message = FHttpGPTMessage(InArgs._MessageRole, InArgs._InputText);

#if ENGINE_MAJOR_VERSION < 5
using FAppStyle = FEditorStyle;
#endif

const ISlateStyle& AppStyle = FAppStyle::Get();

ChildSlot
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.Padding(MessagingHandlerObject->Message.Role == EHttpGPTRole::User ? FMargin(Slot_Padding * 16.f, Slot_Padding, Slot_Padding, Slot_Padding) : FMargin(Slot_Padding, Slot_Padding, Slot_Padding * 16.f, Slot_Padding))
[
SNew(SBorder)
.BorderImage(AppStyle.GetBrush("Menu.Background"))
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.Padding(Slot_Padding)
.AutoHeight()
[
SNew(STextBlock)
.Font(FCoreStyle::GetDefaultFontStyle("Bold", 10))
.Text(FText::FromString(MessagingHandlerObject->Message.Role == EHttpGPTRole::User ? "User:" : "Assistant:"))
]
+ SVerticalBox::Slot()
.Padding(FMargin(Slot_Padding * 4, Slot_Padding, Slot_Padding, Slot_Padding))
.FillHeight(1.f)
[
SAssignNew(MessageBox, STextBlock)
.AutoWrapText(true)
.Text(this, &SHttpGPTChatItem::GetMessageText)
]
]
]
];
}

FText SHttpGPTChatItem::GetMessageText() const
{
return FText::FromString(MessagingHandlerObject->Message.Content);
}

void SHttpGPTChatView::Construct([[maybe_unused]] const FArguments&)
{
#if ENGINE_MAJOR_VERSION < 5
using FAppStyle = FEditorStyle;
#endif

const ISlateStyle& AppStyle = FAppStyle::Get();

ModelsComboBox = SNew(STextComboBox)
.OptionsSource(&AvailableModels)
.ToolTipText(FText::FromString("Selected GPT Model"));
Expand All @@ -58,10 +112,15 @@ void SHttpGPTChatView::Construct([[maybe_unused]] const FArguments&)
.Padding(Slot_Padding)
.FillHeight(1.f)
[
SAssignNew(ListView, SListView<UHttpGPTMessagingHandlerPtr>)
.ListItemsSource(&ListItems)
.SelectionMode(ESelectionMode::None)
.OnGenerateRow(this, &SHttpGPTChatView::OnGenerateRow)
SNew(SBorder)
.BorderImage(AppStyle.GetBrush("NoBorder"))
[
SNew(SScrollBox)
+ SScrollBox::Slot()
[
SAssignNew(ChatBox, SVerticalBox)
]
]
]
+ SVerticalBox::Slot()
.Padding(Slot_Padding)
Expand Down Expand Up @@ -104,45 +163,37 @@ void SHttpGPTChatView::Construct([[maybe_unused]] const FArguments&)
];
}

TSharedRef<ITableRow> SHttpGPTChatView::OnGenerateRow(UHttpGPTMessagingHandlerPtr Item, const TSharedRef<STableViewBase>& OwnerTable)
{
return SNew(SHttpGPTChatItem, OwnerTable, Item);
}

FReply SHttpGPTChatView::HandleSendMessageButton()
{
UHttpGPTMessagingHandlerPtr UserMessage = NewObject<UHttpGPTMessagingHandler>();
UserMessage->Message = FHttpGPTMessage(EHttpGPTRole::User, InputTextBox->GetText().ToString());

ListItems.Add(UserMessage);
SHttpGPTChatItemPtr UserMessage = SNew(SHttpGPTChatItem).MessageRole(EHttpGPTRole::User).InputText(InputTextBox->GetText().ToString());
ChatBox->AddSlot().AutoHeight() [ UserMessage.ToSharedRef() ];
ChatItems.Add(UserMessage);

UHttpGPTMessagingHandlerPtr NewMessage = NewObject<UHttpGPTMessagingHandler>();
NewMessage->Message.Role = EHttpGPTRole::Assistant;
SHttpGPTChatItemPtr AssistantMessage = SNew(SHttpGPTChatItem).MessageRole(EHttpGPTRole::Assistant);

FHttpGPTOptions Options;
Options.Model = UHttpGPTHelper::NameToModel(*(*ModelsComboBox->GetSelectedItem().Get()));
Options.bStream = true;

RequestReference = UHttpGPTRequest::SendMessages_CustomOptions(GEditor->GetEditorWorldContext().World(), GetChatHistory(), Options);

RequestReference->ProgressStarted.AddDynamic(NewMessage, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->ProgressUpdated.AddDynamic(NewMessage, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->ProcessCompleted.AddDynamic(NewMessage, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->ErrorReceived.AddDynamic(NewMessage, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->RequestFailed.AddDynamic(NewMessage, &UHttpGPTMessagingHandler::RequestFailed);
RequestReference->RequestSent.AddDynamic(NewMessage, &UHttpGPTMessagingHandler::RequestSent);
RequestReference->ProgressStarted.AddDynamic(AssistantMessage->MessagingHandlerObject, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->ProgressUpdated.AddDynamic(AssistantMessage->MessagingHandlerObject, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->ProcessCompleted.AddDynamic(AssistantMessage->MessagingHandlerObject, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->ErrorReceived.AddDynamic(AssistantMessage->MessagingHandlerObject, &UHttpGPTMessagingHandler::ResponseReceived);
RequestReference->RequestFailed.AddDynamic(AssistantMessage->MessagingHandlerObject, &UHttpGPTMessagingHandler::RequestFailed);
RequestReference->RequestSent.AddDynamic(AssistantMessage->MessagingHandlerObject, &UHttpGPTMessagingHandler::RequestSent);

RequestReference->Activate();

if (RequestReference->IsTaskActive())
{
ListItems.Add(NewMessage);
ChatBox->AddSlot().AutoHeight() [AssistantMessage.ToSharedRef()];
ChatItems.Add(AssistantMessage);
}

InputTextBox->SetText(FText::GetEmpty());

ListView->RequestListRefresh();

return FReply::Handled();
}

Expand All @@ -153,9 +204,8 @@ bool SHttpGPTChatView::IsSendMessageEnabled() const

FReply SHttpGPTChatView::HandleClearChatButton()
{
ListItems.Empty();

ListView->RequestListRefresh();
ChatItems.Empty();
ChatBox->ClearChildren();

if (RequestReference)
{
Expand All @@ -167,7 +217,7 @@ FReply SHttpGPTChatView::HandleClearChatButton()

bool SHttpGPTChatView::IsClearChatEnabled() const
{
return !ListItems.IsEmpty();
return !ChatItems.IsEmpty();
}

TArray<FHttpGPTMessage> SHttpGPTChatView::GetChatHistory() const
Expand All @@ -177,9 +227,9 @@ TArray<FHttpGPTMessage> SHttpGPTChatView::GetChatHistory() const
FHttpGPTMessage(EHttpGPTRole::System, GetSystemContext())
};

for (const auto& Item : ListItems)
for (const auto& Item : ChatItems)
{
Output.Add(Item->Message);
Output.Add(Item->MessagingHandlerObject->Message);
}

return Output;
Expand Down Expand Up @@ -213,50 +263,3 @@ void SHttpGPTChatView::InitializeModelsOptions()
}
}
}

void SHttpGPTChatItem::Construct(const FArguments& InArgs, const TSharedRef<STableViewBase>& InOwnerTableView, const UHttpGPTMessagingHandlerPtr InMessagingHandlerObject)
{
#if ENGINE_MAJOR_VERSION < 5
using FAppStyle = FEditorStyle;
#endif

const ISlateStyle& AppStyle = FAppStyle::Get();

MessagingHandlerObject = InMessagingHandlerObject;

STableRow<UHttpGPTMessagingHandlerPtr>::Construct(STableRow<UHttpGPTMessagingHandlerPtr>::FArguments(), InOwnerTableView);

ChildSlot
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.Padding(MessagingHandlerObject->Message.Role == EHttpGPTRole::User ? FMargin(Slot_Padding * 16.f, Slot_Padding, Slot_Padding, Slot_Padding) : FMargin(Slot_Padding, Slot_Padding, Slot_Padding * 16.f, Slot_Padding))
[
SNew(SBorder)
.BorderImage(AppStyle.GetBrush("ToolPanel.GroupBorder"))
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.Padding(Slot_Padding)
.AutoHeight()
[
SNew(STextBlock)
.Text(FText::FromString(MessagingHandlerObject->Message.Role == EHttpGPTRole::User ? "User:" : "Assistant:"))
]
+ SVerticalBox::Slot()
.Padding(FMargin(Slot_Padding * 4, Slot_Padding, Slot_Padding, Slot_Padding))
.FillHeight(1.f)
[
SAssignNew(MessageBox, STextBlock)
.AutoWrapText(true)
.Text(this, &SHttpGPTChatItem::GetMessageText)
]
]
]
];
}

FText SHttpGPTChatItem::GetMessageText() const
{
return FText::FromString(MessagingHandlerObject->Message.Content);
}
44 changes: 24 additions & 20 deletions Source/HttpGPTEditor/Private/SHttpGPTChatView.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <CoreMinimal.h>
#include <HttpGPTTypes.h>
#include <Widgets/Views/SListView.h>
#include <Widgets/Input/SEditableTextBox.h>
#include <Widgets/Input/STextComboBox.h>
#include "SHttpGPTChatView.generated.h"
Expand All @@ -33,6 +32,28 @@ class UHttpGPTMessagingHandler : public UObject

typedef UHttpGPTMessagingHandler* UHttpGPTMessagingHandlerPtr;

class SHttpGPTChatItem final : public SCompoundWidget
{
public:
SLATE_BEGIN_ARGS(SHttpGPTChatItem) : _MessageRole(), _InputText()
{
}
SLATE_ARGUMENT(EHttpGPTRole, MessageRole)
SLATE_ARGUMENT(FString, InputText)
SLATE_END_ARGS()

void Construct(const FArguments& InArgs);

FText GetMessageText() const;

UHttpGPTMessagingHandlerPtr MessagingHandlerObject;

private:
TSharedPtr<STextBlock, ESPMode::ThreadSafe> MessageBox;
};

typedef TSharedPtr<SHttpGPTChatItem, ESPMode::ThreadSafe> SHttpGPTChatItemPtr;

class SHttpGPTChatView final : public SCompoundWidget
{
public:
Expand All @@ -43,8 +64,6 @@ class SHttpGPTChatView final : public SCompoundWidget

void Construct(const FArguments& InArgs);

TSharedRef<ITableRow> OnGenerateRow(UHttpGPTMessagingHandlerPtr Item, const TSharedRef<STableViewBase>& OwnerTable);

FReply HandleSendMessageButton();
bool IsSendMessageEnabled() const;

Expand All @@ -58,8 +77,8 @@ class SHttpGPTChatView final : public SCompoundWidget
void InitializeModelsOptions();

private:
TArray<UHttpGPTMessagingHandlerPtr> ListItems;
TSharedPtr<SListView<UHttpGPTMessagingHandlerPtr>> ListView;
TSharedPtr<SVerticalBox> ChatBox;
TArray<SHttpGPTChatItemPtr> ChatItems;

TSharedPtr<SEditableTextBox, ESPMode::ThreadSafe> InputTextBox;
TSharedPtr<STextComboBox, ESPMode::ThreadSafe> ModelsComboBox;
Expand All @@ -72,18 +91,3 @@ class SHttpGPTChatView final : public SCompoundWidget
class UHttpGPTRequest* RequestReference;
#endif
};

class SHttpGPTChatItem : public STableRow<UHttpGPTMessagingHandlerPtr>
{
public:
SLATE_BEGIN_ARGS(SHttpGPTChatItem) { }
SLATE_END_ARGS()

void Construct(const FArguments& InArgs, const TSharedRef<STableViewBase>& InOwnerTableView, const UHttpGPTMessagingHandlerPtr InMessagingHandlerObject);

FText GetMessageText() const;

private:
UHttpGPTMessagingHandlerPtr MessagingHandlerObject;
TSharedPtr<STextBlock, ESPMode::ThreadSafe> MessageBox;
};

0 comments on commit 21391a0

Please sign in to comment.