diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index 45fd3251c..733528922 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -36,6 +36,8 @@ public interface IAgentService FunctionParametersDef? RenderFunctionProperty(Agent agent, FunctionDef def); + bool RenderVisibility(string? visibilityExpression, Dictionary dict); + /// /// Get agent detail without trigger any hook. /// diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentUtility.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentUtility.cs index ce39c5687..6233dfa73 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentUtility.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentUtility.cs @@ -2,60 +2,38 @@ namespace BotSharp.Abstraction.Agents.Models; public class AgentUtility { + public string Category { get; set; } public string Name { get; set; } public bool Disabled { get; set; } - public IEnumerable Functions { get; set; } = []; - public IEnumerable Templates { get; set; } = []; - public AgentUtility() - { - - } - - public AgentUtility( - string name, - IEnumerable? functions = null, - IEnumerable? templates = null) - { - Name = name; - Functions = functions ?? []; - Templates = templates ?? []; - } - - public override string ToString() - { - return Name; - } -} + [JsonPropertyName("visibility_expression")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? VisibilityExpression { get; set; } + public IEnumerable Items { get; set; } = []; -public class UtilityFunction : UtilityBase -{ - public UtilityFunction() + public AgentUtility() { } - public UtilityFunction(string name) + public override string ToString() { - Name = name; + return $"{Category}-{Name}"; } } -public class UtilityTemplate : UtilityBase +public class UtilityItem { - public UtilityTemplate() - { - - } + [JsonPropertyName("function_name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? FunctionName { get; set; } - public UtilityTemplate(string name) - { - Name = name; - } -} + [JsonPropertyName("template_name")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? TemplateName { get; set; } -public class UtilityBase -{ - public string Name { get; set; } + [JsonPropertyName("visibility_expression")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? VisibilityExpression { get; set; } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Enum/UtilityName.cs b/src/Infrastructure/BotSharp.Core.Crontab/Enum/UtilityName.cs index eba15bd99..5963c8e8e 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Enum/UtilityName.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Enum/UtilityName.cs @@ -2,5 +2,5 @@ namespace BotSharp.Core.Crontab.Enum; public class UtilityName { - public const string ScheduleTask = "crontab.schedule-task"; + public const string ScheduleTask = "schedule-task"; } diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs b/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs index 7fed754a9..54dd3d064 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Hooks/CrontabUtilityHook.cs @@ -15,9 +15,19 @@ public void AddUtilities(List utilities) { new AgentUtility { + Category = "crontab", Name = UtilityName.ScheduleTask, - Functions = [new(SCHEDULE_TASK_FN), new(TASK_WAIT_FN)], - Templates = [new($"{SCHEDULE_TASK_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = SCHEDULE_TASK_FN, + TemplateName = $"{SCHEDULE_TASK_FN}.fn" + }, + new UtilityItem + { + FunctionName = TASK_WAIT_FN + }, + ] } }; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Hooks/BasicAgentHook.cs b/src/Infrastructure/BotSharp.Core/Agents/Hooks/BasicAgentHook.cs index 10fa376c9..91ded29f3 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Hooks/BasicAgentHook.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Hooks/BasicAgentHook.cs @@ -19,9 +19,9 @@ public override void OnAgentUtilityLoaded(Agent agent) var isConvMode = conv.IsConversationMode(); if (!isConvMode) return; + agent.Utilities ??= []; agent.SecondaryFunctions ??= []; agent.SecondaryInstructions ??= []; - agent.Utilities ??= []; var (functions, templates) = GetUtilityContent(agent); @@ -34,7 +34,7 @@ public override void OnAgentUtilityLoaded(Agent agent) private (IEnumerable, IEnumerable) GetUtilityContent(Agent agent) { var db = _services.GetRequiredService(); - var (functionNames, templateNames) = GetUniqueContent(agent.Utilities); + var (functionNames, templateNames) = FilterUtilityContent(agent.Utilities, agent); if (agent.MergeUtility) { @@ -43,7 +43,7 @@ public override void OnAgentUtilityLoaded(Agent agent) if (!string.IsNullOrEmpty(entryAgentId)) { var entryAgent = db.GetAgent(entryAgentId, basicsOnly: true); - var (fns, tps) = GetUniqueContent(entryAgent?.Utilities); + var (fns, tps) = FilterUtilityContent(entryAgent?.Utilities, agent); functionNames = functionNames.Concat(fns).Distinct().ToList(); templateNames = templateNames.Concat(tps).Distinct().ToList(); } @@ -55,23 +55,41 @@ public override void OnAgentUtilityLoaded(Agent agent) return (functions, templates); } - private (IEnumerable, IEnumerable) GetUniqueContent(IEnumerable? utilities) + private (IEnumerable, IEnumerable) FilterUtilityContent(IEnumerable? utilities, Agent agent) { if (utilities.IsNullOrEmpty()) { return ([], []); } - utilities = utilities?.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled)?.ToList() ?? []; - var functionNames = utilities.SelectMany(x => x.Functions) - .Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(UTIL_PREFIX)) - .Select(x => x.Name) - .Distinct().ToList(); - var templateNames = utilities.SelectMany(x => x.Templates) - .Where(x => !string.IsNullOrEmpty(x.Name) && x.Name.StartsWith(UTIL_PREFIX)) - .Select(x => x.Name) - .Distinct().ToList(); - - return (functionNames, templateNames); + var agentService = _services.GetRequiredService(); + var innerUtilities = utilities!.Where(x => !string.IsNullOrEmpty(x.Name) && !x.Disabled).ToList(); + + var functionNames = new List(); + var templateNames = new List(); + + foreach (var utility in innerUtilities) + { + var isVisible = agentService.RenderVisibility(utility.VisibilityExpression, agent.TemplateDict); + if (!isVisible || utility.Items.IsNullOrEmpty()) continue; + + foreach (var item in utility.Items) + { + isVisible = agentService.RenderVisibility(item.VisibilityExpression, agent.TemplateDict); + if (!isVisible) continue; + + if (item.FunctionName?.StartsWith(UTIL_PREFIX) == true) + { + functionNames.Add(item.FunctionName); + } + + if (item.TemplateName?.StartsWith(UTIL_PREFIX) == true) + { + templateNames.Add(item.TemplateName); + } + } + } + + return (functionNames.Distinct(), templateNames.Distinct()); } } diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs index 0d6e84809..ef81adafd 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.LoadAgent.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Infrastructures; using BotSharp.Abstraction.Routing.Models; using System.Collections.Concurrent; @@ -18,12 +17,15 @@ public async Task LoadAgent(string id, bool loadUtility = true) var agent = await GetAgent(id); if (agent == null) return null; + agent.TemplateDict = []; + agent.SecondaryInstructions = []; + agent.SecondaryFunctions = []; + await InheritAgent(agent); OverrideInstructionByChannel(agent); AddOrUpdateParameters(agent); // Populate state into dictionary - agent.TemplateDict = new Dictionary(); PopulateState(agent.TemplateDict); // After agent is loaded diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs index f862482ca..ffaffb476 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.Rendering.cs @@ -46,12 +46,7 @@ public bool RenderFunction(Agent agent, FunctionDef def) if (!string.IsNullOrWhiteSpace(def.VisibilityExpression)) { - var render = _services.GetRequiredService(); - var result = render.Render(def.VisibilityExpression, new Dictionary - { - { "states", agent.TemplateDict } - }); - isRender = isRender && result == "visible"; + isRender = RenderVisibility(def.VisibilityExpression, agent.TemplateDict); } return isRender; @@ -76,12 +71,7 @@ public bool RenderFunction(Agent agent, FunctionDef def) if (node.TryGetProperty(visibleExpress, out var element)) { var expression = element.GetString(); - var render = _services.GetRequiredService(); - var result = render.Render(expression, new Dictionary - { - { "states", agent.TemplateDict } - }); - matched = result == "visible"; + matched = RenderVisibility(expression, agent.TemplateDict); } if (matched) @@ -137,4 +127,20 @@ public string RenderedTemplate(Agent agent, string templateName) return content; } + + public bool RenderVisibility(string? visibilityExpression, Dictionary dict) + { + if (string.IsNullOrWhiteSpace(visibilityExpression)) + { + return true; + } + + var render = _services.GetRequiredService(); + var result = render.Render(visibilityExpression, new Dictionary + { + { "states", dict ?? [] } + }); + + return result.IsEqualTo("visible"); + } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Instructs/Hooks/InstructUtilityHook.cs b/src/Infrastructure/BotSharp.Core/Instructs/Hooks/InstructUtilityHook.cs index e0c6d686e..eaee39cad 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/Hooks/InstructUtilityHook.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/Hooks/InstructUtilityHook.cs @@ -9,9 +9,14 @@ public void AddUtilities(List utilities) { utilities.Add(new AgentUtility { - Name = "instruct.template", - Functions = [new($"{EXECUTE_TEMPLATE}")], - Templates = [new($"{EXECUTE_TEMPLATE}.fn")] + Category = "instruct", + Name = "template", + Items = [ + new UtilityItem { + FunctionName = $"{EXECUTE_TEMPLATE}", + TemplateName = $"{EXECUTE_TEMPLATE}.fn" + } + ] }); } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingUtilityHook.cs b/src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingUtilityHook.cs index de7a556b2..b909bc0f2 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingUtilityHook.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Hooks/RoutingUtilityHook.cs @@ -10,9 +10,20 @@ public void AddUtilities(List utilities) { utilities.Add(new AgentUtility { + Category = "routing", Name = "routing.tools", - Functions = [new($"{REDIRECT_TO_AGENT}"), new($"{FALLBACK_TO_ROUTER}")], - Templates = [new($"{REDIRECT_TO_AGENT}.fn"), new($"{FALLBACK_TO_ROUTER}.fn")] + Items = [ + new UtilityItem + { + FunctionName = $"{REDIRECT_TO_AGENT}", + TemplateName = $"{REDIRECT_TO_AGENT}.fn" + }, + new UtilityItem + { + FunctionName = $"{FALLBACK_TO_ROUTER}", + TemplateName = $"{FALLBACK_TO_ROUTER}.fn" + } + ] }); } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index 529835066..0e53c7463 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -167,7 +167,9 @@ public IEnumerable GetAgentUtilityOptions() { hook.AddUtilities(utilities); } - return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList(); + return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Category) + && !string.IsNullOrWhiteSpace(x.Name) + && !x.Items.IsNullOrEmpty()).ToList(); } [HttpGet("/agent/labels")] diff --git a/src/Plugins/BotSharp.Plugin.AudioHandler/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.AudioHandler/Enums/UtilityName.cs index 51c86863e..d11bd65ac 100644 --- a/src/Plugins/BotSharp.Plugin.AudioHandler/Enums/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.AudioHandler/Enums/UtilityName.cs @@ -2,5 +2,5 @@ namespace BotSharp.Plugin.AudioHandler.Enums; public class UtilityName { - public const string AudioHandler = "audio.audio-handler"; + public const string AudioHandler = "audio-handler"; } diff --git a/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerUtilityHook.cs index 4cb6d61c7..9a389d9a4 100644 --- a/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.AudioHandler/Hooks/AudioHandlerUtilityHook.cs @@ -9,9 +9,15 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility { + Category = "audio", Name = UtilityName.AudioHandler, - Functions = [new(HANDLER_AUDIO)], - Templates = [new($"{HANDLER_AUDIO}.fn")] + Items = [ + new UtilityItem + { + FunctionName = HANDLER_AUDIO, + TemplateName = $"{HANDLER_AUDIO}.fn" + } + ] }; utilities.Add(utility); diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Enums/UtilityName.cs index e9297d8c6..d6bc9b014 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Enums/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Enums/UtilityName.cs @@ -2,5 +2,5 @@ namespace BotSharp.Plugin.EmailHandler.Enums; public class UtilityName { - public const string EmailHandler = "email.email-handler"; + public const string EmailHandler = "email-handler"; } diff --git a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs index a99dfc8c0..39c97881b 100644 --- a/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.EmailHandler/Hooks/EmailHandlerUtilityHook.cs @@ -13,9 +13,20 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility { + Category = "email", Name = UtilityName.EmailHandler, - Functions = [new(EMAIL_READER_FN), new(EMAIL_SENDER_FN)], - Templates = [new($"{EMAIL_READER_FN}.fn"), new($"{EMAIL_SENDER_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = EMAIL_READER_FN, + TemplateName = $"{EMAIL_READER_FN}.fn" + }, + new UtilityItem + { + FunctionName = EMAIL_SENDER_FN, + TemplateName = $"{EMAIL_SENDER_FN}.fn" + } + ] }; utilities.Add(utility); diff --git a/src/Plugins/BotSharp.Plugin.ExcelHandler/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.ExcelHandler/Enums/UtilityName.cs index bc6892590..66ef3f36e 100644 --- a/src/Plugins/BotSharp.Plugin.ExcelHandler/Enums/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.ExcelHandler/Enums/UtilityName.cs @@ -2,5 +2,5 @@ namespace BotSharp.Plugin.ExcelHandler.Enums; public class UtilityName { - public const string ExcelHandler = "excel.excel-handler"; + public const string ExcelHandler = "excel-handler"; } diff --git a/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerUtilityHook.cs index 6293a8328..89d827669 100644 --- a/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.ExcelHandler/Hooks/ExcelHandlerUtilityHook.cs @@ -9,9 +9,15 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility { + Category = "file", Name = UtilityName.ExcelHandler, - Functions = [new(HANDLER_EXCEL)], - Templates = [new($"{HANDLER_EXCEL}.fn")] + Items = [ + new UtilityItem + { + FunctionName = HANDLER_EXCEL, + TemplateName = $"{HANDLER_EXCEL}.fn" + } + ] }; utilities.Add(utility); diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Enums/UtilityName.cs index 68242202c..eb2b4e2ba 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Enums/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Enums/UtilityName.cs @@ -2,8 +2,8 @@ namespace BotSharp.Plugin.FileHandler.Enums; public class UtilityName { - public const string ImageGenerator = "file.image-generator"; - public const string ImageReader = "file.image-reader"; - public const string ImageEditor = "file.image-editor"; - public const string PdfReader = "file.pdf-reader"; + public const string ImageGenerator = "image-generator"; + public const string ImageReader = "image-reader"; + public const string ImageEditor = "image-editor"; + public const string PdfReader = "pdf-reader"; } diff --git a/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerUtilityHook.cs index 3409c4481..ab2ee2abc 100644 --- a/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.FileHandler/Hooks/FileHandlerUtilityHook.cs @@ -13,27 +13,50 @@ public void AddUtilities(List utilities) { new AgentUtility { + Category = "file", Name = UtilityName.ImageGenerator, - Functions = [new(GENERATE_IMAGE_FN)], - Templates = [new($"{GENERATE_IMAGE_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = GENERATE_IMAGE_FN, + TemplateName = $"{GENERATE_IMAGE_FN}.fn" + } + ] }, new AgentUtility { + Category = "file", Name = UtilityName.ImageReader, - Functions = [new(READ_IMAGE_FN)], - Templates = [new($"{READ_IMAGE_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = READ_IMAGE_FN, + TemplateName = $"{READ_IMAGE_FN}.fn" + } + ] }, new AgentUtility { Name = UtilityName.ImageEditor, - Functions = [new(EDIT_IMAGE_FN)], - Templates = [new($"{EDIT_IMAGE_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = EDIT_IMAGE_FN, + TemplateName = $"{EDIT_IMAGE_FN}.fn" + } + ] }, new AgentUtility { + Category = "file", Name = UtilityName.PdfReader, - Functions = [new(READ_PDF_FN)], - Templates = [new($"{READ_PDF_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = READ_PDF_FN, + TemplateName = $"{READ_PDF_FN}.fn" + } + ] } }; diff --git a/src/Plugins/BotSharp.Plugin.HttpHandler/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.HttpHandler/Enums/UtilityName.cs index 3a1e7cd53..2e0146bfb 100644 --- a/src/Plugins/BotSharp.Plugin.HttpHandler/Enums/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.HttpHandler/Enums/UtilityName.cs @@ -2,5 +2,5 @@ namespace BotSharp.Plugin.HttpHandler.Enums; public class UtilityName { - public const string HttpHandler = "http.http-handler"; + public const string HttpHandler = "http-handler"; } diff --git a/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerUtilityHook.cs index d99b6084b..786380c67 100644 --- a/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.HttpHandler/Hooks/HttpHandlerUtilityHook.cs @@ -11,9 +11,15 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility { + Category = "http", Name = UtilityName.HttpHandler, - Functions = [new(HTTP_HANDLER_FN)], - Templates = [new($"{HTTP_HANDLER_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = HTTP_HANDLER_FN, + TemplateName = $"{HTTP_HANDLER_FN}.fn" + } + ] }; utilities.Add(utility); diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Enum/UtilityName.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Enum/UtilityName.cs index e45183443..0f1e15a56 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Enum/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Enum/UtilityName.cs @@ -2,5 +2,5 @@ namespace BotSharp.Plugin.KnowledgeBase.Enum; public class UtilityName { - public const string KnowledgeRetrieval = "kg.knowledge-base"; + public const string KnowledgeRetrieval = "knowledge-base"; } diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseUtilityHook.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseUtilityHook.cs index 84254841a..aab73b57d 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Hooks/KnowledgeBaseUtilityHook.cs @@ -9,9 +9,15 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility { + Category = "knowledge", Name = UtilityName.KnowledgeRetrieval, - Functions = [new(KNOWLEDGE_RETRIEVAL_FN)], - Templates = [new($"{KNOWLEDGE_RETRIEVAL_FN}.fn")] + Items = [ + new UtilityItem + { + FunctionName = KNOWLEDGE_RETRIEVAL_FN, + TemplateName = $"{KNOWLEDGE_RETRIEVAL_FN}.fn" + } + ] }; utilities.Add(utility); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentUtilityMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentUtilityMongoElement.cs index 131226b3c..be052a1e5 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentUtilityMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentUtilityMongoElement.cs @@ -5,19 +5,26 @@ namespace BotSharp.Plugin.MongoStorage.Models; [BsonIgnoreExtraElements(Inherited = true)] public class AgentUtilityMongoElement { + public string Category { get; set; } = default!; public string Name { get; set; } = default!; public bool Disabled { get; set; } - public List Functions { get; set; } = []; - public List Templates { get; set; } = []; + public string? VisibilityExpression { get; set; } + public List Items { get; set; } = []; public static AgentUtilityMongoElement ToMongoElement(AgentUtility utility) { return new AgentUtilityMongoElement { + Category = utility.Category, Name = utility.Name, Disabled = utility.Disabled, - Functions = utility.Functions?.Select(x => new UtilityFunctionMongoElement(x.Name))?.ToList() ?? [], - Templates = utility.Templates?.Select(x => new UtilityTemplateMongoElement(x.Name))?.ToList() ?? [] + VisibilityExpression = utility.VisibilityExpression, + Items = utility.Items?.Select(x => new AgentUtilityItemMongoElement + { + FunctionName = x.FunctionName, + TemplateName = x.TemplateName, + VisibilityExpression = x.VisibilityExpression + })?.ToList() ?? [] }; } @@ -25,20 +32,23 @@ public static AgentUtility ToDomainElement(AgentUtilityMongoElement utility) { return new AgentUtility { + Category = utility.Category, Name = utility.Name, Disabled = utility.Disabled, - Functions = utility.Functions?.Select(x => new UtilityFunction(x.Name))?.ToList() ?? [], - Templates = utility.Templates?.Select(x => new UtilityTemplate(x.Name))?.ToList() ?? [] + VisibilityExpression = utility.VisibilityExpression, + Items = utility.Items?.Select(x => new UtilityItem + { + FunctionName = x.FunctionName, + TemplateName = x.TemplateName, + VisibilityExpression = x.VisibilityExpression + })?.ToList() ?? [], }; } } -public class UtilityFunctionMongoElement(string name) +public class AgentUtilityItemMongoElement { - public string Name { get; set; } = name; -} - -public class UtilityTemplateMongoElement(string name) -{ - public string Name { get; set; } = name; + public string? FunctionName { get; set; } + public string? TemplateName { get; set; } + public string? VisibilityExpression { get; set; } } \ No newline at end of file diff --git a/src/Plugins/BotSharp.Plugin.Planner/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.Planner/Enums/UtilityName.cs index 7594fae04..8a3ef6e3a 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/Enums/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/Enums/UtilityName.cs @@ -2,5 +2,5 @@ namespace BotSharp.Plugin.Planner.Enums; public class UtilityName { - public const string TwoStagePlanner = "planner.two-stage-planner"; + public const string TwoStagePlanner = "two-stage-planner"; } diff --git a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Hooks/TwoStagingPlannerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Hooks/TwoStagingPlannerUtilityHook.cs index 80dfad570..cd41bed44 100644 --- a/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Hooks/TwoStagingPlannerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.Planner/TwoStaging/Hooks/TwoStagingPlannerUtilityHook.cs @@ -10,16 +10,24 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility { + Category = "planner", Name = UtilityName.TwoStagePlanner, - 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") + Items = [ + new UtilityItem + { + FunctionName = PRIMARY_STAGE_FN, + TemplateName = $"{PRIMARY_STAGE_FN}.fn" + }, + new UtilityItem + { + FunctionName = SECONDARY_STAGE_FN, + TemplateName = $"{SECONDARY_STAGE_FN}.fn" + }, + new UtilityItem + { + FunctionName = SUMMARY_FN, + TemplateName = $"{SUMMARY_FN}.fn" + } ] }; diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/InterpreterUtilityHook.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/InterpreterUtilityHook.cs index 540870beb..7b644be2b 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/InterpreterUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Hooks/InterpreterUtilityHook.cs @@ -8,9 +8,15 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility() { + Category = "coding", Name = UtilityName.PythonInterpreter, - Functions = [new(FUNCTION_NAME)], - Templates = [new($"{FUNCTION_NAME}.fn")] + Items = [ + new UtilityItem + { + FunctionName = FUNCTION_NAME, + TemplateName = $"{FUNCTION_NAME}.fn" + } + ] }; utilities.Add(utility); diff --git a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs index 6756b8c05..760076c65 100644 --- a/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.SqlDriver/Hooks/SqlUtilityHook.cs @@ -14,18 +14,24 @@ public void AddUtilities(List utilities) { new AgentUtility { - Name = "db.tools", - Functions = - [ - new(SQL_TABLE_DEFINITION_FN), - new(VERIFY_DICTIONARY_TERM_FN), - new(SQL_SELECT_FN), - ], - Templates = - [ - new($"{VERIFY_DICTIONARY_TERM_FN}.fn"), - new($"{SQL_TABLE_DEFINITION_FN}.fn"), - new($"{SQL_EXECUTOR_FN}.fn") + Category = "database", + Name = "sql.tools", + Items = [ + new UtilityItem + { + FunctionName = SQL_TABLE_DEFINITION_FN, + TemplateName = $"{SQL_TABLE_DEFINITION_FN}.fn" + }, + new UtilityItem + { + FunctionName = VERIFY_DICTIONARY_TERM_FN, + TemplateName = $"{VERIFY_DICTIONARY_TERM_FN}.fn" + }, + new UtilityItem + { + FunctionName = SQL_SELECT_FN, + TemplateName = $"{SQL_EXECUTOR_FN}.fn" + } ] } }; diff --git a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Enums/UtilityName.cs b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Enums/UtilityName.cs index b7244e7db..3795f320b 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Enums/UtilityName.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Enums/UtilityName.cs @@ -2,6 +2,6 @@ namespace BotSharp.Plugin.Twilio.OutboundPhoneCallHandler.Enums { public class UtilityName { - public const string OutboundPhoneCall = "phone.twilio-phone-call"; + public const string OutboundPhoneCall = "twilio-phone-call"; } } diff --git a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs index 88586ea8e..5ffd692e9 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Functions/OutboundPhoneCallFn.cs @@ -1,5 +1,4 @@ using BotSharp.Abstraction.Files; -using BotSharp.Abstraction.Files.Models; using BotSharp.Abstraction.Infrastructures.Enums; using BotSharp.Abstraction.Options; using BotSharp.Abstraction.Routing; diff --git a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerUtilityHook.cs b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerUtilityHook.cs index 58999457b..5bd48a3c5 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/OutboundPhoneCallHandler/Hooks/OutboundPhoneCallHandlerUtilityHook.cs @@ -16,17 +16,29 @@ public void AddUtilities(List utilities) { var utility = new AgentUtility { + Category = "phone", Name = UtilityName.OutboundPhoneCall, - Functions = - [ - new($"{OUTBOUND_PHONE_CALL_FN}"), - new($"{TRANSFER_PHONE_CALL_FN}"), - new($"{HANGUP_PHONE_CALL_FN}"), - new($"{TEXT_MESSAGE_FN}"), - new($"{LEAVE_VOICEMAIL_FN}") - ], - Templates = - [ + Items = [ + new UtilityItem + { + FunctionName = OUTBOUND_PHONE_CALL_FN + }, + new UtilityItem + { + FunctionName = TRANSFER_PHONE_CALL_FN + }, + new UtilityItem + { + FunctionName = HANGUP_PHONE_CALL_FN + }, + new UtilityItem + { + FunctionName = TEXT_MESSAGE_FN + }, + new UtilityItem + { + FunctionName = LEAVE_VOICEMAIL_FN + } ] }; diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs index 636371191..36f693573 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Hooks/WebUtilityHook.cs @@ -14,18 +14,27 @@ public void AddUtilities(List utilities) { new AgentUtility { - Name = "web.tools", - Functions = - [ - new(CLOSE_BROWSER_FN), - new(GO_TO_PAGE_FN), - new(LOCATE_ELEMENT_FN), - new(ACTION_ON_ELEMENT_FN) - ], - Templates = - [ - new($"{GO_TO_PAGE_FN}.fn"), - new($"{ACTION_ON_ELEMENT_FN}.fn") + Category = "web", + Name = "browser.tools", + Items = [ + new UtilityItem + { + FunctionName = GO_TO_PAGE_FN, + TemplateName = $"{GO_TO_PAGE_FN}.fn" + }, + new UtilityItem + { + FunctionName = ACTION_ON_ELEMENT_FN, + TemplateName = $"{ACTION_ON_ELEMENT_FN}.fn" + }, + new UtilityItem + { + FunctionName = LOCATE_ELEMENT_FN + }, + new UtilityItem + { + FunctionName = CLOSE_BROWSER_FN + } ] } }; diff --git a/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs b/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs index 6716519a1..afaf28998 100644 --- a/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs +++ b/tests/BotSharp.LLM.Tests/Core/TestAgentService.cs @@ -6,6 +6,7 @@ using BotSharp.Abstraction.Plugins.Models; using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Utilities; +using NetTopologySuite.Algorithm; namespace BotSharp.Plugin.Google.Core { @@ -61,6 +62,11 @@ public bool RenderFunction(Agent agent, FunctionDef def) return def.Parameters; } + public bool RenderVisibility(string? visibilityExpression, Dictionary dict) + { + return true; + } + public Task GetAgent(string id) { return Task.FromResult(new Agent());