Task.ts 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632
  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 { AskIgnoredError } from "./AskIgnoredError"
  7. import { Anthropic } from "@anthropic-ai/sdk"
  8. import OpenAI from "openai"
  9. import debounce from "lodash.debounce"
  10. import delay from "delay"
  11. import pWaitFor from "p-wait-for"
  12. import { serializeError } from "serialize-error"
  13. import { Package } from "../../shared/package"
  14. import { formatToolInvocation } from "../tools/helpers/toolResultFormatting"
  15. import {
  16. type TaskLike,
  17. type TaskMetadata,
  18. type TaskEvents,
  19. type ProviderSettings,
  20. type TokenUsage,
  21. type ToolUsage,
  22. type ToolName,
  23. type ContextCondense,
  24. type ContextTruncation,
  25. type ClineMessage,
  26. type ClineSay,
  27. type ClineAsk,
  28. type ToolProgressStatus,
  29. type HistoryItem,
  30. type CreateTaskOptions,
  31. type ModelInfo,
  32. type ToolProtocol,
  33. type ClineApiReqCancelReason,
  34. type ClineApiReqInfo,
  35. RooCodeEventName,
  36. TelemetryEventName,
  37. TaskStatus,
  38. TodoItem,
  39. getApiProtocol,
  40. getModelId,
  41. isIdleAsk,
  42. isInteractiveAsk,
  43. isResumableAsk,
  44. isNativeProtocol,
  45. QueuedMessage,
  46. DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
  47. DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
  48. MAX_CHECKPOINT_TIMEOUT_SECONDS,
  49. MIN_CHECKPOINT_TIMEOUT_SECONDS,
  50. TOOL_PROTOCOL,
  51. ConsecutiveMistakeError,
  52. } from "@roo-code/types"
  53. import { TelemetryService } from "@roo-code/telemetry"
  54. import { CloudService, BridgeOrchestrator } from "@roo-code/cloud"
  55. import { resolveToolProtocol, detectToolProtocolFromHistory } from "../../utils/resolveToolProtocol"
  56. // api
  57. import { ApiHandler, ApiHandlerCreateMessageMetadata, buildApiHandler } from "../../api"
  58. import { ApiStream, GroundingSource } from "../../api/transform/stream"
  59. import { maybeRemoveImageBlocks } from "../../api/transform/image-cleaning"
  60. // shared
  61. import { findLastIndex } from "../../shared/array"
  62. import { combineApiRequests } from "../../shared/combineApiRequests"
  63. import { combineCommandSequences } from "../../shared/combineCommandSequences"
  64. import { t } from "../../i18n"
  65. import { getApiMetrics, hasTokenUsageChanged, hasToolUsageChanged } from "../../shared/getApiMetrics"
  66. import { ClineAskResponse } from "../../shared/WebviewMessage"
  67. import { defaultModeSlug, getModeBySlug, getGroupName } from "../../shared/modes"
  68. import { DiffStrategy, type ToolUse, type ToolParamName, toolParamNames } from "../../shared/tools"
  69. import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
  70. import { getModelMaxOutputTokens } from "../../shared/api"
  71. // services
  72. import { UrlContentFetcher } from "../../services/browser/UrlContentFetcher"
  73. import { BrowserSession } from "../../services/browser/BrowserSession"
  74. import { McpHub } from "../../services/mcp/McpHub"
  75. import { McpServerManager } from "../../services/mcp/McpServerManager"
  76. import { RepoPerTaskCheckpointService } from "../../services/checkpoints"
  77. // integrations
  78. import { DiffViewProvider } from "../../integrations/editor/DiffViewProvider"
  79. import { findToolName } from "../../integrations/misc/export-markdown"
  80. import { RooTerminalProcess } from "../../integrations/terminal/types"
  81. import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry"
  82. // utils
  83. import { calculateApiCostAnthropic, calculateApiCostOpenAI } from "../../shared/cost"
  84. import { getWorkspacePath } from "../../utils/path"
  85. // prompts
  86. import { formatResponse } from "../prompts/responses"
  87. import { SYSTEM_PROMPT } from "../prompts/system"
  88. import { buildNativeToolsArray } from "./build-tools"
  89. // core modules
  90. import { ToolRepetitionDetector } from "../tools/ToolRepetitionDetector"
  91. import { restoreTodoListForTask } from "../tools/UpdateTodoListTool"
  92. import { FileContextTracker } from "../context-tracking/FileContextTracker"
  93. import { RooIgnoreController } from "../ignore/RooIgnoreController"
  94. import { RooProtectedController } from "../protect/RooProtectedController"
  95. import { type AssistantMessageContent, presentAssistantMessage } from "../assistant-message"
  96. import { AssistantMessageParser } from "../assistant-message/AssistantMessageParser"
  97. import { NativeToolCallParser } from "../assistant-message/NativeToolCallParser"
  98. import { manageContext, willManageContext } from "../context-management"
  99. import { ClineProvider } from "../webview/ClineProvider"
  100. import { MultiSearchReplaceDiffStrategy } from "../diff/strategies/multi-search-replace"
  101. import { MultiFileSearchReplaceDiffStrategy } from "../diff/strategies/multi-file-search-replace"
  102. import {
  103. type ApiMessage,
  104. readApiMessages,
  105. saveApiMessages,
  106. readTaskMessages,
  107. saveTaskMessages,
  108. taskMetadata,
  109. } from "../task-persistence"
  110. import { getEnvironmentDetails } from "../environment/getEnvironmentDetails"
  111. import { checkContextWindowExceededError } from "../context/context-management/context-error-handling"
  112. import {
  113. type CheckpointDiffOptions,
  114. type CheckpointRestoreOptions,
  115. getCheckpointService,
  116. checkpointSave,
  117. checkpointRestore,
  118. checkpointDiff,
  119. } from "../checkpoints"
  120. import { processUserContentMentions } from "../mentions/processUserContentMentions"
  121. import { getMessagesSinceLastSummary, summarizeConversation, getEffectiveApiHistory } from "../condense"
  122. import { MessageQueueService } from "../message-queue/MessageQueueService"
  123. import { AutoApprovalHandler, checkAutoApproval } from "../auto-approval"
  124. import { MessageManager } from "../message-manager"
  125. import { validateAndFixToolResultIds } from "./validateToolResultIds"
  126. const MAX_EXPONENTIAL_BACKOFF_SECONDS = 600 // 10 minutes
  127. const DEFAULT_USAGE_COLLECTION_TIMEOUT_MS = 5000 // 5 seconds
  128. const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Keep 75% of context (remove 25%) on context window errors
  129. const MAX_CONTEXT_WINDOW_RETRIES = 3 // Maximum retries for context window errors
  130. export interface TaskOptions extends CreateTaskOptions {
  131. provider: ClineProvider
  132. apiConfiguration: ProviderSettings
  133. enableDiff?: boolean
  134. enableCheckpoints?: boolean
  135. checkpointTimeout?: number
  136. enableBridge?: boolean
  137. fuzzyMatchThreshold?: number
  138. consecutiveMistakeLimit?: number
  139. task?: string
  140. images?: string[]
  141. historyItem?: HistoryItem
  142. experiments?: Record<string, boolean>
  143. startTask?: boolean
  144. rootTask?: Task
  145. parentTask?: Task
  146. taskNumber?: number
  147. onCreated?: (task: Task) => void
  148. initialTodos?: TodoItem[]
  149. workspacePath?: string
  150. /** Initial status for the task's history item (e.g., "active" for child tasks) */
  151. initialStatus?: "active" | "delegated" | "completed"
  152. }
  153. export class Task extends EventEmitter<TaskEvents> implements TaskLike {
  154. readonly taskId: string
  155. readonly rootTaskId?: string
  156. readonly parentTaskId?: string
  157. childTaskId?: string
  158. pendingNewTaskToolCallId?: string
  159. readonly instanceId: string
  160. readonly metadata: TaskMetadata
  161. todoList?: TodoItem[]
  162. readonly rootTask: Task | undefined = undefined
  163. readonly parentTask: Task | undefined = undefined
  164. readonly taskNumber: number
  165. readonly workspacePath: string
  166. /**
  167. * The mode associated with this task. Persisted across sessions
  168. * to maintain user context when reopening tasks from history.
  169. *
  170. * ## Lifecycle
  171. *
  172. * ### For new tasks:
  173. * 1. Initially `undefined` during construction
  174. * 2. Asynchronously initialized from provider state via `initializeTaskMode()`
  175. * 3. Falls back to `defaultModeSlug` if provider state is unavailable
  176. *
  177. * ### For history items:
  178. * 1. Immediately set from `historyItem.mode` during construction
  179. * 2. Falls back to `defaultModeSlug` if mode is not stored in history
  180. *
  181. * ## Important
  182. * This property should NOT be accessed directly until `taskModeReady` promise resolves.
  183. * Use `getTaskMode()` for async access or `taskMode` getter for sync access after initialization.
  184. *
  185. * @private
  186. * @see {@link getTaskMode} - For safe async access
  187. * @see {@link taskMode} - For sync access after initialization
  188. * @see {@link waitForModeInitialization} - To ensure initialization is complete
  189. */
  190. private _taskMode: string | undefined
  191. /**
  192. * The tool protocol locked to this task. Once set, the task will continue
  193. * using this protocol even if user settings change.
  194. *
  195. * ## Why This Matters
  196. * When NTC (Native Tool Calling) is enabled, XML parsing does NOT occur.
  197. * If a task previously used XML tools, resuming it with NTC enabled would
  198. * break because the tool calls in the history would not be parseable.
  199. *
  200. * ## Lifecycle
  201. *
  202. * ### For new tasks:
  203. * 1. Set immediately in constructor via `resolveToolProtocol()`
  204. * 2. Locked for the lifetime of the task
  205. *
  206. * ### For history items:
  207. * 1. If `historyItem.toolProtocol` exists, use it
  208. * 2. Otherwise, detect from API history via `detectToolProtocolFromHistory()`
  209. * 3. If no tools in history, use `resolveToolProtocol()` from current settings
  210. *
  211. * @private
  212. */
  213. private _taskToolProtocol: ToolProtocol | undefined
  214. /**
  215. * Promise that resolves when the task mode has been initialized.
  216. * This ensures async mode initialization completes before the task is used.
  217. *
  218. * ## Purpose
  219. * - Prevents race conditions when accessing task mode
  220. * - Ensures provider state is properly loaded before mode-dependent operations
  221. * - Provides a synchronization point for async initialization
  222. *
  223. * ## Resolution timing
  224. * - For history items: Resolves immediately (sync initialization)
  225. * - For new tasks: Resolves after provider state is fetched (async initialization)
  226. *
  227. * @private
  228. * @see {@link waitForModeInitialization} - Public method to await this promise
  229. */
  230. private taskModeReady: Promise<void>
  231. /**
  232. * The API configuration name (provider profile) associated with this task.
  233. * Persisted across sessions to maintain the provider profile when reopening tasks from history.
  234. *
  235. * ## Lifecycle
  236. *
  237. * ### For new tasks:
  238. * 1. Initially `undefined` during construction
  239. * 2. Asynchronously initialized from provider state via `initializeTaskApiConfigName()`
  240. * 3. Falls back to "default" if provider state is unavailable
  241. *
  242. * ### For history items:
  243. * 1. Immediately set from `historyItem.apiConfigName` during construction
  244. * 2. Falls back to undefined if not stored in history (for backward compatibility)
  245. *
  246. * ## Important
  247. * If you need a non-`undefined` provider profile (e.g., for profile-dependent operations),
  248. * wait for `taskApiConfigReady` first (or use `getTaskApiConfigName()`).
  249. * The sync `taskApiConfigName` getter may return `undefined` for backward compatibility.
  250. *
  251. * @private
  252. * @see {@link getTaskApiConfigName} - For safe async access
  253. * @see {@link taskApiConfigName} - For sync access after initialization
  254. */
  255. private _taskApiConfigName: string | undefined
  256. /**
  257. * Promise that resolves when the task API config name has been initialized.
  258. * This ensures async API config name initialization completes before the task is used.
  259. *
  260. * ## Purpose
  261. * - Prevents race conditions when accessing task API config name
  262. * - Ensures provider state is properly loaded before profile-dependent operations
  263. * - Provides a synchronization point for async initialization
  264. *
  265. * ## Resolution timing
  266. * - For history items: Resolves immediately (sync initialization)
  267. * - For new tasks: Resolves after provider state is fetched (async initialization)
  268. *
  269. * @private
  270. */
  271. private taskApiConfigReady: Promise<void>
  272. providerRef: WeakRef<ClineProvider>
  273. private readonly globalStoragePath: string
  274. abort: boolean = false
  275. currentRequestAbortController?: AbortController
  276. skipPrevResponseIdOnce: boolean = false
  277. // TaskStatus
  278. idleAsk?: ClineMessage
  279. resumableAsk?: ClineMessage
  280. interactiveAsk?: ClineMessage
  281. didFinishAbortingStream = false
  282. abandoned = false
  283. abortReason?: ClineApiReqCancelReason
  284. isInitialized = false
  285. isPaused: boolean = false
  286. // API
  287. apiConfiguration: ProviderSettings
  288. api: ApiHandler
  289. private static lastGlobalApiRequestTime?: number
  290. private autoApprovalHandler: AutoApprovalHandler
  291. /**
  292. * Reset the global API request timestamp. This should only be used for testing.
  293. * @internal
  294. */
  295. static resetGlobalApiRequestTime(): void {
  296. Task.lastGlobalApiRequestTime = undefined
  297. }
  298. toolRepetitionDetector: ToolRepetitionDetector
  299. rooIgnoreController?: RooIgnoreController
  300. rooProtectedController?: RooProtectedController
  301. fileContextTracker: FileContextTracker
  302. urlContentFetcher: UrlContentFetcher
  303. terminalProcess?: RooTerminalProcess
  304. // Computer User
  305. browserSession: BrowserSession
  306. // Editing
  307. diffViewProvider: DiffViewProvider
  308. diffStrategy?: DiffStrategy
  309. diffEnabled: boolean = false
  310. fuzzyMatchThreshold: number
  311. didEditFile: boolean = false
  312. // LLM Messages & Chat Messages
  313. apiConversationHistory: ApiMessage[] = []
  314. clineMessages: ClineMessage[] = []
  315. // Ask
  316. private askResponse?: ClineAskResponse
  317. private askResponseText?: string
  318. private askResponseImages?: string[]
  319. public lastMessageTs?: number
  320. private autoApprovalTimeoutRef?: NodeJS.Timeout
  321. // Tool Use
  322. consecutiveMistakeCount: number = 0
  323. consecutiveMistakeLimit: number
  324. consecutiveMistakeCountForApplyDiff: Map<string, number> = new Map()
  325. consecutiveNoToolUseCount: number = 0
  326. consecutiveNoAssistantMessagesCount: number = 0
  327. toolUsage: ToolUsage = {}
  328. // Checkpoints
  329. enableCheckpoints: boolean
  330. checkpointTimeout: number
  331. checkpointService?: RepoPerTaskCheckpointService
  332. checkpointServiceInitializing = false
  333. // Task Bridge
  334. enableBridge: boolean
  335. // Message Queue Service
  336. public readonly messageQueueService: MessageQueueService
  337. private messageQueueStateChangedHandler: (() => void) | undefined
  338. // Streaming
  339. isWaitingForFirstChunk = false
  340. isStreaming = false
  341. currentStreamingContentIndex = 0
  342. currentStreamingDidCheckpoint = false
  343. assistantMessageContent: AssistantMessageContent[] = []
  344. presentAssistantMessageLocked = false
  345. presentAssistantMessageHasPendingUpdates = false
  346. userMessageContent: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam | Anthropic.ToolResultBlockParam)[] = []
  347. userMessageContentReady = false
  348. /**
  349. * Push a tool_result block to userMessageContent, preventing duplicates.
  350. * This is critical for native tool protocol where duplicate tool_use_ids cause API errors.
  351. *
  352. * @param toolResult - The tool_result block to add
  353. * @returns true if added, false if duplicate was skipped
  354. */
  355. public pushToolResultToUserContent(toolResult: Anthropic.ToolResultBlockParam): boolean {
  356. const existingResult = this.userMessageContent.find(
  357. (block): block is Anthropic.ToolResultBlockParam =>
  358. block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id,
  359. )
  360. if (existingResult) {
  361. console.warn(
  362. `[Task#pushToolResultToUserContent] Skipping duplicate tool_result for tool_use_id: ${toolResult.tool_use_id}`,
  363. )
  364. return false
  365. }
  366. this.userMessageContent.push(toolResult)
  367. return true
  368. }
  369. didRejectTool = false
  370. didAlreadyUseTool = false
  371. didToolFailInCurrentTurn = false
  372. didCompleteReadingStream = false
  373. assistantMessageParser?: AssistantMessageParser
  374. private providerProfileChangeListener?: (config: { name: string; provider?: string }) => void
  375. // Native tool call streaming state (track which index each tool is at)
  376. private streamingToolCallIndices: Map<string, number> = new Map()
  377. // Cached model info for current streaming session (set at start of each API request)
  378. // This prevents excessive getModel() calls during tool execution
  379. cachedStreamingModel?: { id: string; info: ModelInfo }
  380. // Token Usage Cache
  381. private tokenUsageSnapshot?: TokenUsage
  382. private tokenUsageSnapshotAt?: number
  383. // Tool Usage Cache
  384. private toolUsageSnapshot?: ToolUsage
  385. // Token Usage Throttling - Debounced emit function
  386. private readonly TOKEN_USAGE_EMIT_INTERVAL_MS = 2000 // 2 seconds
  387. private debouncedEmitTokenUsage: ReturnType<typeof debounce>
  388. // Cloud Sync Tracking
  389. private cloudSyncedMessageTimestamps: Set<number> = new Set()
  390. // Initial status for the task's history item (set at creation time to avoid race conditions)
  391. private readonly initialStatus?: "active" | "delegated" | "completed"
  392. // MessageManager for high-level message operations (lazy initialized)
  393. private _messageManager?: MessageManager
  394. constructor({
  395. provider,
  396. apiConfiguration,
  397. enableDiff = false,
  398. enableCheckpoints = true,
  399. checkpointTimeout = DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
  400. enableBridge = false,
  401. fuzzyMatchThreshold = 1.0,
  402. consecutiveMistakeLimit = DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
  403. task,
  404. images,
  405. historyItem,
  406. experiments: experimentsConfig,
  407. startTask = true,
  408. rootTask,
  409. parentTask,
  410. taskNumber = -1,
  411. onCreated,
  412. initialTodos,
  413. workspacePath,
  414. initialStatus,
  415. }: TaskOptions) {
  416. super()
  417. if (startTask && !task && !images && !historyItem) {
  418. throw new Error("Either historyItem or task/images must be provided")
  419. }
  420. if (
  421. !checkpointTimeout ||
  422. checkpointTimeout > MAX_CHECKPOINT_TIMEOUT_SECONDS ||
  423. checkpointTimeout < MIN_CHECKPOINT_TIMEOUT_SECONDS
  424. ) {
  425. throw new Error(
  426. "checkpointTimeout must be between " +
  427. MIN_CHECKPOINT_TIMEOUT_SECONDS +
  428. " and " +
  429. MAX_CHECKPOINT_TIMEOUT_SECONDS +
  430. " seconds",
  431. )
  432. }
  433. this.taskId = historyItem ? historyItem.id : crypto.randomUUID()
  434. this.rootTaskId = historyItem ? historyItem.rootTaskId : rootTask?.taskId
  435. this.parentTaskId = historyItem ? historyItem.parentTaskId : parentTask?.taskId
  436. this.childTaskId = undefined
  437. this.metadata = {
  438. task: historyItem ? historyItem.task : task,
  439. images: historyItem ? [] : images,
  440. }
  441. // Normal use-case is usually retry similar history task with new workspace.
  442. this.workspacePath = parentTask
  443. ? parentTask.workspacePath
  444. : (workspacePath ?? getWorkspacePath(path.join(os.homedir(), "Desktop")))
  445. this.instanceId = crypto.randomUUID().slice(0, 8)
  446. this.taskNumber = -1
  447. this.rooIgnoreController = new RooIgnoreController(this.cwd)
  448. this.rooProtectedController = new RooProtectedController(this.cwd)
  449. this.fileContextTracker = new FileContextTracker(provider, this.taskId)
  450. this.rooIgnoreController.initialize().catch((error) => {
  451. console.error("Failed to initialize RooIgnoreController:", error)
  452. })
  453. this.apiConfiguration = apiConfiguration
  454. this.api = buildApiHandler(apiConfiguration)
  455. this.autoApprovalHandler = new AutoApprovalHandler()
  456. this.urlContentFetcher = new UrlContentFetcher(provider.context)
  457. this.browserSession = new BrowserSession(provider.context, (isActive: boolean) => {
  458. // Add a message to indicate browser session status change
  459. this.say("browser_session_status", isActive ? "Browser session opened" : "Browser session closed")
  460. // Broadcast to browser panel
  461. this.broadcastBrowserSessionUpdate()
  462. // When a browser session becomes active, automatically open/reveal the Browser Session tab
  463. if (isActive) {
  464. try {
  465. // Lazy-load to avoid circular imports at module load time
  466. const { BrowserSessionPanelManager } = require("../webview/BrowserSessionPanelManager")
  467. const providerRef = this.providerRef.deref()
  468. if (providerRef) {
  469. BrowserSessionPanelManager.getInstance(providerRef)
  470. .show()
  471. .catch(() => {})
  472. }
  473. } catch (err) {
  474. console.error("[Task] Failed to auto-open Browser Session panel:", err)
  475. }
  476. }
  477. })
  478. this.diffEnabled = enableDiff
  479. this.fuzzyMatchThreshold = fuzzyMatchThreshold
  480. this.consecutiveMistakeLimit = consecutiveMistakeLimit ?? DEFAULT_CONSECUTIVE_MISTAKE_LIMIT
  481. this.providerRef = new WeakRef(provider)
  482. this.globalStoragePath = provider.context.globalStorageUri.fsPath
  483. this.diffViewProvider = new DiffViewProvider(this.cwd, this)
  484. this.enableCheckpoints = enableCheckpoints
  485. this.checkpointTimeout = checkpointTimeout
  486. this.enableBridge = enableBridge
  487. this.parentTask = parentTask
  488. this.taskNumber = taskNumber
  489. this.initialStatus = initialStatus
  490. // Store the task's mode and API config name when it's created.
  491. // For history items, use the stored values; for new tasks, we'll set them
  492. // after getting state.
  493. if (historyItem) {
  494. this._taskMode = historyItem.mode || defaultModeSlug
  495. this._taskApiConfigName = historyItem.apiConfigName
  496. this.taskModeReady = Promise.resolve()
  497. this.taskApiConfigReady = Promise.resolve()
  498. TelemetryService.instance.captureTaskRestarted(this.taskId)
  499. // For history items, use the persisted tool protocol if available.
  500. // If not available (old tasks), it will be detected in resumeTaskFromHistory.
  501. this._taskToolProtocol = historyItem.toolProtocol
  502. } else {
  503. // For new tasks, don't set the mode/apiConfigName yet - wait for async initialization.
  504. this._taskMode = undefined
  505. this._taskApiConfigName = undefined
  506. this.taskModeReady = this.initializeTaskMode(provider)
  507. this.taskApiConfigReady = this.initializeTaskApiConfigName(provider)
  508. TelemetryService.instance.captureTaskCreated(this.taskId)
  509. // For new tasks, resolve and lock the tool protocol immediately.
  510. // This ensures the task will continue using this protocol even if
  511. // user settings change.
  512. const modelInfo = this.api.getModel().info
  513. this._taskToolProtocol = resolveToolProtocol(this.apiConfiguration, modelInfo)
  514. }
  515. // Initialize the assistant message parser based on the locked tool protocol.
  516. // For native protocol, tool calls come as tool_call chunks, not XML.
  517. // For history items without a persisted protocol, we default to XML parser
  518. // and will update it in resumeTaskFromHistory after detection.
  519. const effectiveProtocol = this._taskToolProtocol || "xml"
  520. this.assistantMessageParser = effectiveProtocol !== "native" ? new AssistantMessageParser() : undefined
  521. this.messageQueueService = new MessageQueueService()
  522. this.messageQueueStateChangedHandler = () => {
  523. this.emit(RooCodeEventName.TaskUserMessage, this.taskId)
  524. this.providerRef.deref()?.postStateToWebview()
  525. }
  526. this.messageQueueService.on("stateChanged", this.messageQueueStateChangedHandler)
  527. // Listen for provider profile changes to update parser state
  528. this.setupProviderProfileChangeListener(provider)
  529. // Only set up diff strategy if diff is enabled.
  530. if (this.diffEnabled) {
  531. // Default to old strategy, will be updated if experiment is enabled.
  532. this.diffStrategy = new MultiSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
  533. // Check experiment asynchronously and update strategy if needed.
  534. provider.getState().then((state) => {
  535. const isMultiFileApplyDiffEnabled = experiments.isEnabled(
  536. state.experiments ?? {},
  537. EXPERIMENT_IDS.MULTI_FILE_APPLY_DIFF,
  538. )
  539. if (isMultiFileApplyDiffEnabled) {
  540. this.diffStrategy = new MultiFileSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
  541. }
  542. })
  543. }
  544. this.toolRepetitionDetector = new ToolRepetitionDetector(this.consecutiveMistakeLimit)
  545. // Initialize todo list if provided
  546. if (initialTodos && initialTodos.length > 0) {
  547. this.todoList = initialTodos
  548. }
  549. // Initialize debounced token usage emit function
  550. // Uses debounce with maxWait to achieve throttle-like behavior:
  551. // - leading: true - Emit immediately on first call
  552. // - trailing: true - Emit final state when updates stop
  553. // - maxWait - Ensures at most one emit per interval during rapid updates (throttle behavior)
  554. this.debouncedEmitTokenUsage = debounce(
  555. (tokenUsage: TokenUsage, toolUsage: ToolUsage) => {
  556. const tokenChanged = hasTokenUsageChanged(tokenUsage, this.tokenUsageSnapshot)
  557. const toolChanged = hasToolUsageChanged(toolUsage, this.toolUsageSnapshot)
  558. if (tokenChanged || toolChanged) {
  559. this.emit(RooCodeEventName.TaskTokenUsageUpdated, this.taskId, tokenUsage, toolUsage)
  560. this.tokenUsageSnapshot = tokenUsage
  561. this.tokenUsageSnapshotAt = this.clineMessages.at(-1)?.ts
  562. // Deep copy tool usage for snapshot
  563. this.toolUsageSnapshot = JSON.parse(JSON.stringify(toolUsage))
  564. }
  565. },
  566. this.TOKEN_USAGE_EMIT_INTERVAL_MS,
  567. { leading: true, trailing: true, maxWait: this.TOKEN_USAGE_EMIT_INTERVAL_MS },
  568. )
  569. onCreated?.(this)
  570. if (startTask) {
  571. if (task || images) {
  572. this.startTask(task, images)
  573. } else if (historyItem) {
  574. this.resumeTaskFromHistory()
  575. } else {
  576. throw new Error("Either historyItem or task/images must be provided")
  577. }
  578. }
  579. }
  580. /**
  581. * Initialize the task mode from the provider state.
  582. * This method handles async initialization with proper error handling.
  583. *
  584. * ## Flow
  585. * 1. Attempts to fetch the current mode from provider state
  586. * 2. Sets `_taskMode` to the fetched mode or `defaultModeSlug` if unavailable
  587. * 3. Handles errors gracefully by falling back to default mode
  588. * 4. Logs any initialization errors for debugging
  589. *
  590. * ## Error handling
  591. * - Network failures when fetching provider state
  592. * - Provider not yet initialized
  593. * - Invalid state structure
  594. *
  595. * All errors result in fallback to `defaultModeSlug` to ensure task can proceed.
  596. *
  597. * @private
  598. * @param provider - The ClineProvider instance to fetch state from
  599. * @returns Promise that resolves when initialization is complete
  600. */
  601. private async initializeTaskMode(provider: ClineProvider): Promise<void> {
  602. try {
  603. const state = await provider.getState()
  604. this._taskMode = state?.mode || defaultModeSlug
  605. } catch (error) {
  606. // If there's an error getting state, use the default mode
  607. this._taskMode = defaultModeSlug
  608. // Use the provider's log method for better error visibility
  609. const errorMessage = `Failed to initialize task mode: ${error instanceof Error ? error.message : String(error)}`
  610. provider.log(errorMessage)
  611. }
  612. }
  613. /**
  614. * Initialize the task API config name from the provider state.
  615. * This method handles async initialization with proper error handling.
  616. *
  617. * ## Flow
  618. * 1. Attempts to fetch the current API config name from provider state
  619. * 2. Sets `_taskApiConfigName` to the fetched name or "default" if unavailable
  620. * 3. Handles errors gracefully by falling back to "default"
  621. * 4. Logs any initialization errors for debugging
  622. *
  623. * ## Error handling
  624. * - Network failures when fetching provider state
  625. * - Provider not yet initialized
  626. * - Invalid state structure
  627. *
  628. * All errors result in fallback to "default" to ensure task can proceed.
  629. *
  630. * @private
  631. * @param provider - The ClineProvider instance to fetch state from
  632. * @returns Promise that resolves when initialization is complete
  633. */
  634. private async initializeTaskApiConfigName(provider: ClineProvider): Promise<void> {
  635. try {
  636. const state = await provider.getState()
  637. // Avoid clobbering a newer value that may have been set while awaiting provider state
  638. // (e.g., user switches provider profile immediately after task creation).
  639. if (this._taskApiConfigName === undefined) {
  640. this._taskApiConfigName = state?.currentApiConfigName ?? "default"
  641. }
  642. } catch (error) {
  643. // If there's an error getting state, use the default profile (unless a newer value was set).
  644. if (this._taskApiConfigName === undefined) {
  645. this._taskApiConfigName = "default"
  646. }
  647. // Use the provider's log method for better error visibility
  648. const errorMessage = `Failed to initialize task API config name: ${error instanceof Error ? error.message : String(error)}`
  649. provider.log(errorMessage)
  650. }
  651. }
  652. /**
  653. * Sets up a listener for provider profile changes to automatically update the parser state.
  654. * This ensures the XML/native protocol parser stays synchronized with the current model.
  655. *
  656. * @private
  657. * @param provider - The ClineProvider instance to listen to
  658. */
  659. private setupProviderProfileChangeListener(provider: ClineProvider): void {
  660. // Only set up listener if provider has the on method (may not exist in test mocks)
  661. if (typeof provider.on !== "function") {
  662. return
  663. }
  664. this.providerProfileChangeListener = async () => {
  665. try {
  666. const newState = await provider.getState()
  667. if (newState?.apiConfiguration) {
  668. this.updateApiConfiguration(newState.apiConfiguration)
  669. }
  670. } catch (error) {
  671. console.error(
  672. `[Task#${this.taskId}.${this.instanceId}] Failed to update API configuration on profile change:`,
  673. error,
  674. )
  675. }
  676. }
  677. provider.on(RooCodeEventName.ProviderProfileChanged, this.providerProfileChangeListener)
  678. }
  679. /**
  680. * Wait for the task mode to be initialized before proceeding.
  681. * This method ensures that any operations depending on the task mode
  682. * will have access to the correct mode value.
  683. *
  684. * ## When to use
  685. * - Before accessing mode-specific configurations
  686. * - When switching between tasks with different modes
  687. * - Before operations that depend on mode-based permissions
  688. *
  689. * ## Example usage
  690. * ```typescript
  691. * // Wait for mode initialization before mode-dependent operations
  692. * await task.waitForModeInitialization();
  693. * const mode = task.taskMode; // Now safe to access synchronously
  694. *
  695. * // Or use with getTaskMode() for a one-liner
  696. * const mode = await task.getTaskMode(); // Internally waits for initialization
  697. * ```
  698. *
  699. * @returns Promise that resolves when the task mode is initialized
  700. * @public
  701. */
  702. public async waitForModeInitialization(): Promise<void> {
  703. return this.taskModeReady
  704. }
  705. /**
  706. * Get the task mode asynchronously, ensuring it's properly initialized.
  707. * This is the recommended way to access the task mode as it guarantees
  708. * the mode is available before returning.
  709. *
  710. * ## Async behavior
  711. * - Internally waits for `taskModeReady` promise to resolve
  712. * - Returns the initialized mode or `defaultModeSlug` as fallback
  713. * - Safe to call multiple times - subsequent calls return immediately if already initialized
  714. *
  715. * ## Example usage
  716. * ```typescript
  717. * // Safe async access
  718. * const mode = await task.getTaskMode();
  719. * console.log(`Task is running in ${mode} mode`);
  720. *
  721. * // Use in conditional logic
  722. * if (await task.getTaskMode() === 'architect') {
  723. * // Perform architect-specific operations
  724. * }
  725. * ```
  726. *
  727. * @returns Promise resolving to the task mode string
  728. * @public
  729. */
  730. public async getTaskMode(): Promise<string> {
  731. await this.taskModeReady
  732. return this._taskMode || defaultModeSlug
  733. }
  734. /**
  735. * Get the task mode synchronously. This should only be used when you're certain
  736. * that the mode has already been initialized (e.g., after waitForModeInitialization).
  737. *
  738. * ## When to use
  739. * - In synchronous contexts where async/await is not available
  740. * - After explicitly waiting for initialization via `waitForModeInitialization()`
  741. * - In event handlers or callbacks where mode is guaranteed to be initialized
  742. *
  743. * ## Example usage
  744. * ```typescript
  745. * // After ensuring initialization
  746. * await task.waitForModeInitialization();
  747. * const mode = task.taskMode; // Safe synchronous access
  748. *
  749. * // In an event handler after task is started
  750. * task.on('taskStarted', () => {
  751. * console.log(`Task started in ${task.taskMode} mode`); // Safe here
  752. * });
  753. * ```
  754. *
  755. * @throws {Error} If the mode hasn't been initialized yet
  756. * @returns The task mode string
  757. * @public
  758. */
  759. public get taskMode(): string {
  760. if (this._taskMode === undefined) {
  761. throw new Error("Task mode accessed before initialization. Use getTaskMode() or wait for taskModeReady.")
  762. }
  763. return this._taskMode
  764. }
  765. /**
  766. * Wait for the task API config name to be initialized before proceeding.
  767. * This method ensures that any operations depending on the task's provider profile
  768. * will have access to the correct value.
  769. *
  770. * ## When to use
  771. * - Before accessing provider profile-specific configurations
  772. * - When switching between tasks with different provider profiles
  773. * - Before operations that depend on the provider profile
  774. *
  775. * @returns Promise that resolves when the task API config name is initialized
  776. * @public
  777. */
  778. public async waitForApiConfigInitialization(): Promise<void> {
  779. return this.taskApiConfigReady
  780. }
  781. /**
  782. * Get the task API config name asynchronously, ensuring it's properly initialized.
  783. * This is the recommended way to access the task's provider profile as it guarantees
  784. * the value is available before returning.
  785. *
  786. * ## Async behavior
  787. * - Internally waits for `taskApiConfigReady` promise to resolve
  788. * - Returns the initialized API config name or undefined as fallback
  789. * - Safe to call multiple times - subsequent calls return immediately if already initialized
  790. *
  791. * @returns Promise resolving to the task API config name string or undefined
  792. * @public
  793. */
  794. public async getTaskApiConfigName(): Promise<string | undefined> {
  795. await this.taskApiConfigReady
  796. return this._taskApiConfigName
  797. }
  798. /**
  799. * Get the task API config name synchronously. This should only be used when you're certain
  800. * that the value has already been initialized (e.g., after waitForApiConfigInitialization).
  801. *
  802. * ## When to use
  803. * - In synchronous contexts where async/await is not available
  804. * - After explicitly waiting for initialization via `waitForApiConfigInitialization()`
  805. * - In event handlers or callbacks where API config name is guaranteed to be initialized
  806. *
  807. * Note: Unlike taskMode, this getter does not throw if uninitialized since the API config
  808. * name can legitimately be undefined (backward compatibility with tasks created before
  809. * this feature was added).
  810. *
  811. * @returns The task API config name string or undefined
  812. * @public
  813. */
  814. public get taskApiConfigName(): string | undefined {
  815. return this._taskApiConfigName
  816. }
  817. /**
  818. * Update the task's API config name. This is called when the user switches
  819. * provider profiles while a task is active, allowing the task to remember
  820. * its new provider profile.
  821. *
  822. * @param apiConfigName - The new API config name to set
  823. * @internal
  824. */
  825. public setTaskApiConfigName(apiConfigName: string | undefined): void {
  826. this._taskApiConfigName = apiConfigName
  827. }
  828. static create(options: TaskOptions): [Task, Promise<void>] {
  829. const instance = new Task({ ...options, startTask: false })
  830. const { images, task, historyItem } = options
  831. let promise
  832. if (images || task) {
  833. promise = instance.startTask(task, images)
  834. } else if (historyItem) {
  835. promise = instance.resumeTaskFromHistory()
  836. } else {
  837. throw new Error("Either historyItem or task/images must be provided")
  838. }
  839. return [instance, promise]
  840. }
  841. // API Messages
  842. private async getSavedApiConversationHistory(): Promise<ApiMessage[]> {
  843. return readApiMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
  844. }
  845. private async addToApiConversationHistory(message: Anthropic.MessageParam, reasoning?: string) {
  846. // Capture the encrypted_content / thought signatures from the provider (e.g., OpenAI Responses API, Google GenAI) if present.
  847. // We only persist data reported by the current response body.
  848. const handler = this.api as ApiHandler & {
  849. getResponseId?: () => string | undefined
  850. getEncryptedContent?: () => { encrypted_content: string; id?: string } | undefined
  851. getThoughtSignature?: () => string | undefined
  852. getSummary?: () => any[] | undefined
  853. getReasoningDetails?: () => any[] | undefined
  854. }
  855. if (message.role === "assistant") {
  856. const responseId = handler.getResponseId?.()
  857. const reasoningData = handler.getEncryptedContent?.()
  858. const thoughtSignature = handler.getThoughtSignature?.()
  859. const reasoningSummary = handler.getSummary?.()
  860. const reasoningDetails = handler.getReasoningDetails?.()
  861. // Only Anthropic's API expects/validates the special `thinking` content block signature.
  862. // Other providers (notably Gemini 3) use different signature semantics (e.g. `thoughtSignature`)
  863. // and require round-tripping the signature in their own format.
  864. const modelId = getModelId(this.apiConfiguration)
  865. const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
  866. const isAnthropicProtocol = apiProtocol === "anthropic"
  867. // Start from the original assistant message
  868. const messageWithTs: any = {
  869. ...message,
  870. ...(responseId ? { id: responseId } : {}),
  871. ts: Date.now(),
  872. }
  873. // Store reasoning_details array if present (for models like Gemini 3)
  874. if (reasoningDetails) {
  875. messageWithTs.reasoning_details = reasoningDetails
  876. }
  877. // Store reasoning: Anthropic thinking (with signature), plain text (most providers), or encrypted (OpenAI Native)
  878. // Skip if reasoning_details already contains the reasoning (to avoid duplication)
  879. if (isAnthropicProtocol && reasoning && thoughtSignature && !reasoningDetails) {
  880. // Anthropic provider with extended thinking: Store as proper `thinking` block
  881. // This format passes through anthropic-filter.ts and is properly round-tripped
  882. // for interleaved thinking with tool use (required by Anthropic API)
  883. const thinkingBlock = {
  884. type: "thinking",
  885. thinking: reasoning,
  886. signature: thoughtSignature,
  887. }
  888. if (typeof messageWithTs.content === "string") {
  889. messageWithTs.content = [
  890. thinkingBlock,
  891. { type: "text", text: messageWithTs.content } satisfies Anthropic.Messages.TextBlockParam,
  892. ]
  893. } else if (Array.isArray(messageWithTs.content)) {
  894. messageWithTs.content = [thinkingBlock, ...messageWithTs.content]
  895. } else if (!messageWithTs.content) {
  896. messageWithTs.content = [thinkingBlock]
  897. }
  898. } else if (reasoning && !reasoningDetails) {
  899. // Other providers (non-Anthropic): Store as generic reasoning block
  900. const reasoningBlock = {
  901. type: "reasoning",
  902. text: reasoning,
  903. summary: reasoningSummary ?? ([] as any[]),
  904. }
  905. if (typeof messageWithTs.content === "string") {
  906. messageWithTs.content = [
  907. reasoningBlock,
  908. { type: "text", text: messageWithTs.content } satisfies Anthropic.Messages.TextBlockParam,
  909. ]
  910. } else if (Array.isArray(messageWithTs.content)) {
  911. messageWithTs.content = [reasoningBlock, ...messageWithTs.content]
  912. } else if (!messageWithTs.content) {
  913. messageWithTs.content = [reasoningBlock]
  914. }
  915. } else if (reasoningData?.encrypted_content) {
  916. // OpenAI Native encrypted reasoning
  917. const reasoningBlock = {
  918. type: "reasoning",
  919. summary: [] as any[],
  920. encrypted_content: reasoningData.encrypted_content,
  921. ...(reasoningData.id ? { id: reasoningData.id } : {}),
  922. }
  923. if (typeof messageWithTs.content === "string") {
  924. messageWithTs.content = [
  925. reasoningBlock,
  926. { type: "text", text: messageWithTs.content } satisfies Anthropic.Messages.TextBlockParam,
  927. ]
  928. } else if (Array.isArray(messageWithTs.content)) {
  929. messageWithTs.content = [reasoningBlock, ...messageWithTs.content]
  930. } else if (!messageWithTs.content) {
  931. messageWithTs.content = [reasoningBlock]
  932. }
  933. }
  934. // For non-Anthropic providers (e.g., Gemini 3), persist the thought signature as its own
  935. // content block so converters can attach it back to the correct provider-specific fields.
  936. // Note: For Anthropic extended thinking, the signature is already included in the thinking block above.
  937. if (thoughtSignature && !isAnthropicProtocol) {
  938. const thoughtSignatureBlock = {
  939. type: "thoughtSignature",
  940. thoughtSignature,
  941. }
  942. if (typeof messageWithTs.content === "string") {
  943. messageWithTs.content = [
  944. { type: "text", text: messageWithTs.content } satisfies Anthropic.Messages.TextBlockParam,
  945. thoughtSignatureBlock,
  946. ]
  947. } else if (Array.isArray(messageWithTs.content)) {
  948. messageWithTs.content = [...messageWithTs.content, thoughtSignatureBlock]
  949. } else if (!messageWithTs.content) {
  950. messageWithTs.content = [thoughtSignatureBlock]
  951. }
  952. }
  953. this.apiConversationHistory.push(messageWithTs)
  954. } else {
  955. // For user messages, validate and fix tool_result IDs against the previous assistant message
  956. const validatedMessage = validateAndFixToolResultIds(message, this.apiConversationHistory)
  957. const messageWithTs = { ...validatedMessage, ts: Date.now() }
  958. this.apiConversationHistory.push(messageWithTs)
  959. }
  960. await this.saveApiConversationHistory()
  961. }
  962. async overwriteApiConversationHistory(newHistory: ApiMessage[]) {
  963. this.apiConversationHistory = newHistory
  964. await this.saveApiConversationHistory()
  965. }
  966. /**
  967. * Flush any pending tool results to the API conversation history.
  968. *
  969. * This is critical for native tool protocol when the task is about to be
  970. * delegated (e.g., via new_task). Before delegation, if other tools were
  971. * called in the same turn before new_task, their tool_result blocks are
  972. * accumulated in `userMessageContent` but haven't been saved to the API
  973. * history yet. If we don't flush them before the parent is disposed,
  974. * the API conversation will be incomplete and cause 400 errors when
  975. * the parent resumes (missing tool_result for tool_use blocks).
  976. *
  977. * NOTE: The assistant message is typically already in history by the time
  978. * tools execute (added in recursivelyMakeClineRequests after streaming completes).
  979. * So we usually only need to flush the pending user message with tool_results.
  980. */
  981. public async flushPendingToolResultsToHistory(): Promise<void> {
  982. // Only flush if there's actually pending content to save
  983. if (this.userMessageContent.length === 0) {
  984. return
  985. }
  986. // Save the user message with tool_result blocks
  987. const userMessage: Anthropic.MessageParam = {
  988. role: "user",
  989. content: this.userMessageContent,
  990. }
  991. // Validate and fix tool_result IDs against the previous assistant message
  992. const validatedMessage = validateAndFixToolResultIds(userMessage, this.apiConversationHistory)
  993. const userMessageWithTs = { ...validatedMessage, ts: Date.now() }
  994. this.apiConversationHistory.push(userMessageWithTs as ApiMessage)
  995. await this.saveApiConversationHistory()
  996. // Clear the pending content since it's now saved
  997. this.userMessageContent = []
  998. }
  999. private async saveApiConversationHistory() {
  1000. try {
  1001. await saveApiMessages({
  1002. messages: this.apiConversationHistory,
  1003. taskId: this.taskId,
  1004. globalStoragePath: this.globalStoragePath,
  1005. })
  1006. } catch (error) {
  1007. // In the off chance this fails, we don't want to stop the task.
  1008. console.error("Failed to save API conversation history:", error)
  1009. }
  1010. }
  1011. // Cline Messages
  1012. private async getSavedClineMessages(): Promise<ClineMessage[]> {
  1013. return readTaskMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
  1014. }
  1015. private async addToClineMessages(message: ClineMessage) {
  1016. this.clineMessages.push(message)
  1017. const provider = this.providerRef.deref()
  1018. await provider?.postStateToWebview()
  1019. this.emit(RooCodeEventName.Message, { action: "created", message })
  1020. await this.saveClineMessages()
  1021. const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
  1022. if (shouldCaptureMessage) {
  1023. CloudService.instance.captureEvent({
  1024. event: TelemetryEventName.TASK_MESSAGE,
  1025. properties: { taskId: this.taskId, message },
  1026. })
  1027. // Track that this message has been synced to cloud
  1028. this.cloudSyncedMessageTimestamps.add(message.ts)
  1029. }
  1030. }
  1031. public async overwriteClineMessages(newMessages: ClineMessage[]) {
  1032. this.clineMessages = newMessages
  1033. restoreTodoListForTask(this)
  1034. await this.saveClineMessages()
  1035. // When overwriting messages (e.g., during task resume), repopulate the cloud sync tracking Set
  1036. // with timestamps from all non-partial messages to prevent re-syncing previously synced messages
  1037. this.cloudSyncedMessageTimestamps.clear()
  1038. for (const msg of newMessages) {
  1039. if (msg.partial !== true) {
  1040. this.cloudSyncedMessageTimestamps.add(msg.ts)
  1041. }
  1042. }
  1043. }
  1044. private async updateClineMessage(message: ClineMessage) {
  1045. const provider = this.providerRef.deref()
  1046. await provider?.postMessageToWebview({ type: "messageUpdated", clineMessage: message })
  1047. this.emit(RooCodeEventName.Message, { action: "updated", message })
  1048. // Check if we should sync to cloud and haven't already synced this message
  1049. const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
  1050. const hasNotBeenSynced = !this.cloudSyncedMessageTimestamps.has(message.ts)
  1051. if (shouldCaptureMessage && hasNotBeenSynced) {
  1052. CloudService.instance.captureEvent({
  1053. event: TelemetryEventName.TASK_MESSAGE,
  1054. properties: { taskId: this.taskId, message },
  1055. })
  1056. // Track that this message has been synced to cloud
  1057. this.cloudSyncedMessageTimestamps.add(message.ts)
  1058. }
  1059. }
  1060. private async saveClineMessages() {
  1061. try {
  1062. await saveTaskMessages({
  1063. messages: this.clineMessages,
  1064. taskId: this.taskId,
  1065. globalStoragePath: this.globalStoragePath,
  1066. })
  1067. if (this._taskApiConfigName === undefined) {
  1068. await this.taskApiConfigReady
  1069. }
  1070. const { historyItem, tokenUsage } = await taskMetadata({
  1071. taskId: this.taskId,
  1072. rootTaskId: this.rootTaskId,
  1073. parentTaskId: this.parentTaskId,
  1074. taskNumber: this.taskNumber,
  1075. messages: this.clineMessages,
  1076. globalStoragePath: this.globalStoragePath,
  1077. workspace: this.cwd,
  1078. mode: this._taskMode || defaultModeSlug, // Use the task's own mode, not the current provider mode.
  1079. apiConfigName: this._taskApiConfigName, // Use the task's own provider profile, not the current provider profile.
  1080. initialStatus: this.initialStatus,
  1081. toolProtocol: this._taskToolProtocol, // Persist the locked tool protocol.
  1082. })
  1083. // Emit token/tool usage updates using debounced function
  1084. // The debounce with maxWait ensures:
  1085. // - Immediate first emit (leading: true)
  1086. // - At most one emit per interval during rapid updates (maxWait)
  1087. // - Final state is emitted when updates stop (trailing: true)
  1088. this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage)
  1089. await this.providerRef.deref()?.updateTaskHistory(historyItem)
  1090. } catch (error) {
  1091. console.error("Failed to save Roo messages:", error)
  1092. }
  1093. }
  1094. private findMessageByTimestamp(ts: number): ClineMessage | undefined {
  1095. for (let i = this.clineMessages.length - 1; i >= 0; i--) {
  1096. if (this.clineMessages[i].ts === ts) {
  1097. return this.clineMessages[i]
  1098. }
  1099. }
  1100. return undefined
  1101. }
  1102. // Note that `partial` has three valid states true (partial message),
  1103. // false (completion of partial message), undefined (individual complete
  1104. // message).
  1105. async ask(
  1106. type: ClineAsk,
  1107. text?: string,
  1108. partial?: boolean,
  1109. progressStatus?: ToolProgressStatus,
  1110. isProtected?: boolean,
  1111. ): Promise<{ response: ClineAskResponse; text?: string; images?: string[] }> {
  1112. // If this Cline instance was aborted by the provider, then the only
  1113. // thing keeping us alive is a promise still running in the background,
  1114. // in which case we don't want to send its result to the webview as it
  1115. // is attached to a new instance of Cline now. So we can safely ignore
  1116. // the result of any active promises, and this class will be
  1117. // deallocated. (Although we set Cline = undefined in provider, that
  1118. // simply removes the reference to this instance, but the instance is
  1119. // still alive until this promise resolves or rejects.)
  1120. if (this.abort) {
  1121. throw new Error(`[RooCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
  1122. }
  1123. let askTs: number
  1124. if (partial !== undefined) {
  1125. const lastMessage = this.clineMessages.at(-1)
  1126. const isUpdatingPreviousPartial =
  1127. lastMessage && lastMessage.partial && lastMessage.type === "ask" && lastMessage.ask === type
  1128. if (partial) {
  1129. if (isUpdatingPreviousPartial) {
  1130. // Existing partial message, so update it.
  1131. lastMessage.text = text
  1132. lastMessage.partial = partial
  1133. lastMessage.progressStatus = progressStatus
  1134. lastMessage.isProtected = isProtected
  1135. // TODO: Be more efficient about saving and posting only new
  1136. // data or one whole message at a time so ignore partial for
  1137. // saves, and only post parts of partial message instead of
  1138. // whole array in new listener.
  1139. this.updateClineMessage(lastMessage)
  1140. // console.log("Task#ask: current ask promise was ignored (#1)")
  1141. throw new AskIgnoredError("updating existing partial")
  1142. } else {
  1143. // This is a new partial message, so add it with partial
  1144. // state.
  1145. askTs = Date.now()
  1146. this.lastMessageTs = askTs
  1147. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, partial, isProtected })
  1148. // console.log("Task#ask: current ask promise was ignored (#2)")
  1149. throw new AskIgnoredError("new partial")
  1150. }
  1151. } else {
  1152. if (isUpdatingPreviousPartial) {
  1153. // This is the complete version of a previously partial
  1154. // message, so replace the partial with the complete version.
  1155. this.askResponse = undefined
  1156. this.askResponseText = undefined
  1157. this.askResponseImages = undefined
  1158. // Bug for the history books:
  1159. // In the webview we use the ts as the chatrow key for the
  1160. // virtuoso list. Since we would update this ts right at the
  1161. // end of streaming, it would cause the view to flicker. The
  1162. // key prop has to be stable otherwise react has trouble
  1163. // reconciling items between renders, causing unmounting and
  1164. // remounting of components (flickering).
  1165. // The lesson here is if you see flickering when rendering
  1166. // lists, it's likely because the key prop is not stable.
  1167. // So in this case we must make sure that the message ts is
  1168. // never altered after first setting it.
  1169. askTs = lastMessage.ts
  1170. this.lastMessageTs = askTs
  1171. lastMessage.text = text
  1172. lastMessage.partial = false
  1173. lastMessage.progressStatus = progressStatus
  1174. lastMessage.isProtected = isProtected
  1175. await this.saveClineMessages()
  1176. this.updateClineMessage(lastMessage)
  1177. } else {
  1178. // This is a new and complete message, so add it like normal.
  1179. this.askResponse = undefined
  1180. this.askResponseText = undefined
  1181. this.askResponseImages = undefined
  1182. askTs = Date.now()
  1183. this.lastMessageTs = askTs
  1184. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
  1185. }
  1186. }
  1187. } else {
  1188. // This is a new non-partial message, so add it like normal.
  1189. this.askResponse = undefined
  1190. this.askResponseText = undefined
  1191. this.askResponseImages = undefined
  1192. askTs = Date.now()
  1193. this.lastMessageTs = askTs
  1194. await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
  1195. }
  1196. let timeouts: NodeJS.Timeout[] = []
  1197. // Automatically approve if the ask according to the user's settings.
  1198. const provider = this.providerRef.deref()
  1199. const state = provider ? await provider.getState() : undefined
  1200. const approval = await checkAutoApproval({ state, ask: type, text, isProtected })
  1201. if (approval.decision === "approve") {
  1202. this.approveAsk()
  1203. } else if (approval.decision === "deny") {
  1204. this.denyAsk()
  1205. } else if (approval.decision === "timeout") {
  1206. // Store the auto-approval timeout so it can be cancelled if user interacts
  1207. this.autoApprovalTimeoutRef = setTimeout(() => {
  1208. const { askResponse, text, images } = approval.fn()
  1209. this.handleWebviewAskResponse(askResponse, text, images)
  1210. this.autoApprovalTimeoutRef = undefined
  1211. }, approval.timeout)
  1212. timeouts.push(this.autoApprovalTimeoutRef)
  1213. }
  1214. // The state is mutable if the message is complete and the task will
  1215. // block (via the `pWaitFor`).
  1216. const isBlocking = !(this.askResponse !== undefined || this.lastMessageTs !== askTs)
  1217. const isMessageQueued = !this.messageQueueService.isEmpty()
  1218. const isStatusMutable = !partial && isBlocking && !isMessageQueued && approval.decision === "ask"
  1219. if (isStatusMutable) {
  1220. const statusMutationTimeout = 2_000
  1221. if (isInteractiveAsk(type)) {
  1222. timeouts.push(
  1223. setTimeout(() => {
  1224. const message = this.findMessageByTimestamp(askTs)
  1225. if (message) {
  1226. this.interactiveAsk = message
  1227. this.emit(RooCodeEventName.TaskInteractive, this.taskId)
  1228. provider?.postMessageToWebview({ type: "interactionRequired" })
  1229. }
  1230. }, statusMutationTimeout),
  1231. )
  1232. } else if (isResumableAsk(type)) {
  1233. timeouts.push(
  1234. setTimeout(() => {
  1235. const message = this.findMessageByTimestamp(askTs)
  1236. if (message) {
  1237. this.resumableAsk = message
  1238. this.emit(RooCodeEventName.TaskResumable, this.taskId)
  1239. }
  1240. }, statusMutationTimeout),
  1241. )
  1242. } else if (isIdleAsk(type)) {
  1243. timeouts.push(
  1244. setTimeout(() => {
  1245. const message = this.findMessageByTimestamp(askTs)
  1246. if (message) {
  1247. this.idleAsk = message
  1248. this.emit(RooCodeEventName.TaskIdle, this.taskId)
  1249. }
  1250. }, statusMutationTimeout),
  1251. )
  1252. }
  1253. } else if (isMessageQueued) {
  1254. const message = this.messageQueueService.dequeueMessage()
  1255. if (message) {
  1256. // Check if this is a tool approval ask that needs to be handled.
  1257. if (
  1258. type === "tool" ||
  1259. type === "command" ||
  1260. type === "browser_action_launch" ||
  1261. type === "use_mcp_server"
  1262. ) {
  1263. // For tool approvals, we need to approve first, then send
  1264. // the message if there's text/images.
  1265. this.handleWebviewAskResponse("yesButtonClicked", message.text, message.images)
  1266. } else {
  1267. // For other ask types (like followup or command_output), fulfill the ask
  1268. // directly.
  1269. this.handleWebviewAskResponse("messageResponse", message.text, message.images)
  1270. }
  1271. }
  1272. }
  1273. // Wait for askResponse to be set
  1274. await pWaitFor(
  1275. () => {
  1276. if (this.askResponse !== undefined || this.lastMessageTs !== askTs) {
  1277. return true
  1278. }
  1279. // If a queued message arrives while we're blocked on an ask (e.g. a follow-up
  1280. // suggestion click that was incorrectly queued due to UI state), consume it
  1281. // immediately so the task doesn't hang.
  1282. if (!this.messageQueueService.isEmpty()) {
  1283. const message = this.messageQueueService.dequeueMessage()
  1284. if (message) {
  1285. // If this is a tool approval ask, we need to approve first (yesButtonClicked)
  1286. // and include any queued text/images.
  1287. if (
  1288. type === "tool" ||
  1289. type === "command" ||
  1290. type === "browser_action_launch" ||
  1291. type === "use_mcp_server"
  1292. ) {
  1293. this.handleWebviewAskResponse("yesButtonClicked", message.text, message.images)
  1294. } else {
  1295. this.handleWebviewAskResponse("messageResponse", message.text, message.images)
  1296. }
  1297. }
  1298. }
  1299. return false
  1300. },
  1301. { interval: 100 },
  1302. )
  1303. if (this.lastMessageTs !== askTs) {
  1304. // Could happen if we send multiple asks in a row i.e. with
  1305. // command_output. It's important that when we know an ask could
  1306. // fail, it is handled gracefully.
  1307. throw new AskIgnoredError("superseded")
  1308. }
  1309. const result = { response: this.askResponse!, text: this.askResponseText, images: this.askResponseImages }
  1310. this.askResponse = undefined
  1311. this.askResponseText = undefined
  1312. this.askResponseImages = undefined
  1313. // Cancel the timeouts if they are still running.
  1314. timeouts.forEach((timeout) => clearTimeout(timeout))
  1315. // Switch back to an active state.
  1316. if (this.idleAsk || this.resumableAsk || this.interactiveAsk) {
  1317. this.idleAsk = undefined
  1318. this.resumableAsk = undefined
  1319. this.interactiveAsk = undefined
  1320. this.emit(RooCodeEventName.TaskActive, this.taskId)
  1321. }
  1322. this.emit(RooCodeEventName.TaskAskResponded)
  1323. return result
  1324. }
  1325. handleWebviewAskResponse(askResponse: ClineAskResponse, text?: string, images?: string[]) {
  1326. // Clear any pending auto-approval timeout when user responds
  1327. this.cancelAutoApprovalTimeout()
  1328. this.askResponse = askResponse
  1329. this.askResponseText = text
  1330. this.askResponseImages = images
  1331. // Create a checkpoint whenever the user sends a message.
  1332. // Use allowEmpty=true to ensure a checkpoint is recorded even if there are no file changes.
  1333. // Suppress the checkpoint_saved chat row for this particular checkpoint to keep the timeline clean.
  1334. if (askResponse === "messageResponse") {
  1335. void this.checkpointSave(false, true)
  1336. }
  1337. // Mark the last follow-up question as answered
  1338. if (askResponse === "messageResponse" || askResponse === "yesButtonClicked") {
  1339. // Find the last unanswered follow-up message using findLastIndex
  1340. const lastFollowUpIndex = findLastIndex(
  1341. this.clineMessages,
  1342. (msg) => msg.type === "ask" && msg.ask === "followup" && !msg.isAnswered,
  1343. )
  1344. if (lastFollowUpIndex !== -1) {
  1345. // Mark this follow-up as answered
  1346. this.clineMessages[lastFollowUpIndex].isAnswered = true
  1347. // Save the updated messages
  1348. this.saveClineMessages().catch((error) => {
  1349. console.error("Failed to save answered follow-up state:", error)
  1350. })
  1351. }
  1352. }
  1353. }
  1354. /**
  1355. * Cancel any pending auto-approval timeout.
  1356. * Called when user interacts (types, clicks buttons, etc.) to prevent the timeout from firing.
  1357. */
  1358. public cancelAutoApprovalTimeout(): void {
  1359. if (this.autoApprovalTimeoutRef) {
  1360. clearTimeout(this.autoApprovalTimeoutRef)
  1361. this.autoApprovalTimeoutRef = undefined
  1362. }
  1363. }
  1364. public approveAsk({ text, images }: { text?: string; images?: string[] } = {}) {
  1365. this.handleWebviewAskResponse("yesButtonClicked", text, images)
  1366. }
  1367. public denyAsk({ text, images }: { text?: string; images?: string[] } = {}) {
  1368. this.handleWebviewAskResponse("noButtonClicked", text, images)
  1369. }
  1370. public supersedePendingAsk(): void {
  1371. this.lastMessageTs = Date.now()
  1372. }
  1373. /**
  1374. * Updates the API configuration but preserves the locked tool protocol.
  1375. * The task's tool protocol is locked at creation time and should NOT change
  1376. * even when switching between models/profiles with different settings.
  1377. *
  1378. * @param newApiConfiguration - The new API configuration to use
  1379. */
  1380. public updateApiConfiguration(newApiConfiguration: ProviderSettings): void {
  1381. // Update the configuration and rebuild the API handler
  1382. this.apiConfiguration = newApiConfiguration
  1383. this.api = buildApiHandler(newApiConfiguration)
  1384. // IMPORTANT: Do NOT change the parser based on the new configuration!
  1385. // The task's tool protocol is locked at creation time and must remain
  1386. // consistent throughout the task's lifetime to ensure history can be
  1387. // properly resumed.
  1388. }
  1389. public async submitUserMessage(
  1390. text: string,
  1391. images?: string[],
  1392. mode?: string,
  1393. providerProfile?: string,
  1394. ): Promise<void> {
  1395. try {
  1396. text = (text ?? "").trim()
  1397. images = images ?? []
  1398. if (text.length === 0 && images.length === 0) {
  1399. return
  1400. }
  1401. const provider = this.providerRef.deref()
  1402. if (provider) {
  1403. if (mode) {
  1404. await provider.setMode(mode)
  1405. }
  1406. if (providerProfile) {
  1407. await provider.setProviderProfile(providerProfile)
  1408. // Update this task's API configuration to match the new profile
  1409. // This ensures the parser state is synchronized with the selected model
  1410. const newState = await provider.getState()
  1411. if (newState?.apiConfiguration) {
  1412. this.updateApiConfiguration(newState.apiConfiguration)
  1413. }
  1414. }
  1415. this.emit(RooCodeEventName.TaskUserMessage, this.taskId)
  1416. provider.postMessageToWebview({ type: "invoke", invoke: "sendMessage", text, images })
  1417. } else {
  1418. console.error("[Task#submitUserMessage] Provider reference lost")
  1419. }
  1420. } catch (error) {
  1421. console.error("[Task#submitUserMessage] Failed to submit user message:", error)
  1422. }
  1423. }
  1424. async handleTerminalOperation(terminalOperation: "continue" | "abort") {
  1425. if (terminalOperation === "continue") {
  1426. this.terminalProcess?.continue()
  1427. } else if (terminalOperation === "abort") {
  1428. this.terminalProcess?.abort()
  1429. }
  1430. }
  1431. public async condenseContext(): Promise<void> {
  1432. // CRITICAL: Flush any pending tool results before condensing
  1433. // to ensure tool_use/tool_result pairs are complete in history
  1434. await this.flushPendingToolResultsToHistory()
  1435. const systemPrompt = await this.getSystemPrompt()
  1436. // Get condensing configuration
  1437. const state = await this.providerRef.deref()?.getState()
  1438. // These properties may not exist in the state type yet, but are used for condensing configuration
  1439. const customCondensingPrompt = state?.customCondensingPrompt
  1440. const condensingApiConfigId = state?.condensingApiConfigId
  1441. const listApiConfigMeta = state?.listApiConfigMeta
  1442. // Determine API handler to use
  1443. let condensingApiHandler: ApiHandler | undefined
  1444. if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
  1445. // Find matching config by ID
  1446. const matchingConfig = listApiConfigMeta.find((config) => config.id === condensingApiConfigId)
  1447. if (matchingConfig) {
  1448. const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
  1449. id: condensingApiConfigId,
  1450. })
  1451. // Ensure profile and apiProvider exist before trying to build handler
  1452. if (profile && profile.apiProvider) {
  1453. condensingApiHandler = buildApiHandler(profile)
  1454. }
  1455. }
  1456. }
  1457. const { contextTokens: prevContextTokens } = this.getTokenUsage()
  1458. // Determine if we're using native tool protocol for proper message handling
  1459. // Use the task's locked protocol, NOT the current settings (fallback to xml if not set)
  1460. const useNativeTools = isNativeProtocol(this._taskToolProtocol ?? "xml")
  1461. const {
  1462. messages,
  1463. summary,
  1464. cost,
  1465. newContextTokens = 0,
  1466. error,
  1467. condenseId,
  1468. } = await summarizeConversation(
  1469. this.apiConversationHistory,
  1470. this.api, // Main API handler (fallback)
  1471. systemPrompt, // Default summarization prompt (fallback)
  1472. this.taskId,
  1473. prevContextTokens,
  1474. false, // manual trigger
  1475. customCondensingPrompt, // User's custom prompt
  1476. condensingApiHandler, // Specific handler for condensing
  1477. useNativeTools, // Pass native tools flag for proper message handling
  1478. )
  1479. if (error) {
  1480. this.say(
  1481. "condense_context_error",
  1482. error,
  1483. undefined /* images */,
  1484. false /* partial */,
  1485. undefined /* checkpoint */,
  1486. undefined /* progressStatus */,
  1487. { isNonInteractive: true } /* options */,
  1488. )
  1489. return
  1490. }
  1491. await this.overwriteApiConversationHistory(messages)
  1492. const contextCondense: ContextCondense = {
  1493. summary,
  1494. cost,
  1495. newContextTokens,
  1496. prevContextTokens,
  1497. condenseId: condenseId!,
  1498. }
  1499. await this.say(
  1500. "condense_context",
  1501. undefined /* text */,
  1502. undefined /* images */,
  1503. false /* partial */,
  1504. undefined /* checkpoint */,
  1505. undefined /* progressStatus */,
  1506. { isNonInteractive: true } /* options */,
  1507. contextCondense,
  1508. )
  1509. // Process any queued messages after condensing completes
  1510. this.processQueuedMessages()
  1511. }
  1512. async say(
  1513. type: ClineSay,
  1514. text?: string,
  1515. images?: string[],
  1516. partial?: boolean,
  1517. checkpoint?: Record<string, unknown>,
  1518. progressStatus?: ToolProgressStatus,
  1519. options: {
  1520. isNonInteractive?: boolean
  1521. } = {},
  1522. contextCondense?: ContextCondense,
  1523. contextTruncation?: ContextTruncation,
  1524. ): Promise<undefined> {
  1525. if (this.abort) {
  1526. throw new Error(`[RooCode#say] task ${this.taskId}.${this.instanceId} aborted`)
  1527. }
  1528. if (partial !== undefined) {
  1529. const lastMessage = this.clineMessages.at(-1)
  1530. const isUpdatingPreviousPartial =
  1531. lastMessage && lastMessage.partial && lastMessage.type === "say" && lastMessage.say === type
  1532. if (partial) {
  1533. if (isUpdatingPreviousPartial) {
  1534. // Existing partial message, so update it.
  1535. lastMessage.text = text
  1536. lastMessage.images = images
  1537. lastMessage.partial = partial
  1538. lastMessage.progressStatus = progressStatus
  1539. this.updateClineMessage(lastMessage)
  1540. } else {
  1541. // This is a new partial message, so add it with partial state.
  1542. const sayTs = Date.now()
  1543. if (!options.isNonInteractive) {
  1544. this.lastMessageTs = sayTs
  1545. }
  1546. await this.addToClineMessages({
  1547. ts: sayTs,
  1548. type: "say",
  1549. say: type,
  1550. text,
  1551. images,
  1552. partial,
  1553. contextCondense,
  1554. contextTruncation,
  1555. })
  1556. }
  1557. } else {
  1558. // New now have a complete version of a previously partial message.
  1559. // This is the complete version of a previously partial
  1560. // message, so replace the partial with the complete version.
  1561. if (isUpdatingPreviousPartial) {
  1562. if (!options.isNonInteractive) {
  1563. this.lastMessageTs = lastMessage.ts
  1564. }
  1565. lastMessage.text = text
  1566. lastMessage.images = images
  1567. lastMessage.partial = false
  1568. lastMessage.progressStatus = progressStatus
  1569. // Instead of streaming partialMessage events, we do a save
  1570. // and post like normal to persist to disk.
  1571. await this.saveClineMessages()
  1572. // More performant than an entire `postStateToWebview`.
  1573. this.updateClineMessage(lastMessage)
  1574. } else {
  1575. // This is a new and complete message, so add it like normal.
  1576. const sayTs = Date.now()
  1577. if (!options.isNonInteractive) {
  1578. this.lastMessageTs = sayTs
  1579. }
  1580. await this.addToClineMessages({
  1581. ts: sayTs,
  1582. type: "say",
  1583. say: type,
  1584. text,
  1585. images,
  1586. contextCondense,
  1587. contextTruncation,
  1588. })
  1589. }
  1590. }
  1591. } else {
  1592. // This is a new non-partial message, so add it like normal.
  1593. const sayTs = Date.now()
  1594. // A "non-interactive" message is a message is one that the user
  1595. // does not need to respond to. We don't want these message types
  1596. // to trigger an update to `lastMessageTs` since they can be created
  1597. // asynchronously and could interrupt a pending ask.
  1598. if (!options.isNonInteractive) {
  1599. this.lastMessageTs = sayTs
  1600. }
  1601. await this.addToClineMessages({
  1602. ts: sayTs,
  1603. type: "say",
  1604. say: type,
  1605. text,
  1606. images,
  1607. checkpoint,
  1608. contextCondense,
  1609. contextTruncation,
  1610. })
  1611. }
  1612. // Broadcast browser session updates to panel when browser-related messages are added
  1613. if (type === "browser_action" || type === "browser_action_result" || type === "browser_session_status") {
  1614. this.broadcastBrowserSessionUpdate()
  1615. }
  1616. }
  1617. async sayAndCreateMissingParamError(toolName: ToolName, paramName: string, relPath?: string) {
  1618. await this.say(
  1619. "error",
  1620. `Roo tried to use ${toolName}${
  1621. relPath ? ` for '${relPath.toPosix()}'` : ""
  1622. } without value for required parameter '${paramName}'. Retrying...`,
  1623. )
  1624. // Use the task's locked protocol, NOT the current settings (fallback to xml if not set)
  1625. return formatResponse.toolError(
  1626. formatResponse.missingToolParameterError(paramName, this._taskToolProtocol ?? "xml"),
  1627. )
  1628. }
  1629. // Lifecycle
  1630. // Start / Resume / Abort / Dispose
  1631. private async startTask(task?: string, images?: string[]): Promise<void> {
  1632. if (this.enableBridge) {
  1633. try {
  1634. await BridgeOrchestrator.subscribeToTask(this)
  1635. } catch (error) {
  1636. console.error(
  1637. `[Task#startTask] BridgeOrchestrator.subscribeToTask() failed: ${error instanceof Error ? error.message : String(error)}`,
  1638. )
  1639. }
  1640. }
  1641. // `conversationHistory` (for API) and `clineMessages` (for webview)
  1642. // need to be in sync.
  1643. // If the extension process were killed, then on restart the
  1644. // `clineMessages` might not be empty, so we need to set it to [] when
  1645. // we create a new Cline client (otherwise webview would show stale
  1646. // messages from previous session).
  1647. this.clineMessages = []
  1648. this.apiConversationHistory = []
  1649. // The todo list is already set in the constructor if initialTodos were provided
  1650. // No need to add any messages - the todoList property is already set
  1651. await this.providerRef.deref()?.postStateToWebview()
  1652. await this.say("text", task, images)
  1653. this.isInitialized = true
  1654. let imageBlocks: Anthropic.ImageBlockParam[] = formatResponse.imageBlocks(images)
  1655. // Task starting
  1656. await this.initiateTaskLoop([
  1657. {
  1658. type: "text",
  1659. text: `<task>\n${task}\n</task>`,
  1660. },
  1661. ...imageBlocks,
  1662. ]).catch((error) => {
  1663. // Swallow loop rejection when the task was intentionally abandoned/aborted
  1664. // during delegation or user cancellation to prevent unhandled rejections.
  1665. if (this.abandoned === true || this.abortReason === "user_cancelled") {
  1666. return
  1667. }
  1668. throw error
  1669. })
  1670. }
  1671. private async resumeTaskFromHistory() {
  1672. if (this.enableBridge) {
  1673. try {
  1674. await BridgeOrchestrator.subscribeToTask(this)
  1675. } catch (error) {
  1676. console.error(
  1677. `[Task#resumeTaskFromHistory] BridgeOrchestrator.subscribeToTask() failed: ${error instanceof Error ? error.message : String(error)}`,
  1678. )
  1679. }
  1680. }
  1681. const modifiedClineMessages = await this.getSavedClineMessages()
  1682. // Remove any resume messages that may have been added before.
  1683. const lastRelevantMessageIndex = findLastIndex(
  1684. modifiedClineMessages,
  1685. (m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task"),
  1686. )
  1687. if (lastRelevantMessageIndex !== -1) {
  1688. modifiedClineMessages.splice(lastRelevantMessageIndex + 1)
  1689. }
  1690. // Remove any trailing reasoning-only UI messages that were not part of the persisted API conversation
  1691. while (modifiedClineMessages.length > 0) {
  1692. const last = modifiedClineMessages[modifiedClineMessages.length - 1]
  1693. if (last.type === "say" && last.say === "reasoning") {
  1694. modifiedClineMessages.pop()
  1695. } else {
  1696. break
  1697. }
  1698. }
  1699. // Since we don't use `api_req_finished` anymore, we need to check if the
  1700. // last `api_req_started` has a cost value, if it doesn't and no
  1701. // cancellation reason to present, then we remove it since it indicates
  1702. // an api request without any partial content streamed.
  1703. const lastApiReqStartedIndex = findLastIndex(
  1704. modifiedClineMessages,
  1705. (m) => m.type === "say" && m.say === "api_req_started",
  1706. )
  1707. if (lastApiReqStartedIndex !== -1) {
  1708. const lastApiReqStarted = modifiedClineMessages[lastApiReqStartedIndex]
  1709. const { cost, cancelReason }: ClineApiReqInfo = JSON.parse(lastApiReqStarted.text || "{}")
  1710. if (cost === undefined && cancelReason === undefined) {
  1711. modifiedClineMessages.splice(lastApiReqStartedIndex, 1)
  1712. }
  1713. }
  1714. await this.overwriteClineMessages(modifiedClineMessages)
  1715. this.clineMessages = await this.getSavedClineMessages()
  1716. // Now present the cline messages to the user and ask if they want to
  1717. // resume (NOTE: we ran into a bug before where the
  1718. // apiConversationHistory wouldn't be initialized when opening a old
  1719. // task, and it was because we were waiting for resume).
  1720. // This is important in case the user deletes messages without resuming
  1721. // the task first.
  1722. this.apiConversationHistory = await this.getSavedApiConversationHistory()
  1723. // If we don't have a persisted tool protocol (old tasks before this feature),
  1724. // detect it from the API history. This ensures tasks that previously used
  1725. // XML tools will continue using XML even if NTC is now enabled.
  1726. if (!this._taskToolProtocol) {
  1727. const detectedProtocol = detectToolProtocolFromHistory(this.apiConversationHistory)
  1728. if (detectedProtocol) {
  1729. // Found tool calls in history - lock to that protocol
  1730. this._taskToolProtocol = detectedProtocol
  1731. } else {
  1732. // No tool calls in history yet - use current settings
  1733. const modelInfo = this.api.getModel().info
  1734. this._taskToolProtocol = resolveToolProtocol(this.apiConfiguration, modelInfo)
  1735. }
  1736. // Update parser state to match the detected/resolved protocol
  1737. const shouldUseXmlParser = this._taskToolProtocol === "xml"
  1738. if (shouldUseXmlParser && !this.assistantMessageParser) {
  1739. this.assistantMessageParser = new AssistantMessageParser()
  1740. } else if (!shouldUseXmlParser && this.assistantMessageParser) {
  1741. this.assistantMessageParser.reset()
  1742. this.assistantMessageParser = undefined
  1743. }
  1744. } else {
  1745. }
  1746. const lastClineMessage = this.clineMessages
  1747. .slice()
  1748. .reverse()
  1749. .find((m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task")) // Could be multiple resume tasks.
  1750. let askType: ClineAsk
  1751. if (lastClineMessage?.ask === "completion_result") {
  1752. askType = "resume_completed_task"
  1753. } else {
  1754. askType = "resume_task"
  1755. }
  1756. this.isInitialized = true
  1757. const { response, text, images } = await this.ask(askType) // Calls `postStateToWebview`.
  1758. let responseText: string | undefined
  1759. let responseImages: string[] | undefined
  1760. if (response === "messageResponse") {
  1761. await this.say("user_feedback", text, images)
  1762. responseText = text
  1763. responseImages = images
  1764. }
  1765. // Make sure that the api conversation history can be resumed by the API,
  1766. // even if it goes out of sync with cline messages.
  1767. let existingApiConversationHistory: ApiMessage[] = await this.getSavedApiConversationHistory()
  1768. // v2.0 xml tags refactor caveat: since we don't use tools anymore for XML protocol,
  1769. // we need to replace all tool use blocks with a text block since the API disallows
  1770. // conversations with tool uses and no tool schema.
  1771. // For native protocol, we preserve tool_use and tool_result blocks as they're expected by the API.
  1772. // IMPORTANT: Use the task's locked protocol, NOT the current settings!
  1773. const useNative = isNativeProtocol(this._taskToolProtocol)
  1774. // Only convert tool blocks to text for XML protocol
  1775. // For native protocol, the API expects proper tool_use/tool_result structure
  1776. if (!useNative) {
  1777. const conversationWithoutToolBlocks = existingApiConversationHistory.map((message) => {
  1778. if (Array.isArray(message.content)) {
  1779. const newContent = message.content.map((block) => {
  1780. if (block.type === "tool_use") {
  1781. // Format tool invocation based on the task's locked protocol
  1782. const params = block.input as Record<string, any>
  1783. const formattedText = formatToolInvocation(block.name, params, this._taskToolProtocol)
  1784. return {
  1785. type: "text",
  1786. text: formattedText,
  1787. } as Anthropic.Messages.TextBlockParam
  1788. } else if (block.type === "tool_result") {
  1789. // Convert block.content to text block array, removing images
  1790. const contentAsTextBlocks = Array.isArray(block.content)
  1791. ? block.content.filter((item) => item.type === "text")
  1792. : [{ type: "text", text: block.content }]
  1793. const textContent = contentAsTextBlocks.map((item) => item.text).join("\n\n")
  1794. const toolName = findToolName(block.tool_use_id, existingApiConversationHistory)
  1795. return {
  1796. type: "text",
  1797. text: `[${toolName} Result]\n\n${textContent}`,
  1798. } as Anthropic.Messages.TextBlockParam
  1799. }
  1800. return block
  1801. })
  1802. return { ...message, content: newContent }
  1803. }
  1804. return message
  1805. })
  1806. existingApiConversationHistory = conversationWithoutToolBlocks
  1807. }
  1808. // FIXME: remove tool use blocks altogether
  1809. // 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
  1810. // if there's no tool use and only a text block, then we can just add a user message
  1811. // (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)
  1812. // 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'
  1813. let modifiedOldUserContent: Anthropic.Messages.ContentBlockParam[] // either the last message if its user message, or the user message before the last (assistant) message
  1814. let modifiedApiConversationHistory: ApiMessage[] // need to remove the last user message to replace with new modified user message
  1815. if (existingApiConversationHistory.length > 0) {
  1816. const lastMessage = existingApiConversationHistory[existingApiConversationHistory.length - 1]
  1817. if (lastMessage.role === "assistant") {
  1818. const content = Array.isArray(lastMessage.content)
  1819. ? lastMessage.content
  1820. : [{ type: "text", text: lastMessage.content }]
  1821. const hasToolUse = content.some((block) => block.type === "tool_use")
  1822. if (hasToolUse) {
  1823. const toolUseBlocks = content.filter(
  1824. (block) => block.type === "tool_use",
  1825. ) as Anthropic.Messages.ToolUseBlock[]
  1826. const toolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks.map((block) => ({
  1827. type: "tool_result",
  1828. tool_use_id: block.id,
  1829. content: "Task was interrupted before this tool call could be completed.",
  1830. }))
  1831. modifiedApiConversationHistory = [...existingApiConversationHistory] // no changes
  1832. modifiedOldUserContent = [...toolResponses]
  1833. } else {
  1834. modifiedApiConversationHistory = [...existingApiConversationHistory]
  1835. modifiedOldUserContent = []
  1836. }
  1837. } else if (lastMessage.role === "user") {
  1838. const previousAssistantMessage: ApiMessage | undefined =
  1839. existingApiConversationHistory[existingApiConversationHistory.length - 2]
  1840. const existingUserContent: Anthropic.Messages.ContentBlockParam[] = Array.isArray(lastMessage.content)
  1841. ? lastMessage.content
  1842. : [{ type: "text", text: lastMessage.content }]
  1843. if (previousAssistantMessage && previousAssistantMessage.role === "assistant") {
  1844. const assistantContent = Array.isArray(previousAssistantMessage.content)
  1845. ? previousAssistantMessage.content
  1846. : [{ type: "text", text: previousAssistantMessage.content }]
  1847. const toolUseBlocks = assistantContent.filter(
  1848. (block) => block.type === "tool_use",
  1849. ) as Anthropic.Messages.ToolUseBlock[]
  1850. if (toolUseBlocks.length > 0) {
  1851. const existingToolResults = existingUserContent.filter(
  1852. (block) => block.type === "tool_result",
  1853. ) as Anthropic.ToolResultBlockParam[]
  1854. const missingToolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks
  1855. .filter(
  1856. (toolUse) => !existingToolResults.some((result) => result.tool_use_id === toolUse.id),
  1857. )
  1858. .map((toolUse) => ({
  1859. type: "tool_result",
  1860. tool_use_id: toolUse.id,
  1861. content: "Task was interrupted before this tool call could be completed.",
  1862. }))
  1863. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1) // removes the last user message
  1864. modifiedOldUserContent = [...existingUserContent, ...missingToolResponses]
  1865. } else {
  1866. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  1867. modifiedOldUserContent = [...existingUserContent]
  1868. }
  1869. } else {
  1870. modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
  1871. modifiedOldUserContent = [...existingUserContent]
  1872. }
  1873. } else {
  1874. throw new Error("Unexpected: Last message is not a user or assistant message")
  1875. }
  1876. } else {
  1877. throw new Error("Unexpected: No existing API conversation history")
  1878. }
  1879. let newUserContent: Anthropic.Messages.ContentBlockParam[] = [...modifiedOldUserContent]
  1880. const agoText = ((): string => {
  1881. const timestamp = lastClineMessage?.ts ?? Date.now()
  1882. const now = Date.now()
  1883. const diff = now - timestamp
  1884. const minutes = Math.floor(diff / 60000)
  1885. const hours = Math.floor(minutes / 60)
  1886. const days = Math.floor(hours / 24)
  1887. if (days > 0) {
  1888. return `${days} day${days > 1 ? "s" : ""} ago`
  1889. }
  1890. if (hours > 0) {
  1891. return `${hours} hour${hours > 1 ? "s" : ""} ago`
  1892. }
  1893. if (minutes > 0) {
  1894. return `${minutes} minute${minutes > 1 ? "s" : ""} ago`
  1895. }
  1896. return "just now"
  1897. })()
  1898. if (responseText) {
  1899. newUserContent.push({
  1900. type: "text",
  1901. text: `\n\nNew instructions for task continuation:\n<user_message>\n${responseText}\n</user_message>`,
  1902. })
  1903. }
  1904. if (responseImages && responseImages.length > 0) {
  1905. newUserContent.push(...formatResponse.imageBlocks(responseImages))
  1906. }
  1907. // Ensure we have at least some content to send to the API.
  1908. // If newUserContent is empty, add a minimal resumption message.
  1909. if (newUserContent.length === 0) {
  1910. newUserContent.push({
  1911. type: "text",
  1912. text: "[TASK RESUMPTION] Resuming task...",
  1913. })
  1914. }
  1915. await this.overwriteApiConversationHistory(modifiedApiConversationHistory)
  1916. // Task resuming from history item.
  1917. await this.initiateTaskLoop(newUserContent)
  1918. }
  1919. /**
  1920. * Cancels the current HTTP request if one is in progress.
  1921. * This immediately aborts the underlying stream rather than waiting for the next chunk.
  1922. */
  1923. public cancelCurrentRequest(): void {
  1924. if (this.currentRequestAbortController) {
  1925. console.log(`[Task#${this.taskId}.${this.instanceId}] Aborting current HTTP request`)
  1926. this.currentRequestAbortController.abort()
  1927. this.currentRequestAbortController = undefined
  1928. }
  1929. }
  1930. /**
  1931. * Force emit a final token usage update, ignoring throttle.
  1932. * Called before task completion or abort to ensure final stats are captured.
  1933. * Triggers the debounce with current values and immediately flushes to ensure emit.
  1934. */
  1935. public emitFinalTokenUsageUpdate(): void {
  1936. const tokenUsage = this.getTokenUsage()
  1937. this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage)
  1938. this.debouncedEmitTokenUsage.flush()
  1939. }
  1940. public async abortTask(isAbandoned = false) {
  1941. // Aborting task
  1942. // Will stop any autonomously running promises.
  1943. if (isAbandoned) {
  1944. this.abandoned = true
  1945. }
  1946. this.abort = true
  1947. // Reset consecutive error counters on abort (manual intervention)
  1948. this.consecutiveNoToolUseCount = 0
  1949. this.consecutiveNoAssistantMessagesCount = 0
  1950. // Force final token usage update before abort event
  1951. this.emitFinalTokenUsageUpdate()
  1952. this.emit(RooCodeEventName.TaskAborted)
  1953. try {
  1954. this.dispose() // Call the centralized dispose method
  1955. } catch (error) {
  1956. console.error(`Error during task ${this.taskId}.${this.instanceId} disposal:`, error)
  1957. // Don't rethrow - we want abort to always succeed
  1958. }
  1959. // Save the countdown message in the automatic retry or other content.
  1960. try {
  1961. // Save the countdown message in the automatic retry or other content.
  1962. await this.saveClineMessages()
  1963. } catch (error) {
  1964. console.error(`Error saving messages during abort for task ${this.taskId}.${this.instanceId}:`, error)
  1965. }
  1966. }
  1967. public dispose(): void {
  1968. console.log(`[Task#dispose] disposing task ${this.taskId}.${this.instanceId}`)
  1969. // Cancel any in-progress HTTP request
  1970. try {
  1971. this.cancelCurrentRequest()
  1972. } catch (error) {
  1973. console.error("Error cancelling current request:", error)
  1974. }
  1975. // Remove provider profile change listener
  1976. try {
  1977. if (this.providerProfileChangeListener) {
  1978. const provider = this.providerRef.deref()
  1979. if (provider) {
  1980. provider.off(RooCodeEventName.ProviderProfileChanged, this.providerProfileChangeListener)
  1981. }
  1982. this.providerProfileChangeListener = undefined
  1983. }
  1984. } catch (error) {
  1985. console.error("Error removing provider profile change listener:", error)
  1986. }
  1987. // Dispose message queue and remove event listeners.
  1988. try {
  1989. if (this.messageQueueStateChangedHandler) {
  1990. this.messageQueueService.removeListener("stateChanged", this.messageQueueStateChangedHandler)
  1991. this.messageQueueStateChangedHandler = undefined
  1992. }
  1993. this.messageQueueService.dispose()
  1994. } catch (error) {
  1995. console.error("Error disposing message queue:", error)
  1996. }
  1997. // Remove all event listeners to prevent memory leaks.
  1998. try {
  1999. this.removeAllListeners()
  2000. } catch (error) {
  2001. console.error("Error removing event listeners:", error)
  2002. }
  2003. if (this.enableBridge) {
  2004. BridgeOrchestrator.getInstance()
  2005. ?.unsubscribeFromTask(this.taskId)
  2006. .catch((error) =>
  2007. console.error(
  2008. `[Task#dispose] BridgeOrchestrator#unsubscribeFromTask() failed: ${error instanceof Error ? error.message : String(error)}`,
  2009. ),
  2010. )
  2011. }
  2012. // Release any terminals associated with this task.
  2013. try {
  2014. // Release any terminals associated with this task.
  2015. TerminalRegistry.releaseTerminalsForTask(this.taskId)
  2016. } catch (error) {
  2017. console.error("Error releasing terminals:", error)
  2018. }
  2019. try {
  2020. this.urlContentFetcher.closeBrowser()
  2021. } catch (error) {
  2022. console.error("Error closing URL content fetcher browser:", error)
  2023. }
  2024. try {
  2025. this.browserSession.closeBrowser()
  2026. } catch (error) {
  2027. console.error("Error closing browser session:", error)
  2028. }
  2029. // Also close the Browser Session panel when the task is disposed
  2030. try {
  2031. const provider = this.providerRef.deref()
  2032. if (provider) {
  2033. const { BrowserSessionPanelManager } = require("../webview/BrowserSessionPanelManager")
  2034. BrowserSessionPanelManager.getInstance(provider).dispose()
  2035. }
  2036. } catch (error) {
  2037. console.error("Error closing browser session panel:", error)
  2038. }
  2039. try {
  2040. if (this.rooIgnoreController) {
  2041. this.rooIgnoreController.dispose()
  2042. this.rooIgnoreController = undefined
  2043. }
  2044. } catch (error) {
  2045. console.error("Error disposing RooIgnoreController:", error)
  2046. // This is the critical one for the leak fix.
  2047. }
  2048. try {
  2049. this.fileContextTracker.dispose()
  2050. } catch (error) {
  2051. console.error("Error disposing file context tracker:", error)
  2052. }
  2053. try {
  2054. // If we're not streaming then `abortStream` won't be called.
  2055. if (this.isStreaming && this.diffViewProvider.isEditing) {
  2056. this.diffViewProvider.revertChanges().catch(console.error)
  2057. }
  2058. } catch (error) {
  2059. console.error("Error reverting diff changes:", error)
  2060. }
  2061. }
  2062. // Subtasks
  2063. // Spawn / Wait / Complete
  2064. public async startSubtask(message: string, initialTodos: TodoItem[], mode: string) {
  2065. const provider = this.providerRef.deref()
  2066. if (!provider) {
  2067. throw new Error("Provider not available")
  2068. }
  2069. const child = await (provider as any).delegateParentAndOpenChild({
  2070. parentTaskId: this.taskId,
  2071. message,
  2072. initialTodos,
  2073. mode,
  2074. })
  2075. return child
  2076. }
  2077. /**
  2078. * Resume parent task after delegation completion without showing resume ask.
  2079. * Used in metadata-driven subtask flow.
  2080. *
  2081. * This method:
  2082. * - Clears any pending ask states
  2083. * - Resets abort and streaming flags
  2084. * - Ensures next API call includes full context
  2085. * - Immediately continues task loop without user interaction
  2086. */
  2087. public async resumeAfterDelegation(): Promise<void> {
  2088. // Clear any ask states that might have been set during history load
  2089. this.idleAsk = undefined
  2090. this.resumableAsk = undefined
  2091. this.interactiveAsk = undefined
  2092. // Reset abort and streaming state to ensure clean continuation
  2093. this.abort = false
  2094. this.abandoned = false
  2095. this.abortReason = undefined
  2096. this.didFinishAbortingStream = false
  2097. this.isStreaming = false
  2098. this.isWaitingForFirstChunk = false
  2099. // Ensure next API call includes full context after delegation
  2100. this.skipPrevResponseIdOnce = true
  2101. // Mark as initialized and active
  2102. this.isInitialized = true
  2103. this.emit(RooCodeEventName.TaskActive, this.taskId)
  2104. // Load conversation history if not already loaded
  2105. if (this.apiConversationHistory.length === 0) {
  2106. this.apiConversationHistory = await this.getSavedApiConversationHistory()
  2107. }
  2108. // Add environment details to the existing last user message (which contains the tool_result)
  2109. // This avoids creating a new user message which would cause consecutive user messages
  2110. const environmentDetails = await getEnvironmentDetails(this, true)
  2111. let lastUserMsgIndex = -1
  2112. for (let i = this.apiConversationHistory.length - 1; i >= 0; i--) {
  2113. if (this.apiConversationHistory[i].role === "user") {
  2114. lastUserMsgIndex = i
  2115. break
  2116. }
  2117. }
  2118. if (lastUserMsgIndex >= 0) {
  2119. const lastUserMsg = this.apiConversationHistory[lastUserMsgIndex]
  2120. if (Array.isArray(lastUserMsg.content)) {
  2121. // Remove any existing environment_details blocks before adding fresh ones
  2122. const contentWithoutEnvDetails = lastUserMsg.content.filter(
  2123. (block: Anthropic.Messages.ContentBlockParam) => {
  2124. if (block.type === "text" && typeof block.text === "string") {
  2125. const isEnvironmentDetailsBlock =
  2126. block.text.trim().startsWith("<environment_details>") &&
  2127. block.text.trim().endsWith("</environment_details>")
  2128. return !isEnvironmentDetailsBlock
  2129. }
  2130. return true
  2131. },
  2132. )
  2133. // Add fresh environment details
  2134. lastUserMsg.content = [...contentWithoutEnvDetails, { type: "text" as const, text: environmentDetails }]
  2135. }
  2136. }
  2137. // Save the updated history
  2138. await this.saveApiConversationHistory()
  2139. // Continue task loop - pass empty array to signal no new user content needed
  2140. // The initiateTaskLoop will handle this by skipping user message addition
  2141. await this.initiateTaskLoop([])
  2142. }
  2143. // Task Loop
  2144. private async initiateTaskLoop(userContent: Anthropic.Messages.ContentBlockParam[]): Promise<void> {
  2145. // Kicks off the checkpoints initialization process in the background.
  2146. getCheckpointService(this)
  2147. let nextUserContent = userContent
  2148. let includeFileDetails = true
  2149. this.emit(RooCodeEventName.TaskStarted)
  2150. while (!this.abort) {
  2151. const didEndLoop = await this.recursivelyMakeClineRequests(nextUserContent, includeFileDetails)
  2152. includeFileDetails = false // We only need file details the first time.
  2153. // The way this agentic loop works is that cline will be given a
  2154. // task that he then calls tools to complete. Unless there's an
  2155. // attempt_completion call, we keep responding back to him with his
  2156. // tool's responses until he either attempt_completion or does not
  2157. // use anymore tools. If he does not use anymore tools, we ask him
  2158. // to consider if he's completed the task and then call
  2159. // attempt_completion, otherwise proceed with completing the task.
  2160. // There is a MAX_REQUESTS_PER_TASK limit to prevent infinite
  2161. // requests, but Cline is prompted to finish the task as efficiently
  2162. // as he can.
  2163. if (didEndLoop) {
  2164. // For now a task never 'completes'. This will only happen if
  2165. // the user hits max requests and denies resetting the count.
  2166. break
  2167. } else {
  2168. // Use the task's locked protocol, NOT the current settings (fallback to xml if not set)
  2169. nextUserContent = [{ type: "text", text: formatResponse.noToolsUsed(this._taskToolProtocol ?? "xml") }]
  2170. }
  2171. }
  2172. }
  2173. public async recursivelyMakeClineRequests(
  2174. userContent: Anthropic.Messages.ContentBlockParam[],
  2175. includeFileDetails: boolean = false,
  2176. ): Promise<boolean> {
  2177. interface StackItem {
  2178. userContent: Anthropic.Messages.ContentBlockParam[]
  2179. includeFileDetails: boolean
  2180. retryAttempt?: number
  2181. userMessageWasRemoved?: boolean // Track if user message was removed due to empty response
  2182. }
  2183. const stack: StackItem[] = [{ userContent, includeFileDetails, retryAttempt: 0 }]
  2184. while (stack.length > 0) {
  2185. const currentItem = stack.pop()!
  2186. const currentUserContent = currentItem.userContent
  2187. const currentIncludeFileDetails = currentItem.includeFileDetails
  2188. if (this.abort) {
  2189. throw new Error(`[RooCode#recursivelyMakeRooRequests] task ${this.taskId}.${this.instanceId} aborted`)
  2190. }
  2191. if (this.consecutiveMistakeLimit > 0 && this.consecutiveMistakeCount >= this.consecutiveMistakeLimit) {
  2192. // Track consecutive mistake errors in telemetry via event and PostHog exception tracking.
  2193. // The reason is "no_tools_used" because this limit is reached via initiateTaskLoop
  2194. // which increments consecutiveMistakeCount when the model doesn't use any tools.
  2195. TelemetryService.instance.captureConsecutiveMistakeError(this.taskId)
  2196. TelemetryService.instance.captureException(
  2197. new ConsecutiveMistakeError(
  2198. `Task reached consecutive mistake limit (${this.consecutiveMistakeLimit})`,
  2199. this.taskId,
  2200. this.consecutiveMistakeCount,
  2201. this.consecutiveMistakeLimit,
  2202. "no_tools_used",
  2203. this.apiConfiguration.apiProvider,
  2204. getModelId(this.apiConfiguration),
  2205. ),
  2206. )
  2207. const { response, text, images } = await this.ask(
  2208. "mistake_limit_reached",
  2209. t("common:errors.mistake_limit_guidance"),
  2210. )
  2211. if (response === "messageResponse") {
  2212. currentUserContent.push(
  2213. ...[
  2214. { type: "text" as const, text: formatResponse.tooManyMistakes(text) },
  2215. ...formatResponse.imageBlocks(images),
  2216. ],
  2217. )
  2218. await this.say("user_feedback", text, images)
  2219. }
  2220. this.consecutiveMistakeCount = 0
  2221. }
  2222. // Getting verbose details is an expensive operation, it uses ripgrep to
  2223. // top-down build file structure of project which for large projects can
  2224. // take a few seconds. For the best UX we show a placeholder api_req_started
  2225. // message with a loading spinner as this happens.
  2226. // Determine API protocol based on provider and model
  2227. const modelId = getModelId(this.apiConfiguration)
  2228. const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
  2229. // Respect user-configured provider rate limiting BEFORE we emit api_req_started.
  2230. // This prevents the UI from showing an "API Request..." spinner while we are
  2231. // intentionally waiting due to the rate limit slider.
  2232. //
  2233. // NOTE: We also set Task.lastGlobalApiRequestTime here to reserve this slot
  2234. // before we build environment details (which can take time).
  2235. // This ensures subsequent requests (including subtasks) still honour the
  2236. // provider rate-limit window.
  2237. await this.maybeWaitForProviderRateLimit(currentItem.retryAttempt ?? 0)
  2238. Task.lastGlobalApiRequestTime = performance.now()
  2239. await this.say(
  2240. "api_req_started",
  2241. JSON.stringify({
  2242. apiProtocol,
  2243. }),
  2244. )
  2245. const {
  2246. showRooIgnoredFiles = false,
  2247. includeDiagnosticMessages = true,
  2248. maxDiagnosticMessages = 50,
  2249. maxReadFileLine = -1,
  2250. } = (await this.providerRef.deref()?.getState()) ?? {}
  2251. const { content: parsedUserContent, mode: slashCommandMode } = await processUserContentMentions({
  2252. userContent: currentUserContent,
  2253. cwd: this.cwd,
  2254. urlContentFetcher: this.urlContentFetcher,
  2255. fileContextTracker: this.fileContextTracker,
  2256. rooIgnoreController: this.rooIgnoreController,
  2257. showRooIgnoredFiles,
  2258. includeDiagnosticMessages,
  2259. maxDiagnosticMessages,
  2260. maxReadFileLine,
  2261. })
  2262. // Switch mode if specified in a slash command's frontmatter
  2263. if (slashCommandMode) {
  2264. const provider = this.providerRef.deref()
  2265. if (provider) {
  2266. const state = await provider.getState()
  2267. const targetMode = getModeBySlug(slashCommandMode, state?.customModes)
  2268. if (targetMode) {
  2269. await provider.handleModeSwitch(slashCommandMode)
  2270. }
  2271. }
  2272. }
  2273. const environmentDetails = await getEnvironmentDetails(this, currentIncludeFileDetails)
  2274. // Remove any existing environment_details blocks before adding fresh ones.
  2275. // This prevents duplicate environment details when resuming tasks with XML tool calls,
  2276. // where the old user message content may already contain environment details from the previous session.
  2277. // We check for both opening and closing tags to ensure we're matching complete environment detail blocks,
  2278. // not just mentions of the tag in regular content.
  2279. const contentWithoutEnvDetails = parsedUserContent.filter((block) => {
  2280. if (block.type === "text" && typeof block.text === "string") {
  2281. // Check if this text block is a complete environment_details block
  2282. // by verifying it starts with the opening tag and ends with the closing tag
  2283. const isEnvironmentDetailsBlock =
  2284. block.text.trim().startsWith("<environment_details>") &&
  2285. block.text.trim().endsWith("</environment_details>")
  2286. return !isEnvironmentDetailsBlock
  2287. }
  2288. return true
  2289. })
  2290. // Add environment details as its own text block, separate from tool
  2291. // results.
  2292. let finalUserContent = [...contentWithoutEnvDetails, { type: "text" as const, text: environmentDetails }]
  2293. // Only add user message to conversation history if:
  2294. // 1. This is the first attempt (retryAttempt === 0), AND
  2295. // 2. The original userContent was not empty (empty signals delegation resume where
  2296. // the user message with tool_result and env details is already in history), OR
  2297. // 3. The message was removed in a previous iteration (userMessageWasRemoved === true)
  2298. // This prevents consecutive user messages while allowing re-add when needed
  2299. const isEmptyUserContent = currentUserContent.length === 0
  2300. const shouldAddUserMessage =
  2301. ((currentItem.retryAttempt ?? 0) === 0 && !isEmptyUserContent) || currentItem.userMessageWasRemoved
  2302. if (shouldAddUserMessage) {
  2303. await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
  2304. TelemetryService.instance.captureConversationMessage(this.taskId, "user")
  2305. }
  2306. // Since we sent off a placeholder api_req_started message to update the
  2307. // webview while waiting to actually start the API request (to load
  2308. // potential details for example), we need to update the text of that
  2309. // message.
  2310. const lastApiReqIndex = findLastIndex(this.clineMessages, (m) => m.say === "api_req_started")
  2311. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  2312. apiProtocol,
  2313. } satisfies ClineApiReqInfo)
  2314. await this.saveClineMessages()
  2315. await this.providerRef.deref()?.postStateToWebview()
  2316. try {
  2317. let cacheWriteTokens = 0
  2318. let cacheReadTokens = 0
  2319. let inputTokens = 0
  2320. let outputTokens = 0
  2321. let totalCost: number | undefined
  2322. // We can't use `api_req_finished` anymore since it's a unique case
  2323. // where it could come after a streaming message (i.e. in the middle
  2324. // of being updated or executed).
  2325. // Fortunately `api_req_finished` was always parsed out for the GUI
  2326. // anyways, so it remains solely for legacy purposes to keep track
  2327. // of prices in tasks from history (it's worth removing a few months
  2328. // from now).
  2329. const updateApiReqMsg = (cancelReason?: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  2330. if (lastApiReqIndex < 0 || !this.clineMessages[lastApiReqIndex]) {
  2331. return
  2332. }
  2333. const existingData = JSON.parse(this.clineMessages[lastApiReqIndex].text || "{}")
  2334. // Calculate total tokens and cost using provider-aware function
  2335. const modelId = getModelId(this.apiConfiguration)
  2336. const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
  2337. const costResult =
  2338. apiProtocol === "anthropic"
  2339. ? calculateApiCostAnthropic(
  2340. streamModelInfo,
  2341. inputTokens,
  2342. outputTokens,
  2343. cacheWriteTokens,
  2344. cacheReadTokens,
  2345. )
  2346. : calculateApiCostOpenAI(
  2347. streamModelInfo,
  2348. inputTokens,
  2349. outputTokens,
  2350. cacheWriteTokens,
  2351. cacheReadTokens,
  2352. )
  2353. this.clineMessages[lastApiReqIndex].text = JSON.stringify({
  2354. ...existingData,
  2355. tokensIn: costResult.totalInputTokens,
  2356. tokensOut: costResult.totalOutputTokens,
  2357. cacheWrites: cacheWriteTokens,
  2358. cacheReads: cacheReadTokens,
  2359. cost: totalCost ?? costResult.totalCost,
  2360. cancelReason,
  2361. streamingFailedMessage,
  2362. } satisfies ClineApiReqInfo)
  2363. }
  2364. const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
  2365. if (this.diffViewProvider.isEditing) {
  2366. await this.diffViewProvider.revertChanges() // closes diff view
  2367. }
  2368. // if last message is a partial we need to update and save it
  2369. const lastMessage = this.clineMessages.at(-1)
  2370. if (lastMessage && lastMessage.partial) {
  2371. // lastMessage.ts = Date.now() DO NOT update ts since it is used as a key for virtuoso list
  2372. lastMessage.partial = false
  2373. // instead of streaming partialMessage events, we do a save and post like normal to persist to disk
  2374. }
  2375. // Update `api_req_started` to have cancelled and cost, so that
  2376. // we can display the cost of the partial stream and the cancellation reason
  2377. updateApiReqMsg(cancelReason, streamingFailedMessage)
  2378. await this.saveClineMessages()
  2379. // Signals to provider that it can retrieve the saved messages
  2380. // from disk, as abortTask can not be awaited on in nature.
  2381. this.didFinishAbortingStream = true
  2382. }
  2383. // Reset streaming state for each new API request
  2384. this.currentStreamingContentIndex = 0
  2385. this.currentStreamingDidCheckpoint = false
  2386. this.assistantMessageContent = []
  2387. this.didCompleteReadingStream = false
  2388. this.userMessageContent = []
  2389. this.userMessageContentReady = false
  2390. this.didRejectTool = false
  2391. this.didAlreadyUseTool = false
  2392. // Reset tool failure flag for each new assistant turn - this ensures that tool failures
  2393. // only prevent attempt_completion within the same assistant message, not across turns
  2394. // (e.g., if a tool fails, then user sends a message saying "just complete anyway")
  2395. this.didToolFailInCurrentTurn = false
  2396. this.presentAssistantMessageLocked = false
  2397. this.presentAssistantMessageHasPendingUpdates = false
  2398. this.assistantMessageParser?.reset()
  2399. this.streamingToolCallIndices.clear()
  2400. // Clear any leftover streaming tool call state from previous interrupted streams
  2401. NativeToolCallParser.clearAllStreamingToolCalls()
  2402. NativeToolCallParser.clearRawChunkState()
  2403. await this.diffViewProvider.reset()
  2404. // Cache model info once per API request to avoid repeated calls during streaming
  2405. // This is especially important for tools and background usage collection
  2406. this.cachedStreamingModel = this.api.getModel()
  2407. const streamModelInfo = this.cachedStreamingModel.info
  2408. const cachedModelId = this.cachedStreamingModel.id
  2409. // Use the task's locked protocol instead of resolving fresh.
  2410. // This ensures task resumption works correctly even if NTC settings changed.
  2411. // Fallback to resolving if somehow _taskToolProtocol is not set (should not happen).
  2412. const streamProtocol = resolveToolProtocol(
  2413. this.apiConfiguration,
  2414. streamModelInfo,
  2415. this._taskToolProtocol,
  2416. )
  2417. const shouldUseXmlParser = streamProtocol === "xml"
  2418. // Yields only if the first chunk is successful, otherwise will
  2419. // allow the user to retry the request (most likely due to rate
  2420. // limit error, which gets thrown on the first chunk).
  2421. const stream = this.attemptApiRequest(currentItem.retryAttempt ?? 0, { skipProviderRateLimit: true })
  2422. let assistantMessage = ""
  2423. let reasoningMessage = ""
  2424. let pendingGroundingSources: GroundingSource[] = []
  2425. this.isStreaming = true
  2426. try {
  2427. const iterator = stream[Symbol.asyncIterator]()
  2428. // Helper to race iterator.next() with abort signal
  2429. const nextChunkWithAbort = async () => {
  2430. const nextPromise = iterator.next()
  2431. // If we have an abort controller, race it with the next chunk
  2432. if (this.currentRequestAbortController) {
  2433. const abortPromise = new Promise<never>((_, reject) => {
  2434. const signal = this.currentRequestAbortController!.signal
  2435. if (signal.aborted) {
  2436. reject(new Error("Request cancelled by user"))
  2437. } else {
  2438. signal.addEventListener("abort", () => {
  2439. reject(new Error("Request cancelled by user"))
  2440. })
  2441. }
  2442. })
  2443. return await Promise.race([nextPromise, abortPromise])
  2444. }
  2445. // No abort controller, just return the next chunk normally
  2446. return await nextPromise
  2447. }
  2448. let item = await nextChunkWithAbort()
  2449. while (!item.done) {
  2450. const chunk = item.value
  2451. item = await nextChunkWithAbort()
  2452. if (!chunk) {
  2453. // Sometimes chunk is undefined, no idea that can cause
  2454. // it, but this workaround seems to fix it.
  2455. continue
  2456. }
  2457. switch (chunk.type) {
  2458. case "reasoning": {
  2459. reasoningMessage += chunk.text
  2460. // Only apply formatting if the message contains sentence-ending punctuation followed by **
  2461. let formattedReasoning = reasoningMessage
  2462. if (reasoningMessage.includes("**")) {
  2463. // Add line breaks before **Title** patterns that appear after sentence endings
  2464. // This targets section headers like "...end of sentence.**Title Here**"
  2465. // Handles periods, exclamation marks, and question marks
  2466. formattedReasoning = reasoningMessage.replace(
  2467. /([.!?])\*\*([^*\n]+)\*\*/g,
  2468. "$1\n\n**$2**",
  2469. )
  2470. }
  2471. await this.say("reasoning", formattedReasoning, undefined, true)
  2472. break
  2473. }
  2474. case "usage":
  2475. inputTokens += chunk.inputTokens
  2476. outputTokens += chunk.outputTokens
  2477. cacheWriteTokens += chunk.cacheWriteTokens ?? 0
  2478. cacheReadTokens += chunk.cacheReadTokens ?? 0
  2479. totalCost = chunk.totalCost
  2480. break
  2481. case "grounding":
  2482. // Handle grounding sources separately from regular content
  2483. // to prevent state persistence issues - store them separately
  2484. if (chunk.sources && chunk.sources.length > 0) {
  2485. pendingGroundingSources.push(...chunk.sources)
  2486. }
  2487. break
  2488. case "tool_call_partial": {
  2489. // Process raw tool call chunk through NativeToolCallParser
  2490. // which handles tracking, buffering, and emits events
  2491. const events = NativeToolCallParser.processRawChunk({
  2492. index: chunk.index,
  2493. id: chunk.id,
  2494. name: chunk.name,
  2495. arguments: chunk.arguments,
  2496. })
  2497. for (const event of events) {
  2498. if (event.type === "tool_call_start") {
  2499. // Initialize streaming in NativeToolCallParser
  2500. NativeToolCallParser.startStreamingToolCall(event.id, event.name as ToolName)
  2501. // Before adding a new tool, finalize any preceding text block
  2502. // This prevents the text block from blocking tool presentation
  2503. const lastBlock =
  2504. this.assistantMessageContent[this.assistantMessageContent.length - 1]
  2505. if (lastBlock?.type === "text" && lastBlock.partial) {
  2506. lastBlock.partial = false
  2507. }
  2508. // Track the index where this tool will be stored
  2509. const toolUseIndex = this.assistantMessageContent.length
  2510. this.streamingToolCallIndices.set(event.id, toolUseIndex)
  2511. // Create initial partial tool use
  2512. const partialToolUse: ToolUse = {
  2513. type: "tool_use",
  2514. name: event.name as ToolName,
  2515. params: {},
  2516. partial: true,
  2517. }
  2518. // Store the ID for native protocol
  2519. ;(partialToolUse as any).id = event.id
  2520. // Add to content and present
  2521. this.assistantMessageContent.push(partialToolUse)
  2522. this.userMessageContentReady = false
  2523. presentAssistantMessage(this)
  2524. } else if (event.type === "tool_call_delta") {
  2525. // Process chunk using streaming JSON parser
  2526. const partialToolUse = NativeToolCallParser.processStreamingChunk(
  2527. event.id,
  2528. event.delta,
  2529. )
  2530. if (partialToolUse) {
  2531. // Get the index for this tool call
  2532. const toolUseIndex = this.streamingToolCallIndices.get(event.id)
  2533. if (toolUseIndex !== undefined) {
  2534. // Store the ID for native protocol
  2535. ;(partialToolUse as any).id = event.id
  2536. // Update the existing tool use with new partial data
  2537. this.assistantMessageContent[toolUseIndex] = partialToolUse
  2538. // Present updated tool use
  2539. presentAssistantMessage(this)
  2540. }
  2541. }
  2542. } else if (event.type === "tool_call_end") {
  2543. // Finalize the streaming tool call
  2544. const finalToolUse = NativeToolCallParser.finalizeStreamingToolCall(event.id)
  2545. // Get the index for this tool call
  2546. const toolUseIndex = this.streamingToolCallIndices.get(event.id)
  2547. if (finalToolUse) {
  2548. // Store the tool call ID
  2549. ;(finalToolUse as any).id = event.id
  2550. // Get the index and replace partial with final
  2551. if (toolUseIndex !== undefined) {
  2552. this.assistantMessageContent[toolUseIndex] = finalToolUse
  2553. }
  2554. // Clean up tracking
  2555. this.streamingToolCallIndices.delete(event.id)
  2556. // Mark that we have new content to process
  2557. this.userMessageContentReady = false
  2558. // Present the finalized tool call
  2559. presentAssistantMessage(this)
  2560. } else if (toolUseIndex !== undefined) {
  2561. // finalizeStreamingToolCall returned null (malformed JSON or missing args)
  2562. // We still need to mark the tool as non-partial so it gets executed
  2563. // The tool's validation will catch any missing required parameters
  2564. const existingToolUse = this.assistantMessageContent[toolUseIndex]
  2565. if (existingToolUse && existingToolUse.type === "tool_use") {
  2566. existingToolUse.partial = false
  2567. // Ensure it has the ID for native protocol
  2568. ;(existingToolUse as any).id = event.id
  2569. }
  2570. // Clean up tracking
  2571. this.streamingToolCallIndices.delete(event.id)
  2572. // Mark that we have new content to process
  2573. this.userMessageContentReady = false
  2574. // Present the tool call - validation will handle missing params
  2575. presentAssistantMessage(this)
  2576. }
  2577. }
  2578. }
  2579. break
  2580. }
  2581. case "tool_call": {
  2582. // Legacy: Handle complete tool calls (for backward compatibility)
  2583. // Convert native tool call to ToolUse format
  2584. const toolUse = NativeToolCallParser.parseToolCall({
  2585. id: chunk.id,
  2586. name: chunk.name as ToolName,
  2587. arguments: chunk.arguments,
  2588. })
  2589. if (!toolUse) {
  2590. console.error(`Failed to parse tool call for task ${this.taskId}:`, chunk)
  2591. break
  2592. }
  2593. // Store the tool call ID on the ToolUse object for later reference
  2594. // This is needed to create tool_result blocks that reference the correct tool_use_id
  2595. toolUse.id = chunk.id
  2596. // Add the tool use to assistant message content
  2597. this.assistantMessageContent.push(toolUse)
  2598. // Mark that we have new content to process
  2599. this.userMessageContentReady = false
  2600. // Present the tool call to user - presentAssistantMessage will execute
  2601. // tools sequentially and accumulate all results in userMessageContent
  2602. presentAssistantMessage(this)
  2603. break
  2604. }
  2605. case "text": {
  2606. assistantMessage += chunk.text
  2607. // Use the protocol determined at the start of streaming
  2608. // Don't rely solely on parser existence - parser might exist from previous state
  2609. if (shouldUseXmlParser && this.assistantMessageParser) {
  2610. // XML protocol: Parse raw assistant message chunk into content blocks
  2611. const prevLength = this.assistantMessageContent.length
  2612. this.assistantMessageContent = this.assistantMessageParser.processChunk(chunk.text)
  2613. if (this.assistantMessageContent.length > prevLength) {
  2614. // New content we need to present, reset to
  2615. // false in case previous content set this to true.
  2616. this.userMessageContentReady = false
  2617. }
  2618. // Present content to user.
  2619. presentAssistantMessage(this)
  2620. } else {
  2621. // Native protocol: Text chunks are plain text, not XML tool calls
  2622. // Create or update a text content block directly
  2623. const lastBlock =
  2624. this.assistantMessageContent[this.assistantMessageContent.length - 1]
  2625. if (lastBlock?.type === "text" && lastBlock.partial) {
  2626. // Update existing partial text block
  2627. lastBlock.content = assistantMessage
  2628. } else {
  2629. // Create new text block
  2630. this.assistantMessageContent.push({
  2631. type: "text",
  2632. content: assistantMessage,
  2633. partial: true,
  2634. })
  2635. this.userMessageContentReady = false
  2636. }
  2637. // Present content to user
  2638. presentAssistantMessage(this)
  2639. }
  2640. break
  2641. }
  2642. }
  2643. if (this.abort) {
  2644. console.log(`aborting stream, this.abandoned = ${this.abandoned}`)
  2645. if (!this.abandoned) {
  2646. // Only need to gracefully abort if this instance
  2647. // isn't abandoned (sometimes OpenRouter stream
  2648. // hangs, in which case this would affect future
  2649. // instances of Cline).
  2650. await abortStream("user_cancelled")
  2651. }
  2652. break // Aborts the stream.
  2653. }
  2654. if (this.didRejectTool) {
  2655. // `userContent` has a tool rejection, so interrupt the
  2656. // assistant's response to present the user's feedback.
  2657. assistantMessage += "\n\n[Response interrupted by user feedback]"
  2658. // Instead of setting this preemptively, we allow the
  2659. // present iterator to finish and set
  2660. // userMessageContentReady when its ready.
  2661. // this.userMessageContentReady = true
  2662. break
  2663. }
  2664. if (this.didAlreadyUseTool) {
  2665. assistantMessage +=
  2666. "\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.]"
  2667. break
  2668. }
  2669. }
  2670. // Finalize any remaining streaming tool calls that weren't explicitly ended
  2671. // This is critical for MCP tools which need tool_call_end events to be properly
  2672. // converted from ToolUse to McpToolUse via finalizeStreamingToolCall()
  2673. const finalizeEvents = NativeToolCallParser.finalizeRawChunks()
  2674. for (const event of finalizeEvents) {
  2675. if (event.type === "tool_call_end") {
  2676. // Finalize the streaming tool call
  2677. const finalToolUse = NativeToolCallParser.finalizeStreamingToolCall(event.id)
  2678. // Get the index for this tool call
  2679. const toolUseIndex = this.streamingToolCallIndices.get(event.id)
  2680. if (finalToolUse) {
  2681. // Store the tool call ID
  2682. ;(finalToolUse as any).id = event.id
  2683. // Get the index and replace partial with final
  2684. if (toolUseIndex !== undefined) {
  2685. this.assistantMessageContent[toolUseIndex] = finalToolUse
  2686. }
  2687. // Clean up tracking
  2688. this.streamingToolCallIndices.delete(event.id)
  2689. // Mark that we have new content to process
  2690. this.userMessageContentReady = false
  2691. // Present the finalized tool call
  2692. presentAssistantMessage(this)
  2693. } else if (toolUseIndex !== undefined) {
  2694. // finalizeStreamingToolCall returned null (malformed JSON or missing args)
  2695. // We still need to mark the tool as non-partial so it gets executed
  2696. // The tool's validation will catch any missing required parameters
  2697. const existingToolUse = this.assistantMessageContent[toolUseIndex]
  2698. if (existingToolUse && existingToolUse.type === "tool_use") {
  2699. existingToolUse.partial = false
  2700. // Ensure it has the ID for native protocol
  2701. ;(existingToolUse as any).id = event.id
  2702. }
  2703. // Clean up tracking
  2704. this.streamingToolCallIndices.delete(event.id)
  2705. // Mark that we have new content to process
  2706. this.userMessageContentReady = false
  2707. // Present the tool call - validation will handle missing params
  2708. presentAssistantMessage(this)
  2709. }
  2710. }
  2711. }
  2712. // Create a copy of current token values to avoid race conditions
  2713. const currentTokens = {
  2714. input: inputTokens,
  2715. output: outputTokens,
  2716. cacheWrite: cacheWriteTokens,
  2717. cacheRead: cacheReadTokens,
  2718. total: totalCost,
  2719. }
  2720. const drainStreamInBackgroundToFindAllUsage = async (apiReqIndex: number) => {
  2721. const timeoutMs = DEFAULT_USAGE_COLLECTION_TIMEOUT_MS
  2722. const startTime = performance.now()
  2723. const modelId = getModelId(this.apiConfiguration)
  2724. // Local variables to accumulate usage data without affecting the main flow
  2725. let bgInputTokens = currentTokens.input
  2726. let bgOutputTokens = currentTokens.output
  2727. let bgCacheWriteTokens = currentTokens.cacheWrite
  2728. let bgCacheReadTokens = currentTokens.cacheRead
  2729. let bgTotalCost = currentTokens.total
  2730. // Helper function to capture telemetry and update messages
  2731. const captureUsageData = async (
  2732. tokens: {
  2733. input: number
  2734. output: number
  2735. cacheWrite: number
  2736. cacheRead: number
  2737. total?: number
  2738. },
  2739. messageIndex: number = apiReqIndex,
  2740. ) => {
  2741. if (
  2742. tokens.input > 0 ||
  2743. tokens.output > 0 ||
  2744. tokens.cacheWrite > 0 ||
  2745. tokens.cacheRead > 0
  2746. ) {
  2747. // Update the shared variables atomically
  2748. inputTokens = tokens.input
  2749. outputTokens = tokens.output
  2750. cacheWriteTokens = tokens.cacheWrite
  2751. cacheReadTokens = tokens.cacheRead
  2752. totalCost = tokens.total
  2753. // Update the API request message with the latest usage data
  2754. updateApiReqMsg()
  2755. await this.saveClineMessages()
  2756. // Update the specific message in the webview
  2757. const apiReqMessage = this.clineMessages[messageIndex]
  2758. if (apiReqMessage) {
  2759. await this.updateClineMessage(apiReqMessage)
  2760. }
  2761. // Capture telemetry with provider-aware cost calculation
  2762. const modelId = getModelId(this.apiConfiguration)
  2763. const apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
  2764. // Use the appropriate cost function based on the API protocol
  2765. const costResult =
  2766. apiProtocol === "anthropic"
  2767. ? calculateApiCostAnthropic(
  2768. streamModelInfo,
  2769. tokens.input,
  2770. tokens.output,
  2771. tokens.cacheWrite,
  2772. tokens.cacheRead,
  2773. )
  2774. : calculateApiCostOpenAI(
  2775. streamModelInfo,
  2776. tokens.input,
  2777. tokens.output,
  2778. tokens.cacheWrite,
  2779. tokens.cacheRead,
  2780. )
  2781. TelemetryService.instance.captureLlmCompletion(this.taskId, {
  2782. inputTokens: costResult.totalInputTokens,
  2783. outputTokens: costResult.totalOutputTokens,
  2784. cacheWriteTokens: tokens.cacheWrite,
  2785. cacheReadTokens: tokens.cacheRead,
  2786. cost: tokens.total ?? costResult.totalCost,
  2787. })
  2788. }
  2789. }
  2790. try {
  2791. // Continue processing the original stream from where the main loop left off
  2792. let usageFound = false
  2793. let chunkCount = 0
  2794. // Use the same iterator that the main loop was using
  2795. while (!item.done) {
  2796. // Check for timeout
  2797. if (performance.now() - startTime > timeoutMs) {
  2798. console.warn(
  2799. `[Background Usage Collection] Timed out after ${timeoutMs}ms for model: ${modelId}, processed ${chunkCount} chunks`,
  2800. )
  2801. // Clean up the iterator before breaking
  2802. if (iterator.return) {
  2803. await iterator.return(undefined)
  2804. }
  2805. break
  2806. }
  2807. const chunk = item.value
  2808. item = await iterator.next()
  2809. chunkCount++
  2810. if (chunk && chunk.type === "usage") {
  2811. usageFound = true
  2812. bgInputTokens += chunk.inputTokens
  2813. bgOutputTokens += chunk.outputTokens
  2814. bgCacheWriteTokens += chunk.cacheWriteTokens ?? 0
  2815. bgCacheReadTokens += chunk.cacheReadTokens ?? 0
  2816. bgTotalCost = chunk.totalCost
  2817. }
  2818. }
  2819. if (
  2820. usageFound ||
  2821. bgInputTokens > 0 ||
  2822. bgOutputTokens > 0 ||
  2823. bgCacheWriteTokens > 0 ||
  2824. bgCacheReadTokens > 0
  2825. ) {
  2826. // We have usage data either from a usage chunk or accumulated tokens
  2827. await captureUsageData(
  2828. {
  2829. input: bgInputTokens,
  2830. output: bgOutputTokens,
  2831. cacheWrite: bgCacheWriteTokens,
  2832. cacheRead: bgCacheReadTokens,
  2833. total: bgTotalCost,
  2834. },
  2835. lastApiReqIndex,
  2836. )
  2837. } else {
  2838. console.warn(
  2839. `[Background Usage Collection] Suspicious: request ${apiReqIndex} is complete, but no usage info was found. Model: ${modelId}`,
  2840. )
  2841. }
  2842. } catch (error) {
  2843. console.error("Error draining stream for usage data:", error)
  2844. // Still try to capture whatever usage data we have collected so far
  2845. if (
  2846. bgInputTokens > 0 ||
  2847. bgOutputTokens > 0 ||
  2848. bgCacheWriteTokens > 0 ||
  2849. bgCacheReadTokens > 0
  2850. ) {
  2851. await captureUsageData(
  2852. {
  2853. input: bgInputTokens,
  2854. output: bgOutputTokens,
  2855. cacheWrite: bgCacheWriteTokens,
  2856. cacheRead: bgCacheReadTokens,
  2857. total: bgTotalCost,
  2858. },
  2859. lastApiReqIndex,
  2860. )
  2861. }
  2862. }
  2863. }
  2864. // Start the background task and handle any errors
  2865. drainStreamInBackgroundToFindAllUsage(lastApiReqIndex).catch((error) => {
  2866. console.error("Background usage collection failed:", error)
  2867. })
  2868. } catch (error) {
  2869. // Abandoned happens when extension is no longer waiting for the
  2870. // Cline instance to finish aborting (error is thrown here when
  2871. // any function in the for loop throws due to this.abort).
  2872. if (!this.abandoned) {
  2873. // Determine cancellation reason
  2874. const cancelReason: ClineApiReqCancelReason = this.abort ? "user_cancelled" : "streaming_failed"
  2875. const rawErrorMessage = error.message ?? JSON.stringify(serializeError(error), null, 2)
  2876. const streamingFailedMessage = this.abort
  2877. ? undefined
  2878. : `${t("common:interruption.streamTerminatedByProvider")}: ${rawErrorMessage}`
  2879. // Clean up partial state
  2880. await abortStream(cancelReason, streamingFailedMessage)
  2881. if (this.abort) {
  2882. // User cancelled - abort the entire task
  2883. this.abortReason = cancelReason
  2884. await this.abortTask()
  2885. } else {
  2886. // Stream failed - log the error and retry with the same content
  2887. // The existing rate limiting will prevent rapid retries
  2888. console.error(
  2889. `[Task#${this.taskId}.${this.instanceId}] Stream failed, will retry: ${streamingFailedMessage}`,
  2890. )
  2891. // Apply exponential backoff similar to first-chunk errors when auto-resubmit is enabled
  2892. const stateForBackoff = await this.providerRef.deref()?.getState()
  2893. if (stateForBackoff?.autoApprovalEnabled) {
  2894. await this.backoffAndAnnounce(currentItem.retryAttempt ?? 0, error)
  2895. // Check if task was aborted during the backoff
  2896. if (this.abort) {
  2897. console.log(
  2898. `[Task#${this.taskId}.${this.instanceId}] Task aborted during mid-stream retry backoff`,
  2899. )
  2900. // Abort the entire task
  2901. this.abortReason = "user_cancelled"
  2902. await this.abortTask()
  2903. break
  2904. }
  2905. }
  2906. // Push the same content back onto the stack to retry, incrementing the retry attempt counter
  2907. stack.push({
  2908. userContent: currentUserContent,
  2909. includeFileDetails: false,
  2910. retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
  2911. })
  2912. // Continue to retry the request
  2913. continue
  2914. }
  2915. }
  2916. } finally {
  2917. this.isStreaming = false
  2918. // Clean up the abort controller when streaming completes
  2919. this.currentRequestAbortController = undefined
  2920. }
  2921. // Need to call here in case the stream was aborted.
  2922. if (this.abort || this.abandoned) {
  2923. throw new Error(
  2924. `[RooCode#recursivelyMakeRooRequests] task ${this.taskId}.${this.instanceId} aborted`,
  2925. )
  2926. }
  2927. this.didCompleteReadingStream = true
  2928. // Set any blocks to be complete to allow `presentAssistantMessage`
  2929. // to finish and set `userMessageContentReady` to true.
  2930. // (Could be a text block that had no subsequent tool uses, or a
  2931. // text block at the very end, or an invalid tool use, etc. Whatever
  2932. // the case, `presentAssistantMessage` relies on these blocks either
  2933. // to be completed or the user to reject a block in order to proceed
  2934. // and eventually set userMessageContentReady to true.)
  2935. const partialBlocks = this.assistantMessageContent.filter((block) => block.partial)
  2936. partialBlocks.forEach((block) => (block.partial = false))
  2937. // Can't just do this b/c a tool could be in the middle of executing.
  2938. // this.assistantMessageContent.forEach((e) => (e.partial = false))
  2939. // Now that the stream is complete, finalize any remaining partial content blocks (XML protocol only)
  2940. // Use the protocol determined at the start of streaming
  2941. if (shouldUseXmlParser && this.assistantMessageParser) {
  2942. this.assistantMessageParser.finalizeContentBlocks()
  2943. const parsedBlocks = this.assistantMessageParser.getContentBlocks()
  2944. // For XML protocol: Use only parsed blocks (includes both text and tool_use parsed from XML)
  2945. this.assistantMessageContent = parsedBlocks
  2946. }
  2947. // Present any partial blocks that were just completed
  2948. // For XML protocol: includes both text and tool_use blocks parsed from the text stream
  2949. // For native protocol: tool_use blocks were already presented during streaming via
  2950. // tool_call_partial events, but we still need to present them if they exist (e.g., malformed)
  2951. if (partialBlocks.length > 0) {
  2952. // If there is content to update then it will complete and
  2953. // update `this.userMessageContentReady` to true, which we
  2954. // `pWaitFor` before making the next request.
  2955. presentAssistantMessage(this)
  2956. }
  2957. // Note: updateApiReqMsg() is now called from within drainStreamInBackgroundToFindAllUsage
  2958. // to ensure usage data is captured even when the stream is interrupted. The background task
  2959. // uses local variables to accumulate usage data before atomically updating the shared state.
  2960. // Complete the reasoning message if it exists
  2961. // We can't use say() here because the reasoning message may not be the last message
  2962. // (other messages like text blocks or tool uses may have been added after it during streaming)
  2963. if (reasoningMessage) {
  2964. const lastReasoningIndex = findLastIndex(
  2965. this.clineMessages,
  2966. (m) => m.type === "say" && m.say === "reasoning",
  2967. )
  2968. if (lastReasoningIndex !== -1 && this.clineMessages[lastReasoningIndex].partial) {
  2969. this.clineMessages[lastReasoningIndex].partial = false
  2970. await this.updateClineMessage(this.clineMessages[lastReasoningIndex])
  2971. }
  2972. }
  2973. await this.saveClineMessages()
  2974. await this.providerRef.deref()?.postStateToWebview()
  2975. // Reset parser after each complete conversation round (XML protocol only)
  2976. this.assistantMessageParser?.reset()
  2977. // Now add to apiConversationHistory.
  2978. // Need to save assistant responses to file before proceeding to
  2979. // tool use since user can exit at any moment and we wouldn't be
  2980. // able to save the assistant's response.
  2981. // Check if we have any content to process (text or tool uses)
  2982. const hasTextContent = assistantMessage.length > 0
  2983. const hasToolUses = this.assistantMessageContent.some(
  2984. (block) => block.type === "tool_use" || block.type === "mcp_tool_use",
  2985. )
  2986. if (hasTextContent || hasToolUses) {
  2987. // Reset counter when we get a successful response with content
  2988. this.consecutiveNoAssistantMessagesCount = 0
  2989. // Display grounding sources to the user if they exist
  2990. if (pendingGroundingSources.length > 0) {
  2991. const citationLinks = pendingGroundingSources.map((source, i) => `[${i + 1}](${source.url})`)
  2992. const sourcesText = `${t("common:gemini.sources")} ${citationLinks.join(", ")}`
  2993. await this.say("text", sourcesText, undefined, false, undefined, undefined, {
  2994. isNonInteractive: true,
  2995. })
  2996. }
  2997. // Build the assistant message content array
  2998. const assistantContent: Array<Anthropic.TextBlockParam | Anthropic.ToolUseBlockParam> = []
  2999. // Add text content if present
  3000. if (assistantMessage) {
  3001. assistantContent.push({
  3002. type: "text" as const,
  3003. text: assistantMessage,
  3004. })
  3005. }
  3006. // Add tool_use blocks with their IDs for native protocol
  3007. // This handles both regular ToolUse and McpToolUse types
  3008. const toolUseBlocks = this.assistantMessageContent.filter(
  3009. (block) => block.type === "tool_use" || block.type === "mcp_tool_use",
  3010. )
  3011. for (const block of toolUseBlocks) {
  3012. if (block.type === "mcp_tool_use") {
  3013. // McpToolUse already has the original tool name (e.g., "mcp_serverName_toolName")
  3014. // The arguments are the raw tool arguments (matching the simplified schema)
  3015. const mcpBlock = block as import("../../shared/tools").McpToolUse
  3016. if (mcpBlock.id) {
  3017. assistantContent.push({
  3018. type: "tool_use" as const,
  3019. id: mcpBlock.id,
  3020. name: mcpBlock.name, // Original dynamic name
  3021. input: mcpBlock.arguments, // Direct tool arguments
  3022. })
  3023. }
  3024. } else {
  3025. // Regular ToolUse
  3026. const toolUse = block as import("../../shared/tools").ToolUse
  3027. const toolCallId = toolUse.id
  3028. if (toolCallId) {
  3029. // nativeArgs is already in the correct API format for all tools
  3030. const input = toolUse.nativeArgs || toolUse.params
  3031. // Use originalName (alias) if present for API history consistency.
  3032. // When tool aliases are used (e.g., "edit_file" -> "search_and_replace"),
  3033. // we want the alias name in the conversation history to match what the model
  3034. // was told the tool was named, preventing confusion in multi-turn conversations.
  3035. const toolNameForHistory = toolUse.originalName ?? toolUse.name
  3036. assistantContent.push({
  3037. type: "tool_use" as const,
  3038. id: toolCallId,
  3039. name: toolNameForHistory,
  3040. input,
  3041. })
  3042. }
  3043. }
  3044. }
  3045. await this.addToApiConversationHistory(
  3046. { role: "assistant", content: assistantContent },
  3047. reasoningMessage || undefined,
  3048. )
  3049. TelemetryService.instance.captureConversationMessage(this.taskId, "assistant")
  3050. // NOTE: This comment is here for future reference - this was a
  3051. // workaround for `userMessageContent` not getting set to true.
  3052. // It was due to it not recursively calling for partial blocks
  3053. // when `didRejectTool`, so it would get stuck waiting for a
  3054. // partial block to complete before it could continue.
  3055. // In case the content blocks finished it may be the api stream
  3056. // finished after the last parsed content block was executed, so
  3057. // we are able to detect out of bounds and set
  3058. // `userMessageContentReady` to true (note you should not call
  3059. // `presentAssistantMessage` since if the last block i
  3060. // completed it will be presented again).
  3061. // const completeBlocks = this.assistantMessageContent.filter((block) => !block.partial) // If there are any partial blocks after the stream ended we can consider them invalid.
  3062. // if (this.currentStreamingContentIndex >= completeBlocks.length) {
  3063. // this.userMessageContentReady = true
  3064. // }
  3065. await pWaitFor(() => this.userMessageContentReady)
  3066. // If the model did not tool use, then we need to tell it to
  3067. // either use a tool or attempt_completion.
  3068. const didToolUse = this.assistantMessageContent.some(
  3069. (block) => block.type === "tool_use" || block.type === "mcp_tool_use",
  3070. )
  3071. if (!didToolUse) {
  3072. // Increment consecutive no-tool-use counter
  3073. this.consecutiveNoToolUseCount++
  3074. // Only show error and count toward mistake limit after 2 consecutive failures
  3075. if (this.consecutiveNoToolUseCount >= 2) {
  3076. await this.say("error", "MODEL_NO_TOOLS_USED")
  3077. // Only count toward mistake limit after second consecutive failure
  3078. this.consecutiveMistakeCount++
  3079. }
  3080. // Use the task's locked protocol for consistent behavior
  3081. this.userMessageContent.push({
  3082. type: "text",
  3083. text: formatResponse.noToolsUsed(this._taskToolProtocol ?? "xml"),
  3084. })
  3085. } else {
  3086. // Reset counter when tools are used successfully
  3087. this.consecutiveNoToolUseCount = 0
  3088. }
  3089. // Push to stack if there's content OR if we're paused waiting for a subtask.
  3090. // When paused, we push an empty item so the loop continues to the pause check.
  3091. if (this.userMessageContent.length > 0 || this.isPaused) {
  3092. stack.push({
  3093. userContent: [...this.userMessageContent], // Create a copy to avoid mutation issues
  3094. includeFileDetails: false, // Subsequent iterations don't need file details
  3095. })
  3096. // Add periodic yielding to prevent blocking
  3097. await new Promise((resolve) => setImmediate(resolve))
  3098. }
  3099. continue
  3100. } else {
  3101. // If there's no assistant_responses, that means we got no text
  3102. // or tool_use content blocks from API which we should assume is
  3103. // an error.
  3104. // Increment consecutive no-assistant-messages counter
  3105. this.consecutiveNoAssistantMessagesCount++
  3106. // Only show error and count toward mistake limit after 2 consecutive failures
  3107. // This provides a "grace retry" - first failure retries silently
  3108. if (this.consecutiveNoAssistantMessagesCount >= 2) {
  3109. await this.say("error", "MODEL_NO_ASSISTANT_MESSAGES")
  3110. }
  3111. // IMPORTANT: For native tool protocol, we already added the user message to
  3112. // apiConversationHistory at line 1876. Since the assistant failed to respond,
  3113. // we need to remove that message before retrying to avoid having two consecutive
  3114. // user messages (which would cause tool_result validation errors).
  3115. let state = await this.providerRef.deref()?.getState()
  3116. // Use the task's locked protocol, NOT current settings
  3117. if (isNativeProtocol(this._taskToolProtocol ?? "xml") && this.apiConversationHistory.length > 0) {
  3118. const lastMessage = this.apiConversationHistory[this.apiConversationHistory.length - 1]
  3119. if (lastMessage.role === "user") {
  3120. // Remove the last user message that we added earlier
  3121. this.apiConversationHistory.pop()
  3122. }
  3123. }
  3124. // Check if we should auto-retry or prompt the user
  3125. // Reuse the state variable from above
  3126. if (state?.autoApprovalEnabled) {
  3127. // Auto-retry with backoff - don't persist failure message when retrying
  3128. await this.backoffAndAnnounce(
  3129. currentItem.retryAttempt ?? 0,
  3130. new Error(
  3131. "Unexpected API Response: The language model did not provide any assistant messages. This may indicate an issue with the API or the model's output.",
  3132. ),
  3133. )
  3134. // Check if task was aborted during the backoff
  3135. if (this.abort) {
  3136. console.log(
  3137. `[Task#${this.taskId}.${this.instanceId}] Task aborted during empty-assistant retry backoff`,
  3138. )
  3139. break
  3140. }
  3141. // Push the same content back onto the stack to retry, incrementing the retry attempt counter
  3142. // Mark that user message was removed so it gets re-added on retry
  3143. stack.push({
  3144. userContent: currentUserContent,
  3145. includeFileDetails: false,
  3146. retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
  3147. userMessageWasRemoved: true,
  3148. })
  3149. // Continue to retry the request
  3150. continue
  3151. } else {
  3152. // Prompt the user for retry decision
  3153. const { response } = await this.ask(
  3154. "api_req_failed",
  3155. "The model returned no assistant messages. This may indicate an issue with the API or the model's output.",
  3156. )
  3157. if (response === "yesButtonClicked") {
  3158. await this.say("api_req_retried")
  3159. // Push the same content back to retry
  3160. stack.push({
  3161. userContent: currentUserContent,
  3162. includeFileDetails: false,
  3163. retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
  3164. })
  3165. // Continue to retry the request
  3166. continue
  3167. } else {
  3168. // User declined to retry
  3169. // For native protocol, re-add the user message we removed
  3170. // Use the task's locked protocol, NOT current settings
  3171. if (isNativeProtocol(this._taskToolProtocol ?? "xml")) {
  3172. await this.addToApiConversationHistory({
  3173. role: "user",
  3174. content: currentUserContent,
  3175. })
  3176. }
  3177. await this.say(
  3178. "error",
  3179. "Unexpected API Response: The language model did not provide any assistant messages. This may indicate an issue with the API or the model's output.",
  3180. )
  3181. await this.addToApiConversationHistory({
  3182. role: "assistant",
  3183. content: [{ type: "text", text: "Failure: I did not provide a response." }],
  3184. })
  3185. }
  3186. }
  3187. }
  3188. // If we reach here without continuing, return false (will always be false for now)
  3189. return false
  3190. } catch (error) {
  3191. // This should never happen since the only thing that can throw an
  3192. // error is the attemptApiRequest, which is wrapped in a try catch
  3193. // that sends an ask where if noButtonClicked, will clear current
  3194. // task and destroy this instance. However to avoid unhandled
  3195. // promise rejection, we will end this loop which will end execution
  3196. // of this instance (see `startTask`).
  3197. return true // Needs to be true so parent loop knows to end task.
  3198. }
  3199. }
  3200. // If we exit the while loop normally (stack is empty), return false
  3201. return false
  3202. }
  3203. private async getSystemPrompt(): Promise<string> {
  3204. const { mcpEnabled } = (await this.providerRef.deref()?.getState()) ?? {}
  3205. let mcpHub: McpHub | undefined
  3206. if (mcpEnabled ?? true) {
  3207. const provider = this.providerRef.deref()
  3208. if (!provider) {
  3209. throw new Error("Provider reference lost during view transition")
  3210. }
  3211. // Wait for MCP hub initialization through McpServerManager
  3212. mcpHub = await McpServerManager.getInstance(provider.context, provider)
  3213. if (!mcpHub) {
  3214. throw new Error("Failed to get MCP hub from server manager")
  3215. }
  3216. // Wait for MCP servers to be connected before generating system prompt
  3217. await pWaitFor(() => !mcpHub!.isConnecting, { timeout: 10_000 }).catch(() => {
  3218. console.error("MCP servers failed to connect in time")
  3219. })
  3220. }
  3221. const rooIgnoreInstructions = this.rooIgnoreController?.getInstructions()
  3222. const state = await this.providerRef.deref()?.getState()
  3223. const {
  3224. browserViewportSize,
  3225. mode,
  3226. customModes,
  3227. customModePrompts,
  3228. customInstructions,
  3229. experiments,
  3230. enableMcpServerCreation,
  3231. browserToolEnabled,
  3232. language,
  3233. maxConcurrentFileReads,
  3234. maxReadFileLine,
  3235. apiConfiguration,
  3236. enableSubfolderRules,
  3237. } = state ?? {}
  3238. return await (async () => {
  3239. const provider = this.providerRef.deref()
  3240. if (!provider) {
  3241. throw new Error("Provider not available")
  3242. }
  3243. // Align browser tool enablement with generateSystemPrompt: require model image support,
  3244. // mode to include the browser group, and the user setting to be enabled.
  3245. const modeConfig = getModeBySlug(mode ?? defaultModeSlug, customModes)
  3246. const modeSupportsBrowser = modeConfig?.groups.some((group) => getGroupName(group) === "browser") ?? false
  3247. // Check if model supports browser capability (images)
  3248. const modelInfo = this.api.getModel().info
  3249. const modelSupportsBrowser = (modelInfo as any)?.supportsImages === true
  3250. const canUseBrowserTool = modelSupportsBrowser && modeSupportsBrowser && (browserToolEnabled ?? true)
  3251. // Use the task's locked protocol for system prompt consistency.
  3252. // This ensures the system prompt matches the protocol the task was started with,
  3253. // even if user settings have changed since then.
  3254. const toolProtocol = resolveToolProtocol(
  3255. apiConfiguration ?? this.apiConfiguration,
  3256. modelInfo,
  3257. this._taskToolProtocol,
  3258. )
  3259. return SYSTEM_PROMPT(
  3260. provider.context,
  3261. this.cwd,
  3262. canUseBrowserTool,
  3263. mcpHub,
  3264. this.diffStrategy,
  3265. browserViewportSize ?? "900x600",
  3266. mode ?? defaultModeSlug,
  3267. customModePrompts,
  3268. customModes,
  3269. customInstructions,
  3270. this.diffEnabled,
  3271. experiments,
  3272. enableMcpServerCreation,
  3273. language,
  3274. rooIgnoreInstructions,
  3275. maxReadFileLine !== -1,
  3276. {
  3277. maxConcurrentFileReads: maxConcurrentFileReads ?? 5,
  3278. todoListEnabled: apiConfiguration?.todoListEnabled ?? true,
  3279. browserToolEnabled: browserToolEnabled ?? true,
  3280. useAgentRules:
  3281. vscode.workspace.getConfiguration(Package.name).get<boolean>("useAgentRules") ?? true,
  3282. enableSubfolderRules: enableSubfolderRules ?? false,
  3283. newTaskRequireTodos: vscode.workspace
  3284. .getConfiguration(Package.name)
  3285. .get<boolean>("newTaskRequireTodos", false),
  3286. toolProtocol,
  3287. isStealthModel: modelInfo?.isStealthModel,
  3288. },
  3289. undefined, // todoList
  3290. this.api.getModel().id,
  3291. provider.getSkillsManager(),
  3292. )
  3293. })()
  3294. }
  3295. private getCurrentProfileId(state: any): string {
  3296. return (
  3297. state?.listApiConfigMeta?.find((profile: any) => profile.name === state?.currentApiConfigName)?.id ??
  3298. "default"
  3299. )
  3300. }
  3301. private async handleContextWindowExceededError(): Promise<void> {
  3302. const state = await this.providerRef.deref()?.getState()
  3303. const { profileThresholds = {} } = state ?? {}
  3304. const { contextTokens } = this.getTokenUsage()
  3305. const modelInfo = this.api.getModel().info
  3306. const maxTokens = getModelMaxOutputTokens({
  3307. modelId: this.api.getModel().id,
  3308. model: modelInfo,
  3309. settings: this.apiConfiguration,
  3310. })
  3311. const contextWindow = modelInfo.contextWindow
  3312. // Get the current profile ID using the helper method
  3313. const currentProfileId = this.getCurrentProfileId(state)
  3314. // Log the context window error for debugging
  3315. console.warn(
  3316. `[Task#${this.taskId}] Context window exceeded for model ${this.api.getModel().id}. ` +
  3317. `Current tokens: ${contextTokens}, Context window: ${contextWindow}. ` +
  3318. `Forcing truncation to ${FORCED_CONTEXT_REDUCTION_PERCENT}% of current context.`,
  3319. )
  3320. // Determine if we're using native tool protocol for proper message handling
  3321. // Use the task's locked protocol, NOT the current settings
  3322. const useNativeTools = isNativeProtocol(this._taskToolProtocol ?? "xml")
  3323. // Send condenseTaskContextStarted to show in-progress indicator
  3324. await this.providerRef.deref()?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId })
  3325. // Force aggressive truncation by keeping only 75% of the conversation history
  3326. const truncateResult = await manageContext({
  3327. messages: this.apiConversationHistory,
  3328. totalTokens: contextTokens || 0,
  3329. maxTokens,
  3330. contextWindow,
  3331. apiHandler: this.api,
  3332. autoCondenseContext: true,
  3333. autoCondenseContextPercent: FORCED_CONTEXT_REDUCTION_PERCENT,
  3334. systemPrompt: await this.getSystemPrompt(),
  3335. taskId: this.taskId,
  3336. profileThresholds,
  3337. currentProfileId,
  3338. useNativeTools,
  3339. })
  3340. if (truncateResult.messages !== this.apiConversationHistory) {
  3341. await this.overwriteApiConversationHistory(truncateResult.messages)
  3342. }
  3343. if (truncateResult.summary) {
  3344. const { summary, cost, prevContextTokens, newContextTokens = 0 } = truncateResult
  3345. const contextCondense: ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
  3346. await this.say(
  3347. "condense_context",
  3348. undefined /* text */,
  3349. undefined /* images */,
  3350. false /* partial */,
  3351. undefined /* checkpoint */,
  3352. undefined /* progressStatus */,
  3353. { isNonInteractive: true } /* options */,
  3354. contextCondense,
  3355. )
  3356. } else if (truncateResult.truncationId) {
  3357. // Sliding window truncation occurred (fallback when condensing fails or is disabled)
  3358. const contextTruncation: ContextTruncation = {
  3359. truncationId: truncateResult.truncationId,
  3360. messagesRemoved: truncateResult.messagesRemoved ?? 0,
  3361. prevContextTokens: truncateResult.prevContextTokens,
  3362. newContextTokens: truncateResult.newContextTokensAfterTruncation ?? 0,
  3363. }
  3364. await this.say(
  3365. "sliding_window_truncation",
  3366. undefined /* text */,
  3367. undefined /* images */,
  3368. false /* partial */,
  3369. undefined /* checkpoint */,
  3370. undefined /* progressStatus */,
  3371. { isNonInteractive: true } /* options */,
  3372. undefined /* contextCondense */,
  3373. contextTruncation,
  3374. )
  3375. }
  3376. // Notify webview that context management is complete (removes in-progress spinner)
  3377. await this.providerRef.deref()?.postMessageToWebview({ type: "condenseTaskContextResponse", text: this.taskId })
  3378. }
  3379. /**
  3380. * Enforce the user-configured provider rate limit.
  3381. *
  3382. * NOTE: This is intentionally treated as expected behavior and is surfaced via
  3383. * the `api_req_rate_limit_wait` say type (not an error).
  3384. */
  3385. private async maybeWaitForProviderRateLimit(retryAttempt: number): Promise<void> {
  3386. const state = await this.providerRef.deref()?.getState()
  3387. const rateLimitSeconds =
  3388. state?.apiConfiguration?.rateLimitSeconds ?? this.apiConfiguration?.rateLimitSeconds ?? 0
  3389. if (rateLimitSeconds <= 0 || !Task.lastGlobalApiRequestTime) {
  3390. return
  3391. }
  3392. const now = performance.now()
  3393. const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
  3394. const rateLimitDelay = Math.ceil(
  3395. Math.min(rateLimitSeconds, Math.max(0, rateLimitSeconds * 1000 - timeSinceLastRequest) / 1000),
  3396. )
  3397. // Only show the countdown UX on the first attempt. Retry flows have their own delay messaging.
  3398. if (rateLimitDelay > 0 && retryAttempt === 0) {
  3399. for (let i = rateLimitDelay; i > 0; i--) {
  3400. // Send structured JSON data for i18n-safe transport
  3401. const delayMessage = JSON.stringify({ seconds: i })
  3402. await this.say("api_req_rate_limit_wait", delayMessage, undefined, true)
  3403. await delay(1000)
  3404. }
  3405. // Finalize the partial message so the UI doesn't keep rendering an in-progress spinner.
  3406. await this.say("api_req_rate_limit_wait", undefined, undefined, false)
  3407. }
  3408. }
  3409. public async *attemptApiRequest(
  3410. retryAttempt: number = 0,
  3411. options: { skipProviderRateLimit?: boolean } = {},
  3412. ): ApiStream {
  3413. const state = await this.providerRef.deref()?.getState()
  3414. const {
  3415. apiConfiguration,
  3416. autoApprovalEnabled,
  3417. requestDelaySeconds,
  3418. mode,
  3419. autoCondenseContext = true,
  3420. autoCondenseContextPercent = 100,
  3421. profileThresholds = {},
  3422. } = state ?? {}
  3423. // Get condensing configuration for automatic triggers.
  3424. const customCondensingPrompt = state?.customCondensingPrompt
  3425. const condensingApiConfigId = state?.condensingApiConfigId
  3426. const listApiConfigMeta = state?.listApiConfigMeta
  3427. // Determine API handler to use for condensing.
  3428. let condensingApiHandler: ApiHandler | undefined
  3429. if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
  3430. // Find matching config by ID
  3431. const matchingConfig = listApiConfigMeta.find((config) => config.id === condensingApiConfigId)
  3432. if (matchingConfig) {
  3433. const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
  3434. id: condensingApiConfigId,
  3435. })
  3436. // Ensure profile and apiProvider exist before trying to build handler.
  3437. if (profile && profile.apiProvider) {
  3438. condensingApiHandler = buildApiHandler(profile)
  3439. }
  3440. }
  3441. }
  3442. if (!options.skipProviderRateLimit) {
  3443. await this.maybeWaitForProviderRateLimit(retryAttempt)
  3444. }
  3445. // Update last request time right before making the request so that subsequent
  3446. // requests — even from new subtasks — will honour the provider's rate-limit.
  3447. //
  3448. // NOTE: When recursivelyMakeClineRequests handles rate limiting, it sets the
  3449. // timestamp earlier to include the environment details build. We still set it
  3450. // here for direct callers (tests) and for the case where we didn't rate-limit
  3451. // in the caller.
  3452. Task.lastGlobalApiRequestTime = performance.now()
  3453. const systemPrompt = await this.getSystemPrompt()
  3454. const { contextTokens } = this.getTokenUsage()
  3455. if (contextTokens) {
  3456. const modelInfo = this.api.getModel().info
  3457. const maxTokens = getModelMaxOutputTokens({
  3458. modelId: this.api.getModel().id,
  3459. model: modelInfo,
  3460. settings: this.apiConfiguration,
  3461. })
  3462. const contextWindow = modelInfo.contextWindow
  3463. // Get the current profile ID using the helper method
  3464. const currentProfileId = this.getCurrentProfileId(state)
  3465. // Determine if we're using native tool protocol for proper message handling
  3466. // Use the task's locked protocol, NOT the current settings
  3467. const useNativeTools = isNativeProtocol(this._taskToolProtocol ?? "xml")
  3468. // Check if context management will likely run (threshold check)
  3469. // This allows us to show an in-progress indicator to the user
  3470. // We use the centralized willManageContext helper to avoid duplicating threshold logic
  3471. const lastMessage = this.apiConversationHistory[this.apiConversationHistory.length - 1]
  3472. const lastMessageContent = lastMessage?.content
  3473. let lastMessageTokens = 0
  3474. if (lastMessageContent) {
  3475. lastMessageTokens = Array.isArray(lastMessageContent)
  3476. ? await this.api.countTokens(lastMessageContent)
  3477. : await this.api.countTokens([{ type: "text", text: lastMessageContent as string }])
  3478. }
  3479. const contextManagementWillRun = willManageContext({
  3480. totalTokens: contextTokens,
  3481. contextWindow,
  3482. maxTokens,
  3483. autoCondenseContext,
  3484. autoCondenseContextPercent,
  3485. profileThresholds,
  3486. currentProfileId,
  3487. lastMessageTokens,
  3488. })
  3489. // Send condenseTaskContextStarted BEFORE manageContext to show in-progress indicator
  3490. // This notification must be sent here (not earlier) because the early check uses stale token count
  3491. // (before user message is added to history), which could incorrectly skip showing the indicator
  3492. if (contextManagementWillRun && autoCondenseContext) {
  3493. await this.providerRef
  3494. .deref()
  3495. ?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId })
  3496. }
  3497. const truncateResult = await manageContext({
  3498. messages: this.apiConversationHistory,
  3499. totalTokens: contextTokens,
  3500. maxTokens,
  3501. contextWindow,
  3502. apiHandler: this.api,
  3503. autoCondenseContext,
  3504. autoCondenseContextPercent,
  3505. systemPrompt,
  3506. taskId: this.taskId,
  3507. customCondensingPrompt,
  3508. condensingApiHandler,
  3509. profileThresholds,
  3510. currentProfileId,
  3511. useNativeTools,
  3512. })
  3513. if (truncateResult.messages !== this.apiConversationHistory) {
  3514. await this.overwriteApiConversationHistory(truncateResult.messages)
  3515. }
  3516. if (truncateResult.error) {
  3517. await this.say("condense_context_error", truncateResult.error)
  3518. } else if (truncateResult.summary) {
  3519. const { summary, cost, prevContextTokens, newContextTokens = 0, condenseId } = truncateResult
  3520. const contextCondense: ContextCondense = {
  3521. summary,
  3522. cost,
  3523. newContextTokens,
  3524. prevContextTokens,
  3525. condenseId,
  3526. }
  3527. await this.say(
  3528. "condense_context",
  3529. undefined /* text */,
  3530. undefined /* images */,
  3531. false /* partial */,
  3532. undefined /* checkpoint */,
  3533. undefined /* progressStatus */,
  3534. { isNonInteractive: true } /* options */,
  3535. contextCondense,
  3536. )
  3537. } else if (truncateResult.truncationId) {
  3538. // Sliding window truncation occurred (fallback when condensing fails or is disabled)
  3539. const contextTruncation: ContextTruncation = {
  3540. truncationId: truncateResult.truncationId,
  3541. messagesRemoved: truncateResult.messagesRemoved ?? 0,
  3542. prevContextTokens: truncateResult.prevContextTokens,
  3543. newContextTokens: truncateResult.newContextTokensAfterTruncation ?? 0,
  3544. }
  3545. await this.say(
  3546. "sliding_window_truncation",
  3547. undefined /* text */,
  3548. undefined /* images */,
  3549. false /* partial */,
  3550. undefined /* checkpoint */,
  3551. undefined /* progressStatus */,
  3552. { isNonInteractive: true } /* options */,
  3553. undefined /* contextCondense */,
  3554. contextTruncation,
  3555. )
  3556. }
  3557. // Notify webview that context management is complete (sets isCondensing = false)
  3558. // This removes the in-progress spinner and allows the completed result to show
  3559. if (contextManagementWillRun && autoCondenseContext) {
  3560. await this.providerRef
  3561. .deref()
  3562. ?.postMessageToWebview({ type: "condenseTaskContextResponse", text: this.taskId })
  3563. }
  3564. }
  3565. // Get the effective API history by filtering out condensed messages
  3566. // This allows non-destructive condensing where messages are tagged but not deleted,
  3567. // enabling accurate rewind operations while still sending condensed history to the API.
  3568. const effectiveHistory = getEffectiveApiHistory(this.apiConversationHistory)
  3569. const messagesSinceLastSummary = getMessagesSinceLastSummary(effectiveHistory)
  3570. const messagesWithoutImages = maybeRemoveImageBlocks(messagesSinceLastSummary, this.api)
  3571. const cleanConversationHistory = this.buildCleanConversationHistory(messagesWithoutImages as ApiMessage[])
  3572. // Check auto-approval limits
  3573. const approvalResult = await this.autoApprovalHandler.checkAutoApprovalLimits(
  3574. state,
  3575. this.combineMessages(this.clineMessages.slice(1)),
  3576. async (type, data) => this.ask(type, data),
  3577. )
  3578. if (!approvalResult.shouldProceed) {
  3579. // User did not approve, task should be aborted
  3580. throw new Error("Auto-approval limit reached and user did not approve continuation")
  3581. }
  3582. // Determine if we should include native tools based on:
  3583. // 1. Task's locked tool protocol is set to NATIVE
  3584. // 2. Model supports native tools
  3585. // CRITICAL: Use the task's locked protocol to ensure tasks that started with XML
  3586. // tools continue using XML even if NTC settings have since changed.
  3587. const modelInfo = this.api.getModel().info
  3588. const taskProtocol = this._taskToolProtocol ?? "xml"
  3589. const shouldIncludeTools = taskProtocol === TOOL_PROTOCOL.NATIVE && (modelInfo.supportsNativeTools ?? false)
  3590. // Build complete tools array: native tools + dynamic MCP tools, filtered by mode restrictions
  3591. let allTools: OpenAI.Chat.ChatCompletionTool[] = []
  3592. if (shouldIncludeTools) {
  3593. const provider = this.providerRef.deref()
  3594. if (!provider) {
  3595. throw new Error("Provider reference lost during tool building")
  3596. }
  3597. allTools = await buildNativeToolsArray({
  3598. provider,
  3599. cwd: this.cwd,
  3600. mode,
  3601. customModes: state?.customModes,
  3602. experiments: state?.experiments,
  3603. apiConfiguration,
  3604. maxReadFileLine: state?.maxReadFileLine ?? -1,
  3605. maxConcurrentFileReads: state?.maxConcurrentFileReads ?? 5,
  3606. browserToolEnabled: state?.browserToolEnabled ?? true,
  3607. modelInfo,
  3608. diffEnabled: this.diffEnabled,
  3609. })
  3610. }
  3611. // Parallel tool calls are disabled - feature is on hold
  3612. // Previously resolved from experiments.isEnabled(..., EXPERIMENT_IDS.MULTIPLE_NATIVE_TOOL_CALLS)
  3613. const parallelToolCallsEnabled = false
  3614. const metadata: ApiHandlerCreateMessageMetadata = {
  3615. mode: mode,
  3616. taskId: this.taskId,
  3617. suppressPreviousResponseId: this.skipPrevResponseIdOnce,
  3618. // Include tools and tool protocol when using native protocol and model supports it
  3619. ...(shouldIncludeTools
  3620. ? {
  3621. tools: allTools,
  3622. tool_choice: "auto",
  3623. toolProtocol: taskProtocol,
  3624. parallelToolCalls: parallelToolCallsEnabled,
  3625. }
  3626. : {}),
  3627. }
  3628. // Create an AbortController to allow cancelling the request mid-stream
  3629. this.currentRequestAbortController = new AbortController()
  3630. const abortSignal = this.currentRequestAbortController.signal
  3631. // Reset the flag after using it
  3632. this.skipPrevResponseIdOnce = false
  3633. // The provider accepts reasoning items alongside standard messages; cast to the expected parameter type.
  3634. const stream = this.api.createMessage(
  3635. systemPrompt,
  3636. cleanConversationHistory as unknown as Anthropic.Messages.MessageParam[],
  3637. metadata,
  3638. )
  3639. const iterator = stream[Symbol.asyncIterator]()
  3640. // Set up abort handling - when the signal is aborted, clean up the controller reference
  3641. abortSignal.addEventListener("abort", () => {
  3642. console.log(`[Task#${this.taskId}.${this.instanceId}] AbortSignal triggered for current request`)
  3643. this.currentRequestAbortController = undefined
  3644. })
  3645. try {
  3646. // Awaiting first chunk to see if it will throw an error.
  3647. this.isWaitingForFirstChunk = true
  3648. // Race between the first chunk and the abort signal
  3649. const firstChunkPromise = iterator.next()
  3650. const abortPromise = new Promise<never>((_, reject) => {
  3651. if (abortSignal.aborted) {
  3652. reject(new Error("Request cancelled by user"))
  3653. } else {
  3654. abortSignal.addEventListener("abort", () => {
  3655. reject(new Error("Request cancelled by user"))
  3656. })
  3657. }
  3658. })
  3659. const firstChunk = await Promise.race([firstChunkPromise, abortPromise])
  3660. yield firstChunk.value
  3661. this.isWaitingForFirstChunk = false
  3662. } catch (error) {
  3663. this.isWaitingForFirstChunk = false
  3664. this.currentRequestAbortController = undefined
  3665. const isContextWindowExceededError = checkContextWindowExceededError(error)
  3666. // If it's a context window error and we haven't exceeded max retries for this error type
  3667. if (isContextWindowExceededError && retryAttempt < MAX_CONTEXT_WINDOW_RETRIES) {
  3668. console.warn(
  3669. `[Task#${this.taskId}] Context window exceeded for model ${this.api.getModel().id}. ` +
  3670. `Retry attempt ${retryAttempt + 1}/${MAX_CONTEXT_WINDOW_RETRIES}. ` +
  3671. `Attempting automatic truncation...`,
  3672. )
  3673. await this.handleContextWindowExceededError()
  3674. // Retry the request after handling the context window error
  3675. yield* this.attemptApiRequest(retryAttempt + 1)
  3676. return
  3677. }
  3678. // 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.
  3679. if (autoApprovalEnabled) {
  3680. // Apply shared exponential backoff and countdown UX
  3681. await this.backoffAndAnnounce(retryAttempt, error)
  3682. // CRITICAL: Check if task was aborted during the backoff countdown
  3683. // This prevents infinite loops when users cancel during auto-retry
  3684. // Without this check, the recursive call below would continue even after abort
  3685. if (this.abort) {
  3686. throw new Error(
  3687. `[Task#attemptApiRequest] task ${this.taskId}.${this.instanceId} aborted during retry`,
  3688. )
  3689. }
  3690. // Delegate generator output from the recursive call with
  3691. // incremented retry count.
  3692. yield* this.attemptApiRequest(retryAttempt + 1)
  3693. return
  3694. } else {
  3695. const { response } = await this.ask(
  3696. "api_req_failed",
  3697. error.message ?? JSON.stringify(serializeError(error), null, 2),
  3698. )
  3699. if (response !== "yesButtonClicked") {
  3700. // This will never happen since if noButtonClicked, we will
  3701. // clear current task, aborting this instance.
  3702. throw new Error("API request failed")
  3703. }
  3704. await this.say("api_req_retried")
  3705. // Delegate generator output from the recursive call.
  3706. yield* this.attemptApiRequest()
  3707. return
  3708. }
  3709. }
  3710. // No error, so we can continue to yield all remaining chunks.
  3711. // (Needs to be placed outside of try/catch since it we want caller to
  3712. // handle errors not with api_req_failed as that is reserved for first
  3713. // chunk failures only.)
  3714. // This delegates to another generator or iterable object. In this case,
  3715. // it's saying "yield all remaining values from this iterator". This
  3716. // effectively passes along all subsequent chunks from the original
  3717. // stream.
  3718. yield* iterator
  3719. }
  3720. // Shared exponential backoff for retries (first-chunk and mid-stream)
  3721. private async backoffAndAnnounce(retryAttempt: number, error: any): Promise<void> {
  3722. try {
  3723. const state = await this.providerRef.deref()?.getState()
  3724. const baseDelay = state?.requestDelaySeconds || 5
  3725. let exponentialDelay = Math.min(
  3726. Math.ceil(baseDelay * Math.pow(2, retryAttempt)),
  3727. MAX_EXPONENTIAL_BACKOFF_SECONDS,
  3728. )
  3729. // Respect provider rate limit window
  3730. let rateLimitDelay = 0
  3731. const rateLimit = (state?.apiConfiguration ?? this.apiConfiguration)?.rateLimitSeconds || 0
  3732. if (Task.lastGlobalApiRequestTime && rateLimit > 0) {
  3733. const elapsed = performance.now() - Task.lastGlobalApiRequestTime
  3734. rateLimitDelay = Math.ceil(Math.min(rateLimit, Math.max(0, rateLimit * 1000 - elapsed) / 1000))
  3735. }
  3736. // Prefer RetryInfo on 429 if present
  3737. if (error?.status === 429) {
  3738. const retryInfo = error?.errorDetails?.find(
  3739. (d: any) => d["@type"] === "type.googleapis.com/google.rpc.RetryInfo",
  3740. )
  3741. const match = retryInfo?.retryDelay?.match?.(/^(\d+)s$/)
  3742. if (match) {
  3743. exponentialDelay = Number(match[1]) + 1
  3744. }
  3745. }
  3746. const finalDelay = Math.max(exponentialDelay, rateLimitDelay)
  3747. if (finalDelay <= 0) {
  3748. return
  3749. }
  3750. // Build header text; fall back to error message if none provided
  3751. let headerText
  3752. if (error.status) {
  3753. // Include both status code (for ChatRow parsing) and detailed message (for error details)
  3754. // Format: "<status>\n<message>" allows ChatRow to extract status via parseInt(text.substring(0,3))
  3755. // while preserving the full error message in errorDetails for debugging
  3756. const errorMessage = error?.message || "Unknown error"
  3757. headerText = `${error.status}\n${errorMessage}`
  3758. } else if (error?.message) {
  3759. headerText = error.message
  3760. } else {
  3761. headerText = "Unknown error"
  3762. }
  3763. headerText = headerText ? `${headerText}\n` : ""
  3764. // Show countdown timer with exponential backoff
  3765. for (let i = finalDelay; i > 0; i--) {
  3766. // Check abort flag during countdown to allow early exit
  3767. if (this.abort) {
  3768. throw new Error(`[Task#${this.taskId}] Aborted during retry countdown`)
  3769. }
  3770. await this.say("api_req_retry_delayed", `${headerText}<retry_timer>${i}</retry_timer>`, undefined, true)
  3771. await delay(1000)
  3772. }
  3773. await this.say("api_req_retry_delayed", headerText, undefined, false)
  3774. } catch (err) {
  3775. console.error("Exponential backoff failed:", err)
  3776. }
  3777. }
  3778. // Checkpoints
  3779. public async checkpointSave(force: boolean = false, suppressMessage: boolean = false) {
  3780. return checkpointSave(this, force, suppressMessage)
  3781. }
  3782. private buildCleanConversationHistory(
  3783. messages: ApiMessage[],
  3784. ): Array<
  3785. Anthropic.Messages.MessageParam | { type: "reasoning"; encrypted_content: string; id?: string; summary?: any[] }
  3786. > {
  3787. type ReasoningItemForRequest = {
  3788. type: "reasoning"
  3789. encrypted_content: string
  3790. id?: string
  3791. summary?: any[]
  3792. }
  3793. const cleanConversationHistory: (Anthropic.Messages.MessageParam | ReasoningItemForRequest)[] = []
  3794. for (const msg of messages) {
  3795. // Standalone reasoning: send encrypted, skip plain text
  3796. if (msg.type === "reasoning") {
  3797. if (msg.encrypted_content) {
  3798. cleanConversationHistory.push({
  3799. type: "reasoning",
  3800. summary: msg.summary,
  3801. encrypted_content: msg.encrypted_content!,
  3802. ...(msg.id ? { id: msg.id } : {}),
  3803. })
  3804. }
  3805. continue
  3806. }
  3807. // Preferred path: assistant message with embedded reasoning as first content block
  3808. if (msg.role === "assistant") {
  3809. const rawContent = msg.content
  3810. const contentArray: Anthropic.Messages.ContentBlockParam[] = Array.isArray(rawContent)
  3811. ? (rawContent as Anthropic.Messages.ContentBlockParam[])
  3812. : rawContent !== undefined
  3813. ? ([
  3814. { type: "text", text: rawContent } satisfies Anthropic.Messages.TextBlockParam,
  3815. ] as Anthropic.Messages.ContentBlockParam[])
  3816. : []
  3817. const [first, ...rest] = contentArray
  3818. // Check if this message has reasoning_details (OpenRouter format for Gemini 3, etc.)
  3819. const msgWithDetails = msg
  3820. if (msgWithDetails.reasoning_details && Array.isArray(msgWithDetails.reasoning_details)) {
  3821. // Build the assistant message with reasoning_details
  3822. let assistantContent: Anthropic.Messages.MessageParam["content"]
  3823. if (contentArray.length === 0) {
  3824. assistantContent = ""
  3825. } else if (contentArray.length === 1 && contentArray[0].type === "text") {
  3826. assistantContent = (contentArray[0] as Anthropic.Messages.TextBlockParam).text
  3827. } else {
  3828. assistantContent = contentArray
  3829. }
  3830. // Create message with reasoning_details property
  3831. cleanConversationHistory.push({
  3832. role: "assistant",
  3833. content: assistantContent,
  3834. reasoning_details: msgWithDetails.reasoning_details,
  3835. } as any)
  3836. continue
  3837. }
  3838. // Embedded reasoning: encrypted (send) or plain text (skip)
  3839. const hasEncryptedReasoning =
  3840. first && (first as any).type === "reasoning" && typeof (first as any).encrypted_content === "string"
  3841. const hasPlainTextReasoning =
  3842. first && (first as any).type === "reasoning" && typeof (first as any).text === "string"
  3843. if (hasEncryptedReasoning) {
  3844. const reasoningBlock = first as any
  3845. // Send as separate reasoning item (OpenAI Native)
  3846. cleanConversationHistory.push({
  3847. type: "reasoning",
  3848. summary: reasoningBlock.summary ?? [],
  3849. encrypted_content: reasoningBlock.encrypted_content,
  3850. ...(reasoningBlock.id ? { id: reasoningBlock.id } : {}),
  3851. })
  3852. // Send assistant message without reasoning
  3853. let assistantContent: Anthropic.Messages.MessageParam["content"]
  3854. if (rest.length === 0) {
  3855. assistantContent = ""
  3856. } else if (rest.length === 1 && rest[0].type === "text") {
  3857. assistantContent = (rest[0] as Anthropic.Messages.TextBlockParam).text
  3858. } else {
  3859. assistantContent = rest
  3860. }
  3861. cleanConversationHistory.push({
  3862. role: "assistant",
  3863. content: assistantContent,
  3864. } satisfies Anthropic.Messages.MessageParam)
  3865. continue
  3866. } else if (hasPlainTextReasoning) {
  3867. // Check if the model's preserveReasoning flag is set
  3868. // If true, include the reasoning block in API requests
  3869. // If false/undefined, strip it out (stored for history only, not sent back to API)
  3870. const shouldPreserveForApi = this.api.getModel().info.preserveReasoning === true
  3871. let assistantContent: Anthropic.Messages.MessageParam["content"]
  3872. if (shouldPreserveForApi) {
  3873. // Include reasoning block in the content sent to API
  3874. assistantContent = contentArray
  3875. } else {
  3876. // Strip reasoning out - stored for history only, not sent back to API
  3877. if (rest.length === 0) {
  3878. assistantContent = ""
  3879. } else if (rest.length === 1 && rest[0].type === "text") {
  3880. assistantContent = (rest[0] as Anthropic.Messages.TextBlockParam).text
  3881. } else {
  3882. assistantContent = rest
  3883. }
  3884. }
  3885. cleanConversationHistory.push({
  3886. role: "assistant",
  3887. content: assistantContent,
  3888. } satisfies Anthropic.Messages.MessageParam)
  3889. continue
  3890. }
  3891. }
  3892. // Default path for regular messages (no embedded reasoning)
  3893. if (msg.role) {
  3894. cleanConversationHistory.push({
  3895. role: msg.role,
  3896. content: msg.content as Anthropic.Messages.ContentBlockParam[] | string,
  3897. })
  3898. }
  3899. }
  3900. return cleanConversationHistory
  3901. }
  3902. public async checkpointRestore(options: CheckpointRestoreOptions) {
  3903. return checkpointRestore(this, options)
  3904. }
  3905. public async checkpointDiff(options: CheckpointDiffOptions) {
  3906. return checkpointDiff(this, options)
  3907. }
  3908. // Metrics
  3909. public combineMessages(messages: ClineMessage[]) {
  3910. return combineApiRequests(combineCommandSequences(messages))
  3911. }
  3912. public getTokenUsage(): TokenUsage {
  3913. return getApiMetrics(this.combineMessages(this.clineMessages.slice(1)))
  3914. }
  3915. public recordToolUsage(toolName: ToolName) {
  3916. if (!this.toolUsage[toolName]) {
  3917. this.toolUsage[toolName] = { attempts: 0, failures: 0 }
  3918. }
  3919. this.toolUsage[toolName].attempts++
  3920. }
  3921. public recordToolError(toolName: ToolName, error?: string) {
  3922. if (!this.toolUsage[toolName]) {
  3923. this.toolUsage[toolName] = { attempts: 0, failures: 0 }
  3924. }
  3925. this.toolUsage[toolName].failures++
  3926. if (error) {
  3927. this.emit(RooCodeEventName.TaskToolFailed, this.taskId, toolName, error)
  3928. }
  3929. }
  3930. // Getters
  3931. public get taskStatus(): TaskStatus {
  3932. if (this.interactiveAsk) {
  3933. return TaskStatus.Interactive
  3934. }
  3935. if (this.resumableAsk) {
  3936. return TaskStatus.Resumable
  3937. }
  3938. if (this.idleAsk) {
  3939. return TaskStatus.Idle
  3940. }
  3941. return TaskStatus.Running
  3942. }
  3943. public get taskAsk(): ClineMessage | undefined {
  3944. return this.idleAsk || this.resumableAsk || this.interactiveAsk
  3945. }
  3946. public get queuedMessages(): QueuedMessage[] {
  3947. return this.messageQueueService.messages
  3948. }
  3949. public get tokenUsage(): TokenUsage | undefined {
  3950. if (this.tokenUsageSnapshot && this.tokenUsageSnapshotAt) {
  3951. return this.tokenUsageSnapshot
  3952. }
  3953. this.tokenUsageSnapshot = this.getTokenUsage()
  3954. this.tokenUsageSnapshotAt = this.clineMessages.at(-1)?.ts
  3955. return this.tokenUsageSnapshot
  3956. }
  3957. public get cwd() {
  3958. return this.workspacePath
  3959. }
  3960. /**
  3961. * Get the tool protocol locked to this task.
  3962. * Returns undefined only if the task hasn't been fully initialized yet.
  3963. *
  3964. * @see {@link _taskToolProtocol} for lifecycle details
  3965. */
  3966. public get taskToolProtocol() {
  3967. return this._taskToolProtocol
  3968. }
  3969. /**
  3970. * Provides convenient access to high-level message operations.
  3971. * Uses lazy initialization - the MessageManager is only created when first accessed.
  3972. * Subsequent accesses return the same cached instance.
  3973. *
  3974. * ## Important: Single Coordination Point
  3975. *
  3976. * **All MessageManager operations must go through this getter** rather than
  3977. * instantiating `new MessageManager(task)` directly. This ensures:
  3978. * - A single shared instance for consistent behavior
  3979. * - Centralized coordination of all rewind/message operations
  3980. * - Ability to add internal state or instrumentation in the future
  3981. *
  3982. * @example
  3983. * ```typescript
  3984. * // Correct: Use the getter
  3985. * await task.messageManager.rewindToTimestamp(ts)
  3986. *
  3987. * // Incorrect: Do NOT create new instances directly
  3988. * // const manager = new MessageManager(task) // Don't do this!
  3989. * ```
  3990. */
  3991. get messageManager(): MessageManager {
  3992. if (!this._messageManager) {
  3993. this._messageManager = new MessageManager(this)
  3994. }
  3995. return this._messageManager
  3996. }
  3997. /**
  3998. * Broadcast browser session updates to the browser panel (if open)
  3999. */
  4000. private broadcastBrowserSessionUpdate(): void {
  4001. const provider = this.providerRef.deref()
  4002. if (!provider) {
  4003. return
  4004. }
  4005. try {
  4006. const { BrowserSessionPanelManager } = require("../webview/BrowserSessionPanelManager")
  4007. const panelManager = BrowserSessionPanelManager.getInstance(provider)
  4008. // Get browser session messages
  4009. const browserSessionStartIndex = this.clineMessages.findIndex(
  4010. (m) =>
  4011. m.ask === "browser_action_launch" ||
  4012. (m.say === "browser_session_status" && m.text?.includes("opened")),
  4013. )
  4014. const browserSessionMessages =
  4015. browserSessionStartIndex !== -1 ? this.clineMessages.slice(browserSessionStartIndex) : []
  4016. const isBrowserSessionActive = this.browserSession?.isSessionActive() ?? false
  4017. // Update the panel asynchronously
  4018. panelManager.updateBrowserSession(browserSessionMessages, isBrowserSessionActive).catch((error: Error) => {
  4019. console.error("Failed to broadcast browser session update:", error)
  4020. })
  4021. } catch (error) {
  4022. // Silently fail if panel manager is not available
  4023. console.debug("Browser panel not available for update:", error)
  4024. }
  4025. }
  4026. /**
  4027. * Process any queued messages by dequeuing and submitting them.
  4028. * This ensures that queued user messages are sent when appropriate,
  4029. * preventing them from getting stuck in the queue.
  4030. *
  4031. * @param context - Context string for logging (e.g., the calling tool name)
  4032. */
  4033. public processQueuedMessages(): void {
  4034. try {
  4035. if (!this.messageQueueService.isEmpty()) {
  4036. const queued = this.messageQueueService.dequeueMessage()
  4037. if (queued) {
  4038. setTimeout(() => {
  4039. this.submitUserMessage(queued.text, queued.images).catch((err) =>
  4040. console.error(`[Task] Failed to submit queued message:`, err),
  4041. )
  4042. }, 0)
  4043. }
  4044. }
  4045. } catch (e) {
  4046. console.error(`[Task] Queue processing error:`, e)
  4047. }
  4048. }
  4049. }