Skip to content

Commit

Permalink
Send the sessionName parameter in the request body
Browse files Browse the repository at this point in the history
  • Loading branch information
alistar-andrei committed Aug 8, 2024
1 parent 93d154a commit 6fa032b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/dotnet/Core/Interfaces/ICoreService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public interface ICoreService
/// Creates a new chat session.
/// </summary>
/// <param name="instanceId">The instance Id.</param>
/// <param name="chatSessionName">The name for the chat session.</param>
Task<Session> CreateNewChatSessionAsync(string instanceId, string chatSessionName);
/// <param name="sessionName">The name for the chat session.</param>
Task<Session> CreateNewChatSessionAsync(string instanceId, string sessionName);

/// <summary>
/// Rename the chat session from its default (eg., "New Chat") to the summary provided by OpenAI.
/// </summary>
/// <param name="instanceId">The instance id.</param>
/// <param name="sessionId">The session id to rename.</param>
/// <param name="newChatSessionName">The new name for the chat session.</param>
Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, string newChatSessionName);
/// <param name="sessionName">The new name for the chat session.</param>
Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, string sessionName);

/// <summary>
/// Delete a chat session and related messages.
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/Core/Interfaces/ICosmosDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public interface ICosmosDbService
/// Updates a session's name through a patch operation.
/// </summary>
/// <param name="id">The session id.</param>
/// <param name="name">The session's new name.</param>
/// <param name="sessionName">The session's new name.</param>
/// <param name="cancellationToken">Cancellation token for async calls.</param>
/// <returns>Revised chat session item.</returns>
Task<Session> UpdateSessionNameAsync(string id, string name, CancellationToken cancellationToken = default);
Task<Session> UpdateSessionNameAsync(string id, string sessionName, CancellationToken cancellationToken = default);

/// <summary>
/// Batch create or update chat messages and session.
Expand Down
13 changes: 6 additions & 7 deletions src/dotnet/Core/Services/CoreService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Azure.Core;
using FoundationaLLM.Common.Constants;
using FoundationaLLM.Common.Constants.ResourceProviders;
using FoundationaLLM.Common.Exceptions;
Expand Down Expand Up @@ -66,26 +65,26 @@ public async Task<List<Message>> GetChatSessionMessagesAsync(string instanceId,
}

/// <inheritdoc/>
public async Task<Session> CreateNewChatSessionAsync(string instanceId, string chatSessionName)
public async Task<Session> CreateNewChatSessionAsync(string instanceId, string sessionName)
{
ArgumentException.ThrowIfNullOrEmpty(chatSessionName);
ArgumentException.ThrowIfNullOrEmpty(sessionName);

Session session = new()
{
Name = chatSessionName,
Name = sessionName,
Type = _sessionType,
UPN = _callContext.CurrentUserIdentity?.UPN ?? throw new InvalidOperationException("Failed to retrieve the identity of the signed in user when creating a new chat session.")
};
return await _cosmosDbService.InsertSessionAsync(session);
}

/// <inheritdoc/>
public async Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, string newChatSessionName)
public async Task<Session> RenameChatSessionAsync(string instanceId, string sessionId, string sessionName)
{
ArgumentNullException.ThrowIfNull(sessionId);
ArgumentException.ThrowIfNullOrEmpty(newChatSessionName);
ArgumentException.ThrowIfNullOrEmpty(sessionName);

return await _cosmosDbService.UpdateSessionNameAsync(sessionId, newChatSessionName);
return await _cosmosDbService.UpdateSessionNameAsync(sessionId, sessionName);
}

/// <inheritdoc/>
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/Core/Services/CosmosDbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,14 @@ public async Task<Session> UpdateSessionAsync(Session session, CancellationToken
}

