From 8048524a31ffb9c01251777ffa5cc29da8c4bfba Mon Sep 17 00:00:00 2001
From: Jicheng Lu <103353@smsassist.com>
Date: Thu, 20 Feb 2025 11:26:49 -0600
Subject: [PATCH] refine state
---
.../Conversations/IConversationStateService.cs | 2 +-
.../Conversations/Services/ConversationStateService.cs | 10 +++++-----
.../Controllers/ConversationController.cs | 5 +++--
3 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs
index df123922..a5563cf3 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs
@@ -6,7 +6,7 @@ namespace BotSharp.Abstraction.Conversations;
///
/// Conversation state service to track the context in the conversation lifecycle
///
-public interface IConversationStateService
+public interface IConversationStateService : IDisposable
{
string GetConversationId();
Dictionary Load(string conversationId, bool isReadOnly = false);
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs
index 531bf06d..4d229866 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs
@@ -22,11 +22,12 @@ namespace BotSharp.Core.Conversations.Services;
///
/// Maintain the conversation state
///
-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;
///
/// 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();
}
public string GetConversationId() => _conversationId;
@@ -139,9 +141,8 @@ public class ConversationStateService : IConversationStateService, IDisposable
_conversationId = !isReadOnly ? conversationId : null;
Reset();
- var sidecar = _services.GetService();
var endNodes = new Dictionary();
- 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();
- if (_conversationId == null || sidecar?.IsEnabled() == true)
+ if (_conversationId == null || _sidecar?.IsEnabled() == true)
{
Reset();
return;
diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
index f41c69fe..75dc1113 100644
--- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
+++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
@@ -79,8 +79,9 @@ public class ConversationController : ControllerBase
[HttpGet("/conversation/{conversationId}/dialogs")]
public async Task> GetDialogs([FromRoute] string conversationId)
{
- var storage = _services.GetRequiredService();
- var history = storage.GetDialogs(conversationId);
+ var conv = _services.GetRequiredService();
+ conv.SetConversationId(conversationId, [], isReadOnly: true);
+ var history = conv.GetDialogHistory(fromBreakpoint: false);
var userService = _services.GetRequiredService();
var agentService = _services.GetRequiredService();