Skip to content

Commit 6a2343c

Browse files
committed
SignalR
1 parent f12f6c6 commit 6a2343c

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentRole.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,21 @@ namespace BotSharp.Abstraction.Agents.Enums;
33
public class AgentRole
44
{
55
public const string System = "system";
6+
7+
/// <summary>
8+
/// AI Assistant
9+
/// </summary>
610
public const string Assistant = "assistant";
11+
12+
/// <summary>
13+
/// Client
14+
/// </summary>
715
public const string User = "user";
16+
817
public const string Function = "function";
18+
19+
/// <summary>
20+
/// Customer service representative (CSR)
21+
/// </summary>
22+
public const string CSR = "csr";
923
}

src/Infrastructure/BotSharp.Abstraction/Planning/IPlaner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace BotSharp.Abstraction.Planning;
44

55
/// <summary>
66
/// Task breakdown and execution plan
7+
/// https://www.promptingguide.ai/techniques/cot
78
/// </summary>
89
public interface IPlaner
910
{

src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
14+
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
1415
</ItemGroup>
1516

1617
<ItemGroup>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.AspNetCore.SignalR;
2+
3+
namespace BotSharp.OpenAPI;
4+
5+
public class ChatHub : Hub
6+
{
7+
private readonly IServiceProvider _services;
8+
private readonly ILogger _logger;
9+
private readonly IUserIdentity _user;
10+
11+
public ChatHub(IServiceProvider services,
12+
ILogger<ChatHub> logger,
13+
IUserIdentity user)
14+
{
15+
_services = services;
16+
_logger = logger;
17+
_user = user;
18+
}
19+
20+
public async Task OnMessageReceivedFromClient(string message)
21+
{
22+
// await Clients.User(_user.Id).SendAsync("ReceiveMessage", message);
23+
}
24+
25+
/// <summary>
26+
/// Received message from client
27+
/// </summary>
28+
/// <param name="user"></param>
29+
/// <param name="message"></param>
30+
/// <returns></returns>
31+
public async Task SendMessage(string user, string message)
32+
{
33+
// await Clients.User(_user.Id).SendAsync("ReceiveMessage", message);
34+
}
35+
36+
public async Task SendMessageToCaller(string user, string message)
37+
=> await Clients.Caller.SendAsync("ReceiveMessage", user, message);
38+
39+
public async Task SendMessageToGroup(string user, string message)
40+
=> await Clients.Group("SignalR Users").SendAsync("ReceiveMessage", user, message);
41+
42+
public override Task OnConnectedAsync()
43+
{
44+
Console.WriteLine($"ChatHub: {""} connected in {Context.ConnectionId}");
45+
return base.OnConnectedAsync();
46+
}
47+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using BotSharp.Abstraction.Agents.Enums;
2+
using BotSharp.Abstraction.ApiAdapters;
3+
using BotSharp.Abstraction.Conversations.Models;
4+
using BotSharp.OpenAPI.ViewModels.Conversations;
5+
using Microsoft.AspNetCore.SignalR;
6+
7+
namespace BotSharp.OpenAPI.Controllers;
8+
9+
[Authorize]
10+
[ApiController]
11+
public class ChatHubController : ControllerBase, IApiAdapter
12+
{
13+
private readonly IServiceProvider _services;
14+
private readonly ILogger _logger;
15+
private readonly IHubContext<ChatHub> _chatHub;
16+
private readonly IUserIdentity _user;
17+
18+
public ChatHubController(IServiceProvider services,
19+
ILogger<ChatHubController> logger,
20+
IHubContext<ChatHub> chatHub,
21+
IUserIdentity user)
22+
{
23+
_services = services;
24+
_logger = logger;
25+
_chatHub = chatHub;
26+
_user = user;
27+
}
28+
29+
[HttpPost("/chat-hub/client/{agentId}/{conversationId}")]
30+
public async Task OnMessageReceivedFromClient([FromRoute] string agentId,
31+
[FromRoute] string conversationId,
32+
[FromBody] NewMessageModel input)
33+
{
34+
var conv = _services.GetRequiredService<IConversationService>();
35+
conv.SetConversationId(conversationId, input.States);
36+
conv.States.SetState("channel", input.Channel);
37+
38+
var response = new MessageResponseModel();
39+
var inputMsg = new RoleDialogModel("user", input.Text);
40+
await conv.SendMessage(agentId, inputMsg,
41+
async msg =>
42+
{
43+
response.Text = msg.Content;
44+
response.Function = msg.FunctionName;
45+
response.RichContent = msg.RichContent;
46+
response.Instruction = msg.Instruction;
47+
response.Data = msg.Data;
48+
},
49+
async fnExecuting =>
50+
{
51+
52+
},
53+
async fnExecuted =>
54+
{
55+
56+
});
57+
58+
var state = _services.GetRequiredService<IConversationStateService>();
59+
response.States = state.GetStates();
60+
response.MessageId = inputMsg.MessageId;
61+
62+
// Update console conversation UI for CSR
63+
await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromClient", new RoleDialogModel(AgentRole.User, input.Text)
64+
{
65+
66+
});
67+
68+
await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromAssistant", new RoleDialogModel(AgentRole.Assistant, response.Text)
69+
{
70+
71+
});
72+
}
73+
74+
[HttpPost("/chat-hub/csr/{agentId}/{conversationId}")]
75+
public async Task OnMessageReceivedFromCsr([FromRoute] string agentId,
76+
[FromRoute] string conversationId,
77+
[FromBody] NewMessageModel input)
78+
{
79+
// Update console conversation UI for User
80+
await _chatHub.Clients.All.SendAsync("OnMessageReceivedFromCsr", new RoleDialogModel(AgentRole.CSR, input.Text)
81+
{
82+
83+
});
84+
}
85+
}

0 commit comments

Comments
 (0)