This commit is contained in:
Haiping Chen 2024-03-28 14:10:07 -05:00
commit 25a4ec3949
8 changed files with 75 additions and 7 deletions

View file

@ -29,7 +29,7 @@ public abstract class ConversationHookBase : IConversationHook
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)

View file

@ -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);

View file

@ -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; }
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Loggers.Models;
public class StateChangeOutputModel : StateChangeModel
{
[JsonPropertyName("created_at")]
public DateTime CreateTime { get; set; } = DateTime.UtcNow;
}

View file

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

View file

@ -52,12 +52,20 @@ public class ConversationStateService : IConversationStateService, IDisposable
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,
@ -118,7 +126,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
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
@ -178,7 +186,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
value.Values.Add(new StateValue
{
Data = lastValue.Data,
MessageId = !string.IsNullOrEmpty(curMsgId) ? curMsgId : lastValue.MessageId,
MessageId = curMsgId,
Active = false,
ActiveRounds = lastValue.ActiveRounds,
UpdateTime = utcNow

View file

@ -407,6 +407,11 @@ namespace BotSharp.Core.Repository
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;

View file

@ -213,6 +213,12 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
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
@ -379,4 +385,19 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
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);
}
}