Skip to content

Commit 5d5abf5

Browse files
authored
Merge pull request #1089 from iceljc/bugfix/fix-sidecar-reset-states
refine side car states
2 parents de34ac9 + 3968463 commit 5d5abf5

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

src/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.SideCar.Models;
2+
13
namespace BotSharp.Abstraction.SideCar;
24

35
public interface IConversationSideCar
@@ -11,5 +13,8 @@ public interface IConversationSideCar
1113
ConversationBreakpoint? GetConversationBreakpoint(string conversationId);
1214
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
1315
Task<RoleDialogModel> SendMessage(string agentId, string text,
14-
PostbackMessageModel? postback = null, List<MessageState>? states = null, List<DialogElement>? dialogs = null);
16+
PostbackMessageModel? postback = null,
17+
List<MessageState>? states = null,
18+
List<DialogElement>? dialogs = null,
19+
SideCarOptions? options = null);
1520
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace BotSharp.Abstraction.SideCar.Models;
2+
3+
public class SideCarOptions
4+
{
5+
public bool IsInheritStates { get; set; }
6+
public IEnumerable<string>? InheritStateKeys { get; set; }
7+
8+
public static SideCarOptions Empty()
9+
{
10+
return new SideCarOptions();
11+
}
12+
}

src/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using BotSharp.Abstraction.SideCar.Models;
1718
using BotSharp.Core.Infrastructures;
1819

1920
namespace BotSharp.Core.SideCar.Services;
@@ -24,6 +25,7 @@ public class BotSharpConversationSideCar : IConversationSideCar
2425
private readonly ILogger<BotSharpConversationSideCar> _logger;
2526

2627
private Stack<ConversationContext> _contextStack = new();
28+
private SideCarOptions? _sideCarOptions;
2729

2830
private bool _enabled = false;
2931
private string _conversationId = string.Empty;
@@ -98,8 +100,13 @@ public void UpdateConversationStates(string conversationId, List<StateKeyValue>
98100
}
99101

100102
public async Task<RoleDialogModel> SendMessage(string agentId, string text,
101-
PostbackMessageModel? postback = null, List<MessageState>? states = null, List<DialogElement>? dialogs = null)
103+
PostbackMessageModel? postback = null,
104+
List<MessageState>? states = null,
105+
List<DialogElement>? dialogs = null,
106+
SideCarOptions? options = null)
102107
{
108+
_sideCarOptions = options;
109+
103110
BeforeExecute(dialogs);
104111
var response = await InnerExecute(agentId, text, postback, states);
105112
AfterExecute();
@@ -166,7 +173,7 @@ private void AfterExecute()
166173
var node = _contextStack.Pop();
167174

168175
// Recover
169-
state.SetCurrentState(node.State);
176+
RestoreStates(node.State);
170177
routing.Context.SetRecursiveCounter(node.RecursiveCounter);
171178
routing.Context.SetAgentStack(node.RoutingStack);
172179
routing.Context.SetDialogs(node.RoutingDialogs);
@@ -181,4 +188,43 @@ private bool IsValid(string conversationId)
181188
&& !string.IsNullOrEmpty(conversationId)
182189
&& !string.IsNullOrEmpty(_conversationId);
183190
}
191+
192+
private void RestoreStates(ConversationState prevStates)
193+
{
194+
var innerStates = prevStates;
195+
var state = _services.GetRequiredService<IConversationStateService>();
196+
197+
if (_sideCarOptions?.IsInheritStates == true)
198+
{
199+
var curStates = state.GetCurrentState();
200+
foreach (var pair in curStates)
201+
{
202+
var endNode = pair.Value.Values.LastOrDefault();
203+
if (endNode == null) continue;
204+
205+
if (_sideCarOptions?.InheritStateKeys?.Any() == true
206+
&& !_sideCarOptions.InheritStateKeys.Contains(pair.Key))
207+
{
208+
continue;
209+
}
210+
211+
if (innerStates.ContainsKey(pair.Key))
212+
{
213+
innerStates[pair.Key].Values.Add(endNode);
214+
}
215+
else
216+
{
217+
innerStates[pair.Key] = new StateKeyValue
218+
{
219+
Key = pair.Key,
220+
Versioning = pair.Value.Versioning,
221+
Readonly = pair.Value.Readonly,
222+
Values = [endNode]
223+
};
224+
}
225+
}
226+
}
227+
228+
state.SetCurrentState(innerStates);
229+
}
184230
}

src/Infrastructure/BotSharp.Core.SideCar/Using.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
global using BotSharp.Abstraction.Models;
1717
global using BotSharp.Abstraction.Routing;
1818
global using BotSharp.Abstraction.SideCar;
19+
global using BotSharp.Abstraction.SideCar.Models;
1920
global using BotSharp.Abstraction.Utilities;
20-
global using BotSharp.Core.SideCar.Settings;
21+
global using BotSharp.Core.SideCar.Settings;

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public ConversationState GetCurrentState()
443443

444444
public void SetCurrentState(ConversationState state)
445445
{
446-
var values = _curStates.Values.ToList();
446+
var values = state.Values.ToList();
447447
var copy = JsonSerializer.Deserialize<List<StateKeyValue>>(JsonSerializer.Serialize(values));
448448
_curStates = new ConversationState(copy ?? []);
449449
}

0 commit comments

Comments
 (0)