Pass vscodeApi if your code already called
acquireVsCodeApi() (VS Code permits only one call); target is for
tests
import { createWebviewRpcClient } from '@kkdev92/vscode-ext-kit/webview-client';
import type { MyRpcSchema } from '../../shared/rpc-schema.js';
const vscodeApi = acquireVsCodeApi(); // you keep it for getState/setState
const rpc = createWebviewRpcClient<MyRpcSchema>({ vscodeApi });
rpc.onRequest('getSelection', () => ({ text: document.getSelection()?.toString() ?? '' }));
rpc.onEvent('theme', ({ kind }) => applyTheme(kind));
const { ok } = await rpc.request('save', { content: editor.value }, { timeoutMs: 5000 });
rpc.emit('dirty', { isDirty: false });
Creates the webview-side endpoint of a
createWebviewRpcchannel — the import that replaces the hand-copiedpostMessageplumbing a webview bundle otherwise needs.Wire-compatible with the extension host side by construction: both are built on the same
protocol.ts, and the direction conventions of WebviewRpcSchema apply symmetrically (this side sendshostRequestsand answerswebviewRequests).