ClaudeDevProvider.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. import { Uri, Webview } from "vscode"
  2. //import * as weather from "weather-js"
  3. import * as vscode from "vscode"
  4. import { ClaudeDev } from "../ClaudeDev"
  5. import { ClaudeMessage, ExtensionMessage } from "../shared/ExtensionMessage"
  6. import { WebviewMessage } from "../shared/WebviewMessage"
  7. import { Anthropic } from "@anthropic-ai/sdk"
  8. /*
  9. https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
  10. https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
  11. */
  12. export class ClaudeDevProvider implements vscode.WebviewViewProvider {
  13. public static readonly viewType = "claude-dev.ClaudeDevProvider"
  14. private disposables: vscode.Disposable[] = []
  15. private view?: vscode.WebviewView | vscode.WebviewPanel
  16. private providerInstanceIdentifier = Date.now()
  17. private claudeDev?: ClaudeDev
  18. private latestAnnouncementId = "jul-25-2024" // update to some unique identifier when we add a new announcement
  19. constructor(private readonly context: vscode.ExtensionContext) {}
  20. /*
  21. VSCode extensions use the disposable pattern to clean up resources when the sidebar/editor tab is closed by the user or system. This applies to event listening, commands, interacting with the UI, etc.
  22. - https://vscode-docs.readthedocs.io/en/stable/extensions/patterns-and-principles/
  23. - https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts
  24. */
  25. async dispose() {
  26. console.log("Disposing provider...")
  27. await this.clearTask() // clears claudeDev, api conversation history, and webview claude messages
  28. console.log("Cleared task")
  29. if (this.view && "dispose" in this.view) {
  30. this.view.dispose()
  31. console.log("Disposed webview")
  32. }
  33. while (this.disposables.length) {
  34. const x = this.disposables.pop()
  35. if (x) {
  36. x.dispose()
  37. }
  38. }
  39. console.log("Disposed disposables")
  40. }
  41. resolveWebviewView(
  42. webviewView: vscode.WebviewView | vscode.WebviewPanel
  43. //context: vscode.WebviewViewResolveContext<unknown>, used to recreate a deallocated webview, but we don't need this since we use retainContextWhenHidden
  44. //token: vscode.CancellationToken
  45. ): void | Thenable<void> {
  46. this.view = webviewView
  47. webviewView.webview.options = {
  48. // Allow scripts in the webview
  49. enableScripts: true,
  50. localResourceRoots: [this.context.extensionUri],
  51. }
  52. webviewView.webview.html = this.getHtmlContent(webviewView.webview)
  53. // Sets up an event listener to listen for messages passed from the webview view context
  54. // and executes code based on the message that is recieved
  55. this.setWebviewMessageListener(webviewView.webview)
  56. // Logs show up in bottom panel > Debug Console
  57. //console.log("registering listener")
  58. // Listen for when the panel becomes visible
  59. // https://github.com/microsoft/vscode-discussions/discussions/840
  60. if ("onDidChangeViewState" in webviewView) {
  61. // WebviewView and WebviewPanel have all the same properties except for this visibility listener
  62. // panel
  63. webviewView.onDidChangeViewState(
  64. () => {
  65. if (this.view?.visible) {
  66. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  67. }
  68. },
  69. null,
  70. this.disposables
  71. )
  72. } else if ("onDidChangeVisibility" in webviewView) {
  73. // sidebar
  74. webviewView.onDidChangeVisibility(
  75. () => {
  76. if (this.view?.visible) {
  77. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  78. }
  79. },
  80. null,
  81. this.disposables
  82. )
  83. }
  84. // Listen for when the view is disposed
  85. // This happens when the user closes the view or when the view is closed programmatically
  86. webviewView.onDidDispose(
  87. async () => {
  88. await this.dispose()
  89. },
  90. null,
  91. this.disposables
  92. )
  93. // Listen for when color changes
  94. vscode.workspace.onDidChangeConfiguration(
  95. (e) => {
  96. if (e.affectsConfiguration("workbench.colorTheme")) {
  97. // Sends latest theme name to webview
  98. this.postStateToWebview()
  99. }
  100. },
  101. null,
  102. this.disposables
  103. )
  104. // if the extension is starting a new session, clear previous task state
  105. this.clearTask()
  106. // Clear previous version's (0.0.6) claudeMessage cache from workspace state. We now store in global state with a unique identifier for each provider instance. We need to store globally rather than per workspace to eventually implement task history
  107. this.updateWorkspaceState("claudeMessages", undefined)
  108. }
  109. async tryToInitClaudeDevWithTask(task: string) {
  110. await this.clearTask() // ensures that an exising task doesn't exist before starting a new one, although this shouldn't be possible since user must clear task before starting a new one
  111. const [apiKey, maxRequestsPerTask] = await Promise.all([
  112. this.getSecret("apiKey") as Promise<string | undefined>,
  113. this.getGlobalState("maxRequestsPerTask") as Promise<number | undefined>,
  114. ])
  115. if (this.view && apiKey) {
  116. this.claudeDev = new ClaudeDev(this, task, apiKey, maxRequestsPerTask)
  117. }
  118. }
  119. // Send any JSON serializable data to the react app
  120. async postMessageToWebview(message: ExtensionMessage) {
  121. await this.view?.webview.postMessage(message)
  122. }
  123. /**
  124. * Defines and returns the HTML that should be rendered within the webview panel.
  125. *
  126. * @remarks This is also the place where references to the React webview build files
  127. * are created and inserted into the webview HTML.
  128. *
  129. * @param webview A reference to the extension webview
  130. * @param extensionUri The URI of the directory containing the extension
  131. * @returns A template string literal containing the HTML that should be
  132. * rendered within the webview panel
  133. */
  134. private getHtmlContent(webview: vscode.Webview): string {
  135. // Get the local path to main script run in the webview,
  136. // then convert it to a uri we can use in the webview.
  137. // The CSS file from the React build output
  138. const stylesUri = getUri(webview, this.context.extensionUri, [
  139. "webview-ui",
  140. "build",
  141. "static",
  142. "css",
  143. "main.css",
  144. ])
  145. // The JS file from the React build output
  146. const scriptUri = getUri(webview, this.context.extensionUri, ["webview-ui", "build", "static", "js", "main.js"])
  147. // The codicon font from the React build output
  148. // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-codicons-sample/src/extension.ts
  149. // 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
  150. // don't forget to add font-src ${webview.cspSource};
  151. const codiconsUri = getUri(webview, this.context.extensionUri, [
  152. "node_modules",
  153. "@vscode",
  154. "codicons",
  155. "dist",
  156. "codicon.css",
  157. ])
  158. // const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.js"))
  159. // const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "reset.css"))
  160. // const styleVSCodeUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "vscode.css"))
  161. // // Same for stylesheet
  162. // const stylesheetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.css"))
  163. // Use a nonce to only allow a specific script to be run.
  164. /*
  165. content security policy of your webview to only allow scripts that have a specific nonce
  166. create a content security policy meta tag so that only loading scripts with a nonce is allowed
  167. 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.
  168. <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}';">
  169. 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.
  170. */
  171. const nonce = getNonce()
  172. // Tip: Install the es6-string-html VS Code extension to enable code highlighting below
  173. return /*html*/ `
  174. <!DOCTYPE html>
  175. <html lang="en">
  176. <head>
  177. <meta charset="utf-8">
  178. <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  179. <meta name="theme-color" content="#000000">
  180. <meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${webview.cspSource}; style-src ${webview.cspSource}; script-src 'nonce-${nonce}';">
  181. <link rel="stylesheet" type="text/css" href="${stylesUri}">
  182. <link href="${codiconsUri}" rel="stylesheet" />
  183. <title>Claude Dev</title>
  184. </head>
  185. <body>
  186. <noscript>You need to enable JavaScript to run this app.</noscript>
  187. <div id="root"></div>
  188. <script nonce="${nonce}" src="${scriptUri}"></script>
  189. </body>
  190. </html>
  191. `
  192. }
  193. /**
  194. * Sets up an event listener to listen for messages passed from the webview context and
  195. * executes code based on the message that is recieved.
  196. *
  197. * @param webview A reference to the extension webview
  198. */
  199. private setWebviewMessageListener(webview: vscode.Webview) {
  200. webview.onDidReceiveMessage(
  201. async (message: WebviewMessage) => {
  202. switch (message.type) {
  203. case "webviewDidLaunch":
  204. await this.postStateToWebview()
  205. break
  206. case "newTask":
  207. // Code that should run in response to the hello message command
  208. //vscode.window.showInformationMessage(message.text!)
  209. // Send a message to our webview.
  210. // You can send any JSON serializable data.
  211. // Could also do this in extension .ts
  212. //this.postMessageToWebview({ type: "text", text: `Extension: ${Date.now()}` })
  213. // initializing new instance of ClaudeDev will make sure that any agentically running promises in old instance don't affect our new task. this essentially creates a fresh slate for the new task
  214. await this.tryToInitClaudeDevWithTask(message.text!)
  215. break
  216. case "apiKey":
  217. await this.storeSecret("apiKey", message.text!)
  218. this.claudeDev?.updateApiKey(message.text!)
  219. await this.postStateToWebview()
  220. break
  221. case "maxRequestsPerTask":
  222. let result: number | undefined = undefined
  223. if (message.text && message.text.trim()) {
  224. const num = Number(message.text)
  225. if (!isNaN(num)) {
  226. result = num
  227. }
  228. }
  229. await this.updateGlobalState("maxRequestsPerTask", result)
  230. this.claudeDev?.updateMaxRequestsPerTask(result)
  231. await this.postStateToWebview()
  232. break
  233. case "askResponse":
  234. this.claudeDev?.handleWebviewAskResponse(message.askResponse!, message.text)
  235. break
  236. case "clearTask":
  237. // newTask will start a new task with a given task text, while clear task resets the current session and allows for a new task to be started
  238. await this.clearTask()
  239. await this.postStateToWebview()
  240. break
  241. case "didShowAnnouncement":
  242. await this.updateGlobalState("lastShownAnnouncementId", this.latestAnnouncementId)
  243. await this.postStateToWebview()
  244. break
  245. // Add more switch case statements here as more webview message commands
  246. // are created within the webview context (i.e. inside media/main.js)
  247. }
  248. },
  249. null,
  250. this.disposables
  251. )
  252. }
  253. async postStateToWebview() {
  254. const [apiKey, maxRequestsPerTask, claudeMessages, lastShownAnnouncementId] = await Promise.all([
  255. this.getSecret("apiKey") as Promise<string | undefined>,
  256. this.getGlobalState("maxRequestsPerTask") as Promise<number | undefined>,
  257. this.getClaudeMessages(),
  258. this.getGlobalState("lastShownAnnouncementId") as Promise<string | undefined>,
  259. ])
  260. this.postMessageToWebview({
  261. type: "state",
  262. state: {
  263. apiKey,
  264. maxRequestsPerTask,
  265. themeName: vscode.workspace.getConfiguration("workbench").get<string>("colorTheme"),
  266. claudeMessages,
  267. shouldShowAnnouncement: lastShownAnnouncementId !== this.latestAnnouncementId,
  268. },
  269. })
  270. }
  271. async clearTask() {
  272. if (this.claudeDev) {
  273. this.claudeDev.abort = true // will stop any agentically running promises
  274. this.claudeDev = undefined // removes reference to it, so once promises end it will be garbage collected
  275. }
  276. await this.setApiConversationHistory(undefined)
  277. await this.setClaudeMessages(undefined)
  278. }
  279. // Caching mechanism to keep track of webview messages + API conversation history per provider instance
  280. /*
  281. Now that we use retainContextWhenHidden, we don't have to store a cache of claude messages in the user's state, but we do to reduce memory footprint in long conversations.
  282. - We have to be careful of what state is shared between ClaudeDevProvider instances since there could be multiple instances of the extension running at once. For example when we cached claude messages using the same key, two instances of the extension could end up using the same key and overwriting each other's messages.
  283. - Some state does need to be shared between the instances, i.e. the API key--however there doesn't seem to be a good way to notfy the other instances that the API key has changed.
  284. We need to use a unique identifier for each ClaudeDevProvider instance's message cache since we could be running several instances of the extension outside of just the sidebar i.e. in editor panels.
  285. For now since we don't need to store task history, we'll just use an identifier unique to this provider instance (since there can be several provider instances open at once).
  286. However in the future when we implement task history, we'll need to use a unique identifier for each task. As well as manage a data structure that keeps track of task history with their associated identifiers and the task message itself, to present in a 'Task History' view.
  287. Task history is a significant undertaking as it would require refactoring how we wait for ask responses--it would need to be a hidden claudeMessage, so that user's can resume tasks that ended with an ask.
  288. */
  289. getClaudeMessagesStateKey() {
  290. return `claudeMessages-${this.providerInstanceIdentifier}`
  291. }
  292. getApiConversationHistoryStateKey() {
  293. return `apiConversationHistory-${this.providerInstanceIdentifier}`
  294. }
  295. // claude messages to present in the webview
  296. async getClaudeMessages(): Promise<ClaudeMessage[]> {
  297. const messages = (await this.getGlobalState(this.getClaudeMessagesStateKey())) as ClaudeMessage[]
  298. return messages || []
  299. }
  300. async setClaudeMessages(messages: ClaudeMessage[] | undefined) {
  301. await this.updateGlobalState(this.getClaudeMessagesStateKey(), messages)
  302. }
  303. async addClaudeMessage(message: ClaudeMessage): Promise<ClaudeMessage[]> {
  304. const messages = await this.getClaudeMessages()
  305. messages.push(message)
  306. await this.setClaudeMessages(messages)
  307. return messages
  308. }
  309. // conversation history to send in API requests
  310. async getApiConversationHistory(): Promise<Anthropic.MessageParam[]> {
  311. const history = (await this.getGlobalState(
  312. this.getApiConversationHistoryStateKey()
  313. )) as Anthropic.MessageParam[]
  314. return history || []
  315. }
  316. async setApiConversationHistory(history: Anthropic.MessageParam[] | undefined) {
  317. await this.updateGlobalState(this.getApiConversationHistoryStateKey(), history)
  318. }
  319. async addMessageToApiConversationHistory(message: Anthropic.MessageParam): Promise<Anthropic.MessageParam[]> {
  320. const history = await this.getApiConversationHistory()
  321. history.push(message)
  322. await this.setApiConversationHistory(history)
  323. return history
  324. }
  325. /*
  326. Storage
  327. https://dev.to/kompotkot/how-to-use-secretstorage-in-your-vscode-extensions-2hco
  328. https://www.eliostruyf.com/devhack-code-extension-storage-options/
  329. */
  330. // global
  331. private async updateGlobalState(key: string, value: any) {
  332. await this.context.globalState.update(key, value)
  333. }
  334. private async getGlobalState(key: string) {
  335. return await this.context.globalState.get(key)
  336. }
  337. // workspace
  338. private async updateWorkspaceState(key: string, value: any) {
  339. await this.context.workspaceState.update(key, value)
  340. }
  341. private async getWorkspaceState(key: string) {
  342. return await this.context.workspaceState.get(key)
  343. }
  344. // private async clearState() {
  345. // this.context.workspaceState.keys().forEach((key) => {
  346. // this.context.workspaceState.update(key, undefined)
  347. // })
  348. // this.context.globalState.keys().forEach((key) => {
  349. // this.context.globalState.update(key, undefined)
  350. // })
  351. // this.context.secrets.delete("apiKey")
  352. // }
  353. // secrets
  354. private async storeSecret(key: string, value: any) {
  355. await this.context.secrets.store(key, value)
  356. }
  357. private async getSecret(key: string) {
  358. return await this.context.secrets.get(key)
  359. }
  360. }
  361. /**
  362. * A helper function that returns a unique alphanumeric identifier called a nonce.
  363. *
  364. * @remarks This function is primarily used to help enforce content security
  365. * policies for resources/scripts being executed in a webview context.
  366. *
  367. * @returns A nonce
  368. */
  369. export function getNonce() {
  370. let text = ""
  371. const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  372. for (let i = 0; i < 32; i++) {
  373. text += possible.charAt(Math.floor(Math.random() * possible.length))
  374. }
  375. return text
  376. }
  377. /**
  378. * A helper function which will get the webview URI of a given file or resource.
  379. *
  380. * @remarks This URI can be used within a webview's HTML as a link to the
  381. * given file/resource.
  382. *
  383. * @param webview A reference to the extension webview
  384. * @param extensionUri The URI of the directory containing the extension
  385. * @param pathList An array of strings representing the path to a file/resource
  386. * @returns A URI pointing to the file/resource
  387. */
  388. export function getUri(webview: Webview, extensionUri: Uri, pathList: string[]) {
  389. return webview.asWebviewUri(Uri.joinPath(extensionUri, ...pathList))
  390. }