Skip to content

Commit 5d06d9a

Browse files
author
hchen2020
committed
Merge branch 'master' of https://github.com/hchen2020/BotSharp
2 parents c54e035 + 8552357 commit 5d06d9a

File tree

17 files changed

+714
-313
lines changed

17 files changed

+714
-313
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616
It's written in C# running on .Net Core that is full cross-platform framework, the plug-in and pipeline flow execution design is adopted to completely decouple the plug-ins. C# is a enterprise grade programming language which is widely used to code business logic in information management related system. More friendly to corporate developers. BotSharp adopts machine learning algrithm in C# directly. That will facilitate the feature of the typed language C#, and be more easier when refactoring code in system scope.
1717

18-
Why we do this? Because we all know Python is not friendly programming language for enterprise developers, it's not only because it's low performance but also it's a type weak language, it will be a disaster if you use Python to build your bussiness system.
19-
20-
BotSharp is in accordance with components principle strictly, decouples every part that is needed in the platform builder. So you can choose different UI/UX, or pick up a different NLP Tagger, or select a more advanced algorithm to do NER task. They are all modulized based on unified interfaces.
18+
**BotSharp** is in accordance with components principle strictly, decouples every part that is needed in the platform builder. So you can choose different UI/UX, or pick up a different LLM providers. They are all modulized based on unified interfaces. **BotSharp** provides an advanced Agent abstraction layer to efficiently manage complex application scenarios in enterprises, allowing enterprise developers to efficiently integrate AI into business systems.
2119

2220
### Some Features
2321

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BotSharp.Abstraction.Agents.Enums;
2+
3+
public enum AgentField
4+
{
5+
All = 1,
6+
Name,
7+
Description,
8+
IsPublic,
9+
Instruction,
10+
Function,
11+
Template,
12+
Response
13+
}

src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface IAgentService
1717

1818
Task<Agent> GetAgent(string id);
1919
Task<bool> DeleteAgent(string id);
20-
Task UpdateAgent(Agent agent);
20+
Task UpdateAgent(Agent agent, AgentField updateField);
2121
Task UpdateAgentFromFile(string id);
2222
string GetDataDir();
2323
string GetAgentDataDir(string agentId);

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Agents.Enums;
12
using BotSharp.Abstraction.Routing.Models;
23
using BotSharp.Abstraction.Users.Models;
34

@@ -15,27 +16,32 @@ public interface IBotSharpRepository
1516
int Transaction<TTableInterface>(Action action);
1617
void Add<TTableInterface>(object entity);
1718

19+
#region User
1820
User GetUserByEmail(string email);
1921
void CreateUser(User user);
20-
void UpdateAgent(Agent agent);
21-
22-
List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems);
23-
List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles);
24-
void DeleteRoutingItems();
25-
void DeleteRoutingProfiles();
22+
#endregion
2623

24+
#region Agent
25+
void UpdateAgent(Agent agent, AgentField field);
2726
Agent GetAgent(string agentId);
2827
List<string> GetAgentResponses(string agentId, string prefix, string intent);
28+
string GetAgentTemplate(string agentId, string templateName);
29+
#endregion
2930

31+
#region Conversation
3032
void CreateNewConversation(Conversation conversation);
3133
string GetConversationDialog(string conversationId);
3234
void UpdateConversationDialog(string conversationId, string dialogs);
33-
3435
List<StateKeyValue> GetConversationStates(string conversationId);
3536
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
36-
3737
Conversation GetConversation(string conversationId);
3838
List<Conversation> GetConversations(string userId);
39+
#endregion
3940

40-
string GetAgentTemplate(string agentId, string templateName);
41+
#region Routing
42+
List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems);
43+
List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles);
44+
void DeleteRoutingItems();
45+
void DeleteRoutingProfiles();
46+
#endregion
4147
}

