@@ -41,9 +41,15 @@ public IQueryable<Agent> Agents
41
41
Name = x . Name ,
42
42
Description = x . Description ,
43
43
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 > ( ) ,
47
53
IsPublic = x . IsPublic ,
48
54
Disabled = x . Disabled ,
49
55
AllowRouting = x . AllowRouting ,
@@ -183,7 +189,7 @@ public int Transaction<TTableInterface>(Action action)
183
189
{
184
190
Id = string . IsNullOrEmpty ( x . Id ) ? Guid . NewGuid ( ) : new Guid ( x . Id ) ,
185
191
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 ,
187
193
Title = x . Title ,
188
194
States = x . States ? . ToKeyValueList ( ) ?? new List < StateKeyValue > ( ) ,
189
195
CreatedTime = x . CreatedTime ,
@@ -211,9 +217,15 @@ public int Transaction<TTableInterface>(Action action)
211
217
Name = x . Name ,
212
218
Description = x . Description ,
213
219
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 > ( ) ,
217
229
IsPublic = x . IsPublic ,
218
230
AllowRouting = x . AllowRouting ,
219
231
Disabled = x . Disabled ,
@@ -281,7 +293,7 @@ public int Transaction<TTableInterface>(Action action)
281
293
{
282
294
Id = string . IsNullOrEmpty ( x . Id ) ? Guid . NewGuid ( ) : new Guid ( x . Id ) ,
283
295
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 ,
285
297
CreatedTime = x . CreatedTime ,
286
298
UpdatedTime = x . UpdatedTime
287
299
} ) . ToList ( ) ;
@@ -446,9 +458,10 @@ private void UpdateAgentFunctions(string agentId, List<FunctionDef> functions)
446
458
{
447
459
if ( functions . IsNullOrEmpty ( ) ) return ;
448
460
461
+ var functionsToUpdate = functions . Select ( f => FunctionDefMongoElement . ToMongoElement ( f ) ) . ToList ( ) ;
449
462
var filter = Builders < AgentCollection > . Filter . Eq ( x => x . Id , Guid . Parse ( agentId ) ) ;
450
463
var update = Builders < AgentCollection > . Update
451
- . Set ( x => x . Functions , functions )
464
+ . Set ( x => x . Functions , functionsToUpdate )
452
465
. Set ( x => x . UpdatedTime , DateTime . UtcNow ) ;
453
466
454
467
_dc . Agents . UpdateOne ( filter , update ) ;
@@ -458,9 +471,10 @@ private void UpdateAgentTemplates(string agentId, List<AgentTemplate> templates)
458
471
{
459
472
if ( templates . IsNullOrEmpty ( ) ) return ;
460
473
474
+ var templatesToUpdate = templates . Select ( t => AgentTemplateMongoElement . ToMongoElement ( t ) ) . ToList ( ) ;
461
475
var filter = Builders < AgentCollection > . Filter . Eq ( x => x . Id , Guid . Parse ( agentId ) ) ;
462
476
var update = Builders < AgentCollection > . Update
463
- . Set ( x => x . Templates , templates )
477
+ . Set ( x => x . Templates , templatesToUpdate )
464
478
. Set ( x => x . UpdatedTime , DateTime . UtcNow ) ;
465
479
466
480
_dc . Agents . UpdateOne ( filter , update ) ;
@@ -470,9 +484,10 @@ private void UpdateAgentResponses(string agentId, List<AgentResponse> responses)
470
484
{
471
485
if ( responses . IsNullOrEmpty ( ) ) return ;
472
486
487
+ var responsesToUpdate = responses . Select ( r => AgentResponseMongoElement . ToMongoElement ( r ) ) . ToList ( ) ;
473
488
var filter = Builders < AgentCollection > . Filter . Eq ( x => x . Id , Guid . Parse ( agentId ) ) ;
474
489
var update = Builders < AgentCollection > . Update
475
- . Set ( x => x . Responses , responses )
490
+ . Set ( x => x . Responses , responsesToUpdate )
476
491
. Set ( x => x . UpdatedTime , DateTime . UtcNow ) ;
477
492
478
493
_dc . Agents . UpdateOne ( filter , update ) ;
@@ -487,11 +502,11 @@ private void UpdateAgentAllFields(Agent agent)
487
502
. Set ( x => x . Disabled , agent . Disabled )
488
503
. Set ( x => x . AllowRouting , agent . AllowRouting )
489
504
. 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 ( ) )
491
506
. 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 ( ) )
495
510
. Set ( x => x . IsPublic , agent . IsPublic )
496
511
. Set ( x => x . UpdatedTime , DateTime . UtcNow ) ;
497
512
@@ -535,7 +550,7 @@ public void CreateNewConversation(Conversation conversation)
535
550
{
536
551
Id = ! string . IsNullOrEmpty ( conversation . Id ) ? Guid . Parse ( conversation . Id ) : Guid . NewGuid ( ) ,
537
552
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 ,
539
554
Title = conversation . Title ,
540
555
States = conversation . States ? . ToKeyValueList ( ) ?? new List < StateKeyValue > ( ) ,
541
556
CreatedTime = DateTime . UtcNow ,
@@ -637,7 +652,7 @@ public Conversation GetConversation(string conversationId)
637
652
public List < Conversation > GetConversations ( string userId )
638
653
{
639
654
var records = new List < Conversation > ( ) ;
640
- if ( string . IsNullOrEmpty ( userId ) ) return records ;
655
+ if ( string . IsNullOrEmpty ( userId ) || ! Guid . TryParse ( userId , out var _ ) ) return records ;
641
656
642
657
var filterByUserId = Builders < ConversationCollection > . Filter . Eq ( x => x . UserId , Guid . Parse ( userId ) ) ;
643
658
var conversations = _dc . Conversations . Find ( filterByUserId ) . ToList ( ) ;
0 commit comments