ClineProvider.ts 100 KB

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