src/Infrastructure/BotSharp.Abstraction/Using.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
global using System.Threading.Tasks;
66
global using System.ComponentModel.DataAnnotations;
77
global using BotSharp.Abstraction.Agents.Models;
8-
global using BotSharp.Abstraction.Conversations.Models;
8+
global using BotSharp.Abstraction.Conversations.Models;
9+
global using BotSharp.Abstraction.Agents.Enums;

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ public partial class AgentService
88
{
99
public async Task<Agent> CreateAgent(Agent agent)
1010
{
11-
var db = _services.GetRequiredService<IBotSharpRepository>();
12-
13-
var agentRecord = (from a in db.Agents
14-
join ua in db.UserAgents on a.Id equals ua.AgentId
15-
join u in db.Users on ua.UserId equals u.Id
11+
var agentRecord = (from a in _db.Agents
12+
join ua in _db.UserAgents on a.Id equals ua.AgentId
13+
join u in _db.Users on ua.UserId equals u.Id
1614
where u.ExternalId == _user.Id && a.Name == agent.Name
1715
select a).FirstOrDefault();
1816

@@ -43,7 +41,7 @@ join u in db.Users on ua.UserId equals u.Id
4341
.SetResponses(foundAgent.Responses);
4442
}
4543

46-
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
44+
var user = _db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
4745
var userAgentRecord = new UserAgent
4846
{
4947
Id = Guid.NewGuid().ToString(),
@@ -53,10 +51,10 @@ join u in db.Users on ua.UserId equals u.Id
5351
UpdatedTime = DateTime.UtcNow
5452
};
5553

56-
db.Transaction<IBotSharpTable>(delegate
54+
_db.Transaction<IBotSharpTable>(delegate
5755
{
58-
db.Add<IBotSharpTable>(agentRecord);
59-
db.Add<IBotSharpTable>(userAgentRecord);
56+
_db.Add<IBotSharpTable>(agentRecord);
57+
_db.Add<IBotSharpTable>(userAgentRecord);
6058
});
6159

6260
return agentRecord;
@@ -86,7 +84,7 @@ private Agent FetchAgentFileByName(string agentName, string filePath)
8684

8785
private string FetchInstructionFromFile(string fileDir)
8886
{
89-
var file = Path.Combine(fileDir, "instruction.liquid");
87+
var file = Path.Combine(fileDir, $"instruction.{_agentSettings.TemplateFormat}");
9088
if (!File.Exists(file)) return null;
9189

9290
var instruction = File.ReadAllText(file);
@@ -102,7 +100,7 @@ private List<AgentTemplate> FetchTemplatesFromFile(string fileDir)
102100
var splits = fileName.ToLower().Split('.');
103101
var name = splits[0];
104102
var extension = splits[1];
105-
if (name != "instruction" && extension == "liquid")
103+
if (name != "instruction" && extension == _agentSettings.TemplateFormat)
106104
{
107105
var content = File.ReadAllText(file);
108106
templates.Add(new AgentTemplate(name, content));

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ public partial class AgentService
77
{
88
public async Task<List<Agent>> GetAgents()
99
{
10-
var db = _services.GetRequiredService<IBotSharpRepository>();
11-
var query = from a in db.Agents
12-
join ua in db.UserAgents on a.Id equals ua.AgentId
13-
join u in db.Users on ua.UserId equals u.Id
10+
var query = from a in _db.Agents
11+
join ua in _db.UserAgents on a.Id equals ua.AgentId
12+
join u in _db.Users on ua.UserId equals u.Id
1413
where ua.UserId == _user.Id || u.ExternalId == _user.Id || a.IsPublic
1514
select a;
1615
return query.ToList();
@@ -21,8 +20,7 @@ join u in db.Users on ua.UserId equals u.Id
2120
#endif
2221
public async Task<Agent> GetAgent(string id)
2322
{
24-
var db = _services.GetRequiredService<IBotSharpRepository>();
25-
var profile = db.GetAgent(id);
23+
var profile = _db.GetAgent(id);
2624

2725
var instructionFile = profile?.Instruction;
2826
if (instructionFile != null)

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,38 @@ namespace BotSharp.Core.Agents.Services;
66

77
public partial class AgentService
88
{
9-
public async Task UpdateAgent(Agent agent)
9+
public async Task UpdateAgent(Agent agent, AgentField updateField)
1010
{
11-
var db = _services.GetRequiredService<IBotSharpRepository>();
12-
13-
var record = (from a in db.Agents
14-
join ua in db.UserAgents on a.Id equals ua.AgentId
15-
join u in db.Users on ua.UserId equals u.Id
16-
where (ua.UserId == _user.Id || u.ExternalId == _user.Id) &&
17-
a.Id == agent.Id
18-
select a).FirstOrDefault();
11+
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;
1912

13+
var record = FindAgent(agent.Id);
2014
if (record == null) return;
2115

22-
record.Name = agent.Name;
23-
24-
if (!string.IsNullOrEmpty(agent.Description))
25-
record.Description = agent.Description;
26-
27-
if (!string.IsNullOrEmpty(agent.Instruction))
28-
record.Instruction = agent.Instruction;
29-
30-
if (!agent.Templates.IsNullOrEmpty())
31-
record.Templates = agent.Templates;
32-
33-
if (!agent.Functions.IsNullOrEmpty())
34-
record.Functions = agent.Functions;
16+
record.Name = agent.Name ?? string.Empty;
17+
record.Description = agent.Description ?? string.Empty;
18+
record.Instruction = agent.Instruction ?? string.Empty;
19+
record.Functions = agent.Functions ?? new List<string>();
20+
record.Templates = agent.Templates ?? new List<AgentTemplate>();
21+
record.Responses = agent.Responses ?? new List<AgentResponse>();
3522

36-
if (!agent.Responses.IsNullOrEmpty())
37-
record.Responses = agent.Responses;
38-
39-
db.UpdateAgent(record);
23+
_db.UpdateAgent(record, updateField);
4024
await Task.CompletedTask;
4125
}
4226

27+
private Agent FindAgent(string agentId)
28+
{
29+
var record = (from a in _db.Agents
30+
join ua in _db.UserAgents on a.Id equals ua.AgentId
31+
join u in _db.Users on ua.UserId equals u.Id
32+
where (ua.UserId == _user.Id || u.ExternalId == _user.Id) &&
33+
a.Id == agentId
34+
select a).FirstOrDefault();
35+
return record;
36+
}
37+
4338
public async Task UpdateAgentFromFile(string id)
4439
{
45-
var db = _services.GetRequiredService<IBotSharpRepository>();
46-
var agent = db.Agents?.FirstOrDefault(x => x.Id == id);
40+
var agent = _db.Agents?.FirstOrDefault(x => x.Id == id);
4741

4842
if (agent == null) return;
4943

@@ -64,10 +58,9 @@ public async Task UpdateAgentFromFile(string id)
6458
.SetFunctions(foundAgent.Functions)
6559
.SetResponses(foundAgent.Responses);
6660

67-
db.UpdateAgent(clonedAgent);
61+
_db.UpdateAgent(clonedAgent, AgentField.All);
6862
}
6963

70-
7164
await Task.CompletedTask;
7265
}
7366

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
using BotSharp.Abstraction.Repositories;
2-
using Microsoft.Extensions.Logging;
32
using System.IO;
43

54
namespace BotSharp.Core.Agents.Services;
65

76
public partial class AgentService : IAgentService
87
{
98
private readonly IServiceProvider _services;
9+
private readonly IBotSharpRepository _db;
1010
private readonly ILogger _logger;
1111
private readonly IUserIdentity _user;
12-
private readonly AgentSettings _settings;
12+
private readonly AgentSettings _agentSettings;
1313
private readonly JsonSerializerOptions _options;
1414

15-
public AgentService(IServiceProvider services,
15+
public AgentService(IServiceProvider services,
16+
IBotSharpRepository db,
1617
ILogger<AgentService> logger,
1718
IUserIdentity user,
18-
AgentSettings settings)
19+
AgentSettings agentSettings)
1920
{
2021
_services = services;
22+
_db = db;
2123
_logger = logger;
2224
_user = user;
23-
_settings = settings;
25+
_agentSettings = agentSettings;
2426
_options = new JsonSerializerOptions
2527
{
2628
PropertyNameCaseInsensitive = true,
@@ -38,7 +40,7 @@ public string GetDataDir()
3840
public string GetAgentDataDir(string agentId)
3941
{
4042
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
41-
var dir = Path.Combine(dbSettings.FileRepository, _settings.DataDir, agentId);
43+
var dir = Path.Combine(dbSettings.FileRepository, _agentSettings.DataDir, agentId);
4244
if (!Directory.Exists(dir))
4345
{
4446
Directory.CreateDirectory(dir);

0 commit comments

Comments
 (0)