fix: handle special case when VS Code LM API returns tokenCount of 4 #6295
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.
Summary
This PR implements the fix requested by @NaccOll in issue #6290 to handle a special case in the VS Code Language Model API token counting.
Problem
The VS Code LM API was not correctly calculating context size when using the "copilot - gpt-4.1" model. The issue was that the API's
countTokensmethod was returning incorrect values forLanguageModelChatMessageobjects.Solution
As requested by @NaccOll, this PR:
internalCountTokensmethod as the primary approachLanguageModelChatMessageANDtokenCountequals 4, it converts the message to a string and recalculates usingthis.client.countTokensChanges
Modified
internalCountTokensmethod insrc/api/providers/vscode-lm.ts:tokenCount === 4forLanguageModelChatMessageobjectsAdded comprehensive test coverage in
src/api/providers/__tests__/vscode-lm.spec.ts:Testing
Fixes #6290
Important
Fixes token counting issue in
vscode-lm.tsby recalculating whentokenCountis 4, with tests added invscode-lm.spec.ts.internalCountTokensinvscode-lm.tsto handletokenCount === 4forLanguageModelChatMessageby converting to string and recalculating.tokenCountis 4 invscode-lm.spec.ts.tokenCountis not 4.This description was created by
for 6433d87. You can customize this summary. It will automatically update as commits are pushed.