Skip to content

Commit a02f40c

Browse files
committed
refactor/add-DelphiAIDev.Chat.ProcessResponse
1 parent 98f96b4 commit a02f40c

File tree

6 files changed

+350
-137
lines changed

6 files changed

+350
-137
lines changed

Package/DelphiAIDeveloper.dpk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ contains
7272
DelphiAIDev.DefaultsQuestions.AddEdit.View in '..\Src\DefaultsQuestions\DelphiAIDev.DefaultsQuestions.AddEdit.View.pas' {DelphiAIDevDefaultsQuestionsAddEditView},
7373
DelphiAIDev.DefaultsQuestions.PopupMenu in '..\Src\DefaultsQuestions\DelphiAIDev.DefaultsQuestions.PopupMenu.pas',
7474
DelphiAIDev.Utils.ABMenuAction in '..\Src\Utils\DelphiAIDev.Utils.ABMenuAction.pas',
75-
DelphiAIDev.IDE.Splash in '..\Src\IDE\Splash\DelphiAIDev.IDE.Splash.pas';
75+
DelphiAIDev.IDE.Splash in '..\Src\IDE\Splash\DelphiAIDev.IDE.Splash.pas',
76+
DelphiAIDev.Chat.ProcessResponse in '..\Src\Chat\DelphiAIDev.Chat.ProcessResponse.pas';
7677

7778
end.

