codereview-aia 0.1.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.
Files changed (153) hide show
  1. package/.cr-aia.yml +23 -0
  2. package/.crignore +0 -0
  3. package/dist/index.js +27 -0
  4. package/package.json +85 -0
  5. package/src/analysis/FindingsExtractor.ts +431 -0
  6. package/src/analysis/ai-detection/analyzers/BaseAnalyzer.ts +267 -0
  7. package/src/analysis/ai-detection/analyzers/DocumentationAnalyzer.ts +622 -0
  8. package/src/analysis/ai-detection/analyzers/GitHistoryAnalyzer.ts +430 -0
  9. package/src/analysis/ai-detection/core/AIDetectionEngine.ts +467 -0
  10. package/src/analysis/ai-detection/types/DetectionTypes.ts +406 -0
  11. package/src/analysis/ai-detection/utils/SubmissionConverter.ts +390 -0
  12. package/src/analysis/context/ReviewContext.ts +378 -0
  13. package/src/analysis/context/index.ts +7 -0
  14. package/src/analysis/index.ts +8 -0
  15. package/src/analysis/tokens/TokenAnalysisFormatter.ts +154 -0
  16. package/src/analysis/tokens/TokenAnalyzer.ts +747 -0
  17. package/src/analysis/tokens/index.ts +8 -0
  18. package/src/clients/base/abstractClient.ts +190 -0
  19. package/src/clients/base/httpClient.ts +160 -0
  20. package/src/clients/base/index.ts +12 -0
  21. package/src/clients/base/modelDetection.ts +107 -0
  22. package/src/clients/base/responseProcessor.ts +586 -0
  23. package/src/clients/factory/clientFactory.ts +55 -0
  24. package/src/clients/factory/index.ts +8 -0
  25. package/src/clients/implementations/index.ts +8 -0
  26. package/src/clients/implementations/openRouterClient.ts +411 -0
  27. package/src/clients/openRouterClient.ts +863 -0
  28. package/src/clients/openRouterClientWrapper.ts +44 -0
  29. package/src/clients/utils/directoryStructure.ts +52 -0
  30. package/src/clients/utils/index.ts +11 -0
  31. package/src/clients/utils/languageDetection.ts +44 -0
  32. package/src/clients/utils/promptFormatter.ts +105 -0
  33. package/src/clients/utils/promptLoader.ts +53 -0
  34. package/src/clients/utils/tokenCounter.ts +297 -0
  35. package/src/core/ApiClientSelector.ts +37 -0
  36. package/src/core/ConfigurationService.ts +591 -0
  37. package/src/core/ConsolidationService.ts +423 -0
  38. package/src/core/InteractiveDisplayManager.ts +81 -0
  39. package/src/core/OutputManager.ts +275 -0
  40. package/src/core/ReviewGenerator.ts +140 -0
  41. package/src/core/fileDiscovery.ts +237 -0
  42. package/src/core/handlers/EstimationHandler.ts +104 -0
  43. package/src/core/handlers/FileProcessingHandler.ts +204 -0
  44. package/src/core/handlers/OutputHandler.ts +125 -0
  45. package/src/core/handlers/ReviewExecutor.ts +104 -0
  46. package/src/core/reviewOrchestrator.ts +333 -0
  47. package/src/core/utils/ModelInfoUtils.ts +56 -0
  48. package/src/formatters/outputFormatter.ts +62 -0
  49. package/src/formatters/utils/IssueFormatters.ts +83 -0
  50. package/src/formatters/utils/JsonFormatter.ts +77 -0
  51. package/src/formatters/utils/MarkdownFormatters.ts +609 -0
  52. package/src/formatters/utils/MetadataFormatter.ts +269 -0
  53. package/src/formatters/utils/ModelInfoExtractor.ts +115 -0
  54. package/src/index.ts +27 -0
  55. package/src/plugins/PluginInterface.ts +50 -0
  56. package/src/plugins/PluginManager.ts +126 -0
  57. package/src/prompts/PromptManager.ts +69 -0
  58. package/src/prompts/cache/PromptCache.ts +50 -0
  59. package/src/prompts/promptText/common/variables/css-frameworks.json +33 -0
  60. package/src/prompts/promptText/common/variables/framework-versions.json +45 -0
  61. package/src/prompts/promptText/frameworks/react/comprehensive.hbs +19 -0
  62. package/src/prompts/promptText/languages/css/comprehensive.hbs +18 -0
  63. package/src/prompts/promptText/languages/generic/comprehensive.hbs +20 -0
  64. package/src/prompts/promptText/languages/html/comprehensive.hbs +18 -0
  65. package/src/prompts/promptText/languages/javascript/comprehensive.hbs +18 -0
  66. package/src/prompts/promptText/languages/python/comprehensive.hbs +18 -0
  67. package/src/prompts/promptText/languages/typescript/comprehensive.hbs +18 -0
  68. package/src/runtime/auth/service.ts +58 -0
  69. package/src/runtime/auth/session.ts +103 -0
  70. package/src/runtime/auth/types.ts +11 -0
  71. package/src/runtime/cliEntry.ts +196 -0
  72. package/src/runtime/errors.ts +13 -0
  73. package/src/runtime/fileCollector.ts +188 -0
  74. package/src/runtime/manifest.ts +64 -0
  75. package/src/runtime/openrouterProxy.ts +45 -0
  76. package/src/runtime/proxyConfig.ts +94 -0
  77. package/src/runtime/proxyEnvironment.ts +71 -0
  78. package/src/runtime/reportMerge.ts +102 -0
  79. package/src/runtime/reporting/markdownReportBuilder.ts +138 -0
  80. package/src/runtime/reporting/reportDataCollector.ts +234 -0
  81. package/src/runtime/reporting/summaryGenerator.ts +86 -0
  82. package/src/runtime/reviewPipeline.ts +155 -0
  83. package/src/runtime/runAiCodeReview.ts +153 -0
  84. package/src/runtime/runtimeConfig.ts +5 -0
  85. package/src/runtime/ui/Layout.tsx +57 -0
  86. package/src/runtime/ui/RuntimeApp.tsx +150 -0
  87. package/src/runtime/ui/inkModules.ts +73 -0
  88. package/src/runtime/ui/screens/AuthScreen.tsx +128 -0
  89. package/src/runtime/ui/screens/ModeSelection.tsx +52 -0
  90. package/src/runtime/ui/screens/ProgressScreen.tsx +55 -0
  91. package/src/runtime/ui/screens/ResultsScreen.tsx +76 -0
  92. package/src/strategies/ArchitecturalReviewStrategy.ts +54 -0
  93. package/src/strategies/CodingTestReviewStrategy.ts +920 -0
  94. package/src/strategies/ConsolidatedReviewStrategy.ts +59 -0
  95. package/src/strategies/ExtractPatternsReviewStrategy.ts +64 -0
  96. package/src/strategies/MultiPassReviewStrategy.ts +785 -0
  97. package/src/strategies/ReviewStrategy.ts +64 -0
  98. package/src/strategies/StrategyFactory.ts +79 -0
  99. package/src/strategies/index.ts +14 -0
  100. package/src/tokenizers/baseTokenizer.ts +61 -0
  101. package/src/tokenizers/gptTokenizer.ts +27 -0
  102. package/src/tokenizers/index.ts +8 -0
  103. package/src/types/apiResponses.ts +40 -0
  104. package/src/types/cli.ts +24 -0
  105. package/src/types/common.ts +39 -0
  106. package/src/types/configuration.ts +201 -0
  107. package/src/types/handlebars.d.ts +5 -0
  108. package/src/types/patch.d.ts +25 -0
  109. package/src/types/review.ts +294 -0
  110. package/src/types/reviewContext.d.ts +65 -0
  111. package/src/types/reviewSchema.ts +181 -0
  112. package/src/types/structuredReview.ts +167 -0
  113. package/src/types/tokenAnalysis.ts +56 -0
  114. package/src/utils/FileReader.ts +93 -0
  115. package/src/utils/FileWriter.ts +76 -0
  116. package/src/utils/PathGenerator.ts +97 -0
  117. package/src/utils/api/apiUtils.ts +14 -0
  118. package/src/utils/api/index.ts +1 -0
  119. package/src/utils/apiErrorHandler.ts +287 -0
  120. package/src/utils/ciDataCollector.ts +252 -0
  121. package/src/utils/codingTestConfigLoader.ts +466 -0
  122. package/src/utils/dependencies/aiDependencyAnalyzer.ts +454 -0
  123. package/src/utils/detection/frameworkDetector.ts +879 -0
  124. package/src/utils/detection/index.ts +10 -0
  125. package/src/utils/detection/projectTypeDetector.ts +518 -0
  126. package/src/utils/diagramGenerator.ts +206 -0
  127. package/src/utils/errorLogger.ts +60 -0
  128. package/src/utils/estimationUtils.ts +407 -0
  129. package/src/utils/fileFilters.ts +373 -0
  130. package/src/utils/fileSystem.ts +57 -0
  131. package/src/utils/index.ts +36 -0
  132. package/src/utils/logger.ts +240 -0
  133. package/src/utils/pathValidator.ts +98 -0
  134. package/src/utils/priorityFilter.ts +59 -0
  135. package/src/utils/projectDocs.ts +189 -0
  136. package/src/utils/promptPaths.ts +29 -0
  137. package/src/utils/promptTemplateManager.ts +157 -0
  138. package/src/utils/review/consolidateReview.ts +553 -0
  139. package/src/utils/review/fixDisplay.ts +100 -0
  140. package/src/utils/review/fixImplementation.ts +61 -0
  141. package/src/utils/review/index.ts +36 -0
  142. package/src/utils/review/interactiveProcessing.ts +294 -0
  143. package/src/utils/review/progressTracker.ts +296 -0
  144. package/src/utils/review/reviewExtraction.ts +382 -0
  145. package/src/utils/review/types.ts +46 -0
  146. package/src/utils/reviewActionHandler.ts +18 -0
  147. package/src/utils/reviewParser.ts +253 -0
  148. package/src/utils/sanitizer.ts +238 -0
  149. package/src/utils/smartFileSelector.ts +255 -0
  150. package/src/utils/templateLoader.ts +514 -0
  151. package/src/utils/treeGenerator.ts +153 -0
  152. package/tsconfig.build.json +14 -0
  153. package/tsconfig.json +59 -0
