Add new email reader utility to read emails using IMAP.

This commit is contained in:
103048 2024-07-31 14:00:11 -05:00
parent 99baebfa7b
commit a36281e2e3
15 changed files with 586 additions and 0 deletions

View file

@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<GenerateDocumentationFile>$(GenerateDocumentationFile)</GenerateDocumentationFile>
<OutputPath>$(SolutionDir)packages</OutputPath>
</PropertyGroup>
<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_reader.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_reader.fn.liquid" />
</ItemGroup>
<ItemGroup>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_reader.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_reader.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="MailKit" Version="4.7.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\onebrain\BusinessCore\BusinessCore.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,41 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Repositories.Enums;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Settings;
using BotSharp.Core.Repository;
using BotSharp.Plugin.EmailReader.Hooks;
using BotSharp.Plugin.EmailReader.Settings;
using EntityFrameworkCore.BootKit;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.EmailReader.Providers;
namespace BotSharp.Plugin.EmailReader;
public class EmailReaderPlugin : IBotSharpPlugin
{
public string Id => "c88d27c8-127e-4aff-9cf4-74b49eec2926";
public string Name => "Email Reader";
public string Description => "Empower agent to read messages from email";
public string IconUrl => "https://cdn-icons-png.freepik.com/512/6711/6711567.png";
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
var emailReaderSettings = new EmailReaderSettings();
config.Bind("EmailReader", emailReaderSettings);
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<EmailReaderSettings>("EmailReader");
});
services.AddSingleton(provider => emailReaderSettings);
services.AddScoped<IAgentHook, EmailReaderHook>();
services.AddScoped<IAgentUtilityHook, EmailReaderUtilityHook>();
services.AddScoped<IEmailReader, DefaultEmailReader>();
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Enums;
public class UtilityName
{
public const string EmailReader = "email-reader";
}

View file

