Skip to content

Commit f301c44

Browse files
authored
Use the DirectLine base URL returned from the 'start-conversation' API (#306)
1 parent 36563c0 commit f301c44

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ internal class ChatSession : IDisposable
1414
private const string PROD_ACCESS_URL = "https://copilotweb.production.portalrp.azure.com/api/access?api-version=2024-09-01";
1515
private const string TEST_ACCESS_URL = "https://copilotweb.canary.production.portalrp.azure.com/api/access?api-version=2024-09-01";
1616
private const string DL_TOKEN_URL = "https://copilotweb.production.portalrp.azure.com/api/conversations/start?api-version=2024-11-15";
17-
private const string CONVERSATION_URL = "https://directline.botframework.com/v3/directline/conversations";
1817

1918
internal bool UserAuthorized { get; private set; }
2019

2120
private string _streamUrl;
21+
private string _dlBaseUrl;
2222
private string _conversationId;
2323
private string _conversationUrl;
2424
private UserDirectLineToken _directLineToken;
@@ -101,6 +101,7 @@ internal async Task<string> RefreshAsync(IStatusContext context, bool force, Can
101101
private void Reset()
102102
{
103103
_streamUrl = null;
104+
_dlBaseUrl = null;
104105
_conversationId = null;
105106
_conversationUrl = null;
106107
_directLineToken = null;
@@ -180,12 +181,14 @@ private async Task GetInitialDLTokenAsync(CancellationToken cancellationToken)
180181

181182
using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken);
182183
var dlToken = JsonSerializer.Deserialize<DirectLineToken>(stream, Utils.JsonOptions);
183-
_directLineToken = new UserDirectLineToken(dlToken.DirectLine.Token, dlToken.DirectLine.TokenExpiryTimeInSeconds);
184+
185+
_dlBaseUrl = dlToken.DirectLine.Endpoint;
186+
_directLineToken = new UserDirectLineToken(dlToken.DirectLine.Token, dlToken.DirectLine.TokenExpiryTimeInSeconds, _dlBaseUrl);
184187
}
185188

186189
private async Task<string> OpenConversationAsync(CancellationToken cancellationToken)
187190
{
188-
HttpRequestMessage request = new(HttpMethod.Post, CONVERSATION_URL);
191+
HttpRequestMessage request = new(HttpMethod.Post, $"{_dlBaseUrl}/conversations");
189192
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _directLineToken.Token);
190193

191194
HttpResponseMessage response = await _httpClient.SendAsync(request, cancellationToken);
@@ -195,8 +198,8 @@ private async Task<string> OpenConversationAsync(CancellationToken cancellationT
195198
SessionPayload spl = JsonSerializer.Deserialize<SessionPayload>(content, Utils.JsonOptions);
196199

197200
_conversationId = spl.ConversationId;
198-
_conversationUrl = $"{CONVERSATION_URL}/{_conversationId}/activities";
199-
_directLineToken = new UserDirectLineToken(spl.Token, spl.ExpiresIn);
201+
_conversationUrl = $"{_dlBaseUrl}/conversations/{_conversationId}/activities";
202+
_directLineToken = new UserDirectLineToken(spl.Token, spl.ExpiresIn, _dlBaseUrl);
200203
_streamUrl = spl.StreamUrl;
201204
_copilotReceiver = await AzureCopilotReceiver.CreateAsync(_streamUrl);
202205

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

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ private void ReplaceAction()
5858
{
5959
host.WriteErrorLine("No AI response available.");
6060
}
61+
else if (cr.TopicName is CopilotActivity.CLIHandlerTopic)
62+
{
63+
host.WriteErrorLine("There is no placeholder left to replace.");
64+
}
6165
else if (!cr.Text.Contains("```") && !cr.Text.Contains("~~~"))
6266
{
6367
host.WriteErrorLine("The last AI response contains no code in it.");

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,9 @@ internal void Reset()
409409

410410
internal class UserDirectLineToken
411411
{
412-
private const string REFRESH_TOKEN_URL = "https://directline.botframework.com/v3/directline/tokens/refresh";
413-
414412
private string _token;
415413
private DateTimeOffset _expireOn;
414+
private readonly string _tokenRenewUrl;
416415

417416
/// <summary>
418417
/// The DirectLine token.
@@ -422,10 +421,11 @@ internal class UserDirectLineToken
422421
/// <summary>
423422
/// Initialize an instance.
424423
/// </summary>
425-
internal UserDirectLineToken(string token, int expiresInSec)
424+
internal UserDirectLineToken(string token, int expiresInSec, string dlBaseUrl)
426425
{
427426
_token = token;
428427
_expireOn = DateTimeOffset.UtcNow.AddSeconds(expiresInSec);
428+
_tokenRenewUrl = $"{dlBaseUrl}/tokens/refresh";
429429
}
430430

431431
/// <summary>
@@ -466,7 +466,7 @@ internal async Task RenewTokenAsync(HttpClient httpClient, CancellationToken can
466466

467467
try
468468
{
469-
HttpRequestMessage request = new(HttpMethod.Post, REFRESH_TOKEN_URL);
469+
HttpRequestMessage request = new(HttpMethod.Post, _tokenRenewUrl);
470470
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
471471

472472
var response = await httpClient.SendAsync(request, cancellationToken);

0 commit comments

Comments
 (0)