Skip to content

Commit 5380ecb

Browse files
authored
Merge pull request #405 from iceljc/features/refine-agent-refresh
refine agent refresh
2 parents 5564287 + e4e3ee5 commit 5380ecb

File tree

11 files changed

+151
-116
lines changed

11 files changed

+151
-116
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BotSharp.Abstraction.Agents;
1010
public interface IAgentService
1111
{
1212
Task<Agent> CreateAgent(Agent agent);
13-
Task RefreshAgents();
13+
Task<string> RefreshAgents();
1414
Task<PagedItems<Agent>> GetAgents(AgentFilter filter);
1515

1616
/// <summary>
@@ -37,7 +37,7 @@ public interface IAgentService
3737

3838
Task<bool> DeleteAgent(string id);
3939
Task UpdateAgent(Agent agent, AgentField updateField);
40-
Task UpdateAgentFromFile(string id);
40+
Task<string> UpdateAgentFromFile(string id);
4141
string GetDataDir();
4242
string GetAgentDataDir(string agentId);
4343

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public interface IBotSharpRepository
3232
void BulkInsertAgents(List<Agent> agents);
3333
void BulkInsertUserAgents(List<UserAgent> userAgents);
3434
bool DeleteAgents();
35+
bool DeleteAgent(string agentId);
3536
List<string> GetAgentResponses(string agentId, string prefix, string intent);
3637
string GetAgentTemplate(string agentId, string templateName);
3738
#endregion
@@ -42,7 +43,7 @@ public interface IBotSharpRepository
4243
void InsertAgentTask(AgentTask task);
4344
void BulkInsertAgentTasks(List<AgentTask> tasks);
4445
void UpdateAgentTask(AgentTask task, AgentTaskField field);
45-
bool DeleteAgentTask(string agentId, string taskId);
46+
bool DeleteAgentTask(string agentId, List<string> taskIds);
4647
bool DeleteAgentTasks();
4748
#endregion
4849

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,79 @@
1-
using BotSharp.Abstraction.Tasks.Models;
21
using System.IO;
32

43
namespace BotSharp.Core.Agents.Services;
54

65
public partial class AgentService
76
{
8-
public async Task RefreshAgents()
7+
public async Task<string> RefreshAgents()
98
{
10-
var isAgentDeleted = _db.DeleteAgents();
11-
var isTaskDeleted = _db.DeleteAgentTasks();
12-
if (!isAgentDeleted) return;
13-
149
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
1510
var agentDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
1611
dbSettings.FileRepository,
1712
_agentSettings.DataDir);
1813

14+
string refreshResult;
15+
if (!Directory.Exists(agentDir))
16+
{
17+
refreshResult = $"Cannot find the directory: {agentDir}";
18+
return refreshResult;
19+
}
20+
1921
var user = _db.GetUserById(_user.Id);
20-
var agents = new List<Agent>();
21-
var userAgents = new List<UserAgent>();
22-
var agentTasks = new List<AgentTask>();
22+
var refreshedAgents = new List<string>();
2323

2424
foreach (var dir in Directory.GetDirectories(agentDir))
2525
{
26-
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
27-
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
28-
if (agent == null) continue;
29-
30-
var functions = FetchFunctionsFromFile(dir);
31-
var instruction = FetchInstructionFromFile(dir);
32-
var responses = FetchResponsesFromFile(dir);
33-
var templates = FetchTemplatesFromFile(dir);
34-
var samples = FetchSamplesFromFile(dir);
35-
agent.SetInstruction(instruction)
36-
.SetTemplates(templates)
37-
.SetFunctions(functions)
38-
.SetResponses(responses)
39-
.SetSamples(samples);
40-
agents.Add(agent);
41-
42-
var userAgent = BuildUserAgent(agent.Id, user.Id);
43-
userAgents.Add(userAgent);
44-
45-
var tasks = FetchTasksFromFile(dir);
46-
agentTasks.AddRange(tasks);
26+
try
27+
{
28+
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
29+
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
30+
31+
if (agent == null)
32+
{
33+
_logger.LogError($"Cannot find agent in file directory: {dir}");
34+
continue;
35+
}
36+
37+
var functions = FetchFunctionsFromFile(dir);
38+
var instruction = FetchInstructionFromFile(dir);
39+
var responses = FetchResponsesFromFile(dir);
40+
var templates = FetchTemplatesFromFile(dir);
41+
var samples = FetchSamplesFromFile(dir);
42+
agent.SetInstruction(instruction)
43+
.SetTemplates(templates)
44+
.SetFunctions(functions)
45+
.SetResponses(responses)
46+
.SetSamples(samples);
47+
48+
var userAgent = BuildUserAgent(agent.Id, user.Id);
49+
var tasks = FetchTasksFromFile(dir);
50+
51+
var isAgentDeleted = _db.DeleteAgent(agent.Id);
52+
if (isAgentDeleted)
53+
{
54+
_db.BulkInsertAgents(new List<Agent> { agent });
55+
_db.BulkInsertUserAgents(new List<UserAgent> { userAgent });
56+
_db.BulkInsertAgentTasks(tasks);
57+
refreshedAgents.Add(agent.Name);
58+
}
59+
}
60+
catch (Exception ex)
61+
{
62+
_logger.LogError($"Failed to migrate agent in file directory: {dir}\r\nError: {ex.Message}");
63+
}
4764
}
4865

49-
_db.BulkInsertAgents(agents);
50-
_db.BulkInsertUserAgents(userAgents);
51-
_db.BulkInsertAgentTasks(agentTasks);
66+
if (!refreshedAgents.IsNullOrEmpty())
67+
{
68+
Utilities.ClearCache();
69+
refreshResult = $"Agents are migrated! {string.Join("\r\n", refreshedAgents)}";
70+
}
71+
else
72+
{
73+
refreshResult = "No agent gets refreshed!";
74+
}
5275

53-
Utilities.ClearCache();
76+
_logger.LogInformation(refreshResult);
77+
return refreshResult;
5478
}
5579
}

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
3939
await Task.CompletedTask;
4040
}
4141

