@@ -491,6 +491,9 @@ function mergeKiroUsage(first: OcxUsage | undefined, second: OcxUsage | undefine
491491 return {
492492 inputTokens : first . inputTokens + second . inputTokens ,
493493 outputTokens : first . outputTokens + second . outputTokens ,
494+ ...( typeof first . contextInputTokens === "number" || typeof second . contextInputTokens === "number"
495+ ? { contextInputTokens : Math . max ( first . contextInputTokens ?? 0 , second . contextInputTokens ?? 0 ) }
496+ : { } ) ,
494497 ...( totalTokens !== undefined ? { totalTokens } : { } ) ,
495498 ...( sumOptional ( "cachedInputTokens" ) !== undefined ? { cachedInputTokens : sumOptional ( "cachedInputTokens" ) } : { } ) ,
496499 ...( sumOptional ( "cacheReadInputTokens" ) !== undefined ? { cacheReadInputTokens : sumOptional ( "cacheReadInputTokens" ) } : { } ) ,
@@ -534,6 +537,7 @@ async function* parseKiroAttempt(
534537 nameMap : Map < string , string > | undefined ,
535538 conversationId : string | undefined ,
536539 previousAssistantText ?: string ,
540+ contextInputTokens ?: number ,
537541) : AsyncGenerator < AdapterEvent , KiroAttemptResult > {
538542 const emptyResult = ( ) : KiroAttemptResult => ( { assistantText : "" , sawReasoning : false } ) ;
539543 if ( ! response . body ) {
@@ -560,11 +564,20 @@ async function* parseKiroAttempt(
560564 const providerState = ( ) : { kiro : { conversationId : string } } | undefined =>
561565 returnedConversationId ? { kiro : { conversationId : returnedConversationId } } : undefined ;
562566
563- const usage = ( ) : OcxUsage => authoritativeUsage ?? ( {
567+ const contextUsageInputFloor = ( ) : number | undefined => {
568+ if ( contextUsagePercentage === undefined || ! contextWindow ) return undefined ;
569+ const floor = Math . ceil ( contextWindow * Math . min ( contextUsagePercentage , 100 ) / 100 ) ;
570+ return Number . isFinite ( floor ) && floor > 0 ? floor : undefined ;
571+ } ;
572+ const usage = ( ) : OcxUsage => {
573+ const contextFloor = Math . max ( contextInputTokens ?? 0 , contextUsageInputFloor ( ) ?? 0 ) ;
574+ const base = authoritativeUsage ?? {
564575 inputTokens,
565576 outputTokens : estimateTokens ( outputChars , modelId ) ,
566577 estimated : true ,
567- } ) ;
578+ } ;
579+ return contextFloor > 0 ? { ...base , contextInputTokens : contextFloor } : base ;
580+ } ;
568581
569582 const classifiedTerminal = ( failure : KiroErrorClassification ) : AdapterEvent => ( {
570583 type : "error" ,
@@ -970,6 +983,7 @@ export async function* parseKiroStream(
970983 conversationId ?: string ,
971984 completionMode : KiroCompletionMode = "disabled" ,
972985 fallbackFactory ?: KiroFallbackFactory ,
986+ contextInputTokens ?: number ,
973987) : AsyncGenerator < AdapterEvent > {
974988 const first = parseKiroAttempt (
975989 response ,
@@ -979,6 +993,8 @@ export async function* parseKiroStream(
979993 contextWindow ,
980994 nameMap ,
981995 conversationId ,
996+ undefined ,
997+ contextInputTokens ,
982998 ) ;
983999 let firstNext = await first . next ( ) ;
9841000 while ( ! firstNext . done ) {
@@ -1043,6 +1059,7 @@ export async function* parseKiroStream(
10431059 fallback . nameMap ,
10441060 fallback . conversationId ,
10451061 firstResult . assistantText ,
1062+ contextInputTokens ,
10461063 ) ;
10471064 let secondNext = await second . next ( ) ;
10481065 while ( ! secondNext . done ) {
@@ -1080,6 +1097,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
10801097 // Per-request closure (resolveAdapter builds a fresh adapter per request — server.ts:440 — so this
10811098 // is race-free) carrying the heuristic input-token estimate from buildRequest into the stream.
10821099 let inputTokens = 0 ;
1100+ let contextInputTokens = 0 ;
10831101 let modelId : string | undefined ;
10841102 let contextWindow : number | undefined ;
10851103 let toolNameMap : Map < string , string > | undefined ;
@@ -1097,6 +1115,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
10971115 conversationId : string ;
10981116 completionMode : KiroCompletionMode ;
10991117 inputTokens : number ;
1118+ contextInputTokens : number ;
11001119 } > => {
11011120 if ( typeof provider . apiKey !== "string" || provider . apiKey . trim ( ) === "" ) {
11021121 throw new Error ( "kiro token missing — run ocx login kiro" ) ;
@@ -1141,6 +1160,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
11411160 conversationId : built . conversationId ,
11421161 completionMode : built . completionMode ,
11431162 inputTokens : estimateKiroInputTokens ( parsed ) ,
1163+ contextInputTokens : estimateKiroLogInputTokens ( parsed ) ,
11441164 } ;
11451165 } ;
11461166
@@ -1191,6 +1211,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
11911211 modelId = parsed . modelId ;
11921212 contextWindow = configuredKiroContextWindow ( provider , parsed . modelId ) ;
11931213 inputTokens = built . inputTokens ;
1214+ contextInputTokens = built . contextInputTokens ;
11941215 toolNameMap = built . nameMap ;
11951216 conversationId = built . conversationId ;
11961217 completionMode = built . completionMode ;
@@ -1209,6 +1230,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
12091230 conversationId ,
12101231 completionMode ,
12111232 completionMode === "required" ? fallbackFactory : undefined ,
1233+ contextInputTokens ,
12121234 ) ;
12131235 } ,
12141236
@@ -1239,6 +1261,7 @@ export function createKiroAdapter(provider: OcxProviderConfig): ProviderAdapter
12391261 conversationId ,
12401262 completionMode ,
12411263 completionMode === "required" ? fallbackFactory : undefined ,
1264+ contextInputTokens ,
12421265 ) ) events . push ( e ) ;
12431266 return events ;
12441267 } ,
0 commit comments