ClineProvider.ts 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505
  1. import os from "os"
  2. import * as path from "path"
  3. import fs from "fs/promises"
  4. import EventEmitter from "events"
  5. import { Anthropic } from "@anthropic-ai/sdk"
  6. import delay from "delay"
  7. import axios from "axios"
  8. import pWaitFor from "p-wait-for"
  9. import * as vscode from "vscode"
  10. import { GlobalState, ProviderSettings, RooCodeSettings } from "../../schemas"
  11. import { t } from "../../i18n"
  12. import { setPanel } from "../../activate/registerCommands"
  13. import {
  14. ApiConfiguration,
  15. ApiProvider,
  16. ModelInfo,
  17. requestyDefaultModelId,
  18. requestyDefaultModelInfo,
  19. openRouterDefaultModelId,
  20. openRouterDefaultModelInfo,
  21. glamaDefaultModelId,
  22. glamaDefaultModelInfo,
  23. } from "../../shared/api"
  24. import { findLast } from "../../shared/array"
  25. import { supportPrompt } from "../../shared/support-prompt"
  26. import { GlobalFileNames } from "../../shared/globalFileNames"
  27. import { HistoryItem } from "../../shared/HistoryItem"
  28. import { ExtensionMessage } from "../../shared/ExtensionMessage"
  29. import { Mode, PromptComponent, defaultModeSlug, getModeBySlug, getGroupName } from "../../shared/modes"
  30. import { experimentDefault } from "../../shared/experiments"
  31. import { formatLanguage } from "../../shared/language"
  32. import { Terminal, TERMINAL_SHELL_INTEGRATION_TIMEOUT } from "../../integrations/terminal/Terminal"
  33. import { downloadTask } from "../../integrations/misc/export-markdown"
  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 { fileExistsAtPath } from "../../utils/fs"
  40. import { setSoundEnabled } from "../../utils/sound"
  41. import { setTtsEnabled, setTtsSpeed } from "../../utils/tts"
  42. import { ContextProxy } from "../config/ContextProxy"
  43. import { ProviderSettingsManager } from "../config/ProviderSettingsManager"
  44. import { CustomModesManager } from "../config/CustomModesManager"
  45. import { buildApiHandler } from "../../api"
  46. import { ACTION_NAMES } from "../CodeActionProvider"
  47. import { Cline, ClineOptions } from "../Cline"
  48. import { getNonce } from "./getNonce"
  49. import { getUri } from "./getUri"
  50. import { telemetryService } from "../../services/telemetry/TelemetryService"
  51. import { getWorkspacePath } from "../../utils/path"
  52. import { webviewMessageHandler } from "./webviewMessageHandler"
  53. import { WebviewMessage } from "../../shared/WebviewMessage"
  54. /**
  55. * https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
  56. * https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
  57. */
  58. export type ClineProviderEvents = {
  59. clineCreated: [cline: Cline]
  60. }
  61. export class ClineProvider extends EventEmitter<ClineProviderEvents> implements vscode.WebviewViewProvider {
  62. public static readonly sideBarId = "roo-cline.SidebarProvider" // used in package.json as the view's id. This value cannot be changed due to how vscode caches views based on their id, and updating the id would break existing instances of the extension.
  63. public static readonly tabPanelId = "roo-cline.TabPanelProvider"
  64. private static activeInstances: Set<ClineProvider> = new Set()
  65. private disposables: vscode.Disposable[] = []
  66. private view?: vscode.WebviewView | vscode.WebviewPanel
  67. private clineStack: Cline[] = []
  68. private _workspaceTracker?: WorkspaceTracker // workSpaceTracker read-only for access outside this class
  69. public get workspaceTracker(): WorkspaceTracker | undefined {
  70. return this._workspaceTracker
  71. }
  72. protected mcpHub?: McpHub // Change from private to protected
  73. public isViewLaunched = false
  74. public settingsImportedAt?: number
  75. public readonly latestAnnouncementId = "apr-04-2025-boomerang" // update for Boomerang Tasks announcement
  76. public readonly contextProxy: ContextProxy
  77. public readonly providerSettingsManager: ProviderSettingsManager
  78. public readonly customModesManager: CustomModesManager
  79. constructor(
  80. readonly context: vscode.ExtensionContext,
  81. private readonly outputChannel: vscode.OutputChannel,
  82. private readonly renderContext: "sidebar" | "editor" = "sidebar",
  83. ) {
  84. super()
  85. this.log("ClineProvider instantiated")
  86. this.contextProxy = new ContextProxy(context)
  87. ClineProvider.activeInstances.add(this)
  88. // Register this provider with the telemetry service to enable it to add
  89. // properties like mode and provider.
  90. telemetryService.setProvider(this)
  91. this._workspaceTracker = new WorkspaceTracker(this)
  92. this.providerSettingsManager = new ProviderSettingsManager(this.context)
  93. this.customModesManager = new CustomModesManager(this.context, async () => {
  94. await this.postStateToWebview()
  95. })
  96. // Initialize MCP Hub through the singleton manager
  97. McpServerManager.getInstance(this.context, this)
  98. .then((hub) => {
  99. this.mcpHub = hub
  100. this.mcpHub.registerClient()
  101. })
  102. .catch((error) => {
  103. this.log(`Failed to initialize MCP Hub: ${error}`)
  104. })
  105. }
  106. // Adds a new Cline instance to clineStack, marking the start of a new task.
  107. // The instance is pushed to the top of the stack (LIFO order).
  108. // When the task is completed, the top instance is removed, reactivating the previous task.
  109. async addClineToStack(cline: Cline) {
  110. console.log(`[subtasks] adding task ${cline.taskId}.${cline.instanceId} to stack`)
  111. // Add this cline instance into the stack that represents the order of all the called tasks.
  112. this.clineStack.push(cline)
  113. // Ensure getState() resolves correctly.
  114. const state = await this.getState()
  115. if (!state || typeof state.mode !== "string") {
  116. throw new Error(t("common:errors.retrieve_current_mode"))
  117. }
  118. }
  119. // Removes and destroys the top Cline instance (the current finished task),
  120. // activating the previous one (resuming the parent task).
  121. async removeClineFromStack() {
  122. if (this.clineStack.length === 0) {
  123. return
  124. }
  125. // Pop the top Cline instance from the stack.
  126. var cline = this.clineStack.pop()
  127. if (cline) {
  128. console.log(`[subtasks] removing task ${cline.taskId}.${cline.instanceId} from stack`)
  129. try {
  130. // Abort the running task and set isAbandoned to true so
  131. // all running promises will exit as well.
  132. await cline.abortTask(true)
  133. } catch (e) {
  134. this.log(
  135. `[subtasks] encountered error while aborting task ${cline.taskId}.${cline.instanceId}: ${e.message}`,
  136. )
  137. }
  138. // Make sure no reference kept, once promises end it will be
  139. // garbage collected.
  140. cline = undefined
  141. }
  142. }
  143. // returns the current cline object in the stack (the top one)
  144. // if the stack is empty, returns undefined
  145. getCurrentCline(): Cline | undefined {
  146. if (this.clineStack.length === 0) {
  147. return undefined
  148. }
  149. return this.clineStack[this.clineStack.length - 1]
  150. }
  151. // returns the current clineStack length (how many cline objects are in the stack)
  152. getClineStackSize(): number {
  153. return this.clineStack.length
  154. }
  155. public getCurrentTaskStack(): string[] {
  156. return this.clineStack.map((cline) => cline.taskId)
  157. }
  158. // remove the current task/cline instance (at the top of the stack), ao this task is finished
  159. // and resume the previous task/cline instance (if it exists)
  160. // this is used when a sub task is finished and the parent task needs to be resumed
  161. async finishSubTask(lastMessage?: string) {
  162. console.log(`[subtasks] finishing subtask ${lastMessage}`)
  163. // remove the last cline instance from the stack (this is the finished sub task)
  164. await this.removeClineFromStack()
  165. // resume the last cline instance in the stack (if it exists - this is the 'parnt' calling task)
  166. this.getCurrentCline()?.resumePausedTask(lastMessage)
  167. }
  168. /*
  169. 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.
  170. - https://vscode-docs.readthedocs.io/en/stable/extensions/patterns-and-principles/
  171. - https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts
  172. */
  173. async dispose() {
  174. this.log("Disposing ClineProvider...")
  175. await this.removeClineFromStack()
  176. this.log("Cleared task")
  177. if (this.view && "dispose" in this.view) {
  178. this.view.dispose()
  179. this.log("Disposed webview")
  180. }
  181. while (this.disposables.length) {
  182. const x = this.disposables.pop()
  183. if (x) {
  184. x.dispose()
  185. }
  186. }
  187. this._workspaceTracker?.dispose()
  188. this._workspaceTracker = undefined
  189. await this.mcpHub?.unregisterClient()
  190. this.mcpHub = undefined
  191. this.customModesManager?.dispose()
  192. this.log("Disposed all disposables")
  193. ClineProvider.activeInstances.delete(this)
  194. // Unregister from McpServerManager
  195. McpServerManager.unregisterProvider(this)
  196. }
  197. public static getVisibleInstance(): ClineProvider | undefined {
  198. return findLast(Array.from(this.activeInstances), (instance) => instance.view?.visible === true)
  199. }
  200. public static async getInstance(): Promise<ClineProvider | undefined> {
  201. let visibleProvider = ClineProvider.getVisibleInstance()
  202. // If no visible provider, try to show the sidebar view
  203. if (!visibleProvider) {
  204. await vscode.commands.executeCommand("roo-cline.SidebarProvider.focus")
  205. // Wait briefly for the view to become visible
  206. await delay(100)
  207. visibleProvider = ClineProvider.getVisibleInstance()
  208. }
  209. // If still no visible provider, return
  210. if (!visibleProvider) {
  211. return
  212. }
  213. return visibleProvider
  214. }
  215. public static async isActiveTask(): Promise<boolean> {
  216. const visibleProvider = await ClineProvider.getInstance()
  217. if (!visibleProvider) {
  218. return false
  219. }
  220. // check if there is a cline instance in the stack (if this provider has an active task)
  221. if (visibleProvider.getCurrentCline()) {
  222. return true
  223. }
  224. return false
  225. }
  226. public static async handleCodeAction(
  227. command: string,
  228. promptType: keyof typeof ACTION_NAMES,
  229. params: Record<string, string | any[]>,
  230. ): Promise<void> {
  231. const visibleProvider = await ClineProvider.getInstance()
  232. if (!visibleProvider) {
  233. return
  234. }
  235. const { customSupportPrompts } = await visibleProvider.getState()
  236. const prompt = supportPrompt.create(promptType, params, customSupportPrompts)
  237. if (command.endsWith("addToContext")) {
  238. await visibleProvider.postMessageToWebview({
  239. type: "invoke",
  240. invoke: "setChatBoxMessage",
  241. text: prompt,
  242. })
  243. return
  244. }
  245. if (visibleProvider.getCurrentCline() && command.endsWith("InCurrentTask")) {
  246. await visibleProvider.postMessageToWebview({ type: "invoke", invoke: "sendMessage", text: prompt })
  247. return
  248. }
  249. await visibleProvider.initClineWithTask(prompt)
  250. }
  251. public static async handleTerminalAction(
  252. command: string,
  253. promptType: "TERMINAL_ADD_TO_CONTEXT" | "TERMINAL_FIX" | "TERMINAL_EXPLAIN",
  254. params: Record<string, string | any[]>,
  255. ): Promise<void> {
  256. const visibleProvider = await ClineProvider.getInstance()
  257. if (!visibleProvider) {
  258. return
  259. }
  260. const { customSupportPrompts } = await visibleProvider.getState()
  261. const prompt = supportPrompt.create(promptType, params, customSupportPrompts)
  262. if (command.endsWith("AddToContext")) {
  263. await visibleProvider.postMessageToWebview({
  264. type: "invoke",
  265. invoke: "setChatBoxMessage",
  266. text: prompt,
  267. })
  268. return
  269. }
  270. if (visibleProvider.getCurrentCline() && command.endsWith("InCurrentTask")) {
  271. await visibleProvider.postMessageToWebview({
  272. type: "invoke",
  273. invoke: "sendMessage",
  274. text: prompt,
  275. })
  276. return
  277. }
  278. await visibleProvider.initClineWithTask(prompt)
  279. }
  280. async resolveWebviewView(webviewView: vscode.WebviewView | vscode.WebviewPanel) {
  281. this.log("Resolving webview view")
  282. if (!this.contextProxy.isInitialized) {
  283. await this.contextProxy.initialize()
  284. }
  285. this.view = webviewView
  286. // Set panel reference according to webview type
  287. if ("onDidChangeViewState" in webviewView) {
  288. // Tag page type
  289. setPanel(webviewView, "tab")
  290. } else if ("onDidChangeVisibility" in webviewView) {
  291. // Sidebar Type
  292. setPanel(webviewView, "sidebar")
  293. }
  294. // Initialize out-of-scope variables that need to recieve persistent global state values
  295. this.getState().then(({ soundEnabled, terminalShellIntegrationTimeout }) => {
  296. setSoundEnabled(soundEnabled ?? false)
  297. Terminal.setShellIntegrationTimeout(terminalShellIntegrationTimeout ?? TERMINAL_SHELL_INTEGRATION_TIMEOUT)
  298. })
  299. // Initialize tts enabled state
  300. this.getState().then(({ ttsEnabled }) => {
  301. setTtsEnabled(ttsEnabled ?? false)
  302. })
  303. // Initialize tts speed state
  304. this.getState().then(({ ttsSpeed }) => {
  305. setTtsSpeed(ttsSpeed ?? 1)
  306. })
  307. webviewView.webview.options = {
  308. // Allow scripts in the webview
  309. enableScripts: true,
  310. localResourceRoots: [this.contextProxy.extensionUri],
  311. }
  312. webviewView.webview.html =
  313. this.contextProxy.extensionMode === vscode.ExtensionMode.Development
  314. ? await this.getHMRHtmlContent(webviewView.webview)
  315. : this.getHtmlContent(webviewView.webview)
  316. // Sets up an event listener to listen for messages passed from the webview view context
  317. // and executes code based on the message that is recieved
  318. this.setWebviewMessageListener(webviewView.webview)
  319. // Logs show up in bottom panel > Debug Console
  320. //console.log("registering listener")
  321. // Listen for when the panel becomes visible
  322. // https://github.com/microsoft/vscode-discussions/discussions/840
  323. if ("onDidChangeViewState" in webviewView) {
  324. // WebviewView and WebviewPanel have all the same properties except for this visibility listener
  325. // panel
  326. webviewView.onDidChangeViewState(
  327. () => {
  328. if (this.view?.visible) {
  329. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  330. }
  331. },
  332. null,
  333. this.disposables,
  334. )
  335. } else if ("onDidChangeVisibility" in webviewView) {
  336. // sidebar
  337. webviewView.onDidChangeVisibility(
  338. () => {
  339. if (this.view?.visible) {
  340. this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
  341. }
  342. },
  343. null,
  344. this.disposables,
  345. )
  346. }
  347. // Listen for when the view is disposed
  348. // This happens when the user closes the view or when the view is closed programmatically
  349. webviewView.onDidDispose(
  350. async () => {
  351. await this.dispose()
  352. },
  353. null,
  354. this.disposables,
  355. )
  356. // Listen for when color changes
  357. vscode.workspace.onDidChangeConfiguration(
  358. async (e) => {
  359. if (e && e.affectsConfiguration("workbench.colorTheme")) {
  360. // Sends latest theme name to webview
  361. await this.postMessageToWebview({ type: "theme", text: JSON.stringify(await getTheme()) })
  362. }
  363. },
  364. null,
  365. this.disposables,
  366. )
  367. // If the extension is starting a new session, clear previous task state.
  368. await this.removeClineFromStack()
  369. this.log("Webview view resolved")
  370. }
  371. public async initClineWithSubTask(parent: Cline, task?: string, images?: string[]) {
  372. return this.initClineWithTask(task, images, parent)
  373. }
  374. // When initializing a new task, (not from history but from a tool command
  375. // new_task) there is no need to remove the previouse task since the new
  376. // task is a subtask of the previous one, and when it finishes it is removed
  377. // from the stack and the caller is resumed in this way we can have a chain
  378. // of tasks, each one being a sub task of the previous one until the main
  379. // task is finished.
  380. public async initClineWithTask(
  381. task?: string,
  382. images?: string[],
  383. parentTask?: Cline,
  384. options: Partial<
  385. Pick<
  386. ClineOptions,
  387. | "customInstructions"
  388. | "enableDiff"
  389. | "enableCheckpoints"
  390. | "checkpointStorage"
  391. | "fuzzyMatchThreshold"
  392. | "consecutiveMistakeLimit"
  393. | "experiments"
  394. >
  395. > = {},
  396. ) {
  397. const {
  398. apiConfiguration,
  399. customModePrompts,
  400. diffEnabled: enableDiff,
  401. enableCheckpoints,
  402. checkpointStorage,
  403. fuzzyMatchThreshold,
  404. mode,
  405. customInstructions: globalInstructions,
  406. experiments,
  407. } = await this.getState()
  408. const modePrompt = customModePrompts?.[mode] as PromptComponent
  409. const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
  410. const cline = new Cline({
  411. provider: this,
  412. apiConfiguration,
  413. customInstructions: effectiveInstructions,
  414. enableDiff,
  415. enableCheckpoints,
  416. checkpointStorage,
  417. fuzzyMatchThreshold,
  418. task,
  419. images,
  420. experiments,
  421. rootTask: this.clineStack.length > 0 ? this.clineStack[0] : undefined,
  422. parentTask,
  423. taskNumber: this.clineStack.length + 1,
  424. onCreated: (cline) => this.emit("clineCreated", cline),
  425. ...options,
  426. })
  427. await this.addClineToStack(cline)
  428. this.log(
  429. `[subtasks] ${cline.parentTask ? "child" : "parent"} task ${cline.taskId}.${cline.instanceId} instantiated`,
  430. )
  431. return cline
  432. }
  433. public async initClineWithHistoryItem(historyItem: HistoryItem & { rootTask?: Cline; parentTask?: Cline }) {
  434. await this.removeClineFromStack()
  435. const {
  436. apiConfiguration,
  437. customModePrompts,
  438. diffEnabled: enableDiff,
  439. enableCheckpoints,
  440. checkpointStorage,
  441. fuzzyMatchThreshold,
  442. mode,
  443. customInstructions: globalInstructions,
  444. experiments,
  445. } = await this.getState()
  446. const modePrompt = customModePrompts?.[mode] as PromptComponent
  447. const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
  448. const taskId = historyItem.id
  449. const globalStorageDir = this.contextProxy.globalStorageUri.fsPath
  450. const workspaceDir = this.cwd
  451. const checkpoints: Pick<ClineOptions, "enableCheckpoints" | "checkpointStorage"> = {
  452. enableCheckpoints,
  453. checkpointStorage,
  454. }
  455. if (enableCheckpoints) {
  456. try {
  457. checkpoints.checkpointStorage = await ShadowCheckpointService.getTaskStorage({
  458. taskId,
  459. globalStorageDir,
  460. workspaceDir,
  461. })
  462. this.log(
  463. `[ClineProvider#initClineWithHistoryItem] Using ${checkpoints.checkpointStorage} storage for ${taskId}`,
  464. )
  465. } catch (error) {
  466. checkpoints.enableCheckpoints = false
  467. this.log(`[ClineProvider#initClineWithHistoryItem] Error getting task storage: ${error.message}`)
  468. }
  469. }
  470. const cline = new Cline({
  471. provider: this,
  472. apiConfiguration,
  473. customInstructions: effectiveInstructions,
  474. enableDiff,
  475. ...checkpoints,
  476. fuzzyMatchThreshold,
  477. historyItem,
  478. experiments,
  479. rootTask: historyItem.rootTask,
  480. parentTask: historyItem.parentTask,
  481. taskNumber: historyItem.number,
  482. onCreated: (cline) => this.emit("clineCreated", cline),
  483. })
  484. await this.addClineToStack(cline)
  485. this.log(
  486. `[subtasks] ${cline.parentTask ? "child" : "parent"} task ${cline.taskId}.${cline.instanceId} instantiated`,
  487. )
  488. return cline
  489. }
  490. public async postMessageToWebview(message: ExtensionMessage) {
  491. await this.view?.webview.postMessage(message)
  492. }
  493. private async getHMRHtmlContent(webview: vscode.Webview): Promise<string> {
  494. const localPort = "5173"
  495. const localServerUrl = `localhost:${localPort}`
  496. // Check if local dev server is running.
  497. try {
  498. await axios.get(`http://${localServerUrl}`)
  499. } catch (error) {
  500. vscode.window.showErrorMessage(t("common:errors.hmr_not_running"))
  501. return this.getHtmlContent(webview)
  502. }
  503. const nonce = getNonce()
  504. const stylesUri = getUri(webview, this.contextProxy.extensionUri, [
  505. "webview-ui",
  506. "build",
  507. "assets",
  508. "index.css",
  509. ])
  510. const codiconsUri = getUri(webview, this.contextProxy.extensionUri, [
  511. "node_modules",
  512. "@vscode",
  513. "codicons",
  514. "dist",
  515. "codicon.css",
  516. ])
  517. const imagesUri = getUri(webview, this.contextProxy.extensionUri, ["assets", "images"])
  518. const file = "src/index.tsx"
  519. const scriptUri = `http://${localServerUrl}/${file}`
  520. const reactRefresh = /*html*/ `
  521. <script nonce="${nonce}" type="module">
  522. import RefreshRuntime from "http://localhost:${localPort}/@react-refresh"
  523. RefreshRuntime.injectIntoGlobalHook(window)
  524. window.$RefreshReg$ = () => {}
  525. window.$RefreshSig$ = () => (type) => type
  526. window.__vite_plugin_react_preamble_installed__ = true
  527. </script>
  528. `
  529. const csp = [
  530. "default-src 'none'",
  531. `font-src ${webview.cspSource}`,
  532. `style-src ${webview.cspSource} 'unsafe-inline' https://* http://${localServerUrl} http://0.0.0.0:${localPort}`,
  533. `img-src ${webview.cspSource} data:`,
  534. `script-src 'unsafe-eval' ${webview.cspSource} https://* https://*.posthog.com http://${localServerUrl} http://0.0.0.0:${localPort} 'nonce-${nonce}'`,
  535. `connect-src https://* https://*.posthog.com ws://${localServerUrl} ws://0.0.0.0:${localPort} http://${localServerUrl} http://0.0.0.0:${localPort}`,
  536. ]
  537. return /*html*/ `
  538. <!DOCTYPE html>
  539. <html lang="en">
  540. <head>
  541. <meta charset="utf-8">
  542. <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  543. <meta http-equiv="Content-Security-Policy" content="${csp.join("; ")}">
  544. <link rel="stylesheet" type="text/css" href="${stylesUri}">
  545. <link href="${codiconsUri}" rel="stylesheet" />
  546. <script nonce="${nonce}">
  547. window.IMAGES_BASE_URI = "${imagesUri}"
  548. </script>
  549. <title>Roo Code</title>
  550. </head>
  551. <body>
  552. <div id="root"></div>
  553. ${reactRefresh}
  554. <script type="module" src="${scriptUri}"></script>
  555. </body>
  556. </html>
  557. `
  558. }
  559. /**
  560. * Defines and returns the HTML that should be rendered within the webview panel.
  561. *
  562. * @remarks This is also the place where references to the React webview build files
  563. * are created and inserted into the webview HTML.
  564. *
  565. * @param webview A reference to the extension webview
  566. * @param extensionUri The URI of the directory containing the extension
  567. * @returns A template string literal containing the HTML that should be
  568. * rendered within the webview panel
  569. */
  570. private getHtmlContent(webview: vscode.Webview): string {
  571. // Get the local path to main script run in the webview,
  572. // then convert it to a uri we can use in the webview.
  573. // The CSS file from the React build output
  574. const stylesUri = getUri(webview, this.contextProxy.extensionUri, [
  575. "webview-ui",
  576. "build",
  577. "assets",
  578. "index.css",
  579. ])
  580. // The JS file from the React build output
  581. const scriptUri = getUri(webview, this.contextProxy.extensionUri, ["webview-ui", "build", "assets", "index.js"])
  582. // The codicon font from the React build output
  583. // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-codicons-sample/src/extension.ts
  584. // 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
  585. // don't forget to add font-src ${webview.cspSource};
  586. const codiconsUri = getUri(webview, this.contextProxy.extensionUri, [
  587. "node_modules",
  588. "@vscode",
  589. "codicons",
  590. "dist",
  591. "codicon.css",
  592. ])
  593. const imagesUri = getUri(webview, this.contextProxy.extensionUri, ["assets", "images"])
  594. // const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.js"))
  595. // const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "reset.css"))
  596. // const styleVSCodeUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "vscode.css"))
  597. // // Same for stylesheet
  598. // const stylesheetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.css"))
  599. // Use a nonce to only allow a specific script to be run.
  600. /*
  601. content security policy of your webview to only allow scripts that have a specific nonce
  602. create a content security policy meta tag so that only loading scripts with a nonce is allowed
  603. 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.
  604. <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}';">
  605. - 'unsafe-inline' is required for styles due to vscode-webview-toolkit's dynamic style injection
  606. - since we pass base64 images to the webview, we need to specify img-src ${webview.cspSource} data:;
  607. 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.
  608. */
  609. const nonce = getNonce()
  610. // Tip: Install the es6-string-html VS Code extension to enable code highlighting below
  611. return /*html*/ `
  612. <!DOCTYPE html>
  613. <html lang="en">
  614. <head>
  615. <meta charset="utf-8">
  616. <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  617. <meta name="theme-color" content="#000000">
  618. <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://api.requesty.ai https://us.i.posthog.com https://us-assets.i.posthog.com;">
  619. <link rel="stylesheet" type="text/css" href="${stylesUri}">
  620. <link href="${codiconsUri}" rel="stylesheet" />
  621. <script nonce="${nonce}">
  622. window.IMAGES_BASE_URI = "${imagesUri}"
  623. </script>
  624. <title>Roo Code</title>
  625. </head>
  626. <body>
  627. <noscript>You need to enable JavaScript to run this app.</noscript>
  628. <div id="root"></div>
  629. <script nonce="${nonce}" type="module" src="${scriptUri}"></script>
  630. </body>
  631. </html>
  632. `
  633. }
  634. /**
  635. * Sets up an event listener to listen for messages passed from the webview context and
  636. * executes code based on the message that is recieved.
  637. *
  638. * @param webview A reference to the extension webview
  639. */
  640. private setWebviewMessageListener(webview: vscode.Webview) {
  641. const onReceiveMessage = async (message: WebviewMessage) => webviewMessageHandler(this, message)
  642. webview.onDidReceiveMessage(onReceiveMessage, null, this.disposables)
  643. }
  644. /**
  645. * Handle switching to a new mode, including updating the associated API configuration
  646. * @param newMode The mode to switch to
  647. */
  648. public async handleModeSwitch(newMode: Mode) {
  649. // Capture mode switch telemetry event
  650. const cline = this.getCurrentCline()
  651. if (cline) {
  652. telemetryService.captureModeSwitch(cline.taskId, newMode)
  653. cline.emit("taskModeSwitched", cline.taskId, newMode)
  654. }
  655. await this.updateGlobalState("mode", newMode)
  656. // Load the saved API config for the new mode if it exists
  657. const savedConfigId = await this.providerSettingsManager.getModeConfigId(newMode)
  658. const listApiConfig = await this.providerSettingsManager.listConfig()
  659. // Update listApiConfigMeta first to ensure UI has latest data
  660. await this.updateGlobalState("listApiConfigMeta", listApiConfig)
  661. // If this mode has a saved config, use it
  662. if (savedConfigId) {
  663. const config = listApiConfig?.find((c) => c.id === savedConfigId)
  664. if (config?.name) {
  665. const apiConfig = await this.providerSettingsManager.loadConfig(config.name)
  666. await Promise.all([
  667. this.updateGlobalState("currentApiConfigName", config.name),
  668. this.updateApiConfiguration(apiConfig),
  669. ])
  670. }
  671. } else {
  672. // If no saved config for this mode, save current config as default
  673. const currentApiConfigName = this.getGlobalState("currentApiConfigName")
  674. if (currentApiConfigName) {
  675. const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
  676. if (config?.id) {
  677. await this.providerSettingsManager.setModeConfig(newMode, config.id)
  678. }
  679. }
  680. }
  681. await this.postStateToWebview()
  682. }
  683. async updateApiConfiguration(providerSettings: ProviderSettings) {
  684. // Update mode's default config.
  685. const { mode } = await this.getState()
  686. if (mode) {
  687. const currentApiConfigName = this.getGlobalState("currentApiConfigName")
  688. const listApiConfig = await this.providerSettingsManager.listConfig()
  689. const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
  690. if (config?.id) {
  691. await this.providerSettingsManager.setModeConfig(mode, config.id)
  692. }
  693. }
  694. await this.contextProxy.setProviderSettings(providerSettings)
  695. if (this.getCurrentCline()) {
  696. this.getCurrentCline()!.api = buildApiHandler(providerSettings)
  697. }
  698. }
  699. async cancelTask() {
  700. const cline = this.getCurrentCline()
  701. if (!cline) {
  702. return
  703. }
  704. console.log(`[subtasks] cancelling task ${cline.taskId}.${cline.instanceId}`)
  705. const { historyItem } = await this.getTaskWithId(cline.taskId)
  706. // Preserve parent and root task information for history item.
  707. const rootTask = cline.rootTask
  708. const parentTask = cline.parentTask
  709. cline.abortTask()
  710. await pWaitFor(
  711. () =>
  712. this.getCurrentCline()! === undefined ||
  713. this.getCurrentCline()!.isStreaming === false ||
  714. this.getCurrentCline()!.didFinishAbortingStream ||
  715. // If only the first chunk is processed, then there's no
  716. // need to wait for graceful abort (closes edits, browser,
  717. // etc).
  718. this.getCurrentCline()!.isWaitingForFirstChunk,
  719. {
  720. timeout: 3_000,
  721. },
  722. ).catch(() => {
  723. console.error("Failed to abort task")
  724. })
  725. if (this.getCurrentCline()) {
  726. // 'abandoned' will prevent this Cline instance from affecting
  727. // future Cline instances. This may happen if its hanging on a
  728. // streaming request.
  729. this.getCurrentCline()!.abandoned = true
  730. }
  731. // Clears task again, so we need to abortTask manually above.
  732. await this.initClineWithHistoryItem({ ...historyItem, rootTask, parentTask })
  733. }
  734. async updateCustomInstructions(instructions?: string) {
  735. // User may be clearing the field.
  736. await this.updateGlobalState("customInstructions", instructions || undefined)
  737. if (this.getCurrentCline()) {
  738. this.getCurrentCline()!.customInstructions = instructions || undefined
  739. }
  740. await this.postStateToWebview()
  741. }
  742. // MCP
  743. async ensureMcpServersDirectoryExists(): Promise<string> {
  744. // Get platform-specific application data directory
  745. let mcpServersDir: string
  746. if (process.platform === "win32") {
  747. // Windows: %APPDATA%\Roo-Code\MCP
  748. mcpServersDir = path.join(os.homedir(), "AppData", "Roaming", "Roo-Code", "MCP")
  749. } else if (process.platform === "darwin") {
  750. // macOS: ~/Documents/Cline/MCP
  751. mcpServersDir = path.join(os.homedir(), "Documents", "Cline", "MCP")
  752. } else {
  753. // Linux: ~/.local/share/Cline/MCP
  754. mcpServersDir = path.join(os.homedir(), ".local", "share", "Roo-Code", "MCP")
  755. }
  756. try {
  757. await fs.mkdir(mcpServersDir, { recursive: true })
  758. } catch (error) {
  759. // Fallback to a relative path if directory creation fails
  760. return path.join(os.homedir(), ".roo-code", "mcp")
  761. }
  762. return mcpServersDir
  763. }
  764. async ensureSettingsDirectoryExists(): Promise<string> {
  765. const { getSettingsDirectoryPath } = await import("../../shared/storagePathManager")
  766. const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
  767. return getSettingsDirectoryPath(globalStoragePath)
  768. }
  769. private async ensureCacheDirectoryExists() {
  770. const { getCacheDirectoryPath } = await import("../../shared/storagePathManager")
  771. const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
  772. return getCacheDirectoryPath(globalStoragePath)
  773. }
  774. async writeModelsToCache<T>(filename: string, data: T) {
  775. const cacheDir = await this.ensureCacheDirectoryExists()
  776. await fs.writeFile(path.join(cacheDir, filename), JSON.stringify(data))
  777. }
  778. async readModelsFromCache(filename: string): Promise<Record<string, ModelInfo> | undefined> {
  779. const filePath = path.join(await this.ensureCacheDirectoryExists(), filename)
  780. const fileExists = await fileExistsAtPath(filePath)
  781. if (fileExists) {
  782. const fileContents = await fs.readFile(filePath, "utf8")
  783. return JSON.parse(fileContents)
  784. }
  785. return undefined
  786. }
  787. // OpenRouter
  788. async handleOpenRouterCallback(code: string) {
  789. let { apiConfiguration, currentApiConfigName } = await this.getState()
  790. let apiKey: string
  791. try {
  792. const baseUrl = apiConfiguration.openRouterBaseUrl || "https://openrouter.ai/api/v1"
  793. // Extract the base domain for the auth endpoint
  794. const baseUrlDomain = baseUrl.match(/^(https?:\/\/[^\/]+)/)?.[1] || "https://openrouter.ai"
  795. const response = await axios.post(`${baseUrlDomain}/api/v1/auth/keys`, { code })
  796. if (response.data && response.data.key) {
  797. apiKey = response.data.key
  798. } else {
  799. throw new Error("Invalid response from OpenRouter API")
  800. }
  801. } catch (error) {
  802. this.log(
  803. `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  804. )
  805. throw error
  806. }
  807. const newConfiguration: ApiConfiguration = {
  808. ...apiConfiguration,
  809. apiProvider: "openrouter",
  810. openRouterApiKey: apiKey,
  811. openRouterModelId: apiConfiguration?.openRouterModelId || openRouterDefaultModelId,
  812. openRouterModelInfo: apiConfiguration?.openRouterModelInfo || openRouterDefaultModelInfo,
  813. }
  814. await this.upsertApiConfiguration(currentApiConfigName, newConfiguration)
  815. }
  816. // Glama
  817. async handleGlamaCallback(code: string) {
  818. let apiKey: string
  819. try {
  820. const response = await axios.post("https://glama.ai/api/gateway/v1/auth/exchange-code", { code })
  821. if (response.data && response.data.apiKey) {
  822. apiKey = response.data.apiKey
  823. } else {
  824. throw new Error("Invalid response from Glama API")
  825. }
  826. } catch (error) {
  827. this.log(
  828. `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  829. )
  830. throw error
  831. }
  832. const { apiConfiguration, currentApiConfigName } = await this.getState()
  833. const newConfiguration: ApiConfiguration = {
  834. ...apiConfiguration,
  835. apiProvider: "glama",
  836. glamaApiKey: apiKey,
  837. glamaModelId: apiConfiguration?.glamaModelId || glamaDefaultModelId,
  838. glamaModelInfo: apiConfiguration?.glamaModelInfo || glamaDefaultModelInfo,
  839. }
  840. await this.upsertApiConfiguration(currentApiConfigName, newConfiguration)
  841. }
  842. // Requesty
  843. async handleRequestyCallback(code: string) {
  844. let { apiConfiguration, currentApiConfigName } = await this.getState()
  845. const newConfiguration: ApiConfiguration = {
  846. ...apiConfiguration,
  847. apiProvider: "requesty",
  848. requestyApiKey: code,
  849. requestyModelId: apiConfiguration?.requestyModelId || requestyDefaultModelId,
  850. requestyModelInfo: apiConfiguration?.requestyModelInfo || requestyDefaultModelInfo,
  851. }
  852. await this.upsertApiConfiguration(currentApiConfigName, newConfiguration)
  853. }
  854. // Save configuration
  855. async upsertApiConfiguration(configName: string, apiConfiguration: ApiConfiguration) {
  856. try {
  857. await this.providerSettingsManager.saveConfig(configName, apiConfiguration)
  858. const listApiConfig = await this.providerSettingsManager.listConfig()
  859. await Promise.all([
  860. this.updateGlobalState("listApiConfigMeta", listApiConfig),
  861. this.updateApiConfiguration(apiConfiguration),
  862. this.updateGlobalState("currentApiConfigName", configName),
  863. ])
  864. await this.postStateToWebview()
  865. } catch (error) {
  866. this.log(
  867. `Error create new api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
  868. )
  869. vscode.window.showErrorMessage(t("common:errors.create_api_config"))
  870. }
  871. }
  872. // Task history
  873. async getTaskWithId(id: string): Promise<{
  874. historyItem: HistoryItem
  875. taskDirPath: string
  876. apiConversationHistoryFilePath: string
  877. uiMessagesFilePath: string
  878. apiConversationHistory: Anthropic.MessageParam[]
  879. }> {
  880. const history = this.getGlobalState("taskHistory") ?? []
  881. const historyItem = history.find((item) => item.id === id)
  882. if (historyItem) {
  883. const { getTaskDirectoryPath } = await import("../../shared/storagePathManager")
  884. const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
  885. const taskDirPath = await getTaskDirectoryPath(globalStoragePath, id)
  886. const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
  887. const uiMessagesFilePath = path.join(taskDirPath, GlobalFileNames.uiMessages)
  888. const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
  889. if (fileExists) {
  890. const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8"))
  891. return {
  892. historyItem,
  893. taskDirPath,
  894. apiConversationHistoryFilePath,
  895. uiMessagesFilePath,
  896. apiConversationHistory,
  897. }
  898. }
  899. }
  900. // if we tried to get a task that doesn't exist, remove it from state
  901. // FIXME: this seems to happen sometimes when the json file doesnt save to disk for some reason
  902. await this.deleteTaskFromState(id)
  903. throw new Error("Task not found")
  904. }
  905. async showTaskWithId(id: string) {
  906. if (id !== this.getCurrentCline()?.taskId) {
  907. // Non-current task.
  908. const { historyItem } = await this.getTaskWithId(id)
  909. await this.initClineWithHistoryItem(historyItem) // Clears existing task.
  910. }
  911. await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
  912. }
  913. async exportTaskWithId(id: string) {
  914. const { historyItem, apiConversationHistory } = await this.getTaskWithId(id)
  915. await downloadTask(historyItem.ts, apiConversationHistory)
  916. }
  917. // this function deletes a task from task hidtory, and deletes it's checkpoints and delete the task folder
  918. async deleteTaskWithId(id: string) {
  919. try {
  920. // get the task directory full path
  921. const { taskDirPath } = await this.getTaskWithId(id)
  922. // remove task from stack if it's the current task
  923. if (id === this.getCurrentCline()?.taskId) {
  924. // if we found the taskid to delete - call finish to abort this task and allow a new task to be started,
  925. // 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)
  926. await this.finishSubTask(t("common:tasks.deleted"))
  927. }
  928. // delete task from the task history state
  929. await this.deleteTaskFromState(id)
  930. // Delete associated shadow repository or branch.
  931. // TODO: Store `workspaceDir` in the `HistoryItem` object.
  932. const globalStorageDir = this.contextProxy.globalStorageUri.fsPath
  933. const workspaceDir = this.cwd
  934. try {
  935. await ShadowCheckpointService.deleteTask({ taskId: id, globalStorageDir, workspaceDir })
  936. } catch (error) {
  937. console.error(
  938. `[deleteTaskWithId${id}] failed to delete associated shadow repository or branch: ${error instanceof Error ? error.message : String(error)}`,
  939. )
  940. }
  941. // delete the entire task directory including checkpoints and all content
  942. try {
  943. await fs.rm(taskDirPath, { recursive: true, force: true })
  944. console.log(`[deleteTaskWithId${id}] removed task directory`)
  945. } catch (error) {
  946. console.error(
  947. `[deleteTaskWithId${id}] failed to remove task directory: ${error instanceof Error ? error.message : String(error)}`,
  948. )
  949. }
  950. } catch (error) {
  951. // If task is not found, just remove it from state
  952. if (error instanceof Error && error.message === "Task not found") {
  953. await this.deleteTaskFromState(id)
  954. return
  955. }
  956. throw error
  957. }
  958. }
  959. async deleteTaskFromState(id: string) {
  960. const taskHistory = this.getGlobalState("taskHistory") ?? []
  961. const updatedTaskHistory = taskHistory.filter((task) => task.id !== id)
  962. await this.updateGlobalState("taskHistory", updatedTaskHistory)
  963. await this.postStateToWebview()
  964. }
  965. async postStateToWebview() {
  966. const state = await this.getStateToPostToWebview()
  967. this.postMessageToWebview({ type: "state", state })
  968. }
  969. async getStateToPostToWebview() {
  970. const {
  971. apiConfiguration,
  972. lastShownAnnouncementId,
  973. customInstructions,
  974. alwaysAllowReadOnly,
  975. alwaysAllowReadOnlyOutsideWorkspace,
  976. alwaysAllowWrite,
  977. alwaysAllowWriteOutsideWorkspace,
  978. alwaysAllowExecute,
  979. alwaysAllowBrowser,
  980. alwaysAllowMcp,
  981. alwaysAllowModeSwitch,
  982. alwaysAllowSubtasks,
  983. soundEnabled,
  984. ttsEnabled,
  985. ttsSpeed,
  986. diffEnabled,
  987. enableCheckpoints,
  988. checkpointStorage,
  989. taskHistory,
  990. soundVolume,
  991. browserViewportSize,
  992. screenshotQuality,
  993. remoteBrowserHost,
  994. remoteBrowserEnabled,
  995. cachedChromeHostUrl,
  996. writeDelayMs,
  997. terminalOutputLineLimit,
  998. terminalShellIntegrationTimeout,
  999. fuzzyMatchThreshold,
  1000. mcpEnabled,
  1001. enableMcpServerCreation,
  1002. alwaysApproveResubmit,
  1003. requestDelaySeconds,
  1004. rateLimitSeconds,
  1005. currentApiConfigName,
  1006. listApiConfigMeta,
  1007. pinnedApiConfigs,
  1008. mode,
  1009. customModePrompts,
  1010. customSupportPrompts,
  1011. enhancementApiConfigId,
  1012. autoApprovalEnabled,
  1013. experiments,
  1014. maxOpenTabsContext,
  1015. maxWorkspaceFiles,
  1016. browserToolEnabled,
  1017. telemetrySetting,
  1018. showRooIgnoredFiles,
  1019. language,
  1020. maxReadFileLine,
  1021. } = await this.getState()
  1022. const telemetryKey = process.env.POSTHOG_API_KEY
  1023. const machineId = vscode.env.machineId
  1024. const allowedCommands = vscode.workspace.getConfiguration("roo-cline").get<string[]>("allowedCommands") || []
  1025. const cwd = this.cwd
  1026. return {
  1027. version: this.context.extension?.packageJSON?.version ?? "",
  1028. apiConfiguration,
  1029. customInstructions,
  1030. alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
  1031. alwaysAllowReadOnlyOutsideWorkspace: alwaysAllowReadOnlyOutsideWorkspace ?? false,
  1032. alwaysAllowWrite: alwaysAllowWrite ?? false,
  1033. alwaysAllowWriteOutsideWorkspace: alwaysAllowWriteOutsideWorkspace ?? false,
  1034. alwaysAllowExecute: alwaysAllowExecute ?? false,
  1035. alwaysAllowBrowser: alwaysAllowBrowser ?? false,
  1036. alwaysAllowMcp: alwaysAllowMcp ?? false,
  1037. alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false,
  1038. alwaysAllowSubtasks: alwaysAllowSubtasks ?? false,
  1039. uriScheme: vscode.env.uriScheme,
  1040. currentTaskItem: this.getCurrentCline()?.taskId
  1041. ? (taskHistory || []).find((item: HistoryItem) => item.id === this.getCurrentCline()?.taskId)
  1042. : undefined,
  1043. clineMessages: this.getCurrentCline()?.clineMessages || [],
  1044. taskHistory: (taskHistory || [])
  1045. .filter((item: HistoryItem) => item.ts && item.task)
  1046. .sort((a: HistoryItem, b: HistoryItem) => b.ts - a.ts),
  1047. soundEnabled: soundEnabled ?? false,
  1048. ttsEnabled: ttsEnabled ?? false,
  1049. ttsSpeed: ttsSpeed ?? 1.0,
  1050. diffEnabled: diffEnabled ?? true,
  1051. enableCheckpoints: enableCheckpoints ?? true,
  1052. checkpointStorage: checkpointStorage ?? "task",
  1053. shouldShowAnnouncement:
  1054. telemetrySetting !== "unset" && lastShownAnnouncementId !== this.latestAnnouncementId,
  1055. allowedCommands,
  1056. soundVolume: soundVolume ?? 0.5,
  1057. browserViewportSize: browserViewportSize ?? "900x600",
  1058. screenshotQuality: screenshotQuality ?? 75,
  1059. remoteBrowserHost,
  1060. remoteBrowserEnabled: remoteBrowserEnabled ?? false,
  1061. cachedChromeHostUrl: cachedChromeHostUrl,
  1062. writeDelayMs: writeDelayMs ?? 1000,
  1063. terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
  1064. terminalShellIntegrationTimeout: terminalShellIntegrationTimeout ?? TERMINAL_SHELL_INTEGRATION_TIMEOUT,
  1065. fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
  1066. mcpEnabled: mcpEnabled ?? true,
  1067. enableMcpServerCreation: enableMcpServerCreation ?? true,
  1068. alwaysApproveResubmit: alwaysApproveResubmit ?? false,
  1069. requestDelaySeconds: requestDelaySeconds ?? 10,
  1070. rateLimitSeconds: rateLimitSeconds ?? 0,
  1071. currentApiConfigName: currentApiConfigName ?? "default",
  1072. listApiConfigMeta: listApiConfigMeta ?? [],
  1073. pinnedApiConfigs: pinnedApiConfigs ?? {},
  1074. mode: mode ?? defaultModeSlug,
  1075. customModePrompts: customModePrompts ?? {},
  1076. customSupportPrompts: customSupportPrompts ?? {},
  1077. enhancementApiConfigId,
  1078. autoApprovalEnabled: autoApprovalEnabled ?? false,
  1079. customModes: await this.customModesManager.getCustomModes(),
  1080. experiments: experiments ?? experimentDefault,
  1081. mcpServers: this.mcpHub?.getAllServers() ?? [],
  1082. maxOpenTabsContext: maxOpenTabsContext ?? 20,
  1083. maxWorkspaceFiles: maxWorkspaceFiles ?? 200,
  1084. cwd,
  1085. browserToolEnabled: browserToolEnabled ?? true,
  1086. telemetrySetting,
  1087. telemetryKey,
  1088. machineId,
  1089. showRooIgnoredFiles: showRooIgnoredFiles ?? true,
  1090. language,
  1091. renderContext: this.renderContext,
  1092. maxReadFileLine: maxReadFileLine ?? 500,
  1093. settingsImportedAt: this.settingsImportedAt,
  1094. }
  1095. }
  1096. /**
  1097. * Storage
  1098. * https://dev.to/kompotkot/how-to-use-secretstorage-in-your-vscode-extensions-2hco
  1099. * https://www.eliostruyf.com/devhack-code-extension-storage-options/
  1100. */
  1101. async getState() {
  1102. const stateValues = this.contextProxy.getValues()
  1103. const customModes = await this.customModesManager.getCustomModes()
  1104. // Determine apiProvider with the same logic as before.
  1105. const apiProvider: ApiProvider = stateValues.apiProvider ? stateValues.apiProvider : "anthropic"
  1106. // Build the apiConfiguration object combining state values and secrets.
  1107. const providerSettings = this.contextProxy.getProviderSettings()
  1108. // Ensure apiProvider is set properly if not already in state
  1109. if (!providerSettings.apiProvider) {
  1110. providerSettings.apiProvider = apiProvider
  1111. }
  1112. // Return the same structure as before
  1113. return {
  1114. apiConfiguration: providerSettings,
  1115. lastShownAnnouncementId: stateValues.lastShownAnnouncementId,
  1116. customInstructions: stateValues.customInstructions,
  1117. alwaysAllowReadOnly: stateValues.alwaysAllowReadOnly ?? false,
  1118. alwaysAllowReadOnlyOutsideWorkspace: stateValues.alwaysAllowReadOnlyOutsideWorkspace ?? false,
  1119. alwaysAllowWrite: stateValues.alwaysAllowWrite ?? false,
  1120. alwaysAllowWriteOutsideWorkspace: stateValues.alwaysAllowWriteOutsideWorkspace ?? false,
  1121. alwaysAllowExecute: stateValues.alwaysAllowExecute ?? false,
  1122. alwaysAllowBrowser: stateValues.alwaysAllowBrowser ?? false,
  1123. alwaysAllowMcp: stateValues.alwaysAllowMcp ?? false,
  1124. alwaysAllowModeSwitch: stateValues.alwaysAllowModeSwitch ?? false,
  1125. alwaysAllowSubtasks: stateValues.alwaysAllowSubtasks ?? false,
  1126. taskHistory: stateValues.taskHistory,
  1127. allowedCommands: stateValues.allowedCommands,
  1128. soundEnabled: stateValues.soundEnabled ?? false,
  1129. ttsEnabled: stateValues.ttsEnabled ?? false,
  1130. ttsSpeed: stateValues.ttsSpeed ?? 1.0,
  1131. diffEnabled: stateValues.diffEnabled ?? true,
  1132. enableCheckpoints: stateValues.enableCheckpoints ?? true,
  1133. checkpointStorage: stateValues.checkpointStorage ?? "task",
  1134. soundVolume: stateValues.soundVolume,
  1135. browserViewportSize: stateValues.browserViewportSize ?? "900x600",
  1136. screenshotQuality: stateValues.screenshotQuality ?? 75,
  1137. remoteBrowserHost: stateValues.remoteBrowserHost,
  1138. remoteBrowserEnabled: stateValues.remoteBrowserEnabled ?? false,
  1139. cachedChromeHostUrl: stateValues.cachedChromeHostUrl as string | undefined,
  1140. fuzzyMatchThreshold: stateValues.fuzzyMatchThreshold ?? 1.0,
  1141. writeDelayMs: stateValues.writeDelayMs ?? 1000,
  1142. terminalOutputLineLimit: stateValues.terminalOutputLineLimit ?? 500,
  1143. terminalShellIntegrationTimeout:
  1144. stateValues.terminalShellIntegrationTimeout ?? TERMINAL_SHELL_INTEGRATION_TIMEOUT,
  1145. mode: stateValues.mode ?? defaultModeSlug,
  1146. language: stateValues.language ?? formatLanguage(vscode.env.language),
  1147. mcpEnabled: stateValues.mcpEnabled ?? true,
  1148. enableMcpServerCreation: stateValues.enableMcpServerCreation ?? true,
  1149. alwaysApproveResubmit: stateValues.alwaysApproveResubmit ?? false,
  1150. requestDelaySeconds: Math.max(5, stateValues.requestDelaySeconds ?? 10),
  1151. rateLimitSeconds: stateValues.rateLimitSeconds ?? 0,
  1152. currentApiConfigName: stateValues.currentApiConfigName ?? "default",
  1153. listApiConfigMeta: stateValues.listApiConfigMeta ?? [],
  1154. pinnedApiConfigs: stateValues.pinnedApiConfigs ?? {},
  1155. modeApiConfigs: stateValues.modeApiConfigs ?? ({} as Record<Mode, string>),
  1156. customModePrompts: stateValues.customModePrompts ?? {},
  1157. customSupportPrompts: stateValues.customSupportPrompts ?? {},
  1158. enhancementApiConfigId: stateValues.enhancementApiConfigId,
  1159. experiments: stateValues.experiments ?? experimentDefault,
  1160. autoApprovalEnabled: stateValues.autoApprovalEnabled ?? false,
  1161. customModes,
  1162. maxOpenTabsContext: stateValues.maxOpenTabsContext ?? 20,
  1163. maxWorkspaceFiles: stateValues.maxWorkspaceFiles ?? 200,
  1164. openRouterUseMiddleOutTransform: stateValues.openRouterUseMiddleOutTransform ?? true,
  1165. browserToolEnabled: stateValues.browserToolEnabled ?? true,
  1166. telemetrySetting: stateValues.telemetrySetting || "unset",
  1167. showRooIgnoredFiles: stateValues.showRooIgnoredFiles ?? true,
  1168. maxReadFileLine: stateValues.maxReadFileLine ?? 500,
  1169. }
  1170. }
  1171. async updateTaskHistory(item: HistoryItem): Promise<HistoryItem[]> {
  1172. const history = (this.getGlobalState("taskHistory") as HistoryItem[] | undefined) || []
  1173. const existingItemIndex = history.findIndex((h) => h.id === item.id)
  1174. if (existingItemIndex !== -1) {
  1175. history[existingItemIndex] = item
  1176. } else {
  1177. history.push(item)
  1178. }
  1179. await this.updateGlobalState("taskHistory", history)
  1180. return history
  1181. }
  1182. // ContextProxy
  1183. // @deprecated - Use `ContextProxy#setValue` instead.
  1184. private async updateGlobalState<K extends keyof GlobalState>(key: K, value: GlobalState[K]) {
  1185. await this.contextProxy.setValue(key, value)
  1186. }
  1187. // @deprecated - Use `ContextProxy#getValue` instead.
  1188. private getGlobalState<K extends keyof GlobalState>(key: K) {
  1189. return this.contextProxy.getValue(key)
  1190. }
  1191. public async setValue<K extends keyof RooCodeSettings>(key: K, value: RooCodeSettings[K]) {
  1192. await this.contextProxy.setValue(key, value)
  1193. }
  1194. public getValue<K extends keyof RooCodeSettings>(key: K) {
  1195. return this.contextProxy.getValue(key)
  1196. }
  1197. public getValues() {
  1198. return this.contextProxy.getValues()
  1199. }
  1200. public async setValues(values: RooCodeSettings) {
  1201. await this.contextProxy.setValues(values)
  1202. }
  1203. // cwd
  1204. get cwd() {
  1205. return getWorkspacePath()
  1206. }
  1207. // dev
  1208. async resetState() {
  1209. const answer = await vscode.window.showInformationMessage(
  1210. t("common:confirmation.reset_state"),
  1211. { modal: true },
  1212. t("common:answers.yes"),
  1213. )
  1214. if (answer !== t("common:answers.yes")) {
  1215. return
  1216. }
  1217. await this.contextProxy.resetAllState()
  1218. await this.providerSettingsManager.resetAllConfigs()
  1219. await this.customModesManager.resetCustomModes()
  1220. await this.removeClineFromStack()
  1221. await this.postStateToWebview()
  1222. await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
  1223. }
  1224. // logging
  1225. public log(message: string) {
  1226. this.outputChannel.appendLine(message)
  1227. console.log(message)
  1228. }
  1229. // integration tests
  1230. get viewLaunched() {
  1231. return this.isViewLaunched
  1232. }
  1233. get messages() {
  1234. return this.getCurrentCline()?.clineMessages || []
  1235. }
  1236. // Add public getter
  1237. public getMcpHub(): McpHub | undefined {
  1238. return this.mcpHub
  1239. }
  1240. /**
  1241. * Returns properties to be included in every telemetry event
  1242. * This method is called by the telemetry service to get context information
  1243. * like the current mode, API provider, etc.
  1244. */
  1245. public async getTelemetryProperties(): Promise<Record<string, any>> {
  1246. const { mode, apiConfiguration, language } = await this.getState()
  1247. const appVersion = this.context.extension?.packageJSON?.version
  1248. const vscodeVersion = vscode.version
  1249. const platform = process.platform
  1250. const properties: Record<string, any> = {
  1251. vscodeVersion,
  1252. platform,
  1253. }
  1254. // Add extension version
  1255. if (appVersion) {
  1256. properties.appVersion = appVersion
  1257. }
  1258. // Add language
  1259. if (language) {
  1260. properties.language = language
  1261. }
  1262. // Add current mode
  1263. if (mode) {
  1264. properties.mode = mode
  1265. }
  1266. // Add API provider
  1267. if (apiConfiguration?.apiProvider) {
  1268. properties.apiProvider = apiConfiguration.apiProvider
  1269. }
  1270. // Add model ID if available
  1271. const currentCline = this.getCurrentCline()
  1272. if (currentCline?.api) {
  1273. const { id: modelId } = currentCline.api.getModel()
  1274. if (modelId) {
  1275. properties.modelId = modelId
  1276. }
  1277. }
  1278. if (currentCline?.diffStrategy) {
  1279. properties.diffStrategy = currentCline.diffStrategy.getName()
  1280. }
  1281. return properties
  1282. }
  1283. }