Cline.ts 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521
  1. import { Anthropic } from "@anthropic-ai/sdk"
  2. import cloneDeep from "clone-deep"
  3. import { DiffStrategy, getDiffStrategy, UnifiedDiffStrategy } from "./diff/DiffStrategy"
  4. import { validateToolUse, isToolAllowedForMode, ToolName } from "./mode-validator"
  5. import delay from "delay"
  6. import fs from "fs/promises"
  7. import os from "os"
  8. import pWaitFor from "p-wait-for"
  9. import getFolderSize from "get-folder-size"
  10. import * as path from "path"
  11. import { serializeError } from "serialize-error"
  12. import * as vscode from "vscode"
  13. import { ApiHandler, SingleCompletionHandler, buildApiHandler } from "../api"
  14. import { ApiStream } from "../api/transform/stream"
  15. import { DIFF_VIEW_URI_SCHEME, DiffViewProvider } from "../integrations/editor/DiffViewProvider"
  16. import { CheckpointService, CheckpointServiceFactory } from "../services/checkpoints"
  17. import { findToolName, formatContentBlockToMarkdown } from "../integrations/misc/export-markdown"
  18. import {
  19. extractTextFromFile,
  20. addLineNumbers,
  21. stripLineNumbers,
  22. everyLineHasLineNumbers,
  23. truncateOutput,
  24. } from "../integrations/misc/extract-text"
  25. import { TerminalManager } from "../integrations/terminal/TerminalManager"
  26. import { UrlContentFetcher } from "../services/browser/UrlContentFetcher"
  27. import { listFiles } from "../services/glob/list-files"
  28. import { regexSearchFiles } from "../services/ripgrep"
  29. import { parseSourceCodeForDefinitionsTopLevel } from "../services/tree-sitter"
  30. import { ApiConfiguration } from "../shared/api"
  31. import { findLastIndex } from "../shared/array"
  32. import { combineApiRequests } from "../shared/combineApiRequests"
  33. import { combineCommandSequences } from "../shared/combineCommandSequences"
  34. import {
  35. BrowserAction,
  36. BrowserActionResult,
  37. browserActions,
  38. ClineApiReqCancelReason,
  39. ClineApiReqInfo,
  40. ClineAsk,
  41. ClineAskUseMcpServer,
  42. ClineMessage,
  43. ClineSay,
  44. ClineSayBrowserAction,
  45. ClineSayTool,
  46. } from "../shared/ExtensionMessage"
  47. import { getApiMetrics } from "../shared/getApiMetrics"
  48. import { HistoryItem } from "../shared/HistoryItem"
  49. import { ClineAskResponse } from "../shared/WebviewMessage"
  50. import { GlobalFileNames } from "../shared/globalFileNames"
  51. import { defaultModeSlug, getModeBySlug, getFullModeDetails } from "../shared/modes"
  52. import { calculateApiCost } from "../utils/cost"
  53. import { fileExistsAtPath } from "../utils/fs"
  54. import { arePathsEqual, getReadablePath } from "../utils/path"
  55. import { parseMentions } from "./mentions"
  56. import { AssistantMessageContent, parseAssistantMessage, ToolParamName, ToolUseName } from "./assistant-message"
  57. import { formatResponse } from "./prompts/responses"
  58. import { SYSTEM_PROMPT } from "./prompts/system"
  59. import { truncateConversationIfNeeded } from "./sliding-window"
  60. import { ClineProvider } from "./webview/ClineProvider"
  61. import { detectCodeOmission } from "../integrations/editor/detect-omission"
  62. import { BrowserSession } from "../services/browser/BrowserSession"
  63. import { McpHub } from "../services/mcp/McpHub"
  64. import crypto from "crypto"
  65. import { insertGroups } from "./diff/insert-groups"
  66. import { EXPERIMENT_IDS, experiments as Experiments, ExperimentId } from "../shared/experiments"
  67. const cwd =
  68. vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution
  69. type ToolResponse = string | Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam>
  70. type UserContent = Array<Anthropic.Messages.ContentBlockParam>
  71. export type ClineOptions = {
  72. provider: ClineProvider
  73. apiConfiguration: ApiConfiguration
  74. customInstructions?: string
  75. enableDiff?: boolean
  76. enableCheckpoints?: boolean
  77. fuzzyMatchThreshold?: number
  78. task?: string
  79. images?: string[]
  80. historyItem?: HistoryItem
  81. experiments?: Record<string, boolean>
  82. startTask?: boolean
  83. }
  84. export class Cline {
  85. readonly taskId: string
  86. readonly apiConfiguration: ApiConfiguration
  87. api: ApiHandler
  88. private terminalManager: TerminalManager
  89. private urlContentFetcher: UrlContentFetcher
  90. private browserSession: BrowserSession
  91. private didEditFile: boolean = false
  92. customInstructions?: string
  93. diffStrategy?: DiffStrategy
  94. diffEnabled: boolean = false
  95. fuzzyMatchThreshold: number = 1.0
  96. apiConversationHistory: (Anthropic.MessageParam & { ts?: number })[] = []
  97. clineMessages: ClineMessage[] = []
  98. private askResponse?: ClineAskResponse
  99. private askResponseText?: string
  100. private askResponseImages?: string[]
  101. private lastMessageTs?: number
  102. private consecutiveMistakeCount: number = 0
  103. private consecutiveMistakeCountForApplyDiff: Map<string, number> = new Map()
  104. private providerRef: WeakRef<ClineProvider>
  105. private abort: boolean = false
  106. didFinishAbortingStream = false
  107. abandoned = false
  108. private diffViewProvider: DiffViewProvider
  109. private lastApiRequestTime?: number
  110. isInitialized = false
  111. // checkpoints
  112. enableCheckpoints: boolean = false
  113. private checkpointService?: CheckpointService
  114. // streaming
  115. isWaitingForFirstChunk = false
  116. isStreaming = false
  117. private currentStreamingContentIndex = 0
  118. private assistantMessageContent: AssistantMessageContent[] = []
  119. private presentAssistantMessageLocked = false
  120. private presentAssistantMessageHasPendingUpdates = false
  121. private userMessageContent: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[] = []
  122. private userMessageContentReady = false
  123. private didRejectTool = false
  124. private didAlreadyUseTool = false
  125. private didCompleteReadingStream = false
  126. constructor({
  127. provider,
  128. apiConfiguration,
  129. customInstructions,
  130. enableDiff,
  131. enableCheckpoints,
  132. fuzzyMatchThreshold,
  133. task,
  134. images,
  135. historyItem,
  136. experiments,
  137. startTask = true,
  138. }: ClineOptions) {
  139. if (startTask && !task && !images && !historyItem) {
  140. throw new Error("Either historyItem or task/images must be provided")
  141. }
  142. this.taskId = crypto.randomUUID()
  143. this.apiConfiguration = apiConfiguration
  144. this.api = buildApiHandler(apiConfiguration)
  145. this.terminalManager = new TerminalManager()
  146. this.urlContentFetcher = new UrlContentFetcher(provider.context)
  147. this.browserSession = new BrowserSession(provider.context)
  148. this.customInstructions = customInstructions
  149. this.diffEnabled = enableDiff ?? false
  150. this.fuzzyMatchThreshold = fuzzyMatchThreshold ?? 1.0
  151. this.providerRef = new WeakRef(provider)
  152. this.diffViewProvider = new DiffViewProvider(cwd)
  153. this.enableCheckpoints = enableCheckpoints ?? false
  154. if (historyItem) {
  155. this.taskId = historyItem.id
  156. }
  157. // Initialize diffStrategy based on current state
  158. this.updateDiffStrategy(Experiments.isEnabled(experiments ?? {}, EXPERIMENT_IDS.DIFF_STRATEGY))
  159. if (startTask) {
  160. if (task || images) {
  161. this.startTask(task, images)
  162. } else if (historyItem) {
  163. this.resumeTaskFromHistory()
  164. } else {
  165. throw new Error("Either historyItem or task/images must be provided")
  166. }
  167. }
  168. }
  169. static create(options: ClineOptions): [Cline, Promise<void>] {
  170. const instance = new Cline({ ...options, startTask: false })
  171. const { images, task, historyItem } = options
  172. let promise
  173. if (images || task) {
  174. promise = instance.startTask(task, images)
  175. } else if (historyItem) {
  176. promise = instance.resumeTaskFromHistory()
  177. } else {
  178. throw new Error("Either historyItem or task/images must be provided")
  179. }
  180. return [instance, promise]
  181. }
  182. // Add method to update diffStrategy
  183. async updateDiffStrategy(experimentalDiffStrategy?: boolean) {
  184. // If not provided, get from current state
  185. if (experimentalDiffStrategy === undefined) {
  186. const { experiments: stateExperimental } = (await this.providerRef.deref()?.getState()) ?? {}
  187. experimentalDiffStrategy = stateExperimental?.[EXPERIMENT_IDS.DIFF_STRATEGY] ?? false
  188. }
  189. this.diffStrategy = getDiffStrategy(this.api.getModel().id, this.fuzzyMatchThreshold, experimentalDiffStrategy)
  190. }
  191. // Storing task to disk for history
  192. private async ensureTaskDirectoryExists(): Promise<string> {
  193. const globalStoragePath = this.providerRef.deref()?.context.globalStorageUri.fsPath
  194. if (!globalStoragePath) {
  195. throw new Error("Global storage uri is invalid")
  196. }
  197. const taskDir = path.join(globalStoragePath, "tasks", this.taskId)
  198. await fs.mkdir(taskDir, { recursive: true })
  199. return taskDir
  200. }
  201. private async getSavedApiConversationHistory(): Promise<Anthropic.MessageParam[]> {
  202. const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.apiConversationHistory)
  203. const fileExists = await fileExistsAtPath(filePath)
  204. if (fileExists) {
  205. return JSON.parse(await fs.readFile(filePath, "utf8"))
  206. }
  207. return []
  208. }
  209. private async addToApiConversationHistory(message: Anthropic.MessageParam) {
  210. const messageWithTs = { ...message, ts: Date.now() }
  211. this.apiConversationHistory.push(messageWithTs)
  212. await this.saveApiConversationHistory()
  213. }
  214. async overwriteApiConversationHistory(newHistory: Anthropic.MessageParam[]) {
  215. this.apiConversationHistory = newHistory
  216. await this.saveApiConversationHistory()
  217. }
  218. private async saveApiConversationHistory() {
  219. try {
  220. const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.apiConversationHistory)
  221. await fs.writeFile(filePath, JSON.stringify(this.apiConversationHistory))
  222. } catch (error) {
  223. // in the off chance this fails, we don't want to stop the task
  224. console.error("Failed to save API conversation history:", error)
  225. }
  226. }
  227. private async getSavedClineMessages(): Promise<ClineMessage[]> {
  228. const filePath = path.join(await this.ensureTaskDirectoryExists(), GlobalFileNames.uiMessages)
  229. if (await fileExistsAtPath(filePath)) {
  230. return JSON.parse(await fs.readFile(filePath, "utf8"))
  231. } else {
  232. // check old location
  233. const oldPath = path.join(await this.ensureTaskDirectoryExists(), "claude_messages.json")
  234. if (await fileExistsAtPath(oldPath)) {
  235. const data = JSON.parse(await fs.readFile(oldPath, "utf8"))
  236. await fs.unlink(oldPath) // remove old file
  237. return data
  238. }
  239. }
  240. return []
  241. }
  242. private async addToClineMessages(message: ClineMessage) {
  243. this.clineMessages.push(message)
  244. await this.saveClineMessages()
  245. }
  246. public async overwriteClineMessages(newMessages: ClineMessage[]) {
  247. this.clineMessages = newMessages
  248. await this.saveClineMessages()
  249. }
  250. private async saveClineMessages() {
  251. try {
  252. const taskDir = await this.ensureTaskDirectoryExists()
  253. const filePath = path.join(taskDir, GlobalFileNames.uiMessages)
  254. await fs.writeFile(filePath, JSON.stringify(this.clineMessages))
  255. // combined as they are in ChatView
  256. const apiMetrics = getApiMetrics(combineApiRequests(combineCommandSequences(this.clineMessages.slice(1))))
  257. const taskMessage = this.clineMessages[0] // first message is always the task say
  258. const lastRelevantMessage =
  259. this.clineMessages[
  260. findLastIndex(
  261. this.clineMessages,
  262. (m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task"),
  263. )
  264. ]
  265. let taskDirSize = 0
  266. try {
  267. taskDirSize = await getFolderSize.loose(taskDir)
  268. } catch (err) {
  269. console.error(
  270. `[saveClineMessages] failed to get task directory size (${taskDir}): ${err instanceof Error ? err.message : String(err)}`,
  271. )
  272. }
  273. await this.providerRef.deref()?.updateTaskHistory({
  274. id: this.taskId,
  275. ts: lastRelevantMessage.ts,
  276. task: taskMessage.text ?? "",
  277. tokensIn: apiMetrics.totalTokensIn,
  278. tokensOut: apiMetrics.totalTokensOut,
  279. cacheWrites: apiMetrics.totalCacheWrites,
  280. cacheReads: apiMetrics.totalCacheReads,
  281. totalCost: apiMetrics.totalCost,
  282. size: taskDirSize,
  283. })
  284. } catch (error) {
  285. console.error("Failed to save cline messages:", error)
  286. }
  287. }
  288. // Communicate with webview
  289. // partial has three valid states true (partial message), false (completion of partial message), undefined (individual complete message)
  290. async ask(
  291. type: ClineAsk,
  292. text?: string,
  293. partial?: boolean,
  294. ): Promise<{ response: ClineAskResponse; text?: string; images?: string[] }> {
  295. // If this Cline instance was aborted by the provider, then the only thing keeping us alive is a promise still running in the background, in which case we don't want to send its result to the webview as it is attached to a new instance of Cline now. So we can safely ignore the result of any active promises, and this class will be deallocated. (Although we set Cline = undefined in provider, that simply removes the reference to this instance, but the instance is still alive until this promise resolves or rejects.)
  296. if (this.abort) {
  297. throw new Error("Roo Code instance aborted")
  298. }
  299. let askTs: number
  300. if (partial !== undefined) {
  301. const lastMessage = this.clineMessages.at(-1)
  302. const isUpdatingPreviousPartial =
  303. lastMessage && lastMessage.partial && lastMessage.type === "ask" && lastMessage.ask === type
  304. if (partial) {
  305. if (isUpdatingPreviousPartial) {
  306. // existing partial message, so update it
  307. lastMessage.text = text
  308. lastMessage.partial = partial
  309. // todo be more efficient about saving and posting only new data or one whole message at a time so ignore partial for saves, and only post parts of partial message instead of whole array in new listener
  310. // await this.saveClineMessages()
  311. // await this.providerRef.deref()?.postStateToWebview()
  312. await this.providerRef
  313. .deref()
  314. ?.postMessageToWebview({ type: "partialMessage", partialMessage: lastMessage })
  315. throw new Error("Current ask promise was ignored 1")
  316. } else {
  317. // this is a new partial message, so add it with partial state
  318. // this.askResponse = undefined
  319. // this.askResponseText = undefined
  320. // this.askResponseImages = undefined
  321. askTs = Date.now()
  322. this.lastMessageTs = askTs
  323. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, partial })
  324. await this.providerRef.deref()?.postStateToWebview()
  325. throw new Error("Current ask promise was ignored 2")
  326. }
  327. } else {
  328. // partial=false means its a complete version of a previously partial message
  329. if (isUpdatingPreviousPartial) {
  330. // this is the complete version of a previously partial message, so replace the partial with the complete version
  331. this.askResponse = undefined
  332. this.askResponseText = undefined
  333. this.askResponseImages = undefined
  334. /*
  335. Bug for the history books:
  336. In the webview we use the ts as the chatrow key for the virtuoso list. Since we would update this ts right at the end of streaming, it would cause the view to flicker. The key prop has to be stable otherwise react has trouble reconciling items between renders, causing unmounting and remounting of components (flickering).
  337. The lesson here is if you see flickering when rendering lists, it's likely because the key prop is not stable.
  338. So in this case we must make sure that the message ts is never altered after first setting it.
  339. */
  340. askTs = lastMessage.ts
  341. this.lastMessageTs = askTs
  342. // lastMessage.ts = askTs
  343. lastMessage.text = text
  344. lastMessage.partial = false
  345. await this.saveClineMessages()
  346. // await this.providerRef.deref()?.postStateToWebview()
  347. await this.providerRef
  348. .deref()
  349. ?.postMessageToWebview({ type: "partialMessage", partialMessage: lastMessage })
  350. } else {
  351. // this is a new partial=false message, so add it like normal
  352. this.askResponse = undefined
  353. this.askResponseText = undefined
  354. this.askResponseImages = undefined
  355. askTs = Date.now()
  356. this.lastMessageTs = askTs
  357. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text })
  358. await this.providerRef.deref()?.postStateToWebview()
  359. }
  360. }
  361. } else {
  362. // this is a new non-partial message, so add it like normal
  363. // const lastMessage = this.clineMessages.at(-1)
  364. this.askResponse = undefined
  365. this.askResponseText = undefined
  366. this.askResponseImages = undefined
  367. askTs = Date.now()
  368. this.lastMessageTs = askTs
  369. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text })
  370. await this.providerRef.deref()?.postStateToWebview()
  371. }
  372. await pWaitFor(() => this.askResponse !== undefined || this.lastMessageTs !== askTs, { interval: 100 })
  373. if (this.lastMessageTs !== askTs) {
  374. throw new Error("Current ask promise was ignored") // could happen if we send multiple asks in a row i.e. with command_output. It's important that when we know an ask could fail, it is handled gracefully
  375. }
  376. const result = { response: this.askResponse!, text: this.askResponseText, images: this.askResponseImages }
  377. this.askResponse = undefined
  378. this.askResponseText = undefined
  379. this.askResponseImages = undefined
  380. return result
  381. }
  382. async handleWebviewAskResponse(askResponse: ClineAskResponse, text?: string, images?: string[]) {
  383. this.askResponse = askResponse
  384. this.askResponseText = text
  385. this.askResponseImages = images
  386. }
  387. async say(
  388. type: ClineSay,
  389. text?: string,
  390. images?: string[],
  391. partial?: boolean,
  392. checkpoint?: Record<string, unknown>,
  393. ): Promise<undefined> {
  394. if (this.abort) {
  395. throw new Error("Roo Code instance aborted")
  396. }
  397. if (partial !== undefined) {
  398. const lastMessage = this.clineMessages.at(-1)
  399. const isUpdatingPreviousPartial =
  400. lastMessage && lastMessage.partial && lastMessage.type === "say" && lastMessage.say === type
  401. if (partial) {
  402. if (isUpdatingPreviousPartial) {
  403. // existing partial message, so update it
  404. lastMessage.text = text
  405. lastMessage.images = images
  406. lastMessage.partial = partial
  407. await this.providerRef
  408. .deref()
  409. ?.postMessageToWebview({ type: "partialMessage", partialMessage: lastMessage })
  410. } else {
  411. // this is a new partial message, so add it with partial state
  412. const sayTs = Date.now()
  413. this.lastMessageTs = sayTs
  414. await this.addToClineMessages({ ts: sayTs, type: "say", say: type, text, images, partial })
  415. await this.providerRef.deref()?.postStateToWebview()
  416. }
  417. } else {
  418. // partial=false means its a complete version of a previously partial message
  419. if (isUpdatingPreviousPartial) {
  420. // this is the complete version of a previously partial message, so replace the partial with the complete version
  421. this.lastMessageTs = lastMessage.ts
  422. // lastMessage.ts = sayTs
  423. lastMessage.text = text
  424. lastMessage.images = images
  425. lastMessage.partial = false
  426. // instead of streaming partialMessage events, we do a save and post like normal to persist to disk
  427. await this.saveClineMessages()
  428. // await this.providerRef.deref()?.postStateToWebview()
  429. await this.providerRef
  430. .deref()
  431. ?.postMessageToWebview({ type: "partialMessage", partialMessage: lastMessage }) // more performant than an entire postStateToWebview
  432. } else {
  433. // this is a new partial=false message, so add it like normal
  434. const sayTs = Date.now()
  435. this.lastMessageTs = sayTs
  436. await this.addToClineMessages({ ts: sayTs, type: "say", say: type, text, images })
  437. await this.providerRef.deref()?.postStateToWebview()
  438. }
  439. }
  440. } else {
  441. // this is a new non-partial message, so add it like normal
  442. const sayTs = Date.now()
  443. this.lastMessageTs = sayTs
  444. await this.addToClineMessages({ ts: sayTs, type: "say", say: type, text, images, checkpoint })
  445. await this.providerRef.deref()?.postStateToWebview()
  446. }
  447. }
  448. async sayAndCreateMissingParamError(toolName: ToolUseName, paramName: string, relPath?: string) {
  449. await this.say(
  450. "error",
  451. `Roo tried to use ${toolName}${
  452. relPath ? ` for '${relPath.toPosix()}'` : ""
  453. } without value for required parameter '${paramName}'. Retrying...`,
  454. )
  455. return formatResponse.toolError(formatResponse.missingToolParameterError(paramName))
  456. }
  457. // Task lifecycle
  458. private async startTask(task?: string, images?: string[]): Promise<void> {
  459. // conversationHistory (for API) and clineMessages (for webview) need to be in sync
  460. // if the extension process were killed, then on restart the clineMessages might not be empty, so we need to set it to [] when we create a new Cline client (otherwise webview would show stale messages from previous session)
  461. this.clineMessages = []
  462. this.apiConversationHistory = []
  463. await this.providerRef.deref()?.postStateToWebview()
  464. await this.say("text", task, images)
  465. this.isInitialized = true
  466. let imageBlocks: Anthropic.ImageBlockParam[] = formatResponse.imageBlocks(images)
  467. await this.initiateTaskLoop([
  468. {
  469. type: "text",
  470. text: `<task>\n${task}\n</task>`,
  471. },
  472. ...imageBlocks,
  473. ])
  474. }
  475. private async resumeTaskFromHistory() {
  476. const modifiedClineMessages = await this.getSavedClineMessages()
  477. // Remove any resume messages that may have been added before
  478. const lastRelevantMessageIndex = findLastIndex(
  479. modifiedClineMessages,
  480. (m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task"),
  481. )
  482. if (lastRelevantMessageIndex !== -1) {
  483. modifiedClineMessages.splice(lastRelevantMessageIndex + 1)
  484. }
  485. // since we don't use api_req_finished anymore, we need to check if the last api_req_started has a cost value, if it doesn't and no cancellation reason to present, then we remove it since it indicates an api request without any partial content streamed
  486. const lastApiReqStartedIndex = findLastIndex(
  487. modifiedClineMessages,
  488. (m) => m.type === "say" && m.say === "api_req_started",
  489. )
  490. if (lastApiReqStartedIndex !== -1) {
  491. const lastApiReqStarted = modifiedClineMessages[lastApiReqStartedIndex]
  492. const { cost, cancelReason }: ClineApiReqInfo = JSON.parse(lastApiReqStarted.text || "{}")
  493. if (cost === undefined && cancelReason === undefined) {
  494. modifiedClineMessages.splice(lastApiReqStartedIndex, 1)
  495. }
  496. }
  497. await this.overwriteClineMessages(modifiedClineMessages)
  498. this.clineMessages = await this.getSavedClineMessages()
  499. // Now present the cline messages to the user and ask if they want to
  500. // resume (NOTE: we ran into a bug before where the
  501. // apiConversationHistory wouldn't be initialized when opening a old
  502. // task, and it was because we were waiting for resume).
  503. // This is important in case the user deletes messages without resuming
  504. // the task first.
  505. this.apiConversationHistory = await this.getSavedApiConversationHistory()
  506. const lastClineMessage = this.clineMessages
  507. .slice()
  508. .reverse()
  509. .find((m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task")) // could be multiple resume tasks
  510. // const lastClineMessage = this.clineMessages[lastClineMessageIndex]
  511. // could be a completion result with a command
  512. // const secondLastClineMessage = this.clineMessages
  513. // .slice()
  514. // .reverse()
  515. // .find(
  516. // (m, index) =>
  517. // index !== lastClineMessageIndex && !(m.ask === "resume_task" || m.ask === "resume_completed_task")
  518. // )
  519. // (lastClineMessage?.ask === "command" && secondLastClineMessage?.ask === "completion_result")
  520. let askType: ClineAsk
  521. if (lastClineMessage?.ask === "completion_result") {
  522. askType = "resume_completed_task"
  523. } else {
  524. askType = "resume_task"
  525. }
  526. this.isInitialized = true
  527. const { response, text, images } = await this.ask(askType) // calls poststatetowebview
  528. let responseText: string | undefined
  529. let responseImages: string[] | undefined
  530. if (response === "messageResponse") {
  531. await this.say("user_feedback", text, images)
  532. responseText = text
  533. responseImages = images
  534. }
  535. // Make sure that the api conversation history can be resumed by the API,
  536. // even if it goes out of sync with cline messages.
  537. let existingApiConversationHistory: Anthropic.Messages.MessageParam[] =
  538. await this.getSavedApiConversationHistory()
  539. // v2.0 xml tags refactor caveat: since we don't use tools anymore, we need to replace all tool use blocks with a text block since the API disallows conversations with tool uses and no tool schema
  540. const conversationWithoutToolBlocks = existingApiConversationHistory.map((message) => {
  541. if (Array.isArray(message.content)) {
  542. const newContent = message.content.map((block) => {
  543. if (block.type === "tool_use") {
  544. // it's important we convert to the new tool schema format so the model doesn't get confused about how to invoke tools
  545. const inputAsXml = Object.entries(block.input as Record<string, string>)
  546. .map(([key, value]) => `<${key}>\n${value}\n</${key}>`)
  547. .join("\n")
  548. return {
  549. type: "text",
  550. text: `<${block.name}>\n${inputAsXml}\n</${block.name}>`,
  551. } as Anthropic.Messages.TextBlockParam
  552. } else if (block.type === "tool_result") {
  553. // Convert block.content to text block array, removing images
  554. const contentAsTextBlocks = Array.isArray(block.content)
  555. ? block.content.filter((item) => item.type === "text")
  556. : [{ type: "text", text: block.content }]
  557. const textContent = contentAsTextBlocks.map((item) => item.text).join("\n\n")
  558. const toolName = findToolName(block.tool_use_id, existingApiConversationHistory)
  559. return {
  560. type: "text",
  561. text: `[${toolName} Result]\n\n${textContent}`,
  562. } as Anthropic.Messages.TextBlockParam
  563. }
  564. return block
  565. })
  566. return { ...message, content: newContent }
  567. }
  568. return message
  569. })
  570. existingApiConversationHistory = conversationWithoutToolBlocks
  571. // FIXME: remove tool use blocks altogether
  572. // if the last message is an assistant message, we need to check if there's tool use since every tool use has to have a tool response
  573. // if there's no tool use and only a text block, then we can just add a user message
  574. // (note this isn't relevant anymore since we use custom tool prompts instead of tool use blocks, but this is here for legacy purposes in case users resume old tasks)
  575. // if the last message is a user message, we can need to get the assistant message before it to see if it made tool calls, and if so, fill in the remaining tool responses with 'interrupted'
  576. let modifiedOldUserContent: UserContent // either the last message if its user message, or the user message before the last (assistant) message
  577. let modifiedApiConversationHistory: Anthropic.Messages.MessageParam[] // need to remove the last user message to replace with new modified user message
  578. if (existingApiConversationHistory.length > 0) {
  579. const lastMessage = existingApiConversationHistory[existingApiConversationHistory.length - 1]
  580. if (lastMessage.role === "assistant") {
  581. const content = Array.isArray(lastMessage.content)
  582. ? lastMessage.content
  583. : [{ type: "text", text: lastMessage.content }]
  584. const hasToolUse = content.some((block) => block.type === "tool_use")
  585. if (hasToolUse) {
  586. const toolUseBlocks = content.filter(
  587. (block) => block.type === "tool_use",
  588. ) as Anthropic.Messages.ToolUseBlock[]
  589. const toolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks.map((block) => ({
  590. type: "tool_result",
  591. tool_use_id: block.id,
  592. content: "Task was interrupted before this tool call could be completed.",
  593. }))
  594. modifiedApiConversationHistory = [...existingApiConversationHistory] // no changes
  595. modifiedOldUserContent = [...toolResponses]
  596. } else {
  597. modifiedApiConversationHistory = [...existingApiConversationHistory]
  598. modifiedOldUserContent = []
  599. }
  600. } else if (lastMessage.role === "user") {
  601. const previousAssistantMessage: Anthropic.Messages.MessageParam | undefined =
  602. existingApiConversationHistory[existingApiConversationHistory.length - 2]
  603. const existingUserContent: UserContent = Array.isArray(lastMessage.content)
  604. ? lastMessage.content
  605. : [{ type: "text", text: lastMessage.content }]
  606. if (previousAssistantMessage && previousAssistantMessage.role === "assistant") {
  607. const assistantContent = Array.isArray(previousAssistantMessage.content)
  608. ? previousAssistantMessage.content
  609. : [{ type: "text", text: previousAssistantMessage.content }]
  610. const toolUseBlocks = assistantContent.filter(
  611. (block) => block.type === "tool_use",
  612. ) as Anthropic.Messages.ToolUseBlock[]
  613. if (toolUseBlocks.length > 0) {
  614. const existingToolResults = existingUserContent.filter(
  615. (block) => block.type === "tool_result",
  616. ) as Anthropic.ToolResultBlockParam[]
  617. const missingToolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks
  618. .filter(
  619. (toolUse) => !existingToolResults.some((result) => result.tool_use_id === toolUse.id),
  620. )
  621. .map((toolUse) => ({
  622. type: "tool_result",
  623. tool_use_id: toolUse.id,
  624. content: "Task was interrupted before this tool call could be completed.",
  625. }))
  626. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1) // removes the last user message
  627. modifiedOldUserContent = [...existingUserContent, ...missingToolResponses]
  628. } else {
  629. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  630. modifiedOldUserContent = [...existingUserContent]
  631. }
  632. } else {
  633. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  634. modifiedOldUserContent = [...existingUserContent]
  635. }
  636. } else {
  637. throw new Error("Unexpected: Last message is not a user or assistant message")
  638. }
  639. } else {
  640. throw new Error("Unexpected: No existing API conversation history")
  641. }
  642. let newUserContent: UserContent = [...modifiedOldUserContent]
  643. const agoText = ((): string => {
  644. const timestamp = lastClineMessage?.ts ?? Date.now()
  645. const now = Date.now()
  646. const diff = now - timestamp
  647. const minutes = Math.floor(diff / 60000)
  648. const hours = Math.floor(minutes / 60)
  649. const days = Math.floor(hours / 24)
  650. if (days > 0) {
  651. return `${days} day${days > 1 ? "s" : ""} ago`
  652. }
  653. if (hours > 0) {
  654. return `${hours} hour${hours > 1 ? "s" : ""} ago`
  655. }
  656. if (minutes > 0) {
  657. return `${minutes} minute${minutes > 1 ? "s" : ""} ago`
  658. }
  659. return "just now"
  660. })()
  661. const wasRecent = lastClineMessage?.ts && Date.now() - lastClineMessage.ts < 30_000
  662. newUserContent.push({
  663. type: "text",
  664. text:
  665. `[TASK RESUMPTION] This task was interrupted ${agoText}. It may or may not be complete, so please reassess the task context. Be aware that the project state may have changed since then. The current working directory is now '${cwd.toPosix()}'. If the task has not been completed, retry the last step before interruption and proceed with completing the task.\n\nNote: If you previously attempted a tool use that the user did not provide a result for, you should assume the tool use was not successful and assess whether you should retry. If the last tool was a browser_action, the browser has been closed and you must launch a new browser if needed.${
  666. wasRecent
  667. ? "\n\nIMPORTANT: If the last tool use was a write_to_file that was interrupted, the file was reverted back to its original state before the interrupted edit, and you do NOT need to re-read the file as you already have its up-to-date contents."
  668. : ""
  669. }` +
  670. (responseText
  671. ? `\n\nNew instructions for task continuation:\n<user_message>\n${responseText}\n</user_message>`
  672. : ""),
  673. })
  674. if (responseImages && responseImages.length > 0) {
  675. newUserContent.push(...formatResponse.imageBlocks(responseImages))
  676. }
  677. await this.overwriteApiConversationHistory(modifiedApiConversationHistory)
  678. await this.initiateTaskLoop(newUserContent)
  679. }
  680. private async initiateTaskLoop(userContent: UserContent): Promise<void> {
  681. let nextUserContent = userContent
  682. let includeFileDetails = true
  683. while (!this.abort) {
  684. const didEndLoop = await this.recursivelyMakeClineRequests(nextUserContent, includeFileDetails)
  685. includeFileDetails = false // we only need file details the first time
  686. // The way this agentic loop works is that cline will be given a task that he then calls tools to complete. unless there's an attempt_completion call, we keep responding back to him with his tool's responses until he either attempt_completion or does not use anymore tools. If he does not use anymore tools, we ask him to consider if he's completed the task and then call attempt_completion, otherwise proceed with completing the task.
  687. // There is a MAX_REQUESTS_PER_TASK limit to prevent infinite requests, but Cline is prompted to finish the task as efficiently as he can.
  688. //const totalCost = this.calculateApiCost(totalInputTokens, totalOutputTokens)
  689. if (didEndLoop) {
  690. // For now a task never 'completes'. This will only happen if the user hits max requests and denies resetting the count.
  691. //this.say("task_completed", `Task completed. Total API usage cost: ${totalCost}`)
  692. break
  693. } else {
  694. // this.say(
  695. // "tool",
  696. // "Cline responded with only text blocks but has not called attempt_completion yet. Forcing him to continue with task..."
  697. // )
  698. nextUserContent = [
  699. {
  700. type: "text",
  701. text: formatResponse.noToolsUsed(),
  702. },
  703. ]
  704. this.consecutiveMistakeCount++
  705. }
  706. }
  707. }
  708. async abortTask(isAbandoned = false) {
  709. // Will stop any autonomously running promises.
  710. if (isAbandoned) {
  711. this.abandoned = true
  712. }
  713. this.abort = true
  714. this.terminalManager.disposeAll()
  715. this.urlContentFetcher.closeBrowser()
  716. this.browserSession.closeBrowser()
  717. // If we're not streaming then `abortStream` (which reverts the diff
  718. // view changes) won't be called, so we need to revert the changes here.
  719. if (this.isStreaming && this.diffViewProvider.isEditing) {
  720. await this.diffViewProvider.revertChanges()
  721. }
  722. }
  723. // Tools
  724. async executeCommandTool(command: string): Promise<[boolean, ToolResponse]> {
  725. const terminalInfo = await this.terminalManager.getOrCreateTerminal(cwd)
  726. terminalInfo.terminal.show() // weird visual bug when creating new terminals (even manually) where there's an empty space at the top.
  727. const process = this.terminalManager.runCommand(terminalInfo, command)
  728. let userFeedback: { text?: string; images?: string[] } | undefined
  729. let didContinue = false
  730. const sendCommandOutput = async (line: string): Promise<void> => {
  731. try {
  732. const { response, text, images } = await this.ask("command_output", line)
  733. if (response === "yesButtonClicked") {
  734. // proceed while running
  735. } else {
  736. userFeedback = { text, images }
  737. }
  738. didContinue = true
  739. process.continue() // continue past the await
  740. } catch {
  741. // This can only happen if this ask promise was ignored, so ignore this error
  742. }
  743. }
  744. let lines: string[] = []
  745. process.on("line", (line) => {
  746. lines.push(line)
  747. if (!didContinue) {
  748. sendCommandOutput(line)
  749. } else {
  750. this.say("command_output", line)
  751. }
  752. })
  753. let completed = false
  754. process.once("completed", () => {
  755. completed = true
  756. })
  757. process.once("no_shell_integration", async () => {
  758. await this.say("shell_integration_warning")
  759. })
  760. await process
  761. // Wait for a short delay to ensure all messages are sent to the webview
  762. // This delay allows time for non-awaited promises to be created and
  763. // for their associated messages to be sent to the webview, maintaining
  764. // the correct order of messages (although the webview is smart about
  765. // grouping command_output messages despite any gaps anyways)
  766. await delay(50)
  767. const { terminalOutputLineLimit } = (await this.providerRef.deref()?.getState()) ?? {}
  768. const output = truncateOutput(lines.join("\n"), terminalOutputLineLimit)
  769. const result = output.trim()
  770. if (userFeedback) {
  771. await this.say("user_feedback", userFeedback.text, userFeedback.images)
  772. return [
  773. true,
  774. formatResponse.toolResult(
  775. `Command is still running in the user's terminal.${
  776. result.length > 0 ? `\nHere's the output so far:\n${result}` : ""
  777. }\n\nThe user provided the following feedback:\n<feedback>\n${userFeedback.text}\n</feedback>`,
  778. userFeedback.images,
  779. ),
  780. ]
  781. }
  782. if (completed) {
  783. return [false, `Command executed.${result.length > 0 ? `\nOutput:\n${result}` : ""}`]
  784. } else {
  785. return [
  786. false,
  787. `Command is still running in the user's terminal.${
  788. result.length > 0 ? `\nHere's the output so far:\n${result}` : ""
  789. }\n\nYou will be updated on the terminal status and new output in the future.`,
  790. ]
  791. }
  792. }
  793. async *attemptApiRequest(previousApiReqIndex: number, retryAttempt: number = 0): ApiStream {
  794. let mcpHub: McpHub | undefined
  795. const { mcpEnabled, alwaysApproveResubmit, requestDelaySeconds, rateLimitSeconds } =
  796. (await this.providerRef.deref()?.getState()) ?? {}
  797. let rateLimitDelay = 0
  798. // Only apply rate limiting if this isn't the first request
  799. if (this.lastApiRequestTime) {
  800. const now = Date.now()
  801. const timeSinceLastRequest = now - this.lastApiRequestTime
  802. const rateLimit = rateLimitSeconds || 0
  803. rateLimitDelay = Math.ceil(Math.max(0, rateLimit * 1000 - timeSinceLastRequest) / 1000)
  804. }
  805. // Only show rate limiting message if we're not retrying. If retrying, we'll include the delay there.
  806. if (rateLimitDelay > 0 && retryAttempt === 0) {
  807. // Show countdown timer
  808. for (let i = rateLimitDelay; i > 0; i--) {
  809. const delayMessage = `Rate limiting for ${i} seconds...`
  810. await this.say("api_req_retry_delayed", delayMessage, undefined, true)
  811. await delay(1000)
  812. }
  813. }
  814. // Update last request time before making the request
  815. this.lastApiRequestTime = Date.now()
  816. if (mcpEnabled ?? true) {
  817. mcpHub = this.providerRef.deref()?.getMcpHub()
  818. if (!mcpHub) {
  819. throw new Error("MCP hub not available")
  820. }
  821. // Wait for MCP servers to be connected before generating system prompt
  822. await pWaitFor(() => mcpHub!.isConnecting !== true, { timeout: 10_000 }).catch(() => {
  823. console.error("MCP servers failed to connect in time")
  824. })
  825. }
  826. const {
  827. browserViewportSize,
  828. mode,
  829. customModePrompts,
  830. preferredLanguage,
  831. experiments,
  832. enableMcpServerCreation,
  833. } = (await this.providerRef.deref()?.getState()) ?? {}
  834. const { customModes } = (await this.providerRef.deref()?.getState()) ?? {}
  835. const systemPrompt = await (async () => {
  836. const provider = this.providerRef.deref()
  837. if (!provider) {
  838. throw new Error("Provider not available")
  839. }
  840. return SYSTEM_PROMPT(
  841. provider.context,
  842. cwd,
  843. this.api.getModel().info.supportsComputerUse ?? false,
  844. mcpHub,
  845. this.diffStrategy,
  846. browserViewportSize,
  847. mode,
  848. customModePrompts,
  849. customModes,
  850. this.customInstructions,
  851. preferredLanguage,
  852. this.diffEnabled,
  853. experiments,
  854. enableMcpServerCreation,
  855. )
  856. })()
  857. // If the previous API request's total token usage is close to the context window, truncate the conversation history to free up space for the new request
  858. if (previousApiReqIndex >= 0) {
  859. const previousRequest = this.clineMessages[previousApiReqIndex]?.text
  860. if (!previousRequest) return
  861. const {
  862. tokensIn = 0,
  863. tokensOut = 0,
  864. cacheWrites = 0,
  865. cacheReads = 0,
  866. }: ClineApiReqInfo = JSON.parse(previousRequest)
  867. const totalTokens = tokensIn + tokensOut + cacheWrites + cacheReads
  868. const modelInfo = this.api.getModel().info
  869. const maxTokens = modelInfo.thinking
  870. ? this.apiConfiguration.modelMaxTokens || modelInfo.maxTokens
  871. : modelInfo.maxTokens
  872. const contextWindow = modelInfo.contextWindow
  873. const trimmedMessages = truncateConversationIfNeeded({
  874. messages: this.apiConversationHistory,
  875. totalTokens,
  876. maxTokens,
  877. contextWindow,
  878. })
  879. if (trimmedMessages !== this.apiConversationHistory) {
  880. await this.overwriteApiConversationHistory(trimmedMessages)
  881. }
  882. }
  883. // Clean conversation history by:
  884. // 1. Converting to Anthropic.MessageParam by spreading only the API-required properties
  885. // 2. Converting image blocks to text descriptions if model doesn't support images
  886. const cleanConversationHistory = this.apiConversationHistory.map(({ role, content }) => {
  887. // Handle array content (could contain image blocks)
  888. if (Array.isArray(content)) {
  889. if (!this.api.getModel().info.supportsImages) {
  890. // Convert image blocks to text descriptions
  891. content = content.map((block) => {
  892. if (block.type === "image") {
  893. // Convert image blocks to text descriptions
  894. // Note: We can't access the actual image content/url due to API limitations,
  895. // but we can indicate that an image was present in the conversation
  896. return {
  897. type: "text",
  898. text: "[Referenced image in conversation]",
  899. }
  900. }
  901. return block
  902. })
  903. }
  904. }
  905. return { role, content }
  906. })
  907. const stream = this.api.createMessage(systemPrompt, cleanConversationHistory)
  908. const iterator = stream[Symbol.asyncIterator]()
  909. try {
  910. // awaiting first chunk to see if it will throw an error
  911. this.isWaitingForFirstChunk = true
  912. const firstChunk = await iterator.next()
  913. yield firstChunk.value
  914. this.isWaitingForFirstChunk = false
  915. } catch (error) {
  916. // note that this api_req_failed ask is unique in that we only present this option if the api hasn't streamed any content yet (ie it fails on the first chunk due), as it would allow them to hit a retry button. However if the api failed mid-stream, it could be in any arbitrary state where some tools may have executed, so that error is handled differently and requires cancelling the task entirely.
  917. if (alwaysApproveResubmit) {
  918. const errorMsg = error.error?.metadata?.raw ?? error.message ?? "Unknown error"
  919. const baseDelay = requestDelaySeconds || 5
  920. const exponentialDelay = Math.ceil(baseDelay * Math.pow(2, retryAttempt))
  921. // Wait for the greater of the exponential delay or the rate limit delay
  922. const finalDelay = Math.max(exponentialDelay, rateLimitDelay)
  923. // Show countdown timer with exponential backoff
  924. for (let i = finalDelay; i > 0; i--) {
  925. await this.say(
  926. "api_req_retry_delayed",
  927. `${errorMsg}\n\nRetry attempt ${retryAttempt + 1}\nRetrying in ${i} seconds...`,
  928. undefined,
  929. true,
  930. )
  931. await delay(1000)
  932. }
  933. await this.say(
  934. "api_req_retry_delayed",
  935. `${errorMsg}\n\nRetry attempt ${retryAttempt + 1}\nRetrying now...`,
  936. undefined,
  937. false,
  938. )
  939. // delegate generator output from the recursive call with incremented retry count
  940. yield* this.attemptApiRequest(previousApiReqIndex, retryAttempt + 1)
  941. return
  942. } else {
  943. const { response } = await this.ask(
  944. "api_req_failed",
  945. error.message ?? JSON.stringify(serializeError(error), null, 2),
  946. )
  947. if (response !== "yesButtonClicked") {
  948. // this will never happen since if noButtonClicked, we will clear current task, aborting this instance
  949. throw new Error("API request failed")
  950. }
  951. await this.say("api_req_retried")
  952. // delegate generator output from the recursive call
  953. yield* this.attemptApiRequest(previousApiReqIndex)
  954. return
  955. }
  956. }
  957. // no error, so we can continue to yield all remaining chunks
  958. // (needs to be placed outside of try/catch since it we want caller to handle errors not with api_req_failed as that is reserved for first chunk failures only)
  959. // this delegates to another generator or iterable object. In this case, it's saying "yield all remaining values from this iterator". This effectively passes along all subsequent chunks from the original stream.
  960. yield* iterator
  961. }
  962. async presentAssistantMessage() {
  963. if (this.abort) {
  964. throw new Error("Roo Code instance aborted")
  965. }
  966. if (this.presentAssistantMessageLocked) {
  967. this.presentAssistantMessageHasPendingUpdates = true
  968. return
  969. }
  970. this.presentAssistantMessageLocked = true
  971. this.presentAssistantMessageHasPendingUpdates = false
  972. if (this.currentStreamingContentIndex >= this.assistantMessageContent.length) {
  973. // this may happen if the last content block was completed before streaming could finish. if streaming is finished, and we're out of bounds then this means we already presented/executed the last content block and are ready to continue to next request
  974. if (this.didCompleteReadingStream) {
  975. this.userMessageContentReady = true
  976. }
  977. // console.log("no more content blocks to stream! this shouldn't happen?")
  978. this.presentAssistantMessageLocked = false
  979. return
  980. //throw new Error("No more content blocks to stream! This shouldn't happen...") // remove and just return after testing
  981. }
  982. const block = cloneDeep(this.assistantMessageContent[this.currentStreamingContentIndex]) // need to create copy bc while stream is updating the array, it could be updating the reference block properties too
  983. let isCheckpointPossible = false
  984. switch (block.type) {
  985. case "text": {
  986. if (this.didRejectTool || this.didAlreadyUseTool) {
  987. break
  988. }
  989. let content = block.content
  990. if (content) {
  991. // (have to do this for partial and complete since sending content in thinking tags to markdown renderer will automatically be removed)
  992. // Remove end substrings of <thinking or </thinking (below xml parsing is only for opening tags)
  993. // (this is done with the xml parsing below now, but keeping here for reference)
  994. // content = content.replace(/<\/?t(?:h(?:i(?:n(?:k(?:i(?:n(?:g)?)?)?$/, "")
  995. // Remove all instances of <thinking> (with optional line break after) and </thinking> (with optional line break before)
  996. // - Needs to be separate since we dont want to remove the line break before the first tag
  997. // - Needs to happen before the xml parsing below
  998. content = content.replace(/<thinking>\s?/g, "")
  999. content = content.replace(/\s?<\/thinking>/g, "")
  1000. // Remove partial XML tag at the very end of the content (for tool use and thinking tags)
  1001. // (prevents scrollview from jumping when tags are automatically removed)
  1002. const lastOpenBracketIndex = content.lastIndexOf("<")
  1003. if (lastOpenBracketIndex !== -1) {
  1004. const possibleTag = content.slice(lastOpenBracketIndex)
  1005. // Check if there's a '>' after the last '<' (i.e., if the tag is complete) (complete thinking and tool tags will have been removed by now)
  1006. const hasCloseBracket = possibleTag.includes(">")
  1007. if (!hasCloseBracket) {
  1008. // Extract the potential tag name
  1009. let tagContent: string
  1010. if (possibleTag.startsWith("</")) {
  1011. tagContent = possibleTag.slice(2).trim()
  1012. } else {
  1013. tagContent = possibleTag.slice(1).trim()
  1014. }
  1015. // Check if tagContent is likely an incomplete tag name (letters and underscores only)
  1016. const isLikelyTagName = /^[a-zA-Z_]+$/.test(tagContent)
  1017. // Preemptively remove < or </ to keep from these artifacts showing up in chat (also handles closing thinking tags)
  1018. const isOpeningOrClosing = possibleTag === "<" || possibleTag === "</"
  1019. // If the tag is incomplete and at the end, remove it from the content
  1020. if (isOpeningOrClosing || isLikelyTagName) {
  1021. content = content.slice(0, lastOpenBracketIndex).trim()
  1022. }
  1023. }
  1024. }
  1025. }
  1026. await this.say("text", content, undefined, block.partial)
  1027. break
  1028. }
  1029. case "tool_use":
  1030. const toolDescription = (): string => {
  1031. switch (block.name) {
  1032. case "execute_command":
  1033. return `[${block.name} for '${block.params.command}']`
  1034. case "read_file":
  1035. return `[${block.name} for '${block.params.path}']`
  1036. case "write_to_file":
  1037. return `[${block.name} for '${block.params.path}']`
  1038. case "apply_diff":
  1039. return `[${block.name} for '${block.params.path}']`
  1040. case "search_files":
  1041. return `[${block.name} for '${block.params.regex}'${
  1042. block.params.file_pattern ? ` in '${block.params.file_pattern}'` : ""
  1043. }]`
  1044. case "insert_content":
  1045. return `[${block.name} for '${block.params.path}']`
  1046. case "search_and_replace":
  1047. return `[${block.name} for '${block.params.path}']`
  1048. case "list_files":
  1049. return `[${block.name} for '${block.params.path}']`
  1050. case "list_code_definition_names":
  1051. return `[${block.name} for '${block.params.path}']`
  1052. case "browser_action":
  1053. return `[${block.name} for '${block.params.action}']`
  1054. case "use_mcp_tool":
  1055. return `[${block.name} for '${block.params.server_name}']`
  1056. case "access_mcp_resource":
  1057. return `[${block.name} for '${block.params.server_name}']`
  1058. case "ask_followup_question":
  1059. return `[${block.name} for '${block.params.question}']`
  1060. case "attempt_completion":
  1061. return `[${block.name}]`
  1062. case "switch_mode":
  1063. return `[${block.name} to '${block.params.mode_slug}'${block.params.reason ? ` because: ${block.params.reason}` : ""}]`
  1064. case "new_task": {
  1065. const mode = block.params.mode ?? defaultModeSlug
  1066. const message = block.params.message ?? "(no message)"
  1067. const modeName = getModeBySlug(mode, customModes)?.name ?? mode
  1068. return `[${block.name} in ${modeName} mode: '${message}']`
  1069. }
  1070. }
  1071. }
  1072. if (this.didRejectTool) {
  1073. // ignore any tool content after user has rejected tool once
  1074. if (!block.partial) {
  1075. this.userMessageContent.push({
  1076. type: "text",
  1077. text: `Skipping tool ${toolDescription()} due to user rejecting a previous tool.`,
  1078. })
  1079. } else {
  1080. // partial tool after user rejected a previous tool
  1081. this.userMessageContent.push({
  1082. type: "text",
  1083. text: `Tool ${toolDescription()} was interrupted and not executed due to user rejecting a previous tool.`,
  1084. })
  1085. }
  1086. break
  1087. }
  1088. if (this.didAlreadyUseTool) {
  1089. // ignore any content after a tool has already been used
  1090. this.userMessageContent.push({
  1091. type: "text",
  1092. text: `Tool [${block.name}] was not executed because a tool has already been used in this message. Only one tool may be used per message. You must assess the first tool's result before proceeding to use the next tool.`,
  1093. })
  1094. break
  1095. }
  1096. const pushToolResult = (content: ToolResponse) => {
  1097. this.userMessageContent.push({
  1098. type: "text",
  1099. text: `${toolDescription()} Result:`,
  1100. })
  1101. if (typeof content === "string") {
  1102. this.userMessageContent.push({
  1103. type: "text",
  1104. text: content || "(tool did not return anything)",
  1105. })
  1106. } else {
  1107. this.userMessageContent.push(...content)
  1108. }
  1109. // once a tool result has been collected, ignore all other tool uses since we should only ever present one tool result per message
  1110. this.didAlreadyUseTool = true
  1111. // Flag a checkpoint as possible since we've used a tool
  1112. // which may have changed the file system.
  1113. isCheckpointPossible = true
  1114. }
  1115. const askApproval = async (type: ClineAsk, partialMessage?: string) => {
  1116. const { response, text, images } = await this.ask(type, partialMessage, false)
  1117. if (response !== "yesButtonClicked") {
  1118. // Handle both messageResponse and noButtonClicked with text
  1119. if (text) {
  1120. await this.say("user_feedback", text, images)
  1121. pushToolResult(
  1122. formatResponse.toolResult(formatResponse.toolDeniedWithFeedback(text), images),
  1123. )
  1124. } else {
  1125. pushToolResult(formatResponse.toolDenied())
  1126. }
  1127. this.didRejectTool = true
  1128. return false
  1129. }
  1130. // Handle yesButtonClicked with text
  1131. if (text) {
  1132. await this.say("user_feedback", text, images)
  1133. pushToolResult(formatResponse.toolResult(formatResponse.toolApprovedWithFeedback(text), images))
  1134. }
  1135. return true
  1136. }
  1137. const handleError = async (action: string, error: Error) => {
  1138. const errorString = `Error ${action}: ${JSON.stringify(serializeError(error))}`
  1139. await this.say(
  1140. "error",
  1141. `Error ${action}:\n${error.message ?? JSON.stringify(serializeError(error), null, 2)}`,
  1142. )
  1143. // this.toolResults.push({
  1144. // type: "tool_result",
  1145. // tool_use_id: toolUseId,
  1146. // content: await this.formatToolError(errorString),
  1147. // })
  1148. pushToolResult(formatResponse.toolError(errorString))
  1149. }
  1150. // If block is partial, remove partial closing tag so its not presented to user
  1151. const removeClosingTag = (tag: ToolParamName, text?: string) => {
  1152. if (!block.partial) {
  1153. return text || ""
  1154. }
  1155. if (!text) {
  1156. return ""
  1157. }
  1158. // This regex dynamically constructs a pattern to match the closing tag:
  1159. // - Optionally matches whitespace before the tag
  1160. // - Matches '<' or '</' optionally followed by any subset of characters from the tag name
  1161. const tagRegex = new RegExp(
  1162. `\\s?<\/?${tag
  1163. .split("")
  1164. .map((char) => `(?:${char})?`)
  1165. .join("")}$`,
  1166. "g",
  1167. )
  1168. return text.replace(tagRegex, "")
  1169. }
  1170. if (block.name !== "browser_action") {
  1171. await this.browserSession.closeBrowser()
  1172. }
  1173. // Validate tool use before execution
  1174. const { mode, customModes } = (await this.providerRef.deref()?.getState()) ?? {}
  1175. try {
  1176. validateToolUse(
  1177. block.name as ToolName,
  1178. mode ?? defaultModeSlug,
  1179. customModes ?? [],
  1180. {
  1181. apply_diff: this.diffEnabled,
  1182. },
  1183. block.params,
  1184. )
  1185. } catch (error) {
  1186. this.consecutiveMistakeCount++
  1187. pushToolResult(formatResponse.toolError(error.message))
  1188. break
  1189. }
  1190. switch (block.name) {
  1191. case "write_to_file": {
  1192. const relPath: string | undefined = block.params.path
  1193. let newContent: string | undefined = block.params.content
  1194. let predictedLineCount: number | undefined = parseInt(block.params.line_count ?? "0")
  1195. if (!relPath || !newContent) {
  1196. // checking for newContent ensure relPath is complete
  1197. // wait so we can determine if it's a new file or editing an existing file
  1198. break
  1199. }
  1200. // Check if file exists using cached map or fs.access
  1201. let fileExists: boolean
  1202. if (this.diffViewProvider.editType !== undefined) {
  1203. fileExists = this.diffViewProvider.editType === "modify"
  1204. } else {
  1205. const absolutePath = path.resolve(cwd, relPath)
  1206. fileExists = await fileExistsAtPath(absolutePath)
  1207. this.diffViewProvider.editType = fileExists ? "modify" : "create"
  1208. }
  1209. // pre-processing newContent for cases where weaker models might add artifacts like markdown codeblock markers (deepseek/llama) or extra escape characters (gemini)
  1210. if (newContent.startsWith("```")) {
  1211. // this handles cases where it includes language specifiers like ```python ```js
  1212. newContent = newContent.split("\n").slice(1).join("\n").trim()
  1213. }
  1214. if (newContent.endsWith("```")) {
  1215. newContent = newContent.split("\n").slice(0, -1).join("\n").trim()
  1216. }
  1217. if (!this.api.getModel().id.includes("claude")) {
  1218. // it seems not just llama models are doing this, but also gemini and potentially others
  1219. if (
  1220. newContent.includes("&gt;") ||
  1221. newContent.includes("&lt;") ||
  1222. newContent.includes("&quot;")
  1223. ) {
  1224. newContent = newContent
  1225. .replace(/&gt;/g, ">")
  1226. .replace(/&lt;/g, "<")
  1227. .replace(/&quot;/g, '"')
  1228. }
  1229. }
  1230. const sharedMessageProps: ClineSayTool = {
  1231. tool: fileExists ? "editedExistingFile" : "newFileCreated",
  1232. path: getReadablePath(cwd, removeClosingTag("path", relPath)),
  1233. }
  1234. try {
  1235. if (block.partial) {
  1236. // update gui message
  1237. const partialMessage = JSON.stringify(sharedMessageProps)
  1238. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1239. // update editor
  1240. if (!this.diffViewProvider.isEditing) {
  1241. // open the editor and prepare to stream content in
  1242. await this.diffViewProvider.open(relPath)
  1243. }
  1244. // editor is open, stream content in
  1245. await this.diffViewProvider.update(
  1246. everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent,
  1247. false,
  1248. )
  1249. break
  1250. } else {
  1251. if (!relPath) {
  1252. this.consecutiveMistakeCount++
  1253. pushToolResult(await this.sayAndCreateMissingParamError("write_to_file", "path"))
  1254. await this.diffViewProvider.reset()
  1255. break
  1256. }
  1257. if (!newContent) {
  1258. this.consecutiveMistakeCount++
  1259. pushToolResult(await this.sayAndCreateMissingParamError("write_to_file", "content"))
  1260. await this.diffViewProvider.reset()
  1261. break
  1262. }
  1263. if (!predictedLineCount) {
  1264. this.consecutiveMistakeCount++
  1265. pushToolResult(
  1266. await this.sayAndCreateMissingParamError("write_to_file", "line_count"),
  1267. )
  1268. await this.diffViewProvider.reset()
  1269. break
  1270. }
  1271. this.consecutiveMistakeCount = 0
  1272. // if isEditingFile false, that means we have the full contents of the file already.
  1273. // it's important to note how this function works, you can't make the assumption that the block.partial conditional will always be called since it may immediately get complete, non-partial data. So this part of the logic will always be called.
  1274. // in other words, you must always repeat the block.partial logic here
  1275. if (!this.diffViewProvider.isEditing) {
  1276. // show gui message before showing edit animation
  1277. const partialMessage = JSON.stringify(sharedMessageProps)
  1278. await this.ask("tool", partialMessage, true).catch(() => {}) // sending true for partial even though it's not a partial, this shows the edit row before the content is streamed into the editor
  1279. await this.diffViewProvider.open(relPath)
  1280. }
  1281. await this.diffViewProvider.update(
  1282. everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent,
  1283. true,
  1284. )
  1285. await delay(300) // wait for diff view to update
  1286. this.diffViewProvider.scrollToFirstDiff()
  1287. // Check for code omissions before proceeding
  1288. if (
  1289. detectCodeOmission(
  1290. this.diffViewProvider.originalContent || "",
  1291. newContent,
  1292. predictedLineCount,
  1293. )
  1294. ) {
  1295. if (this.diffStrategy) {
  1296. await this.diffViewProvider.revertChanges()
  1297. pushToolResult(
  1298. formatResponse.toolError(
  1299. `Content appears to be truncated (file has ${
  1300. newContent.split("\n").length
  1301. } lines but was predicted to have ${predictedLineCount} lines), and found comments indicating omitted code (e.g., '// rest of code unchanged', '/* previous code */'). Please provide the complete file content without any omissions if possible, or otherwise use the 'apply_diff' tool to apply the diff to the original file.`,
  1302. ),
  1303. )
  1304. break
  1305. } else {
  1306. vscode.window
  1307. .showWarningMessage(
  1308. "Potential code truncation detected. This happens when the AI reaches its max output limit.",
  1309. "Follow this guide to fix the issue",
  1310. )
  1311. .then((selection) => {
  1312. if (selection === "Follow this guide to fix the issue") {
  1313. vscode.env.openExternal(
  1314. vscode.Uri.parse(
  1315. "https://github.com/cline/cline/wiki/Troubleshooting-%E2%80%90-Cline-Deleting-Code-with-%22Rest-of-Code-Here%22-Comments",
  1316. ),
  1317. )
  1318. }
  1319. })
  1320. }
  1321. }
  1322. const completeMessage = JSON.stringify({
  1323. ...sharedMessageProps,
  1324. content: fileExists ? undefined : newContent,
  1325. diff: fileExists
  1326. ? formatResponse.createPrettyPatch(
  1327. relPath,
  1328. this.diffViewProvider.originalContent,
  1329. newContent,
  1330. )
  1331. : undefined,
  1332. } satisfies ClineSayTool)
  1333. const didApprove = await askApproval("tool", completeMessage)
  1334. if (!didApprove) {
  1335. await this.diffViewProvider.revertChanges()
  1336. break
  1337. }
  1338. const { newProblemsMessage, userEdits, finalContent } =
  1339. await this.diffViewProvider.saveChanges()
  1340. this.didEditFile = true // used to determine if we should wait for busy terminal to update before sending api request
  1341. if (userEdits) {
  1342. await this.say(
  1343. "user_feedback_diff",
  1344. JSON.stringify({
  1345. tool: fileExists ? "editedExistingFile" : "newFileCreated",
  1346. path: getReadablePath(cwd, relPath),
  1347. diff: userEdits,
  1348. } satisfies ClineSayTool),
  1349. )
  1350. pushToolResult(
  1351. `The user made the following updates to your content:\n\n${userEdits}\n\n` +
  1352. `The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file, including line numbers:\n\n` +
  1353. `<final_file_content path="${relPath.toPosix()}">\n${addLineNumbers(
  1354. finalContent || "",
  1355. )}\n</final_file_content>\n\n` +
  1356. `Please note:\n` +
  1357. `1. You do not need to re-write the file with these changes, as they have already been applied.\n` +
  1358. `2. Proceed with the task using this updated file content as the new baseline.\n` +
  1359. `3. If the user's edits have addressed part of the task or changed the requirements, adjust your approach accordingly.` +
  1360. `${newProblemsMessage}`,
  1361. )
  1362. } else {
  1363. pushToolResult(
  1364. `The content was successfully saved to ${relPath.toPosix()}.${newProblemsMessage}`,
  1365. )
  1366. }
  1367. await this.diffViewProvider.reset()
  1368. break
  1369. }
  1370. } catch (error) {
  1371. await handleError("writing file", error)
  1372. await this.diffViewProvider.reset()
  1373. break
  1374. }
  1375. }
  1376. case "apply_diff": {
  1377. const relPath: string | undefined = block.params.path
  1378. const diffContent: string | undefined = block.params.diff
  1379. const sharedMessageProps: ClineSayTool = {
  1380. tool: "appliedDiff",
  1381. path: getReadablePath(cwd, removeClosingTag("path", relPath)),
  1382. }
  1383. try {
  1384. if (block.partial) {
  1385. // update gui message
  1386. const partialMessage = JSON.stringify(sharedMessageProps)
  1387. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1388. break
  1389. } else {
  1390. if (!relPath) {
  1391. this.consecutiveMistakeCount++
  1392. pushToolResult(await this.sayAndCreateMissingParamError("apply_diff", "path"))
  1393. break
  1394. }
  1395. if (!diffContent) {
  1396. this.consecutiveMistakeCount++
  1397. pushToolResult(await this.sayAndCreateMissingParamError("apply_diff", "diff"))
  1398. break
  1399. }
  1400. const absolutePath = path.resolve(cwd, relPath)
  1401. const fileExists = await fileExistsAtPath(absolutePath)
  1402. if (!fileExists) {
  1403. this.consecutiveMistakeCount++
  1404. const formattedError = `File does not exist at path: ${absolutePath}\n\n<error_details>\nThe specified file could not be found. Please verify the file path and try again.\n</error_details>`
  1405. await this.say("error", formattedError)
  1406. pushToolResult(formattedError)
  1407. break
  1408. }
  1409. const originalContent = await fs.readFile(absolutePath, "utf-8")
  1410. // Apply the diff to the original content
  1411. const diffResult = (await this.diffStrategy?.applyDiff(
  1412. originalContent,
  1413. diffContent,
  1414. parseInt(block.params.start_line ?? ""),
  1415. parseInt(block.params.end_line ?? ""),
  1416. )) ?? {
  1417. success: false,
  1418. error: "No diff strategy available",
  1419. }
  1420. if (!diffResult.success) {
  1421. this.consecutiveMistakeCount++
  1422. const currentCount =
  1423. (this.consecutiveMistakeCountForApplyDiff.get(relPath) || 0) + 1
  1424. this.consecutiveMistakeCountForApplyDiff.set(relPath, currentCount)
  1425. const errorDetails = diffResult.details
  1426. ? JSON.stringify(diffResult.details, null, 2)
  1427. : ""
  1428. const formattedError = `Unable to apply diff to file: ${absolutePath}\n\n<error_details>\n${
  1429. diffResult.error
  1430. }${errorDetails ? `\n\nDetails:\n${errorDetails}` : ""}\n</error_details>`
  1431. if (currentCount >= 2) {
  1432. await this.say("error", formattedError)
  1433. }
  1434. pushToolResult(formattedError)
  1435. break
  1436. }
  1437. this.consecutiveMistakeCount = 0
  1438. this.consecutiveMistakeCountForApplyDiff.delete(relPath)
  1439. // Show diff view before asking for approval
  1440. this.diffViewProvider.editType = "modify"
  1441. await this.diffViewProvider.open(relPath)
  1442. await this.diffViewProvider.update(diffResult.content, true)
  1443. await this.diffViewProvider.scrollToFirstDiff()
  1444. const completeMessage = JSON.stringify({
  1445. ...sharedMessageProps,
  1446. diff: diffContent,
  1447. } satisfies ClineSayTool)
  1448. const didApprove = await askApproval("tool", completeMessage)
  1449. if (!didApprove) {
  1450. await this.diffViewProvider.revertChanges() // This likely handles closing the diff view
  1451. break
  1452. }
  1453. const { newProblemsMessage, userEdits, finalContent } =
  1454. await this.diffViewProvider.saveChanges()
  1455. this.didEditFile = true // used to determine if we should wait for busy terminal to update before sending api request
  1456. if (userEdits) {
  1457. await this.say(
  1458. "user_feedback_diff",
  1459. JSON.stringify({
  1460. tool: fileExists ? "editedExistingFile" : "newFileCreated",
  1461. path: getReadablePath(cwd, relPath),
  1462. diff: userEdits,
  1463. } satisfies ClineSayTool),
  1464. )
  1465. pushToolResult(
  1466. `The user made the following updates to your content:\n\n${userEdits}\n\n` +
  1467. `The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file, including line numbers:\n\n` +
  1468. `<final_file_content path="${relPath.toPosix()}">\n${addLineNumbers(
  1469. finalContent || "",
  1470. )}\n</final_file_content>\n\n` +
  1471. `Please note:\n` +
  1472. `1. You do not need to re-write the file with these changes, as they have already been applied.\n` +
  1473. `2. Proceed with the task using this updated file content as the new baseline.\n` +
  1474. `3. If the user's edits have addressed part of the task or changed the requirements, adjust your approach accordingly.` +
  1475. `${newProblemsMessage}`,
  1476. )
  1477. } else {
  1478. pushToolResult(
  1479. `Changes successfully applied to ${relPath.toPosix()}:\n\n${newProblemsMessage}`,
  1480. )
  1481. }
  1482. await this.diffViewProvider.reset()
  1483. break
  1484. }
  1485. } catch (error) {
  1486. await handleError("applying diff", error)
  1487. await this.diffViewProvider.reset()
  1488. break
  1489. }
  1490. }
  1491. case "insert_content": {
  1492. const relPath: string | undefined = block.params.path
  1493. const operations: string | undefined = block.params.operations
  1494. const sharedMessageProps: ClineSayTool = {
  1495. tool: "appliedDiff",
  1496. path: getReadablePath(cwd, removeClosingTag("path", relPath)),
  1497. }
  1498. try {
  1499. if (block.partial) {
  1500. const partialMessage = JSON.stringify(sharedMessageProps)
  1501. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1502. break
  1503. }
  1504. // Validate required parameters
  1505. if (!relPath) {
  1506. this.consecutiveMistakeCount++
  1507. pushToolResult(await this.sayAndCreateMissingParamError("insert_content", "path"))
  1508. break
  1509. }
  1510. if (!operations) {
  1511. this.consecutiveMistakeCount++
  1512. pushToolResult(await this.sayAndCreateMissingParamError("insert_content", "operations"))
  1513. break
  1514. }
  1515. const absolutePath = path.resolve(cwd, relPath)
  1516. const fileExists = await fileExistsAtPath(absolutePath)
  1517. if (!fileExists) {
  1518. this.consecutiveMistakeCount++
  1519. const formattedError = `File does not exist at path: ${absolutePath}\n\n<error_details>\nThe specified file could not be found. Please verify the file path and try again.\n</error_details>`
  1520. await this.say("error", formattedError)
  1521. pushToolResult(formattedError)
  1522. break
  1523. }
  1524. let parsedOperations: Array<{
  1525. start_line: number
  1526. content: string
  1527. }>
  1528. try {
  1529. parsedOperations = JSON.parse(operations)
  1530. if (!Array.isArray(parsedOperations)) {
  1531. throw new Error("Operations must be an array")
  1532. }
  1533. } catch (error) {
  1534. this.consecutiveMistakeCount++
  1535. await this.say("error", `Failed to parse operations JSON: ${error.message}`)
  1536. pushToolResult(formatResponse.toolError("Invalid operations JSON format"))
  1537. break
  1538. }
  1539. this.consecutiveMistakeCount = 0
  1540. // Read the file
  1541. const fileContent = await fs.readFile(absolutePath, "utf8")
  1542. this.diffViewProvider.editType = "modify"
  1543. this.diffViewProvider.originalContent = fileContent
  1544. const lines = fileContent.split("\n")
  1545. const updatedContent = insertGroups(
  1546. lines,
  1547. parsedOperations.map((elem) => {
  1548. return {
  1549. index: elem.start_line - 1,
  1550. elements: elem.content.split("\n"),
  1551. }
  1552. }),
  1553. ).join("\n")
  1554. // Show changes in diff view
  1555. if (!this.diffViewProvider.isEditing) {
  1556. await this.ask("tool", JSON.stringify(sharedMessageProps), true).catch(() => {})
  1557. // First open with original content
  1558. await this.diffViewProvider.open(relPath)
  1559. await this.diffViewProvider.update(fileContent, false)
  1560. this.diffViewProvider.scrollToFirstDiff()
  1561. await delay(200)
  1562. }
  1563. const diff = formatResponse.createPrettyPatch(relPath, fileContent, updatedContent)
  1564. if (!diff) {
  1565. pushToolResult(`No changes needed for '${relPath}'`)
  1566. break
  1567. }
  1568. await this.diffViewProvider.update(updatedContent, true)
  1569. const completeMessage = JSON.stringify({
  1570. ...sharedMessageProps,
  1571. diff,
  1572. } satisfies ClineSayTool)
  1573. const didApprove = await this.ask("tool", completeMessage, false).then(
  1574. (response) => response.response === "yesButtonClicked",
  1575. )
  1576. if (!didApprove) {
  1577. await this.diffViewProvider.revertChanges()
  1578. pushToolResult("Changes were rejected by the user.")
  1579. break
  1580. }
  1581. const { newProblemsMessage, userEdits, finalContent } =
  1582. await this.diffViewProvider.saveChanges()
  1583. this.didEditFile = true
  1584. if (!userEdits) {
  1585. pushToolResult(
  1586. `The content was successfully inserted in ${relPath.toPosix()}.${newProblemsMessage}`,
  1587. )
  1588. await this.diffViewProvider.reset()
  1589. break
  1590. }
  1591. const userFeedbackDiff = JSON.stringify({
  1592. tool: "appliedDiff",
  1593. path: getReadablePath(cwd, relPath),
  1594. diff: userEdits,
  1595. } satisfies ClineSayTool)
  1596. console.debug("[DEBUG] User made edits, sending feedback diff:", userFeedbackDiff)
  1597. await this.say("user_feedback_diff", userFeedbackDiff)
  1598. pushToolResult(
  1599. `The user made the following updates to your content:\n\n${userEdits}\n\n` +
  1600. `The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file:\n\n` +
  1601. `<final_file_content path="${relPath.toPosix()}">\n${finalContent}\n</final_file_content>\n\n` +
  1602. `Please note:\n` +
  1603. `1. You do not need to re-write the file with these changes, as they have already been applied.\n` +
  1604. `2. Proceed with the task using this updated file content as the new baseline.\n` +
  1605. `3. If the user's edits have addressed part of the task or changed the requirements, adjust your approach accordingly.` +
  1606. `${newProblemsMessage}`,
  1607. )
  1608. await this.diffViewProvider.reset()
  1609. } catch (error) {
  1610. handleError("insert content", error)
  1611. await this.diffViewProvider.reset()
  1612. }
  1613. break
  1614. }
  1615. case "search_and_replace": {
  1616. const relPath: string | undefined = block.params.path
  1617. const operations: string | undefined = block.params.operations
  1618. const sharedMessageProps: ClineSayTool = {
  1619. tool: "appliedDiff",
  1620. path: getReadablePath(cwd, removeClosingTag("path", relPath)),
  1621. }
  1622. try {
  1623. if (block.partial) {
  1624. const partialMessage = JSON.stringify({
  1625. path: removeClosingTag("path", relPath),
  1626. operations: removeClosingTag("operations", operations),
  1627. })
  1628. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1629. break
  1630. } else {
  1631. if (!relPath) {
  1632. this.consecutiveMistakeCount++
  1633. pushToolResult(
  1634. await this.sayAndCreateMissingParamError("search_and_replace", "path"),
  1635. )
  1636. break
  1637. }
  1638. if (!operations) {
  1639. this.consecutiveMistakeCount++
  1640. pushToolResult(
  1641. await this.sayAndCreateMissingParamError("search_and_replace", "operations"),
  1642. )
  1643. break
  1644. }
  1645. const absolutePath = path.resolve(cwd, relPath)
  1646. const fileExists = await fileExistsAtPath(absolutePath)
  1647. if (!fileExists) {
  1648. this.consecutiveMistakeCount++
  1649. const formattedError = `File does not exist at path: ${absolutePath}\n\n<error_details>\nThe specified file could not be found. Please verify the file path and try again.\n</error_details>`
  1650. await this.say("error", formattedError)
  1651. pushToolResult(formattedError)
  1652. break
  1653. }
  1654. let parsedOperations: Array<{
  1655. search: string
  1656. replace: string
  1657. start_line?: number
  1658. end_line?: number
  1659. use_regex?: boolean
  1660. ignore_case?: boolean
  1661. regex_flags?: string
  1662. }>
  1663. try {
  1664. parsedOperations = JSON.parse(operations)
  1665. if (!Array.isArray(parsedOperations)) {
  1666. throw new Error("Operations must be an array")
  1667. }
  1668. } catch (error) {
  1669. this.consecutiveMistakeCount++
  1670. await this.say("error", `Failed to parse operations JSON: ${error.message}`)
  1671. pushToolResult(formatResponse.toolError("Invalid operations JSON format"))
  1672. break
  1673. }
  1674. // Read the original file content
  1675. const fileContent = await fs.readFile(absolutePath, "utf-8")
  1676. this.diffViewProvider.editType = "modify"
  1677. this.diffViewProvider.originalContent = fileContent
  1678. let lines = fileContent.split("\n")
  1679. for (const op of parsedOperations) {
  1680. const flags = op.regex_flags ?? (op.ignore_case ? "gi" : "g")
  1681. const multilineFlags = flags.includes("m") ? flags : flags + "m"
  1682. const searchPattern = op.use_regex
  1683. ? new RegExp(op.search, multilineFlags)
  1684. : new RegExp(escapeRegExp(op.search), multilineFlags)
  1685. if (op.start_line || op.end_line) {
  1686. const startLine = Math.max((op.start_line ?? 1) - 1, 0)
  1687. const endLine = Math.min((op.end_line ?? lines.length) - 1, lines.length - 1)
  1688. // Get the content before and after the target section
  1689. const beforeLines = lines.slice(0, startLine)
  1690. const afterLines = lines.slice(endLine + 1)
  1691. // Get the target section and perform replacement
  1692. const targetContent = lines.slice(startLine, endLine + 1).join("\n")
  1693. const modifiedContent = targetContent.replace(searchPattern, op.replace)
  1694. const modifiedLines = modifiedContent.split("\n")
  1695. // Reconstruct the full content with the modified section
  1696. lines = [...beforeLines, ...modifiedLines, ...afterLines]
  1697. } else {
  1698. // Global replacement
  1699. const fullContent = lines.join("\n")
  1700. const modifiedContent = fullContent.replace(searchPattern, op.replace)
  1701. lines = modifiedContent.split("\n")
  1702. }
  1703. }
  1704. const newContent = lines.join("\n")
  1705. this.consecutiveMistakeCount = 0
  1706. // Show diff preview
  1707. const diff = formatResponse.createPrettyPatch(relPath, fileContent, newContent)
  1708. if (!diff) {
  1709. pushToolResult(`No changes needed for '${relPath}'`)
  1710. break
  1711. }
  1712. await this.diffViewProvider.open(relPath)
  1713. await this.diffViewProvider.update(newContent, true)
  1714. this.diffViewProvider.scrollToFirstDiff()
  1715. const completeMessage = JSON.stringify({
  1716. ...sharedMessageProps,
  1717. diff: diff,
  1718. } satisfies ClineSayTool)
  1719. const didApprove = await askApproval("tool", completeMessage)
  1720. if (!didApprove) {
  1721. await this.diffViewProvider.revertChanges() // This likely handles closing the diff view
  1722. break
  1723. }
  1724. const { newProblemsMessage, userEdits, finalContent } =
  1725. await this.diffViewProvider.saveChanges()
  1726. this.didEditFile = true // used to determine if we should wait for busy terminal to update before sending api request
  1727. if (userEdits) {
  1728. await this.say(
  1729. "user_feedback_diff",
  1730. JSON.stringify({
  1731. tool: fileExists ? "editedExistingFile" : "newFileCreated",
  1732. path: getReadablePath(cwd, relPath),
  1733. diff: userEdits,
  1734. } satisfies ClineSayTool),
  1735. )
  1736. pushToolResult(
  1737. `The user made the following updates to your content:\n\n${userEdits}\n\n` +
  1738. `The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file, including line numbers:\n\n` +
  1739. `<final_file_content path="${relPath.toPosix()}">\n${addLineNumbers(finalContent || "")}\n</final_file_content>\n\n` +
  1740. `Please note:\n` +
  1741. `1. You do not need to re-write the file with these changes, as they have already been applied.\n` +
  1742. `2. Proceed with the task using this updated file content as the new baseline.\n` +
  1743. `3. If the user's edits have addressed part of the task or changed the requirements, adjust your approach accordingly.` +
  1744. `${newProblemsMessage}`,
  1745. )
  1746. } else {
  1747. pushToolResult(
  1748. `Changes successfully applied to ${relPath.toPosix()}:\n\n${newProblemsMessage}`,
  1749. )
  1750. }
  1751. await this.diffViewProvider.reset()
  1752. break
  1753. }
  1754. } catch (error) {
  1755. await handleError("applying search and replace", error)
  1756. await this.diffViewProvider.reset()
  1757. break
  1758. }
  1759. }
  1760. case "read_file": {
  1761. const relPath: string | undefined = block.params.path
  1762. const sharedMessageProps: ClineSayTool = {
  1763. tool: "readFile",
  1764. path: getReadablePath(cwd, removeClosingTag("path", relPath)),
  1765. }
  1766. try {
  1767. if (block.partial) {
  1768. const partialMessage = JSON.stringify({
  1769. ...sharedMessageProps,
  1770. content: undefined,
  1771. } satisfies ClineSayTool)
  1772. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1773. break
  1774. } else {
  1775. if (!relPath) {
  1776. this.consecutiveMistakeCount++
  1777. pushToolResult(await this.sayAndCreateMissingParamError("read_file", "path"))
  1778. break
  1779. }
  1780. this.consecutiveMistakeCount = 0
  1781. const absolutePath = path.resolve(cwd, relPath)
  1782. const completeMessage = JSON.stringify({
  1783. ...sharedMessageProps,
  1784. content: absolutePath,
  1785. } satisfies ClineSayTool)
  1786. const didApprove = await askApproval("tool", completeMessage)
  1787. if (!didApprove) {
  1788. break
  1789. }
  1790. // now execute the tool like normal
  1791. const content = await extractTextFromFile(absolutePath)
  1792. pushToolResult(content)
  1793. break
  1794. }
  1795. } catch (error) {
  1796. await handleError("reading file", error)
  1797. break
  1798. }
  1799. }
  1800. case "list_files": {
  1801. const relDirPath: string | undefined = block.params.path
  1802. const recursiveRaw: string | undefined = block.params.recursive
  1803. const recursive = recursiveRaw?.toLowerCase() === "true"
  1804. const sharedMessageProps: ClineSayTool = {
  1805. tool: !recursive ? "listFilesTopLevel" : "listFilesRecursive",
  1806. path: getReadablePath(cwd, removeClosingTag("path", relDirPath)),
  1807. }
  1808. try {
  1809. if (block.partial) {
  1810. const partialMessage = JSON.stringify({
  1811. ...sharedMessageProps,
  1812. content: "",
  1813. } satisfies ClineSayTool)
  1814. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1815. break
  1816. } else {
  1817. if (!relDirPath) {
  1818. this.consecutiveMistakeCount++
  1819. pushToolResult(await this.sayAndCreateMissingParamError("list_files", "path"))
  1820. break
  1821. }
  1822. this.consecutiveMistakeCount = 0
  1823. const absolutePath = path.resolve(cwd, relDirPath)
  1824. const [files, didHitLimit] = await listFiles(absolutePath, recursive, 200)
  1825. const result = formatResponse.formatFilesList(absolutePath, files, didHitLimit)
  1826. const completeMessage = JSON.stringify({
  1827. ...sharedMessageProps,
  1828. content: result,
  1829. } satisfies ClineSayTool)
  1830. const didApprove = await askApproval("tool", completeMessage)
  1831. if (!didApprove) {
  1832. break
  1833. }
  1834. pushToolResult(result)
  1835. break
  1836. }
  1837. } catch (error) {
  1838. await handleError("listing files", error)
  1839. break
  1840. }
  1841. }
  1842. case "list_code_definition_names": {
  1843. const relDirPath: string | undefined = block.params.path
  1844. const sharedMessageProps: ClineSayTool = {
  1845. tool: "listCodeDefinitionNames",
  1846. path: getReadablePath(cwd, removeClosingTag("path", relDirPath)),
  1847. }
  1848. try {
  1849. if (block.partial) {
  1850. const partialMessage = JSON.stringify({
  1851. ...sharedMessageProps,
  1852. content: "",
  1853. } satisfies ClineSayTool)
  1854. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1855. break
  1856. } else {
  1857. if (!relDirPath) {
  1858. this.consecutiveMistakeCount++
  1859. pushToolResult(
  1860. await this.sayAndCreateMissingParamError("list_code_definition_names", "path"),
  1861. )
  1862. break
  1863. }
  1864. this.consecutiveMistakeCount = 0
  1865. const absolutePath = path.resolve(cwd, relDirPath)
  1866. const result = await parseSourceCodeForDefinitionsTopLevel(absolutePath)
  1867. const completeMessage = JSON.stringify({
  1868. ...sharedMessageProps,
  1869. content: result,
  1870. } satisfies ClineSayTool)
  1871. const didApprove = await askApproval("tool", completeMessage)
  1872. if (!didApprove) {
  1873. break
  1874. }
  1875. pushToolResult(result)
  1876. break
  1877. }
  1878. } catch (error) {
  1879. await handleError("parsing source code definitions", error)
  1880. break
  1881. }
  1882. }
  1883. case "search_files": {
  1884. const relDirPath: string | undefined = block.params.path
  1885. const regex: string | undefined = block.params.regex
  1886. const filePattern: string | undefined = block.params.file_pattern
  1887. const sharedMessageProps: ClineSayTool = {
  1888. tool: "searchFiles",
  1889. path: getReadablePath(cwd, removeClosingTag("path", relDirPath)),
  1890. regex: removeClosingTag("regex", regex),
  1891. filePattern: removeClosingTag("file_pattern", filePattern),
  1892. }
  1893. try {
  1894. if (block.partial) {
  1895. const partialMessage = JSON.stringify({
  1896. ...sharedMessageProps,
  1897. content: "",
  1898. } satisfies ClineSayTool)
  1899. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  1900. break
  1901. } else {
  1902. if (!relDirPath) {
  1903. this.consecutiveMistakeCount++
  1904. pushToolResult(await this.sayAndCreateMissingParamError("search_files", "path"))
  1905. break
  1906. }
  1907. if (!regex) {
  1908. this.consecutiveMistakeCount++
  1909. pushToolResult(await this.sayAndCreateMissingParamError("search_files", "regex"))
  1910. break
  1911. }
  1912. this.consecutiveMistakeCount = 0
  1913. const absolutePath = path.resolve(cwd, relDirPath)
  1914. const results = await regexSearchFiles(cwd, absolutePath, regex, filePattern)
  1915. const completeMessage = JSON.stringify({
  1916. ...sharedMessageProps,
  1917. content: results,
  1918. } satisfies ClineSayTool)
  1919. const didApprove = await askApproval("tool", completeMessage)
  1920. if (!didApprove) {
  1921. break
  1922. }
  1923. pushToolResult(results)
  1924. break
  1925. }
  1926. } catch (error) {
  1927. await handleError("searching files", error)
  1928. break
  1929. }
  1930. }
  1931. case "browser_action": {
  1932. const action: BrowserAction | undefined = block.params.action as BrowserAction
  1933. const url: string | undefined = block.params.url
  1934. const coordinate: string | undefined = block.params.coordinate
  1935. const text: string | undefined = block.params.text
  1936. if (!action || !browserActions.includes(action)) {
  1937. // checking for action to ensure it is complete and valid
  1938. if (!block.partial) {
  1939. // if the block is complete and we don't have a valid action this is a mistake
  1940. this.consecutiveMistakeCount++
  1941. pushToolResult(await this.sayAndCreateMissingParamError("browser_action", "action"))
  1942. await this.browserSession.closeBrowser()
  1943. }
  1944. break
  1945. }
  1946. try {
  1947. if (block.partial) {
  1948. if (action === "launch") {
  1949. await this.ask(
  1950. "browser_action_launch",
  1951. removeClosingTag("url", url),
  1952. block.partial,
  1953. ).catch(() => {})
  1954. } else {
  1955. await this.say(
  1956. "browser_action",
  1957. JSON.stringify({
  1958. action: action as BrowserAction,
  1959. coordinate: removeClosingTag("coordinate", coordinate),
  1960. text: removeClosingTag("text", text),
  1961. } satisfies ClineSayBrowserAction),
  1962. undefined,
  1963. block.partial,
  1964. )
  1965. }
  1966. break
  1967. } else {
  1968. let browserActionResult: BrowserActionResult
  1969. if (action === "launch") {
  1970. if (!url) {
  1971. this.consecutiveMistakeCount++
  1972. pushToolResult(
  1973. await this.sayAndCreateMissingParamError("browser_action", "url"),
  1974. )
  1975. await this.browserSession.closeBrowser()
  1976. break
  1977. }
  1978. this.consecutiveMistakeCount = 0
  1979. const didApprove = await askApproval("browser_action_launch", url)
  1980. if (!didApprove) {
  1981. break
  1982. }
  1983. // NOTE: it's okay that we call this message since the partial inspect_site is finished streaming. The only scenario we have to avoid is sending messages WHILE a partial message exists at the end of the messages array. For example the api_req_finished message would interfere with the partial message, so we needed to remove that.
  1984. // await this.say("inspect_site_result", "") // no result, starts the loading spinner waiting for result
  1985. await this.say("browser_action_result", "") // starts loading spinner
  1986. await this.browserSession.launchBrowser()
  1987. browserActionResult = await this.browserSession.navigateToUrl(url)
  1988. } else {
  1989. if (action === "click") {
  1990. if (!coordinate) {
  1991. this.consecutiveMistakeCount++
  1992. pushToolResult(
  1993. await this.sayAndCreateMissingParamError(
  1994. "browser_action",
  1995. "coordinate",
  1996. ),
  1997. )
  1998. await this.browserSession.closeBrowser()
  1999. break // can't be within an inner switch
  2000. }
  2001. }
  2002. if (action === "type") {
  2003. if (!text) {
  2004. this.consecutiveMistakeCount++
  2005. pushToolResult(
  2006. await this.sayAndCreateMissingParamError("browser_action", "text"),
  2007. )
  2008. await this.browserSession.closeBrowser()
  2009. break
  2010. }
  2011. }
  2012. this.consecutiveMistakeCount = 0
  2013. await this.say(
  2014. "browser_action",
  2015. JSON.stringify({
  2016. action: action as BrowserAction,
  2017. coordinate,
  2018. text,
  2019. } satisfies ClineSayBrowserAction),
  2020. undefined,
  2021. false,
  2022. )
  2023. switch (action) {
  2024. case "click":
  2025. browserActionResult = await this.browserSession.click(coordinate!)
  2026. break
  2027. case "type":
  2028. browserActionResult = await this.browserSession.type(text!)
  2029. break
  2030. case "scroll_down":
  2031. browserActionResult = await this.browserSession.scrollDown()
  2032. break
  2033. case "scroll_up":
  2034. browserActionResult = await this.browserSession.scrollUp()
  2035. break
  2036. case "close":
  2037. browserActionResult = await this.browserSession.closeBrowser()
  2038. break
  2039. }
  2040. }
  2041. switch (action) {
  2042. case "launch":
  2043. case "click":
  2044. case "type":
  2045. case "scroll_down":
  2046. case "scroll_up":
  2047. await this.say("browser_action_result", JSON.stringify(browserActionResult))
  2048. pushToolResult(
  2049. formatResponse.toolResult(
  2050. `The browser action has been executed. The console logs and screenshot have been captured for your analysis.\n\nConsole logs:\n${
  2051. browserActionResult.logs || "(No new logs)"
  2052. }\n\n(REMEMBER: if you need to proceed to using non-\`browser_action\` tools or launch a new browser, you MUST first close this browser. For example, if after analyzing the logs and screenshot you need to edit a file, you must first close the browser before you can use the write_to_file tool.)`,
  2053. browserActionResult.screenshot ? [browserActionResult.screenshot] : [],
  2054. ),
  2055. )
  2056. break
  2057. case "close":
  2058. pushToolResult(
  2059. formatResponse.toolResult(
  2060. `The browser has been closed. You may now proceed to using other tools.`,
  2061. ),
  2062. )
  2063. break
  2064. }
  2065. break
  2066. }
  2067. } catch (error) {
  2068. await this.browserSession.closeBrowser() // if any error occurs, the browser session is terminated
  2069. await handleError("executing browser action", error)
  2070. break
  2071. }
  2072. }
  2073. case "execute_command": {
  2074. const command: string | undefined = block.params.command
  2075. try {
  2076. if (block.partial) {
  2077. await this.ask("command", removeClosingTag("command", command), block.partial).catch(
  2078. () => {},
  2079. )
  2080. break
  2081. } else {
  2082. if (!command) {
  2083. this.consecutiveMistakeCount++
  2084. pushToolResult(
  2085. await this.sayAndCreateMissingParamError("execute_command", "command"),
  2086. )
  2087. break
  2088. }
  2089. this.consecutiveMistakeCount = 0
  2090. const didApprove = await askApproval("command", command)
  2091. if (!didApprove) {
  2092. break
  2093. }
  2094. const [userRejected, result] = await this.executeCommandTool(command)
  2095. if (userRejected) {
  2096. this.didRejectTool = true
  2097. }
  2098. pushToolResult(result)
  2099. break
  2100. }
  2101. } catch (error) {
  2102. await handleError("executing command", error)
  2103. break
  2104. }
  2105. }
  2106. case "use_mcp_tool": {
  2107. const server_name: string | undefined = block.params.server_name
  2108. const tool_name: string | undefined = block.params.tool_name
  2109. const mcp_arguments: string | undefined = block.params.arguments
  2110. try {
  2111. if (block.partial) {
  2112. const partialMessage = JSON.stringify({
  2113. type: "use_mcp_tool",
  2114. serverName: removeClosingTag("server_name", server_name),
  2115. toolName: removeClosingTag("tool_name", tool_name),
  2116. arguments: removeClosingTag("arguments", mcp_arguments),
  2117. } satisfies ClineAskUseMcpServer)
  2118. await this.ask("use_mcp_server", partialMessage, block.partial).catch(() => {})
  2119. break
  2120. } else {
  2121. if (!server_name) {
  2122. this.consecutiveMistakeCount++
  2123. pushToolResult(
  2124. await this.sayAndCreateMissingParamError("use_mcp_tool", "server_name"),
  2125. )
  2126. break
  2127. }
  2128. if (!tool_name) {
  2129. this.consecutiveMistakeCount++
  2130. pushToolResult(
  2131. await this.sayAndCreateMissingParamError("use_mcp_tool", "tool_name"),
  2132. )
  2133. break
  2134. }
  2135. // arguments are optional, but if they are provided they must be valid JSON
  2136. // if (!mcp_arguments) {
  2137. // this.consecutiveMistakeCount++
  2138. // pushToolResult(await this.sayAndCreateMissingParamError("use_mcp_tool", "arguments"))
  2139. // break
  2140. // }
  2141. let parsedArguments: Record<string, unknown> | undefined
  2142. if (mcp_arguments) {
  2143. try {
  2144. parsedArguments = JSON.parse(mcp_arguments)
  2145. } catch (error) {
  2146. this.consecutiveMistakeCount++
  2147. await this.say(
  2148. "error",
  2149. `Roo tried to use ${tool_name} with an invalid JSON argument. Retrying...`,
  2150. )
  2151. pushToolResult(
  2152. formatResponse.toolError(
  2153. formatResponse.invalidMcpToolArgumentError(server_name, tool_name),
  2154. ),
  2155. )
  2156. break
  2157. }
  2158. }
  2159. this.consecutiveMistakeCount = 0
  2160. const completeMessage = JSON.stringify({
  2161. type: "use_mcp_tool",
  2162. serverName: server_name,
  2163. toolName: tool_name,
  2164. arguments: mcp_arguments,
  2165. } satisfies ClineAskUseMcpServer)
  2166. const didApprove = await askApproval("use_mcp_server", completeMessage)
  2167. if (!didApprove) {
  2168. break
  2169. }
  2170. // now execute the tool
  2171. await this.say("mcp_server_request_started") // same as browser_action_result
  2172. const toolResult = await this.providerRef
  2173. .deref()
  2174. ?.getMcpHub()
  2175. ?.callTool(server_name, tool_name, parsedArguments)
  2176. // TODO: add progress indicator and ability to parse images and non-text responses
  2177. const toolResultPretty =
  2178. (toolResult?.isError ? "Error:\n" : "") +
  2179. toolResult?.content
  2180. .map((item) => {
  2181. if (item.type === "text") {
  2182. return item.text
  2183. }
  2184. if (item.type === "resource") {
  2185. const { blob, ...rest } = item.resource
  2186. return JSON.stringify(rest, null, 2)
  2187. }
  2188. return ""
  2189. })
  2190. .filter(Boolean)
  2191. .join("\n\n") || "(No response)"
  2192. await this.say("mcp_server_response", toolResultPretty)
  2193. pushToolResult(formatResponse.toolResult(toolResultPretty))
  2194. break
  2195. }
  2196. } catch (error) {
  2197. await handleError("executing MCP tool", error)
  2198. break
  2199. }
  2200. }
  2201. case "access_mcp_resource": {
  2202. const server_name: string | undefined = block.params.server_name
  2203. const uri: string | undefined = block.params.uri
  2204. try {
  2205. if (block.partial) {
  2206. const partialMessage = JSON.stringify({
  2207. type: "access_mcp_resource",
  2208. serverName: removeClosingTag("server_name", server_name),
  2209. uri: removeClosingTag("uri", uri),
  2210. } satisfies ClineAskUseMcpServer)
  2211. await this.ask("use_mcp_server", partialMessage, block.partial).catch(() => {})
  2212. break
  2213. } else {
  2214. if (!server_name) {
  2215. this.consecutiveMistakeCount++
  2216. pushToolResult(
  2217. await this.sayAndCreateMissingParamError("access_mcp_resource", "server_name"),
  2218. )
  2219. break
  2220. }
  2221. if (!uri) {
  2222. this.consecutiveMistakeCount++
  2223. pushToolResult(
  2224. await this.sayAndCreateMissingParamError("access_mcp_resource", "uri"),
  2225. )
  2226. break
  2227. }
  2228. this.consecutiveMistakeCount = 0
  2229. const completeMessage = JSON.stringify({
  2230. type: "access_mcp_resource",
  2231. serverName: server_name,
  2232. uri,
  2233. } satisfies ClineAskUseMcpServer)
  2234. const didApprove = await askApproval("use_mcp_server", completeMessage)
  2235. if (!didApprove) {
  2236. break
  2237. }
  2238. // now execute the tool
  2239. await this.say("mcp_server_request_started")
  2240. const resourceResult = await this.providerRef
  2241. .deref()
  2242. ?.getMcpHub()
  2243. ?.readResource(server_name, uri)
  2244. const resourceResultPretty =
  2245. resourceResult?.contents
  2246. .map((item) => {
  2247. if (item.text) {
  2248. return item.text
  2249. }
  2250. return ""
  2251. })
  2252. .filter(Boolean)
  2253. .join("\n\n") || "(Empty response)"
  2254. await this.say("mcp_server_response", resourceResultPretty)
  2255. pushToolResult(formatResponse.toolResult(resourceResultPretty))
  2256. break
  2257. }
  2258. } catch (error) {
  2259. await handleError("accessing MCP resource", error)
  2260. break
  2261. }
  2262. }
  2263. case "ask_followup_question": {
  2264. const question: string | undefined = block.params.question
  2265. try {
  2266. if (block.partial) {
  2267. await this.ask("followup", removeClosingTag("question", question), block.partial).catch(
  2268. () => {},
  2269. )
  2270. break
  2271. } else {
  2272. if (!question) {
  2273. this.consecutiveMistakeCount++
  2274. pushToolResult(
  2275. await this.sayAndCreateMissingParamError("ask_followup_question", "question"),
  2276. )
  2277. break
  2278. }
  2279. this.consecutiveMistakeCount = 0
  2280. const { text, images } = await this.ask("followup", question, false)
  2281. await this.say("user_feedback", text ?? "", images)
  2282. pushToolResult(formatResponse.toolResult(`<answer>\n${text}\n</answer>`, images))
  2283. break
  2284. }
  2285. } catch (error) {
  2286. await handleError("asking question", error)
  2287. break
  2288. }
  2289. }
  2290. case "switch_mode": {
  2291. const mode_slug: string | undefined = block.params.mode_slug
  2292. const reason: string | undefined = block.params.reason
  2293. try {
  2294. if (block.partial) {
  2295. const partialMessage = JSON.stringify({
  2296. tool: "switchMode",
  2297. mode: removeClosingTag("mode_slug", mode_slug),
  2298. reason: removeClosingTag("reason", reason),
  2299. })
  2300. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  2301. break
  2302. } else {
  2303. if (!mode_slug) {
  2304. this.consecutiveMistakeCount++
  2305. pushToolResult(await this.sayAndCreateMissingParamError("switch_mode", "mode_slug"))
  2306. break
  2307. }
  2308. this.consecutiveMistakeCount = 0
  2309. // Verify the mode exists
  2310. const targetMode = getModeBySlug(
  2311. mode_slug,
  2312. (await this.providerRef.deref()?.getState())?.customModes,
  2313. )
  2314. if (!targetMode) {
  2315. pushToolResult(formatResponse.toolError(`Invalid mode: ${mode_slug}`))
  2316. break
  2317. }
  2318. // Check if already in requested mode
  2319. const currentMode =
  2320. (await this.providerRef.deref()?.getState())?.mode ?? defaultModeSlug
  2321. if (currentMode === mode_slug) {
  2322. pushToolResult(`Already in ${targetMode.name} mode.`)
  2323. break
  2324. }
  2325. const completeMessage = JSON.stringify({
  2326. tool: "switchMode",
  2327. mode: mode_slug,
  2328. reason,
  2329. })
  2330. const didApprove = await askApproval("tool", completeMessage)
  2331. if (!didApprove) {
  2332. break
  2333. }
  2334. // Switch the mode using shared handler
  2335. const provider = this.providerRef.deref()
  2336. if (provider) {
  2337. await provider.handleModeSwitch(mode_slug)
  2338. }
  2339. pushToolResult(
  2340. `Successfully switched from ${getModeBySlug(currentMode)?.name ?? currentMode} mode to ${
  2341. targetMode.name
  2342. } mode${reason ? ` because: ${reason}` : ""}.`,
  2343. )
  2344. await delay(500) // delay to allow mode change to take effect before next tool is executed
  2345. break
  2346. }
  2347. } catch (error) {
  2348. await handleError("switching mode", error)
  2349. break
  2350. }
  2351. }
  2352. case "new_task": {
  2353. const mode: string | undefined = block.params.mode
  2354. const message: string | undefined = block.params.message
  2355. try {
  2356. if (block.partial) {
  2357. const partialMessage = JSON.stringify({
  2358. tool: "newTask",
  2359. mode: removeClosingTag("mode", mode),
  2360. message: removeClosingTag("message", message),
  2361. })
  2362. await this.ask("tool", partialMessage, block.partial).catch(() => {})
  2363. break
  2364. } else {
  2365. if (!mode) {
  2366. this.consecutiveMistakeCount++
  2367. pushToolResult(await this.sayAndCreateMissingParamError("new_task", "mode"))
  2368. break
  2369. }
  2370. if (!message) {
  2371. this.consecutiveMistakeCount++
  2372. pushToolResult(await this.sayAndCreateMissingParamError("new_task", "message"))
  2373. break
  2374. }
  2375. this.consecutiveMistakeCount = 0
  2376. // Verify the mode exists
  2377. const targetMode = getModeBySlug(
  2378. mode,
  2379. (await this.providerRef.deref()?.getState())?.customModes,
  2380. )
  2381. if (!targetMode) {
  2382. pushToolResult(formatResponse.toolError(`Invalid mode: ${mode}`))
  2383. break
  2384. }
  2385. // Show what we're about to do
  2386. const toolMessage = JSON.stringify({
  2387. tool: "newTask",
  2388. mode: targetMode.name,
  2389. content: message,
  2390. })
  2391. const didApprove = await askApproval("tool", toolMessage)
  2392. if (!didApprove) {
  2393. break
  2394. }
  2395. // Switch mode first, then create new task instance
  2396. const provider = this.providerRef.deref()
  2397. if (provider) {
  2398. await provider.handleModeSwitch(mode)
  2399. await provider.initClineWithTask(message)
  2400. pushToolResult(
  2401. `Successfully created new task in ${targetMode.name} mode with message: ${message}`,
  2402. )
  2403. } else {
  2404. pushToolResult(
  2405. formatResponse.toolError("Failed to create new task: provider not available"),
  2406. )
  2407. }
  2408. break
  2409. }
  2410. } catch (error) {
  2411. await handleError("creating new task", error)
  2412. break
  2413. }
  2414. }
  2415. case "attempt_completion": {
  2416. /*
  2417. this.consecutiveMistakeCount = 0
  2418. let resultToSend = result
  2419. if (command) {
  2420. await this.say("completion_result", resultToSend)
  2421. // TODO: currently we don't handle if this command fails, it could be useful to let cline know and retry
  2422. const [didUserReject, commandResult] = await this.executeCommand(command, true)
  2423. // if we received non-empty string, the command was rejected or failed
  2424. if (commandResult) {
  2425. return [didUserReject, commandResult]
  2426. }
  2427. resultToSend = ""
  2428. }
  2429. const { response, text, images } = await this.ask("completion_result", resultToSend) // this prompts webview to show 'new task' button, and enable text input (which would be the 'text' here)
  2430. if (response === "yesButtonClicked") {
  2431. return [false, ""] // signals to recursive loop to stop (for now this never happens since yesButtonClicked will trigger a new task)
  2432. }
  2433. await this.say("user_feedback", text ?? "", images)
  2434. return [
  2435. */
  2436. const result: string | undefined = block.params.result
  2437. const command: string | undefined = block.params.command
  2438. try {
  2439. const lastMessage = this.clineMessages.at(-1)
  2440. if (block.partial) {
  2441. if (command) {
  2442. // the attempt_completion text is done, now we're getting command
  2443. // remove the previous partial attempt_completion ask, replace with say, post state to webview, then stream command
  2444. // const secondLastMessage = this.clineMessages.at(-2)
  2445. if (lastMessage && lastMessage.ask === "command") {
  2446. // update command
  2447. await this.ask(
  2448. "command",
  2449. removeClosingTag("command", command),
  2450. block.partial,
  2451. ).catch(() => {})
  2452. } else {
  2453. // last message is completion_result
  2454. // we have command string, which means we have the result as well, so finish it (doesnt have to exist yet)
  2455. await this.say(
  2456. "completion_result",
  2457. removeClosingTag("result", result),
  2458. undefined,
  2459. false,
  2460. )
  2461. await this.ask(
  2462. "command",
  2463. removeClosingTag("command", command),
  2464. block.partial,
  2465. ).catch(() => {})
  2466. }
  2467. } else {
  2468. // no command, still outputting partial result
  2469. await this.say(
  2470. "completion_result",
  2471. removeClosingTag("result", result),
  2472. undefined,
  2473. block.partial,
  2474. )
  2475. }
  2476. break
  2477. } else {
  2478. if (!result) {
  2479. this.consecutiveMistakeCount++
  2480. pushToolResult(
  2481. await this.sayAndCreateMissingParamError("attempt_completion", "result"),
  2482. )
  2483. break
  2484. }
  2485. this.consecutiveMistakeCount = 0
  2486. let commandResult: ToolResponse | undefined
  2487. if (command) {
  2488. if (lastMessage && lastMessage.ask !== "command") {
  2489. // havent sent a command message yet so first send completion_result then command
  2490. await this.say("completion_result", result, undefined, false)
  2491. }
  2492. // complete command message
  2493. const didApprove = await askApproval("command", command)
  2494. if (!didApprove) {
  2495. break
  2496. }
  2497. const [userRejected, execCommandResult] = await this.executeCommandTool(command!)
  2498. if (userRejected) {
  2499. this.didRejectTool = true
  2500. pushToolResult(execCommandResult)
  2501. break
  2502. }
  2503. // user didn't reject, but the command may have output
  2504. commandResult = execCommandResult
  2505. } else {
  2506. await this.say("completion_result", result, undefined, false)
  2507. }
  2508. // we already sent completion_result says, an empty string asks relinquishes control over button and field
  2509. const { response, text, images } = await this.ask("completion_result", "", false)
  2510. if (response === "yesButtonClicked") {
  2511. pushToolResult("") // signals to recursive loop to stop (for now this never happens since yesButtonClicked will trigger a new task)
  2512. break
  2513. }
  2514. await this.say("user_feedback", text ?? "", images)
  2515. const toolResults: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[] = []
  2516. if (commandResult) {
  2517. if (typeof commandResult === "string") {
  2518. toolResults.push({ type: "text", text: commandResult })
  2519. } else if (Array.isArray(commandResult)) {
  2520. toolResults.push(...commandResult)
  2521. }
  2522. }
  2523. toolResults.push({
  2524. type: "text",
  2525. text: `The user has provided feedback on the results. Consider their input to continue the task, and then attempt completion again.\n<feedback>\n${text}\n</feedback>`,
  2526. })
  2527. toolResults.push(...formatResponse.imageBlocks(images))
  2528. this.userMessageContent.push({
  2529. type: "text",
  2530. text: `${toolDescription()} Result:`,
  2531. })
  2532. this.userMessageContent.push(...toolResults)
  2533. break
  2534. }
  2535. } catch (error) {
  2536. await handleError("inspecting site", error)
  2537. break
  2538. }
  2539. }
  2540. }
  2541. break
  2542. }
  2543. if (isCheckpointPossible) {
  2544. await this.checkpointSave({ isFirst: false })
  2545. }
  2546. /*
  2547. Seeing out of bounds is fine, it means that the next too call is being built up and ready to add to assistantMessageContent to present.
  2548. When you see the UI inactive during this, it means that a tool is breaking without presenting any UI. For example the write_to_file tool was breaking when relpath was undefined, and for invalid relpath it never presented UI.
  2549. */
  2550. this.presentAssistantMessageLocked = false // this needs to be placed here, if not then calling this.presentAssistantMessage below would fail (sometimes) since it's locked
  2551. // NOTE: when tool is rejected, iterator stream is interrupted and it waits for userMessageContentReady to be true. Future calls to present will skip execution since didRejectTool and iterate until contentIndex is set to message length and it sets userMessageContentReady to true itself (instead of preemptively doing it in iterator)
  2552. if (!block.partial || this.didRejectTool || this.didAlreadyUseTool) {
  2553. // block is finished streaming and executing
  2554. if (this.currentStreamingContentIndex === this.assistantMessageContent.length - 1) {
  2555. // its okay that we increment if !didCompleteReadingStream, it'll just return bc out of bounds and as streaming continues it will call presentAssitantMessage if a new block is ready. if streaming is finished then we set userMessageContentReady to true when out of bounds. This gracefully allows the stream to continue on and all potential content blocks be presented.
  2556. // last block is complete and it is finished executing
  2557. this.userMessageContentReady = true // will allow pwaitfor to continue
  2558. }
  2559. // call next block if it exists (if not then read stream will call it when its ready)
  2560. this.currentStreamingContentIndex++ // need to increment regardless, so when read stream calls this function again it will be streaming the next block
  2561. if (this.currentStreamingContentIndex < this.assistantMessageContent.length) {
  2562. // there are already more content blocks to stream, so we'll call this function ourselves
  2563. // await this.presentAssistantContent()
  2564. this.presentAssistantMessage()
  2565. return
  2566. }
  2567. }
  2568. // block is partial, but the read stream may have finished
  2569. if (this.presentAssistantMessageHasPendingUpdates) {
  2570. this.presentAssistantMessage()
  2571. }
  2572. }
  2573. async recursivelyMakeClineRequests(
  2574. userContent: UserContent,
  2575. includeFileDetails: boolean = false,
  2576. ): Promise<boolean> {
  2577. if (this.abort) {
  2578. throw new Error("Roo Code instance aborted")
  2579. }
  2580. if (this.consecutiveMistakeCount >= 3) {
  2581. const { response, text, images } = await this.ask(
  2582. "mistake_limit_reached",
  2583. this.api.getModel().id.includes("claude")
  2584. ? `This may indicate a failure in his thought process or inability to use a tool properly, which can be mitigated with some user guidance (e.g. "Try breaking down the task into smaller steps").`
  2585. : "Roo Code uses complex prompts and iterative task execution that may be challenging for less capable models. For best results, it's recommended to use Claude 3.7 Sonnet for its advanced agentic coding capabilities.",
  2586. )
  2587. if (response === "messageResponse") {
  2588. userContent.push(
  2589. ...[
  2590. {
  2591. type: "text",
  2592. text: formatResponse.tooManyMistakes(text),
  2593. } as Anthropic.Messages.TextBlockParam,
  2594. ...formatResponse.imageBlocks(images),
  2595. ],
  2596. )
  2597. }
  2598. this.consecutiveMistakeCount = 0
  2599. }
  2600. // get previous api req's index to check token usage and determine if we need to truncate conversation history
  2601. const previousApiReqIndex = findLastIndex(this.clineMessages, (m) => m.say === "api_req_started")
  2602. // Save checkpoint if this is the first API request.
  2603. const isFirstRequest = this.clineMessages.filter((m) => m.say === "api_req_started").length === 0
  2604. if (isFirstRequest) {
  2605. await this.checkpointSave({ isFirst: true })
  2606. }
  2607. // getting verbose details is an expensive operation, it uses globby to top-down build file structure of project which for large projects can take a few seconds
  2608. // for the best UX we show a placeholder api_req_started message with a loading spinner as this happens
  2609. await this.say(
  2610. "api_req_started",
  2611. JSON.stringify({
  2612. request:
  2613. userContent.map((block) => formatContentBlockToMarkdown(block)).join("\n\n") + "\n\nLoading...",
  2614. }),
  2615. )
  2616. const [parsedUserContent, environmentDetails] = await this.loadContext(userContent, includeFileDetails)
  2617. userContent = parsedUserContent
  2618. // add environment details as its own text block, separate from tool results
  2619. userContent.push({ type: "text", text: environmentDetails })
  2620. await this.addToApiConversationHistory({ role: "user", content: userContent })
  2621. // since we sent off a placeholder api_req_started message to update the webview while waiting to actually start the API request (to load potential details for example), we need to update the text of that message
  2622. const lastApiReqIndex = findLastIndex(this.clineMessages, (m) => m.say === "api_req_started")
  2623. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  2624. request: userContent.map((block) => formatContentBlockToMarkdown(block)).join("\n\n"),
  2625. } satisfies ClineApiReqInfo)
  2626. await this.saveClineMessages()
  2627. await this.providerRef.deref()?.postStateToWebview()
  2628. try {
  2629. let cacheWriteTokens = 0
  2630. let cacheReadTokens = 0
  2631. let inputTokens = 0
  2632. let outputTokens = 0
  2633. let totalCost: number | undefined
  2634. // update api_req_started. we can't use api_req_finished anymore since it's a unique case where it could come after a streaming message (ie in the middle of being updated or executed)
  2635. // fortunately api_req_finished was always parsed out for the gui anyways, so it remains solely for legacy purposes to keep track of prices in tasks from history
  2636. // (it's worth removing a few months from now)
  2637. const updateApiReqMsg = (cancelReason?: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  2638. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  2639. ...JSON.parse(this.clineMessages[lastApiReqIndex].text || "{}"),
  2640. tokensIn: inputTokens,
  2641. tokensOut: outputTokens,
  2642. cacheWrites: cacheWriteTokens,
  2643. cacheReads: cacheReadTokens,
  2644. cost:
  2645. totalCost ??
  2646. calculateApiCost(
  2647. this.api.getModel().info,
  2648. inputTokens,
  2649. outputTokens,
  2650. cacheWriteTokens,
  2651. cacheReadTokens,
  2652. ),
  2653. cancelReason,
  2654. streamingFailedMessage,
  2655. } satisfies ClineApiReqInfo)
  2656. }
  2657. const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  2658. if (this.diffViewProvider.isEditing) {
  2659. await this.diffViewProvider.revertChanges() // closes diff view
  2660. }
  2661. // if last message is a partial we need to update and save it
  2662. const lastMessage = this.clineMessages.at(-1)
  2663. if (lastMessage && lastMessage.partial) {
  2664. // lastMessage.ts = Date.now() DO NOT update ts since it is used as a key for virtuoso list
  2665. lastMessage.partial = false
  2666. // instead of streaming partialMessage events, we do a save and post like normal to persist to disk
  2667. console.log("updating partial message", lastMessage)
  2668. // await this.saveClineMessages()
  2669. }
  2670. // Let assistant know their response was interrupted for when task is resumed
  2671. await this.addToApiConversationHistory({
  2672. role: "assistant",
  2673. content: [
  2674. {
  2675. type: "text",
  2676. text:
  2677. assistantMessage +
  2678. `\n\n[${
  2679. cancelReason === "streaming_failed"
  2680. ? "Response interrupted by API Error"
  2681. : "Response interrupted by user"
  2682. }]`,
  2683. },
  2684. ],
  2685. })
  2686. // update api_req_started to have cancelled and cost, so that we can display the cost of the partial stream
  2687. updateApiReqMsg(cancelReason, streamingFailedMessage)
  2688. await this.saveClineMessages()
  2689. // signals to provider that it can retrieve the saved messages from disk, as abortTask can not be awaited on in nature
  2690. this.didFinishAbortingStream = true
  2691. }
  2692. // reset streaming state
  2693. this.currentStreamingContentIndex = 0
  2694. this.assistantMessageContent = []
  2695. this.didCompleteReadingStream = false
  2696. this.userMessageContent = []
  2697. this.userMessageContentReady = false
  2698. this.didRejectTool = false
  2699. this.didAlreadyUseTool = false
  2700. this.presentAssistantMessageLocked = false
  2701. this.presentAssistantMessageHasPendingUpdates = false
  2702. await this.diffViewProvider.reset()
  2703. const stream = this.attemptApiRequest(previousApiReqIndex) // yields only if the first chunk is successful, otherwise will allow the user to retry the request (most likely due to rate limit error, which gets thrown on the first chunk)
  2704. let assistantMessage = ""
  2705. let reasoningMessage = ""
  2706. this.isStreaming = true
  2707. try {
  2708. for await (const chunk of stream) {
  2709. if (!chunk) {
  2710. // Sometimes chunk is undefined, no idea that can cause it, but this workaround seems to fix it
  2711. continue
  2712. }
  2713. switch (chunk.type) {
  2714. case "reasoning":
  2715. reasoningMessage += chunk.text
  2716. await this.say("reasoning", reasoningMessage, undefined, true)
  2717. break
  2718. case "usage":
  2719. inputTokens += chunk.inputTokens
  2720. outputTokens += chunk.outputTokens
  2721. cacheWriteTokens += chunk.cacheWriteTokens ?? 0
  2722. cacheReadTokens += chunk.cacheReadTokens ?? 0
  2723. totalCost = chunk.totalCost
  2724. break
  2725. case "text":
  2726. assistantMessage += chunk.text
  2727. // parse raw assistant message into content blocks
  2728. const prevLength = this.assistantMessageContent.length
  2729. this.assistantMessageContent = parseAssistantMessage(assistantMessage)
  2730. if (this.assistantMessageContent.length > prevLength) {
  2731. this.userMessageContentReady = false // new content we need to present, reset to false in case previous content set this to true
  2732. }
  2733. // present content to user
  2734. this.presentAssistantMessage()
  2735. break
  2736. }
  2737. if (this.abort) {
  2738. console.log(`aborting stream, this.abandoned = ${this.abandoned}`)
  2739. if (!this.abandoned) {
  2740. // only need to gracefully abort if this instance isn't abandoned (sometimes openrouter stream hangs, in which case this would affect future instances of cline)
  2741. await abortStream("user_cancelled")
  2742. }
  2743. break // aborts the stream
  2744. }
  2745. if (this.didRejectTool) {
  2746. // userContent has a tool rejection, so interrupt the assistant's response to present the user's feedback
  2747. assistantMessage += "\n\n[Response interrupted by user feedback]"
  2748. // this.userMessageContentReady = true // instead of setting this premptively, we allow the present iterator to finish and set userMessageContentReady when its ready
  2749. break
  2750. }
  2751. // PREV: we need to let the request finish for openrouter to get generation details
  2752. // UPDATE: it's better UX to interrupt the request at the cost of the api cost not being retrieved
  2753. if (this.didAlreadyUseTool) {
  2754. assistantMessage +=
  2755. "\n\n[Response interrupted by a tool use result. Only one tool may be used at a time and should be placed at the end of the message.]"
  2756. break
  2757. }
  2758. }
  2759. } catch (error) {
  2760. // abandoned happens when extension is no longer waiting for the cline instance to finish aborting (error is thrown here when any function in the for loop throws due to this.abort)
  2761. if (!this.abandoned) {
  2762. this.abortTask() // if the stream failed, there's various states the task could be in (i.e. could have streamed some tools the user may have executed), so we just resort to replicating a cancel task
  2763. await abortStream(
  2764. "streaming_failed",
  2765. error.message ?? JSON.stringify(serializeError(error), null, 2),
  2766. )
  2767. const history = await this.providerRef.deref()?.getTaskWithId(this.taskId)
  2768. if (history) {
  2769. await this.providerRef.deref()?.initClineWithHistoryItem(history.historyItem)
  2770. // await this.providerRef.deref()?.postStateToWebview()
  2771. }
  2772. }
  2773. } finally {
  2774. this.isStreaming = false
  2775. }
  2776. // need to call here in case the stream was aborted
  2777. if (this.abort || this.abandoned) {
  2778. throw new Error("Roo Code instance aborted")
  2779. }
  2780. this.didCompleteReadingStream = true
  2781. // set any blocks to be complete to allow presentAssistantMessage to finish and set userMessageContentReady to true
  2782. // (could be a text block that had no subsequent tool uses, or a text block at the very end, or an invalid tool use, etc. whatever the case, presentAssistantMessage relies on these blocks either to be completed or the user to reject a block in order to proceed and eventually set userMessageContentReady to true)
  2783. const partialBlocks = this.assistantMessageContent.filter((block) => block.partial)
  2784. partialBlocks.forEach((block) => {
  2785. block.partial = false
  2786. })
  2787. // this.assistantMessageContent.forEach((e) => (e.partial = false)) // cant just do this bc a tool could be in the middle of executing ()
  2788. if (partialBlocks.length > 0) {
  2789. this.presentAssistantMessage() // if there is content to update then it will complete and update this.userMessageContentReady to true, which we pwaitfor before making the next request. all this is really doing is presenting the last partial message that we just set to complete
  2790. }
  2791. updateApiReqMsg()
  2792. await this.saveClineMessages()
  2793. await this.providerRef.deref()?.postStateToWebview()
  2794. // now add to apiconversationhistory
  2795. // need to save assistant responses to file before proceeding to tool use since user can exit at any moment and we wouldn't be able to save the assistant's response
  2796. let didEndLoop = false
  2797. if (assistantMessage.length > 0) {
  2798. await this.addToApiConversationHistory({
  2799. role: "assistant",
  2800. content: [{ type: "text", text: assistantMessage }],
  2801. })
  2802. // NOTE: this comment is here for future reference - this was a workaround for userMessageContent not getting set to true. It was due to it not recursively calling for partial blocks when didRejectTool, so it would get stuck waiting for a partial block to complete before it could continue.
  2803. // in case the content blocks finished
  2804. // it may be the api stream finished after the last parsed content block was executed, so we are able to detect out of bounds and set userMessageContentReady to true (note you should not call presentAssistantMessage since if the last block is completed it will be presented again)
  2805. // const completeBlocks = this.assistantMessageContent.filter((block) => !block.partial) // if there are any partial blocks after the stream ended we can consider them invalid
  2806. // if (this.currentStreamingContentIndex >= completeBlocks.length) {
  2807. // this.userMessageContentReady = true
  2808. // }
  2809. await pWaitFor(() => this.userMessageContentReady)
  2810. // if the model did not tool use, then we need to tell it to either use a tool or attempt_completion
  2811. const didToolUse = this.assistantMessageContent.some((block) => block.type === "tool_use")
  2812. if (!didToolUse) {
  2813. this.userMessageContent.push({
  2814. type: "text",
  2815. text: formatResponse.noToolsUsed(),
  2816. })
  2817. this.consecutiveMistakeCount++
  2818. }
  2819. const recDidEndLoop = await this.recursivelyMakeClineRequests(this.userMessageContent)
  2820. didEndLoop = recDidEndLoop
  2821. } else {
  2822. // if there's no assistant_responses, that means we got no text or tool_use content blocks from API which we should assume is an error
  2823. await this.say(
  2824. "error",
  2825. "Unexpected API Response: The language model did not provide any assistant messages. This may indicate an issue with the API or the model's output.",
  2826. )
  2827. await this.addToApiConversationHistory({
  2828. role: "assistant",
  2829. content: [{ type: "text", text: "Failure: I did not provide a response." }],
  2830. })
  2831. }
  2832. return didEndLoop // will always be false for now
  2833. } catch (error) {
  2834. // this should never happen since the only thing that can throw an error is the attemptApiRequest, which is wrapped in a try catch that sends an ask where if noButtonClicked, will clear current task and destroy this instance. However to avoid unhandled promise rejection, we will end this loop which will end execution of this instance (see startTask)
  2835. return true // needs to be true so parent loop knows to end task
  2836. }
  2837. }
  2838. async loadContext(userContent: UserContent, includeFileDetails: boolean = false) {
  2839. return await Promise.all([
  2840. // Process userContent array, which contains various block types:
  2841. // TextBlockParam, ImageBlockParam, ToolUseBlockParam, and ToolResultBlockParam.
  2842. // We need to apply parseMentions() to:
  2843. // 1. All TextBlockParam's text (first user message with task)
  2844. // 2. ToolResultBlockParam's content/context text arrays if it contains "<feedback>" (see formatToolDeniedFeedback, attemptCompletion, executeCommand, and consecutiveMistakeCount >= 3) or "<answer>" (see askFollowupQuestion), we place all user generated content in these tags so they can effectively be used as markers for when we should parse mentions)
  2845. Promise.all(
  2846. userContent.map(async (block) => {
  2847. const shouldProcessMentions = (text: string) =>
  2848. text.includes("<task>") || text.includes("<feedback>")
  2849. if (block.type === "text") {
  2850. if (shouldProcessMentions(block.text)) {
  2851. return {
  2852. ...block,
  2853. text: await parseMentions(block.text, cwd, this.urlContentFetcher),
  2854. }
  2855. }
  2856. return block
  2857. } else if (block.type === "tool_result") {
  2858. if (typeof block.content === "string") {
  2859. if (shouldProcessMentions(block.content)) {
  2860. return {
  2861. ...block,
  2862. content: await parseMentions(block.content, cwd, this.urlContentFetcher),
  2863. }
  2864. }
  2865. return block
  2866. } else if (Array.isArray(block.content)) {
  2867. const parsedContent = await Promise.all(
  2868. block.content.map(async (contentBlock) => {
  2869. if (contentBlock.type === "text" && shouldProcessMentions(contentBlock.text)) {
  2870. return {
  2871. ...contentBlock,
  2872. text: await parseMentions(contentBlock.text, cwd, this.urlContentFetcher),
  2873. }
  2874. }
  2875. return contentBlock
  2876. }),
  2877. )
  2878. return {
  2879. ...block,
  2880. content: parsedContent,
  2881. }
  2882. }
  2883. return block
  2884. }
  2885. return block
  2886. }),
  2887. ),
  2888. this.getEnvironmentDetails(includeFileDetails),
  2889. ])
  2890. }
  2891. async getEnvironmentDetails(includeFileDetails: boolean = false) {
  2892. let details = ""
  2893. // It could be useful for cline to know if the user went from one or no file to another between messages, so we always include this context
  2894. details += "\n\n# VSCode Visible Files"
  2895. const visibleFiles = vscode.window.visibleTextEditors
  2896. ?.map((editor) => editor.document?.uri?.fsPath)
  2897. .filter(Boolean)
  2898. .map((absolutePath) => path.relative(cwd, absolutePath).toPosix())
  2899. .join("\n")
  2900. if (visibleFiles) {
  2901. details += `\n${visibleFiles}`
  2902. } else {
  2903. details += "\n(No visible files)"
  2904. }
  2905. details += "\n\n# VSCode Open Tabs"
  2906. const { maxOpenTabsContext } = (await this.providerRef.deref()?.getState()) ?? {}
  2907. const maxTabs = maxOpenTabsContext ?? 20
  2908. const openTabs = vscode.window.tabGroups.all
  2909. .flatMap((group) => group.tabs)
  2910. .map((tab) => (tab.input as vscode.TabInputText)?.uri?.fsPath)
  2911. .filter(Boolean)
  2912. .map((absolutePath) => path.relative(cwd, absolutePath).toPosix())
  2913. .slice(0, maxTabs)
  2914. .join("\n")
  2915. if (openTabs) {
  2916. details += `\n${openTabs}`
  2917. } else {
  2918. details += "\n(No open tabs)"
  2919. }
  2920. const busyTerminals = this.terminalManager.getTerminals(true)
  2921. const inactiveTerminals = this.terminalManager.getTerminals(false)
  2922. // const allTerminals = [...busyTerminals, ...inactiveTerminals]
  2923. if (busyTerminals.length > 0 && this.didEditFile) {
  2924. // || this.didEditFile
  2925. await delay(300) // delay after saving file to let terminals catch up
  2926. }
  2927. // let terminalWasBusy = false
  2928. if (busyTerminals.length > 0) {
  2929. // wait for terminals to cool down
  2930. // terminalWasBusy = allTerminals.some((t) => this.terminalManager.isProcessHot(t.id))
  2931. await pWaitFor(() => busyTerminals.every((t) => !this.terminalManager.isProcessHot(t.id)), {
  2932. interval: 100,
  2933. timeout: 15_000,
  2934. }).catch(() => {})
  2935. }
  2936. // we want to get diagnostics AFTER terminal cools down for a few reasons: terminal could be scaffolding a project, dev servers (compilers like webpack) will first re-compile and then send diagnostics, etc
  2937. /*
  2938. let diagnosticsDetails = ""
  2939. const diagnostics = await this.diagnosticsMonitor.getCurrentDiagnostics(this.didEditFile || terminalWasBusy) // if cline ran a command (ie npm install) or edited the workspace then wait a bit for updated diagnostics
  2940. for (const [uri, fileDiagnostics] of diagnostics) {
  2941. const problems = fileDiagnostics.filter((d) => d.severity === vscode.DiagnosticSeverity.Error)
  2942. if (problems.length > 0) {
  2943. diagnosticsDetails += `\n## ${path.relative(cwd, uri.fsPath)}`
  2944. for (const diagnostic of problems) {
  2945. // let severity = diagnostic.severity === vscode.DiagnosticSeverity.Error ? "Error" : "Warning"
  2946. const line = diagnostic.range.start.line + 1 // VSCode lines are 0-indexed
  2947. const source = diagnostic.source ? `[${diagnostic.source}] ` : ""
  2948. diagnosticsDetails += `\n- ${source}Line ${line}: ${diagnostic.message}`
  2949. }
  2950. }
  2951. }
  2952. */
  2953. this.didEditFile = false // reset, this lets us know when to wait for saved files to update terminals
  2954. // waiting for updated diagnostics lets terminal output be the most up-to-date possible
  2955. let terminalDetails = ""
  2956. if (busyTerminals.length > 0) {
  2957. // terminals are cool, let's retrieve their output
  2958. terminalDetails += "\n\n# Actively Running Terminals"
  2959. for (const busyTerminal of busyTerminals) {
  2960. terminalDetails += `\n## Original command: \`${busyTerminal.lastCommand}\``
  2961. const newOutput = this.terminalManager.getUnretrievedOutput(busyTerminal.id)
  2962. if (newOutput) {
  2963. terminalDetails += `\n### New Output\n${newOutput}`
  2964. } else {
  2965. // details += `\n(Still running, no new output)` // don't want to show this right after running the command
  2966. }
  2967. }
  2968. }
  2969. // only show inactive terminals if there's output to show
  2970. if (inactiveTerminals.length > 0) {
  2971. const inactiveTerminalOutputs = new Map<number, string>()
  2972. for (const inactiveTerminal of inactiveTerminals) {
  2973. const newOutput = this.terminalManager.getUnretrievedOutput(inactiveTerminal.id)
  2974. if (newOutput) {
  2975. inactiveTerminalOutputs.set(inactiveTerminal.id, newOutput)
  2976. }
  2977. }
  2978. if (inactiveTerminalOutputs.size > 0) {
  2979. terminalDetails += "\n\n# Inactive Terminals"
  2980. for (const [terminalId, newOutput] of inactiveTerminalOutputs) {
  2981. const inactiveTerminal = inactiveTerminals.find((t) => t.id === terminalId)
  2982. if (inactiveTerminal) {
  2983. terminalDetails += `\n## ${inactiveTerminal.lastCommand}`
  2984. terminalDetails += `\n### New Output\n${newOutput}`
  2985. }
  2986. }
  2987. }
  2988. }
  2989. // details += "\n\n# VSCode Workspace Errors"
  2990. // if (diagnosticsDetails) {
  2991. // details += diagnosticsDetails
  2992. // } else {
  2993. // details += "\n(No errors detected)"
  2994. // }
  2995. if (terminalDetails) {
  2996. details += terminalDetails
  2997. }
  2998. // Add current time information with timezone
  2999. const now = new Date()
  3000. const formatter = new Intl.DateTimeFormat(undefined, {
  3001. year: "numeric",
  3002. month: "numeric",
  3003. day: "numeric",
  3004. hour: "numeric",
  3005. minute: "numeric",
  3006. second: "numeric",
  3007. hour12: true,
  3008. })
  3009. const timeZone = formatter.resolvedOptions().timeZone
  3010. const timeZoneOffset = -now.getTimezoneOffset() / 60 // Convert to hours and invert sign to match conventional notation
  3011. const timeZoneOffsetStr = `${timeZoneOffset >= 0 ? "+" : ""}${timeZoneOffset}:00`
  3012. details += `\n\n# Current Time\n${formatter.format(now)} (${timeZone}, UTC${timeZoneOffsetStr})`
  3013. // Add context tokens information
  3014. const { contextTokens } = getApiMetrics(this.clineMessages)
  3015. const modelInfo = this.api.getModel().info
  3016. const contextWindow = modelInfo.contextWindow
  3017. const contextPercentage =
  3018. contextTokens && contextWindow ? Math.round((contextTokens / contextWindow) * 100) : undefined
  3019. details += `\n\n# Current Context Size (Tokens)\n${contextTokens ? `${contextTokens.toLocaleString()} (${contextPercentage}%)` : "(Not available)"}`
  3020. // Add current mode and any mode-specific warnings
  3021. const {
  3022. mode,
  3023. customModes,
  3024. customModePrompts,
  3025. experiments = {} as Record<ExperimentId, boolean>,
  3026. customInstructions: globalCustomInstructions,
  3027. preferredLanguage,
  3028. } = (await this.providerRef.deref()?.getState()) ?? {}
  3029. const currentMode = mode ?? defaultModeSlug
  3030. const modeDetails = await getFullModeDetails(currentMode, customModes, customModePrompts, {
  3031. cwd,
  3032. globalCustomInstructions,
  3033. preferredLanguage,
  3034. })
  3035. details += `\n\n# Current Mode\n`
  3036. details += `<slug>${currentMode}</slug>\n`
  3037. details += `<name>${modeDetails.name}</name>\n`
  3038. if (Experiments.isEnabled(experiments ?? {}, EXPERIMENT_IDS.POWER_STEERING)) {
  3039. details += `<role>${modeDetails.roleDefinition}</role>\n`
  3040. if (modeDetails.customInstructions) {
  3041. details += `<custom_instructions>${modeDetails.customInstructions}</custom_instructions>\n`
  3042. }
  3043. }
  3044. // Add warning if not in code mode
  3045. if (
  3046. !isToolAllowedForMode("write_to_file", currentMode, customModes ?? [], {
  3047. apply_diff: this.diffEnabled,
  3048. }) &&
  3049. !isToolAllowedForMode("apply_diff", currentMode, customModes ?? [], { apply_diff: this.diffEnabled })
  3050. ) {
  3051. const currentModeName = getModeBySlug(currentMode, customModes)?.name ?? currentMode
  3052. const defaultModeName = getModeBySlug(defaultModeSlug, customModes)?.name ?? defaultModeSlug
  3053. details += `\n\nNOTE: You are currently in '${currentModeName}' mode which only allows read-only operations. To write files or execute commands, the user will need to switch to '${defaultModeName}' mode. Note that only the user can switch modes.`
  3054. }
  3055. if (includeFileDetails) {
  3056. details += `\n\n# Current Working Directory (${cwd.toPosix()}) Files\n`
  3057. const isDesktop = arePathsEqual(cwd, path.join(os.homedir(), "Desktop"))
  3058. if (isDesktop) {
  3059. // don't want to immediately access desktop since it would show permission popup
  3060. details += "(Desktop files not shown automatically. Use list_files to explore if needed.)"
  3061. } else {
  3062. const [files, didHitLimit] = await listFiles(cwd, true, 200)
  3063. const result = formatResponse.formatFilesList(cwd, files, didHitLimit)
  3064. details += result
  3065. }
  3066. }
  3067. return `<environment_details>\n${details.trim()}\n</environment_details>`
  3068. }
  3069. // Checkpoints
  3070. private async getCheckpointService() {
  3071. if (!this.enableCheckpoints) {
  3072. throw new Error("Checkpoints are disabled")
  3073. }
  3074. if (!this.checkpointService) {
  3075. const workspaceDir = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0)
  3076. const shadowDir = this.providerRef.deref()?.context.globalStorageUri.fsPath
  3077. if (!workspaceDir) {
  3078. this.providerRef.deref()?.log("[getCheckpointService] workspace folder not found")
  3079. throw new Error("Workspace directory not found")
  3080. }
  3081. if (!shadowDir) {
  3082. this.providerRef.deref()?.log("[getCheckpointService] shadowDir not found")
  3083. throw new Error("Global storage directory not found")
  3084. }
  3085. this.checkpointService = await CheckpointServiceFactory.create({
  3086. strategy: "shadow",
  3087. options: {
  3088. taskId: this.taskId,
  3089. workspaceDir,
  3090. shadowDir,
  3091. log: (message) => this.providerRef.deref()?.log(message),
  3092. },
  3093. })
  3094. }
  3095. return this.checkpointService
  3096. }
  3097. public async checkpointDiff({
  3098. ts,
  3099. commitHash,
  3100. mode,
  3101. }: {
  3102. ts: number
  3103. commitHash: string
  3104. mode: "full" | "checkpoint"
  3105. }) {
  3106. if (!this.enableCheckpoints) {
  3107. return
  3108. }
  3109. let previousCommitHash = undefined
  3110. if (mode === "checkpoint") {
  3111. const previousCheckpoint = this.clineMessages
  3112. .filter(({ say }) => say === "checkpoint_saved")
  3113. .sort((a, b) => b.ts - a.ts)
  3114. .find((message) => message.ts < ts)
  3115. previousCommitHash = previousCheckpoint?.text
  3116. }
  3117. try {
  3118. const service = await this.getCheckpointService()
  3119. const changes = await service.getDiff({ from: previousCommitHash, to: commitHash })
  3120. if (!changes?.length) {
  3121. vscode.window.showInformationMessage("No changes found.")
  3122. return
  3123. }
  3124. await vscode.commands.executeCommand(
  3125. "vscode.changes",
  3126. mode === "full" ? "Changes since task started" : "Changes since previous checkpoint",
  3127. changes.map((change) => [
  3128. vscode.Uri.file(change.paths.absolute),
  3129. vscode.Uri.parse(`${DIFF_VIEW_URI_SCHEME}:${change.paths.relative}`).with({
  3130. query: Buffer.from(change.content.before ?? "").toString("base64"),
  3131. }),
  3132. vscode.Uri.parse(`${DIFF_VIEW_URI_SCHEME}:${change.paths.relative}`).with({
  3133. query: Buffer.from(change.content.after ?? "").toString("base64"),
  3134. }),
  3135. ]),
  3136. )
  3137. } catch (err) {
  3138. this.providerRef.deref()?.log("[checkpointDiff] disabling checkpoints for this task")
  3139. this.enableCheckpoints = false
  3140. }
  3141. }
  3142. public async checkpointSave({ isFirst }: { isFirst: boolean }) {
  3143. if (!this.enableCheckpoints) {
  3144. return
  3145. }
  3146. try {
  3147. const service = await this.getCheckpointService()
  3148. const strategy = service.strategy
  3149. const version = service.version
  3150. const commit = await service.saveCheckpoint(`Task: ${this.taskId}, Time: ${Date.now()}`)
  3151. const fromHash = service.baseHash
  3152. const toHash = isFirst ? commit?.commit || fromHash : commit?.commit
  3153. if (toHash) {
  3154. await this.providerRef.deref()?.postMessageToWebview({ type: "currentCheckpointUpdated", text: toHash })
  3155. const checkpoint = { isFirst, from: fromHash, to: toHash, strategy, version }
  3156. await this.say("checkpoint_saved", toHash, undefined, undefined, checkpoint)
  3157. }
  3158. } catch (err) {
  3159. this.providerRef.deref()?.log("[checkpointSave] disabling checkpoints for this task")
  3160. this.enableCheckpoints = false
  3161. }
  3162. }
  3163. public async checkpointRestore({
  3164. ts,
  3165. commitHash,
  3166. mode,
  3167. }: {
  3168. ts: number
  3169. commitHash: string
  3170. mode: "preview" | "restore"
  3171. }) {
  3172. if (!this.enableCheckpoints) {
  3173. return
  3174. }
  3175. const index = this.clineMessages.findIndex((m) => m.ts === ts)
  3176. if (index === -1) {
  3177. return
  3178. }
  3179. try {
  3180. const service = await this.getCheckpointService()
  3181. await service.restoreCheckpoint(commitHash)
  3182. await this.providerRef.deref()?.postMessageToWebview({ type: "currentCheckpointUpdated", text: commitHash })
  3183. if (mode === "restore") {
  3184. await this.overwriteApiConversationHistory(
  3185. this.apiConversationHistory.filter((m) => !m.ts || m.ts < ts),
  3186. )
  3187. const deletedMessages = this.clineMessages.slice(index + 1)
  3188. const { totalTokensIn, totalTokensOut, totalCacheWrites, totalCacheReads, totalCost } = getApiMetrics(
  3189. combineApiRequests(combineCommandSequences(deletedMessages)),
  3190. )
  3191. await this.overwriteClineMessages(this.clineMessages.slice(0, index + 1))
  3192. // TODO: Verify that this is working as expected.
  3193. await this.say(
  3194. "api_req_deleted",
  3195. JSON.stringify({
  3196. tokensIn: totalTokensIn,
  3197. tokensOut: totalTokensOut,
  3198. cacheWrites: totalCacheWrites,
  3199. cacheReads: totalCacheReads,
  3200. cost: totalCost,
  3201. } satisfies ClineApiReqInfo),
  3202. )
  3203. }
  3204. // The task is already cancelled by the provider beforehand, but we
  3205. // need to re-init to get the updated messages.
  3206. //
  3207. // This was take from Cline's implementation of the checkpoints
  3208. // feature. The cline instance will hang if we don't cancel twice,
  3209. // so this is currently necessary, but it seems like a complicated
  3210. // and hacky solution to a problem that I don't fully understand.
  3211. // I'd like to revisit this in the future and try to improve the
  3212. // task flow and the communication between the webview and the
  3213. // Cline instance.
  3214. this.providerRef.deref()?.cancelTask()
  3215. } catch (err) {
  3216. this.providerRef.deref()?.log("[checkpointRestore] disabling checkpoints for this task")
  3217. this.enableCheckpoints = false
  3218. }
  3219. }
  3220. }
  3221. function escapeRegExp(string: string): string {
  3222. return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
  3223. }