Skip to content

refine conversation states #245

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
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -8,10 +8,10 @@ namespace BotSharp.Abstraction.Conversations;
public interface IConversationStateService
{
string GetConversationId();
ConversationState Load(string conversationId);
Dictionary<string, string> Load(string conversationId);
string GetState(string name, string defaultValue = "");
bool ContainsState(string name);
ConversationState GetStates();
Dictionary<string, string> GetStates();
IConversationStateService SetState<T>(string name, T value, bool isNeedVersion = true);
void SaveStateByArgs(JsonDocument args);
void CleanState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Conversation
public List<DialogElement> Dialogs { get; set; } = new List<DialogElement>();

[JsonIgnore]
public ConversationState States { get; set; } = new ConversationState();
public Dictionary<string, string> States { get; set; } = new Dictionary<string, string>();

public string Status { get; set; } = ConversationStatus.Open;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
namespace BotSharp.Abstraction.Conversations.Models;

public class ConversationState : Dictionary<string, string>
public class ConversationState : Dictionary<string, List<StateValue>>
{
public ConversationState()
{

}

public ConversationState(List<StateKeyValue> pairs)
{
foreach (var pair in pairs)
{
this[pair.Key] = pair.Value;
this[pair.Key] = pair.Values;
}
}

public List<StateKeyValue> ToKeyValueList()
{
return this.Select(x => new StateKeyValue(x.Key, x.Value)).ToList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ namespace BotSharp.Abstraction.Conversations.Models;
public class StateKeyValue
{
public string Key { get; set; }
public string Value { get; set; }
public List<StateValue> Values { get; set; } = new List<StateValue>();

public StateKeyValue()
{

}

public StateKeyValue(string key, string value)
public StateKeyValue(string key, List<StateValue> values)
{
Key = key;
Value = value;
Values = values;
}
}

public class StateValue
{
public string Data { get; set; }
public DateTime UpdateTime { get; set; }

public StateValue()
{

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ public class InstructResult : ITrackableMessage
public string MessageId { get; set; }
public string Text { get; set; }
public object Data { get; set; }
public ConversationState States { get; set; }
public Dictionary<string, string> States { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public interface IBotSharpRepository
List<DialogElement> GetConversationDialogs(string conversationId);
void UpdateConversationDialogElements(string conversationId, List<DialogContentUpdateModel> updateElements);
void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs);
List<HistoryStateKeyValue> GetConversationStates(string conversationId);
void UpdateConversationStates(string conversationId, List<HistoryStateKeyValue> states);
List<StateKeyValue> GetConversationStates(string conversationId);
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
void UpdateConversationStatus(string conversationId, string status);
Conversation GetConversation(string conversationId);
List<Conversation> GetConversations(ConversationFilter filter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Text.Json.Serialization;

namespace BotSharp.Abstraction.Repositories.Records;

public class ConversationRecord : RecordBase
Expand All @@ -18,9 +16,6 @@ public class ConversationRecord : RecordBase
[JsonIgnore]
public string Dialog { get; set; }

[JsonIgnore]
public List<StateKeyValue> States { get; set; }

[Required]
public DateTime UpdatedTime { get; set; } = DateTime.UtcNow;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.Repositories;
using System.Linq;

namespace BotSharp.Core.Conversations.Services;

Expand All @@ -10,7 +11,6 @@ public class ConversationStateService : IConversationStateService, IDisposable
private readonly ILogger _logger;
private readonly IServiceProvider _services;
private ConversationState _states;
private ConversationHistoryState _historyStates;
private string _conversationId;
private readonly IBotSharpRepository _db;

Expand All @@ -22,7 +22,6 @@ public ConversationStateService(ILogger<ConversationStateService> logger,
_services = services;
_db = db;
_states = new ConversationState();
_historyStates = new ConversationHistoryState();
}

public string GetConversationId() => _conversationId;
Expand All @@ -43,51 +42,56 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
return this;
}

var preValue = string.Empty;
var currentValue = value.ToString();
var hooks = _services.GetServices<IConversationHook>();
string preValue = _states.ContainsKey(name) ? _states[name] : "";

if (!_states.ContainsKey(name) || _states[name] != currentValue)
if (_states.TryGetValue(name, out var values))
{
preValue = values.LastOrDefault()?.Data ?? string.Empty;
}

if (!_states.ContainsKey(name) || preValue != currentValue)
{
_states[name] = currentValue;
_logger.LogInformation($"[STATE] {name} = {value}");
foreach (var hook in hooks)
{
hook.OnStateChanged(name, preValue, currentValue).Wait();
}

var historyStateValue = new HistoryStateValue
var stateValue = new StateValue
{
Data = currentValue,
UpdateTime = DateTime.UtcNow
};

if (!_historyStates.ContainsKey(name) || !isNeedVersion)
if (!_states.ContainsKey(name) || !isNeedVersion)
{
_historyStates[name] = new List<HistoryStateValue> { historyStateValue };
_states[name] = new List<StateValue> { stateValue };
}
else
{
_historyStates[name].Add(historyStateValue);
_states[name].Add(stateValue);
}
}

return this;
}

public ConversationState Load(string conversationId)
public Dictionary<string, string> Load(string conversationId)
{
_conversationId = conversationId;

var savedStates = _db.GetConversationStates(_conversationId).ToList();
_historyStates = new ConversationHistoryState(savedStates);
_states = new ConversationState(savedStates);
var curStates = new Dictionary<string, string>();

if (!savedStates.IsNullOrEmpty())
{
foreach (var state in savedStates)
{
var value = state.Values.LastOrDefault()?.Data ?? string.Empty;
_states[state.Key] = value;
curStates[state.Key] = value;
_logger.LogInformation($"[STATE] {state.Key} : {value}");
}
}
Expand All @@ -99,7 +103,7 @@ public ConversationState Load(string conversationId)
hook.OnStateLoaded(_states).Wait();
}

return _states;
return curStates;
}

public void Save()
Expand All @@ -109,32 +113,40 @@ public void Save()
return;
}

var historyStates = new List<HistoryStateKeyValue>();
var states = new List<StateKeyValue>();

foreach (var dic in _historyStates)
foreach (var dic in _states)
{
historyStates.Add(new HistoryStateKeyValue(dic.Key, dic.Value));
states.Add(new StateKeyValue(dic.Key, dic.Value));
}

_db.UpdateConversationStates(_conversationId, historyStates);
_db.UpdateConversationStates(_conversationId, states);
_logger.LogInformation($"Saved states of conversation {_conversationId}");
}

public void CleanState()
{

_states.Clear();
}

public ConversationState GetStates() => _states;
public Dictionary<string, string> GetStates()
{
var curStates = new Dictionary<string, string>();
foreach (var state in _states)
{
curStates[state.Key] = state.Value.LastOrDefault()?.Data ?? string.Empty;
}
return curStates;
}

public string GetState(string name, string defaultValue = "")
{
if (!_states.ContainsKey(name))
if (!_states.ContainsKey(name) || _states[name].IsNullOrEmpty())
{
return defaultValue;
}

return _states[name];
return _states[name].Last().Data;
}

public void Dispose()
Expand All @@ -144,7 +156,9 @@ public void Dispose()

public bool ContainsState(string name)
{
return _states.ContainsKey(name) && !string.IsNullOrEmpty(_states[name]);
return _states.ContainsKey(name)
&& !_states[name].IsNullOrEmpty()
&& !string.IsNullOrEmpty(_states[name].Last().Data);
}

public void SaveStateByArgs(JsonDocument args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void UpdateConversationDialogElements(string conversationId, List<DialogC
throw new NotImplementedException();
}

public List<HistoryStateKeyValue> GetConversationStates(string conversationId)
public List<StateKeyValue> GetConversationStates(string conversationId)
{
throw new NotImplementedException();
}
Expand All @@ -165,7 +165,7 @@ public void UpdateConversationTitle(string conversationId, string title)
{
throw new NotImplementedException();
}
public void UpdateConversationStates(string conversationId, List<HistoryStateKeyValue> states)
public void UpdateConversationStates(string conversationId, List<StateKeyValue> states)
{
throw new NotImplementedException();
}
Expand Down
Loading