Skip to content

Commit 11b6366

Browse files
Check unified Copilot API before legacy API (#14167)
Co-authored-by: Sean McManus <seanmcm@microsoft.com>
1 parent 920997d commit 11b6366

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

Extension/src/LanguageServer/copilotCompletionContextProvider.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -453,26 +453,6 @@ ${copilotCompletionContext?.areSnippetsMissing ? "(missing code snippets)" : ""}
453453
resolver: this
454454
};
455455
type RegistrationResult = { message: string } | boolean;
456-
const clientPromise: Promise<RegistrationResult> = getCopilotClientApi().then(async (api) => {
457-
if (!api) {
458-
throw new CopilotContextProviderException("getCopilotApi() returned null, Copilot client is missing or inactive.");
459-
}
460-
const disposable = await this.installContextProvider(api, contextProvider);
461-
if (disposable) {
462-
this.contextProviderDisposables = this.contextProviderDisposables ?? [];
463-
this.contextProviderDisposables.push(disposable);
464-
return true;
465-
} else {
466-
throw new CopilotContextProviderException("getContextProviderAPI() is not available in Copilot client.");
467-
}
468-
}).catch((e) => {
469-
console.debug("Failed to register the Copilot Context Provider with Copilot client.");
470-
let message = "Failed to register the Copilot Context Provider with Copilot client";
471-
if (e instanceof CopilotContextProviderException) {
472-
message += `: ${e.message} `;
473-
}
474-
return { message };
475-
});
476456
const chatPromise: Promise<RegistrationResult> = getCopilotChatApi().then(async (api) => {
477457
if (!api) {
478458
throw new CopilotContextProviderException("getCopilotChatApi() returned null, Copilot Chat is missing or inactive.");
@@ -493,24 +473,50 @@ ${copilotCompletionContext?.areSnippetsMissing ? "(missing code snippets)" : ""}
493473
}
494474
return { message };
495475
});
496-
// The client usually doesn't block. So test it first.
497-
clientPromise.then((clientResult) => {
476+
477+
chatPromise.then((chatResult) => {
498478
const properties: Record<string, string> = {};
499-
if (isBoolean(clientResult) && clientResult) {
479+
if (isBoolean(chatResult) && chatResult) {
500480
properties["cppCodeSnippetsProviderRegistered"] = "true";
501481
telemetry.logCopilotEvent(registerCopilotContextProvider, { ...properties });
502482
return;
503483
}
504-
return chatPromise.then((chatResult) => {
505-
const properties: Record<string, string> = {};
506-
if (isBoolean(chatResult) && chatResult) {
484+
485+
// Don't even start checking the github.copilot extension until the chat API is confirmed to be unavailable,
486+
// as users can only follow this code path if they temporarily opted out of Copilot extension unification.
487+
const clientPromise: Promise<RegistrationResult> = getCopilotClientApi().then(async (api) => {
488+
if (!api) {
489+
if (vscode.extensions.getExtension('github.copilot') === undefined) {
490+
return { message: "getCopilotApi() returned null, Copilot extension is not installed." };
491+
}
492+
throw new CopilotContextProviderException("getCopilotApi() returned null, Copilot client is missing or inactive.");
493+
}
494+
const disposable = await this.installContextProvider(api, contextProvider);
495+
if (disposable) {
496+
this.contextProviderDisposables = this.contextProviderDisposables ?? [];
497+
this.contextProviderDisposables.push(disposable);
498+
return true;
499+
} else {
500+
throw new CopilotContextProviderException("getContextProviderAPI() is not available in Copilot client.");
501+
}
502+
}).catch((e) => {
503+
console.debug("Failed to register the Copilot Context Provider with Copilot client.");
504+
let message = "Failed to register the Copilot Context Provider with Copilot client";
505+
if (e instanceof CopilotContextProviderException) {
506+
message += `: ${e.message} `;
507+
}
508+
return { message };
509+
});
510+
511+
return clientPromise.then((clientResult) => {
512+
if (isBoolean(clientResult) && clientResult) {
507513
properties["cppCodeSnippetsProviderRegistered"] = "true";
508514
telemetry.logCopilotEvent(registerCopilotContextProvider, { ...properties });
509515
return;
510-
} else if (!isBoolean(clientResult) && isString(clientResult.message)) {
511-
properties["error"] = clientResult.message;
512516
} else if (!isBoolean(chatResult) && isString(chatResult.message)) {
513517
properties["error"] = chatResult.message;
518+
} else if (!isBoolean(clientResult) && isString(clientResult.message)) {
519+
properties["error"] = clientResult.message;
514520
} else {
515521
properties["error"] = "Failed to register the Copilot Context Provider for unknown reason.";
516522
}

0 commit comments

Comments
 (0)