Skip to content

Commit 82da7e7

Browse files
authored
Merge pull request #324 from iceljc/features/add-function-name
add delete log
2 parents 888eaa8 + 61dd64b commit 82da7e7

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public interface IBotSharpRepository
5959
PagedItems<Conversation> GetConversations(ConversationFilter filter);
6060
void UpdateConversationTitle(string conversationId, string title);
6161
List<Conversation> GetLastConversations();
62-
bool TruncateConversation(string conversationId, string messageId);
62+
bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false);
6363
#endregion
6464

6565
#region Execution Log

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public partial class ConversationService : IConversationService
77
public async Task<bool> TruncateConversation(string conversationId, string messageId)
88
{
99
var db = _services.GetRequiredService<IBotSharpRepository>();
10-
var isSaved = db.TruncateConversation(conversationId, messageId);
10+
var isSaved = db.TruncateConversation(conversationId, messageId, true);
1111
return await Task.FromResult(isSaved);
1212
}
1313
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public void UpdateConversationStatus(string conversationId, string status)
217217
throw new NotImplementedException();
218218
}
219219

220-
public bool TruncateConversation(string conversationId, string messageId)
220+
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
221221
{
222222
throw new NotImplementedException();
223223
}

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BotSharp.Abstraction.Loggers.Models;
12
using BotSharp.Abstraction.Repositories.Filters;
23
using BotSharp.Abstraction.Repositories.Models;
34
using System.Globalization;
@@ -263,7 +264,7 @@ public List<Conversation> GetLastConversations()
263264
}
264265

265266

266-
public bool TruncateConversation(string conversationId, string messageId)
267+
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
267268
{
268269
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) return false;
269270

@@ -288,6 +289,12 @@ public bool TruncateConversation(string conversationId, string messageId)
288289
var states = CollectConversationStates(stateDir);
289290
isSaved = HandleTruncatedStates(stateDir, states, refTime);
290291

292+
// Remove logs
293+
if (cleanLog)
294+
{
295+
HandleTruncatedLogs(convDir, refTime);
296+
}
297+
291298
return isSaved;
292299
}
293300

@@ -392,6 +399,44 @@ private bool HandleTruncatedStates(string stateDir, List<StateKeyValue> states,
392399
return isSaved;
393400
}
394401

402+
private bool HandleTruncatedLogs(string convDir, DateTime refTime)
403+
{
404+
var contentLogDir = Path.Combine(convDir, "content_log");
405+
var stateLogDir = Path.Combine(convDir, "state_log");
406+
407+
if (Directory.Exists(contentLogDir))
408+
{
409+
foreach (var file in Directory.GetFiles(contentLogDir))
410+
{
411+
var text = File.ReadAllText(file);
412+
var log = JsonSerializer.Deserialize<ConversationContentLogModel>(text);
413+
if (log == null) continue;
414+
415+
if (log.CreateTime >= refTime)
416+
{
417+
File.Delete(file);
418+
}
419+
}
420+
}
421+
422+
if (Directory.Exists(stateLogDir))
423+
{
424+
foreach (var file in Directory.GetFiles(stateLogDir))
425+
{
426+
var text = File.ReadAllText(file);
427+
var log = JsonSerializer.Deserialize<ConversationStateLogModel>(text);
428+
if (log == null) continue;
429+
430+
if (log.CreateTime >= refTime)
431+
{
432+
File.Delete(file);
433+
}
434+
}
435+
}
436+
437+
return true;
438+
}
439+
395440
private bool SaveTruncatedDialogs(string dialogDir, List<DialogElement> dialogs)
396441
{
397442
if (string.IsNullOrEmpty(dialogDir) || dialogs == null) return false;

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,10 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS
8585

8686
var conversationId = _state.GetConversationId();
8787
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
88-
var logSource = string.Empty;
8988

9089
var log = tokenStats.Prompt;
91-
logSource = ContentLogSource.Prompt;
9290
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
93-
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
91+
BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.Prompt, message));
9492
}
9593

9694
/// <summary>

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using BotSharp.Abstraction.Repositories.Models;
44
using BotSharp.Plugin.MongoStorage.Collections;
55
using BotSharp.Plugin.MongoStorage.Models;
6+
using MongoDB.Driver;
67

78
namespace BotSharp.Plugin.MongoStorage.Repository;
89

@@ -273,7 +274,7 @@ public List<Conversation> GetLastConversations()
273274
}).ToList();
274275
}
275276

276-
public bool TruncateConversation(string conversationId, string messageId)
277+
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
277278
{
278279
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) return false;
279280

@@ -308,6 +309,28 @@ public bool TruncateConversation(string conversationId, string messageId)
308309
foundStates.States = truncatedStates;
309310
_dc.ConversationDialogs.ReplaceOne(dialogFilter, foundDialog);
310311
_dc.ConversationStates.ReplaceOne(stateFilter, foundStates);
312+
313+
// Remove logs
314+
if (cleanLog)
315+
{
316+
var contentLogBuilder = Builders<ConversationContentLogDocument>.Filter;
317+
var stateLogBuilder = Builders<ConversationStateLogDocument>.Filter;
318+
319+
var contentLogFilters = new List<FilterDefinition<ConversationContentLogDocument>>()
320+
{
321+
contentLogBuilder.Eq(x => x.ConversationId, conversationId),
322+
contentLogBuilder.Gte(x => x.CreateTime, refTime)
323+
};
324+
var stateLogFilters = new List<FilterDefinition<ConversationStateLogDocument>>()
325+
{
326+
stateLogBuilder.Eq(x => x.ConversationId, conversationId),
327+
stateLogBuilder.Gte(x => x.CreateTime, refTime)
328+
};
329+
330+
_dc.ContentLogs.DeleteMany(contentLogBuilder.And(contentLogFilters));
331+
_dc.StateLogs.DeleteMany(stateLogBuilder.And(stateLogFilters));
332+
}
333+
311334
return true;
312335
}
313336
}

0 commit comments

Comments
 (0)