Skip to content

Commit 0d23f06

Browse files
author
Jicheng Lu
committed
save rich content
1 parent 3bf9081 commit 0d23f06

File tree

5 files changed

+38
-20
lines changed

5 files changed

+38
-20
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.Loggers.Enums;
2+
3+
public static class ContentLogSource
4+
{
5+
public const string UserInput = "user input";
6+
public const string Prompt = "prompt";
7+
public const string FunctionCall = "function call";
8+
public const string AgentResponse = "agent response";
9+
}

src/Infrastructure/BotSharp.Abstraction/Loggers/Models/ConversationContentLogModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ public class ConversationContentLogModel
44
{
55
[JsonPropertyName("conversation_id")]
66
public string ConversationId { get; set; }
7+
78
[JsonPropertyName("message_id")]
89
public string MessageId { get; set; }
10+
911
[JsonPropertyName("name")]
1012
public string? Name { get; set; }
13+
1114
[JsonPropertyName("role")]
1215
public string Role { get; set; }
1316

17+
[JsonPropertyName("source")]
18+
public string Source { get; set; }
19+
1420
[JsonPropertyName("content")]
1521
public string Content { get; set; }
1622

src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Functions.Models;
33
using BotSharp.Abstraction.Loggers;
4+
using BotSharp.Abstraction.Loggers.Enums;
45
using BotSharp.Abstraction.Loggers.Models;
56
using BotSharp.Abstraction.Repositories;
6-
using BotSharp.Abstraction.Repositories.Filters;
7-
using BotSharp.Abstraction.Routing.Settings;
87
using Microsoft.AspNetCore.SignalR;
8+
using Serilog;
99

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

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationContentLogDocument.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class ConversationContentLogDocument : MongoBase
66
public string MessageId { get; set; }
77
public string? Name { get; set; }
88
public string Role { get; set; }
9+
public string Source { get; set; }
910
public string Content { get; set; }
1011
public DateTime CreateTime { get; set; }
1112
}

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public void SaveConversationContentLog(ConversationContentLogModel log)
7373
MessageId = messageId,
7474
Name = log.Name,
7575
Role = log.Role,
76+
Source = log.Source,
7677
Content = log.Content,
7778
CreateTime = log.CreateTime
7879
};
@@ -91,6 +92,7 @@ public List<ConversationContentLogModel> GetConversationContentLogs(string conve
9192
MessageId = x.MessageId,
9293
Name = x.Name,
9394
Role = x.Role,
95+
Source = x.Source,
9496
Content = x.Content,
9597
CreateTime = x.CreateTime
9698
})

0 commit comments

Comments
 (0)