add update agent from file

This commit is contained in:
Jicheng Lu 2023-09-13 17:34:19 -05:00
parent be3150a254
commit 4ad51f0e38
5 changed files with 68 additions and 3 deletions

View file

@ -18,6 +18,7 @@ public interface IAgentService
Task<Agent> GetAgent(string id);
Task<bool> DeleteAgent(string id);
Task UpdateAgent(Agent agent);
Task UpdateAgentFromFile(string id);
string GetDataDir();
string GetAgentDataDir(string agentId);
}

View file

@ -29,7 +29,7 @@ public partial class AgentService
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
var foundAgent = FetchAgentInfoFromFile(agent.Name, filePath);
var foundAgent = FetchAgentFileByName(agent.Name, filePath);
if (foundAgent != null)
{
@ -62,7 +62,7 @@ public partial class AgentService
return agentRecord;
}
private Agent FetchAgentInfoFromFile(string agentName, string filePath)
private Agent FetchAgentFileByName(string agentName, string filePath)
{
foreach (var dir in Directory.GetDirectories(filePath))
{

View file

@ -27,6 +27,9 @@ public partial class AgentService
if (!string.IsNullOrEmpty(agent.Instruction))
record.Instruction = agent.Instruction;
if (!agent.Templates.IsNullOrEmpty())
record.Templates = agent.Templates;
if (!agent.Functions.IsNullOrEmpty())
record.Functions = agent.Functions;
@ -36,4 +39,57 @@ public partial class AgentService
db.UpdateAgent(record);
await Task.CompletedTask;
}
public async Task UpdateAgentFromFile(string id)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var agent = db.Agents?.FirstOrDefault(x => x.Id == id);
if (agent == null) return;
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentSettings = _services.GetRequiredService<AgentSettings>();
var filePath = Path.Combine(dbSettings.FileRepository, agentSettings.DataDir);
var clonedAgent = Agent.Clone(agent);
var foundAgent = FetchAgentFileById(agent.Id, filePath);
if (foundAgent != null)
{
clonedAgent.SetId(foundAgent.Id)
.SetName(foundAgent.Name)
.SetDescription(foundAgent.Description)
.SetIsPublic(foundAgent.IsPublic)
.SetInstruction(foundAgent.Instruction)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
db.UpdateAgent(clonedAgent);
}
await Task.CompletedTask;
}
private Agent FetchAgentFileById(string agentId, string filePath)
{
foreach (var dir in Directory.GetDirectories(filePath))
{
var agentJson = File.ReadAllText(Path.Combine(dir, "agent.json"));
var agent = JsonSerializer.Deserialize<Agent>(agentJson, _options);
if (agent != null && agent.Id == agentId)
{
var functions = FetchFunctionsFromFile(dir);
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
return agent.SetInstruction(instruction)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses);
}
}
return null;
}
}

View file

@ -29,6 +29,12 @@ public class AgentController : ControllerBase, IApiAdapter
await _agentService.UpdateAgent(model);
}
[HttpPut("/agent/file/{agentId}")]
public async Task UpdateAgentFromFile([FromRoute] string agentId)
{
await _agentService.UpdateAgentFromFile(agentId);
}
[HttpGet("/agents")]
public async Task<List<AgentViewModel>> GetAgents()
{

View file

@ -379,6 +379,7 @@ public class MongoRepository : IBotSharpRepository
Name = agent.Name,
Description = agent.Description,
Instruction = agent.Instruction,
Templates = agent.Templates,
Functions = agent.Functions,
Responses = agent.Responses,
IsPublic = agent.IsPublic,
@ -391,12 +392,13 @@ public class MongoRepository : IBotSharpRepository
.Set(x => x.Name, agent.Name)
.Set(x => x.Description, agent.Description)
.Set(x => x.Instruction, agent.Instruction)
.Set(x => x.Templates, agent.Templates)
.Set(x => x.Functions, agent.Functions)
.Set(x => x.Responses, agent.Responses)
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.UpdatedTime, agent.UpdatedDateTime);
_dc.Agents.UpdateOne(filter, update, _options);
_dc.Agents.UpdateOne(filter, update);
}
public void DeleteRoutingItems()