Package/DelphiAIDeveloper.dproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<DCCReference Include="..\Src\DefaultsQuestions\DelphiAIDev.DefaultsQuestions.PopupMenu.pas"/>
180180
<DCCReference Include="..\Src\Utils\DelphiAIDev.Utils.ABMenuAction.pas"/>
181181
<DCCReference Include="..\Src\IDE\Splash\DelphiAIDev.IDE.Splash.pas"/>
182+
<DCCReference Include="..\Src\Chat\DelphiAIDev.Chat.ProcessResponse.pas"/>
182183
<RcItem Include="Img\c4d_gear.bmp">
183184
<ResourceType>BITMAP</ResourceType>
184185
<ResourceId>c4d_gear</ResourceId>
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
unit DelphiAIDev.Chat.ProcessResponse;
2+
3+
interface
4+
5+
uses
6+
System.SysUtils,
7+
System.StrUtils,
8+
System.Classes,
9+
Winapi.Windows,
10+
Winapi.Messages,
11+
Vcl.ComCtrls,
12+
Vcl.Graphics,
13+
DelphiAIDev.Consts,
14+
DelphiAIDev.Utils,
15+
DelphiAIDev.Utils.OTA,
16+
DelphiAIDev.Settings;
17+
18+
type
19+
TDelphiAIDevChatProcessResponse = class
20+
private
21+
FRichEdit: TRichEdit;
22+
FSettings: TDelphiAIDevSettings;
23+
FCurrentColor: TColor;
24+
procedure AddResponseLine(const ALineStr: string);
25+
procedure BoldInWordsBetweenBacktick(const ALineStr: string);
26+
procedure BoldInWordsBetweenTwoAsterisk(const ALineStr: string);
27+
public
28+
constructor Create(const ARichEdit: TRichEdit);
29+
procedure AddResponseComplete(const AStrings: TStrings);
30+
end;
31+
32+
implementation
33+
34+
const
35+
BACKTICK = '`';
36+
ASTERISK = '*';
37+
38+
constructor TDelphiAIDevChatProcessResponse.Create(const ARichEdit: TRichEdit);
39+
begin
40+
FRichEdit := ARichEdit;
41+
FSettings := TDelphiAIDevSettings.GetInstance;
42+
end;
43+
44+
//Add line-by-line response to color where Delphi code is
45+
procedure TDelphiAIDevChatProcessResponse.AddResponseComplete(const AStrings: TStrings);
46+
var
47+
LLineNum: Integer;
48+
LLineStr: string;
49+
LCodeStarted: Boolean;
50+
begin
51+
FRichEdit.Lines.Clear;
52+
FCurrentColor := TUtilsOTA.ActiveThemeColorDefault;
53+
FRichEdit.SelAttributes.Color := FCurrentColor;
54+
FRichEdit.SelAttributes.Style := [];
55+
56+
LCodeStarted := False;
57+
for LLineNum := 0 to Pred(AStrings.Count) do
58+
begin
59+
LLineStr := AStrings[LLineNum].TrimRight;
60+
61+
if not LCodeStarted then
62+
begin
63+
if TUtils.CodeIdMarkBeginCode(LLineStr) then
64+
begin
65+
LCodeStarted := True;
66+
Continue;
67+
end;
68+
end;
69+
70+
if LLineStr.Trim = TConsts.MARK_END then
71+
begin
72+
LCodeStarted := False;
73+
FCurrentColor := TUtilsOTA.ActiveThemeColorDefault;
74+
FRichEdit.SelAttributes.Color := FCurrentColor;
75+
Continue;
76+
end;
77+
78+
if LCodeStarted then
79+
begin
80+
if (FSettings.ColorHighlightCodeDelphiUse) and (FSettings.ColorHighlightCodeDelphi <> clNone) then
81+
FCurrentColor := FSettings.ColorHighlightCodeDelphi
82+
else
83+
FCurrentColor := TUtilsOTA.ActiveThemeForCode;
84+
end
85+
else
86+
FCurrentColor := TUtilsOTA.ActiveThemeColorDefault;
87+
88+
FRichEdit.SelAttributes.Color := FCurrentColor;
89+
90+
//Optional use of one of the following lines
91+
//FRichEdit.Lines.Add(LLineStr);
92+
Self.AddResponseLine(LLineStr);
93+
end;
94+
//Self.Last;
95+
end;
96+
97+
procedure TDelphiAIDevChatProcessResponse.AddResponseLine(const ALineStr: string);
98+
begin
99+
if ALineStr.Contains(BACKTICK) then
100+
Self.BoldInWordsBetweenBacktick(ALineStr)
101+
else if ALineStr.Contains(ASTERISK + ASTERISK) then
102+
Self.BoldInWordsBetweenTwoAsterisk(ALineStr)
103+
else
104+
FRichEdit.Lines.Add(IfThen(ALineStr.IsEmpty, ' ', ALineStr));
105+
end;
106+
107+
procedure TDelphiAIDevChatProcessResponse.BoldInWordsBetweenBacktick(const ALineStr: string);
108+
var
109+
LPosLetter: Integer;
110+
LCurrentLetter: Char;
111+
LNextLetter: Char;
112+
LLineStarted: Boolean;
113+
LCodeStarted: Boolean;
114+
begin
115+
LLineStarted := False;
116+
LCodeStarted := False;
117+
for LPosLetter := 0 to ALineStr.Length do
118+
begin
119+
LCurrentLetter := ALineStr[LPosLetter];
120+
LNextLetter := ALineStr[Succ(LPosLetter)];
121+
122+
if not LCodeStarted then
123+
begin
124+
if(LCurrentLetter = BACKTICK)and(LNextLetter <> BACKTICK)then
125+
begin
126+
LCodeStarted := True;
127+
Continue;
128+
end;
129+
end;
130+
131+
if(LCurrentLetter = BACKTICK)and(LNextLetter <> BACKTICK)then
132+
begin
133+
LCodeStarted := False;
134+
FRichEdit.SelAttributes.Style := [];
135+
Continue;
136+
end;
137+
138+
SendMessage(FRichEdit.Handle, WM_VSCROLL, SB_BOTTOM, 0);
139+
if LCodeStarted then
140+
FRichEdit.SelAttributes.Style := [fsBold]
141+
else
142+
FRichEdit.SelAttributes.Style := [];
143+
144+
FRichEdit.SelAttributes.Color := FCurrentColor;
145+
146+
if LLineStarted then
147+
FRichEdit.SelText := LCurrentLetter
148+
else
149+
begin
150+
FRichEdit.Lines.Add('');
151+
FRichEdit.SelText := LCurrentLetter;
152+
153+
LLineStarted := True;
154+
end;
155+
SendMessage(FRichEdit.Handle, WM_VSCROLL, SB_BOTTOM, 0);
156+
end;
157+
FRichEdit.SelText := ' ';
158+
SendMessage(FRichEdit.Handle, WM_VSCROLL, SB_BOTTOM, 0);
159+
end;
160+
161+
procedure TDelphiAIDevChatProcessResponse.BoldInWordsBetweenTwoAsterisk(const ALineStr: string);
162+
var
163+
LPosLetter: Integer;
164+
LCurrentLetter: Char;
165+
LNextLetter: Char;
166+
LLineStarted: Boolean;
167+
LCodeStarted: Boolean;
168+
begin
169+
LLineStarted := False;
170+
LCodeStarted := False;
171+
172+
LPosLetter := 0;
173+
while LPosLetter <= ALineStr.Length do
174+
begin
175+
LCurrentLetter := ALineStr[LPosLetter];
176+
LNextLetter := ALineStr[Succ(LPosLetter)];
177+
178+
if not LCodeStarted then
179+
begin
180+
if(LCurrentLetter = ASTERISK)and(LNextLetter = ASTERISK)then
181+
begin
182+
LCodeStarted := True;
183+
Inc(LPosLetter, 2);
184+
Continue;
185+
end;
186+
end;
187+
188+
if(LCurrentLetter = ASTERISK)and(LNextLetter = ASTERISK)then
189+
begin
190+
LCodeStarted := False;
191+
FRichEdit.SelAttributes.Style := [];
192+
Inc(LPosLetter, 2);
193+
Continue;
194+
end;
195+
196+
SendMessage(FRichEdit.Handle, WM_VSCROLL, SB_BOTTOM, 0);
197+
if LCodeStarted then
198+
FRichEdit.SelAttributes.Style := [fsBold]
199+
else
200+
FRichEdit.SelAttributes.Style := [];
201+
202+
FRichEdit.SelAttributes.Color := FCurrentColor;
203+
204+
if LLineStarted then
205+
FRichEdit.SelText := LCurrentLetter
206+
else
207+
begin
208+
FRichEdit.Lines.Add('');
209+
FRichEdit.SelText := LCurrentLetter;
210+
211+
LLineStarted := True;
212+
end;
213+
SendMessage(FRichEdit.Handle, WM_VSCROLL, SB_BOTTOM, 0);
214+
Inc(LPosLetter);
215+
end;
216+
FRichEdit.SelText := ' ';
217+
SendMessage(FRichEdit.Handle, WM_VSCROLL, SB_BOTTOM, 0);
218+
end;
219+
220+
end.

0 commit comments

Comments
 (0)