Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

## Unreleased

### Added

- The extension is now enabled by default in the VS Code Agents window.
- `Coder: Open Workspace in Agents Window` opens a workspace in the VS Code
Agents window, starting it first if needed. It opens the template's
directory, or the folder you last opened on the workspace.

### Security

- Workspace hosts now set `PermitLocalCommand no`, so a `LocalCommand` in your
Expand Down
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@
"category": "Coder",
"icon": "$(play)"
},
{
"command": "coder.openInAgentsWindow",
"title": "Open Workspace in Agents Window",
"category": "Coder"
},
{
"command": "coder.openDevContainer",
"title": "Open Dev Container",
Expand Down Expand Up @@ -674,6 +679,10 @@
"command": "coder.openFromSidebar",
"when": "false"
},
{
"command": "coder.openInAgentsWindow",
"when": "coder.authenticated && coder.agentsWindowSupported"
},
{
"command": "coder.openDevContainer",
"when": "false"
Expand Down Expand Up @@ -925,6 +934,9 @@
},
"icon": "media/logo.png",
"capabilities": {
"agentsWindow": {
"supported": true
},
"untrustedWorkspaces": {
"supported": true
}
Expand Down
190 changes: 164 additions & 26 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
} from "./instrumentation/diagnostics";
import { WorkspaceOperationTelemetry } from "./instrumentation/workspace";
import {
type AgentsWindowOpenTrace,
type DevContainerMode,
type WorkspaceOpenSource,
WorkspaceOpenTelemetry,
Expand All @@ -44,6 +45,7 @@ import {
type WorkspacePickerResult,
type WorkspacePickerSource,
} from "./instrumentation/workspaceOpen";
import { prepareAgentsWindowConnection } from "./remote/agentsWindowConnection";
import {
LegacyEditorId,
currentEditorId,
Expand All @@ -58,6 +60,7 @@ import {
applySettingOverrides,
getRecommendedSshSettings,
} from "./remote/sshOverrides";
import { WorkspaceStartCancelledError } from "./remote/workspaceStateMachine";
import { isKeyringEnabled, resolveCliAuth } from "./settings/cli";
import { appendVsCodeLogs } from "./supportBundle/appendVsCodeLogs";
import {
Expand All @@ -71,6 +74,7 @@ import {
withProgress,
} from "./ui/progress";
import { maybeAskAgent, maybeAskUrl } from "./ui/prompts";
import { openAgentsWindow } from "./vscode/agentsWindow";
import { vscodeProposed } from "./vscode/proposed";
import { parseNetcheckReport } from "./webviews/netcheck/types";
import { parseSpeedtestResult } from "./webviews/speedtest/types";
Expand All @@ -91,6 +95,7 @@ import type { ServiceContainer } from "./container";
import type { DeploymentManager } from "./deployment/deploymentManager";
import type { Logger } from "./logging/logger";
import type { LoginCoordinator, LoginMethod } from "./login/loginCoordinator";
import type { ConnectionSetup } from "./remote/connectionSetup";
import type { MementoManager } from "./storage/mementoManager";
import type { PathResolver } from "./storage/pathResolver";
import type { SecretsManager, SessionAuth } from "./storage/secretsManager";
Expand Down Expand Up @@ -155,7 +160,14 @@ type SelectedWorkspace = Extract<WorkspaceResolution, { status: "selected" }>;

type OpenWorkspaceResult =
| { readonly status: "opened"; readonly handoff: "folder" | "empty_window" }
| { readonly status: "cancelled"; readonly stage: "recent_folder_picker" };
| {
readonly status: "cancelled";
readonly stage: "recent_folder_picker" | "start_prompt";
};

type FolderChoice =
| { readonly status: "chosen"; readonly folderPath?: string }
| { readonly status: "cancelled" };

const UPDATE_AND_RESTART_ACTION = "Update and Restart";

Expand Down Expand Up @@ -211,9 +223,10 @@ export class Commands {
public remoteWorkspaceClient?: CoderApi;

public constructor(
serviceContainer: ServiceContainer,
private readonly serviceContainer: ServiceContainer,
private readonly extensionClient: CoderApi,
private readonly deploymentManager: DeploymentManager,
private readonly connectionSetup: ConnectionSetup,
) {
this.telemetryService = serviceContainer.getTelemetryService();
this.authTelemetry = new AuthTelemetry(this.telemetryService);
Expand Down Expand Up @@ -1208,6 +1221,104 @@ export class Commands {
return recordOpenResult(telemetry, selection, result);
}

/**
* Open a workspace of the logged-in deployment in the VS Code Agents window,
* starting it first if needed.
*/
public async openInAgentsWindow(): Promise<boolean> {
return this.workspaceOpenTelemetry.traceAgentsWindow((telemetry) =>
this.runOpenInAgentsWindow(telemetry),
);
}

private async runOpenInAgentsWindow(
telemetry: AgentsWindowOpenTrace,
): Promise<boolean> {
const baseUrl = this.requireExtensionBaseUrl();
const pick = await this.pickWorkspace("workspace_open");
if (pick.status === "cancelled") {
telemetry.abort("workspace_picker");
return false;
}
if (pick.status === "failed") {
telemetry.error(pick.category);
return false;
}
const agents = await this.extractAgentsWithFallback(pick.workspace);
const picked = await maybeAskAgent(agents);
if (!picked) {
telemetry.abort("agent_picker", { workspace: pick.workspace });
return false;
}
telemetry.select({ workspace: pick.workspace, agent: picked });

const remoteAuthority = toRemoteAuthority(
baseUrl,
pick.workspace.owner_name,
pick.workspace.name,
picked.name,
);
let ready: { workspace: Workspace; agent: WorkspaceAgent };
try {
ready = await prepareAgentsWindowConnection({
client: this.extensionClient,
baseUrl,
workspace: pick.workspace,
remoteAuthority,
connectionSetup: this.connectionSetup,
serviceContainer: this.serviceContainer,
trace: telemetry,
});
} catch (error) {
if (error instanceof WorkspaceStartCancelledError) {
return recordOpenResult(
telemetry,
{ workspace: pick.workspace, agent: picked },
{ status: "cancelled", stage: "start_prompt" },
);
}
throw error;
}

const { folders } = await this.recentlyOpened();
const folder = await this.chooseFolder(
folders,
remoteAuthority,
ready.agent,
);
if (folder.status === "cancelled") {
return recordOpenResult(telemetry, ready, {
status: "cancelled",
stage: "recent_folder_picker",
});
}
const { folderPath } = folder;
const handoff = folderPath ? "folder" : "empty_window";
if (!folderPath) {
void vscode.window.showWarningMessage(
`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.`,
);
}

this.logger.info("Opening workspace in the Agents window", {
remoteAuthority,
handoff,
});
await openAgentsWindow(
folderPath
? vscode.Uri.from({
scheme: "vscode-remote",
authority: remoteAuthority,
path: folderPath,
})
: undefined,
);
return recordOpenResult(telemetry, ready, {
status: "opened",
handoff,
});
}

/**
* Open a devcontainer from a workspace belonging to the currently logged-in deployment.
*
Expand Down Expand Up @@ -1712,44 +1823,27 @@ export class Commands {
}

/**
* Given a workspace and agent, build the host name, find a directory to open,
* and pass both to the Remote SSH plugin in the form of a remote authority
* URI.
*
* If provided, folderPath is always used, otherwise expanded_directory from
* the agent is used.
* The folder to open on the agent: `folderPath`, else the template's default
* directory, else a recently opened folder on this workspace.
*/
private async openWorkspace(
baseUrl: string,
workspace: Workspace,
private async chooseFolder(
folders: vscode.Uri[],
remoteAuthority: string,
agent: WorkspaceAgent,
options: Pick<
OpenOptions,
"folderPath" | "openRecent" | "useDefaultDirectory"
> = {},
): Promise<OpenWorkspaceResult> {
): Promise<FolderChoice> {
const { openRecent, useDefaultDirectory } = {
...openDefaults,
...options,
};
let { folderPath } = options;
let remoteAuthority = toRemoteAuthority(
baseUrl,
workspace.owner_name,
workspace.name,
agent.name,
);

let newWindow = true;
// Open in the existing window if no workspaces are open.
if (!vscode.workspace.workspaceFolders?.length) {
newWindow = false;
}
if (!folderPath && useDefaultDirectory) {
folderPath = agent.expanded_directory;
}

const { folders, entries } = await this.recentlyOpened();
// If the agent had no folder or we have been asked to open the most recent,
// we can try to open a recently opened folder/workspace.
if (!folderPath || openRecent) {
Expand All @@ -1773,10 +1867,54 @@ export class Commands {
});
if (!folderPath) {
// User aborted.
return { status: "cancelled", stage: "recent_folder_picker" };
return { status: "cancelled" };
}
}
}
return { status: "chosen", folderPath };
}

/**
* Given a workspace and agent, build the host name, find a directory to open,
* and pass both to the Remote SSH plugin in the form of a remote authority
* URI.
*
* If provided, folderPath is always used, otherwise expanded_directory from
* the agent is used.
*/
private async openWorkspace(
baseUrl: string,
workspace: Workspace,
agent: WorkspaceAgent,
options: Pick<
OpenOptions,
"folderPath" | "openRecent" | "useDefaultDirectory"
> = {},
): Promise<OpenWorkspaceResult> {
let remoteAuthority = toRemoteAuthority(
baseUrl,
workspace.owner_name,
workspace.name,
agent.name,
);

let newWindow = true;
// Open in the existing window if no workspaces are open.
if (!vscode.workspace.workspaceFolders?.length) {
newWindow = false;
}

const { folders, entries } = await this.recentlyOpened();
const folder = await this.chooseFolder(
folders,
remoteAuthority,
agent,
options,
);
if (folder.status === "cancelled") {
return { status: "cancelled", stage: "recent_folder_picker" };
}
const { folderPath } = folder;
remoteAuthority = this.reusableAuthority(entries, remoteAuthority);

// Only set the memento when opening a new folder/window
Expand Down
25 changes: 22 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import {
type ActivationTracer,
} from "./instrumentation/activation";
import { OAuthSessionManager } from "./oauth/sessionManager";
import { ConnectionSetup } from "./remote/connectionSetup";
import { Remote } from "./remote/remote";
import { getRemoteSshExtension } from "./remote/sshExtension";
import { registerUriHandler } from "./uri/uriHandler";
import { canOpenAgentsWindow, isAgentsWindow } from "./vscode/agentsWindow";
import { initVscodeProposed } from "./vscode/proposed";
import { TasksPanelProvider } from "./webviews/tasks/tasksPanelProvider";
import { WorkspaceActionRunner } from "./webviews/workspaces/workspaceActionRunner";
Expand Down Expand Up @@ -76,7 +78,8 @@ async function doActivate(
path.join(remoteSshExtension.extensionPath, "package.json"),
);
vscodeProposed = extensionRequire("vscode") as typeof vscode;
} else {
} else if (!isAgentsWindow()) {
// The Agents window disables Remote SSH, so its absence there is expected.
vscode.window.showErrorMessage(
"Remote SSH extension not found, this may not work as expected.\n" +
// NB should we link to documentation or marketplace?
Expand Down Expand Up @@ -195,6 +198,9 @@ async function doActivate(
contextManager.set("coder.tasksSupported", supported);
};
void updateTasksSupported();
void canOpenAgentsWindow().then((supported) =>
contextManager.set("coder.agentsWindowSupported", supported),
);

ctx.subscriptions.push(
deploymentManager.session.onDidChange(() => {
Expand All @@ -204,7 +210,16 @@ async function doActivate(

// Register globally available commands. Many of these have visibility
// controlled by contexts, see `when` in the package.json.
const commands = new Commands(serviceContainer, client, deploymentManager);
const connectionSetup = new ConnectionSetup(
serviceContainer,
ctx.extensionMode,
);
const commands = new Commands(
serviceContainer,
client,
deploymentManager,
connectionSetup,
);

// Placeholder tree view for the coderTasks container when not authenticated.
// Works around a Cursor bug where containers with all views hidden via `when`
Expand Down Expand Up @@ -349,6 +364,10 @@ async function doActivate(
commands.switchDeployment.bind(commands),
);
commandManager.register("coder.open", commands.open.bind(commands));
commandManager.register(
"coder.openInAgentsWindow",
commands.openInAgentsWindow.bind(commands),
);
commandManager.register(
"coder.openDevContainer",
commands.openDevContainer.bind(commands),
Expand Down Expand Up @@ -430,7 +449,7 @@ async function doActivate(
commands.supportBundle.bind(commands),
);

const remote = new Remote(serviceContainer, commands, ctx);
const remote = new Remote(serviceContainer, commands, ctx, connectionSetup);

// Respond to PINGs and DUPLICATE commands from other windows.
const duplicateWorkspaceIpc = serviceContainer.getDuplicateWorkspaceIpc();
Expand Down
Loading
Loading