Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/endpoint/node/domainServiceImpl.ts
13400 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { ConfigurationChangeEvent } from 'vscode';
7
import { Emitter, Event } from '../../../util/vs/base/common/event';
8
import { Disposable } from '../../../util/vs/base/common/lifecycle';
9
import { CopilotToken } from '../../authentication/common/copilotToken';
10
import { ICopilotTokenStore } from '../../authentication/common/copilotTokenStore';
11
import { AuthProviderId, ConfigKey, CopilotConfigPrefix, IConfigurationService } from '../../configuration/common/configurationService';
12
import { ICAPIClientService } from '../common/capiClient';
13
import { IDomainChangeEvent, IDomainService } from '../common/domainService';
14
15
const EnterpriseURLConfig = 'github-enterprise.uri';
16
17
export class DomainService extends Disposable implements IDomainService {
18
declare readonly _serviceBrand: undefined;
19
20
private readonly _onDidChangeDomains = this._register(new Emitter<IDomainChangeEvent>());
21
onDidChangeDomains: Event<IDomainChangeEvent> = this._onDidChangeDomains.event;
22
23
constructor(
24
@IConfigurationService private readonly _configurationService: IConfigurationService,
25
@ICopilotTokenStore private readonly _tokenStore: ICopilotTokenStore,
26
@ICAPIClientService private readonly _capiClientService: ICAPIClientService
27
) {
28
super();
29
this._register(this._configurationService.onDidChangeConfiguration(e => this._onDidConfigChangeHandler(e)));
30
this._processCopilotToken(this._tokenStore.copilotToken);
31
this._register(this._tokenStore.onDidStoreUpdate(() => this._processCopilotToken(this._tokenStore.copilotToken)));
32
33
}
34
35
private _onDidConfigChangeHandler(event: ConfigurationChangeEvent) {
36
// Updated configs that have to do with GHE Domains
37
if (
38
event.affectsConfiguration(`${CopilotConfigPrefix}.advanced`) ||
39
event.affectsConfiguration(EnterpriseURLConfig)
40
) {
41
this._processCAPIModuleChange(this._tokenStore.copilotToken);
42
}
43
}
44
45
private _processCAPIModuleChange(token: CopilotToken | undefined): void {
46
let capiConfigUrl = this._configurationService.getConfig(ConfigKey.Shared.DebugOverrideCAPIUrl);
47
if (capiConfigUrl && capiConfigUrl.endsWith('/')) {
48
capiConfigUrl = capiConfigUrl.slice(0, -1);
49
}
50
let proxyConfigUrl = this._configurationService.getConfig(ConfigKey.Shared.DebugOverrideProxyUrl);
51
if (proxyConfigUrl) {
52
proxyConfigUrl = proxyConfigUrl.replace(/\/$/, '');
53
}
54
const enterpriseValue = this._configurationService.getConfig(ConfigKey.Shared.AuthProvider) === AuthProviderId.GitHubEnterprise ? this._configurationService.getNonExtensionConfig<string>(EnterpriseURLConfig) : undefined;
55
const moduleToken = {
56
endpoints: {
57
api: capiConfigUrl || token?.endpoints?.api,
58
proxy: proxyConfigUrl || token?.endpoints?.proxy,
59
telemetry: token?.endpoints?.telemetry,
60
'origin-tracker': token?.endpoints?.['origin-tracker']
61
},
62
sku: token?.sku || 'unknown',
63
};
64
const domainsChanged = this._capiClientService.updateDomains(moduleToken, enterpriseValue);
65
if (domainsChanged.capiUrlChanged || domainsChanged.proxyUrlChanged || domainsChanged.telemetryUrlChanged || domainsChanged.dotcomUrlChanged) {
66
this._onDidChangeDomains.fire({
67
capiUrlChanged: domainsChanged.capiUrlChanged,
68
telemetryUrlChanged: domainsChanged.telemetryUrlChanged,
69
proxyUrlChanged: domainsChanged.proxyUrlChanged,
70
dotcomUrlChanged: domainsChanged.dotcomUrlChanged
71
});
72
}
73
}
74
75
76
77
private _processCopilotToken(token: CopilotToken | undefined): void {
78
this._processCAPIModuleChange(token);
79
}
80
81
}
82
83