Skip to content

Commit

Permalink
Use the DirectLine base URL returned from the 'start-conversation' API (
Browse files Browse the repository at this point in the history
  • Loading branch information
daxian-dbw authored Nov 18, 2024
1 parent 36563c0 commit f301c44
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
13 changes: 8 additions & 5 deletions shell/agents/Microsoft.Azure.Agent/ChatSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,6 +101,7 @@ internal async Task<string> RefreshAsync(IStatusContext context, bool force, Can
private void Reset()
{
_streamUrl = null;
_dlBaseUrl = null;
_conversationId = null;
_conversationUrl = null;
_directLineToken = null;
Expand Down Expand Up @@ -180,12 +181,14 @@ private async Task GetInitialDLTokenAsync(CancellationToken cancellationToken)

using Stream stream = await response.Content.ReadAsStreamAsync(cancellationToken);
var dlToken = JsonSerializer.Deserialize<DirectLineToken>(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<string> 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);
Expand All @@ -195,8 +198,8 @@ private async Task<string> OpenConversationAsync(CancellationToken cancellationT
SessionPayload spl = JsonSerializer.Deserialize<SessionPayload>(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);

Expand Down
4 changes: 4 additions & 0 deletions shell/agents/Microsoft.Azure.Agent/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
8 changes: 4 additions & 4 deletions shell/agents/Microsoft.Azure.Agent/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// The DirectLine token.
Expand All @@ -422,10 +421,11 @@ internal class UserDirectLineToken
/// <summary>
/// Initialize an instance.
/// </summary>
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";
}

/// <summary>
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f301c44

Please sign in to comment.