Skip to content
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

Features/add state change log #371

Merged
merged 2 commits into from
Mar 28, 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 @@ -29,7 +29,7 @@ public IConversationHook SetConversation(Conversation conversation)
public virtual Task OnStateLoaded(ConversationState state)
=> Task.CompletedTask;

public virtual Task OnStateChanged(string name, string preValue, string currentValue)
public virtual Task OnStateChanged(StateChangeModel stateChange)
=> Task.CompletedTask;

public virtual Task OnDialogRecordLoaded(RoleDialogModel dialog)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface IConversationHook
Task OnDialogRecordLoaded(RoleDialogModel dialog);

Task OnStateLoaded(ConversationState state);
Task OnStateChanged(string name, string preValue, string currentValue);
Task OnStateChanged(StateChangeModel stateChange);

Task OnMessageReceived(RoleDialogModel message);
Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace BotSharp.Abstraction.Conversations.Models;

public class StateChangeModel
{
[JsonPropertyName("conversation_id")]
public string ConversationId { get; set; }

[JsonPropertyName("message_id")]
public string MessageId { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("before_value")]
public string BeforeValue { get; set; }

[JsonPropertyName("after_value")]
public string AfterValue { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Loggers.Models;

public class StateChangeOutputModel : StateChangeModel
{
[JsonPropertyName("created_at")]
public DateTime CreateTime { get; set; } = DateTime.UtcNow;
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public MessageParser()
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
}
}
}

Expand Down Expand Up @@ -80,6 +84,10 @@ public MessageParser()
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
}
else if (elementType == typeof(ButtonElement).Name)
{
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
if (!ContainsState(name) || preValue != currentValue)
{
_logger.LogInformation($"[STATE] {name} = {value}");
var routingCtx = _services.GetRequiredService<IRoutingContext>();

foreach (var hook in hooks)
{
hook.OnStateChanged(name, preValue, currentValue).Wait();
hook.OnStateChanged(new StateChangeModel
{
ConversationId = _conversationId,
MessageId = routingCtx.MessageId,
Name = name,
BeforeValue = preValue,
AfterValue = currentValue
}).Wait();
}

var routingCtx = _services.GetRequiredService<IRoutingContext>();

var newPair = new StateKeyValue
{
Key = name,
Expand Down Expand Up @@ -118,7 +126,7 @@ public Dictionary<string, string> Load(string conversationId)
state.Value.Values.Add(new StateValue
{
Data = value.Data,
MessageId = !string.IsNullOrEmpty(curMsgId) ? curMsgId : value.MessageId,
MessageId = curMsgId,
Active = false,
ActiveRounds = value.ActiveRounds,
UpdateTime = DateTime.UtcNow
Expand Down Expand Up @@ -178,7 +186,7 @@ public void CleanStates()
value.Values.Add(new StateValue
{
Data = lastValue.Data,
MessageId = !string.IsNullOrEmpty(curMsgId) ? curMsgId : lastValue.MessageId,
MessageId = curMsgId,
Active = false,
ActiveRounds = lastValue.ActiveRounds,
UpdateTime = utcNow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ public List<string> GetIdleConversations(int batchSize, int messageLimit, int bu
var utcNow = DateTime.UtcNow;
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);

if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

if (batchSize <= 0 || batchSize > batchLimit)
{
batchSize = batchLimit;
Expand Down
21 changes: 21 additions & 0 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ public override async Task OnBreakpointUpdated(string conversationId, bool reset
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
}

public override async Task OnStateChanged(StateChangeModel stateChange)
{
if (stateChange == null) return;

await _chatHub.Clients.User(_user.Id).SendAsync("OnStateChangeGenerated", BuildStateChangeLog(stateChange));
}
#endregion

#region IRoutingHook
Expand Down Expand Up @@ -379,4 +385,19 @@ private string BuildStateLog(string conversationId, Dictionary<string, string> s

return JsonSerializer.Serialize(log, _options.JsonSerializerOptions);
}

private string BuildStateChangeLog(StateChangeModel stateChange)
{
var log = new StateChangeOutputModel
{
ConversationId = stateChange.ConversationId,
MessageId = stateChange.MessageId,
Name = stateChange.Name,
BeforeValue = stateChange.BeforeValue,
AfterValue = stateChange.AfterValue,
CreateTime = DateTime.UtcNow
};

return JsonSerializer.Serialize(log, _options.JsonSerializerOptions);
}
}