This commit is contained in:
Haiping Chen 2025-01-02 15:29:30 -06:00
parent a13f925916
commit 2dbe6c4853
19 changed files with 124 additions and 23 deletions

View file

@ -50,5 +50,5 @@ public class BuiltInAgentId
/// <summary>
/// Translates user-defined natural language rules into programmatic code
/// </summary>
public const string RuleEncoder = "6acfb93c-3412-402e-9ba5-c5d3cd8f0161";
public const string RulesInterpreter = "201e49a2-40b3-4ccd-b8cc-2476565a1b40";
}

View file

@ -1,7 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>$(TargetFramework)</TargetFramework>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<OutputPath>$(SolutionDir)packages</OutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

View file

@ -24,7 +24,7 @@ public class CrontabWatcher : BackgroundService
{
var locker = scope.ServiceProvider.GetRequiredService<IDistributedLocker>();
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.");
}

View file

@ -0,0 +1,5 @@
namespace BotSharp.Core.Rules.Actions;
public interface IRuleAction
{
}

View file

@ -1,11 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>$(TargetFramework)</TargetFramework>
<LangVersion>$(LangVersion)</LangVersion>
<VersionPrefix>$(BotSharpVersion)</VersionPrefix>
<GeneratePackageOnBuild>$(GeneratePackageOnBuild)</GeneratePackageOnBuild>
<OutputPath>$(SolutionDir)packages</OutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Content Include="data\agents\201e49a2-40b3-4ccd-b8cc-2476565a1b40\agent.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\201e49a2-40b3-4ccd-b8cc-2476565a1b40\instructions\instruction.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\201e49a2-40b3-4ccd-b8cc-2476565a1b40\templates\criteria_check.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Infrastructure\BotSharp.Abstraction\BotSharp.Abstraction.csproj" />
</ItemGroup>

View file

@ -0,0 +1,5 @@
namespace BotSharp.Core.Rules.Criterias;
public interface IRuleCriteria
{
}

View file

@ -0,0 +1,8 @@
using BotSharp.Core.Rules.Triggers;
namespace BotSharp.Core.Rules.Engines;
public interface IRuleEngine
{
Task Triggered(IRuleTrigger trigger, string data);
}

View file

@ -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<IInstructService>();
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
}
}

View file

@ -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<IRuleEngine, RuleEngine>();
}
}

View file

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

View file

@ -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;
global using BotSharp.Abstraction.Agents;
global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Instructs;
global using BotSharp.Abstraction.Instructs.Models;

View file

@ -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": []
}

View file

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

View file

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

View file

@ -9,7 +9,8 @@ public partial class AgentService
{
public async Task<Agent> CreateAgent(Agent agent)
{
var userAgents = _db.GetUserAgents(_user.Id);
var userIdentity = _services.GetRequiredService<IUserIdentity>();
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<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
var user = _db.GetUserById(_user.Id);
var user = _db.GetUserById(userIdentity.Id);
var userService = _services.GetRequiredService<IUserService>();
var auth = await userService.GetUserAuthorizations();

View file

@ -16,8 +16,9 @@ public partial class AgentService
return refreshResult;
}
var userIdentity = _services.GetRequiredService<IUserIdentity>();
var userService = _services.GetRequiredService<IUserService>();
var isValid = await userService.IsAdminUser(_user.Id);
var isValid = await userService.IsAdminUser(userIdentity.Id);
if (!isValid)
{
return "Unauthorized user.";

View file

@ -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<AgentService> logger,
IUserIdentity user,
AgentSettings agentSettings)
{
_services = services;
_db = db;
_logger = logger;
_user = user;
_agentSettings = agentSettings;
_options = new JsonSerializerOptions
{

View file

@ -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<BotSharpOptions>();
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<T>(text, _options.JsonSerializerOptions);
result = JsonSerializer.Deserialize<T>(text, botsharpOptions.JsonSerializerOptions);
}
}
else
@ -36,7 +40,7 @@ public partial class InstructService
var text = response.Content.JsonContent();
if (!string.IsNullOrWhiteSpace(text))
{
result = JsonSerializer.Deserialize<T>(text, _options.JsonSerializerOptions);
result = JsonSerializer.Deserialize<T>(text, botsharpOptions.JsonSerializerOptions);
}
}
}

View file

@ -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<InstructService> _logger;
public InstructService(
IServiceProvider services,
BotSharpOptions options,
ILogger<InstructService> logger)
{
_services = services;
_options = options;
_logger = logger;
}
}