diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs index ecdda1c7..96aa4754 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/BuiltInAgentId.cs @@ -50,5 +50,5 @@ public class BuiltInAgentId /// /// Translates user-defined natural language rules into programmatic code /// - public const string RuleEncoder = "6acfb93c-3412-402e-9ba5-c5d3cd8f0161"; + public const string RulesInterpreter = "201e49a2-40b3-4ccd-b8cc-2476565a1b40"; } diff --git a/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj b/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj index cc072fe6..ae4c9fef 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj +++ b/src/Infrastructure/BotSharp.Core.Crontab/BotSharp.Core.Crontab.csproj @@ -1,7 +1,11 @@ - + - net8.0 + $(TargetFramework) + $(LangVersion) + $(BotSharpVersion) + $(GeneratePackageOnBuild) + $(SolutionDir)packages enable enable diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs index 5a48f58b..4711a6aa 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs @@ -24,7 +24,7 @@ public class CrontabWatcher : BackgroundService { var locker = scope.ServiceProvider.GetRequiredService(); - while (!stoppingToken.IsCancellationRequested) + /*while (!stoppingToken.IsCancellationRequested) { var delay = Task.Delay(1000, stoppingToken); @@ -34,7 +34,7 @@ public class CrontabWatcher : BackgroundService }); await delay; - } + }*/ _logger.LogWarning("Crontab Watcher background service is stopped."); } diff --git a/src/Infrastructure/BotSharp.Core.Rules/Actions/IRuleAction.cs b/src/Infrastructure/BotSharp.Core.Rules/Actions/IRuleAction.cs new file mode 100644 index 00000000..6361950b --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/Actions/IRuleAction.cs @@ -0,0 +1,5 @@ +namespace BotSharp.Core.Rules.Actions; + +public interface IRuleAction +{ +} diff --git a/src/Infrastructure/BotSharp.Core.Rules/BotSharp.Core.Rules.csproj b/src/Infrastructure/BotSharp.Core.Rules/BotSharp.Core.Rules.csproj index caef78b8..38b59c48 100644 --- a/src/Infrastructure/BotSharp.Core.Rules/BotSharp.Core.Rules.csproj +++ b/src/Infrastructure/BotSharp.Core.Rules/BotSharp.Core.Rules.csproj @@ -1,11 +1,27 @@ - + - net8.0 + $(TargetFramework) + $(LangVersion) + $(BotSharpVersion) + $(GeneratePackageOnBuild) + $(SolutionDir)packages enable enable + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + diff --git a/src/Infrastructure/BotSharp.Core.Rules/Criterias/IRuleCriteria.cs b/src/Infrastructure/BotSharp.Core.Rules/Criterias/IRuleCriteria.cs new file mode 100644 index 00000000..c65743ce --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/Criterias/IRuleCriteria.cs @@ -0,0 +1,5 @@ +namespace BotSharp.Core.Rules.Criterias; + +public interface IRuleCriteria +{ +} diff --git a/src/Infrastructure/BotSharp.Core.Rules/Engines/IRuleEngine.cs b/src/Infrastructure/BotSharp.Core.Rules/Engines/IRuleEngine.cs new file mode 100644 index 00000000..f7db3a96 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/Engines/IRuleEngine.cs @@ -0,0 +1,8 @@ +using BotSharp.Core.Rules.Triggers; + +namespace BotSharp.Core.Rules.Engines; + +public interface IRuleEngine +{ + Task Triggered(IRuleTrigger trigger, string data); +} diff --git a/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs b/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs new file mode 100644 index 00000000..c69d654b --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs @@ -0,0 +1,26 @@ +using BotSharp.Core.Rules.Triggers; + +namespace BotSharp.Core.Rules.Engines; + +public class RuleEngine : IRuleEngine +{ + private readonly IServiceProvider _services; + public RuleEngine(IServiceProvider services) + { + _services = services; + } + + public async Task Triggered(IRuleTrigger trigger, string data) + { + // Pull all user defined rules + + var instructService = _services.GetRequiredService(); + + var userSay = $"===Input data===\r\n{data}\r\n\r\nWhen WO NTE is greater than 100, notify resident."; + + var result = await instructService.Execute(BuiltInAgentId.RulesInterpreter, new RoleDialogModel(AgentRole.User, data), "criteria_check", "#TEMPLATE#"); + + string[] rules = []; + // Check if meet the criteria + } +} diff --git a/src/Infrastructure/BotSharp.Core.Rules/RulesPlugin.cs b/src/Infrastructure/BotSharp.Core.Rules/RulesPlugin.cs index 14177437..56e1fb8a 100644 --- a/src/Infrastructure/BotSharp.Core.Rules/RulesPlugin.cs +++ b/src/Infrastructure/BotSharp.Core.Rules/RulesPlugin.cs @@ -1,3 +1,5 @@ +using BotSharp.Core.Rules.Engines; + namespace BotSharp.Core.Rules; public class RulesPlugin : IBotSharpPlugin @@ -9,11 +11,11 @@ public class RulesPlugin : IBotSharpPlugin public string[] AgentIds = [ - BuiltInAgentId.RuleEncoder + BuiltInAgentId.RulesInterpreter ]; public void RegisterDI(IServiceCollection services, IConfiguration config) { - + services.AddScoped(); } } diff --git a/src/Infrastructure/BotSharp.Core.Rules/Triggers/IRuleTrigger.cs b/src/Infrastructure/BotSharp.Core.Rules/Triggers/IRuleTrigger.cs new file mode 100644 index 00000000..f4925bfe --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/Triggers/IRuleTrigger.cs @@ -0,0 +1,11 @@ +namespace BotSharp.Core.Rules.Triggers; + +public interface IRuleTrigger +{ + string Channel => throw new NotImplementedException("Please set the channel of trigger"); + + string EventName { get; set; } + string EntityType { get; set; } + + string EntityId { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core.Rules/Using.cs b/src/Infrastructure/BotSharp.Core.Rules/Using.cs index d982daae..5a83b419 100644 --- a/src/Infrastructure/BotSharp.Core.Rules/Using.cs +++ b/src/Infrastructure/BotSharp.Core.Rules/Using.cs @@ -1,4 +1,9 @@ +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; + global using BotSharp.Abstraction.Agents.Enums; global using BotSharp.Abstraction.Plugins; -global using Microsoft.Extensions.Configuration; -global using Microsoft.Extensions.DependencyInjection; \ No newline at end of file +global using BotSharp.Abstraction.Agents; +global using BotSharp.Abstraction.Conversations.Models; +global using BotSharp.Abstraction.Instructs; +global using BotSharp.Abstraction.Instructs.Models; \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/agent.json b/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/agent.json new file mode 100644 index 00000000..a58a2ae8 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/agent.json @@ -0,0 +1,13 @@ +{ + "id": "201e49a2-40b3-4ccd-b8cc-2476565a1b40", + "name": "Rules Agent", + "description": "Utility assistant that can be used to complete many different tasks", + "type": "static", + "createdDateTime": "2024-12-30T00:00:00Z", + "updatedDateTime": "2024-12-30T00:00:00Z", + "iconUrl": "https://cdn-icons-png.flaticon.com/512/3143/3143414.png", + "disabled": false, + "isPublic": true, + "profiles": [ "rules" ], + "routingRules": [] +} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/instructions/instruction.liquid b/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/instructions/instruction.liquid new file mode 100644 index 00000000..de39c498 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/instructions/instruction.liquid @@ -0,0 +1,3 @@ +You are a rule interpreter, analyze and respond according to the following steps: +1. Understand the rules customized by the user; +2. Determine whether the input data meets the user's action execution conditions; diff --git a/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/templates/criteria_check.liquid b/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/templates/criteria_check.liquid new file mode 100644 index 00000000..3052e417 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core.Rules/data/agents/201e49a2-40b3-4ccd-b8cc-2476565a1b40/templates/criteria_check.liquid @@ -0,0 +1,4 @@ +You are a rule interpreter, analyze and respond according to the following steps: +1. Understand the rules customized by the user; +2. Determine whether the input data meets the user's action execution conditions; +3. If it meets the conditions, output "true", otherwise output "false" \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 7d4af3ac..3260e867 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -9,7 +9,8 @@ public partial class AgentService { public async Task CreateAgent(Agent agent) { - var userAgents = _db.GetUserAgents(_user.Id); + var userIdentity = _services.GetRequiredService(); + var userAgents = _db.GetUserAgents(userIdentity.Id); var found = userAgents?.FirstOrDefault(x => x.Agent != null && x.Agent.Name.IsEqualTo(agent.Name)); if (found != null) { @@ -24,7 +25,7 @@ public partial class AgentService var dbSettings = _services.GetRequiredService(); var agentSettings = _services.GetRequiredService(); - var user = _db.GetUserById(_user.Id); + var user = _db.GetUserById(userIdentity.Id); var userService = _services.GetRequiredService(); var auth = await userService.GetUserAuthorizations(); diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs index f0838ce5..eb0497ae 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs @@ -16,8 +16,9 @@ public partial class AgentService return refreshResult; } + var userIdentity = _services.GetRequiredService(); var userService = _services.GetRequiredService(); - var isValid = await userService.IsAdminUser(_user.Id); + var isValid = await userService.IsAdminUser(userIdentity.Id); if (!isValid) { return "Unauthorized user."; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs index 1dd331af..80cdc56c 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs @@ -8,20 +8,17 @@ public partial class AgentService : IAgentService private readonly IServiceProvider _services; private readonly IBotSharpRepository _db; private readonly ILogger _logger; - private readonly IUserIdentity _user; private readonly AgentSettings _agentSettings; private readonly JsonSerializerOptions _options; public AgentService(IServiceProvider services, IBotSharpRepository db, ILogger logger, - IUserIdentity user, AgentSettings agentSettings) { _services = services; _db = db; _logger = logger; - _user = user; _agentSettings = agentSettings; _options = new JsonSerializerOptions { diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.Instruct.cs b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.Instruct.cs index 93fa64e2..b2de2f28 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.Instruct.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.Instruct.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Instructs.Models; +using BotSharp.Abstraction.Options; using BotSharp.Abstraction.Templating; using System.Collections; using System.Reflection; @@ -19,6 +20,8 @@ public partial class InstructService try { + var botsharpOptions = _services.GetRequiredService(); + if (IsStringType(type)) { result = response.Content as T; @@ -28,7 +31,8 @@ public partial class InstructService var text = response.Content.JsonArrayContent(); if (!string.IsNullOrWhiteSpace(text)) { - result = JsonSerializer.Deserialize(text, _options.JsonSerializerOptions); + + result = JsonSerializer.Deserialize(text, botsharpOptions.JsonSerializerOptions); } } else @@ -36,7 +40,7 @@ public partial class InstructService var text = response.Content.JsonContent(); if (!string.IsNullOrWhiteSpace(text)) { - result = JsonSerializer.Deserialize(text, _options.JsonSerializerOptions); + result = JsonSerializer.Deserialize(text, botsharpOptions.JsonSerializerOptions); } } } diff --git a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs index a1a31e0e..8ce1076e 100644 --- a/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs +++ b/src/Infrastructure/BotSharp.Core/Instructs/InstructService.cs @@ -1,21 +1,17 @@ using BotSharp.Abstraction.Instructs; -using BotSharp.Abstraction.Options; namespace BotSharp.Core.Instructs; public partial class InstructService : IInstructService { private readonly IServiceProvider _services; - private readonly BotSharpOptions _options; private readonly ILogger _logger; public InstructService( IServiceProvider services, - BotSharpOptions options, ILogger logger) { _services = services; - _options = options; _logger = logger; } }