@@ -25,7 +25,10 @@ import {
2525 ensureReplayAssistantMessage ,
2626 getTrackedReplayAssistantMessageId ,
2727} from "./acpReplayAssistant" ;
28- import { getLocalSessionId } from "./acpSessionTracker" ;
28+ import {
29+ getLocalSessionId ,
30+ subscribeToSessionRegistration ,
31+ } from "./acpSessionTracker" ;
2932import { perfLog } from "@/shared/lib/perfLog" ;
3033
3134// Pre-set message ID for the next live stream per goose session
@@ -44,6 +47,20 @@ interface LivePerf {
4447 chunkCount : number ;
4548}
4649const livePerf = new Map < string , LivePerf > ( ) ;
50+ const pendingUsageUpdates = new Map <
51+ string ,
52+ { accumulatedTotal : number ; contextLimit : number }
53+ > ( ) ;
54+
55+ subscribeToSessionRegistration ( ( localSessionId , gooseSessionId ) => {
56+ const pendingUsage = pendingUsageUpdates . get ( gooseSessionId ) ;
57+ if ( ! pendingUsage ) {
58+ return ;
59+ }
60+
61+ useChatStore . getState ( ) . updateTokenState ( localSessionId , pendingUsage ) ;
62+ pendingUsageUpdates . delete ( gooseSessionId ) ;
63+ } ) ;
4764
4865export function setActiveMessageId (
4966 gooseSessionId : string ,
@@ -78,7 +95,8 @@ export async function handleSessionNotification(
7895 notification : SessionNotification ,
7996) : Promise < void > {
8097 const gooseSessionId = notification . sessionId ;
81- const sessionId = getLocalSessionId ( gooseSessionId ) ?? gooseSessionId ;
98+ const localSessionId = getLocalSessionId ( gooseSessionId ) ;
99+ const sessionId = localSessionId ?? gooseSessionId ;
82100 const { update } = notification ;
83101 const isReplay = useChatStore . getState ( ) . loadingSessionIds . has ( sessionId ) ;
84102
@@ -93,7 +111,7 @@ export async function handleSessionNotification(
93111 }
94112 perf . lastAt = now ;
95113 perf . count += 1 ;
96- handleReplay ( sessionId , gooseSessionId , update ) ;
114+ handleReplay ( sessionId , gooseSessionId , localSessionId , update ) ;
97115 } else {
98116 const perf = livePerf . get ( gooseSessionId ) ;
99117 if ( perf && update . sessionUpdate === "agent_message_chunk" ) {
@@ -106,7 +124,7 @@ export async function handleSessionNotification(
106124 ) ;
107125 }
108126 }
109- handleLive ( sessionId , gooseSessionId , update ) ;
127+ handleLive ( sessionId , gooseSessionId , localSessionId , update ) ;
110128 }
111129}
112130
@@ -125,6 +143,7 @@ export function clearReplayPerf(sessionId: string): void {
125143function handleReplay (
126144 sessionId : string ,
127145 gooseSessionId : string ,
146+ localSessionId : string | null ,
128147 update : SessionUpdate ,
129148) : void {
130149 switch ( update . sessionUpdate ) {
@@ -247,7 +266,7 @@ function handleReplay(
247266 case "session_info_update" :
248267 case "config_option_update" :
249268 case "usage_update" :
250- handleShared ( sessionId , update ) ;
269+ handleShared ( sessionId , gooseSessionId , localSessionId , update ) ;
251270 break ;
252271
253272 default :
@@ -258,6 +277,7 @@ function handleReplay(
258277function handleLive (
259278 sessionId : string ,
260279 gooseSessionId : string ,
280+ localSessionId : string | null ,
261281 update : SessionUpdate ,
262282) : void {
263283 const store = useChatStore . getState ( ) ;
@@ -350,15 +370,20 @@ function handleLive(
350370 case "session_info_update" :
351371 case "config_option_update" :
352372 case "usage_update" :
353- handleShared ( sessionId , update ) ;
373+ handleShared ( sessionId , gooseSessionId , localSessionId , update ) ;
354374 break ;
355375
356376 default :
357377 break ;
358378 }
359379}
360380
361- function handleShared ( sessionId : string , update : SessionUpdate ) : void {
381+ function handleShared (
382+ sessionId : string ,
383+ gooseSessionId : string ,
384+ localSessionId : string | null ,
385+ update : SessionUpdate ,
386+ ) : void {
362387 switch ( update . sessionUpdate ) {
363388 case "session_info_update" : {
364389 const info = update as SessionUpdate & {
@@ -410,7 +435,6 @@ function handleShared(sessionId: string, update: SessionUpdate): void {
410435 currentModelId ;
411436
412437 const sessionStore = useChatSessionStore . getState ( ) ;
413- sessionStore . setSessionModels ( sessionId , availableModels ) ;
414438 sessionStore . updateSession (
415439 sessionId ,
416440 { modelId : currentModelId , modelName : currentModelName } ,
@@ -423,7 +447,16 @@ function handleShared(sessionId: string, update: SessionUpdate): void {
423447
424448 case "usage_update" : {
425449 const usage = update as SessionUpdate & { sessionUpdate : "usage_update" } ;
426- useChatStore . getState ( ) . updateTokenState ( sessionId , {
450+
451+ if ( ! localSessionId ) {
452+ pendingUsageUpdates . set ( gooseSessionId , {
453+ accumulatedTotal : usage . used ,
454+ contextLimit : usage . size ,
455+ } ) ;
456+ break ;
457+ }
458+
459+ useChatStore . getState ( ) . updateTokenState ( localSessionId , {
427460 accumulatedTotal : usage . used ,
428461 contextLimit : usage . size ,
429462 } ) ;
@@ -485,6 +518,7 @@ function ensureLiveAssistantMessage(
485518
486519export function clearMessageTracking ( ) : void {
487520 presetMessageIds . clear ( ) ;
521+ pendingUsageUpdates . clear ( ) ;
488522 clearReplayAssistantTracking ( ) ;
489523}
490524
0 commit comments