Skip to content

Commit 4e151d3

Browse files
authored
Merge pull request #160 from iceljc/bugfix/adjust-function-def-mongo
decouple mongo element with domain element
2 parents 433359f + e3f607b commit 4e151d3

File tree

8 files changed

+156
-25
lines changed

8 files changed

+156
-25
lines changed

src/Infrastructure/BotSharp.Abstraction/Functions/Models/FunctionParametersDef.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public class FunctionParametersDef
1818

1919
[JsonPropertyName("required")]
2020
public List<string> Required { get; set; } = new List<string>();
21+
22+
public FunctionParametersDef()
23+
{
24+
25+
}
2126
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public async Task<Conversation> NewConversation(Conversation sess)
5656
var dbSettings = _services.GetRequiredService<BotSharpDatabaseSettings>();
5757
var conversationSettings = _services.GetRequiredService<ConversationSetting>();
5858
var user = db.Users.FirstOrDefault(x => x.ExternalId == _user.Id);
59-
var foundUserId = user?.Id ?? _user.Id;
59+
var foundUserId = user?.Id ?? string.Empty;
6060

6161
var record = sess;
6262
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());

src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using BotSharp.Abstraction.Agents.Enums;
2-
using BotSharp.Abstraction.Agents.Models;
32
using BotSharp.Abstraction.ApiAdapters;
43
using BotSharp.Abstraction.Routing;
5-
using BotSharp.Abstraction.Routing.Settings;
64
using BotSharp.OpenAPI.ViewModels.Agents;
75

86
namespace BotSharp.OpenAPI.Controllers;

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentCollection.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Agents.Models;
2-
using BotSharp.Abstraction.Functions.Models;
31
using BotSharp.Plugin.MongoStorage.Models;
42

