refine state loading

This commit is contained in:
Jicheng Lu 2025-03-14 17:04:54 -05:00
parent 995b71e145
commit 5d4dcc3e13
12 changed files with 139 additions and 87 deletions

View file

@ -9,7 +9,7 @@ public interface IConversationService
string ConversationId { get; }
Task<Conversation> NewConversation(Conversation conversation);
void SetConversationId(string conversationId, List<MessageState> states, bool isReadOnly = false);
Task<Conversation> GetConversation(string id);
Task<Conversation> GetConversation(string id, bool isLoadStates = false);
Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter);
Task<Conversation> UpdateConversationTitle(string id, string title);
Task<Conversation> UpdateConversationTitleAlias(string id, string titleAlias);

View file

@ -27,6 +27,8 @@ public class ConversationFilter
public List<string>? Tags { get; set; }
public bool IsLoadLatestStates { get; set; }
public static ConversationFilter Empty()
{
return new ConversationFilter();

View file

@ -124,7 +124,7 @@ public interface IBotSharpRepository : IHaveServiceProvider
=> throw new NotImplementedException();
void UpdateConversationStatus(string conversationId, string status)
=> throw new NotImplementedException();
Conversation GetConversation(string conversationId)
Conversation GetConversation(string conversationId, bool isLoadStates = false)
=> throw new NotImplementedException();
PagedItems<Conversation> GetConversations(ConversationFilter filter)
=> throw new NotImplementedException();

View file

@ -71,7 +71,7 @@ public partial class ConversationService : IConversationService
return db.UpdateConversationMessage(conversationId, request);
}
public async Task<Conversation> GetConversation(string id)
public async Task<Conversation> GetConversation(string id, bool isLoadStates = false)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var conversation = db.GetConversation(id);
@ -80,6 +80,11 @@ public partial class ConversationService : IConversationService
public async Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
{
if (filter == null)
{
filter = ConversationFilter.Empty();
}
var db = _services.GetRequiredService<IBotSharpRepository>();
var conversations = db.GetConversations(filter);
return conversations;

View file

@ -69,23 +69,34 @@ public class ConversationStateService : IConversationStateService
return this;
}
var defaultRound = -1;
var preValue = string.Empty;
var currentValue = value.ToString();
var hooks = _services.GetServices<IConversationHook>();
var curActiveRounds = activeRounds > 0 ? activeRounds : -1;
int? preActiveRounds = null;
var curActive = true;
StateKeyValue? pair = null;
StateValue? prevLeafNode = null;
var curActiveRounds = activeRounds > 0 ? activeRounds : defaultRound;
if (ContainsState(name) && _curStates.TryGetValue(name, out var pair))
if (ContainsState(name) && _curStates.TryGetValue(name, out pair))
{
var leafNode = pair?.Values?.LastOrDefault();
preActiveRounds = leafNode?.ActiveRounds;
preValue = leafNode?.Data ?? string.Empty;
prevLeafNode = pair?.Values?.LastOrDefault();
preValue = prevLeafNode?.Data ?? string.Empty;
}
_logger.LogInformation($"[STATE] {name} = {value}");
var routingCtx = _services.GetRequiredService<IRoutingContext>();
if (!ContainsState(name) || preValue != currentValue || preActiveRounds != curActiveRounds)
var isNoChange = ContainsState(name)
&& preValue == currentValue
&& prevLeafNode?.ActiveRounds == curActiveRounds
&& curActiveRounds == defaultRound
&& prevLeafNode?.Source == source
&& prevLeafNode?.DataType == valueType
&& prevLeafNode?.Active == curActive
&& pair?.Readonly == readOnly;
var hooks = _services.GetServices<IConversationHook>();
if (!ContainsState(name) || preValue != currentValue || prevLeafNode?.ActiveRounds != curActiveRounds)
{
foreach (var hook in hooks)
{
@ -95,7 +106,7 @@ public class ConversationStateService : IConversationStateService
MessageId = routingCtx.MessageId,
Name = name,
BeforeValue = preValue,
BeforeActiveRounds = preActiveRounds,
BeforeActiveRounds = prevLeafNode?.ActiveRounds,
AfterValue = currentValue,
AfterActiveRounds = curActiveRounds,
DataType = valueType,
@ -116,7 +127,7 @@ public class ConversationStateService : IConversationStateService
{
Data = currentValue,
MessageId = routingCtx.MessageId,
Active = true,
Active = curActive,
ActiveRounds = curActiveRounds,
DataType = valueType,
Source = source,
@ -128,6 +139,10 @@ public class ConversationStateService : IConversationStateService
newPair.Values = new List<StateValue> { newValue };
_curStates[name] = newPair;
}
else if (isNoChange)
{
// do nothing
}
else
{
_curStates[name].Values.Add(newValue);
@ -415,14 +430,14 @@ public class ConversationStateService : IConversationStateService
{
var values = _curStates.Values.ToList();
var copy = JsonSerializer.Deserialize<List<StateKeyValue>>(JsonSerializer.Serialize(values));
return new ConversationState(copy ?? new());
return new ConversationState(copy ?? []);
}
public void SetCurrentState(ConversationState state)
{
var values = _curStates.Values.ToList();
var copy = JsonSerializer.Deserialize<List<StateKeyValue>>(JsonSerializer.Serialize(values));
_curStates = new ConversationState(copy ?? new());
_curStates = new ConversationState(copy ?? []);
}
public void ResetCurrentState()

View file

@ -80,7 +80,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
public bool DeleteConversations(IEnumerable<string> conversationIds)
=> throw new NotImplementedException();
public Conversation GetConversation(string conversationId)
public Conversation GetConversation(string conversationId, bool isLoadStates = false)
=> throw new NotImplementedException();
public PagedItems<Conversation> GetConversations(ConversationFilter filter)

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Users.Models;
using System;
using System.IO;
namespace BotSharp.Core.Repository;
@ -346,7 +347,7 @@ public partial class FileRepository
}
}
public Conversation GetConversation(string conversationId)
public Conversation GetConversation(string conversationId, bool isLoadStates = false)
{
var convDir = FindConversationDirectory(conversationId);
if (string.IsNullOrEmpty(convDir)) return null;
@ -361,18 +362,20 @@ public partial class FileRepository
record.Dialogs = CollectDialogElements(dialogFile);
}
var stateFile = Path.Combine(convDir, STATE_FILE);
if (record != null)
if (isLoadStates)
{
var states = CollectConversationStates(stateFile);
var curStates = new Dictionary<string, string>();
states.ForEach(x =>
var latestStateFile = Path.Combine(convDir, CONV_LATEST_STATE_FILE);
if (record != null && File.Exists(latestStateFile))
{
curStates[x.Key] = x.Values?.LastOrDefault()?.Data ?? string.Empty;
});
record.States = curStates;
var stateJson = File.ReadAllText(latestStateFile);
var states = JsonSerializer.Deserialize<Dictionary<string, JsonDocument>>(stateJson, _options) ?? [];
record.States = states.ToDictionary(x => x.Key, x =>
{
var elem = x.Value.RootElement.GetProperty("data");
return elem.ValueKind != JsonValueKind.Null ? elem.ToString() : null;
});
}
}
return record;
}
@ -508,6 +511,21 @@ public partial class FileRepository
if (!matched) continue;
if (filter.IsLoadLatestStates)
{
var latestStateFile = Path.Combine(d, CONV_LATEST_STATE_FILE);
if (File.Exists(latestStateFile))
{
var stateJson = File.ReadAllText(latestStateFile);
var states = JsonSerializer.Deserialize<Dictionary<string, JsonDocument>>(stateJson, _options) ?? [];
record.States = states.ToDictionary(x => x.Key, x =>
{
var elem = x.Value.RootElement.GetProperty("data");
return elem.ValueKind != JsonValueKind.Null ? elem.ToString() : null;
});
}
}
records.Add(record);
}

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Functions;
using BotSharp.Abstraction.Options;
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Routing;
@ -9,18 +8,20 @@ public partial class RoutingService
public async Task<bool> InvokeFunction(string name, RoleDialogModel message)
{
var function = _services.GetServices<IFunctionCallback>().FirstOrDefault(x => x.Name == name);
var isFillDummyContent = false;
var dummyFuncResponse = string.Empty;
if (function == null)
{
var executed = await InvokeDummyFunction(name, message);
if (executed)
dummyFuncResponse = await GetDummyFunctionOutput(name, message);
isFillDummyContent = !string.IsNullOrEmpty(dummyFuncResponse);
if (!isFillDummyContent)
{
return true;
message.StopCompletion = true;
message.Content = $"Can't find function implementation of {name}.";
_logger.LogError(message.Content);
return false;
}
message.StopCompletion = true;
message.Content = $"Can't find function implementation of {name}.";
_logger.LogError(message.Content);
return false;
}
// Clone message
@ -34,7 +35,15 @@ public partial class RoutingService
var progressService = _services.GetService<IConversationProgressService>();
// Before executing functions
clonedMessage.Indication = await function.GetIndication(message);
if (!isFillDummyContent)
{
clonedMessage.Indication = await function.GetIndication(message);
}
else
{
clonedMessage.Indication = "Running";
}
if (progressService?.OnFunctionExecuting != null)
{
await progressService.OnFunctionExecuting(clonedMessage);
@ -49,7 +58,15 @@ public partial class RoutingService
try
{
result = await function.Execute(clonedMessage);
if (!isFillDummyContent)
{
result = await function.Execute(clonedMessage);
}
else
{
clonedMessage.Content = dummyFuncResponse;
result = true;
}
// After functions have been executed
foreach (var hook in hooks)
@ -97,11 +114,11 @@ public partial class RoutingService
return result;
}
private async Task<bool> InvokeDummyFunction(string functionName, RoleDialogModel message)
private async Task<string?> GetDummyFunctionOutput(string functionName, RoleDialogModel message)
{
if (string.IsNullOrEmpty(message.CurrentAgentId))
{
return false;
return null;
}
var agentService = _services.GetRequiredService<IAgentService>();
@ -109,25 +126,11 @@ public partial class RoutingService
var found = agent?.Functions?.FirstOrDefault(x => x.Name == functionName);
if (string.IsNullOrWhiteSpace(found?.Output))
{
return false;
}
var clonedMessage = RoleDialogModel.From(message);
clonedMessage.FunctionName = functionName;
clonedMessage.Indication = "Running";
var hooks = _services
.GetRequiredService<ConversationHookProvider>()
.HooksOrderByPriority;
foreach (var hook in hooks)
{
await hook.OnFunctionExecuting(clonedMessage);
return null;
}
var render = _services.GetRequiredService<ITemplateRender>();
var state = _services.GetRequiredService<IConversationStateService>();
var options = _services.GetRequiredService<BotSharpOptions>();
var dict = new Dictionary<string, object>();
foreach (var item in state.GetStates())
@ -136,14 +139,6 @@ public partial class RoutingService
}
var text = render.Render(found.Output, dict);
message.Content = text;
clonedMessage.Content = text;
foreach (var hook in hooks)
{
await hook.OnFunctionExecuted(clonedMessage);
}
return true;
return text;
}
}

