Skip to content

Commit e3e00b0

Browse files
authored
Merge pull request #680 from rajbos/refactor/fix-activate-di-casting
refactor: remove (tokenTracker as any) casts in activate()
2 parents d89b94e + cf7422e commit e3e00b0

1 file changed

Lines changed: 54 additions & 54 deletions

File tree

vscode-extension/src/extension.ts

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as vscode from 'vscode';
1+
import * as vscode from 'vscode';
22
import * as fs from 'fs';
33
import * as path from 'path';
44
import * as os from 'os';
@@ -178,9 +178,9 @@ class CopilotTokenTracker implements vscode.Disposable {
178178
// Cache of the last diagnostic report text for copy/issue operations
179179
private lastDiagnosticReport: string = '';
180180
private logViewerPanel?: vscode.WebviewPanel;
181-
private openCode: OpenCodeDataAccess;
182-
private crush: CrushDataAccess;
183-
private visualStudio: VisualStudioDataAccess;
181+
public openCode: OpenCodeDataAccess;
182+
public crush: CrushDataAccess;
183+
public visualStudio: VisualStudioDataAccess;
184184
private continue_: ContinueDataAccess;
185185
private claudeCode: ClaudeCodeDataAccess;
186186
private claudeDesktopCowork: ClaudeDesktopCoworkDataAccess;
@@ -191,7 +191,7 @@ class CopilotTokenTracker implements vscode.Disposable {
191191
private get usageAnalysisDeps(): UsageAnalysisDeps {
192192
return { warn: (m: string) => this.warn(m), tokenEstimators: this.tokenEstimators, modelPricing: this.modelPricing, toolNameMap: this.toolNameMap, ecosystems: this.ecosystems };
193193
}
194-
private sessionDiscovery: SessionDiscovery;
194+
public sessionDiscovery: SessionDiscovery;
195195
private statusBarItem: vscode.StatusBarItem;
196196
private readonly extensionUri: vscode.Uri;
197197
private readonly context: vscode.ExtensionContext;
@@ -276,7 +276,7 @@ class CopilotTokenTracker implements vscode.Disposable {
276276
private modelPricing: { [key: string]: ModelPricing } = modelPricingData.pricing as { [key: string]: ModelPricing };
277277

278278
// GitHub authentication session
279-
private githubSession: vscode.AuthenticationSession | undefined;
279+
public githubSession: vscode.AuthenticationSession | undefined;
280280
// Promise that resolves when the startup session restore completes
281281
private _sessionRestorePromise: Promise<void> | undefined;
282282
/** True when the user explicitly signed out from our extension this VS Code session. Gated by globalState so it survives reloads. */
@@ -289,7 +289,7 @@ class CopilotTokenTracker implements vscode.Disposable {
289289
private toolNameMap: { [key: string]: string } = toolNamesData as { [key: string]: string };
290290

291291
// Backend facade instance for accessing table storage data
292-
private backend: BackendFacade | undefined;
292+
public backend: BackendFacade | undefined;
293293

294294
// Helper method to get repository URL from package.json
295295
private getRepositoryUrl(): string {
@@ -325,7 +325,7 @@ class CopilotTokenTracker implements vscode.Disposable {
325325
* Stat a session file, handling virtual paths for both OpenCode and Crush.
326326
* Must be used instead of fs.promises.stat() directly.
327327
*/
328-
private async statSessionFile(sessionFile: string): Promise<import('fs').Stats> {
328+
public async statSessionFile(sessionFile: string): Promise<import('fs').Stats> {
329329
const eco = this.findEcosystem(sessionFile);
330330
if (eco) { return eco.stat(sessionFile); }
331331
return fs.promises.stat(sessionFile);
@@ -378,7 +378,7 @@ class CopilotTokenTracker implements vscode.Disposable {
378378
this.outputChannel.appendLine(`[${timestamp}] ${message}`);
379379
}
380380

381-
private warn(message: string): void {
381+
public warn(message: string): void {
382382
if (this._disposed) { return; }
383383
const timestamp = new Date().toLocaleTimeString();
384384
this.outputChannel.appendLine(`[${timestamp}] WARNING: ${message}`);
@@ -936,7 +936,7 @@ class CopilotTokenTracker implements vscode.Disposable {
936936
this.startBackendSyncAfterInitialAnalysis();
937937
// Force an immediate sync so the "Last Sync" timestamp updates right away
938938
// instead of waiting for the next timer tick.
939-
const backend = (this as any).backend;
939+
const backend = this.backend;
940940
if (backend && typeof backend.syncToBackendStore === 'function') {
941941
backend.syncToBackendStore(true).then(() => {
942942
// Refresh diagnostics again after sync completes so "Last Sync" shows the new time
@@ -994,7 +994,7 @@ class CopilotTokenTracker implements vscode.Disposable {
994994
*/
995995
private startBackendSyncAfterInitialAnalysis(): void {
996996
try {
997-
const backend = (this as any).backend;
997+
const backend = this.backend;
998998
if (backend && typeof backend.startTimerIfEnabled === 'function') {
999999
backend.startTimerIfEnabled();
10001000
}
@@ -2800,7 +2800,7 @@ class CopilotTokenTracker implements vscode.Disposable {
28002800
}
28012801

28022802
// Cached versions of session file reading methods
2803-
private async getSessionFileDataCached(sessionFilePath: string, mtime: number, fileSize: number): Promise<SessionFileCache> {
2803+
public async getSessionFileDataCached(sessionFilePath: string, mtime: number, fileSize: number): Promise<SessionFileCache> {
28042804
// Check if we have valid cached data
28052805
const cached = this.getCachedSessionData(sessionFilePath);
28062806
if (cached && cached.mtime === mtime && cached.size === fileSize) {
@@ -3825,7 +3825,7 @@ usageAnalysis: undefined
38253825
return { responseText, thinkingText, toolCalls, mcpTools };
38263826
}
38273827

3828-
private calculateEstimatedCost(modelUsage: ModelUsage): number {
3828+
public calculateEstimatedCost(modelUsage: ModelUsage): number {
38293829
return _calculateEstimatedCost(modelUsage, this.modelPricing);
38303830
}
38313831

@@ -3932,7 +3932,7 @@ usageAnalysis: undefined
39323932

39333933

39343934

3935-
private getModelFromRequest(request: any): string {
3935+
public getModelFromRequest(request: any): string {
39363936
return _getModelFromRequest(request, this.modelPricing);
39373937
}
39383938

@@ -3949,7 +3949,7 @@ usageAnalysis: undefined
39493949
}
39503950

39513951

3952-
private estimateTokensFromText(text: string, model: string = 'gpt-4'): number {
3952+
public estimateTokensFromText(text: string, model: string = 'gpt-4'): number {
39533953
return _estimateTokensFromText(text, model, this.tokenEstimators);
39543954
}
39553955

@@ -7835,72 +7835,72 @@ export async function activate(context: vscode.ExtensionContext) {
78357835

78367836
// Migrate settings from the old copilotTokenTracker namespace to aiEngineeringFluency.
78377837
// Run before any other settings are read so the new keys are populated first.
7838-
await migrateSettingsIfNeeded((m) => (tokenTracker as any).log(m));
7838+
await migrateSettingsIfNeeded((m) => tokenTracker.log(m));
78397839

78407840
// Wire up backend facade and commands so the diagnostics webview can launch the
78417841
// configuration wizard. Uses tokenTracker logging and helpers via casting to any.
78427842
try {
78437843
const backendFacade = new BackendFacade({
78447844
context,
7845-
log: (m: string) => (tokenTracker as any).log(m),
7846-
warn: (m: string) => (tokenTracker as any).warn(m),
7847-
updateTokenStats: () => (tokenTracker as any).updateTokenStats(),
7848-
calculateEstimatedCost: (modelUsage: any) => {
7849-
let total = 0;
7850-
const pricing = (modelPricingData as any).pricing || {};
7851-
for (const [model, usage] of Object.entries(modelUsage || {})) {
7852-
const p = pricing[model] || pricing["gpt-4o-mini"];
7853-
if (!p) {
7854-
continue;
7855-
}
7856-
const usageData = usage as {
7857-
inputTokens?: number;
7858-
outputTokens?: number;
7859-
};
7860-
total +=
7861-
((usageData.inputTokens || 0) / 1_000_000) * p.inputCostPerMillion;
7862-
total +=
7863-
((usageData.outputTokens || 0) / 1_000_000) *
7864-
p.outputCostPerMillion;
7865-
}
7866-
return total;
7867-
},
7845+
log: (m: string) => tokenTracker.log(m),
7846+
warn: (m: string) => tokenTracker.warn(m),
7847+
updateTokenStats: async () => { await tokenTracker.updateTokenStats(); },
7848+
calculateEstimatedCost: (modelUsage: ModelUsage) => tokenTracker.calculateEstimatedCost(modelUsage),
7849+
7850+
7851+
7852+
7853+
7854+
7855+
7856+
7857+
7858+
7859+
7860+
7861+
7862+
7863+
7864+
7865+
7866+
7867+
78687868
co2Per1kTokens: 0.2,
78697869
waterUsagePer1kTokens: 0.3,
78707870
co2AbsorptionPerTreePerYear: 21000,
78717871
getCopilotSessionFiles: () =>
7872-
(tokenTracker as any).sessionDiscovery.getCopilotSessionFiles(),
7872+
tokenTracker.sessionDiscovery.getCopilotSessionFiles(),
78737873
estimateTokensFromText: (text: string, model?: string) =>
7874-
(tokenTracker as any).estimateTokensFromText(text, model),
7874+
tokenTracker.estimateTokensFromText(text, model),
78757875
getModelFromRequest: (req: any) =>
7876-
(tokenTracker as any).getModelFromRequest(req),
7876+
tokenTracker.getModelFromRequest(req),
78777877
getSessionFileDataCached: (p: string, m: number, s: number) =>
7878-
(tokenTracker as any).getSessionFileDataCached(p, m, s),
7878+
tokenTracker.getSessionFileDataCached(p, m, s),
78797879
statSessionFile: (sessionFile: string) =>
7880-
(tokenTracker as any).statSessionFile(sessionFile),
7880+
tokenTracker.statSessionFile(sessionFile),
78817881
isOpenCodeSession: (sessionFile: string) =>
7882-
(tokenTracker as any).openCode.isOpenCodeSessionFile(sessionFile),
7882+
tokenTracker.openCode.isOpenCodeSessionFile(sessionFile),
78837883
getOpenCodeSessionData: (sessionFile: string) =>
7884-
(tokenTracker as any).getOpenCodeSessionData(sessionFile),
7884+
tokenTracker.openCode.getOpenCodeSessionData(sessionFile),
78857885
isCrushSession: (sessionFile: string) =>
7886-
(tokenTracker as any).crush.isCrushSessionFile(sessionFile),
7886+
tokenTracker.crush.isCrushSessionFile(sessionFile),
78877887
getCrushSessionData: (sessionFile: string) =>
7888-
(tokenTracker as any).crush.getCrushSessionData(sessionFile),
7888+
tokenTracker.crush.getCrushSessionData(sessionFile),
78897889
isVSSessionFile: (sessionFile: string) =>
7890-
(tokenTracker as any).visualStudio.isVSSessionFile(sessionFile),
7891-
getGithubToken: () => (tokenTracker as any).githubSession?.accessToken,
7890+
tokenTracker.visualStudio.isVSSessionFile(sessionFile),
7891+
getGithubToken: () => tokenTracker.githubSession?.accessToken,
78927892
});
78937893

78947894
const backendHandler = new BackendCommandHandler({
78957895
facade: backendFacade as any,
78967896
integration: undefined,
78977897
calculateEstimatedCost: (mu: any) => 0,
7898-
warn: (m: string) => (tokenTracker as any).warn(m),
7899-
log: (m: string) => (tokenTracker as any).log(m),
7898+
warn: (m: string) => tokenTracker.warn(m),
7899+
log: (m: string) => tokenTracker.log(m),
79007900
});
79017901

79027902
// Store backend facade in the tracker instance for dashboard access
7903-
(tokenTracker as any).backend = backendFacade;
7903+
tokenTracker.backend = backendFacade;
79047904

79057905
// Backend sync timer will be started after initial token analysis completes
79067906
// (see startBackendSyncAfterInitialAnalysis method)
@@ -7924,7 +7924,7 @@ export async function activate(context: vscode.ExtensionContext) {
79247924
context.subscriptions.push(configureTeamServerCommand);
79257925
} catch (err) {
79267926
// If backend wiring fails for any reason, don't block activation - fall back to settings behavior.
7927-
(tokenTracker as any).warn(
7927+
tokenTracker.warn(
79287928
"Failed to wire backend commands: " + String(err),
79297929
);
79307930
}

0 commit comments

Comments
 (0)