-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: add AI Inference Summary for total costs and token usage #6824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
- Add persistent usage tracking service that stores data in ~/.roo/usage-tracking.json - Create CostSummary React component to display costs and token breakdown - Integrate usage tracking with task history updates - Add workspace filtering (Current vs All Workspaces) - Implement mode breakdown showing costs per AI mode - Data persists even when tasks are deleted from history Fixes #6822
| } | ||
|
|
||
| const content = await fs.readFile(this.filePath, "utf-8") | ||
| const data = JSON.parse(content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON.parse is wrapped in a try/catch, which is good. For easier debugging, consider including the file path in the error log when parsing fails.
This comment was generated because it violated a code review rule: irule_PTI8rjtnhwrWq6jS.
| includeTaskHistoryInEnhance?: boolean | ||
| setIncludeTaskHistoryInEnhance: (value: boolean) => void | ||
| persistentUsageData?: { | ||
| all: any // UsageSummary type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The persistentUsageData state property is currently typed as 'any'. For improved type safety, consider using the UsageSummary type from '@roo-code/types' instead of 'any'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reviewed my own code and found 7 issues. The recursion depth is concerning.
| } | ||
|
|
||
| // Export singleton instance getter | ||
| export const getUsageTrackingService = () => UsageTrackingService.getInstance() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This service needs test coverage. The PR claims tests pass but doesn't add any tests for this critical new functionality. Could we add unit tests to verify data persistence, workspace filtering, and migration logic?
| await usageService.migrateFromTaskHistory(taskHistory, this.cwd) | ||
| } | ||
| } catch (error) { | ||
| this.log(`Failed to load persistent usage data: ${error}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling here only logs failures. Should we consider showing a user-friendly notification if usage data loading fails? Users might wonder why their cost summary isn't appearing.
| // Migrate existing task history if needed | ||
| const taskHistory = this.getGlobalState("taskHistory") ?? [] | ||
| if (taskHistory.length > 0) { | ||
| await usageService.migrateFromTaskHistory(taskHistory, this.cwd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This migration could run multiple times if loadPersistentUsageDataAsync() is called repeatedly. Consider adding a flag to track if migration has already been attempted for this session?
| includeTaskHistoryInEnhance?: boolean | ||
| setIncludeTaskHistoryInEnhance: (value: boolean) => void | ||
| persistentUsageData?: { | ||
| all: any // UsageSummary type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use the proper UsageSummary type from @roo-code/types instead of 'any' here? Type safety would help prevent runtime errors:
| return ( | ||
| <div className="cost-summary-card border border-vscode-panel-border rounded-lg p-4 mb-4"> | ||
| <h3 className="text-lg font-semibold mb-3">AI Inference Summary</h3> | ||
| <div className="text-vscode-descriptionForeground">Loading usage data...</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component shows 'Loading usage data...' but never handles error states. What happens if the data fetch fails? Consider adding an error state display.
| ) | ||
| } | ||
|
|
||
| export default CostSummary |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This component also needs test coverage. Consider adding tests for workspace filtering, mode breakdown display, and number formatting.
| filteredData.push(newItem) | ||
|
|
||
| // Save updated data | ||
| await this.saveData(filteredData) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For users with extensive history, this JSON file could grow indefinitely. Should we implement data pruning for records older than a certain threshold (e.g., 90 days)?
|
Closing, the author of the issue is interested in implementing this |
Summary
This PR implements the AI Inference Summary feature as requested in #6822. The feature provides a comprehensive view of total costs and token usage across all AI tasks, with persistent storage that survives task deletion.
Key Features
1. Persistent Usage Tracking
~/.roo/usage-tracking.json2. Cost Summary Component
CostSummaryReact component displays:3. Integration Points
ClineProviderto track usage on every task updateExtensionStateContextfor state managementImplementation Details
New Files Created:
packages/types/src/usage.ts- Type definitions for usage trackingsrc/services/usage-tracking/index.ts- Singleton service for persistent storagewebview-ui/src/components/welcome/CostSummary.tsx- React component for displayModified Files:
src/core/webview/ClineProvider.ts- Added usage tracking integrationwebview-ui/src/components/welcome/WelcomeView.tsx- Added CostSummary componentsrc/shared/ExtensionMessage.ts- Added message types for usage datawebview-ui/src/context/ExtensionStateContext.tsx- Added persistent usage data statepackages/types/src/index.ts- Exported new usage typesTesting
Screenshots
The Cost Summary appears in the Welcome View showing:
Fixes #6822
Important
Adds AI Inference Summary feature for tracking and displaying total costs and token usage with persistent storage and a new UI component.
~/.roo/usage-tracking.json, surviving task deletions.CostSummaryReact component inCostSummary.tsxto display total costs and token usage.ClineProviderto update usage data on task updates.usage-tracking/index.ts: ImplementsUsageTrackingServicefor managing usage data.ClineProvider.ts: Loads and updates persistent usage data.webviewMessageHandler.ts: Handles requests for persistent usage data.WelcomeView.tsx: DisplaysCostSummarycomponent.ExtensionStateContext.tsx: Manages state for persistent usage data.UsageSummarytype inusage.tsfor data structure.ExtensionMessage.tsandWebviewMessage.tsfor new message types.This description was created by
for c138f3d. You can customize this summary. It will automatically update as commits are pushed.