BotSharp/src/Infrastructure/BotSharp.Core/Conversations/ConversationController.cs

71 lines
2.1 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.ApiAdapters;
2023-06-23 04:43:00 +00:00
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Core.Conversations.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BotSharp.Core.Conversations;
[Authorize]
[ApiController]
public class ConversationController : ControllerBase, IApiAdapter
{
private readonly IServiceProvider _services;
2023-06-23 04:43:00 +00:00
private readonly IUserIdentity _user;
2023-06-23 04:43:00 +00:00
public ConversationController(IServiceProvider services,
IUserIdentity user)
{
_services = services;
2023-06-23 04:43:00 +00:00
_user = user;
}
2023-06-23 04:43:00 +00:00
[HttpPost("/conversation/{agentId}")]
public async Task<SessionViewModel> NewSession([FromRoute] string agentId)
{
var service = _services.GetRequiredService<ISessionService>();
2023-06-23 04:43:00 +00:00
var sess = new Session
{
AgentId = agentId
};
sess = await service.NewSession(sess);
return SessionViewModel.FromSession(sess);
}
2023-06-23 04:43:00 +00:00
[HttpDelete("/conversation/{agentId}/{sessionId}")]
public async Task DeleteSession([FromRoute] string agentId, [FromRoute] string sessionId)
{
var service = _services.GetRequiredService<ISessionService>();
}
2023-06-13 03:27:31 +00:00
2023-06-23 04:43:00 +00:00
[HttpPost("/conversation/{agentId}/{sessionId}")]
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
[FromRoute] string sessionId,
[FromBody] NewMessageModel input)
2023-06-13 03:27:31 +00:00
{
var transmitter = _services.GetRequiredService<IContentTransfer>();
var container = new ContentContainer
{
2023-06-23 04:43:00 +00:00
AgentId = agentId,
SessionId = sessionId,
2023-06-13 03:27:31 +00:00
Conversations = new List<RoleDialogModel>
{
new RoleDialogModel
{
Role = "user",
2023-06-23 04:43:00 +00:00
Text = input.Text
2023-06-13 03:27:31 +00:00
}
2023-06-23 04:43:00 +00:00
},
UserId = _user.Id
2023-06-13 03:27:31 +00:00
};
var result = await transmitter.Transport(container);
return new MessageResponseModel
{
2023-06-23 04:43:00 +00:00
Content = result.IsSuccess ? container.Output.Text : result.Messages.First()
2023-06-13 03:27:31 +00:00
};
}
}