Merge pull request #324 from iceljc/features/add-function-name
add delete log
This commit is contained in:
commit
82da7e7067
|
|
@ -59,7 +59,7 @@ public interface IBotSharpRepository
|
||||||
PagedItems<Conversation> GetConversations(ConversationFilter filter);
|
PagedItems<Conversation> GetConversations(ConversationFilter filter);
|
||||||
void UpdateConversationTitle(string conversationId, string title);
|
void UpdateConversationTitle(string conversationId, string title);
|
||||||
List<Conversation> GetLastConversations();
|
List<Conversation> GetLastConversations();
|
||||||
bool TruncateConversation(string conversationId, string messageId);
|
bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Execution Log
|
#region Execution Log
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ public partial class ConversationService : IConversationService
|
||||||
public async Task<bool> TruncateConversation(string conversationId, string messageId)
|
public async Task<bool> TruncateConversation(string conversationId, string messageId)
|
||||||
{
|
{
|
||||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||||
var isSaved = db.TruncateConversation(conversationId, messageId);
|
var isSaved = db.TruncateConversation(conversationId, messageId, true);
|
||||||
return await Task.FromResult(isSaved);
|
return await Task.FromResult(isSaved);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -217,7 +217,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TruncateConversation(string conversationId, string messageId)
|
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
using BotSharp.Abstraction.Loggers.Models;
|
||||||
using BotSharp.Abstraction.Repositories.Filters;
|
using BotSharp.Abstraction.Repositories.Filters;
|
||||||
using BotSharp.Abstraction.Repositories.Models;
|
using BotSharp.Abstraction.Repositories.Models;
|
||||||
using System.Globalization;
|
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;
|
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) return false;
|
||||||
|
|
||||||
|
|
@ -288,6 +289,12 @@ namespace BotSharp.Core.Repository
|
||||||
var states = CollectConversationStates(stateDir);
|
var states = CollectConversationStates(stateDir);
|
||||||
isSaved = HandleTruncatedStates(stateDir, states, refTime);
|
isSaved = HandleTruncatedStates(stateDir, states, refTime);
|
||||||
|
|
||||||
|
// Remove logs
|
||||||
|
if (cleanLog)
|
||||||
|
{
|
||||||
|
HandleTruncatedLogs(convDir, refTime);
|
||||||
|
}
|
||||||
|
|
||||||
return isSaved;
|
return isSaved;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -392,6 +399,44 @@ namespace BotSharp.Core.Repository
|
||||||
return isSaved;
|
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)
|
private bool SaveTruncatedDialogs(string dialogDir, List<DialogElement> dialogs)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(dialogDir) || dialogs == null) return false;
|
if (string.IsNullOrEmpty(dialogDir) || dialogs == null) return false;
|
||||||
|
|
|
||||||
|
|
@ -85,12 +85,10 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
|
||||||
|
|
||||||
var conversationId = _state.GetConversationId();
|
var conversationId = _state.GetConversationId();
|
||||||
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
|
var agent = await _agentService.LoadAgent(message.CurrentAgentId);
|
||||||
var logSource = string.Empty;
|
|
||||||
|
|
||||||
var log = tokenStats.Prompt;
|
var log = tokenStats.Prompt;
|
||||||
logSource = ContentLogSource.Prompt;
|
|
||||||
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
await _chatHub.Clients.User(_user.Id).SendAsync("OnConversationContentLogGenerated",
|
||||||
BuildContentLog(conversationId, agent?.Name, log, logSource, message));
|
BuildContentLog(conversationId, agent?.Name, log, ContentLogSource.Prompt, message));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ using BotSharp.Abstraction.Repositories.Filters;
|
||||||
using BotSharp.Abstraction.Repositories.Models;
|
using BotSharp.Abstraction.Repositories.Models;
|
||||||
using BotSharp.Plugin.MongoStorage.Collections;
|
using BotSharp.Plugin.MongoStorage.Collections;
|
||||||
using BotSharp.Plugin.MongoStorage.Models;
|
using BotSharp.Plugin.MongoStorage.Models;
|
||||||
|
using MongoDB.Driver;
|
||||||
|
|
||||||
namespace BotSharp.Plugin.MongoStorage.Repository;
|
namespace BotSharp.Plugin.MongoStorage.Repository;
|
||||||
|
|
||||||
|
|
@ -273,7 +274,7 @@ public partial class MongoRepository
|
||||||
}).ToList();
|
}).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;
|
if (string.IsNullOrEmpty(conversationId) || string.IsNullOrEmpty(messageId)) return false;
|
||||||
|
|
||||||
|
|
@ -308,6 +309,28 @@ public partial class MongoRepository
|
||||||
foundStates.States = truncatedStates;
|
foundStates.States = truncatedStates;
|
||||||
_dc.ConversationDialogs.ReplaceOne(dialogFilter, foundDialog);
|
_dc.ConversationDialogs.ReplaceOne(dialogFilter, foundDialog);
|
||||||
_dc.ConversationStates.ReplaceOne(stateFilter, foundStates);
|
_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;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue