@aiready/pattern-detect 0.16.19 → 0.16.21

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.
Files changed (56) hide show
  1. package/dist/analyzer-entry/index.d.mts +3 -0
  2. package/dist/analyzer-entry/index.d.ts +3 -0
  3. package/dist/analyzer-entry/index.js +693 -0
  4. package/dist/analyzer-entry/index.mjs +12 -0
  5. package/dist/analyzer-entry.d.mts +100 -3
  6. package/dist/analyzer-entry.d.ts +100 -3
  7. package/dist/analyzer-entry.js +9 -126
  8. package/dist/analyzer-entry.mjs +2 -2
  9. package/dist/chunk-65UQ5J2J.mjs +64 -0
  10. package/dist/chunk-6JTVOBJX.mjs +64 -0
  11. package/dist/chunk-BKRPSTT2.mjs +64 -0
  12. package/dist/chunk-CMWW24HW.mjs +259 -0
  13. package/dist/chunk-DNZS4ESD.mjs +391 -0
  14. package/dist/chunk-GLKAGFKX.mjs +391 -0
  15. package/dist/chunk-GREN7X5H.mjs +143 -0
  16. package/dist/chunk-JBUZ6YHE.mjs +391 -0
  17. package/dist/chunk-KWMNN3TG.mjs +391 -0
  18. package/dist/chunk-LYKRYBSM.mjs +64 -0
  19. package/dist/chunk-MHU3CL4R.mjs +64 -0
  20. package/dist/chunk-RS73WLNI.mjs +251 -0
  21. package/dist/chunk-SVCSIZ2A.mjs +259 -0
  22. package/dist/chunk-VGMM3L3O.mjs +143 -0
  23. package/dist/chunk-XNPID6FU.mjs +391 -0
  24. package/dist/cli.js +29 -147
  25. package/dist/cli.mjs +27 -25
  26. package/dist/context-rules-entry/index.d.mts +2 -0
  27. package/dist/context-rules-entry/index.d.ts +2 -0
  28. package/dist/context-rules-entry/index.js +207 -0
  29. package/dist/context-rules-entry/index.mjs +12 -0
  30. package/dist/context-rules-entry.d.mts +55 -2
  31. package/dist/context-rules-entry.d.ts +55 -2
  32. package/dist/detector-entry/index.d.mts +14 -0
  33. package/dist/detector-entry/index.d.ts +14 -0
  34. package/dist/detector-entry/index.js +301 -0
  35. package/dist/detector-entry/index.mjs +7 -0
  36. package/dist/detector-entry.d.mts +2 -2
  37. package/dist/detector-entry.d.ts +2 -2
  38. package/dist/detector-entry.js +9 -126
  39. package/dist/detector-entry.mjs +1 -1
  40. package/dist/index-BVz-HnZd.d.mts +119 -0
  41. package/dist/index-BwuoiCNm.d.ts +119 -0
  42. package/dist/index-y2uJSngh.d.mts +60 -0
  43. package/dist/index-y2uJSngh.d.ts +60 -0
  44. package/dist/index.d.mts +4 -4
  45. package/dist/index.d.ts +4 -4
  46. package/dist/index.js +9 -126
  47. package/dist/index.mjs +3 -3
  48. package/dist/scoring-entry/index.d.mts +23 -0
  49. package/dist/scoring-entry/index.d.ts +23 -0
  50. package/dist/scoring-entry/index.js +133 -0
  51. package/dist/scoring-entry/index.mjs +6 -0
  52. package/dist/scoring-entry.d.mts +1 -1
  53. package/dist/scoring-entry.d.ts +1 -1
  54. package/dist/types-C4lmb2Yh.d.mts +36 -0
  55. package/dist/types-C4lmb2Yh.d.ts +36 -0
  56. package/package.json +16 -16
