Skip to content

Commit a245d71

Browse files
authored
Merge pull request #381 from iceljc/features/refine-state-change
add active round log
2 parents da9becb + f61ee50 commit a245d71

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/StateChangeModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public class StateChangeModel
1414
[JsonPropertyName("before_value")]
1515
public string BeforeValue { get; set; }
1616

17+
[JsonPropertyName("before_active_rounds")]
18+
public int? BeforeActiveRounds { get; set; }
19+
1720
[JsonPropertyName("after_value")]
1821
public string AfterValue { get; set; }
22+
23+
[JsonPropertyName("after_active_rounds")]
24+
public int? AfterActiveRounds { get; set; }
1925
}

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,31 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
4343
var preValue = string.Empty;
4444
var currentValue = value.ToString();
4545
var hooks = _services.GetServices<IConversationHook>();
46+
var curActiveRounds = activeRounds > 0 ? activeRounds : -1;
47+
int? preActiveRounds = null;
4648

4749
if (ContainsState(name) && _states.TryGetValue(name, out var pair))
4850
{
49-
var lastNode = pair?.Values.LastOrDefault();
51+
var lastNode = pair?.Values?.LastOrDefault();
52+
preActiveRounds = lastNode?.ActiveRounds;
5053
preValue = lastNode?.Data ?? string.Empty;
5154
}
5255

5356
_logger.LogInformation($"[STATE] {name} = {value}");
5457
var routingCtx = _services.GetRequiredService<IRoutingContext>();
5558

56-
if (!ContainsState(name) || preValue != currentValue)
59+
foreach (var hook in hooks)
5760
{
58-
foreach (var hook in hooks)
61+
hook.OnStateChanged(new StateChangeModel
5962
{
60-
hook.OnStateChanged(new StateChangeModel
61-
{
62-
ConversationId = _conversationId,
63-
MessageId = routingCtx.MessageId,
64-
Name = name,
65-
BeforeValue = preValue,
66-
AfterValue = currentValue
67-
}).Wait();
68-
}
63+
ConversationId = _conversationId,
64+
MessageId = routingCtx.MessageId,
65+
Name = name,
66+
BeforeValue = preValue,
67+
BeforeActiveRounds = preActiveRounds,
68+
AfterValue = currentValue,
69+
AfterActiveRounds = curActiveRounds
70+
}).Wait();
6971
}
7072

7173
var newPair = new StateKeyValue
@@ -79,7 +81,7 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
7981
Data = currentValue,
8082
MessageId = routingCtx.MessageId,
8183
Active = true,
82-
ActiveRounds = activeRounds > 0 ? activeRounds : -1,
84+
ActiveRounds = curActiveRounds,
8385
UpdateTime = DateTime.UtcNow,
8486
};
8587

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ private string BuildStateChangeLog(StateChangeModel stateChange)
394394
MessageId = stateChange.MessageId,
395395
Name = stateChange.Name,
396396
BeforeValue = stateChange.BeforeValue,
397+
BeforeActiveRounds = stateChange.BeforeActiveRounds,
397398
AfterValue = stateChange.AfterValue,
399+
AfterActiveRounds = stateChange.AfterActiveRounds,
398400
CreateTime = DateTime.UtcNow
399401
};
400402

0 commit comments

Comments
 (0)