The VS Code Extension API is the primary interface through which extensions interact with the workbench. It is implemented as a decoupled system where the extension code runs in a dedicated Extension Host process, while the editor UI and core logic reside in the Main Thread (Renderer process). Communication between these two occurs over an RPC protocol using Data Transfer Objects (DTOs).
The vscode API object available to extensions is not a simple static object but is dynamically constructed by the API factory. This factory pattern ensures that each extension host (whether local, remote, or web-worker) receives an implementation tailored to its environment while maintaining a consistent surface area defined in vscode.d.ts [src/vscode-dts/vscode.d.ts:6-7].
ExtHostDocuments, ExtHostLanguageFeatures, ExtHostChatAgents2). [src/vs/workbench/api/common/extHost.api.impl.ts:34-105]IExtHostRpcService to establish the connection back to the Main Thread via MainContext and ExtHostContext identifiers. [src/vs/workbench/api/common/extHost.api.impl.ts:94-94]vscode namespace by aggregating services into categories like workspace, window, languages, and chat. [src/vs/workbench/api/common/extHost.api.impl.ts:1030-1050]The following diagram illustrates how the vscode object is instantiated and how it maps internal implementation classes to the public API.
Sources: [src/vs/workbench/api/common/extHost.api.impl.ts:34-105], [src/vs/workbench/api/common/extHost.protocol.ts:33-33]
Because the Extension Host and the Main Thread are in separate processes, they cannot share live objects. Every piece of data sent across the boundary must be serialized into a DTO.
vscode.Range, vscode.Position). They often contain helper methods like contains(). [src/vs/workbench/api/common/extHostTypes.ts:27-45]extHostTypes into plain JSON-compatible objects (DTOs). For example, typeConvert.Range.from converts a vscode.Range to an extHostProtocol.IRange. [src/vs/workbench/api/common/extHostTypeConverters.ts:30-30]vscode.Uri objects are correctly revived as URI instances on the other side. [src/vs/workbench/api/common/extHost.protocol.ts:18-18]When a notebook kernel provides output, the flow is:
vscode.NotebookCellOutput (from extHostTypes). [src/vs/workbench/api/common/extHostTypes.ts:42-42]typeConvert.NotebookCellOutput.from converts it to an extHostProtocol.INotebookOutputDto. [src/vs/workbench/api/common/extHostTypeConverters.ts:58-60]MainThreadNotebooks receives the DTO and passes it to the NotebookTextModel in the renderer.Sources: [src/vs/workbench/api/common/extHost.protocol.ts:1-50], [src/vs/workbench/api/common/extHostTypeConverters.ts:1-40], [src/vs/workbench/api/common/extHostTypes.ts:1-50]
Main Thread actors (prefixed with MainThread) are the "customers" of the Extension Host. They reside in the workbench and implement the Shape interfaces defined in the RPC protocol [src/vs/workbench/api/common/extHost.protocol.ts:33-36].
| Main Thread Class | Responsibility | ExtHost Peer |
|---|---|---|
MainThreadLanguageFeatures | Registers providers in ILanguageFeaturesService | ExtHostLanguageFeatures |
MainThreadDocuments | Syncs text model changes to the ext host | ExtHostDocuments |
MainThreadChatAgents2 | Manages chat participants and progress streams | ExtHostChatAgents2 |
MainThreadNotebooks | Orchestrates notebook model and kernel communication | ExtHostNotebookController |
Sources: [src/vs/workbench/api/browser/mainThreadLanguageFeatures.ts:46-62], [src/vs/workbench/api/common/extHostLanguageFeatures.ts:44-105], [src/vs/workbench/api/common/extHost.protocol.ts:33-36]
Chat Agents use a streaming pattern to provide real-time feedback. ExtHostChatAgents2 manages the response stream via ChatAgentResponseStream, pushing progress chunks to the main thread. [src/vs/workbench/api/common/extHostChatAgents2.ts:44-60]
The ChatAgentResponseStream handles various types of progress, such as markdown content, references, and tool calls.
extHostTypeConverters. [src/vs/workbench/api/common/extHostTypeConverters.ts:46-56]$handleProgressChunk with serialized tool data. [src/vs/workbench/api/common/extHost.protocol.ts:68-68]Extension API Type (vscode.d.ts) | DTO Interface (extHost.protocol.ts) | Internal Editor/Workbench Type |
|---|---|---|
Position | IPosition | IPosition (editor) |
ChatResponsePart | IChatResponsePart | IChatResponsePart |
CompletionItem | ISuggestDataDto | CompletionItem (editor) |
Location | ILocationDto | Location (editor) |
Sources: [src/vs/workbench/api/common/extHostTypes.ts:43-50], [src/vs/workbench/api/common/extHost.protocol.ts:36-36], [src/vs/editor/common/languages.ts:18-20], [src/vs/workbench/api/common/extHostTypeConverters.ts:46-56]
Refresh this wiki
This wiki was recently refreshed. Please wait 7 days to refresh again.