@ -0,0 +1,196 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Files;
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Core.Infrastructures;
using BotSharp.Plugin.EmailReader.LlmContexts;
using BotSharp.Plugin.EmailReader.Models;
using BotSharp.Plugin.EmailReader.Providers;
using BotSharp.Plugin.EmailReader.Settings;
using BotSharp.Plugin.EmailReader.Templates;
using BusinessCore.Utils;
using MailKit;
using MailKit.Net.Imap;
using MailKit.Search;
using MailKit.Security;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using MimeKit;
namespace BotSharp.Plugin.EmailReader.Functions;
public class HandleEmailReaderFn : IFunctionCallback
{
public string Name => "handle_email_reader";
public readonly static string PROMPT_SUMMARY = "Provide a text summary of the following content.";
public readonly static string RICH_CONTENT_SUMMARIZE = "Summarize the particular email by messageId";
public readonly static string RICH_CONTENT_READ_EMAIL = "Read the email by messageId";
public readonly static string RICH_CONTENT_MARK_READ = "Mark the email message as read by messageId";
public string Indication => "Handling email read";
private readonly IServiceProvider _services;
private readonly ILogger<HandleEmailReaderFn> _logger;
private readonly IHttpContextAccessor _context;
private readonly BotSharpOptions _options;
private readonly EmailReaderSettings _emailSettings;
private readonly IConversationStateService _state;
private readonly IEmailReader _emailProvider;
public HandleEmailReaderFn(IServiceProvider services,
ILogger<HandleEmailReaderFn> logger,
IHttpContextAccessor context,
BotSharpOptions options,
EmailReaderSettings emailPluginSettings,
IConversationStateService state,
IEmailReader emailProvider)
{
_services = services;
_logger = logger;
_context = context;
_options = options;
_emailSettings = emailPluginSettings;
_state = state;
_emailProvider = emailProvider;
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
var isMarkRead = args?.IsMarkRead ?? false;
var isSummarize = args?.IsSummarize ?? false;
var messageId = args?.MessageId;
try
{
if (!string.IsNullOrEmpty(messageId))
{
if (isMarkRead)
{
await _emailProvider.MarkEmailAsReadById(messageId);
message.Content = $"The email message has been marked as read.";
return true;
}
var emailMessage = await _emailProvider.GetEmailById(messageId);
if (isSummarize)
{
var prompt = $"{PROMPT_SUMMARY} The content was sent by {emailMessage.From.ToString()}. Details: {emailMessage.TextBody}";
var agent = new Agent
{
Id = BuiltInAgentId.UtilityAssistant,
Name = "Utility Assistant",
Instruction = prompt
};
var llmProviderService = _services.GetRequiredService<ILlmProviderService>();
var provider = llmProviderService.GetProviders().FirstOrDefault(x => x == "openai");
var model = llmProviderService.GetProviderModel(provider: provider, id: "gpt-4");
var completion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model.Name);
var convService = _services.GetService<IConversationService>();
var conversationId = convService.ConversationId;
var dialogs = convService.GetDialogHistory(fromBreakpoint: false);
var response = await completion.GetChatCompletions(agent, dialogs);
var content = response?.Content ?? string.Empty;
message.Content = content;
message.RichContent = BuildRichContent.TextPostBackRichContent(_state.GetConversationId(), message.Content);
return true;
}
UniqueId.TryParse(messageId, out UniqueId uid);
message.RichContent = BuildRichContentForEmail(emailMessage, uid.ToString());
return true;
}
var emails = await _emailProvider.GetUnreadEmails();
message.Content = "Please choose which one to read for you.";
message.RichContent = BuildRichContentForSubject(emails.OrderByDescending(x => x.CreateDate).ToList());
return true;
}
catch (Exception ex)
{
var msg = $"Failed to read the emails. {ex.Message}";
_logger.LogError($"{msg}\n(Error: {ex.Message})");
message.Content = msg;
return false;
}
}
private RichContent<IRichMessage> BuildRichContentForSubject(List<EmailModel> emailSubjects)
{
var text = "Please let me know which message I need to read?";
return new RichContent<IRichMessage>
{
FillPostback = true,
Editor = EditorTypeEnum.None,
Recipient = new Recipient
{
Id = _state.GetConversationId()
},
Message = new GenericTemplateMessage<EmailSubjectElement>
{
Text = text,
Elements = GetElements(emailSubjects)
}
};
}
private RichContent<IRichMessage> BuildRichContentForEmail(EmailModel email, string uid)
{
var text = "The email details are given below. \n";
return new RichContent<IRichMessage>
{
FillPostback = true,
Editor = EditorTypeEnum.None,
Recipient = new Recipient
{
Id = _state.GetConversationId()
},
Message = new GenericTemplateMessage<EmailSubjectElement>
{
Text = $"{text}<b>From</b>: {email.From.ToString()}\n<b>Subject</b>: {email.Subject}\n{email.Body}",
Elements = GetElements(uid)
}
};
}
private static List<EmailSubjectElement> GetElements(string uid)
{
var element = new EmailSubjectElement()
{
Buttons = new ElementButton[]
{
BuildMarkReadElementButton(uid)
}
};
return new List<EmailSubjectElement>() { element };
}
private static List<EmailSubjectElement> GetElements(List<EmailModel> emails)
{
var elements = emails.Select(e => new EmailSubjectElement
{
Title = $"Subject: {e.Subject}",
Subtitle = $"From: {e.From}<br/> Date: {e.CreateDate}",
Buttons = BuildElementButton(e)
}).ToList();
return elements;
}
private static ElementButton[] BuildElementButton(EmailModel email)
{
var elements = new List<ElementButton>() { };
elements.Add(new ElementButton
{
Title = "Read",
Payload = $"{RICH_CONTENT_READ_EMAIL}: {email.UId}.",
Type = "text",
});
elements.Add(new ElementButton
{
Title = "Summarize",
Payload = $"{RICH_CONTENT_SUMMARIZE}: {email.UId}.",
Type = "text",
});
return elements.ToArray();
}
private static ElementButton BuildMarkReadElementButton(string uId)
{
return new ElementButton
{
Title = "Mark as read",
Payload = $"{RICH_CONTENT_MARK_READ}: {uId}.",
Type = "text",
};
}
}

