Skip to content

Commit 57d9583

Browse files
committed
feat: add a command to open a workspace in the VS Code Agents window
Coder: Open Workspace in Agents Window starts the workspace if needed, writes the SSH config to ~/.ssh/config for plain ssh, and opens the template's directory (or the last folder opened on the workspace) in the Agents window. The command shows only where VS Code has the Agents window.
1 parent f955f32 commit 57d9583

18 files changed

Lines changed: 694 additions & 63 deletions

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
- The slow-connection warning tooltip now links to the networking
1414
troubleshooting guide, opening the relay section when connected through a
1515
relay.
16+
- `Coder: Open Workspace in Agents Window` opens a workspace in the VS Code
17+
Agents window, starting it first if needed. It opens the template's
18+
directory, or the folder you last opened on the workspace.
1619

1720
### Fixed
1821

‎package.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@
442442
"category": "Coder",
443443
"icon": "$(play)"
444444
},
445+
{
446+
"command": "coder.openInAgentsWindow",
447+
"title": "Open Workspace in Agents Window",
448+
"category": "Coder"
449+
},
445450
{
446451
"command": "coder.openDevContainer",
447452
"title": "Open Dev Container",
@@ -675,6 +680,10 @@
675680
"command": "coder.openFromSidebar",
676681
"when": "false"
677682
},
683+
{
684+
"command": "coder.openInAgentsWindow",
685+
"when": "coder.authenticated && coder.agentsWindowSupported"
686+
},
678687
{
679688
"command": "coder.openDevContainer",
680689
"when": "false"

‎src/commands.ts‎

Lines changed: 164 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
} from "./instrumentation/diagnostics";
3737
import { WorkspaceOperationTelemetry } from "./instrumentation/workspace";
3838
import {
39+
type AgentsWindowOpenTrace,
3940
type DevContainerMode,
4041
type WorkspaceOpenSource,
4142
WorkspaceOpenTelemetry,
@@ -44,6 +45,7 @@ import {
4445
type WorkspacePickerResult,
4546
type WorkspacePickerSource,
4647
} from "./instrumentation/workspaceOpen";
48+
import { prepareAgentsWindowConnection } from "./remote/agentsWindowConnection";
4749
import {
4850
LegacyEditorId,
4951
currentEditorId,
@@ -58,6 +60,7 @@ import {
5860
applySettingOverrides,
5961
getRecommendedSshSettings,
6062
} from "./remote/sshOverrides";
63+
import { WorkspaceStartCancelledError } from "./remote/workspaceStateMachine";
6164
import { isKeyringEnabled, resolveCliAuth } from "./settings/cli";
6265
import { appendVsCodeLogs } from "./supportBundle/appendVsCodeLogs";
6366
import {
@@ -71,6 +74,7 @@ import {
7174
withProgress,
7275
} from "./ui/progress";
7376
import { maybeAskAgent, maybeAskUrl } from "./ui/prompts";
77+
import { openAgentsWindow } from "./vscode/agentsWindow";
7478
import { vscodeProposed } from "./vscode/proposed";
7579
import { parseNetcheckReport } from "./webviews/netcheck/types";
7680
import { parseSpeedtestResult } from "./webviews/speedtest/types";
@@ -91,6 +95,7 @@ import type { ServiceContainer } from "./container";
9195
import type { DeploymentManager } from "./deployment/deploymentManager";
9296
import type { Logger } from "./logging/logger";
9397
import type { LoginCoordinator, LoginMethod } from "./login/loginCoordinator";
98+
import type { ConnectionSetup } from "./remote/connectionSetup";
9499
import type { MementoManager } from "./storage/mementoManager";
95100
import type { PathResolver } from "./storage/pathResolver";
96101
import type { SecretsManager, SessionAuth } from "./storage/secretsManager";
@@ -155,7 +160,14 @@ type SelectedWorkspace = Extract<WorkspaceResolution, { status: "selected" }>;
155160

156161
type OpenWorkspaceResult =
157162
| { readonly status: "opened"; readonly handoff: "folder" | "empty_window" }
158-
| { readonly status: "cancelled"; readonly stage: "recent_folder_picker" };
163+
| {
164+
readonly status: "cancelled";
165+
readonly stage: "recent_folder_picker" | "start_prompt";
166+
};
167+
168+
type FolderChoice =
169+
| { readonly status: "chosen"; readonly folderPath?: string }
170+
| { readonly status: "cancelled" };
159171

160172
const UPDATE_AND_RESTART_ACTION = "Update and Restart";
161173

@@ -211,9 +223,10 @@ export class Commands {
211223
public remoteWorkspaceClient?: CoderApi;
212224

213225
public constructor(
214-
serviceContainer: ServiceContainer,
226+
private readonly serviceContainer: ServiceContainer,
215227
private readonly extensionClient: CoderApi,
216228
private readonly deploymentManager: DeploymentManager,
229+
private readonly connectionSetup: ConnectionSetup,
217230
) {
218231
this.telemetryService = serviceContainer.getTelemetryService();
219232
this.authTelemetry = new AuthTelemetry(this.telemetryService);
@@ -1199,6 +1212,104 @@ export class Commands {
11991212
return recordOpenResult(telemetry, selection, result);
12001213
}
12011214

1215+
/**
1216+
* Open a workspace of the logged-in deployment in the VS Code Agents window,
1217+
* starting it first if needed.
1218+
*/
1219+
public async openInAgentsWindow(): Promise<boolean> {
1220+
return this.workspaceOpenTelemetry.traceAgentsWindow((telemetry) =>
1221+
this.runOpenInAgentsWindow(telemetry),
1222+
);
1223+
}
1224+
1225+
private async runOpenInAgentsWindow(
1226+
telemetry: AgentsWindowOpenTrace,
1227+
): Promise<boolean> {
1228+
const baseUrl = this.requireExtensionBaseUrl();
1229+
const pick = await this.pickWorkspace("workspace_open");
1230+
if (pick.status === "cancelled") {
1231+
telemetry.abort("workspace_picker");
1232+
return false;
1233+
}
1234+
if (pick.status === "failed") {
1235+
telemetry.error(pick.category);
1236+
return false;
1237+
}
1238+
const agents = await this.extractAgentsWithFallback(pick.workspace);
1239+
const picked = await maybeAskAgent(agents);
1240+
if (!picked) {
1241+
telemetry.abort("agent_picker", { workspace: pick.workspace });
1242+
return false;
1243+
}
1244+
telemetry.select({ workspace: pick.workspace, agent: picked });
1245+
1246+
const remoteAuthority = toRemoteAuthority(
1247+
baseUrl,
1248+
pick.workspace.owner_name,
1249+
pick.workspace.name,
1250+
picked.name,
1251+
);
1252+
let ready: { workspace: Workspace; agent: WorkspaceAgent };
1253+
try {
1254+
ready = await prepareAgentsWindowConnection({
1255+
client: this.extensionClient,
1256+
baseUrl,
1257+
workspace: pick.workspace,
1258+
remoteAuthority,
1259+
connectionSetup: this.connectionSetup,
1260+
serviceContainer: this.serviceContainer,
1261+
trace: telemetry,
1262+
});
1263+
} catch (error) {
1264+
if (error instanceof WorkspaceStartCancelledError) {
1265+
return recordOpenResult(
1266+
telemetry,
1267+
{ workspace: pick.workspace, agent: picked },
1268+
{ status: "cancelled", stage: "start_prompt" },
1269+
);
1270+
}
1271+
throw error;
1272+
}
1273+
1274+
const { folders } = await this.recentlyOpened();
1275+
const folder = await this.chooseFolder(
1276+
folders,
1277+
remoteAuthority,
1278+
ready.agent,
1279+
);
1280+
if (folder.status === "cancelled") {
1281+
return recordOpenResult(telemetry, ready, {
1282+
status: "cancelled",
1283+
stage: "recent_folder_picker",
1284+
});
1285+
}
1286+
const { folderPath } = folder;
1287+
const handoff = folderPath ? "folder" : "empty_window";
1288+
if (!folderPath) {
1289+
void vscode.window.showWarningMessage(
1290+
`The template for ${createWorkspaceIdentifier(ready.workspace)} sets no directory and no folder was opened on it before, so the Agents window opens without a folder on it.`,
1291+
);
1292+
}
1293+
1294+
this.logger.info("Opening workspace in the Agents window", {
1295+
remoteAuthority,
1296+
handoff,
1297+
});
1298+
await openAgentsWindow(
1299+
folderPath
1300+
? vscode.Uri.from({
1301+
scheme: "vscode-remote",
1302+
authority: remoteAuthority,
1303+
path: folderPath,
1304+
})
1305+
: undefined,
1306+
);
1307+
return recordOpenResult(telemetry, ready, {
1308+
status: "opened",
1309+
handoff,
1310+
});
1311+
}
1312+
12021313
/**
12031314
* Open a devcontainer from a workspace belonging to the currently logged-in deployment.
12041315
*
@@ -1703,44 +1814,27 @@ export class Commands {
17031814
}
17041815

17051816
/**
1706-
* Given a workspace and agent, build the host name, find a directory to open,
1707-
* and pass both to the Remote SSH plugin in the form of a remote authority
1708-
* URI.
1709-
*
1710-
* If provided, folderPath is always used, otherwise expanded_directory from
1711-
* the agent is used.
1817+
* The folder to open on the agent: `folderPath`, else the template's default
1818+
* directory, else a recently opened folder on this workspace.
17121819
*/
1713-
private async openWorkspace(
1714-
baseUrl: string,
1715-
workspace: Workspace,
1820+
private async chooseFolder(
1821+
folders: vscode.Uri[],
1822+
remoteAuthority: string,
17161823
agent: WorkspaceAgent,
17171824
options: Pick<
17181825
OpenOptions,
17191826
"folderPath" | "openRecent" | "useDefaultDirectory"
17201827
> = {},
1721-
): Promise<OpenWorkspaceResult> {
1828+
): Promise<FolderChoice> {
17221829
const { openRecent, useDefaultDirectory } = {
17231830
...openDefaults,
17241831
...options,
17251832
};
17261833
let { folderPath } = options;
1727-
let remoteAuthority = toRemoteAuthority(
1728-
baseUrl,
1729-
workspace.owner_name,
1730-
workspace.name,
1731-
agent.name,
1732-
);
1733-
1734-
let newWindow = true;
1735-
// Open in the existing window if no workspaces are open.
1736-
if (!vscode.workspace.workspaceFolders?.length) {
1737-
newWindow = false;
1738-
}
17391834
if (!folderPath && useDefaultDirectory) {
17401835
folderPath = agent.expanded_directory;
17411836
}
17421837

1743-
const { folders, entries } = await this.recentlyOpened();
17441838
// If the agent had no folder or we have been asked to open the most recent,
17451839
// we can try to open a recently opened folder/workspace.
17461840
if (!folderPath || openRecent) {
@@ -1764,10 +1858,54 @@ export class Commands {
17641858
});
17651859
if (!folderPath) {
17661860
// User aborted.
1767-
return { status: "cancelled", stage: "recent_folder_picker" };
1861+
return { status: "cancelled" };
17681862
}
17691863
}
17701864
}
1865+
return { status: "chosen", folderPath };
1866+
}
1867+
1868+
/**
1869+
* Given a workspace and agent, build the host name, find a directory to open,
1870+
* and pass both to the Remote SSH plugin in the form of a remote authority
1871+
* URI.
1872+
*
1873+
* If provided, folderPath is always used, otherwise expanded_directory from
1874+
* the agent is used.
1875+
*/
1876+
private async openWorkspace(
1877+
baseUrl: string,
1878+
workspace: Workspace,
1879+
agent: WorkspaceAgent,
1880+
options: Pick<
1881+
OpenOptions,
1882+
"folderPath" | "openRecent" | "useDefaultDirectory"
1883+
> = {},
1884+
): Promise<OpenWorkspaceResult> {
1885+
let remoteAuthority = toRemoteAuthority(
1886+
baseUrl,
1887+
workspace.owner_name,
1888+
workspace.name,
1889+
agent.name,
1890+
);
1891+
1892+
let newWindow = true;
1893+
// Open in the existing window if no workspaces are open.
1894+
if (!vscode.workspace.workspaceFolders?.length) {
1895+
newWindow = false;
1896+
}
1897+
1898+
const { folders, entries } = await this.recentlyOpened();
1899+
const folder = await this.chooseFolder(
1900+
folders,
1901+
remoteAuthority,
1902+
agent,
1903+
options,
1904+
);
1905+
if (folder.status === "cancelled") {
1906+
return { status: "cancelled", stage: "recent_folder_picker" };
1907+
}
1908+
const { folderPath } = folder;
17711909
remoteAuthority = this.reusableAuthority(entries, remoteAuthority);
17721910

17731911
// Only set the memento when opening a new folder/window

‎src/extension.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ConnectionSetup } from "./remote/connectionSetup";
2828
import { Remote } from "./remote/remote";
2929
import { getRemoteSshExtension } from "./remote/sshExtension";
3030
import { registerUriHandler } from "./uri/uriHandler";
31-
import { isAgentsWindow } from "./vscode/agentsWindow";
31+
import { canOpenAgentsWindow, isAgentsWindow } from "./vscode/agentsWindow";
3232
import { initVscodeProposed } from "./vscode/proposed";
3333
import { TasksPanelProvider } from "./webviews/tasks/tasksPanelProvider";
3434
import { WorkspaceActionRunner } from "./webviews/workspaces/workspaceActionRunner";
@@ -198,6 +198,9 @@ async function doActivate(
198198
contextManager.set("coder.tasksSupported", supported);
199199
};
200200
void updateTasksSupported();
201+
void canOpenAgentsWindow().then((supported) =>
202+
contextManager.set("coder.agentsWindowSupported", supported),
203+
);
201204

202205
ctx.subscriptions.push(
203206
deploymentManager.session.onDidChange(() => {
@@ -211,7 +214,12 @@ async function doActivate(
211214
serviceContainer,
212215
ctx.extensionMode,
213216
);
214-
const commands = new Commands(serviceContainer, client, deploymentManager);
217+
const commands = new Commands(
218+
serviceContainer,
219+
client,
220+
deploymentManager,
221+
connectionSetup,
222+
);
215223

216224
// Placeholder tree view for the coderTasks container when not authenticated.
217225
// Works around a Cursor bug where containers with all views hidden via `when`
@@ -356,6 +364,10 @@ async function doActivate(
356364
commands.switchDeployment.bind(commands),
357365
);
358366
commandManager.register("coder.open", commands.open.bind(commands));
367+
commandManager.register(
368+
"coder.openInAgentsWindow",
369+
commands.openInAgentsWindow.bind(commands),
370+
);
359371
commandManager.register(
360372
"coder.openDevContainer",
361373
commands.openDevContainer.bind(commands),

0 commit comments

Comments
 (0)