42-
public async Task UpdateAgentFromFile(string id)
42+
public async Task<string> UpdateAgentFromFile(string id)
4343
{
4444
var agent = _db.GetAgent(id);
45-
46-
if (agent == null) return;
45+
if (agent == null)
46+
{
47+
return $"Cannot find agent ${id}";
48+
}
4749

4850
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
4951
var agentSettings = _services.GetRequiredService<AgentSettings>();
@@ -53,7 +55,12 @@ public async Task UpdateAgentFromFile(string id)
5355

5456
var clonedAgent = Agent.Clone(agent);
5557
var foundAgent = FetchAgentFileById(agent.Id, filePath);
56-
if (foundAgent != null)
58+
if (foundAgent == null)
59+
{
60+
return $"Cannot find agent {agent.Name} in file directory: {filePath}";
61+
}
62+
63+
try
5764
{
5865
clonedAgent.SetId(foundAgent.Id)
5966
.SetName(foundAgent.Name)
@@ -71,15 +78,19 @@ public async Task UpdateAgentFromFile(string id)
7178
.SetLlmConfig(foundAgent.LlmConfig);
7279

7380
_db.UpdateAgent(clonedAgent, AgentField.All);
74-
7581
Utilities.ClearCache();
82+
return $"Agent {agent.Name} has been migrated!";
83+
}
84+
catch (Exception ex)
85+
{
86+
return $"Failed to migrate agent {agent.Name} in file directory {filePath}.\r\nError: {ex.Message}";
7687
}
77-
78-
await Task.CompletedTask;
7988
}
8089

81-
private Agent FetchAgentFileById(string agentId, string filePath)
90+
private Agent? FetchAgentFileById(string agentId, string filePath)
8291
{
92+
if (!Directory.Exists(filePath)) return null;
93+
8394
foreach (var dir in Directory.GetDirectories(filePath))
8495
{
8596
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));

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

Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -73,86 +73,57 @@ public int Transaction<TTableInterface>(Action action)
7373

7474
#region Agent
7575
public Agent GetAgent(string agentId)
76-
{
77-
throw new NotImplementedException();
78-
}
76+
=> throw new NotImplementedException();
7977

8078
public List<Agent> GetAgents(AgentFilter filter)
81-
{
82-
throw new NotImplementedException();
83-
}
79+
=> throw new NotImplementedException();
8480

8581
public List<Agent> GetAgentsByUser(string userId)
86-
{
87-
throw new NotImplementedException();
88-
}
82+
=> throw new NotImplementedException();
8983

9084
public void UpdateAgent(Agent agent, AgentField field)
91-
{
92-
throw new NotImplementedException();
93-
}
85+
=> throw new NotImplementedException();
9486

9587
public string GetAgentTemplate(string agentId, string templateName)
96-
{
97-
throw new NotImplementedException();
98-
}
88+
=> throw new NotImplementedException();
9989

10090
public List<string> GetAgentResponses(string agentId, string prefix, string intent)
101-
{
102-
throw new NotImplementedException();
103-
}
91+
=> throw new NotImplementedException();
10492

