@@ -44,6 +44,7 @@ import { CloudService, BridgeOrchestrator } from "@roo-code/cloud"
4444import { ApiHandler , ApiHandlerCreateMessageMetadata , buildApiHandler } from "../../api"
4545import { ApiStream , GroundingSource } from "../../api/transform/stream"
4646import { maybeRemoveImageBlocks } from "../../api/transform/image-cleaning"
47+ import { VirtualQuotaFallbackHandler } from "../../api/providers/virtual-quota-fallback" // kilocode_change: Import VirtualQuotaFallbackHandler for model change notifications
4748
4849// shared
4950import { findLastIndex } from "../../shared/array"
@@ -373,6 +374,13 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
373374
374375 this . apiConfiguration = apiConfiguration
375376 this . api = buildApiHandler ( apiConfiguration )
377+ // kilocode_change start: Listen for model changes in virtual quota fallback
378+ if ( this . api instanceof VirtualQuotaFallbackHandler ) {
379+ this . api . on ( "handlerChanged" , ( ) => {
380+ this . emit ( "modelChanged" )
381+ } )
382+ }
383+ // kilocode_change end
376384 this . autoApprovalHandler = new AutoApprovalHandler ( )
377385
378386 this . urlContentFetcher = new UrlContentFetcher ( provider . context )
@@ -411,6 +419,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
411419 this . messageQueueStateChangedHandler = ( ) => {
412420 this . emit ( RooCodeEventName . TaskUserMessage , this . taskId )
413421 this . providerRef . deref ( ) ?. postStateToWebview ( )
422+ this . emit ( "modelChanged" ) // kilocode_change: Emit modelChanged for virtual quota fallback UI updates
414423 }
415424
416425 this . messageQueueService . on ( "stateChanged" , this . messageQueueStateChangedHandler )
@@ -2740,6 +2749,11 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
27402749 const { profileThresholds = { } } = state ?? { }
27412750
27422751 const { contextTokens } = this . getTokenUsage ( )
2752+ // kilocode_change start: Initialize virtual quota fallback handler
2753+ if ( this . api instanceof VirtualQuotaFallbackHandler ) {
2754+ await this . api . initialize ( )
2755+ }
2756+ // kilocode_change end
27432757 const modelInfo = this . api . getModel ( ) . info
27442758
27452759 const maxTokens = getModelMaxOutputTokens ( {
@@ -2748,7 +2762,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
27482762 settings : this . apiConfiguration ,
27492763 } )
27502764
2751- const contextWindow = modelInfo . contextWindow
2765+ const contextWindow = this . api . contextWindow ?? modelInfo . contextWindow // kilocode_change: Use contextWindow from API handler if available
27522766
27532767 // Get the current profile ID using the helper method
27542768 const currentProfileId = this . getCurrentProfileId ( state )
@@ -2872,6 +2886,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
28722886 const { contextTokens } = this . getTokenUsage ( )
28732887
28742888 if ( contextTokens ) {
2889+ // kilocode_change start: Initialize and adjust virtual quota fallback handler
2890+ if ( this . api instanceof VirtualQuotaFallbackHandler ) {
2891+ await this . api . initialize ( )
2892+ await this . api . adjustActiveHandler ( "Pre-Request Adjustment" )
2893+ }
2894+ // kilocode_change end
28752895 const modelInfo = this . api . getModel ( ) . info
28762896
28772897 const maxTokens = getModelMaxOutputTokens ( {
@@ -2880,7 +2900,7 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
28802900 settings : this . apiConfiguration ,
28812901 } )
28822902
2883- const contextWindow = modelInfo . contextWindow
2903+ const contextWindow = this . api . contextWindow ?? modelInfo . contextWindow // kilocode_change
28842904
28852905 // Get the current profile ID using the helper method
28862906 const currentProfileId = this . getCurrentProfileId ( state )
0 commit comments