Extensibility: VS Code Extension
Relevant source files
The following files were used as context for generating this wiki page:
The OpenCode VS Code extension integrates the OpenCode AI coding agent directly into the Visual Studio Code editor. Its primary purpose is to provide developers with convenient access to OpenCode's terminal-based interface without leaving their editor. This enhances workflow efficiency by allowing users to invoke the agent, manage sessions, and pass file context through commands, keybindings, and UI elements. The extension is one of several integrations, including for Cursor and Windsurf.
The extension is designed to be lightweight, activating only when one of its commands is invoked. It focuses on providing a bridge to the core opencode terminal application.
Sources: sdks/vscode/package.json:3-4, sdks/vscode/README.md, packages/console/app/src/routes/index.tsx:112-116
Manifest: package.json
The package.json file serves as the manifest for the extension, defining its metadata, capabilities, and activation rules for VS Code.
Sources: sdks/vscode/package.json
Key Manifest Properties
| Property | Value | Description |
|---|---|---|
name | opencode | The official identifier for the extension. |
displayName | opencode | The name displayed in the VS Code Marketplace and UI. |
description | opencode for VS Code | A brief description of the extension's purpose. |
publisher | sst-dev | The publisher of the extension. |
engines.vscode | ^1.94.0 | Specifies the minimum required version of VS Code. |
activationEvents | [] | An empty array indicates that the extension uses lazy activation and is loaded only when one of its commands is executed for the first time. |
main | ./dist/extension.js | The entry point file for the extension's code. |
contributes | (object) | Defines the commands, menus, and keybindings the extension adds to VS Code. |
Sources: sdks/vscode/package.json:2-24
Core Functionality
The core logic is contained within sdks/vscode/src/extension.ts. It defines the activate function, which is the main entry point for the extension, and a deactivate function for cleanup.
Sources: sdks/vscode/src/extension.ts
Activation
The activate function is executed by VS Code the first time a user runs one of the extension's commands. Its primary responsibility is to register the commands that the extension provides.
The following diagram illustrates the activation and command registration flow.
Sources: sdks/vscode/src/extension.ts:4-14
Terminal Management
The extension manages interaction with VS Code's integrated terminal.
openTerminal(newTab: boolean): This function is the heart of the terminal interaction.- If
newTabisfalse, it first searches for an existing terminal named "opencode". If found, it shows that terminal. If not, it creates a new one. - If
newTabistrue, it always creates a new terminal named "opencode". - In both cases, it runs the
opencodecommand in the terminal.
- If
Sources: sdks/vscode/src/extension.ts:16-32
Commands and Keybindings
The extension contributes three main commands, each with default keybindings for discoverability and ease of use.
Sources: sdks/vscode/package.json:25-78
| Command ID | Title | Default Keybinding (Mac/Win/Linux) |
|---|---|---|
opencode.openTerminal | Open opencode | cmd+escape / ctrl+escape |
opencode.openNewTerminal | Open opencode in new tab | cmd+shift+escape / ctrl+shift+escape |
opencode.addFilepathToTerminal | Add Filepath to Terminal | cmd+alt+k / ctrl+alt+k |
Sources: sdks/vscode/package.json:27-77
Command Details
opencode.openTerminal: Opens theopencodeCLI in an existing "opencode" terminal or creates a new one if it doesn't exist. This is useful for resuming a session.opencode.openNewTerminal: Always opens theopencodeCLI in a brand new terminal tab. This is useful for starting multiple agent sessions in parallel.opencode.addFilepathToTerminal: Gets the file path of the currently active editor and sends it as an@-mentionto the active terminal. This is a quick way to provide file context to the OpenCode agent.
The sequence diagram below shows the flow for the addFilepathToTerminal command.
User Interface Contributions
The extension adds a button to the editor's title bar for quick access.
- Editor Title Button: An icon is added to the
editor/titlemenu group. Clicking this icon executes theopencode.openNewTerminalcommand. The icon is defined for both light and dark themes.
Sources: sdks/vscode/package.json:33-36, sdks/vscode/package.json:47-52
Development and Debugging
The package.json and CONTRIBUTING.md files provide instructions for developing and debugging the extension.
Build Scripts
The following scripts are defined in package.json for managing the development lifecycle:
| Script | Description |
|---|---|
vscode:prepublish | Runs the package script before publishing. |
compile | Type-checks, lints, and builds the extension using esbuild. |
watch:esbuild | Builds the extension and watches for changes. |
package | Creates a production-ready build of the extension. |
test | Runs the extension tests using @vscode/test-cli. |
Sources: sdks/vscode/package.json:80-91
Debugging Setup
The CONTRIBUTING.md file outlines the recommended process for debugging the extension, acknowledging that Bun's debugging support has some caveats.
- Recommended Method: Run the extension manually in a terminal with the
--inspectflag and attach a debugger.bun run --inspect=<url> dev ... - Caveats:
- Breakpoints in
*.tsxfiles may not be mapped correctly. Using adebugger;statement is the current workaround. - When debugging the TUI and server code together,
bun dev spawnmight be required instead ofbun devbecause the latter runs the server in a worker thread where breakpoints may not work.
- Breakpoints in
- VS Code Configuration: The repository provides example
settings.jsonandlaunch.jsonfiles in the.vscodedirectory to aid in setting up a debugging environment.
Sources: CONTRIBUTING.md:32-61