Skip to content

Commit bf3915c

Browse files
authored
Merge pull request #1152 from iceljc/master
reset message wrapper and fix path error
2 parents 9fb6d9f + a5157a6 commit bf3915c

File tree

4 files changed

+88
-16
lines changed

4 files changed

+88
-16
lines changed

src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.Audio.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,17 @@ public bool SaveSpeechFile(string conversationId, string fileName, BinaryData da
3131

3232
public BinaryData GetSpeechFile(string conversationId, string fileName)
3333
{
34+
if (string.IsNullOrWhiteSpace(conversationId) || string.IsNullOrWhiteSpace(fileName))
35+
{
36+
return BinaryData.Empty;
37+
}
38+
3439
var path = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, TEXT_TO_SPEECH_FOLDER, fileName);
3540
if (!File.Exists(path))
3641
{
3742
return BinaryData.Empty;
3843
}
44+
3945
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
4046
return BinaryData.FromStream(fs);
4147
}

src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.Conversation.cs

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,30 @@ public async Task<IEnumerable<MessageFileModel>> GetMessageFileScreenshotsAsync(
1919

2020
foreach (var messageId in messageIds)
2121
{
22+
if (string.IsNullOrWhiteSpace(messageId))
23+
{
24+
continue;
25+
}
26+
2227
var dir = Path.Combine(pathPrefix, messageId, FileSourceType.User);
23-
if (!ExistDirectory(dir)) continue;
28+
if (!ExistDirectory(dir))
29+
{
30+
continue;
31+
}
2432

2533
foreach (var subDir in Directory.GetDirectories(dir))
2634
{
2735
var file = Directory.GetFiles(subDir).FirstOrDefault();
28-
if (file == null) continue;
36+
if (file == null)
37+
{
38+
continue;
39+
}
2940

3041
var screenshots = await GetScreenshots(file, subDir, messageId, source);
31-
if (screenshots.IsNullOrEmpty()) continue;
42+
if (screenshots.IsNullOrEmpty())
43+
{
44+
continue;
45+
}
3246

3347
files.AddRange(screenshots);
3448
}
@@ -41,10 +55,18 @@ public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnu
4155
string source, IEnumerable<string>? contentTypes = null)
4256
{
4357
var files = new List<MessageFileModel>();
44-
if (string.IsNullOrWhiteSpace(conversationId) || messageIds.IsNullOrEmpty()) return files;
58+
if (string.IsNullOrWhiteSpace(conversationId) || messageIds.IsNullOrEmpty())
59+
{
60+
return files;
61+
}
4562

4663
foreach (var messageId in messageIds)
4764
{
65+
if (string.IsNullOrWhiteSpace(messageId))
66+
{
67+
continue;
68+
}
69+
4870
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId, source);
4971
if (!ExistDirectory(dir))
5072
{
@@ -85,6 +107,14 @@ public IEnumerable<MessageFileModel> GetMessageFiles(string conversationId, IEnu
85107

86108
public string GetMessageFile(string conversationId, string messageId, string source, string index, string fileName)
87109
{
110+
if (string.IsNullOrWhiteSpace(conversationId)
111+
|| string.IsNullOrWhiteSpace(messageId)
112+
|| string.IsNullOrWhiteSpace(source)
113+
|| string.IsNullOrWhiteSpace(index))
114+
{
115+
return string.Empty;
116+
}
117+
88118
var dir = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId, source, index);
89119
if (!ExistDirectory(dir))
90120
{
@@ -98,10 +128,18 @@ public string GetMessageFile(string conversationId, string messageId, string sou
98128
public IEnumerable<MessageFileModel> GetMessagesWithFile(string conversationId, IEnumerable<string> messageIds)
99129
{
100130
var foundMsgs = new List<MessageFileModel>();
101-
if (string.IsNullOrWhiteSpace(conversationId) || messageIds.IsNullOrEmpty()) return foundMsgs;
131+
if (string.IsNullOrWhiteSpace(conversationId) || messageIds.IsNullOrEmpty())
132+
{
133+
return foundMsgs;
134+
}
102135

103136
foreach (var messageId in messageIds)
104137
{
138+
if (string.IsNullOrWhiteSpace(messageId))
139+
{
140+
continue;
141+
}
142+
105143
var prefix = Path.Combine(_baseDir, CONVERSATION_FOLDER, conversationId, FILE_FOLDER, messageId);
106144
var userDir = Path.Combine(prefix, FileSourceType.User);
107145
if (ExistDirectory(userDir))
@@ -121,10 +159,19 @@ public IEnumerable<MessageFileModel> GetMessagesWithFile(string conversationId,
121159

122160
public bool SaveMessageFiles(string conversationId, string messageId, string source, List<FileDataModel> files)
123161
{
124-
if (files.IsNullOrEmpty()) return false;
162+
if (string.IsNullOrWhiteSpace(conversationId)
163+
|| string.IsNullOrWhiteSpace(messageId)
164+
|| string.IsNullOrWhiteSpace(source)
165+
|| files.IsNullOrEmpty())
166+
{
167+
return false;
168+
}
125169

126170
var dir = GetConversationFileDirectory(conversationId, messageId, createNewDir: true);
127-
if (!ExistDirectory(dir)) return false;
171+
if (!ExistDirectory(dir))
172+
{
173+
return false;
174+
}
128175

129176
for (int i = 0; i < files.Count; i++)
130177
{
@@ -164,7 +211,10 @@ public bool SaveMessageFiles(string conversationId, string messageId, string sou
164211

165212
public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messageIds, string targetMessageId, string? newMessageId = null)
166213
{
167-
if (string.IsNullOrEmpty(conversationId) || messageIds == null) return false;
214+
if (string.IsNullOrEmpty(conversationId) || messageIds == null)
215+
{
216+
return false;
217+
}
168218

169219
if (!string.IsNullOrEmpty(targetMessageId) && !string.IsNullOrEmpty(newMessageId))
170220
{
@@ -192,7 +242,10 @@ public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messag
192242
foreach (var messageId in messageIds)
193243
{
194244
var dir = GetConversationFileDirectory(conversationId, messageId);
195-
if (!ExistDirectory(dir)) continue;
245+
if (!ExistDirectory(dir))
246+
{
247+
continue;
248+
}
196249

197250
DeleteDirectory(dir);
198251
Thread.Sleep(100);
@@ -203,12 +256,18 @@ public bool DeleteMessageFiles(string conversationId, IEnumerable<string> messag
203256

204257
public bool DeleteConversationFiles(IEnumerable<string> conversationIds)
205258
{
206-
if (conversationIds.IsNullOrEmpty()) return false;
259+
if (conversationIds.IsNullOrEmpty())
260+
{
261+
return false;
262+
}
207263

208264
foreach (var conversationId in conversationIds)
209265
{
210266
var convDir = GetConversationDirectory(conversationId);
211-
if (!ExistDirectory(convDir)) continue;
267+
if (!ExistDirectory(convDir))
268+
{
269+
continue;
270+
}
212271

213272
DeleteDirectory(convDir);
214273
}
@@ -241,7 +300,10 @@ private string GetConversationFileDirectory(string? conversationId, string? mess
241300

242301
private IEnumerable<string> GetMessageIds(IEnumerable<RoleDialogModel> dialogs, int? offset = null)
243302
{
244-
if (dialogs.IsNullOrEmpty()) return Enumerable.Empty<string>();
303+
if (dialogs.IsNullOrEmpty())
304+
{
305+
return Enumerable.Empty<string>();
306+
}
245307

246308
if (offset.HasValue && offset < 1)
247309
{
@@ -264,13 +326,17 @@ private IEnumerable<string> GetMessageIds(IEnumerable<RoleDialogModel> dialogs,
264326
private async Task<IEnumerable<string>> ConvertPdfToImages(string pdfLoc, string imageLoc)
265327
{
266328
var converters = _services.GetServices<IPdf2ImageConverter>();
267-
if (converters.IsNullOrEmpty()) return Enumerable.Empty<string>();
329+
if (converters.IsNullOrEmpty())
330+
{
331+
return Enumerable.Empty<string>();
332+
}
268333

269334
var converter = GetPdf2ImageConverter();
270335
if (converter == null)
271336
{
272337
return Enumerable.Empty<string>();
273338
}
339+
274340
return await converter.ConvertPdfToImages(pdfLoc, imageLoc);
275341
}
276342

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public async Task<bool> InvokeAgent(
5959
message.Indication = response.Indication;
6060
message.CurrentAgentId = agent.Id;
6161
message.IsStreaming = response.IsStreaming;
62+
message.AdditionalMessageWrapper = null;
6263

6364
await InvokeFunction(message, dialogs, options);
6465
}
@@ -74,6 +75,7 @@ public async Task<bool> InvokeAgent(
7475
message = RoleDialogModel.From(message, role: AgentRole.Assistant, content: response.Content);
7576
message.CurrentAgentId = agent.Id;
7677
message.IsStreaming = response.IsStreaming;
78+
message.AdditionalMessageWrapper = null;
7779
dialogs.Add(message);
7880
Context.SetDialogs(dialogs);
7981
}

src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ public async Task<bool> Execute(RoleDialogModel message)
7676
SaveToDb = true,
7777
Messages = new List<RoleDialogModel>
7878
{
79-
new()
79+
new(AgentRole.Assistant, obj.ReportSummary)
8080
{
81-
Role = AgentRole.Assistant,
8281
MessageId = message.MessageId,
8382
CurrentAgentId = message.CurrentAgentId,
84-
Content = obj.ReportSummary,
8583
Indication = "Summarizing",
8684
FunctionName = message.FunctionName,
8785
FunctionArgs = message.FunctionArgs,

0 commit comments

Comments
 (0)