diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs index eeb39f73..c6af0438 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs @@ -12,7 +12,7 @@ public interface IConversationStateService string GetState(string name, string defaultValue = ""); bool ContainsState(string name); ConversationState GetStates(); - IConversationStateService SetState(string name, T value); + IConversationStateService SetState(string name, T value, bool isNeedVersion = true); void SaveStateByArgs(JsonDocument args); void CleanState(); void Save(); diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/HistoryStateKeyValue.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/HistoryStateKeyValue.cs index 2134f9e8..e5368f42 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/HistoryStateKeyValue.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/HistoryStateKeyValue.cs @@ -19,7 +19,6 @@ public class HistoryStateKeyValue public class HistoryStateValue { - public string? MessageId { get; set; } public string Data { get; set; } public DateTime UpdateTime { get; set; } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 1975e53d..fec0dcdb 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -34,9 +34,9 @@ public class ConversationStateService : IConversationStateService, IDisposable /// /// /// - /// whether the state is related to message or not + /// whether the state is related to message or not /// - public IConversationStateService SetState(string name, T value) + public IConversationStateService SetState(string name, T value, bool isNeedVersion = true) { if (value == null) { @@ -58,17 +58,18 @@ public class ConversationStateService : IConversationStateService, IDisposable var historyStateValue = new HistoryStateValue { - MessageId = GetCurrentMessageId(), Data = currentValue, UpdateTime = DateTime.UtcNow }; - if (!_historyStates.ContainsKey(name)) + if (!_historyStates.ContainsKey(name) || !isNeedVersion) { - _historyStates[name] = new List(); + _historyStates[name] = new List { historyStateValue }; + } + else + { + _historyStates[name].Add(historyStateValue); } - - _historyStates[name].Add(historyStateValue); } return this; @@ -164,12 +165,4 @@ public class ConversationStateService : IConversationStateService, IDisposable } } } - - private string? GetCurrentMessageId() - { - if (string.IsNullOrEmpty(_conversationId)) return null; - - var dialogs = _db.GetConversationDialogs(_conversationId); - return dialogs.LastOrDefault()?.MetaData?.MessageId; - } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs index 62b52f53..1f824dd8 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/TokenStatistics.cs @@ -47,14 +47,14 @@ public class TokenStatistics : ITokenStatistics // Accumulated Token var stat = _services.GetRequiredService(); var inputCount = int.Parse(stat.GetState("prompt_total", "0")); - stat.SetState("prompt_total", stats.PromptCount + inputCount); + stat.SetState("prompt_total", stats.PromptCount + inputCount, false); var outputCount = int.Parse(stat.GetState("completion_total", "0")); - stat.SetState("completion_total", stats.CompletionCount + outputCount); + stat.SetState("completion_total", stats.CompletionCount + outputCount, false); // Total cost var total_cost = float.Parse(stat.GetState("llm_total_cost", "0")); total_cost += Cost; - stat.SetState("llm_total_cost", total_cost); + stat.SetState("llm_total_cost", total_cost, false); } public void PrintStatistics() diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs index 4053d2e7..ee4228fd 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/StateMongoElement.cs @@ -28,7 +28,6 @@ public class StateMongoElement public class StateValueMongoElement { - public string? MessageId { get; set; } public string Data { get; set; } public DateTime UpdateTime { get; set; } @@ -36,7 +35,6 @@ public class StateValueMongoElement { return new StateValueMongoElement { - MessageId = element.MessageId, Data = element.Data, UpdateTime = element.UpdateTime }; @@ -46,7 +44,6 @@ public class StateValueMongoElement { return new HistoryStateValue { - MessageId = element.MessageId, Data = element.Data, UpdateTime = element.UpdateTime };