Task.ts 59 KB

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