View file

@ -0,0 +1,63 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Settings;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Plugin.EmailReader.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Hooks;
public class EmailReaderHook : AgentHookBase
{
private static string FUNCTION_NAME = "handle_email_reader";
public override string SelfId => string.Empty;
public EmailReaderHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
}
public override void OnAgentLoaded(Agent agent)
{
var conv = _services.GetRequiredService<IConversationService>();
var isConvMode = conv.IsConversationMode();
var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.EmailReader);
if (isConvMode && isEnabled)
{
var (prompt, fn) = GetPromptAndFunction();
if (fn != null)
{
if (!string.IsNullOrWhiteSpace(prompt))
{
agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n";
}
if (agent.Functions == null)
{
agent.Functions = new List<FunctionDef> { fn };
}
else
{
agent.Functions.Add(fn);
}
}
}
base.OnAgentLoaded(agent);
}
private (string, FunctionDef?) GetPromptAndFunction()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant);
var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{FUNCTION_NAME}.fn"))?.Content ?? string.Empty;
var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(FUNCTION_NAME));
return (prompt, loadAttachmentFn);
}
}

View file

@ -0,0 +1,17 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Plugin.EmailReader.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Hooks;
public class EmailReaderUtilityHook : IAgentUtilityHook
{
public void AddUtilities(List<string> utilities)
{
utilities.Add(UtilityName.EmailReader);
}
}

View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.LlmContexts;
public class LlmContextIn
{
[JsonPropertyName("mark_as_read")]
public bool? IsMarkRead { get; set; }
[JsonPropertyName("message_id")]
public string? MessageId { get; set; }
[JsonPropertyName("is_email_summarize")]
public bool? IsSummarize { get; set; }
}

View file

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Models;
public class EmailModel
{
public DateTime CreateDate { get; set; }
public string Subject { get; set; }
public string UId { get; set; }
public string From { get; set; }
public string Body { get; set; }
public string TextBody { get; set; }
}

View file

