-
Notifications
You must be signed in to change notification settings - Fork 307
feat: usage counter stories and basic UI #516
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 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1718c66
feat: usage counter stories and basic UI
alashchev17 1bb22c8
chore: fixed prettier check
alashchev17 71d5507
feat: saving usage to assistant message on stream & reasoning mode se…
alashchev17 7037a5b
fix: stubs for usage counters, fixed stories and refactored UsageCoun…
alashchev17 973a3cf
chore: removed console.log
alashchev17 4c44946
feat: storing usage object for chat thread instead of for each assist…
alashchev17 fc475d4
chore: better place for calculateInputTokens utility function
alashchev17 ebcdd2e
refactoring(middleware): simplified chat response middleware
alashchev17 59b2fa8
chore: remove unused imports
alashchev17 8d30662
refactoring: remove duplicate code
alashchev17 4f1ed78
Merge branch 'main' into feat/usage-counters-for-messages
alashchev17 33380a4
feat: calculating recommended maximum input limit based on model n_ctx
alashchev17 ba48139
fix: fix tests
alashchev17 f68d201
fix: moved UsageCounter to separate component modlet out of ChatConte…
alashchev17 8729191
Merge branch 'main' into feat/usage-counters-for-messages
alashchev17 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
27 changes: 27 additions & 0 deletions
27
refact-agent/gui/src/components/ChatContent/UsageCounter/UsageCounter.fixtures.ts
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,27 @@ | ||
| import { Usage } from "../../../services/refact"; | ||
|
|
||
| export const USAGE_COUNTER_STUB_GPT: Usage = { | ||
| completion_tokens: 30, | ||
| prompt_tokens: 3391, | ||
| total_tokens: 3421, | ||
| completion_tokens_details: { | ||
| accepted_prediction_tokens: 0, | ||
| audio_tokens: 0, | ||
| reasoning_tokens: 0, | ||
| rejected_prediction_tokens: 0, | ||
| }, | ||
| prompt_tokens_details: { | ||
| audio_tokens: 0, | ||
| cached_tokens: 3328, | ||
| }, | ||
| }; | ||
|
|
||
| export const USAGE_COUNTER_STUB_ANTHROPIC: Usage = { | ||
| completion_tokens: 142, | ||
| prompt_tokens: 5, | ||
| total_tokens: 147, | ||
| completion_tokens_details: null, | ||
| prompt_tokens_details: null, | ||
| cache_creation_input_tokens: 3291, | ||
| cache_read_input_tokens: 3608, | ||
| }; |
12 changes: 12 additions & 0 deletions
12
refact-agent/gui/src/components/ChatContent/UsageCounter/UsageCounter.module.css
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,12 @@ | ||
| .usageCounterContainer { | ||
| /* position: absolute; */ | ||
| /* top: 0; | ||
| right: 0; */ | ||
| margin-left: auto; | ||
| display: flex; | ||
| align-items: center; | ||
| padding: var(--space-2) var(--space-3); | ||
| gap: 8px; | ||
| max-width: max-content; | ||
| opacity: 0.7; | ||
| } |
57 changes: 57 additions & 0 deletions
57
refact-agent/gui/src/components/ChatContent/UsageCounter/UsageCounter.stories.tsx
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,57 @@ | ||
| import React from "react"; | ||
| import type { Meta, StoryObj } from "@storybook/react"; | ||
| import { Provider } from "react-redux"; | ||
|
|
||
| import { setUpStore } from "../../../app/store"; | ||
| import { Theme } from "../../Theme"; | ||
| import { AbortControllerProvider } from "../../../contexts/AbortControllers"; | ||
|
|
||
| import { UsageCounter } from "."; | ||
| import { Usage } from "../../../services/refact"; | ||
| import { | ||
| USAGE_COUNTER_STUB_ANTHROPIC, | ||
| USAGE_COUNTER_STUB_GPT, | ||
| } from "./UsageCounter.fixtures"; | ||
|
|
||
| const MockedStore: React.FC<{ usage: Usage }> = ({ usage }) => { | ||
| const store = setUpStore({ | ||
| config: { | ||
| themeProps: { | ||
| appearance: "dark", | ||
| }, | ||
| host: "web", | ||
| lspPort: 8001, | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <Provider store={store}> | ||
| <AbortControllerProvider> | ||
| <Theme accentColor="gray"> | ||
| <UsageCounter usage={usage} /> | ||
| </Theme> | ||
| </AbortControllerProvider> | ||
| </Provider> | ||
| ); | ||
| }; | ||
|
|
||
| const meta: Meta<typeof MockedStore> = { | ||
| title: "UsageCounter", | ||
| component: MockedStore, | ||
| args: { | ||
| usage: USAGE_COUNTER_STUB_GPT, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| export const GPTUsageCounter: StoryObj<typeof UsageCounter> = { | ||
| args: { | ||
| usage: USAGE_COUNTER_STUB_GPT, | ||
| }, | ||
| }; | ||
| export const AnthropicUsageCounter: StoryObj<typeof UsageCounter> = { | ||
| args: { | ||
| usage: USAGE_COUNTER_STUB_ANTHROPIC, | ||
| }, | ||
| }; |
99 changes: 99 additions & 0 deletions
99
refact-agent/gui/src/components/ChatContent/UsageCounter/UsageCounter.tsx
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,99 @@ | ||
| import React from "react"; | ||
| import { Card, Flex, HoverCard, Text } from "@radix-ui/themes"; | ||
| import { ArrowDownIcon, ArrowUpIcon } from "@radix-ui/react-icons"; | ||
|
|
||
| import { ScrollArea } from "../../ScrollArea"; | ||
| import { calculateUsageInputTokens } from "../../../utils/calculateUsageInputTokens"; | ||
| import type { Usage } from "../../../services/refact"; | ||
|
|
||
| import styles from "./UsageCounter.module.css"; | ||
|
|
||
| type UsageCounterProps = { | ||
| usage: Usage; | ||
| }; | ||
|
|
||
| function formatNumber(num: number): string { | ||
| return num >= 1_000_000 | ||
| ? (num / 1_000_000).toFixed(1) + "M" | ||
| : num >= 1_000 | ||
| ? (num / 1_000).toFixed(2) + "k" | ||
| : num.toString(); | ||
| } | ||
|
|
||
| const TokenDisplay: React.FC<{ label: string; value: number }> = ({ | ||
| label, | ||
| value, | ||
| }) => ( | ||
| <Flex align="center" justify="between" width="100%"> | ||
| <Text size="1" weight="bold"> | ||
| {label} | ||
| </Text> | ||
| <Text size="1">{value}</Text> | ||
| </Flex> | ||
| ); | ||
|
|
||
| export const UsageCounter: React.FC<UsageCounterProps> = ({ usage }) => { | ||
| const inputTokens = calculateUsageInputTokens(usage, [ | ||
| "prompt_tokens", | ||
| "cache_creation_input_tokens", | ||
| "cache_read_input_tokens", | ||
| ]); | ||
| const outputTokens = calculateUsageInputTokens(usage, ["completion_tokens"]); | ||
|
|
||
| return ( | ||
| <HoverCard.Root> | ||
| <HoverCard.Trigger> | ||
| <Card className={styles.usageCounterContainer}> | ||
| <Flex align="center"> | ||
| <ArrowUpIcon width="12" height="12" /> | ||
| <Text size="1">{formatNumber(inputTokens)}</Text> | ||
| </Flex> | ||
| <Flex align="center"> | ||
| <ArrowDownIcon width="12" height="12" /> | ||
| <Text size="1">{outputTokens}</Text> | ||
| </Flex> | ||
| </Card> | ||
| </HoverCard.Trigger> | ||
| <ScrollArea scrollbars="both" asChild> | ||
| <HoverCard.Content | ||
| size="1" | ||
| maxHeight="50vh" | ||
| maxWidth="90vw" | ||
| minWidth="300px" | ||
| avoidCollisions | ||
| align="end" | ||
| side="top" | ||
| > | ||
| <Flex direction="column" align="start" gap="2"> | ||
| <Text size="2" mb="2"> | ||
| Tokens spent per message: | ||
| </Text> | ||
| <TokenDisplay | ||
| label="Input tokens (in total):" | ||
| value={inputTokens} | ||
| /> | ||
| {usage.cache_read_input_tokens !== undefined && ( | ||
| <TokenDisplay | ||
| label="Cache read input tokens:" | ||
| value={usage.cache_read_input_tokens} | ||
| /> | ||
| )} | ||
| {usage.cache_creation_input_tokens !== undefined && ( | ||
| <TokenDisplay | ||
| label="Cache creation input tokens:" | ||
| value={usage.cache_creation_input_tokens} | ||
| /> | ||
| )} | ||
| <TokenDisplay label="Completion tokens:" value={outputTokens} /> | ||
| {usage.completion_tokens_details && ( | ||
| <TokenDisplay | ||
| label="Reasoning tokens:" | ||
| value={usage.completion_tokens_details.reasoning_tokens} | ||
| /> | ||
| )} | ||
| </Flex> | ||
| </HoverCard.Content> | ||
| </ScrollArea> | ||
| </HoverCard.Root> | ||
| ); | ||
| }; |
1 change: 1 addition & 0 deletions
1
refact-agent/gui/src/components/ChatContent/UsageCounter/index.ts
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 @@ | ||
| export { UsageCounter } from "./UsageCounter"; |
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
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.
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 move up a directory to
src/components/UsageCounter?