@@ -0,0 +1,119 @@
1
+ import { Severity, ScanOptions, AnalysisResult } from '@aiready/core';
2
+ import { P as PatternType, D as DuplicatePattern } from './types-DU2mmhwb.js';
3
+
4
+ interface DuplicateGroup {
5
+ filePair: string;
6
+ severity: Severity;
7
+ occurrences: number;
8
+ totalTokenCost: number;
9
+ averageSimilarity: number;
10
+ patternTypes: Set<PatternType>;
11
+ lineRanges: Array<{
12
+ file1: {
13
+ start: number;
14
+ end: number;
15
+ };
16
+ file2: {
17
+ start: number;
18
+ end: number;
19
+ };
20
+ }>;
21
+ }
22
+ interface RefactorCluster {
23
+ id: string;
24
+ name: string;
25
+ files: string[];
26
+ severity: Severity;
27
+ duplicateCount: number;
28
+ totalTokenCost: number;
29
+ averageSimilarity: number;
30
+ reason?: string;
31
+ suggestion?: string;
32
+ }
33
+ /**
34
+ * Group raw duplicates by file pairs to reduce noise
35
+ */
36
+ declare function groupDuplicatesByFilePair(duplicates: DuplicatePattern[]): DuplicateGroup[];
37
+ /**
38
+ * Create clusters of highly related files (refactor targets)
39
+ * Uses a simple connected components algorithm
40
+ * @param duplicates - Array of duplicate patterns to cluster
41
+ * @returns Array of refactor clusters
42
+ */
43
+ declare function createRefactorClusters(duplicates: DuplicatePattern[]): RefactorCluster[];
44
+ /**
45
+ * Filter clusters by impact threshold
46
+ * @param clusters - Array of refactor clusters to filter
47
+ * @param minTokenCost - Minimum token cost threshold (default: 1000)
48
+ * @param minFiles - Minimum number of files in cluster (default: 3)
49
+ * @returns Filtered array of refactor clusters
50
+ */
51
+ declare function filterClustersByImpact(clusters: RefactorCluster[], minTokenCost?: number, minFiles?: number): RefactorCluster[];
52
+
53
+ interface PatternDetectOptions extends ScanOptions {
54
+ minSimilarity?: number;
55
+ minLines?: number;
56
+ batchSize?: number;
57
+ approx?: boolean;
58
+ minSharedTokens?: number;
59
+ maxCandidatesPerBlock?: number;
60
+ streamResults?: boolean;
61
+ severity?: string;
62
+ includeTests?: boolean;
63
+ useSmartDefaults?: boolean;
64
+ groupByFilePair?: boolean;
65
+ createClusters?: boolean;
66
+ minClusterTokenCost?: number;
67
+ minClusterFiles?: number;
68
+ excludePatterns?: string[];
69
+ confidenceThreshold?: number;
70
+ ignoreWhitelist?: string[];
71
+ onProgress?: (processed: number, total: number, message: string) => void;
72
+ }
73
+ interface PatternSummary {
74
+ totalPatterns: number;
75
+ totalTokenCost: number;
76
+ patternsByType: Record<PatternType, number>;
77
+ topDuplicates: Array<{
78
+ files: Array<{
79
+ path: string;
80
+ startLine: number;
81
+ endLine: number;
82
+ }>;
83
+ similarity: number;
84
+ patternType: PatternType;
85
+ tokenCost: number;
86
+ }>;
87
+ }
88
+ /**
89
+ * Determine smart defaults based on repository size estimation.
90
+ *
91
+ * @param directory - The directory to analyze for size.
92
+ * @param userOptions - User-provided option overrides.
93
+ * @returns Promise resolving to optimal detection options.
94
+ */
95
+ declare function getSmartDefaults(directory: string, userOptions: Partial<PatternDetectOptions>): Promise<PatternDetectOptions>;
96
+ /**
97
+ * Main entry point for pattern detection analysis.
98
+ *
99
+ * @param options - Configuration including rootDir and detection parameters.
100
+ * @returns Promise resolving to the comprehensive pattern detect report.
101
+ * @lastUpdated 2026-03-18
102
+ */
103
+ declare function analyzePatterns(options: PatternDetectOptions): Promise<{
104
+ results: AnalysisResult[];
105
+ duplicates: DuplicatePattern[];
106
+ files: string[];
107
+ groups?: DuplicateGroup[];
108
+ clusters?: RefactorCluster[];
109
+ config: PatternDetectOptions;
110
+ }>;
111
+ /**
112
+ * Generate a summary of pattern detection results.
113
+ *
114
+ * @param results - Array of file-level analysis results.
115
+ * @returns Consolidated pattern summary object.
116
+ */
117
+ declare function generateSummary(results: AnalysisResult[]): PatternSummary;
118
+
119
+ export { type DuplicateGroup as D, type PatternDetectOptions as P, type RefactorCluster as R, type PatternSummary as a, analyzePatterns as b, createRefactorClusters as c, getSmartDefaults as d, groupDuplicatesByFilePair as e, filterClustersByImpact as f, generateSummary as g };
@@ -0,0 +1,60 @@
1
+ import { Severity } from '@aiready/core';
2
+
3
+ /**
4
+ * Context-aware severity detection for duplicate patterns
5
+ * Identifies intentional duplication patterns and adjusts severity accordingly
6
+ */
7
+ interface ContextRule {
8
+ name: string;
9
+ detect: (file: string, code: string) => boolean;
10
+ severity: Severity;
11
+ reason: string;
12
+ suggestion?: string;
13
+ }
14
+ /**
15
+ * Context rules for detecting intentional or acceptable duplication patterns
16
+ * Rules are checked in order - first match wins
17
+ */
18
+ declare const CONTEXT_RULES: ContextRule[];
19
+ /**
20
+ * Calculate severity based on context rules and code characteristics
21
+ *
22
+ * @param file1 - First file path in the duplicate pair.
23
+ * @param file2 - Second file path in the duplicate pair.
24
+ * @param code - Snippet of the duplicated code.
25
+ * @param similarity - The calculated similarity score (0-1).
26
+ * @param linesOfCode - Number of lines in the duplicated block.
27
+ * @returns An object containing the severity level and reasoning.
28
+ */
29
+ declare function calculateSeverity(file1: string, file2: string, code: string, similarity: number, linesOfCode: number): {
30
+ severity: Severity;
31
+ reason?: string;
32
+ suggestion?: string;
33
+ matchedRule?: string;
34
+ };
35
+ /**
36
+ * Get a human-readable severity label with emoji
37
+ *
38
+ * @param severity - The severity level to label.
39
+ * @returns Formatted label string for UI display.
40
+ */
41
+ declare function getSeverityLabel(severity: Severity): string;
42
+ /**
43
+ * Filter duplicates by minimum severity threshold
44
+ *
45
+ * @param duplicates - List of items with a severity property.
46
+ * @param minSeverity - Minimum threshold for inclusion.
47
+ * @returns Filtered list of items.
48
+ */
49
+ declare function filterBySeverity<T extends {
50
+ severity: Severity;
51
+ }>(duplicates: T[], minSeverity: Severity): T[];
52
+ /**
53
+ * Get numerical similarity threshold associated with a severity level
54
+ *
55
+ * @param severity - The severity level to look up.
56
+ * @returns Minimum similarity value for this severity.
57
+ */
58
+ declare function getSeverityThreshold(severity: Severity): number;
59
+
60
+ export { CONTEXT_RULES as C, type ContextRule as a, getSeverityThreshold as b, calculateSeverity as c, filterBySeverity as f, getSeverityLabel as g };
@@ -0,0 +1,60 @@
1
+ import { Severity } from '@aiready/core';
2
+
3
+ /**
4
+ * Context-aware severity detection for duplicate patterns
5
+ * Identifies intentional duplication patterns and adjusts severity accordingly
6
+ */
7
+ interface ContextRule {
8
+ name: string;
9
+ detect: (file: string, code: string) => boolean;
10
+ severity: Severity;
11
+ reason: string;
12
+ suggestion?: string;
13
+ }
14
+ /**
15
+ * Context rules for detecting intentional or acceptable duplication patterns
16
+ * Rules are checked in order - first match wins
17
+ */
18
+ declare const CONTEXT_RULES: ContextRule[];
19
+ /**
20
+ * Calculate severity based on context rules and code characteristics
21
+ *
22
+ * @param file1 - First file path in the duplicate pair.
23
+ * @param file2 - Second file path in the duplicate pair.
24
+ * @param code - Snippet of the duplicated code.
25
+ * @param similarity - The calculated similarity score (0-1).
26
+ * @param linesOfCode - Number of lines in the duplicated block.
27
+ * @returns An object containing the severity level and reasoning.
28
+ */
29
+ declare function calculateSeverity(file1: string, file2: string, code: string, similarity: number, linesOfCode: number): {
30
+ severity: Severity;
31
+ reason?: string;
32
+ suggestion?: string;
33
+ matchedRule?: string;
34
+ };
35
+ /**
36
+ * Get a human-readable severity label with emoji
37
+ *
38
+ * @param severity - The severity level to label.
39
+ * @returns Formatted label string for UI display.
40
+ */
41
+ declare function getSeverityLabel(severity: Severity): string;
42
+ /**
43
+ * Filter duplicates by minimum severity threshold
44
+ *
45
+ * @param duplicates - List of items with a severity property.
46
+ * @param minSeverity - Minimum threshold for inclusion.
47
+ * @returns Filtered list of items.
48
+ */
49
+ declare function filterBySeverity<T extends {
50
+ severity: Severity;
51
+ }>(duplicates: T[], minSeverity: Severity): T[];
52
+ /**
53
+ * Get numerical similarity threshold associated with a severity level
54
+ *
55
+ * @param severity - The severity level to look up.
56
+ * @returns Minimum similarity value for this severity.
57
+ */
58
+ declare function getSeverityThreshold(severity: Severity): number;
59
+
60
+ export { CONTEXT_RULES as C, type ContextRule as a, getSeverityThreshold as b, calculateSeverity as c, filterBySeverity as f, getSeverityLabel as g };
package/dist/index.d.mts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { ToolProvider } from '@aiready/core';
2
2
  export { Severity } from '@aiready/core';
