Skip to content

Commit

Permalink
Merge pull request #887 from iceljc/features/send-chat-event-by-group
Browse files Browse the repository at this point in the history
send by group
  • Loading branch information
iceljc authored Feb 19, 2025
2 parents 26e9eb5 + 8f5a307 commit 1d8f13d
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ Task<bool> SendMessage(string agentId,
/// <param name="convLimit">conversation limit</param>
/// <param name="preLoad">if pre-loading, then keys are not filter by the search query</param>
/// <returns></returns>
Task<List<string>> GetConversationStateSearhKeys(string query, int convlimit = 100, int keyLimit = 10, bool preLoad = false);
Task<List<string>> GetConversationStateSearhKeys(string query, int convLimit = 100, int keyLimit = 10, bool preload = false);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,25 @@ public class DialogElement
[JsonPropertyName("payload")]
public string? Payload { get; set; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonPropertyName("data")]
public object? Data { get; set; }

public DialogElement()
{

}

public DialogElement(DialogMetaData meta, string content, string? richContent = null,
string? secondaryContent = null, string? secondaryRichContent = null, string? payload = null)
string? secondaryContent = null, string? secondaryRichContent = null, string? payload = null, object? data = null)
{
MetaData = meta;
Content = content;
RichContent = richContent;
SecondaryContent = secondaryContent;
SecondaryRichContent = secondaryRichContent;
Payload = payload;
Data = data;
}

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ List<string> GetIdleConversations(int batchSize, int messageLimit, int bufferHou
=> throw new NotImplementedException();
List<string> TruncateConversation(string conversationId, string messageId, bool cleanLog = false)
=> throw new NotImplementedException();
List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperlimit = 100)
List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperLimit = 100)
=> throw new NotImplementedException();
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,17 @@ public void SaveStates()
_state.Save();
}

public async Task<List<string>> GetConversationStateSearhKeys(string query, int convlimit = 100, int keyLimit = 10, bool preLoad = false)
public async Task<List<string>> GetConversationStateSearhKeys(string query, int convLimit = 100, int keyLimit = 10, bool preload = false)
{
var keys = new List<string>();
if (!preLoad && string.IsNullOrWhiteSpace(query))
if (!preload && string.IsNullOrWhiteSpace(query))
{
return keys;
}

var db = _services.GetRequiredService<IBotSharpRepository>();
keys = db.GetConversationStateSearchKeys(convUpperlimit: convlimit);
keys = preLoad ? keys : keys.Where(x => x.Contains(query, StringComparison.OrdinalIgnoreCase)).ToList();
keys = db.GetConversationStateSearchKeys(convUpperLimit: convLimit);
keys = preload ? keys : keys.Where(x => x.Contains(query, StringComparison.OrdinalIgnoreCase)).ToList();
return keys.OrderBy(x => x).Take(keyLimit).ToList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
******************************************************************************/

using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.SideCar;

namespace BotSharp.Core.Conversations.Services;

Expand All @@ -26,6 +27,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
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
Expand All @@ -39,10 +41,12 @@ public class ConversationStateService : IConversationStateService, IDisposable
public ConversationStateService(
IServiceProvider services,
IBotSharpRepository db,
IConversationSideCar sidecar,
ILogger<ConversationStateService> logger)
{
_services = services;
_db = db;
_sidecar = sidecar;
_logger = logger;
_curStates = new ConversationState();
_historyStates = new ConversationState();
Expand Down Expand Up @@ -138,15 +142,20 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =
_conversationId = !isReadOnly ? conversationId : null;
Reset();

var routingCtx = _services.GetRequiredService<IRoutingContext>();
var curMsgId = routingCtx.MessageId;

_historyStates = _db.GetConversationStates(conversationId);

var endNodes = new Dictionary<string, string>();
if (_sidecar?.IsEnabled() == true)
{
return endNodes;
}

if (_historyStates.IsNullOrEmpty()) return endNodes;
_historyStates = _db.GetConversationStates(conversationId);
if (_historyStates.IsNullOrEmpty())
{
return endNodes;
}

var routingCtx = _services.GetRequiredService<IRoutingContext>();
var curMsgId = routingCtx.MessageId;
var dialogs = _db.GetConversationDialogs(conversationId);
var userDialogs = dialogs.Where(x => x.MetaData?.Role == AgentRole.User)
.GroupBy(x => x.MetaData?.MessageId)
Expand Down Expand Up @@ -210,7 +219,7 @@ public Dictionary<string, string> Load(string conversationId, bool isReadOnly =

public void Save()
{
if (_conversationId == null)
if (_conversationId == null || _sidecar?.IsEnabled() == true)
{
Reset();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void Append(string conversationId, RoleDialogModel dialog)
MetaData = meta,
Content = dialog.Content,
SecondaryContent = dialog.SecondaryContent,
Payload = dialog.Payload
Payload = dialog.Payload,
Data = dialog.Data
});
}
else
Expand Down Expand Up @@ -83,7 +84,8 @@ public void Append(string conversationId, RoleDialogModel dialog)
SecondaryContent = dialog.SecondaryContent,
RichContent = richContent,
SecondaryRichContent = secondaryRichContent,
Payload = dialog.Payload
Payload = dialog.Payload,
Data = dialog.Data
});
}

