Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the DirectLine base URL returned from the 'start-conversation' API #306

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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