Merge pull request #160 from iceljc/bugfix/adjust-function-def-mongo

decouple mongo element with domain element
This commit is contained in:
Haiping 2023-09-28 12:34:25 -05:00 committed by GitHub
commit 4e151d301b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 156 additions and 25 deletions

View file

@ -18,4 +18,9 @@ public class FunctionParametersDef
[JsonPropertyName("required")]
public List<string> Required { get; set; } = new List<string>();
public FunctionParametersDef()
{
}
}

View file

@ -56,7 +56,7 @@ public partial class ConversationService : IConversationService
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());

View file

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

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Agents.Models;
using BotSharp.Abstraction.Functions.Models;
using BotSharp.Plugin.MongoStorage.Models;
namespace BotSharp.Plugin.MongoStorage.Collections;
@ -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; }

View file

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

View file

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

View file

@ -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()
{
}
}

View file

@ -41,9 +41,15 @@ public class MongoRepository : IBotSharpRepository
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,
@ -183,7 +189,7 @@ public class MongoRepository : IBotSharpRepository
{
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,
@ -211,9 +217,15 @@ public class MongoRepository : IBotSharpRepository
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,
@ -281,7 +293,7 @@ public class MongoRepository : IBotSharpRepository
{
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();
@ -446,9 +458,10 @@ public class MongoRepository : IBotSharpRepository
{
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);
@ -458,9 +471,10 @@ public class MongoRepository : IBotSharpRepository
{
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);
@ -470,9 +484,10 @@ public class MongoRepository : IBotSharpRepository
{
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);
@ -487,11 +502,11 @@ public class MongoRepository : IBotSharpRepository
.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);
@ -535,7 +550,7 @@ public class MongoRepository : IBotSharpRepository
{
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,
@ -637,7 +652,7 @@ public class MongoRepository : IBotSharpRepository
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();