Skip to content

Commit db970fd

Browse files
committed
Better Free handling
1 parent 48c3ba9 commit db970fd

File tree

3 files changed

+90
-57
lines changed

3 files changed

+90
-57
lines changed

FetchXmlBuilder/DockControls/AiChatControl.cs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public partial class AiChatControl : WeifenLuo.WinFormsUI.Docking.DockContent
3838

3939
public AiChatControl(FetchXmlBuilder fetchXmlBuilder)
4040
{
41+
ChatMessageHistory.UserTextColor = OnlineSettings.Instance.Colors.Bright;
42+
ChatMessageHistory.UserBackgroundColor = OnlineSettings.Instance.Colors.Medium;
43+
ChatMessageHistory.AssistansTextColor = OnlineSettings.Instance.Colors.Dark;
44+
ChatMessageHistory.AssistansBackgroundColor = OnlineSettings.Instance.Colors.Bright;
45+
ChatMessageHistory.WaitingBackColor = Color.FromArgb(240, 240, 240);
46+
4147
fxb = fetchXmlBuilder;
4248
InitializeComponent();
4349
Initialize();
@@ -50,12 +56,6 @@ public AiChatControl(FetchXmlBuilder fetchXmlBuilder)
5056

5157
internal void Initialize()
5258
{
53-
ChatMessageHistory.UserTextColor = OnlineSettings.Instance.Colors.Bright;
54-
ChatMessageHistory.UserBackgroundColor = OnlineSettings.Instance.Colors.Medium;
55-
ChatMessageHistory.AssistansTextColor = OnlineSettings.Instance.Colors.Dark;
56-
ChatMessageHistory.AssistansBackgroundColor = OnlineSettings.Instance.Colors.Bright;
57-
ChatMessageHistory.WaitingBackColor = Color.FromArgb(240, 240, 240);
58-
5959
freeusers = null;
6060
ClosingSession();
6161

@@ -77,9 +77,13 @@ internal void Initialize()
7777
var apikey = "";
7878
if (supplier.IsFree)
7979
{
80+
logname = "AI-Free";
8081
if (!IsFreeAiUser(fxb))
8182
{
82-
PromptToUseForFree(fxb);
83+
if (MessageBoxEx.Show(fxb, $"To use the free AI provider, you have to fill in this form.\nOn this webpage you can read details about why and why.", "Free AI by Jonas", MessageBoxButtons.OKCancel) == DialogResult.OK)
84+
{
85+
PromptToUseForFree(fxb);
86+
}
8387
return;
8488
}
8589
apikey = model.ApiKey;
@@ -110,14 +114,18 @@ internal static bool IsFreeAiUser(PluginControlBase tool)
110114

111115
internal static void PromptToUseForFree(PluginControlBase tool)
112116
{
113-
if (MessageBoxEx.Show(tool, $"To use the free AI provider, you have to fill in this form.\nOn this webpage you can read details about why and why.", "Free AI by Jonas", MessageBoxButtons.OKCancel) == DialogResult.OK)
114-
{
115-
var url = OnlineSettings.Instance.AiSupport.UrlToUseForFree;
116-
var wpf = OnlineSettings.Instance.AiSupport.WpfToUseForFree;
117-
var installid = InstallationInfo.Instance.InstallationId;
118-
var version = Assembly.GetExecutingAssembly().GetName().Version;
119-
Process.Start($"{url}?wpf{wpf}_31={tool.ToolName}&wpf{wpf}_32={version}&wpf{wpf}_33={installid}");
120-
}
117+
var install = Installation.Load(null);
118+
var url = OnlineSettings.Instance.AiSupport.UrlToUseForFree;
119+
var wpf = OnlineSettings.Instance.AiSupport.WpfToUseForFree;
120+
var installid = InstallationInfo.Instance.InstallationId;
121+
var version = Assembly.GetExecutingAssembly().GetName().Version;
122+
var fullurl = $"{url}?" +
123+
$"wpf{wpf}_1_first={install?.PersonalFirstName}&" +
124+
$"wpf{wpf}_1_last={install?.PersonalLastName}&" +
125+
$"wpf{wpf}_3={install?.PersonalCountry}&" +
126+
$"wpf{wpf}_4={install?.PersonalEmail}&" +
127+
$"wpf{wpf}_31={tool.ToolName}&wpf{wpf}_32={version}&wpf{wpf}_33={installid}";
128+
Process.Start(fullurl);
121129
}
122130

123131
#endregion Internal Methods
@@ -225,7 +233,7 @@ private void SendChatToAI(object sender, EventArgs e = null)
225233
intro += Environment.NewLine + PromptMyName.Replace("{callme}", fxb.settings.AiSettings.MyName).Trim();
226234
}
227235
chatHistory.Initialize(intro);
228-
fxb.LogUse($"{logname}-{model}-Init", count: intro.Length, ai2: true, ai1: false);
236+
Log($"{model}-Init", count: intro.Length);
229237
sessionstopwatch = Stopwatch.StartNew();
230238
}
231239
else if (!manualquery.EqualXml(lastquery))
@@ -236,7 +244,7 @@ private void SendChatToAI(object sender, EventArgs e = null)
236244

237245
chatHistory.IsRunning = true;
238246
EnableButtons();
239-
fxb.LogUse($"{logname}-{action}", count: text.Length, ai2: true, ai1: false);
247+
Log(action, count: text.Length);
240248
callingstopwatch = Stopwatch.StartNew();
241249
try
242250
{
@@ -261,7 +269,7 @@ private void SendChatToAI(object sender, EventArgs e = null)
261269
private void HandlingResponseFromAi(ChatResponse response)
262270
{
263271
callingstopwatch?.Stop();
264-
fxb.LogUse($"{logname}-Response", count: response?.ToString()?.Length, duration: callingstopwatch?.ElapsedMilliseconds, ai2: true, ai1: false);
272+
Log("Response", response?.ToString()?.Length, callingstopwatch?.ElapsedMilliseconds);
265273
txtAiChat.Clear();
266274
txtUsage.Text = chatHistory.Responses.UsageToString();
267275
EnableButtons();
@@ -277,7 +285,7 @@ private string ExecuteFetchXMLQuery([Description("The FetchXML Query to be Execu
277285
var sw = Stopwatch.StartNew();
278286
var result = fxb.RetrieveMultipleSync(fetchXml, null, null);
279287
sw.Stop();
280-
fxb.LogUse($"{logname}-Query-Execute", count: result is QueryInfo qi ? qi.Results.Entities.Count : 0, duration: sw.ElapsedMilliseconds, ai2: true, ai1: false);
288+
Log($"Query-Execute", result is QueryInfo qi ? qi.Results.Entities.Count : 0, sw.ElapsedMilliseconds);
281289
fxb.HandleRetrieveMultipleResult(result);
282290
//fxb.dockControlGrid?.ResetLayout(); Commented it out since it exploded, but it might be good to do this after each new query execute
283291
return "Query executed successfully";
@@ -321,7 +329,7 @@ private string GetMetadataForUnknownEntity([Description("The name/description of
321329
var result = AiCommunication.SamplingAI(PromptEntityMeta.Replace("{metadata}", json),
322330
$"Please find entries that match the description {tableDescription}", chatHistory);
323331
sw.Stop();
324-
fxb.LogUse($"{logname}-Meta-Entity-{tableDescription}", count: entities.Count, duration: sw.ElapsedMilliseconds, ai2: true, ai1: false);
332+
Log($"Meta-Entity-{tableDescription}", entities.Count, sw.ElapsedMilliseconds);
325333

326334
chatHistory.Add(result, true);
327335
return result.Text;
@@ -359,7 +367,7 @@ private string GetMetadataForUnknownAttribute([Description("The logical name of
359367
var result = AiCommunication.SamplingAI(PromptAttributeMeta.Replace("{metadata}", json),
360368
$"Please find attributes that match the description {attributeDescription}", chatHistory);
361369
sw.Stop();
362-
fxb.LogUse($"{logname}-Meta-Attribute-{entityName}", count: attributes.Count, duration: sw.ElapsedMilliseconds, ai2: true, ai1: false);
370+
Log($"Meta-Attribute-{entityName}", attributes.Count, sw.ElapsedMilliseconds);
363371

364372
chatHistory.Add(result, true);
365373

@@ -375,7 +383,7 @@ private void SetQueryFromAi(string fetch)
375383
lastquery = fetch;
376384
MethodInvoker mi = () => { fxb.dockControlBuilder.Init(fetch, null, false, "Query from AI", true); };
377385
if (InvokeRequired) Invoke(mi); else mi();
378-
fxb.LogUse($"{logname}-Query-Change", ai2: true, ai1: false);
386+
Log($"Query-Change");
379387
}
380388

381389
private void PopupMessageIfRelevant()
@@ -410,15 +418,22 @@ private void ClosingSession()
410418
chatHistory.Save(Paths.LogsPath, "FXB");
411419
if (manualcalls > 0)
412420
{
413-
fxb.LogUse($"{logname}-Session-Calls", count: manualcalls, ai2: true, ai1: false);
421+
Log($"Session-Calls", count: manualcalls);
414422
}
415-
if (chatHistory?.Initialized == true)
423+
if (chatHistory.Initialized)
416424
{
417-
fxb.LogUse($"{logname}-Closing", count: chatHistory.Responses?.Count, duration: sessionstopwatch?.ElapsedMilliseconds, ai2: true, ai1: false);
425+
Log($"Session-Responses", chatHistory.Responses?.Count, sessionstopwatch?.ElapsedMilliseconds);
418426
}
427+
chatHistory = null;
428+
Log("Close");
419429
}
420430
}
421431

432+
private void Log(string action, double? count = null, double? duration = null)
433+
{
434+
fxb.LogUse($"{logname}-{action}", false, count, duration, false, true);
435+
}
436+
422437
#endregion Private Methods
423438

424439
#region Private Event Handlers

FetchXmlBuilder/Forms/Settings.Designer.cs

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FetchXmlBuilder/Forms/Settings.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Rappen.AI.WinForm;
88
using Rappen.XTB.Helpers;
99
using System.Collections.Generic;
10+
using Rappen.XTB.FetchXmlBuilder.DockControls;
1011

1112
namespace Rappen.XTB.FetchXmlBuilder.Forms
1213
{
@@ -91,6 +92,7 @@ private void PopulateSettings(FXBSettings settings)
9192
}
9293
txtAiApiKey.Text = settings.AiSettings.ApiKey;
9394
txtAiCallMe.Text = settings.AiSettings.MyName;
95+
cmbAiSupplier_SelectedIndexChanged();
9496
}
9597

9698
private int SettingResultToComboBoxItem(ResultOutput resultOutput)
@@ -140,7 +142,7 @@ internal FXBSettings GetSettings()
140142
settings.AlwaysShowAggregationProperties = chkAlwaysShowAggregateProperties.Checked;
141143
settings.AiSettings.Supplier = cmbAiSupplier.Text;
142144
settings.AiSettings.Model = cmbAiModel.Text;
143-
settings.AiSettings.ApiKey = txtAiApiKey.Text;
145+
settings.AiSettings.ApiKey = txtAiApiKey.Enabled ? txtAiApiKey.Text : "";
144146
settings.AiSettings.MyName = txtAiCallMe.Text;
145147
UpdateAiSettingsList();
146148
settings.AiSettingsList = aisettingslist;
@@ -305,7 +307,7 @@ cmbAiModel.SelectedItem is AiModel model &&
305307
}
306308
}
307309

308-
private void cmbAiSupplier_SelectedIndexChanged(object sender, EventArgs e)
310+
private void cmbAiSupplier_SelectedIndexChanged(object sender = null, EventArgs e = null)
309311
{
310312
cmbAiModel.Items.Clear();
311313
picAiSupplier.Tag = null;
@@ -324,7 +326,16 @@ private void cmbAiSupplier_SelectedIndexChanged(object sender, EventArgs e)
324326
}
325327
if (supplier.IsFree)
326328
{
327-
txtAiApiKey.Text = "";
329+
if (AiChatControl.IsFreeAiUser(fxb))
330+
{
331+
txtAiApiKey.Text = "ApiKey is handled by Jonas for this free provider.";
332+
}
333+
else
334+
{
335+
txtAiApiKey.Text = "To use the free provider, you have to submit a form only for Jonas to make sure you are not using gazillian tokens. Click the (i) button on the provider above!";
336+
picAiSupplier.Tag = "FREE";
337+
tt.SetToolTip(picAiSupplier, $"Click to fill in the form to get free provider! at {OnlineSettings.Instance.AiSupport.UrlToUseForFree}");
338+
}
328339
txtAiApiKey.Enabled = false;
329340
}
330341
else
@@ -346,7 +357,14 @@ private void cmbAiModel_SelectedIndexChanged(object sender, EventArgs e)
346357

347358
private void picAiSupplier_Click(object sender, EventArgs e)
348359
{
349-
UrlUtils.OpenUrl(sender);
360+
if (sender is PictureBox pic && pic.Tag == "FREE")
361+
{
362+
AiChatControl.PromptToUseForFree(fxb);
363+
}
364+
else
365+
{
366+
UrlUtils.OpenUrl(sender);
367+
}
350368
}
351369

352370
private void chkWorkWithLayout_CheckedChanged(object sender, EventArgs e)

0 commit comments

Comments
 (0)