Merge pull request #381 from iceljc/features/refine-state-change

add active round log
This commit is contained in:
C. Oceania 2024-03-31 07:27:29 -05:00 committed by GitHub
commit a245d71ca7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 23 additions and 13 deletions

View file

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

View file

@ -43,29 +43,31 @@ public class ConversationStateService : IConversationStateService, IDisposable
var preValue = string.Empty;
var currentValue = value.ToString();
var hooks = _services.GetServices<IConversationHook>();
var curActiveRounds = activeRounds > 0 ? activeRounds : -1;
int? preActiveRounds = null;
if (ContainsState(name) && _states.TryGetValue(name, out var pair))
{
var lastNode = pair?.Values.LastOrDefault();
var lastNode = pair?.Values?.LastOrDefault();
preActiveRounds = lastNode?.ActiveRounds;
preValue = lastNode?.Data ?? string.Empty;
}
_logger.LogInformation($"[STATE] {name} = {value}");
var routingCtx = _services.GetRequiredService<IRoutingContext>();
if (!ContainsState(name) || preValue != currentValue)
foreach (var hook in hooks)
{
foreach (var hook in hooks)
hook.OnStateChanged(new StateChangeModel
{
hook.OnStateChanged(new StateChangeModel
{
ConversationId = _conversationId,
MessageId = routingCtx.MessageId,
Name = name,
BeforeValue = preValue,
AfterValue = currentValue
}).Wait();
}
ConversationId = _conversationId,
MessageId = routingCtx.MessageId,
Name = name,
BeforeValue = preValue,
BeforeActiveRounds = preActiveRounds,
AfterValue = currentValue,
AfterActiveRounds = curActiveRounds
}).Wait();
}
var newPair = new StateKeyValue
@ -79,7 +81,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
Data = currentValue,
MessageId = routingCtx.MessageId,
Active = true,
ActiveRounds = activeRounds > 0 ? activeRounds : -1,
ActiveRounds = curActiveRounds,
UpdateTime = DateTime.UtcNow,
};

View file

@ -394,7 +394,9 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
MessageId = stateChange.MessageId,
Name = stateChange.Name,
BeforeValue = stateChange.BeforeValue,
BeforeActiveRounds = stateChange.BeforeActiveRounds,
AfterValue = stateChange.AfterValue,
AfterActiveRounds = stateChange.AfterActiveRounds,
CreateTime = DateTime.UtcNow
};