import * as vscode from 'vscode'; import type { VSCodeController } from '../controller/VSCodeController.js'; import { isGrpcRequest } from '{{PACKAGE_SCOPE}}/core/shared/messages.js'; export class SidebarProvider implements vscode.WebviewViewProvider { private view?: vscode.WebviewView; constructor( private readonly extensionUri: vscode.Uri, private readonly controller: VSCodeController, ) { controller.on('state-changed', (state) => { this.view?.webview.postMessage({ type: 'state-update', state: JSON.stringify(state), }); }); } resolveWebviewView(webviewView: vscode.WebviewView): void { this.view = webviewView; webviewView.webview.options = { enableScripts: true, localResourceRoots: [ vscode.Uri.joinPath(this.extensionUri, 'webview-ui', 'dist'), ], }; webviewView.webview.html = this.getHtml(webviewView.webview); webviewView.webview.onDidReceiveMessage(async (msg) => { if (isGrpcRequest(msg)) { await this.controller.handleRequest( async (response) => { webviewView.webview.postMessage(response); }, msg.grpc_request, ); } }); } private getHtml(webview: vscode.Webview): string { const scriptUri = webview.asWebviewUri( vscode.Uri.joinPath(this.extensionUri, 'webview-ui', 'dist', 'assets', 'index.js') ); const styleUri = webview.asWebviewUri( vscode.Uri.joinPath(this.extensionUri, 'webview-ui', 'dist', 'assets', 'index.css') ); const nonce = getNonce(); return `
`; } } function getNonce(): string { let text = ''; const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; for (let i = 0; i < 32; i++) { text += chars.charAt(Math.floor(Math.random() * chars.length)); } return text; }