SidebarProvider.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { Uri, Webview } from "vscode"
  2. //import * as weather from "weather-js"
  3. import * as vscode from "vscode"
  4. import { ExtensionMessage } from "../shared/ExtensionMessage"
  5. import { WebviewMessage } from "../shared/WebviewMessage"
  6. /*
  7. https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
  8. https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
  9. */
  10. type ExtensionSecretKey = "apiKey"
  11. type ExtensionGlobalStateKey = "didOpenOnce" | "maxRequestsPerTask"
  12. export class SidebarProvider implements vscode.WebviewViewProvider {
  13. public static readonly viewType = "claude-dev.SidebarProvider"
  14. private _view?: vscode.WebviewView
  15. constructor(private readonly context: vscode.ExtensionContext) {}
  16. resolveWebviewView(
  17. webviewView: vscode.WebviewView,
  18. context: vscode.WebviewViewResolveContext<unknown>,
  19. token: vscode.CancellationToken
  20. ): void | Thenable<void> {
  21. this._view = webviewView
  22. webviewView.webview.options = {
  23. // Allow scripts in the webview
  24. enableScripts: true,
  25. localResourceRoots: [this.context.extensionUri],
  26. }
  27. webviewView.webview.html = this.getHtmlContent(webviewView.webview)
  28. // Sets up an event listener to listen for messages passed from the webview view context
  29. // and executes code based on the message that is recieved
  30. this._setWebviewMessageListener(webviewView.webview)
  31. }
  32. // Send any JSON serializable data to the react app
  33. postMessageToWebview(message: ExtensionMessage) {
  34. this._view?.webview.postMessage(message)
  35. }
  36. /**
  37. * Defines and returns the HTML that should be rendered within the webview panel.
  38. *
  39. * @remarks This is also the place where references to the React webview build files
  40. * are created and inserted into the webview HTML.
  41. *
  42. * @param webview A reference to the extension webview
  43. * @param extensionUri The URI of the directory containing the extension
  44. * @returns A template string literal containing the HTML that should be
  45. * rendered within the webview panel
  46. */
  47. private getHtmlContent(webview: vscode.Webview): string {
  48. // Get the local path to main script run in the webview,
  49. // then convert it to a uri we can use in the webview.
  50. // The CSS file from the React build output
  51. const stylesUri = getUri(webview, this.context.extensionUri, [
  52. "webview-ui",
  53. "build",
  54. "static",
  55. "css",
  56. "main.css",
  57. ])
  58. // The JS file from the React build output
  59. const scriptUri = getUri(webview, this.context.extensionUri, ["webview-ui", "build", "static", "js", "main.js"])
  60. // The codicon font from the React build output
  61. // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-codicons-sample/src/extension.ts
  62. // we installed this package in the extension so that we can access it how its intended from the extension (the font file is likely bundled in vscode), and we just import the css fileinto our react app we don't have access to it
  63. // don't forget to add font-src ${webview.cspSource};
  64. const codiconsUri = getUri(webview, this.context.extensionUri, [
  65. "node_modules",
  66. "@vscode",
  67. "codicons",
  68. "dist",
  69. "codicon.css",
  70. ])
  71. // const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.js"))
  72. // const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "reset.css"))
  73. // const styleVSCodeUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "vscode.css"))
  74. // // Same for stylesheet
  75. // const stylesheetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.css"))
  76. // Use a nonce to only allow a specific script to be run.
  77. /*
  78. content security policy of your webview to only allow scripts that have a specific nonce
  79. create a content security policy meta tag so that only loading scripts with a nonce is allowed
  80. As your extension grows you will likely want to add custom styles, fonts, and/or images to your webview. If you do, you will need to update the content security policy meta tag to explicity allow for these resources. E.g.
  81. <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource}; font-src ${webview.cspSource}; img-src ${webview.cspSource} https:; script-src 'nonce-${nonce}';">
  82. in meta tag we add nonce attribute: A cryptographic nonce (only used once) to allow scripts. The server must generate a unique nonce value each time it transmits a policy. It is critical to provide a nonce that cannot be guessed as bypassing a resource's policy is otherwise trivial.
  83. */
  84. const nonce = getNonce()
  85. // Tip: Install the es6-string-html VS Code extension to enable code highlighting below
  86. return /*html*/ `
  87. <!DOCTYPE html>
  88. <html lang="en">
  89. <head>
  90. <meta charset="utf-8">
  91. <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  92. <meta name="theme-color" content="#000000">
  93. <meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${webview.cspSource}; style-src ${webview.cspSource}; script-src 'nonce-${nonce}';">
  94. <link rel="stylesheet" type="text/css" href="${stylesUri}">
  95. <link href="${codiconsUri}" rel="stylesheet" />
  96. <title>Claude Dev</title>
  97. </head>
  98. <body>
  99. <noscript>You need to enable JavaScript to run this app.</noscript>
  100. <div id="root"></div>
  101. <script nonce="${nonce}" src="${scriptUri}"></script>
  102. </body>
  103. </html>
  104. `
  105. }
  106. /**
  107. * Sets up an event listener to listen for messages passed from the webview context and
  108. * executes code based on the message that is recieved.
  109. *
  110. * @param webview A reference to the extension webview
  111. * @param context A reference to the extension context
  112. */
  113. private _setWebviewMessageListener(webview: vscode.Webview) {
  114. webview.onDidReceiveMessage(async (message: WebviewMessage) => {
  115. switch (message.type) {
  116. case "webviewDidLaunch":
  117. await this.updateGlobalState("didOpenOnce", true)
  118. await this.postStateToWebview()
  119. break
  120. case "text":
  121. // Code that should run in response to the hello message command
  122. vscode.window.showInformationMessage(message.text!)
  123. // Send a message to our webview.
  124. // You can send any JSON serializable data.
  125. // Could also do this in extension .ts
  126. this.postMessageToWebview({ type: "text", text: `Extension: ${Date.now()}` })
  127. break
  128. case "apiKey":
  129. await this.storeSecret("apiKey", message.text!)
  130. await this.postStateToWebview()
  131. break
  132. case "maxRequestsPerTask":
  133. let result: number | undefined = undefined
  134. if (message.text && message.text.trim()) {
  135. const num = Number(message.text)
  136. if (!isNaN(num)) {
  137. result = num
  138. }
  139. }
  140. await this.updateGlobalState("maxRequestsPerTask", result)
  141. await this.postStateToWebview()
  142. break
  143. // Add more switch case statements here as more webview message commands
  144. // are created within the webview context (i.e. inside media/main.js)
  145. }
  146. })
  147. }
  148. private async postStateToWebview() {
  149. const [didOpenOnce, apiKey, maxRequestsPerTask] = await Promise.all([
  150. this.getGlobalState("didOpenOnce") as Promise<boolean | undefined>,
  151. this.getSecret("apiKey") as Promise<string | undefined>,
  152. this.getGlobalState("maxRequestsPerTask") as Promise<number | undefined>,
  153. ])
  154. this.postMessageToWebview({
  155. type: "state",
  156. state: { didOpenOnce: !!didOpenOnce, apiKey: apiKey, maxRequestsPerTask: maxRequestsPerTask },
  157. })
  158. }
  159. /*
  160. Storage
  161. https://dev.to/kompotkot/how-to-use-secretstorage-in-your-vscode-extensions-2hco
  162. https://www.eliostruyf.com/devhack-code-extension-storage-options/
  163. */
  164. private async updateGlobalState(key: ExtensionGlobalStateKey, value: any) {
  165. await this.context.globalState.update(key, value)
  166. }
  167. private async getGlobalState(key: ExtensionGlobalStateKey) {
  168. return await this.context.globalState.get(key)
  169. }
  170. private async storeSecret(key: ExtensionSecretKey, value: any) {
  171. await this.context.secrets.store(key, value)
  172. }
  173. private async getSecret(key: ExtensionSecretKey) {
  174. return await this.context.secrets.get(key)
  175. }
  176. }
  177. /**
  178. * A helper function that returns a unique alphanumeric identifier called a nonce.
  179. *
  180. * @remarks This function is primarily used to help enforce content security
  181. * policies for resources/scripts being executed in a webview context.
  182. *
  183. * @returns A nonce
  184. */
  185. export function getNonce() {
  186. let text = ""
  187. const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  188. for (let i = 0; i < 32; i++) {
  189. text += possible.charAt(Math.floor(Math.random() * possible.length))
  190. }
  191. return text
  192. }
  193. /**
  194. * A helper function which will get the webview URI of a given file or resource.
  195. *
  196. * @remarks This URI can be used within a webview's HTML as a link to the
  197. * given file/resource.
  198. *
  199. * @param webview A reference to the extension webview
  200. * @param extensionUri The URI of the directory containing the extension
  201. * @param pathList An array of strings representing the path to a file/resource
  202. * @returns A URI pointing to the file/resource
  203. */
  204. export function getUri(webview: Webview, extensionUri: Uri, pathList: string[]) {
  205. return webview.asWebviewUri(Uri.joinPath(extensionUri, ...pathList))
  206. }