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 @@ -9,9 +9,10 @@ public enum AgentField
Disabled,
AllowRouting,
Profiles,
RoutingRules,
RoutingRule,
Instruction,
Function,
Template,
Response
Response,
Sample
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ namespace BotSharp.Abstraction.Repositories;

public interface IBotSharpRepository
{
IQueryable<User> Users { get; }
IQueryable<Agent> Agents { get; }
IQueryable<UserAgent> UserAgents { get; }
IQueryable<Conversation> Conversations { get; }

int Transaction<TTableInterface>(Action action);
void Add<TTableInterface>(object entity);

#region User
User? GetUserByEmail(string email);
User? GetUserByExternalId(string externalId);
void CreateUser(User user);
#endregion

#region Agent
void UpdateAgent(Agent agent, AgentField field);
Agent? GetAgent(string agentId);
List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRouting = null,
bool? isPublic = null, List<string>? agentIds = null);
List<Agent> GetAgentsByUser(string userId);
void BulkInsertAgents(List<Agent> agents);
void BulkInsertUserAgents(List<UserAgent> userAgents);
bool DeleteAgents();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Abstraction.Repositories;
using BotSharp.Abstraction.Utilities;
using System.IO;

namespace BotSharp.Core.Agents.Services;
Expand All @@ -10,11 +9,7 @@ public partial class AgentService
{
public async Task<Agent> CreateAgent(Agent agent)
{
var agentRecord = (from a in _db.Agents
join ua in _db.UserAgents on a.Id equals ua.AgentId
join u in _db.Users on ua.UserId equals u.Id
where u.ExternalId == _user.Id && a.Name == agent.Name
select a).FirstOrDefault();
var agentRecord = _db.GetAgentsByUser(_user.Id).FirstOrDefault(x => x.Name.IsEqualTo(agent.Name));

if (agentRecord != null)
{
Expand Down Expand Up @@ -47,7 +42,7 @@ join u in _db.Users on ua.UserId equals u.Id
.SetResponses(foundAgent.Responses);
}

var user = _db.Users.FirstOrDefault(x => x.Id == _user.Id || x.ExternalId == _user.Id);
var user = _db.GetUserByExternalId(_user.Id);
var userAgentRecord = new UserAgent
{
Id = Guid.NewGuid().ToString(),
Expand Down Expand Up @@ -78,10 +73,12 @@ private Agent FetchAgentFileByName(string agentName, string filePath)
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
var samples = FetchSamplesFromFile(dir);
return agent.SetInstruction(instruction)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses);
.SetResponses(responses)
.SetSamples(samples);
}
}

Expand Down Expand Up @@ -146,4 +143,13 @@ private List<AgentResponse> FetchResponsesFromFile(string fileDir)
}
return responses;
}

private List<string> FetchSamplesFromFile(string fileDir)
{
var file = Path.Combine(fileDir, "samples.txt");
if (!File.Exists(file)) return new List<string>();

var samples = File.ReadAllLines(file);
return samples?.ToList() ?? new List<string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ public partial class AgentService
#endif
public async Task<List<Agent>> GetAgents()
{
var query = from a in _db.Agents
join ua in _db.UserAgents on a.Id equals ua.AgentId
join u in _db.Users on ua.UserId equals u.Id
where ua.UserId == _user.Id || u.ExternalId == _user.Id || a.IsPublic
select a;
return query.ToList();
var agents = _db.GetAgentsByUser(_user.Id);
return await Task.FromResult(agents);
}

#if !DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task RefreshAgents()

var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var agentDir = Path.Combine(dbSettings.FileRepository, _agentSettings.DataDir);
var user = _db.Users.FirstOrDefault(x => x.Id == _user.Id || x.ExternalId == _user.Id);
var user = _db.GetUserByExternalId(_user.Id);
var agents = new List<Agent>();
var userAgents = new List<UserAgent>();

Expand All @@ -27,10 +27,12 @@ public async Task RefreshAgents()
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
var samples = FetchSamplesFromFile(dir);
agent.SetInstruction(instruction)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses);
.SetResponses(responses)
.SetSamples(samples);

