-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix:Feature/enhanced token counting #3396
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
Closed
SannidhyaSah
wants to merge
3
commits into
RooCodeInc:main
from
SannidhyaSah:feature/enhanced-token-counting
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,71 @@ | ||
| import { Anthropic } from "@anthropic-ai/sdk" | ||
|
|
||
| import { ModelInfo } from "../../shared/api" | ||
|
|
||
| import { ApiHandler } from "../index" | ||
| import { ApiStream } from "../transform/stream" | ||
| import { countTokens } from "../../utils/countTokens" | ||
| import { formatTokenInfo, createTokenTooltip } from "../../utils/tokenDisplay" | ||
| import type { ModelInfo, TokenUsageInfo } from "../../shared/api" | ||
|
|
||
| // Use any to bypass strict type checking for compatibility | ||
| type ContentBlockParam = any | ||
| type MessageParam = any | ||
|
|
||
| /** | ||
| * Base class for API providers that implements common functionality. | ||
| */ | ||
| export abstract class BaseProvider implements ApiHandler { | ||
| abstract createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream | ||
| abstract createMessage(systemPrompt: string, messages: MessageParam[]): ApiStream | ||
| abstract getModel(): { id: string; info: ModelInfo } | ||
|
|
||
| /** | ||
| * Default token counting implementation using tiktoken. | ||
| * Gets the last token usage information | ||
| */ | ||
| lastTokenUsage?: TokenUsageInfo | ||
|
|
||
| /** | ||
| * Default token counting implementation using enhanced tiktoken. | ||
| * Providers can override this to use their native token counting endpoints. | ||
| * | ||
| * @param content The content to count tokens for | ||
| * @returns A promise resolving to the token count | ||
| */ | ||
| async countTokens(content: Anthropic.Messages.ContentBlockParam[]): Promise<number> { | ||
| async countTokens(content: ContentBlockParam[]): Promise<number> { | ||
| if (content.length === 0) { | ||
| return 0 | ||
| } | ||
|
|
||
| return countTokens(content, { useWorker: true }) | ||
| // Get the provider ID from the model info | ||
| const { id: providerId } = this.getModel() | ||
|
|
||
| // Use the provider ID to get provider-specific token counting with enhanced accuracy | ||
| return countTokens(content, { | ||
| useWorker: true, | ||
| provider: providerId, | ||
| useEnhanced: true, // Use enhanced tiktoken implementation by default | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Formats token information for display in the UI | ||
| * @returns Formatted token usage string | ||
| */ | ||
| formatTokenDisplay(): string { | ||
| if (!this.lastTokenUsage) { | ||
| return "No token usage information available" | ||
| } | ||
|
|
||
| const { id: providerId } = this.getModel() | ||
| return formatTokenInfo(this.lastTokenUsage, providerId) | ||
| } | ||
|
|
||
| /** | ||
| * Creates a detailed tooltip for token usage | ||
| * @returns Tooltip text with detailed token usage | ||
| */ | ||
| createTokenTooltip(): string { | ||
| if (!this.lastTokenUsage) { | ||
| return "No token usage information available" | ||
| } | ||
|
|
||
| const { id: providerId } = this.getModel() | ||
| return createTokenTooltip(this.lastTokenUsage, providerId) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
The properties
cachedTokensandcacheReadTokensboth include the comment 'Number of tokens read from cache (if applicable)'. It might be confusing for future developers to distinguish between them. Please clarify the distinction in the comments or consider renaming one of them if they serve different purposes.This comment was generated because it violated a code review rule: mrule_aQsEnH8jWdOfHq2Z.