Skip to content

decouple mongo element with domain element #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 28, 2023
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 @@ -18,4 +18,9 @@ public class FunctionParametersDef

[JsonPropertyName("required")]
public List<string> Required { get; set; } = new List<string>();

public FunctionParametersDef()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<Conversation> NewConversation(Conversation sess)
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
var conversationSettings = _services.GetRequiredService<ConversationSetting>();
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
var foundUserId = user?.Id ?? _user.Id;
var foundUserId = user?.Id ?? string.Empty;

var record = sess;
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Routing.Settings;
using BotSharp.OpenAPI.ViewModels.Agents;

namespace BotSharp.OpenAPI.Controllers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Plugin.MongoStorage.Models;

namespace BotSharp.Plugin.MongoStorage.Collections;
Expand All @@ -9,9 +7,9 @@ public class AgentCollection : MongoBase
public string Name { get; set; }
public string Description { get; set; }
public string Instruction { get; set; }
public List<AgentTemplate> Templates { get; set; }
public List<FunctionDef> Functions { get; set; }
public List<AgentResponse> Responses { get; set; }
public List<AgentTemplateMongoElement> Templates { get; set; }
public List<FunctionDefMongoElement> Functions { get; set; }
public List<AgentResponseMongoElement> Responses { get; set; }
public bool IsPublic { get; set; }
public bool AllowRouting { get; set; }
public bool Disabled { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using BotSharp.Abstraction.Agents.Models;

namespace BotSharp.Plugin.MongoStorage.Models;

public class AgentResponseMongoElement
{
public string Prefix { get; set; }
public string Intent { get; set; }
public string Content { get; set; }

public static AgentResponseMongoElement ToMongoElement(AgentResponse response)
{
return new AgentResponseMongoElement
{
Prefix = response.Prefix,
Intent = response.Intent,
Content = response.Content
};
}

public static AgentResponse ToDomainElement(AgentResponseMongoElement mongoResponse)
{
return new AgentResponse
{
Prefix = mongoResponse.Prefix,
Intent = mongoResponse.Intent,
Content = mongoResponse.Content
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using BotSharp.Abstraction.Agents.Models;

namespace BotSharp.Plugin.MongoStorage.Models;

public class AgentTemplateMongoElement
{
public string Name { get; set; }
public string Content { get; set; }

public static AgentTemplateMongoElement ToMongoElement(AgentTemplate template)
{
return new AgentTemplateMongoElement
{
Name = template.Name,
Content = template.Content
};
}

public static AgentTemplate ToDomainElement(AgentTemplateMongoElement mongoTemplate)
{
return new AgentTemplate
{
Name = mongoTemplate.Name,
Content = mongoTemplate.Content
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using BotSharp.Abstraction.Functions.Models;
using System.Text.Json;

namespace BotSharp.Plugin.MongoStorage.Models;

public class FunctionDefMongoElement
{
public string Name { get; set; }
public string Description { get; set; }
public FunctionParametersDefMongoElement Parameters { get; set; } = new FunctionParametersDefMongoElement();

public FunctionDefMongoElement()
{

}

public static FunctionDefMongoElement ToMongoElement(FunctionDef function)
{
return new FunctionDefMongoElement
{
Name = function.Name,
Description = function.Description,
Parameters = new FunctionParametersDefMongoElement
{
Type = function.Parameters.Type,
Properties = JsonSerializer.Serialize(function.Parameters.Properties),
Required = function.Parameters.Required,
}
};
}

public static FunctionDef ToDomainElement(FunctionDefMongoElement mongoFunction)
{
return new FunctionDef
{
Name = mongoFunction.Name,
Description = mongoFunction.Description,
Parameters = new FunctionParametersDef
{
Type = mongoFunction.Parameters.Type,
Properties = JsonSerializer.Deserialize<JsonDocument>(mongoFunction.Parameters.Properties),
Required = mongoFunction.Parameters.Required,
}
};
}
}

public class FunctionParametersDefMongoElement
{
public string Type { get; set; }
public string Properties { get; set; }
public List<string> Required { get; set; } = new List<string>();

public FunctionParametersDefMongoElement()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ public IQueryable<Agent> Agents
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Templates = x.Templates,
Functions = x.Functions,
Responses = x.Responses,
Templates = x.Templates?
.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?
.ToList() ?? new List<AgentTemplate>(),
Functions = x.Functions?
.Select(f => FunctionDefMongoElement.ToDomainElement(f))?
.ToList() ?? new List<FunctionDef>(),
Responses = x.Responses?
.Select(r => AgentResponseMongoElement.ToDomainElement(r))?
.ToList() ?? new List<AgentResponse>(),
IsPublic = x.IsPublic,
Disabled = x.Disabled,
AllowRouting = x.AllowRouting,
Expand Down Expand Up @@ -183,7 +189,7 @@ public int Transaction<TTableInterface>(Action action)
{
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
AgentId = Guid.Parse(x.AgentId),
UserId = Guid.Parse(x.UserId),
UserId = !string.IsNullOrEmpty(x.UserId) && Guid.TryParse(x.UserId, out var _) ? Guid.Parse(x.UserId) : Guid.Empty,
Title = x.Title,
States = x.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
CreatedTime = x.CreatedTime,
Expand Down Expand Up @@ -211,9 +217,15 @@ public int Transaction<TTableInterface>(Action action)
Name = x.Name,
Description = x.Description,
Instruction = x.Instruction,
Templates = x.Templates,
Functions = x.Functions,
Responses = x.Responses,
Templates = x.Templates?
.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?
.ToList() ?? new List<AgentTemplateMongoElement>(),
Functions = x.Functions?
.Select(f => FunctionDefMongoElement.ToMongoElement(f))?
.ToList() ?? new List<FunctionDefMongoElement>(),
Responses = x.Responses?
.Select(r => AgentResponseMongoElement.ToMongoElement(r))?
.ToList() ?? new List<AgentResponseMongoElement>(),
IsPublic = x.IsPublic,
AllowRouting = x.AllowRouting,
Disabled = x.Disabled,
Expand Down Expand Up @@ -281,7 +293,7 @@ public int Transaction<TTableInterface>(Action action)
{
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
AgentId = Guid.Parse(x.AgentId),
UserId = Guid.Parse(x.UserId),
UserId = !string.IsNullOrEmpty(x.UserId) && Guid.TryParse(x.UserId, out var _) ? Guid.Parse(x.UserId) : Guid.Empty,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
Expand Down Expand Up @@ -446,9 +458,10 @@ private void UpdateAgentFunctions(string agentId, List<FunctionDef> functions)
{
if (functions.IsNullOrEmpty()) return;

var functionsToUpdate = functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList();
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Functions, functions)
.Set(x => x.Functions, functionsToUpdate)
.Set(x => x.UpdatedTime, DateTime.UtcNow);

_dc.Agents.UpdateOne(filter, update);
Expand All @@ -458,9 +471,10 @@ private void UpdateAgentTemplates(string agentId, List<AgentTemplate> templates)
{
if (templates.IsNullOrEmpty()) return;

var templatesToUpdate = templates.Select(t => AgentTemplateMongoElement.ToMongoElement(t)).ToList();
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Templates, templates)
.Set(x => x.Templates, templatesToUpdate)
.Set(x => x.UpdatedTime, DateTime.UtcNow);

_dc.Agents.UpdateOne(filter, update);
Expand All @@ -470,9 +484,10 @@ private void UpdateAgentResponses(string agentId, List<AgentResponse> responses)
{
if (responses.IsNullOrEmpty()) return;

var responsesToUpdate = responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList();
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
var update = Builders<AgentCollection>.Update
.Set(x => x.Responses, responses)
.Set(x => x.Responses, responsesToUpdate)
.Set(x => x.UpdatedTime, DateTime.UtcNow);

_dc.Agents.UpdateOne(filter, update);
Expand All @@ -487,11 +502,11 @@ private void UpdateAgentAllFields(Agent agent)
.Set(x => x.Disabled, agent.Disabled)
.Set(x => x.AllowRouting, agent.AllowRouting)
.Set(x => x.Profiles, agent.Profiles)
.Set(x => x.RoutingRules, agent.RoutingRules.Select(x => RoutingRuleMongoElement.ToMongoElement(x)).ToList())
.Set(x => x.RoutingRules, agent.RoutingRules.Select(r => RoutingRuleMongoElement.ToMongoElement(r)).ToList())
.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.Templates, agent.Templates.Select(t => AgentTemplateMongoElement.ToMongoElement(t)).ToList())
.Set(x => x.Functions, agent.Functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList())
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
.Set(x => x.IsPublic, agent.IsPublic)
.Set(x => x.UpdatedTime, DateTime.UtcNow);

Expand Down Expand Up @@ -535,7 +550,7 @@ public void CreateNewConversation(Conversation conversation)
{
Id = !string.IsNullOrEmpty(conversation.Id) ? Guid.Parse(conversation.Id) : Guid.NewGuid(),
AgentId = Guid.Parse(conversation.AgentId),
UserId = Guid.Parse(conversation.UserId),
UserId = !string.IsNullOrEmpty(conversation.UserId) && Guid.TryParse(conversation.UserId, out var _) ? Guid.Parse(conversation.UserId) : Guid.Empty,
Title = conversation.Title,
States = conversation.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
CreatedTime = DateTime.UtcNow,
Expand Down Expand Up @@ -637,7 +652,7 @@ public Conversation GetConversation(string conversationId)
public List<Conversation> GetConversations(string userId)
{
var records = new List<Conversation>();
if (string.IsNullOrEmpty(userId)) return records;
if (string.IsNullOrEmpty(userId) || !Guid.TryParse(userId, out var _)) return records;

var filterByUserId = Builders<ConversationCollection>.Filter.Eq(x => x.UserId, Guid.Parse(userId));
var conversations = _dc.Conversations.Find(filterByUserId).ToList();
Expand Down