Skip to content

Commit c81b9b6

Browse files
authored
[releases/26.0] Move quota checks to page background task (#3117)
This pull request backports #3115 to releases/26.0 Fixes [AB#567347](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/567347)
1 parent 3cc901a commit c81b9b6

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/System Application/App/AI/src/Copilot/CopilotAICapabilities.Page.al

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ page 7775 "Copilot AI Capabilities"
252252
EnvironmentInformation: Codeunit "Environment Information";
253253
WithinGeo: Boolean;
254254
WithinEUDB: Boolean;
255+
TaskId: Integer;
255256
begin
256257
OnRegisterCopilotCapability();
257258

@@ -280,7 +281,43 @@ page 7775 "Copilot AI Capabilities"
280281

281282
if EnvironmentInformation.IsSaaSInfrastructure() then begin
282283
CopilotNotifications.ShowBillingInTheFutureNotification();
283-
CopilotNotifications.CheckAIQuotaAndShowNotification();
284+
CurrPage.EnqueueBackgroundTask(TaskId, Codeunit::"Copilot Quota Impl.");
285+
end;
286+
end;
287+
288+
trigger OnPageBackgroundTaskCompleted(TaskId: Integer; Results: Dictionary of [Text, Text])
289+
var
290+
CopilotNotifications: Codeunit "Copilot Notifications";
291+
Value: Text;
292+
CanConsume: Boolean;
293+
HasBilling: Boolean;
294+
QuotaUsedPercentage: Decimal;
295+
CanConsumeLbl: Label 'CanConsume', Locked = true;
296+
HasSetupBillingLbl: Label 'HasSetupBilling', Locked = true;
297+
QuotaUsedPercentageLbl: Label 'QuotaUsedPercentage', Locked = true;
298+
begin
299+
if Results.ContainsKey(CanConsumeLbl) then begin
300+
Results.Get(CanConsumeLbl, Value);
301+
if Evaluate(CanConsume, Value) then;
302+
if not CanConsume then begin
303+
CopilotNotifications.ShowAIQuotaUsedUpNotification();
304+
exit;
305+
end;
306+
end;
307+
308+
if Results.ContainsKey(HasSetupBillingLbl) then begin
309+
Results.Get(HasSetupBillingLbl, Value);
310+
if Evaluate(HasBilling, Value) then;
311+
if HasBilling then
312+
exit;
313+
end;
314+
315+
if Results.ContainsKey(QuotaUsedPercentageLbl) then begin
316+
Results.Get(QuotaUsedPercentageLbl, Value);
317+
if Evaluate(QuotaUsedPercentage, Value) then;
318+
if QuotaUsedPercentage >= 80.0 then
319+
CopilotNotifications.ShowAIQuotaNearlyUsedUpNotification();
320+
exit;
284321
end;
285322
end;
286323

src/System Application/App/AI/src/Copilot/CopilotQuotaImpl.Codeunit.al

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ codeunit 7786 "Copilot Quota Impl."
1717
CapabilityNotRegisteredTelemetryMsg: Label 'Capability "%1" is not registered in the system but is logging usage.', Locked = true;
1818
LoggingUsageTelemetryMsg: Label 'Capability "%1" is logging %2 usage of type %3.', Locked = true;
1919

20+
trigger OnRun()
21+
var
22+
ALCopilotFunctions: DotNet ALCopilotFunctions;
23+
ALCopilotQuotaDetails: Dotnet ALCopilotQuotaDetails;
24+
Results: Dictionary of [Text, Text];
25+
begin
26+
ALCopilotQuotaDetails := ALCopilotFunctions.GetCopilotQuotaDetails();
27+
28+
if IsNull(ALCopilotQuotaDetails) then
29+
exit;
30+
31+
Results.Add('CanConsume', Format(ALCopilotQuotaDetails.CanConsume()));
32+
Results.Add('HasSetupBilling', Format(ALCopilotQuotaDetails.HasSetupBilling()));
33+
Results.Add('QuotaUsedPercentage', Format(ALCopilotQuotaDetails.QuotaUsedPercentage()));
34+
35+
Page.SetBackgroundTaskResult(Results);
36+
end;
37+
2038
procedure LogQuotaUsage(CopilotCapability: Enum "Copilot Capability"; Usage: Integer; CopilotQuotaUsageType: Enum "Copilot Quota Usage Type"; CallerModuleInfo: ModuleInfo)
2139
var
2240
CopilotCapabilityImpl: Codeunit "Copilot Capability Impl";

0 commit comments

Comments
 (0)