Repository navigation
feat(extension): show session cost in the context popover #4751
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b5fa9ea
feat(extension): track per-conversation session cost from gateway usage
iscekic fbabb3e
test(extension): cover session cost display in context popover
iscekic a3a3322
Merge remote-tracking branch 'origin/main' into feat/ext-session-cost
iscekic 92bb4c1
refactor(extension): reuse TurnUsage type for run usage updates
iscekic c5ee163
Merge remote-tracking branch 'origin/main' into feat/ext-session-cost
iscekic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { addSessionCost, formatSessionCost } from './session-cost'; | ||
|
|
||
| describe('session cost helpers', () => { | ||
| it('formats zero and non-positive values as $0.0000', () => { | ||
| expect(formatSessionCost(0)).toBe('$0.0000'); | ||
| expect(formatSessionCost(-0.01)).toBe('$0.0000'); | ||
| expect(formatSessionCost(Number.NaN)).toBe('$0.0000'); | ||
| expect(formatSessionCost(Number.POSITIVE_INFINITY)).toBe('$0.0000'); | ||
| }); | ||
|
|
||
| it('formats positive costs to four decimal places', () => { | ||
| expect(formatSessionCost(0.0123)).toBe('$0.0123'); | ||
| expect(formatSessionCost(1.234)).toBe('$1.2340'); | ||
| expect(formatSessionCost(5e-5)).toBe('$0.0001'); | ||
| }); | ||
|
|
||
| it('adds finite non-negative costs', () => { | ||
| expect(addSessionCost(0.0123, 0.0007)).toBeCloseTo(0.013); | ||
| expect(addSessionCost(0.013, 0.001)).toBeCloseTo(0.014); | ||
| expect(addSessionCost(0, 0)).toBe(0); | ||
| }); | ||
|
|
||
| it('leaves the previous total unchanged for missing, non-finite, or negative cost', () => { | ||
| const usageWithoutCost: { costUsd?: number } = {}; | ||
| expect(addSessionCost(0.5, usageWithoutCost.costUsd)).toBe(0.5); | ||
| expect(addSessionCost(0.5, Number.NaN)).toBe(0.5); | ||
| expect(addSessionCost(0.5, Number.POSITIVE_INFINITY)).toBe(0.5); | ||
| expect(addSessionCost(0.5, -0.01)).toBe(0.5); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** Format session spend like mobile `formatCost`: always `$X.XXXX`. */ | ||
| export const formatSessionCost = (cost: number): string => { | ||
| if (!Number.isFinite(cost) || cost <= 0) { | ||
| return '$0.0000'; | ||
| } | ||
|
|
||
| return `$${cost.toFixed(4)}`; | ||
| }; | ||
|
|
||
| /** | ||
| * Add a completion's USD cost to a running session total. | ||
| * Missing, non-finite, or negative values leave the previous total unchanged. | ||
| */ | ||
| export const addSessionCost = (previous: number, costUsd: number | undefined): number => { | ||
| if (costUsd === undefined || !Number.isFinite(costUsd) || costUsd < 0) { | ||
| return previous; | ||
| } | ||
|
|
||
| return previous + costUsd; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.