View file

@ -138,7 +138,7 @@ public class ConversationController : ControllerBase
}
[HttpGet("/conversation/{conversationId}")]
public async Task<ConversationViewModel?> GetConversation([FromRoute] string conversationId)
public async Task<ConversationViewModel?> GetConversation([FromRoute] string conversationId, [FromQuery] bool isLoadStates = false)
{
var service = _services.GetRequiredService<IConversationService>();
var userService = _services.GetRequiredService<IUserService>();
@ -151,7 +151,8 @@ public class ConversationController : ControllerBase
var filter = new ConversationFilter
{
Id = conversationId,
UserId = !isAdmin ? user.Id : null
UserId = !isAdmin ? user.Id : null,
IsLoadLatestStates = isLoadStates
};
var conversations = await service.GetConversations(filter);
if (conversations.Items.IsNullOrEmpty())
@ -161,7 +162,6 @@ public class ConversationController : ControllerBase
var result = ConversationViewModel.FromSession(conversations.Items.First());
var state = _services.GetRequiredService<IConversationStateService>();
result.States = state.Load(conversationId, isReadOnly: true);
user = await userService.GetUser(result.User.Id);
result.User = UserViewModel.FromUser(user);

View file

@ -31,7 +31,7 @@ public class ConversationViewModel
public string? TaskId { get; set; }
public string Status { get; set; }
public Dictionary<string, string> States { get; set; }
public Dictionary<string, string> States { get; set; } = [];
public List<string> Tags { get; set; } = new();
@ -55,7 +55,8 @@ public class ConversationViewModel
Channel = sess.Channel,
Status = sess.Status,
TaskId = sess.TaskId,
Tags = sess.Tags ?? new(),
Tags = sess.Tags ?? [],
States = sess.States ?? [],
CreatedTime = sess.CreatedTime,
UpdatedTime = sess.UpdatedTime
};

