Skip to content

Commit 5359a79

Browse files
authored
[Copilot] Interface for logging quota usage (#3018)
<!-- Thank you for submitting a Pull Request. If you're new to contributing to BCApps please read our pull request guideline below * https://github.com/microsoft/BCApps/Contributing.md --> #### Summary <!-- Provide a general summary of your changes --> We need to abstract the interface to log Copilot Quota Usage in the System Application. #### Work Item(s) <!-- Add the issue number here after the #. The issue needs to be open and approved. Submitting PRs with no linked issues or unapproved issues is highly discouraged. --> Fixes [AB#566503](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/566503)
1 parent 9e440c7 commit 5359a79

File tree

6 files changed

+131
-6
lines changed

6 files changed

+131
-6
lines changed

src/System Application/App/AI/app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"idRanges": [
9797
{
9898
"from": 7757,
99-
"to": 7782
99+
"to": 7786
100100
}
101101
],
102102
"target": "OnPrem",

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,22 @@ codeunit 7774 "Copilot Capability Impl"
202202
end;
203203

204204
procedure GetCapabilityName(): Text
205+
begin
206+
CheckCapabilitySet();
207+
208+
exit(CapabilityToEnumName(CopilotSettings.Capability));
209+
end;
210+
211+
procedure CapabilityToEnumName(CopilotCapability: Enum "Copilot Capability"): Text
205212
var
206213
CapabilityIndex: Integer;
207214
CapabilityName: Text;
208215
begin
209-
CheckCapabilitySet();
210-
211-
CapabilityIndex := CopilotSettings.Capability.Ordinals.IndexOf(CopilotSettings.Capability.AsInteger());
212-
CapabilityName := CopilotSettings.Capability.Names.Get(CapabilityIndex);
216+
CapabilityIndex := CopilotCapability.Ordinals.IndexOf(CopilotCapability.AsInteger());
217+
CapabilityName := CopilotCapability.Names.Get(CapabilityIndex);
213218

214219
if CapabilityName.Trim() = '' then
215-
exit(Format(CopilotSettings.Capability, 0, 9));
220+
exit(Format(CopilotCapability, 0, 9));
216221

217222
exit(CapabilityName);
218223
end;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ------------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License. See License.txt in the project root for license information.
4+
// ------------------------------------------------------------------------------------------------
5+
namespace System.AI;
6+
7+
/// <summary>
8+
/// Codeunit that exposes functionality related to Copilot Quota, such as logging quota usage
9+
/// </summary>
10+
codeunit 7785 "Copilot Quota"
11+
{
12+
Access = Public;
13+
InherentEntitlements = X;
14+
InherentPermissions = X;
15+
16+
var
17+
CopilotQuotaImpl: Codeunit "Copilot Quota Impl.";
18+
19+
/// <summary>
20+
/// Try function to log usage of Copilot quota in the system. This function is only available for Microsoft Copilot features.
21+
/// </summary>
22+
/// <param name="CopilotCapability">The Copilot Capability to log usage for.</param>
23+
/// <param name="Usage">The usage to log.</param>
24+
/// <param name="CopilotQuotaUsageType">The type of Copilot Quota to log.</param>
25+
[TryFunction]
26+
[Scope('OnPrem')]
27+
procedure TryLogQuotaUsage(CopilotCapability: Enum "Copilot Capability"; Usage: Integer; CopilotQuotaUsageType: Enum "Copilot Quota Usage Type")
28+
var
29+
CallerModuleInfo: ModuleInfo;
30+
begin
31+
NavApp.GetCallerModuleInfo(CallerModuleInfo);
32+
CopilotQuotaImpl.LogQuotaUsage(CopilotCapability, Usage, CopilotQuotaUsageType, CallerModuleInfo);
33+
end;
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ------------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License. See License.txt in the project root for license information.
4+
// ------------------------------------------------------------------------------------------------
5+
namespace System.AI;
6+
7+
using System;
8+
9+
codeunit 7786 "Copilot Quota Impl."
10+
{
11+
Access = Internal;
12+
InherentEntitlements = X;
13+
InherentPermissions = X;
14+
15+
var
16+
InvalidUsageTypeErr: Label 'The value "%1" is not a valid Copilot Quota Usage Type.', Comment = '%1=a value such as "AI response" or "5"';
17+
CapabilityNotRegisteredTelemetryMsg: Label 'Capability "%1" is not registered in the system but is logging usage.', Locked = true;
18+
LoggingUsageTelemetryMsg: Label 'Capability "%1" is logging %2 usage of type %3.', Locked = true;
19+
20+
procedure LogQuotaUsage(CopilotCapability: Enum "Copilot Capability"; Usage: Integer; CopilotQuotaUsageType: Enum "Copilot Quota Usage Type"; CallerModuleInfo: ModuleInfo)
21+
var
22+
CopilotCapabilityImpl: Codeunit "Copilot Capability Impl";
23+
ALCopilotFunctions: DotNet ALCopilotFunctions;
24+
AlCopilotCapability: DotNet ALCopilotCapability;
25+
AlCopilotUsageType: DotNet ALCopilotUsageType;
26+
begin
27+
if not CopilotCapabilityImpl.IsCapabilityRegistered(CopilotCapability, CallerModuleInfo) then
28+
Session.LogMessage('0000OSL', StrSubstNo(CapabilityNotRegisteredTelemetryMsg, CopilotCapability), Verbosity::Warning, DataClassification::SystemMetadata, TelemetryScope::ExtensionPublisher, 'Category', CopilotCapabilityImpl.GetCopilotCategory());
29+
30+
Session.LogMessage('0000OSM', StrSubstNo(LoggingUsageTelemetryMsg, CopilotCapability, Usage, CopilotQuotaUsageType), Verbosity::Verbose, DataClassification::SystemMetadata, TelemetryScope::ExtensionPublisher, 'Category', CopilotCapabilityImpl.GetCopilotCategory());
31+
32+
ALCopilotCapability := ALCopilotCapability.ALCopilotCapability(
33+
CallerModuleInfo.Publisher(), CallerModuleInfo.Id(), Format(CallerModuleInfo.AppVersion()), CopilotCapabilityImpl.CapabilityToEnumName(CopilotCapability));
34+
35+
UsageTypeToDotnetUsageType(CopilotQuotaUsageType, AlCopilotUsageType);
36+
37+
ALCopilotFunctions.LogCopilotQuotaUsage(AlCopilotCapability, Usage, AlCopilotUsageType);
38+
end;
39+
40+
local procedure UsageTypeToDotnetUsageType(CopilotQuotaUsageType: Enum "Copilot Quota Usage Type"; var AlCopilotUsageType: DotNet AlCopilotUsageType)
41+
begin
42+
case CopilotQuotaUsageType of
43+
CopilotQuotaUsageType::"Generative AI Answer":
44+
AlCopilotUsageType := AlCopilotUsageType::GenAIAnswer;
45+
CopilotQuotaUsageType::"Autonomous Action":
46+
AlCopilotUsageType := AlCopilotUsageType::AutonomousAction;
47+
else
48+
Error(InvalidUsageTypeErr, CopilotQuotaUsageType);
49+
end;
50+
end;
51+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// ------------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License. See License.txt in the project root for license information.
4+
// ------------------------------------------------------------------------------------------------
5+
namespace System.AI;
6+
7+
/// <summary>
8+
/// Enumeration of valid types of usage of the Copilot Quota.
9+
/// </summary>
10+
enum 7785 "Copilot Quota Usage Type"
11+
{
12+
Caption = 'Copilot Quota Usage Type';
13+
Access = Public;
14+
Extensible = false;
15+
16+
/// <summary>
17+
/// Represents a Generative AI Answer usage type.
18+
/// </summary>
19+
value(0; "Generative AI Answer")
20+
{
21+
Caption = 'Generative AI Answer';
22+
}
23+
24+
/// <summary>
25+
/// Represents an Autonomous Action usage type.
26+
/// </summary>
27+
value(1; "Autonomous Action")
28+
{
29+
Caption = 'Autonomous Action';
30+
}
31+
}

src/System Application/App/DotNet Aliases/src/dotnet.al

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,10 @@ dotnet
21442144
{
21452145
}
21462146

2147+
type("Microsoft.Dynamics.Nav.Service.CopilotApi.AL.ALCopilotUsageType"; ALCopilotUsageType)
2148+
{
2149+
}
2150+
21472151
type("Microsoft.Dynamics.Nav.Service.CopilotApi.AL.ALCopilotQuotaDetails"; ALCopilotQuotaDetails)
21482152
{
21492153
}

0 commit comments

Comments
 (0)