Task.ts 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. import * as path from "path"
  2. import os from "os"
  3. import crypto from "crypto"
  4. import EventEmitter from "events"
  5. import { Anthropic } from "@anthropic-ai/sdk"
  6. import delay from "delay"
  7. import pWaitFor from "p-wait-for"
  8. import { serializeError } from "serialize-error"
  9. import {
  10. type ProviderSettings,
  11. type TokenUsage,
  12. type ToolUsage,
  13. type ToolName,
  14. type ContextCondense,
  15. type ClineAsk,
  16. type ClineMessage,
  17. type ClineSay,
  18. type ToolProgressStatus,
  19. type HistoryItem,
  20. TelemetryEventName,
  21. } from "@roo-code/types"
  22. import { TelemetryService } from "@roo-code/telemetry"
  23. import { CloudService } from "@roo-code/cloud"
  24. // api
  25. import { ApiHandler, ApiHandlerCreateMessageMetadata, buildApiHandler } from "../../api"
  26. import { ApiStream } from "../../api/transform/stream"
  27. // shared
  28. import { findLastIndex } from "../../shared/array"
  29. import { combineApiRequests } from "../../shared/combineApiRequests"
  30. import { combineCommandSequences } from "../../shared/combineCommandSequences"
  31. import { t } from "../../i18n"
  32. import { ClineApiReqCancelReason, ClineApiReqInfo } from "../../shared/ExtensionMessage"
  33. import { getApiMetrics } from "../../shared/getApiMetrics"
  34. import { ClineAskResponse } from "../../shared/WebviewMessage"
  35. import { defaultModeSlug } from "../../shared/modes"
  36. import { DiffStrategy } from "../../shared/tools"
  37. import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
  38. // services
  39. import { UrlContentFetcher } from "../../services/browser/UrlContentFetcher"
  40. import { BrowserSession } from "../../services/browser/BrowserSession"
  41. import { McpHub } from "../../services/mcp/McpHub"
  42. import { McpServerManager } from "../../services/mcp/McpServerManager"
  43. import { RepoPerTaskCheckpointService } from "../../services/checkpoints"
  44. // integrations
  45. import { DiffViewProvider } from "../../integrations/editor/DiffViewProvider"
  46. import { findToolName, formatContentBlockToMarkdown } from "../../integrations/misc/export-markdown"
  47. import { RooTerminalProcess } from "../../integrations/terminal/types"
  48. import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry"
  49. // utils
  50. import { calculateApiCostAnthropic } from "../../shared/cost"
  51. import { getWorkspacePath } from "../../utils/path"
  52. // prompts
  53. import { formatResponse } from "../prompts/responses"
  54. import { SYSTEM_PROMPT } from "../prompts/system"
  55. // core modules
  56. import { ToolRepetitionDetector } from "../tools/ToolRepetitionDetector"
  57. import { FileContextTracker } from "../context-tracking/FileContextTracker"
  58. import { RooIgnoreController } from "../ignore/RooIgnoreController"
  59. import { RooProtectedController } from "../protect/RooProtectedController"
  60. import { type AssistantMessageContent, parseAssistantMessage, presentAssistantMessage } from "../assistant-message"
  61. import { truncateConversationIfNeeded } from "../sliding-window"
  62. import { ClineProvider } from "../webview/ClineProvider"
  63. import { MultiSearchReplaceDiffStrategy } from "../diff/strategies/multi-search-replace"
  64. import { MultiFileSearchReplaceDiffStrategy } from "../diff/strategies/multi-file-search-replace"
  65. import { readApiMessages, saveApiMessages, readTaskMessages, saveTaskMessages, taskMetadata } from "../task-persistence"
  66. import { getEnvironmentDetails } from "../environment/getEnvironmentDetails"
  67. import {
  68. type CheckpointDiffOptions,
  69. type CheckpointRestoreOptions,
  70. getCheckpointService,
  71. checkpointSave,
  72. checkpointRestore,
  73. checkpointDiff,
  74. } from "../checkpoints"
  75. import { processUserContentMentions } from "../mentions/processUserContentMentions"
  76. import { ApiMessage } from "../task-persistence/apiMessages"
  77. import { getMessagesSinceLastSummary, summarizeConversation } from "../condense"
  78. import { maybeRemoveImageBlocks } from "../../api/transform/image-cleaning"
  79. export type ClineEvents = {
  80. message: [{ action: "created" | "updated"; message: ClineMessage }]
  81. taskStarted: []
  82. taskModeSwitched: [taskId: string, mode: string]
  83. taskPaused: []
  84. taskUnpaused: []
  85. taskAskResponded: []
  86. taskAborted: []
  87. taskSpawned: [taskId: string]
  88. taskCompleted: [taskId: string, tokenUsage: TokenUsage, toolUsage: ToolUsage]
  89. taskTokenUsageUpdated: [taskId: string, tokenUsage: TokenUsage]
  90. taskToolFailed: [taskId: string, tool: ToolName, error: string]
  91. }
  92. export type TaskOptions = {
  93. provider: ClineProvider
  94. apiConfiguration: ProviderSettings
  95. enableDiff?: boolean
  96. enableCheckpoints?: boolean
  97. fuzzyMatchThreshold?: number
  98. consecutiveMistakeLimit?: number
  99. task?: string
  100. images?: string[]
  101. historyItem?: HistoryItem
  102. experiments?: Record<string, boolean>
  103. startTask?: boolean
  104. rootTask?: Task
  105. parentTask?: Task
  106. taskNumber?: number
  107. onCreated?: (cline: Task) => void
  108. }
  109. export class Task extends EventEmitter<ClineEvents> {
  110. readonly taskId: string
  111. readonly instanceId: string
  112. readonly rootTask: Task | undefined = undefined
  113. readonly parentTask: Task | undefined = undefined
  114. readonly taskNumber: number
  115. readonly workspacePath: string
  116. providerRef: WeakRef<ClineProvider>
  117. private readonly globalStoragePath: string
  118. abort: boolean = false
  119. didFinishAbortingStream = false
  120. abandoned = false
  121. isInitialized = false
  122. isPaused: boolean = false
  123. pausedModeSlug: string = defaultModeSlug
  124. private pauseInterval: NodeJS.Timeout | undefined
  125. // API
  126. readonly apiConfiguration: ProviderSettings
  127. api: ApiHandler
  128. private static lastGlobalApiRequestTime?: number
  129. private consecutiveAutoApprovedRequestsCount: number = 0
  130. /**
  131. * Reset the global API request timestamp. This should only be used for testing.
  132. * @internal
  133. */
  134. static resetGlobalApiRequestTime(): void {
  135. Task.lastGlobalApiRequestTime = undefined
  136. }
  137. toolRepetitionDetector: ToolRepetitionDetector
  138. rooIgnoreController?: RooIgnoreController
  139. rooProtectedController?: RooProtectedController
  140. fileContextTracker: FileContextTracker
  141. urlContentFetcher: UrlContentFetcher
  142. terminalProcess?: RooTerminalProcess
  143. // Computer User
  144. browserSession: BrowserSession
  145. // Editing
  146. diffViewProvider: DiffViewProvider
  147. diffStrategy?: DiffStrategy
  148. diffEnabled: boolean = false
  149. fuzzyMatchThreshold: number
  150. didEditFile: boolean = false
  151. // LLM Messages & Chat Messages
  152. apiConversationHistory: ApiMessage[] = []
  153. clineMessages: ClineMessage[] = []
  154. // Ask
  155. private askResponse?: ClineAskResponse
  156. private askResponseText?: string
  157. private askResponseImages?: string[]
  158. public lastMessageTs?: number
  159. // Tool Use
  160. consecutiveMistakeCount: number = 0
  161. consecutiveMistakeLimit: number
  162. consecutiveMistakeCountForApplyDiff: Map<string, number> = new Map()
  163. toolUsage: ToolUsage = {}
  164. // Checkpoints
  165. enableCheckpoints: boolean
  166. checkpointService?: RepoPerTaskCheckpointService
  167. checkpointServiceInitializing = false
  168. // Streaming
  169. isWaitingForFirstChunk = false
  170. isStreaming = false
  171. currentStreamingContentIndex = 0
  172. assistantMessageContent: AssistantMessageContent[] = []
  173. presentAssistantMessageLocked = false
  174. presentAssistantMessageHasPendingUpdates = false
  175. userMessageContent: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam)[] = []
  176. userMessageContentReady = false
  177. didRejectTool = false
  178. didAlreadyUseTool = false
  179. didCompleteReadingStream = false
  180. constructor({
  181. provider,
  182. apiConfiguration,
  183. enableDiff = false,
  184. enableCheckpoints = true,
  185. fuzzyMatchThreshold = 1.0,
  186. consecutiveMistakeLimit = 3,
  187. task,
  188. images,
  189. historyItem,
  190. startTask = true,
  191. rootTask,
  192. parentTask,
  193. taskNumber = -1,
  194. onCreated,
  195. }: TaskOptions) {
  196. super()
  197. if (startTask && !task && !images && !historyItem) {
  198. throw new Error("Either historyItem or task/images must be provided")
  199. }
  200. this.taskId = historyItem ? historyItem.id : crypto.randomUUID()
  201. // normal use-case is usually retry similar history task with new workspace
  202. this.workspacePath = parentTask
  203. ? parentTask.workspacePath
  204. : getWorkspacePath(path.join(os.homedir(), "Desktop"))
  205. this.instanceId = crypto.randomUUID().slice(0, 8)
  206. this.taskNumber = -1
  207. this.rooIgnoreController = new RooIgnoreController(this.cwd)
  208. this.rooProtectedController = new RooProtectedController(this.cwd)
  209. this.fileContextTracker = new FileContextTracker(provider, this.taskId)
  210. this.rooIgnoreController.initialize().catch((error) => {
  211. console.error("Failed to initialize RooIgnoreController:", error)
  212. })
  213. this.apiConfiguration = apiConfiguration
  214. this.api = buildApiHandler(apiConfiguration)
  215. this.urlContentFetcher = new UrlContentFetcher(provider.context)
  216. this.browserSession = new BrowserSession(provider.context)
  217. this.diffEnabled = enableDiff
  218. this.fuzzyMatchThreshold = fuzzyMatchThreshold
  219. this.consecutiveMistakeLimit = consecutiveMistakeLimit
  220. this.providerRef = new WeakRef(provider)
  221. this.globalStoragePath = provider.context.globalStorageUri.fsPath
  222. this.diffViewProvider = new DiffViewProvider(this.cwd)
  223. this.enableCheckpoints = enableCheckpoints
  224. this.rootTask = rootTask
  225. this.parentTask = parentTask
  226. this.taskNumber = taskNumber
  227. if (historyItem) {
  228. TelemetryService.instance.captureTaskRestarted(this.taskId)
  229. } else {
  230. TelemetryService.instance.captureTaskCreated(this.taskId)
  231. }
  232. // Only set up diff strategy if diff is enabled
  233. if (this.diffEnabled) {
  234. // Default to old strategy, will be updated if experiment is enabled
  235. this.diffStrategy = new MultiSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
  236. // Check experiment asynchronously and update strategy if needed
  237. provider.getState().then((state) => {
  238. const isMultiFileApplyDiffEnabled = experiments.isEnabled(
  239. state.experiments ?? {},
  240. EXPERIMENT_IDS.MULTI_FILE_APPLY_DIFF,
  241. )
  242. if (isMultiFileApplyDiffEnabled) {
  243. this.diffStrategy = new MultiFileSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
  244. }
  245. })
  246. }
  247. this.toolRepetitionDetector = new ToolRepetitionDetector(this.consecutiveMistakeLimit)
  248. onCreated?.(this)
  249. if (startTask) {
  250. if (task || images) {
  251. this.startTask(task, images)
  252. } else if (historyItem) {
  253. this.resumeTaskFromHistory()
  254. } else {
  255. throw new Error("Either historyItem or task/images must be provided")
  256. }
  257. }
  258. }
  259. static create(options: TaskOptions): [Task, Promise<void>] {
  260. const instance = new Task({ ...options, startTask: false })
  261. const { images, task, historyItem } = options
  262. let promise
  263. if (images || task) {
  264. promise = instance.startTask(task, images)
  265. } else if (historyItem) {
  266. promise = instance.resumeTaskFromHistory()
  267. } else {
  268. throw new Error("Either historyItem or task/images must be provided")
  269. }
  270. return [instance, promise]
  271. }
  272. // API Messages
  273. private async getSavedApiConversationHistory(): Promise<ApiMessage[]> {
  274. return readApiMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
  275. }
  276. private async addToApiConversationHistory(message: Anthropic.MessageParam) {
  277. const messageWithTs = { ...message, ts: Date.now() }
  278. this.apiConversationHistory.push(messageWithTs)
  279. await this.saveApiConversationHistory()
  280. }
  281. async overwriteApiConversationHistory(newHistory: ApiMessage[]) {
  282. this.apiConversationHistory = newHistory
  283. await this.saveApiConversationHistory()
  284. }
  285. private async saveApiConversationHistory() {
  286. try {
  287. await saveApiMessages({
  288. messages: this.apiConversationHistory,
  289. taskId: this.taskId,
  290. globalStoragePath: this.globalStoragePath,
  291. })
  292. } catch (error) {
  293. // In the off chance this fails, we don't want to stop the task.
  294. console.error("Failed to save API conversation history:", error)
  295. }
  296. }
  297. // Cline Messages
  298. private async getSavedClineMessages(): Promise<ClineMessage[]> {
  299. return readTaskMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
  300. }
  301. private async addToClineMessages(message: ClineMessage) {
  302. this.clineMessages.push(message)
  303. const provider = this.providerRef.deref()
  304. await provider?.postStateToWebview()
  305. this.emit("message", { action: "created", message })
  306. await this.saveClineMessages()
  307. const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
  308. if (shouldCaptureMessage) {
  309. CloudService.instance.captureEvent({
  310. event: TelemetryEventName.TASK_MESSAGE,
  311. properties: { taskId: this.taskId, message },
  312. })
  313. }
  314. }
  315. public async overwriteClineMessages(newMessages: ClineMessage[]) {
  316. this.clineMessages = newMessages
  317. await this.saveClineMessages()
  318. }
  319. private async updateClineMessage(message: ClineMessage) {
  320. const provider = this.providerRef.deref()
  321. await provider?.postMessageToWebview({ type: "messageUpdated", clineMessage: message })
  322. this.emit("message", { action: "updated", message })
  323. const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
  324. if (shouldCaptureMessage) {
  325. CloudService.instance.captureEvent({
  326. event: TelemetryEventName.TASK_MESSAGE,
  327. properties: { taskId: this.taskId, message },
  328. })
  329. }
  330. }
  331. private async saveClineMessages() {
  332. try {
  333. await saveTaskMessages({
  334. messages: this.clineMessages,
  335. taskId: this.taskId,
  336. globalStoragePath: this.globalStoragePath,
  337. })
  338. const { historyItem, tokenUsage } = await taskMetadata({
  339. messages: this.clineMessages,
  340. taskId: this.taskId,
  341. taskNumber: this.taskNumber,
  342. globalStoragePath: this.globalStoragePath,
  343. workspace: this.cwd,
  344. })
  345. this.emit("taskTokenUsageUpdated", this.taskId, tokenUsage)
  346. await this.providerRef.deref()?.updateTaskHistory(historyItem)
  347. } catch (error) {
  348. console.error("Failed to save Roo messages:", error)
  349. }
  350. }
  351. // Note that `partial` has three valid states true (partial message),
  352. // false (completion of partial message), undefined (individual complete
  353. // message).
  354. async ask(
  355. type: ClineAsk,
  356. text?: string,
  357. partial?: boolean,
  358. progressStatus?: ToolProgressStatus,
  359. isProtected?: boolean,
  360. ): Promise<{ response: ClineAskResponse; text?: string; images?: string[] }> {
  361. // If this Cline instance was aborted by the provider, then the only
  362. // thing keeping us alive is a promise still running in the background,
  363. // in which case we don't want to send its result to the webview as it
  364. // is attached to a new instance of Cline now. So we can safely ignore
  365. // the result of any active promises, and this class will be
  366. // deallocated. (Although we set Cline = undefined in provider, that
  367. // simply removes the reference to this instance, but the instance is
  368. // still alive until this promise resolves or rejects.)
  369. if (this.abort) {
  370. throw new Error(`[RooCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
  371. }
  372. let askTs: number
  373. if (partial !== undefined) {
  374. const lastMessage = this.clineMessages.at(-1)
  375. const isUpdatingPreviousPartial =
  376. lastMessage && lastMessage.partial && lastMessage.type === "ask" && lastMessage.ask === type
  377. if (partial) {
  378. if (isUpdatingPreviousPartial) {
  379. // Existing partial message, so update it.
  380. lastMessage.text = text
  381. lastMessage.partial = partial
  382. lastMessage.progressStatus = progressStatus
  383. lastMessage.isProtected = isProtected
  384. // TODO: Be more efficient about saving and posting only new
  385. // data or one whole message at a time so ignore partial for
  386. // saves, and only post parts of partial message instead of
  387. // whole array in new listener.
  388. this.updateClineMessage(lastMessage)
  389. throw new Error("Current ask promise was ignored (#1)")
  390. } else {
  391. // This is a new partial message, so add it with partial
  392. // state.
  393. askTs = Date.now()
  394. this.lastMessageTs = askTs
  395. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, partial, isProtected })
  396. throw new Error("Current ask promise was ignored (#2)")
  397. }
  398. } else {
  399. if (isUpdatingPreviousPartial) {
  400. // This is the complete version of a previously partial
  401. // message, so replace the partial with the complete version.
  402. this.askResponse = undefined
  403. this.askResponseText = undefined
  404. this.askResponseImages = undefined
  405. // Bug for the history books:
  406. // In the webview we use the ts as the chatrow key for the
  407. // virtuoso list. Since we would update this ts right at the
  408. // end of streaming, it would cause the view to flicker. The
  409. // key prop has to be stable otherwise react has trouble
  410. // reconciling items between renders, causing unmounting and
  411. // remounting of components (flickering).
  412. // The lesson here is if you see flickering when rendering
  413. // lists, it's likely because the key prop is not stable.
  414. // So in this case we must make sure that the message ts is
  415. // never altered after first setting it.
  416. askTs = lastMessage.ts
  417. this.lastMessageTs = askTs
  418. lastMessage.text = text
  419. lastMessage.partial = false
  420. lastMessage.progressStatus = progressStatus
  421. lastMessage.isProtected = isProtected
  422. await this.saveClineMessages()
  423. this.updateClineMessage(lastMessage)
  424. } else {
  425. // This is a new and complete message, so add it like normal.
  426. this.askResponse = undefined
  427. this.askResponseText = undefined
  428. this.askResponseImages = undefined
  429. askTs = Date.now()
  430. this.lastMessageTs = askTs
  431. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
  432. }
  433. }
  434. } else {
  435. // This is a new non-partial message, so add it like normal.
  436. this.askResponse = undefined
  437. this.askResponseText = undefined
  438. this.askResponseImages = undefined
  439. askTs = Date.now()
  440. this.lastMessageTs = askTs
  441. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
  442. }
  443. await pWaitFor(() => this.askResponse !== undefined || this.lastMessageTs !== askTs, { interval: 100 })
  444. if (this.lastMessageTs !== askTs) {
  445. // Could happen if we send multiple asks in a row i.e. with
  446. // command_output. It's important that when we know an ask could
  447. // fail, it is handled gracefully.
  448. throw new Error("Current ask promise was ignored")
  449. }
  450. const result = { response: this.askResponse!, text: this.askResponseText, images: this.askResponseImages }
  451. this.askResponse = undefined
  452. this.askResponseText = undefined
  453. this.askResponseImages = undefined
  454. this.emit("taskAskResponded")
  455. return result
  456. }
  457. async handleWebviewAskResponse(askResponse: ClineAskResponse, text?: string, images?: string[]) {
  458. this.askResponse = askResponse
  459. this.askResponseText = text
  460. this.askResponseImages = images
  461. }
  462. async handleTerminalOperation(terminalOperation: "continue" | "abort") {
  463. if (terminalOperation === "continue") {
  464. this.terminalProcess?.continue()
  465. } else if (terminalOperation === "abort") {
  466. this.terminalProcess?.abort()
  467. }
  468. }
  469. public async condenseContext(): Promise<void> {
  470. const systemPrompt = await this.getSystemPrompt()
  471. // Get condensing configuration
  472. // Using type assertion to handle the case where Phase 1 hasn't been implemented yet
  473. const state = await this.providerRef.deref()?.getState()
  474. const customCondensingPrompt = state ? (state as any).customCondensingPrompt : undefined
  475. const condensingApiConfigId = state ? (state as any).condensingApiConfigId : undefined
  476. const listApiConfigMeta = state ? (state as any).listApiConfigMeta : undefined
  477. // Determine API handler to use
  478. let condensingApiHandler: ApiHandler | undefined
  479. if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
  480. // Using type assertion for the id property to avoid implicit any
  481. const matchingConfig = listApiConfigMeta.find((config: any) => config.id === condensingApiConfigId)
  482. if (matchingConfig) {
  483. const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
  484. id: condensingApiConfigId,
  485. })
  486. // Ensure profile and apiProvider exist before trying to build handler
  487. if (profile && profile.apiProvider) {
  488. condensingApiHandler = buildApiHandler(profile)
  489. }
  490. }
  491. }
  492. const { contextTokens: prevContextTokens } = this.getTokenUsage()
  493. const {
  494. messages,
  495. summary,
  496. cost,
  497. newContextTokens = 0,
  498. error,
  499. } = await summarizeConversation(
  500. this.apiConversationHistory,
  501. this.api, // Main API handler (fallback)
  502. systemPrompt, // Default summarization prompt (fallback)
  503. this.taskId,
  504. prevContextTokens,
  505. false, // manual trigger
  506. customCondensingPrompt, // User's custom prompt
  507. condensingApiHandler, // Specific handler for condensing
  508. )
  509. if (error) {
  510. this.say(
  511. "condense_context_error",
  512. error,
  513. undefined /* images */,
  514. false /* partial */,
  515. undefined /* checkpoint */,
  516. undefined /* progressStatus */,
  517. { isNonInteractive: true } /* options */,
  518. )
  519. return
  520. }
  521. await this.overwriteApiConversationHistory(messages)
  522. const contextCondense: ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
  523. await this.say(
  524. "condense_context",
  525. undefined /* text */,
  526. undefined /* images */,
  527. false /* partial */,
  528. undefined /* checkpoint */,
  529. undefined /* progressStatus */,
  530. { isNonInteractive: true } /* options */,
  531. contextCondense,
  532. )
  533. }
  534. async say(
  535. type: ClineSay,
  536. text?: string,
  537. images?: string[],
  538. partial?: boolean,
  539. checkpoint?: Record<string, unknown>,
  540. progressStatus?: ToolProgressStatus,
  541. options: {
  542. isNonInteractive?: boolean
  543. } = {},
  544. contextCondense?: ContextCondense,
  545. ): Promise<undefined> {
  546. if (this.abort) {
  547. throw new Error(`[RooCode#say] task ${this.taskId}.${this.instanceId} aborted`)
  548. }
  549. if (partial !== undefined) {
  550. const lastMessage = this.clineMessages.at(-1)
  551. const isUpdatingPreviousPartial =
  552. lastMessage && lastMessage.partial && lastMessage.type === "say" && lastMessage.say === type
  553. if (partial) {
  554. if (isUpdatingPreviousPartial) {
  555. // Existing partial message, so update it.
  556. lastMessage.text = text
  557. lastMessage.images = images
  558. lastMessage.partial = partial
  559. lastMessage.progressStatus = progressStatus
  560. this.updateClineMessage(lastMessage)
  561. } else {
  562. // This is a new partial message, so add it with partial state.
  563. const sayTs = Date.now()
  564. if (!options.isNonInteractive) {
  565. this.lastMessageTs = sayTs
  566. }
  567. await this.addToClineMessages({
  568. ts: sayTs,
  569. type: "say",
  570. say: type,
  571. text,
  572. images,
  573. partial,
  574. contextCondense,
  575. })
  576. }
  577. } else {
  578. // New now have a complete version of a previously partial message.
  579. // This is the complete version of a previously partial
  580. // message, so replace the partial with the complete version.
  581. if (isUpdatingPreviousPartial) {
  582. if (!options.isNonInteractive) {
  583. this.lastMessageTs = lastMessage.ts
  584. }
  585. lastMessage.text = text
  586. lastMessage.images = images
  587. lastMessage.partial = false
  588. lastMessage.progressStatus = progressStatus
  589. // Instead of streaming partialMessage events, we do a save
  590. // and post like normal to persist to disk.
  591. await this.saveClineMessages()
  592. // More performant than an entire `postStateToWebview`.
  593. this.updateClineMessage(lastMessage)
  594. } else {
  595. // This is a new and complete message, so add it like normal.
  596. const sayTs = Date.now()
  597. if (!options.isNonInteractive) {
  598. this.lastMessageTs = sayTs
  599. }
  600. await this.addToClineMessages({ ts: sayTs, type: "say", say: type, text, images, contextCondense })
  601. }
  602. }
  603. } else {
  604. // This is a new non-partial message, so add it like normal.
  605. const sayTs = Date.now()
  606. // A "non-interactive" message is a message is one that the user
  607. // does not need to respond to. We don't want these message types
  608. // to trigger an update to `lastMessageTs` since they can be created
  609. // asynchronously and could interrupt a pending ask.
  610. if (!options.isNonInteractive) {
  611. this.lastMessageTs = sayTs
  612. }
  613. await this.addToClineMessages({
  614. ts: sayTs,
  615. type: "say",
  616. say: type,
  617. text,
  618. images,
  619. checkpoint,
  620. contextCondense,
  621. })
  622. }
  623. }
  624. async sayAndCreateMissingParamError(toolName: ToolName, paramName: string, relPath?: string) {
  625. await this.say(
  626. "error",
  627. `Roo tried to use ${toolName}${
  628. relPath ? ` for '${relPath.toPosix()}'` : ""
  629. } without value for required parameter '${paramName}'. Retrying...`,
  630. )
  631. return formatResponse.toolError(formatResponse.missingToolParameterError(paramName))
  632. }
  633. // Start / Abort / Resume
  634. private async startTask(task?: string, images?: string[]): Promise<void> {
  635. // `conversationHistory` (for API) and `clineMessages` (for webview)
  636. // need to be in sync.
  637. // If the extension process were killed, then on restart the
  638. // `clineMessages` might not be empty, so we need to set it to [] when
  639. // we create a new Cline client (otherwise webview would show stale
  640. // messages from previous session).
  641. this.clineMessages = []
  642. this.apiConversationHistory = []
  643. await this.providerRef.deref()?.postStateToWebview()
  644. await this.say("text", task, images)
  645. this.isInitialized = true
  646. let imageBlocks: Anthropic.ImageBlockParam[] = formatResponse.imageBlocks(images)
  647. console.log(`[subtasks] task ${this.taskId}.${this.instanceId} starting`)
  648. await this.initiateTaskLoop([
  649. {
  650. type: "text",
  651. text: `<task>\n${task}\n</task>`,
  652. },
  653. ...imageBlocks,
  654. ])
  655. }
  656. public async resumePausedTask(lastMessage: string) {
  657. // Release this Cline instance from paused state.
  658. this.isPaused = false
  659. this.emit("taskUnpaused")
  660. // Fake an answer from the subtask that it has completed running and
  661. // this is the result of what it has done add the message to the chat
  662. // history and to the webview ui.
  663. try {
  664. await this.say("subtask_result", lastMessage)
  665. await this.addToApiConversationHistory({
  666. role: "user",
  667. content: [{ type: "text", text: `[new_task completed] Result: ${lastMessage}` }],
  668. })
  669. } catch (error) {
  670. this.providerRef
  671. .deref()
  672. ?.log(`Error failed to add reply from subtask into conversation of parent task, error: ${error}`)
  673. throw error
  674. }
  675. }
  676. private async resumeTaskFromHistory() {
  677. const modifiedClineMessages = await this.getSavedClineMessages()
  678. // Remove any resume messages that may have been added before
  679. const lastRelevantMessageIndex = findLastIndex(
  680. modifiedClineMessages,
  681. (m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task"),
  682. )
  683. if (lastRelevantMessageIndex !== -1) {
  684. modifiedClineMessages.splice(lastRelevantMessageIndex + 1)
  685. }
  686. // 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
  687. const lastApiReqStartedIndex = findLastIndex(
  688. modifiedClineMessages,
  689. (m) => m.type === "say" && m.say === "api_req_started",
  690. )
  691. if (lastApiReqStartedIndex !== -1) {
  692. const lastApiReqStarted = modifiedClineMessages[lastApiReqStartedIndex]
  693. const { cost, cancelReason }: ClineApiReqInfo = JSON.parse(lastApiReqStarted.text || "{}")
  694. if (cost === undefined && cancelReason === undefined) {
  695. modifiedClineMessages.splice(lastApiReqStartedIndex, 1)
  696. }
  697. }
  698. await this.overwriteClineMessages(modifiedClineMessages)
  699. this.clineMessages = await this.getSavedClineMessages()
  700. // Now present the cline messages to the user and ask if they want to
  701. // resume (NOTE: we ran into a bug before where the
  702. // apiConversationHistory wouldn't be initialized when opening a old
  703. // task, and it was because we were waiting for resume).
  704. // This is important in case the user deletes messages without resuming
  705. // the task first.
  706. this.apiConversationHistory = await this.getSavedApiConversationHistory()
  707. const lastClineMessage = this.clineMessages
  708. .slice()
  709. .reverse()
  710. .find((m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task")) // could be multiple resume tasks
  711. let askType: ClineAsk
  712. if (lastClineMessage?.ask === "completion_result") {
  713. askType = "resume_completed_task"
  714. } else {
  715. askType = "resume_task"
  716. }
  717. this.isInitialized = true
  718. const { response, text, images } = await this.ask(askType) // calls poststatetowebview
  719. let responseText: string | undefined
  720. let responseImages: string[] | undefined
  721. if (response === "messageResponse") {
  722. await this.say("user_feedback", text, images)
  723. responseText = text
  724. responseImages = images
  725. }
  726. // Make sure that the api conversation history can be resumed by the API,
  727. // even if it goes out of sync with cline messages.
  728. let existingApiConversationHistory: ApiMessage[] = await this.getSavedApiConversationHistory()
  729. // 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
  730. const conversationWithoutToolBlocks = existingApiConversationHistory.map((message) => {
  731. if (Array.isArray(message.content)) {
  732. const newContent = message.content.map((block) => {
  733. if (block.type === "tool_use") {
  734. // It's important we convert to the new tool schema
  735. // format so the model doesn't get confused about how to
  736. // invoke tools.
  737. const inputAsXml = Object.entries(block.input as Record<string, string>)
  738. .map(([key, value]) => `<${key}>\n${value}\n</${key}>`)
  739. .join("\n")
  740. return {
  741. type: "text",
  742. text: `<${block.name}>\n${inputAsXml}\n</${block.name}>`,
  743. } as Anthropic.Messages.TextBlockParam
  744. } else if (block.type === "tool_result") {
  745. // Convert block.content to text block array, removing images
  746. const contentAsTextBlocks = Array.isArray(block.content)
  747. ? block.content.filter((item) => item.type === "text")
  748. : [{ type: "text", text: block.content }]
  749. const textContent = contentAsTextBlocks.map((item) => item.text).join("\n\n")
  750. const toolName = findToolName(block.tool_use_id, existingApiConversationHistory)
  751. return {
  752. type: "text",
  753. text: `[${toolName} Result]\n\n${textContent}`,
  754. } as Anthropic.Messages.TextBlockParam
  755. }
  756. return block
  757. })
  758. return { ...message, content: newContent }
  759. }
  760. return message
  761. })
  762. existingApiConversationHistory = conversationWithoutToolBlocks
  763. // FIXME: remove tool use blocks altogether
  764. // 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
  765. // if there's no tool use and only a text block, then we can just add a user message
  766. // (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)
  767. // 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'
  768. let modifiedOldUserContent: Anthropic.Messages.ContentBlockParam[] // either the last message if its user message, or the user message before the last (assistant) message
  769. let modifiedApiConversationHistory: ApiMessage[] // need to remove the last user message to replace with new modified user message
  770. if (existingApiConversationHistory.length > 0) {
  771. const lastMessage = existingApiConversationHistory[existingApiConversationHistory.length - 1]
  772. if (lastMessage.role === "assistant") {
  773. const content = Array.isArray(lastMessage.content)
  774. ? lastMessage.content
  775. : [{ type: "text", text: lastMessage.content }]
  776. const hasToolUse = content.some((block) => block.type === "tool_use")
  777. if (hasToolUse) {
  778. const toolUseBlocks = content.filter(
  779. (block) => block.type === "tool_use",
  780. ) as Anthropic.Messages.ToolUseBlock[]
  781. const toolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks.map((block) => ({
  782. type: "tool_result",
  783. tool_use_id: block.id,
  784. content: "Task was interrupted before this tool call could be completed.",
  785. }))
  786. modifiedApiConversationHistory = [...existingApiConversationHistory] // no changes
  787. modifiedOldUserContent = [...toolResponses]
  788. } else {
  789. modifiedApiConversationHistory = [...existingApiConversationHistory]
  790. modifiedOldUserContent = []
  791. }
  792. } else if (lastMessage.role === "user") {
  793. const previousAssistantMessage: ApiMessage | undefined =
  794. existingApiConversationHistory[existingApiConversationHistory.length - 2]
  795. const existingUserContent: Anthropic.Messages.ContentBlockParam[] = Array.isArray(lastMessage.content)
  796. ? lastMessage.content
  797. : [{ type: "text", text: lastMessage.content }]
  798. if (previousAssistantMessage && previousAssistantMessage.role === "assistant") {
  799. const assistantContent = Array.isArray(previousAssistantMessage.content)
  800. ? previousAssistantMessage.content
  801. : [{ type: "text", text: previousAssistantMessage.content }]
  802. const toolUseBlocks = assistantContent.filter(
  803. (block) => block.type === "tool_use",
  804. ) as Anthropic.Messages.ToolUseBlock[]
  805. if (toolUseBlocks.length > 0) {
  806. const existingToolResults = existingUserContent.filter(
  807. (block) => block.type === "tool_result",
  808. ) as Anthropic.ToolResultBlockParam[]
  809. const missingToolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks
  810. .filter(
  811. (toolUse) => !existingToolResults.some((result) => result.tool_use_id === toolUse.id),
  812. )
  813. .map((toolUse) => ({
  814. type: "tool_result",
  815. tool_use_id: toolUse.id,
  816. content: "Task was interrupted before this tool call could be completed.",
  817. }))
  818. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1) // removes the last user message
  819. modifiedOldUserContent = [...existingUserContent, ...missingToolResponses]
  820. } else {
  821. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  822. modifiedOldUserContent = [...existingUserContent]
  823. }
  824. } else {
  825. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  826. modifiedOldUserContent = [...existingUserContent]
  827. }
  828. } else {
  829. throw new Error("Unexpected: Last message is not a user or assistant message")
  830. }
  831. } else {
  832. throw new Error("Unexpected: No existing API conversation history")
  833. }
  834. let newUserContent: Anthropic.Messages.ContentBlockParam[] = [...modifiedOldUserContent]
  835. const agoText = ((): string => {
  836. const timestamp = lastClineMessage?.ts ?? Date.now()
  837. const now = Date.now()
  838. const diff = now - timestamp
  839. const minutes = Math.floor(diff / 60000)
  840. const hours = Math.floor(minutes / 60)
  841. const days = Math.floor(hours / 24)
  842. if (days > 0) {
  843. return `${days} day${days > 1 ? "s" : ""} ago`
  844. }
  845. if (hours > 0) {
  846. return `${hours} hour${hours > 1 ? "s" : ""} ago`
  847. }
  848. if (minutes > 0) {
  849. return `${minutes} minute${minutes > 1 ? "s" : ""} ago`
  850. }
  851. return "just now"
  852. })()
  853. const lastTaskResumptionIndex = newUserContent.findIndex(
  854. (x) => x.type === "text" && x.text.startsWith("[TASK RESUMPTION]"),
  855. )
  856. if (lastTaskResumptionIndex !== -1) {
  857. newUserContent.splice(lastTaskResumptionIndex, newUserContent.length - lastTaskResumptionIndex)
  858. }
  859. const wasRecent = lastClineMessage?.ts && Date.now() - lastClineMessage.ts < 30_000
  860. newUserContent.push({
  861. type: "text",
  862. text:
  863. `[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. 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.${
  864. wasRecent
  865. ? "\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."
  866. : ""
  867. }` +
  868. (responseText
  869. ? `\n\nNew instructions for task continuation:\n<user_message>\n${responseText}\n</user_message>`
  870. : ""),
  871. })
  872. if (responseImages && responseImages.length > 0) {
  873. newUserContent.push(...formatResponse.imageBlocks(responseImages))
  874. }
  875. await this.overwriteApiConversationHistory(modifiedApiConversationHistory)
  876. console.log(`[subtasks] task ${this.taskId}.${this.instanceId} resuming from history item`)
  877. await this.initiateTaskLoop(newUserContent)
  878. }
  879. public dispose(): void {
  880. // Stop waiting for child task completion.
  881. if (this.pauseInterval) {
  882. clearInterval(this.pauseInterval)
  883. this.pauseInterval = undefined
  884. }
  885. // Release any terminals associated with this task.
  886. try {
  887. // Release any terminals associated with this task.
  888. TerminalRegistry.releaseTerminalsForTask(this.taskId)
  889. } catch (error) {
  890. console.error("Error releasing terminals:", error)
  891. }
  892. try {
  893. this.urlContentFetcher.closeBrowser()
  894. } catch (error) {
  895. console.error("Error closing URL content fetcher browser:", error)
  896. }
  897. try {
  898. this.browserSession.closeBrowser()
  899. } catch (error) {
  900. console.error("Error closing browser session:", error)
  901. }
  902. try {
  903. if (this.rooIgnoreController) {
  904. this.rooIgnoreController.dispose()
  905. this.rooIgnoreController = undefined
  906. }
  907. } catch (error) {
  908. console.error("Error disposing RooIgnoreController:", error)
  909. // This is the critical one for the leak fix
  910. }
  911. try {
  912. this.fileContextTracker.dispose()
  913. } catch (error) {
  914. console.error("Error disposing file context tracker:", error)
  915. }
  916. try {
  917. // If we're not streaming then `abortStream` won't be called
  918. if (this.isStreaming && this.diffViewProvider.isEditing) {
  919. this.diffViewProvider.revertChanges().catch(console.error)
  920. }
  921. } catch (error) {
  922. console.error("Error reverting diff changes:", error)
  923. }
  924. }
  925. public async abortTask(isAbandoned = false) {
  926. console.log(`[subtasks] aborting task ${this.taskId}.${this.instanceId}`)
  927. // Will stop any autonomously running promises.
  928. if (isAbandoned) {
  929. this.abandoned = true
  930. }
  931. this.abort = true
  932. this.emit("taskAborted")
  933. try {
  934. this.dispose() // Call the centralized dispose method
  935. } catch (error) {
  936. console.error(`Error during task ${this.taskId}.${this.instanceId} disposal:`, error)
  937. // Don't rethrow - we want abort to always succeed
  938. }
  939. // Save the countdown message in the automatic retry or other content.
  940. try {
  941. // Save the countdown message in the automatic retry or other content.
  942. await this.saveClineMessages()
  943. } catch (error) {
  944. console.error(`Error saving messages during abort for task ${this.taskId}.${this.instanceId}:`, error)
  945. }
  946. }
  947. // Used when a sub-task is launched and the parent task is waiting for it to
  948. // finish.
  949. // TBD: The 1s should be added to the settings, also should add a timeout to
  950. // prevent infinite waiting.
  951. public async waitForResume() {
  952. await new Promise<void>((resolve) => {
  953. this.pauseInterval = setInterval(() => {
  954. if (!this.isPaused) {
  955. clearInterval(this.pauseInterval)
  956. this.pauseInterval = undefined
  957. resolve()
  958. }
  959. }, 1000)
  960. })
  961. }
  962. // Task Loop
  963. private async initiateTaskLoop(userContent: Anthropic.Messages.ContentBlockParam[]): Promise<void> {
  964. // Kicks off the checkpoints initialization process in the background.
  965. getCheckpointService(this)
  966. let nextUserContent = userContent
  967. let includeFileDetails = true
  968. this.emit("taskStarted")
  969. while (!this.abort) {
  970. const didEndLoop = await this.recursivelyMakeClineRequests(nextUserContent, includeFileDetails)
  971. includeFileDetails = false // we only need file details the first time
  972. // The way this agentic loop works is that cline will be given a
  973. // task that he then calls tools to complete. Unless there's an
  974. // attempt_completion call, we keep responding back to him with his
  975. // tool's responses until he either attempt_completion or does not
  976. // use anymore tools. If he does not use anymore tools, we ask him
  977. // to consider if he's completed the task and then call
  978. // attempt_completion, otherwise proceed with completing the task.
  979. // There is a MAX_REQUESTS_PER_TASK limit to prevent infinite
  980. // requests, but Cline is prompted to finish the task as efficiently
  981. // as he can.
  982. if (didEndLoop) {
  983. // For now a task never 'completes'. This will only happen if
  984. // the user hits max requests and denies resetting the count.
  985. break
  986. } else {
  987. nextUserContent = [{ type: "text", text: formatResponse.noToolsUsed() }]
  988. this.consecutiveMistakeCount++
  989. }
  990. }
  991. }
  992. public async recursivelyMakeClineRequests(
  993. userContent: Anthropic.Messages.ContentBlockParam[],
  994. includeFileDetails: boolean = false,
  995. ): Promise<boolean> {
  996. if (this.abort) {
  997. throw new Error(`[RooCode#recursivelyMakeRooRequests] task ${this.taskId}.${this.instanceId} aborted`)
  998. }
  999. if (this.consecutiveMistakeCount >= this.consecutiveMistakeLimit) {
  1000. const { response, text, images } = await this.ask(
  1001. "mistake_limit_reached",
  1002. t("common:errors.mistake_limit_guidance"),
  1003. )
  1004. if (response === "messageResponse") {
  1005. userContent.push(
  1006. ...[
  1007. { type: "text" as const, text: formatResponse.tooManyMistakes(text) },
  1008. ...formatResponse.imageBlocks(images),
  1009. ],
  1010. )
  1011. await this.say("user_feedback", text, images)
  1012. // Track consecutive mistake errors in telemetry.
  1013. TelemetryService.instance.captureConsecutiveMistakeError(this.taskId)
  1014. }
  1015. this.consecutiveMistakeCount = 0
  1016. }
  1017. // In this Cline request loop, we need to check if this task instance
  1018. // has been asked to wait for a subtask to finish before continuing.
  1019. const provider = this.providerRef.deref()
  1020. if (this.isPaused && provider) {
  1021. provider.log(`[subtasks] paused ${this.taskId}.${this.instanceId}`)
  1022. await this.waitForResume()
  1023. provider.log(`[subtasks] resumed ${this.taskId}.${this.instanceId}`)
  1024. const currentMode = (await provider.getState())?.mode ?? defaultModeSlug
  1025. if (currentMode !== this.pausedModeSlug) {
  1026. // The mode has changed, we need to switch back to the paused mode.
  1027. await provider.handleModeSwitch(this.pausedModeSlug)
  1028. // Delay to allow mode change to take effect before next tool is executed.
  1029. await delay(500)
  1030. provider.log(
  1031. `[subtasks] task ${this.taskId}.${this.instanceId} has switched back to '${this.pausedModeSlug}' from '${currentMode}'`,
  1032. )
  1033. }
  1034. }
  1035. // Getting verbose details is an expensive operation, it uses ripgrep to
  1036. // top-down build file structure of project which for large projects can
  1037. // take a few seconds. For the best UX we show a placeholder api_req_started
  1038. // message with a loading spinner as this happens.
  1039. await this.say(
  1040. "api_req_started",
  1041. JSON.stringify({
  1042. request:
  1043. userContent.map((block) => formatContentBlockToMarkdown(block)).join("\n\n") + "\n\nLoading...",
  1044. }),
  1045. )
  1046. const { showRooIgnoredFiles = true } = (await this.providerRef.deref()?.getState()) ?? {}
  1047. const parsedUserContent = await processUserContentMentions({
  1048. userContent,
  1049. cwd: this.cwd,
  1050. urlContentFetcher: this.urlContentFetcher,
  1051. fileContextTracker: this.fileContextTracker,
  1052. rooIgnoreController: this.rooIgnoreController,
  1053. showRooIgnoredFiles,
  1054. })
  1055. const environmentDetails = await getEnvironmentDetails(this, includeFileDetails)
  1056. // Add environment details as its own text block, separate from tool
  1057. // results.
  1058. const finalUserContent = [...parsedUserContent, { type: "text" as const, text: environmentDetails }]
  1059. await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
  1060. TelemetryService.instance.captureConversationMessage(this.taskId, "user")
  1061. // Since we sent off a placeholder api_req_started message to update the
  1062. // webview while waiting to actually start the API request (to load
  1063. // potential details for example), we need to update the text of that
  1064. // message.
  1065. const lastApiReqIndex = findLastIndex(this.clineMessages, (m) => m.say === "api_req_started")
  1066. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  1067. request: finalUserContent.map((block) => formatContentBlockToMarkdown(block)).join("\n\n"),
  1068. } satisfies ClineApiReqInfo)
  1069. await this.saveClineMessages()
  1070. await provider?.postStateToWebview()
  1071. try {
  1072. let cacheWriteTokens = 0
  1073. let cacheReadTokens = 0
  1074. let inputTokens = 0
  1075. let outputTokens = 0
  1076. let totalCost: number | undefined
  1077. // We can't use `api_req_finished` anymore since it's a unique case
  1078. // where it could come after a streaming message (i.e. in the middle
  1079. // of being updated or executed).
  1080. // Fortunately `api_req_finished` was always parsed out for the GUI
  1081. // anyways, so it remains solely for legacy purposes to keep track
  1082. // of prices in tasks from history (it's worth removing a few months
  1083. // from now).
  1084. const updateApiReqMsg = (cancelReason?: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  1085. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  1086. ...JSON.parse(this.clineMessages[lastApiReqIndex].text || "{}"),
  1087. tokensIn: inputTokens,
  1088. tokensOut: outputTokens,
  1089. cacheWrites: cacheWriteTokens,
  1090. cacheReads: cacheReadTokens,
  1091. cost:
  1092. totalCost ??
  1093. calculateApiCostAnthropic(
  1094. this.api.getModel().info,
  1095. inputTokens,
  1096. outputTokens,
  1097. cacheWriteTokens,
  1098. cacheReadTokens,
  1099. ),
  1100. cancelReason,
  1101. streamingFailedMessage,
  1102. } satisfies ClineApiReqInfo)
  1103. }
  1104. const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  1105. if (this.diffViewProvider.isEditing) {
  1106. await this.diffViewProvider.revertChanges() // closes diff view
  1107. }
  1108. // if last message is a partial we need to update and save it
  1109. const lastMessage = this.clineMessages.at(-1)
  1110. if (lastMessage && lastMessage.partial) {
  1111. // lastMessage.ts = Date.now() DO NOT update ts since it is used as a key for virtuoso list
  1112. lastMessage.partial = false
  1113. // instead of streaming partialMessage events, we do a save and post like normal to persist to disk
  1114. console.log("updating partial message", lastMessage)
  1115. // await this.saveClineMessages()
  1116. }
  1117. // Let assistant know their response was interrupted for when task is resumed
  1118. await this.addToApiConversationHistory({
  1119. role: "assistant",
  1120. content: [
  1121. {
  1122. type: "text",
  1123. text:
  1124. assistantMessage +
  1125. `\n\n[${
  1126. cancelReason === "streaming_failed"
  1127. ? "Response interrupted by API Error"
  1128. : "Response interrupted by user"
  1129. }]`,
  1130. },
  1131. ],
  1132. })
  1133. // Update `api_req_started` to have cancelled and cost, so that
  1134. // we can display the cost of the partial stream.
  1135. updateApiReqMsg(cancelReason, streamingFailedMessage)
  1136. await this.saveClineMessages()
  1137. // Signals to provider that it can retrieve the saved messages
  1138. // from disk, as abortTask can not be awaited on in nature.
  1139. this.didFinishAbortingStream = true
  1140. }
  1141. // Reset streaming state.
  1142. this.currentStreamingContentIndex = 0
  1143. this.assistantMessageContent = []
  1144. this.didCompleteReadingStream = false
  1145. this.userMessageContent = []
  1146. this.userMessageContentReady = false
  1147. this.didRejectTool = false
  1148. this.didAlreadyUseTool = false
  1149. this.presentAssistantMessageLocked = false
  1150. this.presentAssistantMessageHasPendingUpdates = false
  1151. await this.diffViewProvider.reset()
  1152. // Yields only if the first chunk is successful, otherwise will
  1153. // allow the user to retry the request (most likely due to rate
  1154. // limit error, which gets thrown on the first chunk).
  1155. const stream = this.attemptApiRequest()
  1156. let assistantMessage = ""
  1157. let reasoningMessage = ""
  1158. this.isStreaming = true
  1159. try {
  1160. for await (const chunk of stream) {
  1161. if (!chunk) {
  1162. // Sometimes chunk is undefined, no idea that can cause
  1163. // it, but this workaround seems to fix it.
  1164. continue
  1165. }
  1166. switch (chunk.type) {
  1167. case "reasoning":
  1168. reasoningMessage += chunk.text
  1169. await this.say("reasoning", reasoningMessage, undefined, true)
  1170. break
  1171. case "usage":
  1172. inputTokens += chunk.inputTokens
  1173. outputTokens += chunk.outputTokens
  1174. cacheWriteTokens += chunk.cacheWriteTokens ?? 0
  1175. cacheReadTokens += chunk.cacheReadTokens ?? 0
  1176. totalCost = chunk.totalCost
  1177. break
  1178. case "text": {
  1179. assistantMessage += chunk.text
  1180. // Parse raw assistant message into content blocks.
  1181. const prevLength = this.assistantMessageContent.length
  1182. this.assistantMessageContent = parseAssistantMessage(assistantMessage)
  1183. if (this.assistantMessageContent.length > prevLength) {
  1184. // New content we need to present, reset to
  1185. // false in case previous content set this to true.
  1186. this.userMessageContentReady = false
  1187. }
  1188. // Present content to user.
  1189. presentAssistantMessage(this)
  1190. break
  1191. }
  1192. }
  1193. if (this.abort) {
  1194. console.log(`aborting stream, this.abandoned = ${this.abandoned}`)
  1195. if (!this.abandoned) {
  1196. // Only need to gracefully abort if this instance
  1197. // isn't abandoned (sometimes OpenRouter stream
  1198. // hangs, in which case this would affect future
  1199. // instances of Cline).
  1200. await abortStream("user_cancelled")
  1201. }
  1202. break // Aborts the stream.
  1203. }
  1204. if (this.didRejectTool) {
  1205. // `userContent` has a tool rejection, so interrupt the
  1206. // assistant's response to present the user's feedback.
  1207. assistantMessage += "\n\n[Response interrupted by user feedback]"
  1208. // Instead of setting this preemptively, we allow the
  1209. // present iterator to finish and set
  1210. // userMessageContentReady when its ready.
  1211. // this.userMessageContentReady = true
  1212. break
  1213. }
  1214. // PREV: We need to let the request finish for openrouter to
  1215. // get generation details.
  1216. // UPDATE: It's better UX to interrupt the request at the
  1217. // cost of the API cost not being retrieved.
  1218. if (this.didAlreadyUseTool) {
  1219. assistantMessage +=
  1220. "\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.]"
  1221. break
  1222. }
  1223. }
  1224. } catch (error) {
  1225. // Abandoned happens when extension is no longer waiting for the
  1226. // Cline instance to finish aborting (error is thrown here when
  1227. // any function in the for loop throws due to this.abort).
  1228. if (!this.abandoned) {
  1229. // If the stream failed, there's various states the task
  1230. // could be in (i.e. could have streamed some tools the user
  1231. // may have executed), so we just resort to replicating a
  1232. // cancel task.
  1233. this.abortTask()
  1234. await abortStream(
  1235. "streaming_failed",
  1236. error.message ?? JSON.stringify(serializeError(error), null, 2),
  1237. )
  1238. const history = await provider?.getTaskWithId(this.taskId)
  1239. if (history) {
  1240. await provider?.initClineWithHistoryItem(history.historyItem)
  1241. }
  1242. }
  1243. } finally {
  1244. this.isStreaming = false
  1245. }
  1246. if (
  1247. inputTokens > 0 ||
  1248. outputTokens > 0 ||
  1249. cacheWriteTokens > 0 ||
  1250. cacheReadTokens > 0 ||
  1251. typeof totalCost !== "undefined"
  1252. ) {
  1253. TelemetryService.instance.captureLlmCompletion(this.taskId, {
  1254. inputTokens,
  1255. outputTokens,
  1256. cacheWriteTokens,
  1257. cacheReadTokens,
  1258. cost: totalCost,
  1259. })
  1260. }
  1261. // Need to call here in case the stream was aborted.
  1262. if (this.abort || this.abandoned) {
  1263. throw new Error(`[RooCode#recursivelyMakeRooRequests] task ${this.taskId}.${this.instanceId} aborted`)
  1264. }
  1265. this.didCompleteReadingStream = true
  1266. // Set any blocks to be complete to allow `presentAssistantMessage`
  1267. // to finish and set `userMessageContentReady` to true.
  1268. // (Could be a text block that had no subsequent tool uses, or a
  1269. // text block at the very end, or an invalid tool use, etc. Whatever
  1270. // the case, `presentAssistantMessage` relies on these blocks either
  1271. // to be completed or the user to reject a block in order to proceed
  1272. // and eventually set userMessageContentReady to true.)
  1273. const partialBlocks = this.assistantMessageContent.filter((block) => block.partial)
  1274. partialBlocks.forEach((block) => (block.partial = false))
  1275. // Can't just do this b/c a tool could be in the middle of executing.
  1276. // this.assistantMessageContent.forEach((e) => (e.partial = false))
  1277. if (partialBlocks.length > 0) {
  1278. // If there is content to update then it will complete and
  1279. // update `this.userMessageContentReady` to true, which we
  1280. // `pWaitFor` before making the next request. All this is really
  1281. // doing is presenting the last partial message that we just set
  1282. // to complete.
  1283. presentAssistantMessage(this)
  1284. }
  1285. updateApiReqMsg()
  1286. await this.saveClineMessages()
  1287. await this.providerRef.deref()?.postStateToWebview()
  1288. // Now add to apiConversationHistory.
  1289. // Need to save assistant responses to file before proceeding to
  1290. // tool use since user can exit at any moment and we wouldn't be
  1291. // able to save the assistant's response.
  1292. let didEndLoop = false
  1293. if (assistantMessage.length > 0) {
  1294. await this.addToApiConversationHistory({
  1295. role: "assistant",
  1296. content: [{ type: "text", text: assistantMessage }],
  1297. })
  1298. TelemetryService.instance.captureConversationMessage(this.taskId, "assistant")
  1299. // NOTE: This comment is here for future reference - this was a
  1300. // workaround for `userMessageContent` not getting set to true.
  1301. // It was due to it not recursively calling for partial blocks
  1302. // when `didRejectTool`, so it would get stuck waiting for a
  1303. // partial block to complete before it could continue.
  1304. // In case the content blocks finished it may be the api stream
  1305. // finished after the last parsed content block was executed, so
  1306. // we are able to detect out of bounds and set
  1307. // `userMessageContentReady` to true (note you should not call
  1308. // `presentAssistantMessage` since if the last block i
  1309. // completed it will be presented again).
  1310. // const completeBlocks = this.assistantMessageContent.filter((block) => !block.partial) // If there are any partial blocks after the stream ended we can consider them invalid.
  1311. // if (this.currentStreamingContentIndex >= completeBlocks.length) {
  1312. // this.userMessageContentReady = true
  1313. // }
  1314. await pWaitFor(() => this.userMessageContentReady)
  1315. // If the model did not tool use, then we need to tell it to
  1316. // either use a tool or attempt_completion.
  1317. const didToolUse = this.assistantMessageContent.some((block) => block.type === "tool_use")
  1318. if (!didToolUse) {
  1319. this.userMessageContent.push({ type: "text", text: formatResponse.noToolsUsed() })
  1320. this.consecutiveMistakeCount++
  1321. }
  1322. const recDidEndLoop = await this.recursivelyMakeClineRequests(this.userMessageContent)
  1323. didEndLoop = recDidEndLoop
  1324. } else {
  1325. // If there's no assistant_responses, that means we got no text
  1326. // or tool_use content blocks from API which we should assume is
  1327. // an error.
  1328. await this.say(
  1329. "error",
  1330. "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.",
  1331. )
  1332. await this.addToApiConversationHistory({
  1333. role: "assistant",
  1334. content: [{ type: "text", text: "Failure: I did not provide a response." }],
  1335. })
  1336. }
  1337. return didEndLoop // Will always be false for now.
  1338. } catch (error) {
  1339. // This should never happen since the only thing that can throw an
  1340. // error is the attemptApiRequest, which is wrapped in a try catch
  1341. // that sends an ask where if noButtonClicked, will clear current
  1342. // task and destroy this instance. However to avoid unhandled
  1343. // promise rejection, we will end this loop which will end execution
  1344. // of this instance (see `startTask`).
  1345. return true // Needs to be true so parent loop knows to end task.
  1346. }
  1347. }
  1348. private async getSystemPrompt(): Promise<string> {
  1349. const { mcpEnabled } = (await this.providerRef.deref()?.getState()) ?? {}
  1350. let mcpHub: McpHub | undefined
  1351. if (mcpEnabled ?? true) {
  1352. const provider = this.providerRef.deref()
  1353. if (!provider) {
  1354. throw new Error("Provider reference lost during view transition")
  1355. }
  1356. // Wait for MCP hub initialization through McpServerManager
  1357. mcpHub = await McpServerManager.getInstance(provider.context, provider)
  1358. if (!mcpHub) {
  1359. throw new Error("Failed to get MCP hub from server manager")
  1360. }
  1361. // Wait for MCP servers to be connected before generating system prompt
  1362. await pWaitFor(() => !mcpHub!.isConnecting, { timeout: 10_000 }).catch(() => {
  1363. console.error("MCP servers failed to connect in time")
  1364. })
  1365. }
  1366. const rooIgnoreInstructions = this.rooIgnoreController?.getInstructions()
  1367. const state = await this.providerRef.deref()?.getState()
  1368. const {
  1369. browserViewportSize,
  1370. mode,
  1371. customModes,
  1372. customModePrompts,
  1373. customInstructions,
  1374. experiments,
  1375. enableMcpServerCreation,
  1376. browserToolEnabled,
  1377. language,
  1378. maxConcurrentFileReads,
  1379. maxReadFileLine,
  1380. } = state ?? {}
  1381. return await (async () => {
  1382. const provider = this.providerRef.deref()
  1383. if (!provider) {
  1384. throw new Error("Provider not available")
  1385. }
  1386. return SYSTEM_PROMPT(
  1387. provider.context,
  1388. this.cwd,
  1389. (this.api.getModel().info.supportsComputerUse ?? false) && (browserToolEnabled ?? true),
  1390. mcpHub,
  1391. this.diffStrategy,
  1392. browserViewportSize,
  1393. mode,
  1394. customModePrompts,
  1395. customModes,
  1396. customInstructions,
  1397. this.diffEnabled,
  1398. experiments,
  1399. enableMcpServerCreation,
  1400. language,
  1401. rooIgnoreInstructions,
  1402. maxReadFileLine !== -1,
  1403. {
  1404. maxConcurrentFileReads,
  1405. },
  1406. )
  1407. })()
  1408. }
  1409. public async *attemptApiRequest(retryAttempt: number = 0): ApiStream {
  1410. const state = await this.providerRef.deref()?.getState()
  1411. const {
  1412. apiConfiguration,
  1413. autoApprovalEnabled,
  1414. alwaysApproveResubmit,
  1415. requestDelaySeconds,
  1416. mode,
  1417. autoCondenseContext = true,
  1418. autoCondenseContextPercent = 100,
  1419. } = state ?? {}
  1420. // Get condensing configuration for automatic triggers
  1421. const customCondensingPrompt = state?.customCondensingPrompt
  1422. const condensingApiConfigId = state?.condensingApiConfigId
  1423. const listApiConfigMeta = state?.listApiConfigMeta
  1424. // Determine API handler to use for condensing
  1425. let condensingApiHandler: ApiHandler | undefined
  1426. if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
  1427. // Using type assertion for the id property to avoid implicit any
  1428. const matchingConfig = listApiConfigMeta.find((config: any) => config.id === condensingApiConfigId)
  1429. if (matchingConfig) {
  1430. const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
  1431. id: condensingApiConfigId,
  1432. })
  1433. // Ensure profile and apiProvider exist before trying to build handler
  1434. if (profile && profile.apiProvider) {
  1435. condensingApiHandler = buildApiHandler(profile)
  1436. }
  1437. }
  1438. }
  1439. let rateLimitDelay = 0
  1440. // Use the shared timestamp so that subtasks respect the same rate-limit
  1441. // window as their parent tasks.
  1442. if (Task.lastGlobalApiRequestTime) {
  1443. const now = Date.now()
  1444. const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
  1445. const rateLimit = apiConfiguration?.rateLimitSeconds || 0
  1446. rateLimitDelay = Math.ceil(Math.max(0, rateLimit * 1000 - timeSinceLastRequest) / 1000)
  1447. }
  1448. // Only show rate limiting message if we're not retrying. If retrying, we'll include the delay there.
  1449. if (rateLimitDelay > 0 && retryAttempt === 0) {
  1450. // Show countdown timer
  1451. for (let i = rateLimitDelay; i > 0; i--) {
  1452. const delayMessage = `Rate limiting for ${i} seconds...`
  1453. await this.say("api_req_retry_delayed", delayMessage, undefined, true)
  1454. await delay(1000)
  1455. }
  1456. }
  1457. // Update last request time before making the request so that subsequent
  1458. // requests — even from new subtasks — will honour the provider's rate-limit.
  1459. Task.lastGlobalApiRequestTime = Date.now()
  1460. const systemPrompt = await this.getSystemPrompt()
  1461. const { contextTokens } = this.getTokenUsage()
  1462. if (contextTokens) {
  1463. // Default max tokens value for thinking models when no specific
  1464. // value is set.
  1465. const DEFAULT_THINKING_MODEL_MAX_TOKENS = 16_384
  1466. const modelInfo = this.api.getModel().info
  1467. const maxTokens = modelInfo.supportsReasoningBudget
  1468. ? this.apiConfiguration.modelMaxTokens || DEFAULT_THINKING_MODEL_MAX_TOKENS
  1469. : modelInfo.maxTokens
  1470. const contextWindow = modelInfo.contextWindow
  1471. const truncateResult = await truncateConversationIfNeeded({
  1472. messages: this.apiConversationHistory,
  1473. totalTokens: contextTokens,
  1474. maxTokens,
  1475. contextWindow,
  1476. apiHandler: this.api,
  1477. autoCondenseContext,
  1478. autoCondenseContextPercent,
  1479. systemPrompt,
  1480. taskId: this.taskId,
  1481. customCondensingPrompt,
  1482. condensingApiHandler,
  1483. })
  1484. if (truncateResult.messages !== this.apiConversationHistory) {
  1485. await this.overwriteApiConversationHistory(truncateResult.messages)
  1486. }
  1487. if (truncateResult.error) {
  1488. await this.say("condense_context_error", truncateResult.error)
  1489. } else if (truncateResult.summary) {
  1490. const { summary, cost, prevContextTokens, newContextTokens = 0 } = truncateResult
  1491. const contextCondense: ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
  1492. await this.say(
  1493. "condense_context",
  1494. undefined /* text */,
  1495. undefined /* images */,
  1496. false /* partial */,
  1497. undefined /* checkpoint */,
  1498. undefined /* progressStatus */,
  1499. { isNonInteractive: true } /* options */,
  1500. contextCondense,
  1501. )
  1502. }
  1503. }
  1504. const messagesSinceLastSummary = getMessagesSinceLastSummary(this.apiConversationHistory)
  1505. const cleanConversationHistory = maybeRemoveImageBlocks(messagesSinceLastSummary, this.api).map(
  1506. ({ role, content }) => ({ role, content }),
  1507. )
  1508. // Check if we've reached the maximum number of auto-approved requests
  1509. const maxRequests = state?.allowedMaxRequests || Infinity
  1510. // Increment the counter for each new API request
  1511. this.consecutiveAutoApprovedRequestsCount++
  1512. if (this.consecutiveAutoApprovedRequestsCount > maxRequests) {
  1513. const { response } = await this.ask("auto_approval_max_req_reached", JSON.stringify({ count: maxRequests }))
  1514. // If we get past the promise, it means the user approved and did not start a new task
  1515. if (response === "yesButtonClicked") {
  1516. this.consecutiveAutoApprovedRequestsCount = 0
  1517. }
  1518. }
  1519. const metadata: ApiHandlerCreateMessageMetadata = {
  1520. mode: mode,
  1521. taskId: this.taskId,
  1522. }
  1523. const stream = this.api.createMessage(systemPrompt, cleanConversationHistory, metadata)
  1524. const iterator = stream[Symbol.asyncIterator]()
  1525. try {
  1526. // Awaiting first chunk to see if it will throw an error.
  1527. this.isWaitingForFirstChunk = true
  1528. const firstChunk = await iterator.next()
  1529. yield firstChunk.value
  1530. this.isWaitingForFirstChunk = false
  1531. } catch (error) {
  1532. this.isWaitingForFirstChunk = false
  1533. // 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.
  1534. if (autoApprovalEnabled && alwaysApproveResubmit) {
  1535. let errorMsg
  1536. if (error.error?.metadata?.raw) {
  1537. errorMsg = JSON.stringify(error.error.metadata.raw, null, 2)
  1538. } else if (error.message) {
  1539. errorMsg = error.message
  1540. } else {
  1541. errorMsg = "Unknown error"
  1542. }
  1543. const baseDelay = requestDelaySeconds || 5
  1544. let exponentialDelay = Math.ceil(baseDelay * Math.pow(2, retryAttempt))
  1545. // If the error is a 429, and the error details contain a retry delay, use that delay instead of exponential backoff
  1546. if (error.status === 429) {
  1547. const geminiRetryDetails = error.errorDetails?.find(
  1548. (detail: any) => detail["@type"] === "type.googleapis.com/google.rpc.RetryInfo",
  1549. )
  1550. if (geminiRetryDetails) {
  1551. const match = geminiRetryDetails?.retryDelay?.match(/^(\d+)s$/)
  1552. if (match) {
  1553. exponentialDelay = Number(match[1]) + 1
  1554. }
  1555. }
  1556. }
  1557. // Wait for the greater of the exponential delay or the rate limit delay
  1558. const finalDelay = Math.max(exponentialDelay, rateLimitDelay)
  1559. // Show countdown timer with exponential backoff
  1560. for (let i = finalDelay; i > 0; i--) {
  1561. await this.say(
  1562. "api_req_retry_delayed",
  1563. `${errorMsg}\n\nRetry attempt ${retryAttempt + 1}\nRetrying in ${i} seconds...`,
  1564. undefined,
  1565. true,
  1566. )
  1567. await delay(1000)
  1568. }
  1569. await this.say(
  1570. "api_req_retry_delayed",
  1571. `${errorMsg}\n\nRetry attempt ${retryAttempt + 1}\nRetrying now...`,
  1572. undefined,
  1573. false,
  1574. )
  1575. // Delegate generator output from the recursive call with
  1576. // incremented retry count.
  1577. yield* this.attemptApiRequest(retryAttempt + 1)
  1578. return
  1579. } else {
  1580. const { response } = await this.ask(
  1581. "api_req_failed",
  1582. error.message ?? JSON.stringify(serializeError(error), null, 2),
  1583. )
  1584. if (response !== "yesButtonClicked") {
  1585. // This will never happen since if noButtonClicked, we will
  1586. // clear current task, aborting this instance.
  1587. throw new Error("API request failed")
  1588. }
  1589. await this.say("api_req_retried")
  1590. // Delegate generator output from the recursive call.
  1591. yield* this.attemptApiRequest()
  1592. return
  1593. }
  1594. }
  1595. // No error, so we can continue to yield all remaining chunks.
  1596. // (Needs to be placed outside of try/catch since it we want caller to
  1597. // handle errors not with api_req_failed as that is reserved for first
  1598. // chunk failures only.)
  1599. // This delegates to another generator or iterable object. In this case,
  1600. // it's saying "yield all remaining values from this iterator". This
  1601. // effectively passes along all subsequent chunks from the original
  1602. // stream.
  1603. yield* iterator
  1604. }
  1605. // Checkpoints
  1606. public async checkpointSave(force: boolean = false) {
  1607. return checkpointSave(this, force)
  1608. }
  1609. public async checkpointRestore(options: CheckpointRestoreOptions) {
  1610. return checkpointRestore(this, options)
  1611. }
  1612. public async checkpointDiff(options: CheckpointDiffOptions) {
  1613. return checkpointDiff(this, options)
  1614. }
  1615. // Metrics
  1616. public combineMessages(messages: ClineMessage[]) {
  1617. return combineApiRequests(combineCommandSequences(messages))
  1618. }
  1619. public getTokenUsage(): TokenUsage {
  1620. return getApiMetrics(this.combineMessages(this.clineMessages.slice(1)))
  1621. }
  1622. public recordToolUsage(toolName: ToolName) {
  1623. if (!this.toolUsage[toolName]) {
  1624. this.toolUsage[toolName] = { attempts: 0, failures: 0 }
  1625. }
  1626. this.toolUsage[toolName].attempts++
  1627. }
  1628. public recordToolError(toolName: ToolName, error?: string) {
  1629. if (!this.toolUsage[toolName]) {
  1630. this.toolUsage[toolName] = { attempts: 0, failures: 0 }
  1631. }
  1632. this.toolUsage[toolName].failures++
  1633. if (error) {
  1634. this.emit("taskToolFailed", this.taskId, toolName, error)
  1635. }
  1636. }
  1637. // Getters
  1638. public get cwd() {
  1639. return this.workspacePath
  1640. }
  1641. }