Merge pull request #172 from iceljc/features/refine-fetch-agent

refine agent data fetching
This commit is contained in:
Haiping 2023-10-16 16:39:38 -05:00 committed by GitHub
commit 346e081ad6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 35 deletions

View file

@ -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

View file

@ -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;
@ -27,27 +24,11 @@ public partial class AgentService
{
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;
}

View file

@ -5,7 +5,6 @@ using BotSharp.Abstraction.Users.Models;
using BotSharp.Abstraction.Agents.Models;
using MongoDB.Driver;
using BotSharp.Abstraction.Routing.Models;
namespace BotSharp.Core.Repository;
public class FileRepository : IBotSharpRepository
@ -490,20 +489,29 @@ public class FileRepository : IBotSharpRepository
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;
}

View file

@ -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;

View file

@ -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; }
@ -18,6 +19,7 @@ public class RoutingRuleMongoElement
return new RoutingRuleMongoElement
{
Field = routingRule.Field,
Description = routingRule.Description,
Required = routingRule.Required,
RedirectTo = !string.IsNullOrEmpty(routingRule.RedirectTo) ? Guid.Parse(routingRule.RedirectTo) : null
};
@ -30,6 +32,7 @@ public class RoutingRuleMongoElement
AgentId = agentId,
AgentName = agentName,
Field = rule.Field,
Description = rule.Description,
Required = rule.Required,
RedirectTo = rule.RedirectTo?.ToString()
};

View file

@ -517,7 +517,7 @@ public class MongoRepository : IBotSharpRepository
public Agent GetAgent(string agentId)
public Agent? GetAgent(string agentId)
{
var foundAgent = Agents.FirstOrDefault(x => x.Id == agentId);
return foundAgent;