refine routing context

This commit is contained in:
Jicheng Lu 2025-09-19 11:24:13 -05:00
parent bc35112e1a
commit 0e89580713
8 changed files with 37 additions and 22 deletions

View file

@ -31,6 +31,7 @@ public interface IRoutingContext
void ResetAgentStack(); void ResetAgentStack();
void SetDialogs(List<RoleDialogModel> dialogs); void SetDialogs(List<RoleDialogModel> dialogs);
void AddDialogs(List<RoleDialogModel> dialogs);
List<RoleDialogModel> GetDialogs(); List<RoleDialogModel> GetDialogs();
void ResetDialogs(); void ResetDialogs();
} }

View file

@ -13,10 +13,10 @@ public partial class FileInstructService
return Enumerable.Empty<MessageFileModel>(); return Enumerable.Empty<MessageFileModel>();
} }
var routeContext = _services.GetRequiredService<IRoutingContext>(); var routingCtx = _services.GetRequiredService<IRoutingContext>();
var convService = _services.GetRequiredService<IConversationService>(); var convService = _services.GetRequiredService<IConversationService>();
var dialogs = routeContext.GetDialogs(); var dialogs = routingCtx.GetDialogs();
if (dialogs.IsNullOrEmpty()) if (dialogs.IsNullOrEmpty())
{ {
dialogs = convService.GetDialogHistory(fromBreakpoint: options.FromBreakpoint); dialogs = convService.GetDialogHistory(fromBreakpoint: options.FromBreakpoint);

View file

@ -31,12 +31,12 @@ public class ConversationObserver : BotSharpObserverBase<HubObserveData<RoleDial
{ {
var conv = _services.GetRequiredService<IConversationService>(); var conv = _services.GetRequiredService<IConversationService>();
var storage = _services.GetRequiredService<IConversationStorage>(); var storage = _services.GetRequiredService<IConversationStorage>();
var routeCtx = _services.GetRequiredService<IRoutingContext>(); var routingCtx = _services.GetRequiredService<IRoutingContext>();
if (value.EventName == ChatEvent.OnIndicationReceived) if (value.EventName == ChatEvent.OnIndicationReceived)
{ {
#if DEBUG #if DEBUG
_logger.LogCritical($"Receiving {value.EventName} ({value.Data.Indication}) in {nameof(ConversationObserver)} - {conv.ConversationId}"); _logger.LogCritical($"[{nameof(ConversationObserver)}]: Receive {value.EventName} => {value.Data.Indication} ({conv.ConversationId})");
#endif #endif
if (_listeners.TryGetValue(value.EventName, out var func) && func != null) if (_listeners.TryGetValue(value.EventName, out var func) && func != null)
{ {
@ -45,10 +45,10 @@ public class ConversationObserver : BotSharpObserverBase<HubObserveData<RoleDial
} }
else if (value.EventName == ChatEvent.OnIntermediateMessageReceivedFromAssistant) else if (value.EventName == ChatEvent.OnIntermediateMessageReceivedFromAssistant)
{ {
var dialogs = routeCtx.GetDialogs(); #if DEBUG
dialogs.Add(value.Data); _logger.LogCritical($"[{nameof(ConversationObserver)}]: Receive {value.EventName} => {value.Data.Content} ({conv.ConversationId})");
routeCtx.SetDialogs(dialogs); #endif
routingCtx.AddDialogs([value.Data]);
if (value.SaveDataToDb) if (value.SaveDataToDb)
{ {
storage.Append(conv.ConversationId, value.Data); storage.Append(conv.ConversationId, value.Data);

View file

@ -269,12 +269,20 @@ public class RoutingContext : IRoutingContext
public void SetDialogs(List<RoleDialogModel> dialogs) public void SetDialogs(List<RoleDialogModel> dialogs)
{ {
_dialogs = dialogs ?? []; _dialogs = new List<RoleDialogModel>(dialogs ?? []);
}
public void AddDialogs(List<RoleDialogModel> dialogs)
{
var items = new List<RoleDialogModel>(dialogs ?? []);
_dialogs ??= [];
_dialogs.AddRange(items);
} }
public List<RoleDialogModel> GetDialogs() public List<RoleDialogModel> GetDialogs()
{ {
return _dialogs ?? []; _dialogs ??= [];
return new List<RoleDialogModel>(_dialogs);
} }
public void ResetDialogs() public void ResetDialogs()

View file

@ -77,7 +77,7 @@ public partial class RoutingService
message.IsStreaming = response.IsStreaming; message.IsStreaming = response.IsStreaming;
message.MessageLabel = response.MessageLabel; message.MessageLabel = response.MessageLabel;
dialogs.Add(message); dialogs.Add(message);
Context.SetDialogs(dialogs); Context.AddDialogs([message]);
} }
return true; return true;
@ -106,10 +106,11 @@ public partial class RoutingService
var responseTemplate = await templateService.RenderFunctionResponse(message.CurrentAgentId, message); var responseTemplate = await templateService.RenderFunctionResponse(message.CurrentAgentId, message);
if (!string.IsNullOrEmpty(responseTemplate)) if (!string.IsNullOrEmpty(responseTemplate))
{ {
dialogs.Add(RoleDialogModel.From(message, var msg = RoleDialogModel.From(message,
role: AgentRole.Assistant, role: AgentRole.Assistant,
content: responseTemplate)); content: responseTemplate);
Context.SetDialogs(dialogs); dialogs.Add(msg);
Context.AddDialogs([msg]);
} }
else else
{ {
@ -119,7 +120,7 @@ public partial class RoutingService
content: message.Content); content: message.Content);
dialogs.Add(msg); dialogs.Add(msg);
Context.SetDialogs(dialogs); Context.AddDialogs([msg]);
// Send to Next LLM // Send to Next LLM
var curAgentId = routing.Context.GetCurrentAgentId(); var curAgentId = routing.Context.GetCurrentAgentId();
@ -128,10 +129,11 @@ public partial class RoutingService
} }
else else
{ {
dialogs.Add(RoleDialogModel.From(message, var msg = RoleDialogModel.From(message,
role: AgentRole.Assistant, role: AgentRole.Assistant,
content: message.Content)); content: message.Content);
Context.SetDialogs(dialogs); dialogs.Add(msg);
Context.AddDialogs([msg]);
} }
return true; return true;

View file

@ -3,6 +3,7 @@ using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.MessageHub.Models; using BotSharp.Abstraction.MessageHub.Models;
using BotSharp.Abstraction.MessageHub.Observers; using BotSharp.Abstraction.MessageHub.Observers;
using BotSharp.Abstraction.SideCar; using BotSharp.Abstraction.SideCar;
using BotSharp.Core.MessageHub.Observers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace BotSharp.Plugin.ChatHub.Observers; namespace BotSharp.Plugin.ChatHub.Observers;
@ -118,7 +119,7 @@ public class ChatHubObserver : BotSharpObserverBase<HubObserveData<RoleDialogMod
}; };
#if DEBUG #if DEBUG
_logger.LogCritical($"Receiving {value.EventName} ({value.Data.Indication}) in {nameof(ChatHubObserver)} - {conv.ConversationId}"); _logger.LogCritical($"[{nameof(ChatHubObserver)}]: Receive {value.EventName} => {value.Data.Indication} ({conv.ConversationId})");
#endif #endif
break; break;
case ChatEvent.OnIntermediateMessageReceivedFromAssistant: case ChatEvent.OnIntermediateMessageReceivedFromAssistant:

View file

@ -1,8 +1,8 @@
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using NPOI.SS.UserModel;
using System.Data; using System.Data;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Newtonsoft.Json;
using MySql.Data.MySqlClient;
using NPOI.SS.UserModel;
namespace BotSharp.Plugin.ExcelHandler.Services; namespace BotSharp.Plugin.ExcelHandler.Services;

View file

@ -7,6 +7,9 @@ public class ExcelHandlerSettings
public class DatabaseSettings public class DatabaseSettings
{ {
/// <summary>
/// Database: mysql, sqlite
/// </summary>
public string Provider { get; set; } = "mysql"; public string Provider { get; set; } = "mysql";
public string ConnectionString { get; set; } public string ConnectionString { get; set; }
} }