| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505 |
- import os from "os"
- import * as path from "path"
- import fs from "fs/promises"
- import EventEmitter from "events"
- import { Anthropic } from "@anthropic-ai/sdk"
- import delay from "delay"
- import axios from "axios"
- import pWaitFor from "p-wait-for"
- import * as vscode from "vscode"
- import { GlobalState, ProviderSettings, RooCodeSettings } from "../../schemas"
- import { t } from "../../i18n"
- import { setPanel } from "../../activate/registerCommands"
- import {
- ApiConfiguration,
- ApiProvider,
- ModelInfo,
- requestyDefaultModelId,
- requestyDefaultModelInfo,
- openRouterDefaultModelId,
- openRouterDefaultModelInfo,
- glamaDefaultModelId,
- glamaDefaultModelInfo,
- } from "../../shared/api"
- import { findLast } from "../../shared/array"
- import { supportPrompt } from "../../shared/support-prompt"
- import { GlobalFileNames } from "../../shared/globalFileNames"
- import { HistoryItem } from "../../shared/HistoryItem"
- import { ExtensionMessage } from "../../shared/ExtensionMessage"
- import { Mode, PromptComponent, defaultModeSlug, getModeBySlug, getGroupName } from "../../shared/modes"
- import { experimentDefault } from "../../shared/experiments"
- import { formatLanguage } from "../../shared/language"
- import { Terminal, TERMINAL_SHELL_INTEGRATION_TIMEOUT } from "../../integrations/terminal/Terminal"
- import { downloadTask } from "../../integrations/misc/export-markdown"
- import { getTheme } from "../../integrations/theme/getTheme"
- import WorkspaceTracker from "../../integrations/workspace/WorkspaceTracker"
- import { McpHub } from "../../services/mcp/McpHub"
- import { McpServerManager } from "../../services/mcp/McpServerManager"
- import { ShadowCheckpointService } from "../../services/checkpoints/ShadowCheckpointService"
- import { fileExistsAtPath } from "../../utils/fs"
- import { setSoundEnabled } from "../../utils/sound"
- import { setTtsEnabled, setTtsSpeed } from "../../utils/tts"
- import { ContextProxy } from "../config/ContextProxy"
- import { ProviderSettingsManager } from "../config/ProviderSettingsManager"
- import { CustomModesManager } from "../config/CustomModesManager"
- import { buildApiHandler } from "../../api"
- import { ACTION_NAMES } from "../CodeActionProvider"
- import { Cline, ClineOptions } from "../Cline"
- import { getNonce } from "./getNonce"
- import { getUri } from "./getUri"
- import { telemetryService } from "../../services/telemetry/TelemetryService"
- import { getWorkspacePath } from "../../utils/path"
- import { webviewMessageHandler } from "./webviewMessageHandler"
- import { WebviewMessage } from "../../shared/WebviewMessage"
- /**
- * https://github.com/microsoft/vscode-webview-ui-toolkit-samples/blob/main/default/weather-webview/src/providers/WeatherViewProvider.ts
- * https://github.com/KumarVariable/vscode-extension-sidebar-html/blob/master/src/customSidebarViewProvider.ts
- */
- export type ClineProviderEvents = {
- clineCreated: [cline: Cline]
- }
- export class ClineProvider extends EventEmitter<ClineProviderEvents> implements vscode.WebviewViewProvider {
- 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.
- public static readonly tabPanelId = "roo-cline.TabPanelProvider"
- private static activeInstances: Set<ClineProvider> = new Set()
- private disposables: vscode.Disposable[] = []
- private view?: vscode.WebviewView | vscode.WebviewPanel
- private clineStack: Cline[] = []
- private _workspaceTracker?: WorkspaceTracker // workSpaceTracker read-only for access outside this class
- public get workspaceTracker(): WorkspaceTracker | undefined {
- return this._workspaceTracker
- }
- protected mcpHub?: McpHub // Change from private to protected
- public isViewLaunched = false
- public settingsImportedAt?: number
- public readonly latestAnnouncementId = "apr-04-2025-boomerang" // update for Boomerang Tasks announcement
- public readonly contextProxy: ContextProxy
- public readonly providerSettingsManager: ProviderSettingsManager
- public readonly customModesManager: CustomModesManager
- constructor(
- readonly context: vscode.ExtensionContext,
- private readonly outputChannel: vscode.OutputChannel,
- private readonly renderContext: "sidebar" | "editor" = "sidebar",
- ) {
- super()
- this.log("ClineProvider instantiated")
- this.contextProxy = new ContextProxy(context)
- ClineProvider.activeInstances.add(this)
- // Register this provider with the telemetry service to enable it to add
- // properties like mode and provider.
- telemetryService.setProvider(this)
- this._workspaceTracker = new WorkspaceTracker(this)
- this.providerSettingsManager = new ProviderSettingsManager(this.context)
- this.customModesManager = new CustomModesManager(this.context, async () => {
- await this.postStateToWebview()
- })
- // Initialize MCP Hub through the singleton manager
- McpServerManager.getInstance(this.context, this)
- .then((hub) => {
- this.mcpHub = hub
- this.mcpHub.registerClient()
- })
- .catch((error) => {
- this.log(`Failed to initialize MCP Hub: ${error}`)
- })
- }
- // Adds a new Cline instance to clineStack, marking the start of a new task.
- // The instance is pushed to the top of the stack (LIFO order).
- // When the task is completed, the top instance is removed, reactivating the previous task.
- async addClineToStack(cline: Cline) {
- console.log(`[subtasks] adding task ${cline.taskId}.${cline.instanceId} to stack`)
- // Add this cline instance into the stack that represents the order of all the called tasks.
- this.clineStack.push(cline)
- // Ensure getState() resolves correctly.
- const state = await this.getState()
- if (!state || typeof state.mode !== "string") {
- throw new Error(t("common:errors.retrieve_current_mode"))
- }
- }
- // Removes and destroys the top Cline instance (the current finished task),
- // activating the previous one (resuming the parent task).
- async removeClineFromStack() {
- if (this.clineStack.length === 0) {
- return
- }
- // Pop the top Cline instance from the stack.
- var cline = this.clineStack.pop()
- if (cline) {
- console.log(`[subtasks] removing task ${cline.taskId}.${cline.instanceId} from stack`)
- try {
- // Abort the running task and set isAbandoned to true so
- // all running promises will exit as well.
- await cline.abortTask(true)
- } catch (e) {
- this.log(
- `[subtasks] encountered error while aborting task ${cline.taskId}.${cline.instanceId}: ${e.message}`,
- )
- }
- // Make sure no reference kept, once promises end it will be
- // garbage collected.
- cline = undefined
- }
- }
- // returns the current cline object in the stack (the top one)
- // if the stack is empty, returns undefined
- getCurrentCline(): Cline | undefined {
- if (this.clineStack.length === 0) {
- return undefined
- }
- return this.clineStack[this.clineStack.length - 1]
- }
- // returns the current clineStack length (how many cline objects are in the stack)
- getClineStackSize(): number {
- return this.clineStack.length
- }
- public getCurrentTaskStack(): string[] {
- return this.clineStack.map((cline) => cline.taskId)
- }
- // remove the current task/cline instance (at the top of the stack), ao this task is finished
- // and resume the previous task/cline instance (if it exists)
- // this is used when a sub task is finished and the parent task needs to be resumed
- async finishSubTask(lastMessage?: string) {
- console.log(`[subtasks] finishing subtask ${lastMessage}`)
- // remove the last cline instance from the stack (this is the finished sub task)
- await this.removeClineFromStack()
- // resume the last cline instance in the stack (if it exists - this is the 'parnt' calling task)
- this.getCurrentCline()?.resumePausedTask(lastMessage)
- }
- /*
- 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.
- - https://vscode-docs.readthedocs.io/en/stable/extensions/patterns-and-principles/
- - https://github.com/microsoft/vscode-extension-samples/blob/main/webview-sample/src/extension.ts
- */
- async dispose() {
- this.log("Disposing ClineProvider...")
- await this.removeClineFromStack()
- this.log("Cleared task")
- if (this.view && "dispose" in this.view) {
- this.view.dispose()
- this.log("Disposed webview")
- }
- while (this.disposables.length) {
- const x = this.disposables.pop()
- if (x) {
- x.dispose()
- }
- }
- this._workspaceTracker?.dispose()
- this._workspaceTracker = undefined
- await this.mcpHub?.unregisterClient()
- this.mcpHub = undefined
- this.customModesManager?.dispose()
- this.log("Disposed all disposables")
- ClineProvider.activeInstances.delete(this)
- // Unregister from McpServerManager
- McpServerManager.unregisterProvider(this)
- }
- public static getVisibleInstance(): ClineProvider | undefined {
- return findLast(Array.from(this.activeInstances), (instance) => instance.view?.visible === true)
- }
- public static async getInstance(): Promise<ClineProvider | undefined> {
- let visibleProvider = ClineProvider.getVisibleInstance()
- // If no visible provider, try to show the sidebar view
- if (!visibleProvider) {
- await vscode.commands.executeCommand("roo-cline.SidebarProvider.focus")
- // Wait briefly for the view to become visible
- await delay(100)
- visibleProvider = ClineProvider.getVisibleInstance()
- }
- // If still no visible provider, return
- if (!visibleProvider) {
- return
- }
- return visibleProvider
- }
- public static async isActiveTask(): Promise<boolean> {
- const visibleProvider = await ClineProvider.getInstance()
- if (!visibleProvider) {
- return false
- }
- // check if there is a cline instance in the stack (if this provider has an active task)
- if (visibleProvider.getCurrentCline()) {
- return true
- }
- return false
- }
- public static async handleCodeAction(
- command: string,
- promptType: keyof typeof ACTION_NAMES,
- params: Record<string, string | any[]>,
- ): Promise<void> {
- const visibleProvider = await ClineProvider.getInstance()
- if (!visibleProvider) {
- return
- }
- const { customSupportPrompts } = await visibleProvider.getState()
- const prompt = supportPrompt.create(promptType, params, customSupportPrompts)
- if (command.endsWith("addToContext")) {
- await visibleProvider.postMessageToWebview({
- type: "invoke",
- invoke: "setChatBoxMessage",
- text: prompt,
- })
- return
- }
- if (visibleProvider.getCurrentCline() && command.endsWith("InCurrentTask")) {
- await visibleProvider.postMessageToWebview({ type: "invoke", invoke: "sendMessage", text: prompt })
- return
- }
- await visibleProvider.initClineWithTask(prompt)
- }
- public static async handleTerminalAction(
- command: string,
- promptType: "TERMINAL_ADD_TO_CONTEXT" | "TERMINAL_FIX" | "TERMINAL_EXPLAIN",
- params: Record<string, string | any[]>,
- ): Promise<void> {
- const visibleProvider = await ClineProvider.getInstance()
- if (!visibleProvider) {
- return
- }
- const { customSupportPrompts } = await visibleProvider.getState()
- const prompt = supportPrompt.create(promptType, params, customSupportPrompts)
- if (command.endsWith("AddToContext")) {
- await visibleProvider.postMessageToWebview({
- type: "invoke",
- invoke: "setChatBoxMessage",
- text: prompt,
- })
- return
- }
- if (visibleProvider.getCurrentCline() && command.endsWith("InCurrentTask")) {
- await visibleProvider.postMessageToWebview({
- type: "invoke",
- invoke: "sendMessage",
- text: prompt,
- })
- return
- }
- await visibleProvider.initClineWithTask(prompt)
- }
- async resolveWebviewView(webviewView: vscode.WebviewView | vscode.WebviewPanel) {
- this.log("Resolving webview view")
- if (!this.contextProxy.isInitialized) {
- await this.contextProxy.initialize()
- }
- this.view = webviewView
- // Set panel reference according to webview type
- if ("onDidChangeViewState" in webviewView) {
- // Tag page type
- setPanel(webviewView, "tab")
- } else if ("onDidChangeVisibility" in webviewView) {
- // Sidebar Type
- setPanel(webviewView, "sidebar")
- }
- // Initialize out-of-scope variables that need to recieve persistent global state values
- this.getState().then(({ soundEnabled, terminalShellIntegrationTimeout }) => {
- setSoundEnabled(soundEnabled ?? false)
- Terminal.setShellIntegrationTimeout(terminalShellIntegrationTimeout ?? TERMINAL_SHELL_INTEGRATION_TIMEOUT)
- })
- // Initialize tts enabled state
- this.getState().then(({ ttsEnabled }) => {
- setTtsEnabled(ttsEnabled ?? false)
- })
- // Initialize tts speed state
- this.getState().then(({ ttsSpeed }) => {
- setTtsSpeed(ttsSpeed ?? 1)
- })
- webviewView.webview.options = {
- // Allow scripts in the webview
- enableScripts: true,
- localResourceRoots: [this.contextProxy.extensionUri],
- }
- webviewView.webview.html =
- this.contextProxy.extensionMode === vscode.ExtensionMode.Development
- ? await this.getHMRHtmlContent(webviewView.webview)
- : this.getHtmlContent(webviewView.webview)
- // Sets up an event listener to listen for messages passed from the webview view context
- // and executes code based on the message that is recieved
- this.setWebviewMessageListener(webviewView.webview)
- // Logs show up in bottom panel > Debug Console
- //console.log("registering listener")
- // Listen for when the panel becomes visible
- // https://github.com/microsoft/vscode-discussions/discussions/840
- if ("onDidChangeViewState" in webviewView) {
- // WebviewView and WebviewPanel have all the same properties except for this visibility listener
- // panel
- webviewView.onDidChangeViewState(
- () => {
- if (this.view?.visible) {
- this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
- }
- },
- null,
- this.disposables,
- )
- } else if ("onDidChangeVisibility" in webviewView) {
- // sidebar
- webviewView.onDidChangeVisibility(
- () => {
- if (this.view?.visible) {
- this.postMessageToWebview({ type: "action", action: "didBecomeVisible" })
- }
- },
- null,
- this.disposables,
- )
- }
- // Listen for when the view is disposed
- // This happens when the user closes the view or when the view is closed programmatically
- webviewView.onDidDispose(
- async () => {
- await this.dispose()
- },
- null,
- this.disposables,
- )
- // Listen for when color changes
- vscode.workspace.onDidChangeConfiguration(
- async (e) => {
- if (e && e.affectsConfiguration("workbench.colorTheme")) {
- // Sends latest theme name to webview
- await this.postMessageToWebview({ type: "theme", text: JSON.stringify(await getTheme()) })
- }
- },
- null,
- this.disposables,
- )
- // If the extension is starting a new session, clear previous task state.
- await this.removeClineFromStack()
- this.log("Webview view resolved")
- }
- public async initClineWithSubTask(parent: Cline, task?: string, images?: string[]) {
- return this.initClineWithTask(task, images, parent)
- }
- // When initializing a new task, (not from history but from a tool command
- // new_task) there is no need to remove the previouse task since the new
- // task is a subtask of the previous one, and when it finishes it is removed
- // from the stack and the caller is resumed 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.
- public async initClineWithTask(
- task?: string,
- images?: string[],
- parentTask?: Cline,
- options: Partial<
- Pick<
- ClineOptions,
- | "customInstructions"
- | "enableDiff"
- | "enableCheckpoints"
- | "checkpointStorage"
- | "fuzzyMatchThreshold"
- | "consecutiveMistakeLimit"
- | "experiments"
- >
- > = {},
- ) {
- const {
- apiConfiguration,
- customModePrompts,
- diffEnabled: enableDiff,
- enableCheckpoints,
- checkpointStorage,
- fuzzyMatchThreshold,
- mode,
- customInstructions: globalInstructions,
- experiments,
- } = await this.getState()
- const modePrompt = customModePrompts?.[mode] as PromptComponent
- const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
- const cline = new Cline({
- provider: this,
- apiConfiguration,
- customInstructions: effectiveInstructions,
- enableDiff,
- enableCheckpoints,
- checkpointStorage,
- fuzzyMatchThreshold,
- task,
- images,
- experiments,
- rootTask: this.clineStack.length > 0 ? this.clineStack[0] : undefined,
- parentTask,
- taskNumber: this.clineStack.length + 1,
- onCreated: (cline) => this.emit("clineCreated", cline),
- ...options,
- })
- await this.addClineToStack(cline)
- this.log(
- `[subtasks] ${cline.parentTask ? "child" : "parent"} task ${cline.taskId}.${cline.instanceId} instantiated`,
- )
- return cline
- }
- public async initClineWithHistoryItem(historyItem: HistoryItem & { rootTask?: Cline; parentTask?: Cline }) {
- await this.removeClineFromStack()
- const {
- apiConfiguration,
- customModePrompts,
- diffEnabled: enableDiff,
- enableCheckpoints,
- checkpointStorage,
- fuzzyMatchThreshold,
- mode,
- customInstructions: globalInstructions,
- experiments,
- } = await this.getState()
- const modePrompt = customModePrompts?.[mode] as PromptComponent
- const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
- const taskId = historyItem.id
- const globalStorageDir = this.contextProxy.globalStorageUri.fsPath
- const workspaceDir = this.cwd
- const checkpoints: Pick<ClineOptions, "enableCheckpoints" | "checkpointStorage"> = {
- enableCheckpoints,
- checkpointStorage,
- }
- if (enableCheckpoints) {
- try {
- checkpoints.checkpointStorage = await ShadowCheckpointService.getTaskStorage({
- taskId,
- globalStorageDir,
- workspaceDir,
- })
- this.log(
- `[ClineProvider#initClineWithHistoryItem] Using ${checkpoints.checkpointStorage} storage for ${taskId}`,
- )
- } catch (error) {
- checkpoints.enableCheckpoints = false
- this.log(`[ClineProvider#initClineWithHistoryItem] Error getting task storage: ${error.message}`)
- }
- }
- const cline = new Cline({
- provider: this,
- apiConfiguration,
- customInstructions: effectiveInstructions,
- enableDiff,
- ...checkpoints,
- fuzzyMatchThreshold,
- historyItem,
- experiments,
- rootTask: historyItem.rootTask,
- parentTask: historyItem.parentTask,
- taskNumber: historyItem.number,
- onCreated: (cline) => this.emit("clineCreated", cline),
- })
- await this.addClineToStack(cline)
- this.log(
- `[subtasks] ${cline.parentTask ? "child" : "parent"} task ${cline.taskId}.${cline.instanceId} instantiated`,
- )
- return cline
- }
- public async postMessageToWebview(message: ExtensionMessage) {
- await this.view?.webview.postMessage(message)
- }
- private async getHMRHtmlContent(webview: vscode.Webview): Promise<string> {
- const localPort = "5173"
- const localServerUrl = `localhost:${localPort}`
- // Check if local dev server is running.
- try {
- await axios.get(`http://${localServerUrl}`)
- } catch (error) {
- vscode.window.showErrorMessage(t("common:errors.hmr_not_running"))
- return this.getHtmlContent(webview)
- }
- const nonce = getNonce()
- const stylesUri = getUri(webview, this.contextProxy.extensionUri, [
- "webview-ui",
- "build",
- "assets",
- "index.css",
- ])
- const codiconsUri = getUri(webview, this.contextProxy.extensionUri, [
- "node_modules",
- "@vscode",
- "codicons",
- "dist",
- "codicon.css",
- ])
- const imagesUri = getUri(webview, this.contextProxy.extensionUri, ["assets", "images"])
- const file = "src/index.tsx"
- const scriptUri = `http://${localServerUrl}/${file}`
- const reactRefresh = /*html*/ `
- <script nonce="${nonce}" type="module">
- import RefreshRuntime from "http://localhost:${localPort}/@react-refresh"
- RefreshRuntime.injectIntoGlobalHook(window)
- window.$RefreshReg$ = () => {}
- window.$RefreshSig$ = () => (type) => type
- window.__vite_plugin_react_preamble_installed__ = true
- </script>
- `
- const csp = [
- "default-src 'none'",
- `font-src ${webview.cspSource}`,
- `style-src ${webview.cspSource} 'unsafe-inline' https://* http://${localServerUrl} http://0.0.0.0:${localPort}`,
- `img-src ${webview.cspSource} data:`,
- `script-src 'unsafe-eval' ${webview.cspSource} https://* https://*.posthog.com http://${localServerUrl} http://0.0.0.0:${localPort} 'nonce-${nonce}'`,
- `connect-src https://* https://*.posthog.com ws://${localServerUrl} ws://0.0.0.0:${localPort} http://${localServerUrl} http://0.0.0.0:${localPort}`,
- ]
- return /*html*/ `
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
- <meta http-equiv="Content-Security-Policy" content="${csp.join("; ")}">
- <link rel="stylesheet" type="text/css" href="${stylesUri}">
- <link href="${codiconsUri}" rel="stylesheet" />
- <script nonce="${nonce}">
- window.IMAGES_BASE_URI = "${imagesUri}"
- </script>
- <title>Roo Code</title>
- </head>
- <body>
- <div id="root"></div>
- ${reactRefresh}
- <script type="module" src="${scriptUri}"></script>
- </body>
- </html>
- `
- }
- /**
- * Defines and returns the HTML that should be rendered within the webview panel.
- *
- * @remarks This is also the place where references to the React webview build files
- * are created and inserted into the webview HTML.
- *
- * @param webview A reference to the extension webview
- * @param extensionUri The URI of the directory containing the extension
- * @returns A template string literal containing the HTML that should be
- * rendered within the webview panel
- */
- private getHtmlContent(webview: vscode.Webview): string {
- // Get the local path to main script run in the webview,
- // then convert it to a uri we can use in the webview.
- // The CSS file from the React build output
- const stylesUri = getUri(webview, this.contextProxy.extensionUri, [
- "webview-ui",
- "build",
- "assets",
- "index.css",
- ])
- // The JS file from the React build output
- const scriptUri = getUri(webview, this.contextProxy.extensionUri, ["webview-ui", "build", "assets", "index.js"])
- // The codicon font from the React build output
- // https://github.com/microsoft/vscode-extension-samples/blob/main/webview-codicons-sample/src/extension.ts
- // 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
- // don't forget to add font-src ${webview.cspSource};
- const codiconsUri = getUri(webview, this.contextProxy.extensionUri, [
- "node_modules",
- "@vscode",
- "codicons",
- "dist",
- "codicon.css",
- ])
- const imagesUri = getUri(webview, this.contextProxy.extensionUri, ["assets", "images"])
- // const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.js"))
- // const styleResetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "reset.css"))
- // const styleVSCodeUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "vscode.css"))
- // // Same for stylesheet
- // const stylesheetUri = webview.asWebviewUri(vscode.Uri.joinPath(this._extensionUri, "assets", "main.css"))
- // Use a nonce to only allow a specific script to be run.
- /*
- content security policy of your webview to only allow scripts that have a specific nonce
- create a content security policy meta tag so that only loading scripts with a nonce is allowed
- 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.
- <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}';">
- - 'unsafe-inline' is required for styles due to vscode-webview-toolkit's dynamic style injection
- - since we pass base64 images to the webview, we need to specify img-src ${webview.cspSource} data:;
- 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.
- */
- const nonce = getNonce()
- // Tip: Install the es6-string-html VS Code extension to enable code highlighting below
- return /*html*/ `
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
- <meta name="theme-color" content="#000000">
- <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;">
- <link rel="stylesheet" type="text/css" href="${stylesUri}">
- <link href="${codiconsUri}" rel="stylesheet" />
- <script nonce="${nonce}">
- window.IMAGES_BASE_URI = "${imagesUri}"
- </script>
- <title>Roo Code</title>
- </head>
- <body>
- <noscript>You need to enable JavaScript to run this app.</noscript>
- <div id="root"></div>
- <script nonce="${nonce}" type="module" src="${scriptUri}"></script>
- </body>
- </html>
- `
- }
- /**
- * Sets up an event listener to listen for messages passed from the webview context and
- * executes code based on the message that is recieved.
- *
- * @param webview A reference to the extension webview
- */
- private setWebviewMessageListener(webview: vscode.Webview) {
- const onReceiveMessage = async (message: WebviewMessage) => webviewMessageHandler(this, message)
- webview.onDidReceiveMessage(onReceiveMessage, null, this.disposables)
- }
- /**
- * Handle switching to a new mode, including updating the associated API configuration
- * @param newMode The mode to switch to
- */
- public async handleModeSwitch(newMode: Mode) {
- // Capture mode switch telemetry event
- const cline = this.getCurrentCline()
- if (cline) {
- telemetryService.captureModeSwitch(cline.taskId, newMode)
- cline.emit("taskModeSwitched", cline.taskId, newMode)
- }
- await this.updateGlobalState("mode", newMode)
- // Load the saved API config for the new mode if it exists
- const savedConfigId = await this.providerSettingsManager.getModeConfigId(newMode)
- const listApiConfig = await this.providerSettingsManager.listConfig()
- // Update listApiConfigMeta first to ensure UI has latest data
- await this.updateGlobalState("listApiConfigMeta", listApiConfig)
- // If this mode has a saved config, use it
- if (savedConfigId) {
- const config = listApiConfig?.find((c) => c.id === savedConfigId)
- if (config?.name) {
- const apiConfig = await this.providerSettingsManager.loadConfig(config.name)
- await Promise.all([
- this.updateGlobalState("currentApiConfigName", config.name),
- this.updateApiConfiguration(apiConfig),
- ])
- }
- } else {
- // If no saved config for this mode, save current config as default
- const currentApiConfigName = this.getGlobalState("currentApiConfigName")
- if (currentApiConfigName) {
- const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
- if (config?.id) {
- await this.providerSettingsManager.setModeConfig(newMode, config.id)
- }
- }
- }
- await this.postStateToWebview()
- }
- async updateApiConfiguration(providerSettings: ProviderSettings) {
- // Update mode's default config.
- const { mode } = await this.getState()
- if (mode) {
- const currentApiConfigName = this.getGlobalState("currentApiConfigName")
- const listApiConfig = await this.providerSettingsManager.listConfig()
- const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
- if (config?.id) {
- await this.providerSettingsManager.setModeConfig(mode, config.id)
- }
- }
- await this.contextProxy.setProviderSettings(providerSettings)
- if (this.getCurrentCline()) {
- this.getCurrentCline()!.api = buildApiHandler(providerSettings)
- }
- }
- async cancelTask() {
- const cline = this.getCurrentCline()
- if (!cline) {
- return
- }
- console.log(`[subtasks] cancelling task ${cline.taskId}.${cline.instanceId}`)
- const { historyItem } = await this.getTaskWithId(cline.taskId)
- // Preserve parent and root task information for history item.
- const rootTask = cline.rootTask
- const parentTask = cline.parentTask
- cline.abortTask()
- await pWaitFor(
- () =>
- this.getCurrentCline()! === undefined ||
- this.getCurrentCline()!.isStreaming === false ||
- this.getCurrentCline()!.didFinishAbortingStream ||
- // If only the first chunk is processed, then there's no
- // need to wait for graceful abort (closes edits, browser,
- // etc).
- this.getCurrentCline()!.isWaitingForFirstChunk,
- {
- timeout: 3_000,
- },
- ).catch(() => {
- console.error("Failed to abort task")
- })
- if (this.getCurrentCline()) {
- // 'abandoned' will prevent this Cline instance from affecting
- // future Cline instances. This may happen if its hanging on a
- // streaming request.
- this.getCurrentCline()!.abandoned = true
- }
- // Clears task again, so we need to abortTask manually above.
- await this.initClineWithHistoryItem({ ...historyItem, rootTask, parentTask })
- }
- async updateCustomInstructions(instructions?: string) {
- // User may be clearing the field.
- await this.updateGlobalState("customInstructions", instructions || undefined)
- if (this.getCurrentCline()) {
- this.getCurrentCline()!.customInstructions = instructions || undefined
- }
- await this.postStateToWebview()
- }
- // MCP
- async ensureMcpServersDirectoryExists(): Promise<string> {
- // Get platform-specific application data directory
- let mcpServersDir: string
- if (process.platform === "win32") {
- // Windows: %APPDATA%\Roo-Code\MCP
- mcpServersDir = path.join(os.homedir(), "AppData", "Roaming", "Roo-Code", "MCP")
- } else if (process.platform === "darwin") {
- // macOS: ~/Documents/Cline/MCP
- mcpServersDir = path.join(os.homedir(), "Documents", "Cline", "MCP")
- } else {
- // Linux: ~/.local/share/Cline/MCP
- mcpServersDir = path.join(os.homedir(), ".local", "share", "Roo-Code", "MCP")
- }
- try {
- await fs.mkdir(mcpServersDir, { recursive: true })
- } catch (error) {
- // Fallback to a relative path if directory creation fails
- return path.join(os.homedir(), ".roo-code", "mcp")
- }
- return mcpServersDir
- }
- async ensureSettingsDirectoryExists(): Promise<string> {
- const { getSettingsDirectoryPath } = await import("../../shared/storagePathManager")
- const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
- return getSettingsDirectoryPath(globalStoragePath)
- }
- private async ensureCacheDirectoryExists() {
- const { getCacheDirectoryPath } = await import("../../shared/storagePathManager")
- const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
- return getCacheDirectoryPath(globalStoragePath)
- }
- async writeModelsToCache<T>(filename: string, data: T) {
- const cacheDir = await this.ensureCacheDirectoryExists()
- await fs.writeFile(path.join(cacheDir, filename), JSON.stringify(data))
- }
- async readModelsFromCache(filename: string): Promise<Record<string, ModelInfo> | undefined> {
- const filePath = path.join(await this.ensureCacheDirectoryExists(), filename)
- const fileExists = await fileExistsAtPath(filePath)
- if (fileExists) {
- const fileContents = await fs.readFile(filePath, "utf8")
- return JSON.parse(fileContents)
- }
- return undefined
- }
- // OpenRouter
- async handleOpenRouterCallback(code: string) {
- let { apiConfiguration, currentApiConfigName } = await this.getState()
- let apiKey: string
- try {
- const baseUrl = apiConfiguration.openRouterBaseUrl || "https://openrouter.ai/api/v1"
- // Extract the base domain for the auth endpoint
- const baseUrlDomain = baseUrl.match(/^(https?:\/\/[^\/]+)/)?.[1] || "https://openrouter.ai"
- const response = await axios.post(`${baseUrlDomain}/api/v1/auth/keys`, { code })
- if (response.data && response.data.key) {
- apiKey = response.data.key
- } else {
- throw new Error("Invalid response from OpenRouter API")
- }
- } catch (error) {
- this.log(
- `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- throw error
- }
- const newConfiguration: ApiConfiguration = {
- ...apiConfiguration,
- apiProvider: "openrouter",
- openRouterApiKey: apiKey,
- openRouterModelId: apiConfiguration?.openRouterModelId || openRouterDefaultModelId,
- openRouterModelInfo: apiConfiguration?.openRouterModelInfo || openRouterDefaultModelInfo,
- }
- await this.upsertApiConfiguration(currentApiConfigName, newConfiguration)
- }
- // Glama
- async handleGlamaCallback(code: string) {
- let apiKey: string
- try {
- const response = await axios.post("https://glama.ai/api/gateway/v1/auth/exchange-code", { code })
- if (response.data && response.data.apiKey) {
- apiKey = response.data.apiKey
- } else {
- throw new Error("Invalid response from Glama API")
- }
- } catch (error) {
- this.log(
- `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- throw error
- }
- const { apiConfiguration, currentApiConfigName } = await this.getState()
- const newConfiguration: ApiConfiguration = {
- ...apiConfiguration,
- apiProvider: "glama",
- glamaApiKey: apiKey,
- glamaModelId: apiConfiguration?.glamaModelId || glamaDefaultModelId,
- glamaModelInfo: apiConfiguration?.glamaModelInfo || glamaDefaultModelInfo,
- }
- await this.upsertApiConfiguration(currentApiConfigName, newConfiguration)
- }
- // Requesty
- async handleRequestyCallback(code: string) {
- let { apiConfiguration, currentApiConfigName } = await this.getState()
- const newConfiguration: ApiConfiguration = {
- ...apiConfiguration,
- apiProvider: "requesty",
- requestyApiKey: code,
- requestyModelId: apiConfiguration?.requestyModelId || requestyDefaultModelId,
- requestyModelInfo: apiConfiguration?.requestyModelInfo || requestyDefaultModelInfo,
- }
- await this.upsertApiConfiguration(currentApiConfigName, newConfiguration)
- }
- // Save configuration
- async upsertApiConfiguration(configName: string, apiConfiguration: ApiConfiguration) {
- try {
- await this.providerSettingsManager.saveConfig(configName, apiConfiguration)
- const listApiConfig = await this.providerSettingsManager.listConfig()
- await Promise.all([
- this.updateGlobalState("listApiConfigMeta", listApiConfig),
- this.updateApiConfiguration(apiConfiguration),
- this.updateGlobalState("currentApiConfigName", configName),
- ])
- await this.postStateToWebview()
- } catch (error) {
- this.log(
- `Error create new api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage(t("common:errors.create_api_config"))
- }
- }
- // Task history
- async getTaskWithId(id: string): Promise<{
- historyItem: HistoryItem
- taskDirPath: string
- apiConversationHistoryFilePath: string
- uiMessagesFilePath: string
- apiConversationHistory: Anthropic.MessageParam[]
- }> {
- const history = this.getGlobalState("taskHistory") ?? []
- const historyItem = history.find((item) => item.id === id)
- if (historyItem) {
- const { getTaskDirectoryPath } = await import("../../shared/storagePathManager")
- const globalStoragePath = this.contextProxy.globalStorageUri.fsPath
- const taskDirPath = await getTaskDirectoryPath(globalStoragePath, id)
- const apiConversationHistoryFilePath = path.join(taskDirPath, GlobalFileNames.apiConversationHistory)
- const uiMessagesFilePath = path.join(taskDirPath, GlobalFileNames.uiMessages)
- const fileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
- if (fileExists) {
- const apiConversationHistory = JSON.parse(await fs.readFile(apiConversationHistoryFilePath, "utf8"))
- return {
- historyItem,
- taskDirPath,
- apiConversationHistoryFilePath,
- uiMessagesFilePath,
- apiConversationHistory,
- }
- }
- }
- // if we tried to get a task that doesn't exist, remove it from state
- // FIXME: this seems to happen sometimes when the json file doesnt save to disk for some reason
- await this.deleteTaskFromState(id)
- throw new Error("Task not found")
- }
- async showTaskWithId(id: string) {
- if (id !== this.getCurrentCline()?.taskId) {
- // Non-current task.
- const { historyItem } = await this.getTaskWithId(id)
- await this.initClineWithHistoryItem(historyItem) // Clears existing task.
- }
- await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
- }
- async exportTaskWithId(id: string) {
- const { historyItem, apiConversationHistory } = await this.getTaskWithId(id)
- await downloadTask(historyItem.ts, apiConversationHistory)
- }
- // this function deletes a task from task hidtory, and deletes it's checkpoints and delete the task folder
- async deleteTaskWithId(id: string) {
- try {
- // get the task directory full path
- const { taskDirPath } = await this.getTaskWithId(id)
- // remove task from stack if it's the current task
- if (id === this.getCurrentCline()?.taskId) {
- // if we found the taskid to delete - call finish to abort this task and allow a new task to be started,
- // 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)
- await this.finishSubTask(t("common:tasks.deleted"))
- }
- // delete task from the task history state
- await this.deleteTaskFromState(id)
- // Delete associated shadow repository or branch.
- // TODO: Store `workspaceDir` in the `HistoryItem` object.
- const globalStorageDir = this.contextProxy.globalStorageUri.fsPath
- const workspaceDir = this.cwd
- try {
- await ShadowCheckpointService.deleteTask({ taskId: id, globalStorageDir, workspaceDir })
- } catch (error) {
- console.error(
- `[deleteTaskWithId${id}] failed to delete associated shadow repository or branch: ${error instanceof Error ? error.message : String(error)}`,
- )
- }
- // delete the entire task directory including checkpoints and all content
- try {
- await fs.rm(taskDirPath, { recursive: true, force: true })
- console.log(`[deleteTaskWithId${id}] removed task directory`)
- } catch (error) {
- console.error(
- `[deleteTaskWithId${id}] failed to remove task directory: ${error instanceof Error ? error.message : String(error)}`,
- )
- }
- } catch (error) {
- // If task is not found, just remove it from state
- if (error instanceof Error && error.message === "Task not found") {
- await this.deleteTaskFromState(id)
- return
- }
- throw error
- }
- }
- async deleteTaskFromState(id: string) {
- const taskHistory = this.getGlobalState("taskHistory") ?? []
- const updatedTaskHistory = taskHistory.filter((task) => task.id !== id)
- await this.updateGlobalState("taskHistory", updatedTaskHistory)
- await this.postStateToWebview()
- }
- async postStateToWebview() {
- const state = await this.getStateToPostToWebview()
- this.postMessageToWebview({ type: "state", state })
- }
- async getStateToPostToWebview() {
- const {
- apiConfiguration,
- lastShownAnnouncementId,
- customInstructions,
- alwaysAllowReadOnly,
- alwaysAllowReadOnlyOutsideWorkspace,
- alwaysAllowWrite,
- alwaysAllowWriteOutsideWorkspace,
- alwaysAllowExecute,
- alwaysAllowBrowser,
- alwaysAllowMcp,
- alwaysAllowModeSwitch,
- alwaysAllowSubtasks,
- soundEnabled,
- ttsEnabled,
- ttsSpeed,
- diffEnabled,
- enableCheckpoints,
- checkpointStorage,
- taskHistory,
- soundVolume,
- browserViewportSize,
- screenshotQuality,
- remoteBrowserHost,
- remoteBrowserEnabled,
- cachedChromeHostUrl,
- writeDelayMs,
- terminalOutputLineLimit,
- terminalShellIntegrationTimeout,
- fuzzyMatchThreshold,
- mcpEnabled,
- enableMcpServerCreation,
- alwaysApproveResubmit,
- requestDelaySeconds,
- rateLimitSeconds,
- currentApiConfigName,
- listApiConfigMeta,
- pinnedApiConfigs,
- mode,
- customModePrompts,
- customSupportPrompts,
- enhancementApiConfigId,
- autoApprovalEnabled,
- experiments,
- maxOpenTabsContext,
- maxWorkspaceFiles,
- browserToolEnabled,
- telemetrySetting,
- showRooIgnoredFiles,
- language,
- maxReadFileLine,
- } = await this.getState()
- const telemetryKey = process.env.POSTHOG_API_KEY
- const machineId = vscode.env.machineId
- const allowedCommands = vscode.workspace.getConfiguration("roo-cline").get<string[]>("allowedCommands") || []
- const cwd = this.cwd
- return {
- version: this.context.extension?.packageJSON?.version ?? "",
- apiConfiguration,
- customInstructions,
- alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
- alwaysAllowReadOnlyOutsideWorkspace: alwaysAllowReadOnlyOutsideWorkspace ?? false,
- alwaysAllowWrite: alwaysAllowWrite ?? false,
- alwaysAllowWriteOutsideWorkspace: alwaysAllowWriteOutsideWorkspace ?? false,
- alwaysAllowExecute: alwaysAllowExecute ?? false,
- alwaysAllowBrowser: alwaysAllowBrowser ?? false,
- alwaysAllowMcp: alwaysAllowMcp ?? false,
- alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false,
- alwaysAllowSubtasks: alwaysAllowSubtasks ?? false,
- uriScheme: vscode.env.uriScheme,
- currentTaskItem: this.getCurrentCline()?.taskId
- ? (taskHistory || []).find((item: HistoryItem) => item.id === this.getCurrentCline()?.taskId)
- : undefined,
- clineMessages: this.getCurrentCline()?.clineMessages || [],
- taskHistory: (taskHistory || [])
- .filter((item: HistoryItem) => item.ts && item.task)
- .sort((a: HistoryItem, b: HistoryItem) => b.ts - a.ts),
- soundEnabled: soundEnabled ?? false,
- ttsEnabled: ttsEnabled ?? false,
- ttsSpeed: ttsSpeed ?? 1.0,
- diffEnabled: diffEnabled ?? true,
- enableCheckpoints: enableCheckpoints ?? true,
- checkpointStorage: checkpointStorage ?? "task",
- shouldShowAnnouncement:
- telemetrySetting !== "unset" && lastShownAnnouncementId !== this.latestAnnouncementId,
- allowedCommands,
- soundVolume: soundVolume ?? 0.5,
- browserViewportSize: browserViewportSize ?? "900x600",
- screenshotQuality: screenshotQuality ?? 75,
- remoteBrowserHost,
- remoteBrowserEnabled: remoteBrowserEnabled ?? false,
- cachedChromeHostUrl: cachedChromeHostUrl,
- writeDelayMs: writeDelayMs ?? 1000,
- terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
- terminalShellIntegrationTimeout: terminalShellIntegrationTimeout ?? TERMINAL_SHELL_INTEGRATION_TIMEOUT,
- fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
- mcpEnabled: mcpEnabled ?? true,
- enableMcpServerCreation: enableMcpServerCreation ?? true,
- alwaysApproveResubmit: alwaysApproveResubmit ?? false,
- requestDelaySeconds: requestDelaySeconds ?? 10,
- rateLimitSeconds: rateLimitSeconds ?? 0,
- currentApiConfigName: currentApiConfigName ?? "default",
- listApiConfigMeta: listApiConfigMeta ?? [],
- pinnedApiConfigs: pinnedApiConfigs ?? {},
- mode: mode ?? defaultModeSlug,
- customModePrompts: customModePrompts ?? {},
- customSupportPrompts: customSupportPrompts ?? {},
- enhancementApiConfigId,
- autoApprovalEnabled: autoApprovalEnabled ?? false,
- customModes: await this.customModesManager.getCustomModes(),
- experiments: experiments ?? experimentDefault,
- mcpServers: this.mcpHub?.getAllServers() ?? [],
- maxOpenTabsContext: maxOpenTabsContext ?? 20,
- maxWorkspaceFiles: maxWorkspaceFiles ?? 200,
- cwd,
- browserToolEnabled: browserToolEnabled ?? true,
- telemetrySetting,
- telemetryKey,
- machineId,
- showRooIgnoredFiles: showRooIgnoredFiles ?? true,
- language,
- renderContext: this.renderContext,
- maxReadFileLine: maxReadFileLine ?? 500,
- settingsImportedAt: this.settingsImportedAt,
- }
- }
- /**
- * Storage
- * https://dev.to/kompotkot/how-to-use-secretstorage-in-your-vscode-extensions-2hco
- * https://www.eliostruyf.com/devhack-code-extension-storage-options/
- */
- async getState() {
- const stateValues = this.contextProxy.getValues()
- const customModes = await this.customModesManager.getCustomModes()
- // Determine apiProvider with the same logic as before.
- const apiProvider: ApiProvider = stateValues.apiProvider ? stateValues.apiProvider : "anthropic"
- // Build the apiConfiguration object combining state values and secrets.
- const providerSettings = this.contextProxy.getProviderSettings()
- // Ensure apiProvider is set properly if not already in state
- if (!providerSettings.apiProvider) {
- providerSettings.apiProvider = apiProvider
- }
- // Return the same structure as before
- return {
- apiConfiguration: providerSettings,
- lastShownAnnouncementId: stateValues.lastShownAnnouncementId,
- customInstructions: stateValues.customInstructions,
- alwaysAllowReadOnly: stateValues.alwaysAllowReadOnly ?? false,
- alwaysAllowReadOnlyOutsideWorkspace: stateValues.alwaysAllowReadOnlyOutsideWorkspace ?? false,
- alwaysAllowWrite: stateValues.alwaysAllowWrite ?? false,
- alwaysAllowWriteOutsideWorkspace: stateValues.alwaysAllowWriteOutsideWorkspace ?? false,
- alwaysAllowExecute: stateValues.alwaysAllowExecute ?? false,
- alwaysAllowBrowser: stateValues.alwaysAllowBrowser ?? false,
- alwaysAllowMcp: stateValues.alwaysAllowMcp ?? false,
- alwaysAllowModeSwitch: stateValues.alwaysAllowModeSwitch ?? false,
- alwaysAllowSubtasks: stateValues.alwaysAllowSubtasks ?? false,
- taskHistory: stateValues.taskHistory,
- allowedCommands: stateValues.allowedCommands,
- soundEnabled: stateValues.soundEnabled ?? false,
- ttsEnabled: stateValues.ttsEnabled ?? false,
- ttsSpeed: stateValues.ttsSpeed ?? 1.0,
- diffEnabled: stateValues.diffEnabled ?? true,
- enableCheckpoints: stateValues.enableCheckpoints ?? true,
- checkpointStorage: stateValues.checkpointStorage ?? "task",
- soundVolume: stateValues.soundVolume,
- browserViewportSize: stateValues.browserViewportSize ?? "900x600",
- screenshotQuality: stateValues.screenshotQuality ?? 75,
- remoteBrowserHost: stateValues.remoteBrowserHost,
- remoteBrowserEnabled: stateValues.remoteBrowserEnabled ?? false,
- cachedChromeHostUrl: stateValues.cachedChromeHostUrl as string | undefined,
- fuzzyMatchThreshold: stateValues.fuzzyMatchThreshold ?? 1.0,
- writeDelayMs: stateValues.writeDelayMs ?? 1000,
- terminalOutputLineLimit: stateValues.terminalOutputLineLimit ?? 500,
- terminalShellIntegrationTimeout:
- stateValues.terminalShellIntegrationTimeout ?? TERMINAL_SHELL_INTEGRATION_TIMEOUT,
- mode: stateValues.mode ?? defaultModeSlug,
- language: stateValues.language ?? formatLanguage(vscode.env.language),
- mcpEnabled: stateValues.mcpEnabled ?? true,
- enableMcpServerCreation: stateValues.enableMcpServerCreation ?? true,
- alwaysApproveResubmit: stateValues.alwaysApproveResubmit ?? false,
- requestDelaySeconds: Math.max(5, stateValues.requestDelaySeconds ?? 10),
- rateLimitSeconds: stateValues.rateLimitSeconds ?? 0,
- currentApiConfigName: stateValues.currentApiConfigName ?? "default",
- listApiConfigMeta: stateValues.listApiConfigMeta ?? [],
- pinnedApiConfigs: stateValues.pinnedApiConfigs ?? {},
- modeApiConfigs: stateValues.modeApiConfigs ?? ({} as Record<Mode, string>),
- customModePrompts: stateValues.customModePrompts ?? {},
- customSupportPrompts: stateValues.customSupportPrompts ?? {},
- enhancementApiConfigId: stateValues.enhancementApiConfigId,
- experiments: stateValues.experiments ?? experimentDefault,
- autoApprovalEnabled: stateValues.autoApprovalEnabled ?? false,
- customModes,
- maxOpenTabsContext: stateValues.maxOpenTabsContext ?? 20,
- maxWorkspaceFiles: stateValues.maxWorkspaceFiles ?? 200,
- openRouterUseMiddleOutTransform: stateValues.openRouterUseMiddleOutTransform ?? true,
- browserToolEnabled: stateValues.browserToolEnabled ?? true,
- telemetrySetting: stateValues.telemetrySetting || "unset",
- showRooIgnoredFiles: stateValues.showRooIgnoredFiles ?? true,
- maxReadFileLine: stateValues.maxReadFileLine ?? 500,
- }
- }
- async updateTaskHistory(item: HistoryItem): Promise<HistoryItem[]> {
- const history = (this.getGlobalState("taskHistory") as HistoryItem[] | undefined) || []
- const existingItemIndex = history.findIndex((h) => h.id === item.id)
- if (existingItemIndex !== -1) {
- history[existingItemIndex] = item
- } else {
- history.push(item)
- }
- await this.updateGlobalState("taskHistory", history)
- return history
- }
- // ContextProxy
- // @deprecated - Use `ContextProxy#setValue` instead.
- private async updateGlobalState<K extends keyof GlobalState>(key: K, value: GlobalState[K]) {
- await this.contextProxy.setValue(key, value)
- }
- // @deprecated - Use `ContextProxy#getValue` instead.
- private getGlobalState<K extends keyof GlobalState>(key: K) {
- return this.contextProxy.getValue(key)
- }
- public async setValue<K extends keyof RooCodeSettings>(key: K, value: RooCodeSettings[K]) {
- await this.contextProxy.setValue(key, value)
- }
- public getValue<K extends keyof RooCodeSettings>(key: K) {
- return this.contextProxy.getValue(key)
- }
- public getValues() {
- return this.contextProxy.getValues()
- }
- public async setValues(values: RooCodeSettings) {
- await this.contextProxy.setValues(values)
- }
- // cwd
- get cwd() {
- return getWorkspacePath()
- }
- // dev
- async resetState() {
- const answer = await vscode.window.showInformationMessage(
- t("common:confirmation.reset_state"),
- { modal: true },
- t("common:answers.yes"),
- )
- if (answer !== t("common:answers.yes")) {
- return
- }
- await this.contextProxy.resetAllState()
- await this.providerSettingsManager.resetAllConfigs()
- await this.customModesManager.resetCustomModes()
- await this.removeClineFromStack()
- await this.postStateToWebview()
- await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
- }
- // logging
- public log(message: string) {
- this.outputChannel.appendLine(message)
- console.log(message)
- }
- // integration tests
- get viewLaunched() {
- return this.isViewLaunched
- }
- get messages() {
- return this.getCurrentCline()?.clineMessages || []
- }
- // Add public getter
- public getMcpHub(): McpHub | undefined {
- return this.mcpHub
- }
- /**
- * Returns properties to be included in every telemetry event
- * This method is called by the telemetry service to get context information
- * like the current mode, API provider, etc.
- */
- public async getTelemetryProperties(): Promise<Record<string, any>> {
- const { mode, apiConfiguration, language } = await this.getState()
- const appVersion = this.context.extension?.packageJSON?.version
- const vscodeVersion = vscode.version
- const platform = process.platform
- const properties: Record<string, any> = {
- vscodeVersion,
- platform,
- }
- // Add extension version
- if (appVersion) {
- properties.appVersion = appVersion
- }
- // Add language
- if (language) {
- properties.language = language
- }
- // Add current mode
- if (mode) {
- properties.mode = mode
- }
- // Add API provider
- if (apiConfiguration?.apiProvider) {
- properties.apiProvider = apiConfiguration.apiProvider
- }
- // Add model ID if available
- const currentCline = this.getCurrentCline()
- if (currentCline?.api) {
- const { id: modelId } = currentCline.api.getModel()
- if (modelId) {
- properties.modelId = modelId
- }
- }
- if (currentCline?.diffStrategy) {
- properties.diffStrategy = currentCline.diffStrategy.getName()
- }
- return properties
- }
- }
|