| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886 |
- import * as path from "path"
- import * as vscode from "vscode"
- import os from "os"
- import crypto from "crypto"
- import { v7 as uuidv7 } from "uuid"
- import EventEmitter from "events"
- import { AskIgnoredError } from "./AskIgnoredError"
- // Note: Anthropic SDK import retained for types used by the API handler interface
- import { Anthropic } from "@anthropic-ai/sdk"
- import OpenAI from "openai"
- import debounce from "lodash.debounce"
- import delay from "delay"
- import pWaitFor from "p-wait-for"
- import { serializeError } from "serialize-error"
- import { Package } from "../../shared/package"
- import { formatToolInvocation } from "../tools/helpers/toolResultFormatting"
- import {
- type TaskLike,
- type TaskMetadata,
- type TaskEvents,
- type ProviderSettings,
- type TokenUsage,
- type ToolUsage,
- type ToolName,
- type ContextCondense,
- type ContextTruncation,
- type ClineMessage,
- type ClineSay,
- type ClineAsk,
- type ToolProgressStatus,
- type HistoryItem,
- type CreateTaskOptions,
- type ModelInfo,
- type ClineApiReqCancelReason,
- type ClineApiReqInfo,
- RooCodeEventName,
- TelemetryEventName,
- TaskStatus,
- TodoItem,
- getApiProtocol,
- getModelId,
- isRetiredProvider,
- isIdleAsk,
- isInteractiveAsk,
- isResumableAsk,
- QueuedMessage,
- DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
- DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
- MAX_CHECKPOINT_TIMEOUT_SECONDS,
- MIN_CHECKPOINT_TIMEOUT_SECONDS,
- ConsecutiveMistakeError,
- MAX_MCP_TOOLS_THRESHOLD,
- countEnabledMcpTools,
- } from "@roo-code/types"
- import { TelemetryService } from "@roo-code/telemetry"
- import { CloudService, BridgeOrchestrator } from "@roo-code/cloud"
- // api
- import { ApiHandler, ApiHandlerCreateMessageMetadata, buildApiHandler } from "../../api"
- import type { AssistantModelMessage } from "ai"
- import { ApiStream, GroundingSource } from "../../api/transform/stream"
- import { maybeRemoveImageBlocks } from "../../api/transform/image-cleaning"
- // shared
- import { findLastIndex } from "../../shared/array"
- import { combineApiRequests } from "../../shared/combineApiRequests"
- import { combineCommandSequences } from "../../shared/combineCommandSequences"
- import { t } from "../../i18n"
- import { getApiMetrics, hasTokenUsageChanged, hasToolUsageChanged } from "../../shared/getApiMetrics"
- import { ClineAskResponse } from "../../shared/WebviewMessage"
- import { defaultModeSlug, getModeBySlug } from "../../shared/modes"
- import { DiffStrategy, type ToolUse, type ToolParamName, toolParamNames } from "../../shared/tools"
- import { getModelMaxOutputTokens } from "../../shared/api"
- // services
- import { McpHub } from "../../services/mcp/McpHub"
- import { McpServerManager } from "../../services/mcp/McpServerManager"
- import { RepoPerTaskCheckpointService } from "../../services/checkpoints"
- // integrations
- import { DiffViewProvider } from "../../integrations/editor/DiffViewProvider"
- import { findToolName } from "../../integrations/misc/export-markdown"
- import { RooTerminalProcess } from "../../integrations/terminal/types"
- import { TerminalRegistry } from "../../integrations/terminal/TerminalRegistry"
- import { OutputInterceptor } from "../../integrations/terminal/OutputInterceptor"
- // utils
- import { calculateApiCostAnthropic, calculateApiCostOpenAI } from "../../shared/cost"
- import { getWorkspacePath } from "../../utils/path"
- import { sanitizeToolUseId } from "../../utils/tool-id"
- import { getTaskDirectoryPath } from "../../utils/storage"
- // prompts
- import { formatResponse } from "../prompts/responses"
- import { SYSTEM_PROMPT } from "../prompts/system"
- import { buildNativeToolsArrayWithRestrictions } from "./build-tools"
- // core modules
- import { ToolRepetitionDetector } from "../tools/ToolRepetitionDetector"
- import { restoreTodoListForTask } from "../tools/UpdateTodoListTool"
- import { FileContextTracker } from "../context-tracking/FileContextTracker"
- import { RooIgnoreController } from "../ignore/RooIgnoreController"
- import { RooProtectedController } from "../protect/RooProtectedController"
- import { type AssistantMessageContent, presentAssistantMessage } from "../assistant-message"
- import { NativeToolCallParser } from "../assistant-message/NativeToolCallParser"
- import { manageContext, willManageContext } from "../context-management"
- import { ClineProvider } from "../webview/ClineProvider"
- import { MultiSearchReplaceDiffStrategy } from "../diff/strategies/multi-search-replace"
- import {
- type ApiMessage,
- readApiMessages,
- saveApiMessages,
- readTaskMessages,
- saveTaskMessages,
- taskMetadata,
- type RooMessage,
- type RooUserMessage,
- type RooAssistantMessage,
- type RooToolMessage,
- type RooReasoningMessage,
- type TextPart,
- type ImagePart,
- type ToolCallPart,
- type ToolResultPart,
- type UserContentPart,
- type AnyToolCallBlock,
- type AnyToolResultBlock,
- isRooUserMessage,
- isRooAssistantMessage,
- isRooToolMessage,
- isRooReasoningMessage,
- isRooRoleMessage,
- isAnyToolResultBlock,
- getToolCallId,
- getToolCallName,
- getToolResultContent,
- readRooMessages,
- saveRooMessages,
- } from "../task-persistence"
- import { getEnvironmentDetails } from "../environment/getEnvironmentDetails"
- import { checkContextWindowExceededError } from "../context/context-management/context-error-handling"
- import {
- type CheckpointDiffOptions,
- type CheckpointRestoreOptions,
- getCheckpointService,
- checkpointSave,
- checkpointRestore,
- checkpointDiff,
- } from "../checkpoints"
- import { processUserContentMentions } from "../mentions/processUserContentMentions"
- import { getMessagesSinceLastSummary, summarizeConversation, getEffectiveApiHistory } from "../condense"
- import { MessageQueueService } from "../message-queue/MessageQueueService"
- import { AutoApprovalHandler, checkAutoApproval } from "../auto-approval"
- import { MessageManager } from "../message-manager"
- import { validateAndFixToolResultIds } from "./validateToolResultIds"
- import { mergeConsecutiveApiMessages } from "./mergeConsecutiveApiMessages"
- const MAX_EXPONENTIAL_BACKOFF_SECONDS = 600 // 10 minutes
- const DEFAULT_USAGE_COLLECTION_TIMEOUT_MS = 5000 // 5 seconds
- const FORCED_CONTEXT_REDUCTION_PERCENT = 75 // Keep 75% of context (remove 25%) on context window errors
- const MAX_CONTEXT_WINDOW_RETRIES = 3 // Maximum retries for context window errors
- export interface TaskOptions extends CreateTaskOptions {
- provider: ClineProvider
- apiConfiguration: ProviderSettings
- enableCheckpoints?: boolean
- checkpointTimeout?: number
- enableBridge?: boolean
- consecutiveMistakeLimit?: number
- task?: string
- images?: string[]
- historyItem?: HistoryItem
- experiments?: Record<string, boolean>
- startTask?: boolean
- rootTask?: Task
- parentTask?: Task
- taskNumber?: number
- onCreated?: (task: Task) => void
- initialTodos?: TodoItem[]
- workspacePath?: string
- /** Initial status for the task's history item (e.g., "active" for child tasks) */
- initialStatus?: "active" | "delegated" | "completed"
- }
- export class Task extends EventEmitter<TaskEvents> implements TaskLike {
- readonly taskId: string
- readonly rootTaskId?: string
- readonly parentTaskId?: string
- childTaskId?: string
- pendingNewTaskToolCallId?: string
- readonly instanceId: string
- readonly metadata: TaskMetadata
- todoList?: TodoItem[]
- readonly rootTask: Task | undefined = undefined
- readonly parentTask: Task | undefined = undefined
- readonly taskNumber: number
- readonly workspacePath: string
- /**
- * The mode associated with this task. Persisted across sessions
- * to maintain user context when reopening tasks from history.
- *
- * ## Lifecycle
- *
- * ### For new tasks:
- * 1. Initially `undefined` during construction
- * 2. Asynchronously initialized from provider state via `initializeTaskMode()`
- * 3. Falls back to `defaultModeSlug` if provider state is unavailable
- *
- * ### For history items:
- * 1. Immediately set from `historyItem.mode` during construction
- * 2. Falls back to `defaultModeSlug` if mode is not stored in history
- *
- * ## Important
- * This property should NOT be accessed directly until `taskModeReady` promise resolves.
- * Use `getTaskMode()` for async access or `taskMode` getter for sync access after initialization.
- *
- * @private
- * @see {@link getTaskMode} - For safe async access
- * @see {@link taskMode} - For sync access after initialization
- * @see {@link waitForModeInitialization} - To ensure initialization is complete
- */
- private _taskMode: string | undefined
- /**
- * Promise that resolves when the task mode has been initialized.
- * This ensures async mode initialization completes before the task is used.
- *
- * ## Purpose
- * - Prevents race conditions when accessing task mode
- * - Ensures provider state is properly loaded before mode-dependent operations
- * - Provides a synchronization point for async initialization
- *
- * ## Resolution timing
- * - For history items: Resolves immediately (sync initialization)
- * - For new tasks: Resolves after provider state is fetched (async initialization)
- *
- * @private
- * @see {@link waitForModeInitialization} - Public method to await this promise
- */
- private taskModeReady: Promise<void>
- /**
- * The API configuration name (provider profile) associated with this task.
- * Persisted across sessions to maintain the provider profile when reopening tasks from history.
- *
- * ## Lifecycle
- *
- * ### For new tasks:
- * 1. Initially `undefined` during construction
- * 2. Asynchronously initialized from provider state via `initializeTaskApiConfigName()`
- * 3. Falls back to "default" if provider state is unavailable
- *
- * ### For history items:
- * 1. Immediately set from `historyItem.apiConfigName` during construction
- * 2. Falls back to undefined if not stored in history (for backward compatibility)
- *
- * ## Important
- * If you need a non-`undefined` provider profile (e.g., for profile-dependent operations),
- * wait for `taskApiConfigReady` first (or use `getTaskApiConfigName()`).
- * The sync `taskApiConfigName` getter may return `undefined` for backward compatibility.
- *
- * @private
- * @see {@link getTaskApiConfigName} - For safe async access
- * @see {@link taskApiConfigName} - For sync access after initialization
- */
- private _taskApiConfigName: string | undefined
- /**
- * Promise that resolves when the task API config name has been initialized.
- * This ensures async API config name initialization completes before the task is used.
- *
- * ## Purpose
- * - Prevents race conditions when accessing task API config name
- * - Ensures provider state is properly loaded before profile-dependent operations
- * - Provides a synchronization point for async initialization
- *
- * ## Resolution timing
- * - For history items: Resolves immediately (sync initialization)
- * - For new tasks: Resolves after provider state is fetched (async initialization)
- *
- * @private
- */
- private taskApiConfigReady: Promise<void>
- providerRef: WeakRef<ClineProvider>
- private readonly globalStoragePath: string
- abort: boolean = false
- currentRequestAbortController?: AbortController
- skipPrevResponseIdOnce: boolean = false
- // TaskStatus
- idleAsk?: ClineMessage
- resumableAsk?: ClineMessage
- interactiveAsk?: ClineMessage
- didFinishAbortingStream = false
- abandoned = false
- abortReason?: ClineApiReqCancelReason
- isInitialized = false
- isPaused: boolean = false
- // API
- apiConfiguration: ProviderSettings
- api: ApiHandler
- private static lastGlobalApiRequestTime?: number
- private autoApprovalHandler: AutoApprovalHandler
- /**
- * Reset the global API request timestamp. This should only be used for testing.
- * @internal
- */
- static resetGlobalApiRequestTime(): void {
- Task.lastGlobalApiRequestTime = undefined
- }
- toolRepetitionDetector: ToolRepetitionDetector
- rooIgnoreController?: RooIgnoreController
- rooProtectedController?: RooProtectedController
- fileContextTracker: FileContextTracker
- terminalProcess?: RooTerminalProcess
- // Editing
- diffViewProvider: DiffViewProvider
- diffStrategy?: DiffStrategy
- didEditFile: boolean = false
- // LLM Messages & Chat Messages
- apiConversationHistory: RooMessage[] = []
- clineMessages: ClineMessage[] = []
- // Ask
- private askResponse?: ClineAskResponse
- private askResponseText?: string
- private askResponseImages?: string[]
- public lastMessageTs?: number
- private autoApprovalTimeoutRef?: NodeJS.Timeout
- // Tool Use
- consecutiveMistakeCount: number = 0
- consecutiveMistakeLimit: number
- consecutiveMistakeCountForApplyDiff: Map<string, number> = new Map()
- consecutiveMistakeCountForEditFile: Map<string, number> = new Map()
- consecutiveNoToolUseCount: number = 0
- consecutiveNoAssistantMessagesCount: number = 0
- toolUsage: ToolUsage = {}
- // Checkpoints
- enableCheckpoints: boolean
- checkpointTimeout: number
- checkpointService?: RepoPerTaskCheckpointService
- checkpointServiceInitializing = false
- // Task Bridge
- enableBridge: boolean
- // Message Queue Service
- public readonly messageQueueService: MessageQueueService
- private messageQueueStateChangedHandler: (() => void) | undefined
- // Streaming
- isWaitingForFirstChunk = false
- isStreaming = false
- currentStreamingContentIndex = 0
- currentStreamingDidCheckpoint = false
- assistantMessageContent: AssistantMessageContent[] = []
- presentAssistantMessageLocked = false
- presentAssistantMessageHasPendingUpdates = false
- userMessageContent: Array<TextPart | ImagePart> = []
- userMessageContentReady = false
- pendingToolResults: Array<ToolResultPart> = []
- /**
- * Flag indicating whether the assistant message for the current streaming session
- * has been saved to API conversation history.
- *
- * This is critical for parallel tool calling: tools should NOT execute until
- * the assistant message is saved. Otherwise, if a tool like `new_task` triggers
- * `flushPendingToolResultsToHistory()`, the user message with tool_results would
- * appear BEFORE the assistant message with tool_uses, causing API errors.
- *
- * Reset to `false` at the start of each API request.
- * Set to `true` after the assistant message is saved in `recursivelyMakeClineRequests`.
- */
- assistantMessageSavedToHistory = false
- /**
- * Push a tool result to pendingToolResults, preventing duplicates.
- * Duplicate toolCallIds cause API errors.
- *
- * @param toolResult - The ToolResultPart to add
- * @returns true if added, false if duplicate was skipped
- */
- public pushToolResultToUserContent(toolResult: ToolResultPart): boolean {
- const existingResult = this.pendingToolResults.find(
- (block): block is ToolResultPart =>
- block.type === "tool-result" && block.toolCallId === toolResult.toolCallId,
- )
- if (existingResult) {
- console.warn(
- `[Task#pushToolResultToUserContent] Skipping duplicate tool_result for toolCallId: ${toolResult.toolCallId}`,
- )
- return false
- }
- this.pendingToolResults.push(toolResult)
- return true
- }
- /**
- * Handle a tool call streaming event (tool_call_start, tool_call_delta, or tool_call_end).
- * This is used both for processing events from NativeToolCallParser (legacy providers)
- * and for direct AI SDK events (DeepSeek, Moonshot, etc.).
- *
- * @param event - The tool call event to process
- */
- private handleToolCallEvent(
- event:
- | { type: "tool_call_start"; id: string; name: string }
- | { type: "tool_call_delta"; id: string; delta: string }
- | { type: "tool_call_end"; id: string },
- ): void {
- if (event.type === "tool_call_start") {
- // Guard against duplicate tool_call_start events for the same tool ID.
- // This can occur due to stream retry, reconnection, or API quirks.
- // Without this check, duplicate tool_use blocks with the same ID would
- // be added to assistantMessageContent, causing API 400 errors:
- // "tool_use ids must be unique"
- if (this.streamingToolCallIndices.has(event.id)) {
- console.warn(
- `[Task#${this.taskId}] Ignoring duplicate tool_call_start for ID: ${event.id} (tool: ${event.name})`,
- )
- return
- }
- // Initialize streaming in NativeToolCallParser
- NativeToolCallParser.startStreamingToolCall(event.id, event.name as ToolName)
- // Before adding a new tool, finalize any preceding text block
- // This prevents the text block from blocking tool presentation
- const lastBlock = this.assistantMessageContent[this.assistantMessageContent.length - 1]
- if (lastBlock?.type === "text" && lastBlock.partial) {
- lastBlock.partial = false
- }
- // Track the index where this tool will be stored
- const toolUseIndex = this.assistantMessageContent.length
- this.streamingToolCallIndices.set(event.id, toolUseIndex)
- // Create initial partial tool use
- const partialToolUse: ToolUse = {
- type: "tool_use",
- name: event.name as ToolName,
- params: {},
- partial: true,
- }
- // Store the ID for native protocol
- ;(partialToolUse as any).id = event.id
- // Add to content and present
- this.assistantMessageContent.push(partialToolUse)
- this.userMessageContentReady = false
- presentAssistantMessage(this)
- } else if (event.type === "tool_call_delta") {
- // Process chunk using streaming JSON parser
- const partialToolUse = NativeToolCallParser.processStreamingChunk(event.id, event.delta)
- if (partialToolUse) {
- // Get the index for this tool call
- const toolUseIndex = this.streamingToolCallIndices.get(event.id)
- if (toolUseIndex !== undefined) {
- // Store the ID for native protocol
- ;(partialToolUse as any).id = event.id
- // Update the existing tool use with new partial data
- this.assistantMessageContent[toolUseIndex] = partialToolUse
- // Present updated tool use
- presentAssistantMessage(this)
- }
- }
- } else if (event.type === "tool_call_end") {
- // Finalize the streaming tool call
- const finalToolUse = NativeToolCallParser.finalizeStreamingToolCall(event.id)
- // Get the index for this tool call
- const toolUseIndex = this.streamingToolCallIndices.get(event.id)
- if (finalToolUse) {
- // Store the tool call ID
- ;(finalToolUse as any).id = event.id
- // Get the index and replace partial with final
- if (toolUseIndex !== undefined) {
- this.assistantMessageContent[toolUseIndex] = finalToolUse
- }
- // Clean up tracking
- this.streamingToolCallIndices.delete(event.id)
- // Mark that we have new content to process
- this.userMessageContentReady = false
- // Present the finalized tool call
- presentAssistantMessage(this)
- } else if (toolUseIndex !== undefined) {
- // finalizeStreamingToolCall returned null (malformed JSON or missing args)
- // Mark the tool as non-partial so it's presented as complete, but execution
- // will be short-circuited in presentAssistantMessage with a structured tool_result.
- const existingToolUse = this.assistantMessageContent[toolUseIndex]
- if (existingToolUse && existingToolUse.type === "tool_use") {
- existingToolUse.partial = false
- // Ensure it has the ID for native protocol
- ;(existingToolUse as any).id = event.id
- }
- // Clean up tracking
- this.streamingToolCallIndices.delete(event.id)
- // Mark that we have new content to process
- this.userMessageContentReady = false
- // Present the tool call - validation will handle missing params
- presentAssistantMessage(this)
- }
- }
- }
- didRejectTool = false
- didAlreadyUseTool = false
- didToolFailInCurrentTurn = false
- didCompleteReadingStream = false
- private _started = false
- // No streaming parser is required.
- assistantMessageParser?: undefined
- private providerProfileChangeListener?: (config: { name: string; provider?: string }) => void
- // Native tool call streaming state (track which index each tool is at)
- private streamingToolCallIndices: Map<string, number> = new Map()
- // Cached model info for current streaming session (set at start of each API request)
- // This prevents excessive getModel() calls during tool execution
- cachedStreamingModel?: { id: string; info: ModelInfo }
- // Token Usage Cache
- private tokenUsageSnapshot?: TokenUsage
- private tokenUsageSnapshotAt?: number
- // Tool Usage Cache
- private toolUsageSnapshot?: ToolUsage
- // Token Usage Throttling - Debounced emit function
- private readonly TOKEN_USAGE_EMIT_INTERVAL_MS = 2000 // 2 seconds
- private debouncedEmitTokenUsage: ReturnType<typeof debounce>
- // Cloud Sync Tracking
- private cloudSyncedMessageTimestamps: Set<number> = new Set()
- // Initial status for the task's history item (set at creation time to avoid race conditions)
- private readonly initialStatus?: "active" | "delegated" | "completed"
- // MessageManager for high-level message operations (lazy initialized)
- private _messageManager?: MessageManager
- constructor({
- provider,
- apiConfiguration,
- enableCheckpoints = true,
- checkpointTimeout = DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
- enableBridge = false,
- consecutiveMistakeLimit = DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
- task,
- images,
- historyItem,
- experiments: experimentsConfig,
- startTask = true,
- rootTask,
- parentTask,
- taskNumber = -1,
- onCreated,
- initialTodos,
- workspacePath,
- initialStatus,
- }: TaskOptions) {
- super()
- if (startTask && !task && !images && !historyItem) {
- throw new Error("Either historyItem or task/images must be provided")
- }
- if (
- !checkpointTimeout ||
- checkpointTimeout > MAX_CHECKPOINT_TIMEOUT_SECONDS ||
- checkpointTimeout < MIN_CHECKPOINT_TIMEOUT_SECONDS
- ) {
- throw new Error(
- "checkpointTimeout must be between " +
- MIN_CHECKPOINT_TIMEOUT_SECONDS +
- " and " +
- MAX_CHECKPOINT_TIMEOUT_SECONDS +
- " seconds",
- )
- }
- this.taskId = historyItem ? historyItem.id : uuidv7()
- this.rootTaskId = historyItem ? historyItem.rootTaskId : rootTask?.taskId
- this.parentTaskId = historyItem ? historyItem.parentTaskId : parentTask?.taskId
- this.childTaskId = undefined
- this.metadata = {
- task: historyItem ? historyItem.task : task,
- images: historyItem ? [] : images,
- }
- // Normal use-case is usually retry similar history task with new workspace.
- this.workspacePath = parentTask
- ? parentTask.workspacePath
- : (workspacePath ?? getWorkspacePath(path.join(os.homedir(), "Desktop")))
- this.instanceId = crypto.randomUUID().slice(0, 8)
- this.taskNumber = -1
- this.rooIgnoreController = new RooIgnoreController(this.cwd)
- this.rooProtectedController = new RooProtectedController(this.cwd)
- this.fileContextTracker = new FileContextTracker(provider, this.taskId)
- this.rooIgnoreController.initialize().catch((error) => {
- console.error("Failed to initialize RooIgnoreController:", error)
- })
- this.apiConfiguration = apiConfiguration
- this.api = buildApiHandler(this.apiConfiguration)
- this.autoApprovalHandler = new AutoApprovalHandler()
- this.consecutiveMistakeLimit = consecutiveMistakeLimit ?? DEFAULT_CONSECUTIVE_MISTAKE_LIMIT
- this.providerRef = new WeakRef(provider)
- this.globalStoragePath = provider.context.globalStorageUri.fsPath
- this.diffViewProvider = new DiffViewProvider(this.cwd, this)
- this.enableCheckpoints = enableCheckpoints
- this.checkpointTimeout = checkpointTimeout
- this.enableBridge = enableBridge
- this.parentTask = parentTask
- this.taskNumber = taskNumber
- this.initialStatus = initialStatus
- // Store the task's mode and API config name when it's created.
- // For history items, use the stored values; for new tasks, we'll set them
- // after getting state.
- if (historyItem) {
- this._taskMode = historyItem.mode || defaultModeSlug
- this._taskApiConfigName = historyItem.apiConfigName
- this.taskModeReady = Promise.resolve()
- this.taskApiConfigReady = Promise.resolve()
- TelemetryService.instance.captureTaskRestarted(this.taskId)
- } else {
- // For new tasks, don't set the mode/apiConfigName yet - wait for async initialization.
- this._taskMode = undefined
- this._taskApiConfigName = undefined
- this.taskModeReady = this.initializeTaskMode(provider)
- this.taskApiConfigReady = this.initializeTaskApiConfigName(provider)
- TelemetryService.instance.captureTaskCreated(this.taskId)
- }
- this.assistantMessageParser = undefined
- this.messageQueueService = new MessageQueueService()
- this.messageQueueStateChangedHandler = () => {
- this.emit(RooCodeEventName.TaskUserMessage, this.taskId)
- this.emit(RooCodeEventName.QueuedMessagesUpdated, this.taskId, this.messageQueueService.messages)
- this.providerRef.deref()?.postStateToWebviewWithoutTaskHistory()
- }
- this.messageQueueService.on("stateChanged", this.messageQueueStateChangedHandler)
- // Listen for provider profile changes to update parser state
- this.setupProviderProfileChangeListener(provider)
- // Set up diff strategy
- this.diffStrategy = new MultiSearchReplaceDiffStrategy()
- this.toolRepetitionDetector = new ToolRepetitionDetector(this.consecutiveMistakeLimit)
- // Initialize todo list if provided
- if (initialTodos && initialTodos.length > 0) {
- this.todoList = initialTodos
- }
- // Initialize debounced token usage emit function
- // Uses debounce with maxWait to achieve throttle-like behavior:
- // - leading: true - Emit immediately on first call
- // - trailing: true - Emit final state when updates stop
- // - maxWait - Ensures at most one emit per interval during rapid updates (throttle behavior)
- this.debouncedEmitTokenUsage = debounce(
- (tokenUsage: TokenUsage, toolUsage: ToolUsage) => {
- const tokenChanged = hasTokenUsageChanged(tokenUsage, this.tokenUsageSnapshot)
- const toolChanged = hasToolUsageChanged(toolUsage, this.toolUsageSnapshot)
- if (tokenChanged || toolChanged) {
- this.emit(RooCodeEventName.TaskTokenUsageUpdated, this.taskId, tokenUsage, toolUsage)
- this.tokenUsageSnapshot = tokenUsage
- this.tokenUsageSnapshotAt = this.clineMessages.at(-1)?.ts
- // Deep copy tool usage for snapshot
- this.toolUsageSnapshot = JSON.parse(JSON.stringify(toolUsage))
- }
- },
- this.TOKEN_USAGE_EMIT_INTERVAL_MS,
- { leading: true, trailing: true, maxWait: this.TOKEN_USAGE_EMIT_INTERVAL_MS },
- )
- onCreated?.(this)
- if (startTask) {
- this._started = true
- if (task || images) {
- this.runLifecycleTaskInBackground(this.startTask(task, images), "startTask")
- } else if (historyItem) {
- this.runLifecycleTaskInBackground(this.resumeTaskFromHistory(), "resumeTaskFromHistory")
- } else {
- throw new Error("Either historyItem or task/images must be provided")
- }
- }
- }
- private runLifecycleTaskInBackground(taskPromise: Promise<void>, operation: "startTask" | "resumeTaskFromHistory") {
- void taskPromise.catch((error) => {
- if (this.shouldIgnoreBackgroundLifecycleError(error)) {
- return
- }
- console.error(
- `[Task#${operation}] task ${this.taskId}.${this.instanceId} failed: ${
- error instanceof Error ? error.message : String(error)
- }`,
- )
- })
- }
- private shouldIgnoreBackgroundLifecycleError(error: unknown): boolean {
- if (error instanceof AskIgnoredError) {
- return true
- }
- if (this.abandoned === true || this.abort === true || this.abortReason === "user_cancelled") {
- return true
- }
- if (!(error instanceof Error)) {
- return false
- }
- const abortedByCurrentTask =
- error.message.includes(`[RooCode#ask] task ${this.taskId}.${this.instanceId} aborted`) ||
- error.message.includes(`[RooCode#say] task ${this.taskId}.${this.instanceId} aborted`)
- return abortedByCurrentTask
- }
- /**
- * Initialize the task mode from the provider state.
- * This method handles async initialization with proper error handling.
- *
- * ## Flow
- * 1. Attempts to fetch the current mode from provider state
- * 2. Sets `_taskMode` to the fetched mode or `defaultModeSlug` if unavailable
- * 3. Handles errors gracefully by falling back to default mode
- * 4. Logs any initialization errors for debugging
- *
- * ## Error handling
- * - Network failures when fetching provider state
- * - Provider not yet initialized
- * - Invalid state structure
- *
- * All errors result in fallback to `defaultModeSlug` to ensure task can proceed.
- *
- * @private
- * @param provider - The ClineProvider instance to fetch state from
- * @returns Promise that resolves when initialization is complete
- */
- private async initializeTaskMode(provider: ClineProvider): Promise<void> {
- try {
- const state = await provider.getState()
- this._taskMode = state?.mode || defaultModeSlug
- } catch (error) {
- // If there's an error getting state, use the default mode
- this._taskMode = defaultModeSlug
- // Use the provider's log method for better error visibility
- const errorMessage = `Failed to initialize task mode: ${error instanceof Error ? error.message : String(error)}`
- provider.log(errorMessage)
- }
- }
- /**
- * Initialize the task API config name from the provider state.
- * This method handles async initialization with proper error handling.
- *
- * ## Flow
- * 1. Attempts to fetch the current API config name from provider state
- * 2. Sets `_taskApiConfigName` to the fetched name or "default" if unavailable
- * 3. Handles errors gracefully by falling back to "default"
- * 4. Logs any initialization errors for debugging
- *
- * ## Error handling
- * - Network failures when fetching provider state
- * - Provider not yet initialized
- * - Invalid state structure
- *
- * All errors result in fallback to "default" to ensure task can proceed.
- *
- * @private
- * @param provider - The ClineProvider instance to fetch state from
- * @returns Promise that resolves when initialization is complete
- */
- private async initializeTaskApiConfigName(provider: ClineProvider): Promise<void> {
- try {
- const state = await provider.getState()
- // Avoid clobbering a newer value that may have been set while awaiting provider state
- // (e.g., user switches provider profile immediately after task creation).
- if (this._taskApiConfigName === undefined) {
- this._taskApiConfigName = state?.currentApiConfigName ?? "default"
- }
- } catch (error) {
- // If there's an error getting state, use the default profile (unless a newer value was set).
- if (this._taskApiConfigName === undefined) {
- this._taskApiConfigName = "default"
- }
- // Use the provider's log method for better error visibility
- const errorMessage = `Failed to initialize task API config name: ${error instanceof Error ? error.message : String(error)}`
- provider.log(errorMessage)
- }
- }
- /**
- * Sets up a listener for provider profile changes.
- *
- * @private
- * @param provider - The ClineProvider instance to listen to
- */
- private setupProviderProfileChangeListener(provider: ClineProvider): void {
- // Only set up listener if provider has the on method (may not exist in test mocks)
- if (typeof provider.on !== "function") {
- return
- }
- this.providerProfileChangeListener = async () => {
- try {
- const newState = await provider.getState()
- if (newState?.apiConfiguration) {
- this.updateApiConfiguration(newState.apiConfiguration)
- }
- } catch (error) {
- console.error(
- `[Task#${this.taskId}.${this.instanceId}] Failed to update API configuration on profile change:`,
- error,
- )
- }
- }
- provider.on(RooCodeEventName.ProviderProfileChanged, this.providerProfileChangeListener)
- }
- /**
- * Wait for the task mode to be initialized before proceeding.
- * This method ensures that any operations depending on the task mode
- * will have access to the correct mode value.
- *
- * ## When to use
- * - Before accessing mode-specific configurations
- * - When switching between tasks with different modes
- * - Before operations that depend on mode-based permissions
- *
- * ## Example usage
- * ```typescript
- * // Wait for mode initialization before mode-dependent operations
- * await task.waitForModeInitialization();
- * const mode = task.taskMode; // Now safe to access synchronously
- *
- * // Or use with getTaskMode() for a one-liner
- * const mode = await task.getTaskMode(); // Internally waits for initialization
- * ```
- *
- * @returns Promise that resolves when the task mode is initialized
- * @public
- */
- public async waitForModeInitialization(): Promise<void> {
- return this.taskModeReady
- }
- /**
- * Get the task mode asynchronously, ensuring it's properly initialized.
- * This is the recommended way to access the task mode as it guarantees
- * the mode is available before returning.
- *
- * ## Async behavior
- * - Internally waits for `taskModeReady` promise to resolve
- * - Returns the initialized mode or `defaultModeSlug` as fallback
- * - Safe to call multiple times - subsequent calls return immediately if already initialized
- *
- * ## Example usage
- * ```typescript
- * // Safe async access
- * const mode = await task.getTaskMode();
- * console.log(`Task is running in ${mode} mode`);
- *
- * // Use in conditional logic
- * if (await task.getTaskMode() === 'architect') {
- * // Perform architect-specific operations
- * }
- * ```
- *
- * @returns Promise resolving to the task mode string
- * @public
- */
- public async getTaskMode(): Promise<string> {
- await this.taskModeReady
- return this._taskMode || defaultModeSlug
- }
- /**
- * Get the task mode synchronously. This should only be used when you're certain
- * that the mode has already been initialized (e.g., after waitForModeInitialization).
- *
- * ## When to use
- * - In synchronous contexts where async/await is not available
- * - After explicitly waiting for initialization via `waitForModeInitialization()`
- * - In event handlers or callbacks where mode is guaranteed to be initialized
- *
- * ## Example usage
- * ```typescript
- * // After ensuring initialization
- * await task.waitForModeInitialization();
- * const mode = task.taskMode; // Safe synchronous access
- *
- * // In an event handler after task is started
- * task.on('taskStarted', () => {
- * console.log(`Task started in ${task.taskMode} mode`); // Safe here
- * });
- * ```
- *
- * @throws {Error} If the mode hasn't been initialized yet
- * @returns The task mode string
- * @public
- */
- public get taskMode(): string {
- if (this._taskMode === undefined) {
- throw new Error("Task mode accessed before initialization. Use getTaskMode() or wait for taskModeReady.")
- }
- return this._taskMode
- }
- /**
- * Wait for the task API config name to be initialized before proceeding.
- * This method ensures that any operations depending on the task's provider profile
- * will have access to the correct value.
- *
- * ## When to use
- * - Before accessing provider profile-specific configurations
- * - When switching between tasks with different provider profiles
- * - Before operations that depend on the provider profile
- *
- * @returns Promise that resolves when the task API config name is initialized
- * @public
- */
- public async waitForApiConfigInitialization(): Promise<void> {
- return this.taskApiConfigReady
- }
- /**
- * Get the task API config name asynchronously, ensuring it's properly initialized.
- * This is the recommended way to access the task's provider profile as it guarantees
- * the value is available before returning.
- *
- * ## Async behavior
- * - Internally waits for `taskApiConfigReady` promise to resolve
- * - Returns the initialized API config name or undefined as fallback
- * - Safe to call multiple times - subsequent calls return immediately if already initialized
- *
- * @returns Promise resolving to the task API config name string or undefined
- * @public
- */
- public async getTaskApiConfigName(): Promise<string | undefined> {
- await this.taskApiConfigReady
- return this._taskApiConfigName
- }
- /**
- * Get the task API config name synchronously. This should only be used when you're certain
- * that the value has already been initialized (e.g., after waitForApiConfigInitialization).
- *
- * ## When to use
- * - In synchronous contexts where async/await is not available
- * - After explicitly waiting for initialization via `waitForApiConfigInitialization()`
- * - In event handlers or callbacks where API config name is guaranteed to be initialized
- *
- * Note: Unlike taskMode, this getter does not throw if uninitialized since the API config
- * name can legitimately be undefined (backward compatibility with tasks created before
- * this feature was added).
- *
- * @returns The task API config name string or undefined
- * @public
- */
- public get taskApiConfigName(): string | undefined {
- return this._taskApiConfigName
- }
- /**
- * Update the task's API config name. This is called when the user switches
- * provider profiles while a task is active, allowing the task to remember
- * its new provider profile.
- *
- * @param apiConfigName - The new API config name to set
- * @internal
- */
- public setTaskApiConfigName(apiConfigName: string | undefined): void {
- this._taskApiConfigName = apiConfigName
- }
- static create(options: TaskOptions): [Task, Promise<void>] {
- const instance = new Task({ ...options, startTask: false })
- const { images, task, historyItem } = options
- let promise
- if (images || task) {
- promise = instance.startTask(task, images)
- } else if (historyItem) {
- promise = instance.resumeTaskFromHistory()
- } else {
- throw new Error("Either historyItem or task/images must be provided")
- }
- return [instance, promise]
- }
- // API Messages
- private async getSavedApiConversationHistory(): Promise<RooMessage[]> {
- return readRooMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
- }
- private async addToApiConversationHistory(message: RooMessage) {
- // Handle RooReasoningMessage (has `type` instead of `role`)
- if (!("role" in message)) {
- this.apiConversationHistory.push({ ...message, ts: message.ts ?? Date.now() })
- await this.saveApiConversationHistory()
- return
- }
- const handler = this.api as ApiHandler & {
- getResponseId?: () => string | undefined
- getEncryptedContent?: () => { encrypted_content: string; id?: string } | undefined
- }
- if (message.role === "assistant") {
- const responseId = handler.getResponseId?.()
- // Check if the message is already in native AI SDK format (from result.response.messages).
- // These messages have providerOptions on content parts (reasoning signatures, etc.)
- // and don't need manual block injection.
- const hasNativeFormat =
- Array.isArray(message.content) &&
- (message.content as Array<{ providerOptions?: unknown }>).some((p) => p.providerOptions)
- if (hasNativeFormat) {
- // Store directly — the AI SDK response message already has reasoning parts
- // with providerOptions (signatures, redactedData, etc.) in the correct format.
- this.apiConversationHistory.push({
- ...message,
- ...(responseId ? { id: responseId } : {}),
- ts: message.ts ?? Date.now(),
- })
- await this.saveApiConversationHistory()
- return
- }
- // Fallback path: store the manually-constructed message with responseId and timestamp.
- // This handles non-AI-SDK providers and AI SDK responses without reasoning
- // (text-only or text + tool calls where no content parts carry providerOptions).
- const reasoningData = handler.getEncryptedContent?.()
- const messageWithTs: RooAssistantMessage & { content: any } = {
- ...message,
- ...(responseId ? { id: responseId } : {}),
- ts: Date.now(),
- }
- // OpenAI Native encrypted reasoning — the only non-AI-SDK reasoning format still needed
- if (reasoningData?.encrypted_content) {
- const reasoningBlock = {
- type: "reasoning",
- summary: [] as any[],
- encrypted_content: reasoningData.encrypted_content,
- ...(reasoningData.id ? { id: reasoningData.id } : {}),
- }
- if (typeof messageWithTs.content === "string") {
- messageWithTs.content = [reasoningBlock, { type: "text", text: messageWithTs.content } as TextPart]
- } else if (Array.isArray(messageWithTs.content)) {
- messageWithTs.content = [reasoningBlock, ...messageWithTs.content]
- } else if (!messageWithTs.content) {
- messageWithTs.content = [reasoningBlock]
- }
- }
- this.apiConversationHistory.push(messageWithTs)
- } else {
- // For user/tool messages, validate tool_result IDs ONLY when the immediately previous
- // *effective* message is an assistant message.
- const effectiveHistoryForValidation = getEffectiveApiHistory(this.apiConversationHistory)
- const lastEffective = effectiveHistoryForValidation[effectiveHistoryForValidation.length - 1]
- const lastIsAssistant = lastEffective ? isRooAssistantMessage(lastEffective) : false
- const historyForValidation = lastIsAssistant ? effectiveHistoryForValidation : []
- // If the previous effective message is NOT an assistant, convert tool_result blocks to text blocks.
- let messageToAdd: RooMessage = message
- if (!lastIsAssistant && isRooUserMessage(message) && Array.isArray(message.content)) {
- const normalizedUserContent = message.content.map((block) => {
- const typedBlock = block as unknown as { type: string }
- if (!isAnyToolResultBlock(typedBlock)) {
- return block
- }
- const raw = getToolResultContent(typedBlock)
- const textValue = (() => {
- if (typeof raw === "string") return raw
- if (raw && typeof raw === "object" && "value" in raw && typeof raw.value === "string") {
- return raw.value
- }
- return JSON.stringify(raw)
- })()
- return {
- type: "text" as const,
- text: `Tool result:\n${textValue}`,
- }
- })
- messageToAdd = {
- ...message,
- content: normalizedUserContent,
- }
- }
- const validatedMessage = validateAndFixToolResultIds(messageToAdd, historyForValidation)
- const messageWithTs: RooMessage = { ...validatedMessage, ts: Date.now() }
- this.apiConversationHistory.push(messageWithTs)
- }
- await this.saveApiConversationHistory()
- }
- // NOTE: We intentionally do NOT mutate stored messages to merge consecutive user turns.
- // For API requests, consecutive same-role messages are merged via mergeConsecutiveApiMessages()
- // so rewind/edit behavior can still reference original message boundaries.
- async overwriteApiConversationHistory(newHistory: RooMessage[]) {
- this.apiConversationHistory = newHistory
- await this.saveApiConversationHistory()
- }
- /**
- * Flush any pending tool results to the API conversation history.
- *
- * This is critical when the task is about to be
- * delegated (e.g., via new_task). Before delegation, if other tools were
- * called in the same turn before new_task, their tool_result blocks are
- * accumulated in `userMessageContent` but haven't been saved to the API
- * history yet. If we don't flush them before the parent is disposed,
- * the API conversation will be incomplete and cause 400 errors when
- * the parent resumes (missing tool_result for tool_use blocks).
- *
- * NOTE: The assistant message is typically already in history by the time
- * tools execute (added in recursivelyMakeClineRequests after streaming completes).
- * So we usually only need to flush the pending user message with tool_results.
- */
- public async flushPendingToolResultsToHistory(): Promise<boolean> {
- // Only flush if there's actually pending content to save
- if (this.userMessageContent.length === 0 && this.pendingToolResults.length === 0) {
- return true
- }
- // CRITICAL: Wait for the assistant message to be saved to API history first.
- // Without this, tool_result blocks would appear BEFORE tool_use blocks in the
- // conversation history, causing API errors like:
- // "unexpected `tool_use_id` found in `tool_result` blocks"
- //
- // This can happen when parallel tools are called (e.g., update_todo_list + new_task).
- // Tools execute during streaming via presentAssistantMessage, BEFORE the assistant
- // message is saved. When new_task triggers delegation, it calls this method to
- // flush pending results - but the assistant message hasn't been saved yet.
- //
- // The assistantMessageSavedToHistory flag is:
- // - Reset to false at the start of each API request
- // - Set to true after the assistant message is saved in recursivelyMakeClineRequests
- if (!this.assistantMessageSavedToHistory) {
- await pWaitFor(() => this.assistantMessageSavedToHistory || this.abort, {
- interval: 50,
- timeout: 30_000, // 30 second timeout as safety net
- }).catch(() => {
- // If timeout or abort, log and proceed anyway to avoid hanging
- console.warn(
- `[Task#${this.taskId}] flushPendingToolResultsToHistory: timed out waiting for assistant message to be saved`,
- )
- })
- }
- // If task was aborted while waiting, don't flush
- if (this.abort) {
- return false
- }
- // Save pending tool results as a RooToolMessage
- if (this.pendingToolResults.length > 0) {
- const toolMessage: RooToolMessage = {
- role: "tool",
- content: [...this.pendingToolResults],
- ts: Date.now(),
- }
- this.apiConversationHistory.push(toolMessage)
- }
- // Save any text/image user content as a RooUserMessage
- if (this.userMessageContent.length > 0) {
- const userMessage: RooUserMessage = {
- role: "user",
- content: [...this.userMessageContent],
- ts: Date.now(),
- }
- this.apiConversationHistory.push(userMessage)
- }
- const saved = await this.saveApiConversationHistory()
- if (saved) {
- this.userMessageContent = []
- this.pendingToolResults = []
- } else {
- console.warn(
- `[Task#${this.taskId}] flushPendingToolResultsToHistory: save failed, retaining pending tool results in memory`,
- )
- }
- return saved
- }
- private async saveApiConversationHistory(): Promise<boolean> {
- try {
- const saved = await saveRooMessages({
- messages: structuredClone(this.apiConversationHistory),
- taskId: this.taskId,
- globalStoragePath: this.globalStoragePath,
- })
- // saveRooMessages historically returned void in some tests/mocks; treat only explicit false as failure.
- if (saved === false) {
- console.error("Failed to save API conversation history: saveRooMessages returned false")
- return false
- }
- return true
- } catch (error) {
- console.error("Failed to save API conversation history:", error)
- return false
- }
- }
- /**
- * Public wrapper to retry saving the API conversation history.
- * Uses exponential backoff: up to 3 attempts with delays of 100 ms, 500 ms, 1500 ms.
- * Used by delegation flow when flushPendingToolResultsToHistory reports failure.
- */
- public async retrySaveApiConversationHistory(): Promise<boolean> {
- const delays = [100, 500, 1500]
- for (let attempt = 0; attempt < delays.length; attempt++) {
- await new Promise<void>((resolve) => setTimeout(resolve, delays[attempt]))
- console.warn(
- `[Task#${this.taskId}] retrySaveApiConversationHistory: retry attempt ${attempt + 1}/${delays.length}`,
- )
- const success = await this.saveApiConversationHistory()
- if (success) {
- return true
- }
- }
- return false
- }
- // Cline Messages
- private async getSavedClineMessages(): Promise<ClineMessage[]> {
- return readTaskMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
- }
- private async addToClineMessages(message: ClineMessage) {
- this.clineMessages.push(message)
- const provider = this.providerRef.deref()
- // Avoid resending large, mostly-static fields (notably taskHistory) on every chat message update.
- // taskHistory is maintained in-memory in the webview and updated via taskHistoryItemUpdated.
- await provider?.postStateToWebviewWithoutTaskHistory()
- this.emit(RooCodeEventName.Message, { action: "created", message })
- await this.saveClineMessages()
- const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
- if (shouldCaptureMessage) {
- CloudService.instance.captureEvent({
- event: TelemetryEventName.TASK_MESSAGE,
- properties: { taskId: this.taskId, message },
- })
- // Track that this message has been synced to cloud
- this.cloudSyncedMessageTimestamps.add(message.ts)
- }
- }
- public async overwriteClineMessages(newMessages: ClineMessage[]) {
- this.clineMessages = newMessages
- restoreTodoListForTask(this)
- await this.saveClineMessages()
- // When overwriting messages (e.g., during task resume), repopulate the cloud sync tracking Set
- // with timestamps from all non-partial messages to prevent re-syncing previously synced messages
- this.cloudSyncedMessageTimestamps.clear()
- for (const msg of newMessages) {
- if (msg.partial !== true) {
- this.cloudSyncedMessageTimestamps.add(msg.ts)
- }
- }
- }
- private async updateClineMessage(message: ClineMessage) {
- const provider = this.providerRef.deref()
- await provider?.postMessageToWebview({ type: "messageUpdated", clineMessage: message })
- this.emit(RooCodeEventName.Message, { action: "updated", message })
- // Check if we should sync to cloud and haven't already synced this message
- const shouldCaptureMessage = message.partial !== true && CloudService.isEnabled()
- const hasNotBeenSynced = !this.cloudSyncedMessageTimestamps.has(message.ts)
- if (shouldCaptureMessage && hasNotBeenSynced) {
- CloudService.instance.captureEvent({
- event: TelemetryEventName.TASK_MESSAGE,
- properties: { taskId: this.taskId, message },
- })
- // Track that this message has been synced to cloud
- this.cloudSyncedMessageTimestamps.add(message.ts)
- }
- }
- private async saveClineMessages(): Promise<boolean> {
- try {
- await saveTaskMessages({
- messages: structuredClone(this.clineMessages),
- taskId: this.taskId,
- globalStoragePath: this.globalStoragePath,
- })
- if (this._taskApiConfigName === undefined) {
- await this.taskApiConfigReady
- }
- const { historyItem, tokenUsage } = await taskMetadata({
- taskId: this.taskId,
- rootTaskId: this.rootTaskId,
- parentTaskId: this.parentTaskId,
- taskNumber: this.taskNumber,
- messages: this.clineMessages,
- globalStoragePath: this.globalStoragePath,
- workspace: this.cwd,
- mode: this._taskMode || defaultModeSlug, // Use the task's own mode, not the current provider mode.
- apiConfigName: this._taskApiConfigName, // Use the task's own provider profile, not the current provider profile.
- initialStatus: this.initialStatus,
- })
- // Emit token/tool usage updates using debounced function
- // The debounce with maxWait ensures:
- // - Immediate first emit (leading: true)
- // - At most one emit per interval during rapid updates (maxWait)
- // - Final state is emitted when updates stop (trailing: true)
- this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage)
- await this.providerRef.deref()?.updateTaskHistory(historyItem)
- return true
- } catch (error) {
- console.error("Failed to save Roo messages:", error)
- return false
- }
- }
- private findMessageByTimestamp(ts: number): ClineMessage | undefined {
- for (let i = this.clineMessages.length - 1; i >= 0; i--) {
- if (this.clineMessages[i].ts === ts) {
- return this.clineMessages[i]
- }
- }
- return undefined
- }
- // Note that `partial` has three valid states true (partial message),
- // false (completion of partial message), undefined (individual complete
- // message).
- async ask(
- type: ClineAsk,
- text?: string,
- partial?: boolean,
- progressStatus?: ToolProgressStatus,
- isProtected?: boolean,
- ): Promise<{ response: ClineAskResponse; text?: string; images?: string[] }> {
- // If this Cline instance was aborted by the provider, then the only
- // thing keeping us alive is a promise still running in the background,
- // in which case we don't want to send its result to the webview as it
- // is attached to a new instance of Cline now. So we can safely ignore
- // the result of any active promises, and this class will be
- // deallocated. (Although we set Cline = undefined in provider, that
- // simply removes the reference to this instance, but the instance is
- // still alive until this promise resolves or rejects.)
- if (this.abort) {
- throw new Error(`[RooCode#ask] task ${this.taskId}.${this.instanceId} aborted`)
- }
- let askTs: number
- if (partial !== undefined) {
- const lastMessage = this.clineMessages.at(-1)
- const isUpdatingPreviousPartial =
- lastMessage && lastMessage.partial && lastMessage.type === "ask" && lastMessage.ask === type
- if (partial) {
- if (isUpdatingPreviousPartial) {
- // Existing partial message, so update it.
- lastMessage.text = text
- lastMessage.partial = partial
- lastMessage.progressStatus = progressStatus
- lastMessage.isProtected = isProtected
- // TODO: Be more efficient about saving and posting only new
- // data or one whole message at a time so ignore partial for
- // saves, and only post parts of partial message instead of
- // whole array in new listener.
- this.updateClineMessage(lastMessage)
- // console.log("Task#ask: current ask promise was ignored (#1)")
- throw new AskIgnoredError("updating existing partial")
- } else {
- // This is a new partial message, so add it with partial
- // state.
- askTs = Date.now()
- this.lastMessageTs = askTs
- await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, partial, isProtected })
- // console.log("Task#ask: current ask promise was ignored (#2)")
- throw new AskIgnoredError("new partial")
- }
- } else {
- if (isUpdatingPreviousPartial) {
- // This is the complete version of a previously partial
- // message, so replace the partial with the complete version.
- this.askResponse = undefined
- this.askResponseText = undefined
- this.askResponseImages = undefined
- // Bug for the history books:
- // In the webview we use the ts as the chatrow key for the
- // virtuoso list. Since we would update this ts right at the
- // end of streaming, it would cause the view to flicker. The
- // key prop has to be stable otherwise react has trouble
- // reconciling items between renders, causing unmounting and
- // remounting of components (flickering).
- // The lesson here is if you see flickering when rendering
- // lists, it's likely because the key prop is not stable.
- // So in this case we must make sure that the message ts is
- // never altered after first setting it.
- askTs = lastMessage.ts
- this.lastMessageTs = askTs
- lastMessage.text = text
- lastMessage.partial = false
- lastMessage.progressStatus = progressStatus
- lastMessage.isProtected = isProtected
- await this.saveClineMessages()
- this.updateClineMessage(lastMessage)
- } else {
- // This is a new and complete message, so add it like normal.
- this.askResponse = undefined
- this.askResponseText = undefined
- this.askResponseImages = undefined
- askTs = Date.now()
- this.lastMessageTs = askTs
- await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
- }
- }
- } else {
- // This is a new non-partial message, so add it like normal.
- this.askResponse = undefined
- this.askResponseText = undefined
- this.askResponseImages = undefined
- askTs = Date.now()
- this.lastMessageTs = askTs
- await this.addToClineMessages({ ts: askTs, type: "ask", ask: type, text, isProtected })
- }
- let timeouts: NodeJS.Timeout[] = []
- // Automatically approve if the ask according to the user's settings.
- const provider = this.providerRef.deref()
- const state = provider ? await provider.getState() : undefined
- const approval = await checkAutoApproval({ state, ask: type, text, isProtected })
- if (approval.decision === "approve") {
- this.approveAsk()
- } else if (approval.decision === "deny") {
- this.denyAsk()
- } else if (approval.decision === "timeout") {
- // Store the auto-approval timeout so it can be cancelled if user interacts
- this.autoApprovalTimeoutRef = setTimeout(() => {
- const { askResponse, text, images } = approval.fn()
- this.handleWebviewAskResponse(askResponse, text, images)
- this.autoApprovalTimeoutRef = undefined
- }, approval.timeout)
- timeouts.push(this.autoApprovalTimeoutRef)
- }
- // The state is mutable if the message is complete and the task will
- // block (via the `pWaitFor`).
- const isBlocking = !(this.askResponse !== undefined || this.lastMessageTs !== askTs)
- const isMessageQueued = !this.messageQueueService.isEmpty()
- const isStatusMutable = !partial && isBlocking && !isMessageQueued && approval.decision === "ask"
- if (isStatusMutable) {
- const statusMutationTimeout = 2_000
- if (isInteractiveAsk(type)) {
- timeouts.push(
- setTimeout(() => {
- const message = this.findMessageByTimestamp(askTs)
- if (message) {
- this.interactiveAsk = message
- this.emit(RooCodeEventName.TaskInteractive, this.taskId)
- provider?.postMessageToWebview({ type: "interactionRequired" })
- }
- }, statusMutationTimeout),
- )
- } else if (isResumableAsk(type)) {
- timeouts.push(
- setTimeout(() => {
- const message = this.findMessageByTimestamp(askTs)
- if (message) {
- this.resumableAsk = message
- this.emit(RooCodeEventName.TaskResumable, this.taskId)
- }
- }, statusMutationTimeout),
- )
- } else if (isIdleAsk(type)) {
- timeouts.push(
- setTimeout(() => {
- const message = this.findMessageByTimestamp(askTs)
- if (message) {
- this.idleAsk = message
- this.emit(RooCodeEventName.TaskIdle, this.taskId)
- }
- }, statusMutationTimeout),
- )
- }
- } else if (isMessageQueued) {
- const message = this.messageQueueService.dequeueMessage()
- if (message) {
- // Check if this is a tool approval ask that needs to be handled.
- if (type === "tool" || type === "command" || type === "use_mcp_server") {
- // For tool approvals, we need to approve first, then send
- // the message if there's text/images.
- this.handleWebviewAskResponse("yesButtonClicked", message.text, message.images)
- } else {
- // For other ask types (like followup or command_output), fulfill the ask
- // directly.
- this.handleWebviewAskResponse("messageResponse", message.text, message.images)
- }
- }
- }
- // Wait for askResponse to be set
- await pWaitFor(
- () => {
- if (this.askResponse !== undefined || this.lastMessageTs !== askTs) {
- return true
- }
- // If a queued message arrives while we're blocked on an ask (e.g. a follow-up
- // suggestion click that was incorrectly queued due to UI state), consume it
- // immediately so the task doesn't hang.
- if (!this.messageQueueService.isEmpty()) {
- const message = this.messageQueueService.dequeueMessage()
- if (message) {
- // If this is a tool approval ask, we need to approve first (yesButtonClicked)
- // and include any queued text/images.
- if (type === "tool" || type === "command" || type === "use_mcp_server") {
- this.handleWebviewAskResponse("yesButtonClicked", message.text, message.images)
- } else {
- this.handleWebviewAskResponse("messageResponse", message.text, message.images)
- }
- }
- }
- return false
- },
- { interval: 100 },
- )
- if (this.lastMessageTs !== askTs) {
- // Could happen if we send multiple asks in a row i.e. with
- // command_output. It's important that when we know an ask could
- // fail, it is handled gracefully.
- throw new AskIgnoredError("superseded")
- }
- const result = { response: this.askResponse!, text: this.askResponseText, images: this.askResponseImages }
- this.askResponse = undefined
- this.askResponseText = undefined
- this.askResponseImages = undefined
- // Cancel the timeouts if they are still running.
- timeouts.forEach((timeout) => clearTimeout(timeout))
- // Switch back to an active state.
- if (this.idleAsk || this.resumableAsk || this.interactiveAsk) {
- this.idleAsk = undefined
- this.resumableAsk = undefined
- this.interactiveAsk = undefined
- this.emit(RooCodeEventName.TaskActive, this.taskId)
- }
- this.emit(RooCodeEventName.TaskAskResponded)
- return result
- }
- handleWebviewAskResponse(askResponse: ClineAskResponse, text?: string, images?: string[]) {
- // Clear any pending auto-approval timeout when user responds
- this.cancelAutoApprovalTimeout()
- this.askResponse = askResponse
- this.askResponseText = text
- this.askResponseImages = images
- // Create a checkpoint whenever the user sends a message.
- // Use allowEmpty=true to ensure a checkpoint is recorded even if there are no file changes.
- // Suppress the checkpoint_saved chat row for this particular checkpoint to keep the timeline clean.
- if (askResponse === "messageResponse") {
- void this.checkpointSave(false, true)
- }
- // Mark the last follow-up question as answered
- if (askResponse === "messageResponse" || askResponse === "yesButtonClicked") {
- // Find the last unanswered follow-up message using findLastIndex
- const lastFollowUpIndex = findLastIndex(
- this.clineMessages,
- (msg) => msg.type === "ask" && msg.ask === "followup" && !msg.isAnswered,
- )
- if (lastFollowUpIndex !== -1) {
- // Mark this follow-up as answered
- this.clineMessages[lastFollowUpIndex].isAnswered = true
- // Save the updated messages
- this.saveClineMessages().catch((error) => {
- console.error("Failed to save answered follow-up state:", error)
- })
- }
- }
- }
- /**
- * Cancel any pending auto-approval timeout.
- * Called when user interacts (types, clicks buttons, etc.) to prevent the timeout from firing.
- */
- public cancelAutoApprovalTimeout(): void {
- if (this.autoApprovalTimeoutRef) {
- clearTimeout(this.autoApprovalTimeoutRef)
- this.autoApprovalTimeoutRef = undefined
- }
- }
- public approveAsk({ text, images }: { text?: string; images?: string[] } = {}) {
- this.handleWebviewAskResponse("yesButtonClicked", text, images)
- }
- public denyAsk({ text, images }: { text?: string; images?: string[] } = {}) {
- this.handleWebviewAskResponse("noButtonClicked", text, images)
- }
- public supersedePendingAsk(): void {
- this.lastMessageTs = Date.now()
- }
- /**
- * Updates the API configuration and rebuilds the API handler.
- * There is no tool-protocol switching or tool parser swapping.
- *
- * @param newApiConfiguration - The new API configuration to use
- */
- public updateApiConfiguration(newApiConfiguration: ProviderSettings): void {
- // Update the configuration and rebuild the API handler
- this.apiConfiguration = newApiConfiguration
- this.api = buildApiHandler(this.apiConfiguration)
- }
- public async submitUserMessage(
- text: string,
- images?: string[],
- mode?: string,
- providerProfile?: string,
- ): Promise<void> {
- try {
- text = (text ?? "").trim()
- images = images ?? []
- if (text.length === 0 && images.length === 0) {
- return
- }
- const provider = this.providerRef.deref()
- if (provider) {
- if (mode) {
- await provider.setMode(mode)
- }
- if (providerProfile) {
- await provider.setProviderProfile(providerProfile)
- // Update this task's API configuration to match the new profile
- // This ensures the parser state is synchronized with the selected model
- const newState = await provider.getState()
- if (newState?.apiConfiguration) {
- this.updateApiConfiguration(newState.apiConfiguration)
- }
- }
- this.emit(RooCodeEventName.TaskUserMessage, this.taskId)
- // Handle the message directly instead of routing through the webview.
- // This avoids a race condition where the webview's message state hasn't
- // hydrated yet, causing it to interpret the message as a new task request.
- this.handleWebviewAskResponse("messageResponse", text, images)
- } else {
- console.error("[Task#submitUserMessage] Provider reference lost")
- }
- } catch (error) {
- console.error("[Task#submitUserMessage] Failed to submit user message:", error)
- }
- }
- async handleTerminalOperation(terminalOperation: "continue" | "abort") {
- if (terminalOperation === "continue") {
- this.terminalProcess?.continue()
- } else if (terminalOperation === "abort") {
- this.terminalProcess?.abort()
- }
- }
- private async getFilesReadByRooSafely(context: string): Promise<string[] | undefined> {
- try {
- return await this.fileContextTracker.getFilesReadByRoo()
- } catch (error) {
- console.error(`[Task#${context}] Failed to get files read by Roo:`, error)
- return undefined
- }
- }
- public async condenseContext(): Promise<void> {
- // CRITICAL: Flush any pending tool results before condensing
- // to ensure tool_use/tool_result pairs are complete in history
- await this.flushPendingToolResultsToHistory()
- const systemPrompt = await this.getSystemPrompt()
- // Get condensing configuration
- const state = await this.providerRef.deref()?.getState()
- const customCondensingPrompt = state?.customSupportPrompts?.CONDENSE
- const { mode, apiConfiguration } = state ?? {}
- const { contextTokens: prevContextTokens } = this.getTokenUsage()
- // Build tools for condensing metadata (same tools used for normal API calls)
- const provider = this.providerRef.deref()
- let allTools: import("openai").default.Chat.ChatCompletionTool[] = []
- if (provider) {
- const modelInfo = this.api.getModel().info
- const toolsResult = await buildNativeToolsArrayWithRestrictions({
- provider,
- cwd: this.cwd,
- mode,
- customModes: state?.customModes,
- experiments: state?.experiments,
- apiConfiguration,
- disabledTools: state?.disabledTools,
- modelInfo,
- includeAllToolsWithRestrictions: false,
- })
- allTools = toolsResult.tools
- }
- // Build metadata with tools and taskId for the condensing API call
- const metadata: ApiHandlerCreateMessageMetadata = {
- mode,
- taskId: this.taskId,
- ...(allTools.length > 0
- ? {
- tools: allTools,
- tool_choice: "auto",
- parallelToolCalls: true,
- }
- : {}),
- }
- // Generate environment details to include in the condensed summary
- const environmentDetails = await getEnvironmentDetails(this, true)
- const filesReadByRoo = await this.getFilesReadByRooSafely("condenseContext")
- const {
- messages,
- summary,
- cost,
- newContextTokens = 0,
- error,
- errorDetails,
- condenseId,
- } = await summarizeConversation({
- messages: this.apiConversationHistory,
- apiHandler: this.api,
- systemPrompt,
- taskId: this.taskId,
- isAutomaticTrigger: false,
- customCondensingPrompt,
- metadata,
- environmentDetails,
- filesReadByRoo,
- cwd: this.cwd,
- rooIgnoreController: this.rooIgnoreController,
- })
- if (error) {
- await this.say(
- "condense_context_error",
- error,
- undefined /* images */,
- false /* partial */,
- undefined /* checkpoint */,
- undefined /* progressStatus */,
- { isNonInteractive: true } /* options */,
- )
- return
- }
- await this.overwriteApiConversationHistory(messages as RooMessage[])
- const contextCondense: ContextCondense = {
- summary,
- cost,
- newContextTokens,
- prevContextTokens,
- condenseId: condenseId!,
- }
- await this.say(
- "condense_context",
- undefined /* text */,
- undefined /* images */,
- false /* partial */,
- undefined /* checkpoint */,
- undefined /* progressStatus */,
- { isNonInteractive: true } /* options */,
- contextCondense,
- )
- // Process any queued messages after condensing completes
- this.processQueuedMessages()
- }
- async say(
- type: ClineSay,
- text?: string,
- images?: string[],
- partial?: boolean,
- checkpoint?: Record<string, unknown>,
- progressStatus?: ToolProgressStatus,
- options: {
- isNonInteractive?: boolean
- } = {},
- contextCondense?: ContextCondense,
- contextTruncation?: ContextTruncation,
- ): Promise<undefined> {
- if (this.abort) {
- throw new Error(`[RooCode#say] task ${this.taskId}.${this.instanceId} aborted`)
- }
- if (partial !== undefined) {
- const lastMessage = this.clineMessages.at(-1)
- const isUpdatingPreviousPartial =
- lastMessage && lastMessage.partial && lastMessage.type === "say" && lastMessage.say === type
- if (partial) {
- if (isUpdatingPreviousPartial) {
- // Existing partial message, so update it.
- lastMessage.text = text
- lastMessage.images = images
- lastMessage.partial = partial
- lastMessage.progressStatus = progressStatus
- this.updateClineMessage(lastMessage)
- } else {
- // This is a new partial message, so add it with partial state.
- const sayTs = Date.now()
- if (!options.isNonInteractive) {
- this.lastMessageTs = sayTs
- }
- await this.addToClineMessages({
- ts: sayTs,
- type: "say",
- say: type,
- text,
- images,
- partial,
- contextCondense,
- contextTruncation,
- })
- }
- } else {
- // New now have a complete version of a previously partial message.
- // This is the complete version of a previously partial
- // message, so replace the partial with the complete version.
- if (isUpdatingPreviousPartial) {
- if (!options.isNonInteractive) {
- this.lastMessageTs = lastMessage.ts
- }
- lastMessage.text = text
- lastMessage.images = images
- lastMessage.partial = false
- lastMessage.progressStatus = progressStatus
- // Instead of streaming partialMessage events, we do a save
- // and post like normal to persist to disk.
- await this.saveClineMessages()
- // More performant than an entire `postStateToWebview`.
- this.updateClineMessage(lastMessage)
- } else {
- // This is a new and complete message, so add it like normal.
- const sayTs = Date.now()
- if (!options.isNonInteractive) {
- this.lastMessageTs = sayTs
- }
- await this.addToClineMessages({
- ts: sayTs,
- type: "say",
- say: type,
- text,
- images,
- contextCondense,
- contextTruncation,
- })
- }
- }
- } else {
- // This is a new non-partial message, so add it like normal.
- const sayTs = Date.now()
- // A "non-interactive" message is a message is one that the user
- // does not need to respond to. We don't want these message types
- // to trigger an update to `lastMessageTs` since they can be created
- // asynchronously and could interrupt a pending ask.
- if (!options.isNonInteractive) {
- this.lastMessageTs = sayTs
- }
- await this.addToClineMessages({
- ts: sayTs,
- type: "say",
- say: type,
- text,
- images,
- checkpoint,
- contextCondense,
- contextTruncation,
- })
- }
- }
- async sayAndCreateMissingParamError(toolName: ToolName, paramName: string, relPath?: string) {
- await this.say(
- "error",
- `Roo tried to use ${toolName}${
- relPath ? ` for '${relPath.toPosix()}'` : ""
- } without value for required parameter '${paramName}'. Retrying...`,
- )
- return formatResponse.toolError(formatResponse.missingToolParameterError(paramName))
- }
- // Lifecycle
- // Start / Resume / Abort / Dispose
- /**
- * Get enabled MCP tools count for this task.
- * Returns the count along with the number of servers contributing.
- *
- * @returns Object with enabledToolCount and enabledServerCount
- */
- private async getEnabledMcpToolsCount(): Promise<{ enabledToolCount: number; enabledServerCount: number }> {
- try {
- const provider = this.providerRef.deref()
- if (!provider) {
- return { enabledToolCount: 0, enabledServerCount: 0 }
- }
- const { mcpEnabled } = (await provider.getState()) ?? {}
- if (!(mcpEnabled ?? true)) {
- return { enabledToolCount: 0, enabledServerCount: 0 }
- }
- const mcpHub = await McpServerManager.getInstance(provider.context, provider)
- if (!mcpHub) {
- return { enabledToolCount: 0, enabledServerCount: 0 }
- }
- const servers = mcpHub.getServers()
- return countEnabledMcpTools(servers)
- } catch (error) {
- console.error("[Task#getEnabledMcpToolsCount] Error counting MCP tools:", error)
- return { enabledToolCount: 0, enabledServerCount: 0 }
- }
- }
- /**
- * Manually start a **new** task when it was created with `startTask: false`.
- *
- * This fires `startTask` as a background async operation for the
- * `task/images` code-path only. It does **not** handle the
- * `historyItem` resume path (use the constructor with `startTask: true`
- * for that). The primary use-case is in the delegation flow where the
- * parent's metadata must be persisted to globalState **before** the
- * child task begins writing its own history (avoiding a read-modify-write
- * race on globalState).
- */
- public start(): void {
- if (this._started) {
- return
- }
- this._started = true
- const { task, images } = this.metadata
- if (task || images) {
- this.runLifecycleTaskInBackground(this.startTask(task ?? undefined, images ?? undefined), "startTask")
- }
- }
- private async startTask(task?: string, images?: string[]): Promise<void> {
- try {
- if (this.enableBridge) {
- try {
- await BridgeOrchestrator.subscribeToTask(this)
- } catch (error) {
- console.error(
- `[Task#startTask] BridgeOrchestrator.subscribeToTask() failed: ${error instanceof Error ? error.message : String(error)}`,
- )
- }
- }
- // `conversationHistory` (for API) and `clineMessages` (for webview)
- // need to be in sync.
- // If the extension process were killed, then on restart the
- // `clineMessages` might not be empty, so we need to set it to [] when
- // we create a new Cline client (otherwise webview would show stale
- // messages from previous session).
- this.clineMessages = []
- this.apiConversationHistory = []
- // The todo list is already set in the constructor if initialTodos were provided
- // No need to add any messages - the todoList property is already set
- await this.providerRef.deref()?.postStateToWebviewWithoutTaskHistory()
- await this.say("text", task, images)
- // Check for too many MCP tools and warn the user
- const { enabledToolCount, enabledServerCount } = await this.getEnabledMcpToolsCount()
- if (enabledToolCount > MAX_MCP_TOOLS_THRESHOLD) {
- await this.say(
- "too_many_tools_warning",
- JSON.stringify({
- toolCount: enabledToolCount,
- serverCount: enabledServerCount,
- threshold: MAX_MCP_TOOLS_THRESHOLD,
- }),
- undefined,
- undefined,
- undefined,
- undefined,
- { isNonInteractive: true },
- )
- }
- this.isInitialized = true
- const imageBlocks: ImagePart[] = formatResponse.imageBlocks(images)
- // Task starting
- await this.initiateTaskLoop([
- {
- type: "text",
- text: `<user_message>\n${task}\n</user_message>`,
- },
- ...imageBlocks,
- ]).catch((error) => {
- // Swallow loop rejection when the task was intentionally abandoned/aborted
- // during delegation or user cancellation to prevent unhandled rejections.
- if (this.abandoned === true || this.abortReason === "user_cancelled") {
- return
- }
- throw error
- })
- } catch (error) {
- // In tests and some UX flows, tasks can be aborted while `startTask` is still
- // initializing. Treat abort/abandon as expected and avoid unhandled rejections.
- if (this.abandoned === true || this.abort === true || this.abortReason === "user_cancelled") {
- return
- }
- throw error
- }
- }
- private async resumeTaskFromHistory() {
- if (this.enableBridge) {
- try {
- await BridgeOrchestrator.subscribeToTask(this)
- } catch (error) {
- console.error(
- `[Task#resumeTaskFromHistory] BridgeOrchestrator.subscribeToTask() failed: ${error instanceof Error ? error.message : String(error)}`,
- )
- }
- }
- const modifiedClineMessages = await this.getSavedClineMessages()
- // Remove any resume messages that may have been added before.
- const lastRelevantMessageIndex = findLastIndex(
- modifiedClineMessages,
- (m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task"),
- )
- if (lastRelevantMessageIndex !== -1) {
- modifiedClineMessages.splice(lastRelevantMessageIndex + 1)
- }
- // Remove any trailing reasoning-only UI messages that were not part of the persisted API conversation
- while (modifiedClineMessages.length > 0) {
- const last = modifiedClineMessages[modifiedClineMessages.length - 1]
- if (last.type === "say" && last.say === "reasoning") {
- modifiedClineMessages.pop()
- } else {
- break
- }
- }
- // Since we don't use `api_req_finished` anymore, we need to check if the
- // last `api_req_started` has a cost value, if it doesn't and no
- // cancellation reason to present, then we remove it since it indicates
- // an api request without any partial content streamed.
- const lastApiReqStartedIndex = findLastIndex(
- modifiedClineMessages,
- (m) => m.type === "say" && m.say === "api_req_started",
- )
- if (lastApiReqStartedIndex !== -1) {
- const lastApiReqStarted = modifiedClineMessages[lastApiReqStartedIndex]
- const { cost, cancelReason }: ClineApiReqInfo = JSON.parse(lastApiReqStarted.text || "{}")
- if (cost === undefined && cancelReason === undefined) {
- modifiedClineMessages.splice(lastApiReqStartedIndex, 1)
- }
- }
- await this.overwriteClineMessages(modifiedClineMessages)
- this.clineMessages = await this.getSavedClineMessages()
- // Now present the cline messages to the user and ask if they want to
- // resume (NOTE: we ran into a bug before where the
- // apiConversationHistory wouldn't be initialized when opening a old
- // task, and it was because we were waiting for resume).
- // This is important in case the user deletes messages without resuming
- // the task first.
- this.apiConversationHistory = await this.getSavedApiConversationHistory()
- const lastClineMessage = this.clineMessages
- .slice()
- .reverse()
- .find((m) => !(m.ask === "resume_task" || m.ask === "resume_completed_task")) // Could be multiple resume tasks.
- let askType: ClineAsk
- if (lastClineMessage?.ask === "completion_result") {
- askType = "resume_completed_task"
- } else {
- askType = "resume_task"
- }
- this.isInitialized = true
- const { response, text, images } = await this.ask(askType) // Calls `postStateToWebview`.
- let responseText: string | undefined
- let responseImages: string[] | undefined
- if (response === "messageResponse") {
- await this.say("user_feedback", text, images)
- responseText = text
- responseImages = images
- }
- // Make sure that the api conversation history can be resumed by the API,
- // even if it goes out of sync with cline messages.
- const existingApiConversationHistory: RooMessage[] = await this.getSavedApiConversationHistory()
- // If the last message is an assistant message with tool calls, every tool call
- // needs a corresponding tool result. Create a RooToolMessage with "interrupted"
- // results for any missing ones.
- // If the last message is a user message, check the preceding assistant for
- // unmatched tool calls and fill in missing tool results.
- // In RooMessage format, tool results live in RooToolMessage (not in user messages).
- let modifiedOldUserContent: UserContentPart[]
- let modifiedApiConversationHistory: RooMessage[]
- if (existingApiConversationHistory.length > 0) {
- // Find the last message that has a role (skip RooReasoningMessage items)
- let lastMsgIndex = existingApiConversationHistory.length - 1
- while (lastMsgIndex >= 0 && isRooReasoningMessage(existingApiConversationHistory[lastMsgIndex])) {
- lastMsgIndex--
- }
- if (lastMsgIndex < 0) {
- throw new Error("Unexpected: No user or assistant messages in API conversation history")
- }
- const lastMessage = existingApiConversationHistory[lastMsgIndex]
- if (isRooAssistantMessage(lastMessage)) {
- const content = Array.isArray(lastMessage.content) ? lastMessage.content : []
- const toolCallParts = content.filter((part): part is ToolCallPart => part.type === "tool-call")
- if (toolCallParts.length > 0) {
- const toolResults: ToolResultPart[] = toolCallParts.map((tc) => ({
- type: "tool-result" as const,
- toolCallId: tc.toolCallId,
- toolName: tc.toolName,
- output: {
- type: "text" as const,
- value: "Task was interrupted before this tool call could be completed.",
- },
- }))
- const toolMessage: RooToolMessage = { role: "tool", content: toolResults }
- modifiedApiConversationHistory = [...existingApiConversationHistory, toolMessage]
- modifiedOldUserContent = []
- } else {
- modifiedApiConversationHistory = [...existingApiConversationHistory]
- modifiedOldUserContent = []
- }
- } else if (isRooUserMessage(lastMessage)) {
- // Find the preceding assistant message (skip tool/reasoning messages)
- let prevAssistantIndex = lastMsgIndex - 1
- while (
- prevAssistantIndex >= 0 &&
- !isRooAssistantMessage(existingApiConversationHistory[prevAssistantIndex])
- ) {
- prevAssistantIndex--
- }
- const previousAssistantMessage =
- prevAssistantIndex >= 0 ? existingApiConversationHistory[prevAssistantIndex] : undefined
- // Extract existing user content for initiateTaskLoop
- const existingUserContent: UserContentPart[] = Array.isArray(lastMessage.content)
- ? (lastMessage.content as UserContentPart[])
- : [{ type: "text" as const, text: String(lastMessage.content) }]
- if (previousAssistantMessage && isRooAssistantMessage(previousAssistantMessage)) {
- const assistantContent = Array.isArray(previousAssistantMessage.content)
- ? previousAssistantMessage.content
- : []
- const toolCallParts = assistantContent.filter(
- (part): part is ToolCallPart => part.type === "tool-call",
- )
- if (toolCallParts.length > 0) {
- // Collect tool call IDs that already have results (in tool messages between assistant and user)
- const answeredToolCallIds = new Set<string>()
- for (let i = prevAssistantIndex + 1; i < lastMsgIndex; i++) {
- const msg = existingApiConversationHistory[i]
- if (isRooToolMessage(msg) && Array.isArray(msg.content)) {
- for (const part of msg.content) {
- if (part.type === "tool-result") {
- answeredToolCallIds.add((part as ToolResultPart).toolCallId)
- }
- }
- }
- }
- const missingToolCalls = toolCallParts.filter((tc) => !answeredToolCallIds.has(tc.toolCallId))
- // Remove last user message; add missing tool results as a RooToolMessage
- const historyWithoutLastUser = existingApiConversationHistory.slice(0, lastMsgIndex)
- if (missingToolCalls.length > 0) {
- const missingResults: ToolResultPart[] = missingToolCalls.map((tc) => ({
- type: "tool-result" as const,
- toolCallId: tc.toolCallId,
- toolName: tc.toolName,
- output: {
- type: "text" as const,
- value: "Task was interrupted before this tool call could be completed.",
- },
- }))
- const toolMessage: RooToolMessage = { role: "tool", content: missingResults }
- modifiedApiConversationHistory = [...historyWithoutLastUser, toolMessage]
- } else {
- modifiedApiConversationHistory = historyWithoutLastUser
- }
- // Strip any legacy tool_result / tool-result blocks from old user content
- modifiedOldUserContent = existingUserContent.filter(
- (block) => !isAnyToolResultBlock(block as { type: string }),
- )
- } else {
- modifiedApiConversationHistory = existingApiConversationHistory.slice(0, lastMsgIndex)
- modifiedOldUserContent = [...existingUserContent]
- }
- } else {
- modifiedApiConversationHistory = existingApiConversationHistory.slice(0, lastMsgIndex)
- modifiedOldUserContent = [...existingUserContent]
- }
- } else if (isRooToolMessage(lastMessage)) {
- // Last message is a tool result — no user message was added yet
- modifiedApiConversationHistory = [...existingApiConversationHistory]
- modifiedOldUserContent = []
- } else {
- throw new Error("Unexpected: Last message is not a user, assistant, or tool message")
- }
- } else {
- throw new Error("Unexpected: No existing API conversation history")
- }
- let newUserContent: UserContentPart[] = [...modifiedOldUserContent]
- const agoText = ((): string => {
- const timestamp = lastClineMessage?.ts ?? Date.now()
- const now = Date.now()
- const diff = now - timestamp
- const minutes = Math.floor(diff / 60000)
- const hours = Math.floor(minutes / 60)
- const days = Math.floor(hours / 24)
- if (days > 0) {
- return `${days} day${days > 1 ? "s" : ""} ago`
- }
- if (hours > 0) {
- return `${hours} hour${hours > 1 ? "s" : ""} ago`
- }
- if (minutes > 0) {
- return `${minutes} minute${minutes > 1 ? "s" : ""} ago`
- }
- return "just now"
- })()
- if (responseText) {
- newUserContent.push({
- type: "text",
- text: `<user_message>\n${responseText}\n</user_message>`,
- })
- }
- if (responseImages && responseImages.length > 0) {
- newUserContent.push(...formatResponse.imageBlocks(responseImages))
- }
- // Ensure we have at least some content to send to the API.
- // If newUserContent is empty, add a minimal resumption message.
- if (newUserContent.length === 0) {
- newUserContent.push({
- type: "text",
- text: "[TASK RESUMPTION] Resuming task...",
- })
- }
- await this.overwriteApiConversationHistory(modifiedApiConversationHistory)
- // Task resuming from history item.
- await this.initiateTaskLoop(newUserContent)
- }
- /**
- * Cancels the current HTTP request if one is in progress.
- * This immediately aborts the underlying stream rather than waiting for the next chunk.
- */
- public cancelCurrentRequest(): void {
- if (this.currentRequestAbortController) {
- console.log(`[Task#${this.taskId}.${this.instanceId}] Aborting current HTTP request`)
- this.currentRequestAbortController.abort()
- this.currentRequestAbortController = undefined
- }
- }
- /**
- * Force emit a final token usage update, ignoring throttle.
- * Called before task completion or abort to ensure final stats are captured.
- * Triggers the debounce with current values and immediately flushes to ensure emit.
- */
- public emitFinalTokenUsageUpdate(): void {
- const tokenUsage = this.getTokenUsage()
- this.debouncedEmitTokenUsage(tokenUsage, this.toolUsage)
- this.debouncedEmitTokenUsage.flush()
- }
- public async abortTask(isAbandoned = false) {
- // Aborting task
- // Will stop any autonomously running promises.
- if (isAbandoned) {
- this.abandoned = true
- }
- this.abort = true
- // Reset consecutive error counters on abort (manual intervention)
- this.consecutiveNoToolUseCount = 0
- this.consecutiveNoAssistantMessagesCount = 0
- // Force final token usage update before abort event
- this.emitFinalTokenUsageUpdate()
- this.emit(RooCodeEventName.TaskAborted)
- try {
- this.dispose() // Call the centralized dispose method
- } catch (error) {
- console.error(`Error during task ${this.taskId}.${this.instanceId} disposal:`, error)
- // Don't rethrow - we want abort to always succeed
- }
- // Save the countdown message in the automatic retry or other content.
- try {
- // Save the countdown message in the automatic retry or other content.
- await this.saveClineMessages()
- } catch (error) {
- console.error(`Error saving messages during abort for task ${this.taskId}.${this.instanceId}:`, error)
- }
- }
- public dispose(): void {
- console.log(`[Task#dispose] disposing task ${this.taskId}.${this.instanceId}`)
- // Cancel any in-progress HTTP request
- try {
- this.cancelCurrentRequest()
- } catch (error) {
- console.error("Error cancelling current request:", error)
- }
- // Remove provider profile change listener
- try {
- if (this.providerProfileChangeListener) {
- const provider = this.providerRef.deref()
- if (provider) {
- provider.off(RooCodeEventName.ProviderProfileChanged, this.providerProfileChangeListener)
- }
- this.providerProfileChangeListener = undefined
- }
- } catch (error) {
- console.error("Error removing provider profile change listener:", error)
- }
- // Dispose message queue and remove event listeners.
- try {
- if (this.messageQueueStateChangedHandler) {
- this.messageQueueService.removeListener("stateChanged", this.messageQueueStateChangedHandler)
- this.messageQueueStateChangedHandler = undefined
- }
- this.messageQueueService.dispose()
- } catch (error) {
- console.error("Error disposing message queue:", error)
- }
- // Remove all event listeners to prevent memory leaks.
- try {
- this.removeAllListeners()
- } catch (error) {
- console.error("Error removing event listeners:", error)
- }
- if (this.enableBridge) {
- BridgeOrchestrator.getInstance()
- ?.unsubscribeFromTask(this.taskId)
- .catch((error) =>
- console.error(
- `[Task#dispose] BridgeOrchestrator#unsubscribeFromTask() failed: ${error instanceof Error ? error.message : String(error)}`,
- ),
- )
- }
- // Release any terminals associated with this task.
- try {
- // Release any terminals associated with this task.
- TerminalRegistry.releaseTerminalsForTask(this.taskId)
- } catch (error) {
- console.error("Error releasing terminals:", error)
- }
- // Cleanup command output artifacts
- getTaskDirectoryPath(this.globalStoragePath, this.taskId)
- .then((taskDir) => {
- const outputDir = path.join(taskDir, "command-output")
- return OutputInterceptor.cleanup(outputDir)
- })
- .catch((error) => {
- console.error("Error cleaning up command output artifacts:", error)
- })
- try {
- if (this.rooIgnoreController) {
- this.rooIgnoreController.dispose()
- this.rooIgnoreController = undefined
- }
- } catch (error) {
- console.error("Error disposing RooIgnoreController:", error)
- // This is the critical one for the leak fix.
- }
- try {
- this.fileContextTracker.dispose()
- } catch (error) {
- console.error("Error disposing file context tracker:", error)
- }
- try {
- // If we're not streaming then `abortStream` won't be called.
- if (this.isStreaming && this.diffViewProvider.isEditing) {
- this.diffViewProvider.revertChanges().catch(console.error)
- }
- } catch (error) {
- console.error("Error reverting diff changes:", error)
- }
- }
- // Subtasks
- // Spawn / Wait / Complete
- public async startSubtask(message: string, initialTodos: TodoItem[], mode: string) {
- const provider = this.providerRef.deref()
- if (!provider) {
- throw new Error("Provider not available")
- }
- const child = await (provider as any).delegateParentAndOpenChild({
- parentTaskId: this.taskId,
- message,
- initialTodos,
- mode,
- })
- return child
- }
- /**
- * Resume parent task after delegation completion without showing resume ask.
- * Used in metadata-driven subtask flow.
- *
- * This method:
- * - Clears any pending ask states
- * - Resets abort and streaming flags
- * - Ensures next API call includes full context
- * - Immediately continues task loop without user interaction
- */
- public async resumeAfterDelegation(): Promise<void> {
- // Clear any ask states that might have been set during history load
- this.idleAsk = undefined
- this.resumableAsk = undefined
- this.interactiveAsk = undefined
- // Reset abort and streaming state to ensure clean continuation
- this.abort = false
- this.abandoned = false
- this.abortReason = undefined
- this.didFinishAbortingStream = false
- this.isStreaming = false
- this.isWaitingForFirstChunk = false
- // Ensure next API call includes full context after delegation
- this.skipPrevResponseIdOnce = true
- // Mark as initialized and active
- this.isInitialized = true
- this.emit(RooCodeEventName.TaskActive, this.taskId)
- // Load conversation history if not already loaded
- if (this.apiConversationHistory.length === 0) {
- this.apiConversationHistory = await this.getSavedApiConversationHistory()
- }
- // Add environment details to the existing last user message (which contains the tool_result)
- // This avoids creating a new user message which would cause consecutive user messages
- const environmentDetails = await getEnvironmentDetails(this, true)
- let lastUserMsgIndex = -1
- for (let i = this.apiConversationHistory.length - 1; i >= 0; i--) {
- const msg = this.apiConversationHistory[i]
- if ("role" in msg && msg.role === "user") {
- lastUserMsgIndex = i
- break
- }
- }
- if (lastUserMsgIndex >= 0) {
- const lastUserMsg = this.apiConversationHistory[lastUserMsgIndex] as any
- if (Array.isArray(lastUserMsg.content)) {
- // Remove any existing environment_details blocks before adding fresh ones
- const contentWithoutEnvDetails = lastUserMsg.content.filter((block: any) => {
- if (block.type === "text" && typeof block.text === "string") {
- const isEnvironmentDetailsBlock =
- block.text.trim().startsWith("<environment_details>") &&
- block.text.trim().endsWith("</environment_details>")
- return !isEnvironmentDetailsBlock
- }
- return true
- })
- // Add fresh environment details
- lastUserMsg.content = [...contentWithoutEnvDetails, { type: "text" as const, text: environmentDetails }]
- }
- }
- // Save the updated history
- await this.saveApiConversationHistory()
- // Continue task loop - pass empty array to signal no new user content needed
- // The initiateTaskLoop will handle this by skipping user message addition
- await this.initiateTaskLoop([])
- }
- // Task Loop
- private async initiateTaskLoop(userContent: UserContentPart[]): Promise<void> {
- // Kicks off the checkpoints initialization process in the background.
- getCheckpointService(this)
- let nextUserContent = userContent
- let includeFileDetails = true
- this.emit(RooCodeEventName.TaskStarted)
- while (!this.abort) {
- const didEndLoop = await this.recursivelyMakeClineRequests(nextUserContent, includeFileDetails)
- includeFileDetails = false // We only need file details the first time.
- // The way this agentic loop works is that cline will be given a
- // task that he then calls tools to complete. Unless there's an
- // attempt_completion call, we keep responding back to him with his
- // tool's responses until he either attempt_completion or does not
- // use anymore tools. If he does not use anymore tools, we ask him
- // to consider if he's completed the task and then call
- // attempt_completion, otherwise proceed with completing the task.
- // There is a MAX_REQUESTS_PER_TASK limit to prevent infinite
- // requests, but Cline is prompted to finish the task as efficiently
- // as he can.
- if (didEndLoop) {
- // For now a task never 'completes'. This will only happen if
- // the user hits max requests and denies resetting the count.
- break
- } else {
- nextUserContent = [{ type: "text", text: formatResponse.noToolsUsed() }]
- }
- }
- }
- public async recursivelyMakeClineRequests(
- userContent: UserContentPart[],
- includeFileDetails: boolean = false,
- ): Promise<boolean> {
- interface StackItem {
- userContent: UserContentPart[]
- includeFileDetails: boolean
- retryAttempt?: number
- userMessageWasRemoved?: boolean // Track if user message was removed due to empty response
- }
- const stack: StackItem[] = [{ userContent, includeFileDetails, retryAttempt: 0 }]
- while (stack.length > 0) {
- const currentItem = stack.pop()!
- const currentUserContent = currentItem.userContent
- const currentIncludeFileDetails = currentItem.includeFileDetails
- if (this.abort) {
- throw new Error(`[RooCode#recursivelyMakeRooRequests] task ${this.taskId}.${this.instanceId} aborted`)
- }
- if (this.consecutiveMistakeLimit > 0 && this.consecutiveMistakeCount >= this.consecutiveMistakeLimit) {
- // Track consecutive mistake errors in telemetry via event and PostHog exception tracking.
- // The reason is "no_tools_used" because this limit is reached via initiateTaskLoop
- // which increments consecutiveMistakeCount when the model doesn't use any tools.
- TelemetryService.instance.captureConsecutiveMistakeError(this.taskId)
- TelemetryService.instance.captureException(
- new ConsecutiveMistakeError(
- `Task reached consecutive mistake limit (${this.consecutiveMistakeLimit})`,
- this.taskId,
- this.consecutiveMistakeCount,
- this.consecutiveMistakeLimit,
- "no_tools_used",
- this.apiConfiguration.apiProvider,
- getModelId(this.apiConfiguration),
- ),
- )
- const { response, text, images } = await this.ask(
- "mistake_limit_reached",
- t("common:errors.mistake_limit_guidance"),
- )
- if (response === "messageResponse") {
- currentUserContent.push(
- ...[
- { type: "text" as const, text: formatResponse.tooManyMistakes(text) },
- ...formatResponse.imageBlocks(images),
- ],
- )
- await this.say("user_feedback", text, images)
- }
- this.consecutiveMistakeCount = 0
- }
- // Getting verbose details is an expensive operation, it uses ripgrep to
- // top-down build file structure of project which for large projects can
- // take a few seconds. For the best UX we show a placeholder api_req_started
- // message with a loading spinner as this happens.
- // Determine API protocol based on provider and model
- const modelId = getModelId(this.apiConfiguration)
- const apiProvider = this.apiConfiguration.apiProvider
- const apiProtocol = getApiProtocol(
- apiProvider && !isRetiredProvider(apiProvider) ? apiProvider : undefined,
- modelId,
- )
- // Respect user-configured provider rate limiting BEFORE we emit api_req_started.
- // This prevents the UI from showing an "API Request..." spinner while we are
- // intentionally waiting due to the rate limit slider.
- //
- // NOTE: We also set Task.lastGlobalApiRequestTime here to reserve this slot
- // before we build environment details (which can take time).
- // This ensures subsequent requests (including subtasks) still honour the
- // provider rate-limit window.
- await this.maybeWaitForProviderRateLimit(currentItem.retryAttempt ?? 0)
- Task.lastGlobalApiRequestTime = performance.now()
- await this.say(
- "api_req_started",
- JSON.stringify({
- apiProtocol,
- }),
- )
- const {
- showRooIgnoredFiles = false,
- includeDiagnosticMessages = true,
- maxDiagnosticMessages = 50,
- } = (await this.providerRef.deref()?.getState()) ?? {}
- const { content: parsedUserContent, mode: slashCommandMode } = await processUserContentMentions({
- userContent: currentUserContent as Array<TextPart | ImagePart>,
- cwd: this.cwd,
- fileContextTracker: this.fileContextTracker,
- rooIgnoreController: this.rooIgnoreController,
- showRooIgnoredFiles,
- includeDiagnosticMessages,
- maxDiagnosticMessages,
- })
- // Switch mode if specified in a slash command's frontmatter
- if (slashCommandMode) {
- const provider = this.providerRef.deref()
- if (provider) {
- const state = await provider.getState()
- const targetMode = getModeBySlug(slashCommandMode, state?.customModes)
- if (targetMode) {
- await provider.handleModeSwitch(slashCommandMode)
- }
- }
- }
- const environmentDetails = await getEnvironmentDetails(this, currentIncludeFileDetails)
- // Remove any existing environment_details blocks before adding fresh ones.
- // This prevents duplicate environment details when resuming tasks,
- // where the old user message content may already contain environment details from the previous session.
- // We check for both opening and closing tags to ensure we're matching complete environment detail blocks,
- // not just mentions of the tag in regular content.
- const contentWithoutEnvDetails = parsedUserContent.filter((block) => {
- if (block.type === "text" && typeof block.text === "string") {
- // Check if this text block is a complete environment_details block
- // by verifying it starts with the opening tag and ends with the closing tag
- const isEnvironmentDetailsBlock =
- block.text.trim().startsWith("<environment_details>") &&
- block.text.trim().endsWith("</environment_details>")
- return !isEnvironmentDetailsBlock
- }
- return true
- })
- // Add environment details as its own text block, separate from tool
- // results.
- let finalUserContent = [...contentWithoutEnvDetails, { type: "text" as const, text: environmentDetails }]
- // Only add user message to conversation history if:
- // 1. This is the first attempt (retryAttempt === 0), AND
- // 2. The original userContent was not empty (empty signals delegation resume where
- // the user message with tool_result and env details is already in history), OR
- // 3. The message was removed in a previous iteration (userMessageWasRemoved === true)
- // This prevents consecutive user messages while allowing re-add when needed
- const isEmptyUserContent = currentUserContent.length === 0
- const shouldAddUserMessage =
- ((currentItem.retryAttempt ?? 0) === 0 && !isEmptyUserContent) || currentItem.userMessageWasRemoved
- if (shouldAddUserMessage) {
- const userMessage: RooUserMessage = { role: "user", content: finalUserContent }
- await this.addToApiConversationHistory(userMessage)
- TelemetryService.instance.captureConversationMessage(this.taskId, "user")
- }
- // Since we sent off a placeholder api_req_started message to update the
- // webview while waiting to actually start the API request (to load
- // potential details for example), we need to update the text of that
- // message.
- const lastApiReqIndex = findLastIndex(this.clineMessages, (m) => m.say === "api_req_started")
- this.clineMessages[lastApiReqIndex].text = JSON.stringify({
- apiProtocol,
- } satisfies ClineApiReqInfo)
- await this.saveClineMessages()
- await this.providerRef.deref()?.postStateToWebviewWithoutTaskHistory()
- try {
- let cacheWriteTokens = 0
- let cacheReadTokens = 0
- let inputTokens = 0
- let outputTokens = 0
- let totalCost: number | undefined
- // We can't use `api_req_finished` anymore since it's a unique case
- // where it could come after a streaming message (i.e. in the middle
- // of being updated or executed).
- // Fortunately `api_req_finished` was always parsed out for the GUI
- // anyways, so it remains solely for legacy purposes to keep track
- // of prices in tasks from history (it's worth removing a few months
- // from now).
- const updateApiReqMsg = (cancelReason?: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
- if (lastApiReqIndex < 0 || !this.clineMessages[lastApiReqIndex]) {
- return
- }
- const existingData = JSON.parse(this.clineMessages[lastApiReqIndex].text || "{}")
- // Calculate total tokens and cost using provider-aware function
- const modelId = getModelId(this.apiConfiguration)
- const apiProvider = this.apiConfiguration.apiProvider
- const apiProtocol = getApiProtocol(
- apiProvider && !isRetiredProvider(apiProvider) ? apiProvider : undefined,
- modelId,
- )
- const costResult =
- apiProtocol === "anthropic"
- ? calculateApiCostAnthropic(
- streamModelInfo,
- inputTokens,
- outputTokens,
- cacheWriteTokens,
- cacheReadTokens,
- )
- : calculateApiCostOpenAI(
- streamModelInfo,
- inputTokens,
- outputTokens,
- cacheWriteTokens,
- cacheReadTokens,
- )
- this.clineMessages[lastApiReqIndex].text = JSON.stringify({
- ...existingData,
- tokensIn: costResult.totalInputTokens,
- tokensOut: costResult.totalOutputTokens,
- cacheWrites: cacheWriteTokens,
- cacheReads: cacheReadTokens,
- cost: totalCost ?? costResult.totalCost,
- cancelReason,
- streamingFailedMessage,
- } satisfies ClineApiReqInfo)
- }
- const abortStream = async (cancelReason: ClineApiReqCancelReason, streamingFailedMessage?: string) => {
- if (this.diffViewProvider.isEditing) {
- await this.diffViewProvider.revertChanges() // closes diff view
- }
- // if last message is a partial we need to update and save it
- const lastMessage = this.clineMessages.at(-1)
- if (lastMessage && lastMessage.partial) {
- // lastMessage.ts = Date.now() DO NOT update ts since it is used as a key for virtuoso list
- lastMessage.partial = false
- // instead of streaming partialMessage events, we do a save and post like normal to persist to disk
- }
- // Update `api_req_started` to have cancelled and cost, so that
- // we can display the cost of the partial stream and the cancellation reason
- updateApiReqMsg(cancelReason, streamingFailedMessage)
- await this.saveClineMessages()
- // Signals to provider that it can retrieve the saved messages
- // from disk, as abortTask can not be awaited on in nature.
- this.didFinishAbortingStream = true
- }
- // Reset streaming state for each new API request
- this.currentStreamingContentIndex = 0
- this.currentStreamingDidCheckpoint = false
- this.assistantMessageContent = []
- this.didCompleteReadingStream = false
- this.userMessageContent = []
- this.pendingToolResults = []
- this.userMessageContentReady = false
- this.didRejectTool = false
- this.didAlreadyUseTool = false
- this.assistantMessageSavedToHistory = false
- // Reset tool failure flag for each new assistant turn - this ensures that tool failures
- // only prevent attempt_completion within the same assistant message, not across turns
- // (e.g., if a tool fails, then user sends a message saying "just complete anyway")
- this.didToolFailInCurrentTurn = false
- this.presentAssistantMessageLocked = false
- this.presentAssistantMessageHasPendingUpdates = false
- // No legacy text-stream tool parser.
- this.streamingToolCallIndices.clear()
- // Clear any leftover streaming tool call state from previous interrupted streams
- NativeToolCallParser.clearAllStreamingToolCalls()
- NativeToolCallParser.clearRawChunkState()
- await this.diffViewProvider.reset()
- // Cache model info once per API request to avoid repeated calls during streaming
- // This is especially important for tools and background usage collection
- this.cachedStreamingModel = this.api.getModel()
- const streamModelInfo = this.cachedStreamingModel.info
- const cachedModelId = this.cachedStreamingModel.id
- // Yields only if the first chunk is successful, otherwise will
- // allow the user to retry the request (most likely due to rate
- // limit error, which gets thrown on the first chunk).
- const stream = this.attemptApiRequest(currentItem.retryAttempt ?? 0, { skipProviderRateLimit: true })
- let assistantMessage = ""
- let reasoningMessage = ""
- let responseAssistantMessage: AssistantModelMessage | undefined
- let pendingGroundingSources: GroundingSource[] = []
- this.isStreaming = true
- try {
- const iterator = stream[Symbol.asyncIterator]()
- // Helper to race iterator.next() with abort signal
- const nextChunkWithAbort = async () => {
- const nextPromise = iterator.next()
- // If we have an abort controller, race it with the next chunk
- if (this.currentRequestAbortController) {
- const abortPromise = new Promise<never>((_, reject) => {
- const signal = this.currentRequestAbortController!.signal
- if (signal.aborted) {
- reject(new Error("Request cancelled by user"))
- } else {
- signal.addEventListener("abort", () => {
- reject(new Error("Request cancelled by user"))
- })
- }
- })
- return await Promise.race([nextPromise, abortPromise])
- }
- // No abort controller, just return the next chunk normally
- return await nextPromise
- }
- let item = await nextChunkWithAbort()
- while (!item.done) {
- const chunk = item.value
- item = await nextChunkWithAbort()
- if (!chunk) {
- // Sometimes chunk is undefined, no idea that can cause
- // it, but this workaround seems to fix it.
- continue
- }
- switch (chunk.type) {
- case "reasoning": {
- reasoningMessage += chunk.text
- // Only apply formatting if the message contains sentence-ending punctuation followed by **
- let formattedReasoning = reasoningMessage
- if (reasoningMessage.includes("**")) {
- // Add line breaks before **Title** patterns that appear after sentence endings
- // This targets section headers like "...end of sentence.**Title Here**"
- // Handles periods, exclamation marks, and question marks
- formattedReasoning = reasoningMessage.replace(
- /([.!?])\*\*([^*\n]+)\*\*/g,
- "$1\n\n**$2**",
- )
- }
- await this.say("reasoning", formattedReasoning, undefined, true)
- break
- }
- case "usage":
- inputTokens += chunk.inputTokens
- outputTokens += chunk.outputTokens
- cacheWriteTokens += chunk.cacheWriteTokens ?? 0
- cacheReadTokens += chunk.cacheReadTokens ?? 0
- totalCost = chunk.totalCost
- break
- case "grounding":
- // Handle grounding sources separately from regular content
- // to prevent state persistence issues - store them separately
- if (chunk.sources && chunk.sources.length > 0) {
- pendingGroundingSources.push(...chunk.sources)
- }
- break
- case "tool_call_partial": {
- // Process raw tool call chunk through NativeToolCallParser
- // which handles tracking, buffering, and emits events
- const events = NativeToolCallParser.processRawChunk({
- index: chunk.index,
- id: chunk.id,
- name: chunk.name,
- arguments: chunk.arguments,
- })
- for (const event of events) {
- this.handleToolCallEvent(event)
- }
- break
- }
- // Direct handlers for AI SDK tool streaming events (DeepSeek, Moonshot, etc.)
- // These providers emit tool_call_start/delta/end directly instead of tool_call_partial
- case "tool_call_start":
- case "tool_call_delta":
- case "tool_call_end":
- this.handleToolCallEvent(chunk)
- break
- case "tool_call": {
- // Legacy: Handle complete tool calls (for backward compatibility)
- // Convert native tool call to ToolUse format
- const toolUse = NativeToolCallParser.parseToolCall({
- id: chunk.id,
- name: chunk.name as ToolName,
- arguments: chunk.arguments,
- })
- if (!toolUse) {
- console.error(`Failed to parse tool call for task ${this.taskId}:`, chunk)
- break
- }
- // Store the tool call ID on the ToolUse object for later reference
- // This is needed to create tool_result blocks that reference the correct tool_use_id
- toolUse.id = chunk.id
- // Add the tool use to assistant message content
- this.assistantMessageContent.push(toolUse)
- // Mark that we have new content to process
- this.userMessageContentReady = false
- // Present the tool call to user - presentAssistantMessage will execute
- // tools sequentially and accumulate all results in userMessageContent
- presentAssistantMessage(this)
- break
- }
- case "text": {
- assistantMessage += chunk.text
- // Native tool calling: text chunks are plain text.
- // Create or update a text content block directly
- const lastBlock = this.assistantMessageContent[this.assistantMessageContent.length - 1]
- if (lastBlock?.type === "text" && lastBlock.partial) {
- lastBlock.content = assistantMessage
- } else {
- this.assistantMessageContent.push({
- type: "text",
- content: assistantMessage,
- partial: true,
- })
- this.userMessageContentReady = false
- }
- presentAssistantMessage(this)
- break
- }
- case "response_message":
- responseAssistantMessage = chunk.message
- break
- }
- if (this.abort) {
- console.log(`aborting stream, this.abandoned = ${this.abandoned}`)
- if (!this.abandoned) {
- // Only need to gracefully abort if this instance
- // isn't abandoned (sometimes OpenRouter stream
- // hangs, in which case this would affect future
- // instances of Cline).
- await abortStream("user_cancelled")
- }
- break // Aborts the stream.
- }
- if (this.didRejectTool) {
- // `userContent` has a tool rejection, so interrupt the
- // assistant's response to present the user's feedback.
- assistantMessage += "\n\n[Response interrupted by user feedback]"
- // Instead of setting this preemptively, we allow the
- // present iterator to finish and set
- // userMessageContentReady when its ready.
- // this.userMessageContentReady = true
- break
- }
- if (this.didAlreadyUseTool) {
- assistantMessage +=
- "\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.]"
- break
- }
- }
- // Create a copy of current token values to avoid race conditions
- const currentTokens = {
- input: inputTokens,
- output: outputTokens,
- cacheWrite: cacheWriteTokens,
- cacheRead: cacheReadTokens,
- total: totalCost,
- }
- const drainStreamInBackgroundToFindAllUsage = async (apiReqIndex: number) => {
- const timeoutMs = DEFAULT_USAGE_COLLECTION_TIMEOUT_MS
- const startTime = performance.now()
- const modelId = getModelId(this.apiConfiguration)
- // Local variables to accumulate usage data without affecting the main flow
- let bgInputTokens = currentTokens.input
- let bgOutputTokens = currentTokens.output
- let bgCacheWriteTokens = currentTokens.cacheWrite
- let bgCacheReadTokens = currentTokens.cacheRead
- let bgTotalCost = currentTokens.total
- // Helper function to capture telemetry and update messages
- const captureUsageData = async (
- tokens: {
- input: number
- output: number
- cacheWrite: number
- cacheRead: number
- total?: number
- },
- messageIndex: number = apiReqIndex,
- ) => {
- if (
- tokens.input > 0 ||
- tokens.output > 0 ||
- tokens.cacheWrite > 0 ||
- tokens.cacheRead > 0
- ) {
- // Update the shared variables atomically
- inputTokens = tokens.input
- outputTokens = tokens.output
- cacheWriteTokens = tokens.cacheWrite
- cacheReadTokens = tokens.cacheRead
- totalCost = tokens.total
- // Update the API request message with the latest usage data
- updateApiReqMsg()
- await this.saveClineMessages()
- // Update the specific message in the webview
- const apiReqMessage = this.clineMessages[messageIndex]
- if (apiReqMessage) {
- await this.updateClineMessage(apiReqMessage)
- }
- // Capture telemetry with provider-aware cost calculation
- const modelId = getModelId(this.apiConfiguration)
- const apiProvider = this.apiConfiguration.apiProvider
- const apiProtocol = getApiProtocol(
- apiProvider && !isRetiredProvider(apiProvider) ? apiProvider : undefined,
- modelId,
- )
- // Use the appropriate cost function based on the API protocol
- const costResult =
- apiProtocol === "anthropic"
- ? calculateApiCostAnthropic(
- streamModelInfo,
- tokens.input,
- tokens.output,
- tokens.cacheWrite,
- tokens.cacheRead,
- )
- : calculateApiCostOpenAI(
- streamModelInfo,
- tokens.input,
- tokens.output,
- tokens.cacheWrite,
- tokens.cacheRead,
- )
- TelemetryService.instance.captureLlmCompletion(this.taskId, {
- inputTokens: costResult.totalInputTokens,
- outputTokens: costResult.totalOutputTokens,
- cacheWriteTokens: tokens.cacheWrite,
- cacheReadTokens: tokens.cacheRead,
- cost: tokens.total ?? costResult.totalCost,
- })
- }
- }
- try {
- // Continue processing the original stream from where the main loop left off
- let usageFound = false
- let chunkCount = 0
- // Use the same iterator that the main loop was using
- while (!item.done) {
- // Check for timeout
- if (performance.now() - startTime > timeoutMs) {
- console.warn(
- `[Background Usage Collection] Timed out after ${timeoutMs}ms for model: ${modelId}, processed ${chunkCount} chunks`,
- )
- // Clean up the iterator before breaking
- if (iterator.return) {
- await iterator.return(undefined)
- }
- break
- }
- const chunk = item.value
- item = await iterator.next()
- chunkCount++
- if (chunk && chunk.type === "usage") {
- usageFound = true
- bgInputTokens += chunk.inputTokens
- bgOutputTokens += chunk.outputTokens
- bgCacheWriteTokens += chunk.cacheWriteTokens ?? 0
- bgCacheReadTokens += chunk.cacheReadTokens ?? 0
- bgTotalCost = chunk.totalCost
- }
- }
- if (
- usageFound ||
- bgInputTokens > 0 ||
- bgOutputTokens > 0 ||
- bgCacheWriteTokens > 0 ||
- bgCacheReadTokens > 0
- ) {
- // We have usage data either from a usage chunk or accumulated tokens
- await captureUsageData(
- {
- input: bgInputTokens,
- output: bgOutputTokens,
- cacheWrite: bgCacheWriteTokens,
- cacheRead: bgCacheReadTokens,
- total: bgTotalCost,
- },
- lastApiReqIndex,
- )
- } else {
- console.warn(
- `[Background Usage Collection] Suspicious: request ${apiReqIndex} is complete, but no usage info was found. Model: ${modelId}`,
- )
- }
- } catch (error) {
- console.error("Error draining stream for usage data:", error)
- // Still try to capture whatever usage data we have collected so far
- if (
- bgInputTokens > 0 ||
- bgOutputTokens > 0 ||
- bgCacheWriteTokens > 0 ||
- bgCacheReadTokens > 0
- ) {
- await captureUsageData(
- {
- input: bgInputTokens,
- output: bgOutputTokens,
- cacheWrite: bgCacheWriteTokens,
- cacheRead: bgCacheReadTokens,
- total: bgTotalCost,
- },
- lastApiReqIndex,
- )
- }
- }
- }
- // Start the background task and handle any errors
- drainStreamInBackgroundToFindAllUsage(lastApiReqIndex).catch((error) => {
- console.error("Background usage collection failed:", error)
- })
- } catch (error) {
- // Abandoned happens when extension is no longer waiting for the
- // Cline instance to finish aborting (error is thrown here when
- // any function in the for loop throws due to this.abort).
- if (!this.abandoned) {
- // Determine cancellation reason
- const cancelReason: ClineApiReqCancelReason = this.abort ? "user_cancelled" : "streaming_failed"
- const rawErrorMessage = error.message ?? JSON.stringify(serializeError(error), null, 2)
- // Check auto-retry state BEFORE abortStream so we can suppress the error
- // message on the api_req_started row when backoffAndAnnounce will display it instead.
- const stateForBackoff = await this.providerRef.deref()?.getState()
- const willAutoRetry = !this.abort && stateForBackoff?.autoApprovalEnabled
- const streamingFailedMessage = this.abort
- ? undefined
- : willAutoRetry
- ? undefined // backoffAndAnnounce will display the error with retry countdown
- : `${t("common:interruption.streamTerminatedByProvider")}: ${rawErrorMessage}`
- // Clean up partial state
- await abortStream(cancelReason, streamingFailedMessage)
- if (this.abort) {
- // User cancelled - abort the entire task
- this.abortReason = cancelReason
- await this.abortTask()
- } else {
- // Stream failed - log the error and retry with the same content
- // The existing rate limiting will prevent rapid retries
- console.error(
- `[Task#${this.taskId}.${this.instanceId}] Stream failed, will retry: ${rawErrorMessage}`,
- )
- // Apply exponential backoff similar to first-chunk errors when auto-resubmit is enabled
- if (stateForBackoff?.autoApprovalEnabled) {
- await this.backoffAndAnnounce(currentItem.retryAttempt ?? 0, error)
- // Check if task was aborted during the backoff
- if (this.abort) {
- console.log(
- `[Task#${this.taskId}.${this.instanceId}] Task aborted during mid-stream retry backoff`,
- )
- // Abort the entire task
- this.abortReason = "user_cancelled"
- await this.abortTask()
- break
- }
- }
- // Push the same content back onto the stack to retry, incrementing the retry attempt counter
- stack.push({
- userContent: currentUserContent,
- includeFileDetails: false,
- retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
- })
- // Continue to retry the request
- continue
- }
- }
- } finally {
- this.isStreaming = false
- // Clean up the abort controller when streaming completes
- this.currentRequestAbortController = undefined
- }
- // Need to call here in case the stream was aborted.
- if (this.abort || this.abandoned) {
- throw new Error(
- `[RooCode#recursivelyMakeRooRequests] task ${this.taskId}.${this.instanceId} aborted`,
- )
- }
- this.didCompleteReadingStream = true
- // Set any blocks to be complete to allow `presentAssistantMessage`
- // to finish and set `userMessageContentReady` to true.
- // (Could be a text block that had no subsequent tool uses, or a
- // text block at the very end, or an invalid tool use, etc. Whatever
- // the case, `presentAssistantMessage` relies on these blocks either
- // to be completed or the user to reject a block in order to proceed
- // and eventually set userMessageContentReady to true.)
- // Finalize any remaining streaming tool calls that weren't explicitly ended
- // This is critical for MCP tools which need tool_call_end events to be properly
- // converted from ToolUse to McpToolUse via finalizeStreamingToolCall()
- const finalizeEvents = NativeToolCallParser.finalizeRawChunks()
- for (const event of finalizeEvents) {
- if (event.type === "tool_call_end") {
- // Finalize the streaming tool call
- const finalToolUse = NativeToolCallParser.finalizeStreamingToolCall(event.id)
- // Get the index for this tool call
- const toolUseIndex = this.streamingToolCallIndices.get(event.id)
- if (finalToolUse) {
- // Store the tool call ID
- ;(finalToolUse as any).id = event.id
- // Get the index and replace partial with final
- if (toolUseIndex !== undefined) {
- this.assistantMessageContent[toolUseIndex] = finalToolUse
- }
- // Clean up tracking
- this.streamingToolCallIndices.delete(event.id)
- // Mark that we have new content to process
- this.userMessageContentReady = false
- // Present the finalized tool call
- presentAssistantMessage(this)
- } else if (toolUseIndex !== undefined) {
- // finalizeStreamingToolCall returned null (malformed JSON or missing args)
- // We still need to mark the tool as non-partial so it gets executed
- // The tool's validation will catch any missing required parameters
- const existingToolUse = this.assistantMessageContent[toolUseIndex]
- if (existingToolUse && existingToolUse.type === "tool_use") {
- existingToolUse.partial = false
- // Ensure it has the ID for native protocol
- ;(existingToolUse as any).id = event.id
- }
- // Clean up tracking
- this.streamingToolCallIndices.delete(event.id)
- // Mark that we have new content to process
- this.userMessageContentReady = false
- // Present the tool call - validation will handle missing params
- presentAssistantMessage(this)
- }
- }
- }
- // IMPORTANT: Capture partialBlocks AFTER finalizeRawChunks() to avoid double-presentation.
- // Tools finalized above are already presented, so we only want blocks still partial after finalization.
- const partialBlocks = this.assistantMessageContent.filter((block) => block.partial)
- partialBlocks.forEach((block) => (block.partial = false))
- // Can't just do this b/c a tool could be in the middle of executing.
- // this.assistantMessageContent.forEach((e) => (e.partial = false))
- // No legacy streaming parser to finalize.
- // Note: updateApiReqMsg() is now called from within drainStreamInBackgroundToFindAllUsage
- // to ensure usage data is captured even when the stream is interrupted. The background task
- // uses local variables to accumulate usage data before atomically updating the shared state.
- // Complete the reasoning message if it exists
- // We can't use say() here because the reasoning message may not be the last message
- // (other messages like text blocks or tool uses may have been added after it during streaming)
- if (reasoningMessage) {
- const lastReasoningIndex = findLastIndex(
- this.clineMessages,
- (m) => m.type === "say" && m.say === "reasoning",
- )
- if (lastReasoningIndex !== -1 && this.clineMessages[lastReasoningIndex].partial) {
- this.clineMessages[lastReasoningIndex].partial = false
- await this.updateClineMessage(this.clineMessages[lastReasoningIndex])
- }
- }
- await this.saveClineMessages()
- await this.providerRef.deref()?.postStateToWebviewWithoutTaskHistory()
- // No legacy text-stream tool parser state to reset.
- // CRITICAL: Save assistant message to API history BEFORE executing tools.
- // This ensures that when new_task triggers delegation and calls flushPendingToolResultsToHistory(),
- // the assistant message is already in history. Otherwise, tool_result blocks would appear
- // BEFORE their corresponding tool_use blocks, causing API errors.
- // Check if we have any content to process (text or tool uses)
- const hasTextContent = assistantMessage.length > 0
- const hasToolUses = this.assistantMessageContent.some(
- (block) => block.type === "tool_use" || block.type === "mcp_tool_use",
- )
- if (hasTextContent || hasToolUses) {
- // Reset counter when we get a successful response with content
- this.consecutiveNoAssistantMessagesCount = 0
- // Display grounding sources to the user if they exist
- if (pendingGroundingSources.length > 0) {
- const citationLinks = pendingGroundingSources.map((source, i) => `[${i + 1}](${source.url})`)
- const sourcesText = `${t("common:gemini.sources")} ${citationLinks.join(", ")}`
- await this.say("text", sourcesText, undefined, false, undefined, undefined, {
- isNonInteractive: true,
- })
- }
- // Build the assistant message content array
- const assistantContent: Array<TextPart | ToolCallPart> = []
- // Add text content if present
- if (assistantMessage) {
- assistantContent.push({
- type: "text" as const,
- text: assistantMessage,
- })
- }
- // Add tool_use blocks with their IDs for native protocol
- // This handles both regular ToolUse and McpToolUse types
- // IMPORTANT: Track seen IDs to prevent duplicates in the API request.
- // Duplicate tool_use IDs cause Anthropic API 400 errors:
- // "tool_use ids must be unique"
- const seenToolUseIds = new Set<string>()
- const toolUseBlocks = this.assistantMessageContent.filter(
- (block) => block.type === "tool_use" || block.type === "mcp_tool_use",
- )
- for (const block of toolUseBlocks) {
- if (block.type === "mcp_tool_use") {
- // McpToolUse already has the original tool name (e.g., "mcp_serverName_toolName")
- // The arguments are the raw tool arguments (matching the simplified schema)
- const mcpBlock = block as import("../../shared/tools").McpToolUse
- if (mcpBlock.id) {
- const sanitizedId = sanitizeToolUseId(mcpBlock.id)
- // Pre-flight deduplication: Skip if we've already added this ID
- if (seenToolUseIds.has(sanitizedId)) {
- console.warn(
- `[Task#${this.taskId}] Pre-flight deduplication: Skipping duplicate MCP tool_use ID: ${sanitizedId} (tool: ${mcpBlock.name})`,
- )
- continue
- }
- seenToolUseIds.add(sanitizedId)
- assistantContent.push({
- type: "tool-call" as const,
- toolCallId: sanitizedId,
- toolName: mcpBlock.name, // Original dynamic name
- input: mcpBlock.arguments, // Direct tool arguments
- })
- }
- } else {
- // Regular ToolUse
- const toolUse = block as import("../../shared/tools").ToolUse
- const toolCallId = toolUse.id
- if (toolCallId) {
- const sanitizedId = sanitizeToolUseId(toolCallId)
- // Pre-flight deduplication: Skip if we've already added this ID
- if (seenToolUseIds.has(sanitizedId)) {
- console.warn(
- `[Task#${this.taskId}] Pre-flight deduplication: Skipping duplicate tool_use ID: ${sanitizedId} (tool: ${toolUse.name})`,
- )
- continue
- }
- seenToolUseIds.add(sanitizedId)
- // nativeArgs is already in the correct API format for all tools
- const input = toolUse.nativeArgs || toolUse.params
- // Use originalName (alias) if present for API history consistency.
- // When tool aliases are used (e.g., "edit_file" -> "search_and_replace" -> "edit" (current canonical name)),
- // we want the alias name in the conversation history to match what the model
- // was told the tool was named, preventing confusion in multi-turn conversations.
- const toolNameForHistory = toolUse.originalName ?? toolUse.name
- assistantContent.push({
- type: "tool-call" as const,
- toolCallId: sanitizedId,
- toolName: toolNameForHistory,
- input,
- })
- }
- }
- }
- // Enforce new_task isolation: if new_task is called alongside other tools,
- // truncate any tools that come after it and inject error tool_results.
- // This prevents orphaned tools when delegation disposes the parent task.
- const newTaskIndex = assistantContent.findIndex(
- (block) => block.type === "tool-call" && (block as ToolCallPart).toolName === "new_task",
- )
- if (newTaskIndex !== -1 && newTaskIndex < assistantContent.length - 1) {
- // new_task found but not last - truncate subsequent tools
- const truncatedTools = assistantContent.slice(newTaskIndex + 1)
- assistantContent.length = newTaskIndex + 1 // Truncate API history array
- // ALSO truncate the execution array (assistantMessageContent) to prevent
- // tools after new_task from being executed by presentAssistantMessage().
- // Find new_task index in assistantMessageContent (may differ from assistantContent
- // due to text blocks being structured differently).
- const executionNewTaskIndex = this.assistantMessageContent.findIndex(
- (block) => block.type === "tool_use" && block.name === "new_task",
- )
- if (executionNewTaskIndex !== -1) {
- this.assistantMessageContent.length = executionNewTaskIndex + 1
- }
- // Pre-inject error tool_results for truncated tools
- for (const tool of truncatedTools) {
- if (tool.type !== "tool-call") continue
- const toolCallId = getToolCallId(tool as AnyToolCallBlock)
- const toolName = getToolCallName(tool as AnyToolCallBlock)
- if (toolCallId) {
- this.pushToolResultToUserContent({
- type: "tool-result",
- toolCallId: sanitizeToolUseId(toolCallId),
- toolName,
- output: {
- type: "text",
- value: "[ERROR] This tool was not executed because new_task was called in the same message turn. The new_task tool must be the last tool in a message.",
- },
- })
- }
- }
- }
- // Save assistant message BEFORE executing tools
- // This is critical for new_task: when it triggers delegation, flushPendingToolResultsToHistory()
- // will save the user message with tool_results. The assistant message must already be in history
- // so that tool_result blocks appear AFTER their corresponding tool_use blocks.
- let assistantMessageForHistory: RooAssistantMessage
- if (responseAssistantMessage) {
- // AI SDK response message is already in native format with providerOptions —
- // store directly without manual reasoning/signature reconstruction.
- // If new_task isolation truncated local tool-calls, apply the same truncation
- // to the native response message so persisted history stays consistent.
- let normalizedResponseMessage = responseAssistantMessage
- if (Array.isArray(normalizedResponseMessage.content)) {
- const responseNewTaskIndex = normalizedResponseMessage.content.findIndex(
- (part) => part.type === "tool-call" && part.toolName === "new_task",
- )
- if (
- responseNewTaskIndex !== -1 &&
- responseNewTaskIndex < normalizedResponseMessage.content.length - 1
- ) {
- normalizedResponseMessage = {
- ...normalizedResponseMessage,
- content: normalizedResponseMessage.content.slice(0, responseNewTaskIndex + 1),
- }
- }
- }
- assistantMessageForHistory = {
- ...normalizedResponseMessage,
- ts: Date.now(),
- }
- } else {
- // Fallback: manual construction for non-AI-SDK providers
- assistantMessageForHistory = {
- role: "assistant",
- content: assistantContent,
- ts: Date.now(),
- }
- }
- await this.addToApiConversationHistory(assistantMessageForHistory)
- this.assistantMessageSavedToHistory = true
- TelemetryService.instance.captureConversationMessage(this.taskId, "assistant")
- }
- // Present any partial blocks that were just completed.
- // Tool calls are typically presented during streaming via tool_call_partial events,
- // but we still present here if any partial blocks remain (e.g., malformed streams).
- // NOTE: This MUST happen AFTER saving the assistant message to API history.
- // When new_task is in the batch, it triggers delegation which calls flushPendingToolResultsToHistory().
- // If the assistant message isn't saved yet, tool_results would appear before tool_use blocks.
- if (partialBlocks.length > 0) {
- // If there is content to update then it will complete and
- // update `this.userMessageContentReady` to true, which we
- // `pWaitFor` before making the next request.
- presentAssistantMessage(this)
- }
- if (hasTextContent || hasToolUses) {
- // NOTE: This comment is here for future reference - this was a
- // workaround for `userMessageContent` not getting set to true.
- // It was due to it not recursively calling for partial blocks
- // when `didRejectTool`, so it would get stuck waiting for a
- // partial block to complete before it could continue.
- // In case the content blocks finished it may be the api stream
- // finished after the last parsed content block was executed, so
- // we are able to detect out of bounds and set
- // `userMessageContentReady` to true (note you should not call
- // `presentAssistantMessage` since if the last block i
- // completed it will be presented again).
- // const completeBlocks = this.assistantMessageContent.filter((block) => !block.partial) // If there are any partial blocks after the stream ended we can consider them invalid.
- // if (this.currentStreamingContentIndex >= completeBlocks.length) {
- // this.userMessageContentReady = true
- // }
- await pWaitFor(() => this.userMessageContentReady)
- // If the model did not tool use, then we need to tell it to
- // either use a tool or attempt_completion.
- const didToolUse = this.assistantMessageContent.some(
- (block) => block.type === "tool_use" || block.type === "mcp_tool_use",
- )
- if (!didToolUse) {
- // Increment consecutive no-tool-use counter
- this.consecutiveNoToolUseCount++
- // Only show error and count toward mistake limit after 2 consecutive failures
- if (this.consecutiveNoToolUseCount >= 2) {
- await this.say("error", "MODEL_NO_TOOLS_USED")
- // Only count toward mistake limit after second consecutive failure
- this.consecutiveMistakeCount++
- }
- // Use the task's locked protocol for consistent behavior
- this.userMessageContent.push({
- type: "text",
- text: formatResponse.noToolsUsed(),
- })
- } else {
- // Reset counter when tools are used successfully
- this.consecutiveNoToolUseCount = 0
- }
- // Save pending tool results to conversation history as a RooToolMessage.
- // After the RooMessage migration, tool results are in pendingToolResults
- // (separate from userMessageContent) and must be explicitly saved.
- // We don't use flushPendingToolResultsToHistory() here because that also
- // flushes userMessageContent — which should instead go via the stack to
- // become part of the next iteration's user message.
- if (this.pendingToolResults.length > 0) {
- const toolMessage: RooToolMessage = {
- role: "tool",
- content: [...this.pendingToolResults],
- ts: Date.now(),
- }
- const previousHistoryLength = this.apiConversationHistory.length
- this.apiConversationHistory.push(toolMessage)
- const saved = await this.saveApiConversationHistory()
- if (saved) {
- this.pendingToolResults = []
- } else {
- // Keep pending results for retry and roll back in-memory insertion to avoid duplicates.
- this.apiConversationHistory = this.apiConversationHistory.slice(0, previousHistoryLength)
- console.warn(
- `[Task#${this.taskId}] Failed to persist pending tool results in main loop; keeping pending results for retry`,
- )
- }
- }
- // Push to stack if there's content OR if we're paused waiting for a subtask.
- // When paused, we push an empty item so the loop continues to the pause check.
- if (this.userMessageContent.length > 0 || this.isPaused) {
- stack.push({
- userContent: [...this.userMessageContent] as UserContentPart[], // Create a copy to avoid mutation issues
- includeFileDetails: false, // Subsequent iterations don't need file details
- })
- // Add periodic yielding to prevent blocking
- await new Promise((resolve) => setImmediate(resolve))
- }
- continue
- } else {
- // If there's no assistant_responses, that means we got no text
- // or tool_use content blocks from API which we should assume is
- // an error.
- // Increment consecutive no-assistant-messages counter
- this.consecutiveNoAssistantMessagesCount++
- // Only show error and count toward mistake limit after 2 consecutive failures
- // This provides a "grace retry" - first failure retries silently
- if (this.consecutiveNoAssistantMessagesCount >= 2) {
- await this.say("error", "MODEL_NO_ASSISTANT_MESSAGES")
- }
- // IMPORTANT: We already added the user message to
- // apiConversationHistory at line 1876. Since the assistant failed to respond,
- // we need to remove that message before retrying to avoid having two consecutive
- // user messages (which would cause tool_result validation errors).
- let state = await this.providerRef.deref()?.getState()
- if (this.apiConversationHistory.length > 0) {
- const lastMessage = this.apiConversationHistory[this.apiConversationHistory.length - 1]
- if ("role" in lastMessage && lastMessage.role === "user") {
- // Remove the last user message that we added earlier
- this.apiConversationHistory.pop()
- }
- }
- // Check if we should auto-retry or prompt the user
- // Reuse the state variable from above
- if (state?.autoApprovalEnabled) {
- // Auto-retry with backoff - don't persist failure message when retrying
- await this.backoffAndAnnounce(
- currentItem.retryAttempt ?? 0,
- new Error(
- "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.",
- ),
- )
- // Check if task was aborted during the backoff
- if (this.abort) {
- console.log(
- `[Task#${this.taskId}.${this.instanceId}] Task aborted during empty-assistant retry backoff`,
- )
- break
- }
- // Push the same content back onto the stack to retry, incrementing the retry attempt counter
- // Mark that user message was removed so it gets re-added on retry
- stack.push({
- userContent: currentUserContent,
- includeFileDetails: false,
- retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
- userMessageWasRemoved: true,
- })
- // Continue to retry the request
- continue
- } else {
- // Prompt the user for retry decision
- const { response } = await this.ask(
- "api_req_failed",
- "The model returned no assistant messages. This may indicate an issue with the API or the model's output.",
- )
- if (response === "yesButtonClicked") {
- await this.say("api_req_retried")
- // Push the same content back to retry
- stack.push({
- userContent: currentUserContent,
- includeFileDetails: false,
- retryAttempt: (currentItem.retryAttempt ?? 0) + 1,
- })
- // Continue to retry the request
- continue
- } else {
- // User declined to retry
- // Re-add the user message we removed.
- await this.addToApiConversationHistory({
- role: "user",
- content: currentUserContent,
- } as RooMessage)
- await this.say(
- "error",
- "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.",
- )
- await this.addToApiConversationHistory({
- role: "assistant",
- content: [{ type: "text", text: "Failure: I did not provide a response." }],
- })
- }
- }
- }
- // If we reach here without continuing, return false (will always be false for now)
- return false
- } catch (error) {
- // This should never happen since the only thing that can throw an
- // error is the attemptApiRequest, which is wrapped in a try catch
- // that sends an ask where if noButtonClicked, will clear current
- // task and destroy this instance. However to avoid unhandled
- // promise rejection, we will end this loop which will end execution
- // of this instance (see `startTask`).
- return true // Needs to be true so parent loop knows to end task.
- }
- }
- // If we exit the while loop normally (stack is empty), return false
- return false
- }
- private async getSystemPrompt(): Promise<string> {
- const { mcpEnabled } = (await this.providerRef.deref()?.getState()) ?? {}
- let mcpHub: McpHub | undefined
- if (mcpEnabled ?? true) {
- const provider = this.providerRef.deref()
- if (!provider) {
- throw new Error("Provider reference lost during view transition")
- }
- // Wait for MCP hub initialization through McpServerManager
- mcpHub = await McpServerManager.getInstance(provider.context, provider)
- if (!mcpHub) {
- throw new Error("Failed to get MCP hub from server manager")
- }
- // Wait for MCP servers to be connected before generating system prompt
- await pWaitFor(() => !mcpHub!.isConnecting, { timeout: 10_000 }).catch(() => {
- console.error("MCP servers failed to connect in time")
- })
- }
- const rooIgnoreInstructions = this.rooIgnoreController?.getInstructions()
- const state = await this.providerRef.deref()?.getState()
- const {
- mode,
- customModes,
- customModePrompts,
- customInstructions,
- experiments,
- language,
- apiConfiguration,
- enableSubfolderRules,
- } = state ?? {}
- return await (async () => {
- const provider = this.providerRef.deref()
- if (!provider) {
- throw new Error("Provider not available")
- }
- const modelInfo = this.api.getModel().info
- return SYSTEM_PROMPT(
- provider.context,
- this.cwd,
- false,
- mcpHub,
- this.diffStrategy,
- mode ?? defaultModeSlug,
- customModePrompts,
- customModes,
- customInstructions,
- experiments,
- language,
- rooIgnoreInstructions,
- {
- todoListEnabled: apiConfiguration?.todoListEnabled ?? true,
- useAgentRules:
- vscode.workspace.getConfiguration(Package.name).get<boolean>("useAgentRules") ?? true,
- enableSubfolderRules: enableSubfolderRules ?? false,
- newTaskRequireTodos: vscode.workspace
- .getConfiguration(Package.name)
- .get<boolean>("newTaskRequireTodos", false),
- isStealthModel: modelInfo?.isStealthModel,
- },
- undefined, // todoList
- this.api.getModel().id,
- provider.getSkillsManager(),
- )
- })()
- }
- private getCurrentProfileId(state: any): string {
- return (
- state?.listApiConfigMeta?.find((profile: any) => profile.name === state?.currentApiConfigName)?.id ??
- "default"
- )
- }
- private async handleContextWindowExceededError(): Promise<void> {
- const state = await this.providerRef.deref()?.getState()
- const { profileThresholds = {}, mode, apiConfiguration } = state ?? {}
- const { contextTokens } = this.getTokenUsage()
- const modelInfo = this.api.getModel().info
- const maxTokens = getModelMaxOutputTokens({
- modelId: this.api.getModel().id,
- model: modelInfo,
- settings: this.apiConfiguration,
- })
- const contextWindow = modelInfo.contextWindow
- // Get the current profile ID using the helper method
- const currentProfileId = this.getCurrentProfileId(state)
- // Log the context window error for debugging
- console.warn(
- `[Task#${this.taskId}] Context window exceeded for model ${this.api.getModel().id}. ` +
- `Current tokens: ${contextTokens}, Context window: ${contextWindow}. ` +
- `Forcing truncation to ${FORCED_CONTEXT_REDUCTION_PERCENT}% of current context.`,
- )
- // Send condenseTaskContextStarted to show in-progress indicator
- await this.providerRef.deref()?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId })
- // Build tools for condensing metadata (same tools used for normal API calls)
- const provider = this.providerRef.deref()
- let allTools: import("openai").default.Chat.ChatCompletionTool[] = []
- if (provider) {
- const toolsResult = await buildNativeToolsArrayWithRestrictions({
- provider,
- cwd: this.cwd,
- mode,
- customModes: state?.customModes,
- experiments: state?.experiments,
- apiConfiguration,
- disabledTools: state?.disabledTools,
- modelInfo,
- includeAllToolsWithRestrictions: false,
- })
- allTools = toolsResult.tools
- }
- // Build metadata with tools and taskId for the condensing API call
- const metadata: ApiHandlerCreateMessageMetadata = {
- mode,
- taskId: this.taskId,
- ...(allTools.length > 0
- ? {
- tools: allTools,
- tool_choice: "auto",
- parallelToolCalls: true,
- }
- : {}),
- }
- try {
- // Generate environment details to include in the condensed summary
- const environmentDetails = await getEnvironmentDetails(this, true)
- // Force aggressive truncation by keeping only 75% of the conversation history
- const truncateResult = await manageContext({
- messages: this.apiConversationHistory,
- totalTokens: contextTokens || 0,
- maxTokens,
- contextWindow,
- apiHandler: this.api,
- autoCondenseContext: true,
- autoCondenseContextPercent: FORCED_CONTEXT_REDUCTION_PERCENT,
- systemPrompt: await this.getSystemPrompt(),
- taskId: this.taskId,
- profileThresholds,
- currentProfileId,
- metadata,
- environmentDetails,
- })
- if (truncateResult.messages !== this.apiConversationHistory) {
- await this.overwriteApiConversationHistory(truncateResult.messages as RooMessage[])
- }
- if (truncateResult.summary) {
- const { summary, cost, prevContextTokens, newContextTokens = 0 } = truncateResult
- const contextCondense: ContextCondense = { summary, cost, newContextTokens, prevContextTokens }
- await this.say(
- "condense_context",
- undefined /* text */,
- undefined /* images */,
- false /* partial */,
- undefined /* checkpoint */,
- undefined /* progressStatus */,
- { isNonInteractive: true } /* options */,
- contextCondense,
- )
- } else if (truncateResult.truncationId) {
- // Sliding window truncation occurred (fallback when condensing fails or is disabled)
- const contextTruncation: ContextTruncation = {
- truncationId: truncateResult.truncationId,
- messagesRemoved: truncateResult.messagesRemoved ?? 0,
- prevContextTokens: truncateResult.prevContextTokens,
- newContextTokens: truncateResult.newContextTokensAfterTruncation ?? 0,
- }
- await this.say(
- "sliding_window_truncation",
- undefined /* text */,
- undefined /* images */,
- false /* partial */,
- undefined /* checkpoint */,
- undefined /* progressStatus */,
- { isNonInteractive: true } /* options */,
- undefined /* contextCondense */,
- contextTruncation,
- )
- }
- } finally {
- // Notify webview that context management is complete (removes in-progress spinner)
- // IMPORTANT: Must always be sent to dismiss the spinner, even on error
- await this.providerRef
- .deref()
- ?.postMessageToWebview({ type: "condenseTaskContextResponse", text: this.taskId })
- }
- }
- /**
- * Enforce the user-configured provider rate limit.
- *
- * NOTE: This is intentionally treated as expected behavior and is surfaced via
- * the `api_req_rate_limit_wait` say type (not an error).
- */
- private async maybeWaitForProviderRateLimit(retryAttempt: number): Promise<void> {
- const state = await this.providerRef.deref()?.getState()
- const rateLimitSeconds =
- state?.apiConfiguration?.rateLimitSeconds ?? this.apiConfiguration?.rateLimitSeconds ?? 0
- if (rateLimitSeconds <= 0 || !Task.lastGlobalApiRequestTime) {
- return
- }
- const now = performance.now()
- const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
- const rateLimitDelay = Math.ceil(
- Math.min(rateLimitSeconds, Math.max(0, rateLimitSeconds * 1000 - timeSinceLastRequest) / 1000),
- )
- // Only show the countdown UX on the first attempt. Retry flows have their own delay messaging.
- if (rateLimitDelay > 0 && retryAttempt === 0) {
- for (let i = rateLimitDelay; i > 0; i--) {
- // Send structured JSON data for i18n-safe transport
- const delayMessage = JSON.stringify({ seconds: i })
- await this.say("api_req_rate_limit_wait", delayMessage, undefined, true)
- await delay(1000)
- }
- // Finalize the partial message so the UI doesn't keep rendering an in-progress spinner.
- await this.say("api_req_rate_limit_wait", undefined, undefined, false)
- }
- }
- public async *attemptApiRequest(
- retryAttempt: number = 0,
- options: { skipProviderRateLimit?: boolean } = {},
- ): ApiStream {
- const state = await this.providerRef.deref()?.getState()
- const {
- apiConfiguration,
- autoApprovalEnabled,
- requestDelaySeconds,
- mode,
- autoCondenseContext = true,
- autoCondenseContextPercent = 100,
- profileThresholds = {},
- } = state ?? {}
- // Get condensing configuration for automatic triggers.
- const customCondensingPrompt = state?.customSupportPrompts?.CONDENSE
- if (!options.skipProviderRateLimit) {
- await this.maybeWaitForProviderRateLimit(retryAttempt)
- }
- // Update last request time right before making the request so that subsequent
- // requests — even from new subtasks — will honour the provider's rate-limit.
- //
- // NOTE: When recursivelyMakeClineRequests handles rate limiting, it sets the
- // timestamp earlier to include the environment details build. We still set it
- // here for direct callers (tests) and for the case where we didn't rate-limit
- // in the caller.
- Task.lastGlobalApiRequestTime = performance.now()
- const systemPrompt = await this.getSystemPrompt()
- const { contextTokens } = this.getTokenUsage()
- if (contextTokens) {
- const modelInfo = this.api.getModel().info
- const maxTokens = getModelMaxOutputTokens({
- modelId: this.api.getModel().id,
- model: modelInfo,
- settings: this.apiConfiguration,
- })
- const contextWindow = modelInfo.contextWindow
- // Get the current profile ID using the helper method
- const currentProfileId = this.getCurrentProfileId(state)
- // Check if context management will likely run (threshold check)
- // This allows us to show an in-progress indicator to the user
- // We use the centralized willManageContext helper to avoid duplicating threshold logic
- const lastMessage = this.apiConversationHistory[this.apiConversationHistory.length - 1]
- const lastMessageContent = isRooRoleMessage(lastMessage) ? lastMessage.content : undefined
- let lastMessageTokens = 0
- if (lastMessageContent) {
- lastMessageTokens = Array.isArray(lastMessageContent)
- ? await this.api.countTokens(lastMessageContent as Parameters<typeof this.api.countTokens>[0])
- : await this.api.countTokens([{ type: "text", text: lastMessageContent as string }])
- }
- const contextManagementWillRun = willManageContext({
- totalTokens: contextTokens,
- contextWindow,
- maxTokens,
- autoCondenseContext,
- autoCondenseContextPercent,
- profileThresholds,
- currentProfileId,
- lastMessageTokens,
- })
- // Send condenseTaskContextStarted BEFORE manageContext to show in-progress indicator
- // This notification must be sent here (not earlier) because the early check uses stale token count
- // (before user message is added to history), which could incorrectly skip showing the indicator
- if (contextManagementWillRun && autoCondenseContext) {
- await this.providerRef
- .deref()
- ?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId })
- }
- // Build tools for condensing metadata (same tools used for normal API calls)
- // This ensures the condensing API call includes tool definitions for providers that need them
- let contextMgmtTools: import("openai").default.Chat.ChatCompletionTool[] = []
- {
- const provider = this.providerRef.deref()
- if (provider) {
- const toolsResult = await buildNativeToolsArrayWithRestrictions({
- provider,
- cwd: this.cwd,
- mode,
- customModes: state?.customModes,
- experiments: state?.experiments,
- apiConfiguration,
- disabledTools: state?.disabledTools,
- modelInfo,
- includeAllToolsWithRestrictions: false,
- })
- contextMgmtTools = toolsResult.tools
- }
- }
- // Build metadata with tools and taskId for the condensing API call
- const contextMgmtMetadata: ApiHandlerCreateMessageMetadata = {
- mode,
- taskId: this.taskId,
- ...(contextMgmtTools.length > 0
- ? {
- tools: contextMgmtTools,
- tool_choice: "auto",
- parallelToolCalls: true,
- }
- : {}),
- }
- // Only generate environment details when context management will actually run.
- // getEnvironmentDetails(this, true) triggers a recursive workspace listing which
- // adds overhead - avoid this for the common case where context is below threshold.
- const contextMgmtEnvironmentDetails = contextManagementWillRun
- ? await getEnvironmentDetails(this, true)
- : undefined
- // Get files read by Roo for code folding - only when context management will run
- const contextMgmtFilesReadByRoo =
- contextManagementWillRun && autoCondenseContext
- ? await this.getFilesReadByRooSafely("attemptApiRequest")
- : undefined
- try {
- const truncateResult = await manageContext({
- messages: this.apiConversationHistory,
- totalTokens: contextTokens,
- maxTokens,
- contextWindow,
- apiHandler: this.api,
- autoCondenseContext,
- autoCondenseContextPercent,
- systemPrompt,
- taskId: this.taskId,
- customCondensingPrompt,
- profileThresholds,
- currentProfileId,
- metadata: contextMgmtMetadata,
- environmentDetails: contextMgmtEnvironmentDetails,
- filesReadByRoo: contextMgmtFilesReadByRoo,
- cwd: this.cwd,
- rooIgnoreController: this.rooIgnoreController,
- })
- if (truncateResult.messages !== this.apiConversationHistory) {
- await this.overwriteApiConversationHistory(truncateResult.messages as RooMessage[])
- }
- if (truncateResult.error) {
- await this.say("condense_context_error", truncateResult.error)
- }
- if (truncateResult.summary) {
- const { summary, cost, prevContextTokens, newContextTokens = 0, condenseId } = truncateResult
- const contextCondense: ContextCondense = {
- summary,
- cost,
- newContextTokens,
- prevContextTokens,
- condenseId,
- }
- await this.say(
- "condense_context",
- undefined /* text */,
- undefined /* images */,
- false /* partial */,
- undefined /* checkpoint */,
- undefined /* progressStatus */,
- { isNonInteractive: true } /* options */,
- contextCondense,
- )
- } else if (truncateResult.truncationId) {
- // Sliding window truncation occurred (fallback when condensing fails or is disabled)
- const contextTruncation: ContextTruncation = {
- truncationId: truncateResult.truncationId,
- messagesRemoved: truncateResult.messagesRemoved ?? 0,
- prevContextTokens: truncateResult.prevContextTokens,
- newContextTokens: truncateResult.newContextTokensAfterTruncation ?? 0,
- }
- await this.say(
- "sliding_window_truncation",
- undefined /* text */,
- undefined /* images */,
- false /* partial */,
- undefined /* checkpoint */,
- undefined /* progressStatus */,
- { isNonInteractive: true } /* options */,
- undefined /* contextCondense */,
- contextTruncation,
- )
- }
- } finally {
- // Notify webview that context management is complete (sets isCondensing = false)
- // This removes the in-progress spinner and allows the completed result to show
- // IMPORTANT: Must always be sent to dismiss the spinner, even on error
- if (contextManagementWillRun && autoCondenseContext) {
- await this.providerRef
- .deref()
- ?.postMessageToWebview({ type: "condenseTaskContextResponse", text: this.taskId })
- }
- }
- }
- // Get the effective API history by filtering out condensed messages
- // This allows non-destructive condensing where messages are tagged but not deleted,
- // enabling accurate rewind operations while still sending condensed history to the API.
- const effectiveHistory = getEffectiveApiHistory(this.apiConversationHistory)
- const messagesSinceLastSummary = getMessagesSinceLastSummary(effectiveHistory)
- // For API only: merge consecutive user messages (excludes summary messages per
- // mergeConsecutiveApiMessages implementation) without mutating stored history.
- const mergedForApi = mergeConsecutiveApiMessages(messagesSinceLastSummary, { roles: ["user"] })
- const messagesWithoutImages = maybeRemoveImageBlocks(mergedForApi, this.api)
- const cleanConversationHistory = this.buildCleanConversationHistory(messagesWithoutImages)
- // Check auto-approval limits
- const approvalResult = await this.autoApprovalHandler.checkAutoApprovalLimits(
- state,
- this.combineMessages(this.clineMessages.slice(1)),
- async (type, data) => this.ask(type, data),
- )
- if (!approvalResult.shouldProceed) {
- // User did not approve, task should be aborted
- throw new Error("Auto-approval limit reached and user did not approve continuation")
- }
- // Whether we include tools is determined by whether we have any tools to send.
- const modelInfo = this.api.getModel().info
- // Build complete tools array: native tools + dynamic MCP tools
- // When includeAllToolsWithRestrictions is true, returns all tools but provides
- // allowedFunctionNames for providers (like Gemini) that need to see all tool
- // definitions in history while restricting callable tools for the current mode.
- // Only Gemini currently supports this - other providers filter tools normally.
- let allTools: OpenAI.Chat.ChatCompletionTool[] = []
- let allowedFunctionNames: string[] | undefined
- // Gemini requires all tool definitions to be present for history compatibility,
- // but uses allowedFunctionNames to restrict which tools can be called.
- // Other providers (Anthropic, OpenAI, etc.) don't support this feature yet,
- // so they continue to receive only the filtered tools for the current mode.
- const supportsAllowedFunctionNames = apiConfiguration?.apiProvider === "gemini"
- {
- const provider = this.providerRef.deref()
- if (!provider) {
- throw new Error("Provider reference lost during tool building")
- }
- const toolsResult = await buildNativeToolsArrayWithRestrictions({
- provider,
- cwd: this.cwd,
- mode,
- customModes: state?.customModes,
- experiments: state?.experiments,
- apiConfiguration,
- disabledTools: state?.disabledTools,
- modelInfo,
- includeAllToolsWithRestrictions: supportsAllowedFunctionNames,
- })
- allTools = toolsResult.tools
- allowedFunctionNames = toolsResult.allowedFunctionNames
- }
- const shouldIncludeTools = allTools.length > 0
- const metadata: ApiHandlerCreateMessageMetadata = {
- mode: mode,
- taskId: this.taskId,
- suppressPreviousResponseId: this.skipPrevResponseIdOnce,
- // Include tools whenever they are present.
- ...(shouldIncludeTools
- ? {
- tools: allTools,
- tool_choice: "auto",
- parallelToolCalls: true,
- // When mode restricts tools, provide allowedFunctionNames so providers
- // like Gemini can see all tools in history but only call allowed ones
- ...(allowedFunctionNames ? { allowedFunctionNames } : {}),
- }
- : {}),
- }
- // Create an AbortController to allow cancelling the request mid-stream
- this.currentRequestAbortController = new AbortController()
- const abortSignal = this.currentRequestAbortController.signal
- // Reset the flag after using it
- this.skipPrevResponseIdOnce = false
- const stream = this.api.createMessage(systemPrompt, cleanConversationHistory, metadata)
- const iterator = stream[Symbol.asyncIterator]()
- // Set up abort handling - when the signal is aborted, clean up the controller reference
- abortSignal.addEventListener("abort", () => {
- console.log(`[Task#${this.taskId}.${this.instanceId}] AbortSignal triggered for current request`)
- this.currentRequestAbortController = undefined
- })
- try {
- // Awaiting first chunk to see if it will throw an error.
- this.isWaitingForFirstChunk = true
- // Race between the first chunk and the abort signal
- const firstChunkPromise = iterator.next()
- const abortPromise = new Promise<never>((_, reject) => {
- if (abortSignal.aborted) {
- reject(new Error("Request cancelled by user"))
- } else {
- abortSignal.addEventListener("abort", () => {
- reject(new Error("Request cancelled by user"))
- })
- }
- })
- const firstChunk = await Promise.race([firstChunkPromise, abortPromise])
- yield firstChunk.value
- this.isWaitingForFirstChunk = false
- } catch (error) {
- this.isWaitingForFirstChunk = false
- this.currentRequestAbortController = undefined
- const isContextWindowExceededError = checkContextWindowExceededError(error)
- // If it's a context window error and we haven't exceeded max retries for this error type
- if (isContextWindowExceededError && retryAttempt < MAX_CONTEXT_WINDOW_RETRIES) {
- console.warn(
- `[Task#${this.taskId}] Context window exceeded for model ${this.api.getModel().id}. ` +
- `Retry attempt ${retryAttempt + 1}/${MAX_CONTEXT_WINDOW_RETRIES}. ` +
- `Attempting automatic truncation...`,
- )
- await this.handleContextWindowExceededError()
- // Retry the request after handling the context window error
- yield* this.attemptApiRequest(retryAttempt + 1)
- return
- }
- // 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.
- if (autoApprovalEnabled) {
- // Apply shared exponential backoff and countdown UX
- await this.backoffAndAnnounce(retryAttempt, error)
- // CRITICAL: Check if task was aborted during the backoff countdown
- // This prevents infinite loops when users cancel during auto-retry
- // Without this check, the recursive call below would continue even after abort
- if (this.abort) {
- throw new Error(
- `[Task#attemptApiRequest] task ${this.taskId}.${this.instanceId} aborted during retry`,
- )
- }
- // Delegate generator output from the recursive call with
- // incremented retry count.
- yield* this.attemptApiRequest(retryAttempt + 1)
- return
- } else {
- const { response } = await this.ask(
- "api_req_failed",
- error.message ?? JSON.stringify(serializeError(error), null, 2),
- )
- if (response !== "yesButtonClicked") {
- // This will never happen since if noButtonClicked, we will
- // clear current task, aborting this instance.
- throw new Error("API request failed")
- }
- await this.say("api_req_retried")
- // Delegate generator output from the recursive call.
- yield* this.attemptApiRequest()
- return
- }
- }
- // No error, so we can continue to yield all remaining chunks.
- // (Needs to be placed outside of try/catch since it we want caller to
- // handle errors not with api_req_failed as that is reserved for first
- // chunk failures only.)
- // This delegates to another generator or iterable object. In this case,
- // it's saying "yield all remaining values from this iterator". This
- // effectively passes along all subsequent chunks from the original
- // stream.
- yield* iterator
- }
- // Shared exponential backoff for retries (first-chunk and mid-stream)
- private async backoffAndAnnounce(retryAttempt: number, error: any): Promise<void> {
- try {
- const state = await this.providerRef.deref()?.getState()
- const baseDelay = state?.requestDelaySeconds || 5
- let exponentialDelay = Math.min(
- Math.ceil(baseDelay * Math.pow(2, retryAttempt)),
- MAX_EXPONENTIAL_BACKOFF_SECONDS,
- )
- // Respect provider rate limit window
- let rateLimitDelay = 0
- const rateLimit = (state?.apiConfiguration ?? this.apiConfiguration)?.rateLimitSeconds || 0
- if (Task.lastGlobalApiRequestTime && rateLimit > 0) {
- const elapsed = performance.now() - Task.lastGlobalApiRequestTime
- rateLimitDelay = Math.ceil(Math.min(rateLimit, Math.max(0, rateLimit * 1000 - elapsed) / 1000))
- }
- // Prefer RetryInfo on 429 if present
- if (error?.status === 429) {
- const retryInfo = error?.errorDetails?.find(
- (d: any) => d["@type"] === "type.googleapis.com/google.rpc.RetryInfo",
- )
- const match = retryInfo?.retryDelay?.match?.(/^(\d+)s$/)
- if (match) {
- exponentialDelay = Number(match[1]) + 1
- }
- }
- const finalDelay = Math.max(exponentialDelay, rateLimitDelay)
- if (finalDelay <= 0) {
- return
- }
- // Build header text; fall back to error message if none provided
- let headerText
- if (error.status) {
- // Include both status code (for ChatRow parsing) and detailed message (for error details)
- // Format: "<status>\n<message>" allows ChatRow to extract status via parseInt(text.substring(0,3))
- // while preserving the full error message in errorDetails for debugging
- const errorMessage = error?.message || "Unknown error"
- headerText = `${error.status}\n${errorMessage}`
- } else if (error?.message) {
- headerText = error.message
- } else {
- headerText = "Unknown error"
- }
- headerText = headerText ? `${headerText}\n` : ""
- // Show countdown timer with exponential backoff
- for (let i = finalDelay; i > 0; i--) {
- // Check abort flag during countdown to allow early exit
- if (this.abort) {
- throw new Error(`[Task#${this.taskId}] Aborted during retry countdown`)
- }
- await this.say("api_req_retry_delayed", `${headerText}<retry_timer>${i}</retry_timer>`, undefined, true)
- await delay(1000)
- }
- await this.say("api_req_retry_delayed", headerText, undefined, false)
- } catch (err) {
- console.error("Exponential backoff failed:", err)
- }
- }
- // Checkpoints
- public async checkpointSave(force: boolean = false, suppressMessage: boolean = false) {
- return checkpointSave(this, force, suppressMessage)
- }
- /**
- * Prepares conversation history for the API request by sanitizing stored
- * RooMessage items into valid AI SDK ModelMessage format.
- *
- * Condense/truncation filtering is handled upstream by getEffectiveApiHistory.
- * This method:
- *
- * - Removes RooReasoningMessage items (standalone encrypted reasoning with no `role`)
- * - Converts custom content blocks in assistant messages to valid AI SDK parts:
- * - `thinking` (Anthropic) → `reasoning` part with signature in providerOptions
- * - `redacted_thinking` (Anthropic) → stripped (no AI SDK equivalent)
- * - `thoughtSignature` (Gemini) → extracted and attached to first tool-call providerOptions
- * - `reasoning` with `encrypted_content` but no `text` → stripped (invalid reasoning part)
- * - Carries `reasoning_details` (OpenRouter) through to providerOptions
- * - Strips all reasoning when the provider does not support it
- */
- private buildCleanConversationHistory(messages: RooMessage[]): RooMessage[] {
- const preserveReasoning = this.api.getModel().info.preserveReasoning === true || this.api.isAiSdkProvider()
- return messages
- .filter((msg) => {
- // Always remove standalone RooReasoningMessage items (no `role` field → invalid ModelMessage)
- if (isRooReasoningMessage(msg)) {
- return false
- }
- return true
- })
- .map((msg) => {
- if (!isRooAssistantMessage(msg) || !Array.isArray(msg.content)) {
- return msg
- }
- // Detect native AI SDK format: content parts already have providerOptions
- // (stored directly from result.response.messages). These don't need legacy sanitization.
- const isNativeFormat = (msg.content as Array<{ providerOptions?: unknown }>).some(
- (p) => p.providerOptions,
- )
- if (isNativeFormat) {
- // Native format: only strip reasoning if the provider doesn't support it
- if (!preserveReasoning) {
- const filtered = (msg.content as Array<{ type: string }>).filter((p) => p.type !== "reasoning")
- return {
- ...msg,
- content: filtered.length > 0 ? filtered : [{ type: "text" as const, text: "" }],
- } as unknown as RooMessage
- }
- // Pass through unchanged — already in valid AI SDK format
- return msg
- }
- // Legacy path: sanitize old-format messages with custom block types
- // (thinking, redacted_thinking, thoughtSignature)
- // Extract thoughtSignature block (Gemini 3) before filtering
- let thoughtSignature: string | undefined
- for (const part of msg.content) {
- const partAny = part as unknown as { type?: string; thoughtSignature?: string }
- if (partAny.type === "thoughtSignature" && partAny.thoughtSignature) {
- thoughtSignature = partAny.thoughtSignature
- }
- }
- const sanitized: Array<{ type: string; [key: string]: unknown }> = []
- let appliedThoughtSignature = false
- for (const part of msg.content) {
- const partType = (part as { type: string }).type
- if (partType === "thinking") {
- // Anthropic extended thinking → AI SDK reasoning part
- if (!preserveReasoning) continue
- const thinkingPart = part as unknown as { thinking?: string; signature?: string }
- if (typeof thinkingPart.thinking === "string" && thinkingPart.thinking.length > 0) {
- const reasoningPart: Record<string, unknown> = {
- type: "reasoning",
- text: thinkingPart.thinking,
- }
- if (thinkingPart.signature) {
- reasoningPart.providerOptions = {
- anthropic: { signature: thinkingPart.signature },
- bedrock: { signature: thinkingPart.signature },
- }
- }
- sanitized.push(reasoningPart as (typeof sanitized)[number])
- }
- continue
- }
- if (partType === "redacted_thinking") {
- // No AI SDK equivalent — strip
- continue
- }
- if (partType === "thoughtSignature") {
- // Extracted above, will be attached to first tool-call — strip block
- continue
- }
- if (partType === "reasoning") {
- if (!preserveReasoning) continue
- const reasoningPart = part as unknown as { text?: string; encrypted_content?: string }
- // Only valid if it has a `text` field (AI SDK schema requires it)
- if (typeof reasoningPart.text === "string" && reasoningPart.text.length > 0) {
- sanitized.push(part as (typeof sanitized)[number])
- }
- // Blocks with encrypted_content but no text are invalid → skip
- continue
- }
- if (partType === "tool-call" && thoughtSignature && !appliedThoughtSignature) {
- // Attach Gemini thoughtSignature to the first tool-call
- const toolCall = { ...(part as object) } as Record<string, unknown>
- toolCall.providerOptions = {
- ...((toolCall.providerOptions as Record<string, unknown>) ?? {}),
- google: { thoughtSignature },
- vertex: { thoughtSignature },
- }
- sanitized.push(toolCall as (typeof sanitized)[number])
- appliedThoughtSignature = true
- continue
- }
- // text, tool-call, tool-result, file — pass through
- sanitized.push(part as (typeof sanitized)[number])
- }
- const content = sanitized.length > 0 ? sanitized : [{ type: "text" as const, text: "" }]
- // Carry reasoning_details through to providerOptions for OpenRouter round-tripping
- const rawReasoningDetails = (msg as unknown as { reasoning_details?: Record<string, unknown>[] })
- .reasoning_details
- const validReasoningDetails = rawReasoningDetails?.filter((detail) => {
- switch (detail.type) {
- case "reasoning.encrypted":
- return typeof detail.data === "string" && detail.data.length > 0
- case "reasoning.text":
- return typeof detail.text === "string"
- case "reasoning.summary":
- return typeof detail.summary === "string"
- default:
- return false
- }
- })
- const result: Record<string, unknown> = {
- ...msg,
- content,
- }
- if (validReasoningDetails && validReasoningDetails.length > 0) {
- result.providerOptions = {
- ...((msg as unknown as { providerOptions?: Record<string, unknown> }).providerOptions ?? {}),
- openrouter: { reasoning_details: validReasoningDetails },
- }
- }
- return result as unknown as RooMessage
- })
- }
- public async checkpointRestore(options: CheckpointRestoreOptions) {
- return checkpointRestore(this, options)
- }
- public async checkpointDiff(options: CheckpointDiffOptions) {
- return checkpointDiff(this, options)
- }
- // Metrics
- public combineMessages(messages: ClineMessage[]) {
- return combineApiRequests(combineCommandSequences(messages))
- }
- public getTokenUsage(): TokenUsage {
- return getApiMetrics(this.combineMessages(this.clineMessages.slice(1)))
- }
- public recordToolUsage(toolName: ToolName) {
- if (!this.toolUsage[toolName]) {
- this.toolUsage[toolName] = { attempts: 0, failures: 0 }
- }
- this.toolUsage[toolName].attempts++
- }
- public recordToolError(toolName: ToolName, error?: string) {
- if (!this.toolUsage[toolName]) {
- this.toolUsage[toolName] = { attempts: 0, failures: 0 }
- }
- this.toolUsage[toolName].failures++
- if (error) {
- this.emit(RooCodeEventName.TaskToolFailed, this.taskId, toolName, error)
- }
- }
- // Getters
- public get taskStatus(): TaskStatus {
- if (this.interactiveAsk) {
- return TaskStatus.Interactive
- }
- if (this.resumableAsk) {
- return TaskStatus.Resumable
- }
- if (this.idleAsk) {
- return TaskStatus.Idle
- }
- return TaskStatus.Running
- }
- public get taskAsk(): ClineMessage | undefined {
- return this.idleAsk || this.resumableAsk || this.interactiveAsk
- }
- public get queuedMessages(): QueuedMessage[] {
- return this.messageQueueService.messages
- }
- public get tokenUsage(): TokenUsage | undefined {
- if (this.tokenUsageSnapshot && this.tokenUsageSnapshotAt) {
- return this.tokenUsageSnapshot
- }
- this.tokenUsageSnapshot = this.getTokenUsage()
- this.tokenUsageSnapshotAt = this.clineMessages.at(-1)?.ts
- return this.tokenUsageSnapshot
- }
- public get cwd() {
- return this.workspacePath
- }
- /**
- * Provides convenient access to high-level message operations.
- * Uses lazy initialization - the MessageManager is only created when first accessed.
- * Subsequent accesses return the same cached instance.
- *
- * ## Important: Single Coordination Point
- *
- * **All MessageManager operations must go through this getter** rather than
- * instantiating `new MessageManager(task)` directly. This ensures:
- * - A single shared instance for consistent behavior
- * - Centralized coordination of all rewind/message operations
- * - Ability to add internal state or instrumentation in the future
- *
- * @example
- * ```typescript
- * // Correct: Use the getter
- * await task.messageManager.rewindToTimestamp(ts)
- *
- * // Incorrect: Do NOT create new instances directly
- * // const manager = new MessageManager(task) // Don't do this!
- * ```
- */
- get messageManager(): MessageManager {
- if (!this._messageManager) {
- this._messageManager = new MessageManager(this)
- }
- return this._messageManager
- }
- /**
- * Process any queued messages by dequeuing and submitting them.
- * This ensures that queued user messages are sent when appropriate,
- * preventing them from getting stuck in the queue.
- *
- * @param context - Context string for logging (e.g., the calling tool name)
- */
- public processQueuedMessages(): void {
- try {
- if (!this.messageQueueService.isEmpty()) {
- const queued = this.messageQueueService.dequeueMessage()
- if (queued) {
- setTimeout(() => {
- this.submitUserMessage(queued.text, queued.images).catch((err) =>
- console.error(`[Task] Failed to submit queued message:`, err),
- )
- }, 0)
- }
- }
- } catch (e) {
- console.error(`[Task] Queue processing error:`, e)
- }
- }
- }
|