Task.ts 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514
  1. import * as path from "path"
  2. import * as vscode from "vscode"
  3. import os from "os"
  4. import crypto from "crypto"
  5. import EventEmitter from "events"
  6. import { Anthropic } from "@anthropic-ai/sdk"
  7. import delay from "delay"
  8. import pWaitFor from "p-wait-for"
  9. import { serializeError } from "serialize-error"
  10. import {
  11. type TaskLike,
  12. type TaskMetadata,
  13. type TaskEvents,
  14. type ProviderSettings,
  15. type TokenUsage,
  16. type ToolUsage,
  17. type ToolName,
  18. type ContextCondense,
  19. type ClineMessage,
  20. type ClineSay,
  21. type ClineAsk,
  22. type ToolProgressStatus,
  23. type HistoryItem,
  24. type CreateTaskOptions,
  25. RooCodeEventName,
  26. TelemetryEventName,
  27. TaskStatus,
  28. TodoItem,
  29. DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
  30. getApiProtocol,
  31. getModelId,
  32. isIdleAsk,
  33. isInteractiveAsk,
  34. isResumableAsk,
  35. QueuedMessage,
  36. getActiveToolUseStyle, // kilocode_change
  37. DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
  38. MAX_CHECKPOINT_TIMEOUT_SECONDS,
  39. MIN_CHECKPOINT_TIMEOUT_SECONDS,
  40. } from "@roo-code/types"
  41. import { TelemetryService } from "@roo-code/telemetry"
  42. import { CloudService, BridgeOrchestrator } from "@roo-code/cloud"
  43. // api
  44. import { ApiHandler, ApiHandlerCreateMessageMetadata, buildApiHandler } from "../../api"
  45. import { ApiStream, GroundingSource } from "../../api/transform/stream"
  46. import { maybeRemoveImageBlocks } from "../../api/transform/image-cleaning"
  47. import { VirtualQuotaFallbackHandler } from "../../api/providers/virtual-quota-fallback" // kilocode_change: Import VirtualQuotaFallbackHandler for model change notifications
  48. // shared
  49. import { findLastIndex } from "../../shared/array"
  50. import { combineApiRequests } from "../../shared/combineApiRequests"
  51. import { combineCommandSequences } from "../../shared/combineCommandSequences"
  52. import { t } from "../../i18n"
  53. import { ClineApiReqCancelReason, ClineApiReqInfo } from "../../shared/ExtensionMessage"
  54. import { getApiMetrics, hasTokenUsageChanged } from "../../shared/getApiMetrics"
  55. import { ClineAskResponse } from "../../shared/WebviewMessage"
  56. import { defaultModeSlug, getModeBySlug, getGroupName } from "../../shared/modes"
  57. import { DiffStrategy } from "../../shared/tools"
  58. import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
  59. import { getModelMaxOutputTokens } from "../../shared/api"
  60. // services
  61. import { UrlContentFetcher } from "../../services/browser/UrlContentFetcher"
  62. import { BrowserSession } from "../../services/browser/BrowserSession"
  63. import { McpHub } from "../../services/mcp/McpHub"
  64. import { McpServerManager } from "../../services/mcp/McpServerManager"
  65. import { RepoPerTaskCheckpointService } from "../../services/checkpoints"
  66. // integrations
  67. import { DiffViewProvider } from "../../integrations/editor/DiffViewProvider"
  68. import { findToolName, formatContentBlockToMarkdown } from "../../integrations/misc/export-markdown"
  69. import { RooTerminalProcess } from "../../integrations/terminal/types"
  70. import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry"
  71. // utils
  72. import { calculateApiCostAnthropic, calculateApiCostOpenAI } from "../../shared/cost"
  73. import { getWorkspacePath } from "../../utils/path"
  74. // prompts
  75. import { formatResponse } from "../prompts/responses"
  76. import { SYSTEM_PROMPT } from "../prompts/system"
  77. import { getAllowedJSONToolsForMode } from "../prompts/tools/native-tools/getAllowedJSONToolsForMode" // kilocode_change
  78. // core modules
  79. import { ToolRepetitionDetector } from "../tools/ToolRepetitionDetector"
  80. import { restoreTodoListForTask } from "../tools/updateTodoListTool"
  81. import { FileContextTracker } from "../context-tracking/FileContextTracker"
  82. import { RooIgnoreController } from "../ignore/RooIgnoreController"
  83. import { RooProtectedController } from "../protect/RooProtectedController"
  84. import { type AssistantMessageContent, presentAssistantMessage } from "../assistant-message"
  85. import { AssistantMessageParser } from "../assistant-message/AssistantMessageParser"
  86. import { truncateConversationIfNeeded } from "../sliding-window"
  87. import { ClineProvider } from "../webview/ClineProvider"
  88. import { MultiSearchReplaceDiffStrategy } from "../diff/strategies/multi-search-replace"
  89. import { MultiFileSearchReplaceDiffStrategy } from "../diff/strategies/multi-file-search-replace"
  90. import {
  91. type ApiMessage,
  92. readApiMessages,
  93. saveApiMessages,
  94. readTaskMessages,
  95. saveTaskMessages,
  96. taskMetadata,
  97. } from "../task-persistence"
  98. import { getEnvironmentDetails } from "../environment/getEnvironmentDetails"
  99. import { checkContextWindowExceededError } from "../context/context-management/context-error-handling"
  100. import {
  101. type CheckpointDiffOptions,
  102. type CheckpointRestoreOptions,
  103. getCheckpointService,
  104. checkpointSave,
  105. checkpointRestore,
  106. checkpointDiff,
  107. } from "../checkpoints"
  108. import { processKiloUserContentMentions } from "../mentions/processKiloUserContentMentions" // kilocode_change
  109. import { refreshWorkflowToggles } from "../context/instructions/workflows" // kilocode_change
  110. import { parseMentions } from "../mentions" // kilocode_change
  111. import { parseKiloSlashCommands } from "../slash-commands/kilo" // kilocode_change
  112. import { GlobalFileNames } from "../../shared/globalFileNames" // kilocode_change
  113. import { ensureLocalKilorulesDirExists } from "../context/instructions/kilo-rules" // kilocode_change
  114. import { getMessagesSinceLastSummary, summarizeConversation } from "../condense"
  115. import { Gpt5Metadata, ClineMessageWithMetadata } from "./types"
  116. import { MessageQueueService } from "../message-queue/MessageQueueService"
  117. import { findPartialAskMessage, findPartialSayMessage, MessageInsertionGuard } from "../kilocode/task/message-utils" // kilocode_change
  118. import { AutoApprovalHandler } from "./AutoApprovalHandler"
  119. import { isAnyRecognizedKiloCodeError, isPaymentRequiredError } from "../../shared/kilocode/errorUtils"
  120. import { getAppUrl } from "@roo-code/types"
  121. import { maybeRemoveReasoningDetails_kilocode, ReasoningDetail } from "../../api/transform/kilocode/reasoning-details"
  122. import { mergeApiMessages } from "./kilocode"
  123. const MAX_EXPONENTIAL_BACKOFF_SECONDS = 600 // 10 minutes
  124. const DEFAULT_USAGE_COLLECTION_TIMEOUT_MS = 5000 // 5 seconds
  125. const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Keep 75% of context (remove 25%) on context window errors
  126. const MAX_CONTEXT_WINDOW_RETRIES = 3 // Maximum retries for context window errors
  127. export interface TaskOptions extends CreateTaskOptions {
  128. context: vscode.ExtensionContext // kilocode_change
  129. provider: ClineProvider
  130. apiConfiguration: ProviderSettings
  131. enableDiff?: boolean
  132. enableCheckpoints?: boolean
  133. checkpointTimeout?: number
  134. enableBridge?: boolean
  135. fuzzyMatchThreshold?: number
  136. consecutiveMistakeLimit?: number
  137. task?: string
  138. images?: string[]
  139. historyItem?: HistoryItem
  140. experiments?: Record<string, boolean>
  141. startTask?: boolean
  142. rootTask?: Task
  143. parentTask?: Task
  144. taskNumber?: number
  145. onCreated?: (task: Task) => void
  146. initialTodos?: TodoItem[]
  147. workspacePath?: string
  148. }
  149. type UserContent = Array<Anthropic.ContentBlockParam> // kilocode_change
  150. export class Task extends EventEmitter<TaskEvents> implements TaskLike {
  151. private context: vscode.ExtensionContext // kilocode_change
  152. readonly taskId: string
  153. private taskIsFavorited?: boolean // kilocode_change
  154. readonly rootTaskId?: string
  155. readonly parentTaskId?: string
  156. childTaskId?: string
  157. readonly instanceId: string
  158. readonly metadata: TaskMetadata
  159. todoList?: TodoItem[]
  160. readonly rootTask: Task | undefined
  161. readonly parentTask: Task | undefined
  162. readonly taskNumber: number
  163. readonly workspacePath: string
  164. /**
  165. * The mode associated with this task. Persisted across sessions
  166. * to maintain user context when reopening tasks from history.
  167. *
  168. * ## Lifecycle
  169. *
  170. * ### For new tasks:
  171. * 1. Initially `undefined` during construction
  172. * 2. Asynchronously initialized from provider state via `initializeTaskMode()`
  173. * 3. Falls back to `defaultModeSlug` if provider state is unavailable
  174. *
  175. * ### For history items:
  176. * 1. Immediately set from `historyItem.mode` during construction
  177. * 2. Falls back to `defaultModeSlug` if mode is not stored in history
  178. *
  179. * ## Important
  180. * This property should NOT be accessed directly until `taskModeReady` promise resolves.
  181. * Use `getTaskMode()` for async access or `taskMode` getter for sync access after initialization.
  182. *
  183. * @private
  184. * @see {@link getTaskMode} - For safe async access
  185. * @see {@link taskMode} - For sync access after initialization
  186. * @see {@link waitForModeInitialization} - To ensure initialization is complete
  187. */
  188. private _taskMode: string | undefined
  189. /**
  190. * Promise that resolves when the task mode has been initialized.
  191. * This ensures async mode initialization completes before the task is used.
  192. *
  193. * ## Purpose
  194. * - Prevents race conditions when accessing task mode
  195. * - Ensures provider state is properly loaded before mode-dependent operations
  196. * - Provides a synchronization point for async initialization
  197. *
  198. * ## Resolution timing
  199. * - For history items: Resolves immediately (sync initialization)
  200. * - For new tasks: Resolves after provider state is fetched (async initialization)
  201. *
  202. * @private
  203. * @see {@link waitForModeInitialization} - Public method to await this promise
  204. */
  205. private taskModeReady: Promise<void>
  206. providerRef: WeakRef<ClineProvider>
  207. private readonly globalStoragePath: string
  208. abort: boolean = false
  209. // kilocode_change start: Message insertion guard to prevent race conditions with checkpoint messages
  210. private readonly messageInsertionGuard = new MessageInsertionGuard()
  211. // kilocode_change end
  212. // TaskStatus
  213. idleAsk?: ClineMessage
  214. resumableAsk?: ClineMessage
  215. interactiveAsk?: ClineMessage
  216. didFinishAbortingStream = false
  217. abandoned = false
  218. abortReason?: ClineApiReqCancelReason
  219. isInitialized = false
  220. isPaused: boolean = false
  221. pausedModeSlug: string = defaultModeSlug
  222. private pauseInterval: NodeJS.Timeout | undefined
  223. // API
  224. readonly apiConfiguration: ProviderSettings
  225. api: ApiHandler
  226. private static lastGlobalApiRequestTime?: number
  227. private autoApprovalHandler: AutoApprovalHandler
  228. /**
  229. * Reset the global API request timestamp. This should only be used for testing.
  230. * @internal
  231. */
  232. static resetGlobalApiRequestTime(): void {
  233. Task.lastGlobalApiRequestTime = undefined
  234. }
  235. toolRepetitionDetector: ToolRepetitionDetector
  236. rooIgnoreController?: RooIgnoreController
  237. rooProtectedController?: RooProtectedController
  238. fileContextTracker: FileContextTracker
  239. urlContentFetcher: UrlContentFetcher
  240. terminalProcess?: RooTerminalProcess
  241. // Computer User
  242. browserSession: BrowserSession
  243. // Editing
  244. diffViewProvider: DiffViewProvider
  245. diffStrategy?: DiffStrategy
  246. diffEnabled: boolean = false
  247. fuzzyMatchThreshold: number
  248. didEditFile: boolean = false
  249. // LLM Messages & Chat Messages
  250. apiConversationHistory: ApiMessage[] = []
  251. clineMessages: ClineMessage[] = []
  252. // Ask
  253. private askResponse?: ClineAskResponse
  254. private askResponseText?: string
  255. private askResponseImages?: string[]
  256. public lastMessageTs?: number
  257. // Tool Use
  258. consecutiveMistakeCount: number = 0
  259. consecutiveMistakeLimit: number
  260. consecutiveMistakeCountForApplyDiff: Map<string, number> = new Map()
  261. toolUsage: ToolUsage = {}
  262. // Checkpoints
  263. enableCheckpoints: boolean
  264. checkpointTimeout: number
  265. checkpointService?: RepoPerTaskCheckpointService
  266. checkpointServiceInitializing = false
  267. // Task Bridge
  268. enableBridge: boolean
  269. // Message Queue Service
  270. public readonly messageQueueService: MessageQueueService
  271. private messageQueueStateChangedHandler: (() => void) | undefined
  272. // Streaming
  273. isWaitingForFirstChunk = false
  274. isStreaming = false
  275. currentStreamingContentIndex = 0
  276. currentStreamingDidCheckpoint = false
  277. assistantMessageContent: AssistantMessageContent[] = []
  278. presentAssistantMessageLocked = false
  279. presentAssistantMessageHasPendingUpdates = false
  280. userMessageContent: (
  281. | Anthropic.TextBlockParam
  282. | Anthropic.ImageBlockParam
  283. | Anthropic.ToolResultBlockParam // kilocode_change
  284. )[] = []
  285. userMessageContentReady = false
  286. didRejectTool = false
  287. didAlreadyUseTool = false
  288. didCompleteReadingStream = false
  289. assistantMessageParser: AssistantMessageParser
  290. private lastUsedInstructions?: string
  291. private skipPrevResponseIdOnce: boolean = false
  292. // Token Usage Cache
  293. private tokenUsageSnapshot?: TokenUsage
  294. private tokenUsageSnapshotAt?: number
  295. constructor({
  296. context, // kilocode_change
  297. provider,
  298. apiConfiguration,
  299. enableDiff = false,
  300. enableCheckpoints = true,
  301. checkpointTimeout = DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
  302. enableBridge = false,
  303. fuzzyMatchThreshold = 1.0,
  304. consecutiveMistakeLimit = DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
  305. task,
  306. images,
  307. historyItem,
  308. startTask = true,
  309. rootTask,
  310. parentTask,
  311. taskNumber = -1,
  312. onCreated,
  313. initialTodos,
  314. workspacePath,
  315. }: TaskOptions) {
  316. super()
  317. this.context = context // kilocode_change
  318. if (startTask && !task && !images && !historyItem) {
  319. throw new Error("Either historyItem or task/images must be provided")
  320. }
  321. if (
  322. !checkpointTimeout ||
  323. checkpointTimeout > MAX_CHECKPOINT_TIMEOUT_SECONDS ||
  324. checkpointTimeout < MIN_CHECKPOINT_TIMEOUT_SECONDS
  325. ) {
  326. throw new Error(
  327. "checkpointTimeout must be between " +
  328. MIN_CHECKPOINT_TIMEOUT_SECONDS +
  329. " and " +
  330. MAX_CHECKPOINT_TIMEOUT_SECONDS +
  331. " seconds",
  332. )
  333. }
  334. this.taskId = historyItem ? historyItem.id : crypto.randomUUID()
  335. this.taskIsFavorited = historyItem?.isFavorited // kilocode_change
  336. this.rootTaskId = historyItem ? historyItem.rootTaskId : rootTask?.taskId
  337. this.parentTaskId = historyItem ? historyItem.parentTaskId : parentTask?.taskId
  338. this.childTaskId = undefined
  339. this.metadata = {
  340. task: historyItem ? historyItem.task : task,
  341. images: historyItem ? [] : images,
  342. }
  343. // Normal use-case is usually retry similar history task with new workspace.
  344. this.workspacePath = parentTask
  345. ? parentTask.workspacePath
  346. : (workspacePath ?? getWorkspacePath(path.join(os.homedir(), "Documents"))) // kilocode_change: use Documents instead of Desktop as default
  347. this.instanceId = crypto.randomUUID().slice(0, 8)
  348. this.taskNumber = -1
  349. this.rooIgnoreController = new RooIgnoreController(this.cwd)
  350. this.rooProtectedController = new RooProtectedController(this.cwd)
  351. this.fileContextTracker = new FileContextTracker(provider, this.taskId)
  352. this.rooIgnoreController.initialize().catch((error) => {
  353. console.error("Failed to initialize RooIgnoreController:", error)
  354. })
  355. this.apiConfiguration = apiConfiguration
  356. this.api = buildApiHandler(apiConfiguration)
  357. // kilocode_change start: Listen for model changes in virtual quota fallback
  358. if (this.api instanceof VirtualQuotaFallbackHandler) {
  359. this.api.on("handlerChanged", () => {
  360. this.emit("modelChanged")
  361. })
  362. }
  363. // kilocode_change end
  364. this.autoApprovalHandler = new AutoApprovalHandler()
  365. this.urlContentFetcher = new UrlContentFetcher(provider.context)
  366. this.browserSession = new BrowserSession(provider.context)
  367. this.diffEnabled = enableDiff
  368. this.fuzzyMatchThreshold = fuzzyMatchThreshold
  369. this.consecutiveMistakeLimit = consecutiveMistakeLimit ?? DEFAULT_CONSECUTIVE_MISTAKE_LIMIT
  370. this.providerRef = new WeakRef(provider)
  371. this.globalStoragePath = provider.context.globalStorageUri.fsPath
  372. this.diffViewProvider = new DiffViewProvider(this.cwd, this)
  373. this.enableCheckpoints = enableCheckpoints
  374. this.checkpointTimeout = checkpointTimeout
  375. this.enableBridge = enableBridge
  376. this.parentTask = parentTask
  377. this.taskNumber = taskNumber
  378. // Store the task's mode when it's created.
  379. // For history items, use the stored mode; for new tasks, we'll set it
  380. // after getting state.
  381. if (historyItem) {
  382. this._taskMode = historyItem.mode || defaultModeSlug
  383. this.taskModeReady = Promise.resolve()
  384. TelemetryService.instance.captureTaskRestarted(this.taskId)
  385. } else {
  386. // For new tasks, don't set the mode yet - wait for async initialization.
  387. this._taskMode = undefined
  388. this.taskModeReady = this.initializeTaskMode(provider)
  389. TelemetryService.instance.captureTaskCreated(this.taskId)
  390. }
  391. // Initialize the assistant message parser.
  392. this.assistantMessageParser = new AssistantMessageParser()
  393. this.messageQueueService = new MessageQueueService()
  394. this.messageQueueStateChangedHandler = () => {
  395. this.emit(RooCodeEventName.TaskUserMessage, this.taskId)
  396. this.providerRef.deref()?.postStateToWebview()
  397. this.emit("modelChanged") // kilocode_change: Emit modelChanged for virtual quota fallback UI updates
  398. }
  399. this.messageQueueService.on("stateChanged", this.messageQueueStateChangedHandler)
  400. // Only set up diff strategy if diff is enabled.
  401. if (this.diffEnabled) {
  402. // Default to old strategy, will be updated if experiment is enabled.
  403. this.diffStrategy = new MultiSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
  404. // Check experiment asynchronously and update strategy if needed.
  405. provider.getState().then((state) => {
  406. const isMultiFileApplyDiffEnabled = experiments.isEnabled(
  407. state.experiments ?? {},
  408. EXPERIMENT_IDS.MULTI_FILE_APPLY_DIFF,
  409. )
  410. if (isMultiFileApplyDiffEnabled) {
  411. this.diffStrategy = new MultiFileSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
  412. }
  413. })
  414. }
  415. this.toolRepetitionDetector = new ToolRepetitionDetector(this.consecutiveMistakeLimit)
  416. // Initialize todo list if provided
  417. if (initialTodos && initialTodos.length > 0) {
  418. this.todoList = initialTodos
  419. }
  420. onCreated?.(this)
  421. if (startTask) {
  422. if (task || images) {
  423. this.startTask(task, images)
  424. } else if (historyItem) {
  425. this.resumeTaskFromHistory()
  426. } else {
  427. throw new Error("Either historyItem or task/images must be provided")
  428. }
  429. }
  430. }
  431. // kilocode_change start
  432. private getContext(): vscode.ExtensionContext {
  433. const context = this.context
  434. if (!context) {
  435. throw new Error("Unable to access extension context")
  436. }
  437. return context
  438. }
  439. // kilocode_change end
  440. /**
  441. * Initialize the task mode from the provider state.
  442. * This method handles async initialization with proper error handling.
  443. *
  444. * ## Flow
  445. * 1. Attempts to fetch the current mode from provider state
  446. * 2. Sets `_taskMode` to the fetched mode or `defaultModeSlug` if unavailable
  447. * 3. Handles errors gracefully by falling back to default mode
  448. * 4. Logs any initialization errors for debugging
  449. *
  450. * ## Error handling
  451. * - Network failures when fetching provider state
  452. * - Provider not yet initialized
  453. * - Invalid state structure
  454. *
  455. * All errors result in fallback to `defaultModeSlug` to ensure task can proceed.
  456. *
  457. * @private
  458. * @param provider - The ClineProvider instance to fetch state from
  459. * @returns Promise that resolves when initialization is complete
  460. */
  461. private async initializeTaskMode(provider: ClineProvider): Promise<void> {
  462. try {
  463. const state = await provider.getState()
  464. this._taskMode = state?.mode || defaultModeSlug
  465. } catch (error) {
  466. // If there's an error getting state, use the default mode
  467. this._taskMode = defaultModeSlug
  468. // Use the provider's log method for better error visibility
  469. const errorMessage = `Failed to initialize task mode: ${error instanceof Error ? error.message : String(error)}`
  470. provider.log(errorMessage)
  471. }
  472. }
  473. /**
  474. * Wait for the task mode to be initialized before proceeding.
  475. * This method ensures that any operations depending on the task mode
  476. * will have access to the correct mode value.
  477. *
  478. * ## When to use
  479. * - Before accessing mode-specific configurations
  480. * - When switching between tasks with different modes
  481. * - Before operations that depend on mode-based permissions
  482. *
  483. * ## Example usage
  484. * ```typescript
  485. * // Wait for mode initialization before mode-dependent operations
  486. * await task.waitForModeInitialization();
  487. * const mode = task.taskMode; // Now safe to access synchronously
  488. *
  489. * // Or use with getTaskMode() for a one-liner
  490. * const mode = await task.getTaskMode(); // Internally waits for initialization
  491. * ```
  492. *
  493. * @returns Promise that resolves when the task mode is initialized
  494. * @public
  495. */
  496. public async waitForModeInitialization(): Promise<void> {
  497. return this.taskModeReady
  498. }
  499. /**
  500. * Get the task mode asynchronously, ensuring it's properly initialized.
  501. * This is the recommended way to access the task mode as it guarantees
  502. * the mode is available before returning.
  503. *
  504. * ## Async behavior
  505. * - Internally waits for `taskModeReady` promise to resolve
  506. * - Returns the initialized mode or `defaultModeSlug` as fallback
  507. * - Safe to call multiple times - subsequent calls return immediately if already initialized
  508. *
  509. * ## Example usage
  510. * ```typescript
  511. * // Safe async access
  512. * const mode = await task.getTaskMode();
  513. * console.log(`Task is running in ${mode} mode`);
  514. *
  515. * // Use in conditional logic
  516. * if (await task.getTaskMode() === 'architect') {
  517. * // Perform architect-specific operations
  518. * }
  519. * ```
  520. *
  521. * @returns Promise resolving to the task mode string
  522. * @public
  523. */
  524. public async getTaskMode(): Promise<string> {
  525. await this.taskModeReady
  526. return this._taskMode || defaultModeSlug
  527. }
  528. /**
  529. * Get the task mode synchronously. This should only be used when you're certain
  530. * that the mode has already been initialized (e.g., after waitForModeInitialization).
  531. *
  532. * ## When to use
  533. * - In synchronous contexts where async/await is not available
  534. * - After explicitly waiting for initialization via `waitForModeInitialization()`
  535. * - In event handlers or callbacks where mode is guaranteed to be initialized
  536. *
  537. * ## Example usage
  538. * ```typescript
  539. * // After ensuring initialization
  540. * await task.waitForModeInitialization();
  541. * const mode = task.taskMode; // Safe synchronous access
  542. *
  543. * // In an event handler after task is started
  544. * task.on('taskStarted', () => {
  545. * console.log(`Task started in ${task.taskMode} mode`); // Safe here
  546. * });
  547. * ```
  548. *
  549. * @throws {Error} If the mode hasn't been initialized yet
  550. * @returns The task mode string
  551. * @public
  552. */
  553. public get taskMode(): string {
  554. if (this._taskMode === undefined) {
  555. throw new Error("Task mode accessed before initialization. Use getTaskMode() or wait for taskModeReady.")
  556. }
  557. return this._taskMode
  558. }
  559. static create(options: TaskOptions): [Task, Promise<void>] {
  560. const instance = new Task({ ...options, startTask: false })
  561. const { images, task, historyItem } = options
  562. let promise
  563. if (images || task) {
  564. promise = instance.startTask(task, images)
  565. } else if (historyItem) {
  566. promise = instance.resumeTaskFromHistory()
  567. } else {
  568. throw new Error("Either historyItem or task/images must be provided")
  569. }
  570. return [instance, promise]
  571. }
  572. // API Messages
  573. private async getSavedApiConversationHistory(): Promise<ApiMessage[]> {
  574. return readApiMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
  575. }
  576. private async addToApiConversationHistory(message: Anthropic.MessageParam) {
  577. // kilocode_change start: prevent consecutive same-role messages, this happens when returning from subtask
  578. const lastMessage = this.apiConversationHistory.at(-1)
  579. if (lastMessage && lastMessage.role === message.role) {
  580. this.apiConversationHistory[this.apiConversationHistory.length - 1] = mergeApiMessages(lastMessage, message)
  581. await this.saveApiConversationHistory()
  582. return
  583. }
  584. // kilocode_change end
  585. const messageWithTs = { ...message, ts: Date.now() }
  586. this.apiConversationHistory.push(messageWithTs)
  587. await this.saveApiConversationHistory()
  588. }
  589. async overwriteApiConversationHistory(newHistory: ApiMessage[]) {
  590. this.apiConversationHistory = newHistory
  591. await this.saveApiConversationHistory()
  592. }
  593. private async saveApiConversationHistory() {
  594. try {
  595. await saveApiMessages({
  596. messages: this.apiConversationHistory,
  597. taskId: this.taskId,
  598. globalStoragePath: this.globalStoragePath,
  599. })
  600. } catch (error) {
  601. // In the off chance this fails, we don't want to stop the task.
  602. console.error("Failed to save API conversation history:", error)
  603. }
  604. }
  605. // Cline Messages
  606. private async getSavedClineMessages(): Promise<ClineMessage[]> {
  607. return readTaskMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
  608. }
  609. // kilocode_change start: Guard against concurrent message insertions to prevent
  610. private async addToClineMessages(message: ClineMessage) {
  611. await this.messageInsertionGuard.waitForClearance()
  612. this.messageInsertionGuard.acquire()
  613. try {
  614. this.clineMessages.push(message)
  615. const provider = this.providerRef.deref()
  616. await provider?.postStateToWebview()
  617. this.emit(RooCodeEventName.Message, { action: "created", message })
  618. await this.saveClineMessages()
  619. // kilocode_change start: no cloud service
  620. // const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
  621. // if (shouldCaptureMessage) {
  622. // CloudService.instance.captureEvent({
  623. // event: TelemetryEventName.TASK_MESSAGE,
  624. // properties: { taskId: this.taskId, message },
  625. // })
  626. // }
  627. // kilocode_change end
  628. } finally {
  629. this.messageInsertionGuard.release()
  630. }
  631. }
  632. // kilocode_change end
  633. public async overwriteClineMessages(newMessages: ClineMessage[]) {
  634. this.clineMessages = newMessages
  635. // If deletion or history truncation leaves a condense_context as the last message,
  636. // ensure the next API call suppresses previous_response_id so the condensed context is respected.
  637. try {
  638. const last = this.clineMessages.at(-1)
  639. if (last && last.type === "say" && last.say === "condense_context") {
  640. this.skipPrevResponseIdOnce = true
  641. }
  642. } catch {
  643. // non-fatal
  644. }
  645. restoreTodoListForTask(this)
  646. await this.saveClineMessages()
  647. }
  648. private async updateClineMessage(message: ClineMessage) {
  649. const provider = this.providerRef.deref()
  650. await provider?.postMessageToWebview({ type: "messageUpdated", clineMessage: message })
  651. this.emit(RooCodeEventName.Message, { action: "updated", message })
  652. const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
  653. // kilocode_change start: no cloud service
  654. // if (shouldCaptureMessage) {
  655. // CloudService.instance.captureEvent({
  656. // event: TelemetryEventName.TASK_MESSAGE,
  657. // properties: { taskId: this.taskId, message },
  658. // })
  659. // }
  660. // kilocode_change end
  661. }
  662. private async saveClineMessages() {
  663. try {
  664. await saveTaskMessages({
  665. messages: this.clineMessages,
  666. taskId: this.taskId,
  667. globalStoragePath: this.globalStoragePath,
  668. })
  669. const { historyItem, tokenUsage } = await taskMetadata({
  670. taskId: this.taskId,
  671. rootTaskId: this.rootTaskId,
  672. parentTaskId: this.parentTaskId,
  673. taskNumber: this.taskNumber,
  674. messages: this.clineMessages,
  675. globalStoragePath: this.globalStoragePath,
  676. workspace: this.cwd,
  677. mode: this._taskMode || defaultModeSlug, // Use the task's own mode, not the current provider mode.
  678. })
  679. if (hasTokenUsageChanged(tokenUsage, this.tokenUsageSnapshot)) {
  680. this.emit(RooCodeEventName.TaskTokenUsageUpdated, this.taskId, tokenUsage)
  681. this.tokenUsageSnapshot = undefined
  682. this.tokenUsageSnapshotAt = undefined
  683. }
  684. await this.providerRef.deref()?.updateTaskHistory(historyItem)
  685. } catch (error) {
  686. console.error("Failed to save messages:", error)
  687. }
  688. }
  689. private findMessageByTimestamp(ts: number): ClineMessage | undefined {
  690. for (let i = this.clineMessages.length - 1; i >= 0; i--) {
  691. if (this.clineMessages[i].ts === ts) {
  692. return this.clineMessages[i]
  693. }
  694. }
  695. return undefined
  696. }
  697. async nextClineMessageTimestamp_kilocode() {
  698. let ts = Date.now()
  699. while (ts <= (this.clineMessages?.at(-1)?.ts ?? 0)) {
  700. console.warn("nextClineMessageTimeStamp: timestamp already taken", ts)
  701. await new Promise<void>((resolve) => setTimeout(() => resolve(), 1))
  702. ts = Date.now()
  703. }
  704. return ts
  705. }
  706. // Note that `partial` has three valid states true (partial message),
  707. // false (completion of partial message), undefined (individual complete
  708. // message).
  709. async ask(
  710. type: ClineAsk,
  711. text?: string,
  712. partial?: boolean,
  713. progressStatus?: ToolProgressStatus,
  714. isProtected?: boolean,
  715. ): Promise<{ response: ClineAskResponse; text?: string; images?: string[] }> {
  716. // If this Cline instance was aborted by the provider, then the only
  717. // thing keeping us alive is a promise still running in the background,
  718. // in which case we don't want to send its result to the webview as it
  719. // is attached to a new instance of Cline now. So we can safely ignore
  720. // the result of any active promises, and this class will be
  721. // deallocated. (Although we set Cline = undefined in provider, that
  722. // simply removes the reference to this instance, but the instance is
  723. // still alive until this promise resolves or rejects.)
  724. if (this.abort) {
  725. throw new Error(`[KiloCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
  726. }
  727. let askTs: number
  728. if (partial !== undefined) {
  729. // kilocode_change start: Fix orphaned partial asks by searching backwards
  730. // Search for the most recent partial ask of this type, handling cases where
  731. // non-interactive messages (like checkpoint_saved) are inserted during streaming
  732. const partialResult = findPartialAskMessage(this.clineMessages, type)
  733. const lastMessage = partialResult?.message
  734. const isUpdatingPreviousPartial = lastMessage !== undefined
  735. // kilocode_change end
  736. if (partial) {
  737. if (isUpdatingPreviousPartial) {
  738. // Existing partial message, so update it.
  739. lastMessage.text = text
  740. lastMessage.partial = partial
  741. lastMessage.progressStatus = progressStatus
  742. lastMessage.isProtected = isProtected
  743. // TODO: Be more efficient about saving and posting only new
  744. // data or one whole message at a time so ignore partial for
  745. // saves, and only post parts of partial message instead of
  746. // whole array in new listener.
  747. this.updateClineMessage(lastMessage)
  748. throw new Error("Current ask promise was ignored (#1)")
  749. } else {
  750. // This is a new partial message, so add it with partial
  751. // state.
  752. askTs = await this.nextClineMessageTimestamp_kilocode()
  753. this.lastMessageTs = askTs
  754. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, partial, isProtected })
  755. throw new Error("Current ask promise was ignored (#2)")
  756. }
  757. } else {
  758. if (isUpdatingPreviousPartial) {
  759. // This is the complete version of a previously partial
  760. // message, so replace the partial with the complete version.
  761. this.askResponse = undefined
  762. this.askResponseText = undefined
  763. this.askResponseImages = undefined
  764. // Bug for the history books:
  765. // In the webview we use the ts as the chatrow key for the
  766. // virtuoso list. Since we would update this ts right at the
  767. // end of streaming, it would cause the view to flicker. The
  768. // key prop has to be stable otherwise react has trouble
  769. // reconciling items between renders, causing unmounting and
  770. // remounting of components (flickering).
  771. // The lesson here is if you see flickering when rendering
  772. // lists, it's likely because the key prop is not stable.
  773. // So in this case we must make sure that the message ts is
  774. // never altered after first setting it.
  775. askTs = lastMessage.ts
  776. this.lastMessageTs = askTs
  777. lastMessage.text = text
  778. lastMessage.partial = false
  779. lastMessage.progressStatus = progressStatus
  780. lastMessage.isProtected = isProtected
  781. await this.saveClineMessages()
  782. this.updateClineMessage(lastMessage)
  783. } else {
  784. // This is a new and complete message, so add it like normal.
  785. this.askResponse = undefined
  786. this.askResponseText = undefined
  787. this.askResponseImages = undefined
  788. askTs = await this.nextClineMessageTimestamp_kilocode()
  789. this.lastMessageTs = askTs
  790. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
  791. }
  792. }
  793. } else {
  794. // This is a new non-partial message, so add it like normal.
  795. this.askResponse = undefined
  796. this.askResponseText = undefined
  797. this.askResponseImages = undefined
  798. askTs = await this.nextClineMessageTimestamp_kilocode()
  799. this.lastMessageTs = askTs
  800. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
  801. }
  802. // kilocode_change start: YOLO mode auto-answer for follow-up questions
  803. // Check if this is a follow-up question with suggestions in YOLO mode
  804. if (type === "followup" && text && !partial) {
  805. try {
  806. const state = await this.providerRef.deref()?.getState()
  807. if (state?.yoloMode) {
  808. // Parse the follow-up JSON to extract suggestions
  809. const followUpData = JSON.parse(text)
  810. if (
  811. followUpData.suggest &&
  812. Array.isArray(followUpData.suggest) &&
  813. followUpData.suggest.length > 0
  814. ) {
  815. // Auto-select the first suggestion
  816. const firstSuggestion = followUpData.suggest[0]
  817. const autoAnswer = firstSuggestion.answer || firstSuggestion
  818. // Immediately set the response as if the user clicked the first suggestion
  819. this.handleWebviewAskResponse("messageResponse", autoAnswer, undefined)
  820. // Return immediately with the auto-selected answer
  821. const result = { response: this.askResponse!, text: autoAnswer, images: undefined }
  822. this.askResponse = undefined
  823. this.askResponseText = undefined
  824. this.askResponseImages = undefined
  825. return result
  826. }
  827. }
  828. } catch (error) {
  829. // If parsing fails or YOLO check fails, continue with normal flow
  830. console.warn("Failed to auto-answer follow-up question in YOLO mode:", error)
  831. }
  832. }
  833. // kilocode_change end
  834. // The state is mutable if the message is complete and the task will
  835. // block (via the `pWaitFor`).
  836. const isBlocking = !(this.askResponse !== undefined || this.lastMessageTs !== askTs)
  837. const isMessageQueued = !this.messageQueueService.isEmpty()
  838. const isStatusMutable = !partial && isBlocking && !isMessageQueued
  839. let statusMutationTimeouts: NodeJS.Timeout[] = []
  840. const statusMutationTimeout = 5_000
  841. if (isStatusMutable) {
  842. console.log(`Task#ask will block -> type: ${type}`)
  843. if (isInteractiveAsk(type)) {
  844. statusMutationTimeouts.push(
  845. setTimeout(() => {
  846. const message = this.findMessageByTimestamp(askTs)
  847. if (message) {
  848. this.interactiveAsk = message
  849. this.emit(RooCodeEventName.TaskInteractive, this.taskId)
  850. }
  851. }, statusMutationTimeout),
  852. )
  853. } else if (isResumableAsk(type)) {
  854. statusMutationTimeouts.push(
  855. setTimeout(() => {
  856. const message = this.findMessageByTimestamp(askTs)
  857. if (message) {
  858. this.resumableAsk = message
  859. this.emit(RooCodeEventName.TaskResumable, this.taskId)
  860. }
  861. }, statusMutationTimeout),
  862. )
  863. } else if (isIdleAsk(type)) {
  864. statusMutationTimeouts.push(
  865. setTimeout(() => {
  866. const message = this.findMessageByTimestamp(askTs)
  867. if (message) {
  868. this.idleAsk = message
  869. this.emit(RooCodeEventName.TaskIdle, this.taskId)
  870. }
  871. }, statusMutationTimeout),
  872. )
  873. }
  874. } else if (isMessageQueued) {
  875. console.log("Task#ask will process message queue")
  876. const message = this.messageQueueService.dequeueMessage()
  877. if (message) {
  878. // Check if this is a tool approval ask that needs to be handled.
  879. if (
  880. type === "tool" ||
  881. type === "command" ||
  882. type === "browser_action_launch" ||
  883. type === "use_mcp_server"
  884. ) {
  885. // For tool approvals, we need to approve first, then send
  886. // the message if there's text/images.
  887. this.handleWebviewAskResponse("yesButtonClicked", message.text, message.images)
  888. } else {
  889. // For other ask types (like followup), fulfill the ask
  890. // directly.
  891. this.setMessageResponse(message.text, message.images)
  892. }
  893. }
  894. }
  895. // Wait for askResponse to be set.
  896. await pWaitFor(() => this.askResponse !== undefined || this.lastMessageTs !== askTs, { interval: 100 })
  897. if (this.lastMessageTs !== askTs) {
  898. // Could happen if we send multiple asks in a row i.e. with
  899. // command_output. It's important that when we know an ask could
  900. // fail, it is handled gracefully.
  901. throw new Error("Current ask promise was ignored")
  902. }
  903. const result = { response: this.askResponse!, text: this.askResponseText, images: this.askResponseImages }
  904. this.askResponse = undefined
  905. this.askResponseText = undefined
  906. this.askResponseImages = undefined
  907. // Cancel the timeouts if they are still running.
  908. statusMutationTimeouts.forEach((timeout) => clearTimeout(timeout))
  909. // Switch back to an active state.
  910. if (this.idleAsk || this.resumableAsk || this.interactiveAsk) {
  911. this.idleAsk = undefined
  912. this.resumableAsk = undefined
  913. this.interactiveAsk = undefined
  914. this.emit(RooCodeEventName.TaskActive, this.taskId)
  915. }
  916. this.emit(RooCodeEventName.TaskAskResponded)
  917. return result
  918. }
  919. public setMessageResponse(text: string, images?: string[]) {
  920. this.handleWebviewAskResponse("messageResponse", text, images)
  921. }
  922. handleWebviewAskResponse(askResponse: ClineAskResponse, text?: string, images?: string[]) {
  923. // this.askResponse = askResponse kilocode_change
  924. this.askResponseText = text
  925. this.askResponseImages = images
  926. // kilocode_change start
  927. // the askResponse assignment needs to happen last to avoid the async
  928. // callbacks triggering before we assign the data above
  929. this.askResponse = askResponse // this triggers async callbacks
  930. // kilocode_change end
  931. // Create a checkpoint whenever the user sends a message.
  932. // Use allowEmpty=true to ensure a checkpoint is recorded even if there are no file changes.
  933. // Suppress the checkpoint_saved chat row for this particular checkpoint to keep the timeline clean.
  934. if (askResponse === "messageResponse") {
  935. void this.checkpointSave(false, true)
  936. }
  937. // Mark the last follow-up question as answered
  938. if (askResponse === "messageResponse" || askResponse === "yesButtonClicked") {
  939. // Find the last unanswered follow-up message using findLastIndex
  940. const lastFollowUpIndex = findLastIndex(
  941. this.clineMessages,
  942. (msg) => msg.type === "ask" && msg.ask === "followup" && !msg.isAnswered,
  943. )
  944. if (lastFollowUpIndex !== -1) {
  945. // Mark this follow-up as answered
  946. this.clineMessages[lastFollowUpIndex].isAnswered = true
  947. // Save the updated messages
  948. this.saveClineMessages().catch((error) => {
  949. console.error("Failed to save answered follow-up state:", error)
  950. })
  951. }
  952. }
  953. }
  954. public approveAsk({ text, images }: { text?: string; images?: string[] } = {}) {
  955. this.handleWebviewAskResponse("yesButtonClicked", text, images)
  956. }
  957. public denyAsk({ text, images }: { text?: string; images?: string[] } = {}) {
  958. this.handleWebviewAskResponse("noButtonClicked", text, images)
  959. }
  960. public async submitUserMessage(
  961. text: string,
  962. images?: string[],
  963. mode?: string,
  964. providerProfile?: string,
  965. ): Promise<void> {
  966. try {
  967. text = (text ?? "").trim()
  968. images = images ?? []
  969. if (text.length === 0 && images.length === 0) {
  970. return
  971. }
  972. const provider = this.providerRef.deref()
  973. if (provider) {
  974. if (mode) {
  975. await provider.setMode(mode)
  976. }
  977. if (providerProfile) {
  978. await provider.setProviderProfile(providerProfile)
  979. }
  980. this.emit(RooCodeEventName.TaskUserMessage, this.taskId)
  981. provider.postMessageToWebview({ type: "invoke", invoke: "sendMessage", text, images })
  982. } else {
  983. console.error("[Task#submitUserMessage] Provider reference lost")
  984. }
  985. } catch (error) {
  986. console.error("[Task#submitUserMessage] Failed to submit user message:", error)
  987. }
  988. }
  989. async handleTerminalOperation(terminalOperation: "continue" | "abort") {
  990. if (terminalOperation === "continue") {
  991. this.terminalProcess?.continue()
  992. } else if (terminalOperation === "abort") {
  993. this.terminalProcess?.abort()
  994. }
  995. }
  996. public async condenseContext(): Promise<void> {
  997. const systemPrompt = await this.getSystemPrompt()
  998. // Get condensing configuration
  999. const state = await this.providerRef.deref()?.getState()
  1000. // These properties may not exist in the state type yet, but are used for condensing configuration
  1001. const customCondensingPrompt = state?.customCondensingPrompt
  1002. const condensingApiConfigId = state?.condensingApiConfigId
  1003. const listApiConfigMeta = state?.listApiConfigMeta
  1004. // Determine API handler to use
  1005. let condensingApiHandler: ApiHandler | undefined
  1006. if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
  1007. // Find matching config by ID
  1008. const matchingConfig = listApiConfigMeta.find((config) => config.id === condensingApiConfigId)
  1009. if (matchingConfig) {
  1010. const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
  1011. id: condensingApiConfigId,
  1012. })
  1013. // Ensure profile and apiProvider exist before trying to build handler
  1014. if (profile && profile.apiProvider) {
  1015. condensingApiHandler = buildApiHandler(profile)
  1016. }
  1017. }
  1018. }
  1019. const { contextTokens: prevContextTokens } = this.getTokenUsage()
  1020. const {
  1021. messages,
  1022. summary,
  1023. cost,
  1024. newContextTokens = 0,
  1025. error,
  1026. } = await summarizeConversation(
  1027. this.apiConversationHistory,
  1028. this.api, // Main API handler (fallback)
  1029. systemPrompt, // Default summarization prompt (fallback)
  1030. this.taskId,
  1031. prevContextTokens,
  1032. false, // manual trigger
  1033. customCondensingPrompt, // User's custom prompt
  1034. condensingApiHandler, // Specific handler for condensing
  1035. )
  1036. if (error) {
  1037. this.say(
  1038. "condense_context_error",
  1039. error,
  1040. undefined /* images */,
  1041. false /* partial */,
  1042. undefined /* checkpoint */,
  1043. undefined /* progressStatus */,
  1044. { isNonInteractive: true } /* options */,
  1045. )
  1046. return
  1047. }
  1048. await this.overwriteApiConversationHistory(messages)
  1049. // Set flag to skip previous_response_id on the next API call after manual condense
  1050. this.skipPrevResponseIdOnce = true
  1051. const contextCondense: ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
  1052. await this.say(
  1053. "condense_context",
  1054. undefined /* text */,
  1055. undefined /* images */,
  1056. false /* partial */,
  1057. undefined /* checkpoint */,
  1058. undefined /* progressStatus */,
  1059. { isNonInteractive: true } /* options */,
  1060. contextCondense,
  1061. )
  1062. // Process any queued messages after condensing completes
  1063. this.processQueuedMessages()
  1064. }
  1065. async say(
  1066. type: ClineSay,
  1067. text?: string,
  1068. images?: string[],
  1069. partial?: boolean,
  1070. checkpoint?: Record<string, unknown>,
  1071. progressStatus?: ToolProgressStatus,
  1072. options: {
  1073. isNonInteractive?: boolean
  1074. metadata?: Record<string, unknown>
  1075. } = {},
  1076. contextCondense?: ContextCondense,
  1077. ): Promise<undefined> {
  1078. if (this.abort) {
  1079. throw new Error(`[Kilo Code#say] task ${this.taskId}.${this.instanceId} aborted`)
  1080. }
  1081. if (partial !== undefined) {
  1082. // kilocode_change start: Fix orphaned partial says by searching backwards
  1083. // Search for the most recent partial say of this type
  1084. const partialResult = findPartialSayMessage(this.clineMessages, type)
  1085. const lastMessage = partialResult?.message
  1086. const isUpdatingPreviousPartial = lastMessage !== undefined
  1087. // kilocode_change end
  1088. if (partial) {
  1089. if (isUpdatingPreviousPartial) {
  1090. // Existing partial message, so update it.
  1091. lastMessage.text = text
  1092. lastMessage.images = images
  1093. lastMessage.partial = partial
  1094. lastMessage.progressStatus = progressStatus
  1095. this.updateClineMessage(lastMessage)
  1096. } else {
  1097. // This is a new partial message, so add it with partial state.
  1098. const sayTs = await this.nextClineMessageTimestamp_kilocode()
  1099. if (!options.isNonInteractive) {
  1100. this.lastMessageTs = sayTs
  1101. }
  1102. await this.addToClineMessages({
  1103. ts: sayTs,
  1104. type: "say",
  1105. say: type,
  1106. text,
  1107. images,
  1108. partial,
  1109. contextCondense,
  1110. metadata: options.metadata,
  1111. })
  1112. }
  1113. } else {
  1114. // New now have a complete version of a previously partial message.
  1115. // This is the complete version of a previously partial
  1116. // message, so replace the partial with the complete version.
  1117. if (isUpdatingPreviousPartial) {
  1118. if (!options.isNonInteractive) {
  1119. this.lastMessageTs = lastMessage.ts
  1120. }
  1121. lastMessage.text = text
  1122. lastMessage.images = images
  1123. lastMessage.partial = false
  1124. lastMessage.progressStatus = progressStatus
  1125. if (options.metadata) {
  1126. // Add metadata to the message
  1127. const messageWithMetadata = lastMessage as ClineMessage & ClineMessageWithMetadata
  1128. if (!messageWithMetadata.metadata) {
  1129. messageWithMetadata.metadata = {}
  1130. }
  1131. Object.assign(messageWithMetadata.metadata, options.metadata)
  1132. }
  1133. // Instead of streaming partialMessage events, we do a save
  1134. // and post like normal to persist to disk.
  1135. await this.saveClineMessages()
  1136. // More performant than an entire `postStateToWebview`.
  1137. this.updateClineMessage(lastMessage)
  1138. } else {
  1139. // This is a new and complete message, so add it like normal.
  1140. const sayTs = await this.nextClineMessageTimestamp_kilocode()
  1141. if (!options.isNonInteractive) {
  1142. this.lastMessageTs = sayTs
  1143. }
  1144. await this.addToClineMessages({
  1145. ts: sayTs,
  1146. type: "say",
  1147. say: type,
  1148. text,
  1149. images,
  1150. contextCondense,
  1151. metadata: options.metadata,
  1152. })
  1153. }
  1154. }
  1155. } else {
  1156. // This is a new non-partial message, so add it like normal.
  1157. const sayTs = await this.nextClineMessageTimestamp_kilocode()
  1158. // A "non-interactive" message is a message is one that the user
  1159. // does not need to respond to. We don't want these message types
  1160. // to trigger an update to `lastMessageTs` since they can be created
  1161. // asynchronously and could interrupt a pending ask.
  1162. if (!options.isNonInteractive) {
  1163. this.lastMessageTs = sayTs
  1164. }
  1165. await this.addToClineMessages({
  1166. ts: sayTs,
  1167. type: "say",
  1168. say: type,
  1169. text,
  1170. images,
  1171. checkpoint,
  1172. contextCondense,
  1173. })
  1174. }
  1175. }
  1176. async sayAndCreateMissingParamError(toolName: ToolName, paramName: string, relPath?: string) {
  1177. const kilocodeExtraText = (() => {
  1178. switch (toolName) {
  1179. case "apply_diff":
  1180. return t("kilocode:task.disableApplyDiff") + " "
  1181. case "edit_file":
  1182. return t("kilocode:task.disableEditFile") + " "
  1183. default:
  1184. return ""
  1185. }
  1186. })()
  1187. await this.say(
  1188. "error",
  1189. `Kilo Code tried to use ${toolName}${
  1190. relPath ? ` for '${relPath.toPosix()}'` : ""
  1191. } without value for required parameter '${paramName}'. ${kilocodeExtraText}Retrying...`,
  1192. )
  1193. return formatResponse.toolError(
  1194. formatResponse.missingToolParameterError(
  1195. paramName,
  1196. getActiveToolUseStyle(this.apiConfiguration), // kilocode_change
  1197. ),
  1198. )
  1199. }
  1200. // Lifecycle
  1201. // Start / Resume / Abort / Dispose
  1202. private async startTask(task?: string, images?: string[]): Promise<void> {
  1203. if (this.enableBridge) {
  1204. try {
  1205. await BridgeOrchestrator.subscribeToTask(this)
  1206. } catch (error) {
  1207. console.error(
  1208. `[Task#startTask] BridgeOrchestrator.subscribeToTask() failed: ${error instanceof Error ? error.message : String(error)}`,
  1209. )
  1210. }
  1211. }
  1212. // `conversationHistory` (for API) and `clineMessages` (for webview)
  1213. // need to be in sync.
  1214. // If the extension process were killed, then on restart the
  1215. // `clineMessages` might not be empty, so we need to set it to [] when
  1216. // we create a new Cline client (otherwise webview would show stale
  1217. // messages from previous session).
  1218. this.clineMessages = []
  1219. this.apiConversationHistory = []
  1220. // The todo list is already set in the constructor if initialTodos were provided
  1221. // No need to add any messages - the todoList property is already set
  1222. await this.providerRef.deref()?.postStateToWebview()
  1223. await this.say("text", task, images)
  1224. this.isInitialized = true
  1225. let imageBlocks: Anthropic.ImageBlockParam[] = formatResponse.imageBlocks(images)
  1226. // Task starting
  1227. await this.initiateTaskLoop([
  1228. {
  1229. type: "text",
  1230. text: `<task>\n${task}\n</task>`,
  1231. },
  1232. ...imageBlocks,
  1233. ])
  1234. }
  1235. private async resumeTaskFromHistory() {
  1236. if (this.enableBridge) {
  1237. try {
  1238. await BridgeOrchestrator.subscribeToTask(this)
  1239. } catch (error) {
  1240. console.error(
  1241. `[Task#resumeTaskFromHistory] BridgeOrchestrator.subscribeToTask() failed: ${error instanceof Error ? error.message : String(error)}`,
  1242. )
  1243. }
  1244. }
  1245. const modifiedClineMessages = await this.getSavedClineMessages()
  1246. // Check for any stored GPT-5 response IDs in the message history.
  1247. const gpt5Messages = modifiedClineMessages.filter(
  1248. (m): m is ClineMessage & ClineMessageWithMetadata =>
  1249. m.type === "say" &&
  1250. m.say === "text" &&
  1251. !!(m as ClineMessageWithMetadata).metadata?.gpt5?.previous_response_id,
  1252. )
  1253. if (gpt5Messages.length > 0) {
  1254. const lastGpt5Message = gpt5Messages[gpt5Messages.length - 1]
  1255. // The lastGpt5Message contains the previous_response_id that can be
  1256. // used for continuity.
  1257. }
  1258. // Remove any resume messages that may have been added before.
  1259. const lastRelevantMessageIndex = findLastIndex(
  1260. modifiedClineMessages,
  1261. (m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task"),
  1262. )
  1263. if (lastRelevantMessageIndex !== -1) {
  1264. modifiedClineMessages.splice(lastRelevantMessageIndex + 1)
  1265. }
  1266. // Remove any trailing reasoning-only UI messages that were not part of the persisted API conversation
  1267. while (modifiedClineMessages.length > 0) {
  1268. const last = modifiedClineMessages[modifiedClineMessages.length - 1]
  1269. if (last.type === "say" && last.say === "reasoning") {
  1270. modifiedClineMessages.pop()
  1271. } else {
  1272. break
  1273. }
  1274. }
  1275. // Since we don't use `api_req_finished` anymore, we need to check if the
  1276. // last `api_req_started` has a cost value, if it doesn't and no
  1277. // cancellation reason to present, then we remove it since it indicates
  1278. // an api request without any partial content streamed.
  1279. const lastApiReqStartedIndex = findLastIndex(
  1280. modifiedClineMessages,
  1281. (m) => m.type === "say" && m.say === "api_req_started",
  1282. )
  1283. if (lastApiReqStartedIndex !== -1) {
  1284. const lastApiReqStarted = modifiedClineMessages[lastApiReqStartedIndex]
  1285. const { cost, cancelReason }: ClineApiReqInfo = JSON.parse(lastApiReqStarted.text || "{}")
  1286. if (cost === undefined && cancelReason === undefined) {
  1287. modifiedClineMessages.splice(lastApiReqStartedIndex, 1)
  1288. }
  1289. }
  1290. await this.overwriteClineMessages(modifiedClineMessages)
  1291. this.clineMessages = await this.getSavedClineMessages()
  1292. // Now present the cline messages to the user and ask if they want to
  1293. // resume (NOTE: we ran into a bug before where the
  1294. // apiConversationHistory wouldn't be initialized when opening a old
  1295. // task, and it was because we were waiting for resume).
  1296. // This is important in case the user deletes messages without resuming
  1297. // the task first.
  1298. this.apiConversationHistory = await this.getSavedApiConversationHistory()
  1299. const lastClineMessage = this.clineMessages
  1300. .slice()
  1301. .reverse()
  1302. .find((m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task")) // Could be multiple resume tasks.
  1303. let askType: ClineAsk
  1304. if (lastClineMessage?.ask === "completion_result") {
  1305. askType = "resume_completed_task"
  1306. } else {
  1307. askType = "resume_task"
  1308. }
  1309. this.isInitialized = true
  1310. const { response, text, images } = await this.ask(askType) // Calls `postStateToWebview`.
  1311. let responseText: string | undefined
  1312. let responseImages: string[] | undefined
  1313. if (response === "messageResponse") {
  1314. await this.say("user_feedback", text, images)
  1315. responseText = text
  1316. responseImages = images
  1317. }
  1318. // Make sure that the api conversation history can be resumed by the API,
  1319. // even if it goes out of sync with cline messages.
  1320. let existingApiConversationHistory: ApiMessage[] = await this.getSavedApiConversationHistory()
  1321. // 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
  1322. // kilocode_change start
  1323. //const conversationWithoutToolBlocks = existingApiConversationHistory.map((message) => {
  1324. // if (Array.isArray(message.content)) {
  1325. // const newContent = message.content.map((block) => {
  1326. // if (block.type === "tool_use") {
  1327. // // It's important we convert to the new tool schema
  1328. // // format so the model doesn't get confused about how to
  1329. // // invoke tools.
  1330. // const inputAsXml = Object.entries(block.input as Record<string, string>)
  1331. // .map(([key, value]) => `<${key}>\n${value}\n</${key}>`)
  1332. // .join("\n")
  1333. // return {
  1334. // type: "text",
  1335. // text: `<${block.name}>\n${inputAsXml}\n</${block.name}>`,
  1336. // } as Anthropic.Messages.TextBlockParam
  1337. // } else if (block.type === "tool_result") {
  1338. // // Convert block.content to text block array, removing images
  1339. // const contentAsTextBlocks = Array.isArray(block.content)
  1340. // ? block.content.filter((item) => item.type === "text")
  1341. // : [{ type: "text", text: block.content }]
  1342. // const textContent = contentAsTextBlocks.map((item) => item.text).join("\n\n")
  1343. // const toolName = findToolName(block.tool_use_id, existingApiConversationHistory)
  1344. // return {
  1345. // type: "text",
  1346. // text: `[${toolName} Result]\n\n${textContent}`,
  1347. // } as Anthropic.Messages.TextBlockParam
  1348. // }
  1349. // return block
  1350. // })
  1351. // return { ...message, content: newContent }
  1352. // }
  1353. // return message
  1354. //})
  1355. //existingApiConversationHistory = conversationWithoutToolBlocks
  1356. // kilocode_change end
  1357. // FIXME: remove tool use blocks altogether
  1358. // 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
  1359. // if there's no tool use and only a text block, then we can just add a user message
  1360. // (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)
  1361. // 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'
  1362. let modifiedOldUserContent: Anthropic.Messages.ContentBlockParam[] // either the last message if its user message, or the user message before the last (assistant) message
  1363. let modifiedApiConversationHistory: ApiMessage[] // need to remove the last user message to replace with new modified user message
  1364. if (existingApiConversationHistory.length > 0) {
  1365. const lastMessage = existingApiConversationHistory[existingApiConversationHistory.length - 1]
  1366. if (lastMessage.role === "assistant") {
  1367. const content = Array.isArray(lastMessage.content)
  1368. ? lastMessage.content
  1369. : [{ type: "text", text: lastMessage.content }]
  1370. const hasToolUse = content.some((block) => block.type === "tool_use")
  1371. if (hasToolUse) {
  1372. const toolUseBlocks = content.filter(
  1373. (block) => block.type === "tool_use",
  1374. ) as Anthropic.Messages.ToolUseBlock[]
  1375. const toolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks.map((block) => ({
  1376. type: "tool_result",
  1377. tool_use_id: block.id,
  1378. content: "Task was interrupted before this tool call could be completed.",
  1379. }))
  1380. modifiedApiConversationHistory = [...existingApiConversationHistory] // no changes
  1381. modifiedOldUserContent = [...toolResponses]
  1382. } else {
  1383. modifiedApiConversationHistory = [...existingApiConversationHistory]
  1384. modifiedOldUserContent = []
  1385. }
  1386. } else if (lastMessage.role === "user") {
  1387. const previousAssistantMessage: ApiMessage | undefined =
  1388. existingApiConversationHistory[existingApiConversationHistory.length - 2]
  1389. const existingUserContent: Anthropic.Messages.ContentBlockParam[] = Array.isArray(lastMessage.content)
  1390. ? lastMessage.content
  1391. : [{ type: "text", text: lastMessage.content }]
  1392. if (previousAssistantMessage && previousAssistantMessage.role === "assistant") {
  1393. const assistantContent = Array.isArray(previousAssistantMessage.content)
  1394. ? previousAssistantMessage.content
  1395. : [{ type: "text", text: previousAssistantMessage.content }]
  1396. const toolUseBlocks = assistantContent.filter(
  1397. (block) => block.type === "tool_use",
  1398. ) as Anthropic.Messages.ToolUseBlock[]
  1399. if (toolUseBlocks.length > 0) {
  1400. const existingToolResults = existingUserContent.filter(
  1401. (block) => block.type === "tool_result",
  1402. ) as Anthropic.ToolResultBlockParam[]
  1403. const missingToolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks
  1404. .filter(
  1405. (toolUse) => !existingToolResults.some((result) => result.tool_use_id === toolUse.id),
  1406. )
  1407. .map((toolUse) => ({
  1408. type: "tool_result",
  1409. tool_use_id: toolUse.id,
  1410. content: "Task was interrupted before this tool call could be completed.",
  1411. }))
  1412. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1) // removes the last user message
  1413. modifiedOldUserContent = [...existingUserContent, ...missingToolResponses]
  1414. } else {
  1415. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  1416. modifiedOldUserContent = [...existingUserContent]
  1417. }
  1418. } else {
  1419. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  1420. modifiedOldUserContent = [...existingUserContent]
  1421. }
  1422. } else {
  1423. throw new Error("Unexpected: Last message is not a user or assistant message")
  1424. }
  1425. } else {
  1426. throw new Error("Unexpected: No existing API conversation history")
  1427. }
  1428. let newUserContent: Anthropic.Messages.ContentBlockParam[] = [...modifiedOldUserContent]
  1429. const agoText = ((): string => {
  1430. const timestamp = lastClineMessage?.ts ?? Date.now()
  1431. const now = Date.now()
  1432. const diff = now - timestamp
  1433. const minutes = Math.floor(diff / 60000)
  1434. const hours = Math.floor(minutes / 60)
  1435. const days = Math.floor(hours / 24)
  1436. if (days > 0) {
  1437. return `${days} day${days > 1 ? "s" : ""} ago`
  1438. }
  1439. if (hours > 0) {
  1440. return `${hours} hour${hours > 1 ? "s" : ""} ago`
  1441. }
  1442. if (minutes > 0) {
  1443. return `${minutes} minute${minutes > 1 ? "s" : ""} ago`
  1444. }
  1445. return "just now"
  1446. })()
  1447. if (responseText) {
  1448. newUserContent.push({
  1449. type: "text",
  1450. text: `\n\nNew instructions for task continuation:\n<user_message>\n${responseText}\n</user_message>`,
  1451. })
  1452. }
  1453. if (responseImages && responseImages.length > 0) {
  1454. newUserContent.push(...formatResponse.imageBlocks(responseImages))
  1455. }
  1456. // Ensure we have at least some content to send to the API.
  1457. // If newUserContent is empty, add a minimal resumption message.
  1458. if (newUserContent.length === 0) {
  1459. newUserContent.push({
  1460. type: "text",
  1461. text: "[TASK RESUMPTION] Resuming task...",
  1462. })
  1463. }
  1464. await this.overwriteApiConversationHistory(modifiedApiConversationHistory)
  1465. // Task resuming from history item.
  1466. await this.initiateTaskLoop(newUserContent)
  1467. }
  1468. public async abortTask(isAbandoned = false) {
  1469. // Aborting task
  1470. // Will stop any autonomously running promises.
  1471. if (isAbandoned) {
  1472. this.abandoned = true
  1473. }
  1474. this.abort = true
  1475. this.emit(RooCodeEventName.TaskAborted)
  1476. try {
  1477. this.dispose() // Call the centralized dispose method
  1478. } catch (error) {
  1479. console.error(`Error during task ${this.taskId}.${this.instanceId} disposal:`, error)
  1480. // Don't rethrow - we want abort to always succeed
  1481. }
  1482. // Save the countdown message in the automatic retry or other content.
  1483. try {
  1484. // Save the countdown message in the automatic retry or other content.
  1485. await this.saveClineMessages()
  1486. } catch (error) {
  1487. console.error(`Error saving messages during abort for task ${this.taskId}.${this.instanceId}:`, error)
  1488. }
  1489. }
  1490. public dispose(): void {
  1491. console.log(`[Task#dispose] disposing task ${this.taskId}.${this.instanceId}`)
  1492. // Dispose message queue and remove event listeners.
  1493. try {
  1494. if (this.messageQueueStateChangedHandler) {
  1495. this.messageQueueService.removeListener("stateChanged", this.messageQueueStateChangedHandler)
  1496. this.messageQueueStateChangedHandler = undefined
  1497. }
  1498. this.messageQueueService.dispose()
  1499. } catch (error) {
  1500. console.error("Error disposing message queue:", error)
  1501. }
  1502. // Remove all event listeners to prevent memory leaks.
  1503. try {
  1504. this.removeAllListeners()
  1505. } catch (error) {
  1506. console.error("Error removing event listeners:", error)
  1507. }
  1508. // Stop waiting for child task completion.
  1509. if (this.pauseInterval) {
  1510. clearInterval(this.pauseInterval)
  1511. this.pauseInterval = undefined
  1512. }
  1513. if (this.enableBridge) {
  1514. BridgeOrchestrator.getInstance()
  1515. ?.unsubscribeFromTask(this.taskId)
  1516. .catch((error) =>
  1517. console.error(
  1518. `[Task#dispose] BridgeOrchestrator#unsubscribeFromTask() failed: ${error instanceof Error ? error.message : String(error)}`,
  1519. ),
  1520. )
  1521. }
  1522. // Release any terminals associated with this task.
  1523. try {
  1524. // Release any terminals associated with this task.
  1525. TerminalRegistry.releaseTerminalsForTask(this.taskId)
  1526. } catch (error) {
  1527. console.error("Error releasing terminals:", error)
  1528. }
  1529. try {
  1530. this.urlContentFetcher.closeBrowser()
  1531. } catch (error) {
  1532. console.error("Error closing URL content fetcher browser:", error)
  1533. }
  1534. try {
  1535. this.browserSession.closeBrowser()
  1536. } catch (error) {
  1537. console.error("Error closing browser session:", error)
  1538. }
  1539. try {
  1540. if (this.rooIgnoreController) {
  1541. this.rooIgnoreController.dispose()
  1542. this.rooIgnoreController = undefined
  1543. }
  1544. } catch (error) {
  1545. console.error("Error disposing RooIgnoreController:", error)
  1546. // This is the critical one for the leak fix.
  1547. }
  1548. try {
  1549. this.fileContextTracker.dispose()
  1550. } catch (error) {
  1551. console.error("Error disposing file context tracker:", error)
  1552. }
  1553. try {
  1554. // If we're not streaming then `abortStream` won't be called.
  1555. if (this.isStreaming && this.diffViewProvider.isEditing) {
  1556. this.diffViewProvider.revertChanges().catch(console.error)
  1557. }
  1558. } catch (error) {
  1559. console.error("Error reverting diff changes:", error)
  1560. }
  1561. }
  1562. // Subtasks
  1563. // Spawn / Wait / Complete
  1564. public async startSubtask(message: string, initialTodos: TodoItem[], mode: string) {
  1565. const provider = this.providerRef.deref()
  1566. if (!provider) {
  1567. throw new Error("Provider not available")
  1568. }
  1569. const newTask = await provider.createTask(message, undefined, this, { initialTodos })
  1570. if (newTask) {
  1571. this.isPaused = true // Pause parent.
  1572. this.childTaskId = newTask.taskId
  1573. await provider.handleModeSwitch(mode) // Set child's mode.
  1574. await delay(500) // Allow mode change to take effect.
  1575. this.emit(RooCodeEventName.TaskPaused, this.taskId)
  1576. this.emit(RooCodeEventName.TaskSpawned, newTask.taskId)
  1577. }
  1578. return newTask
  1579. }
  1580. // Used when a sub-task is launched and the parent task is waiting for it to
  1581. // finish.
  1582. // TBD: Add a timeout to prevent infinite waiting.
  1583. public async waitForSubtask() {
  1584. await new Promise<void>((resolve) => {
  1585. this.pauseInterval = setInterval(() => {
  1586. if (!this.isPaused) {
  1587. clearInterval(this.pauseInterval)
  1588. this.pauseInterval = undefined
  1589. resolve()
  1590. }
  1591. }, 1000)
  1592. })
  1593. }
  1594. public async completeSubtask(lastMessage: string) {
  1595. this.isPaused = false
  1596. this.childTaskId = undefined
  1597. this.emit(RooCodeEventName.TaskUnpaused, this.taskId)
  1598. // Fake an answer from the subtask that it has completed running and
  1599. // this is the result of what it has done add the message to the chat
  1600. // history and to the webview ui.
  1601. try {
  1602. await this.say("subtask_result", lastMessage)
  1603. await this.addToApiConversationHistory({
  1604. role: "user",
  1605. content: [{ type: "text", text: `[new_task completed] Result: ${lastMessage}` }],
  1606. })
  1607. // Set skipPrevResponseIdOnce to ensure the next API call sends the full conversation
  1608. // including the subtask result, not just from before the subtask was created
  1609. this.skipPrevResponseIdOnce = true
  1610. } catch (error) {
  1611. this.providerRef
  1612. .deref()
  1613. ?.log(`Error failed to add reply from subtask into conversation of parent task, error: ${error}`)
  1614. throw error
  1615. }
  1616. }
  1617. // Task Loop
  1618. private async initiateTaskLoop(userContent: Anthropic.Messages.ContentBlockParam[]): Promise<void> {
  1619. // Kicks off the checkpoints initialization process in the background.
  1620. getCheckpointService(this)
  1621. let nextUserContent = userContent
  1622. let includeFileDetails = true
  1623. this.emit(RooCodeEventName.TaskStarted)
  1624. while (!this.abort) {
  1625. const didEndLoop = await this.recursivelyMakeClineRequests(nextUserContent, includeFileDetails)
  1626. includeFileDetails = false // We only need file details the first time.
  1627. // The way this agentic loop works is that cline will be given a
  1628. // task that he then calls tools to complete. Unless there's an
  1629. // attempt_completion call, we keep responding back to him with his
  1630. // tool's responses until he either attempt_completion or does not
  1631. // use anymore tools. If he does not use anymore tools, we ask him
  1632. // to consider if he's completed the task and then call
  1633. // attempt_completion, otherwise proceed with completing the task.
  1634. // There is a MAX_REQUESTS_PER_TASK limit to prevent infinite
  1635. // requests, but Cline is prompted to finish the task as efficiently
  1636. // as he can.
  1637. if (didEndLoop) {
  1638. // For now a task never 'completes'. This will only happen if
  1639. // the user hits max requests and denies resetting the count.
  1640. break
  1641. } else {
  1642. nextUserContent = [
  1643. {
  1644. type: "text",
  1645. text: formatResponse.noToolsUsed(
  1646. getActiveToolUseStyle(this.apiConfiguration), // kilocode_change
  1647. ),
  1648. },
  1649. ]
  1650. this.consecutiveMistakeCount++
  1651. }
  1652. }
  1653. }
  1654. public async recursivelyMakeClineRequests(
  1655. userContent: Anthropic.Messages.ContentBlockParam[],
  1656. includeFileDetails: boolean = false,
  1657. ): Promise<boolean> {
  1658. interface StackItem {
  1659. userContent: Anthropic.Messages.ContentBlockParam[]
  1660. includeFileDetails: boolean
  1661. retryAttempt?: number
  1662. }
  1663. const stack: StackItem[] = [{ userContent, includeFileDetails, retryAttempt: 0 }]
  1664. while (stack.length > 0) {
  1665. const currentItem = stack.pop()!
  1666. const currentUserContent = currentItem.userContent
  1667. const currentIncludeFileDetails = currentItem.includeFileDetails
  1668. if (this.abort) {
  1669. throw new Error(
  1670. `[KiloCode#recursivelyMakeClineRequests] task ${this.taskId}.${this.instanceId} aborted`,
  1671. )
  1672. }
  1673. if (this.consecutiveMistakeLimit > 0 && this.consecutiveMistakeCount >= this.consecutiveMistakeLimit) {
  1674. const { response, text, images } = await this.ask(
  1675. "mistake_limit_reached",
  1676. t("common:errors.mistake_limit_guidance"),
  1677. )
  1678. if (response === "messageResponse") {
  1679. currentUserContent.push(
  1680. ...[
  1681. { type: "text" as const, text: formatResponse.tooManyMistakes(text) },
  1682. ...formatResponse.imageBlocks(images),
  1683. ],
  1684. )
  1685. await this.say("user_feedback", text, images)
  1686. // Track consecutive mistake errors in telemetry.
  1687. TelemetryService.instance.captureConsecutiveMistakeError(this.taskId)
  1688. }
  1689. this.consecutiveMistakeCount = 0
  1690. }
  1691. // In this Cline request loop, we need to check if this task instance
  1692. // has been asked to wait for a subtask to finish before continuing.
  1693. const provider = this.providerRef.deref()
  1694. if (this.isPaused && provider) {
  1695. provider.log(`[subtasks] paused ${this.taskId}.${this.instanceId}`)
  1696. await this.waitForSubtask()
  1697. provider.log(`[subtasks] resumed ${this.taskId}.${this.instanceId}`)
  1698. const currentMode = (await provider.getState())?.mode ?? defaultModeSlug
  1699. if (currentMode !== this.pausedModeSlug) {
  1700. // The mode has changed, we need to switch back to the paused mode.
  1701. await provider.handleModeSwitch(this.pausedModeSlug)
  1702. // Delay to allow mode change to take effect before next tool is executed.
  1703. await delay(500)
  1704. provider.log(
  1705. `[subtasks] task ${this.taskId}.${this.instanceId} has switched back to '${this.pausedModeSlug}' from '${currentMode}'`,
  1706. )
  1707. }
  1708. }
  1709. // Getting verbose details is an expensive operation, it uses ripgrep to
  1710. // top-down build file structure of project which for large projects can
  1711. // take a few seconds. For the best UX we show a placeholder api_req_started
  1712. // message with a loading spinner as this happens.
  1713. // Determine API protocol based on provider and model
  1714. const modelId = getModelId(this.apiConfiguration)
  1715. const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
  1716. await this.say(
  1717. "api_req_started",
  1718. JSON.stringify({
  1719. apiProtocol,
  1720. }),
  1721. )
  1722. const {
  1723. showRooIgnoredFiles = false,
  1724. includeDiagnosticMessages = true,
  1725. maxDiagnosticMessages = 50,
  1726. maxReadFileLine = -1,
  1727. } = (await this.providerRef.deref()?.getState()) ?? {}
  1728. // kilocode_change start
  1729. const [parsedUserContent, needsRulesFileCheck] = await processKiloUserContentMentions({
  1730. context: this.getContext(),
  1731. userContent: currentUserContent,
  1732. cwd: this.cwd,
  1733. urlContentFetcher: this.urlContentFetcher,
  1734. fileContextTracker: this.fileContextTracker,
  1735. rooIgnoreController: this.rooIgnoreController,
  1736. showRooIgnoredFiles,
  1737. includeDiagnosticMessages,
  1738. maxDiagnosticMessages,
  1739. maxReadFileLine,
  1740. })
  1741. if (needsRulesFileCheck) {
  1742. await this.say(
  1743. "error",
  1744. "Issue with processing the /newrule command. Double check that, if '.kilocode/rules' already exists, it's a directory and not a file. Otherwise there was an issue referencing this file/directory",
  1745. )
  1746. }
  1747. // kilocode_change end
  1748. const environmentDetails = await getEnvironmentDetails(this, currentIncludeFileDetails)
  1749. // Add environment details as its own text block, separate from tool
  1750. // results.
  1751. const finalUserContent = [...parsedUserContent, { type: "text" as const, text: environmentDetails }]
  1752. await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
  1753. TelemetryService.instance.captureConversationMessage(this.taskId, "user")
  1754. // Since we sent off a placeholder api_req_started message to update the
  1755. // webview while waiting to actually start the API request (to load
  1756. // potential details for example), we need to update the text of that
  1757. // message.
  1758. const lastApiReqIndex = findLastIndex(this.clineMessages, (m) => m.say === "api_req_started")
  1759. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  1760. apiProtocol,
  1761. } satisfies ClineApiReqInfo)
  1762. await this.saveClineMessages()
  1763. await provider?.postStateToWebview()
  1764. try {
  1765. let cacheWriteTokens = 0
  1766. let cacheReadTokens = 0
  1767. let inputTokens = 0
  1768. let outputTokens = 0
  1769. let totalCost: number | undefined
  1770. // kilocode_change start
  1771. let inferenceProvider: string | undefined
  1772. let usageMissing = false
  1773. const apiRequestStartTime = performance.now()
  1774. // kilocode_change end
  1775. // We can't use `api_req_finished` anymore since it's a unique case
  1776. // where it could come after a streaming message (i.e. in the middle
  1777. // of being updated or executed).
  1778. // Fortunately `api_req_finished` was always parsed out for the GUI
  1779. // anyways, so it remains solely for legacy purposes to keep track
  1780. // of prices in tasks from history (it's worth removing a few months
  1781. // from now).
  1782. const updateApiReqMsg = (cancelReason?: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  1783. if (lastApiReqIndex < 0 || !this.clineMessages[lastApiReqIndex]) {
  1784. return
  1785. }
  1786. const existingData = JSON.parse(this.clineMessages[lastApiReqIndex].text || "{}")
  1787. // Calculate total tokens and cost using provider-aware function
  1788. const modelId = getModelId(this.apiConfiguration)
  1789. const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
  1790. const costResult =
  1791. apiProtocol === "anthropic"
  1792. ? calculateApiCostAnthropic(
  1793. this.api.getModel().info,
  1794. inputTokens,
  1795. outputTokens,
  1796. cacheWriteTokens,
  1797. cacheReadTokens,
  1798. )
  1799. : calculateApiCostOpenAI(
  1800. this.api.getModel().info,
  1801. inputTokens,
  1802. outputTokens,
  1803. cacheWriteTokens,
  1804. cacheReadTokens,
  1805. )
  1806. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  1807. ...existingData,
  1808. tokensIn: costResult.totalInputTokens,
  1809. tokensOut: costResult.totalOutputTokens,
  1810. cacheWrites: cacheWriteTokens,
  1811. cacheReads: cacheReadTokens,
  1812. cost: totalCost ?? costResult.totalCost,
  1813. // kilocode_change start
  1814. usageMissing,
  1815. inferenceProvider,
  1816. // kilocode_change end
  1817. cancelReason,
  1818. streamingFailedMessage,
  1819. } satisfies ClineApiReqInfo)
  1820. }
  1821. const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  1822. if (this.diffViewProvider.isEditing) {
  1823. await this.diffViewProvider.revertChanges() // closes diff view
  1824. }
  1825. // if last message is a partial we need to update and save it
  1826. const lastMessage = this.clineMessages.at(-1)
  1827. if (lastMessage && lastMessage.partial) {
  1828. // lastMessage.ts = Date.now() DO NOT update ts since it is used as a key for virtuoso list
  1829. lastMessage.partial = false
  1830. // instead of streaming partialMessage events, we do a save and post like normal to persist to disk
  1831. console.log("updating partial message", lastMessage)
  1832. }
  1833. // Update `api_req_started` to have cancelled and cost, so that
  1834. // we can display the cost of the partial stream and the cancellation reason
  1835. updateApiReqMsg(cancelReason, streamingFailedMessage)
  1836. await this.saveClineMessages()
  1837. // Signals to provider that it can retrieve the saved messages
  1838. // from disk, as abortTask can not be awaited on in nature.
  1839. this.didFinishAbortingStream = true
  1840. }
  1841. // Reset streaming state for each new API request
  1842. this.currentStreamingContentIndex = 0
  1843. this.currentStreamingDidCheckpoint = false
  1844. this.assistantMessageContent = []
  1845. this.didCompleteReadingStream = false
  1846. this.userMessageContent = []
  1847. this.userMessageContentReady = false
  1848. this.didRejectTool = false
  1849. this.didAlreadyUseTool = false
  1850. this.presentAssistantMessageLocked = false
  1851. this.presentAssistantMessageHasPendingUpdates = false
  1852. this.assistantMessageParser.reset()
  1853. await this.diffViewProvider.reset()
  1854. // Yields only if the first chunk is successful, otherwise will
  1855. // allow the user to retry the request (most likely due to rate
  1856. // limit error, which gets thrown on the first chunk).
  1857. const stream = this.attemptApiRequest()
  1858. let assistantMessage = ""
  1859. let reasoningMessage = ""
  1860. let pendingGroundingSources: GroundingSource[] = []
  1861. this.isStreaming = true
  1862. // kilocode_change start
  1863. const assistantToolUses = new Array<Anthropic.Messages.ToolUseBlockParam>()
  1864. const reasoningDetails = new Array<ReasoningDetail>()
  1865. const antThinkingContent = new Array<
  1866. Anthropic.Messages.RedactedThinkingBlock | Anthropic.Messages.ThinkingBlock
  1867. >()
  1868. // kilocode_change end
  1869. try {
  1870. const iterator = stream[Symbol.asyncIterator]()
  1871. let item = await iterator.next()
  1872. while (!item.done) {
  1873. const chunk = item.value
  1874. item = await iterator.next()
  1875. if (!chunk) {
  1876. // Sometimes chunk is undefined, no idea that can cause
  1877. // it, but this workaround seems to fix it.
  1878. continue
  1879. }
  1880. switch (chunk.type) {
  1881. case "reasoning": {
  1882. reasoningMessage += chunk.text
  1883. // Only apply formatting if the message contains sentence-ending punctuation followed by **
  1884. let formattedReasoning = reasoningMessage
  1885. if (reasoningMessage.includes("**")) {
  1886. // Add line breaks before **Title** patterns that appear after sentence endings
  1887. // This targets section headers like "...end of sentence.**Title Here**"
  1888. // Handles periods, exclamation marks, and question marks
  1889. formattedReasoning = reasoningMessage.replace(
  1890. /([.!?])\*\*([^*\n]+)\*\*/g,
  1891. "$1\n\n**$2**",
  1892. )
  1893. }
  1894. await this.say("reasoning", formattedReasoning, undefined, true)
  1895. break
  1896. }
  1897. case "usage":
  1898. inputTokens += chunk.inputTokens
  1899. outputTokens += chunk.outputTokens
  1900. cacheWriteTokens += chunk.cacheWriteTokens ?? 0
  1901. cacheReadTokens += chunk.cacheReadTokens ?? 0
  1902. totalCost = chunk.totalCost
  1903. inferenceProvider = chunk.inferenceProvider // kilocode_change
  1904. break
  1905. case "grounding":
  1906. // Handle grounding sources separately from regular content
  1907. // to prevent state persistence issues - store them separately
  1908. if (chunk.sources && chunk.sources.length > 0) {
  1909. pendingGroundingSources.push(...chunk.sources)
  1910. }
  1911. break
  1912. // kilocode_change start
  1913. case "reasoning_details":
  1914. // reasoning_details may be an array of 0 or 1 items depending on how openrouter returns it
  1915. if (Array.isArray(chunk.reasoning_details)) {
  1916. reasoningDetails.push(...chunk.reasoning_details)
  1917. } else {
  1918. reasoningDetails.push(chunk.reasoning_details)
  1919. }
  1920. break
  1921. case "native_tool_calls": {
  1922. // Handle native OpenAI-format tool calls
  1923. // Process native tool calls through the parser
  1924. for (const toolUse of this.assistantMessageParser.processNativeToolCalls(
  1925. chunk.toolCalls,
  1926. )) {
  1927. assistantToolUses.push(toolUse)
  1928. }
  1929. // Update content blocks after processing native tool calls
  1930. const prevLength = this.assistantMessageContent.length
  1931. this.assistantMessageContent = this.assistantMessageParser.getContentBlocks()
  1932. if (this.assistantMessageContent.length > prevLength) {
  1933. // New content we need to present
  1934. this.userMessageContentReady = false
  1935. }
  1936. // Present content to user
  1937. presentAssistantMessage(this)
  1938. break
  1939. }
  1940. case "ant_thinking":
  1941. antThinkingContent.push({
  1942. type: "thinking",
  1943. thinking: chunk.thinking,
  1944. signature: chunk.signature,
  1945. })
  1946. break
  1947. case "ant_redacted_thinking":
  1948. antThinkingContent.push({
  1949. type: "redacted_thinking",
  1950. data: chunk.data,
  1951. })
  1952. break
  1953. // kilocode_change end
  1954. case "text": {
  1955. assistantMessage += chunk.text
  1956. // Parse raw assistant message chunk into content blocks.
  1957. const prevLength = this.assistantMessageContent.length
  1958. this.assistantMessageContent = this.assistantMessageParser.processChunk(chunk.text)
  1959. if (this.assistantMessageContent.length > prevLength) {
  1960. // New content we need to present, reset to
  1961. // false in case previous content set this to true.
  1962. this.userMessageContentReady = false
  1963. }
  1964. // Present content to user.
  1965. presentAssistantMessage(this)
  1966. break
  1967. }
  1968. }
  1969. if (this.abort) {
  1970. console.log(`aborting stream, this.abandoned = ${this.abandoned}`)
  1971. if (!this.abandoned) {
  1972. // Only need to gracefully abort if this instance
  1973. // isn't abandoned (sometimes OpenRouter stream
  1974. // hangs, in which case this would affect future
  1975. // instances of Cline).
  1976. await abortStream("user_cancelled")
  1977. }
  1978. break // Aborts the stream.
  1979. }
  1980. if (this.didRejectTool) {
  1981. // `userContent` has a tool rejection, so interrupt the
  1982. // assistant's response to present the user's feedback.
  1983. assistantMessage += "\n\n[Response interrupted by user feedback]"
  1984. // Instead of setting this preemptively, we allow the
  1985. // present iterator to finish and set
  1986. // userMessageContentReady when its ready.
  1987. // this.userMessageContentReady = true
  1988. break
  1989. }
  1990. if (this.didAlreadyUseTool) {
  1991. assistantMessage +=
  1992. "\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.]"
  1993. break
  1994. }
  1995. }
  1996. // Create a copy of current token values to avoid race conditions
  1997. const currentTokens = {
  1998. input: inputTokens,
  1999. output: outputTokens,
  2000. cacheWrite: cacheWriteTokens,
  2001. cacheRead: cacheReadTokens,
  2002. total: totalCost,
  2003. }
  2004. const drainStreamInBackgroundToFindAllUsage = async (apiReqIndex: number) => {
  2005. const timeoutMs = DEFAULT_USAGE_COLLECTION_TIMEOUT_MS
  2006. const startTime = performance.now()
  2007. const modelId = getModelId(this.apiConfiguration)
  2008. // Local variables to accumulate usage data without affecting the main flow
  2009. let bgInputTokens = currentTokens.input
  2010. let bgOutputTokens = currentTokens.output
  2011. let bgCacheWriteTokens = currentTokens.cacheWrite
  2012. let bgCacheReadTokens = currentTokens.cacheRead
  2013. let bgTotalCost = currentTokens.total
  2014. // kilocode_change start
  2015. const refreshApiReqMsg = async (messageIndex: number) => {
  2016. // Update the API request message with the latest usage data
  2017. updateApiReqMsg()
  2018. await this.saveClineMessages()
  2019. // Update the specific message in the webview
  2020. const apiReqMessage = this.clineMessages[messageIndex]
  2021. if (apiReqMessage) {
  2022. await this.updateClineMessage(apiReqMessage)
  2023. }
  2024. }
  2025. // kilocode_change end
  2026. // Helper function to capture telemetry and update messages
  2027. const captureUsageData = async (
  2028. tokens: {
  2029. input: number
  2030. output: number
  2031. cacheWrite: number
  2032. cacheRead: number
  2033. total?: number
  2034. },
  2035. messageIndex: number = apiReqIndex,
  2036. ) => {
  2037. if (
  2038. tokens.input > 0 ||
  2039. tokens.output > 0 ||
  2040. tokens.cacheWrite > 0 ||
  2041. tokens.cacheRead > 0
  2042. ) {
  2043. // Update the shared variables atomically
  2044. inputTokens = tokens.input
  2045. outputTokens = tokens.output
  2046. cacheWriteTokens = tokens.cacheWrite
  2047. cacheReadTokens = tokens.cacheRead
  2048. totalCost = tokens.total
  2049. // Update the API request message with the latest usage data
  2050. updateApiReqMsg()
  2051. await this.saveClineMessages()
  2052. // Update the specific message in the webview
  2053. const apiReqMessage = this.clineMessages[messageIndex]
  2054. if (apiReqMessage) {
  2055. await this.updateClineMessage(apiReqMessage)
  2056. }
  2057. // Capture telemetry with provider-aware cost calculation
  2058. const modelId = getModelId(this.apiConfiguration)
  2059. const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
  2060. // Use the appropriate cost function based on the API protocol
  2061. const costResult =
  2062. apiProtocol === "anthropic"
  2063. ? calculateApiCostAnthropic(
  2064. this.api.getModel().info,
  2065. tokens.input,
  2066. tokens.output,
  2067. tokens.cacheWrite,
  2068. tokens.cacheRead,
  2069. )
  2070. : calculateApiCostOpenAI(
  2071. this.api.getModel().info,
  2072. tokens.input,
  2073. tokens.output,
  2074. tokens.cacheWrite,
  2075. tokens.cacheRead,
  2076. )
  2077. TelemetryService.instance.captureLlmCompletion(this.taskId, {
  2078. inputTokens: costResult.totalInputTokens,
  2079. outputTokens: costResult.totalOutputTokens,
  2080. cacheWriteTokens: tokens.cacheWrite,
  2081. cacheReadTokens: tokens.cacheRead,
  2082. cost: tokens.total ?? costResult.totalCost,
  2083. // kilocode_change start
  2084. completionTime: performance.now() - apiRequestStartTime,
  2085. inferenceProvider,
  2086. // kilocode_change end
  2087. })
  2088. }
  2089. }
  2090. try {
  2091. // Continue processing the original stream from where the main loop left off
  2092. let usageFound = false
  2093. let chunkCount = 0
  2094. // Use the same iterator that the main loop was using
  2095. while (!item.done) {
  2096. // Check for timeout
  2097. if (performance.now() - startTime > timeoutMs) {
  2098. console.warn(
  2099. `[Background Usage Collection] Timed out after ${timeoutMs}ms for model: ${modelId}, processed ${chunkCount} chunks`,
  2100. )
  2101. // Clean up the iterator before breaking
  2102. if (iterator.return) {
  2103. await iterator.return(undefined)
  2104. }
  2105. break
  2106. }
  2107. const chunk = item.value
  2108. item = await iterator.next()
  2109. chunkCount++
  2110. if (chunk && chunk.type === "usage") {
  2111. usageFound = true
  2112. bgInputTokens += chunk.inputTokens
  2113. bgOutputTokens += chunk.outputTokens
  2114. bgCacheWriteTokens += chunk.cacheWriteTokens ?? 0
  2115. bgCacheReadTokens += chunk.cacheReadTokens ?? 0
  2116. bgTotalCost = chunk.totalCost
  2117. inferenceProvider = chunk.inferenceProvider // kilocode_change
  2118. }
  2119. }
  2120. if (
  2121. usageFound ||
  2122. bgInputTokens > 0 ||
  2123. bgOutputTokens > 0 ||
  2124. bgCacheWriteTokens > 0 ||
  2125. bgCacheReadTokens > 0
  2126. ) {
  2127. // We have usage data either from a usage chunk or accumulated tokens
  2128. await captureUsageData(
  2129. {
  2130. input: bgInputTokens,
  2131. output: bgOutputTokens,
  2132. cacheWrite: bgCacheWriteTokens,
  2133. cacheRead: bgCacheReadTokens,
  2134. total: bgTotalCost,
  2135. },
  2136. lastApiReqIndex,
  2137. )
  2138. } else {
  2139. console.warn(
  2140. `[Background Usage Collection] Suspicious: request ${apiReqIndex} is complete, but no usage info was found. Model: ${modelId}`,
  2141. )
  2142. // kilocode_change start
  2143. usageMissing = true
  2144. await refreshApiReqMsg(apiReqIndex)
  2145. // kilocode_change end
  2146. }
  2147. } catch (error) {
  2148. console.error("Error draining stream for usage data:", error)
  2149. // Still try to capture whatever usage data we have collected so far
  2150. if (
  2151. bgInputTokens > 0 ||
  2152. bgOutputTokens > 0 ||
  2153. bgCacheWriteTokens > 0 ||
  2154. bgCacheReadTokens > 0
  2155. ) {
  2156. await captureUsageData(
  2157. {
  2158. input: bgInputTokens,
  2159. output: bgOutputTokens,
  2160. cacheWrite: bgCacheWriteTokens,
  2161. cacheRead: bgCacheReadTokens,
  2162. total: bgTotalCost,
  2163. },
  2164. lastApiReqIndex,
  2165. )
  2166. // kilocode_change start
  2167. } else {
  2168. usageMissing = true
  2169. await refreshApiReqMsg(apiReqIndex)
  2170. // kilocode_change end
  2171. }
  2172. }
  2173. }
  2174. // Start the background task and handle any errors
  2175. drainStreamInBackgroundToFindAllUsage(lastApiReqIndex).catch((error) => {
  2176. console.error("Background usage collection failed:", error)
  2177. })
  2178. } catch (error) {
  2179. // Abandoned happens when extension is no longer waiting for the
  2180. // Cline instance to finish aborting (error is thrown here when
  2181. // any function in the for loop throws due to this.abort).
  2182. if (!this.abandoned) {
  2183. // Determine cancellation reason
  2184. const cancelReason: ClineApiReqCancelReason = this.abort ? "user_cancelled" : "streaming_failed"
  2185. const streamingFailedMessage = this.abort
  2186. ? undefined
  2187. : (error.message ?? JSON.stringify(serializeError(error), null, 2))
  2188. // Clean up partial state
  2189. await abortStream(cancelReason, streamingFailedMessage)
  2190. if (this.abort) {
  2191. // User cancelled - abort the entire task
  2192. this.abortReason = cancelReason
  2193. await this.abortTask()
  2194. } else {
  2195. // Stream failed - log the error and retry with the same content
  2196. // The existing rate limiting will prevent rapid retries
  2197. console.error(
  2198. `[Task#${this.taskId}.${this.instanceId}] Stream failed, will retry: ${streamingFailedMessage}`,
  2199. )
  2200. // Apply exponential backoff similar to first-chunk errors when auto-resubmit is enabled
  2201. const stateForBackoff = await this.providerRef.deref()?.getState()
  2202. if (stateForBackoff?.autoApprovalEnabled && stateForBackoff?.alwaysApproveResubmit) {
  2203. await this.backoffAndAnnounce(
  2204. currentItem.retryAttempt ?? 0,
  2205. error,
  2206. streamingFailedMessage,
  2207. )
  2208. // Check if task was aborted during the backoff
  2209. if (this.abort) {
  2210. console.log(
  2211. `[Task#${this.taskId}.${this.instanceId}] Task aborted during mid-stream retry backoff`,
  2212. )
  2213. // Abort the entire task
  2214. this.abortReason = "user_cancelled"
  2215. await this.abortTask()
  2216. break
  2217. }
  2218. }
  2219. // Push the same content back onto the stack to retry, incrementing the retry attempt counter
  2220. stack.push({
  2221. userContent: currentUserContent,
  2222. includeFileDetails: false,
  2223. retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
  2224. })
  2225. // Continue to retry the request
  2226. continue
  2227. }
  2228. }
  2229. } finally {
  2230. this.isStreaming = false
  2231. }
  2232. // Need to call here in case the stream was aborted.
  2233. if (this.abort || this.abandoned) {
  2234. throw new Error(
  2235. `[KiloCode#recursivelyMakeClineRequests] task ${this.taskId}.${this.instanceId} aborted`,
  2236. )
  2237. }
  2238. this.didCompleteReadingStream = true
  2239. // Set any blocks to be complete to allow `presentAssistantMessage`
  2240. // to finish and set `userMessageContentReady` to true.
  2241. // (Could be a text block that had no subsequent tool uses, or a
  2242. // text block at the very end, or an invalid tool use, etc. Whatever
  2243. // the case, `presentAssistantMessage` relies on these blocks either
  2244. // to be completed or the user to reject a block in order to proceed
  2245. // and eventually set userMessageContentReady to true.)
  2246. const partialBlocks = this.assistantMessageContent.filter((block) => block.partial)
  2247. partialBlocks.forEach((block) => (block.partial = false))
  2248. // Can't just do this b/c a tool could be in the middle of executing.
  2249. // this.assistantMessageContent.forEach((e) => (e.partial = false))
  2250. // Now that the stream is complete, finalize any remaining partial content blocks
  2251. this.assistantMessageParser.finalizeContentBlocks()
  2252. this.assistantMessageContent = this.assistantMessageParser.getContentBlocks()
  2253. if (partialBlocks.length > 0) {
  2254. // If there is content to update then it will complete and
  2255. // update `this.userMessageContentReady` to true, which we
  2256. // `pWaitFor` before making the next request. All this is really
  2257. // doing is presenting the last partial message that we just set
  2258. // to complete.
  2259. presentAssistantMessage(this)
  2260. }
  2261. // Note: updateApiReqMsg() is now called from within drainStreamInBackgroundToFindAllUsage
  2262. // to ensure usage data is captured even when the stream is interrupted. The background task
  2263. // uses local variables to accumulate usage data before atomically updating the shared state.
  2264. // Complete the reasoning message if it exists
  2265. // We can't use say() here because the reasoning message may not be the last message
  2266. // (other messages like text blocks or tool uses may have been added after it during streaming)
  2267. if (reasoningMessage) {
  2268. const lastReasoningIndex = findLastIndex(
  2269. this.clineMessages,
  2270. (m) => m.type === "say" && m.say === "reasoning",
  2271. )
  2272. if (lastReasoningIndex !== -1 && this.clineMessages[lastReasoningIndex].partial) {
  2273. this.clineMessages[lastReasoningIndex].partial = false
  2274. await this.updateClineMessage(this.clineMessages[lastReasoningIndex])
  2275. }
  2276. }
  2277. await this.persistGpt5Metadata()
  2278. await this.saveClineMessages()
  2279. await this.providerRef.deref()?.postStateToWebview()
  2280. // Reset parser after each complete conversation round
  2281. this.assistantMessageParser.reset()
  2282. // Now add to apiConversationHistory.
  2283. // Need to save assistant responses to file before proceeding to
  2284. // tool use since user can exit at any moment and we wouldn't be
  2285. // able to save the assistant's response.
  2286. let didEndLoop = false
  2287. if (assistantMessage.length > 0 || assistantToolUses.length > 0 /* kilocode_change */) {
  2288. // Display grounding sources to the user if they exist
  2289. if (pendingGroundingSources.length > 0) {
  2290. const citationLinks = pendingGroundingSources.map((source, i) => `[${i + 1}](${source.url})`)
  2291. const sourcesText = `${t("common:gemini.sources")} ${citationLinks.join(", ")}`
  2292. await this.say("text", sourcesText, undefined, false, undefined, undefined, {
  2293. isNonInteractive: true,
  2294. })
  2295. }
  2296. // Check if we should preserve reasoning in the assistant message
  2297. let finalAssistantMessage = assistantMessage
  2298. // kilocode_change start: also add tool calls, reasoning_details to history
  2299. const assistantMessageContent = new Array<Anthropic.Messages.ContentBlockParam>()
  2300. assistantMessageContent.push(...antThinkingContent)
  2301. if (finalAssistantMessage || reasoningDetails.length > 0) {
  2302. assistantMessageContent.push({
  2303. type: "text",
  2304. text: finalAssistantMessage,
  2305. // @ts-ignore-next-line OpenRouter-specific property
  2306. reasoning_details: reasoningDetails.length > 0 ? reasoningDetails : undefined,
  2307. })
  2308. }
  2309. assistantMessageContent.push(...assistantToolUses)
  2310. await this.addToApiConversationHistory({
  2311. role: "assistant",
  2312. content: assistantMessageContent,
  2313. })
  2314. // kilocode_change end
  2315. TelemetryService.instance.captureConversationMessage(this.taskId, "assistant")
  2316. // NOTE: This comment is here for future reference - this was a
  2317. // workaround for `userMessageContent` not getting set to true.
  2318. // It was due to it not recursively calling for partial blocks
  2319. // when `didRejectTool`, so it would get stuck waiting for a
  2320. // partial block to complete before it could continue.
  2321. // In case the content blocks finished it may be the api stream
  2322. // finished after the last parsed content block was executed, so
  2323. // we are able to detect out of bounds and set
  2324. // `userMessageContentReady` to true (note you should not call
  2325. // `presentAssistantMessage` since if the last block i
  2326. // completed it will be presented again).
  2327. // const completeBlocks = this.assistantMessageContent.filter((block) => !block.partial) // If there are any partial blocks after the stream ended we can consider them invalid.
  2328. // if (this.currentStreamingContentIndex >= completeBlocks.length) {
  2329. // this.userMessageContentReady = true
  2330. // }
  2331. await pWaitFor(() => this.userMessageContentReady)
  2332. // If the model did not tool use, then we need to tell it to
  2333. // either use a tool or attempt_completion.
  2334. const didToolUse = this.assistantMessageContent.some((block) => block.type === "tool_use")
  2335. if (!didToolUse) {
  2336. this.userMessageContent.push({
  2337. type: "text",
  2338. text: formatResponse.noToolsUsed(
  2339. getActiveToolUseStyle(this.apiConfiguration), // kilocode_change
  2340. ),
  2341. })
  2342. this.consecutiveMistakeCount++
  2343. }
  2344. if (this.userMessageContent.length > 0) {
  2345. stack.push({
  2346. userContent: [...this.userMessageContent], // Create a copy to avoid mutation issues
  2347. includeFileDetails: false, // Subsequent iterations don't need file details
  2348. })
  2349. // Add periodic yielding to prevent blocking
  2350. await new Promise((resolve) => setImmediate(resolve))
  2351. }
  2352. // Continue to next iteration instead of setting didEndLoop from recursive call
  2353. continue
  2354. } else {
  2355. // If there's no assistant_responses, that means we got no text
  2356. // or tool_use content blocks from API which we should assume is
  2357. // an error.
  2358. await this.say(
  2359. "error",
  2360. t("kilocode:task.noAssistantMessages"), // kilocode_change
  2361. )
  2362. // kilocode_change start
  2363. TelemetryService.instance.captureEvent(TelemetryEventName.NO_ASSISTANT_MESSAGES)
  2364. // kilocode_change end
  2365. await this.addToApiConversationHistory({
  2366. role: "assistant",
  2367. content: [{ type: "text", text: "Failure: I did not provide a response." }],
  2368. })
  2369. }
  2370. // If we reach here without continuing, return false (will always be false for now)
  2371. return false
  2372. } catch (error) {
  2373. // This should never happen since the only thing that can throw an
  2374. // error is the attemptApiRequest, which is wrapped in a try catch
  2375. // that sends an ask where if noButtonClicked, will clear current
  2376. // task and destroy this instance. However to avoid unhandled
  2377. // promise rejection, we will end this loop which will end execution
  2378. // of this instance (see `startTask`).
  2379. return true // Needs to be true so parent loop knows to end task.
  2380. }
  2381. }
  2382. // If we exit the while loop normally (stack is empty), return false
  2383. return false
  2384. }
  2385. // kilocode_change start
  2386. async loadContext(
  2387. userContent: UserContent,
  2388. includeFileDetails: boolean = false,
  2389. ): Promise<[UserContent, string, boolean]> {
  2390. // Track if we need to check clinerulesFile
  2391. let needsClinerulesFileCheck = false
  2392. // bookmark
  2393. const { localWorkflowToggles, globalWorkflowToggles } = await refreshWorkflowToggles(
  2394. this.getContext(),
  2395. this.cwd,
  2396. )
  2397. const processUserContent = async () => {
  2398. // This is a temporary solution to dynamically load context mentions from tool results. It checks for the presence of tags that indicate that the tool was rejected and feedback was provided (see formatToolDeniedFeedback, attemptCompletion, executeCommand, and consecutiveMistakeCount >= 3) or "<answer>" (see askFollowupQuestion), we place all user generated content in these tags so they can effectively be used as markers for when we should parse mentions). However if we allow multiple tools responses in the future, we will need to parse mentions specifically within the user content tags.
  2399. // (Note: this caused the @/ import alias bug where file contents were being parsed as well, since v2 converted tool results to text blocks)
  2400. return await Promise.all(
  2401. userContent.map(async (block) => {
  2402. if (block.type === "text") {
  2403. // We need to ensure any user generated content is wrapped in one of these tags so that we know to parse mentions
  2404. // FIXME: Only parse text in between these tags instead of the entire text block which may contain other tool results. This is part of a larger issue where we shouldn't be using regex to parse mentions in the first place (ie for cases where file paths have spaces)
  2405. if (
  2406. block.text.includes("<feedback>") ||
  2407. block.text.includes("<answer>") ||
  2408. block.text.includes("<task>") ||
  2409. block.text.includes("<user_message>")
  2410. ) {
  2411. const parsedText = await parseMentions(
  2412. block.text,
  2413. this.cwd,
  2414. this.urlContentFetcher,
  2415. this.fileContextTracker,
  2416. )
  2417. // when parsing slash commands, we still want to allow the user to provide their desired context
  2418. const { processedText, needsRulesFileCheck: needsCheck } = await parseKiloSlashCommands(
  2419. parsedText,
  2420. localWorkflowToggles,
  2421. globalWorkflowToggles,
  2422. )
  2423. if (needsCheck) {
  2424. needsClinerulesFileCheck = true
  2425. }
  2426. return {
  2427. ...block,
  2428. text: processedText,
  2429. }
  2430. }
  2431. }
  2432. return block
  2433. }),
  2434. )
  2435. }
  2436. // Run initial promises in parallel
  2437. const [processedUserContent, environmentDetails] = await Promise.all([
  2438. processUserContent(),
  2439. getEnvironmentDetails(this, includeFileDetails),
  2440. ])
  2441. // const [parsedUserContent, environmentDetails, clinerulesError] = await this.loadContext(
  2442. // userContent,
  2443. // includeFileDetails,
  2444. // )
  2445. // After processing content, check clinerulesData if needed
  2446. let clinerulesError = false
  2447. if (needsClinerulesFileCheck) {
  2448. clinerulesError = await ensureLocalKilorulesDirExists(this.cwd, GlobalFileNames.kiloRules)
  2449. }
  2450. // Return all results
  2451. return [processedUserContent, environmentDetails, clinerulesError]
  2452. }
  2453. // kilocode_change end
  2454. /*private kilocode_change*/ async getSystemPrompt(): Promise<string> {
  2455. const { mcpEnabled } = (await this.providerRef.deref()?.getState()) ?? {}
  2456. let mcpHub: McpHub | undefined
  2457. if (mcpEnabled ?? true) {
  2458. const provider = this.providerRef.deref()
  2459. if (!provider) {
  2460. throw new Error("Provider reference lost during view transition")
  2461. }
  2462. // Wait for MCP hub initialization through McpServerManager
  2463. mcpHub = await McpServerManager.getInstance(provider.context, provider)
  2464. if (!mcpHub) {
  2465. throw new Error("Failed to get MCP hub from server manager")
  2466. }
  2467. // Wait for MCP servers to be connected before generating system prompt
  2468. await pWaitFor(() => !mcpHub!.isConnecting, { timeout: 10_000 }).catch(() => {
  2469. console.error("MCP servers failed to connect in time")
  2470. })
  2471. }
  2472. const rooIgnoreInstructions = this.rooIgnoreController?.getInstructions()
  2473. const state = await this.providerRef.deref()?.getState()
  2474. const {
  2475. browserViewportSize,
  2476. mode,
  2477. customModes,
  2478. customModePrompts,
  2479. customInstructions,
  2480. experiments,
  2481. enableMcpServerCreation,
  2482. browserToolEnabled,
  2483. language,
  2484. maxConcurrentFileReads,
  2485. maxReadFileLine,
  2486. apiConfiguration,
  2487. } = state ?? {}
  2488. return await (async () => {
  2489. const provider = this.providerRef.deref()
  2490. if (!provider) {
  2491. throw new Error("Provider not available")
  2492. }
  2493. // Align browser tool enablement with generateSystemPrompt: require model image support,
  2494. // mode to include the browser group, and the user setting to be enabled.
  2495. const modeConfig = getModeBySlug(mode ?? defaultModeSlug, customModes)
  2496. const modeSupportsBrowser = modeConfig?.groups.some((group) => getGroupName(group) === "browser") ?? false
  2497. // Check if model supports browser capability (images)
  2498. const modelInfo = this.api.getModel().info
  2499. const modelSupportsBrowser = (modelInfo as any)?.supportsImages === true
  2500. const canUseBrowserTool = modelSupportsBrowser && modeSupportsBrowser && (browserToolEnabled ?? true)
  2501. return SYSTEM_PROMPT(
  2502. provider.context,
  2503. this.cwd,
  2504. canUseBrowserTool,
  2505. mcpHub,
  2506. this.diffStrategy,
  2507. browserViewportSize ?? "900x600",
  2508. mode ?? defaultModeSlug,
  2509. customModePrompts,
  2510. customModes,
  2511. customInstructions,
  2512. this.diffEnabled,
  2513. experiments,
  2514. enableMcpServerCreation,
  2515. language,
  2516. rooIgnoreInstructions,
  2517. maxReadFileLine !== -1,
  2518. {
  2519. maxConcurrentFileReads: maxConcurrentFileReads ?? 5,
  2520. todoListEnabled: apiConfiguration?.todoListEnabled ?? true,
  2521. useAgentRules: vscode.workspace.getConfiguration("kilo-code").get<boolean>("useAgentRules") ?? true,
  2522. newTaskRequireTodos: vscode.workspace
  2523. .getConfiguration("kilo-code")
  2524. .get<boolean>("newTaskRequireTodos", false),
  2525. },
  2526. undefined, // todoList
  2527. this.api.getModel().id,
  2528. // kilocode_change start
  2529. getActiveToolUseStyle(apiConfiguration),
  2530. state,
  2531. // kilocode_change end
  2532. )
  2533. })()
  2534. }
  2535. private getCurrentProfileId(state: any): string {
  2536. return (
  2537. state?.listApiConfigMeta?.find((profile: any) => profile.name === state?.currentApiConfigName)?.id ??
  2538. "default"
  2539. )
  2540. }
  2541. private async handleContextWindowExceededError(): Promise<void> {
  2542. const state = await this.providerRef.deref()?.getState()
  2543. const { profileThresholds = {} } = state ?? {}
  2544. const { contextTokens } = this.getTokenUsage()
  2545. // kilocode_change start: Initialize virtual quota fallback handler
  2546. if (this.api instanceof VirtualQuotaFallbackHandler) {
  2547. await this.api.initialize()
  2548. }
  2549. // kilocode_change end
  2550. const modelInfo = this.api.getModel().info
  2551. const maxTokens = getModelMaxOutputTokens({
  2552. modelId: this.api.getModel().id,
  2553. model: modelInfo,
  2554. settings: this.apiConfiguration,
  2555. })
  2556. const contextWindow = this.api.contextWindow ?? modelInfo.contextWindow // kilocode_change: Use contextWindow from API handler if available
  2557. // Get the current profile ID using the helper method
  2558. const currentProfileId = this.getCurrentProfileId(state)
  2559. // Log the context window error for debugging
  2560. console.warn(
  2561. `[Task#${this.taskId}] Context window exceeded for model ${this.api.getModel().id}. ` +
  2562. `Current tokens: ${contextTokens}, Context window: ${contextWindow}. ` +
  2563. `Forcing truncation to ${FORCED_CONTEXT_REDUCTION_PERCENT}% of current context.`,
  2564. )
  2565. // Force aggressive truncation by keeping only 75% of the conversation history
  2566. const truncateResult = await truncateConversationIfNeeded({
  2567. messages: this.apiConversationHistory,
  2568. totalTokens: contextTokens || 0,
  2569. maxTokens,
  2570. contextWindow,
  2571. apiHandler: this.api,
  2572. autoCondenseContext: true,
  2573. autoCondenseContextPercent: FORCED_CONTEXT_REDUCTION_PERCENT,
  2574. systemPrompt: await this.getSystemPrompt(),
  2575. taskId: this.taskId,
  2576. profileThresholds,
  2577. currentProfileId,
  2578. })
  2579. if (truncateResult.messages !== this.apiConversationHistory) {
  2580. await this.overwriteApiConversationHistory(truncateResult.messages)
  2581. }
  2582. if (truncateResult.summary) {
  2583. const { summary, cost, prevContextTokens, newContextTokens = 0 } = truncateResult
  2584. const contextCondense: ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
  2585. await this.say(
  2586. "condense_context",
  2587. undefined /* text */,
  2588. undefined /* images */,
  2589. false /* partial */,
  2590. undefined /* checkpoint */,
  2591. undefined /* progressStatus */,
  2592. { isNonInteractive: true } /* options */,
  2593. contextCondense,
  2594. )
  2595. }
  2596. }
  2597. public async *attemptApiRequest(retryAttempt: number = 0): ApiStream {
  2598. const state = await this.providerRef.deref()?.getState()
  2599. const {
  2600. apiConfiguration,
  2601. autoApprovalEnabled,
  2602. alwaysApproveResubmit,
  2603. requestDelaySeconds,
  2604. mode,
  2605. autoCondenseContext = true,
  2606. autoCondenseContextPercent = 100,
  2607. profileThresholds = {},
  2608. } = state ?? {}
  2609. // Get condensing configuration for automatic triggers.
  2610. const customCondensingPrompt = state?.customCondensingPrompt
  2611. const condensingApiConfigId = state?.condensingApiConfigId
  2612. const listApiConfigMeta = state?.listApiConfigMeta
  2613. // Determine API handler to use for condensing.
  2614. let condensingApiHandler: ApiHandler | undefined
  2615. if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
  2616. // Find matching config by ID
  2617. const matchingConfig = listApiConfigMeta.find((config) => config.id === condensingApiConfigId)
  2618. if (matchingConfig) {
  2619. const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
  2620. id: condensingApiConfigId,
  2621. })
  2622. // Ensure profile and apiProvider exist before trying to build handler.
  2623. if (profile && profile.apiProvider) {
  2624. condensingApiHandler = buildApiHandler(profile)
  2625. }
  2626. }
  2627. }
  2628. let rateLimitDelay = 0
  2629. // Use the shared timestamp so that subtasks respect the same rate-limit
  2630. // window as their parent tasks.
  2631. if (Task.lastGlobalApiRequestTime) {
  2632. const now = performance.now()
  2633. const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
  2634. const rateLimit = apiConfiguration?.rateLimitSeconds || 0
  2635. rateLimitDelay = Math.ceil(Math.min(rateLimit, Math.max(0, rateLimit * 1000 - timeSinceLastRequest) / 1000))
  2636. }
  2637. // Only show rate limiting message if we're not retrying. If retrying, we'll include the delay there.
  2638. if (rateLimitDelay > 0 && retryAttempt === 0) {
  2639. // Show countdown timer
  2640. for (let i = rateLimitDelay; i > 0; i--) {
  2641. const delayMessage = `Rate limiting for ${i} seconds...`
  2642. await this.say("api_req_retry_delayed", delayMessage, undefined, true)
  2643. await delay(1000)
  2644. }
  2645. }
  2646. // Update last request time before making the request so that subsequent
  2647. // requests — even from new subtasks — will honour the provider's rate-limit.
  2648. Task.lastGlobalApiRequestTime = performance.now()
  2649. const systemPrompt = await this.getSystemPrompt()
  2650. this.lastUsedInstructions = systemPrompt
  2651. const { contextTokens } = this.getTokenUsage()
  2652. if (contextTokens) {
  2653. // kilocode_change start: Initialize and adjust virtual quota fallback handler
  2654. if (this.api instanceof VirtualQuotaFallbackHandler) {
  2655. await this.api.initialize()
  2656. await this.api.adjustActiveHandler("Pre-Request Adjustment")
  2657. }
  2658. // kilocode_change end
  2659. const modelInfo = this.api.getModel().info
  2660. const maxTokens = getModelMaxOutputTokens({
  2661. modelId: this.api.getModel().id,
  2662. model: modelInfo,
  2663. settings: this.apiConfiguration,
  2664. })
  2665. const contextWindow = this.api.contextWindow ?? modelInfo.contextWindow // kilocode_change
  2666. // Get the current profile ID using the helper method
  2667. const currentProfileId = this.getCurrentProfileId(state)
  2668. const truncateResult = await truncateConversationIfNeeded({
  2669. messages: this.apiConversationHistory,
  2670. totalTokens: contextTokens,
  2671. maxTokens,
  2672. contextWindow,
  2673. apiHandler: this.api,
  2674. autoCondenseContext,
  2675. autoCondenseContextPercent,
  2676. systemPrompt,
  2677. taskId: this.taskId,
  2678. customCondensingPrompt,
  2679. condensingApiHandler,
  2680. profileThresholds,
  2681. currentProfileId,
  2682. })
  2683. if (truncateResult.messages !== this.apiConversationHistory) {
  2684. await this.overwriteApiConversationHistory(truncateResult.messages)
  2685. }
  2686. if (truncateResult.error) {
  2687. await this.say("condense_context_error", truncateResult.error)
  2688. } else if (truncateResult.summary) {
  2689. // A condense operation occurred; for the next GPT‑5 API call we should NOT
  2690. // send previous_response_id so the request reflects the fresh condensed context.
  2691. this.skipPrevResponseIdOnce = true
  2692. const { summary, cost, prevContextTokens, newContextTokens = 0 } = truncateResult
  2693. const contextCondense: ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
  2694. await this.say(
  2695. "condense_context",
  2696. undefined /* text */,
  2697. undefined /* images */,
  2698. false /* partial */,
  2699. undefined /* checkpoint */,
  2700. undefined /* progressStatus */,
  2701. { isNonInteractive: true } /* options */,
  2702. contextCondense,
  2703. )
  2704. }
  2705. }
  2706. const messagesSinceLastSummary = getMessagesSinceLastSummary(this.apiConversationHistory)
  2707. let cleanConversationHistory = maybeRemoveReasoningDetails_kilocode(
  2708. maybeRemoveImageBlocks(messagesSinceLastSummary, this.api).map(({ role, content }) => ({ role, content })),
  2709. apiConfiguration?.apiProvider,
  2710. )
  2711. // kilocode_change start
  2712. // Fetch project properties for KiloCode provider tracking
  2713. const kiloConfig = this.providerRef.deref()?.getKiloConfig()
  2714. // kilocode_change end
  2715. // Check auto-approval limits
  2716. const approvalResult = await this.autoApprovalHandler.checkAutoApprovalLimits(
  2717. state,
  2718. this.combineMessages(this.clineMessages.slice(1)),
  2719. async (type, data) => this.ask(type, data),
  2720. )
  2721. if (!approvalResult.shouldProceed) {
  2722. // User did not approve, task should be aborted
  2723. throw new Error("Auto-approval limit reached and user did not approve continuation")
  2724. }
  2725. // Determine GPT‑5 previous_response_id from last persisted assistant turn (if available),
  2726. // unless a condense just occurred (skip once after condense).
  2727. let previousResponseId: string | undefined = undefined
  2728. try {
  2729. const modelId = this.api.getModel().id
  2730. if (modelId && modelId.startsWith("gpt-5") && !this.skipPrevResponseIdOnce) {
  2731. // Find the last assistant message that has a previous_response_id stored
  2732. const idx = findLastIndex(
  2733. this.clineMessages,
  2734. (m): m is ClineMessage & ClineMessageWithMetadata =>
  2735. m.type === "say" &&
  2736. m.say === "text" &&
  2737. !!(m as ClineMessageWithMetadata).metadata?.gpt5?.previous_response_id,
  2738. )
  2739. if (idx !== -1) {
  2740. // Use the previous_response_id from the last assistant message for this request
  2741. const message = this.clineMessages[idx] as ClineMessage & ClineMessageWithMetadata
  2742. previousResponseId = message.metadata?.gpt5?.previous_response_id
  2743. }
  2744. } else if (this.skipPrevResponseIdOnce) {
  2745. // Skipping previous_response_id due to recent condense operation - will send full conversation context
  2746. }
  2747. } catch (error) {
  2748. console.error(`[Task#${this.taskId}] Error retrieving GPT-5 response ID:`, error)
  2749. // non-fatal
  2750. }
  2751. const metadata: ApiHandlerCreateMessageMetadata = {
  2752. mode: mode,
  2753. taskId: this.taskId,
  2754. // Only include previousResponseId if we're NOT suppressing it
  2755. ...(previousResponseId && !this.skipPrevResponseIdOnce ? { previousResponseId } : {}),
  2756. // If a condense just occurred, explicitly suppress continuity fallback for the next call
  2757. ...(this.skipPrevResponseIdOnce ? { suppressPreviousResponseId: true } : {}),
  2758. // kilocode_change start
  2759. // KiloCode-specific: pass projectId for backend tracking (ignored by other providers)
  2760. projectId: (await kiloConfig)?.project?.id,
  2761. // kilocode_change end
  2762. }
  2763. // kilocode_change start
  2764. // Add allowed tools for JSON tool style
  2765. if (getActiveToolUseStyle(apiConfiguration) === "json" && mode) {
  2766. try {
  2767. const provider = this.providerRef.deref()
  2768. metadata.allowedTools = await getAllowedJSONToolsForMode(
  2769. mode,
  2770. provider,
  2771. this.diffEnabled,
  2772. this.api?.getModel(),
  2773. )
  2774. } catch (error) {
  2775. console.error("[Task] Error getting allowed tools for mode:", error)
  2776. // Continue without allowedTools - will fall back to default behavior
  2777. }
  2778. }
  2779. // kilocode_change end
  2780. // Reset skip flag after applying (it only affects the immediate next call)
  2781. if (this.skipPrevResponseIdOnce) {
  2782. this.skipPrevResponseIdOnce = false
  2783. }
  2784. const stream = this.api.createMessage(systemPrompt, cleanConversationHistory, metadata)
  2785. const iterator = stream[Symbol.asyncIterator]()
  2786. try {
  2787. // Awaiting first chunk to see if it will throw an error.
  2788. this.isWaitingForFirstChunk = true
  2789. const firstChunk = await iterator.next()
  2790. yield firstChunk.value
  2791. this.isWaitingForFirstChunk = false
  2792. } catch (error) {
  2793. this.isWaitingForFirstChunk = false
  2794. // kilocode_change start
  2795. if (apiConfiguration?.apiProvider === "kilocode" && isAnyRecognizedKiloCodeError(error)) {
  2796. const { response } = await (isPaymentRequiredError(error)
  2797. ? this.ask(
  2798. "payment_required_prompt",
  2799. JSON.stringify({
  2800. title: error.error?.title ?? t("kilocode:lowCreditWarning.title"),
  2801. message: error.error?.message ?? t("kilocode:lowCreditWarning.message"),
  2802. balance: error.error?.balance ?? "0.00",
  2803. buyCreditsUrl: error.error?.buyCreditsUrl ?? getAppUrl("/profile"),
  2804. }),
  2805. )
  2806. : this.ask(
  2807. "invalid_model",
  2808. JSON.stringify({
  2809. modelId: apiConfiguration.kilocodeModel,
  2810. error: {
  2811. status: error.status,
  2812. message: error.message,
  2813. },
  2814. }),
  2815. ))
  2816. if (response === "retry_clicked") {
  2817. yield* this.attemptApiRequest(retryAttempt + 1)
  2818. } else {
  2819. // Handle other responses or cancellations if necessary
  2820. // If the user cancels the dialog, we should probably abort.
  2821. throw error // Rethrow to signal failure upwards
  2822. }
  2823. return
  2824. }
  2825. // kilocode_change end
  2826. // 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.
  2827. if (autoApprovalEnabled && alwaysApproveResubmit) {
  2828. let errorMsg
  2829. if (error.error?.metadata?.raw) {
  2830. errorMsg = JSON.stringify(error.error.metadata.raw, null, 2)
  2831. } else if (error.message) {
  2832. errorMsg = error.message
  2833. } else {
  2834. errorMsg = "Unknown error"
  2835. }
  2836. // Apply shared exponential backoff and countdown UX
  2837. await this.backoffAndAnnounce(retryAttempt, error, errorMsg)
  2838. // CRITICAL: Check if task was aborted during the backoff countdown
  2839. // This prevents infinite loops when users cancel during auto-retry
  2840. // Without this check, the recursive call below would continue even after abort
  2841. if (this.abort) {
  2842. throw new Error(
  2843. `[Task#attemptApiRequest] task ${this.taskId}.${this.instanceId} aborted during retry`,
  2844. )
  2845. }
  2846. // Delegate generator output from the recursive call with
  2847. // incremented retry count.
  2848. yield* this.attemptApiRequest(retryAttempt + 1)
  2849. return
  2850. } else {
  2851. const { response } = await this.ask(
  2852. "api_req_failed",
  2853. error.message ?? JSON.stringify(serializeError(error), null, 2),
  2854. )
  2855. if (response !== "yesButtonClicked") {
  2856. // This will never happen since if noButtonClicked, we will
  2857. // clear current task, aborting this instance.
  2858. throw new Error("API request failed")
  2859. }
  2860. await this.say("api_req_retried")
  2861. // Delegate generator output from the recursive call.
  2862. yield* this.attemptApiRequest()
  2863. return
  2864. }
  2865. }
  2866. // No error, so we can continue to yield all remaining chunks.
  2867. // (Needs to be placed outside of try/catch since it we want caller to
  2868. // handle errors not with api_req_failed as that is reserved for first
  2869. // chunk failures only.)
  2870. // This delegates to another generator or iterable object. In this case,
  2871. // it's saying "yield all remaining values from this iterator". This
  2872. // effectively passes along all subsequent chunks from the original
  2873. // stream.
  2874. yield* iterator
  2875. // kilocode_change start
  2876. if (apiConfiguration?.rateLimitAfter) {
  2877. Task.lastGlobalApiRequestTime = performance.now()
  2878. }
  2879. // kilocode_change end
  2880. }
  2881. // Shared exponential backoff for retries (first-chunk and mid-stream)
  2882. private async backoffAndAnnounce(retryAttempt: number, error: any, header?: string): Promise<void> {
  2883. try {
  2884. const state = await this.providerRef.deref()?.getState()
  2885. const baseDelay = state?.requestDelaySeconds || 5
  2886. let exponentialDelay = Math.min(
  2887. Math.ceil(baseDelay * Math.pow(2, retryAttempt)),
  2888. MAX_EXPONENTIAL_BACKOFF_SECONDS,
  2889. )
  2890. // Respect provider rate limit window
  2891. let rateLimitDelay = 0
  2892. const rateLimit = state?.apiConfiguration?.rateLimitSeconds || 0
  2893. if (Task.lastGlobalApiRequestTime && rateLimit > 0) {
  2894. const elapsed = performance.now() - Task.lastGlobalApiRequestTime
  2895. rateLimitDelay = Math.ceil(Math.min(rateLimit, Math.max(0, rateLimit * 1000 - elapsed) / 1000))
  2896. }
  2897. // Prefer RetryInfo on 429 if present
  2898. if (error?.status === 429) {
  2899. const retryInfo = error?.errorDetails?.find(
  2900. (d: any) => d["@type"] === "type.googleapis.com/google.rpc.RetryInfo",
  2901. )
  2902. const match = retryInfo?.retryDelay?.match?.(/^(\d+)s$/)
  2903. if (match) {
  2904. exponentialDelay = Number(match[1]) + 1
  2905. }
  2906. }
  2907. const finalDelay = Math.max(exponentialDelay, rateLimitDelay)
  2908. if (finalDelay <= 0) return
  2909. // Build header text; fall back to error message if none provided
  2910. let headerText = header
  2911. if (!headerText) {
  2912. if (error?.error?.metadata?.raw) {
  2913. headerText = JSON.stringify(error.error.metadata.raw, null, 2)
  2914. } else if (error?.message) {
  2915. headerText = error.message
  2916. } else {
  2917. headerText = "Unknown error"
  2918. }
  2919. }
  2920. headerText = headerText ? `${headerText}\n\n` : ""
  2921. // Show countdown timer with exponential backoff
  2922. for (let i = finalDelay; i > 0; i--) {
  2923. // Check abort flag during countdown to allow early exit
  2924. if (this.abort) {
  2925. throw new Error(`[Task#${this.taskId}] Aborted during retry countdown`)
  2926. }
  2927. await this.say(
  2928. "api_req_retry_delayed",
  2929. `${headerText}Retry attempt ${retryAttempt + 1}\nRetrying in ${i} seconds...`,
  2930. undefined,
  2931. true,
  2932. )
  2933. await delay(1000)
  2934. }
  2935. await this.say(
  2936. "api_req_retry_delayed",
  2937. `${headerText}Retry attempt ${retryAttempt + 1}\nRetrying now...`,
  2938. undefined,
  2939. false,
  2940. )
  2941. } catch (err) {
  2942. console.error("Exponential backoff failed:", err)
  2943. }
  2944. }
  2945. // Checkpoints
  2946. public async checkpointSave(force: boolean = false, suppressMessage: boolean = false) {
  2947. return checkpointSave(this, force, suppressMessage)
  2948. }
  2949. public async checkpointRestore(options: CheckpointRestoreOptions) {
  2950. return checkpointRestore(this, options)
  2951. }
  2952. public async checkpointDiff(options: CheckpointDiffOptions) {
  2953. return checkpointDiff(this, options)
  2954. }
  2955. // Metrics
  2956. public combineMessages(messages: ClineMessage[]) {
  2957. return combineApiRequests(combineCommandSequences(messages))
  2958. }
  2959. public getTokenUsage(): TokenUsage {
  2960. return getApiMetrics(this.combineMessages(this.clineMessages.slice(1)))
  2961. }
  2962. public recordToolUsage(toolName: ToolName) {
  2963. if (!this.toolUsage[toolName]) {
  2964. this.toolUsage[toolName] = { attempts: 0, failures: 0 }
  2965. }
  2966. this.toolUsage[toolName].attempts++
  2967. }
  2968. public recordToolError(toolName: ToolName, error?: string) {
  2969. if (!this.toolUsage[toolName]) {
  2970. this.toolUsage[toolName] = { attempts: 0, failures: 0 }
  2971. }
  2972. this.toolUsage[toolName].failures++
  2973. if (error) {
  2974. this.emit(RooCodeEventName.TaskToolFailed, this.taskId, toolName, error)
  2975. }
  2976. TelemetryService.instance.captureEvent(TelemetryEventName.TOOL_ERROR, { toolName, error }) // kilocode_change
  2977. }
  2978. /**
  2979. * Persist GPT-5 per-turn metadata (previous_response_id only)
  2980. * onto the last complete assistant say("text") message.
  2981. *
  2982. * Note: We do not persist system instructions or reasoning summaries.
  2983. */
  2984. private async persistGpt5Metadata(): Promise<void> {
  2985. try {
  2986. const modelId = this.api.getModel().id
  2987. if (!modelId || !modelId.startsWith("gpt-5")) return
  2988. // Check if the API handler has a getLastResponseId method (OpenAiNativeHandler specific)
  2989. const handler = this.api as ApiHandler & { getLastResponseId?: () => string | undefined }
  2990. const lastResponseId = handler.getLastResponseId?.()
  2991. const idx = findLastIndex(
  2992. this.clineMessages,
  2993. (m) => m.type === "say" && m.say === "text" && m.partial !== true,
  2994. )
  2995. if (idx !== -1) {
  2996. const msg = this.clineMessages[idx] as ClineMessage & ClineMessageWithMetadata
  2997. if (!msg.metadata) {
  2998. msg.metadata = {}
  2999. }
  3000. const gpt5Metadata: Gpt5Metadata = {
  3001. ...(msg.metadata.gpt5 ?? {}),
  3002. ...(lastResponseId ? { previous_response_id: lastResponseId } : {}),
  3003. }
  3004. msg.metadata.gpt5 = gpt5Metadata
  3005. }
  3006. } catch (error) {
  3007. console.error(`[Task#${this.taskId}] Error persisting GPT-5 metadata:`, error)
  3008. // Non-fatal error in metadata persistence
  3009. }
  3010. }
  3011. // Getters
  3012. public get taskStatus(): TaskStatus {
  3013. if (this.interactiveAsk) {
  3014. return TaskStatus.Interactive
  3015. }
  3016. if (this.resumableAsk) {
  3017. return TaskStatus.Resumable
  3018. }
  3019. if (this.idleAsk) {
  3020. return TaskStatus.Idle
  3021. }
  3022. return TaskStatus.Running
  3023. }
  3024. public get taskAsk(): ClineMessage | undefined {
  3025. return this.idleAsk || this.resumableAsk || this.interactiveAsk
  3026. }
  3027. public get queuedMessages(): QueuedMessage[] {
  3028. return this.messageQueueService.messages
  3029. }
  3030. public get tokenUsage(): TokenUsage | undefined {
  3031. if (this.tokenUsageSnapshot && this.tokenUsageSnapshotAt) {
  3032. return this.tokenUsageSnapshot
  3033. }
  3034. this.tokenUsageSnapshot = this.getTokenUsage()
  3035. this.tokenUsageSnapshotAt = this.clineMessages.at(-1)?.ts
  3036. return this.tokenUsageSnapshot
  3037. }
  3038. public get cwd() {
  3039. return this.workspacePath
  3040. }
  3041. /**
  3042. * Process any queued messages by dequeuing and submitting them.
  3043. * This ensures that queued user messages are sent when appropriate,
  3044. * preventing them from getting stuck in the queue.
  3045. *
  3046. * @param context - Context string for logging (e.g., the calling tool name)
  3047. */
  3048. public processQueuedMessages(): void {
  3049. try {
  3050. if (!this.messageQueueService.isEmpty()) {
  3051. const queued = this.messageQueueService.dequeueMessage()
  3052. if (queued) {
  3053. setTimeout(() => {
  3054. this.submitUserMessage(queued.text, queued.images).catch((err) =>
  3055. console.error(`[Task] Failed to submit queued message:`, err),
  3056. )
  3057. }, 0)
  3058. }
  3059. }
  3060. } catch (e) {
  3061. console.error(`[Task] Queue processing error:`, e)
  3062. }
  3063. }
  3064. }