ClineProvider.ts 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. import { Anthropic } from "@anthropic-ai/sdk"
  2. import delay from "delay"
  3. import axios from "axios"
  4. import fs from "fs/promises"
  5. import os from "os"
  6. import pWaitFor from "p-wait-for"
  7. import * as path from "path"
  8. import * as vscode from "vscode"
  9. import { buildApiHandler } from "../../api"
  10. import { downloadTask } from "../../integrations/misc/export-markdown"
  11. import { openFile, openImage } from "../../integrations/misc/open-file"
  12. import { selectImages } from "../../integrations/misc/process-images"
  13. import { getTheme } from "../../integrations/theme/getTheme"
  14. import { getDiffStrategy } from "../diff/DiffStrategy"
  15. import WorkspaceTracker from "../../integrations/workspace/WorkspaceTracker"
  16. import { McpHub } from "../../services/mcp/McpHub"
  17. import { ApiConfiguration, ApiProvider, ModelInfo } from "../../shared/api"
  18. import { findLast } from "../../shared/array"
  19. import { ApiConfigMeta, ExtensionMessage } from "../../shared/ExtensionMessage"
  20. import { HistoryItem } from "../../shared/HistoryItem"
  21. import { WebviewMessage } from "../../shared/WebviewMessage"
  22. import {
  23. Mode,
  24. modes,
  25. CustomModePrompts,
  26. PromptComponent,
  27. ModeConfig,
  28. defaultModeSlug,
  29. getModeBySlug,
  30. } from "../../shared/modes"
  31. import { SYSTEM_PROMPT } from "../prompts/system"
  32. import { fileExistsAtPath } from "../../utils/fs"
  33. import { Cline } from "../Cline"
  34. import { openMention } from "../mentions"
  35. import { getNonce } from "./getNonce"
  36. import { getUri } from "./getUri"
  37. import { playSound, setSoundEnabled, setSoundVolume } from "../../utils/sound"
  38. import { checkExistKey } from "../../shared/checkExistApiConfig"
  39. import { singleCompletionHandler } from "../../utils/single-completion-handler"
  40. import { getCommitInfo, searchCommits, getWorkingState } from "../../utils/git"
  41. import { ConfigManager } from "../config/ConfigManager"
  42. import { CustomModesManager } from "../config/CustomModesManager"
  43. import { EXPERIMENT_IDS, experiments as Experiments, experimentDefault, ExperimentId } from "../../shared/experiments"
  44. import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt"
  45. import { ACTION_NAMES } from "../CodeActionProvider"
  46. /*
  47. https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
  48. https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
  49. */
  50. type SecretKey =
  51. | "apiKey"
  52. | "glamaApiKey"
  53. | "openRouterApiKey"
  54. | "awsAccessKey"
  55. | "awsSecretKey"
  56. | "awsSessionToken"
  57. | "openAiApiKey"
  58. | "geminiApiKey"
  59. | "openAiNativeApiKey"
  60. | "deepSeekApiKey"
  61. | "mistralApiKey"
  62. | "unboundApiKey"
  63. type GlobalStateKey =
  64. | "apiProvider"
  65. | "apiModelId"
  66. | "glamaModelId"
  67. | "glamaModelInfo"
  68. | "awsRegion"
  69. | "awsUseCrossRegionInference"
  70. | "awsProfile"
  71. | "awsUseProfile"
  72. | "vertexProjectId"
  73. | "vertexRegion"
  74. | "lastShownAnnouncementId"
  75. | "customInstructions"
  76. | "alwaysAllowReadOnly"
  77. | "alwaysAllowWrite"
  78. | "alwaysAllowExecute"
  79. | "alwaysAllowBrowser"
  80. | "alwaysAllowMcp"
  81. | "alwaysAllowModeSwitch"
  82. | "taskHistory"
  83. | "openAiBaseUrl"
  84. | "openAiModelId"
  85. | "openAiCustomModelInfo"
  86. | "openAiUseAzure"
  87. | "ollamaModelId"
  88. | "ollamaBaseUrl"
  89. | "lmStudioModelId"
  90. | "lmStudioBaseUrl"
  91. | "anthropicBaseUrl"
  92. | "azureApiVersion"
  93. | "openAiStreamingEnabled"
  94. | "openRouterModelId"
  95. | "openRouterModelInfo"
  96. | "openRouterBaseUrl"
  97. | "openRouterUseMiddleOutTransform"
  98. | "allowedCommands"
  99. | "soundEnabled"
  100. | "soundVolume"
  101. | "diffEnabled"
  102. | "browserViewportSize"
  103. | "screenshotQuality"
  104. | "fuzzyMatchThreshold"
  105. | "preferredLanguage" // Language setting for Cline's communication
  106. | "writeDelayMs"
  107. | "terminalOutputLineLimit"
  108. | "mcpEnabled"
  109. | "alwaysApproveResubmit"
  110. | "requestDelaySeconds"
  111. | "currentApiConfigName"
  112. | "listApiConfigMeta"
  113. | "vsCodeLmModelSelector"
  114. | "mode"
  115. | "modeApiConfigs"
  116. | "customModePrompts"
  117. | "customSupportPrompts"
  118. | "enhancementApiConfigId"
  119. | "experiments" // Map of experiment IDs to their enabled state
  120. | "autoApprovalEnabled"
  121. | "customModes" // Array of custom modes
  122. | "unboundModelId"
  123. export const GlobalFileNames = {
  124. apiConversationHistory: "api_conversation_history.json",
  125. uiMessages: "ui_messages.json",
  126. glamaModels: "glama_models.json",
  127. openRouterModels: "openrouter_models.json",
  128. mcpSettings: "cline_mcp_settings.json",
  129. }
  130. export class ClineProvider implements vscode.WebviewViewProvider {
  131. public static readonly sideBarId = "roo-cline.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.
  132. public static readonly tabPanelId = "roo-cline.TabPanelProvider"
  133. private static activeInstances: Set<ClineProvider> = new Set()
  134. private disposables: vscode.Disposable[] = []
  135. private view?: vscode.WebviewView | vscode.WebviewPanel
  136. private cline?: Cline
  137. private workspaceTracker?: WorkspaceTracker
  138. mcpHub?: McpHub
  139. private latestAnnouncementId = "jan-21-2025-custom-modes" // update to some unique identifier when we add a new announcement
  140. configManager: ConfigManager
  141. customModesManager: CustomModesManager
  142. constructor(
  143. readonly context: vscode.ExtensionContext,
  144. private readonly outputChannel: vscode.OutputChannel,
  145. ) {
  146. this.outputChannel.appendLine("ClineProvider instantiated")
  147. ClineProvider.activeInstances.add(this)
  148. this.workspaceTracker = new WorkspaceTracker(this)
  149. this.mcpHub = new McpHub(this)
  150. this.configManager = new ConfigManager(this.context)
  151. this.customModesManager = new CustomModesManager(this.context, async () => {
  152. await this.postStateToWebview()
  153. })
  154. }
  155. /*
  156. 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.
  157. - https://vscode-docs.readthedocs.io/en/stable/extensions/patterns-and-principles/
  158. - https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts
  159. */
  160. async dispose() {
  161. this.outputChannel.appendLine("Disposing ClineProvider...")
  162. await this.clearTask()
  163. this.outputChannel.appendLine("Cleared task")
  164. if (this.view && "dispose" in this.view) {
  165. this.view.dispose()
  166. this.outputChannel.appendLine("Disposed webview")
  167. }
  168. while (this.disposables.length) {
  169. const x = this.disposables.pop()
  170. if (x) {
  171. x.dispose()
  172. }
  173. }
  174. this.workspaceTracker?.dispose()
  175. this.workspaceTracker = undefined
  176. this.mcpHub?.dispose()
  177. this.mcpHub = undefined
  178. this.customModesManager?.dispose()
  179. this.outputChannel.appendLine("Disposed all disposables")
  180. ClineProvider.activeInstances.delete(this)
  181. }
  182. public static getVisibleInstance(): ClineProvider | undefined {
  183. return findLast(Array.from(this.activeInstances), (instance) => instance.view?.visible === true)
  184. }
  185. public static async getInstance(): Promise<ClineProvider | undefined> {
  186. let visibleProvider = ClineProvider.getVisibleInstance()
  187. // If no visible provider, try to show the sidebar view
  188. if (!visibleProvider) {
  189. await vscode.commands.executeCommand("roo-cline.SidebarProvider.focus")
  190. // Wait briefly for the view to become visible
  191. await delay(100)
  192. visibleProvider = ClineProvider.getVisibleInstance()
  193. }
  194. // If still no visible provider, return
  195. if (!visibleProvider) {
  196. return
  197. }
  198. return visibleProvider
  199. }
  200. public static async isActiveTask(): Promise<boolean> {
  201. const visibleProvider = await ClineProvider.getInstance()
  202. if (!visibleProvider) {
  203. return false
  204. }
  205. if (visibleProvider.cline) {
  206. return true
  207. }
  208. return false
  209. }
  210. public static async handleCodeAction(
  211. command: string,
  212. promptType: keyof typeof ACTION_NAMES,
  213. params: Record<string, string | any[]>,
  214. ): Promise<void> {
  215. const visibleProvider = await ClineProvider.getInstance()
  216. if (!visibleProvider) {
  217. return
  218. }
  219. const { customSupportPrompts } = await visibleProvider.getState()
  220. const prompt = supportPrompt.create(promptType, params, customSupportPrompts)
  221. if (command.endsWith("addToContext")) {
  222. await visibleProvider.postMessageToWebview({
  223. type: "invoke",
  224. invoke: "setChatBoxMessage",
  225. text: prompt,
  226. })
  227. return
  228. }
  229. if (visibleProvider.cline && command.endsWith("InCurrentTask")) {
  230. await visibleProvider.postMessageToWebview({
  231. type: "invoke",
  232. invoke: "sendMessage",
  233. text: prompt,
  234. })
  235. return
  236. }
  237. await visibleProvider.initClineWithTask(prompt)
  238. }
  239. resolveWebviewView(
  240. webviewView: vscode.WebviewView | vscode.WebviewPanel,
  241. //context: vscode.WebviewViewResolveContext<unknown>, used to recreate a deallocated webview, but we don't need this since we use retainContextWhenHidden
  242. //token: vscode.CancellationToken
  243. ): void | Thenable<void> {
  244. this.outputChannel.appendLine("Resolving webview view")
  245. this.view = webviewView
  246. // Initialize sound enabled state
  247. this.getState().then(({ soundEnabled }) => {
  248. setSoundEnabled(soundEnabled ?? false)
  249. })
  250. webviewView.webview.options = {
  251. // Allow scripts in the webview
  252. enableScripts: true,
  253. localResourceRoots: [this.context.extensionUri],
  254. }
  255. webviewView.webview.html =
  256. this.context.extensionMode === vscode.ExtensionMode.Development
  257. ? this.getHMRHtmlContent(webviewView.webview)
  258. : this.getHtmlContent(webviewView.webview)
  259. // Sets up an event listener to listen for messages passed from the webview view context
  260. // and executes code based on the message that is recieved
  261. this.setWebviewMessageListener(webviewView.webview)
  262. // Logs show up in bottom panel > Debug Console
  263. //console.log("registering listener")
  264. // Listen for when the panel becomes visible
  265. // https://github.com/microsoft/vscode-discussions/discussions/840
  266. if ("onDidChangeViewState" in webviewView) {
  267. // WebviewView and WebviewPanel have all the same properties except for this visibility listener
  268. // panel
  269. webviewView.onDidChangeViewState(
  270. () => {
  271. if (this.view?.visible) {
  272. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  273. }
  274. },
  275. null,
  276. this.disposables,
  277. )
  278. } else if ("onDidChangeVisibility" in webviewView) {
  279. // sidebar
  280. webviewView.onDidChangeVisibility(
  281. () => {
  282. if (this.view?.visible) {
  283. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  284. }
  285. },
  286. null,
  287. this.disposables,
  288. )
  289. }
  290. // Listen for when the view is disposed
  291. // This happens when the user closes the view or when the view is closed programmatically
  292. webviewView.onDidDispose(
  293. async () => {
  294. await this.dispose()
  295. },
  296. null,
  297. this.disposables,
  298. )
  299. // Listen for when color changes
  300. vscode.workspace.onDidChangeConfiguration(
  301. async (e) => {
  302. if (e && e.affectsConfiguration("workbench.colorTheme")) {
  303. // Sends latest theme name to webview
  304. await this.postMessageToWebview({ type: "theme", text: JSON.stringify(await getTheme()) })
  305. }
  306. },
  307. null,
  308. this.disposables,
  309. )
  310. // if the extension is starting a new session, clear previous task state
  311. this.clearTask()
  312. this.outputChannel.appendLine("Webview view resolved")
  313. }
  314. public async initClineWithTask(task?: string, images?: string[]) {
  315. await this.clearTask()
  316. const {
  317. apiConfiguration,
  318. customModePrompts,
  319. diffEnabled,
  320. fuzzyMatchThreshold,
  321. mode,
  322. customInstructions: globalInstructions,
  323. experiments,
  324. } = await this.getState()
  325. const modePrompt = customModePrompts?.[mode] as PromptComponent
  326. const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
  327. this.cline = new Cline(
  328. this,
  329. apiConfiguration,
  330. effectiveInstructions,
  331. diffEnabled,
  332. fuzzyMatchThreshold,
  333. task,
  334. images,
  335. undefined,
  336. experiments,
  337. )
  338. }
  339. public async initClineWithHistoryItem(historyItem: HistoryItem) {
  340. await this.clearTask()
  341. const {
  342. apiConfiguration,
  343. customModePrompts,
  344. diffEnabled,
  345. fuzzyMatchThreshold,
  346. mode,
  347. customInstructions: globalInstructions,
  348. experiments,
  349. } = await this.getState()
  350. const modePrompt = customModePrompts?.[mode] as PromptComponent
  351. const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
  352. this.cline = new Cline(
  353. this,
  354. apiConfiguration,
  355. effectiveInstructions,
  356. diffEnabled,
  357. fuzzyMatchThreshold,
  358. undefined,
  359. undefined,
  360. historyItem,
  361. experiments,
  362. )
  363. }
  364. public async postMessageToWebview(message: ExtensionMessage) {
  365. await this.view?.webview.postMessage(message)
  366. }
  367. private getHMRHtmlContent(webview: vscode.Webview): string {
  368. const nonce = getNonce()
  369. const stylesUri = getUri(webview, this.context.extensionUri, ["webview-ui", "build", "assets", "index.css"])
  370. const codiconsUri = getUri(webview, this.context.extensionUri, [
  371. "node_modules",
  372. "@vscode",
  373. "codicons",
  374. "dist",
  375. "codicon.css",
  376. ])
  377. const file = "src/index.tsx"
  378. const localPort = "5173"
  379. const localServerUrl = `localhost:${localPort}`
  380. const scriptUri = `http://${localServerUrl}/${file}`
  381. const reactRefresh = /*html*/ `
  382. <script nonce="${nonce}" type="module">
  383. import RefreshRuntime from "http://localhost:${localPort}/@react-refresh"
  384. RefreshRuntime.injectIntoGlobalHook(window)
  385. window.$RefreshReg$ = () => {}
  386. window.$RefreshSig$ = () => (type) => type
  387. window.__vite_plugin_react_preamble_installed__ = true
  388. </script>
  389. `
  390. const csp = [
  391. "default-src 'none'",
  392. `font-src ${webview.cspSource}`,
  393. `style-src ${webview.cspSource} 'unsafe-inline' https://* http://${localServerUrl} http://0.0.0.0:${localPort}`,
  394. `img-src ${webview.cspSource} data:`,
  395. `script-src 'unsafe-eval' https://* http://${localServerUrl} http://0.0.0.0:${localPort} 'nonce-${nonce}'`,
  396. `connect-src https://* ws://${localServerUrl} ws://0.0.0.0:${localPort} http://${localServerUrl} http://0.0.0.0:${localPort}`,
  397. ]
  398. return /*html*/ `
  399. <!DOCTYPE html>
  400. <html lang="en">
  401. <head>
  402. <meta charset="utf-8">
  403. <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  404. <meta http-equiv="Content-Security-Policy" content="${csp.join("; ")}">
  405. <link rel="stylesheet" type="text/css" href="${stylesUri}">
  406. <link href="${codiconsUri}" rel="stylesheet" />
  407. <title>Roo Code</title>
  408. </head>
  409. <body>
  410. <div id="root"></div>
  411. ${reactRefresh}
  412. <script type="module" src="${scriptUri}"></script>
  413. </body>
  414. </html>
  415. `
  416. }
  417. /**
  418. * Defines and returns the HTML that should be rendered within the webview panel.
  419. *
  420. * @remarks This is also the place where references to the React webview build files
  421. * are created and inserted into the webview HTML.
  422. *
  423. * @param webview A reference to the extension webview
  424. * @param extensionUri The URI of the directory containing the extension
  425. * @returns A template string literal containing the HTML that should be
  426. * rendered within the webview panel
  427. */
  428. private getHtmlContent(webview: vscode.Webview): string {
  429. // Get the local path to main script run in the webview,
  430. // then convert it to a uri we can use in the webview.
  431. // The CSS file from the React build output
  432. const stylesUri = getUri(webview, this.context.extensionUri, ["webview-ui", "build", "assets", "index.css"])
  433. // The JS file from the React build output
  434. const scriptUri = getUri(webview, this.context.extensionUri, ["webview-ui", "build", "assets", "index.js"])
  435. // The codicon font from the React build output
  436. // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-codicons-sample/src/extension.ts
  437. // 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
  438. // don't forget to add font-src ${webview.cspSource};
  439. const codiconsUri = getUri(webview, this.context.extensionUri, [
  440. "node_modules",
  441. "@vscode",
  442. "codicons",
  443. "dist",
  444. "codicon.css",
  445. ])
  446. // const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.js"))
  447. // const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "reset.css"))
  448. // const styleVSCodeUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "vscode.css"))
  449. // // Same for stylesheet
  450. // const stylesheetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.css"))
  451. // Use a nonce to only allow a specific script to be run.
  452. /*
  453. content security policy of your webview to only allow scripts that have a specific nonce
  454. create a content security policy meta tag so that only loading scripts with a nonce is allowed
  455. 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.
  456. <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}';">
  457. - 'unsafe-inline' is required for styles due to vscode-webview-toolkit's dynamic style injection
  458. - since we pass base64 images to the webview, we need to specify img-src ${webview.cspSource} data:;
  459. 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.
  460. */
  461. const nonce = getNonce()
  462. // Tip: Install the es6-string-html VS Code extension to enable code highlighting below
  463. return /*html*/ `
  464. <!DOCTYPE html>
  465. <html lang="en">
  466. <head>
  467. <meta charset="utf-8">
  468. <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  469. <meta name="theme-color" content="#000000">
  470. <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}';">
  471. <link rel="stylesheet" type="text/css" href="${stylesUri}">
  472. <link href="${codiconsUri}" rel="stylesheet" />
  473. <title>Roo Code</title>
  474. </head>
  475. <body>
  476. <noscript>You need to enable JavaScript to run this app.</noscript>
  477. <div id="root"></div>
  478. <script nonce="${nonce}" src="${scriptUri}"></script>
  479. </body>
  480. </html>
  481. `
  482. }
  483. /**
  484. * Sets up an event listener to listen for messages passed from the webview context and
  485. * executes code based on the message that is recieved.
  486. *
  487. * @param webview A reference to the extension webview
  488. */
  489. private setWebviewMessageListener(webview: vscode.Webview) {
  490. webview.onDidReceiveMessage(
  491. async (message: WebviewMessage) => {
  492. switch (message.type) {
  493. case "webviewDidLaunch":
  494. // Load custom modes first
  495. const customModes = await this.customModesManager.getCustomModes()
  496. await this.updateGlobalState("customModes", customModes)
  497. this.postStateToWebview()
  498. this.workspaceTracker?.initializeFilePaths() // don't await
  499. getTheme().then((theme) =>
  500. this.postMessageToWebview({ type: "theme", text: JSON.stringify(theme) }),
  501. )
  502. // post last cached models in case the call to endpoint fails
  503. this.readOpenRouterModels().then((openRouterModels) => {
  504. if (openRouterModels) {
  505. this.postMessageToWebview({ type: "openRouterModels", openRouterModels })
  506. }
  507. })
  508. // 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.
  509. // 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
  510. // (see normalizeApiConfiguration > openrouter)
  511. this.refreshOpenRouterModels().then(async (openRouterModels) => {
  512. if (openRouterModels) {
  513. // 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)
  514. const { apiConfiguration } = await this.getState()
  515. if (apiConfiguration.openRouterModelId) {
  516. await this.updateGlobalState(
  517. "openRouterModelInfo",
  518. openRouterModels[apiConfiguration.openRouterModelId],
  519. )
  520. await this.postStateToWebview()
  521. }
  522. }
  523. })
  524. this.readGlamaModels().then((glamaModels) => {
  525. if (glamaModels) {
  526. this.postMessageToWebview({ type: "glamaModels", glamaModels })
  527. }
  528. })
  529. this.refreshGlamaModels().then(async (glamaModels) => {
  530. if (glamaModels) {
  531. // 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)
  532. const { apiConfiguration } = await this.getState()
  533. if (apiConfiguration.glamaModelId) {
  534. await this.updateGlobalState(
  535. "glamaModelInfo",
  536. glamaModels[apiConfiguration.glamaModelId],
  537. )
  538. await this.postStateToWebview()
  539. }
  540. }
  541. })
  542. this.configManager
  543. .listConfig()
  544. .then(async (listApiConfig) => {
  545. if (!listApiConfig) {
  546. return
  547. }
  548. if (listApiConfig.length === 1) {
  549. // check if first time init then sync with exist config
  550. if (!checkExistKey(listApiConfig[0])) {
  551. const { apiConfiguration } = await this.getState()
  552. await this.configManager.saveConfig(
  553. listApiConfig[0].name ?? "default",
  554. apiConfiguration,
  555. )
  556. listApiConfig[0].apiProvider = apiConfiguration.apiProvider
  557. }
  558. }
  559. const currentConfigName = (await this.getGlobalState("currentApiConfigName")) as string
  560. if (currentConfigName) {
  561. if (!(await this.configManager.hasConfig(currentConfigName))) {
  562. // current config name not valid, get first config in list
  563. await this.updateGlobalState("currentApiConfigName", listApiConfig?.[0]?.name)
  564. if (listApiConfig?.[0]?.name) {
  565. const apiConfig = await this.configManager.loadConfig(
  566. listApiConfig?.[0]?.name,
  567. )
  568. await Promise.all([
  569. this.updateGlobalState("listApiConfigMeta", listApiConfig),
  570. this.postMessageToWebview({ type: "listApiConfig", listApiConfig }),
  571. this.updateApiConfiguration(apiConfig),
  572. ])
  573. await this.postStateToWebview()
  574. return
  575. }
  576. }
  577. }
  578. await Promise.all([
  579. await this.updateGlobalState("listApiConfigMeta", listApiConfig),
  580. await this.postMessageToWebview({ type: "listApiConfig", listApiConfig }),
  581. ])
  582. })
  583. .catch((error) =>
  584. this.outputChannel.appendLine(
  585. `Error list api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  586. ),
  587. )
  588. break
  589. case "newTask":
  590. // Code that should run in response to the hello message command
  591. //vscode.window.showInformationMessage(message.text!)
  592. // Send a message to our webview.
  593. // You can send any JSON serializable data.
  594. // Could also do this in extension .ts
  595. //this.postMessageToWebview({ type: "text", text: `Extension: ${Date.now()}` })
  596. // 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
  597. await this.initClineWithTask(message.text, message.images)
  598. break
  599. case "apiConfiguration":
  600. if (message.apiConfiguration) {
  601. await this.updateApiConfiguration(message.apiConfiguration)
  602. }
  603. await this.postStateToWebview()
  604. break
  605. case "customInstructions":
  606. await this.updateCustomInstructions(message.text)
  607. break
  608. case "alwaysAllowReadOnly":
  609. await this.updateGlobalState("alwaysAllowReadOnly", message.bool ?? undefined)
  610. await this.postStateToWebview()
  611. break
  612. case "alwaysAllowWrite":
  613. await this.updateGlobalState("alwaysAllowWrite", message.bool ?? undefined)
  614. await this.postStateToWebview()
  615. break
  616. case "alwaysAllowExecute":
  617. await this.updateGlobalState("alwaysAllowExecute", message.bool ?? undefined)
  618. await this.postStateToWebview()
  619. break
  620. case "alwaysAllowBrowser":
  621. await this.updateGlobalState("alwaysAllowBrowser", message.bool ?? undefined)
  622. await this.postStateToWebview()
  623. break
  624. case "alwaysAllowMcp":
  625. await this.updateGlobalState("alwaysAllowMcp", message.bool)
  626. await this.postStateToWebview()
  627. break
  628. case "alwaysAllowModeSwitch":
  629. await this.updateGlobalState("alwaysAllowModeSwitch", message.bool)
  630. await this.postStateToWebview()
  631. break
  632. case "askResponse":
  633. this.cline?.handleWebviewAskResponse(message.askResponse!, message.text, message.images)
  634. break
  635. case "clearTask":
  636. // 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
  637. await this.clearTask()
  638. await this.postStateToWebview()
  639. break
  640. case "didShowAnnouncement":
  641. await this.updateGlobalState("lastShownAnnouncementId", this.latestAnnouncementId)
  642. await this.postStateToWebview()
  643. break
  644. case "selectImages":
  645. const images = await selectImages()
  646. await this.postMessageToWebview({ type: "selectedImages", images })
  647. break
  648. case "exportCurrentTask":
  649. const currentTaskId = this.cline?.taskId
  650. if (currentTaskId) {
  651. this.exportTaskWithId(currentTaskId)
  652. }
  653. break
  654. case "showTaskWithId":
  655. this.showTaskWithId(message.text!)
  656. break
  657. case "deleteTaskWithId":
  658. this.deleteTaskWithId(message.text!)
  659. break
  660. case "exportTaskWithId":
  661. this.exportTaskWithId(message.text!)
  662. break
  663. case "resetState":
  664. await this.resetState()
  665. break
  666. case "requestOllamaModels":
  667. const ollamaModels = await this.getOllamaModels(message.text)
  668. this.postMessageToWebview({ type: "ollamaModels", ollamaModels })
  669. break
  670. case "requestLmStudioModels":
  671. const lmStudioModels = await this.getLmStudioModels(message.text)
  672. this.postMessageToWebview({ type: "lmStudioModels", lmStudioModels })
  673. break
  674. case "requestVsCodeLmModels":
  675. const vsCodeLmModels = await this.getVsCodeLmModels()
  676. this.postMessageToWebview({ type: "vsCodeLmModels", vsCodeLmModels })
  677. break
  678. case "refreshGlamaModels":
  679. await this.refreshGlamaModels()
  680. break
  681. case "refreshOpenRouterModels":
  682. await this.refreshOpenRouterModels()
  683. break
  684. case "refreshOpenAiModels":
  685. if (message?.values?.baseUrl && message?.values?.apiKey) {
  686. const openAiModels = await this.getOpenAiModels(
  687. message?.values?.baseUrl,
  688. message?.values?.apiKey,
  689. )
  690. this.postMessageToWebview({ type: "openAiModels", openAiModels })
  691. }
  692. break
  693. case "openImage":
  694. openImage(message.text!)
  695. break
  696. case "openFile":
  697. openFile(message.text!, message.values as { create?: boolean; content?: string })
  698. break
  699. case "openMention":
  700. openMention(message.text)
  701. break
  702. case "cancelTask":
  703. if (this.cline) {
  704. const { historyItem } = await this.getTaskWithId(this.cline.taskId)
  705. this.cline.abortTask()
  706. await pWaitFor(() => this.cline === undefined || this.cline.didFinishAborting, {
  707. timeout: 3_000,
  708. }).catch((error) => {
  709. this.outputChannel.appendLine(
  710. `Failed to abort task ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  711. )
  712. })
  713. if (this.cline) {
  714. // 'abandoned' will prevent this cline instance from affecting future cline instance gui. this may happen if its hanging on a streaming request
  715. this.cline.abandoned = true
  716. }
  717. await this.initClineWithHistoryItem(historyItem) // clears task again, so we need to abortTask manually above
  718. // 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
  719. }
  720. break
  721. case "allowedCommands":
  722. await this.context.globalState.update("allowedCommands", message.commands)
  723. // Also update workspace settings
  724. await vscode.workspace
  725. .getConfiguration("roo-cline")
  726. .update("allowedCommands", message.commands, vscode.ConfigurationTarget.Global)
  727. break
  728. case "openMcpSettings": {
  729. const mcpSettingsFilePath = await this.mcpHub?.getMcpSettingsFilePath()
  730. if (mcpSettingsFilePath) {
  731. openFile(mcpSettingsFilePath)
  732. }
  733. break
  734. }
  735. case "openCustomModesSettings": {
  736. const customModesFilePath = await this.customModesManager.getCustomModesFilePath()
  737. if (customModesFilePath) {
  738. openFile(customModesFilePath)
  739. }
  740. break
  741. }
  742. case "restartMcpServer": {
  743. try {
  744. await this.mcpHub?.restartConnection(message.text!)
  745. } catch (error) {
  746. this.outputChannel.appendLine(
  747. `Failed to retry connection for ${message.text}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  748. )
  749. }
  750. break
  751. }
  752. case "toggleToolAlwaysAllow": {
  753. try {
  754. await this.mcpHub?.toggleToolAlwaysAllow(
  755. message.serverName!,
  756. message.toolName!,
  757. message.alwaysAllow!,
  758. )
  759. } catch (error) {
  760. this.outputChannel.appendLine(
  761. `Failed to toggle auto-approve for tool ${message.toolName}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  762. )
  763. }
  764. break
  765. }
  766. case "toggleMcpServer": {
  767. try {
  768. await this.mcpHub?.toggleServerDisabled(message.serverName!, message.disabled!)
  769. } catch (error) {
  770. this.outputChannel.appendLine(
  771. `Failed to toggle MCP server ${message.serverName}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  772. )
  773. }
  774. break
  775. }
  776. case "mcpEnabled":
  777. const mcpEnabled = message.bool ?? true
  778. await this.updateGlobalState("mcpEnabled", mcpEnabled)
  779. await this.postStateToWebview()
  780. break
  781. case "playSound":
  782. if (message.audioType) {
  783. const soundPath = path.join(this.context.extensionPath, "audio", `${message.audioType}.wav`)
  784. playSound(soundPath)
  785. }
  786. break
  787. case "soundEnabled":
  788. const soundEnabled = message.bool ?? true
  789. await this.updateGlobalState("soundEnabled", soundEnabled)
  790. setSoundEnabled(soundEnabled) // Add this line to update the sound utility
  791. await this.postStateToWebview()
  792. break
  793. case "soundVolume":
  794. const soundVolume = message.value ?? 0.5
  795. await this.updateGlobalState("soundVolume", soundVolume)
  796. setSoundVolume(soundVolume)
  797. await this.postStateToWebview()
  798. break
  799. case "diffEnabled":
  800. const diffEnabled = message.bool ?? true
  801. await this.updateGlobalState("diffEnabled", diffEnabled)
  802. await this.postStateToWebview()
  803. break
  804. case "browserViewportSize":
  805. const browserViewportSize = message.text ?? "900x600"
  806. await this.updateGlobalState("browserViewportSize", browserViewportSize)
  807. await this.postStateToWebview()
  808. break
  809. case "fuzzyMatchThreshold":
  810. await this.updateGlobalState("fuzzyMatchThreshold", message.value)
  811. await this.postStateToWebview()
  812. break
  813. case "alwaysApproveResubmit":
  814. await this.updateGlobalState("alwaysApproveResubmit", message.bool ?? false)
  815. await this.postStateToWebview()
  816. break
  817. case "requestDelaySeconds":
  818. await this.updateGlobalState("requestDelaySeconds", message.value ?? 5)
  819. await this.postStateToWebview()
  820. break
  821. case "preferredLanguage":
  822. await this.updateGlobalState("preferredLanguage", message.text)
  823. await this.postStateToWebview()
  824. break
  825. case "writeDelayMs":
  826. await this.updateGlobalState("writeDelayMs", message.value)
  827. await this.postStateToWebview()
  828. break
  829. case "terminalOutputLineLimit":
  830. await this.updateGlobalState("terminalOutputLineLimit", message.value)
  831. await this.postStateToWebview()
  832. break
  833. case "mode":
  834. await this.handleModeSwitch(message.text as Mode)
  835. break
  836. case "updateSupportPrompt":
  837. try {
  838. if (Object.keys(message?.values ?? {}).length === 0) {
  839. return
  840. }
  841. const existingPrompts = (await this.getGlobalState("customSupportPrompts")) || {}
  842. const updatedPrompts = {
  843. ...existingPrompts,
  844. ...message.values,
  845. }
  846. await this.updateGlobalState("customSupportPrompts", updatedPrompts)
  847. await this.postStateToWebview()
  848. } catch (error) {
  849. this.outputChannel.appendLine(
  850. `Error update support prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  851. )
  852. vscode.window.showErrorMessage("Failed to update support prompt")
  853. }
  854. break
  855. case "resetSupportPrompt":
  856. try {
  857. if (!message?.text) {
  858. return
  859. }
  860. const existingPrompts = ((await this.getGlobalState("customSupportPrompts")) ||
  861. {}) as Record<string, any>
  862. const updatedPrompts = {
  863. ...existingPrompts,
  864. }
  865. updatedPrompts[message.text] = undefined
  866. await this.updateGlobalState("customSupportPrompts", updatedPrompts)
  867. await this.postStateToWebview()
  868. } catch (error) {
  869. this.outputChannel.appendLine(
  870. `Error reset support prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  871. )
  872. vscode.window.showErrorMessage("Failed to reset support prompt")
  873. }
  874. break
  875. case "updatePrompt":
  876. if (message.promptMode && message.customPrompt !== undefined) {
  877. const existingPrompts = (await this.getGlobalState("customModePrompts")) || {}
  878. const updatedPrompts = {
  879. ...existingPrompts,
  880. [message.promptMode]: message.customPrompt,
  881. }
  882. await this.updateGlobalState("customModePrompts", updatedPrompts)
  883. // Get current state and explicitly include customModePrompts
  884. const currentState = await this.getState()
  885. const stateWithPrompts = {
  886. ...currentState,
  887. customModePrompts: updatedPrompts,
  888. }
  889. // Post state with prompts
  890. this.view?.webview.postMessage({
  891. type: "state",
  892. state: stateWithPrompts,
  893. })
  894. }
  895. break
  896. case "deleteMessage": {
  897. const answer = await vscode.window.showInformationMessage(
  898. "What would you like to delete?",
  899. { modal: true },
  900. "Just this message",
  901. "This and all subsequent messages",
  902. )
  903. if (
  904. (answer === "Just this message" || answer === "This and all subsequent messages") &&
  905. this.cline &&
  906. typeof message.value === "number" &&
  907. message.value
  908. ) {
  909. const timeCutoff = message.value - 1000 // 1 second buffer before the message to delete
  910. const messageIndex = this.cline.clineMessages.findIndex(
  911. (msg) => msg.ts && msg.ts >= timeCutoff,
  912. )
  913. const apiConversationHistoryIndex = this.cline.apiConversationHistory.findIndex(
  914. (msg) => msg.ts && msg.ts >= timeCutoff,
  915. )
  916. if (messageIndex !== -1) {
  917. const { historyItem } = await this.getTaskWithId(this.cline.taskId)
  918. if (answer === "Just this message") {
  919. // Find the next user message first
  920. const nextUserMessage = this.cline.clineMessages
  921. .slice(messageIndex + 1)
  922. .find((msg) => msg.type === "say" && msg.say === "user_feedback")
  923. // Handle UI messages
  924. if (nextUserMessage) {
  925. // Find absolute index of next user message
  926. const nextUserMessageIndex = this.cline.clineMessages.findIndex(
  927. (msg) => msg === nextUserMessage,
  928. )
  929. // Keep messages before current message and after next user message
  930. await this.cline.overwriteClineMessages([
  931. ...this.cline.clineMessages.slice(0, messageIndex),
  932. ...this.cline.clineMessages.slice(nextUserMessageIndex),
  933. ])
  934. } else {
  935. // If no next user message, keep only messages before current message
  936. await this.cline.overwriteClineMessages(
  937. this.cline.clineMessages.slice(0, messageIndex),
  938. )
  939. }
  940. // Handle API messages
  941. if (apiConversationHistoryIndex !== -1) {
  942. if (nextUserMessage && nextUserMessage.ts) {
  943. // Keep messages before current API message and after next user message
  944. await this.cline.overwriteApiConversationHistory([
  945. ...this.cline.apiConversationHistory.slice(
  946. 0,
  947. apiConversationHistoryIndex,
  948. ),
  949. ...this.cline.apiConversationHistory.filter(
  950. (msg) => msg.ts && msg.ts >= nextUserMessage.ts,
  951. ),
  952. ])
  953. } else {
  954. // If no next user message, keep only messages before current API message
  955. await this.cline.overwriteApiConversationHistory(
  956. this.cline.apiConversationHistory.slice(0, apiConversationHistoryIndex),
  957. )
  958. }
  959. }
  960. } else if (answer === "This and all subsequent messages") {
  961. // Delete this message and all that follow
  962. await this.cline.overwriteClineMessages(
  963. this.cline.clineMessages.slice(0, messageIndex),
  964. )
  965. if (apiConversationHistoryIndex !== -1) {
  966. await this.cline.overwriteApiConversationHistory(
  967. this.cline.apiConversationHistory.slice(0, apiConversationHistoryIndex),
  968. )
  969. }
  970. }
  971. await this.initClineWithHistoryItem(historyItem)
  972. }
  973. }
  974. break
  975. }
  976. case "screenshotQuality":
  977. await this.updateGlobalState("screenshotQuality", message.value)
  978. await this.postStateToWebview()
  979. break
  980. case "enhancementApiConfigId":
  981. await this.updateGlobalState("enhancementApiConfigId", message.text)
  982. await this.postStateToWebview()
  983. break
  984. case "autoApprovalEnabled":
  985. await this.updateGlobalState("autoApprovalEnabled", message.bool ?? false)
  986. await this.postStateToWebview()
  987. break
  988. case "enhancePrompt":
  989. if (message.text) {
  990. try {
  991. const {
  992. apiConfiguration,
  993. customSupportPrompts,
  994. listApiConfigMeta,
  995. enhancementApiConfigId,
  996. } = await this.getState()
  997. // Try to get enhancement config first, fall back to current config
  998. let configToUse: ApiConfiguration = apiConfiguration
  999. if (enhancementApiConfigId) {
  1000. const config = listApiConfigMeta?.find((c) => c.id === enhancementApiConfigId)
  1001. if (config?.name) {
  1002. const loadedConfig = await this.configManager.loadConfig(config.name)
  1003. if (loadedConfig.apiProvider) {
  1004. configToUse = loadedConfig
  1005. }
  1006. }
  1007. }
  1008. const enhancedPrompt = await singleCompletionHandler(
  1009. configToUse,
  1010. supportPrompt.create(
  1011. "ENHANCE",
  1012. {
  1013. userInput: message.text,
  1014. },
  1015. customSupportPrompts,
  1016. ),
  1017. )
  1018. await this.postMessageToWebview({
  1019. type: "enhancedPrompt",
  1020. text: enhancedPrompt,
  1021. })
  1022. } catch (error) {
  1023. this.outputChannel.appendLine(
  1024. `Error enhancing prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1025. )
  1026. vscode.window.showErrorMessage("Failed to enhance prompt")
  1027. await this.postMessageToWebview({
  1028. type: "enhancedPrompt",
  1029. })
  1030. }
  1031. }
  1032. break
  1033. case "getSystemPrompt":
  1034. try {
  1035. const {
  1036. apiConfiguration,
  1037. customModePrompts,
  1038. customInstructions,
  1039. preferredLanguage,
  1040. browserViewportSize,
  1041. diffEnabled,
  1042. mcpEnabled,
  1043. fuzzyMatchThreshold,
  1044. experiments,
  1045. } = await this.getState()
  1046. // Create diffStrategy based on current model and settings
  1047. const diffStrategy = getDiffStrategy(
  1048. apiConfiguration.apiModelId || apiConfiguration.openRouterModelId || "",
  1049. fuzzyMatchThreshold,
  1050. Experiments.isEnabled(experiments, EXPERIMENT_IDS.DIFF_STRATEGY),
  1051. )
  1052. const cwd =
  1053. vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) || ""
  1054. const mode = message.mode ?? defaultModeSlug
  1055. const customModes = await this.customModesManager.getCustomModes()
  1056. const systemPrompt = await SYSTEM_PROMPT(
  1057. this.context,
  1058. cwd,
  1059. apiConfiguration.openRouterModelInfo?.supportsComputerUse ?? false,
  1060. mcpEnabled ? this.mcpHub : undefined,
  1061. diffStrategy,
  1062. browserViewportSize ?? "900x600",
  1063. mode,
  1064. customModePrompts,
  1065. customModes,
  1066. customInstructions,
  1067. preferredLanguage,
  1068. diffEnabled,
  1069. experiments,
  1070. )
  1071. await this.postMessageToWebview({
  1072. type: "systemPrompt",
  1073. text: systemPrompt,
  1074. mode: message.mode,
  1075. })
  1076. } catch (error) {
  1077. this.outputChannel.appendLine(
  1078. `Error getting system prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1079. )
  1080. vscode.window.showErrorMessage("Failed to get system prompt")
  1081. }
  1082. break
  1083. case "searchCommits": {
  1084. const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0)
  1085. if (cwd) {
  1086. try {
  1087. const commits = await searchCommits(message.query || "", cwd)
  1088. await this.postMessageToWebview({
  1089. type: "commitSearchResults",
  1090. commits,
  1091. })
  1092. } catch (error) {
  1093. this.outputChannel.appendLine(
  1094. `Error searching commits: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1095. )
  1096. vscode.window.showErrorMessage("Failed to search commits")
  1097. }
  1098. }
  1099. break
  1100. }
  1101. case "upsertApiConfiguration":
  1102. if (message.text && message.apiConfiguration) {
  1103. try {
  1104. await this.configManager.saveConfig(message.text, message.apiConfiguration)
  1105. const listApiConfig = await this.configManager.listConfig()
  1106. await Promise.all([
  1107. this.updateGlobalState("listApiConfigMeta", listApiConfig),
  1108. this.updateApiConfiguration(message.apiConfiguration),
  1109. this.updateGlobalState("currentApiConfigName", message.text),
  1110. ])
  1111. await this.postStateToWebview()
  1112. } catch (error) {
  1113. this.outputChannel.appendLine(
  1114. `Error create new api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1115. )
  1116. vscode.window.showErrorMessage("Failed to create api configuration")
  1117. }
  1118. }
  1119. break
  1120. case "renameApiConfiguration":
  1121. if (message.values && message.apiConfiguration) {
  1122. try {
  1123. const { oldName, newName } = message.values
  1124. await this.configManager.saveConfig(newName, message.apiConfiguration)
  1125. await this.configManager.deleteConfig(oldName)
  1126. const listApiConfig = await this.configManager.listConfig()
  1127. const config = listApiConfig?.find((c) => c.name === newName)
  1128. // Update listApiConfigMeta first to ensure UI has latest data
  1129. await this.updateGlobalState("listApiConfigMeta", listApiConfig)
  1130. await Promise.all([this.updateGlobalState("currentApiConfigName", newName)])
  1131. await this.postStateToWebview()
  1132. } catch (error) {
  1133. this.outputChannel.appendLine(
  1134. `Error create new api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1135. )
  1136. vscode.window.showErrorMessage("Failed to create api configuration")
  1137. }
  1138. }
  1139. break
  1140. case "loadApiConfiguration":
  1141. if (message.text) {
  1142. try {
  1143. const apiConfig = await this.configManager.loadConfig(message.text)
  1144. const listApiConfig = await this.configManager.listConfig()
  1145. await Promise.all([
  1146. this.updateGlobalState("listApiConfigMeta", listApiConfig),
  1147. this.updateGlobalState("currentApiConfigName", message.text),
  1148. this.updateApiConfiguration(apiConfig),
  1149. ])
  1150. await this.postStateToWebview()
  1151. } catch (error) {
  1152. this.outputChannel.appendLine(
  1153. `Error load api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1154. )
  1155. vscode.window.showErrorMessage("Failed to load api configuration")
  1156. }
  1157. }
  1158. break
  1159. case "deleteApiConfiguration":
  1160. if (message.text) {
  1161. const answer = await vscode.window.showInformationMessage(
  1162. "Are you sure you want to delete this configuration profile?",
  1163. { modal: true },
  1164. "Yes",
  1165. )
  1166. if (answer !== "Yes") {
  1167. break
  1168. }
  1169. try {
  1170. await this.configManager.deleteConfig(message.text)
  1171. const listApiConfig = await this.configManager.listConfig()
  1172. // Update listApiConfigMeta first to ensure UI has latest data
  1173. await this.updateGlobalState("listApiConfigMeta", listApiConfig)
  1174. // If this was the current config, switch to first available
  1175. const currentApiConfigName = await this.getGlobalState("currentApiConfigName")
  1176. if (message.text === currentApiConfigName && listApiConfig?.[0]?.name) {
  1177. const apiConfig = await this.configManager.loadConfig(listApiConfig[0].name)
  1178. await Promise.all([
  1179. this.updateGlobalState("currentApiConfigName", listApiConfig[0].name),
  1180. this.updateApiConfiguration(apiConfig),
  1181. ])
  1182. }
  1183. await this.postStateToWebview()
  1184. } catch (error) {
  1185. this.outputChannel.appendLine(
  1186. `Error delete api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1187. )
  1188. vscode.window.showErrorMessage("Failed to delete api configuration")
  1189. }
  1190. }
  1191. break
  1192. case "getListApiConfiguration":
  1193. try {
  1194. const listApiConfig = await this.configManager.listConfig()
  1195. await this.updateGlobalState("listApiConfigMeta", listApiConfig)
  1196. this.postMessageToWebview({ type: "listApiConfig", listApiConfig })
  1197. } catch (error) {
  1198. this.outputChannel.appendLine(
  1199. `Error get list api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1200. )
  1201. vscode.window.showErrorMessage("Failed to get list api configuration")
  1202. }
  1203. break
  1204. case "updateExperimental": {
  1205. if (!message.values) {
  1206. break
  1207. }
  1208. const updatedExperiments = {
  1209. ...((await this.getGlobalState("experiments")) ?? experimentDefault),
  1210. ...message.values,
  1211. } as Record<ExperimentId, boolean>
  1212. await this.updateGlobalState("experiments", updatedExperiments)
  1213. // Update diffStrategy in current Cline instance if it exists
  1214. if (message.values[EXPERIMENT_IDS.DIFF_STRATEGY] !== undefined && this.cline) {
  1215. await this.cline.updateDiffStrategy(
  1216. Experiments.isEnabled(updatedExperiments, EXPERIMENT_IDS.DIFF_STRATEGY),
  1217. )
  1218. }
  1219. await this.postStateToWebview()
  1220. break
  1221. }
  1222. case "updateMcpTimeout":
  1223. if (message.serverName && typeof message.timeout === "number") {
  1224. try {
  1225. await this.mcpHub?.updateServerTimeout(message.serverName, message.timeout)
  1226. } catch (error) {
  1227. this.outputChannel.appendLine(
  1228. `Failed to update timeout for ${message.serverName}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1229. )
  1230. vscode.window.showErrorMessage("Failed to update server timeout")
  1231. }
  1232. }
  1233. break
  1234. case "updateCustomMode":
  1235. if (message.modeConfig) {
  1236. await this.customModesManager.updateCustomMode(message.modeConfig.slug, message.modeConfig)
  1237. // Update state after saving the mode
  1238. const customModes = await this.customModesManager.getCustomModes()
  1239. await this.updateGlobalState("customModes", customModes)
  1240. await this.updateGlobalState("mode", message.modeConfig.slug)
  1241. await this.postStateToWebview()
  1242. }
  1243. break
  1244. case "deleteCustomMode":
  1245. if (message.slug) {
  1246. const answer = await vscode.window.showInformationMessage(
  1247. "Are you sure you want to delete this custom mode?",
  1248. { modal: true },
  1249. "Yes",
  1250. )
  1251. if (answer !== "Yes") {
  1252. break
  1253. }
  1254. await this.customModesManager.deleteCustomMode(message.slug)
  1255. // Switch back to default mode after deletion
  1256. await this.updateGlobalState("mode", defaultModeSlug)
  1257. await this.postStateToWebview()
  1258. }
  1259. }
  1260. },
  1261. null,
  1262. this.disposables,
  1263. )
  1264. }
  1265. /**
  1266. * Handle switching to a new mode, including updating the associated API configuration
  1267. * @param newMode The mode to switch to
  1268. */
  1269. public async handleModeSwitch(newMode: Mode) {
  1270. await this.updateGlobalState("mode", newMode)
  1271. // Load the saved API config for the new mode if it exists
  1272. const savedConfigId = await this.configManager.getModeConfigId(newMode)
  1273. const listApiConfig = await this.configManager.listConfig()
  1274. // Update listApiConfigMeta first to ensure UI has latest data
  1275. await this.updateGlobalState("listApiConfigMeta", listApiConfig)
  1276. // If this mode has a saved config, use it
  1277. if (savedConfigId) {
  1278. const config = listApiConfig?.find((c) => c.id === savedConfigId)
  1279. if (config?.name) {
  1280. const apiConfig = await this.configManager.loadConfig(config.name)
  1281. await Promise.all([
  1282. this.updateGlobalState("currentApiConfigName", config.name),
  1283. this.updateApiConfiguration(apiConfig),
  1284. ])
  1285. }
  1286. } else {
  1287. // If no saved config for this mode, save current config as default
  1288. const currentApiConfigName = await this.getGlobalState("currentApiConfigName")
  1289. if (currentApiConfigName) {
  1290. const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
  1291. if (config?.id) {
  1292. await this.configManager.setModeConfig(newMode, config.id)
  1293. }
  1294. }
  1295. }
  1296. await this.postStateToWebview()
  1297. }
  1298. private async updateApiConfiguration(apiConfiguration: ApiConfiguration) {
  1299. // Update mode's default config
  1300. const { mode } = await this.getState()
  1301. if (mode) {
  1302. const currentApiConfigName = await this.getGlobalState("currentApiConfigName")
  1303. const listApiConfig = await this.configManager.listConfig()
  1304. const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
  1305. if (config?.id) {
  1306. await this.configManager.setModeConfig(mode, config.id)
  1307. }
  1308. }
  1309. const {
  1310. apiProvider,
  1311. apiModelId,
  1312. apiKey,
  1313. glamaModelId,
  1314. glamaModelInfo,
  1315. glamaApiKey,
  1316. openRouterApiKey,
  1317. awsAccessKey,
  1318. awsSecretKey,
  1319. awsSessionToken,
  1320. awsRegion,
  1321. awsUseCrossRegionInference,
  1322. awsProfile,
  1323. awsUseProfile,
  1324. vertexProjectId,
  1325. vertexRegion,
  1326. openAiBaseUrl,
  1327. openAiApiKey,
  1328. openAiModelId,
  1329. openAiCustomModelInfo,
  1330. openAiUseAzure,
  1331. ollamaModelId,
  1332. ollamaBaseUrl,
  1333. lmStudioModelId,
  1334. lmStudioBaseUrl,
  1335. anthropicBaseUrl,
  1336. geminiApiKey,
  1337. openAiNativeApiKey,
  1338. deepSeekApiKey,
  1339. azureApiVersion,
  1340. openAiStreamingEnabled,
  1341. openRouterModelId,
  1342. openRouterBaseUrl,
  1343. openRouterModelInfo,
  1344. openRouterUseMiddleOutTransform,
  1345. vsCodeLmModelSelector,
  1346. mistralApiKey,
  1347. unboundApiKey,
  1348. unboundModelId,
  1349. } = apiConfiguration
  1350. await this.updateGlobalState("apiProvider", apiProvider)
  1351. await this.updateGlobalState("apiModelId", apiModelId)
  1352. await this.storeSecret("apiKey", apiKey)
  1353. await this.updateGlobalState("glamaModelId", glamaModelId)
  1354. await this.updateGlobalState("glamaModelInfo", glamaModelInfo)
  1355. await this.storeSecret("glamaApiKey", glamaApiKey)
  1356. await this.storeSecret("openRouterApiKey", openRouterApiKey)
  1357. await this.storeSecret("awsAccessKey", awsAccessKey)
  1358. await this.storeSecret("awsSecretKey", awsSecretKey)
  1359. await this.storeSecret("awsSessionToken", awsSessionToken)
  1360. await this.updateGlobalState("awsRegion", awsRegion)
  1361. await this.updateGlobalState("awsUseCrossRegionInference", awsUseCrossRegionInference)
  1362. await this.updateGlobalState("awsProfile", awsProfile)
  1363. await this.updateGlobalState("awsUseProfile", awsUseProfile)
  1364. await this.updateGlobalState("vertexProjectId", vertexProjectId)
  1365. await this.updateGlobalState("vertexRegion", vertexRegion)
  1366. await this.updateGlobalState("openAiBaseUrl", openAiBaseUrl)
  1367. await this.storeSecret("openAiApiKey", openAiApiKey)
  1368. await this.updateGlobalState("openAiModelId", openAiModelId)
  1369. await this.updateGlobalState("openAiCustomModelInfo", openAiCustomModelInfo)
  1370. await this.updateGlobalState("openAiUseAzure", openAiUseAzure)
  1371. await this.updateGlobalState("ollamaModelId", ollamaModelId)
  1372. await this.updateGlobalState("ollamaBaseUrl", ollamaBaseUrl)
  1373. await this.updateGlobalState("lmStudioModelId", lmStudioModelId)
  1374. await this.updateGlobalState("lmStudioBaseUrl", lmStudioBaseUrl)
  1375. await this.updateGlobalState("anthropicBaseUrl", anthropicBaseUrl)
  1376. await this.storeSecret("geminiApiKey", geminiApiKey)
  1377. await this.storeSecret("openAiNativeApiKey", openAiNativeApiKey)
  1378. await this.storeSecret("deepSeekApiKey", deepSeekApiKey)
  1379. await this.updateGlobalState("azureApiVersion", azureApiVersion)
  1380. await this.updateGlobalState("openAiStreamingEnabled", openAiStreamingEnabled)
  1381. await this.updateGlobalState("openRouterModelId", openRouterModelId)
  1382. await this.updateGlobalState("openRouterModelInfo", openRouterModelInfo)
  1383. await this.updateGlobalState("openRouterBaseUrl", openRouterBaseUrl)
  1384. await this.updateGlobalState("openRouterUseMiddleOutTransform", openRouterUseMiddleOutTransform)
  1385. await this.updateGlobalState("vsCodeLmModelSelector", vsCodeLmModelSelector)
  1386. await this.storeSecret("mistralApiKey", mistralApiKey)
  1387. await this.storeSecret("unboundApiKey", unboundApiKey)
  1388. await this.updateGlobalState("unboundModelId", unboundModelId)
  1389. if (this.cline) {
  1390. this.cline.api = buildApiHandler(apiConfiguration)
  1391. }
  1392. }
  1393. async updateCustomInstructions(instructions?: string) {
  1394. // User may be clearing the field
  1395. await this.updateGlobalState("customInstructions", instructions || undefined)
  1396. if (this.cline) {
  1397. this.cline.customInstructions = instructions || undefined
  1398. }
  1399. await this.postStateToWebview()
  1400. }
  1401. // MCP
  1402. async ensureMcpServersDirectoryExists(): Promise<string> {
  1403. const mcpServersDir = path.join(os.homedir(), "Documents", "Cline", "MCP")
  1404. try {
  1405. await fs.mkdir(mcpServersDir, { recursive: true })
  1406. } catch (error) {
  1407. return "~/Documents/Cline/MCP" // in case creating a directory in documents fails for whatever reason (e.g. permissions) - this is fine since this path is only ever used in the system prompt
  1408. }
  1409. return mcpServersDir
  1410. }
  1411. async ensureSettingsDirectoryExists(): Promise<string> {
  1412. const settingsDir = path.join(this.context.globalStorageUri.fsPath, "settings")
  1413. await fs.mkdir(settingsDir, { recursive: true })
  1414. return settingsDir
  1415. }
  1416. // Ollama
  1417. async getOllamaModels(baseUrl?: string) {
  1418. try {
  1419. if (!baseUrl) {
  1420. baseUrl = "http://localhost:11434"
  1421. }
  1422. if (!URL.canParse(baseUrl)) {
  1423. return []
  1424. }
  1425. const response = await axios.get(`${baseUrl}/api/tags`)
  1426. const modelsArray = response.data?.models?.map((model: any) => model.name) || []
  1427. const models = [...new Set<string>(modelsArray)]
  1428. return models
  1429. } catch (error) {
  1430. return []
  1431. }
  1432. }
  1433. // LM Studio
  1434. async getLmStudioModels(baseUrl?: string) {
  1435. try {
  1436. if (!baseUrl) {
  1437. baseUrl = "http://localhost:1234"
  1438. }
  1439. if (!URL.canParse(baseUrl)) {
  1440. return []
  1441. }
  1442. const response = await axios.get(`${baseUrl}/v1/models`)
  1443. const modelsArray = response.data?.data?.map((model: any) => model.id) || []
  1444. const models = [...new Set<string>(modelsArray)]
  1445. return models
  1446. } catch (error) {
  1447. return []
  1448. }
  1449. }
  1450. // VSCode LM API
  1451. private async getVsCodeLmModels() {
  1452. try {
  1453. const models = await vscode.lm.selectChatModels({})
  1454. return models || []
  1455. } catch (error) {
  1456. this.outputChannel.appendLine(
  1457. `Error fetching VS Code LM models: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1458. )
  1459. return []
  1460. }
  1461. }
  1462. // OpenAi
  1463. async getOpenAiModels(baseUrl?: string, apiKey?: string) {
  1464. try {
  1465. if (!baseUrl) {
  1466. return []
  1467. }
  1468. if (!URL.canParse(baseUrl)) {
  1469. return []
  1470. }
  1471. const config: Record<string, any> = {}
  1472. if (apiKey) {
  1473. config["headers"] = { Authorization: `Bearer ${apiKey}` }
  1474. }
  1475. const response = await axios.get(`${baseUrl}/models`, config)
  1476. const modelsArray = response.data?.data?.map((model: any) => model.id) || []
  1477. const models = [...new Set<string>(modelsArray)]
  1478. return models
  1479. } catch (error) {
  1480. return []
  1481. }
  1482. }
  1483. // OpenRouter
  1484. async handleOpenRouterCallback(code: string) {
  1485. let apiKey: string
  1486. try {
  1487. const response = await axios.post("https://openrouter.ai/api/v1/auth/keys", { code })
  1488. if (response.data && response.data.key) {
  1489. apiKey = response.data.key
  1490. } else {
  1491. throw new Error("Invalid response from OpenRouter API")
  1492. }
  1493. } catch (error) {
  1494. this.outputChannel.appendLine(
  1495. `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1496. )
  1497. throw error
  1498. }
  1499. const openrouter: ApiProvider = "openrouter"
  1500. await this.updateGlobalState("apiProvider", openrouter)
  1501. await this.storeSecret("openRouterApiKey", apiKey)
  1502. await this.postStateToWebview()
  1503. if (this.cline) {
  1504. this.cline.api = buildApiHandler({ apiProvider: openrouter, openRouterApiKey: apiKey })
  1505. }
  1506. // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome
  1507. }
  1508. private async ensureCacheDirectoryExists(): Promise<string> {
  1509. const cacheDir = path.join(this.context.globalStorageUri.fsPath, "cache")
  1510. await fs.mkdir(cacheDir, { recursive: true })
  1511. return cacheDir
  1512. }
  1513. async handleGlamaCallback(code: string) {
  1514. let apiKey: string
  1515. try {
  1516. const response = await axios.post("https://glama.ai/api/gateway/v1/auth/exchange-code", { code })
  1517. if (response.data && response.data.apiKey) {
  1518. apiKey = response.data.apiKey
  1519. } else {
  1520. throw new Error("Invalid response from Glama API")
  1521. }
  1522. } catch (error) {
  1523. this.outputChannel.appendLine(
  1524. `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1525. )
  1526. throw error
  1527. }
  1528. const glama: ApiProvider = "glama"
  1529. await this.updateGlobalState("apiProvider", glama)
  1530. await this.storeSecret("glamaApiKey", apiKey)
  1531. await this.postStateToWebview()
  1532. if (this.cline) {
  1533. this.cline.api = buildApiHandler({
  1534. apiProvider: glama,
  1535. glamaApiKey: apiKey,
  1536. })
  1537. }
  1538. // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome
  1539. }
  1540. async readGlamaModels(): Promise<Record<string, ModelInfo> | undefined> {
  1541. const glamaModelsFilePath = path.join(await this.ensureCacheDirectoryExists(), GlobalFileNames.glamaModels)
  1542. const fileExists = await fileExistsAtPath(glamaModelsFilePath)
  1543. if (fileExists) {
  1544. const fileContents = await fs.readFile(glamaModelsFilePath, "utf8")
  1545. return JSON.parse(fileContents)
  1546. }
  1547. return undefined
  1548. }
  1549. async refreshGlamaModels() {
  1550. const glamaModelsFilePath = path.join(await this.ensureCacheDirectoryExists(), GlobalFileNames.glamaModels)
  1551. const models: Record<string, ModelInfo> = {}
  1552. try {
  1553. const response = await axios.get("https://glama.ai/api/gateway/v1/models")
  1554. /*
  1555. {
  1556. "added": "2024-12-24T15:12:49.324Z",
  1557. "capabilities": [
  1558. "adjustable_safety_settings",
  1559. "caching",
  1560. "code_execution",
  1561. "function_calling",
  1562. "json_mode",
  1563. "json_schema",
  1564. "system_instructions",
  1565. "tuning",
  1566. "input:audio",
  1567. "input:image",
  1568. "input:text",
  1569. "input:video",
  1570. "output:text"
  1571. ],
  1572. "id": "google-vertex/gemini-1.5-flash-002",
  1573. "maxTokensInput": 1048576,
  1574. "maxTokensOutput": 8192,
  1575. "pricePerToken": {
  1576. "cacheRead": null,
  1577. "cacheWrite": null,
  1578. "input": "0.000000075",
  1579. "output": "0.0000003"
  1580. }
  1581. }
  1582. */
  1583. if (response.data) {
  1584. const rawModels = response.data
  1585. const parsePrice = (price: any) => {
  1586. if (price) {
  1587. return parseFloat(price) * 1_000_000
  1588. }
  1589. return undefined
  1590. }
  1591. for (const rawModel of rawModels) {
  1592. const modelInfo: ModelInfo = {
  1593. maxTokens: rawModel.maxTokensOutput,
  1594. contextWindow: rawModel.maxTokensInput,
  1595. supportsImages: rawModel.capabilities?.includes("input:image"),
  1596. supportsComputerUse: rawModel.capabilities?.includes("computer_use"),
  1597. supportsPromptCache: rawModel.capabilities?.includes("caching"),
  1598. inputPrice: parsePrice(rawModel.pricePerToken?.input),
  1599. outputPrice: parsePrice(rawModel.pricePerToken?.output),
  1600. description: undefined,
  1601. cacheWritesPrice: parsePrice(rawModel.pricePerToken?.cacheWrite),
  1602. cacheReadsPrice: parsePrice(rawModel.pricePerToken?.cacheRead),
  1603. }
  1604. models[rawModel.id] = modelInfo
  1605. }
  1606. } else {
  1607. this.outputChannel.appendLine("Invalid response from Glama API")
  1608. }
  1609. await fs.writeFile(glamaModelsFilePath, JSON.stringify(models))
  1610. this.outputChannel.appendLine(`Glama models fetched and saved: ${JSON.stringify(models, null, 2)}`)
  1611. } catch (error) {
  1612. this.outputChannel.appendLine(
  1613. `Error fetching Glama models: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1614. )
  1615. }
  1616. await this.postMessageToWebview({ type: "glamaModels", glamaModels: models })
  1617. return models
  1618. }
  1619. async readOpenRouterModels(): Promise<Record<string, ModelInfo> | undefined> {
  1620. const openRouterModelsFilePath = path.join(
  1621. await this.ensureCacheDirectoryExists(),
  1622. GlobalFileNames.openRouterModels,
  1623. )
  1624. const fileExists = await fileExistsAtPath(openRouterModelsFilePath)
  1625. if (fileExists) {
  1626. const fileContents = await fs.readFile(openRouterModelsFilePath, "utf8")
  1627. return JSON.parse(fileContents)
  1628. }
  1629. return undefined
  1630. }
  1631. async refreshOpenRouterModels() {
  1632. const openRouterModelsFilePath = path.join(
  1633. await this.ensureCacheDirectoryExists(),
  1634. GlobalFileNames.openRouterModels,
  1635. )
  1636. const models: Record<string, ModelInfo> = {}
  1637. try {
  1638. const response = await axios.get("https://openrouter.ai/api/v1/models")
  1639. /*
  1640. {
  1641. "id": "anthropic/claude-3.5-sonnet",
  1642. "name": "Anthropic: Claude 3.5 Sonnet",
  1643. "created": 1718841600,
  1644. "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",
  1645. "context_length": 200000,
  1646. "architecture": {
  1647. "modality": "text+image-\u003Etext",
  1648. "tokenizer": "Claude",
  1649. "instruct_type": null
  1650. },
  1651. "pricing": {
  1652. "prompt": "0.000003",
  1653. "completion": "0.000015",
  1654. "image": "0.0048",
  1655. "request": "0"
  1656. },
  1657. "top_provider": {
  1658. "context_length": 200000,
  1659. "max_completion_tokens": 8192,
  1660. "is_moderated": true
  1661. },
  1662. "per_request_limits": null
  1663. },
  1664. */
  1665. if (response.data?.data) {
  1666. const rawModels = response.data.data
  1667. const parsePrice = (price: any) => {
  1668. if (price) {
  1669. return parseFloat(price) * 1_000_000
  1670. }
  1671. return undefined
  1672. }
  1673. for (const rawModel of rawModels) {
  1674. const modelInfo: ModelInfo = {
  1675. maxTokens: rawModel.top_provider?.max_completion_tokens,
  1676. contextWindow: rawModel.context_length,
  1677. supportsImages: rawModel.architecture?.modality?.includes("image"),
  1678. supportsPromptCache: false,
  1679. inputPrice: parsePrice(rawModel.pricing?.prompt),
  1680. outputPrice: parsePrice(rawModel.pricing?.completion),
  1681. description: rawModel.description,
  1682. }
  1683. switch (rawModel.id) {
  1684. case "anthropic/claude-3.5-sonnet":
  1685. case "anthropic/claude-3.5-sonnet:beta":
  1686. // NOTE: this needs to be synced with api.ts/openrouter default model info
  1687. modelInfo.supportsComputerUse = true
  1688. modelInfo.supportsPromptCache = true
  1689. modelInfo.cacheWritesPrice = 3.75
  1690. modelInfo.cacheReadsPrice = 0.3
  1691. break
  1692. case "anthropic/claude-3.5-sonnet-20240620":
  1693. case "anthropic/claude-3.5-sonnet-20240620:beta":
  1694. modelInfo.supportsPromptCache = true
  1695. modelInfo.cacheWritesPrice = 3.75
  1696. modelInfo.cacheReadsPrice = 0.3
  1697. break
  1698. case "anthropic/claude-3-5-haiku":
  1699. case "anthropic/claude-3-5-haiku:beta":
  1700. case "anthropic/claude-3-5-haiku-20241022":
  1701. case "anthropic/claude-3-5-haiku-20241022:beta":
  1702. case "anthropic/claude-3.5-haiku":
  1703. case "anthropic/claude-3.5-haiku:beta":
  1704. case "anthropic/claude-3.5-haiku-20241022":
  1705. case "anthropic/claude-3.5-haiku-20241022:beta":
  1706. modelInfo.supportsPromptCache = true
  1707. modelInfo.cacheWritesPrice = 1.25
  1708. modelInfo.cacheReadsPrice = 0.1
  1709. break
  1710. case "anthropic/claude-3-opus":
  1711. case "anthropic/claude-3-opus:beta":
  1712. modelInfo.supportsPromptCache = true
  1713. modelInfo.cacheWritesPrice = 18.75
  1714. modelInfo.cacheReadsPrice = 1.5
  1715. break
  1716. case "anthropic/claude-3-haiku":
  1717. case "anthropic/claude-3-haiku:beta":
  1718. modelInfo.supportsPromptCache = true
  1719. modelInfo.cacheWritesPrice = 0.3
  1720. modelInfo.cacheReadsPrice = 0.03
  1721. break
  1722. }
  1723. models[rawModel.id] = modelInfo
  1724. }
  1725. } else {
  1726. this.outputChannel.appendLine("Invalid response from OpenRouter API")
  1727. }
  1728. await fs.writeFile(openRouterModelsFilePath, JSON.stringify(models))
  1729. this.outputChannel.appendLine(`OpenRouter models fetched and saved: ${JSON.stringify(models, null, 2)}`)
  1730. } catch (error) {
  1731. this.outputChannel.appendLine(
  1732. `Error fetching OpenRouter models: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  1733. )
  1734. }
  1735. await this.postMessageToWebview({ type: "openRouterModels", openRouterModels: models })
  1736. return models
  1737. }
  1738. // Task history
  1739. async getTaskWithId(id: string): Promise<{
  1740. historyItem: HistoryItem
  1741. taskDirPath: string
  1742. apiConversationHistoryFilePath: string
  1743. uiMessagesFilePath: string
  1744. apiConversationHistory: Anthropic.MessageParam[]
  1745. }> {
  1746. const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || []
  1747. const historyItem = history.find((item) => item.id === id)
  1748. if (historyItem) {
  1749. const taskDirPath = path.join(this.context.globalStorageUri.fsPath, "tasks", id)
  1750. const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
  1751. const uiMessagesFilePath = path.join(taskDirPath, GlobalFileNames.uiMessages)
  1752. const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
  1753. if (fileExists) {
  1754. const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8"))
  1755. return {
  1756. historyItem,
  1757. taskDirPath,
  1758. apiConversationHistoryFilePath,
  1759. uiMessagesFilePath,
  1760. apiConversationHistory,
  1761. }
  1762. }
  1763. }
  1764. // if we tried to get a task that doesn't exist, remove it from state
  1765. // FIXME: this seems to happen sometimes when the json file doesnt save to disk for some reason
  1766. await this.deleteTaskFromState(id)
  1767. throw new Error("Task not found")
  1768. }
  1769. async showTaskWithId(id: string) {
  1770. if (id !== this.cline?.taskId) {
  1771. // non-current task
  1772. const { historyItem } = await this.getTaskWithId(id)
  1773. await this.initClineWithHistoryItem(historyItem) // clears existing task
  1774. }
  1775. await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
  1776. }
  1777. async exportTaskWithId(id: string) {
  1778. const { historyItem, apiConversationHistory } = await this.getTaskWithId(id)
  1779. await downloadTask(historyItem.ts, apiConversationHistory)
  1780. }
  1781. async deleteTaskWithId(id: string) {
  1782. if (id === this.cline?.taskId) {
  1783. await this.clearTask()
  1784. }
  1785. const { taskDirPath, apiConversationHistoryFilePath, uiMessagesFilePath } = await this.getTaskWithId(id)
  1786. await this.deleteTaskFromState(id)
  1787. // Delete the task files
  1788. const apiConversationHistoryFileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
  1789. if (apiConversationHistoryFileExists) {
  1790. await fs.unlink(apiConversationHistoryFilePath)
  1791. }
  1792. const uiMessagesFileExists = await fileExistsAtPath(uiMessagesFilePath)
  1793. if (uiMessagesFileExists) {
  1794. await fs.unlink(uiMessagesFilePath)
  1795. }
  1796. const legacyMessagesFilePath = path.join(taskDirPath, "claude_messages.json")
  1797. if (await fileExistsAtPath(legacyMessagesFilePath)) {
  1798. await fs.unlink(legacyMessagesFilePath)
  1799. }
  1800. await fs.rmdir(taskDirPath) // succeeds if the dir is empty
  1801. }
  1802. async deleteTaskFromState(id: string) {
  1803. // Remove the task from history
  1804. const taskHistory = ((await this.getGlobalState("taskHistory")) as HistoryItem[]) || []
  1805. const updatedTaskHistory = taskHistory.filter((task) => task.id !== id)
  1806. await this.updateGlobalState("taskHistory", updatedTaskHistory)
  1807. // Notify the webview that the task has been deleted
  1808. await this.postStateToWebview()
  1809. }
  1810. async postStateToWebview() {
  1811. const state = await this.getStateToPostToWebview()
  1812. this.postMessageToWebview({ type: "state", state })
  1813. }
  1814. async getStateToPostToWebview() {
  1815. const {
  1816. apiConfiguration,
  1817. lastShownAnnouncementId,
  1818. customInstructions,
  1819. alwaysAllowReadOnly,
  1820. alwaysAllowWrite,
  1821. alwaysAllowExecute,
  1822. alwaysAllowBrowser,
  1823. alwaysAllowMcp,
  1824. alwaysAllowModeSwitch,
  1825. soundEnabled,
  1826. diffEnabled,
  1827. taskHistory,
  1828. soundVolume,
  1829. browserViewportSize,
  1830. screenshotQuality,
  1831. preferredLanguage,
  1832. writeDelayMs,
  1833. terminalOutputLineLimit,
  1834. fuzzyMatchThreshold,
  1835. mcpEnabled,
  1836. alwaysApproveResubmit,
  1837. requestDelaySeconds,
  1838. currentApiConfigName,
  1839. listApiConfigMeta,
  1840. mode,
  1841. customModePrompts,
  1842. customSupportPrompts,
  1843. enhancementApiConfigId,
  1844. autoApprovalEnabled,
  1845. experiments,
  1846. } = await this.getState()
  1847. const allowedCommands = vscode.workspace.getConfiguration("roo-cline").get<string[]>("allowedCommands") || []
  1848. return {
  1849. version: this.context.extension?.packageJSON?.version ?? "",
  1850. apiConfiguration,
  1851. customInstructions,
  1852. alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
  1853. alwaysAllowWrite: alwaysAllowWrite ?? false,
  1854. alwaysAllowExecute: alwaysAllowExecute ?? false,
  1855. alwaysAllowBrowser: alwaysAllowBrowser ?? false,
  1856. alwaysAllowMcp: alwaysAllowMcp ?? false,
  1857. alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false,
  1858. uriScheme: vscode.env.uriScheme,
  1859. clineMessages: this.cline?.clineMessages || [],
  1860. taskHistory: (taskHistory || [])
  1861. .filter((item: HistoryItem) => item.ts && item.task)
  1862. .sort((a: HistoryItem, b: HistoryItem) => b.ts - a.ts),
  1863. soundEnabled: soundEnabled ?? false,
  1864. diffEnabled: diffEnabled ?? true,
  1865. shouldShowAnnouncement: lastShownAnnouncementId !== this.latestAnnouncementId,
  1866. allowedCommands,
  1867. soundVolume: soundVolume ?? 0.5,
  1868. browserViewportSize: browserViewportSize ?? "900x600",
  1869. screenshotQuality: screenshotQuality ?? 75,
  1870. preferredLanguage: preferredLanguage ?? "English",
  1871. writeDelayMs: writeDelayMs ?? 1000,
  1872. terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
  1873. fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
  1874. mcpEnabled: mcpEnabled ?? true,
  1875. alwaysApproveResubmit: alwaysApproveResubmit ?? false,
  1876. requestDelaySeconds: requestDelaySeconds ?? 10,
  1877. currentApiConfigName: currentApiConfigName ?? "default",
  1878. listApiConfigMeta: listApiConfigMeta ?? [],
  1879. mode: mode ?? defaultModeSlug,
  1880. customModePrompts: customModePrompts ?? {},
  1881. customSupportPrompts: customSupportPrompts ?? {},
  1882. enhancementApiConfigId,
  1883. autoApprovalEnabled: autoApprovalEnabled ?? false,
  1884. customModes: await this.customModesManager.getCustomModes(),
  1885. experiments: experiments ?? experimentDefault,
  1886. }
  1887. }
  1888. async clearTask() {
  1889. this.cline?.abortTask()
  1890. this.cline = undefined // removes reference to it, so once promises end it will be garbage collected
  1891. }
  1892. // Caching mechanism to keep track of webview messages + API conversation history per provider instance
  1893. /*
  1894. 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.
  1895. - 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.
  1896. - 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.
  1897. 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.
  1898. // conversation history to send in API requests
  1899. /*
  1900. 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.
  1901. VSCode docs about state: "The value must be JSON-stringifyable ... value — A value. MUST not contain cyclic references."
  1902. 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.
  1903. */
  1904. // getApiConversationHistory(): Anthropic.MessageParam[] {
  1905. // // const history = (await this.getGlobalState(
  1906. // // this.getApiConversationHistoryStateKey()
  1907. // // )) as Anthropic.MessageParam[]
  1908. // // return history || []
  1909. // return this.apiConversationHistory
  1910. // }
  1911. // setApiConversationHistory(history: Anthropic.MessageParam[] | undefined) {
  1912. // // await this.updateGlobalState(this.getApiConversationHistoryStateKey(), history)
  1913. // this.apiConversationHistory = history || []
  1914. // }
  1915. // addMessageToApiConversationHistory(message: Anthropic.MessageParam): Anthropic.MessageParam[] {
  1916. // // const history = await this.getApiConversationHistory()
  1917. // // history.push(message)
  1918. // // await this.setApiConversationHistory(history)
  1919. // // return history
  1920. // this.apiConversationHistory.push(message)
  1921. // return this.apiConversationHistory
  1922. // }
  1923. /*
  1924. Storage
  1925. https://dev.to/kompotkot/how-to-use-secretstorage-in-your-vscode-extensions-2hco
  1926. https://www.eliostruyf.com/devhack-code-extension-storage-options/
  1927. */
  1928. async getState() {
  1929. const [
  1930. storedApiProvider,
  1931. apiModelId,
  1932. apiKey,
  1933. glamaApiKey,
  1934. glamaModelId,
  1935. glamaModelInfo,
  1936. openRouterApiKey,
  1937. awsAccessKey,
  1938. awsSecretKey,
  1939. awsSessionToken,
  1940. awsRegion,
  1941. awsUseCrossRegionInference,
  1942. awsProfile,
  1943. awsUseProfile,
  1944. vertexProjectId,
  1945. vertexRegion,
  1946. openAiBaseUrl,
  1947. openAiApiKey,
  1948. openAiModelId,
  1949. openAiCustomModelInfo,
  1950. openAiUseAzure,
  1951. ollamaModelId,
  1952. ollamaBaseUrl,
  1953. lmStudioModelId,
  1954. lmStudioBaseUrl,
  1955. anthropicBaseUrl,
  1956. geminiApiKey,
  1957. openAiNativeApiKey,
  1958. deepSeekApiKey,
  1959. mistralApiKey,
  1960. azureApiVersion,
  1961. openAiStreamingEnabled,
  1962. openRouterModelId,
  1963. openRouterModelInfo,
  1964. openRouterBaseUrl,
  1965. openRouterUseMiddleOutTransform,
  1966. lastShownAnnouncementId,
  1967. customInstructions,
  1968. alwaysAllowReadOnly,
  1969. alwaysAllowWrite,
  1970. alwaysAllowExecute,
  1971. alwaysAllowBrowser,
  1972. alwaysAllowMcp,
  1973. alwaysAllowModeSwitch,
  1974. taskHistory,
  1975. allowedCommands,
  1976. soundEnabled,
  1977. diffEnabled,
  1978. soundVolume,
  1979. browserViewportSize,
  1980. fuzzyMatchThreshold,
  1981. preferredLanguage,
  1982. writeDelayMs,
  1983. screenshotQuality,
  1984. terminalOutputLineLimit,
  1985. mcpEnabled,
  1986. alwaysApproveResubmit,
  1987. requestDelaySeconds,
  1988. currentApiConfigName,
  1989. listApiConfigMeta,
  1990. vsCodeLmModelSelector,
  1991. mode,
  1992. modeApiConfigs,
  1993. customModePrompts,
  1994. customSupportPrompts,
  1995. enhancementApiConfigId,
  1996. autoApprovalEnabled,
  1997. customModes,
  1998. experiments,
  1999. unboundApiKey,
  2000. unboundModelId,
  2001. ] = await Promise.all([
  2002. this.getGlobalState("apiProvider") as Promise<ApiProvider | undefined>,
  2003. this.getGlobalState("apiModelId") as Promise<string | undefined>,
  2004. this.getSecret("apiKey") as Promise<string | undefined>,
  2005. this.getSecret("glamaApiKey") as Promise<string | undefined>,
  2006. this.getGlobalState("glamaModelId") as Promise<string | undefined>,
  2007. this.getGlobalState("glamaModelInfo") as Promise<ModelInfo | undefined>,
  2008. this.getSecret("openRouterApiKey") as Promise<string | undefined>,
  2009. this.getSecret("awsAccessKey") as Promise<string | undefined>,
  2010. this.getSecret("awsSecretKey") as Promise<string | undefined>,
  2011. this.getSecret("awsSessionToken") as Promise<string | undefined>,
  2012. this.getGlobalState("awsRegion") as Promise<string | undefined>,
  2013. this.getGlobalState("awsUseCrossRegionInference") as Promise<boolean | undefined>,
  2014. this.getGlobalState("awsProfile") as Promise<string | undefined>,
  2015. this.getGlobalState("awsUseProfile") as Promise<boolean | undefined>,
  2016. this.getGlobalState("vertexProjectId") as Promise<string | undefined>,
  2017. this.getGlobalState("vertexRegion") as Promise<string | undefined>,
  2018. this.getGlobalState("openAiBaseUrl") as Promise<string | undefined>,
  2019. this.getSecret("openAiApiKey") as Promise<string | undefined>,
  2020. this.getGlobalState("openAiModelId") as Promise<string | undefined>,
  2021. this.getGlobalState("openAiCustomModelInfo") as Promise<ModelInfo | undefined>,
  2022. this.getGlobalState("openAiUseAzure") as Promise<boolean | undefined>,
  2023. this.getGlobalState("ollamaModelId") as Promise<string | undefined>,
  2024. this.getGlobalState("ollamaBaseUrl") as Promise<string | undefined>,
  2025. this.getGlobalState("lmStudioModelId") as Promise<string | undefined>,
  2026. this.getGlobalState("lmStudioBaseUrl") as Promise<string | undefined>,
  2027. this.getGlobalState("anthropicBaseUrl") as Promise<string | undefined>,
  2028. this.getSecret("geminiApiKey") as Promise<string | undefined>,
  2029. this.getSecret("openAiNativeApiKey") as Promise<string | undefined>,
  2030. this.getSecret("deepSeekApiKey") as Promise<string | undefined>,
  2031. this.getSecret("mistralApiKey") as Promise<string | undefined>,
  2032. this.getGlobalState("azureApiVersion") as Promise<string | undefined>,
  2033. this.getGlobalState("openAiStreamingEnabled") as Promise<boolean | undefined>,
  2034. this.getGlobalState("openRouterModelId") as Promise<string | undefined>,
  2035. this.getGlobalState("openRouterModelInfo") as Promise<ModelInfo | undefined>,
  2036. this.getGlobalState("openRouterBaseUrl") as Promise<string | undefined>,
  2037. this.getGlobalState("openRouterUseMiddleOutTransform") as Promise<boolean | undefined>,
  2038. this.getGlobalState("lastShownAnnouncementId") as Promise<string | undefined>,
  2039. this.getGlobalState("customInstructions") as Promise<string | undefined>,
  2040. this.getGlobalState("alwaysAllowReadOnly") as Promise<boolean | undefined>,
  2041. this.getGlobalState("alwaysAllowWrite") as Promise<boolean | undefined>,
  2042. this.getGlobalState("alwaysAllowExecute") as Promise<boolean | undefined>,
  2043. this.getGlobalState("alwaysAllowBrowser") as Promise<boolean | undefined>,
  2044. this.getGlobalState("alwaysAllowMcp") as Promise<boolean | undefined>,
  2045. this.getGlobalState("alwaysAllowModeSwitch") as Promise<boolean | undefined>,
  2046. this.getGlobalState("taskHistory") as Promise<HistoryItem[] | undefined>,
  2047. this.getGlobalState("allowedCommands") as Promise<string[] | undefined>,
  2048. this.getGlobalState("soundEnabled") as Promise<boolean | undefined>,
  2049. this.getGlobalState("diffEnabled") as Promise<boolean | undefined>,
  2050. this.getGlobalState("soundVolume") as Promise<number | undefined>,
  2051. this.getGlobalState("browserViewportSize") as Promise<string | undefined>,
  2052. this.getGlobalState("fuzzyMatchThreshold") as Promise<number | undefined>,
  2053. this.getGlobalState("preferredLanguage") as Promise<string | undefined>,
  2054. this.getGlobalState("writeDelayMs") as Promise<number | undefined>,
  2055. this.getGlobalState("screenshotQuality") as Promise<number | undefined>,
  2056. this.getGlobalState("terminalOutputLineLimit") as Promise<number | undefined>,
  2057. this.getGlobalState("mcpEnabled") as Promise<boolean | undefined>,
  2058. this.getGlobalState("alwaysApproveResubmit") as Promise<boolean | undefined>,
  2059. this.getGlobalState("requestDelaySeconds") as Promise<number | undefined>,
  2060. this.getGlobalState("currentApiConfigName") as Promise<string | undefined>,
  2061. this.getGlobalState("listApiConfigMeta") as Promise<ApiConfigMeta[] | undefined>,
  2062. this.getGlobalState("vsCodeLmModelSelector") as Promise<vscode.LanguageModelChatSelector | undefined>,
  2063. this.getGlobalState("mode") as Promise<Mode | undefined>,
  2064. this.getGlobalState("modeApiConfigs") as Promise<Record<Mode, string> | undefined>,
  2065. this.getGlobalState("customModePrompts") as Promise<CustomModePrompts | undefined>,
  2066. this.getGlobalState("customSupportPrompts") as Promise<CustomSupportPrompts | undefined>,
  2067. this.getGlobalState("enhancementApiConfigId") as Promise<string | undefined>,
  2068. this.getGlobalState("autoApprovalEnabled") as Promise<boolean | undefined>,
  2069. this.customModesManager.getCustomModes(),
  2070. this.getGlobalState("experiments") as Promise<Record<ExperimentId, boolean> | undefined>,
  2071. this.getSecret("unboundApiKey") as Promise<string | undefined>,
  2072. this.getGlobalState("unboundModelId") as Promise<string | undefined>,
  2073. ])
  2074. let apiProvider: ApiProvider
  2075. if (storedApiProvider) {
  2076. apiProvider = storedApiProvider
  2077. } else {
  2078. // Either new user or legacy user that doesn't have the apiProvider stored in state
  2079. // (If they're using OpenRouter or Bedrock, then apiProvider state will exist)
  2080. if (apiKey) {
  2081. apiProvider = "anthropic"
  2082. } else {
  2083. // New users should default to openrouter
  2084. apiProvider = "openrouter"
  2085. }
  2086. }
  2087. return {
  2088. apiConfiguration: {
  2089. apiProvider,
  2090. apiModelId,
  2091. apiKey,
  2092. glamaApiKey,
  2093. glamaModelId,
  2094. glamaModelInfo,
  2095. openRouterApiKey,
  2096. awsAccessKey,
  2097. awsSecretKey,
  2098. awsSessionToken,
  2099. awsRegion,
  2100. awsUseCrossRegionInference,
  2101. awsProfile,
  2102. awsUseProfile,
  2103. vertexProjectId,
  2104. vertexRegion,
  2105. openAiBaseUrl,
  2106. openAiApiKey,
  2107. openAiModelId,
  2108. openAiCustomModelInfo,
  2109. openAiUseAzure,
  2110. ollamaModelId,
  2111. ollamaBaseUrl,
  2112. lmStudioModelId,
  2113. lmStudioBaseUrl,
  2114. anthropicBaseUrl,
  2115. geminiApiKey,
  2116. openAiNativeApiKey,
  2117. deepSeekApiKey,
  2118. mistralApiKey,
  2119. azureApiVersion,
  2120. openAiStreamingEnabled,
  2121. openRouterModelId,
  2122. openRouterModelInfo,
  2123. openRouterBaseUrl,
  2124. openRouterUseMiddleOutTransform,
  2125. vsCodeLmModelSelector,
  2126. unboundApiKey,
  2127. unboundModelId,
  2128. },
  2129. lastShownAnnouncementId,
  2130. customInstructions,
  2131. alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
  2132. alwaysAllowWrite: alwaysAllowWrite ?? false,
  2133. alwaysAllowExecute: alwaysAllowExecute ?? false,
  2134. alwaysAllowBrowser: alwaysAllowBrowser ?? false,
  2135. alwaysAllowMcp: alwaysAllowMcp ?? false,
  2136. alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false,
  2137. taskHistory,
  2138. allowedCommands,
  2139. soundEnabled: soundEnabled ?? false,
  2140. diffEnabled: diffEnabled ?? true,
  2141. soundVolume,
  2142. browserViewportSize: browserViewportSize ?? "900x600",
  2143. screenshotQuality: screenshotQuality ?? 75,
  2144. fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
  2145. writeDelayMs: writeDelayMs ?? 1000,
  2146. terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
  2147. mode: mode ?? defaultModeSlug,
  2148. preferredLanguage:
  2149. preferredLanguage ??
  2150. (() => {
  2151. // Get VSCode's locale setting
  2152. const vscodeLang = vscode.env.language
  2153. // Map VSCode locale to our supported languages
  2154. const langMap: { [key: string]: string } = {
  2155. en: "English",
  2156. ar: "Arabic",
  2157. "pt-br": "Brazilian Portuguese",
  2158. cs: "Czech",
  2159. fr: "French",
  2160. de: "German",
  2161. hi: "Hindi",
  2162. hu: "Hungarian",
  2163. it: "Italian",
  2164. ja: "Japanese",
  2165. ko: "Korean",
  2166. pl: "Polish",
  2167. pt: "Portuguese",
  2168. ru: "Russian",
  2169. "zh-cn": "Simplified Chinese",
  2170. es: "Spanish",
  2171. "zh-tw": "Traditional Chinese",
  2172. tr: "Turkish",
  2173. }
  2174. // Return mapped language or default to English
  2175. return langMap[vscodeLang.split("-")[0]] ?? "English"
  2176. })(),
  2177. mcpEnabled: mcpEnabled ?? true,
  2178. alwaysApproveResubmit: alwaysApproveResubmit ?? false,
  2179. requestDelaySeconds: Math.max(5, requestDelaySeconds ?? 10),
  2180. currentApiConfigName: currentApiConfigName ?? "default",
  2181. listApiConfigMeta: listApiConfigMeta ?? [],
  2182. modeApiConfigs: modeApiConfigs ?? ({} as Record<Mode, string>),
  2183. customModePrompts: customModePrompts ?? {},
  2184. customSupportPrompts: customSupportPrompts ?? {},
  2185. enhancementApiConfigId,
  2186. experiments: experiments ?? experimentDefault,
  2187. autoApprovalEnabled: autoApprovalEnabled ?? false,
  2188. customModes,
  2189. }
  2190. }
  2191. async updateTaskHistory(item: HistoryItem): Promise<HistoryItem[]> {
  2192. const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || []
  2193. const existingItemIndex = history.findIndex((h) => h.id === item.id)
  2194. if (existingItemIndex !== -1) {
  2195. history[existingItemIndex] = item
  2196. } else {
  2197. history.push(item)
  2198. }
  2199. await this.updateGlobalState("taskHistory", history)
  2200. return history
  2201. }
  2202. // global
  2203. async updateGlobalState(key: GlobalStateKey, value: any) {
  2204. await this.context.globalState.update(key, value)
  2205. }
  2206. async getGlobalState(key: GlobalStateKey) {
  2207. return await this.context.globalState.get(key)
  2208. }
  2209. // workspace
  2210. private async updateWorkspaceState(key: string, value: any) {
  2211. await this.context.workspaceState.update(key, value)
  2212. }
  2213. private async getWorkspaceState(key: string) {
  2214. return await this.context.workspaceState.get(key)
  2215. }
  2216. // private async clearState() {
  2217. // this.context.workspaceState.keys().forEach((key) => {
  2218. // this.context.workspaceState.update(key, undefined)
  2219. // })
  2220. // this.context.globalState.keys().forEach((key) => {
  2221. // this.context.globalState.update(key, undefined)
  2222. // })
  2223. // this.context.secrets.delete("apiKey")
  2224. // }
  2225. // secrets
  2226. private async storeSecret(key: SecretKey, value?: string) {
  2227. if (value) {
  2228. await this.context.secrets.store(key, value)
  2229. } else {
  2230. await this.context.secrets.delete(key)
  2231. }
  2232. }
  2233. private async getSecret(key: SecretKey) {
  2234. return await this.context.secrets.get(key)
  2235. }
  2236. // dev
  2237. async resetState() {
  2238. const answer = await vscode.window.showInformationMessage(
  2239. "Are you sure you want to reset all state and secret storage in the extension? This cannot be undone.",
  2240. { modal: true },
  2241. "Yes",
  2242. )
  2243. if (answer !== "Yes") {
  2244. return
  2245. }
  2246. for (const key of this.context.globalState.keys()) {
  2247. await this.context.globalState.update(key, undefined)
  2248. }
  2249. const secretKeys: SecretKey[] = [
  2250. "apiKey",
  2251. "glamaApiKey",
  2252. "openRouterApiKey",
  2253. "awsAccessKey",
  2254. "awsSecretKey",
  2255. "awsSessionToken",
  2256. "openAiApiKey",
  2257. "geminiApiKey",
  2258. "openAiNativeApiKey",
  2259. "deepSeekApiKey",
  2260. "mistralApiKey",
  2261. "unboundApiKey",
  2262. ]
  2263. for (const key of secretKeys) {
  2264. await this.storeSecret(key, undefined)
  2265. }
  2266. await this.configManager.resetAllConfigs()
  2267. await this.customModesManager.resetCustomModes()
  2268. if (this.cline) {
  2269. this.cline.abortTask()
  2270. this.cline = undefined
  2271. }
  2272. await this.postStateToWebview()
  2273. await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
  2274. }
  2275. }