Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IBotSharpRepository

#region Agent
void UpdateAgent(Agent agent, AgentField field);
Agent GetAgent(string agentId);
Agent? GetAgent(string agentId);
List<string> GetAgentResponses(string agentId, string prefix, string intent);
string GetAgentTemplate(string agentId, string templateName);
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;

namespace BotSharp.Core.Agents.Services;

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

var instructionFile = profile?.Instruction;
if (instructionFile != null)
if (profile == null)
{
profile.Instruction = instructionFile;
}
else
{
_logger.LogError($"Can't find instruction file from {instructionFile}");
}

var samplesFile = profile?.Samples;
if (samplesFile != null)
{
profile.Samples = samplesFile;
}

var functionsFile = profile?.Functions;
if (functionsFile != null)
{
profile.Functions = functionsFile;
}
_logger.LogError($"Can't find agent {id}");
return null;
};

return profile;
}
Expand Down
26 changes: 17 additions & 9 deletions src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using BotSharp.Abstraction.Agents.Models;
using MongoDB.Driver;
using BotSharp.Abstraction.Routing.Models;

namespace BotSharp.Core.Repository;

public class FileRepository : IBotSharpRepository
Expand Down Expand Up @@ -490,20 +489,29 @@ public List<string> GetAgentResponses(string agentId, string prefix, string inte
return responses;
}

public Agent GetAgent(string agentId)
public Agent? GetAgent(string agentId)
{
var agentDir = Path.Combine(_dbSettings.FileRepository, _agentSettings.DataDir);
foreach (var dir in Directory.GetDirectories(agentDir))
var dir = Directory.GetDirectories(agentDir).FirstOrDefault(x => x.Split(Path.DirectorySeparatorChar).Last() == agentId);

if (!string.IsNullOrEmpty(dir))
{
var json = File.ReadAllText(Path.Combine(dir, "agent.json"));
if (string.IsNullOrEmpty(json)) return null;

var record = JsonSerializer.Deserialize<Agent>(json, _options);
if (record != null && record.Id == agentId)
{
var instruction = FetchInstruction(dir);
var functions = FetchFunctions(dir);
return record.SetInstruction(instruction).SetFunctions(functions);
}
if (record == null) return null;

var instruction = FetchInstruction(dir);
var functions = FetchFunctions(dir);
var templates = FetchTemplates(dir);
var responses = FetchResponses(dir);
return record.SetInstruction(instruction)
.SetFunctions(functions)
.SetTemplates(templates)
.SetResponses(responses);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Routing;
using BotSharp.OpenAPI.ViewModels.Agents;

namespace BotSharp.OpenAPI.Controllers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BotSharp.Plugin.MongoStorage.Models;
public class RoutingRuleMongoElement
{
public string Field { get; set; }
public string Description { get; set; }
public bool Required { get; set; }
public Guid? RedirectTo { get; set; }

Expand All @@ -18,6 +19,7 @@ public static RoutingRuleMongoElement ToMongoElement(RoutingRule routingRule)
return new RoutingRuleMongoElement
{
Field = routingRule.Field,
Description = routingRule.Description,
Required = routingRule.Required,
RedirectTo = !string.IsNullOrEmpty(routingRule.RedirectTo) ? Guid.Parse(routingRule.RedirectTo) : null
};
Expand All @@ -30,6 +32,7 @@ public static RoutingRule ToDomainElement(string agentId, string agentName, Rout
AgentId = agentId,
AgentName = agentName,
Field = rule.Field,
Description = rule.Description,
Required = rule.Required,
RedirectTo = rule.RedirectTo?.ToString()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private void UpdateAgentAllFields(Agent agent)



public Agent GetAgent(string agentId)
public Agent? GetAgent(string agentId)
{
var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId);
return foundAgent;
Expand Down