Path: blob/main/src/vs/workbench/services/inlineCompletions/common/inlineCompletionsUnification.ts
3296 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 { equals } from '../../../../base/common/arrays.js';6import { Event, Emitter } from '../../../../base/common/event.js';7import { Disposable } from '../../../../base/common/lifecycle.js';8import { InstantiationType, registerSingleton } from '../../../../platform/instantiation/common/extensions.js';9import { createDecorator } from '../../../../platform/instantiation/common/instantiation.js';10import { IWorkbenchAssignmentService } from '../../assignment/common/assignmentService.js';1112export const IInlineCompletionsUnificationService = createDecorator<IInlineCompletionsUnificationService>('inlineCompletionsUnificationService');1314export interface IInlineCompletionsUnificationState {15codeUnification: boolean;16modelUnification: boolean;17expAssignments: string[];18}1920export interface IInlineCompletionsUnificationService {21readonly _serviceBrand: undefined;2223readonly state: IInlineCompletionsUnificationState;24onDidStateChange: Event<void>;25}2627const CODE_UNIFICATION_PREFIX = 'cmp-cht-';28const CODE_UNIFICATION_FF = 'inlineCompletionsUnificationCode';29const MODEL_UNIFICATION_FF = 'inlineCompletionsUnificationModel';3031export class InlineCompletionsUnificationImpl extends Disposable implements IInlineCompletionsUnificationService {32readonly _serviceBrand: undefined;3334private _state = new InlineCompletionsUnificationState(false, false, []);35public get state(): IInlineCompletionsUnificationState { return this._state; }3637private readonly _onDidStateChange = this._register(new Emitter<void>());38public readonly onDidStateChange = this._onDidStateChange.event;3940constructor(41@IWorkbenchAssignmentService private readonly _assignmentService: IWorkbenchAssignmentService42) {43super();44this._register(this._assignmentService.onDidRefetchAssignments(() => this._update()));45this._update();46}4748private async _update(): Promise<void> {49const [codeUnificationFF, modelUnificationFF] = await Promise.all([50this._assignmentService.getTreatment<boolean>(CODE_UNIFICATION_FF),51this._assignmentService.getTreatment<boolean>(MODEL_UNIFICATION_FF),52]);53// Intentionally read the current experiments after fetching the treatments54const currentExperiments = await this._assignmentService.getCurrentExperiments();55const newState = new InlineCompletionsUnificationState(56codeUnificationFF === true,57modelUnificationFF === true,58currentExperiments?.filter(exp => exp.startsWith(CODE_UNIFICATION_PREFIX)) ?? []59);60if (this._state.equals(newState)) {61return;62}63this._state = newState;64this._onDidStateChange.fire();65}66}6768class InlineCompletionsUnificationState implements IInlineCompletionsUnificationState {69constructor(70public readonly codeUnification: boolean,71public readonly modelUnification: boolean,72public readonly expAssignments: string[]73) {74}7576equals(other: IInlineCompletionsUnificationState): boolean {77return this.codeUnification === other.codeUnification78&& this.modelUnification === other.modelUnification79&& equals(this.expAssignments, other.expAssignments);80}81}8283registerSingleton(IInlineCompletionsUnificationService, InlineCompletionsUnificationImpl, InstantiationType.Delayed);848586