// ------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// ------------------------------------------------------------------------------------------------
namespace System.Agents;
using System.Environment;
codeunit 4303 "Agent Task"
{
InherentEntitlements = X;
InherentPermissions = X;
///
/// Check if a task exists for the given agent user and conversation
///
/// The user security ID of the agent.
/// The external ID to check.
/// True if task exists, false if not.
procedure TaskExists(AgentUserSecurityId: Guid; ExternalId: Text): Boolean
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
exit(AgentTaskImpl.TaskExists(AgentUserSecurityId, ExternalId));
end;
///
/// Get the task for the given agent user and external ID.
///
/// The agent user ID.
/// The external ID of the task.
/// A record with the given task.
procedure GetTaskByExternalId(AgentUserSecurityId: Guid; ExternalId: Text): Record "Agent Task"
var
AgentTask: Record "Agent Task";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
AgentTask.SetRange("Agent User Security ID", AgentUserSecurityId);
AgentTask.SetRange("External ID", ExternalId);
AgentTask.FindFirst();
exit(AgentTask);
end;
///
/// Set the status of the task to ready if the task is in the state that it can be started again.
/// The agent task will be be picked up for processing shortly after updating the status.
///
/// The agent task to set to ready.
/// The agent task with the status set to ready.
procedure SetStatusToReady(var AgentTask: Record "Agent Task")
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
AgentTaskImpl.SetTaskStatusToReadyIfPossible(AgentTask);
end;
///
/// Checks if the task can be set to ready and started again.
///
/// The agent task to check.
/// True if agent task can be set to ready, false otherwise
procedure CanSetStatusToReady(AgentTask: Record "Agent Task"): Boolean
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
exit(AgentTaskImpl.CanAgentTaskBeSetToReady(AgentTask));
end;
///
/// Stops the agent task.
///
/// The agent task to stop.
/// Whether to show a confirmation dialog to the user.
procedure StopTask(var AgentTask: Record "Agent Task"; UserConfirm: Boolean)
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
TaskStatus: Enum "Agent Task Status";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
AgentTaskImpl.StopTask(AgentTask, TaskStatus::"Stopped by User", UserConfirm);
end;
///
/// Restarts the agent task by setting its status to ready.
///
/// The agent task to restart.
/// Whether to show a confirmation dialog to the user.
procedure RestartTask(var AgentTask: Record "Agent Task"; UserConfirm: Boolean)
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
AgentTaskImpl.RestartTask(AgentTask, UserConfirm);
end;
///
/// Checks if the agent task is currently running.
///
/// The agent task to check.
/// True if the task is running, false otherwise.
procedure IsTaskRunning(var AgentTask: Record "Agent Task"): Boolean
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
exit(AgentTaskImpl.IsTaskRunning(AgentTask));
end;
///
/// Checks if the agent task is completed.
///
/// The agent task to check.
/// True if the task is completed, false otherwise.
procedure IsTaskCompleted(var AgentTask: Record "Agent Task"): Boolean
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
exit(AgentTaskImpl.IsTaskCompleted(AgentTask));
end;
///
/// Checks if the agent task is stopped (by user or system).
///
/// The agent task to check.
/// True if the task is stopped, false otherwise.
procedure IsTaskStopped(var AgentTask: Record "Agent Task"): Boolean
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
exit(AgentTaskImpl.IsTaskStopped(AgentTask));
end;
#if not CLEAN29
///
/// Gets the total Copilot credits consumed by the agent task.
///
/// The ID of the agent task to get consumed credits for.
/// The total Copilot credits consumed by the agent task.
[Obsolete('Use the methods in the "Agent Consumption Overview" codeunit instead.', '29.0')]
procedure GetCopilotCreditsConsumed(AgentTaskID: BigInteger): Decimal
var
AgentConsumptionOverview: Codeunit "Agent Consumption Overview";
begin
exit(AgentConsumptionOverview.GetCopilotCreditsConsumed(AgentTaskID));
end;
#endif
///
/// Gets the details for the specified agent task log entry.
///
/// The agent task log entry to get details for.
/// The details of the agent task log entry.
[Scope('OnPrem')]
procedure GetLogEntryDetails(var AgentTaskLogEntry: Record "Agent Task Log Entry"): Text
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
exit(AgentTaskImpl.GetDetailsForAgentTaskLogEntry(AgentTaskLogEntry));
end;
///
/// Archives the agent task.
///
/// The ID of the agent task to archive.
/// Whether to show a confirmation dialog to the user.
[Scope('OnPrem')]
procedure ArchiveTask(AgentTaskID: BigInteger; UserConfirm: Boolean)
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
FeatureAccessManagement.AgentManagementAllowed(true);
AgentTaskImpl.ArchiveTask(AgentTaskID, UserConfirm);
end;
var
FeatureAccessManagement: Codeunit "Feature Access Management";
}