@vaultcompass/vault-guard-core 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,22 +2,12 @@ import { TokenReport } from '../types';
|
|
|
2
2
|
export declare class TokenCounter {
|
|
3
3
|
private tokenRates;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* Real implementation would use tiktoken or similar
|
|
5
|
+
* Rough token estimate from file contents (heuristic, not tokenizer-accurate).
|
|
7
6
|
*/
|
|
8
7
|
countTokensInFile(filePath: string): number;
|
|
9
|
-
/**
|
|
10
|
-
* Estimate token count from text with improved accuracy
|
|
11
|
-
* Accounts for code density, symbols, and whitespace
|
|
12
|
-
*/
|
|
8
|
+
/** Heuristic token count from text (words + symbols; code gets a small bump). */
|
|
13
9
|
estimateTokens(text: string): number;
|
|
14
|
-
/**
|
|
15
|
-
* Calculate cost from token usage
|
|
16
|
-
*/
|
|
17
10
|
calculateCost(provider: 'anthropic' | 'openai', inputTokens: number, outputTokens: number): number;
|
|
18
|
-
/**
|
|
19
|
-
* Generate token report for a directory
|
|
20
|
-
*/
|
|
21
11
|
generateReport(directoryPath: string): TokenReport;
|
|
22
12
|
private getAllFiles;
|
|
23
13
|
private shouldIgnore;
|
|
@@ -18,8 +18,7 @@ class TokenCounter {
|
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
* Real implementation would use tiktoken or similar
|
|
21
|
+
* Rough token estimate from file contents (heuristic, not tokenizer-accurate).
|
|
23
22
|
*/
|
|
24
23
|
countTokensInFile(filePath) {
|
|
25
24
|
if (!fs_1.default.existsSync(filePath)) {
|
|
@@ -28,10 +27,7 @@ class TokenCounter {
|
|
|
28
27
|
const content = fs_1.default.readFileSync(filePath, 'utf-8');
|
|
29
28
|
return this.estimateTokens(content);
|
|
30
29
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Estimate token count from text with improved accuracy
|
|
33
|
-
* Accounts for code density, symbols, and whitespace
|
|
34
|
-
*/
|
|
30
|
+
/** Heuristic token count from text (words + symbols; code gets a small bump). */
|
|
35
31
|
estimateTokens(text) {
|
|
36
32
|
if (!text || text.length === 0) {
|
|
37
33
|
return 0;
|
|
@@ -39,37 +35,24 @@ class TokenCounter {
|
|
|
39
35
|
// Count whitespace-separated words
|
|
40
36
|
const words = text.split(/\s+/).filter(w => w.length > 0);
|
|
41
37
|
const wordCount = words.length;
|
|
42
|
-
// Count symbols and operators (more common in code)
|
|
43
38
|
const symbolMatches = text.match(/[{}[\]();,:.<>+\-*/%=|^&!~?]/g);
|
|
44
39
|
const symbolCount = symbolMatches ? symbolMatches.length : 0;
|
|
45
|
-
// Base estimate: words + symbols (rough approximation)
|
|
46
40
|
let tokenEstimate = wordCount + symbolCount;
|
|
47
|
-
// Adjust for code density (code typically has higher token/word ratio)
|
|
48
|
-
// If text has many symbols relative to words, it's likely code
|
|
49
41
|
const symbolToWordRatio = wordCount > 0 ? symbolCount / wordCount : 0;
|
|
50
42
|
if (symbolToWordRatio > 0.5) {
|
|
51
|
-
// Code-like content: increase estimate
|
|
52
43
|
tokenEstimate = Math.floor(tokenEstimate * 1.3);
|
|
53
44
|
}
|
|
54
45
|
else if (symbolToWordRatio < 0.1) {
|
|
55
|
-
// Natural language: decrease slightly (words are better tokens)
|
|
56
46
|
tokenEstimate = Math.floor(tokenEstimate * 0.9);
|
|
57
47
|
}
|
|
58
|
-
// Ensure minimum estimate
|
|
59
48
|
return Math.max(tokenEstimate, Math.ceil(text.length / 8));
|
|
60
49
|
}
|
|
61
|
-
/**
|
|
62
|
-
* Calculate cost from token usage
|
|
63
|
-
*/
|
|
64
50
|
calculateCost(provider, inputTokens, outputTokens) {
|
|
65
51
|
const rates = this.tokenRates[provider];
|
|
66
52
|
const inputCost = (inputTokens / 1_000_000) * rates.input;
|
|
67
53
|
const outputCost = (outputTokens / 1_000_000) * rates.output;
|
|
68
54
|
return inputCost + outputCost;
|
|
69
55
|
}
|
|
70
|
-
/**
|
|
71
|
-
* Generate token report for a directory
|
|
72
|
-
*/
|
|
73
56
|
generateReport(directoryPath) {
|
|
74
57
|
let totalTokens = 0;
|
|
75
58
|
const breakdown = {};
|
|
@@ -33,7 +33,7 @@ function isDocumentationPath(filePath) {
|
|
|
33
33
|
return true;
|
|
34
34
|
return false;
|
|
35
35
|
}
|
|
36
|
-
/** Placeholder shapes like `
|
|
36
|
+
/** Placeholder shapes like `your_example_api_key` in docs (no regex; bounded scan). */
|
|
37
37
|
function isYourUnderscoreKeyPlaceholder(value) {
|
|
38
38
|
const MAX = 256;
|
|
39
39
|
if (value.length > MAX || value.length < 10)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaultcompass/vault-guard-core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.2",
|
|
4
4
|
"description": "Secret-scanning engine: vendor-anchored patterns, entropy gating, baselines, SARIF/JSON, hook helpers.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|