refine valid condition
This commit is contained in:
parent
796388a8fc
commit
3458e9f70f
|
|
@ -23,7 +23,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
private readonly IServiceProvider _services;
|
||||
private readonly ILogger<BotSharpConversationSideCar> _logger;
|
||||
|
||||
private Stack<ConversationContext> contextStack = new();
|
||||
private Stack<ConversationContext> _contextStack = new();
|
||||
|
||||
private bool _enabled = false;
|
||||
private string _conversationId = string.Empty;
|
||||
|
|
@ -45,55 +45,55 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
|
||||
public void AppendConversationDialogs(string conversationId, List<DialogElement> messages)
|
||||
{
|
||||
if (contextStack.IsNullOrEmpty() || _conversationId != conversationId)
|
||||
if (!IsValid(conversationId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var top = contextStack.Peek();
|
||||
var top = _contextStack.Peek();
|
||||
top.Dialogs.AddRange(messages);
|
||||
}
|
||||
|
||||
public List<DialogElement> GetConversationDialogs(string conversationId)
|
||||
{
|
||||
if (contextStack.IsNullOrEmpty() || _conversationId != conversationId)
|
||||
if (!IsValid(conversationId))
|
||||
{
|
||||
return new List<DialogElement>();
|
||||
}
|
||||
|
||||
return contextStack.Peek().Dialogs;
|
||||
return _contextStack.Peek().Dialogs;
|
||||
}
|
||||
|
||||
public void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint)
|
||||
{
|
||||
if (contextStack.IsNullOrEmpty() || _conversationId != conversationId)
|
||||
if (!IsValid(conversationId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var top = contextStack.Peek().Breakpoints;
|
||||
var top = _contextStack.Peek().Breakpoints;
|
||||
top.Add(breakpoint);
|
||||
}
|
||||
|
||||
public ConversationBreakpoint? GetConversationBreakpoint(string conversationId)
|
||||
{
|
||||
if (contextStack.IsNullOrEmpty() || _conversationId != conversationId)
|
||||
if (!IsValid(conversationId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var top = contextStack.Peek().Breakpoints;
|
||||
var top = _contextStack.Peek().Breakpoints;
|
||||
return top.LastOrDefault();
|
||||
}
|
||||
|
||||
public void UpdateConversationStates(string conversationId, List<StateKeyValue> states)
|
||||
{
|
||||
if (contextStack.IsNullOrEmpty() || _conversationId != conversationId)
|
||||
if (!IsValid(conversationId))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var top = contextStack.Peek();
|
||||
var top = _contextStack.Peek();
|
||||
top.State = new ConversationState(states);
|
||||
}
|
||||
|
||||
|
|
@ -148,7 +148,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
RecursiveCounter = routing.Context.GetRecursiveCounter(),
|
||||
RoutingStack = routing.Context.GetAgentStack()
|
||||
};
|
||||
contextStack.Push(node);
|
||||
_contextStack.Push(node);
|
||||
|
||||
// Reset
|
||||
state.ResetCurrentState();
|
||||
|
|
@ -163,7 +163,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
var state = _services.GetRequiredService<IConversationStateService>();
|
||||
var routing = _services.GetRequiredService<IRoutingService>();
|
||||
|
||||
var node = contextStack.Pop();
|
||||
var node = _contextStack.Pop();
|
||||
|
||||
// Recover
|
||||
state.SetCurrentState(node.State);
|
||||
|
|
@ -173,4 +173,12 @@ public class BotSharpConversationSideCar : IConversationSideCar
|
|||
Utilities.ClearCache();
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
private bool IsValid(string conversationId)
|
||||
{
|
||||
return !_contextStack.IsNullOrEmpty()
|
||||
&& _conversationId == conversationId
|
||||
&& !string.IsNullOrEmpty(conversationId)
|
||||
&& !string.IsNullOrEmpty(_conversationId);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue