Skip to content

Commit 759f1f7

Browse files
authored
Merge pull request #371 from iceljc/features/add-state-change-log
Features/add state change log
2 parents 714cbae + bd7d3ff commit 759f1f7

File tree

8 files changed

+75
-7
lines changed

8 files changed

+75
-7
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.Abstraction/Messaging/MessageParser.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public MessageParser()
4545
{
4646
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
4747
}
48+
else if (elementType == typeof(ButtonElement).Name)
49+
{
50+
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
51+
}
4852
}
4953
}
5054

@@ -80,6 +84,10 @@ public MessageParser()
8084
{
8185
res = JsonSerializer.Deserialize<GenericTemplateMessage<GenericElement>>(jsonText, options);
8286
}
87+
else if (elementType == typeof(ButtonElement).Name)
88+
{
89+
res = JsonSerializer.Deserialize<GenericTemplateMessage<ButtonElement>>(jsonText, options);
90+
}
8391
}
8492
}
8593

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

Lines changed: 13 additions & 5 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,
@@ -118,7 +126,7 @@ public Dictionary<string, string> Load(string conversationId)
118126
state.Value.Values.Add(new StateValue
119127
{
120128
Data = value.Data,
121-
MessageId = !string.IsNullOrEmpty(curMsgId) ? curMsgId : value.MessageId,
129+
MessageId = curMsgId,
122130
Active = false,
123131
ActiveRounds = value.ActiveRounds,
124132
UpdateTime = DateTime.UtcNow
@@ -178,7 +186,7 @@ public void CleanStates()
178186
value.Values.Add(new StateValue
179187
{
180188
Data = lastValue.Data,
181-
MessageId = !string.IsNullOrEmpty(curMsgId) ? curMsgId : lastValue.MessageId,
189+
MessageId = curMsgId,
182190
Active = false,
183191
ActiveRounds = lastValue.ActiveRounds,
184192
UpdateTime = utcNow

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ public List<string> GetIdleConversations(int batchSize, int messageLimit, int bu
407407
var utcNow = DateTime.UtcNow;
408408
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
409409

410+
if (!Directory.Exists(dir))
411+
{
412+
Directory.CreateDirectory(dir);
413+
}
414+
410415
if (batchSize <= 0 || batchSize > batchLimit)
411416
{
412417
batchSize = batchLimit;

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)