Skip to content

add delete log #324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public void UpdateConversationStatus(string conversationId, string status)
throw new NotImplementedException();
}

public bool TruncateConversation(string conversationId, string messageId)
public bool TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Repositories.Models;
using System.Globalization;
Expand Down Expand Up @@ -263,7 +264,7 @@ public List<Conversation> GetLastConversations()
}


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;

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

// Remove logs
if (cleanLog)
{
HandleTruncatedLogs(convDir, refTime);
}

return isSaved;
}

Expand Down Expand Up @@ -392,6 +399,44 @@ private bool HandleTruncatedStates(string stateDir, List<StateKeyValue> states,
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@ public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenS

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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using BotSharp.Abstraction.Repositories.Models;
using BotSharp.Plugin.MongoStorage.Collections;
using BotSharp.Plugin.MongoStorage.Models;
using MongoDB.Driver;

namespace BotSharp.Plugin.MongoStorage.Repository;

Expand Down Expand Up @@ -273,7 +274,7 @@ public List<Conversation> GetLastConversations()
}).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;

Expand Down Expand Up @@ -308,6 +309,28 @@ public bool TruncateConversation(string conversationId, string messageId)
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;
}
}