refine state

This commit is contained in:
Jicheng Lu 2025-02-20 11:26:49 -06:00
parent 2e18372f79
commit 8048524a31
3 changed files with 9 additions and 8 deletions

View file

@ -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);

View file

@ -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
@ -47,6 +48,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
_logger = logger;
_curStates = new ConversationState();
_historyStates = new ConversationState();
_sidecar = services.GetService<IConversationSideCar>();
}
public string GetConversationId() => _conversationId;
@ -139,9 +141,8 @@ public class ConversationStateService : IConversationStateService, IDisposable
_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;
}
@ -217,8 +218,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
public void Save()
{
var sidecar = _services.GetService<IConversationSideCar>();
if (_conversationId == null || sidecar?.IsEnabled() == true)
if (_conversationId == null || _sidecar?.IsEnabled() == true)
{
Reset();
return;

View file

@ -79,8 +79,9 @@ public class ConversationController : ControllerBase
[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>();