ClineProvider.ts 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. import { Anthropic } from "@anthropic-ai/sdk"
  2. import axios from "axios"
  3. import fs from "fs/promises"
  4. import pWaitFor from "p-wait-for"
  5. import * as path from "path"
  6. import * as vscode from "vscode"
  7. import { buildApiHandler } from "../../api"
  8. import { downloadTask } from "../../integrations/misc/export-markdown"
  9. import { openFile, openImage } from "../../integrations/misc/open-file"
  10. import { selectImages } from "../../integrations/misc/process-images"
  11. import { getTheme } from "../../integrations/theme/getTheme"
  12. import WorkspaceTracker from "../../integrations/workspace/WorkspaceTracker"
  13. import { ApiProvider, ModelInfo } from "../../shared/api"
  14. import { findLast } from "../../shared/array"
  15. import { ExtensionMessage } from "../../shared/ExtensionMessage"
  16. import { HistoryItem } from "../../shared/HistoryItem"
  17. import { WebviewMessage } from "../../shared/WebviewMessage"
  18. import { fileExistsAtPath } from "../../utils/fs"
  19. import { Cline } from "../Cline"
  20. import { openMention } from "../mentions"
  21. import { getNonce } from "./getNonce"
  22. import { getUri } from "./getUri"
  23. /*
  24. https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
  25. https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
  26. */
  27. type SecretKey =
  28. | "apiKey"
  29. | "openRouterApiKey"
  30. | "awsAccessKey"
  31. | "awsSecretKey"
  32. | "awsSessionToken"
  33. | "openAiApiKey"
  34. | "geminiApiKey"
  35. | "openAiNativeApiKey"
  36. type GlobalStateKey =
  37. | "apiProvider"
  38. | "apiModelId"
  39. | "awsRegion"
  40. | "awsUseCrossRegionInference"
  41. | "vertexProjectId"
  42. | "vertexRegion"
  43. | "lastShownAnnouncementId"
  44. | "customInstructions"
  45. | "alwaysAllowReadOnly"
  46. | "alwaysAllowWrite"
  47. | "alwaysAllowExecute"
  48. | "alwaysAllowBrowser"
  49. | "taskHistory"
  50. | "openAiBaseUrl"
  51. | "openAiModelId"
  52. | "ollamaModelId"
  53. | "ollamaBaseUrl"
  54. | "lmStudioModelId"
  55. | "lmStudioBaseUrl"
  56. | "anthropicBaseUrl"
  57. | "azureApiVersion"
  58. | "openRouterModelId"
  59. | "openRouterModelInfo"
  60. export const GlobalFileNames = {
  61. apiConversationHistory: "api_conversation_history.json",
  62. uiMessages: "ui_messages.json",
  63. openRouterModels: "openrouter_models.json",
  64. }
  65. export class ClineProvider implements vscode.WebviewViewProvider {
  66. public static readonly sideBarId = "claude-dev.SidebarProvider" // used in package.json as the view's id. This value cannot be changed due to how vscode caches views based on their id, and updating the id would break existing instances of the extension.
  67. public static readonly tabPanelId = "claude-dev.TabPanelProvider"
  68. private static activeInstances: Set<ClineProvider> = new Set()
  69. private disposables: vscode.Disposable[] = []
  70. private view?: vscode.WebviewView | vscode.WebviewPanel
  71. private cline?: Cline
  72. private workspaceTracker?: WorkspaceTracker
  73. private latestAnnouncementId = "oct-28-2024" // update to some unique identifier when we add a new announcement
  74. constructor(readonly context: vscode.ExtensionContext, private readonly outputChannel: vscode.OutputChannel) {
  75. this.outputChannel.appendLine("ClineProvider instantiated")
  76. ClineProvider.activeInstances.add(this)
  77. this.workspaceTracker = new WorkspaceTracker(this)
  78. }
  79. /*
  80. 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.
  81. - https://vscode-docs.readthedocs.io/en/stable/extensions/patterns-and-principles/
  82. - https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts
  83. */
  84. async dispose() {
  85. this.outputChannel.appendLine("Disposing ClineProvider...")
  86. await this.clearTask()
  87. this.outputChannel.appendLine("Cleared task")
  88. if (this.view && "dispose" in this.view) {
  89. this.view.dispose()
  90. this.outputChannel.appendLine("Disposed webview")
  91. }
  92. while (this.disposables.length) {
  93. const x = this.disposables.pop()
  94. if (x) {
  95. x.dispose()
  96. }
  97. }
  98. this.workspaceTracker?.dispose()
  99. this.workspaceTracker = undefined
  100. this.outputChannel.appendLine("Disposed all disposables")
  101. ClineProvider.activeInstances.delete(this)
  102. }
  103. public static getVisibleInstance(): ClineProvider | undefined {
  104. return findLast(Array.from(this.activeInstances), (instance) => instance.view?.visible === true)
  105. }
  106. resolveWebviewView(
  107. webviewView: vscode.WebviewView | vscode.WebviewPanel
  108. //context: vscode.WebviewViewResolveContext<unknown>, used to recreate a deallocated webview, but we don't need this since we use retainContextWhenHidden
  109. //token: vscode.CancellationToken
  110. ): void | Thenable<void> {
  111. this.outputChannel.appendLine("Resolving webview view")
  112. this.view = webviewView
  113. webviewView.webview.options = {
  114. // Allow scripts in the webview
  115. enableScripts: true,
  116. localResourceRoots: [this.context.extensionUri],
  117. }
  118. webviewView.webview.html = this.getHtmlContent(webviewView.webview)
  119. // Sets up an event listener to listen for messages passed from the webview view context
  120. // and executes code based on the message that is recieved
  121. this.setWebviewMessageListener(webviewView.webview)
  122. // Logs show up in bottom panel > Debug Console
  123. //console.log("registering listener")
  124. // Listen for when the panel becomes visible
  125. // https://github.com/microsoft/vscode-discussions/discussions/840
  126. if ("onDidChangeViewState" in webviewView) {
  127. // WebviewView and WebviewPanel have all the same properties except for this visibility listener
  128. // panel
  129. webviewView.onDidChangeViewState(
  130. () => {
  131. if (this.view?.visible) {
  132. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  133. }
  134. },
  135. null,
  136. this.disposables
  137. )
  138. } else if ("onDidChangeVisibility" in webviewView) {
  139. // sidebar
  140. webviewView.onDidChangeVisibility(
  141. () => {
  142. if (this.view?.visible) {
  143. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  144. }
  145. },
  146. null,
  147. this.disposables
  148. )
  149. }
  150. // Listen for when the view is disposed
  151. // This happens when the user closes the view or when the view is closed programmatically
  152. webviewView.onDidDispose(
  153. async () => {
  154. await this.dispose()
  155. },
  156. null,
  157. this.disposables
  158. )
  159. // Listen for when color changes
  160. vscode.workspace.onDidChangeConfiguration(
  161. async (e) => {
  162. if (e && e.affectsConfiguration("workbench.colorTheme")) {
  163. // Sends latest theme name to webview
  164. await this.postMessageToWebview({ type: "theme", text: JSON.stringify(await getTheme()) })
  165. }
  166. },
  167. null,
  168. this.disposables
  169. )
  170. // if the extension is starting a new session, clear previous task state
  171. this.clearTask()
  172. this.outputChannel.appendLine("Webview view resolved")
  173. }
  174. async initClineWithTask(task?: string, images?: string[]) {
  175. await this.clearTask()
  176. const {
  177. apiConfiguration,
  178. customInstructions,
  179. alwaysAllowReadOnly,
  180. alwaysAllowWrite,
  181. alwaysAllowExecute,
  182. alwaysAllowBrowser
  183. } = await this.getState()
  184. this.cline = new Cline(
  185. this,
  186. apiConfiguration,
  187. customInstructions,
  188. alwaysAllowReadOnly,
  189. alwaysAllowWrite,
  190. alwaysAllowExecute,
  191. alwaysAllowBrowser,
  192. task,
  193. images
  194. )
  195. }
  196. async initClineWithHistoryItem(historyItem: HistoryItem) {
  197. await this.clearTask()
  198. const {
  199. apiConfiguration,
  200. customInstructions,
  201. alwaysAllowReadOnly,
  202. alwaysAllowWrite,
  203. alwaysAllowExecute,
  204. alwaysAllowBrowser
  205. } = await this.getState()
  206. this.cline = new Cline(
  207. this,
  208. apiConfiguration,
  209. customInstructions,
  210. alwaysAllowReadOnly,
  211. alwaysAllowWrite,
  212. alwaysAllowExecute,
  213. alwaysAllowBrowser,
  214. undefined,
  215. undefined,
  216. historyItem
  217. )
  218. }
  219. // Send any JSON serializable data to the react app
  220. async postMessageToWebview(message: ExtensionMessage) {
  221. await this.view?.webview.postMessage(message)
  222. }
  223. /**
  224. * Defines and returns the HTML that should be rendered within the webview panel.
  225. *
  226. * @remarks This is also the place where references to the React webview build files
  227. * are created and inserted into the webview HTML.
  228. *
  229. * @param webview A reference to the extension webview
  230. * @param extensionUri The URI of the directory containing the extension
  231. * @returns A template string literal containing the HTML that should be
  232. * rendered within the webview panel
  233. */
  234. private getHtmlContent(webview: vscode.Webview): string {
  235. // Get the local path to main script run in the webview,
  236. // then convert it to a uri we can use in the webview.
  237. // The CSS file from the React build output
  238. const stylesUri = getUri(webview, this.context.extensionUri, [
  239. "webview-ui",
  240. "build",
  241. "static",
  242. "css",
  243. "main.css",
  244. ])
  245. // The JS file from the React build output
  246. const scriptUri = getUri(webview, this.context.extensionUri, ["webview-ui", "build", "static", "js", "main.js"])
  247. // The codicon font from the React build output
  248. // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-codicons-sample/src/extension.ts
  249. // 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
  250. // don't forget to add font-src ${webview.cspSource};
  251. const codiconsUri = getUri(webview, this.context.extensionUri, [
  252. "node_modules",
  253. "@vscode",
  254. "codicons",
  255. "dist",
  256. "codicon.css",
  257. ])
  258. // const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.js"))
  259. // const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "reset.css"))
  260. // const styleVSCodeUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "vscode.css"))
  261. // // Same for stylesheet
  262. // const stylesheetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.css"))
  263. // Use a nonce to only allow a specific script to be run.
  264. /*
  265. content security policy of your webview to only allow scripts that have a specific nonce
  266. create a content security policy meta tag so that only loading scripts with a nonce is allowed
  267. 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.
  268. <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}';">
  269. - 'unsafe-inline' is required for styles due to vscode-webview-toolkit's dynamic style injection
  270. - since we pass base64 images to the webview, we need to specify img-src ${webview.cspSource} data:;
  271. 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.
  272. */
  273. const nonce = getNonce()
  274. // Tip: Install the es6-string-html VS Code extension to enable code highlighting below
  275. return /*html*/ `
  276. <!DOCTYPE html>
  277. <html lang="en">
  278. <head>
  279. <meta charset="utf-8">
  280. <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  281. <meta name="theme-color" content="#000000">
  282. <meta http-equiv="Content-Security-Policy" content="default-src 'none'; font-src ${webview.cspSource}; style-src ${webview.cspSource} 'unsafe-inline'; img-src ${webview.cspSource} data:; script-src 'nonce-${nonce}';">
  283. <link rel="stylesheet" type="text/css" href="${stylesUri}">
  284. <link href="${codiconsUri}" rel="stylesheet" />
  285. <title>Cline</title>
  286. </head>
  287. <body>
  288. <noscript>You need to enable JavaScript to run this app.</noscript>
  289. <div id="root"></div>
  290. <script nonce="${nonce}" src="${scriptUri}"></script>
  291. </body>
  292. </html>
  293. `
  294. }
  295. /**
  296. * Sets up an event listener to listen for messages passed from the webview context and
  297. * executes code based on the message that is recieved.
  298. *
  299. * @param webview A reference to the extension webview
  300. */
  301. private setWebviewMessageListener(webview: vscode.Webview) {
  302. webview.onDidReceiveMessage(
  303. async (message: WebviewMessage) => {
  304. switch (message.type) {
  305. case "webviewDidLaunch":
  306. this.postStateToWebview()
  307. this.workspaceTracker?.initializeFilePaths() // don't await
  308. getTheme().then((theme) =>
  309. this.postMessageToWebview({ type: "theme", text: JSON.stringify(theme) })
  310. )
  311. // post last cached models in case the call to endpoint fails
  312. this.readOpenRouterModels().then((openRouterModels) => {
  313. if (openRouterModels) {
  314. this.postMessageToWebview({ type: "openRouterModels", openRouterModels })
  315. }
  316. })
  317. // gui relies on model info to be up-to-date to provide the most accurate pricing, so we need to fetch the latest details on launch.
  318. // we do this for all users since many users switch between api providers and if they were to switch back to openrouter it would be showing outdated model info if we hadn't retrieved the latest at this point
  319. // (see normalizeApiConfiguration > openrouter)
  320. this.refreshOpenRouterModels().then(async (openRouterModels) => {
  321. if (openRouterModels) {
  322. // update model info in state (this needs to be done here since we don't want to update state while settings is open, and we may refresh models there)
  323. const { apiConfiguration } = await this.getState()
  324. if (apiConfiguration.openRouterModelId) {
  325. await this.updateGlobalState(
  326. "openRouterModelInfo",
  327. openRouterModels[apiConfiguration.openRouterModelId]
  328. )
  329. await this.postStateToWebview()
  330. }
  331. }
  332. })
  333. break
  334. case "newTask":
  335. // Code that should run in response to the hello message command
  336. //vscode.window.showInformationMessage(message.text!)
  337. // Send a message to our webview.
  338. // You can send any JSON serializable data.
  339. // Could also do this in extension .ts
  340. //this.postMessageToWebview({ type: "text", text: `Extension: ${Date.now()}` })
  341. // initializing new instance of Cline 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
  342. await this.initClineWithTask(message.text, message.images)
  343. break
  344. case "apiConfiguration":
  345. if (message.apiConfiguration) {
  346. const {
  347. apiProvider,
  348. apiModelId,
  349. apiKey,
  350. openRouterApiKey,
  351. awsAccessKey,
  352. awsSecretKey,
  353. awsSessionToken,
  354. awsRegion,
  355. awsUseCrossRegionInference,
  356. vertexProjectId,
  357. vertexRegion,
  358. openAiBaseUrl,
  359. openAiApiKey,
  360. openAiModelId,
  361. ollamaModelId,
  362. ollamaBaseUrl,
  363. lmStudioModelId,
  364. lmStudioBaseUrl,
  365. anthropicBaseUrl,
  366. geminiApiKey,
  367. openAiNativeApiKey,
  368. azureApiVersion,
  369. openRouterModelId,
  370. openRouterModelInfo,
  371. } = message.apiConfiguration
  372. await this.updateGlobalState("apiProvider", apiProvider)
  373. await this.updateGlobalState("apiModelId", apiModelId)
  374. await this.storeSecret("apiKey", apiKey)
  375. await this.storeSecret("openRouterApiKey", openRouterApiKey)
  376. await this.storeSecret("awsAccessKey", awsAccessKey)
  377. await this.storeSecret("awsSecretKey", awsSecretKey)
  378. await this.storeSecret("awsSessionToken", awsSessionToken)
  379. await this.updateGlobalState("awsRegion", awsRegion)
  380. await this.updateGlobalState("awsUseCrossRegionInference", awsUseCrossRegionInference)
  381. await this.updateGlobalState("vertexProjectId", vertexProjectId)
  382. await this.updateGlobalState("vertexRegion", vertexRegion)
  383. await this.updateGlobalState("openAiBaseUrl", openAiBaseUrl)
  384. await this.storeSecret("openAiApiKey", openAiApiKey)
  385. await this.updateGlobalState("openAiModelId", openAiModelId)
  386. await this.updateGlobalState("ollamaModelId", ollamaModelId)
  387. await this.updateGlobalState("ollamaBaseUrl", ollamaBaseUrl)
  388. await this.updateGlobalState("lmStudioModelId", lmStudioModelId)
  389. await this.updateGlobalState("lmStudioBaseUrl", lmStudioBaseUrl)
  390. await this.updateGlobalState("anthropicBaseUrl", anthropicBaseUrl)
  391. await this.storeSecret("geminiApiKey", geminiApiKey)
  392. await this.storeSecret("openAiNativeApiKey", openAiNativeApiKey)
  393. await this.updateGlobalState("azureApiVersion", azureApiVersion)
  394. await this.updateGlobalState("openRouterModelId", openRouterModelId)
  395. await this.updateGlobalState("openRouterModelInfo", openRouterModelInfo)
  396. if (this.cline) {
  397. this.cline.api = buildApiHandler(message.apiConfiguration)
  398. }
  399. }
  400. await this.postStateToWebview()
  401. break
  402. case "customInstructions":
  403. await this.updateCustomInstructions(message.text)
  404. break
  405. case "alwaysAllowReadOnly":
  406. await this.updateGlobalState("alwaysAllowReadOnly", message.bool ?? undefined)
  407. if (this.cline) {
  408. this.cline.alwaysAllowReadOnly = message.bool ?? false
  409. }
  410. await this.postStateToWebview()
  411. break
  412. case "alwaysAllowWrite":
  413. await this.updateGlobalState("alwaysAllowWrite", message.bool ?? undefined)
  414. if (this.cline) {
  415. this.cline.alwaysAllowWrite = message.bool ?? false
  416. }
  417. await this.postStateToWebview()
  418. break
  419. case "alwaysAllowExecute":
  420. await this.updateGlobalState("alwaysAllowExecute", message.bool ?? undefined)
  421. if (this.cline) {
  422. this.cline.alwaysAllowExecute = message.bool ?? false
  423. }
  424. await this.postStateToWebview()
  425. break
  426. case "askResponse":
  427. this.cline?.handleWebviewAskResponse(message.askResponse!, message.text, message.images)
  428. break
  429. case "clearTask":
  430. // 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
  431. await this.clearTask()
  432. await this.postStateToWebview()
  433. break
  434. case "didShowAnnouncement":
  435. await this.updateGlobalState("lastShownAnnouncementId", this.latestAnnouncementId)
  436. await this.postStateToWebview()
  437. break
  438. case "selectImages":
  439. const images = await selectImages()
  440. await this.postMessageToWebview({ type: "selectedImages", images })
  441. break
  442. case "exportCurrentTask":
  443. const currentTaskId = this.cline?.taskId
  444. if (currentTaskId) {
  445. this.exportTaskWithId(currentTaskId)
  446. }
  447. break
  448. case "showTaskWithId":
  449. this.showTaskWithId(message.text!)
  450. break
  451. case "deleteTaskWithId":
  452. this.deleteTaskWithId(message.text!)
  453. break
  454. case "exportTaskWithId":
  455. this.exportTaskWithId(message.text!)
  456. break
  457. case "resetState":
  458. await this.resetState()
  459. break
  460. case "requestOllamaModels":
  461. const ollamaModels = await this.getOllamaModels(message.text)
  462. this.postMessageToWebview({ type: "ollamaModels", ollamaModels })
  463. break
  464. case "requestLmStudioModels":
  465. const lmStudioModels = await this.getLmStudioModels(message.text)
  466. this.postMessageToWebview({ type: "lmStudioModels", lmStudioModels })
  467. break
  468. case "refreshOpenRouterModels":
  469. await this.refreshOpenRouterModels()
  470. break
  471. case "openImage":
  472. openImage(message.text!)
  473. break
  474. case "openFile":
  475. openFile(message.text!)
  476. break
  477. case "openMention":
  478. openMention(message.text)
  479. break
  480. case "cancelTask":
  481. if (this.cline) {
  482. const { historyItem } = await this.getTaskWithId(this.cline.taskId)
  483. this.cline.abortTask()
  484. await pWaitFor(() => this.cline === undefined || this.cline.didFinishAborting, {
  485. timeout: 3_000,
  486. }).catch(() => {
  487. console.error("Failed to abort task")
  488. })
  489. if (this.cline) {
  490. // 'abandoned' will prevent this cline instance from affecting future cline instance gui. this may happen if its hanging on a streaming request
  491. this.cline.abandoned = true
  492. }
  493. await this.initClineWithHistoryItem(historyItem) // clears task again, so we need to abortTask manually above
  494. // await this.postStateToWebview() // new Cline instance will post state when it's ready. having this here sent an empty messages array to webview leading to virtuoso having to reload the entire list
  495. }
  496. break
  497. case "alwaysAllowBrowser":
  498. await this.updateGlobalState("alwaysAllowBrowser", message.bool ?? undefined)
  499. if (this.cline) {
  500. this.cline.alwaysAllowBrowser = message.bool ?? false
  501. }
  502. await this.postStateToWebview()
  503. break
  504. // Add more switch case statements here as more webview message commands
  505. // are created within the webview context (i.e. inside media/main.js)
  506. }
  507. },
  508. null,
  509. this.disposables
  510. )
  511. }
  512. async updateCustomInstructions(instructions?: string) {
  513. // User may be clearing the field
  514. await this.updateGlobalState("customInstructions", instructions || undefined)
  515. if (this.cline) {
  516. this.cline.customInstructions = instructions || undefined
  517. }
  518. await this.postStateToWebview()
  519. }
  520. // Ollama
  521. async getOllamaModels(baseUrl?: string) {
  522. try {
  523. if (!baseUrl) {
  524. baseUrl = "http://localhost:11434"
  525. }
  526. if (!URL.canParse(baseUrl)) {
  527. return []
  528. }
  529. const response = await axios.get(`${baseUrl}/api/tags`)
  530. const modelsArray = response.data?.models?.map((model: any) => model.name) || []
  531. const models = [...new Set<string>(modelsArray)]
  532. return models
  533. } catch (error) {
  534. return []
  535. }
  536. }
  537. // LM Studio
  538. async getLmStudioModels(baseUrl?: string) {
  539. try {
  540. if (!baseUrl) {
  541. baseUrl = "http://localhost:1234"
  542. }
  543. if (!URL.canParse(baseUrl)) {
  544. return []
  545. }
  546. const response = await axios.get(`${baseUrl}/v1/models`)
  547. const modelsArray = response.data?.data?.map((model: any) => model.id) || []
  548. const models = [...new Set<string>(modelsArray)]
  549. return models
  550. } catch (error) {
  551. return []
  552. }
  553. }
  554. // OpenRouter
  555. async handleOpenRouterCallback(code: string) {
  556. let apiKey: string
  557. try {
  558. const response = await axios.post("https://openrouter.ai/api/v1/auth/keys", { code })
  559. if (response.data && response.data.key) {
  560. apiKey = response.data.key
  561. } else {
  562. throw new Error("Invalid response from OpenRouter API")
  563. }
  564. } catch (error) {
  565. console.error("Error exchanging code for API key:", error)
  566. throw error
  567. }
  568. const openrouter: ApiProvider = "openrouter"
  569. await this.updateGlobalState("apiProvider", openrouter)
  570. await this.storeSecret("openRouterApiKey", apiKey)
  571. await this.postStateToWebview()
  572. if (this.cline) {
  573. this.cline.api = buildApiHandler({ apiProvider: openrouter, openRouterApiKey: apiKey })
  574. }
  575. // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome
  576. }
  577. private async ensureCacheDirectoryExists(): Promise<string> {
  578. const cacheDir = path.join(this.context.globalStorageUri.fsPath, "cache")
  579. await fs.mkdir(cacheDir, { recursive: true })
  580. return cacheDir
  581. }
  582. async readOpenRouterModels(): Promise<Record<string, ModelInfo> | undefined> {
  583. const openRouterModelsFilePath = path.join(
  584. await this.ensureCacheDirectoryExists(),
  585. GlobalFileNames.openRouterModels
  586. )
  587. const fileExists = await fileExistsAtPath(openRouterModelsFilePath)
  588. if (fileExists) {
  589. const fileContents = await fs.readFile(openRouterModelsFilePath, "utf8")
  590. return JSON.parse(fileContents)
  591. }
  592. return undefined
  593. }
  594. async refreshOpenRouterModels() {
  595. const openRouterModelsFilePath = path.join(
  596. await this.ensureCacheDirectoryExists(),
  597. GlobalFileNames.openRouterModels
  598. )
  599. let models: Record<string, ModelInfo> = {}
  600. try {
  601. const response = await axios.get("https://openrouter.ai/api/v1/models")
  602. /*
  603. {
  604. "id": "anthropic/claude-3.5-sonnet",
  605. "name": "Anthropic: Claude 3.5 Sonnet",
  606. "created": 1718841600,
  607. "description": "Claude 3.5 Sonnet delivers better-than-Opus capabilities, faster-than-Sonnet speeds, at the same Sonnet prices. Sonnet is particularly good at:\n\n- Coding: Autonomously writes, edits, and runs code with reasoning and troubleshooting\n- Data science: Augments human data science expertise; navigates unstructured data while using multiple tools for insights\n- Visual processing: excelling at interpreting charts, graphs, and images, accurately transcribing text to derive insights beyond just the text alone\n- Agentic tasks: exceptional tool use, making it great at agentic tasks (i.e. complex, multi-step problem solving tasks that require engaging with other systems)\n\n#multimodal",
  608. "context_length": 200000,
  609. "architecture": {
  610. "modality": "text+image-\u003Etext",
  611. "tokenizer": "Claude",
  612. "instruct_type": null
  613. },
  614. "pricing": {
  615. "prompt": "0.000003",
  616. "completion": "0.000015",
  617. "image": "0.0048",
  618. "request": "0"
  619. },
  620. "top_provider": {
  621. "context_length": 200000,
  622. "max_completion_tokens": 8192,
  623. "is_moderated": true
  624. },
  625. "per_request_limits": null
  626. },
  627. */
  628. if (response.data?.data) {
  629. const rawModels = response.data.data
  630. const parsePrice = (price: any) => {
  631. if (price) {
  632. return parseFloat(price) * 1_000_000
  633. }
  634. return undefined
  635. }
  636. for (const rawModel of rawModels) {
  637. const modelInfo: ModelInfo = {
  638. maxTokens: rawModel.top_provider?.max_completion_tokens,
  639. contextWindow: rawModel.context_length,
  640. supportsImages: rawModel.architecture?.modality?.includes("image"),
  641. supportsPromptCache: false,
  642. inputPrice: parsePrice(rawModel.pricing?.prompt),
  643. outputPrice: parsePrice(rawModel.pricing?.completion),
  644. description: rawModel.description,
  645. }
  646. switch (rawModel.id) {
  647. case "anthropic/claude-3.5-sonnet":
  648. case "anthropic/claude-3.5-sonnet:beta":
  649. // NOTE: this needs to be synced with api.ts/openrouter default model info
  650. modelInfo.supportsComputerUse = true
  651. modelInfo.supportsPromptCache = true
  652. modelInfo.cacheWritesPrice = 3.75
  653. modelInfo.cacheReadsPrice = 0.3
  654. break
  655. case "anthropic/claude-3.5-sonnet-20240620":
  656. case "anthropic/claude-3.5-sonnet-20240620:beta":
  657. modelInfo.supportsPromptCache = true
  658. modelInfo.cacheWritesPrice = 3.75
  659. modelInfo.cacheReadsPrice = 0.3
  660. break
  661. case "anthropic/claude-3-5-haiku":
  662. case "anthropic/claude-3-5-haiku:beta":
  663. case "anthropic/claude-3-5-haiku-20241022":
  664. case "anthropic/claude-3-5-haiku-20241022:beta":
  665. case "anthropic/claude-3.5-haiku":
  666. case "anthropic/claude-3.5-haiku:beta":
  667. case "anthropic/claude-3.5-haiku-20241022":
  668. case "anthropic/claude-3.5-haiku-20241022:beta":
  669. modelInfo.supportsPromptCache = true
  670. modelInfo.cacheWritesPrice = 1.25
  671. modelInfo.cacheReadsPrice = 0.1
  672. break
  673. case "anthropic/claude-3-opus":
  674. case "anthropic/claude-3-opus:beta":
  675. modelInfo.supportsPromptCache = true
  676. modelInfo.cacheWritesPrice = 18.75
  677. modelInfo.cacheReadsPrice = 1.5
  678. break
  679. case "anthropic/claude-3-haiku":
  680. case "anthropic/claude-3-haiku:beta":
  681. modelInfo.supportsPromptCache = true
  682. modelInfo.cacheWritesPrice = 0.3
  683. modelInfo.cacheReadsPrice = 0.03
  684. break
  685. }
  686. models[rawModel.id] = modelInfo
  687. }
  688. } else {
  689. console.error("Invalid response from OpenRouter API")
  690. }
  691. await fs.writeFile(openRouterModelsFilePath, JSON.stringify(models))
  692. console.log("OpenRouter models fetched and saved", models)
  693. } catch (error) {
  694. console.error("Error fetching OpenRouter models:", error)
  695. }
  696. await this.postMessageToWebview({ type: "openRouterModels", openRouterModels: models })
  697. return models
  698. }
  699. // Task history
  700. async getTaskWithId(id: string): Promise<{
  701. historyItem: HistoryItem
  702. taskDirPath: string
  703. apiConversationHistoryFilePath: string
  704. uiMessagesFilePath: string
  705. apiConversationHistory: Anthropic.MessageParam[]
  706. }> {
  707. const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || []
  708. const historyItem = history.find((item) => item.id === id)
  709. if (historyItem) {
  710. const taskDirPath = path.join(this.context.globalStorageUri.fsPath, "tasks", id)
  711. const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
  712. const uiMessagesFilePath = path.join(taskDirPath, GlobalFileNames.uiMessages)
  713. const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
  714. if (fileExists) {
  715. const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8"))
  716. return {
  717. historyItem,
  718. taskDirPath,
  719. apiConversationHistoryFilePath,
  720. uiMessagesFilePath,
  721. apiConversationHistory,
  722. }
  723. }
  724. }
  725. // if we tried to get a task that doesn't exist, remove it from state
  726. // FIXME: this seems to happen sometimes when the json file doesnt save to disk for some reason
  727. await this.deleteTaskFromState(id)
  728. throw new Error("Task not found")
  729. }
  730. async showTaskWithId(id: string) {
  731. if (id !== this.cline?.taskId) {
  732. // non-current task
  733. const { historyItem } = await this.getTaskWithId(id)
  734. await this.initClineWithHistoryItem(historyItem) // clears existing task
  735. }
  736. await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
  737. }
  738. async exportTaskWithId(id: string) {
  739. const { historyItem, apiConversationHistory } = await this.getTaskWithId(id)
  740. await downloadTask(historyItem.ts, apiConversationHistory)
  741. }
  742. async deleteTaskWithId(id: string) {
  743. if (id === this.cline?.taskId) {
  744. await this.clearTask()
  745. }
  746. const { taskDirPath, apiConversationHistoryFilePath, uiMessagesFilePath } = await this.getTaskWithId(id)
  747. await this.deleteTaskFromState(id)
  748. // Delete the task files
  749. const apiConversationHistoryFileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
  750. if (apiConversationHistoryFileExists) {
  751. await fs.unlink(apiConversationHistoryFilePath)
  752. }
  753. const uiMessagesFileExists = await fileExistsAtPath(uiMessagesFilePath)
  754. if (uiMessagesFileExists) {
  755. await fs.unlink(uiMessagesFilePath)
  756. }
  757. const legacyMessagesFilePath = path.join(taskDirPath, "claude_messages.json")
  758. if (await fileExistsAtPath(legacyMessagesFilePath)) {
  759. await fs.unlink(legacyMessagesFilePath)
  760. }
  761. await fs.rmdir(taskDirPath) // succeeds if the dir is empty
  762. }
  763. async deleteTaskFromState(id: string) {
  764. // Remove the task from history
  765. const taskHistory = ((await this.getGlobalState("taskHistory")) as HistoryItem[]) || []
  766. const updatedTaskHistory = taskHistory.filter((task) => task.id !== id)
  767. await this.updateGlobalState("taskHistory", updatedTaskHistory)
  768. // Notify the webview that the task has been deleted
  769. await this.postStateToWebview()
  770. }
  771. async postStateToWebview() {
  772. const state = await this.getStateToPostToWebview()
  773. this.postMessageToWebview({ type: "state", state })
  774. }
  775. async getStateToPostToWebview() {
  776. const {
  777. apiConfiguration,
  778. lastShownAnnouncementId,
  779. customInstructions,
  780. alwaysAllowReadOnly,
  781. alwaysAllowWrite,
  782. alwaysAllowExecute,
  783. alwaysAllowBrowser,
  784. taskHistory
  785. } = await this.getState()
  786. return {
  787. version: this.context.extension?.packageJSON?.version ?? "",
  788. apiConfiguration,
  789. customInstructions,
  790. alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
  791. alwaysAllowWrite: alwaysAllowWrite ?? false,
  792. alwaysAllowExecute: alwaysAllowExecute ?? false,
  793. alwaysAllowBrowser: alwaysAllowBrowser ?? false,
  794. uriScheme: vscode.env.uriScheme,
  795. clineMessages: this.cline?.clineMessages || [],
  796. taskHistory: (taskHistory || [])
  797. .filter((item) => item.ts && item.task)
  798. .sort((a, b) => b.ts - a.ts),
  799. shouldShowAnnouncement: lastShownAnnouncementId !== this.latestAnnouncementId,
  800. }
  801. }
  802. async clearTask() {
  803. this.cline?.abortTask()
  804. this.cline = undefined // removes reference to it, so once promises end it will be garbage collected
  805. }
  806. // Caching mechanism to keep track of webview messages + API conversation history per provider instance
  807. /*
  808. Now that we use retainContextWhenHidden, we don't have to store a cache of cline messages in the user's state, but we could to reduce memory footprint in long conversations.
  809. - We have to be careful of what state is shared between ClineProvider instances since there could be multiple instances of the extension running at once. For example when we cached cline messages using the same key, two instances of the extension could end up using the same key and overwriting each other's messages.
  810. - 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.
  811. We need to use a unique identifier for each ClineProvider instance's message cache since we could be running several instances of the extension outside of just the sidebar i.e. in editor panels.
  812. // conversation history to send in API requests
  813. /*
  814. It seems that some API messages do not comply with vscode state requirements. Either the Anthropic library is manipulating these values somehow in the backend in a way thats creating cyclic references, or the API returns a function or a Symbol as part of the message content.
  815. VSCode docs about state: "The value must be JSON-stringifyable ... value — A value. MUST not contain cyclic references."
  816. For now we'll store the conversation history in memory, and if we need to store in state directly we'd need to do a manual conversion to ensure proper json stringification.
  817. */
  818. // getApiConversationHistory(): Anthropic.MessageParam[] {
  819. // // const history = (await this.getGlobalState(
  820. // // this.getApiConversationHistoryStateKey()
  821. // // )) as Anthropic.MessageParam[]
  822. // // return history || []
  823. // return this.apiConversationHistory
  824. // }
  825. // setApiConversationHistory(history: Anthropic.MessageParam[] | undefined) {
  826. // // await this.updateGlobalState(this.getApiConversationHistoryStateKey(), history)
  827. // this.apiConversationHistory = history || []
  828. // }
  829. // addMessageToApiConversationHistory(message: Anthropic.MessageParam): Anthropic.MessageParam[] {
  830. // // const history = await this.getApiConversationHistory()
  831. // // history.push(message)
  832. // // await this.setApiConversationHistory(history)
  833. // // return history
  834. // this.apiConversationHistory.push(message)
  835. // return this.apiConversationHistory
  836. // }
  837. /*
  838. Storage
  839. https://dev.to/kompotkot/how-to-use-secretstorage-in-your-vscode-extensions-2hco
  840. https://www.eliostruyf.com/devhack-code-extension-storage-options/
  841. */
  842. async getState() {
  843. const [
  844. storedApiProvider,
  845. apiModelId,
  846. apiKey,
  847. openRouterApiKey,
  848. awsAccessKey,
  849. awsSecretKey,
  850. awsSessionToken,
  851. awsRegion,
  852. awsUseCrossRegionInference,
  853. vertexProjectId,
  854. vertexRegion,
  855. openAiBaseUrl,
  856. openAiApiKey,
  857. openAiModelId,
  858. ollamaModelId,
  859. ollamaBaseUrl,
  860. lmStudioModelId,
  861. lmStudioBaseUrl,
  862. anthropicBaseUrl,
  863. geminiApiKey,
  864. openAiNativeApiKey,
  865. azureApiVersion,
  866. openRouterModelId,
  867. openRouterModelInfo,
  868. lastShownAnnouncementId,
  869. customInstructions,
  870. alwaysAllowReadOnly,
  871. alwaysAllowWrite,
  872. alwaysAllowExecute,
  873. taskHistory,
  874. alwaysAllowBrowser,
  875. ] = await Promise.all([
  876. this.getGlobalState("apiProvider") as Promise<ApiProvider | undefined>,
  877. this.getGlobalState("apiModelId") as Promise<string | undefined>,
  878. this.getSecret("apiKey") as Promise<string | undefined>,
  879. this.getSecret("openRouterApiKey") as Promise<string | undefined>,
  880. this.getSecret("awsAccessKey") as Promise<string | undefined>,
  881. this.getSecret("awsSecretKey") as Promise<string | undefined>,
  882. this.getSecret("awsSessionToken") as Promise<string | undefined>,
  883. this.getGlobalState("awsRegion") as Promise<string | undefined>,
  884. this.getGlobalState("awsUseCrossRegionInference") as Promise<boolean | undefined>,
  885. this.getGlobalState("vertexProjectId") as Promise<string | undefined>,
  886. this.getGlobalState("vertexRegion") as Promise<string | undefined>,
  887. this.getGlobalState("openAiBaseUrl") as Promise<string | undefined>,
  888. this.getSecret("openAiApiKey") as Promise<string | undefined>,
  889. this.getGlobalState("openAiModelId") as Promise<string | undefined>,
  890. this.getGlobalState("ollamaModelId") as Promise<string | undefined>,
  891. this.getGlobalState("ollamaBaseUrl") as Promise<string | undefined>,
  892. this.getGlobalState("lmStudioModelId") as Promise<string | undefined>,
  893. this.getGlobalState("lmStudioBaseUrl") as Promise<string | undefined>,
  894. this.getGlobalState("anthropicBaseUrl") as Promise<string | undefined>,
  895. this.getSecret("geminiApiKey") as Promise<string | undefined>,
  896. this.getSecret("openAiNativeApiKey") as Promise<string | undefined>,
  897. this.getGlobalState("azureApiVersion") as Promise<string | undefined>,
  898. this.getGlobalState("openRouterModelId") as Promise<string | undefined>,
  899. this.getGlobalState("openRouterModelInfo") as Promise<ModelInfo | undefined>,
  900. this.getGlobalState("lastShownAnnouncementId") as Promise<string | undefined>,
  901. this.getGlobalState("customInstructions") as Promise<string | undefined>,
  902. this.getGlobalState("alwaysAllowReadOnly") as Promise<boolean | undefined>,
  903. this.getGlobalState("alwaysAllowWrite") as Promise<boolean | undefined>,
  904. this.getGlobalState("alwaysAllowExecute") as Promise<boolean | undefined>,
  905. this.getGlobalState("taskHistory") as Promise<HistoryItem[] | undefined>,
  906. this.getGlobalState("alwaysAllowBrowser") as Promise<boolean | undefined>,
  907. ])
  908. let apiProvider: ApiProvider
  909. if (storedApiProvider) {
  910. apiProvider = storedApiProvider
  911. } else {
  912. // Either new user or legacy user that doesn't have the apiProvider stored in state
  913. // (If they're using OpenRouter or Bedrock, then apiProvider state will exist)
  914. if (apiKey) {
  915. apiProvider = "anthropic"
  916. } else {
  917. // New users should default to openrouter
  918. apiProvider = "openrouter"
  919. }
  920. }
  921. return {
  922. apiConfiguration: {
  923. apiProvider,
  924. apiModelId,
  925. apiKey,
  926. openRouterApiKey,
  927. awsAccessKey,
  928. awsSecretKey,
  929. awsSessionToken,
  930. awsRegion,
  931. awsUseCrossRegionInference,
  932. vertexProjectId,
  933. vertexRegion,
  934. openAiBaseUrl,
  935. openAiApiKey,
  936. openAiModelId,
  937. ollamaModelId,
  938. ollamaBaseUrl,
  939. lmStudioModelId,
  940. lmStudioBaseUrl,
  941. anthropicBaseUrl,
  942. geminiApiKey,
  943. openAiNativeApiKey,
  944. azureApiVersion,
  945. openRouterModelId,
  946. openRouterModelInfo,
  947. },
  948. lastShownAnnouncementId,
  949. customInstructions,
  950. alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
  951. alwaysAllowWrite: alwaysAllowWrite ?? false,
  952. alwaysAllowExecute: alwaysAllowExecute ?? false,
  953. alwaysAllowBrowser: alwaysAllowBrowser ?? false,
  954. taskHistory,
  955. }
  956. }
  957. async updateTaskHistory(item: HistoryItem): Promise<HistoryItem[]> {
  958. const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[]) || []
  959. const existingItemIndex = history.findIndex((h) => h.id === item.id)
  960. if (existingItemIndex !== -1) {
  961. history[existingItemIndex] = item
  962. } else {
  963. history.push(item)
  964. }
  965. await this.updateGlobalState("taskHistory", history)
  966. return history
  967. }
  968. // global
  969. async updateGlobalState(key: GlobalStateKey, value: any) {
  970. await this.context.globalState.update(key, value)
  971. }
  972. async getGlobalState(key: GlobalStateKey) {
  973. return await this.context.globalState.get(key)
  974. }
  975. // workspace
  976. private async updateWorkspaceState(key: string, value: any) {
  977. await this.context.workspaceState.update(key, value)
  978. }
  979. private async getWorkspaceState(key: string) {
  980. return await this.context.workspaceState.get(key)
  981. }
  982. // private async clearState() {
  983. // this.context.workspaceState.keys().forEach((key) => {
  984. // this.context.workspaceState.update(key, undefined)
  985. // })
  986. // this.context.globalState.keys().forEach((key) => {
  987. // this.context.globalState.update(key, undefined)
  988. // })
  989. // this.context.secrets.delete("apiKey")
  990. // }
  991. // secrets
  992. private async storeSecret(key: SecretKey, value?: string) {
  993. if (value) {
  994. await this.context.secrets.store(key, value)
  995. } else {
  996. await this.context.secrets.delete(key)
  997. }
  998. }
  999. private async getSecret(key: SecretKey) {
  1000. return await this.context.secrets.get(key)
  1001. }
  1002. // dev
  1003. async resetState() {
  1004. vscode.window.showInformationMessage("Resetting state...")
  1005. for (const key of this.context.globalState.keys()) {
  1006. await this.context.globalState.update(key, undefined)
  1007. }
  1008. const secretKeys: SecretKey[] = [
  1009. "apiKey",
  1010. "openRouterApiKey",
  1011. "awsAccessKey",
  1012. "awsSecretKey",
  1013. "awsSessionToken",
  1014. "openAiApiKey",
  1015. "geminiApiKey",
  1016. "openAiNativeApiKey",
  1017. ]
  1018. for (const key of secretKeys) {
  1019. await this.storeSecret(key, undefined)
  1020. }
  1021. if (this.cline) {
  1022. this.cline.abortTask()
  1023. this.cline = undefined
  1024. }
  1025. vscode.window.showInformationMessage("State reset")
  1026. await this.postStateToWebview()
  1027. await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
  1028. }
  1029. }