3
- export { D as DuplicateGroup, P as PatternDetectOptions, a as PatternSummary, R as RefactorCluster, b as analyzePatterns, c as createRefactorClusters, f as filterClustersByImpact, g as generateSummary, d as getSmartDefaults, e as groupDuplicatesByFilePair } from './analyzer-entry-BVz-HnZd.mjs';
4
- export { detectDuplicatePatterns } from './detector-entry.mjs';
5
- export { calculatePatternScore } from './scoring-entry.mjs';
6
- export { C as CONTEXT_RULES, a as ContextRule, c as calculateSeverity, f as filterBySeverity, g as getSeverityLabel, b as getSeverityThreshold } from './context-rules-entry-y2uJSngh.mjs';
3
+ export { D as DuplicateGroup, P as PatternDetectOptions, a as PatternSummary, R as RefactorCluster, b as analyzePatterns, c as createRefactorClusters, f as filterClustersByImpact, g as generateSummary, d as getSmartDefaults, e as groupDuplicatesByFilePair } from './index-BVz-HnZd.mjs';
4
+ export { detectDuplicatePatterns } from './detector-entry/index.mjs';
5
+ export { calculatePatternScore } from './scoring-entry/index.mjs';
6
+ export { C as CONTEXT_RULES, a as ContextRule, c as calculateSeverity, f as filterBySeverity, g as getSeverityLabel, b as getSeverityThreshold } from './index-y2uJSngh.mjs';
7
7
  export { D as DuplicatePattern, P as PatternType } from './types-DU2mmhwb.mjs';
