Merge pull request #814 from iceljc/master

add agent event rule
This commit is contained in:
iceljc 2025-01-06 00:01:47 -06:00 committed by GitHub
commit b8344a9c5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 157 additions and 33 deletions

View file

@ -19,6 +19,7 @@ public enum AgentField
LlmConfig,
Utility,
KnowledgeBase,
EventRule,
MaxMessageCount
}

View file

@ -58,6 +58,4 @@ public interface IAgentService
Task<List<UserAgent>> GetUserAgents(string userId);
PluginDef GetPlugin(string agentId);
IEnumerable<AgentUtility> GetAgentUtilityOptions();
}

View file

@ -99,6 +99,11 @@ public class Agent
/// </summary>
public List<AgentUtility> Utilities { get; set; } = new();
/// <summary>
/// Agent rules
/// </summary>
public List<AgentEventRule> EventRules { get; set; } = new();
/// <summary>
/// Agent knowledge bases
/// </summary>
@ -154,6 +159,7 @@ public class Agent
MaxMessageCount = agent.MaxMessageCount,
Profiles = agent.Profiles,
RoutingRules = agent.RoutingRules,
EventRules = agent.EventRules,
LlmConfig = agent.LlmConfig,
KnowledgeBases = agent.KnowledgeBases,
CreatedDateTime = agent.CreatedDateTime,
@ -269,6 +275,12 @@ public class Agent
return this;
}
public Agent SetEventRules(List<AgentEventRule> rules)
{
EventRules = rules ?? [];
return this;
}
public Agent SetLlmConfig(AgentLlmConfig? llmConfig)
{
LlmConfig = llmConfig;

View file

@ -0,0 +1,13 @@
namespace BotSharp.Abstraction.Agents.Models;
public class AgentEventRule
{
public string Name { get; set; }
public bool Disabled { get; set; }
[JsonPropertyName("event_name")]
public string EventName { get; set; }
[JsonPropertyName("entity_type")]
public string EntityType { get; set; }
}

View file

@ -0,0 +1,8 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Core.Rules.Hooks;
public interface IEventRuleHook
{
void AddRules(List<AgentEventRule> rules);
}

View file

@ -40,6 +40,7 @@ public partial class AgentService
record.Samples = agent.Samples ?? [];
record.Utilities = agent.Utilities ?? [];
record.KnowledgeBases = agent.KnowledgeBases ?? [];
record.EventRules = agent.EventRules ?? [];
if (agent.LlmConfig != null && !agent.LlmConfig.IsInherit)
{
record.LlmConfig = agent.LlmConfig;
@ -104,6 +105,7 @@ public partial class AgentService
.SetSamples(foundAgent.Samples)
.SetUtilities(foundAgent.Utilities)
.SetKnowledgeBases(foundAgent.KnowledgeBases)
.SetEventRules(foundAgent.EventRules)
.SetLlmConfig(foundAgent.LlmConfig);
_db.UpdateAgent(clonedAgent, AgentField.All);

View file

@ -53,15 +53,4 @@ public partial class AgentService : IAgentService
var userAgents = _db.GetUserAgents(userId);
return userAgents;
}
public IEnumerable<AgentUtility> GetAgentUtilityOptions()
{
var utilities = new List<AgentUtility>();
var hooks = _services.GetServices<IAgentUtilityHook>();
foreach (var hook in hooks)
{
hook.AddUtilities(utilities);
}
return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList();
}
}

View file

@ -60,6 +60,9 @@ namespace BotSharp.Core.Repository
case AgentField.KnowledgeBase:
UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases);
break;
case AgentField.EventRule:
UpdateAgentEventRules(agent.Id, agent.EventRules);
break;
case AgentField.MaxMessageCount:
UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount);
break;
@ -184,6 +187,19 @@ namespace BotSharp.Core.Repository
File.WriteAllText(agentFile, json);
}
private void UpdateAgentEventRules(string agentId, List<AgentEventRule> rules)
{
if (rules == null) return;
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
agent.EventRules = rules;
agent.UpdatedDateTime = DateTime.UtcNow;
var json = JsonSerializer.Serialize(agent, _options);
File.WriteAllText(agentFile, json);
}
private void UpdateAgentRoutingRules(string agentId, List<RoutingRule> rules)
{
if (rules == null) return;
@ -240,7 +256,7 @@ namespace BotSharp.Core.Repository
var text = JsonSerializer.Serialize(func, _options);
var file = Path.Combine(functionDir, $"{func.Name}.json");
File.WriteAllText(file, text);
Thread.Sleep(100);
Thread.Sleep(50);
}
}
@ -328,6 +344,7 @@ namespace BotSharp.Core.Repository
agent.Utilities = inputAgent.Utilities;
agent.KnowledgeBases = inputAgent.KnowledgeBases;
agent.RoutingRules = inputAgent.RoutingRules;
agent.EventRules = inputAgent.EventRules;
agent.LlmConfig = inputAgent.LlmConfig;
agent.MaxMessageCount = inputAgent.MaxMessageCount;
agent.UpdatedDateTime = DateTime.UtcNow;