var userAgent = new UserAgent
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
{
if (agent == null || string.IsNullOrEmpty(agent.Id)) return;

var record = FindAgent(agent.Id);
var record = _db.GetAgent(agent.Id);
if (record == null) return;

record.Name = agent.Name ?? string.Empty;
Expand All @@ -26,25 +26,15 @@ public async Task UpdateAgent(Agent agent, AgentField updateField)
record.Functions = agent.Functions ?? new List<FunctionDef>();
record.Templates = agent.Templates ?? new List<AgentTemplate>();
record.Responses = agent.Responses ?? new List<AgentResponse>();
record.Samples = agent.Samples ?? new List<string>();

_db.UpdateAgent(record, updateField);
await Task.CompletedTask;
}

private Agent FindAgent(string agentId)
{
var record = (from a in _db.Agents
join ua in _db.UserAgents on a.Id equals ua.AgentId
join u in _db.Users on ua.UserId equals u.Id
where (ua.UserId == _user.Id || u.ExternalId == _user.Id) &&
a.Id == agentId
select a).FirstOrDefault();
return record;
}

public async Task UpdateAgentFromFile(string id)
{
var agent = _db.Agents?.FirstOrDefault(x => x.Id == id);
var agent = _db.GetAgent(id);

if (agent == null) return;

Expand All @@ -67,7 +57,8 @@ public async Task UpdateAgentFromFile(string id)
.SetInstruction(foundAgent.Instruction)
.SetTemplates(foundAgent.Templates)
.SetFunctions(foundAgent.Functions)
.SetResponses(foundAgent.Responses);
.SetResponses(foundAgent.Responses)
.SetSamples(foundAgent.Samples);

_db.UpdateAgent(clonedAgent, AgentField.All);
}
Expand All @@ -87,10 +78,12 @@ private Agent FetchAgentFileById(string agentId, string filePath)
var instruction = FetchInstructionFromFile(dir);
var responses = FetchResponsesFromFile(dir);
var templates = FetchTemplatesFromFile(dir);
var samples = FetchSamplesFromFile(dir);
return agent.SetInstruction(instruction)
.SetTemplates(templates)
.SetFunctions(functions)
.SetResponses(responses);
.SetResponses(responses)
.SetSamples(samples);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Repositories;
using System.IO;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public async Task<Conversation> GetConversation(string id)
public async Task<List<Conversation>> GetConversations()
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
var user = db.GetUserByExternalId(_user.Id);
var conversations = db.GetConversations(user?.Id);
return conversations.OrderByDescending(x => x.CreatedTime).ToList();
}
Expand All @@ -56,7 +56,7 @@ public async Task<Conversation> NewConversation(Conversation sess)
var db = _services.GetRequiredService<IBotSharpRepository>();
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var conversationSettings = _services.GetRequiredService<ConversationSetting>();
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
var user = db.GetUserByExternalId(_user.Id);
var foundUserId = user?.Id ?? string.Empty;

var record = sess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void Append(string conversationId, RoleDialogModel dialog)
else
{
var routingSetting = _services.GetRequiredService<RoutingSettings>();
var agentName = routingSetting.RouterId == agentId ? "Router" : db.Agents.First(x => x.Id == agentId).Name;
var agentName = routingSetting.RouterId == agentId ? "Router" : db.GetAgent(agentId)?.Name;

sb.AppendLine($"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{agentName}|");
var content = dialog.Content.Replace("\r", " ").Replace("\n", " ").Trim();
Expand Down
16 changes: 16 additions & 0 deletions src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public Agent GetAgent(string agentId)
throw new NotImplementedException();
}

public List<Agent> GetAgents(string? name = null, bool? disabled = null, bool? allowRouting = null,
bool? isPublic = null, List<string>? agentIds = null)
{
throw new NotImplementedException();
}

public List<Agent> GetAgentsByUser(string userId)
{
throw new NotImplementedException();
}

public void UpdateAgent(Agent agent, AgentField field)
{
throw new NotImplementedException();
Expand Down Expand Up @@ -148,6 +159,11 @@ public void UpdateConversationStates(string conversationId, List<StateKeyValue>
throw new NotImplementedException();
}

public User? GetUserByExternalId(string externalId)
{
throw new NotImplementedException();
}

public void CreateUser(User user)
{
throw new NotImplementedException();
Expand Down
Loading