8
8
 
9
9
  /**
package/dist/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { ToolProvider } from '@aiready/core';
2
2
  export { Severity } from '@aiready/core';
3
- export { D as DuplicateGroup, P as PatternDetectOptions, a as PatternSummary, R as RefactorCluster, b as analyzePatterns, c as createRefactorClusters, f as filterClustersByImpact, g as generateSummary, d as getSmartDefaults, e as groupDuplicatesByFilePair } from './analyzer-entry-BwuoiCNm.js';
4
- export { detectDuplicatePatterns } from './detector-entry.js';
5
- export { calculatePatternScore } from './scoring-entry.js';
6
- export { C as CONTEXT_RULES, a as ContextRule, c as calculateSeverity, f as filterBySeverity, g as getSeverityLabel, b as getSeverityThreshold } from './context-rules-entry-y2uJSngh.js';
3
+ export { D as DuplicateGroup, P as PatternDetectOptions, a as PatternSummary, R as RefactorCluster, b as analyzePatterns, c as createRefactorClusters, f as filterClustersByImpact, g as generateSummary, d as getSmartDefaults, e as groupDuplicatesByFilePair } from './index-BwuoiCNm.js';
4
+ export { detectDuplicatePatterns } from './detector-entry/index.js';
5
+ export { calculatePatternScore } from './scoring-entry/index.js';
6
+ export { C as CONTEXT_RULES, a as ContextRule, c as calculateSeverity, f as filterBySeverity, g as getSeverityLabel, b as getSeverityThreshold } from './index-y2uJSngh.js';
7
7
  export { D as DuplicatePattern, P as PatternType } from './types-DU2mmhwb.js';
8
8
 
9
9
  /**
package/dist/index.js CHANGED
@@ -230,144 +230,27 @@ function getSeverityThreshold(severity) {
230
230
  return thresholds[severity] || 0;
231
231
  }
232
232
 
233
- // src/detector.ts
233
+ // src/core/normalizer.ts
234
234
  function normalizeCode(code, isPython = false) {
235
+ if (!code) return "";
235
236
  let normalized = code;
236
237
  if (isPython) {
237
238
  normalized = normalized.replace(/#.*/g, "");