View file

@ -48,6 +48,7 @@
<ItemGroup>
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
<ProjectReference Include="..\BotSharp.Core.Rules\BotSharp.Core.Rules.csproj" />
</ItemGroup>
<ItemGroup>

View file

@ -1,4 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Core.Infrastructures;
using BotSharp.Core.Rules.Hooks;
namespace BotSharp.OpenAPI.Controllers;
@ -152,6 +154,24 @@ public class AgentController : ControllerBase
[HttpGet("/agent/utility/options")]
public IEnumerable<AgentUtility> GetAgentUtilityOptions()
{
return _agentService.GetAgentUtilityOptions();
var utilities = new List<AgentUtility>();
var hooks = _services.GetServices<IAgentUtilityHook>();
foreach (var hook in hooks)
{
hook.AddUtilities(utilities);
}
return utilities.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList();
}
[HttpGet("/agent/event-rule/options")]
public IEnumerable<AgentEventRule> GetAgentEventRuleOptions()
{
var rules = new List<AgentEventRule>();
var hooks = _services.GetServices<IEventRuleHook>();
foreach (var hook in hooks)
{
hook.AddRules(rules);
}
return rules.Where(x => !string.IsNullOrWhiteSpace(x.Name)).OrderBy(x => x.Name).ToList();
}
}

View file

@ -55,6 +55,7 @@ public class AgentCreationModel
public List<AgentUtility> Utilities { get; set; } = new();
public List<RoutingRuleUpdateModel> RoutingRules { get; set; } = new();
public List<AgentKnowledgeBase> KnowledgeBases { get; set; } = new();
public List<AgentEventRule> EventRules { get; set; } = new();
public AgentLlmConfig? LlmConfig { get; set; }
public Agent ToAgent()
@ -78,6 +79,7 @@ public class AgentCreationModel
Profiles = Profiles,
LlmConfig = LlmConfig,
KnowledgeBases = KnowledgeBases,
EventRules = EventRules,
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [],
};
}

View file

@ -75,6 +75,9 @@ public class AgentUpdateModel
[JsonPropertyName("routing_rules")]
public List<RoutingRuleUpdateModel>? RoutingRules { get; set; }
[JsonPropertyName("event_rules")]
public List<AgentEventRule>? EventRules { get; set; }
[JsonPropertyName("llm_config")]
public AgentLlmConfig? LlmConfig { get; set; }
@ -89,15 +92,16 @@ public class AgentUpdateModel
MergeUtility = MergeUtility,
MaxMessageCount = MaxMessageCount,
Type = Type,
Profiles = Profiles ?? new List<string>(),
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? new List<RoutingRule>(),
Profiles = Profiles ?? [],
RoutingRules = RoutingRules?.Select(x => RoutingRuleUpdateModel.ToDomainElement(x))?.ToList() ?? [],
Instruction = Instruction ?? string.Empty,
ChannelInstructions = ChannelInstructions ?? new List<ChannelInstruction>(),
Templates = Templates ?? new List<AgentTemplate>(),
Functions = Functions ?? new List<FunctionDef>(),
Responses = Responses ?? new List<AgentResponse>(),
Utilities = Utilities ?? new List<AgentUtility>(),
ChannelInstructions = ChannelInstructions ?? [],
Templates = Templates ?? [],
Functions = Functions ?? [],
Responses = Responses ?? [],
Utilities = Utilities ?? [],
KnowledgeBases = KnowledgeBases ?? [],
EventRules = EventRules ?? [],
LlmConfig = LlmConfig
};

View file

@ -28,6 +28,9 @@ public class AgentViewModel
[JsonPropertyName("knowledge_bases")]
public List<AgentKnowledgeBase> KnowledgeBases { get; set; }
[JsonPropertyName("event_rules")]
public List<AgentEventRule> EventRules { get; set; }
[JsonPropertyName("is_public")]
public bool IsPublic { get; set; }
@ -72,20 +75,21 @@ public class AgentViewModel
Description = agent.Description,
Type = agent.Type,
Instruction = agent.Instruction,
ChannelInstructions = agent.ChannelInstructions,
Templates = agent.Templates,
Functions = agent.Functions,
Responses = agent.Responses,
Samples = agent.Samples,
Utilities = agent.Utilities,
KnowledgeBases = agent.KnowledgeBases,
ChannelInstructions = agent.ChannelInstructions ?? [],
Templates = agent.Templates ?? [],
Functions = agent.Functions ?? [],
Responses = agent.Responses ?? [],
Samples = agent.Samples ?? [],
Utilities = agent.Utilities ?? [],
KnowledgeBases = agent.KnowledgeBases ?? [],
IsPublic= agent.IsPublic,
Disabled = agent.Disabled,
MergeUtility = agent.MergeUtility,
IconUrl = agent.IconUrl,
MaxMessageCount = agent.MaxMessageCount,
Profiles = agent.Profiles ?? new List<string>(),
RoutingRules = agent.RoutingRules,
Profiles = agent.Profiles ?? [],
RoutingRules = agent.RoutingRules ?? [],
EventRules = agent.EventRules ?? [],
LlmConfig = agent.LlmConfig,
Plugin = agent.Plugin,
CreatedDateTime = agent.CreatedDateTime,

