Update email handler to include email read capabilities.

Rename to email sender
This commit is contained in:
103048 2024-07-31 15:46:14 -05:00
parent a36281e2e3
commit 6c6ff566a5
25 changed files with 95 additions and 200 deletions

View file

@ -11,16 +11,20 @@
</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\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">
@ -33,6 +37,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\onebrain\BusinessCore\BusinessCore.csproj" />
<ProjectReference Include="..\..\Infrastructure\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>

View file

@ -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 @@ namespace BotSharp.Plugin.EmailHandler
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>();
}
}
}

View file

@ -3,11 +3,8 @@ 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 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<HandleEmailReaderFn> _logger;
@ -53,7 +50,7 @@ public class HandleEmailReaderFn : IFunctionCallback
}
public async Task<bool> Execute(RoleDialogModel message)
{
var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs, _options.JsonSerializerOptions);
var args = JsonSerializer.Deserialize<LlmContextReader>(message.FunctionArgs, _options.JsonSerializerOptions);
var isMarkRead = args?.IsMarkRead ?? false;
var isSummarize = args?.IsSummarize ?? false;
var messageId = args?.MessageId;

View file

@ -5,25 +5,25 @@ using System.IO;
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;

View file

@ -3,14 +3,14 @@ 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 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 class EmailReaderHook : AgentHookBase
{
var conv = _services.GetRequiredService<IConversationService>();
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)
{

View file

@ -12,13 +12,13 @@ using System.Threading.Tasks;
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)
{
}

View file

@ -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; }
}
}

View file

@ -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; }
}
}

View file

@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Templates
namespace BotSharp.Plugin.EmailHandler.Models
{
public class EmailSubjectElement : GenericElement
{

View file

@ -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
{

View file

@ -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
{

View file

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.EmailReader.Settings;
namespace BotSharp.Plugin.EmailHandler.Settings;
public class EmailReaderSettings
{

View file

@ -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;

View file

@ -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",

View file

@ -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.

View file

@ -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.

View file

@ -1,37 +0,0 @@
<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

@ -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<ISettingService>();
return settingService.Bind<EmailReaderSettings>("EmailReader");
});
services.AddSingleton(provider => emailReaderSettings);
services.AddScoped<IAgentHook, EmailReaderHook>();
services.AddScoped<IAgentUtilityHook, EmailReaderUtilityHook>();
services.AddScoped<IEmailReader, DefaultEmailReader>();
}
}

View file

@ -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";
}

View file

@ -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<string> utilities)
{
utilities.Add(UtilityName.EmailReader);
}
}

View file

@ -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; }
}

View file

@ -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; }
}

View file

@ -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;