From 504a1ebc6edd651afcff4cf7affef8f109aa66ea Mon Sep 17 00:00:00 2001 From: Quentin Tresontani Date: Mon, 11 May 2026 07:41:30 +0200 Subject: [PATCH] [28.0][Agent] Expose setting/getting model on Agent Task level --- .../Agent/Interaction/AgentTask.Codeunit.al | 26 +++++++++++++++++++ .../Interaction/AgentTaskBuilder.Codeunit.al | 15 ++++++++++- .../Internal/AgentTaskBuilderImpl.Codeunit.al | 10 ++++++- .../Internal/AgentTaskImpl.Codeunit.al | 21 ++++++++++++++- 4 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/System Application/App/Agent/Interaction/AgentTask.Codeunit.al b/src/System Application/App/Agent/Interaction/AgentTask.Codeunit.al index 99509811bf..58834f6b21 100644 --- a/src/System Application/App/Agent/Interaction/AgentTask.Codeunit.al +++ b/src/System Application/App/Agent/Interaction/AgentTask.Codeunit.al @@ -148,6 +148,32 @@ codeunit 4303 "Agent Task" exit(AgentTaskImpl.GetCopilotCreditsConsumed(AgentTaskID)); end; + /// + /// Gets the model ID of the agent task. + /// + /// The ID of the agent task. + /// The model ID of the agent task. Must be valid value from Agent Model table. + procedure GetModelId(AgentTaskID: BigInteger): Code[30] + var + AgentTaskImpl: Codeunit "Agent Task Impl."; + begin + FeatureAccessManagement.AgentManagementAllowed(true); + exit(AgentTaskImpl.GetModelId(AgentTaskID)); + end; + + /// + /// Gets the model name of the agent task. + /// + /// The ID of the agent task. + /// The model name of the agent task. + procedure GetModelName(AgentTaskID: BigInteger): Text[70] + var + AgentTaskImpl: Codeunit "Agent Task Impl."; + begin + FeatureAccessManagement.AgentManagementAllowed(true); + exit(AgentTaskImpl.GetModelName(AgentTaskID)); + end; + var FeatureAccessManagement: Codeunit "Feature Access Management"; diff --git a/src/System Application/App/Agent/Interaction/AgentTaskBuilder.Codeunit.al b/src/System Application/App/Agent/Interaction/AgentTaskBuilder.Codeunit.al index 1a97a96d0f..0bc983f8f2 100644 --- a/src/System Application/App/Agent/Interaction/AgentTaskBuilder.Codeunit.al +++ b/src/System Application/App/Agent/Interaction/AgentTaskBuilder.Codeunit.al @@ -62,7 +62,6 @@ codeunit 4315 "Agent Task Builder" /// Specifies whether a message is required, default is true. /// Agent task that was created. /// The builder keeps the state, do not reuse the same instance of the builder to create multiple tasks. - [Scope('OnPrem')] procedure Create(SetTaskStatusToReady: Boolean; RequiresMessage: Boolean): Record "Agent Task" begin FeatureAccessManagement.AgentTaskManagementPreviewEnabled(true); @@ -81,6 +80,20 @@ codeunit 4315 "Agent Task Builder" exit(AgentTaskBuilderImpl.GetAgentTaskMessageCreated()); end; + /// + /// Set the model ID that will be used to process the task. + /// If the model ID is not set, the model from the agent will be used, if any. + /// If the agent does not have a model, the default model will be used. + /// + /// The model ID of the task. This field is used to connect to external systems, like Message ID for emails. + /// This instance of the Agent Task Builder. + procedure SetModelId(ModelId: Code[30]): codeunit "Agent Task Builder" + begin + FeatureAccessManagement.AgentManagementAllowed(true); + AgentTaskBuilderImpl.SetModelId(ModelId); + exit(this); + end; + /// /// Set the external ID of the task. /// diff --git a/src/System Application/App/Agent/Interaction/Internal/AgentTaskBuilderImpl.Codeunit.al b/src/System Application/App/Agent/Interaction/Internal/AgentTaskBuilderImpl.Codeunit.al index fbc435b6b5..76e6c71468 100644 --- a/src/System Application/App/Agent/Interaction/Internal/AgentTaskBuilderImpl.Codeunit.al +++ b/src/System Application/App/Agent/Interaction/Internal/AgentTaskBuilderImpl.Codeunit.al @@ -20,6 +20,7 @@ codeunit 4310 "Agent Task Builder Impl." GlobalAgentUserSecurityId: Guid; GlobalTaskTitle: Text[150]; GlobalExternalID: Text[2048]; + GlobalModelId: Code[30]; [Scope('OnPrem')] procedure Initialize(NewAgentUserSecurityId: Guid; NewTaskTitle: Text[150]): codeunit "Agent Task Builder Impl." @@ -38,7 +39,7 @@ codeunit 4310 "Agent Task Builder Impl." VerifyMandatoryFieldsSet(); VerifyTaskCanBeCreated(RequiresMessage); - AgentTaskImpl.CreateTask(GlobalAgentUserSecurityId, GlobalTaskTitle, GlobalExternalID, AgentTaskRecord); + AgentTaskImpl.CreateTask(GlobalAgentUserSecurityId, GlobalTaskTitle, GlobalExternalID, GlobalModelId, AgentTaskRecord); if MessageSet then begin GlobalAgentTaskMessageBuilder.SetAgentTask(AgentTaskRecord); GlobalAgentTaskMessageBuilder.Create(false); @@ -63,6 +64,13 @@ codeunit 4310 "Agent Task Builder Impl." exit(this); end; + [Scope('OnPrem')] + procedure SetModelId(ModelId: Code[30]): codeunit "Agent Task Builder Impl." + begin + GlobalModelId := ModelId; + exit(this); + end; + [Scope('OnPrem')] procedure AddTaskMessage(From: Text[250]; MessageText: Text): codeunit "Agent Task Builder Impl." begin diff --git a/src/System Application/App/Agent/Interaction/Internal/AgentTaskImpl.Codeunit.al b/src/System Application/App/Agent/Interaction/Internal/AgentTaskImpl.Codeunit.al index d3c254c99d..cd9c949b5a 100644 --- a/src/System Application/App/Agent/Interaction/Internal/AgentTaskImpl.Codeunit.al +++ b/src/System Application/App/Agent/Interaction/Internal/AgentTaskImpl.Codeunit.al @@ -54,7 +54,7 @@ codeunit 4300 "Agent Task Impl." Page.Run(Page::"Agent Task Log Entry List", AgentTaskLogEntry); end; - procedure CreateTask(AgentUserSecurityID: Guid; TaskTitle: Text[150]; ExternalID: Text[2048]; var NewAgentTask: Record "Agent Task") + procedure CreateTask(AgentUserSecurityID: Guid; TaskTitle: Text[150]; ExternalID: Text[2048]; ModelId: Code[30]; var NewAgentTask: Record "Agent Task") begin NewAgentTask."Agent User Security ID" := AgentUserSecurityID; NewAgentTask."Created By" := UserSecurityId(); @@ -62,6 +62,7 @@ codeunit 4300 "Agent Task Impl." NewAgentTask."Needs Attention" := false; NewAgentTask.Status := NewAgentTask.Status::Paused; NewAgentTask."External ID" := ExternalID; + NewAgentTask."Model ID" := ModelId; NewAgentTask.Insert(); end; @@ -176,6 +177,24 @@ codeunit 4300 "Agent Task Impl." exit(false); end; + procedure GetModelId(TaskId: BigInteger): Code[30] + var + AgentTaskRecord: Record "Agent Task"; + begin + AgentTaskRecord.Get(TaskId); + exit(AgentTaskRecord."Model ID") + end; + + procedure GetModelName(TaskId: BigInteger): Text[70] + var + AgentTaskRecord: Record "Agent Task"; + begin + AgentTaskRecord.Get(TaskId); + AgentTaskRecord.CalcFields("Model Name"); + exit(AgentTaskRecord."Model Name"); + end; + + [EventSubscriber(ObjectType::Codeunit, Codeunit::"System Action Triggers", GetAgentTaskMessagePageId, '', true, true)] local procedure OnGetAgentTaskMessagePageId(var PageId: Integer) begin