10593
public void BulkInsertAgents(List<Agent> agents)
106-
{
107-
throw new NotImplementedException();
108-
}
94+
=> throw new NotImplementedException();
10995

11096
public void BulkInsertUserAgents(List<UserAgent> userAgents)
111-
{
112-
throw new NotImplementedException();
113-
}
97+
=> throw new NotImplementedException();
11498

11599
public bool DeleteAgents()
116-
{
117-
throw new NotImplementedException();
118-
}
100+
=> throw new NotImplementedException();
101+
102+
public bool DeleteAgent(string agentId)
103+
=> throw new NotImplementedException();
119104
#endregion
120105

121106
#region Agent Task
122107
public PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter)
123-
{
124-
throw new NotImplementedException();
125-
}
108+
=> throw new NotImplementedException();
126109

127110
public AgentTask? GetAgentTask(string agentId, string taskId)
128-
{
129-
throw new NotImplementedException();
130-
}
111+
=> throw new NotImplementedException();
131112

132113
public void InsertAgentTask(AgentTask task)
133-
{
134-
throw new NotImplementedException();
135-
}
114+
=> throw new NotImplementedException();
136115

137116
public void BulkInsertAgentTasks(List<AgentTask> tasks)
138-
{
139-
throw new NotImplementedException();
140-
}
117+
=> throw new NotImplementedException();
141118

142119
public void UpdateAgentTask(AgentTask task, AgentTaskField field)
143-
{
144-
throw new NotImplementedException();
145-
}
120+
=> throw new NotImplementedException();
146121

147-
public bool DeleteAgentTask(string agentId, string taskId)
148-
{
149-
throw new NotImplementedException();
150-
}
122+
public bool DeleteAgentTask(string agentId, List<string> taskIds)
123+
=> throw new NotImplementedException();
151124

152125
public bool DeleteAgentTasks()
153-
{
154-
throw new NotImplementedException();
155-
}
126+
=> throw new NotImplementedException();
156127
#endregion
157128

158129
#region Conversation

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Agent.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Functions.Models;
3-
using BotSharp.Abstraction.Repositories.Filters;
41
using BotSharp.Abstraction.Routing.Models;
5-
using BotSharp.Abstraction.Tasks.Models;
6-
using Microsoft.Extensions.Logging;
72
using System.IO;
83

94
namespace BotSharp.Core.Repository
@@ -419,5 +414,10 @@ public bool DeleteAgents()
419414
{
420415
return false;
421416
}
417+
418+
public bool DeleteAgent(string agentId)
419+
{
420+
return false;
421+
}
422422
}
423423
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using BotSharp.Abstraction.Repositories.Filters;
21
using BotSharp.Abstraction.Tasks.Models;
32
using System.IO;
4-
using System.Threading.Tasks;
53

64
namespace BotSharp.Core.Repository;
75

@@ -192,19 +190,25 @@ public void UpdateAgentTask(AgentTask task, AgentTaskField field)
192190
File.WriteAllText(taskFile, fileContent);
193191
}
194192

195-
public bool DeleteAgentTask(string agentId, string taskId)
193+
public bool DeleteAgentTask(string agentId, List<string> taskIds)
196194
{
197195
var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir, agentId);
198-
if (!Directory.Exists(agentDir)) return false;
196+
if (!Directory.Exists(agentDir) || taskIds.IsNullOrEmpty()) return false;
199197

200198
var taskDir = Path.Combine(agentDir, "tasks");
201199
if (!Directory.Exists(taskDir)) return false;
202200

203-
var taskFile = FindTaskFileById(taskDir, taskId);
204-
if (string.IsNullOrWhiteSpace(taskFile)) return false;
201+
var deletedTasks = new List<string>();
202+
foreach (var taskId in taskIds)
203+
{
204+
var taskFile = FindTaskFileById(taskDir, taskId);
205+
if (string.IsNullOrWhiteSpace(taskFile)) continue;
205206

206-
File.Delete(taskFile);
207-
return true;
207+
File.Delete(taskFile);
208+
deletedTasks.Add(taskId);
209+
}
210+
211+
return deletedTasks.Any();
208212
}
209213

210214
public bool DeleteAgentTasks()

src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task UpdateTask(AgentTask task, AgentTaskField field)
7272
public async Task<bool> DeleteTask(string agentId, string taskId)
7373
{
7474
var db = _services.GetRequiredService<IBotSharpRepository>();
75-
var isDeleted = db.DeleteAgentTask(agentId, taskId);
75+
var isDeleted = db.DeleteAgentTask(agentId, new List<string> { taskId });
7676
return await Task.FromResult(isDeleted);
7777
}
7878
}

0 commit comments

Comments
 (0)