Skip to content

clean code #400

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 1 commit into from
Apr 8, 2024
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 @@ -58,9 +58,9 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe

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}");
Expand Down Expand Up @@ -139,7 +139,7 @@ public Dictionary<string, string> Load(string conversationId)
{
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
Expand Down Expand Up @@ -261,7 +261,7 @@ public bool RemoveState(string name)
AfterActiveRounds = leafNode.ActiveRounds,
DataType = leafNode.DataType,
Source = leafNode.Source,
Readonly = _curStates[name].Readonly
Readonly = value.Readonly
}).Wait();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,30 +34,13 @@ public void CreateNewConversation(Conversation conversation)
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));
}
}

Expand Down Expand Up @@ -180,8 +162,8 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea
{
MessageId = breakpoint.MessageId,
Breakpoint = breakpoint.Breakpoint,
CreatedTime = DateTime.UtcNow,
Reason = breakpoint.Reason,
CreatedTime = DateTime.UtcNow,
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Plugin.MongoStorage.Collections;
using BotSharp.Plugin.MongoStorage.Models;
using System.Text.RegularExpressions;

namespace BotSharp.Plugin.MongoStorage.Repository;

Expand Down Expand Up @@ -34,21 +33,11 @@ public void CreateNewConversation(Conversation conversation)
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>()
};

Expand Down Expand Up @@ -169,19 +158,20 @@ public void UpdateConversationBreakpoint(string conversationId, ConversationBrea

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)
Expand Down