Skip to content

Commit

Permalink
Code(API::CreateAssistant): Implement create assistant feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutak1337 committed May 8, 2024
1 parent 8820eca commit 411cbcc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace StellarChat.Server.Api.Features.Assistants.CreateAssistant;

internal sealed record CreateAssistant(
[Required] Guid Id,
string Name,
string Metaprompt,
string Description,
string AvatarUrl,
string DefaultModel,
string DefaultVoice,
bool IsDefault) : ICommand;
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using StellarChat.Server.Api.Features.Chat.CreateChatSession;

namespace StellarChat.Server.Api.Features.Assistants.CreateAssistant;

internal sealed class CreateAssistantEndpoint : IEndpoint
{
public void Expose(IEndpointRouteBuilder endpoints)
{
var assistants = endpoints.MapGroup("/assistants").WithTags("Assistants");

assistants.MapPost("", async ([FromBody] CreateAssistantRequest request, IMediator mediator) =>
{
var id = Guid.NewGuid();
var command = request.Adapt<CreateAssistant>();

command = command with { Id = id };
await mediator.Send(command);

return Results.CreatedAtRoute("GetAssistant", new { Id = id }, id);
})
.Produces(StatusCodes.Status201Created)
.Produces(StatusCodes.Status400BadRequest)
.WithOpenApi(operation => new(operation)
{
Summary = "Creates a new assistant."
});
}

public void Register(IServiceCollection services, IConfiguration configuration) { }

public void Use(IApplicationBuilder app) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace StellarChat.Server.Api.Features.Assistants.CreateAssistant;

internal sealed class CreateAssistantHandler : ICommandHandler<CreateAssistant>
{
private readonly IAssistantRepository _assistantRepository;
private readonly TimeProvider _clock;
private readonly ILogger<CreateAssistantHandler> _logger;

public CreateAssistantHandler(IAssistantRepository assistantRepository, TimeProvider clock, ILogger<CreateAssistantHandler> logger)
{
_assistantRepository = assistantRepository;
_clock = clock;
_logger = logger;
}

public async ValueTask<Unit> Handle(CreateAssistant command, CancellationToken cancellationToken)
{
if (await _assistantRepository.ExistsAsync(command.Id))
{
throw new AssistantAlreadyExistsException(command.Id);
}

var now = _clock.GetUtcNow();

if (command.IsDefault)
{
await UnsetPreviousDefaultAssistant();
}

var assistant = Assistant.Create(
command.Id,
command.Name,
command.Metaprompt,
command.Description,
command.AvatarUrl,
command.DefaultModel,
command.DefaultVoice,
command.IsDefault,
createdAt: now,
updatedAt: now);

await _assistantRepository.AddAsync(assistant);
_logger.LogInformation($"Assistant with ID: '{assistant.Id}' has been created.");

return Unit.Value;
}

private async Task UnsetPreviousDefaultAssistant()
{
var assistants = await _assistantRepository.BrowseAsync();
var currentDefaultAssistant = assistants.SingleOrDefault(assistant => assistant.IsDefault);

if (currentDefaultAssistant is not null)
{
currentDefaultAssistant.IsDefault = false;
await _assistantRepository.UpdateAsync(currentDefaultAssistant);
}
}
}

0 comments on commit 411cbcc

Please sign in to comment.