Skip to content

Commit 6beaf33

Browse files
authored
Use prod access endpoint first and fall back to the test endpoint when it's not available (#288)
1 parent 722d774 commit 6beaf33

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

shell/agents/Microsoft.Azure.Agent/ChatSession.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ namespace Microsoft.Azure.Agent;
1111

1212
internal class ChatSession : IDisposable
1313
{
14-
// TODO: production URL not yet working for some regions.
15-
// private const string ACCESS_URL = "https://copilotweb.production.portalrp.azure.com/api/access?api-version=2024-09-01";
16-
private const string ACCESS_URL = "https://copilotweb.canary.production.portalrp.azure.com/api/access?api-version=2024-09-01";
14+
private const string PROD_ACCESS_URL = "https://copilotweb.production.portalrp.azure.com/api/access?api-version=2024-09-01";
15+
private const string TEST_ACCESS_URL = "https://copilotweb.canary.production.portalrp.azure.com/api/access?api-version=2024-09-01";
1716
private const string DL_TOKEN_URL = "https://copilotweb.production.portalrp.azure.com/api/conversations/start?api-version=2024-11-15";
1817
private const string CONVERSATION_URL = "https://directline.botframework.com/v3/directline/conversations";
1918

@@ -141,11 +140,13 @@ private async Task<string> SetupNewChat(IStatusContext context, CancellationToke
141140

142141
private async Task CheckAuthorizationAsync(CancellationToken cancellationToken)
143142
{
144-
HttpRequestMessage request = new(HttpMethod.Get, ACCESS_URL);
145-
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
146-
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token);
147-
148-
HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken);
143+
HttpResponseMessage response = await SendRequestAsync(PROD_ACCESS_URL);
144+
if (response.StatusCode is not System.Net.HttpStatusCode.OK)
145+
{
146+
// We fall back to the test endpoint when the prod endpoint is unavailable.
147+
Telemetry.Trace(AzTrace.Exception($"Prod access endpoint unavailable. HTTP status: {response.StatusCode}. Fall back to canary endpoint."));
148+
response = await SendRequestAsync(TEST_ACCESS_URL);
149+
}
149150
await response.EnsureSuccessStatusCodeForTokenRequest("Failed to check Copilot authorization.");
150151

151152
using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken);
@@ -158,6 +159,14 @@ private async Task CheckAuthorizationAsync(CancellationToken cancellationToken)
158159
Telemetry.Trace(AzTrace.Exception(message));
159160
throw new TokenRequestException(message) { UserUnauthorized = true };
160161
}
162+
163+
async Task<HttpResponseMessage> SendRequestAsync(string url)
164+
{
165+
HttpRequestMessage request = new(HttpMethod.Get, url);
166+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
167+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken.Token);
168+
return await _httpClient.SendAsync(request, cancellationToken);
169+
}
161170
}
162171

163172
private async Task GetInitialDLTokenAsync(CancellationToken cancellationToken)

0 commit comments

Comments
 (0)