1
1
using BotSharp . Abstraction . Agents . Models ;
2
2
using BotSharp . Abstraction . Functions . Models ;
3
3
using BotSharp . Abstraction . Loggers ;
4
+ using BotSharp . Abstraction . Loggers . Enums ;
4
5
using BotSharp . Abstraction . Loggers . Models ;
5
6
using BotSharp . Abstraction . Repositories ;
6
- using BotSharp . Abstraction . Repositories . Filters ;
7
- using BotSharp . Abstraction . Routing . Settings ;
8
7
using Microsoft . AspNetCore . SignalR ;
8
+ using Serilog ;
9
9
10
10
namespace BotSharp . Plugin . ChatHub . Hooks ;
11
11
@@ -37,11 +37,13 @@ public StreamingLogHook(
37
37
AllowTrailingCommas = true
38
38
} ;
39
39
}
40
+
40
41
public override async Task OnMessageReceived ( RoleDialogModel message )
41
42
{
42
43
var conversationId = _state . GetConversationId ( ) ;
43
44
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 ) ) ;
45
47
}
46
48
47
49
public async Task BeforeGenerating ( Agent agent , List < RoleDialogModel > conversations )
@@ -62,7 +64,8 @@ public override async Task OnFunctionExecuted(RoleDialogModel message)
62
64
var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
63
65
var log = $ "[{ agent ? . Name } ]: { message . FunctionName } ({ message . FunctionArgs } ) => { message . Content } ";
64
66
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 ) ) ;
66
69
}
67
70
68
71
/// <summary>
@@ -78,30 +81,25 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
78
81
var agentService = _services . GetRequiredService < IAgentService > ( ) ;
79
82
var conversationId = _state . GetConversationId ( ) ;
80
83
var agent = await agentService . LoadAgent ( message . CurrentAgentId ) ;
84
+ var logSource = string . Empty ;
81
85
82
86
// Log routing output
83
87
try
84
88
{
85
89
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 ) ) ;
87
93
}
88
94
catch
89
95
{
90
96
// ignore
91
97
}
92
98
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 ) ) ;
105
103
}
106
104
107
105
/// <summary>
@@ -127,19 +125,21 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
127
125
log += $ "\r \n { richContent } ";
128
126
}
129
127
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 ) ) ;
131
130
}
132
131
}
133
132
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 )
135
134
{
136
135
var log = new ConversationContentLogModel
137
136
{
138
137
ConversationId = conversationId ,
139
138
MessageId = message . MessageId ,
140
139
Name = name ,
141
140
Role = message . Role ,
142
- Content = content ,
141
+ Content = logContent ,
142
+ Source = logSource ,
143
143
CreateTime = DateTime . UtcNow
144
144
} ;
145
145
0 commit comments