Skip to content

Commit 5f4060b

Browse files
committed
distro
1 parent 1a434cb commit 5f4060b

File tree

1 file changed

+114
-107
lines changed

1 file changed

+114
-107
lines changed

src/vs/workbench/contrib/chat/browser/chatSetup.contribution.ts

Lines changed: 114 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ enum ChatSetupStep {
344344
Running
345345
}
346346

347-
class ChatSetupModel extends Disposable {
347+
class ChatSetupController extends Disposable {
348348

349349
private readonly _onDidChange = this._register(new Emitter<void>());
350350
readonly onDidChange = this._onDidChange.event;
@@ -358,7 +358,16 @@ class ChatSetupModel extends Disposable {
358358
return this.entitlementResolver.entitlement;
359359
}
360360

361-
constructor(private readonly entitlementResolver: ChatSetupEntitlementResolver) {
361+
constructor(
362+
private readonly entitlementResolver: ChatSetupEntitlementResolver,
363+
@ITelemetryService private readonly telemetryService: ITelemetryService,
364+
@IAuthenticationService private readonly authenticationService: IAuthenticationService,
365+
@IInstantiationService private readonly instantiationService: IInstantiationService,
366+
@IViewsService private readonly viewsService: IViewsService,
367+
@IExtensionsWorkbenchService private readonly extensionsWorkbenchService: IExtensionsWorkbenchService,
368+
@IProductService private readonly productService: IProductService,
369+
@ILogService private readonly logService: ILogService
370+
) {
362371
super();
363372

364373
this.registerListeners();
@@ -376,6 +385,86 @@ class ChatSetupModel extends Disposable {
376385
this._step = step;
377386
this._onDidChange.fire();
378387
}
388+
389+
async setup(enableTelemetry: boolean | undefined, enableDetection: boolean | undefined): Promise<void> {
390+
this.setStep(ChatSetupStep.Running);
391+
try {
392+
await this.doSetup(enableTelemetry, enableDetection);
393+
} finally {
394+
this.setStep(ChatSetupStep.Initial);
395+
}
396+
}
397+
398+
private async doSetup(enableTelemetry: boolean | undefined, enableDetection: boolean | undefined): Promise<void> {
399+
let session: AuthenticationSession | undefined;
400+
if (this.entitlement === ChatEntitlement.Unknown) {
401+
session = await this.signIn();
402+
if (!session) {
403+
return; // user cancelled
404+
}
405+
}
406+
407+
return this.install(session, enableTelemetry, enableDetection);
408+
}
409+
410+
private async signIn(): Promise<AuthenticationSession | undefined> {
411+
let session: AuthenticationSession | undefined;
412+
try {
413+
showChatView(this.viewsService);
414+
session = await this.authenticationService.createSession(defaultChat.providerId, defaultChat.providerScopes[0]);
415+
} catch (error) {
416+
// noop
417+
}
418+
419+
if (!session) {
420+
this.telemetryService.publicLog2<InstallChatEvent, InstallChatClassification>('commandCenter.chatInstall', { installResult: 'failedNotSignedIn', signedIn: false });
421+
}
422+
423+
return session;
424+
}
425+
426+
private async install(session: AuthenticationSession | undefined, enableTelemetry: boolean | undefined, enableDetection: boolean | undefined): Promise<void> {
427+
const signedIn = !!session;
428+
const activeElement = getActiveElement();
429+
430+
let installResult: 'installed' | 'cancelled' | 'failedInstall';
431+
try {
432+
showChatView(this.viewsService);
433+
434+
if (this.entitlement !== ChatEntitlement.Unavailable) {
435+
const body = {
436+
public_code_suggestions: enableDetection ? 'enabled' : 'disabled',
437+
restricted_telemetry: enableTelemetry ? 'enabled' : 'disabled'
438+
};
439+
this.logService.trace(`[chat setup] install: signing up to limited SKU with ${JSON.stringify(body)}`);
440+
441+
const response = await this.instantiationService.invokeFunction(accessor => ChatSetupRequestHelper.request(accessor, defaultChat.entitlementSignupLimitedUrl, 'POST', body, session, CancellationToken.None));
442+
if (response && this.logService.getLevel() === LogLevel.Trace) {
443+
this.logService.trace(`[chat setup] install: response from signing up to limited SKU ${JSON.stringify(await asText(response))}`);
444+
}
445+
} else {
446+
this.logService.trace('[chat setup] install: not signing up to limited SKU');
447+
}
448+
449+
await this.extensionsWorkbenchService.install(defaultChat.extensionId, {
450+
enable: true,
451+
isMachineScoped: false,
452+
installPreReleaseVersion: this.productService.quality !== 'stable'
453+
}, ChatViewId);
454+
455+
installResult = 'installed';
456+
} catch (error) {
457+
this.logService.trace(`[chat setup] install: error ${error}`);
458+
459+
installResult = isCancellationError(error) ? 'cancelled' : 'failedInstall';
460+
}
461+
462+
this.telemetryService.publicLog2<InstallChatEvent, InstallChatClassification>('commandCenter.chatInstall', { installResult, signedIn });
463+
464+
if (activeElement === getActiveElement()) {
465+
(await showChatView(this.viewsService))?.focusInput();
466+
}
467+
}
379468
}
380469

381470
class ChatSetupWelcomeContent extends Disposable {
@@ -391,21 +480,16 @@ class ChatSetupWelcomeContent extends Disposable {
391480

392481
readonly element = $('.chat-setup-view');
393482

394-
private readonly model: ChatSetupModel;
483+
private readonly controller: ChatSetupController;
395484

396485
constructor(
397486
entitlementResolver: ChatSetupEntitlementResolver,
398487
@ITelemetryService private readonly telemetryService: ITelemetryService,
399-
@IAuthenticationService private readonly authenticationService: IAuthenticationService,
400-
@IInstantiationService private readonly instantiationService: IInstantiationService,
401-
@IViewsService private readonly viewsService: IViewsService,
402-
@IExtensionsWorkbenchService private readonly extensionsWorkbenchService: IExtensionsWorkbenchService,
403-
@IProductService private readonly productService: IProductService,
404-
@ILogService private readonly logService: ILogService,
488+
@IInstantiationService private readonly instantiationService: IInstantiationService
405489
) {
406490
super();
407491

408-
this.model = this._register(new ChatSetupModel(entitlementResolver));
492+
this.controller = this._register(instantiationService.createInstance(ChatSetupController, entitlementResolver));
409493

410494
this.create();
411495
}
@@ -429,23 +513,14 @@ class ChatSetupWelcomeContent extends Disposable {
429513
// Setup Button
430514
const buttonRow = this.element.appendChild($('p'));
431515
const button = this._register(new Button(buttonRow, { ...defaultButtonStyles, supportIcons: true }));
432-
433-
this._register(button.onDidClick(async () => {
434-
this.model.setStep(ChatSetupStep.Running);
435-
436-
try {
437-
await this.setup(telemetryCheckbox?.checked, detectionCheckbox?.checked);
438-
} finally {
439-
this.model.setStep(ChatSetupStep.Initial);
440-
}
441-
}));
516+
this._register(button.onDidClick(() => this.controller.setup(telemetryCheckbox?.checked, detectionCheckbox?.checked)));
442517

443518
// Footer
444519
const footer = localize({ key: 'privacyFooter', comment: ['{Locked="]({0})"}'] }, "By proceeding you agree to our [privacy statement]({0}). You can [learn more]({1}) about {2}.", defaultChat.privacyStatementUrl, defaultChat.documentationUrl, defaultChat.name);
445520
this.element.appendChild($('p')).appendChild(this._register(markdown.render(new MarkdownString(footer, { isTrusted: true }))).element);
446521

447522
// Update based on model state
448-
this._register(Event.runAndSubscribe(this.model.onDidChange, () => this.update([limitedSkuHeaderElement, telemetryContainer, detectionContainer], [telemetryCheckbox, detectionCheckbox], button)));
523+
this._register(Event.runAndSubscribe(this.controller.onDidChange, () => this.update([limitedSkuHeaderElement, telemetryContainer, detectionContainer], [telemetryCheckbox, detectionCheckbox], button)));
449524
}
450525

451526
private createCheckBox(label: string, checked: boolean): { container: HTMLElement; checkbox: Checkbox } {
@@ -464,96 +539,28 @@ class ChatSetupWelcomeContent extends Disposable {
464539
return { container, checkbox };
465540
}
466541

467-
private update(limitedContainers: HTMLElement[], checkboxes: Checkbox[], button: Button,): void {
468-
if (this.model.step === ChatSetupStep.Running) {
469-
button.enabled = false;
470-
button.label = localize('setupChatInstalling', "$(loading~spin) Completing Setup...");
471-
472-
for (const checkbox of checkboxes) {
473-
checkbox.disable();
474-
}
475-
} else {
476-
setVisibility(this.model.entitlement !== ChatEntitlement.Unavailable, ...limitedContainers);
477-
478-
button.enabled = true;
479-
button.label = this.model.entitlement === ChatEntitlement.Unknown ?
480-
localize('signInAndSetup', "Sign in and Complete Setup") :
481-
localize('setup', "Complete Setup");
482-
483-
for (const checkbox of checkboxes) {
484-
checkbox.enable();
485-
}
486-
}
487-
}
488-
489-
private async setup(enableTelemetry: boolean | undefined, enableDetection: boolean | undefined): Promise<void> {
490-
let session: AuthenticationSession | undefined;
491-
if (this.model.entitlement === ChatEntitlement.Unknown) {
492-
session = await this.signIn();
493-
if (!session) {
494-
return; // user cancelled
495-
}
496-
}
497-
498-
return this.install(session, enableTelemetry, enableDetection);
499-
}
500-
501-
private async signIn(): Promise<AuthenticationSession | undefined> {
502-
let session: AuthenticationSession | undefined;
503-
try {
504-
showChatView(this.viewsService);
505-
session = await this.authenticationService.createSession(defaultChat.providerId, defaultChat.providerScopes[0]);
506-
} catch (error) {
507-
// noop
508-
}
509-
510-
if (!session) {
511-
this.telemetryService.publicLog2<InstallChatEvent, InstallChatClassification>('commandCenter.chatInstall', { installResult: 'failedNotSignedIn', signedIn: false });
512-
}
513-
514-
return session;
515-
}
516-
517-
private async install(session: AuthenticationSession | undefined, enableTelemetry: boolean | undefined, enableDetection: boolean | undefined): Promise<void> {
518-
const signedIn = !!session;
519-
const activeElement = getActiveElement();
520-
521-
let installResult: 'installed' | 'cancelled' | 'failedInstall';
522-
try {
523-
showChatView(this.viewsService);
542+
private update(limitedContainers: HTMLElement[], limitedCheckboxes: Checkbox[], button: Button): void {
543+
switch (this.controller.step) {
544+
case ChatSetupStep.Initial:
545+
setVisibility(this.controller.entitlement !== ChatEntitlement.Unavailable, ...limitedContainers);
524546

525-
if (this.model.entitlement !== ChatEntitlement.Unavailable) {
526-
const body = {
527-
public_code_suggestions: enableDetection ? 'enabled' : 'disabled',
528-
restricted_telemetry: enableTelemetry ? 'enabled' : 'disabled'
529-
};
530-
this.logService.trace(`[chat setup] install: signing up to limited SKU with ${JSON.stringify(body)}`);
547+
button.enabled = true;
548+
button.label = this.controller.entitlement === ChatEntitlement.Unknown ?
549+
localize('signInAndSetup', "Sign in and Complete Setup") :
550+
localize('setup', "Complete Setup");
531551

532-
const response = await this.instantiationService.invokeFunction(accessor => ChatSetupRequestHelper.request(accessor, defaultChat.entitlementSignupLimitedUrl, 'POST', body, session, CancellationToken.None));
533-
if (response && this.logService.getLevel() === LogLevel.Trace) {
534-
this.logService.trace(`[chat setup] install: response from signing up to limited SKU ${JSON.stringify(await asText(response))}`);
552+
for (const checkbox of limitedCheckboxes) {
553+
checkbox.enable();
535554
}
536-
} else {
537-
this.logService.trace('[chat setup] install: not signing up to limited SKU');
538-
}
539-
540-
await this.extensionsWorkbenchService.install(defaultChat.extensionId, {
541-
enable: true,
542-
isMachineScoped: false,
543-
installPreReleaseVersion: this.productService.quality !== 'stable'
544-
}, ChatViewId);
545-
546-
installResult = 'installed';
547-
} catch (error) {
548-
this.logService.trace(`[chat setup] install: error ${error}`);
549-
550-
installResult = isCancellationError(error) ? 'cancelled' : 'failedInstall';
551-
}
555+
break;
556+
case ChatSetupStep.Running:
557+
button.enabled = false;
558+
button.label = localize('setupChatInstalling', "$(loading~spin) Completing Setup...");
552559

553-
this.telemetryService.publicLog2<InstallChatEvent, InstallChatClassification>('commandCenter.chatInstall', { installResult, signedIn });
554-
555-
if (activeElement === getActiveElement()) {
556-
(await showChatView(this.viewsService))?.focusInput();
560+
for (const checkbox of limitedCheckboxes) {
561+
checkbox.disable();
562+
}
563+
break;
557564
}
558565
}
559566
}

0 commit comments

Comments
 (0)