Skip to content

Commit 4b6229f

Browse files
authored
Fix vscode command in "Open in Copilot Agent Mode" (#20159) (#20162)
1 parent a90a3e8 commit 4b6229f

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

localization/l10n/bundle.l10n.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,7 @@
20082008
},
20092009
"Unknown error": "Unknown error",
20102010
"No active database connection in the current editor. Please establish a connection to continue.": "No active database connection in the current editor. Please establish a connection to continue.",
2011+
"Chat command not available in this VS Code version": "Chat command not available in this VS Code version",
20112012
"$(plug) Connect to MSSQL": "$(plug) Connect to MSSQL",
20122013
"Cancel failed: {0}/{0} is the error message": {
20132014
"message": "Cancel failed: {0}",

localization/xliff/vscode-mssql.xlf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@
429429
<note>{0} is the connection ID
430430
{1} is the database name</note>
431431
</trans-unit>
432+
<trans-unit id="++CODE++6822b8489308b650f2e36cb439297b0c4a43859fe39e164aea3ad453f4bd14d7">
433+
<source xml:lang="en">Chat command not available in this VS Code version</source>
434+
</trans-unit>
432435
<trans-unit id="++CODE++c8cabf4a998e678eb3213d46a3e9e80217b7839fb5f3537ee550a65e3b2202c9">
433436
<source xml:lang="en">Check Constraint</source>
434437
</trans-unit>

src/constants/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ export const cmdAzureSignInToCloud = "azure-account.loginToCloud";
9898
export const cmdAadRemoveAccount = "mssql.removeAadAccount";
9999
export const cmdAadAddAccount = "mssql.addAadAccount";
100100
export const cmdClearAzureTokenCache = "mssql.clearAzureAccountTokenCache";
101+
export const vscodeWorkbenchChatOpenAgent = "workbench.action.chat.openagent";
102+
export const vscodeWorkbenchChatOpenAgentLegacy = "workbench.action.chat.openAgent";
101103
export const cmdShowEstimatedPlan = "mssql.showEstimatedPlan";
102104
export const cmdEnableActualPlan = "mssql.enableActualPlan";
103105
export const cmdDisableActualPlan = "mssql.disableActualPlan";

src/constants/locConstants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,6 +1731,9 @@ export class MssqlChatAgent {
17311731
public static noActiveDatabaseConnection = l10n.t(
17321732
"No active database connection in the current editor. Please establish a connection to continue.",
17331733
);
1734+
public static chatCommandNotAvailable = l10n.t(
1735+
"Chat command not available in this VS Code version",
1736+
);
17341737
}
17351738

17361739
export class QueryEditor {

src/controllers/mainController.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export default class MainController implements vscode.Disposable {
113113
private _queryHistoryProvider: QueryHistoryProvider;
114114
private _scriptingService: ScriptingService;
115115
private _queryHistoryRegistered: boolean = false;
116+
private _availableCommands: string[] | undefined;
116117
public sqlTasksService: SqlTasksService;
117118
public dacFxService: DacFxService;
118119
public schemaCompareService: SchemaCompareService;
@@ -451,10 +452,20 @@ export default class MainController implements vscode.Disposable {
451452
} else if (databaseName === LocalizedConstants.defaultDatabaseLabel) {
452453
connectionCredentials.database = "";
453454
}
454-
vscode.commands.executeCommand(
455-
"workbench.action.chat.openAgent",
456-
`Connect to ${connectionCredentials.server},${connectionCredentials.database}${connectionCredentials.profileName ? ` using profile ${connectionCredentials.profileName}` : ""}.`,
457-
);
455+
456+
// Use the improved command detection
457+
const chatCommand = await this.findChatOpenAgentCommand();
458+
if (chatCommand) {
459+
vscode.commands.executeCommand(
460+
chatCommand,
461+
`Connect to ${connectionCredentials.server},${connectionCredentials.database}${connectionCredentials.profileName ? ` using profile ${connectionCredentials.profileName}` : ""}.`,
462+
);
463+
} else {
464+
// Fallback or error handling
465+
this._vscodeWrapper.showErrorMessage(
466+
LocalizedConstants.MssqlChatAgent.chatCommandNotAvailable,
467+
);
468+
}
458469
},
459470
);
460471

@@ -846,6 +857,37 @@ export default class MainController implements vscode.Disposable {
846857
return this._initialized;
847858
}
848859

860+
/**
861+
* Get available VS Code commands (cached for performance)
862+
*/
863+
private async getAvailableCommands(): Promise<string[]> {
864+
if (!this._availableCommands) {
865+
this._availableCommands = await vscode.commands.getCommands();
866+
}
867+
return this._availableCommands;
868+
}
869+
870+
/**
871+
* Find the correct chat open agent command variant that exists in the current VS Code version
872+
*/
873+
private async findChatOpenAgentCommand(): Promise<string | undefined> {
874+
const commands = await this.getAvailableCommands();
875+
876+
// Try to find the correct command, checking both variants
877+
const possibleCommands = [
878+
Constants.vscodeWorkbenchChatOpenAgent, // Current VS Code
879+
Constants.vscodeWorkbenchChatOpenAgentLegacy, // Legacy VS Code
880+
];
881+
882+
for (const cmd of possibleCommands) {
883+
if (commands.includes(cmd)) {
884+
return cmd;
885+
}
886+
}
887+
888+
return undefined;
889+
}
890+
849891
public get context(): vscode.ExtensionContext {
850892
return this._context;
851893
}

0 commit comments

Comments
 (0)