code --install-extension SahilTiwaskar.vscode-context-bridgeThe extension exposes editor context information via HTTP and WebSocket APIs, allowing external applications to access real-time VS Code editor state. This can be useful for building integrations, tools, or services that need to interact with the VS Code editor environment.
- Real-time Updates: WebSocket support for live context updates
- Active File Information: Get details about the currently open and active file
- Text Selection: Access currently selected text with position information
- Open Tabs: List all files/tabs currently open in the workspace
- Code Diffs: Retrieve code changes and modifications in the workspace
- Diagnostics: Access linting errors, warnings, and other diagnostics
- Command Execution: Send commands to VS Code from external processes
- Change Proposals: External tools can propose code changes with accept/reject interface
- Configurable: Control what information is shared for privacy
- Visual Studio Code 1.74.0 or higher
- Node.js 16.0.0 or higher
- Clone the repository:
git clone https://github.com/tsahil01/vscode-context-bridge
cd vscode-context-bridge- Install dependencies:
npm install
npm install -g vsce- Compile the extension:
npm run compile- Package the extension (optional):
npm run vscode:prepublish- Generate the VSIX file:
npm run package- Open VS Code
- Press
Ctrl+Shift+P(orCmd+Shift+Pon macOS) to open the command palette - Type "Extensions: Install from VSIX" and select it
- Choose the compiled extension file (if packaged) or use the development version
- Open VS Code with a workspace
- Press
Ctrl+Shift+Pto open the command palette - Type
Start Context Bridge Serverand select it - The server will start on the configured port (default: 3210)
The extension can be configured through the VS Code settings:
- Open settings (
Ctrl+,orCmd+,on macOS) - Search for
Context Bridgeand adjust the things you want to change. - OR you can change it from the status bar by clicking the
Context Bridgeicon.
Retrieve all context information.
Response:
{
"activeFile": {
"path": "/path/to/file.ts",
"name": "file.ts",
"language": "typescript",
"content": "file content...",
"lineCount": 100,
"isDirty": false
},
"textSelection": {
"text": "selected text",
"startLine": 10,
"startCharacter": 5,
"endLine": 10,
"endCharacter": 15,
"range": {
"start": {"line": 10, "character": 5},
"end": {"line": 10, "character": 15}
}
},
"openTabs": [
{
"path": "/path/to/file.ts",
"name": "file.ts",
"language": "typescript",
"isActive": true,
"isDirty": false
}
],
"diffs": [
{
"filePath": "/path/to/file.ts",
"fileName": "file.ts",
"changes": [
{
"type": "add",
"lineNumber": 1,
"newText": "added content"
},
{
"type": "delete",
"lineNumber": 2,
"originalText": "deleted content"
},
{
"type": "modify",
"lineNumber": 3,
"newText": "modified content"
}
]
}
],
"diagnostics": [
{
"filePath": "/path/to/file.ts",
"fileName": "file.ts",
"diagnostics": [
{
"message": "Error message",
"severity": "error",
"range": {
"start": {"line": 10, "character": 5},
"end": {"line": 10, "character": 15}
},
"source": "typescript",
"code": "TS2304"
}
]
}
],
"timestamp": 1640995200000
}Execute a command in VS Code.
Request:
{
"command": "openFile",
"arguments": ["/path/to/file.ts"],
"options": {"preview": false}
}Response:
{
"success": true,
"data": {
"filePath": "/path/to/file.ts"
},
"message": "Command executed successfully"
}Propose a code change that will be shown to the user with accept/reject options.
Request:
{
"title": "Add error handling",
"filePath": "/path/to/file.js",
"changes": [
{
"originalContent": "function getData() {\n const data = JSON.parse(response);\n return data;\n}",
"proposedContent": "function getData() {\n try {\n const data = JSON.parse(response);\n return data;\n } catch (error) {\n console.error('Failed to parse response:', error);\n return null;\n }\n}",
"description": "Add try-catch error handling"
}
]
}Response:
{
"success": true,
"proposalId": "change_1640995200000_abc123",
"data": {
"proposalId": "change_1640995200000_abc123",
"filePath": "/path/to/file.js"
},
"message": "Change proposal change_1640995200000_abc123 accepted and applied",
"accepted": true
}Available Commands:
openFile(filePath, options): Open a file in editorselectText(startLine, startChar, endLine, endChar): Select text in the active editorwriteFile(filePath, content): Write content to a filedeleteFile(filePath): Delete a fileshowNotification(message, type): Show a notification (type: 'info', 'warning', 'error')proposeChange(proposalRequest): Propose a code change with accept/reject dialogacceptProposal(proposalId): Accept a change proposalrejectProposal(proposalId): Reject a change proposal
Health check endpoint.
Response:
{
"status": "ok",
"server": "running"
}Connect to ws://localhost:3210 for real-time updates.
Client Message Types:
- Get Context
{
"type": "getContext"
}- Execute Command
{
"type": "command",
"command": {
"command": "openFile",
"arguments": ["/path/to/file.ts"],
"options": {"preview": false}
}
}Server Response Types:
- Context Update
{
"type": "context",
"data": {
// Same as HTTP /context response
}
}- Command Response
{
"type": "commandResponse",
"data": {
"success": true,
"data": {"filePath": "/path/to/file.ts"},
"message": "Command executed successfully"
}
}- Error Response
{
"error": "Invalid msg format"
}Real-time Updates: The WebSocket connection automatically receives context updates when:
- Active file changes
- Text selection changes
- Document content changes
See examples/client.js for a complete Node.js client implementation that demonstrates how to connect to the Context Bridge server via HTTP and WebSocket, and how to use its API.
- Disable tool: The extension can be disabled in VS Code settings to prevent unauthorized access over HTTP/WebSocket networks.
- Network Access: The server binds to localhost by default for security
- Information Sharing: Configure what information is shared based on your needs
- Port Already in Use: Change the port in VS Code settings
- Connection Refused: Ensure the VS Code extension server is running
- Permission Denied: Check file permissions for the workspace
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the ISC License - see the LICENSE file for details.
For issues and feature requests, please use the GitHub issue tracker.