-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code(API::CreatetChatSession): Implement Create chat session feature
- Loading branch information
1 parent
75d9135
commit b8340f2
Showing
7 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...ver/StellarChat.Server.Api/DAL/Mongo/Exceptions/Chat/ChatSessionAlreadyExistsException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using StellarChat.Shared.Abstractions.Exceptions; | ||
|
||
namespace StellarChat.Server.Api.DAL.Mongo.Exceptions.Chat; | ||
|
||
public class ChatSessionAlreadyExistsException : StellarChatException | ||
{ | ||
public Guid Id { get; } | ||
|
||
public ChatSessionAlreadyExistsException(Guid id) | ||
: base( | ||
message: $"Chat session with Id: '{id}' already exists.", | ||
userMessage: $"A chat session with the same details already exists.") | ||
=> Id = id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Server/StellarChat.Server.Api/Features/Chat/CreateChatSession/CreateChatSession.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
using Mediator; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace StellarChat.Server.Api.Features.Chat.CreateChatSession; | ||
|
||
internal sealed record CreateChatSession([Required] Guid ChatId, string Title) : ICommand; |
35 changes: 35 additions & 0 deletions
35
...erver/StellarChat.Server.Api/Features/Chat/CreateChatSession/CreateChatSessionEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Mapster; | ||
using Mediator; | ||
using Microsoft.AspNetCore.Mvc; | ||
using StellarChat.Shared.Abstractions.API.Endpoints; | ||
using StellarChat.Shared.Abstractions.Contracts.Chat; | ||
|
||
namespace StellarChat.Server.Api.Features.Chat.CreateChatSession; | ||
|
||
internal sealed class CreateChatSessionEndpoint : IEndpoint | ||
{ | ||
public void Expose(IEndpointRouteBuilder endpoints) | ||
{ | ||
var chatHistory = endpoints.MapGroup("/chat-history").WithTags("Chat history"); | ||
|
||
chatHistory.MapPost("/sessions", async ([FromBody] CreateChatSessionRequest request, IMediator mediator) => | ||
{ | ||
var chatId = Guid.NewGuid(); | ||
var command = request.Adapt<CreateChatSession>(); | ||
|
||
command = command with { ChatId = chatId }; | ||
await mediator.Send(command); | ||
|
||
return Results.CreatedAtRoute("GetChatSession", new { ChatId = chatId }, chatId); | ||
}) | ||
.Produces(StatusCodes.Status201Created) | ||
.WithOpenApi(operation => new(operation) | ||
{ | ||
Summary = "Creates a new chat session." | ||
}); | ||
} | ||
|
||
public void Register(IServiceCollection services, IConfiguration configuration) { } | ||
|
||
public void Use(IApplicationBuilder app) { } | ||
} |
42 changes: 42 additions & 0 deletions
42
...Server/StellarChat.Server.Api/Features/Chat/CreateChatSession/CreateChatSessionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Mediator; | ||
using StellarChat.Server.Api.DAL.Mongo.Exceptions.Chat; | ||
using StellarChat.Server.Api.Domain.Chat.Models; | ||
using StellarChat.Server.Api.Domain.Chat.Repositories; | ||
|
||
namespace StellarChat.Server.Api.Features.Chat.CreateChatSession; | ||
|
||
internal class CreateChatSessionHandler : ICommandHandler<CreateChatSession> | ||
{ | ||
private readonly IChatSessionRepository _chatSessionRepository; | ||
private readonly TimeProvider _clock; | ||
private readonly ILogger<CreateChatSessionHandler> _logger; | ||
|
||
public CreateChatSessionHandler(IChatSessionRepository chatSessionRepository, TimeProvider clock, ILogger<CreateChatSessionHandler> logger) | ||
{ | ||
_chatSessionRepository = chatSessionRepository; | ||
_clock = clock; | ||
_logger = logger; | ||
} | ||
|
||
public async ValueTask<Unit> Handle(CreateChatSession command, CancellationToken cancellationToken) | ||
{ | ||
if (await _chatSessionRepository.ExistsAsync(command.ChatId)) | ||
{ | ||
throw new ChatSessionAlreadyExistsException(command.ChatId); | ||
} | ||
|
||
var now = _clock.GetUtcNow(); | ||
|
||
// TODO: Retrieve activePlugins and metaprompt from settings | ||
var activePlugins = new HashSet<string>(); | ||
|
||
var chatSession = ChatSession.Create(command.ChatId, command.Title, metaprompt: "", activePlugins, createdAt: now, updatedAt: now); | ||
|
||
await _chatSessionRepository.AddAsync(chatSession); | ||
_logger.LogInformation($"Chat session with ID: '{chatSession.Id}' has been created."); | ||
|
||
// TODO: Create initial bot message | ||
|
||
return Unit.Value; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Server/StellarChat.Server.Api/StellarChat.Server.Api.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/Shared/StellarChat.Shared.Abstractions/Contracts/Chat/CreateChatSessionRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace StellarChat.Shared.Abstractions.Contracts.Chat; | ||
|
||
public record CreateChatSessionRequest([property: JsonIgnore] Guid? ChatId, string Title); |