53
namespace BotSharp.Plugin.MongoStorage.Collections;
@@ -9,9 +7,9 @@ public class AgentCollection : MongoBase
97
public string Name { get; set; }
108
public string Description { get; set; }
119
public string Instruction { get; set; }
12-
public List<AgentTemplate> Templates { get; set; }
13-
public List<FunctionDef> Functions { get; set; }
14-
public List<AgentResponse> Responses { get; set; }
10+
public List<AgentTemplateMongoElement> Templates { get; set; }
11+
public List<FunctionDefMongoElement> Functions { get; set; }
12+
public List<AgentResponseMongoElement> Responses { get; set; }
1513
public bool IsPublic { get; set; }
1614
public bool AllowRouting { get; set; }
1715
public bool Disabled { get; set; }
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using BotSharp.Abstraction.Agents.Models;
2+
3+
namespace BotSharp.Plugin.MongoStorage.Models;
4+
5+
public class AgentResponseMongoElement
6+
{
7+
public string Prefix { get; set; }
8+
public string Intent { get; set; }
9+
public string Content { get; set; }
10+
11+
public static AgentResponseMongoElement ToMongoElement(AgentResponse response)
12+
{
13+
return new AgentResponseMongoElement
14+
{
15+
Prefix = response.Prefix,
16+
Intent = response.Intent,
17+
Content = response.Content
18+
};
19+
}
20+
21+
public static AgentResponse ToDomainElement(AgentResponseMongoElement mongoResponse)
22+
{
23+
return new AgentResponse
24+
{
25+
Prefix = mongoResponse.Prefix,
26+
Intent = mongoResponse.Intent,
27+
Content = mongoResponse.Content
28+
};
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using BotSharp.Abstraction.Agents.Models;
2+
3+
namespace BotSharp.Plugin.MongoStorage.Models;
4+
5+
public class AgentTemplateMongoElement
6+
{
7+
public string Name { get; set; }
8+
public string Content { get; set; }
9+
10+
public static AgentTemplateMongoElement ToMongoElement(AgentTemplate template)
11+
{
12+
return new AgentTemplateMongoElement
13+
{
14+
Name = template.Name,
15+
Content = template.Content
16+
};
17+
}
18+
19+
public static AgentTemplate ToDomainElement(AgentTemplateMongoElement mongoTemplate)
20+
{
21+
return new AgentTemplate
22+
{
23+
Name = mongoTemplate.Name,
24+
Content = mongoTemplate.Content
25+
};
26+
}
27+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using BotSharp.Abstraction.Functions.Models;
2+
using System.Text.Json;
3+
4+
namespace BotSharp.Plugin.MongoStorage.Models;
5+
6+
public class FunctionDefMongoElement
7+
{
8+
public string Name { get; set; }
9+
public string Description { get; set; }
10+
public FunctionParametersDefMongoElement Parameters { get; set; } = new FunctionParametersDefMongoElement();
11+
12+
public FunctionDefMongoElement()
13+
{
14+
15+
}
16+
17+
public static FunctionDefMongoElement ToMongoElement(FunctionDef function)
18+
{
19+
return new FunctionDefMongoElement
20+
{
21+
Name = function.Name,
22+
Description = function.Description,
23+
Parameters = new FunctionParametersDefMongoElement
24+
{
25+
Type = function.Parameters.Type,
26+
Properties = JsonSerializer.Serialize(function.Parameters.Properties),
27+
Required = function.Parameters.Required,
28+
}
29+
};
30+
}
31+
32+
public static FunctionDef ToDomainElement(FunctionDefMongoElement mongoFunction)
33+
{
34+
return new FunctionDef
35+
{
36+
Name = mongoFunction.Name,
37+
Description = mongoFunction.Description,
38+
Parameters = new FunctionParametersDef
39+
{
40+
Type = mongoFunction.Parameters.Type,
41+
Properties = JsonSerializer.Deserialize<JsonDocument>(mongoFunction.Parameters.Properties),
42+
Required = mongoFunction.Parameters.Required,
43+
}
44+
};
45+
}
46+
}
47+
48+
public class FunctionParametersDefMongoElement
49+
{
50+
public string Type { get; set; }
51+
public string Properties { get; set; }
52+
public List<string> Required { get; set; } = new List<string>();
53+
54+
public FunctionParametersDefMongoElement()
55+
{
56+
57+
}
58+
}

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ public IQueryable<Agent> Agents
4141
Name = x.Name,
4242
Description = x.Description,
4343
Instruction = x.Instruction,
44-
Templates = x.Templates,
45-
Functions = x.Functions,
46-
Responses = x.Responses,
44+
Templates = x.Templates?
45+
.Select(t => AgentTemplateMongoElement.ToDomainElement(t))?
46+
.ToList() ?? new List<AgentTemplate>(),
47+
Functions = x.Functions?
48+
.Select(f => FunctionDefMongoElement.ToDomainElement(f))?
49+
.ToList() ?? new List<FunctionDef>(),
50+
Responses = x.Responses?
51+
.Select(r => AgentResponseMongoElement.ToDomainElement(r))?
52+
.ToList() ?? new List<AgentResponse>(),
4753
IsPublic = x.IsPublic,
4854
Disabled = x.Disabled,
4955
AllowRouting = x.AllowRouting,
@@ -183,7 +189,7 @@ public int Transaction<TTableInterface>(Action action)
183189
{
184190
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
185191
AgentId = Guid.Parse(x.AgentId),
186-
UserId = Guid.Parse(x.UserId),
192+
UserId = !string.IsNullOrEmpty(x.UserId) && Guid.TryParse(x.UserId, out var _) ? Guid.Parse(x.UserId) : Guid.Empty,
187193
Title = x.Title,
188194
States = x.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
189195
CreatedTime = x.CreatedTime,
@@ -211,9 +217,15 @@ public int Transaction<TTableInterface>(Action action)
211217
Name = x.Name,
212218
Description = x.Description,
213219
Instruction = x.Instruction,
214-
Templates = x.Templates,
215-
Functions = x.Functions,
216-
Responses = x.Responses,
220+
Templates = x.Templates?
221+
.Select(t => AgentTemplateMongoElement.ToMongoElement(t))?
222+
.ToList() ?? new List<AgentTemplateMongoElement>(),
223+
Functions = x.Functions?
224+
.Select(f => FunctionDefMongoElement.ToMongoElement(f))?
225+
.ToList() ?? new List<FunctionDefMongoElement>(),
226+
Responses = x.Responses?
227+
.Select(r => AgentResponseMongoElement.ToMongoElement(r))?
228+
.ToList() ?? new List<AgentResponseMongoElement>(),
217229
IsPublic = x.IsPublic,
218230
AllowRouting = x.AllowRouting,
219231
Disabled = x.Disabled,
@@ -281,7 +293,7 @@ public int Transaction<TTableInterface>(Action action)
281293
{
282294
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid() : new Guid(x.Id),
283295
AgentId = Guid.Parse(x.AgentId),
284-
UserId = Guid.Parse(x.UserId),
296+
UserId = !string.IsNullOrEmpty(x.UserId) && Guid.TryParse(x.UserId, out var _) ? Guid.Parse(x.UserId) : Guid.Empty,
285297
CreatedTime = x.CreatedTime,
286298
UpdatedTime = x.UpdatedTime
287299
}).ToList();
@@ -446,9 +458,10 @@ private void UpdateAgentFunctions(string agentId, List<FunctionDef> functions)
446458
{
447459
if (functions.IsNullOrEmpty()) return;
448460

461+
var functionsToUpdate = functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList();
449462
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
450463
var update = Builders<AgentCollection>.Update
451-
.Set(x => x.Functions, functions)
464+
.Set(x => x.Functions, functionsToUpdate)
452465
.Set(x => x.UpdatedTime, DateTime.UtcNow);
453466

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

474+
var templatesToUpdate = templates.Select(t => AgentTemplateMongoElement.ToMongoElement(t)).ToList();
461475
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
462476
var update = Builders<AgentCollection>.Update
463-
.Set(x => x.Templates, templates)
477+
.Set(x => x.Templates, templatesToUpdate)
464478
.Set(x => x.UpdatedTime, DateTime.UtcNow);
465479

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

487+
var responsesToUpdate = responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList();
473488
var filter = Builders<AgentCollection>.Filter.Eq(x => x.Id, Guid.Parse(agentId));
474489
var update = Builders<AgentCollection>.Update
475-
.Set(x => x.Responses, responses)
490+
.Set(x => x.Responses, responsesToUpdate)
476491
.Set(x => x.UpdatedTime, DateTime.UtcNow);
477492

478493
_dc.Agents.UpdateOne(filter, update);
@@ -487,11 +502,11 @@ private void UpdateAgentAllFields(Agent agent)
487502
.Set(x => x.Disabled, agent.Disabled)
488503
.Set(x => x.AllowRouting, agent.AllowRouting)
489504
.Set(x => x.Profiles, agent.Profiles)
490-
.Set(x => x.RoutingRules, agent.RoutingRules.Select(x => RoutingRuleMongoElement.ToMongoElement(x)).ToList())
505+
.Set(x => x.RoutingRules, agent.RoutingRules.Select(r => RoutingRuleMongoElement.ToMongoElement(r)).ToList())
491506
.Set(x => x.Instruction, agent.Instruction)
492-
.Set(x => x.Templates, agent.Templates)
493-
.Set(x => x.Functions, agent.Functions)
494-
.Set(x => x.Responses, agent.Responses)
507+
.Set(x => x.Templates, agent.Templates.Select(t => AgentTemplateMongoElement.ToMongoElement(t)).ToList())
508+
.Set(x => x.Functions, agent.Functions.Select(f => FunctionDefMongoElement.ToMongoElement(f)).ToList())
509+
.Set(x => x.Responses, agent.Responses.Select(r => AgentResponseMongoElement.ToMongoElement(r)).ToList())
495510
.Set(x => x.IsPublic, agent.IsPublic)
496511
.Set(x => x.UpdatedTime, DateTime.UtcNow);
497512

@@ -535,7 +550,7 @@ public void CreateNewConversation(Conversation conversation)
535550
{
536551
Id = !string.IsNullOrEmpty(conversation.Id) ? Guid.Parse(conversation.Id) : Guid.NewGuid(),
537552
AgentId = Guid.Parse(conversation.AgentId),
538-
UserId = Guid.Parse(conversation.UserId),
553+
UserId = !string.IsNullOrEmpty(conversation.UserId) && Guid.TryParse(conversation.UserId, out var _) ? Guid.Parse(conversation.UserId) : Guid.Empty,
539554
Title = conversation.Title,
540555
States = conversation.States?.ToKeyValueList() ?? new List<StateKeyValue>(),
541556
CreatedTime = DateTime.UtcNow,
@@ -637,7 +652,7 @@ public Conversation GetConversation(string conversationId)
637652
public List<Conversation> GetConversations(string userId)
638653
{
639654
var records = new List<Conversation>();
640-
if (string.IsNullOrEmpty(userId)) return records;
655+
if (string.IsNullOrEmpty(userId) || !Guid.TryParse(userId, out var _)) return records;
641656

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

0 commit comments

Comments
 (0)