Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new email reader utility to read emails using IMAP. #568

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@
</PropertyGroup>

<ItemGroup>
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_request.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_request.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_reader.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_sender.json" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_sender.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_reader.fn.liquid" />
<None Remove="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_attachment_prompt.liquid" />
</ItemGroup>

<ItemGroup>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_request.json">
<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_request.fn.liquid">
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\functions\handle_email_sender.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\handle_email_sender.fn.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\6745151e-6d46-4a02-8de4-1c4f21c7da95\templates\select_attachment_prompt.liquid">
<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>
Expand Down
16 changes: 14 additions & 2 deletions src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Settings;
using BotSharp.Plugin.EmailHandler.Providers;
using Microsoft.Extensions.Configuration;

namespace BotSharp.Plugin.EmailHandler
Expand All @@ -16,11 +17,22 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
services.AddScoped(provider =>
{
var settingService = provider.GetRequiredService<ISettingService>();
return settingService.Bind<EmailHandlerSettings>("EmailHandler");
return settingService.Bind<EmailSenderSettings>("EmailSender");
});

services.AddScoped<IAgentHook, EmailHandlerHook>();
services.AddScoped<IAgentHook, EmailSenderHook>();
services.AddScoped<IAgentHook, EmailReaderHook>();
services.AddScoped<IAgentUtilityHook, EmailHandlerUtilityHook>();

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<IEmailReader, DefaultEmailReader>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
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.EmailHandler.Models;
using BotSharp.Plugin.EmailHandler.Providers;
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 = "is_email_summarize: true. messageId";
public readonly static string RICH_CONTENT_READ_EMAIL = "Read the email by messageId";
public readonly static string RICH_CONTENT_MARK_READ = "mark_as_read: true. 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<LlmContextReader>(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}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The prompt should be placed under Utility Agent templates folder. This will allow user to updated the prompt without updating code.

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 = BuildRichContentForSummary(_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;
}
}
public RichContent<IRichMessage> BuildRichContentForSummary(string conversationId, string content, string editorType = EditorTypeEnum.Text)
{
return new RichContent<IRichMessage>()
{
FillPostback = true,
Editor = editorType,
Recipient = new Recipient() { Id = conversationId },
Message = new ButtonTemplateMessage()
{
Text = content,
}
};
}
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",
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

namespace BotSharp.Plugin.EmailHandler.Functions;

public class HandleEmailRequestFn : IFunctionCallback
public class HandleEmailSenderFn : IFunctionCallback
{
public string Name => "handle_email_request";
public string Indication => "Handling email request";
public string Name => "handle_email_sender";
public string Indication => "Handling email send request";

private readonly IServiceProvider _services;
private readonly ILogger<HandleEmailRequestFn> _logger;
private readonly ILogger<HandleEmailSenderFn> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IHttpContextAccessor _context;
private readonly BotSharpOptions _options;
private readonly EmailHandlerSettings _emailSettings;
private readonly EmailSenderSettings _emailSettings;

public HandleEmailRequestFn(
public HandleEmailSenderFn(
IServiceProvider services,
ILogger<HandleEmailRequestFn> logger,
ILogger<HandleEmailSenderFn> logger,
IHttpClientFactory httpClientFactory,
IHttpContextAccessor context,
BotSharpOptions options,
EmailHandlerSettings emailPluginSettings)
EmailSenderSettings emailPluginSettings)
{
_services = services;
_logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@

namespace BotSharp.Plugin.EmailHandler.Hooks;

public class EmailHandlerHook : AgentHookBase
public class EmailReaderHook : AgentHookBase
{
private static string FUNCTION_NAME = "handle_email_request";
private static string FUNCTION_NAME = "handle_email_reader";

public override string SelfId => string.Empty;

public EmailHandlerHook(IServiceProvider services, AgentSettings settings)
public EmailReaderHook(IServiceProvider services, AgentSettings settings)
: base(services, settings)
{
}
Expand Down
63 changes: 63 additions & 0 deletions src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailSenderHook.cs
Original file line number Diff line number Diff line change
@@ -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.EmailHandler.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BotSharp.Plugin.EmailHandler.Hooks;

public class EmailSenderHook : AgentHookBase
{
private static string FUNCTION_NAME = "handle_email_sender";

public override string SelfId => string.Empty;

public EmailSenderHook(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.EmailHandler);

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);
}
}
Loading