From a36281e2e3a7e3ebab244eeb14c2f715d3963ef5 Mon Sep 17 00:00:00 2001 From: 103048 <103048@MJ0AJ7XO-LNPC> Date: Wed, 31 Jul 2024 14:00:11 -0500 Subject: [PATCH 1/3] Add new email reader utility to read emails using IMAP. --- .../BotSharp.Plugin.EmailReader.csproj | 37 ++++ .../EmailReaderPlugin.cs | 41 ++++ .../Enums/UtilityName.cs | 12 ++ .../Functions/HandleEmailReaderFn.cs | 196 ++++++++++++++++++ .../Hooks/EmailReaderHook.cs | 63 ++++++ .../Hooks/EmailReaderUtilityHook.cs | 17 ++ .../LlmContexts/LlmContextIn.cs | 18 ++ .../Models/EmailModel.cs | 18 ++ .../Providers/DefaultEmailReader.cs | 98 +++++++++ .../Providers/IEmailReader.cs | 16 ++ .../Settings/EmailReaderSettings.cs | 15 ++ .../Templates/EmailSubjectElement.cs | 14 ++ .../BotSharp.Plugin.EmailReader/Using.cs | 18 ++ .../functions/handle_email_reader.json | 22 ++ .../templates/handle_email_reader.fn.liquid | 1 + 15 files changed, 586 insertions(+) create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Functions/HandleEmailReaderFn.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderHook.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Providers/DefaultEmailReader.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Providers/IEmailReader.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Settings/EmailReaderSettings.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Templates/EmailSubjectElement.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Using.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json create mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj b/src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj new file mode 100644 index 000000000..35c1a1db4 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj @@ -0,0 +1,37 @@ + + + + $(TargetFramework) + enable + $(LangVersion) + $(BotSharpVersion) + $(GeneratePackageOnBuild) + $(GenerateDocumentationFile) + $(SolutionDir)packages + + + + + + + + + + PreserveNewest + + + PreserveNewest + + + + + + + + + + + + + + diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs b/src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs new file mode 100644 index 000000000..a88abc66b --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs @@ -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(); + return settingService.Bind("EmailReader"); + }); + services.AddSingleton(provider => emailReaderSettings); + services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs new file mode 100644 index 000000000..2d0042e04 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs @@ -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"; +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Functions/HandleEmailReaderFn.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Functions/HandleEmailReaderFn.cs new file mode 100644 index 000000000..4c23c0e8f --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Functions/HandleEmailReaderFn.cs @@ -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 _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 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 Execute(RoleDialogModel message) + { + var args = JsonSerializer.Deserialize(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(); + 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(); + 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 BuildRichContentForSubject(List emailSubjects) + { + var text = "Please let me know which message I need to read?"; + + return new RichContent + { + FillPostback = true, + Editor = EditorTypeEnum.None, + Recipient = new Recipient + { + Id = _state.GetConversationId() + }, + Message = new GenericTemplateMessage + { + Text = text, + Elements = GetElements(emailSubjects) + } + }; + } + private RichContent BuildRichContentForEmail(EmailModel email, string uid) + { + var text = "The email details are given below. \n"; + + return new RichContent + { + FillPostback = true, + Editor = EditorTypeEnum.None, + Recipient = new Recipient + { + Id = _state.GetConversationId() + }, + Message = new GenericTemplateMessage + { + Text = $"{text}From: {email.From.ToString()}\nSubject: {email.Subject}\n{email.Body}", + Elements = GetElements(uid) + } + }; + } + private static List GetElements(string uid) + { + var element = new EmailSubjectElement() + { + Buttons = new ElementButton[] + { + BuildMarkReadElementButton(uid) + } + }; + return new List() { element }; + } + private static List GetElements(List emails) + { + var elements = emails.Select(e => new EmailSubjectElement + { + Title = $"Subject: {e.Subject}", + Subtitle = $"From: {e.From}
Date: {e.CreateDate}", + Buttons = BuildElementButton(e) + }).ToList(); + return elements; + } + private static ElementButton[] BuildElementButton(EmailModel email) + { + var elements = new List() { }; + 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", + }; + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderHook.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderHook.cs new file mode 100644 index 000000000..b1222eaa0 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderHook.cs @@ -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(); + 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 { fn }; + } + else + { + agent.Functions.Add(fn); + } + } + } + + base.OnAgentLoaded(agent); + } + + private (string, FunctionDef?) GetPromptAndFunction() + { + var db = _services.GetRequiredService(); + 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); + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs new file mode 100644 index 000000000..cc85bfcf6 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs @@ -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 utilities) + { + utilities.Add(UtilityName.EmailReader); + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs b/src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs new file mode 100644 index 000000000..4305f674b --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs @@ -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; } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs new file mode 100644 index 000000000..9136d465d --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs @@ -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; } + +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Providers/DefaultEmailReader.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Providers/DefaultEmailReader.cs new file mode 100644 index 000000000..8af414e24 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Providers/DefaultEmailReader.cs @@ -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 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> GetUnreadEmails() + { + var emails = new List(); + 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 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 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; + } + } + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Providers/IEmailReader.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Providers/IEmailReader.cs new file mode 100644 index 000000000..653c01afc --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Providers/IEmailReader.cs @@ -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> GetUnreadEmails(); + Task GetEmailById(string id); + Task MarkEmailAsReadById(string id); + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Settings/EmailReaderSettings.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Settings/EmailReaderSettings.cs new file mode 100644 index 000000000..f2b87aef8 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Settings/EmailReaderSettings.cs @@ -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; } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Templates/EmailSubjectElement.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Templates/EmailSubjectElement.cs new file mode 100644 index 000000000..b3eb583ad --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Templates/EmailSubjectElement.cs @@ -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; } + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Using.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Using.cs new file mode 100644 index 000000000..80d160dd5 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/Using.cs @@ -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; \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json b/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json new file mode 100644 index 000000000..bcea2ac8e --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json @@ -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" ] + } +} \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid b/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid new file mode 100644 index 000000000..0efb6229d --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid @@ -0,0 +1 @@ +Please call handle_email_reader if user wants to read messages from email inbox. \ No newline at end of file From 6c6ff566a55ec6b178afa7ca35a5dce92f294e05 Mon Sep 17 00:00:00 2001 From: 103048 <103048@MJ0AJ7XO-LNPC> Date: Wed, 31 Jul 2024 15:46:14 -0500 Subject: [PATCH 2/3] Update email handler to include email read capabilities. Rename to email sender --- .../BotSharp.Plugin.EmailHandler.csproj | 13 ++++-- .../EmailHandlerPlugin.cs | 16 +++++++- .../Functions/HandleEmailReaderFn.cs | 13 +++--- ...ailRequestFn.cs => HandleEmailSenderFn.cs} | 16 ++++---- .../Hooks/EmailReaderHook.cs | 6 +-- ...EmailHandlerHook.cs => EmailSenderHook.cs} | 6 +-- .../LlmContexts/LlmContextReader.cs | 19 +++++++++ .../Models/EmailModel.cs | 19 +++++++++ .../Models}/EmailSubjectElement.cs | 2 +- .../Providers/DefaultEmailReader.cs | 10 +++-- .../Providers/IEmailReader.cs | 4 +- .../Settings/EmailReaderSettings.cs | 2 +- ...dlerSettings.cs => EmailSenderSettings.cs} | 2 +- .../functions/handle_email_reader.json | 0 ..._request.json => handle_email_sender.json} | 2 +- .../templates/handle_email_reader.fn.liquid | 0 .../templates/handle_email_request.fn.liquid | 2 - .../templates/handle_email_sender.fn.liquid | 2 + .../BotSharp.Plugin.EmailReader.csproj | 37 ----------------- .../EmailReaderPlugin.cs | 41 ------------------- .../Enums/UtilityName.cs | 12 ------ .../Hooks/EmailReaderUtilityHook.cs | 17 -------- .../LlmContexts/LlmContextIn.cs | 18 -------- .../Models/EmailModel.cs | 18 -------- .../BotSharp.Plugin.EmailReader/Using.cs | 18 -------- 25 files changed, 95 insertions(+), 200 deletions(-) rename src/Plugins/{BotSharp.Plugin.EmailReader => BotSharp.Plugin.EmailHandler}/Functions/HandleEmailReaderFn.cs (93%) rename src/Plugins/BotSharp.Plugin.EmailHandler/Functions/{HandleEmailRequestFn.cs => HandleEmailSenderFn.cs} (93%) rename src/Plugins/{BotSharp.Plugin.EmailReader => BotSharp.Plugin.EmailHandler}/Hooks/EmailReaderHook.cs (93%) rename src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/{EmailHandlerHook.cs => EmailSenderHook.cs} (90%) create mode 100644 src/Plugins/BotSharp.Plugin.EmailHandler/LlmContexts/LlmContextReader.cs create mode 100644 src/Plugins/BotSharp.Plugin.EmailHandler/Models/EmailModel.cs rename src/Plugins/{BotSharp.Plugin.EmailReader/Templates => BotSharp.Plugin.EmailHandler/Models}/EmailSubjectElement.cs (86%) rename src/Plugins/{BotSharp.Plugin.EmailReader => BotSharp.Plugin.EmailHandler}/Providers/DefaultEmailReader.cs (94%) rename src/Plugins/{BotSharp.Plugin.EmailReader => BotSharp.Plugin.EmailHandler}/Providers/IEmailReader.cs (77%) rename src/Plugins/{BotSharp.Plugin.EmailReader => BotSharp.Plugin.EmailHandler}/Settings/EmailReaderSettings.cs (88%) rename src/Plugins/BotSharp.Plugin.EmailHandler/Settings/{EmailHandlerSettings.cs => EmailSenderSettings.cs} (91%) rename src/Plugins/{BotSharp.Plugin.EmailReader => BotSharp.Plugin.EmailHandler}/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json (100%) rename src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/{handle_email_request.json => handle_email_sender.json} (97%) rename src/Plugins/{BotSharp.Plugin.EmailReader => BotSharp.Plugin.EmailHandler}/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid (100%) delete mode 100644 src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_request.fn.liquid create mode 100644 src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_sender.fn.liquid delete mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj delete mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs delete mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs delete mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs delete mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs delete mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs delete mode 100644 src/Plugins/BotSharp.Plugin.EmailReader/Using.cs diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj b/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj index 6323706ff..fb241175c 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj @@ -11,16 +11,20 @@ - - + + + - + PreserveNewest - + + PreserveNewest + + PreserveNewest @@ -33,6 +37,7 @@ + diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs index 5063093b9..5d7ff586c 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs @@ -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 @@ -16,11 +17,22 @@ public void RegisterDI(IServiceCollection services, IConfiguration config) services.AddScoped(provider => { var settingService = provider.GetRequiredService(); - return settingService.Bind("EmailHandler"); + return settingService.Bind("EmailSender"); }); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); services.AddScoped(); + + var emailReaderSettings = new EmailReaderSettings(); + config.Bind("EmailReader", emailReaderSettings); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("EmailReader"); + }); + services.AddSingleton(provider => emailReaderSettings); + services.AddScoped(); } } } diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Functions/HandleEmailReaderFn.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs similarity index 93% rename from src/Plugins/BotSharp.Plugin.EmailReader/Functions/HandleEmailReaderFn.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs index 4c23c0e8f..964ce0288 100644 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Functions/HandleEmailReaderFn.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs @@ -3,11 +3,8 @@ 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 BotSharp.Plugin.EmailHandler.Models; +using BotSharp.Plugin.EmailHandler.Providers; using BusinessCore.Utils; using MailKit; using MailKit.Net.Imap; @@ -23,9 +20,9 @@ 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_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 the email message as read 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 _logger; @@ -53,7 +50,7 @@ public HandleEmailReaderFn(IServiceProvider services, } public async Task Execute(RoleDialogModel message) { - var args = JsonSerializer.Deserialize(message.FunctionArgs, _options.JsonSerializerOptions); + var args = JsonSerializer.Deserialize(message.FunctionArgs, _options.JsonSerializerOptions); var isMarkRead = args?.IsMarkRead ?? false; var isSummarize = args?.IsSummarize ?? false; var messageId = args?.MessageId; diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailRequestFn.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailSenderFn.cs similarity index 93% rename from src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailRequestFn.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailSenderFn.cs index 9f8406a8b..45dc65beb 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailRequestFn.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailSenderFn.cs @@ -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 _logger; + private readonly ILogger _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 logger, + ILogger logger, IHttpClientFactory httpClientFactory, IHttpContextAccessor context, BotSharpOptions options, - EmailHandlerSettings emailPluginSettings) + EmailSenderSettings emailPluginSettings) { _services = services; _logger = logger; diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderHook.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailReaderHook.cs similarity index 93% rename from src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderHook.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailReaderHook.cs index b1222eaa0..222c27b91 100644 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderHook.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailReaderHook.cs @@ -3,14 +3,14 @@ using BotSharp.Abstraction.Agents.Settings; using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories; -using BotSharp.Plugin.EmailReader.Enums; +using BotSharp.Plugin.EmailHandler.Enums; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace BotSharp.Plugin.EmailReader.Hooks; +namespace BotSharp.Plugin.EmailHandler.Hooks; public class EmailReaderHook : AgentHookBase { @@ -26,7 +26,7 @@ public override void OnAgentLoaded(Agent agent) { var conv = _services.GetRequiredService(); var isConvMode = conv.IsConversationMode(); - var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.EmailReader); + var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.EmailHandler); if (isConvMode && isEnabled) { diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerHook.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailSenderHook.cs similarity index 90% rename from src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerHook.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailSenderHook.cs index 59d7baf99..d4ae8c16a 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerHook.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailSenderHook.cs @@ -12,13 +12,13 @@ namespace BotSharp.Plugin.EmailHandler.Hooks; -public class EmailHandlerHook : AgentHookBase +public class EmailSenderHook : AgentHookBase { - private static string FUNCTION_NAME = "handle_email_request"; + private static string FUNCTION_NAME = "handle_email_sender"; public override string SelfId => string.Empty; - public EmailHandlerHook(IServiceProvider services, AgentSettings settings) + public EmailSenderHook(IServiceProvider services, AgentSettings settings) : base(services, settings) { } diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/LlmContexts/LlmContextReader.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/LlmContexts/LlmContextReader.cs new file mode 100644 index 000000000..ed71203d8 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/LlmContexts/LlmContextReader.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.EmailHandler.LlmContexts +{ + public class LlmContextReader + { + [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; } + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Models/EmailModel.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Models/EmailModel.cs new file mode 100644 index 000000000..9414febba --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Models/EmailModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BotSharp.Plugin.EmailHandler.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; } + + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Templates/EmailSubjectElement.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Models/EmailSubjectElement.cs similarity index 86% rename from src/Plugins/BotSharp.Plugin.EmailReader/Templates/EmailSubjectElement.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Models/EmailSubjectElement.cs index b3eb583ad..a1cd263d9 100644 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Templates/EmailSubjectElement.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Models/EmailSubjectElement.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; -namespace BotSharp.Plugin.EmailReader.Templates +namespace BotSharp.Plugin.EmailHandler.Models { public class EmailSubjectElement : GenericElement { diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Providers/DefaultEmailReader.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Providers/DefaultEmailReader.cs similarity index 94% rename from src/Plugins/BotSharp.Plugin.EmailReader/Providers/DefaultEmailReader.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Providers/DefaultEmailReader.cs index 8af414e24..27c702b64 100644 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Providers/DefaultEmailReader.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Providers/DefaultEmailReader.cs @@ -1,12 +1,16 @@ -using BotSharp.Plugin.EmailReader.Models; -using BotSharp.Plugin.EmailReader.Settings; +using BotSharp.Plugin.EmailHandler.Models; using MailKit; using MailKit.Net.Imap; using MailKit.Search; using MailKit.Security; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; using System.Text.RegularExpressions; +using System.Threading.Tasks; -namespace BotSharp.Plugin.EmailReader.Providers +namespace BotSharp.Plugin.EmailHandler.Providers { public class DefaultEmailReader : IEmailReader { diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Providers/IEmailReader.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Providers/IEmailReader.cs similarity index 77% rename from src/Plugins/BotSharp.Plugin.EmailReader/Providers/IEmailReader.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Providers/IEmailReader.cs index 653c01afc..9df635c47 100644 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Providers/IEmailReader.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Providers/IEmailReader.cs @@ -1,11 +1,11 @@ -using BotSharp.Plugin.EmailReader.Models; +using BotSharp.Plugin.EmailHandler.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace BotSharp.Plugin.EmailReader.Providers +namespace BotSharp.Plugin.EmailHandler.Providers { public interface IEmailReader { diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Settings/EmailReaderSettings.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailReaderSettings.cs similarity index 88% rename from src/Plugins/BotSharp.Plugin.EmailReader/Settings/EmailReaderSettings.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailReaderSettings.cs index f2b87aef8..cc130fe29 100644 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Settings/EmailReaderSettings.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailReaderSettings.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace BotSharp.Plugin.EmailReader.Settings; +namespace BotSharp.Plugin.EmailHandler.Settings; public class EmailReaderSettings { diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailHandlerSettings.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailSenderSettings.cs similarity index 91% rename from src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailHandlerSettings.cs rename to src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailSenderSettings.cs index ae7636ab0..6859e0981 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailHandlerSettings.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Settings/EmailSenderSettings.cs @@ -1,6 +1,6 @@ namespace BotSharp.Plugin.EmailHandler.Settings; -public class EmailHandlerSettings +public class EmailSenderSettings { public string EmailAddress { get; set; } = string.Empty; public string Name { get; set; } = string.Empty; diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json b/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json similarity index 100% rename from src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json rename to src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_reader.json diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_request.json b/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_sender.json similarity index 97% rename from src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_request.json rename to src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_sender.json index a8a79998c..730c45970 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_request.json +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/handle_email_sender.json @@ -1,5 +1,5 @@ { - "name": "handle_email_request", + "name": "handle_email_sender", "description": "If the user requests to send an email with or without attachments or files, you need to capture the email content and the recipient email address. If the user explicitly enter email subject use the same if not intelligently capture the email subject from the content. Then call this function to send out email.", "parameters": { "type": "object", diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid b/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid similarity index 100% rename from src/Plugins/BotSharp.Plugin.EmailReader/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid rename to src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_reader.fn.liquid diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_request.fn.liquid b/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_request.fn.liquid deleted file mode 100644 index 9091f6bf3..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_request.fn.liquid +++ /dev/null @@ -1,2 +0,0 @@ -Please call handle_email_request if user wants to send email. -** Please take a look at the conversation and decide whether user wants to send email with files/attachments/images or not. \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_sender.fn.liquid b/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_sender.fn.liquid new file mode 100644 index 000000000..647942c6a --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/handle_email_sender.fn.liquid @@ -0,0 +1,2 @@ +Please call handle_email_sender if user wants to send email. +** Please take a look at the conversation and decide whether user wants to send email with files/attachments/images or not. diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj b/src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj deleted file mode 100644 index 35c1a1db4..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailReader/BotSharp.Plugin.EmailReader.csproj +++ /dev/null @@ -1,37 +0,0 @@ - - - - $(TargetFramework) - enable - $(LangVersion) - $(BotSharpVersion) - $(GeneratePackageOnBuild) - $(GenerateDocumentationFile) - $(SolutionDir)packages - - - - - - - - - - PreserveNewest - - - PreserveNewest - - - - - - - - - - - - - - diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs b/src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs deleted file mode 100644 index a88abc66b..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailReader/EmailReaderPlugin.cs +++ /dev/null @@ -1,41 +0,0 @@ -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(); - return settingService.Bind("EmailReader"); - }); - services.AddSingleton(provider => emailReaderSettings); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - } -} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs deleted file mode 100644 index 2d0042e04..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Enums/UtilityName.cs +++ /dev/null @@ -1,12 +0,0 @@ -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"; -} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs deleted file mode 100644 index cc85bfcf6..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Hooks/EmailReaderUtilityHook.cs +++ /dev/null @@ -1,17 +0,0 @@ -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 utilities) - { - utilities.Add(UtilityName.EmailReader); - } -} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs b/src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs deleted file mode 100644 index 4305f674b..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailReader/LlmContexts/LlmContextIn.cs +++ /dev/null @@ -1,18 +0,0 @@ -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; } -} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs deleted file mode 100644 index 9136d465d..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Models/EmailModel.cs +++ /dev/null @@ -1,18 +0,0 @@ -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; } - -} diff --git a/src/Plugins/BotSharp.Plugin.EmailReader/Using.cs b/src/Plugins/BotSharp.Plugin.EmailReader/Using.cs deleted file mode 100644 index 80d160dd5..000000000 --- a/src/Plugins/BotSharp.Plugin.EmailReader/Using.cs +++ /dev/null @@ -1,18 +0,0 @@ -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; \ No newline at end of file From 0bb98a19e51564e8dbdad5fb58c7f994258d2405 Mon Sep 17 00:00:00 2001 From: 103048 <103048@MJ0AJ7XO-LNPC> Date: Wed, 31 Jul 2024 15:54:39 -0500 Subject: [PATCH 3/3] code improvement --- .../BotSharp.Plugin.EmailHandler.csproj | 5 ++++- .../Functions/HandleEmailReaderFn.cs | 16 ++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj b/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj index fb241175c..f5926a53a 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/BotSharp.Plugin.EmailHandler.csproj @@ -14,6 +14,7 @@ + @@ -30,6 +31,9 @@ PreserveNewest + + PreserveNewest + @@ -37,7 +41,6 @@ - diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs index 964ce0288..121638e73 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Functions/HandleEmailReaderFn.cs @@ -5,7 +5,6 @@ using BotSharp.Core.Infrastructures; using BotSharp.Plugin.EmailHandler.Models; using BotSharp.Plugin.EmailHandler.Providers; -using BusinessCore.Utils; using MailKit; using MailKit.Net.Imap; using MailKit.Search; @@ -85,7 +84,7 @@ public async Task Execute(RoleDialogModel message) var response = await completion.GetChatCompletions(agent, dialogs); var content = response?.Content ?? string.Empty; message.Content = content; - message.RichContent = BuildRichContent.TextPostBackRichContent(_state.GetConversationId(), message.Content); + message.RichContent = BuildRichContentForSummary(_state.GetConversationId(), message.Content); return true; } UniqueId.TryParse(messageId, out UniqueId uid); @@ -105,6 +104,19 @@ public async Task Execute(RoleDialogModel message) return false; } } + public RichContent BuildRichContentForSummary(string conversationId, string content, string editorType = EditorTypeEnum.Text) + { + return new RichContent() + { + FillPostback = true, + Editor = editorType, + Recipient = new Recipient() { Id = conversationId }, + Message = new ButtonTemplateMessage() + { + Text = content, + } + }; + } private RichContent BuildRichContentForSubject(List emailSubjects) { var text = "Please let me know which message I need to read?";