-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathGithubCopilotLLMProvider.cpp
414 lines (364 loc) · 19.3 KB
/
GithubCopilotLLMProvider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "GithubCopilotLLMProvider.h"
#include "../../types/inc/utils.hpp"
#include "LibraryResources.h"
#include "WindowsTerminalIDAndSecret.h"
#include "GithubCopilotLLMProvider.g.cpp"
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::System;
namespace WWH = ::winrt::Windows::Web::Http;
namespace WSS = ::winrt::Windows::Storage::Streams;
namespace WDJ = ::winrt::Windows::Data::Json;
// branding data
static constexpr wil::zwstring_view headerIconPath{ L"ms-appx:///ProfileIcons/githubCopilotLogo.png" };
static constexpr wil::zwstring_view badgeIconPath{ L"ms-appx:///ProfileIcons/githubCopilotBadge.png" };
// header and request strings
static constexpr std::wstring_view applicationJsonString{ L"application/json" };
static constexpr std::wstring_view bearerString{ L"Bearer" };
static constexpr std::wstring_view copilotIntegrationIdString{ L"Copilot-Integration-Id" };
static constexpr std::wstring_view clientIdKey{ L"client_id" };
static constexpr std::wstring_view clientSecretKey{ L"client_secret" };
static constexpr std::wstring_view endpointAndUsernameRequestString{ L"{ viewer { copilotEndpoints { api } login } }" };
// json keys
static constexpr std::wstring_view accessTokenKey{ L"access_token" };
static constexpr std::wstring_view refreshTokenKey{ L"refresh_token" };
static constexpr std::wstring_view stateKey{ L"state" };
static constexpr std::wstring_view urlKey{ L"url" };
static constexpr std::wstring_view queryKey{ L"query" };
static constexpr std::wstring_view codeKey{ L"code" };
static constexpr std::wstring_view errorKey{ L"error" };
static constexpr std::wstring_view errorDescriptionKey{ L"error_description" };
static constexpr std::wstring_view dataKey{ L"data" };
static constexpr std::wstring_view apiKey{ L"api" };
static constexpr std::wstring_view viewerKey{ L"viewer" };
static constexpr std::wstring_view copilotEndpointsKey{ L"copilotEndpoints" };
static constexpr std::wstring_view loginKey{ L"login" };
static constexpr std::wstring_view grantTypeKey{ L"grant_type" };
static constexpr std::wstring_view contentKey{ L"content" };
static constexpr std::wstring_view messageKey{ L"message" };
static constexpr std::wstring_view messagesKey{ L"messages" };
static constexpr std::wstring_view choicesKey{ L"choices" };
static constexpr std::wstring_view roleKey{ L"role" };
static constexpr std::wstring_view assistantKey{ L"assistant" };
static constexpr std::wstring_view userKey{ L"user" };
static constexpr std::wstring_view systemKey{ L"system" };
// endpoints
static constexpr std::wstring_view githubGraphQLEndpoint{ L"https://api.github.com/graphql" };
static constexpr std::wstring_view chatCompletionSuffix{ L"/chat/completions" };
static constexpr std::wstring_view accessTokenEndpoint{ L"https://github.com/login/oauth/access_token" };
// Windows Terminal specific strings
static constexpr std::wstring_view windowsTerminalUserAgent{ L"Windows Terminal" };
static constexpr std::wstring_view windowsTerminalIntegrationId{ L"windows-terminal-chat" };
namespace winrt::Microsoft::Terminal::Query::Extension::implementation
{
winrt::hstring GithubCopilotBranding::HeaderIconPath() const noexcept
{
return headerIconPath.c_str();
}
winrt::hstring GithubCopilotBranding::HeaderText() const noexcept
{
return RS_(L"GithubCopilot_HeaderText");
}
winrt::hstring GithubCopilotBranding::SubheaderText() const noexcept
{
return RS_(L"GithubCopilot_SubheaderText");
}
winrt::hstring GithubCopilotBranding::BadgeIconPath() const noexcept
{
return badgeIconPath.c_str();
}
void GithubCopilotLLMProvider::SetAuthentication(const winrt::hstring& authValues)
{
_httpClient = winrt::Windows::Web::Http::HttpClient{};
_httpClient.DefaultRequestHeaders().Accept().TryParseAdd(applicationJsonString);
_httpClient.DefaultRequestHeaders().Append(copilotIntegrationIdString, windowsTerminalIntegrationId);
_httpClient.DefaultRequestHeaders().UserAgent().TryParseAdd(windowsTerminalUserAgent);
if (!authValues.empty())
{
WDJ::JsonObject authValuesObject{ WDJ::JsonObject::Parse(authValues) };
if (authValuesObject.HasKey(urlKey) && authValuesObject.HasKey(stateKey))
{
const Windows::Foundation::Uri parsedUrl{ authValuesObject.GetNamedString(urlKey) };
// only handle this if the state strings match
if (authValuesObject.GetNamedString(stateKey) == parsedUrl.QueryParsed().GetFirstValueByName(stateKey))
{
// we got a valid URL, fire off the URL auth flow
_completeAuthWithUrl(parsedUrl);
}
}
else if (authValuesObject.HasKey(accessTokenKey) && authValuesObject.HasKey(refreshTokenKey))
{
_authToken = authValuesObject.GetNamedString(accessTokenKey);
_refreshToken = authValuesObject.GetNamedString(refreshTokenKey);
// we got tokens, use them
_httpClient.DefaultRequestHeaders().Authorization(WWH::Headers::HttpCredentialsHeaderValue{ bearerString, _authToken });
_obtainUsernameAndRefreshTokensIfNeeded();
}
}
}
IAsyncAction GithubCopilotLLMProvider::_obtainUsernameAndRefreshTokensIfNeeded()
{
WDJ::JsonObject endpointAndUsernameRequestJson;
endpointAndUsernameRequestJson.SetNamedValue(queryKey, WDJ::JsonValue::CreateStringValue(endpointAndUsernameRequestString));
const auto endpointAndUsernameRequestString = endpointAndUsernameRequestJson.ToString();
WWH::HttpStringContent endpointAndUsernameRequestContent{
endpointAndUsernameRequestString,
WSS::UnicodeEncoding::Utf8,
applicationJsonString
};
auto strongThis = get_strong();
co_await winrt::resume_background();
for (bool refreshAttempted = false;;)
{
try
{
const auto endpointAndUsernameResult = co_await _SendRequestReturningJson(githubGraphQLEndpoint, endpointAndUsernameRequestContent, WWH::HttpMethod::Post());
const auto viewerObject = endpointAndUsernameResult.GetNamedObject(dataKey).GetNamedObject(viewerKey);
const auto userName = viewerObject.GetNamedString(loginKey);
const auto copilotEndpoint = viewerObject.GetNamedObject(copilotEndpointsKey).GetNamedString(apiKey);
_endpointUri = copilotEndpoint + chatCompletionSuffix;
const auto brandingData{ get_self<GithubCopilotBranding>(_brandingData) };
brandingData->QueryAttribution(userName);
break;
}
CATCH_LOG();
// unknown failure, try refreshing the auth token if we haven't already
if (refreshAttempted)
{
break;
}
co_await _refreshAuthTokens();
refreshAttempted = true;
}
co_return;
}
IAsyncAction GithubCopilotLLMProvider::_completeAuthWithUrl(const Windows::Foundation::Uri url)
{
WDJ::JsonObject jsonContent;
jsonContent.SetNamedValue(clientIdKey, WDJ::JsonValue::CreateStringValue(windowsTerminalClientID));
jsonContent.SetNamedValue(clientSecretKey, WDJ::JsonValue::CreateStringValue(windowsTerminalClientSecret));
jsonContent.SetNamedValue(codeKey, WDJ::JsonValue::CreateStringValue(url.QueryParsed().GetFirstValueByName(codeKey)));
const auto stringContent = jsonContent.ToString();
WWH::HttpStringContent requestContent{
stringContent,
WSS::UnicodeEncoding::Utf8,
applicationJsonString
};
auto strongThis = get_strong();
co_await winrt::resume_background();
try
{
// Get the user's oauth token
const auto jsonResult = co_await _SendRequestReturningJson(accessTokenEndpoint, requestContent, WWH::HttpMethod::Post());
if (jsonResult.HasKey(errorKey))
{
const auto errorMessage = jsonResult.GetNamedString(errorDescriptionKey);
_AuthChangedHandlers(*this, winrt::make<GithubCopilotAuthenticationResult>(errorMessage, winrt::hstring{}));
}
else
{
const auto authToken{ jsonResult.GetNamedString(accessTokenKey) };
const auto refreshToken{ jsonResult.GetNamedString(refreshTokenKey) };
if (!authToken.empty() && !refreshToken.empty())
{
_authToken = authToken;
_refreshToken = refreshToken;
_httpClient.DefaultRequestHeaders().Authorization(WWH::Headers::HttpCredentialsHeaderValue{ bearerString, _authToken });
// raise the new tokens so the app can store them
Windows::Data::Json::JsonObject authValuesJson;
authValuesJson.SetNamedValue(accessTokenKey, WDJ::JsonValue::CreateStringValue(_authToken));
authValuesJson.SetNamedValue(refreshTokenKey, WDJ::JsonValue::CreateStringValue(_refreshToken));
_AuthChangedHandlers(*this, winrt::make<GithubCopilotAuthenticationResult>(winrt::hstring{}, authValuesJson.ToString()));
// we also need to get the correct endpoint to use and the username
_obtainUsernameAndRefreshTokensIfNeeded();
}
}
}
catch (...)
{
// some unknown error happened and we didn't get an "error" key, bubble the raw string of the last response if we have one
const auto errorMessage = _lastResponse.empty() ? RS_(L"UnknownErrorMessage") : _lastResponse;
_AuthChangedHandlers(*this, winrt::make<GithubCopilotAuthenticationResult>(errorMessage, winrt::hstring{}));
}
co_return;
}
void GithubCopilotLLMProvider::ClearMessageHistory()
{
_jsonMessages.Clear();
}
void GithubCopilotLLMProvider::SetSystemPrompt(const winrt::hstring& systemPrompt)
{
WDJ::JsonObject systemMessageObject;
winrt::hstring systemMessageContent{ systemPrompt };
systemMessageObject.Insert(roleKey, WDJ::JsonValue::CreateStringValue(systemKey));
systemMessageObject.Insert(contentKey, WDJ::JsonValue::CreateStringValue(systemMessageContent));
_jsonMessages.Append(systemMessageObject);
}
void GithubCopilotLLMProvider::SetContext(const Extension::IContext context)
{
_context = context;
}
winrt::Windows::Foundation::IAsyncOperation<Extension::IResponse> GithubCopilotLLMProvider::GetResponseAsync(const winrt::hstring& userPrompt)
{
// Use the ErrorTypes enum to flag whether the response the user receives is an error message
// we pass this enum back to the caller so they can handle it appropriately (specifically, ExtensionPalette will send the correct telemetry event)
ErrorTypes errorType{ ErrorTypes::None };
hstring message{};
// Make a copy of the prompt because we are switching threads
const auto promptCopy{ userPrompt };
// Make sure we are on the background thread for the http request
auto strongThis = get_strong();
co_await winrt::resume_background();
auto cancellationToken{ co_await winrt::get_cancellation_token() };
for (bool refreshAttempted = false;;)
{
try
{
// create the request content
// we construct the request content within the while loop because if we do need to attempt
// a request again after refreshing the tokens, we need a new request object
WDJ::JsonObject jsonContent;
WDJ::JsonObject messageObject;
winrt::hstring engineeredPrompt{ promptCopy };
if (_context && !_context.ActiveCommandline().empty())
{
engineeredPrompt = promptCopy + L". The shell I am running is " + _context.ActiveCommandline();
}
messageObject.Insert(roleKey, WDJ::JsonValue::CreateStringValue(userKey));
messageObject.Insert(contentKey, WDJ::JsonValue::CreateStringValue(engineeredPrompt));
_jsonMessages.Append(messageObject);
jsonContent.SetNamedValue(messagesKey, _jsonMessages);
const auto stringContent = jsonContent.ToString();
WWH::HttpStringContent requestContent{
stringContent,
WSS::UnicodeEncoding::Utf8,
applicationJsonString
};
// Send the request
const auto sendRequestOperation = _SendRequestReturningJson(_endpointUri, requestContent, WWH::HttpMethod::Post());
// if the caller cancels this operation, make sure to cancel the http request as well
cancellationToken.callback([sendRequestOperation] {
sendRequestOperation.Cancel();
});
if (sendRequestOperation.wait_for(std::chrono::seconds(5)) == AsyncStatus::Completed)
{
// Parse out the suggestion from the response
const auto jsonResult = sendRequestOperation.GetResults();
if (jsonResult.HasKey(errorKey))
{
const auto errorObject = jsonResult.GetNamedObject(errorKey);
message = errorObject.GetNamedString(messageKey);
errorType = ErrorTypes::FromProvider;
}
else
{
const auto choices = jsonResult.GetNamedArray(choicesKey);
const auto firstChoice = choices.GetAt(0).GetObject();
const auto messageObject = firstChoice.GetNamedObject(messageKey);
message = messageObject.GetNamedString(contentKey);
}
}
else
{
// if the http request takes too long, cancel the http request and return an error
sendRequestOperation.Cancel();
message = RS_(L"UnknownErrorMessage");
errorType = ErrorTypes::Unknown;
}
break;
}
CATCH_LOG();
// unknown failure, if we have already attempted a refresh report failure
// otherwise, try refreshing the auth token
if (refreshAttempted)
{
// if we have a last recorded response, bubble that instead of the unknown error message
// since that's likely going to be more useful
message = _lastResponse.empty() ? RS_(L"UnknownErrorMessage") : _lastResponse;
errorType = ErrorTypes::Unknown;
break;
}
const auto refreshTokensAction = _refreshAuthTokens();
cancellationToken.callback([refreshTokensAction] {
refreshTokensAction.Cancel();
});
// allow up to 10 seconds for reauthentication
if (refreshTokensAction.wait_for(std::chrono::seconds(10)) == AsyncStatus::Completed)
{
refreshAttempted = true;
}
else
{
// if the refresh action takes too long, cancel it and return an error
refreshTokensAction.Cancel();
message = RS_(L"UnknownErrorMessage");
errorType = ErrorTypes::Unknown;
break;
}
}
// Also make a new entry in our jsonMessages list, so the AI knows the full conversation so far
WDJ::JsonObject responseMessageObject;
responseMessageObject.Insert(roleKey, WDJ::JsonValue::CreateStringValue(assistantKey));
responseMessageObject.Insert(contentKey, WDJ::JsonValue::CreateStringValue(message));
_jsonMessages.Append(responseMessageObject);
co_return winrt::make<GithubCopilotResponse>(message, errorType, RS_(L"GithubCopilot_ResponseMetaData"));
}
IAsyncAction GithubCopilotLLMProvider::_refreshAuthTokens()
{
WDJ::JsonObject jsonContent;
jsonContent.SetNamedValue(clientIdKey, WDJ::JsonValue::CreateStringValue(windowsTerminalClientID));
jsonContent.SetNamedValue(grantTypeKey, WDJ::JsonValue::CreateStringValue(refreshTokenKey));
jsonContent.SetNamedValue(clientSecretKey, WDJ::JsonValue::CreateStringValue(windowsTerminalClientSecret));
jsonContent.SetNamedValue(refreshTokenKey, WDJ::JsonValue::CreateStringValue(_refreshToken));
const auto stringContent = jsonContent.ToString();
WWH::HttpStringContent requestContent{
stringContent,
WSS::UnicodeEncoding::Utf8,
applicationJsonString
};
try
{
const auto reAuthOperation = _SendRequestReturningJson(accessTokenEndpoint, requestContent, WWH::HttpMethod::Post());
auto cancellationToken{ co_await winrt::get_cancellation_token() };
cancellationToken.callback([reAuthOperation] {
reAuthOperation.Cancel();
});
const auto jsonResult{ co_await reAuthOperation };
_authToken = jsonResult.GetNamedString(accessTokenKey);
_refreshToken = jsonResult.GetNamedString(refreshTokenKey);
_httpClient.DefaultRequestHeaders().Authorization(WWH::Headers::HttpCredentialsHeaderValue{ bearerString, _authToken });
// raise the new tokens so the app can store them
Windows::Data::Json::JsonObject authValuesJson;
authValuesJson.SetNamedValue(accessTokenKey, WDJ::JsonValue::CreateStringValue(_authToken));
authValuesJson.SetNamedValue(refreshTokenKey, WDJ::JsonValue::CreateStringValue(_refreshToken));
_AuthChangedHandlers(*this, winrt::make<GithubCopilotAuthenticationResult>(winrt::hstring{}, authValuesJson.ToString()));
}
CATCH_LOG();
co_return;
}
IAsyncOperation<WDJ::JsonObject> GithubCopilotLLMProvider::_SendRequestReturningJson(std::wstring_view uri, const winrt::Windows::Web::Http::IHttpContent& content, winrt::Windows::Web::Http::HttpMethod method)
{
if (!method)
{
method = content == nullptr ? WWH::HttpMethod::Get() : WWH::HttpMethod::Post();
}
WWH::HttpRequestMessage request{ method, Uri{ uri } };
request.Content(content);
const auto sendRequestOperation = _httpClient.SendRequestAsync(request);
auto cancellationToken{ co_await winrt::get_cancellation_token() };
cancellationToken.callback([sendRequestOperation] {
sendRequestOperation.Cancel();
});
const auto response{ co_await sendRequestOperation };
const auto string{ co_await response.Content().ReadAsStringAsync() };
_lastResponse = string;
const auto jsonResult{ WDJ::JsonObject::Parse(string) };
co_return jsonResult;
}
}