238
239
  } else {
239
- normalized = normalized.replace(/\/\/.*/g, "").replace(/\/\*[\s\S]*?\*\//g, "");
240
+ normalized = normalized.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
240
241
  }
241
- return normalized.replace(/['"`]/g, '"').replace(/\s+/g, " ").trim().toLowerCase();
242
+ return normalized.replace(/"[^"]*"/g, '"STR"').replace(/'[^']*'/g, "'STR'").replace(/`[^`]*`/g, "`STR`").replace(/\b\d+\b/g, "NUM").replace(/\s+/g, " ").trim().toLowerCase();
242
243
  }
244
+
245
+ // src/detector.ts
243
246
  function extractBlocks(file, content) {
244
- const isPython = file.toLowerCase().endsWith(".py");
245
- if (isPython) {
246
- return extractBlocksPython(file, content);
247
- }
248
- const blocks = [];
249
- const lines = content.split("\n");
250
- const blockRegex = /^\s*(?:export\s+)?(?:async\s+)?(?:public\s+|private\s+|protected\s+|internal\s+|static\s+|readonly\s+|virtual\s+|abstract\s+|override\s+)*(function|class|interface|type|enum|record|struct|void|func|[a-zA-Z0-9_<>[]]+)\s+([a-zA-Z0-9_]+)(?:\s*\(|(?:\s+extends|\s+implements|\s+where)?\s*\{)|^\s*(?:export\s+)?const\s+([a-zA-Z0-9_]+)\s*=\s*[a-zA-Z0-9_.]+\.object\(|^\s*(app\.(?:get|post|put|delete|patch|use))\(/gm;
251
- let match;
252
- while ((match = blockRegex.exec(content)) !== null) {
253
- const startLine = content.substring(0, match.index).split("\n").length;
254
- let type;
255
- let name;
256
- if (match[1]) {
257
- type = match[1];
258
- name = match[2];
259
- } else if (match[3]) {
260
- type = "const";
261
- name = match[3];
262
- } else {
263
- type = "handler";
264
- name = match[4];
265
- }
266
- let endLine = -1;
267
- let openBraces = 0;
268
- let foundStart = false;
269
- for (let i = match.index; i < content.length; i++) {
270
- if (content[i] === "{") {
271
- openBraces++;
272
- foundStart = true;
273
- } else if (content[i] === "}") {
274
- openBraces--;
275
- }
276
- if (foundStart && openBraces === 0) {
277
- endLine = content.substring(0, i + 1).split("\n").length;
278
- break;
279
- }
280
- }
281
- if (endLine === -1) {
282
- const remaining = content.slice(match.index);
283
- const nextLineMatch = remaining.indexOf("\n");
284
- if (nextLineMatch !== -1) {
285
- endLine = startLine;
286
- } else {
287
- endLine = lines.length;
288
- }
289
- }
290
- endLine = Math.max(startLine, endLine);
291
- const blockCode = lines.slice(startLine - 1, endLine).join("\n");
292
- const tokens = (0, import_core2.estimateTokens)(blockCode);
293
- blocks.push({
294
- file,
295
- startLine,
296
- endLine,
297
- code: blockCode,
298
- tokens,
299
- patternType: inferPatternType(type, name)
300
- });
301
- }
302
- return blocks;
303
- }
304
- function extractBlocksPython(file, content) {
305
- const blocks = [];
306
- const lines = content.split("\n");
307
- const blockRegex = /^\s*(?:async\s+)?(def|class)\s+([a-zA-Z0-9_]+)/gm;
308
- let match;
309
- while ((match = blockRegex.exec(content)) !== null) {
310
- const startLinePos = content.substring(0, match.index).split("\n").length;
311
- const startLineIdx = startLinePos - 1;
312
- const initialIndent = lines[startLineIdx].search(/\S/);
313
- let endLineIdx = startLineIdx;
314
- for (let i = startLineIdx + 1; i < lines.length; i++) {
315
- const line = lines[i];
316
- if (line.trim().length === 0) {
317
- endLineIdx = i;
318
- continue;
319
- }
320
- const currentIndent = line.search(/\S/);
321
- if (currentIndent <= initialIndent) {
322
- break;
323
- }
324
- endLineIdx = i;
325
- }
326
- while (endLineIdx > startLineIdx && lines[endLineIdx].trim().length === 0) {
327
- endLineIdx--;
328
- }
329
- const blockCode = lines.slice(startLineIdx, endLineIdx + 1).join("\n");
330
- const tokens = (0, import_core2.estimateTokens)(blockCode);
331
- blocks.push({
332
- file,
333
- startLine: startLinePos,
334
- endLine: endLineIdx + 1,
335
- code: blockCode,
336
- tokens,
337
- patternType: inferPatternType(match[1], match[2])
338
- });
339
- }
340
- return blocks;
341
- }
342
- function inferPatternType(keyword, name) {
343
- const n = name.toLowerCase();
344
- if (keyword === "handler" || n.includes("handler") || n.includes("controller") || n.startsWith("app.")) {
345
- return "api-handler";
346
- }
347
- if (n.includes("validate") || n.includes("schema")) return "validator";
348
- if (n.includes("util") || n.includes("helper")) return "utility";
349
- if (keyword === "class") return "class-method";
350
- if (n.match(/^[A-Z]/)) return "component";
351
- if (keyword === "function") return "function";
352
- return "unknown";
247
+ return (0, import_core2.extractCodeBlocks)(file, content);
353
248
  }
354
249
  function calculateSimilarity(a, b) {
355
- if (a === b) return 1;
356
- const tokensA = a.split(/[^a-zA-Z0-9]+/).filter((t) => t.length > 0);
357
- const tokensB = b.split(/[^a-zA-Z0-9]+/).filter((t) => t.length > 0);
358
- if (tokensA.length === 0 || tokensB.length === 0) return 0;
359
- const setA = new Set(tokensA);
360
- const setB = new Set(tokensB);
361
- const intersection = new Set([...setA].filter((x) => setB.has(x)));
362
- const union = /* @__PURE__ */ new Set([...setA, ...setB]);
363
- return intersection.size / union.size;
250
+ return (0, import_core2.calculateStringSimilarity)(a, b);
364
251
  }
365
252
  function calculateConfidence(similarity, tokens, lines) {
366
- let confidence = similarity;
367
- if (lines > 20) confidence += 0.05;
368
- if (tokens > 200) confidence += 0.05;
369
- if (lines < 5) confidence -= 0.1;
370
- return Math.max(0, Math.min(1, confidence));
253
+ return (0, import_core2.calculateHeuristicConfidence)(similarity, tokens, lines);
371
254
  }
372
255
  async function detectDuplicatePatterns(fileContents, options) {
373
256
  const {
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PatternDetectProvider,
3
3
  Severity
4
- } from "./chunk-UB3CGOQ7.mjs";
4
+ } from "./chunk-6JTVOBJX.mjs";
5
5
  import {
6
6
  analyzePatterns,
7
7
  createRefactorClusters,
@@ -9,10 +9,10 @@ import {
9
9
  generateSummary,
10
10
  getSmartDefaults,
11
11
  groupDuplicatesByFilePair
12
- } from "./chunk-WMOGJFME.mjs";
12
+ } from "./chunk-DNZS4ESD.mjs";
13
13
  import {
14
14
  detectDuplicatePatterns
15
- } from "./chunk-THF4RW63.mjs";
15
+ } from "./chunk-VGMM3L3O.mjs";
16
16
  import {
17
17
  CONTEXT_RULES,
18
18
  calculateSeverity,
@@ -0,0 +1,23 @@
1
+ import { CostConfig, ToolScoringOutput } from '@aiready/core';
2
+ import { D as DuplicatePattern } from '../types-DU2mmhwb.mjs';
3
+
4
+ /**
5
+ * Calculate AI Readiness Score for pattern duplication (0-100)
6
+ *
7
+ * Based on:
8
+ * - Number of duplicates per file
9
+ * - Token waste per file
10
+ * - High-impact duplicates (>1000 tokens or >70% similarity)
11
+ *
12
+ * Includes business value metrics:
13
+ * - Estimated monthly cost of token waste
14
+ * - Estimated developer hours to fix
15
+ *
16
+ * @param duplicates - Array of detected duplicate patterns.
17
+ * @param totalFilesAnalyzed - Total count of files scanned.
18
+ * @param costConfig - Optional configuration for business value calculations.
19
+ * @returns Standardized scoring output for pattern detection.
20
+ */
21
+ declare function calculatePatternScore(duplicates: DuplicatePattern[], totalFilesAnalyzed: number, costConfig?: Partial<CostConfig>): ToolScoringOutput;
22
+
23
+ export { calculatePatternScore };
@@ -0,0 +1,23 @@
1
+ import { CostConfig, ToolScoringOutput } from '@aiready/core';
2
+ import { D as DuplicatePattern } from '../types-DU2mmhwb.js';
3
+
4
+ /**
5
+ * Calculate AI Readiness Score for pattern duplication (0-100)
6
+ *
7
+ * Based on:
8
+ * - Number of duplicates per file
9
+ * - Token waste per file
10
+ * - High-impact duplicates (>1000 tokens or >70% similarity)
11
+ *
12
+ * Includes business value metrics:
13
+ * - Estimated monthly cost of token waste
14
+ * - Estimated developer hours to fix
15
+ *
16
+ * @param duplicates - Array of detected duplicate patterns.
17
+ * @param totalFilesAnalyzed - Total count of files scanned.
18
+ * @param costConfig - Optional configuration for business value calculations.
19
+ * @returns Standardized scoring output for pattern detection.
20
+ */
21
+ declare function calculatePatternScore(duplicates: DuplicatePattern[], totalFilesAnalyzed: number, costConfig?: Partial<CostConfig>): ToolScoringOutput;
22
+
23
+ export { calculatePatternScore };
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/scoring-entry/index.ts
21
+ var scoring_entry_exports = {};
22
+ __export(scoring_entry_exports, {
23
+ calculatePatternScore: () => calculatePatternScore
24
+ });
25
+ module.exports = __toCommonJS(scoring_entry_exports);
26
+
27
+ // src/scoring.ts
28
+ var import_core = require("@aiready/core");
29
+ function calculatePatternScore(duplicates, totalFilesAnalyzed, costConfig) {
30
+ const totalDuplicates = duplicates.length;
31
+ const totalTokenCost = duplicates.reduce((sum, d) => sum + d.tokenCost, 0);
32
+ const highImpactDuplicates = duplicates.filter(
33
+ (d) => d.tokenCost > 1e3 || d.similarity > 0.7
34
+ ).length;
35
+ if (totalFilesAnalyzed === 0) {
36
+ return {
37
+ toolName: import_core.ToolName.PatternDetect,
38
+ score: 100,
39
+ rawMetrics: {
40
+ totalDuplicates: 0,
41
+ totalTokenCost: 0,
42
+ highImpactDuplicates: 0,
43
+ totalFilesAnalyzed: 0
44
+ },
45
+ factors: [],
46
+ recommendations: []
47
+ };
48
+ }
49
+ const duplicatesPerFile = totalDuplicates / totalFilesAnalyzed * 100;
50
+ const tokenWastePerFile = totalTokenCost / totalFilesAnalyzed;
51
+ const duplicatesPenalty = Math.min(60, duplicatesPerFile * 0.6);
52
+ const tokenPenalty = Math.min(40, tokenWastePerFile / 125);
53
+ const highImpactPenalty = highImpactDuplicates > 0 ? Math.min(15, highImpactDuplicates * 2 - 5) : -5;
54
+ const score = 100 - duplicatesPenalty - tokenPenalty - highImpactPenalty;
55
+ const finalScore = Math.max(0, Math.min(100, Math.round(score)));
56
+ const factors = [
57
+ {
58
+ name: "Duplication Density",
59
+ impact: -Math.round(duplicatesPenalty),
60
+ description: `${duplicatesPerFile.toFixed(1)} duplicates per 100 files`
61
+ },
62
+ {
63
+ name: "Token Waste",
64
+ impact: -Math.round(tokenPenalty),
65
+ description: `${Math.round(tokenWastePerFile)} tokens wasted per file`
66
+ }
67
+ ];
68
+ if (highImpactDuplicates > 0) {
69
+ factors.push({
70
+ name: "High-Impact Patterns",
71
+ impact: -Math.round(highImpactPenalty),
72
+ description: `${highImpactDuplicates} high-impact duplicates (>1000 tokens or >70% similar)`
73
+ });
74
+ } else {
75
+ factors.push({
76
+ name: "No High-Impact Patterns",
77
+ impact: 5,
78
+ description: "No severe duplicates detected"
79
+ });
80
+ }
81
+ const recommendations = [];
82
+ if (highImpactDuplicates > 0) {
83
+ const estimatedImpact = Math.min(15, highImpactDuplicates * 3);
84
+ recommendations.push({
85
+ action: `Deduplicate ${highImpactDuplicates} high-impact pattern${highImpactDuplicates > 1 ? "s" : ""}`,
86
+ estimatedImpact,
87
+ priority: "high"
88
+ });
89
+ }
90
+ if (totalDuplicates > 10 && duplicatesPerFile > 20) {
91
+ const estimatedImpact = Math.min(10, Math.round(duplicatesPenalty * 0.3));
92
+ recommendations.push({
93
+ action: "Extract common patterns into shared utilities",
94
+ estimatedImpact,
95
+ priority: "medium"
96
+ });
97
+ }
98
+ if (tokenWastePerFile > 2e3) {
99
+ const estimatedImpact = Math.min(8, Math.round(tokenPenalty * 0.4));
100
+ recommendations.push({
101
+ action: "Consolidate duplicated logic to reduce AI context waste",
102
+ estimatedImpact,
103
+ priority: totalTokenCost > 1e4 ? "high" : "medium"
104
+ });
105
+ }
106
+ const cfg = { ...import_core.DEFAULT_COST_CONFIG, ...costConfig };
107
+ const estimatedMonthlyCost = (0, import_core.calculateMonthlyCost)(totalTokenCost, cfg);
108
+ const issues = duplicates.map((d) => ({
109
+ severity: d.severity === "critical" ? "critical" : d.severity === "major" ? "major" : "minor"
110
+ }));
111
+ const productivityImpact = (0, import_core.calculateProductivityImpact)(issues);
112
+ return {
113
+ toolName: "pattern-detect",
114
+ score: finalScore,
115
+ rawMetrics: {
116
+ totalDuplicates,
117
+ totalTokenCost,
118
+ highImpactDuplicates,
119
+ totalFilesAnalyzed,
120
+ duplicatesPerFile: Math.round(duplicatesPerFile * 10) / 10,
121
+ tokenWastePerFile: Math.round(tokenWastePerFile),
122
+ // Business value metrics
123
+ estimatedMonthlyCost,
124
+ estimatedDeveloperHours: productivityImpact.totalHours
125
+ },
126
+ factors,
127
+ recommendations
128
+ };
129
+ }
130
+ // Annotate the CommonJS export names for ESM import in node:
131
+ 0 && (module.exports = {
132
+ calculatePatternScore
133
+ });
@@ -0,0 +1,6 @@
1
+ import {
2
+ calculatePatternScore
3
+ } from "../chunk-WBBO35SC.mjs";
4
+ export {
5
+ calculatePatternScore
6
+ };
@@ -1,5 +1,5 @@
1
1
  import { CostConfig, ToolScoringOutput } from '@aiready/core';
2
- import { D as DuplicatePattern } from './types-DU2mmhwb.mjs';
2
+ import { a as DuplicatePattern } from './types-C4lmb2Yh.mjs';
3
3
 
4
4
  /**
5
5
  * Calculate AI Readiness Score for pattern duplication (0-100)
@@ -1,5 +1,5 @@
1
1
  import { CostConfig, ToolScoringOutput } from '@aiready/core';
2
- import { D as DuplicatePattern } from './types-DU2mmhwb.js';
2
+ import { a as DuplicatePattern } from './types-C4lmb2Yh.js';
3
3
 
4
4
  /**
5
5
  * Calculate AI Readiness Score for pattern duplication (0-100)