add state change event
This commit is contained in:
parent
78ef7b6c37
commit
bd7d3ffafa
|
|
@ -29,7 +29,7 @@ public abstract class ConversationHookBase : IConversationHook
|
||||||
public virtual Task OnStateLoaded(ConversationState state)
|
public virtual Task OnStateLoaded(ConversationState state)
|
||||||
=> Task.CompletedTask;
|
=> Task.CompletedTask;
|
||||||
|
|
||||||
public virtual Task OnStateChanged(string name, string preValue, string currentValue)
|
public virtual Task OnStateChanged(StateChangeModel stateChange)
|
||||||
=> Task.CompletedTask;
|
=> Task.CompletedTask;
|
||||||
|
|
||||||
public virtual Task OnDialogRecordLoaded(RoleDialogModel dialog)
|
public virtual Task OnDialogRecordLoaded(RoleDialogModel dialog)
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ public interface IConversationHook
|
||||||
Task OnDialogRecordLoaded(RoleDialogModel dialog);
|
Task OnDialogRecordLoaded(RoleDialogModel dialog);
|
||||||
|
|
||||||
Task OnStateLoaded(ConversationState state);
|
Task OnStateLoaded(ConversationState state);
|
||||||
Task OnStateChanged(string name, string preValue, string currentValue);
|
Task OnStateChanged(StateChangeModel stateChange);
|
||||||
|
|
||||||
Task OnMessageReceived(RoleDialogModel message);
|
Task OnMessageReceived(RoleDialogModel message);
|
||||||
Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg);
|
Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg);
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
namespace BotSharp.Abstraction.Loggers.Models;
|
||||||
|
|
||||||
|
public class StateChangeOutputModel : StateChangeModel
|
||||||
|
{
|
||||||
|
[JsonPropertyName("created_at")]
|
||||||
|
public DateTime CreateTime { get; set; } = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
@ -52,12 +52,20 @@ public class ConversationStateService : IConversationStateService, IDisposable
|
||||||
if (!ContainsState(name) || preValue != currentValue)
|
if (!ContainsState(name) || preValue != currentValue)
|
||||||
{
|
{
|
||||||
_logger.LogInformation($"[STATE] {name} = {value}");
|
_logger.LogInformation($"[STATE] {name} = {value}");
|
||||||
|
var routingCtx = _services.GetRequiredService<IRoutingContext>();
|
||||||
|
|
||||||
foreach (var hook in hooks)
|
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
|
var newPair = new StateKeyValue
|
||||||
{
|
{
|
||||||
Key = name,
|
Key = name,
|
||||||
|
|
|
||||||
|
|
@ -213,6 +213,12 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
|
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
|
#endregion
|
||||||
|
|
||||||
#region IRoutingHook
|
#region IRoutingHook
|
||||||
|
|
@ -379,4 +385,19 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
|
|
||||||
return JsonSerializer.Serialize(log, _options.JsonSerializerOptions);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue