fix: handle VS Code LM API token count of 4 by recalculating with string content #6294
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.
Fixes #6290
Summary
This PR addresses the issue where VS Code Language Model API was not correctly calculating the context size when using the "copilot - gpt-4.1" model. The problem was that the API would return a token count of 4 for
LanguageModelChatMessageobjects, which appears to be a placeholder value rather than the actual token count.Changes
Modified
internalCountTokensmethod: Added special handling when the token count equals 4 forLanguageModelChatMessageobjectsUpdated type checking: Changed from
instanceof vscode.LanguageModelChatMessageto a duck-typing approach to work with both real and mocked objectsAdded comprehensive test coverage:
Implementation Details
As requested by @NaccOll, the solution:
internalCountTokensmethodLanguageModelChatMessageANDtokenCountequals 4, converts theLanguageModelChatMessageto a string and recalculates viathis.client.countTokensTesting
This ensures that Roo Code continues to work correctly with VS Code's Language Model API and properly calculates context sizes.
Important
Fixes token count calculation in
internalCountTokensforLanguageModelChatMessagewhen count is 4 by recalculating with string content.internalCountTokensinvscode-lm.tsforLanguageModelChatMessagewhen count is 4 by recalculating with string content.instanceofto duck-typing for compatibility with real and mocked objects.vscode-lm.spec.tsfor normal token counting, special case when token count is 4, and various edge cases.api/providersdirectory.This description was created by
for ce29a53. You can customize this summary. It will automatically update as commits are pushed.