Skip to content

Commit 798224a

Browse files
resolve revies
1 parent 331181f commit 798224a

File tree

6 files changed

+35
-84
lines changed

6 files changed

+35
-84
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
namespace SharedWebComponents.Models;
4+
5+
internal class StreamingMessage
6+
{
7+
public string Type { get; set; } = "";
8+
public object? Content { get; set; }
9+
}

app/SharedWebComponents/Pages/Chat.razor.cs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SharedWebComponents.Pages;
44
using Microsoft.AspNetCore.SignalR.Client;
55
using System.Text.Json;
6+
using SharedWebComponents.Models;
67

78
public sealed partial class Chat : IAsyncDisposable
89
{
@@ -250,28 +251,6 @@ private async Task OnAskQuestionAsync(string question)
250251
await OnAskClickedAsync();
251252
}
252253

253-
private async Task OnStreamingToggled(bool isEnabled)
254-
{
255-
_useStreaming = isEnabled;
256-
if (isEnabled)
257-
{
258-
await ConnectToHub();
259-
}
260-
else
261-
{
262-
await DisconnectFromHub();
263-
}
264-
}
265-
266-
private async Task DisconnectFromHub()
267-
{
268-
if (_hubConnection is not null)
269-
{
270-
await _hubConnection.DisposeAsync();
271-
_hubConnection = null;
272-
}
273-
}
274-
275254
private void UpdateAnswerInMap(string answer, string? citationBaseUrl = null)
276255
{
277256
var currentResponse = _questionAndAnswerMap[_currentQuestion];
@@ -354,10 +333,4 @@ private void UpdateImagesInMap(SupportingImageRecord[] images)
354333
});
355334
}
356335
}
357-
}
358-
359-
internal class StreamingMessage
360-
{
361-
public string Type { get; set; } = "";
362-
public object? Content { get; set; }
363-
}
336+
}

app/backend/Extensions/WebApplicationExtensions.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ internal static WebApplication MapApi(this WebApplication app)
1616
// Long-form chat w/ contextual history endpoint
1717
api.MapPost("chat", OnPostChatAsync);
1818

19-
api.MapPost("chat/stream", OnPostChatStreamingAsync);
20-
2119
// Upload a document
2220
api.MapPost("documents", OnPostDocumentAsync);
2321

@@ -93,24 +91,6 @@ private static async Task<IResult> OnPostChatAsync(
9391
return Results.BadRequest();
9492
}
9593

96-
private static async Task<IResult> OnPostChatStreamingAsync(
97-
ChatRequest request,
98-
ReadRetrieveReadChatService chatService,
99-
CancellationToken cancellationToken)
100-
{
101-
if (request is { History.Length: > 0 })
102-
{
103-
await chatService.ReplyStreamingAsync(
104-
request.History,
105-
request.Overrides,
106-
request.ConnectionId,
107-
cancellationToken);
108-
109-
return TypedResults.Ok();
110-
}
111-
return Results.BadRequest();
112-
}
113-
11494
private static async Task<IResult> OnPostDocumentAsync(
11595
[FromForm] IFormFileCollection files,
11696
[FromServices] AzureBlobStorageService service,

app/backend/Hubs/ChatHub.cs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.SignalR;
2+
using System;
23

34
namespace MinimalApi.Hubs;
45

@@ -9,32 +10,14 @@ public class ChatHub : Hub
910

1011
public ChatHub(ReadRetrieveReadChatService chatService)
1112
{
12-
_chatService = chatService;
13+
_chatService = chatService ?? throw new ArgumentNullException(nameof(chatService));
1314
}
1415

1516
public async Task SendChatRequest(ChatRequest request)
1617
{
17-
try
18-
{
19-
request.ConnectionId = Context.ConnectionId;
20-
await _chatService.ReplyStreamingAsync(
21-
request.History,
22-
request.Overrides,
23-
request.ConnectionId);
24-
}
25-
catch
26-
{
27-
throw;
28-
}
29-
}
30-
31-
public override async Task OnConnectedAsync()
32-
{
33-
await base.OnConnectedAsync();
34-
}
35-
36-
public override async Task OnDisconnectedAsync(Exception? exception)
37-
{
38-
await base.OnDisconnectedAsync(exception);
18+
await _chatService.ReplyStreamingAsync(
19+
request.History,
20+
request.Overrides,
21+
Context.ConnectionId);
3922
}
4023
}

app/backend/Program.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,24 @@
2323
{
2424
static string? GetEnvVar(string key) => Environment.GetEnvironmentVariable(key);
2525

26-
var endpoint = GetEnvVar("AZURE_SIGNALR_ENDPOINT")
27-
?? throw new InvalidOperationException("AZURE_SIGNALR_ENDPOINT is not configured");
28-
var clientId = GetEnvVar("AZURE_CLIENT_ID")
29-
?? throw new InvalidOperationException("AZURE_CLIENT_ID is not configured");
30-
31-
options.Endpoints = new[]
32-
{
33-
new ServiceEndpoint(new Uri(endpoint), new ManagedIdentityCredential(clientId))
34-
};
26+
// Try to get endpoint and client ID first
27+
var endpoint = GetEnvVar("AZURE_SIGNALR_ENDPOINT");
28+
var clientId = GetEnvVar("AZURE_CLIENT_ID");
29+
30+
if (endpoint != null && clientId != null)
31+
{
32+
options.Endpoints = new[]
33+
{
34+
new ServiceEndpoint(new Uri(endpoint), new ManagedIdentityCredential(clientId))
35+
};
36+
}
37+
else
38+
{
39+
// Fall back to connection string
40+
var connectionString = GetEnvVar("AZURE_SIGNALR_CONNECTION_STRING")
41+
?? throw new InvalidOperationException("Neither managed identity credentials nor connection string are configured for Azure SignalR");
42+
options.ConnectionString = connectionString;
43+
}
3544
});
3645

3746
if (builder.Environment.IsDevelopment())

app/shared/Shared/Models/ChatRequest.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,4 @@ public record class ChatRequest(
1010
) : ApproachRequest(Approach.RetrieveThenRead)
1111
{
1212
public string? LastUserQuestion => History?.Last(m => m.Role == "user")?.Content;
13-
14-
[JsonPropertyName("connectionId")]
15-
public string? ConnectionId { get; set; }
1613
}

0 commit comments

Comments
 (0)