Path: blob/main/extensions/copilot/src/platform/endpoint/node/domainServiceImpl.ts
13400 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type { ConfigurationChangeEvent } from 'vscode';6import { Emitter, Event } from '../../../util/vs/base/common/event';7import { Disposable } from '../../../util/vs/base/common/lifecycle';8import { CopilotToken } from '../../authentication/common/copilotToken';9import { ICopilotTokenStore } from '../../authentication/common/copilotTokenStore';10import { AuthProviderId, ConfigKey, CopilotConfigPrefix, IConfigurationService } from '../../configuration/common/configurationService';11import { ICAPIClientService } from '../common/capiClient';12import { IDomainChangeEvent, IDomainService } from '../common/domainService';1314const EnterpriseURLConfig = 'github-enterprise.uri';1516export class DomainService extends Disposable implements IDomainService {17declare readonly _serviceBrand: undefined;1819private readonly _onDidChangeDomains = this._register(new Emitter<IDomainChangeEvent>());20onDidChangeDomains: Event<IDomainChangeEvent> = this._onDidChangeDomains.event;2122constructor(23@IConfigurationService private readonly _configurationService: IConfigurationService,24@ICopilotTokenStore private readonly _tokenStore: ICopilotTokenStore,25@ICAPIClientService private readonly _capiClientService: ICAPIClientService26) {27super();28this._register(this._configurationService.onDidChangeConfiguration(e => this._onDidConfigChangeHandler(e)));29this._processCopilotToken(this._tokenStore.copilotToken);30this._register(this._tokenStore.onDidStoreUpdate(() => this._processCopilotToken(this._tokenStore.copilotToken)));3132}3334private _onDidConfigChangeHandler(event: ConfigurationChangeEvent) {35// Updated configs that have to do with GHE Domains36if (37event.affectsConfiguration(`${CopilotConfigPrefix}.advanced`) ||38event.affectsConfiguration(EnterpriseURLConfig)39) {40this._processCAPIModuleChange(this._tokenStore.copilotToken);41}42}4344private _processCAPIModuleChange(token: CopilotToken | undefined): void {45let capiConfigUrl = this._configurationService.getConfig(ConfigKey.Shared.DebugOverrideCAPIUrl);46if (capiConfigUrl && capiConfigUrl.endsWith('/')) {47capiConfigUrl = capiConfigUrl.slice(0, -1);48}49let proxyConfigUrl = this._configurationService.getConfig(ConfigKey.Shared.DebugOverrideProxyUrl);50if (proxyConfigUrl) {51proxyConfigUrl = proxyConfigUrl.replace(/\/$/, '');52}53const enterpriseValue = this._configurationService.getConfig(ConfigKey.Shared.AuthProvider) === AuthProviderId.GitHubEnterprise ? this._configurationService.getNonExtensionConfig<string>(EnterpriseURLConfig) : undefined;54const moduleToken = {55endpoints: {56api: capiConfigUrl || token?.endpoints?.api,57proxy: proxyConfigUrl || token?.endpoints?.proxy,58telemetry: token?.endpoints?.telemetry,59'origin-tracker': token?.endpoints?.['origin-tracker']60},61sku: token?.sku || 'unknown',62};63const domainsChanged = this._capiClientService.updateDomains(moduleToken, enterpriseValue);64if (domainsChanged.capiUrlChanged || domainsChanged.proxyUrlChanged || domainsChanged.telemetryUrlChanged || domainsChanged.dotcomUrlChanged) {65this._onDidChangeDomains.fire({66capiUrlChanged: domainsChanged.capiUrlChanged,67telemetryUrlChanged: domainsChanged.telemetryUrlChanged,68proxyUrlChanged: domainsChanged.proxyUrlChanged,69dotcomUrlChanged: domainsChanged.dotcomUrlChanged70});71}72}73747576private _processCopilotToken(token: CopilotToken | undefined): void {77this._processCAPIModuleChange(token);78}7980}818283