Skip to content

Commit 4ad51f0

Browse files
author
Jicheng Lu
committed
add update agent from file
1 parent be3150a commit 4ad51f0

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IAgentService
1818
Task<Agent> GetAgent(string id);
1919
Task<bool> DeleteAgent(string id);
2020
Task UpdateAgent(Agent agent);
21+
Task UpdateAgentFromFile(string id);
2122
string GetDataDir();
2223
string GetAgentDataDir(string agentId);
2324
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ join u in db.Users on ua.UserId equals u.Id
2929
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
3030
var agentSettings = _services.GetRequiredService<AgentSettings>();
3131
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
32-
var foundAgent = FetchAgentInfoFromFile(agent.Name, filePath);
32+
var foundAgent = FetchAgentFileByName(agent.Name, filePath);
3333

3434
if (foundAgent != null)
3535
{
@@ -62,7 +62,7 @@ join u in db.Users on ua.UserId equals u.Id
6262
return agentRecord;
6363
}
6464

65-
private Agent FetchAgentInfoFromFile(string agentName, string filePath)
65+
private Agent FetchAgentFileByName(string agentName, string filePath)
6666
{
6767
foreach (var dir in Directory.GetDirectories(filePath))
6868
{

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ join u in db.Users on ua.UserId equals u.Id
2727
if (!string.IsNullOrEmpty(agent.Instruction))
2828
record.Instruction = agent.Instruction;
2929

30+
if (!agent.Templates.IsNullOrEmpty())
31+
record.Templates = agent.Templates;
32+
3033
if (!agent.Functions.IsNullOrEmpty())
3134
record.Functions = agent.Functions;
3235

@@ -36,4 +39,57 @@ join u in db.Users on ua.UserId equals u.Id
3639
db.UpdateAgent(record);
3740
await Task.CompletedTask;
3841
}
42+
43+
public async Task UpdateAgentFromFile(string id)
44+
{
45+
var db = _services.GetRequiredService<IBotSharpRepository>();
46+
var agent = db.Agents?.FirstOrDefault(x => x.Id == id);
47+
48+
if (agent == null) return;
49+
50+
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
51+
var agentSettings = _services.GetRequiredService<AgentSettings>();
52+
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
53+
54+
var clonedAgent = Agent.Clone(agent);
55+
var foundAgent = FetchAgentFileById(agent.Id, filePath);
56+
if (foundAgent != null)
57+
{
58+
clonedAgent.SetId(foundAgent.Id)
59+
.SetName(foundAgent.Name)
60+
.SetDescription(foundAgent.Description)
61+
.SetIsPublic(foundAgent.IsPublic)
62+
.SetInstruction(foundAgent.Instruction)
63+
.SetTemplates(foundAgent.Templates)
64+
.SetFunctions(foundAgent.Functions)
65+
.SetResponses(foundAgent.Responses);
66+
67+
db.UpdateAgent(clonedAgent);
68+
}
69+
70+
71+
await Task.CompletedTask;
72+
}
73+
74+
private Agent FetchAgentFileById(string agentId, string filePath)
75+
{
76+
foreach (var dir in Directory.GetDirectories(filePath))
77+
{
78+
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
79+
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
80+
if (agent != null && agent.Id == agentId)
81+
{
82+
var functions = FetchFunctionsFromFile(dir);
83+
var instruction = FetchInstructionFromFile(dir);
84+
var responses = FetchResponsesFromFile(dir);
85+
var templates = FetchTemplatesFromFile(dir);
86+
return agent.SetInstruction(instruction)
87+
.SetTemplates(templates)
88+
.SetFunctions(functions)
89+
.SetResponses(responses);
90+
}
91+
}
92+
93+
return null;
94+
}
3995
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public async Task UpdateAgent([FromRoute] string agentId,
2929
await _agentService.UpdateAgent(model);
3030
}
3131

32+
[HttpPut("/agent/file/{agentId}")]
33+
public async Task UpdateAgentFromFile([FromRoute] string agentId)
34+
{
35+
await _agentService.UpdateAgentFromFile(agentId);
36+
}
37+
3238
[HttpGet("/agents")]
3339
public async Task<List<AgentViewModel>> GetAgents()
3440
{

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ public void UpdateAgent(Agent agent)
379379
Name = agent.Name,
380380
Description = agent.Description,
381381
Instruction = agent.Instruction,
382+
Templates = agent.Templates,
382383
Functions = agent.Functions,
383384
Responses = agent.Responses,
384385
IsPublic = agent.IsPublic,
@@ -391,12 +392,13 @@ public void UpdateAgent(Agent agent)
391392
.Set(x => x.Name, agent.Name)
392393
.Set(x => x.Description, agent.Description)
393394
.Set(x => x.Instruction, agent.Instruction)
395+
.Set(x => x.Templates, agent.Templates)
394396
.Set(x => x.Functions, agent.Functions)
395397
.Set(x => x.Responses, agent.Responses)
396398
.Set(x => x.IsPublic, agent.IsPublic)
397399
.Set(x => x.UpdatedTime, agent.UpdatedDateTime);
398400

399-
_dc.Agents.UpdateOne(filter, update, _options);
401+
_dc.Agents.UpdateOne(filter, update);
400402
}
401403

402404
public void DeleteRoutingItems()

0 commit comments

Comments
 (0)