55using BotSharp . Abstraction . Loggers . Models ;
66using BotSharp . Abstraction . Repositories ;
77using Microsoft . AspNetCore . SignalR ;
8- using Serilog ;
98
109namespace BotSharp . Plugin . ChatHub . Hooks ;
1110
@@ -17,24 +16,28 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook
1716 private readonly IHubContext < SignalRHub > _chatHub ;
1817 private readonly IConversationStateService _state ;
1918 private readonly IUserIdentity _user ;
19+ private readonly IAgentService _agentService ;
2020
2121 public StreamingLogHook (
2222 ConversationSetting convSettings ,
2323 IServiceProvider serivces ,
2424 IHubContext < SignalRHub > chatHub ,
2525 IConversationStateService state ,
26- IUserIdentity user )
26+ IUserIdentity user ,
27+ IAgentService agentService )
2728 {
2829 _convSettings = convSettings ;
2930 _services = serivces ;
3031 _chatHub = chatHub ;
3132 _state = state ;
3233 _user = user ;
34+ _agentService = agentService ;
3335 _serializerOptions = new JsonSerializerOptions
3436 {
3537 PropertyNameCaseInsensitive = true ,
3638 PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
37- AllowTrailingCommas = true
39+ AllowTrailingCommas = true ,
40+ WriteIndented = true
3841 } ;
3942 }
4043
@@ -46,12 +49,20 @@ await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerat
4649 BuildContentLog ( conversationId , _user . UserName , log , ContentLogSource . UserInput , message ) ) ;
4750 }
4851
52+ public override async Task OnConversationRouting ( FunctionCallFromLlm instruct , RoleDialogModel message )
53+ {
54+ var conversationId = _state . GetConversationId ( ) ;
55+ var agent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
56+ var log = JsonSerializer . Serialize ( instruct , _serializerOptions ) ;
57+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
58+ BuildContentLog ( conversationId , agent ? . Name , log , ContentLogSource . AgentResponse , message ) ) ;
59+ }
60+
4961 public override async Task OnConversationRedirected ( string toAgentId , RoleDialogModel message )
5062 {
51- var agentService = _services . GetRequiredService < IAgentService > ( ) ;
5263 var conversationId = _state . GetConversationId ( ) ;
53- var fromAgent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
54- var toAgent = await agentService . LoadAgent ( toAgentId ) ;
64+ var fromAgent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
65+ var toAgent = await _agentService . LoadAgent ( toAgentId ) ;
5566
5667 var log = $ "{ message . Content } \r \n =====\r \n REDIRECTED TO { toAgent . Name } ";
5768 await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
@@ -71,10 +82,9 @@ public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversati
7182
7283 public override async Task OnFunctionExecuted ( RoleDialogModel message )
7384 {
74- var agentService = _services . GetRequiredService < IAgentService > ( ) ;
7585 var conversationId = _state . GetConversationId ( ) ;
76- var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
77- var log = $ "[ { agent ? . Name } ]: { message . FunctionName } ({ message . FunctionArgs } ) => { message . Content } ";
86+ var agent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
87+ var log = $ "{ message . FunctionName } ({ message . FunctionArgs } )\r \n => { message . Content } ";
7888 log += $ "\r \n <== MessageId: { message . MessageId } ";
7989 await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
8090 BuildContentLog ( conversationId , agent ? . Name , log , ContentLogSource . FunctionCall , message ) ) ;
@@ -90,31 +100,14 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
90100 {
91101 if ( ! _convSettings . ShowVerboseLog ) return ;
92102
93- var agentService = _services . GetRequiredService < IAgentService > ( ) ;
94103 var conversationId = _state . GetConversationId ( ) ;
95- var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
104+ var agent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
96105 var logSource = string . Empty ;
97106
98107 var log = tokenStats . Prompt ;
99108 logSource = ContentLogSource . Prompt ;
100109 await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
101110 BuildContentLog ( conversationId , agent ? . Name , log , logSource , message ) ) ;
102-
103- // Log routing output
104- try
105- {
106- var inst = message . Content . JsonContent < FunctionCallFromLlm > ( ) ;
107- if ( ! string . IsNullOrEmpty ( inst . Function ) )
108- {
109- logSource = ContentLogSource . AgentResponse ;
110- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversationContentLogGenerated" ,
111- BuildContentLog ( conversationId , agent ? . Name , message . Content , logSource , message ) ) ;
112- }
113- }
114- catch
115- {
116- // ignore
117- }
118111 }
119112
120113 /// <summary>
@@ -125,14 +118,12 @@ await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerat
125118 public override async Task OnResponseGenerated ( RoleDialogModel message )
126119 {
127120 var conv = _services . GetRequiredService < IConversationService > ( ) ;
128- var state = _services . GetRequiredService < IConversationStateService > ( ) ;
129121
130- await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversateStateLogGenerated" , BuildStateLog ( conv . ConversationId , state . GetStates ( ) , message ) ) ;
122+ await _chatHub . Clients . User ( _user . Id ) . SendAsync ( "OnConversateStateLogGenerated" , BuildStateLog ( conv . ConversationId , _state . GetStates ( ) , message ) ) ;
131123
132124 if ( message . Role == AgentRole . Assistant )
133125 {
134- var agentService = _services . GetRequiredService < IAgentService > ( ) ;
135- var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
126+ var agent = await _agentService . LoadAgent ( message . CurrentAgentId ) ;
136127 var log = $ "{ message . Content } ";
137128 if ( message . RichContent != null && message . RichContent . Message . RichType != "text" )
138129 {
0 commit comments