ClineProvider.ts 91 KB

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