Task.ts 67 KB

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