| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351 |
- import * as path from "path"
- import * as vscode from "vscode"
- import os from "os"
- import crypto from "crypto"
- import EventEmitter from "events"
- import { AskIgnoredError } from "./AskIgnoredError"
- 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 ToolProtocol,
- RooCodeEventName,
- TelemetryEventName,
- TaskStatus,
- TodoItem,
- getApiProtocol,
- getModelId,
- isIdleAsk,
- isInteractiveAsk,
- isResumableAsk,
- isNativeProtocol,
- QueuedMessage,
- DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
- DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
- MAX_CHECKPOINT_TIMEOUT_SECONDS,
- MIN_CHECKPOINT_TIMEOUT_SECONDS,
- TOOL_PROTOCOL,
- ConsecutiveMistakeError,
- } from "@roo-code/types"
- import { TelemetryService } from "@roo-code/telemetry"
- import { CloudService, BridgeOrchestrator } from "@roo-code/cloud"
- import { resolveToolProtocol, detectToolProtocolFromHistory } from "../../utils/resolveToolProtocol"
- // api
- import { ApiHandler, ApiHandlerCreateMessageMetadata, buildApiHandler } from "../../api"
- 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 { ClineApiReqCancelReason, ClineApiReqInfo } from "../../shared/ExtensionMessage"
- import { getApiMetrics, hasTokenUsageChanged, hasToolUsageChanged } from "../../shared/getApiMetrics"
- import { ClineAskResponse } from "../../shared/WebviewMessage"
- import { defaultModeSlug, getModeBySlug, getGroupName } from "../../shared/modes"
- import { DiffStrategy, type ToolUse, type ToolParamName, toolParamNames } from "../../shared/tools"
- import { EXPERIMENT_IDS, experiments } from "../../shared/experiments"
- import { getModelMaxOutputTokens } from "../../shared/api"
- // services
- import { UrlContentFetcher } from "../../services/browser/UrlContentFetcher"
- import { BrowserSession } from "../../services/browser/BrowserSession"
- 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"
- // utils
- import { calculateApiCostAnthropic, calculateApiCostOpenAI } from "../../shared/cost"
- import { getWorkspacePath } from "../../utils/path"
- // prompts
- import { formatResponse } from "../prompts/responses"
- import { SYSTEM_PROMPT } from "../prompts/system"
- import { buildNativeToolsArray } 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 { AssistantMessageParser } from "../assistant-message/AssistantMessageParser"
- 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 { MultiFileSearchReplaceDiffStrategy } from "../diff/strategies/multi-file-search-replace"
- import {
- type ApiMessage,
- readApiMessages,
- saveApiMessages,
- readTaskMessages,
- saveTaskMessages,
- taskMetadata,
- } 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"
- 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
- enableDiff?: boolean
- enableCheckpoints?: boolean
- checkpointTimeout?: number
- enableBridge?: boolean
- fuzzyMatchThreshold?: number
- 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
- /**
- * The tool protocol locked to this task. Once set, the task will continue
- * using this protocol even if user settings change.
- *
- * ## Why This Matters
- * When NTC (Native Tool Calling) is enabled, XML parsing does NOT occur.
- * If a task previously used XML tools, resuming it with NTC enabled would
- * break because the tool calls in the history would not be parseable.
- *
- * ## Lifecycle
- *
- * ### For new tasks:
- * 1. Set immediately in constructor via `resolveToolProtocol()`
- * 2. Locked for the lifetime of the task
- *
- * ### For history items:
- * 1. If `historyItem.toolProtocol` exists, use it
- * 2. Otherwise, detect from API history via `detectToolProtocolFromHistory()`
- * 3. If no tools in history, use `resolveToolProtocol()` from current settings
- *
- * @private
- */
- private _taskToolProtocol: ToolProtocol | 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>
- 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
- urlContentFetcher: UrlContentFetcher
- terminalProcess?: RooTerminalProcess
- // Computer User
- browserSession: BrowserSession
- // Editing
- diffViewProvider: DiffViewProvider
- diffStrategy?: DiffStrategy
- diffEnabled: boolean = false
- fuzzyMatchThreshold: number
- didEditFile: boolean = false
- // LLM Messages & Chat Messages
- apiConversationHistory: ApiMessage[] = []
- 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()
- consecutiveNoToolUseCount: 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: (Anthropic.TextBlockParam | Anthropic.ImageBlockParam | Anthropic.ToolResultBlockParam)[] = []
- userMessageContentReady = false
- didRejectTool = false
- didAlreadyUseTool = false
- didToolFailInCurrentTurn = false
- didCompleteReadingStream = false
- assistantMessageParser?: AssistantMessageParser
- 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,
- enableDiff = false,
- enableCheckpoints = true,
- checkpointTimeout = DEFAULT_CHECKPOINT_TIMEOUT_SECONDS,
- enableBridge = false,
- fuzzyMatchThreshold = 1.0,
- 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 : crypto.randomUUID()
- 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(apiConfiguration)
- this.autoApprovalHandler = new AutoApprovalHandler()
- this.urlContentFetcher = new UrlContentFetcher(provider.context)
- this.browserSession = new BrowserSession(provider.context, (isActive: boolean) => {
- // Add a message to indicate browser session status change
- this.say("browser_session_status", isActive ? "Browser session opened" : "Browser session closed")
- // Broadcast to browser panel
- this.broadcastBrowserSessionUpdate()
- // When a browser session becomes active, automatically open/reveal the Browser Session tab
- if (isActive) {
- try {
- // Lazy-load to avoid circular imports at module load time
- const { BrowserSessionPanelManager } = require("../webview/BrowserSessionPanelManager")
- const providerRef = this.providerRef.deref()
- if (providerRef) {
- BrowserSessionPanelManager.getInstance(providerRef)
- .show()
- .catch(() => {})
- }
- } catch (err) {
- console.error("[Task] Failed to auto-open Browser Session panel:", err)
- }
- }
- })
- this.diffEnabled = enableDiff
- this.fuzzyMatchThreshold = fuzzyMatchThreshold
- 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 when it's created.
- // For history items, use the stored mode; for new tasks, we'll set it
- // after getting state.
- if (historyItem) {
- this._taskMode = historyItem.mode || defaultModeSlug
- this.taskModeReady = Promise.resolve()
- TelemetryService.instance.captureTaskRestarted(this.taskId)
- // For history items, use the persisted tool protocol if available.
- // If not available (old tasks), it will be detected in resumeTaskFromHistory.
- this._taskToolProtocol = historyItem.toolProtocol
- } else {
- // For new tasks, don't set the mode yet - wait for async initialization.
- this._taskMode = undefined
- this.taskModeReady = this.initializeTaskMode(provider)
- TelemetryService.instance.captureTaskCreated(this.taskId)
- // For new tasks, resolve and lock the tool protocol immediately.
- // This ensures the task will continue using this protocol even if
- // user settings change.
- const modelInfo = this.api.getModel().info
- this._taskToolProtocol = resolveToolProtocol(this.apiConfiguration, modelInfo)
- }
- // Initialize the assistant message parser based on the locked tool protocol.
- // For native protocol, tool calls come as tool_call chunks, not XML.
- // For history items without a persisted protocol, we default to XML parser
- // and will update it in resumeTaskFromHistory after detection.
- const effectiveProtocol = this._taskToolProtocol || "xml"
- this.assistantMessageParser = effectiveProtocol !== "native" ? new AssistantMessageParser() : undefined
- this.messageQueueService = new MessageQueueService()
- this.messageQueueStateChangedHandler = () => {
- this.emit(RooCodeEventName.TaskUserMessage, this.taskId)
- this.providerRef.deref()?.postStateToWebview()
- }
- this.messageQueueService.on("stateChanged", this.messageQueueStateChangedHandler)
- // Listen for provider profile changes to update parser state
- this.setupProviderProfileChangeListener(provider)
- // Only set up diff strategy if diff is enabled.
- if (this.diffEnabled) {
- // Default to old strategy, will be updated if experiment is enabled.
- this.diffStrategy = new MultiSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
- // Check experiment asynchronously and update strategy if needed.
- provider.getState().then((state) => {
- const isMultiFileApplyDiffEnabled = experiments.isEnabled(
- state.experiments ?? {},
- EXPERIMENT_IDS.MULTI_FILE_APPLY_DIFF,
- )
- if (isMultiFileApplyDiffEnabled) {
- this.diffStrategy = new MultiFileSearchReplaceDiffStrategy(this.fuzzyMatchThreshold)
- }
- })
- }
- 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) {
- if (task || images) {
- this.startTask(task, images)
- } else if (historyItem) {
- this.resumeTaskFromHistory()
- } else {
- throw new Error("Either historyItem or task/images must be provided")
- }
- }
- }
- /**
- * 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)
- }
- }
- /**
- * Sets up a listener for provider profile changes to automatically update the parser state.
- * This ensures the XML/native protocol parser stays synchronized with the current model.
- *
- * @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
- }
- 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<ApiMessage[]> {
- return readApiMessages({ taskId: this.taskId, globalStoragePath: this.globalStoragePath })
- }
- private async addToApiConversationHistory(message: Anthropic.MessageParam, reasoning?: string) {
- // Capture the encrypted_content / thought signatures from the provider (e.g., OpenAI Responses API, Google GenAI) if present.
- // We only persist data reported by the current response body.
- const handler = this.api as ApiHandler & {
- getResponseId?: () => string | undefined
- getEncryptedContent?: () => { encrypted_content: string; id?: string } | undefined
- getThoughtSignature?: () => string | undefined
- getSummary?: () => any[] | undefined
- getReasoningDetails?: () => any[] | undefined
- }
- if (message.role === "assistant") {
- const responseId = handler.getResponseId?.()
- const reasoningData = handler.getEncryptedContent?.()
- const thoughtSignature = handler.getThoughtSignature?.()
- const reasoningSummary = handler.getSummary?.()
- const reasoningDetails = handler.getReasoningDetails?.()
- // Start from the original assistant message
- const messageWithTs: any = {
- ...message,
- ...(responseId ? { id: responseId } : {}),
- ts: Date.now(),
- }
- // Store reasoning_details array if present (for models like Gemini 3)
- if (reasoningDetails) {
- messageWithTs.reasoning_details = reasoningDetails
- }
- // Store reasoning: Anthropic thinking (with signature), plain text (most providers), or encrypted (OpenAI Native)
- // Skip if reasoning_details already contains the reasoning (to avoid duplication)
- if (reasoning && thoughtSignature && !reasoningDetails) {
- // Anthropic provider with extended thinking: Store as proper `thinking` block
- // This format passes through anthropic-filter.ts and is properly round-tripped
- // for interleaved thinking with tool use (required by Anthropic API)
- const thinkingBlock = {
- type: "thinking",
- thinking: reasoning,
- signature: thoughtSignature,
- }
- if (typeof messageWithTs.content === "string") {
- messageWithTs.content = [
- thinkingBlock,
- { type: "text", text: messageWithTs.content } satisfies Anthropic.Messages.TextBlockParam,
- ]
- } else if (Array.isArray(messageWithTs.content)) {
- messageWithTs.content = [thinkingBlock, ...messageWithTs.content]
- } else if (!messageWithTs.content) {
- messageWithTs.content = [thinkingBlock]
- }
- } else if (reasoning && !reasoningDetails) {
- // Other providers (non-Anthropic): Store as generic reasoning block
- const reasoningBlock = {
- type: "reasoning",
- text: reasoning,
- summary: reasoningSummary ?? ([] as any[]),
- }
- if (typeof messageWithTs.content === "string") {
- messageWithTs.content = [
- reasoningBlock,
- { type: "text", text: messageWithTs.content } satisfies Anthropic.Messages.TextBlockParam,
- ]
- } else if (Array.isArray(messageWithTs.content)) {
- messageWithTs.content = [reasoningBlock, ...messageWithTs.content]
- } else if (!messageWithTs.content) {
- messageWithTs.content = [reasoningBlock]
- }
- } else if (reasoningData?.encrypted_content) {
- // OpenAI Native encrypted reasoning
- 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 } satisfies Anthropic.Messages.TextBlockParam,
- ]
- } else if (Array.isArray(messageWithTs.content)) {
- messageWithTs.content = [reasoningBlock, ...messageWithTs.content]
- } else if (!messageWithTs.content) {
- messageWithTs.content = [reasoningBlock]
- }
- }
- // If we have a thought signature WITHOUT reasoning text (edge case),
- // append it as a dedicated content block for non-Anthropic providers (e.g., Gemini).
- // Note: For Anthropic, the signature is already included in the thinking block above.
- if (thoughtSignature && !reasoning) {
- const thoughtSignatureBlock = {
- type: "thoughtSignature",
- thoughtSignature,
- }
- if (typeof messageWithTs.content === "string") {
- messageWithTs.content = [
- { type: "text", text: messageWithTs.content } satisfies Anthropic.Messages.TextBlockParam,
- thoughtSignatureBlock,
- ]
- } else if (Array.isArray(messageWithTs.content)) {
- messageWithTs.content = [...messageWithTs.content, thoughtSignatureBlock]
- } else if (!messageWithTs.content) {
- messageWithTs.content = [thoughtSignatureBlock]
- }
- }
- this.apiConversationHistory.push(messageWithTs)
- } else {
- // For user messages, validate and fix tool_result IDs against the previous assistant message
- const validatedMessage = validateAndFixToolResultIds(message, this.apiConversationHistory)
- const messageWithTs = { ...validatedMessage, ts: Date.now() }
- this.apiConversationHistory.push(messageWithTs)
- }
- await this.saveApiConversationHistory()
- }
- async overwriteApiConversationHistory(newHistory: ApiMessage[]) {
- this.apiConversationHistory = newHistory
- await this.saveApiConversationHistory()
- }
- /**
- * Flush any pending tool results to the API conversation history.
- *
- * This is critical for native tool protocol 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<void> {
- // Only flush if there's actually pending content to save
- if (this.userMessageContent.length === 0) {
- return
- }
- // Save the user message with tool_result blocks
- const userMessage: Anthropic.MessageParam = {
- role: "user",
- content: this.userMessageContent,
- }
- // Validate and fix tool_result IDs against the previous assistant message
- const validatedMessage = validateAndFixToolResultIds(userMessage, this.apiConversationHistory)
- const userMessageWithTs = { ...validatedMessage, ts: Date.now() }
- this.apiConversationHistory.push(userMessageWithTs as ApiMessage)
- await this.saveApiConversationHistory()
- // Clear the pending content since it's now saved
- this.userMessageContent = []
- }
- private async saveApiConversationHistory() {
- try {
- await saveApiMessages({
- messages: this.apiConversationHistory,
- taskId: this.taskId,
- globalStoragePath: this.globalStoragePath,
- })
- } catch (error) {
- // In the off chance this fails, we don't want to stop the task.
- console.error("Failed to save API conversation history:", error)
- }
- }
- // 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()
- await provider?.postStateToWebview()
- 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() {
- try {
- await saveTaskMessages({
- messages: this.clineMessages,
- taskId: this.taskId,
- globalStoragePath: this.globalStoragePath,
- })
- 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.
- initialStatus: this.initialStatus,
- toolProtocol: this._taskToolProtocol, // Persist the locked tool protocol.
- })
- // 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)
- } catch (error) {
- console.error("Failed to save Roo messages:", error)
- }
- }
- 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
- console.log(`Task#ask: new partial ask -> ${type} @ ${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
- console.log(`Task#ask: updating previous partial ask -> ${type} @ ${askTs}`)
- 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()
- console.log(`Task#ask: new complete ask -> ${type} @ ${askTs}`)
- 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()
- console.log(`Task#ask: new complete ask -> ${type} @ ${askTs}`)
- 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 (isBlocking) {
- console.log(`Task#ask will block -> type: ${type}`)
- }
- if (isStatusMutable) {
- console.log(`Task#ask: status is mutable -> type: ${type}`)
- 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) {
- console.log(`Task#ask: will process message queue -> type: ${type}`)
- 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 === "browser_action_launch" ||
- 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(() => this.askResponse !== undefined || this.lastMessageTs !== askTs, { 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.
- console.log("Task#ask: current ask promise was ignored")
- 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)
- }
- /**
- * Updates the API configuration but preserves the locked tool protocol.
- * The task's tool protocol is locked at creation time and should NOT change
- * even when switching between models/profiles with different settings.
- *
- * @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(newApiConfiguration)
- // IMPORTANT: Do NOT change the parser based on the new configuration!
- // The task's tool protocol is locked at creation time and must remain
- // consistent throughout the task's lifetime to ensure history can be
- // properly resumed.
- }
- 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)
- provider.postMessageToWebview({ type: "invoke", invoke: "sendMessage", 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()
- }
- }
- public async condenseContext(): Promise<void> {
- const systemPrompt = await this.getSystemPrompt()
- // Get condensing configuration
- const state = await this.providerRef.deref()?.getState()
- // These properties may not exist in the state type yet, but are used for condensing configuration
- const customCondensingPrompt = state?.customCondensingPrompt
- const condensingApiConfigId = state?.condensingApiConfigId
- const listApiConfigMeta = state?.listApiConfigMeta
- // Determine API handler to use
- let condensingApiHandler: ApiHandler | undefined
- if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
- // Find matching config by ID
- const matchingConfig = listApiConfigMeta.find((config) => config.id === condensingApiConfigId)
- if (matchingConfig) {
- const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
- id: condensingApiConfigId,
- })
- // Ensure profile and apiProvider exist before trying to build handler
- if (profile && profile.apiProvider) {
- condensingApiHandler = buildApiHandler(profile)
- }
- }
- }
- const { contextTokens: prevContextTokens } = this.getTokenUsage()
- // Determine if we're using native tool protocol for proper message handling
- // Use the task's locked protocol, NOT the current settings (fallback to xml if not set)
- const useNativeTools = isNativeProtocol(this._taskToolProtocol ?? "xml")
- const {
- messages,
- summary,
- cost,
- newContextTokens = 0,
- error,
- condenseId,
- } = await summarizeConversation(
- this.apiConversationHistory,
- this.api, // Main API handler (fallback)
- systemPrompt, // Default summarization prompt (fallback)
- this.taskId,
- prevContextTokens,
- false, // manual trigger
- customCondensingPrompt, // User's custom prompt
- condensingApiHandler, // Specific handler for condensing
- useNativeTools, // Pass native tools flag for proper message handling
- )
- if (error) {
- this.say(
- "condense_context_error",
- error,
- undefined /* images */,
- false /* partial */,
- undefined /* checkpoint */,
- undefined /* progressStatus */,
- { isNonInteractive: true } /* options */,
- )
- return
- }
- await this.overwriteApiConversationHistory(messages)
- 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,
- })
- }
- // Broadcast browser session updates to panel when browser-related messages are added
- if (type === "browser_action" || type === "browser_action_result" || type === "browser_session_status") {
- this.broadcastBrowserSessionUpdate()
- }
- }
- 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...`,
- )
- // Use the task's locked protocol, NOT the current settings (fallback to xml if not set)
- return formatResponse.toolError(
- formatResponse.missingToolParameterError(paramName, this._taskToolProtocol ?? "xml"),
- )
- }
- // Lifecycle
- // Start / Resume / Abort / Dispose
- private async startTask(task?: string, images?: string[]): Promise<void> {
- 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()?.postStateToWebview()
- await this.say("text", task, images)
- this.isInitialized = true
- let imageBlocks: Anthropic.ImageBlockParam[] = formatResponse.imageBlocks(images)
- // Task starting
- await this.initiateTaskLoop([
- {
- type: "text",
- text: `<task>\n${task}\n</task>`,
- },
- ...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
- })
- }
- 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()
- // If we don't have a persisted tool protocol (old tasks before this feature),
- // detect it from the API history. This ensures tasks that previously used
- // XML tools will continue using XML even if NTC is now enabled.
- if (!this._taskToolProtocol) {
- const detectedProtocol = detectToolProtocolFromHistory(this.apiConversationHistory)
- if (detectedProtocol) {
- // Found tool calls in history - lock to that protocol
- this._taskToolProtocol = detectedProtocol
- } else {
- // No tool calls in history yet - use current settings
- const modelInfo = this.api.getModel().info
- this._taskToolProtocol = resolveToolProtocol(this.apiConfiguration, modelInfo)
- }
- // Update parser state to match the detected/resolved protocol
- const shouldUseXmlParser = this._taskToolProtocol === "xml"
- if (shouldUseXmlParser && !this.assistantMessageParser) {
- this.assistantMessageParser = new AssistantMessageParser()
- } else if (!shouldUseXmlParser && this.assistantMessageParser) {
- this.assistantMessageParser.reset()
- this.assistantMessageParser = undefined
- }
- } else {
- }
- 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.
- let existingApiConversationHistory: ApiMessage[] = await this.getSavedApiConversationHistory()
- // v2.0 xml tags refactor caveat: since we don't use tools anymore for XML protocol,
- // we need to replace all tool use blocks with a text block since the API disallows
- // conversations with tool uses and no tool schema.
- // For native protocol, we preserve tool_use and tool_result blocks as they're expected by the API.
- // IMPORTANT: Use the task's locked protocol, NOT the current settings!
- const useNative = isNativeProtocol(this._taskToolProtocol)
- // Only convert tool blocks to text for XML protocol
- // For native protocol, the API expects proper tool_use/tool_result structure
- if (!useNative) {
- const conversationWithoutToolBlocks = existingApiConversationHistory.map((message) => {
- if (Array.isArray(message.content)) {
- const newContent = message.content.map((block) => {
- if (block.type === "tool_use") {
- // Format tool invocation based on the task's locked protocol
- const params = block.input as Record<string, any>
- const formattedText = formatToolInvocation(block.name, params, this._taskToolProtocol)
- return {
- type: "text",
- text: formattedText,
- } as Anthropic.Messages.TextBlockParam
- } else if (block.type === "tool_result") {
- // Convert block.content to text block array, removing images
- const contentAsTextBlocks = Array.isArray(block.content)
- ? block.content.filter((item) => item.type === "text")
- : [{ type: "text", text: block.content }]
- const textContent = contentAsTextBlocks.map((item) => item.text).join("\n\n")
- const toolName = findToolName(block.tool_use_id, existingApiConversationHistory)
- return {
- type: "text",
- text: `[${toolName} Result]\n\n${textContent}`,
- } as Anthropic.Messages.TextBlockParam
- }
- return block
- })
- return { ...message, content: newContent }
- }
- return message
- })
- existingApiConversationHistory = conversationWithoutToolBlocks
- }
- // FIXME: remove tool use blocks altogether
- // if the last message is an assistant message, we need to check if there's tool use since every tool use has to have a tool response
- // if there's no tool use and only a text block, then we can just add a user message
- // (note this isn't relevant anymore since we use custom tool prompts instead of tool use blocks, but this is here for legacy purposes in case users resume old tasks)
- // if the last message is a user message, we can need to get the assistant message before it to see if it made tool calls, and if so, fill in the remaining tool responses with 'interrupted'
- let modifiedOldUserContent: Anthropic.Messages.ContentBlockParam[] // either the last message if its user message, or the user message before the last (assistant) message
- let modifiedApiConversationHistory: ApiMessage[] // need to remove the last user message to replace with new modified user message
- if (existingApiConversationHistory.length > 0) {
- const lastMessage = existingApiConversationHistory[existingApiConversationHistory.length - 1]
- if (lastMessage.role === "assistant") {
- const content = Array.isArray(lastMessage.content)
- ? lastMessage.content
- : [{ type: "text", text: lastMessage.content }]
- const hasToolUse = content.some((block) => block.type === "tool_use")
- if (hasToolUse) {
- const toolUseBlocks = content.filter(
- (block) => block.type === "tool_use",
- ) as Anthropic.Messages.ToolUseBlock[]
- const toolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks.map((block) => ({
- type: "tool_result",
- tool_use_id: block.id,
- content: "Task was interrupted before this tool call could be completed.",
- }))
- modifiedApiConversationHistory = [...existingApiConversationHistory] // no changes
- modifiedOldUserContent = [...toolResponses]
- } else {
- modifiedApiConversationHistory = [...existingApiConversationHistory]
- modifiedOldUserContent = []
- }
- } else if (lastMessage.role === "user") {
- const previousAssistantMessage: ApiMessage | undefined =
- existingApiConversationHistory[existingApiConversationHistory.length - 2]
- const existingUserContent: Anthropic.Messages.ContentBlockParam[] = Array.isArray(lastMessage.content)
- ? lastMessage.content
- : [{ type: "text", text: lastMessage.content }]
- if (previousAssistantMessage && previousAssistantMessage.role === "assistant") {
- const assistantContent = Array.isArray(previousAssistantMessage.content)
- ? previousAssistantMessage.content
- : [{ type: "text", text: previousAssistantMessage.content }]
- const toolUseBlocks = assistantContent.filter(
- (block) => block.type === "tool_use",
- ) as Anthropic.Messages.ToolUseBlock[]
- if (toolUseBlocks.length > 0) {
- const existingToolResults = existingUserContent.filter(
- (block) => block.type === "tool_result",
- ) as Anthropic.ToolResultBlockParam[]
- const missingToolResponses: Anthropic.ToolResultBlockParam[] = toolUseBlocks
- .filter(
- (toolUse) => !existingToolResults.some((result) => result.tool_use_id === toolUse.id),
- )
- .map((toolUse) => ({
- type: "tool_result",
- tool_use_id: toolUse.id,
- content: "Task was interrupted before this tool call could be completed.",
- }))
- modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1) // removes the last user message
- modifiedOldUserContent = [...existingUserContent, ...missingToolResponses]
- } else {
- modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
- modifiedOldUserContent = [...existingUserContent]
- }
- } else {
- modifiedApiConversationHistory = existingApiConversationHistory.slice(0, -1)
- modifiedOldUserContent = [...existingUserContent]
- }
- } else {
- throw new Error("Unexpected: Last message is not a user or assistant message")
- }
- } else {
- throw new Error("Unexpected: No existing API conversation history")
- }
- let newUserContent: Anthropic.Messages.ContentBlockParam[] = [...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: `\n\nNew instructions for task continuation:\n<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
- // 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)
- }
- try {
- this.urlContentFetcher.closeBrowser()
- } catch (error) {
- console.error("Error closing URL content fetcher browser:", error)
- }
- try {
- this.browserSession.closeBrowser()
- } catch (error) {
- console.error("Error closing browser session:", error)
- }
- // Also close the Browser Session panel when the task is disposed
- try {
- const provider = this.providerRef.deref()
- if (provider) {
- const { BrowserSessionPanelManager } = require("../webview/BrowserSessionPanelManager")
- BrowserSessionPanelManager.getInstance(provider).dispose()
- }
- } catch (error) {
- console.error("Error closing browser session panel:", 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--) {
- if (this.apiConversationHistory[i].role === "user") {
- lastUserMsgIndex = i
- break
- }
- }
- if (lastUserMsgIndex >= 0) {
- const lastUserMsg = this.apiConversationHistory[lastUserMsgIndex]
- if (Array.isArray(lastUserMsg.content)) {
- // Remove any existing environment_details blocks before adding fresh ones
- const contentWithoutEnvDetails = lastUserMsg.content.filter(
- (block: Anthropic.Messages.ContentBlockParam) => {
- 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: Anthropic.Messages.ContentBlockParam[]): 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 {
- // Use the task's locked protocol, NOT the current settings (fallback to xml if not set)
- nextUserContent = [{ type: "text", text: formatResponse.noToolsUsed(this._taskToolProtocol ?? "xml") }]
- }
- }
- }
- public async recursivelyMakeClineRequests(
- userContent: Anthropic.Messages.ContentBlockParam[],
- includeFileDetails: boolean = false,
- ): Promise<boolean> {
- interface StackItem {
- userContent: Anthropic.Messages.ContentBlockParam[]
- 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 apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, modelId)
- await this.say(
- "api_req_started",
- JSON.stringify({
- apiProtocol,
- }),
- )
- const {
- showRooIgnoredFiles = false,
- includeDiagnosticMessages = true,
- maxDiagnosticMessages = 50,
- maxReadFileLine = -1,
- } = (await this.providerRef.deref()?.getState()) ?? {}
- const parsedUserContent = await processUserContentMentions({
- userContent: currentUserContent,
- cwd: this.cwd,
- urlContentFetcher: this.urlContentFetcher,
- fileContextTracker: this.fileContextTracker,
- rooIgnoreController: this.rooIgnoreController,
- showRooIgnoredFiles,
- includeDiagnosticMessages,
- maxDiagnosticMessages,
- maxReadFileLine,
- })
- const environmentDetails = await getEnvironmentDetails(this, currentIncludeFileDetails)
- // Remove any existing environment_details blocks before adding fresh ones.
- // This prevents duplicate environment details when resuming tasks with XML tool calls,
- // 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) {
- await this.addToApiConversationHistory({ role: "user", content: finalUserContent })
- 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()?.postStateToWebview()
- 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 apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, 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
- console.log("updating partial message", lastMessage)
- }
- // 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.userMessageContentReady = false
- this.didRejectTool = false
- this.didAlreadyUseTool = 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
- this.assistantMessageParser?.reset()
- 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
- // Use the task's locked protocol instead of resolving fresh.
- // This ensures task resumption works correctly even if NTC settings changed.
- // Fallback to resolving if somehow _taskToolProtocol is not set (should not happen).
- const streamProtocol = resolveToolProtocol(
- this.apiConfiguration,
- streamModelInfo,
- this._taskToolProtocol,
- )
- const shouldUseXmlParser = streamProtocol === "xml"
- // 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()
- let assistantMessage = ""
- let reasoningMessage = ""
- 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) {
- if (event.type === "tool_call_start") {
- // 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)
- // 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)
- }
- }
- }
- 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
- // Use the protocol determined at the start of streaming
- // Don't rely solely on parser existence - parser might exist from previous state
- if (shouldUseXmlParser && this.assistantMessageParser) {
- // XML protocol: Parse raw assistant message chunk into content blocks
- const prevLength = this.assistantMessageContent.length
- this.assistantMessageContent = this.assistantMessageParser.processChunk(chunk.text)
- if (this.assistantMessageContent.length > prevLength) {
- // New content we need to present, reset to
- // false in case previous content set this to true.
- this.userMessageContentReady = false
- }
- // Present content to user.
- presentAssistantMessage(this)
- } else {
- // Native protocol: Text chunks are plain text, not XML tool calls
- // Create or update a text content block directly
- const lastBlock =
- this.assistantMessageContent[this.assistantMessageContent.length - 1]
- if (lastBlock?.type === "text" && lastBlock.partial) {
- // Update existing partial text block
- lastBlock.content = assistantMessage
- } else {
- // Create new text block
- this.assistantMessageContent.push({
- type: "text",
- content: assistantMessage,
- partial: true,
- })
- this.userMessageContentReady = false
- }
- // Present content to user
- presentAssistantMessage(this)
- }
- 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
- }
- }
- // 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)
- }
- }
- }
- // 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 apiProtocol = getApiProtocol(this.apiConfiguration.apiProvider, 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 streamingFailedMessage = this.abort
- ? undefined
- : (error.message ?? JSON.stringify(serializeError(error), null, 2))
- // 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: ${streamingFailedMessage}`,
- )
- // Apply exponential backoff similar to first-chunk errors when auto-resubmit is enabled
- const stateForBackoff = await this.providerRef.deref()?.getState()
- 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.)
- 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))
- // Now that the stream is complete, finalize any remaining partial content blocks (XML protocol only)
- // Use the protocol determined at the start of streaming
- if (shouldUseXmlParser && this.assistantMessageParser) {
- this.assistantMessageParser.finalizeContentBlocks()
- const parsedBlocks = this.assistantMessageParser.getContentBlocks()
- // For XML protocol: Use only parsed blocks (includes both text and tool_use parsed from XML)
- this.assistantMessageContent = parsedBlocks
- }
- // Present any partial blocks that were just completed
- // For XML protocol: includes both text and tool_use blocks parsed from the text stream
- // For native protocol: tool_use blocks were already presented during streaming via
- // tool_call_partial events, but we still need to present them if they exist (e.g., malformed)
- 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)
- }
- // 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()?.postStateToWebview()
- // Reset parser after each complete conversation round (XML protocol only)
- this.assistantMessageParser?.reset()
- // Now add to apiConversationHistory.
- // Need to save assistant responses to file before proceeding to
- // tool use since user can exit at any moment and we wouldn't be
- // able to save the assistant's response.
- // 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) {
- // 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<Anthropic.TextBlockParam | Anthropic.ToolUseBlockParam> = []
- // 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
- 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) {
- assistantContent.push({
- type: "tool_use" as const,
- id: mcpBlock.id,
- name: 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) {
- // 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"),
- // 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_use" as const,
- id: toolCallId,
- name: toolNameForHistory,
- input,
- })
- }
- }
- }
- await this.addToApiConversationHistory(
- { role: "assistant", content: assistantContent },
- reasoningMessage || undefined,
- )
- TelemetryService.instance.captureConversationMessage(this.taskId, "assistant")
- // 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(this._taskToolProtocol ?? "xml"),
- })
- } else {
- // Reset counter when tools are used successfully
- this.consecutiveNoToolUseCount = 0
- }
- // 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], // 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.
- // IMPORTANT: For native tool protocol, 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()
- // Use the task's locked protocol, NOT current settings
- if (isNativeProtocol(this._taskToolProtocol ?? "xml") && this.apiConversationHistory.length > 0) {
- const lastMessage = this.apiConversationHistory[this.apiConversationHistory.length - 1]
- if (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
- // For native protocol, re-add the user message we removed
- // Use the task's locked protocol, NOT current settings
- if (isNativeProtocol(this._taskToolProtocol ?? "xml")) {
- await this.addToApiConversationHistory({
- role: "user",
- content: currentUserContent,
- })
- }
- 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 {
- browserViewportSize,
- mode,
- customModes,
- customModePrompts,
- customInstructions,
- experiments,
- enableMcpServerCreation,
- browserToolEnabled,
- language,
- maxConcurrentFileReads,
- maxReadFileLine,
- apiConfiguration,
- } = state ?? {}
- return await (async () => {
- const provider = this.providerRef.deref()
- if (!provider) {
- throw new Error("Provider not available")
- }
- // Align browser tool enablement with generateSystemPrompt: require model image support,
- // mode to include the browser group, and the user setting to be enabled.
- const modeConfig = getModeBySlug(mode ?? defaultModeSlug, customModes)
- const modeSupportsBrowser = modeConfig?.groups.some((group) => getGroupName(group) === "browser") ?? false
- // Check if model supports browser capability (images)
- const modelInfo = this.api.getModel().info
- const modelSupportsBrowser = (modelInfo as any)?.supportsImages === true
- const canUseBrowserTool = modelSupportsBrowser && modeSupportsBrowser && (browserToolEnabled ?? true)
- // Use the task's locked protocol for system prompt consistency.
- // This ensures the system prompt matches the protocol the task was started with,
- // even if user settings have changed since then.
- const toolProtocol = resolveToolProtocol(
- apiConfiguration ?? this.apiConfiguration,
- modelInfo,
- this._taskToolProtocol,
- )
- return SYSTEM_PROMPT(
- provider.context,
- this.cwd,
- canUseBrowserTool,
- mcpHub,
- this.diffStrategy,
- browserViewportSize ?? "900x600",
- mode ?? defaultModeSlug,
- customModePrompts,
- customModes,
- customInstructions,
- this.diffEnabled,
- experiments,
- enableMcpServerCreation,
- language,
- rooIgnoreInstructions,
- maxReadFileLine !== -1,
- {
- maxConcurrentFileReads: maxConcurrentFileReads ?? 5,
- todoListEnabled: apiConfiguration?.todoListEnabled ?? true,
- browserToolEnabled: browserToolEnabled ?? true,
- useAgentRules:
- vscode.workspace.getConfiguration(Package.name).get<boolean>("useAgentRules") ?? true,
- newTaskRequireTodos: vscode.workspace
- .getConfiguration(Package.name)
- .get<boolean>("newTaskRequireTodos", false),
- toolProtocol,
- isStealthModel: modelInfo?.isStealthModel,
- },
- undefined, // todoList
- this.api.getModel().id,
- )
- })()
- }
- 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 = {} } = 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.`,
- )
- // Determine if we're using native tool protocol for proper message handling
- // Use the task's locked protocol, NOT the current settings
- const useNativeTools = isNativeProtocol(this._taskToolProtocol ?? "xml")
- // Send condenseTaskContextStarted to show in-progress indicator
- await this.providerRef.deref()?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId })
- // 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,
- useNativeTools,
- })
- if (truncateResult.messages !== this.apiConversationHistory) {
- await this.overwriteApiConversationHistory(truncateResult.messages)
- }
- 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,
- )
- }
- // Notify webview that context management is complete (removes in-progress spinner)
- await this.providerRef.deref()?.postMessageToWebview({ type: "condenseTaskContextResponse", text: this.taskId })
- }
- public async *attemptApiRequest(retryAttempt: number = 0): 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?.customCondensingPrompt
- const condensingApiConfigId = state?.condensingApiConfigId
- const listApiConfigMeta = state?.listApiConfigMeta
- // Determine API handler to use for condensing.
- let condensingApiHandler: ApiHandler | undefined
- if (condensingApiConfigId && listApiConfigMeta && Array.isArray(listApiConfigMeta)) {
- // Find matching config by ID
- const matchingConfig = listApiConfigMeta.find((config) => config.id === condensingApiConfigId)
- if (matchingConfig) {
- const profile = await this.providerRef.deref()?.providerSettingsManager.getProfile({
- id: condensingApiConfigId,
- })
- // Ensure profile and apiProvider exist before trying to build handler.
- if (profile && profile.apiProvider) {
- condensingApiHandler = buildApiHandler(profile)
- }
- }
- }
- let rateLimitDelay = 0
- // Use the shared timestamp so that subtasks respect the same rate-limit
- // window as their parent tasks.
- if (Task.lastGlobalApiRequestTime) {
- const now = performance.now()
- const timeSinceLastRequest = now - Task.lastGlobalApiRequestTime
- const rateLimit = apiConfiguration?.rateLimitSeconds || 0
- rateLimitDelay = Math.ceil(Math.min(rateLimit, Math.max(0, rateLimit * 1000 - timeSinceLastRequest) / 1000))
- }
- // Only show rate limiting message if we're not retrying. If retrying, we'll include the delay there.
- if (rateLimitDelay > 0 && retryAttempt === 0) {
- // Show countdown timer
- for (let i = rateLimitDelay; i > 0; i--) {
- const delayMessage = `Rate limiting for ${i} seconds...`
- await this.say("api_req_retry_delayed", delayMessage, undefined, true)
- await delay(1000)
- }
- }
- // Update last request time before making the request so that subsequent
- // requests — even from new subtasks — will honour the provider's rate-limit.
- 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)
- // Determine if we're using native tool protocol for proper message handling
- // Use the task's locked protocol, NOT the current settings
- const useNativeTools = isNativeProtocol(this._taskToolProtocol ?? "xml")
- // 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 = lastMessage?.content
- let lastMessageTokens = 0
- if (lastMessageContent) {
- lastMessageTokens = Array.isArray(lastMessageContent)
- ? await this.api.countTokens(lastMessageContent)
- : 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 })
- }
- const truncateResult = await manageContext({
- messages: this.apiConversationHistory,
- totalTokens: contextTokens,
- maxTokens,
- contextWindow,
- apiHandler: this.api,
- autoCondenseContext,
- autoCondenseContextPercent,
- systemPrompt,
- taskId: this.taskId,
- customCondensingPrompt,
- condensingApiHandler,
- profileThresholds,
- currentProfileId,
- useNativeTools,
- })
- if (truncateResult.messages !== this.apiConversationHistory) {
- await this.overwriteApiConversationHistory(truncateResult.messages)
- }
- if (truncateResult.error) {
- await this.say("condense_context_error", truncateResult.error)
- } else 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,
- )
- }
- // Notify webview that context management is complete (sets isCondensing = false)
- // This removes the in-progress spinner and allows the completed result to show
- 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)
- const messagesWithoutImages = maybeRemoveImageBlocks(messagesSinceLastSummary, this.api)
- const cleanConversationHistory = this.buildCleanConversationHistory(messagesWithoutImages as ApiMessage[])
- // 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")
- }
- // Determine if we should include native tools based on:
- // 1. Task's locked tool protocol is set to NATIVE
- // 2. Model supports native tools
- // CRITICAL: Use the task's locked protocol to ensure tasks that started with XML
- // tools continue using XML even if NTC settings have since changed.
- const modelInfo = this.api.getModel().info
- const taskProtocol = this._taskToolProtocol ?? "xml"
- const shouldIncludeTools = taskProtocol === TOOL_PROTOCOL.NATIVE && (modelInfo.supportsNativeTools ?? false)
- // Build complete tools array: native tools + dynamic MCP tools, filtered by mode restrictions
- let allTools: OpenAI.Chat.ChatCompletionTool[] = []
- if (shouldIncludeTools) {
- const provider = this.providerRef.deref()
- if (!provider) {
- throw new Error("Provider reference lost during tool building")
- }
- allTools = await buildNativeToolsArray({
- provider,
- cwd: this.cwd,
- mode,
- customModes: state?.customModes,
- experiments: state?.experiments,
- apiConfiguration,
- maxReadFileLine: state?.maxReadFileLine ?? -1,
- browserToolEnabled: state?.browserToolEnabled ?? true,
- modelInfo,
- diffEnabled: this.diffEnabled,
- })
- }
- // Parallel tool calls are disabled - feature is on hold
- // Previously resolved from experiments.isEnabled(..., EXPERIMENT_IDS.MULTIPLE_NATIVE_TOOL_CALLS)
- const parallelToolCallsEnabled = false
- const metadata: ApiHandlerCreateMessageMetadata = {
- mode: mode,
- taskId: this.taskId,
- suppressPreviousResponseId: this.skipPrevResponseIdOnce,
- // Include tools and tool protocol when using native protocol and model supports it
- ...(shouldIncludeTools
- ? {
- tools: allTools,
- tool_choice: "auto",
- toolProtocol: taskProtocol,
- parallelToolCalls: parallelToolCallsEnabled,
- }
- : {}),
- }
- // 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
- // The provider accepts reasoning items alongside standard messages; cast to the expected parameter type.
- const stream = this.api.createMessage(
- systemPrompt,
- cleanConversationHistory as unknown as Anthropic.Messages.MessageParam[],
- 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?.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) {
- // This sets the message as just the error code, for which
- // ChatRow knows how to handle and use an i18n'd error string
- // In development, hardcode headerText to an HTTP status code to check it
- headerText = error.status
- } 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)
- }
- private buildCleanConversationHistory(
- messages: ApiMessage[],
- ): Array<
- Anthropic.Messages.MessageParam | { type: "reasoning"; encrypted_content: string; id?: string; summary?: any[] }
- > {
- type ReasoningItemForRequest = {
- type: "reasoning"
- encrypted_content: string
- id?: string
- summary?: any[]
- }
- const cleanConversationHistory: (Anthropic.Messages.MessageParam | ReasoningItemForRequest)[] = []
- for (const msg of messages) {
- // Standalone reasoning: send encrypted, skip plain text
- if (msg.type === "reasoning") {
- if (msg.encrypted_content) {
- cleanConversationHistory.push({
- type: "reasoning",
- summary: msg.summary,
- encrypted_content: msg.encrypted_content!,
- ...(msg.id ? { id: msg.id } : {}),
- })
- }
- continue
- }
- // Preferred path: assistant message with embedded reasoning as first content block
- if (msg.role === "assistant") {
- const rawContent = msg.content
- const contentArray: Anthropic.Messages.ContentBlockParam[] = Array.isArray(rawContent)
- ? (rawContent as Anthropic.Messages.ContentBlockParam[])
- : rawContent !== undefined
- ? ([
- { type: "text", text: rawContent } satisfies Anthropic.Messages.TextBlockParam,
- ] as Anthropic.Messages.ContentBlockParam[])
- : []
- const [first, ...rest] = contentArray
- // Check if this message has reasoning_details (OpenRouter format for Gemini 3, etc.)
- const msgWithDetails = msg
- if (msgWithDetails.reasoning_details && Array.isArray(msgWithDetails.reasoning_details)) {
- // Build the assistant message with reasoning_details
- let assistantContent: Anthropic.Messages.MessageParam["content"]
- if (contentArray.length === 0) {
- assistantContent = ""
- } else if (contentArray.length === 1 && contentArray[0].type === "text") {
- assistantContent = (contentArray[0] as Anthropic.Messages.TextBlockParam).text
- } else {
- assistantContent = contentArray
- }
- // Create message with reasoning_details property
- cleanConversationHistory.push({
- role: "assistant",
- content: assistantContent,
- reasoning_details: msgWithDetails.reasoning_details,
- } as any)
- continue
- }
- // Embedded reasoning: encrypted (send) or plain text (skip)
- const hasEncryptedReasoning =
- first && (first as any).type === "reasoning" && typeof (first as any).encrypted_content === "string"
- const hasPlainTextReasoning =
- first && (first as any).type === "reasoning" && typeof (first as any).text === "string"
- if (hasEncryptedReasoning) {
- const reasoningBlock = first as any
- // Send as separate reasoning item (OpenAI Native)
- cleanConversationHistory.push({
- type: "reasoning",
- summary: reasoningBlock.summary ?? [],
- encrypted_content: reasoningBlock.encrypted_content,
- ...(reasoningBlock.id ? { id: reasoningBlock.id } : {}),
- })
- // Send assistant message without reasoning
- let assistantContent: Anthropic.Messages.MessageParam["content"]
- if (rest.length === 0) {
- assistantContent = ""
- } else if (rest.length === 1 && rest[0].type === "text") {
- assistantContent = (rest[0] as Anthropic.Messages.TextBlockParam).text
- } else {
- assistantContent = rest
- }
- cleanConversationHistory.push({
- role: "assistant",
- content: assistantContent,
- } satisfies Anthropic.Messages.MessageParam)
- continue
- } else if (hasPlainTextReasoning) {
- // Check if the model's preserveReasoning flag is set
- // If true, include the reasoning block in API requests
- // If false/undefined, strip it out (stored for history only, not sent back to API)
- const shouldPreserveForApi = this.api.getModel().info.preserveReasoning === true
- let assistantContent: Anthropic.Messages.MessageParam["content"]
- if (shouldPreserveForApi) {
- // Include reasoning block in the content sent to API
- assistantContent = contentArray
- } else {
- // Strip reasoning out - stored for history only, not sent back to API
- if (rest.length === 0) {
- assistantContent = ""
- } else if (rest.length === 1 && rest[0].type === "text") {
- assistantContent = (rest[0] as Anthropic.Messages.TextBlockParam).text
- } else {
- assistantContent = rest
- }
- }
- cleanConversationHistory.push({
- role: "assistant",
- content: assistantContent,
- } satisfies Anthropic.Messages.MessageParam)
- continue
- }
- }
- // Default path for regular messages (no embedded reasoning)
- if (msg.role) {
- cleanConversationHistory.push({
- role: msg.role,
- content: msg.content as Anthropic.Messages.ContentBlockParam[] | string,
- })
- }
- }
- return cleanConversationHistory
- }
- 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
- }
- /**
- * Get the tool protocol locked to this task.
- * Returns undefined only if the task hasn't been fully initialized yet.
- *
- * @see {@link _taskToolProtocol} for lifecycle details
- */
- public get taskToolProtocol() {
- return this._taskToolProtocol
- }
- /**
- * 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
- }
- /**
- * Broadcast browser session updates to the browser panel (if open)
- */
- private broadcastBrowserSessionUpdate(): void {
- const provider = this.providerRef.deref()
- if (!provider) {
- return
- }
- try {
- const { BrowserSessionPanelManager } = require("../webview/BrowserSessionPanelManager")
- const panelManager = BrowserSessionPanelManager.getInstance(provider)
- // Get browser session messages
- const browserSessionStartIndex = this.clineMessages.findIndex(
- (m) =>
- m.ask === "browser_action_launch" ||
- (m.say === "browser_session_status" && m.text?.includes("opened")),
- )
- const browserSessionMessages =
- browserSessionStartIndex !== -1 ? this.clineMessages.slice(browserSessionStartIndex) : []
- const isBrowserSessionActive = this.browserSession?.isSessionActive() ?? false
- // Update the panel asynchronously
- panelManager.updateBrowserSession(browserSessionMessages, isBrowserSessionActive).catch((error: Error) => {
- console.error("Failed to broadcast browser session update:", error)
- })
- } catch (error) {
- // Silently fail if panel manager is not available
- console.debug("Browser panel not available for update:", error)
- }
- }
- /**
- * 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)
- }
- }
- }
|