Skip to content

Commit 346e081

Browse files
authored
Merge pull request #172 from iceljc/features/refine-fetch-agent
refine agent data fetching
2 parents 9b2524a + 541b8fd commit 346e081

File tree

6 files changed

+26
-35
lines changed

6 files changed

+26
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface IBotSharpRepository
1919

2020
#region Agent
2121
void UpdateAgent(Agent agent, AgentField field);
22-
Agent GetAgent(string agentId);
22+
Agent? GetAgent(string agentId);
2323
List<string> GetAgentResponses(string agentId, string prefix, string intent);
2424
string GetAgentTemplate(string agentId, string templateName);
2525
#endregion

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Repositories;
3-
using BotSharp.Abstraction.Routing;
4-
using BotSharp.Abstraction.Routing.Settings;
52

63
namespace BotSharp.Core.Agents.Services;
74

@@ -27,27 +24,11 @@ public async Task<Agent> GetAgent(string id)
2724
{
2825
var profile = _db.GetAgent(id);
2926

30-
var instructionFile = profile?.Instruction;
31-
if (instructionFile != null)
27+
if (profile == null)
3228
{
33-
profile.Instruction = instructionFile;
34-
}
35-
else
36-
{
37-
_logger.LogError($"Can't find instruction file from {instructionFile}");
38-
}
39-
40-
var samplesFile = profile?.Samples;
41-
if (samplesFile != null)
42-
{
43-
profile.Samples = samplesFile;
44-
}
45-
46-
var functionsFile = profile?.Functions;
47-
if (functionsFile != null)
48-
{
49-
profile.Functions = functionsFile;
50-
}
29+
_logger.LogError($"Can't find agent {id}");
30+
return null;
31+
};
5132

5233
return profile;
5334
}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using BotSharp.Abstraction.Agents.Models;
66
using MongoDB.Driver;
77
using BotSharp.Abstraction.Routing.Models;
8-
98
namespace BotSharp.Core.Repository;
109

1110
public class FileRepository : IBotSharpRepository
@@ -490,20 +489,29 @@ public List<string> GetAgentResponses(string agentId, string prefix, string inte
490489
return responses;
491490
}
492491

493-
public Agent GetAgent(string agentId)
492+
public Agent? GetAgent(string agentId)
494493
{
495494
var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir);
496-
foreach (var dir in Directory.GetDirectories(agentDir))
495+
var dir = Directory.GetDirectories(agentDir).FirstOrDefault(x => x.Split(Path.DirectorySeparatorChar).Last() == agentId);
496+
497+
if (!string.IsNullOrEmpty(dir))
497498
{
498499
var json = File.ReadAllText(Path.Combine(dir, "agent.json"));
500+
if (string.IsNullOrEmpty(json)) return null;
501+
499502
var record = JsonSerializer.Deserialize<Agent>(json, _options);
500-
if (record != null && record.Id == agentId)
501-
{
502-
var instruction = FetchInstruction(dir);
503-
var functions = FetchFunctions(dir);
504-
return record.SetInstruction(instruction).SetFunctions(functions);
505-
}
503+
if (record == null) return null;
504+
505+
var instruction = FetchInstruction(dir);
506+
var functions = FetchFunctions(dir);
507+
var templates = FetchTemplates(dir);
508+
var responses = FetchResponses(dir);
509+
return record.SetInstruction(instruction)
510+
.SetFunctions(functions)
511+
.SetTemplates(templates)
512+
.SetResponses(responses);
506513
}
514+
507515
return null;
508516
}
509517

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using BotSharp.Abstraction.Agents.Enums;
22
using BotSharp.Abstraction.ApiAdapters;
3-
using BotSharp.Abstraction.Routing;
43
using BotSharp.OpenAPI.ViewModels.Agents;
54

65
namespace BotSharp.OpenAPI.Controllers;

src/Plugins/BotSharp.Plugin.MongoStorage/Models/RoutingRuleMongoElement.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Models;
55
public class RoutingRuleMongoElement
66
{
77
public string Field { get; set; }
8+
public string Description { get; set; }
89
public bool Required { get; set; }
910
public Guid? RedirectTo { get; set; }
1011

@@ -18,6 +19,7 @@ public static RoutingRuleMongoElement ToMongoElement(RoutingRule routingRule)
1819
return new RoutingRuleMongoElement
1920
{
2021
Field = routingRule.Field,
22+
Description = routingRule.Description,
2123
Required = routingRule.Required,
2224
RedirectTo = !string.IsNullOrEmpty(routingRule.RedirectTo) ? Guid.Parse(routingRule.RedirectTo) : null
2325
};
@@ -30,6 +32,7 @@ public static RoutingRule ToDomainElement(string agentId, string agentName, Rout
3032
AgentId = agentId,
3133
AgentName = agentName,
3234
Field = rule.Field,
35+
Description = rule.Description,
3336
Required = rule.Required,
3437
RedirectTo = rule.RedirectTo?.ToString()
3538
};

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ private void UpdateAgentAllFields(Agent agent)
517517

518518

519519

520-
public Agent GetAgent(string agentId)
520+
public Agent? GetAgent(string agentId)
521521
{
522522
var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId);
523523
return foundAgent;

0 commit comments

Comments
 (0)