Skip to content

Commit

Permalink
Agents - Introduce agent system app (#2459)
Browse files Browse the repository at this point in the history
#### Summary Introduce Agents System app

#### Work Item(s) 
Fixes
[AB#559557](https://dynamicssmb2.visualstudio.com/1fcb79e7-ab07-432a-a3c6-6cf5a88ba4a5/_workitems/edit/559557)
  • Loading branch information
nikolakukrika authored Dec 4, 2024
1 parent f316f0d commit 26af2ea
Show file tree
Hide file tree
Showing 21 changed files with 2,774 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// ------------------------------------------------------------------------------------------------
// 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;

codeunit 4307 "Agent Message"
{
InherentEntitlements = X;
InherentPermissions = X;

/// <summary>
/// Get the message text for the given agent task message.
/// </summary>
/// <param name="AgentTaskMessage">Agent task message.</param>
/// <returns>The body of the agent task message.</returns>
[Scope('OnPrem')]
procedure GetText(var AgentTaskMessage: Record "Agent Task Message"): Text
var
AgentMessageImpl: Codeunit "Agent Message Impl.";
begin
exit(AgentMessageImpl.GetMessageText(AgentTaskMessage));
end;

/// <summary>
/// Updates the message text.
/// </summary>
/// <param name="AgentTaskMessage">The message record to update.</param>
/// <param name="NewMessageText">New message text to set.</param>
[Scope('OnPrem')]
procedure UpdateText(var AgentTaskMessage: Record "Agent Task Message"; NewMessageText: Text)
var
AgentMessageImpl: Codeunit "Agent Message Impl.";
begin
AgentMessageImpl.UpdateText(AgentTaskMessage, NewMessageText);
end;

/// <summary>
/// Check if it is possible to edit the message.
/// </summary>
/// <param name="AgentTaskMessage">Agent task message to verify.</param>
/// <returns>If it is possible to change the message.</returns>
[Scope('OnPrem')]
procedure IsEditable(var AgentTaskMessage: Record "Agent Task Message"): Boolean
var
AgentMessageImpl: Codeunit "Agent Message Impl.";
begin
exit(AgentMessageImpl.IsMessageEditable(AgentTaskMessage));
end;

/// <summary>
/// Sets the message status to sent.
/// </summary>
/// <param name="AgentTaskMessage">Agent task message to update status.</param>
[Scope('OnPrem')]
procedure SetStatusToSent(var AgentTaskMessage: Record "Agent Task Message")
var
AgentMessageImpl: Codeunit "Agent Message Impl.";
begin
AgentMessageImpl.SetStatusToSent(AgentTaskMessage);
end;

/// <summary>
/// Downloads the attachments for a specific message.
/// </summary>
/// <param name="AgentTaskMessage">Message to download attachments for.</param>
[Scope('OnPrem')]
procedure DownloadAttachments(var AgentTaskMessage: Record "Agent Task Message")
var
AgentMessageImpl: Codeunit "Agent Message Impl.";
begin
AgentMessageImpl.DownloadAttachments(AgentTaskMessage);
end;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// ------------------------------------------------------------------------------------------------
// 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;

codeunit 4308 "Agent Message Impl."
{
Access = Internal;
InherentEntitlements = X;
InherentPermissions = X;

procedure GetMessageText(var AgentTaskMessage: Record "Agent Task Message"): Text
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
ContentInStream: InStream;
ContentText: Text;
begin
AgentTaskMessage.CalcFields(Content);
AgentTaskMessage.Content.CreateInStream(ContentInStream, AgentTaskImpl.GetDefaultEncoding());
ContentInStream.Read(ContentText);
exit(ContentText);
end;

procedure UpdateText(var AgentTaskMessage: Record "Agent Task Message"; NewMessageText: Text)
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
ContentOutStream: OutStream;
begin
Clear(AgentTaskMessage.Content);
AgentTaskMessage.Content.CreateOutStream(ContentOutStream, AgentTaskImpl.GetDefaultEncoding());
ContentOutStream.Write(NewMessageText);
AgentTaskMessage.Modify(true);
end;

procedure IsMessageEditable(var AgentTaskMessage: Record "Agent Task Message"): Boolean
begin
if AgentTaskMessage.Type <> AgentTaskMessage.Type::Output then
exit(false);

exit(AgentTaskMessage.Status = AgentTaskMessage.Status::Draft);
end;

procedure SetStatusToSent(var AgentTaskMessage: Record "Agent Task Message")
begin
UpdateAgentTaskMessageStatus(AgentTaskMessage, AgentTaskMessage.Status::Sent);
end;

procedure DownloadAttachments(var AgentTaskMessage: Record "Agent Task Message")
var
AgentTaskFile: Record "Agent Task File";
AgentTaskMessageAttachment: Record "Agent Task Message Attachment";
AgentTaskImpl: Codeunit "Agent Task Impl.";
InStream: InStream;
FileName: Text;
DownloadDialogTitleLbl: Label 'Download Email Attachment';
begin
AgentTaskMessageAttachment.SetRange("Task ID", AgentTaskMessage."Task ID");
AgentTaskMessageAttachment.SetRange("Message ID", AgentTaskMessage.ID);
if not AgentTaskMessageAttachment.FindSet() then
exit;

repeat
if not AgentTaskFile.Get(AgentTaskMessageAttachment."Task ID", AgentTaskMessageAttachment."File ID") then
exit;

FileName := AgentTaskFile."File Name";
AgentTaskFile.CalcFields(Content);
AgentTaskFile.Content.CreateInStream(InStream, AgentTaskImpl.GetDefaultEncoding());
File.DownloadFromStream(InStream, DownloadDialogTitleLbl, '', '', FileName);
until AgentTaskMessageAttachment.Next() = 0;
end;

procedure UpdateAgentTaskMessageStatus(var AgentTaskMessage: Record "Agent Task Message"; Status: Option)
begin
AgentTaskMessage.Status := Status;
AgentTaskMessage.Modify(true);
end;
}
41 changes: 41 additions & 0 deletions src/System Application/App/Agent/Interaction/AgentTask.Codeunit.al
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// ------------------------------------------------------------------------------------------------
// 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;

codeunit 4303 "Agent Task"
{
InherentEntitlements = X;
InherentPermissions = X;

/// <summary>
/// Check if a task exists for the given agent user and conversation
/// </summary>
/// <param name="AgentUserSecurityID">The user security ID of the agent.</param>
/// <param name="ConversationId">The conversation ID to check.</param>
/// <returns>True if task exists, false if not.</returns>
[Scope('OnPrem')]
procedure TaskExists(AgentUserSecurityId: Guid; ConversationId: Text): Boolean
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
exit(AgentTaskImpl.TaskExists(AgentUserSecurityId, ConversationId));
end;

/// <summary>
/// Create a new task message for the given agent user and conversation.
/// If task does not exist, it will be created.
/// </summary>
/// <param name="From">Specifies from address.</param>
/// <param name="MessageText">The message text for the task.</param>
/// <param name="CurrentAgentTask">Current Agent Task to which the message will be added.</param>
[Scope('OnPrem')]
procedure CreateTaskMessage(From: Text[250]; MessageText: Text; ExternalMessageId: Text[2048]; var CurrentAgentTask: Record "Agent Task")
var
AgentTaskImpl: Codeunit "Agent Task Impl.";
begin
AgentTaskImpl.CreateTaskMessage(From, MessageText, ExternalMessageId, CurrentAgentTask);
end;
}
Loading

0 comments on commit 26af2ea

Please sign in to comment.