Skip to content

Commit bd7d3ff

Browse files
author
Jicheng Lu
committed
add state change event
1 parent 78ef7b6 commit bd7d3ff

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationHookBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public IConversationHook SetConversation(Conversation conversation)
2929
public virtual Task OnStateLoaded(ConversationState state)
3030
=> Task.CompletedTask;
3131

32-
public virtual Task OnStateChanged(string name, string preValue, string currentValue)
32+
public virtual Task OnStateChanged(StateChangeModel stateChange)
3333
=> Task.CompletedTask;
3434

3535
public virtual Task OnDialogRecordLoaded(RoleDialogModel dialog)

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public interface IConversationHook
4343
Task OnDialogRecordLoaded(RoleDialogModel dialog);
4444

4545
Task OnStateLoaded(ConversationState state);
46-
Task OnStateChanged(string name, string preValue, string currentValue);
46+
Task OnStateChanged(StateChangeModel stateChange);
4747

4848
Task OnMessageReceived(RoleDialogModel message);
4949
Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace BotSharp.Abstraction.Conversations.Models;
2+
3+
public class StateChangeModel
4+
{
5+
[JsonPropertyName("conversation_id")]
6+
public string ConversationId { get; set; }
7+
8+
[JsonPropertyName("message_id")]
9+
public string MessageId { get; set; }
10+
11+
[JsonPropertyName("name")]
12+
public string Name { get; set; }
13+
14+
[JsonPropertyName("before_value")]
15+
public string BeforeValue { get; set; }
16+
17+
[JsonPropertyName("after_value")]
18+
public string AfterValue { get; set; }
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace BotSharp.Abstraction.Loggers.Models;
2+
3+
public class StateChangeOutputModel : StateChangeModel
4+
{
5+
[JsonPropertyName("created_at")]
6+
public DateTime CreateTime { get; set; } = DateTime.UtcNow;
7+
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,20 @@ public IConversationStateService SetState<T>(string name, T value, bool isNeedVe
5252
if (!ContainsState(name) || preValue != currentValue)
5353
{
5454
_logger.LogInformation($"[STATE] {name} = {value}");
55+
var routingCtx = _services.GetRequiredService<IRoutingContext>();
56+
5557
foreach (var hook in hooks)
5658
{
57-
hook.OnStateChanged(name, preValue, currentValue).Wait();
59+
hook.OnStateChanged(new StateChangeModel
60+
{
61+
ConversationId = _conversationId,
62+
MessageId = routingCtx.MessageId,
63+
Name = name,
64+
BeforeValue = preValue,
65+
AfterValue = currentValue
66+
}).Wait();
5867
}
59-
60-
var routingCtx = _services.GetRequiredService<IRoutingContext>();
68+
6169
var newPair = new StateKeyValue
6270
{
6371
Key = name,

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ public override async Task OnBreakpointUpdated(string conversationId, bool reset
213213
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated", BuildContentLog(input));
214214
}
215215

216+
public override async Task OnStateChanged(StateChangeModel stateChange)
217+
{
218+
if (stateChange == null) return;
219+
220+
await _chatHub.Clients.User(_user.Id).SendAsync("OnStateChangeGenerated", BuildStateChangeLog(stateChange));
221+
}
216222
#endregion
217223

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

380386
return JsonSerializer.Serialize(log, _options.JsonSerializerOptions);
381387
}
388+
389+
private string BuildStateChangeLog(StateChangeModel stateChange)
390+
{
391+
var log = new StateChangeOutputModel
392+
{
393+
ConversationId = stateChange.ConversationId,
394+
MessageId = stateChange.MessageId,
395+
Name = stateChange.Name,
396+
BeforeValue = stateChange.BeforeValue,
397+
AfterValue = stateChange.AfterValue,
398+
CreateTime = DateTime.UtcNow
399+
};
400+
401+
return JsonSerializer.Serialize(log, _options.JsonSerializerOptions);
402+
}
382403
}

0 commit comments

Comments
 (0)