Merge pull request #398 from iceljc/features/add-state-change-hook-remove-state

add hook
This commit is contained in:
C. Oceania 2024-04-07 13:40:47 -05:00 committed by GitHub
commit 4dece4b4d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 7 deletions

View file

@ -12,13 +12,13 @@ public class StateChangeModel
public string Name { get; set; }
[JsonPropertyName("before_value")]
public string BeforeValue { get; set; }
public string? BeforeValue { get; set; }
[JsonPropertyName("before_active_rounds")]
public int? BeforeActiveRounds { get; set; }
[JsonPropertyName("after_value")]
public string AfterValue { get; set; }
public string? AfterValue { get; set; }
[JsonPropertyName("after_active_rounds")]
public int? AfterActiveRounds { get; set; }

View file

@ -201,9 +201,9 @@ public class ConversationStateService : IConversationStateService, IDisposable
var key = pair.Key;
var curValue = pair.Value;
if (!_historyStates.TryGetValue(key, out var historyValue)
|| historyValue == null
|| historyValue.Values.IsNullOrEmpty()
if (!_historyStates.TryGetValue(key, out var historyValue)
|| historyValue == null
|| historyValue.Values.IsNullOrEmpty()
|| !curValue.Versioning)
{
states.Add(curValue);
@ -232,8 +232,9 @@ public class ConversationStateService : IConversationStateService, IDisposable
if (!ContainsState(name)) return false;
var routingCtx = _services.GetRequiredService<IRoutingContext>();
var leafNode = _curStates[name].Values?.LastOrDefault();
if (leafNode == null) return false;
var value = _curStates[name];
var leafNode = value?.Values?.LastOrDefault();
if (value == null || !value.Versioning || leafNode == null) return false;
_curStates[name].Values.Add(new StateValue
{
@ -246,6 +247,24 @@ public class ConversationStateService : IConversationStateService, IDisposable
UpdateTime = DateTime.UtcNow
});
var hooks = _services.GetServices<IConversationHook>();
foreach (var hook in hooks)
{
hook.OnStateChanged(new StateChangeModel
{
ConversationId = _conversationId,
MessageId = routingCtx.MessageId,
Name = name,
BeforeValue = leafNode.Data,
BeforeActiveRounds = leafNode.ActiveRounds,
AfterValue = null,
AfterActiveRounds = leafNode.ActiveRounds,
DataType = leafNode.DataType,
Source = leafNode.Source,
Readonly = _curStates[name].Readonly
}).Wait();
}
return true;
}