@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,102 @@
|
|
|
1
|
+
import { isResponsesBody } from "./openai-context.js";
|
|
2
|
+
/**
|
|
3
|
+
* Auto-detects whether a single request object is Anthropic Messages API or
|
|
4
|
+
* OpenAI Chat Completions shaped. Scores a handful of shape-specific
|
|
5
|
+
* signals rather than requiring one exact marker, since real requests often
|
|
6
|
+
* carry only a subset (e.g. no tools, no system prompt).
|
|
7
|
+
*/
|
|
8
|
+
export function detectFormat(request) {
|
|
9
|
+
const scores = formatScores(request);
|
|
10
|
+
return scores.openai > scores.anthropic ? "openai" : "anthropic";
|
|
11
|
+
}
|
|
12
|
+
/** Strong provider evidence only: a bare user message works in either format. */
|
|
13
|
+
export function detectProvider(request) {
|
|
14
|
+
const scores = formatScores(request);
|
|
15
|
+
if (Math.max(scores.openai, scores.anthropic) < 2 || Math.abs(scores.openai - scores.anthropic) < 1)
|
|
16
|
+
return undefined;
|
|
17
|
+
return scores.openai > scores.anthropic ? "openai" : "anthropic";
|
|
18
|
+
}
|
|
19
|
+
function formatScores(request) {
|
|
20
|
+
const scores = { anthropic: 0, openai: 0 };
|
|
21
|
+
if (request === null || typeof request !== "object")
|
|
22
|
+
return scores;
|
|
23
|
+
const obj = request;
|
|
24
|
+
// The model id is the strongest signal a minimal request carries: the canonical Anthropic
|
|
25
|
+
// quickstart body ({model, max_tokens, messages:[{role:"user", content:"Hello"}]}) has nothing else.
|
|
26
|
+
const model = typeof obj["model"] === "string" ? obj["model"].toLowerCase() : "";
|
|
27
|
+
if (/^claude|(^|\.)anthropic\./.test(model))
|
|
28
|
+
scores.anthropic += 3;
|
|
29
|
+
if (/^(gpt-|chatgpt|o\d)/.test(model))
|
|
30
|
+
scores.openai += 3;
|
|
31
|
+
// Top-level `system` as a field is Anthropic-only; OpenAI has no such field.
|
|
32
|
+
if ("system" in obj)
|
|
33
|
+
scores.anthropic += 2;
|
|
34
|
+
if ("mcp_servers" in obj)
|
|
35
|
+
scores.anthropic += 2;
|
|
36
|
+
// OpenAI Responses API bodies carry `input`/`instructions` instead of `messages`.
|
|
37
|
+
if (isResponsesBody(obj))
|
|
38
|
+
scores.openai += 4;
|
|
39
|
+
const tools = Array.isArray(obj["tools"]) ? obj["tools"] : [];
|
|
40
|
+
for (const tool of tools) {
|
|
41
|
+
if (tool !== null && typeof tool === "object") {
|
|
42
|
+
const t = tool;
|
|
43
|
+
if ("input_schema" in t)
|
|
44
|
+
scores.anthropic += 2;
|
|
45
|
+
if (t["type"] === "mcp_toolset")
|
|
46
|
+
scores.anthropic += 2;
|
|
47
|
+
if (t["type"] === "mcp")
|
|
48
|
+
scores.openai += 2;
|
|
49
|
+
if (t["type"] === "function" && "function" in t)
|
|
50
|
+
scores.openai += 2;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const messages = Array.isArray(obj["messages"]) ? obj["messages"] : [];
|
|
54
|
+
// String content is shared by both providers. Repetition must not outweigh a model/system
|
|
55
|
+
// signal: a longer Anthropic history remains Anthropic.
|
|
56
|
+
if (messages.some((message) => message !== null && typeof message === "object" && typeof message["content"] === "string"))
|
|
57
|
+
scores.openai += 0.25;
|
|
58
|
+
messages.forEach((message, index) => {
|
|
59
|
+
if (message === null || typeof message !== "object")
|
|
60
|
+
return;
|
|
61
|
+
const m = message;
|
|
62
|
+
if (m["role"] === "tool")
|
|
63
|
+
scores.openai += 2;
|
|
64
|
+
if (m["role"] === "developer")
|
|
65
|
+
scores.openai += 2;
|
|
66
|
+
// A leading system message is the OpenAI convention. Anthropic accepts system messages only
|
|
67
|
+
// mid-conversation (never as messages[0]), so a later one says nothing about the provider.
|
|
68
|
+
if (m["role"] === "system" && index === 0)
|
|
69
|
+
scores.openai += 1;
|
|
70
|
+
if (Array.isArray(m["tool_calls"]))
|
|
71
|
+
scores.openai += 2;
|
|
72
|
+
const content = Array.isArray(m["content"]) ? m["content"] : [];
|
|
73
|
+
for (const block of content) {
|
|
74
|
+
if (block === null || typeof block !== "object")
|
|
75
|
+
continue;
|
|
76
|
+
const b = block;
|
|
77
|
+
switch (b["type"]) {
|
|
78
|
+
case "tool_use":
|
|
79
|
+
case "tool_result":
|
|
80
|
+
case "document":
|
|
81
|
+
case "thinking":
|
|
82
|
+
case "redacted_thinking":
|
|
83
|
+
scores.anthropic += 2;
|
|
84
|
+
break;
|
|
85
|
+
case "image_url":
|
|
86
|
+
case "input_audio":
|
|
87
|
+
case "refusal":
|
|
88
|
+
scores.openai += 2;
|
|
89
|
+
break;
|
|
90
|
+
case "image":
|
|
91
|
+
// Anthropic image blocks use {type:"image", source:{...}}; OpenAI uses {type:"image_url", image_url:{...}}.
|
|
92
|
+
if ("source" in b)
|
|
93
|
+
scores.anthropic += 2;
|
|
94
|
+
break;
|
|
95
|
+
default:
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return scores;
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=detect.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DuplicateGroup, ParsedRequest } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Inspect terminal requests of conversations, not copies of their shared history.
|
|
4
|
+
* Groups are a deterministic, greedy complete-link partition: every pair must
|
|
5
|
+
* meet the threshold. Similarity is not transitive; an A-B-C edit chain cannot
|
|
6
|
+
* establish that A and C are interchangeable. The partition is conservative and
|
|
7
|
+
* order-dependent, and need not enumerate every qualifying pair.
|
|
8
|
+
*
|
|
9
|
+
* Equal text is pooled before shingling/comparison. Actual strings establish
|
|
10
|
+
* identity; a short hash is only a display-independent debugging signature.
|
|
11
|
+
*/
|
|
12
|
+
export declare function findDuplicates(requests: ParsedRequest[], within?: number[]): DuplicateGroup[];
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { fnv1a } from "./json-utils.js";
|
|
2
|
+
const SHINGLE_SIZE = 5;
|
|
3
|
+
const MIN_CHARS = 200;
|
|
4
|
+
const SIMILARITY_THRESHOLD = 0.85;
|
|
5
|
+
const ELIGIBLE_CATEGORIES = new Set(["tool_result", "user", "assistant", "system"]);
|
|
6
|
+
function shingles(text) {
|
|
7
|
+
const words = text.trim().split(/\s+/);
|
|
8
|
+
if (words.length < SHINGLE_SIZE)
|
|
9
|
+
return new Set([words.join(" ")]);
|
|
10
|
+
const set = new Set();
|
|
11
|
+
for (let i = 0; i + SHINGLE_SIZE <= words.length; i++)
|
|
12
|
+
set.add(words.slice(i, i + SHINGLE_SIZE).join(" "));
|
|
13
|
+
return set;
|
|
14
|
+
}
|
|
15
|
+
function similarity(a, b) {
|
|
16
|
+
const [small, large] = a.size <= b.size ? [a, b] : [b, a];
|
|
17
|
+
// Even complete containment cannot meet the threshold in this case.
|
|
18
|
+
if (small.size < SIMILARITY_THRESHOLD * large.size)
|
|
19
|
+
return 0;
|
|
20
|
+
let intersection = 0;
|
|
21
|
+
for (const value of small)
|
|
22
|
+
if (large.has(value))
|
|
23
|
+
intersection++;
|
|
24
|
+
const union = a.size + b.size - intersection;
|
|
25
|
+
return union === 0 ? 1 : intersection / union;
|
|
26
|
+
}
|
|
27
|
+
/** Necessary-match filter, not an approximate similarity score. */
|
|
28
|
+
class ClusterCandidates {
|
|
29
|
+
frequency = new Map();
|
|
30
|
+
postings = new Map();
|
|
31
|
+
constructor(contents) {
|
|
32
|
+
for (const content of contents)
|
|
33
|
+
for (const gram of content.shingles) {
|
|
34
|
+
this.frequency.set(gram, (this.frequency.get(gram) ?? 0) + 1);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
prefix(grams) {
|
|
38
|
+
// One total order for this request; rare shingles first keep common prose
|
|
39
|
+
// out of the index where possible. Lexical ties must NOT use per-text order.
|
|
40
|
+
const sorted = [...grams].sort((a, b) => this.frequency.get(a) - this.frequency.get(b) || (a < b ? -1 : a > b ? 1 : 0));
|
|
41
|
+
// A matching pair must share a prefix shingle: otherwise its first shared
|
|
42
|
+
// shingle is past one prefix, leaving too few shared elements for Jaccard
|
|
43
|
+
// >= t. floor (rather than ceil) keeps an extra element at nonintegral
|
|
44
|
+
// thresholds, conservatively avoiding floating-point boundary exclusions.
|
|
45
|
+
return sorted.slice(0, grams.size - Math.floor(SIMILARITY_THRESHOLD * grams.size) + 1);
|
|
46
|
+
}
|
|
47
|
+
candidates(prefix, clusters) {
|
|
48
|
+
const ids = new Set();
|
|
49
|
+
for (const gram of prefix)
|
|
50
|
+
for (const id of this.postings.get(gram) ?? [])
|
|
51
|
+
ids.add(id);
|
|
52
|
+
// Preserve the first-compatible-cluster rule, independently of index order.
|
|
53
|
+
return [...ids].sort((a, b) => a - b).map(id => clusters[id]);
|
|
54
|
+
}
|
|
55
|
+
add(prefix, clusterId) {
|
|
56
|
+
// Only index the first content of each cluster. Joining a complete-link
|
|
57
|
+
// cluster necessarily requires matching that representative; every member
|
|
58
|
+
// is still checked below before accepting the candidate.
|
|
59
|
+
for (const gram of prefix) {
|
|
60
|
+
const ids = this.postings.get(gram);
|
|
61
|
+
if (ids)
|
|
62
|
+
ids.push(clusterId);
|
|
63
|
+
else
|
|
64
|
+
this.postings.set(gram, [clusterId]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Inspect terminal requests of conversations, not copies of their shared history.
|
|
70
|
+
* Groups are a deterministic, greedy complete-link partition: every pair must
|
|
71
|
+
* meet the threshold. Similarity is not transitive; an A-B-C edit chain cannot
|
|
72
|
+
* establish that A and C are interchangeable. The partition is conservative and
|
|
73
|
+
* order-dependent, and need not enumerate every qualifying pair.
|
|
74
|
+
*
|
|
75
|
+
* Equal text is pooled before shingling/comparison. Actual strings establish
|
|
76
|
+
* identity; a short hash is only a display-independent debugging signature.
|
|
77
|
+
*/
|
|
78
|
+
export function findDuplicates(requests, within = requests.length > 0 ? [requests.length - 1] : []) {
|
|
79
|
+
const out = [];
|
|
80
|
+
const textIds = new Map();
|
|
81
|
+
const identify = (text) => {
|
|
82
|
+
let id = textIds.get(text);
|
|
83
|
+
if (id === undefined) {
|
|
84
|
+
id = textIds.size;
|
|
85
|
+
textIds.set(text, id);
|
|
86
|
+
}
|
|
87
|
+
return id;
|
|
88
|
+
};
|
|
89
|
+
// The same content in the same positions of separate conversations is one finding.
|
|
90
|
+
const seen = new Set();
|
|
91
|
+
for (const index of [...within].sort((a, b) => a - b)) {
|
|
92
|
+
const request = requests[index];
|
|
93
|
+
if (!request)
|
|
94
|
+
continue;
|
|
95
|
+
const unique = new Map();
|
|
96
|
+
request.segments.forEach((segment, ordinal) => {
|
|
97
|
+
if (segment.claudeTokensEstimate === null || !ELIGIBLE_CATEGORIES.has(segment.category) || segment.charLength < MIN_CHARS)
|
|
98
|
+
return;
|
|
99
|
+
const id = identify(segment.text);
|
|
100
|
+
let content = unique.get(id);
|
|
101
|
+
if (!content) {
|
|
102
|
+
content = { id, shingles: shingles(segment.text), members: [] };
|
|
103
|
+
unique.set(id, content);
|
|
104
|
+
}
|
|
105
|
+
content.members.push({ requestIndex: request.index, segmentId: segment.id, label: segment.label, tokens: segment.claudeTokensEstimate, ordinal });
|
|
106
|
+
});
|
|
107
|
+
const clusters = [];
|
|
108
|
+
// Small histories don't warrant building and sorting an index.
|
|
109
|
+
const candidateIndex = unique.size >= 64 ? new ClusterCandidates(unique.values()) : undefined;
|
|
110
|
+
for (const content of unique.values()) {
|
|
111
|
+
const prefix = candidateIndex?.prefix(content.shingles);
|
|
112
|
+
const candidates = prefix ? candidateIndex.candidates(prefix, clusters) : clusters;
|
|
113
|
+
let joined = false;
|
|
114
|
+
for (const cluster of candidates) {
|
|
115
|
+
let minimum = cluster.minimum;
|
|
116
|
+
const compatible = cluster.contents.every((other) => {
|
|
117
|
+
const score = similarity(content.shingles, other.shingles);
|
|
118
|
+
minimum = Math.min(minimum, score);
|
|
119
|
+
return score >= SIMILARITY_THRESHOLD;
|
|
120
|
+
});
|
|
121
|
+
if (!compatible)
|
|
122
|
+
continue;
|
|
123
|
+
cluster.contents.push(content);
|
|
124
|
+
cluster.minimum = minimum;
|
|
125
|
+
joined = true;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
if (!joined) {
|
|
129
|
+
if (prefix)
|
|
130
|
+
candidateIndex.add(prefix, clusters.length);
|
|
131
|
+
clusters.push({ contents: [content], minimum: 1 });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (const cluster of clusters) {
|
|
135
|
+
const members = cluster.contents.flatMap((content) => content.members).sort((a, b) => a.ordinal - b.ordinal);
|
|
136
|
+
if (members.length < 2)
|
|
137
|
+
continue;
|
|
138
|
+
const identity = JSON.stringify(cluster.contents.flatMap((content) => content.members.map((m) => [m.segmentId, content.id])).sort());
|
|
139
|
+
if (seen.has(identity))
|
|
140
|
+
continue;
|
|
141
|
+
seen.add(identity);
|
|
142
|
+
out.push({
|
|
143
|
+
signature: fnv1a(members.map((m) => m.segmentId).sort().join("|")),
|
|
144
|
+
members: members.map((m) => ({ requestIndex: m.requestIndex, segmentId: m.segmentId, label: m.label, tokens: m.tokens })),
|
|
145
|
+
matchKind: cluster.contents.length === 1 ? "exact" : "near",
|
|
146
|
+
similarity: cluster.minimum,
|
|
147
|
+
estimatedReviewTokens: members.slice(1).reduce((sum, member) => sum + member.tokens, 0),
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return out.sort((a, b) => b.estimatedReviewTokens - a.estimatedReviewTokens);
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=duplicates.js.map
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CacheSimulation, DuplicateGroup, Finding, ParsedRequest, PrefixMatch, Provider } from "./types.js";
|
|
2
|
+
/** Only the window of positions that moved: "positions 2-3: [read_file, grep_search] → [grep_search, read_file]". */
|
|
3
|
+
export declare function describeReorder(before: string[], after: string[]): string;
|
|
4
|
+
export declare function computeFindings(provider: Provider, requests: ParsedRequest[], prefixMatches: PrefixMatch[], model: string | undefined, duplicates: DuplicateGroup[], cacheSimulation: CacheSimulation): Finding[];
|