Skip to content

Commit 98ce9b8

Browse files
authored
Merge pull request #1332 from yileicn/master
feat(conversation): add ExcludeFromContext flag, drop record_only,notification message type
2 parents e96e370 + 1bba400 commit 98ce9b8

9 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/Infrastructure/BotSharp.Abstraction/Conversations/Enums/MessageTypeName.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ namespace BotSharp.Abstraction.Conversations.Enums;
33
public static class MessageTypeName
44
{
55
public const string Plain = "plain";
6-
/// <summary>
7-
/// Persisted for record/audit but excluded from default LLM dialog history.
8-
/// </summary>
9-
public const string RecordOnly = "record_only";
10-
public const string Notification = "notification";
116
public const string FunctionCall = "function";
127
public const string Audio = "audio";
138
public const string Error = "error";

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ public class DialogMetaData
121121
[JsonPropertyName("sender_id")]
122122
public string? SenderId { get; set; }
123123

124+
/// <summary>
125+
/// When true, message is persisted but omitted from default LLM dialog history and routing conversation text.
126+
/// </summary>
127+
[JsonPropertyName("exclude_from_context")]
128+
public bool ExcludeFromContext { get; set; }
129+
124130
[JsonPropertyName("create_at")]
125131
public DateTime CreatedTime { get; set; }
126132
}

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class RoleDialogModel : ITrackableMessage
1919
/// </summary>
2020
public string MessageType { get; set; } = MessageTypeName.Plain;
2121

