Skip to content

Commit 5473713

Browse files
authored
Merge pull request #825 from iceljc/master
add conv append
2 parents 798bcc8 + 6e9f2c9 commit 5473713

File tree

6 files changed

+114
-4
lines changed

6 files changed

+114
-4
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStorage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ namespace BotSharp.Abstraction.Conversations;
33
public interface IConversationStorage
44
{
55
void Append(string conversationId, RoleDialogModel dialog);
6+
void Append(string conversationId, IEnumerable<RoleDialogModel> dialogs);
67
List<RoleDialogModel> GetDialogs(string conversationId);
78
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
9090
void UpdateConversationTitle(string conversationId, string title);
9191
void UpdateConversationTitleAlias(string conversationId, string titleAlias);
9292
bool UpdateConversationTags(string conversationId, List<string> tags);
93+
bool AppendConversationTags(string conversationId, List<string> tags);
9394
bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request);
9495
void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint);
9596
ConversationBreakpoint? GetConversationBreakpoint(string conversationId);

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

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
using BotSharp.Abstraction.Messaging;
22
using BotSharp.Abstraction.Messaging.Models.RichContent;
33
using BotSharp.Abstraction.Options;
4-
using System.IO;
54

65
namespace BotSharp.Core.Conversations.Services;
76

87
public class ConversationStorage : IConversationStorage
98
{
10-
private readonly BotSharpDatabaseSettings _dbSettings;
119
private readonly BotSharpOptions _options;
1210
private readonly IServiceProvider _services;
1311

1412
public ConversationStorage(
15-
BotSharpDatabaseSettings dbSettings,
1613
BotSharpOptions options,
1714
IServiceProvider services)
1815
{
19-
_dbSettings = dbSettings;
2016
_services = services;
2117
_options = options;
2218
}
@@ -94,6 +90,76 @@ public void Append(string conversationId, RoleDialogModel dialog)
9490
db.AppendConversationDialogs(conversationId, dialogElements);
9591
}
9692

93+
public void Append(string conversationId, IEnumerable<RoleDialogModel> dialogs)
94+
{
95+
if (dialogs.IsNullOrEmpty()) return;
96+
97+
var db = _services.GetRequiredService<IBotSharpRepository>();
98+
var dialogElements = new List<DialogElement>();
99+
100+
foreach ( var dialog in dialogs)
101+
{
102+
if (dialog.Role == AgentRole.Function)
103+
{
104+
var meta = new DialogMetaData
105+
{
106+
Role = dialog.Role,
107+
AgentId = dialog.CurrentAgentId,
108+
MessageId = dialog.MessageId,
109+
MessageType = dialog.MessageType,
110+
FunctionName = dialog.FunctionName,
111+
CreateTime = dialog.CreatedAt
112+
};
113+
114+
var content = dialog.Content.RemoveNewLine();
115+
if (string.IsNullOrEmpty(content))
116+
{
117+
continue;
118+
}
119+
dialogElements.Add(new DialogElement
120+
{
121+
MetaData = meta,
122+
Content = dialog.Content,
123+
SecondaryContent = dialog.SecondaryContent,
124+
Payload = dialog.Payload
125+
});
126+
}
127+
else
128+
{
129+
var meta = new DialogMetaData
130+
{
131+
Role = dialog.Role,
132+
AgentId = dialog.CurrentAgentId,
133+
MessageId = dialog.MessageId,
134+
MessageType = dialog.MessageType,
135+
SenderId = dialog.SenderId,
136+
FunctionName = dialog.FunctionName,
137+
CreateTime = dialog.CreatedAt
138+
};
139+
140+
var content = dialog.Content.RemoveNewLine();
141+
if (string.IsNullOrEmpty(content))
142+
{
143+
continue;
144+
}
145+
146+
var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _options.JsonSerializerOptions) : null;
147+
var secondaryRichContent = dialog.SecondaryRichContent != null ? JsonSerializer.Serialize(dialog.SecondaryRichContent, _options.JsonSerializerOptions) : null;
148+
dialogElements.Add(new DialogElement
149+
{
150+
MetaData = meta,
151+
Content = dialog.Content,
152+
SecondaryContent = dialog.SecondaryContent,
153+
RichContent = richContent,
154+
SecondaryRichContent = secondaryRichContent,
155+
Payload = dialog.Payload
156+
});
157+
}
158+
}
159+
160+
db.AppendConversationDialogs(conversationId, dialogElements);
161+
}
162+
97163
public List<RoleDialogModel> GetDialogs(string conversationId)
98164
{
99165
var db = _services.GetRequiredService<IBotSharpRepository>();

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public void UpdateConversationTitleAlias(string conversationId, string titleAlia
111111
public bool UpdateConversationTags(string conversationId, List<string> tags)
112112
=> throw new NotImplementedException();
113113

114+
public bool AppendConversationTags(string conversationId, List<string> tags)
115+
=> throw new NotImplementedException();
116+
114117
public bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
115118
=> throw new NotImplementedException();
116119

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,27 @@ public bool UpdateConversationTags(string conversationId, List<string> tags)
169169
return true;
170170
}
171171

172+
public bool AppendConversationTags(string conversationId, List<string> tags)
173+
{
174+
if (string.IsNullOrEmpty(conversationId) || tags.IsNullOrEmpty()) return false;
175+
176+
var convDir = FindConversationDirectory(conversationId);
177+
if (string.IsNullOrEmpty(convDir)) return false;
178+
179+
var convFile = Path.Combine(convDir, CONVERSATION_FILE);
180+
if (!File.Exists(convFile)) return false;
181+
182+
var json = File.ReadAllText(convFile);
183+
var conv = JsonSerializer.Deserialize<Conversation>(json, _options);
184+
185+
var curTags = conv.Tags ?? new();
186+
var newTags = curTags.Concat(tags).Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
187+
conv.Tags = newTags;
188+
conv.UpdatedTime = DateTime.UtcNow;
189+
File.WriteAllText(convFile, JsonSerializer.Serialize(conv, _options));
190+
return true;
191+
}
192+
172193
public bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
173194
{
174195
if (string.IsNullOrEmpty(conversationId)) return false;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ public bool UpdateConversationTags(string conversationId, List<string> tags)
139139
return res.ModifiedCount > 0;
140140
}
141141

142+
public bool AppendConversationTags(string conversationId, List<string> tags)
143+
{
144+
if (string.IsNullOrEmpty(conversationId) || tags.IsNullOrEmpty()) return false;
145+
146+
var filter = Builders<ConversationDocument>.Filter.Eq(x => x.Id, conversationId);
147+
var conv = _dc.Conversations.Find(filter).FirstOrDefault();
148+
if (conv == null) return false;
149+
150+
var curTags = conv.Tags ?? new();
151+
var newTags = curTags.Concat(tags).Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
152+
var update = Builders<ConversationDocument>.Update
153+
.Set(x => x.Tags, newTags)
154+
.Set(x => x.UpdatedTime, DateTime.UtcNow);
155+
156+
var res = _dc.Conversations.UpdateOne(filter, update);
157+
return res.ModifiedCount > 0;
158+
}
159+
142160
public bool UpdateConversationMessage(string conversationId, UpdateMessageRequest request)
143161
{
144162
if (string.IsNullOrEmpty(conversationId)) return false;

0 commit comments

Comments
 (0)