@@ -0,0 +1,390 @@
1
+ /**
2
+ * @fileoverview Utility to convert existing code review data to AI detection format.
3
+ *
4
+ * This module provides functions to transform the existing FileInfo and project structure
5
+ * into the CodeSubmission format required by the AI detection engine.
6
+ */
7
+
8
+ import { execSync } from 'node:child_process';
9
+ import { statSync } from 'node:fs';
10
+ import path from 'node:path';
11
+ import type { FileInfo } from '../../../types/review';
12
+ import type { ProjectDocs } from '../../../utils/projectDocs';
13
+ import type {
14
+ CodeFile,
15
+ CodeSubmission,
16
+ DocumentationSet,
17
+ GitCommit,
18
+ GitRepository,
19
+ ParsedCodebase,
20
+ ParsedFunction,
21
+ } from '../types/DetectionTypes';
22
+
23
+ /**
24
+ * Convert review data to AI detection submission format
25
+ */
26
+ export class SubmissionConverter {
27
+ /**
28
+ * Convert FileInfo array and project data to CodeSubmission
29
+ * @param files Array of FileInfo objects
30
+ * @param projectName Project name
31
+ * @param projectDocs Project documentation
32
+ * @param projectPath Path to project root
33
+ * @returns CodeSubmission object for AI detection
34
+ */
35
+ static async convert(
36
+ files: FileInfo[],
37
+ _projectName: string,
38
+ projectDocs: ProjectDocs | null,
39
+ projectPath: string = process.cwd(),
40
+ ): Promise<CodeSubmission> {
41
+ try {
42
+ const repository = await SubmissionConverter.extractGitRepository(projectPath);
43
+ const codebase = SubmissionConverter.createParsedCodebase(files);
44
+ const documentation = SubmissionConverter.createDocumentationSet(files, projectDocs);
45
+
46
+ return {
47
+ repository,
48
+ codebase,
49
+ documentation,
50
+ };
51
+ } catch (error) {
52
+ console.warn('Error converting submission:', error);
53
+ return SubmissionConverter.createFallbackSubmission(files, projectDocs);
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Extract git repository information
59
+ * @param projectPath Path to project root
60
+ * @returns Git repository data
61
+ */
62
+ private static async extractGitRepository(projectPath: string): Promise<GitRepository> {
63
+ try {
64
+ // Check if git repository exists
65
+ execSync('git rev-parse --git-dir', {
66
+ cwd: projectPath,
67
+ stdio: 'pipe',
68
+ });
69
+
70
+ // Get commit history (last 50 commits)
71
+ const gitLogOutput = execSync(
72
+ 'git log --pretty=format:"%H|%s|%ai|%an|%ae" -n 50 --name-only',
73
+ {
74
+ cwd: projectPath,
75
+ encoding: 'utf8',
76
+ stdio: 'pipe',
77
+ },
78
+ );
79
+
80
+ const commits = SubmissionConverter.parseGitLog(gitLogOutput);
81
+
82
+ return {
83
+ commits,
84
+ rootPath: projectPath,
85
+ };
86
+ } catch (error) {
87
+ console.warn('Failed to extract git repository info:', error);
88
+ return {
89
+ commits: [],
90
+ rootPath: projectPath,
91
+ };
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Parse git log output into commit objects
97
+ * @param gitLogOutput Raw git log output
98
+ * @returns Array of parsed git commits
99
+ */
100
+ private static parseGitLog(gitLogOutput: string): GitCommit[] {
101
+ const commits: GitCommit[] = [];
102
+ const lines = gitLogOutput.split('\n').filter((line) => line.trim());
103
+
104
+ let currentCommit: Partial<GitCommit> | null = null;
105
+ let collectingFiles = false;
106
+
107
+ for (const line of lines) {
108
+ if (line.includes('|')) {
109
+ // Commit header line
110
+ if (currentCommit) {
111
+ commits.push(currentCommit as GitCommit);
112
+ }
113
+
114
+ const parts = line.split('|');
115
+ if (parts.length >= 5) {
116
+ currentCommit = {
117
+ hash: parts[0],
118
+ message: parts[1],
119
+ timestamp: new Date(parts[2]),
120
+ author: {
121
+ name: parts[3],
122
+ email: parts[4],
123
+ },
124
+ changedFiles: [],
125
+ };
126
+ collectingFiles = true;
127
+ }
128
+ } else if (collectingFiles && currentCommit && line.trim()) {
129
+ // File name line
130
+ currentCommit.changedFiles?.push(line.trim());
131
+ }
132
+ }
133
+
134
+ // Add the last commit
135
+ if (currentCommit) {
136
+ commits.push(currentCommit as GitCommit);
137
+ }
138
+
139
+ return commits.reverse(); // Oldest first
140
+ }
141
+
142
+ /**
143
+ * Create parsed codebase from FileInfo array
144
+ * @param files Array of FileInfo objects
145
+ * @returns Parsed codebase structure
146
+ */
147
+ private static createParsedCodebase(files: FileInfo[]): ParsedCodebase {
148
+ const codeFiles: CodeFile[] = files.map((file) => ({
149
+ path: file.path,
150
+ content: file.content,
151
+ language: SubmissionConverter.detectLanguage(file.path),
152
+ size: Buffer.byteLength(file.content, 'utf8'),
153
+ lastModified: SubmissionConverter.getFileModifiedTime(file.path),
154
+ }));
155
+
156
+ const functions = SubmissionConverter.extractFunctions(codeFiles);
157
+
158
+ return {
159
+ files: codeFiles,
160
+ functions,
161
+ statistics: {
162
+ totalLines: codeFiles.reduce((sum, file) => sum + file.content.split('\n').length, 0),
163
+ totalFiles: codeFiles.length,
164
+ languages: Array.from(new Set(codeFiles.map((f) => f.language))),
165
+ },
166
+ };
167
+ }
168
+
169
+ /**
170
+ * Create documentation set from files and project docs
171
+ * @param files Array of FileInfo objects
172
+ * @param projectDocs Project documentation
173
+ * @returns Documentation set
174
+ */
175
+ private static createDocumentationSet(
176
+ files: FileInfo[],
177
+ projectDocs: ProjectDocs | null,
178
+ ): DocumentationSet {
179
+ // Find README file
180
+ const readmeFile = files.find((file) => /readme/i.test(path.basename(file.path)));
181
+
182
+ // Get code files for comment analysis
183
+ const codeFiles = files
184
+ .filter((file) => SubmissionConverter.isCodeFile(file.path))
185
+ .map((file) => ({
186
+ path: file.path,
187
+ content: file.content,
188
+ language: SubmissionConverter.detectLanguage(file.path),
189
+ size: Buffer.byteLength(file.content, 'utf8'),
190
+ }));
191
+
192
+ return {
193
+ readme: readmeFile?.content || projectDocs?.readme,
194
+ codeFiles,
195
+ otherDocs: SubmissionConverter.extractOtherDocs(files),
196
+ };
197
+ }
198
+
199
+ /**
200
+ * Detect programming language from file path
201
+ * @param filePath File path
202
+ * @returns Programming language
203
+ */
204
+ private static detectLanguage(filePath: string): string {
205
+ const ext = path.extname(filePath).toLowerCase();
206
+ const langMap: Record<string, string> = {
207
+ '.js': 'javascript',
208
+ '.jsx': 'javascript',
209
+ '.ts': 'typescript',
210
+ '.tsx': 'typescript',
211
+ '.py': 'python',
212
+ '.java': 'java',
213
+ '.go': 'go',
214
+ '.rb': 'ruby',
215
+ '.php': 'php',
216
+ '.cpp': 'cpp',
217
+ '.c': 'c',
218
+ '.cs': 'csharp',
219
+ '.swift': 'swift',
220
+ '.kt': 'kotlin',
221
+ '.rs': 'rust',
222
+ '.dart': 'dart',
223
+ };
224
+
225
+ return langMap[ext] || 'unknown';
226
+ }
227
+
228
+ /**
229
+ * Get file modification time
230
+ * @param filePath File path
231
+ * @returns Last modified date or undefined
232
+ */
233
+ private static getFileModifiedTime(filePath: string): Date | undefined {
234
+ try {
235
+ const stats = statSync(filePath);
236
+ return stats.mtime;
237
+ } catch {
238
+ return undefined;
239
+ }
240
+ }
241
+
242
+ /**
243
+ * Check if file is a code file
244
+ * @param filePath File path
245
+ * @returns True if it's a code file
246
+ */
247
+ private static isCodeFile(filePath: string): boolean {
248
+ const codeExtensions = [
249
+ '.js',
250
+ '.jsx',
251
+ '.ts',
252
+ '.tsx',
253
+ '.py',
254
+ '.java',
255
+ '.go',
256
+ '.rb',
257
+ '.php',
258
+ '.cpp',
259
+ '.c',
260
+ '.cs',
261
+ '.swift',
262
+ '.kt',
263
+ '.rs',
264
+ '.scala',
265
+ '.clj',
266
+ '.dart',
267
+ ];
268
+
269
+ const ext = path.extname(filePath).toLowerCase();
270
+ return codeExtensions.includes(ext);
271
+ }
272
+
273
+ /**
274
+ * Extract function information from code files (simplified)
275
+ * @param codeFiles Array of code files
276
+ * @returns Array of parsed functions
277
+ */
278
+ private static extractFunctions(codeFiles: CodeFile[]): ParsedFunction[] {
279
+ const functions: ParsedFunction[] = [];
280
+
281
+ codeFiles.forEach((file) => {
282
+ if (file.language === 'typescript' || file.language === 'javascript') {
283
+ const extractedFunctions = SubmissionConverter.extractJSFunctions(file);
284
+ functions.push(...extractedFunctions);
285
+ }
286
+ // Add other language parsers as needed
287
+ });
288
+
289
+ return functions;
290
+ }
291
+
292
+ /**
293
+ * Extract JavaScript/TypeScript functions (simplified regex-based)
294
+ * @param file Code file
295
+ * @returns Array of parsed functions
296
+ */
297
+ private static extractJSFunctions(file: CodeFile): ParsedFunction[] {
298
+ const functions: ParsedFunction[] = [];
299
+ const lines = file.content.split('\n');
300
+
301
+ // Simple regex patterns for function detection
302
+ const functionPatterns = [
303
+ /function\s+(\w+)\s*\([^)]*\)/,
304
+ /const\s+(\w+)\s*=\s*\([^)]*\)\s*=>/,
305
+ /(\w+)\s*:\s*\([^)]*\)\s*=>/,
306
+ /async\s+function\s+(\w+)/,
307
+ ];
308
+
309
+ lines.forEach((line, index) => {
310
+ for (const pattern of functionPatterns) {
311
+ const match = line.match(pattern);
312
+ if (match) {
313
+ functions.push({
314
+ name: match[1] || 'anonymous',
315
+ filePath: file.path,
316
+ startLine: index + 1,
317
+ endLine: index + 1, // Simplified - would need proper parsing
318
+ parameters: [], // Simplified
319
+ conditionals: [],
320
+ loops: [],
321
+ catches: [],
322
+ logicalOperators: [],
323
+ });
324
+ break;
325
+ }
326
+ }
327
+ });
328
+
329
+ return functions;
330
+ }
331
+
332
+ /**
333
+ * Extract other documentation files
334
+ * @param files Array of FileInfo objects
335
+ * @returns Array of documentation files
336
+ */
337
+ private static extractOtherDocs(files: FileInfo[]) {
338
+ const docPatterns = [/\.md$/i, /changelog/i, /license/i, /contributing/i, /authors/i];
339
+
340
+ return files
341
+ .filter(
342
+ (file) =>
343
+ docPatterns.some((pattern) => pattern.test(file.path)) &&
344
+ !/readme/i.test(path.basename(file.path)),
345
+ )
346
+ .map((file) => ({
347
+ path: file.path,
348
+ content: file.content,
349
+ type: SubmissionConverter.categorizeDocFile(file.path),
350
+ }));
351
+ }
352
+
353
+ /**
354
+ * Categorize documentation file type
355
+ * @param filePath File path
356
+ * @returns Documentation type
357
+ */
358
+ private static categorizeDocFile(
359
+ filePath: string,
360
+ ): 'readme' | 'changelog' | 'api' | 'guide' | 'other' {
361
+ const fileName = path.basename(filePath).toLowerCase();
362
+
363
+ if (fileName.includes('changelog')) return 'changelog';
364
+ if (fileName.includes('api')) return 'api';
365
+ if (fileName.includes('guide') || fileName.includes('tutorial')) return 'guide';
366
+ if (fileName.includes('readme')) return 'readme';
367
+
368
+ return 'other';
369
+ }
370
+
371
+ /**
372
+ * Create fallback submission when git extraction fails
373
+ * @param files Array of FileInfo objects
374
+ * @param projectDocs Project documentation
375
+ * @returns Minimal CodeSubmission
376
+ */
377
+ private static createFallbackSubmission(
378
+ files: FileInfo[],
379
+ projectDocs: ProjectDocs | null,
380
+ ): CodeSubmission {
381
+ return {
382
+ repository: {
383
+ commits: [],
384
+ rootPath: process.cwd(),
385
+ },
386
+ codebase: SubmissionConverter.createParsedCodebase(files),
387
+ documentation: SubmissionConverter.createDocumentationSet(files, projectDocs),
388
+ };
389
+ }
390
+ }