22+
/// <summary>
23+
/// When true, message is stored but omitted from default LLM history and routing <c>[CONVERSATION]</c> (orthogonal to <see cref="MessageType"/>).
24+
/// </summary>
25+
public bool ExcludeFromContext { get; set; }
26+
2227
/// <summary>
2328
/// The message label
2429
/// </summary>
@@ -191,6 +196,7 @@ public static RoleDialogModel From(RoleDialogModel source,
191196
CurrentAgentId = source.CurrentAgentId,
192197
MessageId = source.MessageId,
193198
MessageType = source.MessageType,
199+
ExcludeFromContext = source.ExcludeFromContext,
194200
MessageLabel = source.MessageLabel,
195201
ToolCallId = source.ToolCallId,
196202
FunctionName = source.FunctionName,

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using BotSharp.Abstraction.Conversations.Enums;
21
using BotSharp.Abstraction.MLTasks;
32
using BotSharp.Abstraction.Models;
43
using BotSharp.Abstraction.Settings;
@@ -24,7 +23,6 @@ public async Task<string> GetConversationSummary(ConversationSummaryModel model)
2423

2524
if (dialogs.IsNullOrEmpty()) continue;
2625

27-
dialogs = dialogs.Where(x => x.MessageType != MessageTypeName.Notification).ToList();
2826
var content = GetConversationContent(dialogs);
2927
if (string.IsNullOrWhiteSpace(content)) continue;
3028

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ public async Task<List<RoleDialogModel>> GetDialogHistory(int lastCount = 100, b
169169
}
170170
else
171171
{
172-
var defaultMessageTypes = new List<string> { MessageTypeName.Plain, MessageTypeName.RecordOnly };
173-
dialogs = dialogs.Where(x => string.IsNullOrEmpty(x.MessageType) || defaultMessageTypes.Contains(x.MessageType)).ToList();
172+
dialogs = dialogs.Where(x => string.IsNullOrEmpty(x.MessageType) || x.MessageType.IsEqualTo(MessageTypeName.Plain)).ToList();
174173
}
175174

176175
if (fromBreakpoint)

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public async Task<List<RoleDialogModel>> GetDialogs(string conversationId, Conve
7575
CurrentAgentId = meta?.AgentId ?? string.Empty,
7676
MessageId = meta?.MessageId ?? string.Empty,
7777
MessageType = meta?.MessageType ?? string.Empty,
78+
ExcludeFromContext = meta?.ExcludeFromContext ?? false,
7879
MessageLabel = meta?.MessageLabel,
7980
CreatedAt = meta?.CreatedTime ?? default,
8081
SenderId = senderId,
@@ -115,6 +116,7 @@ public async Task<List<RoleDialogModel>> GetDialogs(string conversationId, Conve
115116
AgentId = dialog.CurrentAgentId,
116117
MessageId = dialog.MessageId,
117118
MessageType = dialog.MessageType,
119+
ExcludeFromContext = dialog.ExcludeFromContext,
118120
MessageLabel = dialog.MessageLabel,
119121
ToolCallId = dialog.ToolCallId,
120122
FunctionName = dialog.FunctionName,
@@ -143,6 +145,7 @@ public async Task<List<RoleDialogModel>> GetDialogs(string conversationId, Conve
143145
AgentId = dialog.CurrentAgentId,
144146
MessageId = dialog.MessageId,
145147
MessageType = dialog.MessageType,
148+
ExcludeFromContext = dialog.ExcludeFromContext,
146149
MessageLabel = dialog.MessageLabel,
147150
SenderId = dialog.SenderId,
148151
FunctionName = dialog.FunctionName,

src/Infrastructure/BotSharp.Core/Routing/RoutingService.GetConversationContent.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Conversations.Enums;
2-
31
namespace BotSharp.Core.Routing;
42

53
public partial class RoutingService
@@ -8,7 +6,7 @@ public async Task<string> GetConversationContent(List<RoleDialogModel> dialogs,
86
{
97
var agentService = _services.GetRequiredService<IAgentService>();
108
var conversation = "";
11-
var conversationDialogs = dialogs.Where(x => x.MessageType != MessageTypeName.RecordOnly).TakeLast(maxDialogCount).ToList();
9+
var conversationDialogs = dialogs.Where(x => !x.ExcludeFromContext).TakeLast(maxDialogCount).ToList();
1210
foreach (var dialog in conversationDialogs)
1311
{
1412
var role = dialog.Role;

src/Infrastructure/BotSharp.Core/Routing/RoutingService.InvokeAgent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task<bool> InvokeAgent(
3838

3939
RoleDialogModel response;
4040
var message = dialogs.Last();
41-
var conversationDialogs = dialogs.Where(x => x.MessageType != MessageTypeName.RecordOnly).ToList();
41+
var conversationDialogs = dialogs.Where(x => !x.ExcludeFromContext).ToList();
4242
if (options?.UseStream == true)
4343
{
4444
response = await chatCompletion.GetChatCompletionsStreamingAsync(agent, conversationDialogs);

src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class DialogMetaDataMongoElement
5252
public string? FunctionArgs { get; set; }
5353
public Dictionary<string, string?>? MetaData { get; set; }
5454
public string? SenderId { get; set; }
55+
public bool ExcludeFromContext { get; set; }
5556
public DateTime CreateTime { get; set; }
5657

5758
public static DialogMetaData ToDomainElement(DialogMetaDataMongoElement meta)
@@ -68,6 +69,7 @@ public static DialogMetaData ToDomainElement(DialogMetaDataMongoElement meta)
6869
FunctionArgs = meta.FunctionArgs,
6970
MetaData = meta.MetaData,
7071
SenderId = meta.SenderId,
72+
ExcludeFromContext = meta.ExcludeFromContext,
7173
CreatedTime = meta.CreateTime
7274
};
7375
}
@@ -86,6 +88,7 @@ public static DialogMetaDataMongoElement ToMongoElement(DialogMetaData meta)
8688
FunctionArgs = meta.FunctionArgs,
8789
MetaData = meta.MetaData,
8890
SenderId = meta.SenderId,
91+
ExcludeFromContext = meta.ExcludeFromContext,
8992
CreateTime = meta.CreatedTime
9093
};
9194
}

0 commit comments

Comments
 (0)