clean code

This commit is contained in:
Jicheng Lu 2024-04-08 09:58:25 -05:00
parent 3e3760f794
commit 4723c64e3c
4 changed files with 19 additions and 47 deletions

View file

@ -58,9 +58,9 @@ public class ConversationStateService : IConversationStateService, IDisposable
if (ContainsState(name) && _curStates.TryGetValue(name, out var pair))
{
var lastNode = pair?.Values?.LastOrDefault();
preActiveRounds = lastNode?.ActiveRounds;
preValue = lastNode?.Data ?? string.Empty;
var leafNode = pair?.Values?.LastOrDefault();
preActiveRounds = leafNode?.ActiveRounds;
preValue = leafNode?.Data ?? string.Empty;
}
_logger.LogInformation($"[STATE] {name} = {value}");
@ -139,7 +139,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
{
var key = state.Key;
var value = state.Value;
var leafNode = value.Values.LastOrDefault();
var leafNode = value?.Values?.LastOrDefault();
if (leafNode == null) continue;
_curStates[key] = new StateKeyValue
@ -261,7 +261,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
AfterActiveRounds = leafNode.ActiveRounds,
DataType = leafNode.DataType,
Source = leafNode.Source,
Readonly = _curStates[name].Readonly
Readonly = value.Readonly
}).Wait();
}

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using System.Globalization;
using System.IO;
@ -35,30 +34,13 @@ namespace BotSharp.Core.Repository
var stateFile = Path.Combine(dir, STATE_FILE);
if (!File.Exists(stateFile))
{
var states = conversation.States ?? new Dictionary<string, string>();
var initialStates = states.Select(x => new StateKeyValue
{
Key = x.Key,
Values = new List<StateValue>
{
new StateValue { Data = x.Value, UpdateTime = DateTime.UtcNow }
}
}).ToList();
File.WriteAllText(stateFile, JsonSerializer.Serialize(initialStates, _options));
File.WriteAllText(stateFile, JsonSerializer.Serialize(new List<StateKeyValue>(), _options));
}
var breakpointFile = Path.Combine(dir, BREAKPOINT_FILE);
if (!File.Exists(breakpointFile))
{
var initialBreakpoints = new List<ConversationBreakpoint>
{
new ConversationBreakpoint()
{
Breakpoint = utcNow.AddMilliseconds(-100),
CreatedTime = DateTime.UtcNow
}
};
File.WriteAllText(breakpointFile, JsonSerializer.Serialize(initialBreakpoints, _options));
File.WriteAllText(breakpointFile, JsonSerializer.Serialize(new List<ConversationBreakpoint>(), _options));
}
}
@ -180,8 +162,8 @@ namespace BotSharp.Core.Repository
{
MessageId = breakpoint.MessageId,
Breakpoint = breakpoint.Breakpoint,
CreatedTime = DateTime.UtcNow,
Reason = breakpoint.Reason,
CreatedTime = DateTime.UtcNow,
}
};

View file

@ -5,6 +5,6 @@ namespace BotSharp.Plugin.MongoStorage.Collections;
public class ConversationStateDocument : MongoBase
{
public string ConversationId { get; set; }
public List<StateMongoElement> States { get; set; }
public List<BreakpointMongoElement> Breakpoints { get; set; }
public List<StateMongoElement> States { get; set; } = new List<StateMongoElement>();
public List<BreakpointMongoElement> Breakpoints { get; set; } = new List<BreakpointMongoElement>();
}

View file

@ -3,7 +3,6 @@ using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Plugin.MongoStorage.Collections;
using BotSharp.Plugin.MongoStorage.Models;
using System.Text.RegularExpressions;
namespace BotSharp.Plugin.MongoStorage.Repository;
@ -34,21 +33,11 @@ public partial class MongoRepository
Dialogs = new List<DialogMongoElement>()
};
var states = conversation.States ?? new Dictionary<string, string>();
var initialStates = states.Select(x => new StateMongoElement
{
Key = x.Key,
Values = new List<StateValueMongoElement>
{
new StateValueMongoElement { Data = x.Value, UpdateTime = DateTime.UtcNow }
}
}).ToList();
var stateDoc = new ConversationStateDocument
{
Id = Guid.NewGuid().ToString(),
ConversationId = convDoc.Id,
States = initialStates,
States = new List<StateMongoElement>(),
Breakpoints = new List<BreakpointMongoElement>()
};
@ -169,19 +158,20 @@ public partial class MongoRepository
var filter = Builders<ConversationStateDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var state = _dc.ConversationStates.Find(filter).FirstOrDefault();
var leafNode = state?.Breakpoints?.LastOrDefault();
if (state == null || state.Breakpoints.IsNullOrEmpty())
if (leafNode == null)
{
return null;
}
return state.Breakpoints.Select(x => new ConversationBreakpoint
return new ConversationBreakpoint
{
Breakpoint = x.Breakpoint,
CreatedTime = x.CreatedTime,
MessageId = x.MessageId,
Reason = x.Reason,
}).LastOrDefault();
Breakpoint = leafNode.Breakpoint,
MessageId = leafNode.MessageId,
Reason = leafNode.Reason,
CreatedTime = leafNode.CreatedTime,
};
}
public ConversationState GetConversationStates(string conversationId)