2023-08-19 00:15:02 +00:00
|
|
|
using BotSharp.Abstraction.ApiAdapters;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
|
|
|
|
using BotSharp.OpenAPI.ViewModels.Conversations;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.OpenAPI.Controllers;
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class ConversationController : ControllerBase, IApiAdapter
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IUserIdentity _user;
|
|
|
|
|
|
|
|
|
|
public ConversationController(IServiceProvider services,
|
|
|
|
|
IUserIdentity user)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
_user = user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/conversation/{agentId}")]
|
|
|
|
|
public async Task<ConversationViewModel> NewConversation([FromRoute] string agentId)
|
|
|
|
|
{
|
|
|
|
|
var service = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
var sess = new Conversation
|
|
|
|
|
{
|
|
|
|
|
AgentId = agentId
|
|
|
|
|
};
|
|
|
|
|
sess = await service.NewConversation(sess);
|
|
|
|
|
return ConversationViewModel.FromSession(sess);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("/conversation/{agentId}/{conversationId}")]
|
|
|
|
|
public async Task DeleteConversation([FromRoute] string agentId, [FromRoute] string conversationId)
|
|
|
|
|
{
|
|
|
|
|
var service = _services.GetRequiredService<IConversationService>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("/conversation/{agentId}/{conversationId}")]
|
|
|
|
|
public async Task<MessageResponseModel> SendMessage([FromRoute] string agentId,
|
|
|
|
|
[FromRoute] string conversationId,
|
2023-09-09 15:37:38 +00:00
|
|
|
[FromBody] NewMessageModel input)
|
2023-08-19 00:15:02 +00:00
|
|
|
{
|
|
|
|
|
var conv = _services.GetRequiredService<IConversationService>();
|
2023-09-14 01:41:51 +00:00
|
|
|
conv.SetConversationId(conversationId, input.States);
|
2023-09-14 16:42:48 +00:00
|
|
|
conv.States.SetState("channel", input.Channel)
|
2023-09-18 08:35:02 +00:00
|
|
|
.SetState("provider", input.Provider)
|
|
|
|
|
.SetState("model", input.Model)
|
|
|
|
|
.SetState("temperature", input.Temperature)
|
|
|
|
|
.SetState("sampling_factor", input.SamplingFactor);
|
2023-09-14 01:41:51 +00:00
|
|
|
|
2023-08-19 00:15:02 +00:00
|
|
|
var response = new MessageResponseModel();
|
|
|
|
|
var stackMsg = new List<RoleDialogModel>();
|
|
|
|
|
|
2023-09-06 03:19:36 +00:00
|
|
|
await conv.SendMessage(agentId,
|
2023-09-14 01:41:51 +00:00
|
|
|
new RoleDialogModel("user", input.Text),
|
2023-08-19 00:15:02 +00:00
|
|
|
async msg =>
|
2023-08-20 19:02:59 +00:00
|
|
|
{
|
|
|
|
|
stackMsg.Add(msg);
|
|
|
|
|
},
|
|
|
|
|
async fnExecuting =>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
async fnExecuted =>
|
|
|
|
|
{
|
2023-08-20 20:20:16 +00:00
|
|
|
response.Function = fnExecuted.FunctionName;
|
|
|
|
|
response.Data = fnExecuted.ExecutionData;
|
2023-08-20 19:02:59 +00:00
|
|
|
});
|
2023-08-19 00:15:02 +00:00
|
|
|
|
|
|
|
|
response.Text = string.Join("\r\n", stackMsg.Select(x => x.Content));
|
2023-09-22 20:38:58 +00:00
|
|
|
response.Data = response.Data ?? stackMsg.Last().ExecutionData;
|
|
|
|
|
|
2023-08-19 00:15:02 +00:00
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
}
|