Skip to content

add active round log #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,31 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
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
Expand All @@ -79,7 +81,7 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
Data = currentValue,
MessageId = routingCtx.MessageId,
Active = true,
ActiveRounds = activeRounds > 0 ? activeRounds : -1,
ActiveRounds = curActiveRounds,
UpdateTime = DateTime.UtcNow,
};

Expand Down
2 changes: 2 additions & 0 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ private string BuildStateChangeLog(StateChangeModel stateChange)
MessageId = stateChange.MessageId,
Name = stateChange.Name,
BeforeValue = stateChange.BeforeValue,
BeforeActiveRounds = stateChange.BeforeActiveRounds,
AfterValue = stateChange.AfterValue,
AfterActiveRounds = stateChange.AfterActiveRounds,
CreateTime = DateTime.UtcNow
};

Expand Down