@@ -1166,6 +1166,25 @@ const sseHandlers: Record<string, SSEHandler> = {
11661166 context . currentTextBlock = null
11671167 updateStreamingMessage ( set , context )
11681168 } ,
1169+ context_usage : ( data , _context , _get , set ) => {
1170+ try {
1171+ const usageData = data ?. data
1172+ if ( usageData ) {
1173+ set ( {
1174+ contextUsage : {
1175+ usage : usageData . usage || 0 ,
1176+ percentage : usageData . percentage || 0 ,
1177+ model : usageData . model || '' ,
1178+ contextWindow : usageData . context_window || usageData . contextWindow || 0 ,
1179+ when : usageData . when || 'start' ,
1180+ estimatedTokens : usageData . estimated_tokens || usageData . estimatedTokens ,
1181+ } ,
1182+ } )
1183+ }
1184+ } catch ( err ) {
1185+ logger . warn ( 'Failed to handle context_usage event:' , err )
1186+ }
1187+ } ,
11691188 default : ( ) => { } ,
11701189}
11711190
@@ -1304,6 +1323,7 @@ const initialState = {
13041323 showPlanTodos : false ,
13051324 toolCallsById : { } as Record < string , CopilotToolCall > ,
13061325 suppressAutoSelect : false ,
1326+ contextUsage : null ,
13071327}
13081328
13091329export const useCopilotStore = create < CopilotStore > ( ) (
@@ -1314,7 +1334,7 @@ export const useCopilotStore = create<CopilotStore>()(
13141334 setMode : ( mode ) => set ( { mode } ) ,
13151335
13161336 // Clear messages
1317- clearMessages : ( ) => set ( { messages : [ ] } ) ,
1337+ clearMessages : ( ) => set ( { messages : [ ] , contextUsage : null } ) ,
13181338
13191339 // Workflow selection
13201340 setWorkflowId : async ( workflowId : string | null ) => {
@@ -1374,6 +1394,7 @@ export const useCopilotStore = create<CopilotStore>()(
13741394 planTodos : [ ] ,
13751395 showPlanTodos : false ,
13761396 suppressAutoSelect : false ,
1397+ contextUsage : null ,
13771398 } )
13781399
13791400 // Background-save the previous chat's latest messages before switching (optimistic)
@@ -1442,6 +1463,7 @@ export const useCopilotStore = create<CopilotStore>()(
14421463 planTodos : [ ] ,
14431464 showPlanTodos : false ,
14441465 suppressAutoSelect : true ,
1466+ contextUsage : null ,
14451467 } )
14461468 } ,
14471469
@@ -2041,6 +2063,7 @@ export const useCopilotStore = create<CopilotStore>()(
20412063 for await ( const data of parseSSEStream ( reader , decoder ) ) {
20422064 const { abortController } = get ( )
20432065 if ( abortController ?. signal . aborted ) break
2066+
20442067 const handler = sseHandlers [ data . type ] || sseHandlers . default
20452068 await handler ( data , context , get , set )
20462069 if ( context . streamComplete ) break
0 commit comments