Skip to content

Commit 8552357

Browse files
authored
Merge pull request #145 from iceljc/features/add-update-agent
Features/add update agent
2 parents 3ef841d + 1376326 commit 8552357

File tree

16 files changed

+713
-310
lines changed

16 files changed

+713
-310
lines changed
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);

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,89 +71,99 @@ public int Transaction<TTableInterface>(Action action)
7171
}
7272

7373

74-
75-
public void CreateNewConversation(Conversation conversation)
74+
#region Agent
75+
public Agent GetAgent(string agentId)
7676
{
7777
throw new NotImplementedException();
7878
}
7979

80-
public List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems)
80+
public void UpdateAgent(Agent agent, AgentField field)
8181
{
8282
throw new NotImplementedException();
8383
}
8484

85-
public List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles)
85+
public string GetAgentTemplate(string agentId, string templateName)
8686
{
8787
throw new NotImplementedException();
8888
}
8989

90-
public void CreateUser(User user)
90+
public List<string> GetAgentResponses(string agentId, string prefix, string intent)
9191
{
9292
throw new NotImplementedException();
9393
}
94+
#endregion
9495

95-
public void DeleteRoutingItems()
96+
97+
#region Conversation
98+
public void CreateNewConversation(Conversation conversation)
9699
{
97100
throw new NotImplementedException();
98101
}
99102

100-
public void DeleteRoutingProfiles()
103+
public Conversation GetConversation(string conversationId)
101104
{
102105
throw new NotImplementedException();
103106
}
104107

105-
public Agent GetAgent(string agentId)
108+
public List<Conversation> GetConversations(string userId)
106109
{
107110
throw new NotImplementedException();
108111
}
109112

110-
public List<string> GetAgentResponses(string agentId, string prefix, string intent)
113+
public string GetConversationDialog(string conversationId)
111114
{
112115
throw new NotImplementedException();
113116
}
114117

115-
public Conversation GetConversation(string conversationId)
118+
public List<StateKeyValue> GetConversationStates(string conversationId)
116119
{
117120
throw new NotImplementedException();
118121
}
119122

120-
public string GetConversationDialog(string conversationId)
123+
public void UpdateConversationDialog(string conversationId, string dialogs)
121124
{
122125
throw new NotImplementedException();
123126
}
124127

125-
public List<Conversation> GetConversations(string userId)
128+
public void UpdateConversationStates(string conversationId, List<StateKeyValue> states)
126129
{
127130
throw new NotImplementedException();
128131
}
132+
#endregion
129133

130-
public List<StateKeyValue> GetConversationStates(string conversationId)
134+
135+
#region User
136+
public User GetUserByEmail(string email)
131137
{
132138
throw new NotImplementedException();
133139
}
134140

135-
public User GetUserByEmail(string email)
141+
public void CreateUser(User user)
136142
{
137143
throw new NotImplementedException();
138144
}
145+
#endregion
146+
139147

140-
public void UpdateAgent(Agent agent)
148+
#region Routing
149+
public List<RoutingItem> CreateRoutingItems(List<RoutingItem> routingItems)
141150
{
142151
throw new NotImplementedException();
143152
}
144153

145-
public void UpdateConversationDialog(string conversationId, string dialogs)
154+
public List<RoutingProfile> CreateRoutingProfiles(List<RoutingProfile> profiles)
146155
{
147156
throw new NotImplementedException();
148157
}
149158

150-
public void UpdateConversationStates(string conversationId, List<StateKeyValue> states)
159+
public void DeleteRoutingItems()
151160
{
152161
throw new NotImplementedException();
153162
}
154163

155-
public string GetAgentTemplate(string agentId, string templateName)
164+
public void DeleteRoutingProfiles()
156165
{
157166
throw new NotImplementedException();
158167
}
168+
#endregion
159169
}

0 commit comments

Comments
 (0)