@ -0,0 +1,98 @@
using BotSharp.Plugin.EmailReader.Models;
using BotSharp.Plugin.EmailReader.Settings;
using MailKit;
using MailKit.Net.Imap;
using MailKit.Search;
using MailKit.Security;
using System.Text.RegularExpressions;
namespace BotSharp.Plugin.EmailReader.Providers
{
public class DefaultEmailReader : IEmailReader
{
public EmailReaderSettings _emailReaderSettings;
public const int MAX_UNREAD_COUNT = 5;
public DefaultEmailReader(EmailReaderSettings emailReaderSettings)
{
_emailReaderSettings = emailReaderSettings;
}
public async Task<ImapClient> GetImapClient()
{
var client = new ImapClient();
await client.ConnectAsync(_emailReaderSettings.IMAPServer, _emailReaderSettings.IMAPPort, SecureSocketOptions.SslOnConnect);
await client.AuthenticateAsync(_emailReaderSettings.Username, _emailReaderSettings.Password);
return client;
}
public async Task<List<EmailModel>> GetUnreadEmails()
{
var emails = new List<EmailModel>();
using var client = await GetImapClient();
await client.Inbox.OpenAsync(FolderAccess.ReadOnly);
var query = SearchQuery.NotSeen;
var result = await client.Inbox.SearchAsync(query);
var uIds = result.TakeLast(MAX_UNREAD_COUNT);
foreach (var uid in uIds)
{
var inboxMsg = await client.Inbox.GetMessageAsync(uid);
emails.Add(new EmailModel()
{
Subject = inboxMsg.Subject,
CreateDate = inboxMsg.Date.UtcDateTime,
From = FormatEmailAddress(inboxMsg.From.ToString()),
UId = uid.ToString()
});
}
await client.DisconnectAsync(true);
return emails;
}
public string FormatEmailAddress(string emailAddress)
{
string pattern = "\"([^\"]+)\"\\s*<([^>]+)>";
var match = Regex.Match(emailAddress, pattern);
if (match.Success)
{
string name = match.Groups[1].Value;
string email = match.Groups[2].Value;
string result = $"{name} {email}";
return result;
}
return emailAddress;
}
public async Task<EmailModel> GetEmailById(string id)
{
UniqueId.TryParse(id, out UniqueId uid);
using var client = await GetImapClient();
await client.Inbox.OpenAsync(FolderAccess.ReadOnly);
var message = await client.Inbox.GetMessageAsync(uid);
return new EmailModel()
{
CreateDate = message.Date.UtcDateTime,
From = FormatEmailAddress(message.From.ToString()),
Subject = message.Subject,
UId = uid.ToString(),
Body = message.HtmlBody,
TextBody = message.TextBody
};
}
public async Task<bool> MarkEmailAsReadById(string id)
{
try
{
UniqueId.TryParse(id, out UniqueId uid);
using var client = await GetImapClient();
await client.Inbox.OpenAsync(FolderAccess.ReadWrite);
await client.Inbox.AddFlagsAsync(uid, MessageFlags.Seen, true);
await client.DisconnectAsync(true);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}

View file

@ -0,0 +1,16 @@
using BotSharp.Plugin.EmailReader.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Providers
{
public interface IEmailReader
{
public Task<List<EmailModel>> GetUnreadEmails();
Task<EmailModel> GetEmailById(string id);
Task<bool> MarkEmailAsReadById(string id);
}
}

View file

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Settings;
public class EmailReaderSettings
{
public string Username { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string IMAPServer { get; set; } = string.Empty;
public int IMAPPort { get; set; }
}

View file

@ -0,0 +1,14 @@
using BotSharp.Abstraction.Messaging.Models.RichContent.Template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Templates
{
public class EmailSubjectElement : GenericElement
{
public string Subject { get; set; }
}
}

View file

@ -0,0 +1,18 @@
global using System;
global using System.Collections.Generic;
global using System.Text;
global using BotSharp.Abstraction.Conversations;
global using BotSharp.Abstraction.Plugins;
global using System.Text.Json;
global using BotSharp.Abstraction.Conversations.Models;
global using System.Threading.Tasks;
global using BotSharp.Abstraction.Functions;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.Templating;
global using Microsoft.Extensions.DependencyInjection;
global using System.Linq;
global using BotSharp.Abstraction.Utilities;
global using BotSharp.Abstraction.Messaging;
global using BotSharp.Abstraction.Messaging.Models.RichContent;
global using BotSharp.Abstraction.Options;
global using BotSharp.Abstraction.Messaging.Enums;

View file

@ -0,0 +1,22 @@
{
"name": "handle_email_reader",
"description": "If the user wants to read messages from email inbox or user wants to mark an email message as read.If message id is provided, capture it but it is not required. Then call this function to read the email message or mark any message as read.",
"parameters": {
"type": "object",
"properties": {
"mark_as_read": {
"to_read": "boolean",
"description": "Mark the boolean as true if the user wants to mark the message as read."
},
"is_email_summarize": {
"to_read": "boolean",
"description": "Mark the boolean as true only if the user explicitly mentions that they want to summarize the particular message."
},
"message_Id": {
"type": "string",
"description": "The message id of a particular email message."
}
},
"required": [ "mark_as_read", "is_email_summarize" ]
}
}

View file

@ -0,0 +1 @@
Please call handle_email_reader if user wants to read messages from email inbox.