| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537 |
- import { Anthropic } from "@anthropic-ai/sdk"
- import delay from "delay"
- import axios from "axios"
- import fs from "fs/promises"
- import os from "os"
- import pWaitFor from "p-wait-for"
- import * as path from "path"
- import * as vscode from "vscode"
- import simpleGit from "simple-git"
- import { ApiConfiguration, ApiProvider, ModelInfo } from "../../shared/api"
- import { findLast } from "../../shared/array"
- import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt"
- import { GlobalFileNames } from "../../shared/globalFileNames"
- import type { SecretKey, GlobalStateKey } from "../../shared/globalState"
- import { HistoryItem } from "../../shared/HistoryItem"
- import { ApiConfigMeta, ExtensionMessage } from "../../shared/ExtensionMessage"
- import { checkoutDiffPayloadSchema, checkoutRestorePayloadSchema, WebviewMessage } from "../../shared/WebviewMessage"
- import { Mode, CustomModePrompts, PromptComponent, defaultModeSlug } from "../../shared/modes"
- import { checkExistKey } from "../../shared/checkExistApiConfig"
- import { EXPERIMENT_IDS, experiments as Experiments, experimentDefault, ExperimentId } from "../../shared/experiments"
- import { downloadTask } from "../../integrations/misc/export-markdown"
- import { openFile, openImage } from "../../integrations/misc/open-file"
- import { selectImages } from "../../integrations/misc/process-images"
- 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 { fileExistsAtPath } from "../../utils/fs"
- import { playSound, setSoundEnabled, setSoundVolume } from "../../utils/sound"
- import { singleCompletionHandler } from "../../utils/single-completion-handler"
- import { searchCommits } from "../../utils/git"
- import { getDiffStrategy } from "../diff/DiffStrategy"
- import { SYSTEM_PROMPT } from "../prompts/system"
- import { ConfigManager } from "../config/ConfigManager"
- import { CustomModesManager } from "../config/CustomModesManager"
- import { buildApiHandler } from "../../api"
- import { getOpenRouterModels } from "../../api/providers/openrouter"
- import { getGlamaModels } from "../../api/providers/glama"
- import { getUnboundModels } from "../../api/providers/unbound"
- import { getRequestyModels } from "../../api/providers/requesty"
- import { getOpenAiModels } from "../../api/providers/openai"
- import { getOllamaModels } from "../../api/providers/ollama"
- import { getVsCodeLmModels } from "../../api/providers/vscode-lm"
- import { getLmStudioModels } from "../../api/providers/lmstudio"
- import { ACTION_NAMES } from "../CodeActionProvider"
- import { Cline } from "../Cline"
- import { openMention } from "../mentions"
- import { getNonce } from "./getNonce"
- import { getUri } from "./getUri"
- /**
- * 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 class ClineProvider 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 isViewLaunched = false
- private cline?: Cline
- private workspaceTracker?: WorkspaceTracker
- protected mcpHub?: McpHub // Change from private to protected
- private latestAnnouncementId = "feb-27-2025-automatic-checkpoints" // update to some unique identifier when we add a new announcement
- configManager: ConfigManager
- customModesManager: CustomModesManager
- constructor(
- readonly context: vscode.ExtensionContext,
- private readonly outputChannel: vscode.OutputChannel,
- ) {
- this.outputChannel.appendLine("ClineProvider instantiated")
- ClineProvider.activeInstances.add(this)
- this.workspaceTracker = new WorkspaceTracker(this)
- this.configManager = new ConfigManager(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
- })
- .catch((error) => {
- this.outputChannel.appendLine(`Failed to initialize MCP Hub: ${error}`)
- })
- }
- /*
- 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.outputChannel.appendLine("Disposing ClineProvider...")
- await this.clearTask()
- this.outputChannel.appendLine("Cleared task")
- if (this.view && "dispose" in this.view) {
- this.view.dispose()
- this.outputChannel.appendLine("Disposed webview")
- }
- while (this.disposables.length) {
- const x = this.disposables.pop()
- if (x) {
- x.dispose()
- }
- }
- this.workspaceTracker?.dispose()
- this.workspaceTracker = undefined
- this.mcpHub?.dispose()
- this.mcpHub = undefined
- this.customModesManager?.dispose()
- this.outputChannel.appendLine("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
- }
- if (visibleProvider.cline) {
- 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.cline && 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.cline && 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.outputChannel.appendLine("Resolving webview view")
- this.view = webviewView
- // Initialize sound enabled state
- this.getState().then(({ soundEnabled }) => {
- setSoundEnabled(soundEnabled ?? false)
- })
- webviewView.webview.options = {
- // Allow scripts in the webview
- enableScripts: true,
- localResourceRoots: [this.context.extensionUri],
- }
- webviewView.webview.html =
- this.context.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
- this.clearTask()
- this.outputChannel.appendLine("Webview view resolved")
- }
- public async initClineWithTask(task?: string, images?: string[]) {
- await this.clearTask()
- const {
- apiConfiguration,
- customModePrompts,
- diffEnabled,
- enableCheckpoints,
- fuzzyMatchThreshold,
- mode,
- customInstructions: globalInstructions,
- experiments,
- } = await this.getState()
- const modePrompt = customModePrompts?.[mode] as PromptComponent
- const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
- this.cline = new Cline({
- provider: this,
- apiConfiguration,
- customInstructions: effectiveInstructions,
- enableDiff: diffEnabled,
- enableCheckpoints,
- fuzzyMatchThreshold,
- task,
- images,
- experiments,
- })
- }
- public async initClineWithHistoryItem(historyItem: HistoryItem) {
- await this.clearTask()
- const {
- apiConfiguration,
- customModePrompts,
- diffEnabled,
- enableCheckpoints,
- fuzzyMatchThreshold,
- mode,
- customInstructions: globalInstructions,
- experiments,
- } = await this.getState()
- const modePrompt = customModePrompts?.[mode] as PromptComponent
- const effectiveInstructions = [globalInstructions, modePrompt?.customInstructions].filter(Boolean).join("\n\n")
- this.cline = new Cline({
- provider: this,
- apiConfiguration,
- customInstructions: effectiveInstructions,
- enableDiff: diffEnabled,
- enableCheckpoints,
- fuzzyMatchThreshold,
- historyItem,
- experiments,
- })
- }
- 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(
- "Local development server is not running, HMR will not work. Please run 'npm run dev' before launching the extension to enable HMR.",
- )
- return this.getHtmlContent(webview)
- }
- const nonce = getNonce()
- const stylesUri = getUri(webview, this.context.extensionUri, ["webview-ui", "build", "assets", "index.css"])
- const codiconsUri = getUri(webview, this.context.extensionUri, [
- "node_modules",
- "@vscode",
- "codicons",
- "dist",
- "codicon.css",
- ])
- 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' https://* http://${localServerUrl} http://0.0.0.0:${localPort} 'nonce-${nonce}'`,
- `connect-src https://* 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" />
- <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.context.extensionUri, ["webview-ui", "build", "assets", "index.css"])
- // The JS file from the React build output
- const scriptUri = getUri(webview, this.context.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.context.extensionUri, [
- "node_modules",
- "@vscode",
- "codicons",
- "dist",
- "codicon.css",
- ])
- // 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}';">
- <link rel="stylesheet" type="text/css" href="${stylesUri}">
- <link href="${codiconsUri}" rel="stylesheet" />
- <title>Roo Code</title>
- </head>
- <body>
- <noscript>You need to enable JavaScript to run this app.</noscript>
- <div id="root"></div>
- <script nonce="${nonce}" 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) {
- webview.onDidReceiveMessage(
- async (message: WebviewMessage) => {
- switch (message.type) {
- case "webviewDidLaunch":
- // Load custom modes first
- const customModes = await this.customModesManager.getCustomModes()
- await this.updateGlobalState("customModes", customModes)
- this.postStateToWebview()
- this.workspaceTracker?.initializeFilePaths() // don't await
- getTheme().then((theme) =>
- this.postMessageToWebview({ type: "theme", text: JSON.stringify(theme) }),
- )
- // If MCP Hub is already initialized, update the webview with current server list
- if (this.mcpHub) {
- this.postMessageToWebview({
- type: "mcpServers",
- mcpServers: this.mcpHub.getAllServers(),
- })
- }
- const cacheDir = await this.ensureCacheDirectoryExists()
- // Post last cached models in case the call to endpoint fails.
- this.readModelsFromCache(GlobalFileNames.openRouterModels).then((openRouterModels) => {
- if (openRouterModels) {
- this.postMessageToWebview({ type: "openRouterModels", openRouterModels })
- }
- })
- // GUI relies on model info to be up-to-date to provide
- // the most accurate pricing, so we need to fetch the
- // latest details on launch.
- // We do this for all users since many users switch
- // between api providers and if they were to switch back
- // to OpenRouter it would be showing outdated model info
- // if we hadn't retrieved the latest at this point
- // (see normalizeApiConfiguration > openrouter).
- getOpenRouterModels().then(async (openRouterModels) => {
- if (Object.keys(openRouterModels).length > 0) {
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.openRouterModels),
- JSON.stringify(openRouterModels),
- )
- await this.postMessageToWebview({ type: "openRouterModels", openRouterModels })
- // Update model info in state (this needs to be
- // done here since we don't want to update state
- // while settings is open, and we may refresh
- // models there).
- const { apiConfiguration } = await this.getState()
- if (apiConfiguration.openRouterModelId) {
- await this.updateGlobalState(
- "openRouterModelInfo",
- openRouterModels[apiConfiguration.openRouterModelId],
- )
- await this.postStateToWebview()
- }
- }
- })
- this.readModelsFromCache(GlobalFileNames.glamaModels).then((glamaModels) => {
- if (glamaModels) {
- this.postMessageToWebview({ type: "glamaModels", glamaModels })
- }
- })
- getGlamaModels().then(async (glamaModels) => {
- if (Object.keys(glamaModels).length > 0) {
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.glamaModels),
- JSON.stringify(glamaModels),
- )
- await this.postMessageToWebview({ type: "glamaModels", glamaModels })
- const { apiConfiguration } = await this.getState()
- if (apiConfiguration.glamaModelId) {
- await this.updateGlobalState(
- "glamaModelInfo",
- glamaModels[apiConfiguration.glamaModelId],
- )
- await this.postStateToWebview()
- }
- }
- })
- this.readModelsFromCache(GlobalFileNames.unboundModels).then((unboundModels) => {
- if (unboundModels) {
- this.postMessageToWebview({ type: "unboundModels", unboundModels })
- }
- })
- getUnboundModels().then(async (unboundModels) => {
- if (Object.keys(unboundModels).length > 0) {
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.unboundModels),
- JSON.stringify(unboundModels),
- )
- await this.postMessageToWebview({ type: "unboundModels", unboundModels })
- const { apiConfiguration } = await this.getState()
- if (apiConfiguration?.unboundModelId) {
- await this.updateGlobalState(
- "unboundModelInfo",
- unboundModels[apiConfiguration.unboundModelId],
- )
- await this.postStateToWebview()
- }
- }
- })
- this.readModelsFromCache(GlobalFileNames.requestyModels).then((requestyModels) => {
- if (requestyModels) {
- this.postMessageToWebview({ type: "requestyModels", requestyModels })
- }
- })
- getRequestyModels().then(async (requestyModels) => {
- if (Object.keys(requestyModels).length > 0) {
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.requestyModels),
- JSON.stringify(requestyModels),
- )
- await this.postMessageToWebview({ type: "requestyModels", requestyModels })
- const { apiConfiguration } = await this.getState()
- if (apiConfiguration.requestyModelId) {
- await this.updateGlobalState(
- "requestyModelInfo",
- requestyModels[apiConfiguration.requestyModelId],
- )
- await this.postStateToWebview()
- }
- }
- })
- this.configManager
- .listConfig()
- .then(async (listApiConfig) => {
- if (!listApiConfig) {
- return
- }
- if (listApiConfig.length === 1) {
- // check if first time init then sync with exist config
- if (!checkExistKey(listApiConfig[0])) {
- const { apiConfiguration } = await this.getState()
- await this.configManager.saveConfig(
- listApiConfig[0].name ?? "default",
- apiConfiguration,
- )
- listApiConfig[0].apiProvider = apiConfiguration.apiProvider
- }
- }
- const currentConfigName = (await this.getGlobalState("currentApiConfigName")) as string
- if (currentConfigName) {
- if (!(await this.configManager.hasConfig(currentConfigName))) {
- // current config name not valid, get first config in list
- await this.updateGlobalState("currentApiConfigName", listApiConfig?.[0]?.name)
- if (listApiConfig?.[0]?.name) {
- const apiConfig = await this.configManager.loadConfig(
- listApiConfig?.[0]?.name,
- )
- await Promise.all([
- this.updateGlobalState("listApiConfigMeta", listApiConfig),
- this.postMessageToWebview({ type: "listApiConfig", listApiConfig }),
- this.updateApiConfiguration(apiConfig),
- ])
- await this.postStateToWebview()
- return
- }
- }
- }
- await Promise.all([
- await this.updateGlobalState("listApiConfigMeta", listApiConfig),
- await this.postMessageToWebview({ type: "listApiConfig", listApiConfig }),
- ])
- })
- .catch((error) =>
- this.outputChannel.appendLine(
- `Error list api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- ),
- )
- this.isViewLaunched = true
- break
- case "newTask":
- // Code that should run in response to the hello message command
- //vscode.window.showInformationMessage(message.text!)
- // Send a message to our webview.
- // You can send any JSON serializable data.
- // Could also do this in extension .ts
- //this.postMessageToWebview({ type: "text", text: `Extension: ${Date.now()}` })
- // initializing new instance of Cline will make sure that any agentically running promises in old instance don't affect our new task. this essentially creates a fresh slate for the new task
- await this.initClineWithTask(message.text, message.images)
- break
- case "apiConfiguration":
- if (message.apiConfiguration) {
- await this.updateApiConfiguration(message.apiConfiguration)
- }
- await this.postStateToWebview()
- break
- case "customInstructions":
- await this.updateCustomInstructions(message.text)
- break
- case "alwaysAllowReadOnly":
- await this.updateGlobalState("alwaysAllowReadOnly", message.bool ?? undefined)
- await this.postStateToWebview()
- break
- case "alwaysAllowWrite":
- await this.updateGlobalState("alwaysAllowWrite", message.bool ?? undefined)
- await this.postStateToWebview()
- break
- case "alwaysAllowExecute":
- await this.updateGlobalState("alwaysAllowExecute", message.bool ?? undefined)
- await this.postStateToWebview()
- break
- case "alwaysAllowBrowser":
- await this.updateGlobalState("alwaysAllowBrowser", message.bool ?? undefined)
- await this.postStateToWebview()
- break
- case "alwaysAllowMcp":
- await this.updateGlobalState("alwaysAllowMcp", message.bool)
- await this.postStateToWebview()
- break
- case "alwaysAllowModeSwitch":
- await this.updateGlobalState("alwaysAllowModeSwitch", message.bool)
- await this.postStateToWebview()
- break
- case "askResponse":
- this.cline?.handleWebviewAskResponse(message.askResponse!, message.text, message.images)
- break
- case "clearTask":
- // newTask will start a new task with a given task text, while clear task resets the current session and allows for a new task to be started
- await this.clearTask()
- await this.postStateToWebview()
- break
- case "didShowAnnouncement":
- await this.updateGlobalState("lastShownAnnouncementId", this.latestAnnouncementId)
- await this.postStateToWebview()
- break
- case "selectImages":
- const images = await selectImages()
- await this.postMessageToWebview({ type: "selectedImages", images })
- break
- case "exportCurrentTask":
- const currentTaskId = this.cline?.taskId
- if (currentTaskId) {
- this.exportTaskWithId(currentTaskId)
- }
- break
- case "showTaskWithId":
- this.showTaskWithId(message.text!)
- break
- case "deleteTaskWithId":
- this.deleteTaskWithId(message.text!)
- break
- case "exportTaskWithId":
- this.exportTaskWithId(message.text!)
- break
- case "resetState":
- await this.resetState()
- break
- case "refreshOpenRouterModels":
- const openRouterModels = await getOpenRouterModels()
- if (Object.keys(openRouterModels).length > 0) {
- const cacheDir = await this.ensureCacheDirectoryExists()
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.openRouterModels),
- JSON.stringify(openRouterModels),
- )
- await this.postMessageToWebview({ type: "openRouterModels", openRouterModels })
- }
- break
- case "refreshGlamaModels":
- const glamaModels = await getGlamaModels()
- if (Object.keys(glamaModels).length > 0) {
- const cacheDir = await this.ensureCacheDirectoryExists()
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.glamaModels),
- JSON.stringify(glamaModels),
- )
- await this.postMessageToWebview({ type: "glamaModels", glamaModels })
- }
- break
- case "refreshUnboundModels":
- const unboundModels = await getUnboundModels()
- if (Object.keys(unboundModels).length > 0) {
- const cacheDir = await this.ensureCacheDirectoryExists()
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.unboundModels),
- JSON.stringify(unboundModels),
- )
- await this.postMessageToWebview({ type: "unboundModels", unboundModels })
- }
- break
- case "refreshRequestyModels":
- const requestyModels = await getRequestyModels()
- if (Object.keys(requestyModels).length > 0) {
- const cacheDir = await this.ensureCacheDirectoryExists()
- await fs.writeFile(
- path.join(cacheDir, GlobalFileNames.requestyModels),
- JSON.stringify(requestyModels),
- )
- await this.postMessageToWebview({ type: "requestyModels", requestyModels })
- }
- break
- case "refreshOpenAiModels":
- if (message?.values?.baseUrl && message?.values?.apiKey) {
- const openAiModels = await getOpenAiModels(
- message?.values?.baseUrl,
- message?.values?.apiKey,
- )
- this.postMessageToWebview({ type: "openAiModels", openAiModels })
- }
- break
- case "requestOllamaModels":
- const ollamaModels = await getOllamaModels(message.text)
- // TODO: Cache like we do for OpenRouter, etc?
- this.postMessageToWebview({ type: "ollamaModels", ollamaModels })
- break
- case "requestLmStudioModels":
- const lmStudioModels = await getLmStudioModels(message.text)
- // TODO: Cache like we do for OpenRouter, etc?
- this.postMessageToWebview({ type: "lmStudioModels", lmStudioModels })
- break
- case "requestVsCodeLmModels":
- const vsCodeLmModels = await getVsCodeLmModels()
- // TODO: Cache like we do for OpenRouter, etc?
- this.postMessageToWebview({ type: "vsCodeLmModels", vsCodeLmModels })
- break
- case "openImage":
- openImage(message.text!)
- break
- case "openFile":
- openFile(message.text!, message.values as { create?: boolean; content?: string })
- break
- case "openMention":
- openMention(message.text)
- break
- case "checkpointDiff":
- const result = checkoutDiffPayloadSchema.safeParse(message.payload)
- if (result.success) {
- await this.cline?.checkpointDiff(result.data)
- }
- break
- case "checkpointRestore": {
- const result = checkoutRestorePayloadSchema.safeParse(message.payload)
- if (result.success) {
- await this.cancelTask()
- try {
- await pWaitFor(() => this.cline?.isInitialized === true, { timeout: 3_000 })
- } catch (error) {
- vscode.window.showErrorMessage("Timed out when attempting to restore checkpoint.")
- }
- try {
- await this.cline?.checkpointRestore(result.data)
- } catch (error) {
- vscode.window.showErrorMessage("Failed to restore checkpoint.")
- }
- }
- break
- }
- case "cancelTask":
- await this.cancelTask()
- break
- case "allowedCommands":
- await this.context.globalState.update("allowedCommands", message.commands)
- // Also update workspace settings
- await vscode.workspace
- .getConfiguration("roo-cline")
- .update("allowedCommands", message.commands, vscode.ConfigurationTarget.Global)
- break
- case "openMcpSettings": {
- const mcpSettingsFilePath = await this.mcpHub?.getMcpSettingsFilePath()
- if (mcpSettingsFilePath) {
- openFile(mcpSettingsFilePath)
- }
- break
- }
- case "openCustomModesSettings": {
- const customModesFilePath = await this.customModesManager.getCustomModesFilePath()
- if (customModesFilePath) {
- openFile(customModesFilePath)
- }
- break
- }
- case "deleteMcpServer": {
- if (!message.serverName) {
- break
- }
- try {
- this.outputChannel.appendLine(`Attempting to delete MCP server: ${message.serverName}`)
- await this.mcpHub?.deleteServer(message.serverName)
- this.outputChannel.appendLine(`Successfully deleted MCP server: ${message.serverName}`)
- } catch (error) {
- const errorMessage = error instanceof Error ? error.message : String(error)
- this.outputChannel.appendLine(`Failed to delete MCP server: ${errorMessage}`)
- // Error messages are already handled by McpHub.deleteServer
- }
- break
- }
- case "restartMcpServer": {
- try {
- await this.mcpHub?.restartConnection(message.text!)
- } catch (error) {
- this.outputChannel.appendLine(
- `Failed to retry connection for ${message.text}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- }
- break
- }
- case "toggleToolAlwaysAllow": {
- try {
- await this.mcpHub?.toggleToolAlwaysAllow(
- message.serverName!,
- message.toolName!,
- message.alwaysAllow!,
- )
- } catch (error) {
- this.outputChannel.appendLine(
- `Failed to toggle auto-approve for tool ${message.toolName}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- }
- break
- }
- case "toggleMcpServer": {
- try {
- await this.mcpHub?.toggleServerDisabled(message.serverName!, message.disabled!)
- } catch (error) {
- this.outputChannel.appendLine(
- `Failed to toggle MCP server ${message.serverName}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- }
- break
- }
- case "mcpEnabled":
- const mcpEnabled = message.bool ?? true
- await this.updateGlobalState("mcpEnabled", mcpEnabled)
- await this.postStateToWebview()
- break
- case "enableMcpServerCreation":
- await this.updateGlobalState("enableMcpServerCreation", message.bool ?? true)
- await this.postStateToWebview()
- break
- case "playSound":
- if (message.audioType) {
- const soundPath = path.join(this.context.extensionPath, "audio", `${message.audioType}.wav`)
- playSound(soundPath)
- }
- break
- case "soundEnabled":
- const soundEnabled = message.bool ?? true
- await this.updateGlobalState("soundEnabled", soundEnabled)
- setSoundEnabled(soundEnabled) // Add this line to update the sound utility
- await this.postStateToWebview()
- break
- case "soundVolume":
- const soundVolume = message.value ?? 0.5
- await this.updateGlobalState("soundVolume", soundVolume)
- setSoundVolume(soundVolume)
- await this.postStateToWebview()
- break
- case "diffEnabled":
- const diffEnabled = message.bool ?? true
- await this.updateGlobalState("diffEnabled", diffEnabled)
- await this.postStateToWebview()
- break
- case "enableCheckpoints":
- const enableCheckpoints = message.bool ?? true
- await this.updateGlobalState("enableCheckpoints", enableCheckpoints)
- await this.postStateToWebview()
- break
- case "browserViewportSize":
- const browserViewportSize = message.text ?? "900x600"
- await this.updateGlobalState("browserViewportSize", browserViewportSize)
- await this.postStateToWebview()
- break
- case "fuzzyMatchThreshold":
- await this.updateGlobalState("fuzzyMatchThreshold", message.value)
- await this.postStateToWebview()
- break
- case "alwaysApproveResubmit":
- await this.updateGlobalState("alwaysApproveResubmit", message.bool ?? false)
- await this.postStateToWebview()
- break
- case "requestDelaySeconds":
- await this.updateGlobalState("requestDelaySeconds", message.value ?? 5)
- await this.postStateToWebview()
- break
- case "rateLimitSeconds":
- await this.updateGlobalState("rateLimitSeconds", message.value ?? 0)
- await this.postStateToWebview()
- break
- case "preferredLanguage":
- await this.updateGlobalState("preferredLanguage", message.text)
- await this.postStateToWebview()
- break
- case "writeDelayMs":
- await this.updateGlobalState("writeDelayMs", message.value)
- await this.postStateToWebview()
- break
- case "terminalOutputLineLimit":
- await this.updateGlobalState("terminalOutputLineLimit", message.value)
- await this.postStateToWebview()
- break
- case "mode":
- await this.handleModeSwitch(message.text as Mode)
- break
- case "updateSupportPrompt":
- try {
- if (Object.keys(message?.values ?? {}).length === 0) {
- return
- }
- const existingPrompts = (await this.getGlobalState("customSupportPrompts")) || {}
- const updatedPrompts = {
- ...existingPrompts,
- ...message.values,
- }
- await this.updateGlobalState("customSupportPrompts", updatedPrompts)
- await this.postStateToWebview()
- } catch (error) {
- this.outputChannel.appendLine(
- `Error update support prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to update support prompt")
- }
- break
- case "resetSupportPrompt":
- try {
- if (!message?.text) {
- return
- }
- const existingPrompts = ((await this.getGlobalState("customSupportPrompts")) ||
- {}) as Record<string, any>
- const updatedPrompts = {
- ...existingPrompts,
- }
- updatedPrompts[message.text] = undefined
- await this.updateGlobalState("customSupportPrompts", updatedPrompts)
- await this.postStateToWebview()
- } catch (error) {
- this.outputChannel.appendLine(
- `Error reset support prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to reset support prompt")
- }
- break
- case "updatePrompt":
- if (message.promptMode && message.customPrompt !== undefined) {
- const existingPrompts = (await this.getGlobalState("customModePrompts")) || {}
- const updatedPrompts = {
- ...existingPrompts,
- [message.promptMode]: message.customPrompt,
- }
- await this.updateGlobalState("customModePrompts", updatedPrompts)
- // Get current state and explicitly include customModePrompts
- const currentState = await this.getState()
- const stateWithPrompts = {
- ...currentState,
- customModePrompts: updatedPrompts,
- }
- // Post state with prompts
- this.view?.webview.postMessage({
- type: "state",
- state: stateWithPrompts,
- })
- }
- break
- case "deleteMessage": {
- const answer = await vscode.window.showInformationMessage(
- "What would you like to delete?",
- { modal: true },
- "Just this message",
- "This and all subsequent messages",
- )
- if (
- (answer === "Just this message" || answer === "This and all subsequent messages") &&
- this.cline &&
- typeof message.value === "number" &&
- message.value
- ) {
- const timeCutoff = message.value - 1000 // 1 second buffer before the message to delete
- const messageIndex = this.cline.clineMessages.findIndex(
- (msg) => msg.ts && msg.ts >= timeCutoff,
- )
- const apiConversationHistoryIndex = this.cline.apiConversationHistory.findIndex(
- (msg) => msg.ts && msg.ts >= timeCutoff,
- )
- if (messageIndex !== -1) {
- const { historyItem } = await this.getTaskWithId(this.cline.taskId)
- if (answer === "Just this message") {
- // Find the next user message first
- const nextUserMessage = this.cline.clineMessages
- .slice(messageIndex + 1)
- .find((msg) => msg.type === "say" && msg.say === "user_feedback")
- // Handle UI messages
- if (nextUserMessage) {
- // Find absolute index of next user message
- const nextUserMessageIndex = this.cline.clineMessages.findIndex(
- (msg) => msg === nextUserMessage,
- )
- // Keep messages before current message and after next user message
- await this.cline.overwriteClineMessages([
- ...this.cline.clineMessages.slice(0, messageIndex),
- ...this.cline.clineMessages.slice(nextUserMessageIndex),
- ])
- } else {
- // If no next user message, keep only messages before current message
- await this.cline.overwriteClineMessages(
- this.cline.clineMessages.slice(0, messageIndex),
- )
- }
- // Handle API messages
- if (apiConversationHistoryIndex !== -1) {
- if (nextUserMessage && nextUserMessage.ts) {
- // Keep messages before current API message and after next user message
- await this.cline.overwriteApiConversationHistory([
- ...this.cline.apiConversationHistory.slice(
- 0,
- apiConversationHistoryIndex,
- ),
- ...this.cline.apiConversationHistory.filter(
- (msg) => msg.ts && msg.ts >= nextUserMessage.ts,
- ),
- ])
- } else {
- // If no next user message, keep only messages before current API message
- await this.cline.overwriteApiConversationHistory(
- this.cline.apiConversationHistory.slice(0, apiConversationHistoryIndex),
- )
- }
- }
- } else if (answer === "This and all subsequent messages") {
- // Delete this message and all that follow
- await this.cline.overwriteClineMessages(
- this.cline.clineMessages.slice(0, messageIndex),
- )
- if (apiConversationHistoryIndex !== -1) {
- await this.cline.overwriteApiConversationHistory(
- this.cline.apiConversationHistory.slice(0, apiConversationHistoryIndex),
- )
- }
- }
- await this.initClineWithHistoryItem(historyItem)
- }
- }
- break
- }
- case "screenshotQuality":
- await this.updateGlobalState("screenshotQuality", message.value)
- await this.postStateToWebview()
- break
- case "maxOpenTabsContext":
- const tabCount = Math.min(Math.max(0, message.value ?? 20), 500)
- await this.updateGlobalState("maxOpenTabsContext", tabCount)
- await this.postStateToWebview()
- break
- case "enhancementApiConfigId":
- await this.updateGlobalState("enhancementApiConfigId", message.text)
- await this.postStateToWebview()
- break
- case "autoApprovalEnabled":
- await this.updateGlobalState("autoApprovalEnabled", message.bool ?? false)
- await this.postStateToWebview()
- break
- case "enhancePrompt":
- if (message.text) {
- try {
- const {
- apiConfiguration,
- customSupportPrompts,
- listApiConfigMeta,
- enhancementApiConfigId,
- } = await this.getState()
- // Try to get enhancement config first, fall back to current config
- let configToUse: ApiConfiguration = apiConfiguration
- if (enhancementApiConfigId) {
- const config = listApiConfigMeta?.find((c) => c.id === enhancementApiConfigId)
- if (config?.name) {
- const loadedConfig = await this.configManager.loadConfig(config.name)
- if (loadedConfig.apiProvider) {
- configToUse = loadedConfig
- }
- }
- }
- const enhancedPrompt = await singleCompletionHandler(
- configToUse,
- supportPrompt.create(
- "ENHANCE",
- {
- userInput: message.text,
- },
- customSupportPrompts,
- ),
- )
- await this.postMessageToWebview({
- type: "enhancedPrompt",
- text: enhancedPrompt,
- })
- } catch (error) {
- this.outputChannel.appendLine(
- `Error enhancing prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to enhance prompt")
- await this.postMessageToWebview({
- type: "enhancedPrompt",
- })
- }
- }
- break
- case "getSystemPrompt":
- try {
- const systemPrompt = await generateSystemPrompt(message)
- await this.postMessageToWebview({
- type: "systemPrompt",
- text: systemPrompt,
- mode: message.mode,
- })
- } catch (error) {
- this.outputChannel.appendLine(
- `Error getting system prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to get system prompt")
- }
- break
- case "copySystemPrompt":
- try {
- const systemPrompt = await generateSystemPrompt(message)
- await vscode.env.clipboard.writeText(systemPrompt)
- await vscode.window.showInformationMessage("System prompt successfully copied to clipboard")
- } catch (error) {
- this.outputChannel.appendLine(
- `Error getting system prompt: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to get system prompt")
- }
- break
- case "searchCommits": {
- const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0)
- if (cwd) {
- try {
- const commits = await searchCommits(message.query || "", cwd)
- await this.postMessageToWebview({
- type: "commitSearchResults",
- commits,
- })
- } catch (error) {
- this.outputChannel.appendLine(
- `Error searching commits: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to search commits")
- }
- }
- break
- }
- case "saveApiConfiguration":
- if (message.text && message.apiConfiguration) {
- try {
- await this.configManager.saveConfig(message.text, message.apiConfiguration)
- const listApiConfig = await this.configManager.listConfig()
- await this.updateGlobalState("listApiConfigMeta", listApiConfig)
- } catch (error) {
- this.outputChannel.appendLine(
- `Error save api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to save api configuration")
- }
- }
- break
- case "upsertApiConfiguration":
- if (message.text && message.apiConfiguration) {
- try {
- await this.configManager.saveConfig(message.text, message.apiConfiguration)
- const listApiConfig = await this.configManager.listConfig()
- await Promise.all([
- this.updateGlobalState("listApiConfigMeta", listApiConfig),
- this.updateApiConfiguration(message.apiConfiguration),
- this.updateGlobalState("currentApiConfigName", message.text),
- ])
- await this.postStateToWebview()
- } catch (error) {
- this.outputChannel.appendLine(
- `Error create new api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to create api configuration")
- }
- }
- break
- case "renameApiConfiguration":
- if (message.values && message.apiConfiguration) {
- try {
- const { oldName, newName } = message.values
- if (oldName === newName) {
- break
- }
- await this.configManager.saveConfig(newName, message.apiConfiguration)
- await this.configManager.deleteConfig(oldName)
- const listApiConfig = await this.configManager.listConfig()
- const config = listApiConfig?.find((c) => c.name === newName)
- // Update listApiConfigMeta first to ensure UI has latest data
- await this.updateGlobalState("listApiConfigMeta", listApiConfig)
- await Promise.all([this.updateGlobalState("currentApiConfigName", newName)])
- await this.postStateToWebview()
- } catch (error) {
- this.outputChannel.appendLine(
- `Error rename api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to rename api configuration")
- }
- }
- break
- case "loadApiConfiguration":
- if (message.text) {
- try {
- const apiConfig = await this.configManager.loadConfig(message.text)
- const listApiConfig = await this.configManager.listConfig()
- await Promise.all([
- this.updateGlobalState("listApiConfigMeta", listApiConfig),
- this.updateGlobalState("currentApiConfigName", message.text),
- this.updateApiConfiguration(apiConfig),
- ])
- await this.postStateToWebview()
- } catch (error) {
- this.outputChannel.appendLine(
- `Error load api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to load api configuration")
- }
- }
- break
- case "deleteApiConfiguration":
- if (message.text) {
- const answer = await vscode.window.showInformationMessage(
- "Are you sure you want to delete this configuration profile?",
- { modal: true },
- "Yes",
- )
- if (answer !== "Yes") {
- break
- }
- try {
- await this.configManager.deleteConfig(message.text)
- const listApiConfig = await this.configManager.listConfig()
- // Update listApiConfigMeta first to ensure UI has latest data
- await this.updateGlobalState("listApiConfigMeta", listApiConfig)
- // If this was the current config, switch to first available
- const currentApiConfigName = await this.getGlobalState("currentApiConfigName")
- if (message.text === currentApiConfigName && listApiConfig?.[0]?.name) {
- const apiConfig = await this.configManager.loadConfig(listApiConfig[0].name)
- await Promise.all([
- this.updateGlobalState("currentApiConfigName", listApiConfig[0].name),
- this.updateApiConfiguration(apiConfig),
- ])
- }
- await this.postStateToWebview()
- } catch (error) {
- this.outputChannel.appendLine(
- `Error delete api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to delete api configuration")
- }
- }
- break
- case "getListApiConfiguration":
- try {
- const listApiConfig = await this.configManager.listConfig()
- await this.updateGlobalState("listApiConfigMeta", listApiConfig)
- this.postMessageToWebview({ type: "listApiConfig", listApiConfig })
- } catch (error) {
- this.outputChannel.appendLine(
- `Error get list api configuration: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to get list api configuration")
- }
- break
- case "updateExperimental": {
- if (!message.values) {
- break
- }
- const updatedExperiments = {
- ...((await this.getGlobalState("experiments")) ?? experimentDefault),
- ...message.values,
- } as Record<ExperimentId, boolean>
- await this.updateGlobalState("experiments", updatedExperiments)
- // Update diffStrategy in current Cline instance if it exists
- if (message.values[EXPERIMENT_IDS.DIFF_STRATEGY] !== undefined && this.cline) {
- await this.cline.updateDiffStrategy(
- Experiments.isEnabled(updatedExperiments, EXPERIMENT_IDS.DIFF_STRATEGY),
- )
- }
- await this.postStateToWebview()
- break
- }
- case "updateMcpTimeout":
- if (message.serverName && typeof message.timeout === "number") {
- try {
- await this.mcpHub?.updateServerTimeout(message.serverName, message.timeout)
- } catch (error) {
- this.outputChannel.appendLine(
- `Failed to update timeout for ${message.serverName}: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- vscode.window.showErrorMessage("Failed to update server timeout")
- }
- }
- break
- case "updateCustomMode":
- if (message.modeConfig) {
- await this.customModesManager.updateCustomMode(message.modeConfig.slug, message.modeConfig)
- // Update state after saving the mode
- const customModes = await this.customModesManager.getCustomModes()
- await this.updateGlobalState("customModes", customModes)
- await this.updateGlobalState("mode", message.modeConfig.slug)
- await this.postStateToWebview()
- }
- break
- case "deleteCustomMode":
- if (message.slug) {
- const answer = await vscode.window.showInformationMessage(
- "Are you sure you want to delete this custom mode?",
- { modal: true },
- "Yes",
- )
- if (answer !== "Yes") {
- break
- }
- await this.customModesManager.deleteCustomMode(message.slug)
- // Switch back to default mode after deletion
- await this.updateGlobalState("mode", defaultModeSlug)
- await this.postStateToWebview()
- }
- }
- },
- null,
- this.disposables,
- )
- const generateSystemPrompt = async (message: WebviewMessage) => {
- const {
- apiConfiguration,
- customModePrompts,
- customInstructions,
- preferredLanguage,
- browserViewportSize,
- diffEnabled,
- mcpEnabled,
- fuzzyMatchThreshold,
- experiments,
- enableMcpServerCreation,
- } = await this.getState()
- // Create diffStrategy based on current model and settings
- const diffStrategy = getDiffStrategy(
- apiConfiguration.apiModelId || apiConfiguration.openRouterModelId || "",
- fuzzyMatchThreshold,
- Experiments.isEnabled(experiments, EXPERIMENT_IDS.DIFF_STRATEGY),
- )
- const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) || ""
- const mode = message.mode ?? defaultModeSlug
- const customModes = await this.customModesManager.getCustomModes()
- const systemPrompt = await SYSTEM_PROMPT(
- this.context,
- cwd,
- apiConfiguration.openRouterModelInfo?.supportsComputerUse ?? false,
- mcpEnabled ? this.mcpHub : undefined,
- diffStrategy,
- browserViewportSize ?? "900x600",
- mode,
- customModePrompts,
- customModes,
- customInstructions,
- preferredLanguage,
- diffEnabled,
- experiments,
- enableMcpServerCreation,
- )
- return systemPrompt
- }
- }
- /**
- * Handle switching to a new mode, including updating the associated API configuration
- * @param newMode The mode to switch to
- */
- public async handleModeSwitch(newMode: Mode) {
- await this.updateGlobalState("mode", newMode)
- // Load the saved API config for the new mode if it exists
- const savedConfigId = await this.configManager.getModeConfigId(newMode)
- const listApiConfig = await this.configManager.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.configManager.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 = await this.getGlobalState("currentApiConfigName")
- if (currentApiConfigName) {
- const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
- if (config?.id) {
- await this.configManager.setModeConfig(newMode, config.id)
- }
- }
- }
- await this.postStateToWebview()
- }
- private async updateApiConfiguration(apiConfiguration: ApiConfiguration) {
- // Update mode's default config
- const { mode } = await this.getState()
- if (mode) {
- const currentApiConfigName = await this.getGlobalState("currentApiConfigName")
- const listApiConfig = await this.configManager.listConfig()
- const config = listApiConfig?.find((c) => c.name === currentApiConfigName)
- if (config?.id) {
- await this.configManager.setModeConfig(mode, config.id)
- }
- }
- const {
- apiProvider,
- apiModelId,
- apiKey,
- glamaModelId,
- glamaModelInfo,
- glamaApiKey,
- openRouterApiKey,
- awsAccessKey,
- awsSecretKey,
- awsSessionToken,
- awsRegion,
- awsUseCrossRegionInference,
- awsProfile,
- awsUseProfile,
- vertexProjectId,
- vertexRegion,
- openAiBaseUrl,
- openAiApiKey,
- openAiModelId,
- openAiCustomModelInfo,
- openAiUseAzure,
- ollamaModelId,
- ollamaBaseUrl,
- lmStudioModelId,
- lmStudioBaseUrl,
- anthropicBaseUrl,
- geminiApiKey,
- openAiNativeApiKey,
- deepSeekApiKey,
- azureApiVersion,
- openAiStreamingEnabled,
- openRouterModelId,
- openRouterBaseUrl,
- openRouterModelInfo,
- openRouterUseMiddleOutTransform,
- vsCodeLmModelSelector,
- mistralApiKey,
- mistralCodestralUrl,
- unboundApiKey,
- unboundModelId,
- unboundModelInfo,
- requestyApiKey,
- requestyModelId,
- requestyModelInfo,
- modelTemperature,
- modelMaxTokens,
- modelMaxThinkingTokens,
- } = apiConfiguration
- await Promise.all([
- this.updateGlobalState("apiProvider", apiProvider),
- this.updateGlobalState("apiModelId", apiModelId),
- this.storeSecret("apiKey", apiKey),
- this.updateGlobalState("glamaModelId", glamaModelId),
- this.updateGlobalState("glamaModelInfo", glamaModelInfo),
- this.storeSecret("glamaApiKey", glamaApiKey),
- this.storeSecret("openRouterApiKey", openRouterApiKey),
- this.storeSecret("awsAccessKey", awsAccessKey),
- this.storeSecret("awsSecretKey", awsSecretKey),
- this.storeSecret("awsSessionToken", awsSessionToken),
- this.updateGlobalState("awsRegion", awsRegion),
- this.updateGlobalState("awsUseCrossRegionInference", awsUseCrossRegionInference),
- this.updateGlobalState("awsProfile", awsProfile),
- this.updateGlobalState("awsUseProfile", awsUseProfile),
- this.updateGlobalState("vertexProjectId", vertexProjectId),
- this.updateGlobalState("vertexRegion", vertexRegion),
- this.updateGlobalState("openAiBaseUrl", openAiBaseUrl),
- this.storeSecret("openAiApiKey", openAiApiKey),
- this.updateGlobalState("openAiModelId", openAiModelId),
- this.updateGlobalState("openAiCustomModelInfo", openAiCustomModelInfo),
- this.updateGlobalState("openAiUseAzure", openAiUseAzure),
- this.updateGlobalState("ollamaModelId", ollamaModelId),
- this.updateGlobalState("ollamaBaseUrl", ollamaBaseUrl),
- this.updateGlobalState("lmStudioModelId", lmStudioModelId),
- this.updateGlobalState("lmStudioBaseUrl", lmStudioBaseUrl),
- this.updateGlobalState("anthropicBaseUrl", anthropicBaseUrl),
- this.storeSecret("geminiApiKey", geminiApiKey),
- this.storeSecret("openAiNativeApiKey", openAiNativeApiKey),
- this.storeSecret("deepSeekApiKey", deepSeekApiKey),
- this.updateGlobalState("azureApiVersion", azureApiVersion),
- this.updateGlobalState("openAiStreamingEnabled", openAiStreamingEnabled),
- this.updateGlobalState("openRouterModelId", openRouterModelId),
- this.updateGlobalState("openRouterModelInfo", openRouterModelInfo),
- this.updateGlobalState("openRouterBaseUrl", openRouterBaseUrl),
- this.updateGlobalState("openRouterUseMiddleOutTransform", openRouterUseMiddleOutTransform),
- this.updateGlobalState("vsCodeLmModelSelector", vsCodeLmModelSelector),
- this.storeSecret("mistralApiKey", mistralApiKey),
- this.updateGlobalState("mistralCodestralUrl", mistralCodestralUrl),
- this.storeSecret("unboundApiKey", unboundApiKey),
- this.updateGlobalState("unboundModelId", unboundModelId),
- this.updateGlobalState("unboundModelInfo", unboundModelInfo),
- this.storeSecret("requestyApiKey", requestyApiKey),
- this.updateGlobalState("requestyModelId", requestyModelId),
- this.updateGlobalState("requestyModelInfo", requestyModelInfo),
- this.updateGlobalState("modelTemperature", modelTemperature),
- this.updateGlobalState("modelMaxTokens", modelMaxTokens),
- this.updateGlobalState("anthropicThinking", modelMaxThinkingTokens),
- ])
- if (this.cline) {
- this.cline.api = buildApiHandler(apiConfiguration)
- }
- }
- async cancelTask() {
- if (this.cline) {
- const { historyItem } = await this.getTaskWithId(this.cline.taskId)
- this.cline.abortTask()
- await pWaitFor(
- () =>
- this.cline === undefined ||
- this.cline.isStreaming === false ||
- this.cline.didFinishAbortingStream ||
- // If only the first chunk is processed, then there's no
- // need to wait for graceful abort (closes edits, browser,
- // etc).
- this.cline.isWaitingForFirstChunk,
- {
- timeout: 3_000,
- },
- ).catch(() => {
- console.error("Failed to abort task")
- })
- if (this.cline) {
- // 'abandoned' will prevent this Cline instance from affecting
- // future Cline instances. This may happen if its hanging on a
- // streaming request.
- this.cline.abandoned = true
- }
- // Clears task again, so we need to abortTask manually above.
- await this.initClineWithHistoryItem(historyItem)
- }
- }
- async updateCustomInstructions(instructions?: string) {
- // User may be clearing the field
- await this.updateGlobalState("customInstructions", instructions || undefined)
- if (this.cline) {
- this.cline.customInstructions = instructions || undefined
- }
- await this.postStateToWebview()
- }
- // MCP
- async ensureMcpServersDirectoryExists(): Promise<string> {
- const mcpServersDir = path.join(os.homedir(), "Documents", "Cline", "MCP")
- try {
- await fs.mkdir(mcpServersDir, { recursive: true })
- } catch (error) {
- return "~/Documents/Cline/MCP" // in case creating a directory in documents fails for whatever reason (e.g. permissions) - this is fine since this path is only ever used in the system prompt
- }
- return mcpServersDir
- }
- async ensureSettingsDirectoryExists(): Promise<string> {
- const settingsDir = path.join(this.context.globalStorageUri.fsPath, "settings")
- await fs.mkdir(settingsDir, { recursive: true })
- return settingsDir
- }
- private async ensureCacheDirectoryExists() {
- const cacheDir = path.join(this.context.globalStorageUri.fsPath, "cache")
- await fs.mkdir(cacheDir, { recursive: true })
- return cacheDir
- }
- private 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 apiKey: string
- try {
- const response = await axios.post("https://openrouter.ai/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.outputChannel.appendLine(
- `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- throw error
- }
- const openrouter: ApiProvider = "openrouter"
- await this.updateGlobalState("apiProvider", openrouter)
- await this.storeSecret("openRouterApiKey", apiKey)
- await this.postStateToWebview()
- if (this.cline) {
- this.cline.api = buildApiHandler({ apiProvider: openrouter, openRouterApiKey: apiKey })
- }
- // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome
- }
- // 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.outputChannel.appendLine(
- `Error exchanging code for API key: ${JSON.stringify(error, Object.getOwnPropertyNames(error), 2)}`,
- )
- throw error
- }
- const glama: ApiProvider = "glama"
- await this.updateGlobalState("apiProvider", glama)
- await this.storeSecret("glamaApiKey", apiKey)
- await this.postStateToWebview()
- if (this.cline) {
- this.cline.api = buildApiHandler({
- apiProvider: glama,
- glamaApiKey: apiKey,
- })
- }
- // await this.postMessageToWebview({ type: "action", action: "settingsButtonClicked" }) // bad ux if user is on welcome
- }
- // Task history
- async getTaskWithId(id: string): Promise<{
- historyItem: HistoryItem
- taskDirPath: string
- apiConversationHistoryFilePath: string
- uiMessagesFilePath: string
- apiConversationHistory: Anthropic.MessageParam[]
- }> {
- const history = ((await this.getGlobalState("taskHistory")) as HistoryItem[] | undefined) || []
- const historyItem = history.find((item) => item.id === id)
- if (historyItem) {
- const taskDirPath = path.join(this.context.globalStorageUri.fsPath, "tasks", 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.cline?.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)
- }
- async deleteTaskWithId(id: string) {
- if (id === this.cline?.taskId) {
- await this.clearTask()
- }
- const { taskDirPath, apiConversationHistoryFilePath, uiMessagesFilePath } = await this.getTaskWithId(id)
- await this.deleteTaskFromState(id)
- // Delete the task files.
- const apiConversationHistoryFileExists = await fileExistsAtPath(apiConversationHistoryFilePath)
- if (apiConversationHistoryFileExists) {
- await fs.unlink(apiConversationHistoryFilePath)
- }
- const uiMessagesFileExists = await fileExistsAtPath(uiMessagesFilePath)
- if (uiMessagesFileExists) {
- await fs.unlink(uiMessagesFilePath)
- }
- const legacyMessagesFilePath = path.join(taskDirPath, "claude_messages.json")
- if (await fileExistsAtPath(legacyMessagesFilePath)) {
- await fs.unlink(legacyMessagesFilePath)
- }
- const { enableCheckpoints } = await this.getState()
- const baseDir = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0)
- // Delete checkpoints branch.
- if (enableCheckpoints && baseDir) {
- const branchSummary = await simpleGit(baseDir)
- .branch(["-D", `roo-code-checkpoints-${id}`])
- .catch(() => undefined)
- if (branchSummary) {
- console.log(`[deleteTaskWithId${id}] deleted checkpoints branch`)
- }
- }
- // Delete checkpoints directory
- const checkpointsDir = path.join(taskDirPath, "checkpoints")
- if (await fileExistsAtPath(checkpointsDir)) {
- try {
- await fs.rm(checkpointsDir, { recursive: true, force: true })
- console.log(`[deleteTaskWithId${id}] removed checkpoints repo`)
- } catch (error) {
- console.error(
- `[deleteTaskWithId${id}] failed to remove checkpoints repo: ${error instanceof Error ? error.message : String(error)}`,
- )
- }
- }
- // Succeeds if the dir is empty.
- await fs.rmdir(taskDirPath)
- }
- async deleteTaskFromState(id: string) {
- // Remove the task from history
- const taskHistory = ((await this.getGlobalState("taskHistory")) as HistoryItem[]) || []
- const updatedTaskHistory = taskHistory.filter((task) => task.id !== id)
- await this.updateGlobalState("taskHistory", updatedTaskHistory)
- // Notify the webview that the task has been deleted
- await this.postStateToWebview()
- }
- async postStateToWebview() {
- const state = await this.getStateToPostToWebview()
- this.postMessageToWebview({ type: "state", state })
- }
- async getStateToPostToWebview() {
- const {
- apiConfiguration,
- lastShownAnnouncementId,
- customInstructions,
- alwaysAllowReadOnly,
- alwaysAllowWrite,
- alwaysAllowExecute,
- alwaysAllowBrowser,
- alwaysAllowMcp,
- alwaysAllowModeSwitch,
- soundEnabled,
- diffEnabled,
- enableCheckpoints,
- taskHistory,
- soundVolume,
- browserViewportSize,
- screenshotQuality,
- preferredLanguage,
- writeDelayMs,
- terminalOutputLineLimit,
- fuzzyMatchThreshold,
- mcpEnabled,
- enableMcpServerCreation,
- alwaysApproveResubmit,
- requestDelaySeconds,
- rateLimitSeconds,
- currentApiConfigName,
- listApiConfigMeta,
- mode,
- customModePrompts,
- customSupportPrompts,
- enhancementApiConfigId,
- autoApprovalEnabled,
- experiments,
- maxOpenTabsContext,
- } = await this.getState()
- const allowedCommands = vscode.workspace.getConfiguration("roo-cline").get<string[]>("allowedCommands") || []
- const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) || ""
- return {
- version: this.context.extension?.packageJSON?.version ?? "",
- apiConfiguration,
- customInstructions,
- alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
- alwaysAllowWrite: alwaysAllowWrite ?? false,
- alwaysAllowExecute: alwaysAllowExecute ?? false,
- alwaysAllowBrowser: alwaysAllowBrowser ?? false,
- alwaysAllowMcp: alwaysAllowMcp ?? false,
- alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false,
- uriScheme: vscode.env.uriScheme,
- currentTaskItem: this.cline?.taskId
- ? (taskHistory || []).find((item) => item.id === this.cline?.taskId)
- : undefined,
- clineMessages: this.cline?.clineMessages || [],
- taskHistory: (taskHistory || [])
- .filter((item: HistoryItem) => item.ts && item.task)
- .sort((a: HistoryItem, b: HistoryItem) => b.ts - a.ts),
- soundEnabled: soundEnabled ?? false,
- diffEnabled: diffEnabled ?? true,
- enableCheckpoints: enableCheckpoints ?? true,
- shouldShowAnnouncement: lastShownAnnouncementId !== this.latestAnnouncementId,
- allowedCommands,
- soundVolume: soundVolume ?? 0.5,
- browserViewportSize: browserViewportSize ?? "900x600",
- screenshotQuality: screenshotQuality ?? 75,
- preferredLanguage: preferredLanguage ?? "English",
- writeDelayMs: writeDelayMs ?? 1000,
- terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
- fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
- mcpEnabled: mcpEnabled ?? true,
- enableMcpServerCreation: enableMcpServerCreation ?? true,
- alwaysApproveResubmit: alwaysApproveResubmit ?? false,
- requestDelaySeconds: requestDelaySeconds ?? 10,
- rateLimitSeconds: rateLimitSeconds ?? 0,
- currentApiConfigName: currentApiConfigName ?? "default",
- listApiConfigMeta: listApiConfigMeta ?? [],
- 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,
- cwd: cwd,
- }
- }
- async clearTask() {
- this.cline?.abortTask()
- this.cline = undefined // removes reference to it, so once promises end it will be garbage collected
- }
- // Caching mechanism to keep track of webview messages + API conversation history per provider instance
- /*
- Now that we use retainContextWhenHidden, we don't have to store a cache of cline messages in the user's state, but we could to reduce memory footprint in long conversations.
- - We have to be careful of what state is shared between ClineProvider instances since there could be multiple instances of the extension running at once. For example when we cached cline messages using the same key, two instances of the extension could end up using the same key and overwriting each other's messages.
- - Some state does need to be shared between the instances, i.e. the API key--however there doesn't seem to be a good way to notfy the other instances that the API key has changed.
- We need to use a unique identifier for each ClineProvider instance's message cache since we could be running several instances of the extension outside of just the sidebar i.e. in editor panels.
- // conversation history to send in API requests
- /*
- It seems that some API messages do not comply with vscode state requirements. Either the Anthropic library is manipulating these values somehow in the backend in a way thats creating cyclic references, or the API returns a function or a Symbol as part of the message content.
- VSCode docs about state: "The value must be JSON-stringifyable ... value — A value. MUST not contain cyclic references."
- For now we'll store the conversation history in memory, and if we need to store in state directly we'd need to do a manual conversion to ensure proper json stringification.
- */
- // getApiConversationHistory(): Anthropic.MessageParam[] {
- // // const history = (await this.getGlobalState(
- // // this.getApiConversationHistoryStateKey()
- // // )) as Anthropic.MessageParam[]
- // // return history || []
- // return this.apiConversationHistory
- // }
- // setApiConversationHistory(history: Anthropic.MessageParam[] | undefined) {
- // // await this.updateGlobalState(this.getApiConversationHistoryStateKey(), history)
- // this.apiConversationHistory = history || []
- // }
- // addMessageToApiConversationHistory(message: Anthropic.MessageParam): Anthropic.MessageParam[] {
- // // const history = await this.getApiConversationHistory()
- // // history.push(message)
- // // await this.setApiConversationHistory(history)
- // // return history
- // this.apiConversationHistory.push(message)
- // return this.apiConversationHistory
- // }
- /*
- 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 [
- storedApiProvider,
- apiModelId,
- apiKey,
- glamaApiKey,
- glamaModelId,
- glamaModelInfo,
- openRouterApiKey,
- awsAccessKey,
- awsSecretKey,
- awsSessionToken,
- awsRegion,
- awsUseCrossRegionInference,
- awsProfile,
- awsUseProfile,
- vertexProjectId,
- vertexRegion,
- openAiBaseUrl,
- openAiApiKey,
- openAiModelId,
- openAiCustomModelInfo,
- openAiUseAzure,
- ollamaModelId,
- ollamaBaseUrl,
- lmStudioModelId,
- lmStudioBaseUrl,
- anthropicBaseUrl,
- geminiApiKey,
- openAiNativeApiKey,
- deepSeekApiKey,
- mistralApiKey,
- mistralCodestralUrl,
- azureApiVersion,
- openAiStreamingEnabled,
- openRouterModelId,
- openRouterModelInfo,
- openRouterBaseUrl,
- openRouterUseMiddleOutTransform,
- lastShownAnnouncementId,
- customInstructions,
- alwaysAllowReadOnly,
- alwaysAllowWrite,
- alwaysAllowExecute,
- alwaysAllowBrowser,
- alwaysAllowMcp,
- alwaysAllowModeSwitch,
- taskHistory,
- allowedCommands,
- soundEnabled,
- diffEnabled,
- enableCheckpoints,
- soundVolume,
- browserViewportSize,
- fuzzyMatchThreshold,
- preferredLanguage,
- writeDelayMs,
- screenshotQuality,
- terminalOutputLineLimit,
- mcpEnabled,
- enableMcpServerCreation,
- alwaysApproveResubmit,
- requestDelaySeconds,
- rateLimitSeconds,
- currentApiConfigName,
- listApiConfigMeta,
- vsCodeLmModelSelector,
- mode,
- modeApiConfigs,
- customModePrompts,
- customSupportPrompts,
- enhancementApiConfigId,
- autoApprovalEnabled,
- customModes,
- experiments,
- unboundApiKey,
- unboundModelId,
- unboundModelInfo,
- requestyApiKey,
- requestyModelId,
- requestyModelInfo,
- modelTemperature,
- modelMaxTokens,
- modelMaxThinkingTokens,
- maxOpenTabsContext,
- ] = await Promise.all([
- this.getGlobalState("apiProvider") as Promise<ApiProvider | undefined>,
- this.getGlobalState("apiModelId") as Promise<string | undefined>,
- this.getSecret("apiKey") as Promise<string | undefined>,
- this.getSecret("glamaApiKey") as Promise<string | undefined>,
- this.getGlobalState("glamaModelId") as Promise<string | undefined>,
- this.getGlobalState("glamaModelInfo") as Promise<ModelInfo | undefined>,
- this.getSecret("openRouterApiKey") as Promise<string | undefined>,
- this.getSecret("awsAccessKey") as Promise<string | undefined>,
- this.getSecret("awsSecretKey") as Promise<string | undefined>,
- this.getSecret("awsSessionToken") as Promise<string | undefined>,
- this.getGlobalState("awsRegion") as Promise<string | undefined>,
- this.getGlobalState("awsUseCrossRegionInference") as Promise<boolean | undefined>,
- this.getGlobalState("awsProfile") as Promise<string | undefined>,
- this.getGlobalState("awsUseProfile") as Promise<boolean | undefined>,
- this.getGlobalState("vertexProjectId") as Promise<string | undefined>,
- this.getGlobalState("vertexRegion") as Promise<string | undefined>,
- this.getGlobalState("openAiBaseUrl") as Promise<string | undefined>,
- this.getSecret("openAiApiKey") as Promise<string | undefined>,
- this.getGlobalState("openAiModelId") as Promise<string | undefined>,
- this.getGlobalState("openAiCustomModelInfo") as Promise<ModelInfo | undefined>,
- this.getGlobalState("openAiUseAzure") as Promise<boolean | undefined>,
- this.getGlobalState("ollamaModelId") as Promise<string | undefined>,
- this.getGlobalState("ollamaBaseUrl") as Promise<string | undefined>,
- this.getGlobalState("lmStudioModelId") as Promise<string | undefined>,
- this.getGlobalState("lmStudioBaseUrl") as Promise<string | undefined>,
- this.getGlobalState("anthropicBaseUrl") as Promise<string | undefined>,
- this.getSecret("geminiApiKey") as Promise<string | undefined>,
- this.getSecret("openAiNativeApiKey") as Promise<string | undefined>,
- this.getSecret("deepSeekApiKey") as Promise<string | undefined>,
- this.getSecret("mistralApiKey") as Promise<string | undefined>,
- this.getGlobalState("mistralCodestralUrl") as Promise<string | undefined>,
- this.getGlobalState("azureApiVersion") as Promise<string | undefined>,
- this.getGlobalState("openAiStreamingEnabled") as Promise<boolean | undefined>,
- this.getGlobalState("openRouterModelId") as Promise<string | undefined>,
- this.getGlobalState("openRouterModelInfo") as Promise<ModelInfo | undefined>,
- this.getGlobalState("openRouterBaseUrl") as Promise<string | undefined>,
- this.getGlobalState("openRouterUseMiddleOutTransform") as Promise<boolean | undefined>,
- this.getGlobalState("lastShownAnnouncementId") as Promise<string | undefined>,
- this.getGlobalState("customInstructions") as Promise<string | undefined>,
- this.getGlobalState("alwaysAllowReadOnly") as Promise<boolean | undefined>,
- this.getGlobalState("alwaysAllowWrite") as Promise<boolean | undefined>,
- this.getGlobalState("alwaysAllowExecute") as Promise<boolean | undefined>,
- this.getGlobalState("alwaysAllowBrowser") as Promise<boolean | undefined>,
- this.getGlobalState("alwaysAllowMcp") as Promise<boolean | undefined>,
- this.getGlobalState("alwaysAllowModeSwitch") as Promise<boolean | undefined>,
- this.getGlobalState("taskHistory") as Promise<HistoryItem[] | undefined>,
- this.getGlobalState("allowedCommands") as Promise<string[] | undefined>,
- this.getGlobalState("soundEnabled") as Promise<boolean | undefined>,
- this.getGlobalState("diffEnabled") as Promise<boolean | undefined>,
- this.getGlobalState("enableCheckpoints") as Promise<boolean | undefined>,
- this.getGlobalState("soundVolume") as Promise<number | undefined>,
- this.getGlobalState("browserViewportSize") as Promise<string | undefined>,
- this.getGlobalState("fuzzyMatchThreshold") as Promise<number | undefined>,
- this.getGlobalState("preferredLanguage") as Promise<string | undefined>,
- this.getGlobalState("writeDelayMs") as Promise<number | undefined>,
- this.getGlobalState("screenshotQuality") as Promise<number | undefined>,
- this.getGlobalState("terminalOutputLineLimit") as Promise<number | undefined>,
- this.getGlobalState("mcpEnabled") as Promise<boolean | undefined>,
- this.getGlobalState("enableMcpServerCreation") as Promise<boolean | undefined>,
- this.getGlobalState("alwaysApproveResubmit") as Promise<boolean | undefined>,
- this.getGlobalState("requestDelaySeconds") as Promise<number | undefined>,
- this.getGlobalState("rateLimitSeconds") as Promise<number | undefined>,
- this.getGlobalState("currentApiConfigName") as Promise<string | undefined>,
- this.getGlobalState("listApiConfigMeta") as Promise<ApiConfigMeta[] | undefined>,
- this.getGlobalState("vsCodeLmModelSelector") as Promise<vscode.LanguageModelChatSelector | undefined>,
- this.getGlobalState("mode") as Promise<Mode | undefined>,
- this.getGlobalState("modeApiConfigs") as Promise<Record<Mode, string> | undefined>,
- this.getGlobalState("customModePrompts") as Promise<CustomModePrompts | undefined>,
- this.getGlobalState("customSupportPrompts") as Promise<CustomSupportPrompts | undefined>,
- this.getGlobalState("enhancementApiConfigId") as Promise<string | undefined>,
- this.getGlobalState("autoApprovalEnabled") as Promise<boolean | undefined>,
- this.customModesManager.getCustomModes(),
- this.getGlobalState("experiments") as Promise<Record<ExperimentId, boolean> | undefined>,
- this.getSecret("unboundApiKey") as Promise<string | undefined>,
- this.getGlobalState("unboundModelId") as Promise<string | undefined>,
- this.getGlobalState("unboundModelInfo") as Promise<ModelInfo | undefined>,
- this.getSecret("requestyApiKey") as Promise<string | undefined>,
- this.getGlobalState("requestyModelId") as Promise<string | undefined>,
- this.getGlobalState("requestyModelInfo") as Promise<ModelInfo | undefined>,
- this.getGlobalState("modelTemperature") as Promise<number | undefined>,
- this.getGlobalState("modelMaxTokens") as Promise<number | undefined>,
- this.getGlobalState("anthropicThinking") as Promise<number | undefined>,
- this.getGlobalState("maxOpenTabsContext") as Promise<number | undefined>,
- ])
- let apiProvider: ApiProvider
- if (storedApiProvider) {
- apiProvider = storedApiProvider
- } else {
- // Either new user or legacy user that doesn't have the apiProvider stored in state
- // (If they're using OpenRouter or Bedrock, then apiProvider state will exist)
- if (apiKey) {
- apiProvider = "anthropic"
- } else {
- // New users should default to openrouter
- apiProvider = "openrouter"
- }
- }
- return {
- apiConfiguration: {
- apiProvider,
- apiModelId,
- apiKey,
- glamaApiKey,
- glamaModelId,
- glamaModelInfo,
- openRouterApiKey,
- awsAccessKey,
- awsSecretKey,
- awsSessionToken,
- awsRegion,
- awsUseCrossRegionInference,
- awsProfile,
- awsUseProfile,
- vertexProjectId,
- vertexRegion,
- openAiBaseUrl,
- openAiApiKey,
- openAiModelId,
- openAiCustomModelInfo,
- openAiUseAzure,
- ollamaModelId,
- ollamaBaseUrl,
- lmStudioModelId,
- lmStudioBaseUrl,
- anthropicBaseUrl,
- geminiApiKey,
- openAiNativeApiKey,
- deepSeekApiKey,
- mistralApiKey,
- mistralCodestralUrl,
- azureApiVersion,
- openAiStreamingEnabled,
- openRouterModelId,
- openRouterModelInfo,
- openRouterBaseUrl,
- openRouterUseMiddleOutTransform,
- vsCodeLmModelSelector,
- unboundApiKey,
- unboundModelId,
- unboundModelInfo,
- requestyApiKey,
- requestyModelId,
- requestyModelInfo,
- modelTemperature,
- modelMaxTokens,
- modelMaxThinkingTokens,
- },
- lastShownAnnouncementId,
- customInstructions,
- alwaysAllowReadOnly: alwaysAllowReadOnly ?? false,
- alwaysAllowWrite: alwaysAllowWrite ?? false,
- alwaysAllowExecute: alwaysAllowExecute ?? false,
- alwaysAllowBrowser: alwaysAllowBrowser ?? false,
- alwaysAllowMcp: alwaysAllowMcp ?? false,
- alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false,
- taskHistory,
- allowedCommands,
- soundEnabled: soundEnabled ?? false,
- diffEnabled: diffEnabled ?? true,
- enableCheckpoints: enableCheckpoints ?? true,
- soundVolume,
- browserViewportSize: browserViewportSize ?? "900x600",
- screenshotQuality: screenshotQuality ?? 75,
- fuzzyMatchThreshold: fuzzyMatchThreshold ?? 1.0,
- writeDelayMs: writeDelayMs ?? 1000,
- terminalOutputLineLimit: terminalOutputLineLimit ?? 500,
- mode: mode ?? defaultModeSlug,
- preferredLanguage:
- preferredLanguage ??
- (() => {
- // Get VSCode's locale setting
- const vscodeLang = vscode.env.language
- // Map VSCode locale to our supported languages
- const langMap: { [key: string]: string } = {
- en: "English",
- ar: "Arabic",
- "pt-br": "Brazilian Portuguese",
- ca: "Catalan",
- cs: "Czech",
- fr: "French",
- de: "German",
- hi: "Hindi",
- hu: "Hungarian",
- it: "Italian",
- ja: "Japanese",
- ko: "Korean",
- pl: "Polish",
- pt: "Portuguese",
- ru: "Russian",
- zh: "Simplified Chinese",
- "zh-cn": "Simplified Chinese",
- es: "Spanish",
- "zh-tw": "Traditional Chinese",
- tr: "Turkish",
- }
- // Return mapped language or default to English
- return langMap[vscodeLang] ?? langMap[vscodeLang.split("-")[0]] ?? "English"
- })(),
- mcpEnabled: mcpEnabled ?? true,
- enableMcpServerCreation: enableMcpServerCreation ?? true,
- alwaysApproveResubmit: alwaysApproveResubmit ?? false,
- requestDelaySeconds: Math.max(5, requestDelaySeconds ?? 10),
- rateLimitSeconds: rateLimitSeconds ?? 0,
- currentApiConfigName: currentApiConfigName ?? "default",
- listApiConfigMeta: listApiConfigMeta ?? [],
- modeApiConfigs: modeApiConfigs ?? ({} as Record<Mode, string>),
- customModePrompts: customModePrompts ?? {},
- customSupportPrompts: customSupportPrompts ?? {},
- enhancementApiConfigId,
- experiments: experiments ?? experimentDefault,
- autoApprovalEnabled: autoApprovalEnabled ?? false,
- customModes,
- maxOpenTabsContext: maxOpenTabsContext ?? 20,
- }
- }
- async updateTaskHistory(item: HistoryItem): Promise<HistoryItem[]> {
- const history = ((await 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
- }
- // global
- async updateGlobalState(key: GlobalStateKey, value: any) {
- await this.context.globalState.update(key, value)
- }
- async getGlobalState(key: GlobalStateKey) {
- return await this.context.globalState.get(key)
- }
- // secrets
- public async storeSecret(key: SecretKey, value?: string) {
- if (value) {
- await this.context.secrets.store(key, value)
- } else {
- await this.context.secrets.delete(key)
- }
- }
- private async getSecret(key: SecretKey) {
- return await this.context.secrets.get(key)
- }
- // dev
- async resetState() {
- const answer = await vscode.window.showInformationMessage(
- "Are you sure you want to reset all state and secret storage in the extension? This cannot be undone.",
- { modal: true },
- "Yes",
- )
- if (answer !== "Yes") {
- return
- }
- for (const key of this.context.globalState.keys()) {
- await this.context.globalState.update(key, undefined)
- }
- const secretKeys: SecretKey[] = [
- "apiKey",
- "glamaApiKey",
- "openRouterApiKey",
- "awsAccessKey",
- "awsSecretKey",
- "awsSessionToken",
- "openAiApiKey",
- "geminiApiKey",
- "openAiNativeApiKey",
- "deepSeekApiKey",
- "mistralApiKey",
- "unboundApiKey",
- "requestyApiKey",
- ]
- for (const key of secretKeys) {
- await this.storeSecret(key, undefined)
- }
- await this.configManager.resetAllConfigs()
- await this.customModesManager.resetCustomModes()
- if (this.cline) {
- this.cline.abortTask()
- this.cline = undefined
- }
- await this.postStateToWebview()
- await this.postMessageToWebview({ type: "action", action: "chatButtonClicked" })
- }
- // logging
- public log(message: string) {
- this.outputChannel.appendLine(message)
- }
- // integration tests
- get viewLaunched() {
- return this.isViewLaunched
- }
- get messages() {
- return this.cline?.clineMessages || []
- }
- // Add public getter
- public getMcpHub(): McpHub | undefined {
- return this.mcpHub
- }
- }
|