11using BotSharp . Abstraction . Agents . Models ;
2+ using BotSharp . Abstraction . Conversations . Models ;
23using BotSharp . Abstraction . Functions . Models ;
34using BotSharp . Abstraction . Loggers ;
45using BotSharp . Abstraction . Loggers . Enums ;
@@ -49,28 +50,35 @@ public override async Task OnMessageReceived(RoleDialogModel message)
4950 {
5051 var conversationId = _state . GetConversationId ( ) ;
5152 var log = $ "{ message . Content } ";
52- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
53- BuildContentLog ( conversationId , _user . UserName , log , ContentLogSource . UserInput , message ) ) ;
53+
54+ var input = new ContentLogInput ( conversationId , message )
55+ {
56+ Name = _user . UserName ,
57+ Source = ContentLogSource . UserInput ,
58+ Log = log
59+ } ;
60+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
5461 }
5562
5663 public async Task BeforeGenerating ( Agent agent , List < RoleDialogModel > conversations )
5764 {
5865 if ( ! _convSettings . ShowVerboseLog ) return ;
59-
60- /*var _state = _services.GetRequiredService<IConversationStateService>();
61- var conversationId = _state.GetConversationId();
62- var dialog = conversations.Last();
63- var log = $"{dialog.Role}: {dialog.Content} [msg_id: {dialog.MessageId}] ==>";
64- await _chatHub.Clients.User(_user.Id).SendAsync("OnContentLogGenerated", BuildLog(conversationId, log));*/
6566 }
6667
6768 public override async Task OnFunctionExecuted ( RoleDialogModel message )
6869 {
6970 var conversationId = _state . GetConversationId ( ) ;
7071 var agent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
7172 var log = $ "{ message . FunctionName } ({ message . FunctionArgs } )\r \n => { message . Content } ";
72- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
73- BuildContentLog ( conversationId , agent ? . Name , log , ContentLogSource . FunctionCall , message ) ) ;
73+
74+ var input = new ContentLogInput ( conversationId , message )
75+ {
76+ Name = agent ? . Name ,
77+ AgentId = agent ? . Id ,
78+ Source = ContentLogSource . FunctionCall ,
79+ Log = log
80+ } ;
81+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
7482 }
7583
7684 /// <summary>
@@ -87,8 +95,15 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
8795 var agent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
8896
8997 var log = tokenStats . Prompt ;
90- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
91- BuildContentLog ( conversationId , agent ? . Name , log , ContentLogSource . Prompt , message ) ) ;
98+
99+ var input = new ContentLogInput ( conversationId , message )
100+ {
101+ Name = agent ? . Name ,
102+ AgentId = agent ? . Id ,
103+ Source = ContentLogSource . Prompt ,
104+ Log = log
105+ } ;
106+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
92107 }
93108
94109 /// <summary>
@@ -111,54 +126,16 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
111126 var richContent = JsonSerializer . Serialize ( message . RichContent , _serializerOptions ) ;
112127 log += $ "\r \n { richContent } ";
113128 }
114- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
115- BuildContentLog ( conv . ConversationId , agent ? . Name , log , ContentLogSource . AgentResponse , message ) ) ;
116- }
117- }
118-
119- private string BuildContentLog ( string conversationId , string ? name , string logContent , string logSource , RoleDialogModel message )
120- {
121- var log = new ConversationContentLogModel
122- {
123- ConversationId = conversationId ,
124- MessageId = message . MessageId ,
125- Name = name ,
126- Role = message . Role ,
127- Content = logContent ,
128- Source = logSource ,
129- CreateTime = DateTime . UtcNow
130- } ;
131-
132- var json = JsonSerializer . Serialize ( log , _serializerOptions ) ;
133-
134- var convSettings = _services . GetRequiredService < ConversationSetting > ( ) ;
135- if ( convSettings . EnableContentLog )
136- {
137- var db = _services . GetRequiredService < IBotSharpRepository > ( ) ;
138- db . SaveConversationContentLog ( log ) ;
139- }
140129
141- return json ;
142- }
143-
144- private string BuildStateLog ( string conversationId , Dictionary < string , string > states , RoleDialogModel message )
145- {
146- var log = new ConversationStateLogModel
147- {
148- ConversationId = conversationId ,
149- MessageId = message . MessageId ,
150- States = states ,
151- CreateTime = DateTime . UtcNow
152- } ;
153-
154- var convSettings = _services . GetRequiredService < ConversationSetting > ( ) ;
155- if ( convSettings . EnableStateLog )
156- {
157- var db = _services . GetRequiredService < IBotSharpRepository > ( ) ;
158- db . SaveConversationStateLog ( log ) ;
130+ var input = new ContentLogInput ( conv . ConversationId , message )
131+ {
132+ Name = agent ? . Name ,
133+ AgentId = agent ? . Id ,
134+ Source = ContentLogSource . AgentResponse ,
135+ Log = log
136+ } ;
137+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
159138 }
160-
161- return JsonSerializer . Serialize ( log , _serializerOptions ) ;
162139 }
163140
164141 #region IRoutingHook
@@ -169,11 +146,18 @@ public async Task OnAgentEnqueued(string agentId, string preAgentId, string? rea
169146 var preAgent = await _agentService . LoadAgent ( preAgentId ) ;
170147
171148 var log = $ "{ agent . Name } is enqueued{ ( reason != null ? $ " ({ reason } )" : "" ) } ";
172- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
173- BuildContentLog ( conversationId , "Router" , log , ContentLogSource . HardRule , new RoleDialogModel ( AgentRole . System , log )
174- {
175- MessageId = _routingCtx . MessageId
176- } ) ) ;
149+ var message = new RoleDialogModel ( AgentRole . System , log )
150+ {
151+ MessageId = _routingCtx . MessageId
152+ } ;
153+
154+ var input = new ContentLogInput ( conversationId , message )
155+ {
156+ Name = "Router" ,
157+ Source = ContentLogSource . HardRule ,
158+ Log = log
159+ } ;
160+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
177161 }
178162
179163 public async Task OnAgentDequeued ( string agentId , string currentAgentId , string ? reason = null )
@@ -183,11 +167,18 @@ public async Task OnAgentDequeued(string agentId, string currentAgentId, string?
183167 var currentAgent = await _agentService . LoadAgent ( currentAgentId ) ;
184168
185169 var log = $ "{ agent . Name } is dequeued{ ( reason != null ? $ " ({ reason } )" : "" ) } , current agent is { currentAgent ? . Name } ";
186- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
187- BuildContentLog ( conversationId , "Router" , log , ContentLogSource . HardRule , new RoleDialogModel ( AgentRole . System , log )
188- {
189- MessageId = _routingCtx . MessageId
190- } ) ) ;
170+ var message = new RoleDialogModel ( AgentRole . System , log )
171+ {
172+ MessageId = _routingCtx . MessageId
173+ } ;
174+
175+ var input = new ContentLogInput ( conversationId , message )
176+ {
177+ Name = "Router" ,
178+ Source = ContentLogSource . HardRule ,
179+ Log = log
180+ } ;
181+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
191182 }
192183
193184 public async Task OnAgentReplaced ( string fromAgentId , string toAgentId , string ? reason = null )
@@ -197,11 +188,18 @@ public async Task OnAgentReplaced(string fromAgentId, string toAgentId, string?
197188 var toAgent = await _agentService . LoadAgent ( toAgentId ) ;
198189
199190 var log = $ "{ fromAgent . Name } is replaced to { toAgent . Name } { ( reason != null ? $ " ({ reason } )" : "" ) } ";
200- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
201- BuildContentLog ( conversationId , "Router" , log , ContentLogSource . HardRule , new RoleDialogModel ( AgentRole . System , log )
202- {
203- MessageId = _routingCtx . MessageId
204- } ) ) ;
191+ var message = new RoleDialogModel ( AgentRole . System , log )
192+ {
193+ MessageId = _routingCtx . MessageId
194+ } ;
195+
196+ var input = new ContentLogInput ( conversationId , message )
197+ {
198+ Name = "Router" ,
199+ Source = ContentLogSource . HardRule ,
200+ Log = log
201+ } ;
202+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
205203 }
206204
207205 public async Task OnAgentQueueEmptied ( string agentId , string ? reason = null )
@@ -210,20 +208,101 @@ public async Task OnAgentQueueEmptied(string agentId, string? reason = null)
210208 var agent = await _agentService . LoadAgent ( agentId ) ;
211209
212210 var log = reason ?? "Agent queue is cleared" ;
213- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
214- BuildContentLog ( conversationId , "Router" , log , ContentLogSource . HardRule , new RoleDialogModel ( AgentRole . System , log )
215- {
216- MessageId = _routingCtx . MessageId
217- } ) ) ;
211+ var message = new RoleDialogModel ( AgentRole . System , log )
212+ {
213+ MessageId = _routingCtx . MessageId
214+ } ;
215+
216+ var input = new ContentLogInput ( conversationId , message )
217+ {
218+ Name = "Router" ,
219+ Source = ContentLogSource . HardRule ,
220+ Log = log
221+ } ;
222+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
218223 }
219224
220225 public async Task OnRoutingInstructionReceived ( FunctionCallFromLlm instruct , RoleDialogModel message )
221226 {
222227 var conversationId = _state . GetConversationId ( ) ;
223228 var agent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
224229 var log = JsonSerializer . Serialize ( instruct , _serializerOptions ) ;
225- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
226- BuildContentLog ( conversationId , agent . Name , log , ContentLogSource . AgentResponse , message ) ) ;
230+
231+ var input = new ContentLogInput ( conversationId , message )
232+ {
233+ Name = agent ? . Name ,
234+ AgentId = agent ? . Id ,
235+ Source = ContentLogSource . AgentResponse ,
236+ Log = log
237+ } ;
238+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" , BuildContentLog ( input ) ) ;
227239 }
228240 #endregion
241+
242+
243+ private string BuildContentLog ( ContentLogInput input )
244+ {
245+ var log = new ConversationContentLogModel
246+ {
247+ ConversationId = input . ConversationId ,
248+ MessageId = input . Message . MessageId ,
249+ Name = input . Name ,
250+ Role = input . Message . Role ,
251+ Content = input . Log ,
252+ Source = input . Source ,
253+ CreateTime = DateTime . UtcNow
254+ } ;
255+
256+ var json = JsonSerializer . Serialize ( log , _serializerOptions ) ;
257+
258+ var convSettings = _services . GetRequiredService < ConversationSetting > ( ) ;
259+ if ( convSettings . EnableContentLog )
260+ {
261+ var db = _services . GetRequiredService < IBotSharpRepository > ( ) ;
262+ db . SaveConversationContentLog ( log ) ;
263+ }
264+
265+ return json ;
266+ }
267+
268+ private string BuildStateLog ( string conversationId , Dictionary < string , string > states , RoleDialogModel message )
269+ {
270+ var log = new ConversationStateLogModel
271+ {
272+ ConversationId = conversationId ,
273+ MessageId = message . MessageId ,
274+ States = states ,
275+ CreateTime = DateTime . UtcNow
276+ } ;
277+
278+ var convSettings = _services . GetRequiredService < ConversationSetting > ( ) ;
279+ if ( convSettings . EnableStateLog )
280+ {
281+ var db = _services . GetRequiredService < IBotSharpRepository > ( ) ;
282+ db . SaveConversationStateLog ( log ) ;
283+ }
284+
285+ return JsonSerializer . Serialize ( log , _serializerOptions ) ;
286+ }
229287}
288+
289+ internal class ContentLogInput
290+ {
291+ public string ConversationId { get ; set ; }
292+ public string ? Name { get ; set ; }
293+ public string ? AgentId { get ; set ; }
294+ public string Log { get ; set ; }
295+ public string Source { get ; set ; }
296+ public RoleDialogModel Message { get ; set ; }
297+
298+ public ContentLogInput ( )
299+ {
300+
301+ }
302+
303+ public ContentLogInput ( string conversationId , RoleDialogModel message )
304+ {
305+ ConversationId = conversationId ;
306+ Message = message ;
307+ }
308+ }
0 commit comments