diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs index 808f7821..17369f28 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/AgentHookBase.cs @@ -1,5 +1,9 @@ using BotSharp.Abstraction.Agents.Settings; +using BotSharp.Abstraction.Conversations; using BotSharp.Abstraction.Functions.Models; +using BotSharp.Abstraction.Repositories; +using Microsoft.Extensions.DependencyInjection; +using System.Data; namespace BotSharp.Abstraction.Agents; @@ -52,4 +56,79 @@ public abstract class AgentHookBase : IAgentHook public virtual void OnAgentLoaded(Agent agent) { } + + public virtual void OnLoadAgentUtility(Agent agent, IEnumerable utilities) + { + if (agent.Type == AgentType.Routing || utilities.IsNullOrEmpty()) return; + + var conv = _services.GetRequiredService(); + var isConvMode = conv.IsConversationMode(); + if (!isConvMode) return; + + var render = _services.GetRequiredService(); + + agent.Functions ??= []; + var agentUtilities = agent.Utilities ?? []; + + foreach (var item in utilities) + { + if (item.UtilityName.IsNullOrEmpty() || item.Content == null) continue; + + var isEnabled = agentUtilities.Contains(item.UtilityName); + if (!isEnabled) continue; + + var (fns, prompts) = GetUtilityContent(item.Content); + + if (!fns.IsNullOrEmpty()) + { + agent.Functions.AddRange(fns); + } + + if (!prompts.IsNullOrEmpty()) + { + foreach (var prompt in prompts) + { + agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n"; + } + } + } + } + + private (IEnumerable, IEnumerable) GetUtilityContent(UtilityContent content) + { + var db = _services.GetRequiredService(); + var render = _services.GetRequiredService(); + + var fns = new List(); + var prompts = new List(); + + var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); + if (agent == null) + { + return (fns, prompts); + } + + if (!content.Functions.IsNullOrEmpty()) + { + var functionNames = content.Functions?.Select(x => x.Name)?.ToList() ?? []; + fns = agent?.Functions?.Where(x => functionNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))?.ToList() ?? []; + } + + if (!content.Templates.IsNullOrEmpty()) + { + foreach (var template in content.Templates) + { + var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(template.Name))?.Content ?? string.Empty; + if (string.IsNullOrWhiteSpace(prompt)) continue; + + if (!template.Data.IsNullOrEmpty()) + { + prompt = render.Render(prompt, template.Data); + } + prompts.Add(prompt); + } + } + + return (fns, prompts); + } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs index 99cdadca..f8e3c545 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentHook.cs @@ -31,4 +31,6 @@ public interface IAgentHook /// /// void OnAgentLoaded(Agent agent); + + void OnLoadAgentUtility(Agent agent, IEnumerable utilities); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentUtilityLoadModel.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentUtilityLoadModel.cs new file mode 100644 index 00000000..938c5546 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentUtilityLoadModel.cs @@ -0,0 +1,64 @@ +namespace BotSharp.Abstraction.Agents.Models; + +public class AgentUtilityLoadModel +{ + public string UtilityName { get; set; } + public UtilityContent Content { get; set; } + + public AgentUtilityLoadModel() + { + + } + + public AgentUtilityLoadModel(string utilityName, UtilityContent content) + { + UtilityName = utilityName; + Content = content; + } +} + + +public class UtilityContent +{ + public IEnumerable Functions { get; set; } = []; + public IEnumerable Templates { get; set; } = []; + + public UtilityContent() + { + + } +} + +public class UtilityFunction : UtilityBase +{ + public UtilityFunction() + { + + } + + public UtilityFunction(string name) + { + Name = name; + } +} + +public class UtilityTemplate : UtilityBase +{ + public Dictionary? Data { get; set; } + + public UtilityTemplate() + { + + } + + public UtilityTemplate(string name, Dictionary? data = null) + { + Name = name; + Data = data; + } +} + +public class UtilityBase +{ + public string Name { get; set; } +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.HasMissingRequiredField.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.HasMissingRequiredField.cs index 3b1f0566..d88b65ba 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.HasMissingRequiredField.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.HasMissingRequiredField.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Routing.Models; -using System.Drawing; namespace BotSharp.Core.Routing; diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs index 25ab3552..39c8ed23 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs @@ -4,13 +4,11 @@ namespace BotSharp.Core.Routing; public partial class RoutingService { - //private int _currentRecursionDepth = 0; public async Task InvokeAgent(string agentId, List dialogs) { var agentService = _services.GetRequiredService(); var agent = await agentService.LoadAgent(agentId); - //_currentRecursionDepth++; Context.IncreaseRecursiveCounter(); if (Context.CurrentRecursionDepth > agent.LlmConfig.MaxRecursionDepth) { diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs index 770acaf9..4f57571f 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs @@ -16,21 +16,6 @@ public partial class RoutingService : IRoutingService public IRoutingContext Context => _context; public Agent Router => _router; - //public int GetRecursiveCounter() - //{ - // return _currentRecursionDepth; - //} - - //public void SetRecursiveCounter(int counter) - //{ - // _currentRecursionDepth = counter; - //} - - //public void ResetRecursiveCounter() - //{ - // _currentRecursionDepth = 0; - //} - public RoutingService( IServiceProvider services, RoutingSettings settings, diff --git a/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerHook.cs b/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerHook.cs index 80acb149..f391ac2a 100644 --- a/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerHook.cs +++ b/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerHook.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; namespace BotSharp.Plugin.AudioHandler.Hooks; @@ -9,52 +8,24 @@ public class AudioHandlerHook : AgentHookBase, IAgentHook public override string SelfId => string.Empty; - public AudioHandlerHook(IServiceProvider services, AgentSettings settings) : base(services, settings) + public AudioHandlerHook(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.AudioHandler); - - if (isEnabled && isConvMode) + var utilityLoad = new AgentUtilityLoadModel { - AddUtility(agent, HANDLER_AUDIO); - } + UtilityName = UtilityName.AudioHandler, + Content = new UtilityContent + { + Functions = [new(HANDLER_AUDIO)], + Templates = [new($"{HANDLER_AUDIO}.fn")] + } + }; + base.OnLoadAgentUtility(agent, [utilityLoad]); base.OnAgentLoaded(agent); } - - private void AddUtility(Agent agent, string functionName) - { - var (prompt, fn) = GetPromptAndFunction(functionName); - - 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); - } - } - } - - private (string, FunctionDef?) GetPromptAndFunction(string functionName) - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty; - var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName)); - return (prompt, loadAttachmentFn); - } } diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs index 5d7ff586..eb52b719 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/EmailHandlerPlugin.cs @@ -20,8 +20,7 @@ namespace BotSharp.Plugin.EmailHandler return settingService.Bind("EmailSender"); }); - services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); var emailReaderSettings = new EmailReaderSettings(); diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerHook.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerHook.cs new file mode 100644 index 00000000..132f84ae --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerHook.cs @@ -0,0 +1,34 @@ +using BotSharp.Abstraction.Agents; +using BotSharp.Abstraction.Agents.Settings; +using BotSharp.Plugin.EmailHandler.Enums; + +namespace BotSharp.Plugin.EmailHandler.Hooks; + +public class EmailHandlerHook : AgentHookBase +{ + private static string EMAIL_READER_FN = "handle_email_reader"; + private static string EMAIL_SENDER_FN = "handle_email_sender"; + + public override string SelfId => string.Empty; + + public EmailHandlerHook(IServiceProvider services, AgentSettings settings) + : base(services, settings) + { + } + + public override void OnAgentLoaded(Agent agent) + { + var utilityLoad = new AgentUtilityLoadModel + { + UtilityName = UtilityName.EmailHandler, + Content = new UtilityContent + { + Functions = [new(EMAIL_READER_FN), new(EMAIL_SENDER_FN)], + Templates = [new($"{EMAIL_READER_FN}.fn"), new($"{EMAIL_SENDER_FN}.fn")] + } + }; + + base.OnLoadAgentUtility(agent, [utilityLoad]); + base.OnAgentLoaded(agent); + } +} diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs index 1d801e66..7d6a3be2 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs @@ -1,18 +1,12 @@ using BotSharp.Abstraction.Agents; 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 +namespace BotSharp.Plugin.EmailHandler.Hooks; + +public class EmailHandlerUtilityHook : IAgentUtilityHook { - public class EmailHandlerUtilityHook : IAgentUtilityHook + public void AddUtilities(List utilities) { - public void AddUtilities(List utilities) - { - utilities.Add(UtilityName.EmailHandler); - } + utilities.Add(UtilityName.EmailHandler); } } diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailReaderHook.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailReaderHook.cs deleted file mode 100644 index beece9e2..00000000 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailReaderHook.cs +++ /dev/null @@ -1,56 +0,0 @@ -using BotSharp.Abstraction.Agents; -using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Plugin.EmailHandler.Enums; - -namespace BotSharp.Plugin.EmailHandler.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.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 { 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.EmailHandler/Hooks/EmailSenderHook.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailSenderHook.cs deleted file mode 100644 index 27491015..00000000 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailSenderHook.cs +++ /dev/null @@ -1,56 +0,0 @@ -using BotSharp.Abstraction.Agents; -using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Plugin.EmailHandler.Enums; - -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(); - 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 { 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.ExcelHandler/Helpers/MySql/MySqlDbHelpers.cs b/src/Plugins/BotSharp.Plugin.ExcelHandler/Helpers/MySql/MySqlDbHelpers.cs index 4b86f89f..8b4c0940 100644 --- a/src/Plugins/BotSharp.Plugin.ExcelHandler/Helpers/MySql/MySqlDbHelpers.cs +++ b/src/Plugins/BotSharp.Plugin.ExcelHandler/Helpers/MySql/MySqlDbHelpers.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; -using BotSharp.Plugin.SqlHero.Settings; -using Microsoft.Data.Sqlite; +using BotSharp.Plugin.SqlDriver.Settings; using MySql.Data.MySqlClient; namespace BotSharp.Plugin.ExcelHandler.Helpers.MySql diff --git a/src/Plugins/BotSharp.Plugin.ExcelHandler/Helpers/Sqlite/SqliteDbHelpers.cs b/src/Plugins/BotSharp.Plugin.ExcelHandler/Helpers/Sqlite/SqliteDbHelpers.cs index c55e1642..464fa467 100644 --- a/src/Plugins/BotSharp.Plugin.ExcelHandler/Helpers/Sqlite/SqliteDbHelpers.cs +++ b/src/Plugins/BotSharp.Plugin.ExcelHandler/Helpers/Sqlite/SqliteDbHelpers.cs @@ -1,11 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BotSharp.Plugin.SqlDriver.Settings; using Microsoft.Data.Sqlite; -using BotSharp.Plugin.SqlDriver.Models; -using BotSharp.Plugin.SqlHero.Settings; namespace BotSharp.Plugin.ExcelHandler.Helpers.Sqlite; diff --git a/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerHook.cs b/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerHook.cs index 8716e4b1..98368bd3 100644 --- a/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerHook.cs +++ b/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerHook.cs @@ -12,47 +12,18 @@ public class ExcelHandlerHook : AgentHookBase, IAgentHook public override void OnAgentLoaded(Agent agent) { - var conv = _services.GetRequiredService(); - var isConvMode = conv.IsConversationMode(); - var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.ExcelHandler); - - if (isEnabled && isConvMode) + var utilityLoad = new AgentUtilityLoadModel { - AddUtility(agent, HANDLER_EXCEL); - } + UtilityName = UtilityName.ExcelHandler, + Content = new UtilityContent + { + Functions = [new(HANDLER_EXCEL)], + Templates = [new($"{HANDLER_EXCEL}.fn")] + } + }; + base.OnLoadAgentUtility(agent, [utilityLoad]); base.OnAgentLoaded(agent); } - - private void AddUtility(Agent agent, string functionName) - { - var (prompt, fn) = GetPromptAndFunction(functionName); - - 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); - } - } - } - - private (string, FunctionDef?) GetPromptAndFunction(string functionName) - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty; - var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName)); - return (prompt, loadAttachmentFn); - } } diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerHook.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerHook.cs index e9375244..c200d7fc 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerHook.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerHook.cs @@ -15,55 +15,47 @@ public class FileHandlerHook : AgentHookBase, IAgentHook public override void OnAgentLoaded(Agent agent) { - var conv = _services.GetRequiredService(); - var isConvMode = conv.IsConversationMode(); - - if (isConvMode) + var utilityLoads = new List { - AddUtility(agent, UtilityName.ImageGenerator, GENERATE_IMAGE_FN); - AddUtility(agent, UtilityName.ImageReader, READ_IMAGE_FN); - AddUtility(agent, UtilityName.ImageEditor, EDIT_IMAGE_FN); - AddUtility(agent, UtilityName.PdfReader, READ_PDF_FN); - - } + new AgentUtilityLoadModel + { + UtilityName = UtilityName.ImageGenerator, + Content = new UtilityContent + { + Functions = [new(GENERATE_IMAGE_FN)], + Templates = [new($"{GENERATE_IMAGE_FN}.fn")] + } + }, + new AgentUtilityLoadModel + { + UtilityName = UtilityName.ImageReader, + Content = new UtilityContent + { + Functions = [new(READ_IMAGE_FN)], + Templates = [new($"{READ_IMAGE_FN}.fn")] + } + }, + new AgentUtilityLoadModel + { + UtilityName = UtilityName.ImageEditor, + Content = new UtilityContent + { + Functions = [new(EDIT_IMAGE_FN)], + Templates = [new($"{EDIT_IMAGE_FN}.fn")] + } + }, + new AgentUtilityLoadModel + { + UtilityName = UtilityName.PdfReader, + Content = new UtilityContent + { + Functions = [new(READ_PDF_FN)], + Templates = [new($"{READ_PDF_FN}.fn")] + } + } + }; + base.OnLoadAgentUtility(agent, utilityLoads); base.OnAgentLoaded(agent); } - - private void AddUtility(Agent agent, string utility, string functionName) - { - if (!IsEnableUtility(agent, utility)) return; - - var (prompt, fn) = GetPromptAndFunction(functionName); - 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); - } - } - } - - private bool IsEnableUtility(Agent agent, string utility) - { - return !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(utility); - } - - private (string, FunctionDef?) GetPromptAndFunction(string functionName) - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty; - var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName)); - return (prompt, loadAttachmentFn); - } } diff --git a/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerHook.cs b/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerHook.cs index 78ee2c09..cf9f77f2 100644 --- a/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerHook.cs +++ b/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerHook.cs @@ -1,14 +1,11 @@ using BotSharp.Abstraction.Agents; -using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Abstraction.Repositories; namespace BotSharp.Plugin.HttpHandler.Hooks; public class HttpHandlerHook : AgentHookBase { - private static string FUNCTION_NAME = "handle_http_request"; + private static string HTTP_HANDLER_FN = "handle_http_request"; public override string SelfId => string.Empty; @@ -19,40 +16,17 @@ public class HttpHandlerHook : AgentHookBase public override void OnAgentLoaded(Agent agent) { - var conv = _services.GetRequiredService(); - var isConvMode = conv.IsConversationMode(); - var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.HttpHandler); - - if (isConvMode && isEnabled) + var utilityLoad = new AgentUtilityLoadModel { - var (prompt, fn) = GetPromptAndFunction(FUNCTION_NAME); - if (fn != null) + UtilityName = UtilityName.HttpHandler, + Content = new UtilityContent { - 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); - } + Functions = [new(HTTP_HANDLER_FN)], + Templates = [new($"{HTTP_HANDLER_FN}.fn")] } - } + }; + base.OnLoadAgentUtility(agent, [utilityLoad]); base.OnAgentLoaded(agent); } - - private (string, FunctionDef?) GetPromptAndFunction(string functionName) - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty; - var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName)); - return (prompt, loadAttachmentFn); - } } diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseAgentHook.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseAgentHook.cs index f8296520..01c6ffd6 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseAgentHook.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseAgentHook.cs @@ -2,59 +2,29 @@ namespace BotSharp.Plugin.KnowledgeBase.Hooks; public class KnowledgeBaseAgentHook : AgentHookBase, IAgentHook { + private const string KNOWLEDGE_RETRIEVAL_FN = "knowledge_retrieval"; + public override string SelfId => string.Empty; - public KnowledgeBaseAgentHook(IServiceProvider services, AgentSettings settings) : base(services, settings) + + public KnowledgeBaseAgentHook(IServiceProvider services, AgentSettings settings) + : base(services, settings) { } public override void OnAgentLoaded(Agent agent) { - var conv = _services.GetRequiredService(); - var isConvMode = conv.IsConversationMode(); - - if (isConvMode) + var utilityLoad = new AgentUtilityLoadModel { - AddUtility(agent, UtilityName.KnowledgeRetrieval, "knowledge_retrieval"); - } + UtilityName = UtilityName.KnowledgeRetrieval, + Content = new UtilityContent + { + Functions = [new(KNOWLEDGE_RETRIEVAL_FN)], + Templates = [new($"{KNOWLEDGE_RETRIEVAL_FN}.fn")] + } + }; + base.OnLoadAgentUtility(agent, [utilityLoad]); base.OnAgentLoaded(agent); } - - private void AddUtility(Agent agent, string utility, string functionName) - { - if (!IsEnableUtility(agent, utility)) return; - - var (prompt, fn) = GetPromptAndFunction(functionName); - 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); - } - } - } - - private bool IsEnableUtility(Agent agent, string utility) - { - return !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(utility); - } - - private (string, FunctionDef?) GetPromptAndFunction(string functionName) - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty; - var fn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName)); - return (prompt, fn); - } } diff --git a/src/Plugins/BotSharp.Plugin.Planner/Hooks/PlannerAgentHook.cs b/src/Plugins/BotSharp.Plugin.Planner/Hooks/PlannerAgentHook.cs index 503de3d5..7c37ef8f 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/Hooks/PlannerAgentHook.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/Hooks/PlannerAgentHook.cs @@ -2,6 +2,10 @@ namespace BotSharp.Plugin.Planner.Hooks; public class PlannerAgentHook : AgentHookBase { + private const string PRIMARY_STAGE_FN = "plan_primary_stage"; + private const string SECONDARY_STAGE_FN = "plan_secondary_stage"; + private const string SUMMARY_FN = "plan_summary"; + public override string SelfId => BuiltInAgentId.Planner; public PlannerAgentHook(IServiceProvider services, AgentSettings settings) @@ -30,76 +34,25 @@ public class PlannerAgentHook : AgentHookBase public override void OnAgentLoaded(Agent agent) { - var conv = _services.GetRequiredService(); - var isConvMode = conv.IsConversationMode(); - var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.TwoStagePlanner); - - if (isConvMode && isEnabled) + var utilityLoad = new AgentUtilityLoadModel { - var (prompt, fn) = GetPromptAndFunction("plan_primary_stage"); - if (fn != null) + UtilityName = UtilityName.TwoStagePlanner, + Content = new UtilityContent { - 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); - } + Functions = [ + new(PRIMARY_STAGE_FN), + new(SECONDARY_STAGE_FN), + new(SUMMARY_FN) + ], + Templates = [ + new($"{PRIMARY_STAGE_FN}.fn"), + new($"{SECONDARY_STAGE_FN}.fn"), + new($"{SUMMARY_FN}.fn") + ] } + }; - (prompt, fn) = GetPromptAndFunction("plan_secondary_stage"); - 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); - } - } - - (prompt, fn) = GetPromptAndFunction("plan_summary"); - 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.OnLoadAgentUtility(agent, [utilityLoad]); base.OnAgentLoaded(agent); } - - private (string, FunctionDef?) GetPromptAndFunction(string functionName) - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo($"{functionName}.fn"))?.Content ?? string.Empty; - var loadAttachmentFn = agent?.Functions?.FirstOrDefault(x => x.Name.IsEqualTo(functionName)); - return (prompt, loadAttachmentFn); - } } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj index 6433aae1..3a090f0c 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/BotSharp.Plugin.SqlDriver.csproj @@ -1,4 +1,4 @@ - + $(TargetFramework) diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Enum/Utility.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Enum/UtilityName.cs similarity index 90% rename from src/Plugins/BotSharp.Plugin.SqlDriver/Enum/Utility.cs rename to src/Plugins/BotSharp.Plugin.SqlDriver/Enum/UtilityName.cs index 4d9142ca..159d5e7a 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Enum/Utility.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Enum/UtilityName.cs @@ -1,6 +1,6 @@ namespace BotSharp.Plugin.SqlDriver.Enum; -public class Utility +public class UtilityName { public const string SqlExecutor = "sql-executor"; public const string SqlDictionaryLookup = "sql-dictionary-lookup"; diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Helpers/SqlDriverHelper.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Helpers/SqlDriverHelper.cs new file mode 100644 index 00000000..3049b754 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Helpers/SqlDriverHelper.cs @@ -0,0 +1,20 @@ +namespace BotSharp.Plugin.SqlDriver.Helpers; + +internal static class SqlDriverHelper +{ + internal static string GetDatabaseType(IServiceProvider services) + { + var settings = services.GetRequiredService(); + var dbType = "MySQL"; + + if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString)) + { + dbType = "SQL Server"; + } + else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString)) + { + dbType = "SQL Lite"; + } + return dbType; + } +} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/GetTableDefinitionHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/GetTableDefinitionHook.cs deleted file mode 100644 index bcfd2093..00000000 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/GetTableDefinitionHook.cs +++ /dev/null @@ -1,84 +0,0 @@ -using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Abstraction.Repositories; - -namespace BotSharp.Plugin.SqlDriver.Hooks; - -public class GetTableDefinitionHook : AgentHookBase, IAgentHook -{ - private const string SQL_EXECUTOR_TEMPLATE = "sql_table_definition.fn"; - private IEnumerable _targetSqlExecutorFunctions = new List - { - "sql_table_definition", - }; - - public override string SelfId => BuiltInAgentId.Planner; - - public GetTableDefinitionHook(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(Utility.SqlTableDefinition); - - if (isConvMode && isEnabled) - { - var (prompt, fns) = GetPromptAndFunctions(); - if (!fns.IsNullOrEmpty()) - { - if (!string.IsNullOrWhiteSpace(prompt)) - { - agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n"; - } - - if (agent.Functions == null) - { - agent.Functions = fns; - } - else - { - agent.Functions.AddRange(fns); - } - } - } - - base.OnAgentLoaded(agent); - } - - private (string, List?) GetPromptAndFunctions() - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var fns = agent?.Functions?.Where(x => _targetSqlExecutorFunctions.Contains(x.Name))?.ToList(); - - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(SQL_EXECUTOR_TEMPLATE))?.Content ?? string.Empty; - var dbType = GetDatabaseType(); - var render = _services.GetRequiredService(); - prompt = render.Render(prompt, new Dictionary - { - { "db_type", dbType } - }); - - return (prompt, fns); - } - - private string GetDatabaseType() - { - var settings = _services.GetRequiredService(); - var dbType = "MySQL"; - - if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString)) - { - dbType = "SQL Server"; - } - else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString)) - { - dbType = "SQL Lite"; - } - return dbType; - } -} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDictionaryLookupHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDictionaryLookupHook.cs deleted file mode 100644 index 534b0bf7..00000000 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDictionaryLookupHook.cs +++ /dev/null @@ -1,84 +0,0 @@ -using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Abstraction.Repositories; - -namespace BotSharp.Plugin.SqlDriver.Hooks; - -public class SqlDictionaryLookupHook : AgentHookBase, IAgentHook -{ - private const string SQL_EXECUTOR_TEMPLATE = "verify_dictionary_term.fn"; - private IEnumerable _targetSqlExecutorFunctions = new List - { - "verify_dictionary_term", - }; - - public override string SelfId => BuiltInAgentId.Planner; - - public SqlDictionaryLookupHook(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(Utility.SqlDictionaryLookup); - - if (isConvMode && isEnabled) - { - var (prompt, fns) = GetPromptAndFunctions(); - if (!fns.IsNullOrEmpty()) - { - if (!string.IsNullOrWhiteSpace(prompt)) - { - agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n"; - } - - if (agent.Functions == null) - { - agent.Functions = fns; - } - else - { - agent.Functions.AddRange(fns); - } - } - } - - base.OnAgentLoaded(agent); - } - - private (string, List?) GetPromptAndFunctions() - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var fns = agent?.Functions?.Where(x => _targetSqlExecutorFunctions.Contains(x.Name))?.ToList(); - - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(SQL_EXECUTOR_TEMPLATE))?.Content ?? string.Empty; - var dbType = GetDatabaseType(); - var render = _services.GetRequiredService(); - prompt = render.Render(prompt, new Dictionary - { - { "db_type", dbType } - }); - - return (prompt, fns); - } - - private string GetDatabaseType() - { - var settings = _services.GetRequiredService(); - var dbType = "MySQL"; - - if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString)) - { - dbType = "SQL Server"; - } - else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString)) - { - dbType = "SQL Lite"; - } - return dbType; - } -} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverAgentHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverAgentHook.cs new file mode 100644 index 00000000..24c803aa --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlDriverAgentHook.cs @@ -0,0 +1,61 @@ +using BotSharp.Abstraction.Agents.Enums; +using BotSharp.Abstraction.Agents.Settings; + +namespace BotSharp.Plugin.SqlDriver.Hooks; + +public class SqlDriverAgentHook : AgentHookBase, IAgentHook +{ + private const string SQL_TABLE_DEFINITION_FN = "sql_table_definition"; + private const string VERIFY_DICTIONARY_TERM_FN = "verify_dictionary_term"; + private const string SQL_SELECT_FN = "sql_select"; + + public override string SelfId => BuiltInAgentId.Planner; + + public SqlDriverAgentHook(IServiceProvider services, AgentSettings settings) + : base(services, settings) + { + } + + public override void OnAgentLoaded(Agent agent) + { + var dbType = SqlDriverHelper.GetDatabaseType(_services); + var promptData = new Dictionary + { + { "db_type", dbType } + }; + + var utilityLoads = new List + { + new AgentUtilityLoadModel + { + UtilityName = UtilityName.SqlTableDefinition, + Content = new UtilityContent + { + Functions = new List { new(SQL_TABLE_DEFINITION_FN) }, + Templates = new List { new($"{SQL_TABLE_DEFINITION_FN}.fn", promptData) } + } + }, + new AgentUtilityLoadModel + { + UtilityName = UtilityName.SqlDictionaryLookup, + Content = new UtilityContent + { + Functions = new List { new(VERIFY_DICTIONARY_TERM_FN) }, + Templates = new List { new($"{VERIFY_DICTIONARY_TERM_FN}.fn", promptData) } + } + }, + new AgentUtilityLoadModel + { + UtilityName = UtilityName.SqlExecutor, + Content = new UtilityContent + { + Functions = new List { new(SQL_SELECT_FN), new(SQL_TABLE_DEFINITION_FN) }, + Templates = new List { new($"sql_executor.fn", promptData) } + } + } + }; + + base.OnLoadAgentUtility(agent, utilityLoads); + base.OnAgentLoaded(agent); + } +} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlExecutorHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlExecutorHook.cs deleted file mode 100644 index 56e932c7..00000000 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlExecutorHook.cs +++ /dev/null @@ -1,85 +0,0 @@ -using BotSharp.Abstraction.Agents.Enums; -using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Functions.Models; -using BotSharp.Abstraction.Repositories; - -namespace BotSharp.Plugin.SqlDriver.Hooks; - -public class SqlExecutorHook : AgentHookBase, IAgentHook -{ - private const string SQL_EXECUTOR_TEMPLATE = "sql_executor.fn"; - private IEnumerable _targetSqlExecutorFunctions = new List - { - "sql_select", - "sql_table_definition", - }; - - public override string SelfId => string.Empty; - - public SqlExecutorHook(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(Utility.SqlExecutor); - - if (isConvMode && isEnabled) - { - var (prompt, fns) = GetPromptAndFunctions(); - if (!fns.IsNullOrEmpty()) - { - if (!string.IsNullOrWhiteSpace(prompt)) - { - agent.Instruction += $"\r\n\r\n{prompt}\r\n\r\n"; - } - - if (agent.Functions == null) - { - agent.Functions = fns; - } - else - { - agent.Functions.AddRange(fns); - } - } - } - - base.OnAgentLoaded(agent); - } - - private (string, List?) GetPromptAndFunctions() - { - var db = _services.GetRequiredService(); - var agent = db.GetAgent(BuiltInAgentId.UtilityAssistant); - var fns = agent?.Functions?.Where(x => _targetSqlExecutorFunctions.Contains(x.Name))?.ToList(); - - var prompt = agent?.Templates?.FirstOrDefault(x => x.Name.IsEqualTo(SQL_EXECUTOR_TEMPLATE))?.Content ?? string.Empty; - var dbType = GetDatabaseType(); //need change-> using hook? - var render = _services.GetRequiredService(); - prompt = render.Render(prompt, new Dictionary - { - { "db_type", dbType } - }); - - return (prompt, fns); - } - - private string GetDatabaseType() - { - var settings = _services.GetRequiredService(); - var dbType = "MySQL"; - - if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString)) - { - dbType = "SQL Server"; - } - else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString)) - { - dbType = "SQL Lite"; - } - return dbType; - } -} diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs index daac8cba..451863ff 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs @@ -4,8 +4,8 @@ public class SqlUtilityHook : IAgentUtilityHook { public void AddUtilities(List utilities) { - utilities.Add(Utility.SqlExecutor); - utilities.Add(Utility.SqlDictionaryLookup); - utilities.Add(Utility.SqlTableDefinition); + utilities.Add(UtilityName.SqlExecutor); + utilities.Add(UtilityName.SqlDictionaryLookup); + utilities.Add(UtilityName.SqlTableDefinition); } } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Settings/SqlDriverSetting.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Settings/SqlDriverSetting.cs index 24f12420..bff2c48c 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Settings/SqlDriverSetting.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Settings/SqlDriverSetting.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Plugin.SqlHero.Settings; +namespace BotSharp.Plugin.SqlDriver.Settings; public class SqlDriverSetting { diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs index 47a7883f..8e21d2b0 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/SqlDriverPlugin.cs @@ -25,12 +25,10 @@ public class SqlDriverPlugin : IBotSharpPlugin services.AddScoped(); services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); - services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Using.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Using.cs index 905c4f05..3aa71b80 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Using.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Using.cs @@ -20,5 +20,5 @@ global using BotSharp.Abstraction.Settings; global using BotSharp.Plugin.SqlDriver.Hooks; global using BotSharp.Plugin.SqlDriver.Services; global using BotSharp.Plugin.SqlDriver.Enum; -global using BotSharp.Plugin.SqlHero.Settings; -global using System.Drawing; +global using BotSharp.Plugin.SqlDriver.Helpers; +global using BotSharp.Plugin.SqlDriver.Settings; diff --git a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerHook.cs b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerHook.cs index 7d8d33fb..9fce1d20 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerHook.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerHook.cs @@ -1,14 +1,12 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Agents.Settings; -using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Utilities; using BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Enums; namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Hooks { internal class OutboundPhoneCallHandlerHook : AgentHookBase { - private static string FUNCTION_NAME = "twilio_outbound_phone_call"; + private static string OUTBOUND_PHONE_CALL_FN = "twilio_outbound_phone_call"; public override string SelfId => string.Empty; @@ -18,41 +16,18 @@ namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Hooks public override void OnAgentLoaded(Agent agent) { - var conv = _services.GetRequiredService(); - var isConvMode = conv.IsConversationMode(); - var isEnabled = !agent.Utilities.IsNullOrEmpty() && agent.Utilities.Contains(UtilityName.OutboundPhoneCall); - - if (isConvMode && isEnabled) + var utilityLoad = new AgentUtilityLoadModel { - var (prompt, fn) = GetPromptAndFunction(); - if (fn != null) + UtilityName = UtilityName.OutboundPhoneCall, + Content = new UtilityContent { - 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); - } + Functions = [new(OUTBOUND_PHONE_CALL_FN)], + Templates = [new($"{OUTBOUND_PHONE_CALL_FN}.fn")] } - } + }; + base.OnLoadAgentUtility(agent, [utilityLoad]); 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); - } } }