View file

@ -106,6 +106,7 @@ public class ChatHubConversationHook : ConversationHookBase
if (!AllowSendingMessage()) return;
var conv = _services.GetRequiredService<IConversationService>();
var state = _services.GetRequiredService<IConversationStateService>();
var json = JsonSerializer.Serialize(new ChatResponseModel()
{
ConversationId = conv.ConversationId,
@ -114,6 +115,7 @@ public class ChatHubConversationHook : ConversationHookBase
Function = message.FunctionName,
RichContent = message.SecondaryRichContent ?? message.RichContent,
Data = message.Data,
States = state.GetStates(),
Sender = new UserViewModel()
{
FirstName = "AI",

View file

@ -299,26 +299,25 @@ public partial class MongoRepository
_dc.Conversations.UpdateOne(filter, update);
}
public Conversation GetConversation(string conversationId)
public Conversation GetConversation(string conversationId, bool isLoadStates = false)
{
if (string.IsNullOrEmpty(conversationId)) return null;
var filterConv = Builders<ConversationDocument>.Filter.Eq(x => x.Id, conversationId);
var filterDialog = Builders<ConversationDialogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var filterState = Builders<ConversationStateDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var conv = _dc.Conversations.Find(filterConv).FirstOrDefault();
var dialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault();
var states = _dc.ConversationStates.Find(filterState).FirstOrDefault();
if (conv == null) return null;
var dialogElements = dialog?.Dialogs?.Select(x => DialogMongoElement.ToDomainElement(x))?.ToList() ?? new List<DialogElement>();
var curStates = new Dictionary<string, string>();
states.States.ForEach(x =>
var curStates = conv.LatestStates?.ToDictionary(x => x.Key, x =>
{
curStates[x.Key] = x.Values?.LastOrDefault()?.Data ?? string.Empty;
});
var jsonDoc = JsonDocument.Parse(x.Value.ToJson());
var data = jsonDoc.RootElement.GetProperty("data");
return data.ValueKind != JsonValueKind.Null ? data.ToString() : null;
}) ?? [];
return new Conversation
{
@ -456,19 +455,34 @@ public partial class MongoRepository
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDef).Skip(pager.Offset).Limit(pager.Size).ToList();
var count = _dc.Conversations.CountDocuments(filterDef);
var conversations = conversationDocs.Select(x => new Conversation
var conversations = conversationDocs.Select(x =>
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
UserId = x.UserId.ToString(),
TaskId = x.TaskId,
Title = x.Title,
Channel = x.Channel,
Status = x.Status,
DialogCount = x.DialogCount,
Tags = x.Tags ?? new(),
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
var states = new Dictionary<string, string>();
if (filter.IsLoadLatestStates)
{
states = x.LatestStates.ToDictionary(p => p.Key, p =>
{
var jsonDoc = JsonDocument.Parse(p.Value.ToJson());
var data = jsonDoc.RootElement.GetProperty("data");
return data.ValueKind != JsonValueKind.Null ? data.ToString() : null;
});
}
return new Conversation
{
Id = x.Id.ToString(),
AgentId = x.AgentId.ToString(),
UserId = x.UserId.ToString(),
TaskId = x.TaskId,
Title = x.Title,
Channel = x.Channel,
Status = x.Status,
DialogCount = x.DialogCount,
Tags = x.Tags ?? [],
States = states,
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
};
}).ToList();
return new PagedItems<Conversation>