Skip to content

Commit 6791c13

Browse files
committed
Handle binary files correctly in token estimation without range
- blobs should not be counted [Image] ├── apps │ └── editor │ └── src │ ├── context │ │ ├── context-initialization.ts │ │ └── providers │ │ └── workspace │ │ ├── modules │ │ │ └── token-calculator.ts │ │ └── workspace-provider.ts │ └── utils │ └── is-binary.ts └── packages └── shared └── src └── utils └── display-token-count.ts
1 parent 1678bba commit 6791c13

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎apps/editor/src/context/providers/workspace/modules/token-calculator.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,15 @@ export class TokenCalculator implements vscode.Disposable {
9292
}
9393
}
9494

95-
const wrapper_len =
96-
`### File: \`${display_path}\`\n\n\`\`\`\n\n\`\`\`\n\n`.length
97-
const estimated_tokens = Math.floor((file_size + wrapper_len) / 4)
95+
let estimated_tokens = 0
96+
if (is_binary_file(file_path)) {
97+
const wrapper = `### File: \`${display_path}\`\n\nBinary file\n\n`
98+
estimated_tokens = Math.floor(wrapper.length / 4)
99+
} else {
100+
const wrapper_len =
101+
`### File: \`${display_path}\`\n\n\`\`\`\n\n\`\`\`\n\n`.length
102+
estimated_tokens = Math.floor((file_size + wrapper_len) / 4)
103+
}
98104

99105
this._file_token_counts.set(file_path, estimated_tokens)
100106

0 commit comments

Comments
 (0)