From f301c440e5b78b61a62910848f41fe5fb0b944b6 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Mon, 18 Nov 2024 15:41:42 -0800 Subject: [PATCH] Use the DirectLine base URL returned from the 'start-conversation' API (#306) --- shell/agents/Microsoft.Azure.Agent/ChatSession.cs | 13 ++++++++----- shell/agents/Microsoft.Azure.Agent/Command.cs | 4 ++++ shell/agents/Microsoft.Azure.Agent/Schema.cs | 8 ++++---- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs index 37b829e4..448b18a2 100644 --- a/shell/agents/Microsoft.Azure.Agent/ChatSession.cs +++ b/shell/agents/Microsoft.Azure.Agent/ChatSession.cs @@ -14,11 +14,11 @@ internal class ChatSession : IDisposable 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"; internal bool UserAuthorized { get; private set; } private string _streamUrl; + private string _dlBaseUrl; private string _conversationId; private string _conversationUrl; private UserDirectLineToken _directLineToken; @@ -101,6 +101,7 @@ internal async Task RefreshAsync(IStatusContext context, bool force, Can private void Reset() { _streamUrl = null; + _dlBaseUrl = null; _conversationId = null; _conversationUrl = null; _directLineToken = null; @@ -180,12 +181,14 @@ private async Task GetInitialDLTokenAsync(CancellationToken cancellationToken) using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken); var dlToken = JsonSerializer.Deserialize(stream, Utils.JsonOptions); - _directLineToken = new UserDirectLineToken(dlToken.DirectLine.Token, dlToken.DirectLine.TokenExpiryTimeInSeconds); + + _dlBaseUrl = dlToken.DirectLine.Endpoint; + _directLineToken = new UserDirectLineToken(dlToken.DirectLine.Token, dlToken.DirectLine.TokenExpiryTimeInSeconds, _dlBaseUrl); } private async Task OpenConversationAsync(CancellationToken cancellationToken) { - HttpRequestMessage request = new(HttpMethod.Post, CONVERSATION_URL); + HttpRequestMessage request = new(HttpMethod.Post, $"{_dlBaseUrl}/conversations"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _directLineToken.Token); HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken); @@ -195,8 +198,8 @@ private async Task OpenConversationAsync(CancellationToken cancellationT SessionPayload spl = JsonSerializer.Deserialize(content, Utils.JsonOptions); _conversationId = spl.ConversationId; - _conversationUrl = $"{CONVERSATION_URL}/{_conversationId}/activities"; - _directLineToken = new UserDirectLineToken(spl.Token, spl.ExpiresIn); + _conversationUrl = $"{_dlBaseUrl}/conversations/{_conversationId}/activities"; + _directLineToken = new UserDirectLineToken(spl.Token, spl.ExpiresIn, _dlBaseUrl); _streamUrl = spl.StreamUrl; _copilotReceiver = await AzureCopilotReceiver.CreateAsync(_streamUrl); diff --git a/shell/agents/Microsoft.Azure.Agent/Command.cs b/shell/agents/Microsoft.Azure.Agent/Command.cs index fe22699f..06784df6 100644 --- a/shell/agents/Microsoft.Azure.Agent/Command.cs +++ b/shell/agents/Microsoft.Azure.Agent/Command.cs @@ -58,6 +58,10 @@ private void ReplaceAction() { host.WriteErrorLine("No AI response available."); } + else if (cr.TopicName is CopilotActivity.CLIHandlerTopic) + { + host.WriteErrorLine("There is no placeholder left to replace."); + } else if (!cr.Text.Contains("```") && !cr.Text.Contains("~~~")) { host.WriteErrorLine("The last AI response contains no code in it."); diff --git a/shell/agents/Microsoft.Azure.Agent/Schema.cs b/shell/agents/Microsoft.Azure.Agent/Schema.cs index 2575e815..0aca7981 100644 --- a/shell/agents/Microsoft.Azure.Agent/Schema.cs +++ b/shell/agents/Microsoft.Azure.Agent/Schema.cs @@ -409,10 +409,9 @@ internal void Reset() internal class UserDirectLineToken { - private const string REFRESH_TOKEN_URL = "https://directline.botframework.com/v3/directline/tokens/refresh"; - private string _token; private DateTimeOffset _expireOn; + private readonly string _tokenRenewUrl; /// /// The DirectLine token. @@ -422,10 +421,11 @@ internal class UserDirectLineToken /// /// Initialize an instance. /// - internal UserDirectLineToken(string token, int expiresInSec) + internal UserDirectLineToken(string token, int expiresInSec, string dlBaseUrl) { _token = token; _expireOn = DateTimeOffset.UtcNow.AddSeconds(expiresInSec); + _tokenRenewUrl = $"{dlBaseUrl}/tokens/refresh"; } /// @@ -466,7 +466,7 @@ internal async Task RenewTokenAsync(HttpClient httpClient, CancellationToken can try { - HttpRequestMessage request = new(HttpMethod.Post, REFRESH_TOKEN_URL); + HttpRequestMessage request = new(HttpMethod.Post, _tokenRenewUrl); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token); var response = await httpClient.SendAsync(request, cancellationToken);