11using BotSharp . Abstraction . Agents . Models ;
22using BotSharp . Abstraction . Loggers ;
3- using BotSharp . Abstraction . Users . Models ;
3+ using BotSharp . Abstraction . Loggers . Models ;
44using Microsoft . AspNetCore . SignalR ;
55
66namespace BotSharp . Plugin . ChatHub . Hooks ;
@@ -10,6 +10,7 @@ public class StreamingLogHook : IContentGeneratingHook
1010 private readonly ConversationSetting _convSettings ;
1111 private readonly IServiceProvider _services ;
1212 private readonly IHubContext < SignalRHub > _chatHub ;
13+ private readonly JsonSerializerOptions _serializerOptions ;
1314
1415 public StreamingLogHook (
1516 ConversationSetting convSettings ,
@@ -19,31 +20,52 @@ public StreamingLogHook(
1920 _convSettings = convSettings ;
2021 _services = serivces ;
2122 _chatHub = chatHub ;
23+ _serializerOptions = new JsonSerializerOptions
24+ {
25+ PropertyNameCaseInsensitive = true ,
26+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
27+ AllowTrailingCommas = true
28+ } ;
2229 }
2330
2431 public async Task BeforeGenerating ( Agent agent , List < RoleDialogModel > conversations )
2532 {
2633 if ( ! _convSettings . ShowVerboseLog ) return ;
2734
2835 var user = _services . GetRequiredService < IUserIdentity > ( ) ;
36+ var states = _services . GetRequiredService < IConversationStateService > ( ) ;
37+ var conversationId = states . GetConversationId ( ) ;
2938 var dialog = conversations . Last ( ) ;
3039 var log = $ "{ dialog . Role } : { dialog . Content } [msg_id: { dialog . MessageId } ] ==>";
31- await _chatHub . Clients . User ( user . Id ) . SendAsync ( "OnContentLogGenerated" , log ) ;
40+ await _chatHub . Clients . User ( user . Id ) . SendAsync ( "OnContentLogGenerated" , BuildLog ( conversationId , log ) ) ;
3241 }
3342
3443 public async Task AfterGenerated ( RoleDialogModel message , TokenStatsModel tokenStats )
3544 {
3645 if ( ! _convSettings . ShowVerboseLog ) return ;
3746
3847 var agentService = _services . GetRequiredService < IAgentService > ( ) ;
48+ var states = _services . GetRequiredService < IConversationStateService > ( ) ;
49+ var conversationId = states . GetConversationId ( ) ;
3950 var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
4051
4152 var log = message . Role == AgentRole . Function ?
4253 $ "[{ agent ? . Name } ]: { message . FunctionName } ({ message . FunctionArgs } )" :
4354 $ "[{ agent ? . Name } ]: { message . Content } " + $ " <== [msg_id: { message . MessageId } ]";
4455
4556 var user = _services . GetRequiredService < IUserIdentity > ( ) ;
46- await _chatHub . Clients . User ( user . Id ) . SendAsync ( "OnContentLogGenerated" , tokenStats . Prompt ) ;
47- await _chatHub . Clients . User ( user . Id ) . SendAsync ( "OnContentLogGenerated" , log ) ;
57+ await _chatHub . Clients . User ( user . Id ) . SendAsync ( "OnContentLogGenerated" , BuildLog ( conversationId , tokenStats . Prompt ) ) ;
58+ await _chatHub . Clients . User ( user . Id ) . SendAsync ( "OnContentLogGenerated" , BuildLog ( conversationId , log ) ) ;
59+ }
60+
61+ private string BuildLog ( string conversationId , string content )
62+ {
63+ var log = new StreamingLogModel
64+ {
65+ ConversationId = conversationId ,
66+ Content = content ,
67+ CreateTime = DateTime . UtcNow
68+ } ;
69+ return JsonSerializer . Serialize ( log , _serializerOptions ) ;
4870 }
4971}
0 commit comments