@anthonymercer/contextscope 0.0.0-stage → 0.4.0
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.
- package/LICENSE +21 -0
- package/README.md +764 -2
- package/dist/cli/ansi.d.ts +31 -0
- package/dist/cli/ansi.js +80 -0
- package/dist/cli/args.d.ts +18 -0
- package/dist/cli/args.js +78 -0
- package/dist/cli/html-report.d.ts +2 -0
- package/dist/cli/html-report.js +119 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +138 -0
- package/dist/cli/json-report.d.ts +1 -0
- package/dist/cli/json-report.js +2 -0
- package/dist/cli/read-input.d.ts +3 -0
- package/dist/cli/read-input.js +43 -0
- package/dist/cli/terminal-report.d.ts +7 -0
- package/dist/cli/terminal-report.js +271 -0
- package/dist/cli/usage-html.d.ts +2 -0
- package/dist/cli/usage-html.js +33 -0
- package/dist/cli/version.d.ts +2 -0
- package/dist/cli/version.js +3 -0
- package/dist/cli/write-reports.d.ts +12 -0
- package/dist/cli/write-reports.js +82 -0
- package/dist/core/analyze.d.ts +13 -0
- package/dist/core/analyze.js +99 -0
- package/dist/core/anthropic-breakpoints.d.ts +6 -0
- package/dist/core/anthropic-breakpoints.js +48 -0
- package/dist/core/anthropic-cache-scope.d.ts +12 -0
- package/dist/core/anthropic-cache-scope.js +47 -0
- package/dist/core/anthropic-injected-settings.d.ts +14 -0
- package/dist/core/anthropic-injected-settings.js +57 -0
- package/dist/core/anthropic-reasoning.d.ts +13 -0
- package/dist/core/anthropic-reasoning.js +57 -0
- package/dist/core/anthropic-speed.d.ts +4 -0
- package/dist/core/anthropic-speed.js +27 -0
- package/dist/core/cache-anthropic.d.ts +2 -0
- package/dist/core/cache-anthropic.js +146 -0
- package/dist/core/cache-openai.d.ts +2 -0
- package/dist/core/cache-openai.js +72 -0
- package/dist/core/calibrate.d.ts +43 -0
- package/dist/core/calibrate.js +97 -0
- package/dist/core/decode-text.d.ts +9 -0
- package/dist/core/decode-text.js +21 -0
- package/dist/core/detect.d.ts +10 -0
- package/dist/core/detect.js +102 -0
- package/dist/core/duplicates.d.ts +12 -0
- package/dist/core/duplicates.js +153 -0
- package/dist/core/findings.d.ts +4 -0
- package/dist/core/findings.js +437 -0
- package/dist/core/group-findings.d.ts +23 -0
- package/dist/core/group-findings.js +69 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +2 -0
- package/dist/core/json-report.d.ts +10 -0
- package/dist/core/json-report.js +46 -0
- package/dist/core/json-utils.d.ts +28 -0
- package/dist/core/json-utils.js +99 -0
- package/dist/core/media-accounting.d.ts +11 -0
- package/dist/core/media-accounting.js +76 -0
- package/dist/core/model-id.d.ts +16 -0
- package/dist/core/model-id.js +36 -0
- package/dist/core/model-summary.d.ts +10 -0
- package/dist/core/model-summary.js +44 -0
- package/dist/core/openai-breakpoints.d.ts +14 -0
- package/dist/core/openai-breakpoints.js +107 -0
- package/dist/core/openai-context.d.ts +15 -0
- package/dist/core/openai-context.js +54 -0
- package/dist/core/parse-anthropic.d.ts +4 -0
- package/dist/core/parse-anthropic.js +284 -0
- package/dist/core/parse-error.d.ts +3 -0
- package/dist/core/parse-error.js +4 -0
- package/dist/core/parse-openai.d.ts +10 -0
- package/dist/core/parse-openai.js +273 -0
- package/dist/core/parse.d.ts +3 -0
- package/dist/core/parse.js +164 -0
- package/dist/core/prefix.d.ts +23 -0
- package/dist/core/prefix.js +290 -0
- package/dist/core/presentation.d.ts +4 -0
- package/dist/core/presentation.js +5 -0
- package/dist/core/pricing.d.ts +76 -0
- package/dist/core/pricing.js +130 -0
- package/dist/core/prompt-identity.d.ts +5 -0
- package/dist/core/prompt-identity.js +84 -0
- package/dist/core/read-json.d.ts +7 -0
- package/dist/core/read-json.js +74 -0
- package/dist/core/report.d.ts +2 -0
- package/dist/core/report.js +31 -0
- package/dist/core/reported-usage.d.ts +7 -0
- package/dist/core/reported-usage.js +195 -0
- package/dist/core/threads.d.ts +48 -0
- package/dist/core/threads.js +189 -0
- package/dist/core/token-counter.d.ts +13 -0
- package/dist/core/token-counter.js +21 -0
- package/dist/core/tokenize-claude.d.ts +18 -0
- package/dist/core/tokenize-claude.js +56 -0
- package/dist/core/tokenize-openai.d.ts +1 -0
- package/dist/core/tokenize-openai.js +15 -0
- package/dist/core/types.d.ts +326 -0
- package/dist/core/types.js +12 -0
- package/dist/core/usage-comparison.d.ts +2 -0
- package/dist/core/usage-comparison.js +70 -0
- package/dist/core/usage-labels.d.ts +5 -0
- package/dist/core/usage-labels.js +18 -0
- package/dist/core/volatile.d.ts +12 -0
- package/dist/core/volatile.js +31 -0
- package/package.json +75 -4
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export function compareReportedUsage(requests, reports, simulation) {
|
|
2
|
+
const rows = [];
|
|
3
|
+
const invalidRequestIndices = [];
|
|
4
|
+
const uncomparedRequestIndices = [];
|
|
5
|
+
let capturedRequests = 0;
|
|
6
|
+
const byReport = new Map(reports.map((report) => [report.requestIndex, report]));
|
|
7
|
+
const byStep = new Map(simulation.actual.map((step) => [step.requestIndex, step]));
|
|
8
|
+
for (const request of requests) {
|
|
9
|
+
const usage = request.reportedUsage;
|
|
10
|
+
if (usage)
|
|
11
|
+
capturedRequests++;
|
|
12
|
+
if (usage?.status === "invalid")
|
|
13
|
+
invalidRequestIndices.push(request.index);
|
|
14
|
+
if (!request.segments.length) {
|
|
15
|
+
if (usage)
|
|
16
|
+
uncomparedRequestIndices.push(request.index);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const report = byReport.get(request.index);
|
|
20
|
+
const step = byStep.get(request.index);
|
|
21
|
+
const input = usage?.inputTokens ?? null;
|
|
22
|
+
const read = usage?.cacheReadTokens ?? null;
|
|
23
|
+
const estimated = simulation.provider === "openai" ? report.totals.openaiTokens : report.totals.claudeTokensEstimate;
|
|
24
|
+
rows.push({
|
|
25
|
+
requestIndex: request.index,
|
|
26
|
+
estimatedInputTokens: estimated,
|
|
27
|
+
simulatedReadTokens: step.readTokens,
|
|
28
|
+
reportedInputTokens: input,
|
|
29
|
+
reportedReadTokens: read,
|
|
30
|
+
reportedWriteTokens: usage?.cacheWriteTokens ?? null,
|
|
31
|
+
inputDeltaTokens: input === null || estimated === null ? null : estimated - input,
|
|
32
|
+
readDeltaTokens: read === null || step.readTokens === null ? null : step.readTokens - read,
|
|
33
|
+
readOutcome: outcome(read, step.readTokens),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const issues = [];
|
|
37
|
+
const metric = (reported, simulated, label) => {
|
|
38
|
+
const covered = rows.filter((row) => row[reported] !== null);
|
|
39
|
+
const sum = (field) => {
|
|
40
|
+
if (!covered.length)
|
|
41
|
+
return null;
|
|
42
|
+
let total = 0;
|
|
43
|
+
for (const row of covered) {
|
|
44
|
+
if (row[field] === null)
|
|
45
|
+
return null;
|
|
46
|
+
total += row[field];
|
|
47
|
+
if (!Number.isSafeInteger(total)) {
|
|
48
|
+
issues.push(`${label}: ${field} aggregate exceeds the safe integer range; inspect individual requests.`);
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return total;
|
|
53
|
+
};
|
|
54
|
+
const reportedTokens = sum(reported);
|
|
55
|
+
const simulatedTokens = sum(simulated);
|
|
56
|
+
return { requestIndices: covered.map((row) => row.requestIndex), reportedTokens, simulatedTokens, deltaTokens: reportedTokens === null || simulatedTokens === null ? null : simulatedTokens - reportedTokens };
|
|
57
|
+
};
|
|
58
|
+
const readOutcomes = { both_zero: 0, both_positive: 0, simulated_hit_reported_zero: 0, reported_hit_simulated_zero: 0, unavailable: 0 };
|
|
59
|
+
for (const row of rows)
|
|
60
|
+
readOutcomes[row.readOutcome]++;
|
|
61
|
+
return { rows, capturedRequests, uncomparedRequestIndices, invalidRequestIndices, input: metric("reportedInputTokens", "estimatedInputTokens", "Input tokens"), cacheRead: metric("reportedReadTokens", "simulatedReadTokens", "Cache reads"), readOutcomes, issues };
|
|
62
|
+
}
|
|
63
|
+
function outcome(read, simulated) {
|
|
64
|
+
if (read === null || simulated === null)
|
|
65
|
+
return "unavailable";
|
|
66
|
+
if (read === 0)
|
|
67
|
+
return simulated === 0 ? "both_zero" : "simulated_hit_reported_zero";
|
|
68
|
+
return simulated === 0 ? "reported_hit_simulated_zero" : "both_positive";
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=usage-comparison.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ParsedRequest, UsageReadOutcome } from "./types.js";
|
|
2
|
+
export declare const USAGE_OUTCOME_LABEL: Record<UsageReadOutcome, string>;
|
|
3
|
+
export declare function usageSourceLabel(request: ParsedRequest): string;
|
|
4
|
+
export declare function usageNumber(value: number | null): string;
|
|
5
|
+
export declare function usageDelta(value: number | null): string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const USAGE_OUTCOME_LABEL = {
|
|
2
|
+
both_zero: "Both zero",
|
|
3
|
+
both_positive: "Reuse on both sides",
|
|
4
|
+
simulated_hit_reported_zero: "Simulated hit / reported zero",
|
|
5
|
+
reported_hit_simulated_zero: "Reported hit / simulated zero",
|
|
6
|
+
unavailable: "Read comparison unavailable",
|
|
7
|
+
};
|
|
8
|
+
export function usageSourceLabel(request) {
|
|
9
|
+
const source = request.source;
|
|
10
|
+
return source?.line !== undefined ? `line ${source.line}` : `record ${(source?.recordIndex ?? request.index) + 1}`;
|
|
11
|
+
}
|
|
12
|
+
export function usageNumber(value) {
|
|
13
|
+
return value === null ? "n/a" : value.toLocaleString("en-US");
|
|
14
|
+
}
|
|
15
|
+
export function usageDelta(value) {
|
|
16
|
+
return value === null ? "n/a" : `${value > 0 ? "+" : ""}${value.toLocaleString("en-US")}`;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=usage-labels.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redacts the substrings that most commonly break an otherwise-stable
|
|
3
|
+
* prefix - ISO-8601 timestamps, large epoch-looking integers, and UUIDs -
|
|
4
|
+
* replacing each with a fixed placeholder. Used ONLY to compute the
|
|
5
|
+
* "optimized" cache simulation (what the hit rate would look like if the
|
|
6
|
+
* volatile field were parameterized out of the cached prefix, e.g. moved to
|
|
7
|
+
* a per-request user message) and to drive the `volatile_prefix` finding.
|
|
8
|
+
* Never used for token counts or the real/actual simulation - those always
|
|
9
|
+
* use the literal text.
|
|
10
|
+
*/
|
|
11
|
+
export declare function stripVolatilePatterns(text: string): string;
|
|
12
|
+
export declare function containsVolatilePattern(text: string): boolean;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redacts the substrings that most commonly break an otherwise-stable
|
|
3
|
+
* prefix - ISO-8601 timestamps, large epoch-looking integers, and UUIDs -
|
|
4
|
+
* replacing each with a fixed placeholder. Used ONLY to compute the
|
|
5
|
+
* "optimized" cache simulation (what the hit rate would look like if the
|
|
6
|
+
* volatile field were parameterized out of the cached prefix, e.g. moved to
|
|
7
|
+
* a per-request user message) and to drive the `volatile_prefix` finding.
|
|
8
|
+
* Never used for token counts or the real/actual simulation - those always
|
|
9
|
+
* use the literal text.
|
|
10
|
+
*/
|
|
11
|
+
const ISO_TIMESTAMP = /\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:?\d{2})?/g;
|
|
12
|
+
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi;
|
|
13
|
+
const EPOCH_MS = /\b1[5-9]\d{11}\b/g; // 13-digit ms timestamps, roughly 2017-2286
|
|
14
|
+
const EPOCH_S = /\b1[5-9]\d{8}\b/g; // 10-digit second timestamps, roughly 2017-2286
|
|
15
|
+
export function stripVolatilePatterns(text) {
|
|
16
|
+
return text
|
|
17
|
+
.replace(ISO_TIMESTAMP, "\u0000TIMESTAMP\u0000")
|
|
18
|
+
.replace(UUID, "\u0000UUID\u0000")
|
|
19
|
+
.replace(EPOCH_MS, "\u0000EPOCH\u0000")
|
|
20
|
+
.replace(EPOCH_S, "\u0000EPOCH\u0000");
|
|
21
|
+
}
|
|
22
|
+
// Non-global copies for boolean checks: a `/g` regex's `.test()` is stateful (lastIndex
|
|
23
|
+
// persists across calls on the same instance), which would silently skip matches here.
|
|
24
|
+
const ISO_TIMESTAMP_ONCE = new RegExp(ISO_TIMESTAMP.source);
|
|
25
|
+
const UUID_ONCE = new RegExp(UUID.source, "i");
|
|
26
|
+
const EPOCH_MS_ONCE = new RegExp(EPOCH_MS.source);
|
|
27
|
+
const EPOCH_S_ONCE = new RegExp(EPOCH_S.source);
|
|
28
|
+
export function containsVolatilePattern(text) {
|
|
29
|
+
return ISO_TIMESTAMP_ONCE.test(text) || UUID_ONCE.test(text) || EPOCH_MS_ONCE.test(text) || EPOCH_S_ONCE.test(text);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=volatile.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anthonymercer/contextscope",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "See what's actually in your LLM context window, and why your prompt cache keeps missing.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": "Anthony Mercer",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://antonsoo.github.io/contextscope/",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/antonsoo/contextscope.git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/antonsoo/contextscope/issues"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"llm",
|
|
21
|
+
"anthropic",
|
|
22
|
+
"claude",
|
|
23
|
+
"openai",
|
|
24
|
+
"prompt-caching",
|
|
25
|
+
"tokenizer",
|
|
26
|
+
"developer-tools",
|
|
27
|
+
"cli"
|
|
28
|
+
],
|
|
29
|
+
"bin": {
|
|
30
|
+
"contextscope": "./dist/cli/index.js"
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/core/index.js",
|
|
33
|
+
"types": "./dist/core/index.d.ts",
|
|
34
|
+
"exports": {
|
|
35
|
+
".": {
|
|
36
|
+
"types": "./dist/core/index.d.ts",
|
|
37
|
+
"default": "./dist/core/index.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"!dist/**/*.map"
|
|
43
|
+
],
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=20"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"prepare": "npm run build:core",
|
|
49
|
+
"build:core": "tsc -p tsconfig.json",
|
|
50
|
+
"build:web": "vite build",
|
|
51
|
+
"build": "npm run build:core && npm run build:web",
|
|
52
|
+
"bench": "npm run build:core && node scripts/bench.mjs",
|
|
53
|
+
"dev:web": "vite",
|
|
54
|
+
"preview:web": "vite preview",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"test:watch": "vitest",
|
|
57
|
+
"lint": "eslint .",
|
|
58
|
+
"typecheck": "tsc -p tsconfig.json --noEmit && tsc -p web/tsconfig.json --noEmit",
|
|
59
|
+
"test:browser": "playwright test"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"gpt-tokenizer": "^4.0.0"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@axe-core/playwright": "4.13.0",
|
|
66
|
+
"@eslint/js": "^10.0.1",
|
|
67
|
+
"@playwright/test": "1.63.0",
|
|
68
|
+
"@types/node": "^26.6.2",
|
|
69
|
+
"diff": "9.0.0",
|
|
70
|
+
"eslint": "^10.11.0",
|
|
71
|
+
"js-tiktoken": "^1.0.21",
|
|
72
|
+
"typescript": "^6.0.3",
|
|
73
|
+
"typescript-eslint": "^8.70.1",
|
|
74
|
+
"vite": "^8.3.0",
|
|
75
|
+
"vitest": "^5.0.1"
|
|
76
|
+
}
|
|
77
|
+
}
|