Skip to content

Commit 0299fb4

Browse files
author
Jicheng Lu
committed
refine agent update in mongo
1 parent 3347d27 commit 0299fb4

File tree

16 files changed

+508
-284
lines changed

16 files changed

+508
-284
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: 7 additions & 9 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;

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: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,31 @@ 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;
35-
36-
if (!agent.Responses.IsNullOrEmpty())
37-
record.Responses = agent.Responses;
38-
39-
db.UpdateAgent(record);
16+
_db.UpdateAgent(record, updateField);
4017
await Task.CompletedTask;
4118
}
4219

20+
private Agent FindAgent(string agentId)
21+
{
22+
var record = (from a in _db.Agents
23+
join ua in _db.UserAgents on a.Id equals ua.AgentId
24+
join u in _db.Users on ua.UserId equals u.Id
25+
where (ua.UserId == _user.Id || u.ExternalId == _user.Id) &&
26+
a.Id == agentId
27+
select a).FirstOrDefault();
28+
return record;
29+
}
30+
4331
public async Task UpdateAgentFromFile(string id)
4432
{
45-
var db = _services.GetRequiredService<IBotSharpRepository>();
46-
var agent = db.Agents?.FirstOrDefault(x => x.Id == id);
33+
var agent = _db.Agents?.FirstOrDefault(x => x.Id == id);
4734

4835
if (agent == null) return;
4936

@@ -64,10 +51,9 @@ public async Task UpdateAgentFromFile(string id)
6451
.SetFunctions(foundAgent.Functions)
6552
.SetResponses(foundAgent.Responses);
6653

67-
db.UpdateAgent(clonedAgent);
54+
_db.UpdateAgent(clonedAgent, AgentField.All);
6855
}
6956

70-
7157
await Task.CompletedTask;
7258
}
7359

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
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;
1212
private readonly AgentSettings _settings;
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,
1819
AgentSettings settings)
1920
{
2021
_services = services;
22+
_db = db;
2123
_logger = logger;
2224
_user = user;
2325
_settings = settings;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Agents.Enums;
12
using BotSharp.Abstraction.Agents.Models;
23
using BotSharp.Abstraction.Repositories;
34
using BotSharp.Abstraction.Routing.Models;
@@ -137,7 +138,7 @@ public User GetUserByEmail(string email)
137138
throw new NotImplementedException();
138139
}
139140

140-
public void UpdateAgent(Agent agent)
141+
public void UpdateAgent(Agent agent, AgentField field)
141142
{
142143
throw new NotImplementedException();
143144
}

0 commit comments

Comments
 (0)