Cline.ts 150 KB

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