Skip to content

refine state #894

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 2 commits into from
Feb 20, 2025
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 @@ -6,7 +6,7 @@ namespace BotSharp.Abstraction.Conversations;
/// <summary>
/// Conversation state service to track the context in the conversation lifecycle
/// </summary>
public interface IConversationStateService
public interface IConversationStateService : IDisposable
{
string GetConversationId();
Dictionary<string, string> Load(string conversationId, bool isReadOnly = false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
List<User> GetUsersByAffiliateId(string affiliateId) => throw new NotImplementedException();
User? GetUserByUserName(string userName) => throw new NotImplementedException();
void UpdateUserName(string userId, string userName) => throw new NotImplementedException();
Dashboard? GetDashboard(string id = null) => throw new NotImplementedException();

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 46 in src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Cannot convert null literal to non-nullable reference type.
void CreateUser(User user) => throw new NotImplementedException();
void UpdateExistUser(string userId, User user) => throw new NotImplementedException();
void UpdateUserVerified(string userId) => throw new NotImplementedException();
Expand Down Expand Up @@ -152,13 +152,6 @@
=> throw new NotImplementedException();
#endregion

#region Execution Log
void AddExecutionLogs(string conversationId, List<string> logs)
=> throw new NotImplementedException();
List<string> GetExecutionLogs(string conversationId)
=> throw new NotImplementedException();
#endregion

#region LLM Completion Log
void SaveLlmCompletionLog(LlmCompletionLog log)
=> throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ namespace BotSharp.Core.Conversations.Services;
/// <summary>
/// Maintain the conversation state
/// </summary>
public class ConversationStateService : IConversationStateService, IDisposable
public class ConversationStateService : IConversationStateService
{
private readonly ILogger _logger;
private readonly IServiceProvider _services;
private readonly IBotSharpRepository _db;
private readonly IConversationSideCar? _sidecar;
private string _conversationId;
/// <summary>
/// States in the current round of conversation
Expand All @@ -47,6 +48,7 @@ public ConversationStateService(
_logger = logger;
_curStates = new ConversationState();
_historyStates = new ConversationState();
_sidecar = services.GetService<IConversationSideCar>();
}

public string GetConversationId() => _conversationId;
Expand Down Expand Up @@ -139,9 +141,8 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =
_conversationId = !isReadOnly ? conversationId : null;
Reset();

var sidecar = _services.GetService<IConversationSideCar>();
var endNodes = new Dictionary<string, string>();
if (sidecar?.IsEnabled() == true)
if (_sidecar?.IsEnabled() == true)
{
return endNodes;
}
Expand Down Expand Up @@ -217,8 +218,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =

public void Save()
{
var sidecar = _services.GetService<IConversationSideCar>();
if (_conversationId == null || sidecar?.IsEnabled() == true)
if (_conversationId == null || _sidecar?.IsEnabled() == true)
{
Reset();
return;
Expand Down
13 changes: 6 additions & 7 deletions src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
using BotSharp.Abstraction.Evaluations;
using BotSharp.Abstraction.Repositories;
using System.Text.RegularExpressions;

namespace BotSharp.Core.Evaluations;

public class ExecutionLogger : IExecutionLogger
{
private readonly BotSharpDatabaseSettings _dbSettings;
private readonly IServiceProvider _services;
private readonly ILogger<ExecutionLogger> _logger;

public ExecutionLogger(
BotSharpDatabaseSettings dbSettings,
IServiceProvider services)
IServiceProvider services,
ILogger<ExecutionLogger> logger)
{
_dbSettings = dbSettings;
_services = services;
_logger = logger;
}

public void Append(string conversationId, string content)
{
content = content.Replace("\r\n", " ").Replace("\n", " ");
content = Regex.Replace(content, @"\s+", " ");
var db = _services.GetRequiredService<IBotSharpRepository>();
db.AddExecutionLogs(conversationId, new List<string> { content });
_logger.LogInformation($"Execution Log: {content}");
}
}
12 changes: 0 additions & 12 deletions src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,6 @@ public List<string> TruncateConversation(string conversationId, string messageId
=> throw new NotImplementedException();
#endregion

#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
throw new NotImplementedException();
}

public List<string> GetExecutionLogs(string conversationId)
{
throw new NotImplementedException();
}
#endregion

#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
using BotSharp.Abstraction.Loggers.Models;
using Serilog;
using System.IO;

namespace BotSharp.Core.Repository
{
public partial class FileRepository
{
#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;

var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

var file = Path.Combine(dir, EXECUTION_LOG_FILE);
File.AppendAllLines(file, logs);
}

public List<string> GetExecutionLogs(string conversationId)
{
var logs = new List<string>();
if (string.IsNullOrEmpty(conversationId)) return logs;

var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir)) return logs;

var file = Path.Combine(dir, EXECUTION_LOG_FILE);
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
return logs;
}
#endregion

#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ public partial class FileRepository : IBotSharpRepository
private const string KNOWLEDGE_DOC_FOLDER = "document";
private const string KNOWLEDGE_DOC_META_FILE = "meta.json";

private const string EXECUTION_LOG_FILE = "execution.log";
private const string PLUGIN_CONFIG_FILE = "config.json";

private const string STATS_FOLDER = "stats";
private const string STATS_FILE = "stats.json";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ private void SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel token
MessageId = message.MessageId,
AgentId = message.CurrentAgentId,
Prompt = tokenStats.Prompt,
Response = message.Content
Response = message.Content,
CreateDateTime = DateTime.UtcNow
};

db.SaveLlmCompletionLog(completionLog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ public async Task<PagedItems<ConversationViewModel>> GetConversations([FromBody]
[HttpGet("/conversation/{conversationId}/dialogs")]
public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string conversationId)
{
var storage = _services.GetRequiredService<IConversationStorage>();
var history = storage.GetDialogs(conversationId);
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(conversationId, [], isReadOnly: true);
var history = conv.GetDialogHistory(fromBreakpoint: false);

var userService = _services.GetRequiredService<IUserService>();
var agentService = _services.GetRequiredService<IAgentService>();
Expand Down

This file was deleted.

3 changes: 0 additions & 3 deletions src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@ public IMongoCollection<ConversationDialogDocument> ConversationDialogs
public IMongoCollection<ConversationStateDocument> ConversationStates
=> CreateConversationStateIndex();

public IMongoCollection<ExecutionLogDocument> ExectionLogs
=> GetCollectionOrCreate<ExecutionLogDocument>("ExecutionLogs");

public IMongoCollection<LlmCompletionLogDocument> LlmCompletionLogs
=> GetCollectionOrCreate<LlmCompletionLogDocument>("LlmCompletionLogs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@ public bool DeleteConversations(IEnumerable<string> conversationIds)
var filterConv = Builders<ConversationDocument>.Filter.In(x => x.Id, conversationIds);
var filterDialog = Builders<ConversationDialogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterSates = Builders<ConversationStateDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterExeLog = Builders<ExecutionLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterPromptLog = Builders<LlmCompletionLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterContentLog = Builders<ConversationContentLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterStateLog = Builders<ConversationStateLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var conbTabItems = Builders<CrontabItemDocument>.Filter.In(x => x.ConversationId, conversationIds);

var exeLogDeleted = _dc.ExectionLogs.DeleteMany(filterExeLog);
var promptLogDeleted = _dc.LlmCompletionLogs.DeleteMany(filterPromptLog);
var contentLogDeleted = _dc.ContentLogs.DeleteMany(filterContentLog);
var stateLogDeleted = _dc.StateLogs.DeleteMany(filterStateLog);
Expand All @@ -71,10 +69,8 @@ public bool DeleteConversations(IEnumerable<string> conversationIds)
var cronDeleted = _dc.CrontabItems.DeleteMany(conbTabItems);
var convDeleted = _dc.Conversations.DeleteMany(filterConv);

return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0
|| exeLogDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0
|| contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0
|| convDeleted.DeletedCount > 0;
return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0
|| contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0 || convDeleted.DeletedCount > 0;
}

[SideCar]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,6 @@ namespace BotSharp.Plugin.MongoStorage.Repository;

public partial class MongoRepository
{
#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;

var filter = Builders<ExecutionLogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var update = Builders<ExecutionLogDocument>.Update
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
.PushEach(x => x.Logs, logs);

_dc.ExectionLogs.UpdateOne(filter, update, _options);
}

public List<string> GetExecutionLogs(string conversationId)
{
List<string> logs = [];
if (string.IsNullOrEmpty(conversationId)) return logs;

var filter = Builders<ExecutionLogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();

logs = logCollection?.Logs ?? [];
return logs;
}
#endregion

#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{
Expand Down
Loading