Skip to content

Commit b6049fd

Browse files
author
Jicheng Lu
committed
structure content log
1 parent 6e8afba commit b6049fd

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace BotSharp.Abstraction.Loggers.Models;
2+
3+
public class StreamingLogModel
4+
{
5+
[JsonPropertyName("conversation_id")]
6+
public string ConversationId { get; set; }
7+
8+
[JsonPropertyName("content")]
9+
public string Content { get; set; }
10+
11+
[JsonPropertyName("created_at")]
12+
public DateTime CreateTime { get; set; }
13+
}
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Loggers;
3-
using BotSharp.Abstraction.Users.Models;
3+
using BotSharp.Abstraction.Loggers.Models;
44
using Microsoft.AspNetCore.SignalR;
55

66
namespace 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

Comments
 (0)