From 46cf1e738b68aaee94a0f57860f02e7f41588584 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 11 Nov 2024 16:08:21 -0800 Subject: [PATCH 1/5] Use prod access endpoint first and fall back to the test endpoint when it's not available --- shell/agents/Microsoft.Azure.Agent/ChatSession.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs index 2ed666b3..b70f9a15 100644 --- a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs +++ b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs @@ -11,9 +11,8 @@ namespace Microsoft.Azure.Agent; internal class ChatSession : IDisposable { - // TODO: production URL not yet working for some regions. - // private const string ACCESS_URL = "https://copilotweb.production.portalrp.azure.com/api/access?api-version=2024-09-01"; - private const string ACCESS_URL = "https://copilotweb.canary.production.portalrp.azure.com/api/access?api-version=2024-09-01"; + private const string PROD_ACCESS_URL = "https://copilotweb.production.portalrp.azure.com/api/access?api-version=2024-09-01"; + private const string TEST_ACCESS_URL = "https://copilotweb.canary.production.portalrp.azure.com/api/access?api-version=2024-09-01"; private const string DL_TOKEN_URL = "https://copilotweb.production.portalrp.azure.com/api/conversations/start?api-version=2024-11-15"; private const string CONVERSATION_URL = "https://directline.botframework.com/v3/directline/conversations"; @@ -141,11 +140,17 @@ private async Task SetupNewChat(IStatusContext context, CancellationToke private async Task CheckAuthorizationAsync(CancellationToken cancellationToken) { - HttpRequestMessage request = new(HttpMethod.Get, ACCESS_URL); + HttpRequestMessage request = new(HttpMethod.Get, PROD_ACCESS_URL); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token); HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken); + if (response.StatusCode is System.Net.HttpStatusCode.Forbidden) + { + // We fall back to the test endpoint when the prod endpoint is unavailable. + request.RequestUri = new Uri(TEST_ACCESS_URL, UriKind.RelativeOrAbsolute); + response = await _httpClient.SendAsync(request, cancellationToken); + } await response.EnsureSuccessStatusCodeForTokenRequest("Failed to check Copilot authorization."); using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken); From 9053951592ed32a060475315c2003f5887e56b75 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 11 Nov 2024 16:20:22 -0800 Subject: [PATCH 2/5] Fix a problem --- .../agents/Microsoft.Azure.Agent/ChatSession.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs index b70f9a15..46678795 100644 --- a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs +++ b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs @@ -140,16 +140,11 @@ private async Task SetupNewChat(IStatusContext context, CancellationToke private async Task CheckAuthorizationAsync(CancellationToken cancellationToken) { - HttpRequestMessage request = new(HttpMethod.Get, PROD_ACCESS_URL); - request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token); - - HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken); + HttpResponseMessage response = await SendRequestAsync(PROD_ACCESS_URL); if (response.StatusCode is System.Net.HttpStatusCode.Forbidden) { // We fall back to the test endpoint when the prod endpoint is unavailable. - request.RequestUri = new Uri(TEST_ACCESS_URL, UriKind.RelativeOrAbsolute); - response = await _httpClient.SendAsync(request, cancellationToken); + response = await SendRequestAsync(TEST_ACCESS_URL); } await response.EnsureSuccessStatusCodeForTokenRequest("Failed to check Copilot authorization."); @@ -163,6 +158,14 @@ private async Task CheckAuthorizationAsync(CancellationToken cancellationToken) Telemetry.Trace(AzTrace.Exception(message)); throw new TokenRequestException(message) { UserUnauthorized = true }; } + + async Task SendRequestAsync(string url) + { + HttpRequestMessage request = new(HttpMethod.Get, url); + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token); + return await _httpClient.SendAsync(request, cancellationToken); + } } private async Task GetInitialDLTokenAsync(CancellationToken cancellationToken) From 7e03908bf1c9e12863095186833d5b4daad6c933 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 11 Nov 2024 16:30:05 -0800 Subject: [PATCH 3/5] Add a telemetry when we fall back to canary endpoint --- shell/agents/Microsoft.Azure.Agent/ChatSession.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs index 46678795..cbd6ddf1 100644 --- a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs +++ b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs @@ -145,6 +145,7 @@ private async Task CheckAuthorizationAsync(CancellationToken cancellationToken) { // We fall back to the test endpoint when the prod endpoint is unavailable. response = await SendRequestAsync(TEST_ACCESS_URL); + Telemetry.Trace(AzTrace.Exception("Prod access endpoint not working. Fall back to canary endpoint.")); } await response.EnsureSuccessStatusCodeForTokenRequest("Failed to check Copilot authorization."); From 29ec1c565d7216a4cbfd3f68292974e71cdfc5e1 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 12 Nov 2024 10:12:19 -0800 Subject: [PATCH 4/5] Fall back to canary when prod returns an unsuccessful response --- shell/agents/Microsoft.Azure.Agent/ChatSession.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs index cbd6ddf1..b6b69cb0 100644 --- a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs +++ b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs @@ -141,11 +141,11 @@ private async Task SetupNewChat(IStatusContext context, CancellationToke private async Task CheckAuthorizationAsync(CancellationToken cancellationToken) { HttpResponseMessage response = await SendRequestAsync(PROD_ACCESS_URL); - if (response.StatusCode is System.Net.HttpStatusCode.Forbidden) + if (response.StatusCode is not System.Net.HttpStatusCode.OK) { // We fall back to the test endpoint when the prod endpoint is unavailable. response = await SendRequestAsync(TEST_ACCESS_URL); - Telemetry.Trace(AzTrace.Exception("Prod access endpoint not working. Fall back to canary endpoint.")); + Telemetry.Trace(AzTrace.Exception($"Prod access endpoint unavailable. HTTP status: {response.StatusCode}. Fall back to canary endpoint.")); } await response.EnsureSuccessStatusCodeForTokenRequest("Failed to check Copilot authorization."); From 5ed5ad7bb86c7c75165822ed8e5d21e8168b4cf9 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 12 Nov 2024 10:41:07 -0800 Subject: [PATCH 5/5] Send telemetry first --- shell/agents/Microsoft.Azure.Agent/ChatSession.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs index b6b69cb0..f7d69b67 100644 --- a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs +++ b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs @@ -144,8 +144,8 @@ private async Task CheckAuthorizationAsync(CancellationToken cancellationToken) if (response.StatusCode is not System.Net.HttpStatusCode.OK) { // We fall back to the test endpoint when the prod endpoint is unavailable. - response = await SendRequestAsync(TEST_ACCESS_URL); Telemetry.Trace(AzTrace.Exception($"Prod access endpoint unavailable. HTTP status: {response.StatusCode}. Fall back to canary endpoint.")); + response = await SendRequestAsync(TEST_ACCESS_URL); } await response.EnsureSuccessStatusCodeForTokenRequest("Failed to check Copilot authorization.");