| 1289 | } |
| 1290 | |
| 1291 | setActiveWorkspaceId(workspaceId: string | null): void { |
| 1292 | assert( |
| 1293 | workspaceId === null || (typeof workspaceId === "string" && workspaceId.length > 0), |
| 1294 | "setActiveWorkspaceId requires a non-empty workspaceId or null" |
| 1295 | ); |
| 1296 | |
| 1297 | if (this.activeWorkspaceId === workspaceId) { |
| 1298 | return; |
| 1299 | } |
| 1300 | |
| 1301 | const previousActiveId = this.activeWorkspaceId; |
| 1302 | this.activeWorkspaceId = workspaceId; |
| 1303 | this.ensureActiveOnChatSubscription(); |
| 1304 | |
| 1305 | // Re-hydrate persisted session usage so cost totals reflect any |
| 1306 | // session-usage-delta events that arrived while this workspace was inactive. |
| 1307 | if (workspaceId) { |
| 1308 | this.refreshSessionUsage(workspaceId); |
| 1309 | } |
| 1310 | |
| 1311 | // Invalidate cached workspace state for both the old and new active |
| 1312 | // workspaces. getWorkspaceState() uses activeOnChatWorkspaceId to decide |
| 1313 | // whether to trust aggregator data or activity snapshots, so a switch |
| 1314 | // requires recomputation even if no new events arrived. |
| 1315 | if (previousActiveId) { |
| 1316 | this.states.bump(previousActiveId); |
| 1317 | } |
| 1318 | if (workspaceId) { |
| 1319 | this.states.bump(workspaceId); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | isOnChatSubscriptionActive(workspaceId: string): boolean { |
| 1324 | assert( |