11using BotSharp . Abstraction . Agents . Models ;
22using BotSharp . Abstraction . Functions . Models ;
33using BotSharp . Abstraction . Loggers ;
4+ using BotSharp . Abstraction . Loggers . Enums ;
45using BotSharp . Abstraction . Loggers . Models ;
56using BotSharp . Abstraction . Repositories ;
6- using BotSharp . Abstraction . Repositories . Filters ;
7- using BotSharp . Abstraction . Routing . Settings ;
87using Microsoft . AspNetCore . SignalR ;
8+ using Serilog ;
99
1010namespace BotSharp . Plugin . ChatHub . Hooks ;
1111
@@ -37,11 +37,13 @@ public StreamingLogHook(
3737 AllowTrailingCommas = true
3838 } ;
3939 }
40+
4041 public override async Task OnMessageReceived ( RoleDialogModel message )
4142 {
4243 var conversationId = _state . GetConversationId ( ) ;
4344 var log = $ "MessageId: { message . MessageId } ==>\r \n { message . Role } : { message . Content } ";
44- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( conversationId , _user . UserName , log , message ) ) ;
45+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
46+ BuildContentLog ( conversationId , _user . UserName , log , ContentLogSource . UserInput , message ) ) ;
4547 }
4648
4749 public async Task BeforeGenerating ( Agent agent , List < RoleDialogModel > conversations )
@@ -62,7 +64,8 @@ public override async Task OnFunctionExecuted(RoleDialogModel message)
6264 var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
6365 var log = $ "[{ agent ? . Name } ]: { message . FunctionName } ({ message . FunctionArgs } ) => { message . Content } ";
6466 log += $ "\r \n <== MessageId: { message . MessageId } ";
65- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( conversationId , agent ? . Name , log , message ) ) ;
67+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
68+ BuildContentLog ( conversationId , agent ? . Name , log , ContentLogSource . FunctionCall , message ) ) ;
6669 }
6770
6871 /// <summary>
@@ -78,30 +81,25 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
7881 var agentService = _services . GetRequiredService < IAgentService > ( ) ;
7982 var conversationId = _state . GetConversationId ( ) ;
8083 var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
84+ var logSource = string . Empty ;
8185
8286 // Log routing output
8387 try
8488 {
8589 var inst = message . Content . JsonContent < FunctionCallFromLlm > ( ) ;
86- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( conversationId , agent ? . Name , message . Content , message ) ) ;
90+ logSource = ContentLogSource . AgentResponse ;
91+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
92+ BuildContentLog ( conversationId , agent ? . Name , message . Content , logSource , message ) ) ;
8793 }
8894 catch
8995 {
9096 // ignore
9197 }
9298
93- string log ;
94- if ( message . Role == AgentRole . Function )
95- {
96- log = $ "[{ agent ? . Name } ]: { message . FunctionName } ({ message . FunctionArgs } ) => { message . Content } ";
97- log += $ "\r \n <== MessageId: { message . MessageId } ";
98- }
99- else
100- {
101- log = tokenStats . Prompt ;
102- }
103-
104- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( conversationId , agent ? . Name , log , message ) ) ;
99+ var log = tokenStats . Prompt ;
100+ logSource = ContentLogSource . Prompt ;
101+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
102+ BuildContentLog ( conversationId , agent ? . Name , log , logSource , message ) ) ;
105103 }
106104
107105 /// <summary>
@@ -127,19 +125,21 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
127125 log += $ "\r \n { richContent } ";
128126 }
129127 log += $ "\r \n <== MessageId: { message . MessageId } ";
130- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( conv . ConversationId , agent ? . Name , log , message ) ) ;
128+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
129+ BuildContentLog ( conv . ConversationId , agent ? . Name , log , ContentLogSource . AgentResponse , message ) ) ;
131130 }
132131 }
133132
134- private string BuildContentLog ( string conversationId , string ? name , string content , RoleDialogModel message )
133+ private string BuildContentLog ( string conversationId , string ? name , string logContent , string logSource , RoleDialogModel message )
135134 {
136135 var log = new ConversationContentLogModel
137136 {
138137 ConversationId = conversationId ,
139138 MessageId = message . MessageId ,
140139 Name = name ,
141140 Role = message . Role ,
142- Content = content ,
141+ Content = logContent ,
142+ Source = logSource ,
143143 CreateTime = DateTime . UtcNow
144144 } ;
145145
0 commit comments