Expand Down Expand Up @@ -121,7 +123,8 @@ public void Append(string conversationId, IEnumerable<RoleDialogModel> dialogs)
MetaData = meta,
Content = dialog.Content,
SecondaryContent = dialog.SecondaryContent,
Payload = dialog.Payload
Payload = dialog.Payload,
Data = dialog.Data
});
}
else
Expand Down Expand Up @@ -152,7 +155,8 @@ public void Append(string conversationId, IEnumerable<RoleDialogModel> dialogs)
SecondaryContent = dialog.SecondaryContent,
RichContent = richContent,
SecondaryRichContent = secondaryRichContent,
Payload = dialog.Payload
Payload = dialog.Payload,
Data = dialog.Data
});
}
}
Expand Down Expand Up @@ -196,7 +200,8 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
RichContent = richContent,
SecondaryContent = secondaryContent,
SecondaryRichContent = secondaryRichContent,
Payload = payload
Payload = payload,
Data = dialog.Data
};
results.Add(record);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ public List<string> TruncateConversation(string conversationId, string messageId
#if !DEBUG
[SharpCache(10)]
#endif
public List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperlimit = 100)
public List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, int convUpperLimit = 100)
{
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
if (!Directory.Exists(dir)) return [];
Expand Down Expand Up @@ -635,7 +635,7 @@ public List<string> GetConversationStateSearchKeys(int messageLowerLimit = 2, in
keys.AddRange(stateKeys);
count++;

if (count >= convUpperlimit)
if (count >= convUpperLimit)
{
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ public async Task<PagedItems<ConversationViewModel>> GetConversations([FromBody]
[HttpGet("/conversation/{conversationId}/dialogs")]
public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string conversationId)
{
var conv = _services.GetRequiredService<IConversationService>();
conv.SetConversationId(conversationId, new List<MessageState>(), isReadOnly: true);
var history = conv.GetDialogHistory(fromBreakpoint: false);
var storage = _services.GetRequiredService<IConversationStorage>();
var history = storage.GetDialogs(conversationId);

var userService = _services.GetRequiredService<IUserService>();
var agentService = _services.GetRequiredService<IAgentService>();
Expand Down Expand Up @@ -555,10 +554,10 @@ public async Task<bool> UnpinConversationFromDashboard([FromRoute] string agentI

#region Search state keys
[HttpGet("/conversation/state/keys")]
public async Task<List<string>> GetConversationStateKeys([FromQuery] string query, [FromQuery] int keyLimit = 10, [FromQuery] bool preLoad = false)
public async Task<List<string>> GetConversationStateKeys([FromQuery] string query, [FromQuery] int keyLimit = 10, [FromQuery] int convLimit = 100, [FromQuery] bool preload = false)
{
var convService = _services.GetRequiredService<IConversationService>();
var keys = await convService.GetConversationStateSearhKeys(query, keyLimit: keyLimit, preLoad: preLoad);
var keys = await convService.GetConversationStateSearhKeys(query, keyLimit: keyLimit, convLimit: convLimit, preload: preload);
return keys;
}
#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(TargetFramework)</TargetFramework>
Expand Down
7 changes: 5 additions & 2 deletions src/Plugins/BotSharp.Plugin.ChatHub/ChatHubPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BotSharp.Abstraction.Loggers;
using BotSharp.Abstraction.Routing;
using BotSharp.Abstraction.Interpreters.Settings;
using BotSharp.Core.Crontab.Abstraction;
using BotSharp.Plugin.ChatHub.Hooks;
using Microsoft.Extensions.Configuration;
Expand All @@ -18,6 +17,10 @@ public class ChatHubPlugin : IBotSharpPlugin

public void RegisterDI(IServiceCollection services, IConfiguration config)
{
var settings = new ChatHubSettings();
config.Bind("ChatHub", settings);
services.AddSingleton(x => settings);

// Register hooks
services.AddScoped<IConversationHook, ChatHubConversationHook>();
services.AddScoped<IConversationHook, StreamingLogHook>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BotSharp.Plugin.ChatHub.Enums;

public static class EventDispatchType
{
public const string Group = "group";
public const string User = "user";
}
Loading

0 comments on commit 1d8f13d

Please sign in to comment.