5
5
#include " SHttpGPTChatView.h"
6
6
#include < HttpGPTHelper.h>
7
7
#include < Interfaces/IPluginManager.h>
8
+ #include < Widgets/Layout/SScrollBox.h>
8
9
9
10
#ifdef UE_INLINE_GENERATED_CPP_BY_NAME
10
11
#include UE_INLINE_GENERATED_CPP_BY_NAME(SHttpGPTChatView)
@@ -43,8 +44,61 @@ void UHttpGPTMessagingHandler::ResponseReceived(const FHttpGPTResponse& Response
43
44
}
44
45
}
45
46
47
+ void SHttpGPTChatItem::Construct (const FArguments& InArgs)
48
+ {
49
+ MessagingHandlerObject = NewObject<UHttpGPTMessagingHandler>();
50
+ MessagingHandlerObject->Message = FHttpGPTMessage (InArgs._MessageRole , InArgs._InputText );
51
+
52
+ #if ENGINE_MAJOR_VERSION < 5
53
+ using FAppStyle = FEditorStyle;
54
+ #endif
55
+
56
+ const ISlateStyle& AppStyle = FAppStyle::Get ();
57
+
58
+ ChildSlot
59
+ [
60
+ SNew (SVerticalBox)
61
+ + SVerticalBox::Slot ()
62
+ .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))
63
+ [
64
+ SNew (SBorder)
65
+ .BorderImage (AppStyle.GetBrush (" Menu.Background" ))
66
+ [
67
+ SNew (SVerticalBox)
68
+ + SVerticalBox::Slot ()
69
+ .Padding (Slot_Padding)
70
+ .AutoHeight ()
71
+ [
72
+ SNew (STextBlock)
73
+ .Font (FCoreStyle::GetDefaultFontStyle (" Bold" , 10 ))
74
+ .Text (FText::FromString (MessagingHandlerObject->Message .Role == EHttpGPTRole::User ? " User:" : " Assistant:" ))
75
+ ]
76
+ + SVerticalBox::Slot ()
77
+ .Padding (FMargin (Slot_Padding * 4 , Slot_Padding, Slot_Padding, Slot_Padding))
78
+ .FillHeight (1 .f )
79
+ [
80
+ SAssignNew (MessageBox, STextBlock)
81
+ .AutoWrapText (true )
82
+ .Text (this , &SHttpGPTChatItem::GetMessageText)
83
+ ]
84
+ ]
85
+ ]
86
+ ];
87
+ }
88
+
89
+ FText SHttpGPTChatItem::GetMessageText () const
90
+ {
91
+ return FText::FromString (MessagingHandlerObject->Message .Content );
92
+ }
93
+
46
94
void SHttpGPTChatView::Construct ([[maybe_unused]] const FArguments&)
47
95
{
96
+ #if ENGINE_MAJOR_VERSION < 5
97
+ using FAppStyle = FEditorStyle;
98
+ #endif
99
+
100
+ const ISlateStyle& AppStyle = FAppStyle::Get ();
101
+
48
102
ModelsComboBox = SNew (STextComboBox)
49
103
.OptionsSource (&AvailableModels)
50
104
.ToolTipText (FText::FromString (" Selected GPT Model" ));
@@ -58,10 +112,15 @@ void SHttpGPTChatView::Construct([[maybe_unused]] const FArguments&)
58
112
.Padding (Slot_Padding)
59
113
.FillHeight (1 .f )
60
114
[
61
- SAssignNew (ListView, SListView<UHttpGPTMessagingHandlerPtr>)
62
- .ListItemsSource (&ListItems)
63
- .SelectionMode (ESelectionMode::None)
64
- .OnGenerateRow (this , &SHttpGPTChatView::OnGenerateRow)
115
+ SNew (SBorder)
116
+ .BorderImage (AppStyle.GetBrush (" NoBorder" ))
117
+ [
118
+ SNew (SScrollBox)
119
+ + SScrollBox::Slot ()
120
+ [
121
+ SAssignNew (ChatBox, SVerticalBox)
122
+ ]
123
+ ]
65
124
]
66
125
+ SVerticalBox::Slot ()
67
126
.Padding (Slot_Padding)
@@ -104,45 +163,37 @@ void SHttpGPTChatView::Construct([[maybe_unused]] const FArguments&)
104
163
];
105
164
}
106
165
107
- TSharedRef<ITableRow> SHttpGPTChatView::OnGenerateRow (UHttpGPTMessagingHandlerPtr Item, const TSharedRef<STableViewBase>& OwnerTable)
108
- {
109
- return SNew (SHttpGPTChatItem, OwnerTable, Item);
110
- }
111
-
112
166
FReply SHttpGPTChatView::HandleSendMessageButton ()
113
167
{
114
- UHttpGPTMessagingHandlerPtr UserMessage = NewObject<UHttpGPTMessagingHandler>();
115
- UserMessage->Message = FHttpGPTMessage (EHttpGPTRole::User, InputTextBox->GetText ().ToString ());
116
-
117
- ListItems.Add (UserMessage);
168
+ SHttpGPTChatItemPtr UserMessage = SNew (SHttpGPTChatItem).MessageRole (EHttpGPTRole::User).InputText (InputTextBox->GetText ().ToString ());
169
+ ChatBox->AddSlot ().AutoHeight () [ UserMessage.ToSharedRef () ];
170
+ ChatItems.Add (UserMessage);
118
171
119
- UHttpGPTMessagingHandlerPtr NewMessage = NewObject<UHttpGPTMessagingHandler>();
120
- NewMessage->Message .Role = EHttpGPTRole::Assistant;
172
+ SHttpGPTChatItemPtr AssistantMessage = SNew (SHttpGPTChatItem).MessageRole (EHttpGPTRole::Assistant);
121
173
122
174
FHttpGPTOptions Options;
123
175
Options.Model = UHttpGPTHelper::NameToModel (*(*ModelsComboBox->GetSelectedItem ().Get ()));
124
176
Options.bStream = true ;
125
177
126
178
RequestReference = UHttpGPTRequest::SendMessages_CustomOptions (GEditor->GetEditorWorldContext ().World (), GetChatHistory (), Options);
127
179
128
- RequestReference->ProgressStarted .AddDynamic (NewMessage , &UHttpGPTMessagingHandler::ResponseReceived);
129
- RequestReference->ProgressUpdated .AddDynamic (NewMessage , &UHttpGPTMessagingHandler::ResponseReceived);
130
- RequestReference->ProcessCompleted .AddDynamic (NewMessage , &UHttpGPTMessagingHandler::ResponseReceived);
131
- RequestReference->ErrorReceived .AddDynamic (NewMessage , &UHttpGPTMessagingHandler::ResponseReceived);
132
- RequestReference->RequestFailed .AddDynamic (NewMessage , &UHttpGPTMessagingHandler::RequestFailed);
133
- RequestReference->RequestSent .AddDynamic (NewMessage , &UHttpGPTMessagingHandler::RequestSent);
180
+ RequestReference->ProgressStarted .AddDynamic (AssistantMessage-> MessagingHandlerObject , &UHttpGPTMessagingHandler::ResponseReceived);
181
+ RequestReference->ProgressUpdated .AddDynamic (AssistantMessage-> MessagingHandlerObject , &UHttpGPTMessagingHandler::ResponseReceived);
182
+ RequestReference->ProcessCompleted .AddDynamic (AssistantMessage-> MessagingHandlerObject , &UHttpGPTMessagingHandler::ResponseReceived);
183
+ RequestReference->ErrorReceived .AddDynamic (AssistantMessage-> MessagingHandlerObject , &UHttpGPTMessagingHandler::ResponseReceived);
184
+ RequestReference->RequestFailed .AddDynamic (AssistantMessage-> MessagingHandlerObject , &UHttpGPTMessagingHandler::RequestFailed);
185
+ RequestReference->RequestSent .AddDynamic (AssistantMessage-> MessagingHandlerObject , &UHttpGPTMessagingHandler::RequestSent);
134
186
135
187
RequestReference->Activate ();
136
188
137
189
if (RequestReference->IsTaskActive ())
138
190
{
139
- ListItems.Add (NewMessage);
191
+ ChatBox->AddSlot ().AutoHeight () [AssistantMessage.ToSharedRef ()];
192
+ ChatItems.Add (AssistantMessage);
140
193
}
141
194
142
195
InputTextBox->SetText (FText::GetEmpty ());
143
196
144
- ListView->RequestListRefresh ();
145
-
146
197
return FReply::Handled ();
147
198
}
148
199
@@ -153,9 +204,8 @@ bool SHttpGPTChatView::IsSendMessageEnabled() const
153
204
154
205
FReply SHttpGPTChatView::HandleClearChatButton ()
155
206
{
156
- ListItems.Empty ();
157
-
158
- ListView->RequestListRefresh ();
207
+ ChatItems.Empty ();
208
+ ChatBox->ClearChildren ();
159
209
160
210
if (RequestReference)
161
211
{
@@ -167,7 +217,7 @@ FReply SHttpGPTChatView::HandleClearChatButton()
167
217
168
218
bool SHttpGPTChatView::IsClearChatEnabled () const
169
219
{
170
- return !ListItems .IsEmpty ();
220
+ return !ChatItems .IsEmpty ();
171
221
}
172
222
173
223
TArray<FHttpGPTMessage> SHttpGPTChatView::GetChatHistory () const
@@ -177,9 +227,9 @@ TArray<FHttpGPTMessage> SHttpGPTChatView::GetChatHistory() const
177
227
FHttpGPTMessage (EHttpGPTRole::System, GetSystemContext ())
178
228
};
179
229
180
- for (const auto & Item : ListItems )
230
+ for (const auto & Item : ChatItems )
181
231
{
182
- Output.Add (Item->Message );
232
+ Output.Add (Item->MessagingHandlerObject -> Message );
183
233
}
184
234
185
235
return Output;
@@ -213,50 +263,3 @@ void SHttpGPTChatView::InitializeModelsOptions()
213
263
}
214
264
}
215
265
}
216
-
217
- void SHttpGPTChatItem::Construct (const FArguments& InArgs, const TSharedRef<STableViewBase>& InOwnerTableView, const UHttpGPTMessagingHandlerPtr InMessagingHandlerObject)
218
- {
219
- #if ENGINE_MAJOR_VERSION < 5
220
- using FAppStyle = FEditorStyle;
221
- #endif
222
-
223
- const ISlateStyle& AppStyle = FAppStyle::Get ();
224
-
225
- MessagingHandlerObject = InMessagingHandlerObject;
226
-
227
- STableRow<UHttpGPTMessagingHandlerPtr>::Construct (STableRow<UHttpGPTMessagingHandlerPtr>::FArguments (), InOwnerTableView);
228
-
229
- ChildSlot
230
- [
231
- SNew (SVerticalBox)
232
- + SVerticalBox::Slot ()
233
- .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))
234
- [
235
- SNew (SBorder)
236
- .BorderImage (AppStyle.GetBrush (" ToolPanel.GroupBorder" ))
237
- [
238
- SNew (SVerticalBox)
239
- + SVerticalBox::Slot ()
240
- .Padding (Slot_Padding)
241
- .AutoHeight ()
242
- [
243
- SNew (STextBlock)
244
- .Text (FText::FromString (MessagingHandlerObject->Message .Role == EHttpGPTRole::User ? " User:" : " Assistant:" ))
245
- ]
246
- + SVerticalBox::Slot ()
247
- .Padding (FMargin (Slot_Padding * 4 , Slot_Padding, Slot_Padding, Slot_Padding))
248
- .FillHeight (1 .f )
249
- [
250
- SAssignNew (MessageBox, STextBlock)
251
- .AutoWrapText (true )
252
- .Text (this , &SHttpGPTChatItem::GetMessageText)
253
- ]
254
- ]
255
- ]
256
- ];
257
- }
258
-
259
- FText SHttpGPTChatItem::GetMessageText () const
260
- {
261
- return FText::FromString (MessagingHandlerObject->Message .Content );
262
- }
0 commit comments