Merge branch 'SciSharp:master' into master

This commit is contained in:
Haiping 2025-01-08 02:48:54 +00:00 committed by GitHub
commit d793605555
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 209 additions and 45 deletions

View file

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

View file

@ -0,0 +1,6 @@
namespace BotSharp.Abstraction.Agents;
public interface IAgentRuleHook
{
void AddRules(List<AgentRule> rules);
}

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<AgentRule> Rules { get; set; } = new();
/// <summary>
/// Agent knowledge bases
/// </summary>
@ -154,6 +159,7 @@ public class Agent
MaxMessageCount = agent.MaxMessageCount,
Profiles = agent.Profiles,
RoutingRules = agent.RoutingRules,
Rules = agent.Rules,
LlmConfig = agent.LlmConfig,
KnowledgeBases = agent.KnowledgeBases,
CreatedDateTime = agent.CreatedDateTime,
@ -269,6 +275,12 @@ public class Agent
return this;
}
public Agent SetRules(List<AgentRule> rules)
{
Rules = rules ?? [];
return this;
}
public Agent SetLlmConfig(AgentLlmConfig? llmConfig)
{
LlmConfig = llmConfig;

View file

@ -0,0 +1,13 @@
namespace BotSharp.Abstraction.Agents.Models;
public class AgentRule
{
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 System.Net.Http.Headers;
namespace BotSharp.Abstraction.Http;
public interface IHttpRequestHook
{
void OnAddHttpHeaders(HttpHeaders headers);
}

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Users.Models;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
namespace BotSharp.Abstraction.Users;

View file

@ -40,6 +40,7 @@ public partial class AgentService
record.Samples = agent.Samples ?? [];
record.Utilities = agent.Utilities ?? [];
record.KnowledgeBases = agent.KnowledgeBases ?? [];
record.Rules = agent.Rules ?? [];
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)
.SetRules(foundAgent.Rules)
.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.Rule:
UpdateAgentRules(agent.Id, agent.Rules);
break;
case AgentField.MaxMessageCount:
UpdateAgentMaxMessageCount(agent.Id, agent.MaxMessageCount);
break;
@ -184,6 +187,19 @@ namespace BotSharp.Core.Repository
File.WriteAllText(agentFile, json);
}
private void UpdateAgentRules(string agentId, List<AgentRule> rules)
{
if (rules == null) return;
var (agent, agentFile) = GetAgentFromFile(agentId);
if (agent == null) return;
agent.Rules = 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.Rules = inputAgent.Rules;
agent.LlmConfig = inputAgent.LlmConfig;
agent.MaxMessageCount = inputAgent.MaxMessageCount;
agent.UpdatedDateTime = DateTime.UtcNow;

View file

@ -152,6 +152,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/rule/options")]
public IEnumerable<AgentRule> GetAgentRuleOptions()
{
var rules = new List<AgentRule>();
var hooks = _services.GetServices<IAgentRuleHook>();
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<AgentRule> Rules { get; set; } = new();
public AgentLlmConfig? LlmConfig { get; set; }
public Agent ToAgent()
@ -78,6 +79,7 @@ public class AgentCreationModel
Profiles = Profiles,
LlmConfig = LlmConfig,
KnowledgeBases = KnowledgeBases,
Rules = Rules,
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("rules")]
public List<AgentRule>? Rules { 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 ?? [],
Rules = Rules ?? [],
LlmConfig = LlmConfig
};

View file

@ -28,6 +28,9 @@ public class AgentViewModel
[JsonPropertyName("knowledge_bases")]
public List<AgentKnowledgeBase> KnowledgeBases { get; set; }
[JsonPropertyName("rules")]
public List<AgentRule> Rules { 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 ?? [],
Rules = agent.Rules ?? [],
LlmConfig = agent.LlmConfig,
Plugin = agent.Plugin,
CreatedDateTime = agent.CreatedDateTime,

View file

@ -1,5 +1,6 @@
using System.Net.Http;
using System.Net.Mime;
using BotSharp.Abstraction.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
@ -13,7 +14,6 @@ public class HandleHttpRequestFn : IFunctionCallback
private readonly IServiceProvider _services;
private readonly ILogger<HandleHttpRequestFn> _logger;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IHttpContextAccessor _context;
private readonly BotSharpOptions _options;
public HandleHttpRequestFn(IServiceProvider services,
@ -25,7 +25,6 @@ public class HandleHttpRequestFn : IFunctionCallback
_services = services;
_logger = logger;
_httpClientFactory = httpClientFactory;
_context = context;
_options = options;
}
@ -46,7 +45,7 @@ public class HandleHttpRequestFn : IFunctionCallback
catch (Exception ex)
{
var msg = $"Fail when sending http request. Url: {url}, method: {method}, content: {content}";
_logger.LogWarning($"{msg}\n(Error: {ex.Message})");
_logger.LogError($"{msg}\n(Error: {ex.Message}\r\n{ex.InnerException})");
message.Content = msg;
return false;
}
@ -57,7 +56,7 @@ public class HandleHttpRequestFn : IFunctionCallback
if (string.IsNullOrEmpty(url)) return null;
using var client = _httpClientFactory.CreateClient();
AddRequestHeaders(client);
PrepareRequestHeaders(client);
var (uri, request) = BuildHttpRequest(url, method, content);
var response = await client.SendAsync(request);
@ -69,15 +68,12 @@ public class HandleHttpRequestFn : IFunctionCallback
return response;
}
private void AddRequestHeaders(HttpClient client)
private void PrepareRequestHeaders(HttpClient client)
{
client.DefaultRequestHeaders.Add("Authorization", $"{_context.HttpContext.Request.Headers["Authorization"]}");
var settings = _services.GetRequiredService<HttpHandlerSettings>();
var origin = !string.IsNullOrEmpty(settings.Origin) ? settings.Origin : $"{_context.HttpContext.Request.Headers["Origin"]}";
if (!string.IsNullOrEmpty(origin))
var hooks = _services.GetServices<IHttpRequestHook>();
foreach (var hook in hooks)
{
client.DefaultRequestHeaders.Add("Origin", origin);
hook.OnAddHttpHeaders(client.DefaultRequestHeaders);
}
}

View file

@ -0,0 +1,40 @@
using BotSharp.Abstraction.Http;
using Microsoft.AspNetCore.Http;
using System.Net.Http.Headers;
namespace BotSharp.Plugin.HttpHandler.Hooks;
public class BasicHttpRequestHook : IHttpRequestHook
{
private readonly IServiceProvider _services;
private readonly IHttpContextAccessor _context;
private const string AUTHORIZATION = "Authorization";
private const string ORIGIN = "Origin";
public BasicHttpRequestHook(
IServiceProvider services,
IHttpContextAccessor context)
{
_services = services;
_context = context;
}
public void OnAddHttpHeaders(HttpHeaders headers)
{
var settings = _services.GetRequiredService<HttpHandlerSettings>();
var auth = $"{_context.HttpContext.Request.Headers[AUTHORIZATION]}";
if (!string.IsNullOrEmpty(auth))
{
headers.Add(AUTHORIZATION, auth);
}
var origin = $"{_context.HttpContext.Request.Headers[ORIGIN]}";
origin = !string.IsNullOrEmpty(settings.Origin) ? settings.Origin : origin;
if (!string.IsNullOrEmpty(origin))
{
headers.Add(ORIGIN, origin);
}
}
}

View file

@ -1,4 +1,5 @@
using BotSharp.Abstraction.Agents;
using BotSharp.Abstraction.Http;
using BotSharp.Abstraction.Settings;
using Microsoft.Extensions.Configuration;
@ -21,5 +22,6 @@ public class HttpHandlerPlugin : IBotSharpPlugin
});
services.AddScoped<IAgentUtilityHook, HttpHandlerUtilityHook>();
services.AddScoped<IHttpRequestHook, BasicHttpRequestHook>();
}
}

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<AgentRuleMongoElement> Rules { 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 AgentRuleMongoElement
{
public string Name { get; set; }
public bool Disabled { get; set; }
public string EventName { get; set; }
public string EntityType { get; set; }
public static AgentRuleMongoElement ToMongoElement(AgentRule rule)
{
return new AgentRuleMongoElement
{
Name = rule.Name,
Disabled = rule.Disabled,
EventName = rule.EventName,
EntityType = rule.EntityType
};
}
public static AgentRule ToDomainElement(AgentRuleMongoElement rule)
{
return new AgentRule
{
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.Rule:
UpdateAgentRules(agent.Id, agent.Rules);
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 UpdateAgentRules(string agentId, List<AgentRule> rules)
{
if (rules == null) return;
var elements = rules?.Select(x => AgentRuleMongoElement.ToMongoElement(x))?.ToList() ?? [];
var filter = Builders<AgentDocument>.Filter.Eq(x => x.Id, agentId);
var update = Builders<AgentDocument>.Update
.Set(x => x.Rules, 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.Rules, agent.Rules.Select(e => AgentRuleMongoElement.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() ?? [],
Rules = x.Rules?.Select(e => AgentRuleMongoElement.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() ?? [],
Rules = agentDoc.Rules?.Select(e => AgentRuleMongoElement.ToDomainElement(e))?.ToList() ?? []
};
}
}