View file

@ -21,6 +21,7 @@ public class AgentDocument : MongoBase
public List<AgentKnowledgeBaseMongoElement> KnowledgeBases { get; set; }
public List<string> Profiles { get; set; }
public List<RoutingRuleMongoElement> RoutingRules { get; set; }
public List<AgentEventRuleMongoElement> EventRules { get; set; }
public AgentLlmConfigMongoElement? LlmConfig { get; set; }
public DateTime CreatedTime { get; set; }

View file

@ -0,0 +1,33 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.Plugin.MongoStorage.Models;
public class AgentEventRuleMongoElement
{
public string Name { get; set; }
public bool Disabled { get; set; }
public string EventName { get; set; }
public string EntityType { get; set; }
public static AgentEventRuleMongoElement ToMongoElement(AgentEventRule rule)
{
return new AgentEventRuleMongoElement
{
Name = rule.Name,
Disabled = rule.Disabled,
EventName = rule.EventName,
EntityType = rule.EntityType
};
}
public static AgentEventRule ToDomainElement(AgentEventRuleMongoElement rule)
{
return new AgentEventRule
{
Name = rule.Name,
Disabled = rule.Disabled,
EventName = rule.EventName,
EntityType = rule.EntityType
};
}
}

View file

@ -61,6 +61,9 @@ public partial class MongoRepository
case AgentField.KnowledgeBase:
UpdateAgentKnowledgeBases(agent.Id, agent.KnowledgeBases);
break;
case AgentField.EventRule:
UpdateAgentEventRules(agent.Id, agent.EventRules);
break;
case AgentField.MaxMessageCount:
UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount);
break;
@ -256,6 +259,20 @@ public partial class MongoRepository
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentEventRules(string agentId, List<AgentEventRule> rules)
{
if (rules == null) return;
var elements = rules?.Select(x => AgentEventRuleMongoElement.ToMongoElement(x))?.ToList() ?? [];
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
var update = Builders<AgentDocument>.Update
.Set(x => x.EventRules, elements)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
_dc.Agents.UpdateOne(filter, update);
}
private void UpdateAgentLlmConfig(string agentId, AgentLlmConfig? config)
{
var llmConfig = AgentLlmConfigMongoElement.ToMongoElement(config);
@ -297,12 +314,12 @@ public partial class MongoRepository
.Set(x => x.Samples, agent.Samples)
.Set(x => x.Utilities, agent.Utilities.Select(u => AgentUtilityMongoElement.ToMongoElement(u)).ToList())
.Set(x => x.KnowledgeBases, agent.KnowledgeBases.Select(u => AgentKnowledgeBaseMongoElement.ToMongoElement(u)).ToList())
.Set(x => x.EventRules, agent.EventRules.Select(e => AgentEventRuleMongoElement.ToMongoElement(e)).ToList())
.Set(x => x.LlmConfig, AgentLlmConfigMongoElement.ToMongoElement(agent.LlmConfig))
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.UpdatedTime, DateTime.UtcNow);
var res = _dc.Agents.UpdateOne(filter, update);
Console.WriteLine();
}
#endregion
@ -455,6 +472,7 @@ public partial class MongoRepository
RoutingRules = x.RoutingRules?.Select(r => RoutingRuleMongoElement.ToMongoElement(r))?.ToList() ?? [],
Utilities = x.Utilities?.Select(u => AgentUtilityMongoElement.ToMongoElement(u))?.ToList() ?? [],
KnowledgeBases = x.KnowledgeBases?.Select(k => AgentKnowledgeBaseMongoElement.ToMongoElement(k))?.ToList() ?? [],
EventRules = x.EventRules?.Select(e => AgentEventRuleMongoElement.ToMongoElement(e))?.ToList() ?? [],
CreatedTime = x.CreatedDateTime,
UpdatedTime = x.UpdatedDateTime
}).ToList();
@ -546,7 +564,8 @@ public partial class MongoRepository
Responses = agentDoc.Responses?.Select(r => AgentResponseMongoElement.ToDomainElement(r))?.ToList() ?? [],
RoutingRules = agentDoc.RoutingRules?.Select(r => RoutingRuleMongoElement.ToDomainElement(agentDoc.Id, agentDoc.Name, r))?.ToList() ?? [],
Utilities = agentDoc.Utilities?.Select(u => AgentUtilityMongoElement.ToDomainElement(u))?.ToList() ?? [],
KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? []
KnowledgeBases = agentDoc.KnowledgeBases?.Select(x => AgentKnowledgeBaseMongoElement.ToDomainElement(x))?.ToList() ?? [],
EventRules = agentDoc.EventRules?.Select(e => AgentEventRuleMongoElement.ToDomainElement(e))?.ToList() ?? []
};
}
}