/// <inheritdoc/>
public async Task<Session> UpdateSessionNameAsync(string id, string name, CancellationToken cancellationToken = default)
public async Task<Session> UpdateSessionNameAsync(string id, string sessionName, CancellationToken cancellationToken = default)
{
var response = await _sessions.PatchItemAsync<Session>(
id: id,
partitionKey: new PartitionKey(id),
patchOperations: new[]
{
PatchOperation.Set("/name", name),
PatchOperation.Set("/name", sessionName),
},
cancellationToken: cancellationToken
);
Expand Down
12 changes: 6 additions & 6 deletions src/dotnet/CoreAPI/Controllers/SessionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ public async Task<CompletionPrompt> GetCompletionPrompt(string instanceId, strin
/// Creates a new chat session.
/// </summary>
/// <param name="instanceId">The id of the instance.</param>
/// <param name="chatSessionName">The name for the chat session.</param>
/// <param name="sessionName">The name for the chat session.</param>
[HttpPost(Name = "CreateNewChatSession")]
public async Task<Session> CreateNewChatSession(string instanceId, string chatSessionName) =>
await _coreService.CreateNewChatSessionAsync(instanceId, chatSessionName);
public async Task<Session> CreateNewChatSession(string instanceId, [FromBody] string sessionName) =>
await _coreService.CreateNewChatSessionAsync(instanceId, sessionName);

/// <summary>
/// Rename the chat session.
/// </summary>
/// <param name="instanceId">The id of the instance.</param>
/// <param name="sessionId">The id of the session to rename.</param>
/// <param name="newChatSessionName">The new name for the session.</param>
/// <param name="sessionName">The new name for the session.</param>
[HttpPost("{sessionId}/rename", Name = "RenameChatSession")]
public async Task<Session> RenameChatSession(string instanceId, string sessionId, string newChatSessionName) =>
await _coreService.RenameChatSessionAsync(instanceId, sessionId, newChatSessionName);
public async Task<Session> RenameChatSession(string instanceId, string sessionId, [FromBody] string sessionName) =>
await _coreService.RenameChatSessionAsync(instanceId, sessionId, sessionName);

/// <summary>
/// Delete a chat session and related messages.
Expand Down
12 changes: 8 additions & 4 deletions src/dotnet/CoreClient/Clients/RESTClients/SessionRESTClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Azure.Core;
using FoundationaLLM.Client.Core.Interfaces;
using FoundationaLLM.Common.Models.Chat;
using System.Text.Encodings.Web;
using System.Net.Http.Json;
using System.Text.Json;

namespace FoundationaLLM.Client.Core.Clients.RESTClients
Expand All @@ -17,10 +17,12 @@ internal class SessionRESTClient(
private readonly string _instanceId = instanceId ?? throw new ArgumentNullException(nameof(instanceId));

/// <inheritdoc/>
public async Task<string> CreateSessionAsync(string chatSessionName)
public async Task<string> CreateSessionAsync(string sessionName)
{
var coreClient = await GetCoreClientAsync();
var responseSession = await coreClient.PostAsync($"instances/{_instanceId}/sessions?chatSessionName={chatSessionName}", null);
var responseSession = await coreClient.PostAsync(
$"instances/{_instanceId}/sessions",
JsonContent.Create(sessionName));

if (responseSession.IsSuccessStatusCode)
{
Expand All @@ -39,7 +41,9 @@ public async Task<string> CreateSessionAsync(string chatSessionName)
public async Task<string> RenameChatSession(string sessionId, string sessionName)
{
var coreClient = await GetCoreClientAsync();
var response = await coreClient.PostAsync($"instances/{_instanceId}/sessions/{sessionId}/rename?newChatSessionName={UrlEncoder.Default.Encode(sessionName)}", null);
var response = await coreClient.PostAsync(
$"instances/{_instanceId}/sessions/{sessionId}/rename",
JsonContent.Create(sessionName));

if (response.IsSuccessStatusCode)
{
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/CoreClient/Interfaces/ISessionRESTClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public interface ISessionRESTClient
/// <summary>
/// Creates a new session with the specified name.
/// </summary>
/// <param name="chatSessionName">The name for the chat session.</param>
/// <param name="sessionName">The name for the chat session.</param>
/// <returns>Returns the new Session ID.</returns>
Task<string> CreateSessionAsync(string chatSessionName);
Task<string> CreateSessionAsync(string sessionName);

/// <summary>
/// Renames a chat session.
Expand Down
12 changes: 6 additions & 6 deletions tests/dotnet/Core.Tests/Services/CoreServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public async Task CreateNewChatSessionAsync_ShouldReturnANewChatSession()
// Arrange
var currentUserUPN = "[email protected]";
var sessionType = "Test_type";
var chatSessionName = "Test_name";
var newSession = new Session { Name = chatSessionName, Type = sessionType, UPN = currentUserUPN };
var sessionName = "Test_name";
var newSession = new Session { Name = sessionName, Type = sessionType, UPN = currentUserUPN };

// Set up mock returns
_callContext.CurrentUserIdentity.Returns(new UnifiedUserIdentity { UPN = currentUserUPN });
Expand All @@ -133,13 +133,13 @@ public async Task CreateNewChatSessionAsync_ShouldReturnANewChatSession()
.Returns(Task.FromResult(newSession));

// Act
var resultSession = await _testedService.CreateNewChatSessionAsync(_instanceId, chatSessionName);
var resultSession = await _testedService.CreateNewChatSessionAsync(_instanceId, sessionName);

// Assert
Assert.NotNull(resultSession);
Assert.Equal(sessionType, resultSession.Type);
Assert.Equal(currentUserUPN, resultSession.UPN);
Assert.Equal(chatSessionName, resultSession.Name);
Assert.Equal(sessionName, resultSession.Name);
}

#endregion
Expand Down Expand Up @@ -176,12 +176,12 @@ public async Task RenameChatSessionAsync_ShouldReturnTheRenamedChatSession()
public async Task RenameChatSessionAsync_ShouldThrowExceptionWhenSessionIdIsNull()
{
// Arrange
var newChatSessionName = "NewName";
var sessionName = "NewName";

// Assert
await Assert.ThrowsAsync<ArgumentNullException>(async () =>
{
await _testedService.RenameChatSessionAsync(_instanceId, null!, newChatSessionName);
await _testedService.RenameChatSessionAsync(_instanceId, null!, sessionName);
});
}

Expand Down

0 comments on commit 6fa032b

Please sign in to comment.