Merge pull request #324 from iceljc/features/add-function-name

add delete log
This commit is contained in:
C. Oceania 2024-02-28 17:43:14 -06:00 committed by GitHub
commit 82da7e7067
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 74 additions and 8 deletions

View file

@ -59,7 +59,7 @@ public interface IBotSharpRepository
PagedItems<Conversation> GetConversations(ConversationFilter filter);
void UpdateConversationTitle(string conversationId, string title);
List<Conversation> GetLastConversations();
bool TruncateConversation(string conversationId, string messageId);
bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false);
#endregion
#region Execution Log

View file

@ -7,7 +7,7 @@ public partial class ConversationService : IConversationService
public async Task<bool> TruncateConversation(string conversationId, string messageId)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var isSaved = db.TruncateConversation(conversationId, messageId);
var isSaved = db.TruncateConversation(conversationId, messageId, true);
return await Task.FromResult(isSaved);
}
}

View file

@ -217,7 +217,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
throw new NotImplementedException();
}
public bool TruncateConversation(string conversationId, string messageId)
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
{
throw new NotImplementedException();
}

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using System.Globalization;
@ -263,7 +264,7 @@ namespace BotSharp.Core.Repository
}
public bool TruncateConversation(string conversationId, string messageId)
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
{
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) return false;
@ -288,6 +289,12 @@ namespace BotSharp.Core.Repository
var states = CollectConversationStates(stateDir);
isSaved = HandleTruncatedStates(stateDir, states, refTime);
// Remove logs
if (cleanLog)
{
HandleTruncatedLogs(convDir, refTime);
}
return isSaved;
}
@ -392,6 +399,44 @@ namespace BotSharp.Core.Repository
return isSaved;
}
private bool HandleTruncatedLogs(string convDir, DateTime refTime)
{
var contentLogDir = Path.Combine(convDir, "content_log");
var stateLogDir = Path.Combine(convDir, "state_log");
if (Directory.Exists(contentLogDir))
{
foreach (var file in Directory.GetFiles(contentLogDir))
{
var text = File.ReadAllText(file);
var log = JsonSerializer.Deserialize<ConversationContentLogModel>(text);
if (log == null) continue;
if (log.CreateTime >= refTime)
{
File.Delete(file);
}
}
}
if (Directory.Exists(stateLogDir))
{
foreach (var file in Directory.GetFiles(stateLogDir))
{
var text = File.ReadAllText(file);
var log = JsonSerializer.Deserialize<ConversationStateLogModel>(text);
if (log == null) continue;
if (log.CreateTime >= refTime)
{
File.Delete(file);
}
}
}
return true;
}
private bool SaveTruncatedDialogs(string dialogDir, List<DialogElement> dialogs)
{
if (string.IsNullOrEmpty(dialogDir) || dialogs == null) return false;

View file

@ -85,12 +85,10 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var conversationId = _state.GetConversationId();
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
var logSource = string.Empty;
var log = tokenStats.Prompt;
logSource = ContentLogSource.Prompt;
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.Prompt, message));
}
/// <summary>

View file

@ -3,6 +3,7 @@ using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Plugin.MongoStorage.Collections;
using BotSharp.Plugin.MongoStorage.Models;
using MongoDB.Driver;
namespace BotSharp.Plugin.MongoStorage.Repository;
@ -273,7 +274,7 @@ public partial class MongoRepository
}).ToList();
}
public bool TruncateConversation(string conversationId, string messageId)
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
{
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) return false;
@ -308,6 +309,28 @@ public partial class MongoRepository
foundStates.States = truncatedStates;
_dc.ConversationDialogs.ReplaceOne(dialogFilter, foundDialog);
_dc.ConversationStates.ReplaceOne(stateFilter, foundStates);
// Remove logs
if (cleanLog)
{
var contentLogBuilder = Builders<ConversationContentLogDocument>.Filter;
var stateLogBuilder = Builders<ConversationStateLogDocument>.Filter;
var contentLogFilters = new List<FilterDefinition<ConversationContentLogDocument>>()
{
contentLogBuilder.Eq(x => x.ConversationId, conversationId),
contentLogBuilder.Gte(x => x.CreateTime, refTime)
};
var stateLogFilters = new List<FilterDefinition<ConversationStateLogDocument>>()
{
stateLogBuilder.Eq(x => x.ConversationId, conversationId),
stateLogBuilder.Gte(x => x.CreateTime, refTime)
};
_dc.ContentLogs.DeleteMany(contentLogBuilder.And(contentLogFilters));
_dc.StateLogs.DeleteMany(stateLogBuilder.And(stateLogFilters));
}
return true;
}
}