hyntx 3.0.2 → 4.0.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/README.md +118 -963
- package/dist/cli.d.ts +1 -440
- package/dist/cli.js +5183 -6762
- package/dist/index.d.ts +696 -2
- package/dist/index.js +5392 -1
- package/package.json +17 -38
- package/dist/api/index.d.ts +0 -1083
- package/dist/api/index.js +0 -1
- package/dist/chunk-24U3LCZC.js +0 -3347
- package/dist/chunk-TLJ2AIHG.js +0 -389
- package/dist/shell-config-VEIMSP5W.js +0 -2
package/dist/chunk-24U3LCZC.js
DELETED
|
@@ -1,3347 +0,0 @@
|
|
|
1
|
-
import { createHash } from 'crypto';
|
|
2
|
-
import { existsSync, readdirSync, readFileSync } from 'fs';
|
|
3
|
-
import { readFile, writeFile, rename, rm, mkdir } from 'fs/promises';
|
|
4
|
-
import { join, basename, dirname } from 'path';
|
|
5
|
-
import { homedir } from 'os';
|
|
6
|
-
import { startOfDay, parseISO, isAfter, subDays, format, subMonths, isSameDay, isBefore } from 'date-fns';
|
|
7
|
-
import { glob } from 'glob';
|
|
8
|
-
import { z } from 'zod';
|
|
9
|
-
|
|
10
|
-
// src/cache/analysis-cache.ts
|
|
11
|
-
|
|
12
|
-
// src/utils/logger-base.ts
|
|
13
|
-
var BaseLogger = class {
|
|
14
|
-
warnings = [];
|
|
15
|
-
verboseEnabled = false;
|
|
16
|
-
/**
|
|
17
|
-
* Enables or disables verbose (debug) mode.
|
|
18
|
-
*
|
|
19
|
-
* @param enabled - Whether to enable verbose mode
|
|
20
|
-
*/
|
|
21
|
-
setVerbose(enabled) {
|
|
22
|
-
this.verboseEnabled = enabled;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Returns whether verbose mode is enabled.
|
|
26
|
-
*
|
|
27
|
-
* @returns true if verbose mode is enabled
|
|
28
|
-
*/
|
|
29
|
-
isVerbose() {
|
|
30
|
-
return this.verboseEnabled;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Logs an error message to stderr.
|
|
34
|
-
*
|
|
35
|
-
* @param message - Error message
|
|
36
|
-
* @param context - Optional context (e.g., file name, operation)
|
|
37
|
-
*/
|
|
38
|
-
error(message, context) {
|
|
39
|
-
const prefix = context ? `[${context}] ` : "";
|
|
40
|
-
process.stderr.write(`ERROR: ${prefix}${message}
|
|
41
|
-
`);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Logs a warning message to stderr.
|
|
45
|
-
*
|
|
46
|
-
* @param message - Warning message
|
|
47
|
-
* @param context - Optional context (e.g., file name, operation)
|
|
48
|
-
*/
|
|
49
|
-
warn(message, context) {
|
|
50
|
-
const prefix = context ? `[${context}] ` : "";
|
|
51
|
-
process.stderr.write(`WARN: ${prefix}${message}
|
|
52
|
-
`);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Logs an info message to stderr.
|
|
56
|
-
*
|
|
57
|
-
* @param message - Info message
|
|
58
|
-
*/
|
|
59
|
-
info(message) {
|
|
60
|
-
process.stderr.write(`INFO: ${message}
|
|
61
|
-
`);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Logs a debug message to stderr (only when verbose mode is enabled).
|
|
65
|
-
*
|
|
66
|
-
* @param message - Debug message
|
|
67
|
-
* @param context - Optional context
|
|
68
|
-
*/
|
|
69
|
-
debug(message, context) {
|
|
70
|
-
if (this.verboseEnabled) {
|
|
71
|
-
const prefix = context ? `[${context}] ` : "";
|
|
72
|
-
process.stderr.write(`[DEBUG] ${prefix}${message}
|
|
73
|
-
`);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Collects a warning for later reporting.
|
|
78
|
-
* Use this for non-fatal issues that should be displayed at the end of execution.
|
|
79
|
-
*
|
|
80
|
-
* @param message - Warning message
|
|
81
|
-
* @param context - Optional context (e.g., file name, operation)
|
|
82
|
-
*/
|
|
83
|
-
collectWarning(message, context) {
|
|
84
|
-
this.warnings.push({ message, context });
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Returns all collected warnings.
|
|
88
|
-
*
|
|
89
|
-
* @returns Array of collected warnings
|
|
90
|
-
*/
|
|
91
|
-
getWarnings() {
|
|
92
|
-
return [...this.warnings];
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Returns the number of collected warnings.
|
|
96
|
-
*
|
|
97
|
-
* @returns Warning count
|
|
98
|
-
*/
|
|
99
|
-
getWarningCount() {
|
|
100
|
-
return this.warnings.length;
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Clears all collected warnings.
|
|
104
|
-
*/
|
|
105
|
-
clearWarnings() {
|
|
106
|
-
this.warnings = [];
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Reports all collected warnings to stderr and clears them.
|
|
110
|
-
* Does nothing if there are no warnings.
|
|
111
|
-
*/
|
|
112
|
-
reportWarnings() {
|
|
113
|
-
if (this.warnings.length === 0) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
process.stderr.write(`
|
|
117
|
-
\u26A0\uFE0F ${String(this.warnings.length)} warning(s):
|
|
118
|
-
`);
|
|
119
|
-
for (const warning of this.warnings) {
|
|
120
|
-
const prefix = warning.context ? `[${warning.context}] ` : "";
|
|
121
|
-
process.stderr.write(` - ${prefix}${warning.message}
|
|
122
|
-
`);
|
|
123
|
-
}
|
|
124
|
-
this.clearWarnings();
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Formats collected warnings as plain text (without ANSI colors).
|
|
128
|
-
* Useful for JSON output or testing.
|
|
129
|
-
*
|
|
130
|
-
* @returns Array of formatted warning strings
|
|
131
|
-
*/
|
|
132
|
-
formatWarnings() {
|
|
133
|
-
return this.warnings.map((w) => {
|
|
134
|
-
const prefix = w.context ? `[${w.context}] ` : "";
|
|
135
|
-
return `${prefix}${w.message}`;
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
var logger = new BaseLogger();
|
|
140
|
-
|
|
141
|
-
// src/providers/schemas.ts
|
|
142
|
-
var ISSUE_TAXONOMY = {
|
|
143
|
-
vague: {
|
|
144
|
-
name: "Vague Request",
|
|
145
|
-
severity: "high",
|
|
146
|
-
suggestion: "Be more specific about what you need - include function names, file paths, error messages, or specific behaviors",
|
|
147
|
-
exampleBefore: "Help me with my code",
|
|
148
|
-
exampleAfter: "Help me debug the calculateTotal() function in utils.ts that returns undefined when called with an empty array"
|
|
149
|
-
},
|
|
150
|
-
"no-context": {
|
|
151
|
-
name: "Missing Context",
|
|
152
|
-
severity: "high",
|
|
153
|
-
suggestion: "Provide relevant background information - include file paths, function names, error messages, or code snippets",
|
|
154
|
-
exampleBefore: "Fix the bug",
|
|
155
|
-
exampleAfter: "Fix the bug in src/auth/login.ts where users are logged out after 5 minutes due to token expiration logic"
|
|
156
|
-
},
|
|
157
|
-
"too-broad": {
|
|
158
|
-
name: "Too Broad",
|
|
159
|
-
severity: "medium",
|
|
160
|
-
suggestion: "Break down into smaller, focused requests - focus on one task or clearly order multiple related tasks",
|
|
161
|
-
exampleBefore: "Build me an app with authentication, database, and API",
|
|
162
|
-
exampleAfter: "Create a React login component with email/password authentication using JWT tokens"
|
|
163
|
-
},
|
|
164
|
-
"no-goal": {
|
|
165
|
-
name: "No Clear Goal",
|
|
166
|
-
severity: "high",
|
|
167
|
-
suggestion: "State what outcome you want to achieve - specify success criteria and desired behavior",
|
|
168
|
-
exampleBefore: "Look at this file",
|
|
169
|
-
exampleAfter: "Review src/auth/login.ts for security vulnerabilities, focusing on input validation and SQL injection risks"
|
|
170
|
-
},
|
|
171
|
-
imperative: {
|
|
172
|
-
name: "Command Without Context",
|
|
173
|
-
severity: "low",
|
|
174
|
-
suggestion: "Explain why you need this - provide context about the use case and requirements",
|
|
175
|
-
exampleBefore: "Add a button",
|
|
176
|
-
exampleAfter: 'Add a "Submit" button to the login form that triggers validation and calls the authentication API'
|
|
177
|
-
},
|
|
178
|
-
"missing-technical-details": {
|
|
179
|
-
name: "Missing Technical Details",
|
|
180
|
-
severity: "medium",
|
|
181
|
-
suggestion: "Include technical details - file paths, function signatures, error messages, stack traces, or code snippets",
|
|
182
|
-
exampleBefore: "The function crashes sometimes",
|
|
183
|
-
exampleAfter: `The validateUser() function in src/utils/auth.ts crashes with "Cannot read property 'email' of null" when called with undefined`
|
|
184
|
-
},
|
|
185
|
-
"unclear-priorities": {
|
|
186
|
-
name: "Unclear Priorities",
|
|
187
|
-
severity: "low",
|
|
188
|
-
suggestion: "Order multiple requests by priority or split into separate prompts for clarity",
|
|
189
|
-
exampleBefore: "Add error handling and logging and also optimize performance and add tests",
|
|
190
|
-
exampleAfter: "First, add comprehensive error handling with try-catch blocks. Then, add logging for debugging. Finally, optimize database queries."
|
|
191
|
-
},
|
|
192
|
-
"insufficient-constraints": {
|
|
193
|
-
name: "Insufficient Constraints",
|
|
194
|
-
severity: "low",
|
|
195
|
-
suggestion: "Specify requirements and constraints - edge cases, performance needs, compatibility requirements",
|
|
196
|
-
exampleBefore: "Make it faster",
|
|
197
|
-
exampleAfter: "Optimize the database query to reduce response time to under 100ms, maintaining backward compatibility with existing API clients"
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
var SYSTEM_PROMPT_MINIMAL = `You analyze prompts for quality issues.
|
|
201
|
-
Respond with JSON only: {"issues": ["issue-id", ...], "score": 0-100}
|
|
202
|
-
|
|
203
|
-
Valid issue IDs: vague, no-context, too-broad, no-goal, imperative, missing-technical-details, unclear-priorities, insufficient-constraints
|
|
204
|
-
|
|
205
|
-
Issue definitions:
|
|
206
|
-
- vague: Generic requests without specifics ("help", "fix", "improve")
|
|
207
|
-
- no-context: Missing background info (uses "this", "it", "the bug" without context)
|
|
208
|
-
- too-broad: Requests covering multiple unrelated topics
|
|
209
|
-
- no-goal: Ambiguous success criteria or desired outcome
|
|
210
|
-
- imperative: Commands without explanation or reasoning
|
|
211
|
-
- missing-technical-details: No file paths, function names, or error messages
|
|
212
|
-
- unclear-priorities: Multiple requests without ordering
|
|
213
|
-
- insufficient-constraints: No requirements or edge cases mentioned
|
|
214
|
-
|
|
215
|
-
Scoring: 0-100 (100=perfect, 90+=excellent, 70-89=good, 50-69=fair, <50=poor)
|
|
216
|
-
|
|
217
|
-
Examples:
|
|
218
|
-
Input: "Help me with code"
|
|
219
|
-
Output: {"issues": ["vague", "no-context"], "score": 35}
|
|
220
|
-
|
|
221
|
-
Input: "Debug this TypeScript function that returns undefined"
|
|
222
|
-
Output: {"issues": ["missing-technical-details"], "score": 70}
|
|
223
|
-
|
|
224
|
-
Input: "Debug calculateTotal() in utils.ts that returns undefined when called with empty array"
|
|
225
|
-
Output: {"issues": [], "score": 90}`;
|
|
226
|
-
var SYSTEM_PROMPT_FULL = `You are an expert prompt quality analyst specializing in code-related prompts for AI assistants like Claude. Your task is to analyze prompts and identify patterns that reduce their effectiveness, providing actionable improvements.
|
|
227
|
-
|
|
228
|
-
CRITICAL: Return ONLY valid JSON following the exact schema below. No markdown, no explanations, no code fences - just pure JSON.
|
|
229
|
-
|
|
230
|
-
Schema:
|
|
231
|
-
{
|
|
232
|
-
"patterns": [
|
|
233
|
-
{
|
|
234
|
-
"id": "kebab-case-id",
|
|
235
|
-
"name": "Human-Readable Issue Name",
|
|
236
|
-
"frequency": 3,
|
|
237
|
-
"severity": "high|medium|low",
|
|
238
|
-
"examples": ["example prompt 1", "example prompt 2"],
|
|
239
|
-
"suggestion": "Actionable advice to fix this pattern",
|
|
240
|
-
"beforeAfter": {
|
|
241
|
-
"before": "Original problematic prompt",
|
|
242
|
-
"after": "Improved version addressing the issue"
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
],
|
|
246
|
-
"stats": {
|
|
247
|
-
"totalPrompts": 10,
|
|
248
|
-
"promptsWithIssues": 7,
|
|
249
|
-
"overallScore": 65
|
|
250
|
-
},
|
|
251
|
-
"topSuggestion": "Single most impactful recommendation"
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
QUALITY DIMENSIONS TO EVALUATE:
|
|
255
|
-
|
|
256
|
-
1. SPECIFICITY (High Priority)
|
|
257
|
-
- Vague: "help me", "fix this", "improve code" without specifics
|
|
258
|
-
- Good: Includes function names, file paths, error messages, specific behaviors
|
|
259
|
-
- Look for: Generic verbs without objects, pronouns without referents
|
|
260
|
-
|
|
261
|
-
2. CONTEXT PROVISION (High Priority)
|
|
262
|
-
- Missing: References to "this code", "the bug", "it" without background
|
|
263
|
-
- Good: Provides file paths, function signatures, relevant code snippets, error stack traces
|
|
264
|
-
- Look for: Unclear referents, assumptions of shared knowledge
|
|
265
|
-
|
|
266
|
-
3. GOAL CLARITY (High Priority)
|
|
267
|
-
- Unclear: "Look at this", "Review my code", "What's wrong?"
|
|
268
|
-
- Good: Explicitly states desired outcome: "Find security vulnerabilities", "Optimize for performance", "Fix the null pointer exception"
|
|
269
|
-
- Look for: Ambiguous success criteria, open-ended questions
|
|
270
|
-
|
|
271
|
-
4. SCOPE APPROPRIATENESS (Medium Priority)
|
|
272
|
-
- Too broad: "Build me an app", "Refactor everything", multiple unrelated tasks
|
|
273
|
-
- Too narrow: Overly specific constraints that limit solution space unnecessarily
|
|
274
|
-
- Good: Focused single task or clearly ordered multiple tasks
|
|
275
|
-
- Look for: Multiple "and" clauses, unrelated requests, scope creep
|
|
276
|
-
|
|
277
|
-
5. ACTIONABILITY (Medium Priority)
|
|
278
|
-
- Low: "What do you think?", "Is this good?", purely exploratory
|
|
279
|
-
- Good: Clear action requested: "Fix", "Implement", "Refactor", "Add"
|
|
280
|
-
- Look for: Vague intentions, exploration without purpose
|
|
281
|
-
|
|
282
|
-
6. TECHNICAL RICHNESS (Medium Priority)
|
|
283
|
-
- Missing: No code examples, file paths, function names, error messages
|
|
284
|
-
- Good: Includes relevant code, stack traces, configuration, environment details
|
|
285
|
-
- Look for: Purely conceptual descriptions without technical details
|
|
286
|
-
|
|
287
|
-
7. CONSTRAINT DEFINITION (Low Priority)
|
|
288
|
-
- Missing: No requirements, edge cases, or constraints mentioned
|
|
289
|
-
- Good: Specifies requirements, edge cases, performance needs, compatibility needs
|
|
290
|
-
- Look for: Requests that could have many valid interpretations
|
|
291
|
-
|
|
292
|
-
SEVERITY GUIDELINES:
|
|
293
|
-
- high: Significantly impairs prompt effectiveness (vague, missing context, no goal)
|
|
294
|
-
- medium: Moderate impact on effectiveness (too broad, lacks technical details)
|
|
295
|
-
- low: Minor improvement opportunity (missing constraints, could be more specific)
|
|
296
|
-
|
|
297
|
-
SCORING GUIDELINES (0-100):
|
|
298
|
-
- 90-100: Excellent prompts - specific, contextual, actionable, well-scoped
|
|
299
|
-
- 75-89: Good prompts - minor improvements possible
|
|
300
|
-
- 60-74: Fair prompts - needs more context, clarity, or specificity
|
|
301
|
-
- 40-59: Poor prompts - multiple significant issues, partially actionable
|
|
302
|
-
- 20-39: Very poor - barely actionable, missing critical information
|
|
303
|
-
- 0-19: Extremely poor - cannot be meaningfully acted upon
|
|
304
|
-
|
|
305
|
-
PATTERN IDENTIFICATION:
|
|
306
|
-
- Group similar issues together (same root cause)
|
|
307
|
-
- Count frequency: how many prompts exhibit this pattern
|
|
308
|
-
- Provide 1-3 real examples from the actual prompts analyzed
|
|
309
|
-
- Create meaningful before/after pairs that clearly show improvement
|
|
310
|
-
- Ensure beforeAfter.before matches one of the examples
|
|
311
|
-
|
|
312
|
-
IMPROVEMENT PRIORITIES:
|
|
313
|
-
1. High severity patterns (vague, missing context, no goal) should appear first
|
|
314
|
-
2. More frequent patterns should rank higher
|
|
315
|
-
3. topSuggestion should address the most common high-severity issue
|
|
316
|
-
|
|
317
|
-
Remember: The goal is actionable feedback. Focus on patterns that, when fixed, would meaningfully improve prompt effectiveness for AI code assistants.`;
|
|
318
|
-
var SYSTEM_PROMPT_BATCH_INDIVIDUAL = `You will analyze multiple coding prompts. You MUST return a JSON array with exactly one result object for each prompt.
|
|
319
|
-
|
|
320
|
-
CRITICAL: Your response must be a JSON array starting with [ and ending with ]. Do not return a single object.
|
|
321
|
-
|
|
322
|
-
Format (JSON array with one object per prompt):
|
|
323
|
-
[
|
|
324
|
-
{
|
|
325
|
-
"status": "correct" | "problems",
|
|
326
|
-
"problems": ["issue1", "issue2"],
|
|
327
|
-
"categories": ["cat1", "cat2"],
|
|
328
|
-
"example": "original prompt",
|
|
329
|
-
"suggestion": "improvement"
|
|
330
|
-
},
|
|
331
|
-
{
|
|
332
|
-
"status": "correct" | "problems",
|
|
333
|
-
"problems": [],
|
|
334
|
-
"categories": [],
|
|
335
|
-
"example": "second prompt",
|
|
336
|
-
"suggestion": "another suggestion"
|
|
337
|
-
}
|
|
338
|
-
]
|
|
339
|
-
|
|
340
|
-
Categories (assign all that apply per prompt):
|
|
341
|
-
- vague-request: Lacks specifics ("help", "fix", "improve")
|
|
342
|
-
- missing-context: No file paths, function names, error messages
|
|
343
|
-
- too-broad: Multiple unrelated tasks
|
|
344
|
-
- unclear-goal: Desired outcome not stated
|
|
345
|
-
- other: Other issues
|
|
346
|
-
|
|
347
|
-
Rules:
|
|
348
|
-
- One result per prompt in the same order
|
|
349
|
-
- status="correct" if prompt is specific, contextual, and actionable
|
|
350
|
-
- status="problems" if ANY issue exists
|
|
351
|
-
- Multiple categories allowed per prompt
|
|
352
|
-
- Be strict but fair
|
|
353
|
-
- Include original prompt text in example field
|
|
354
|
-
|
|
355
|
-
Quality Guidelines:
|
|
356
|
-
- CORRECT: Has file path, function name, and specific issue
|
|
357
|
-
- CORRECT: States clear goal with necessary context
|
|
358
|
-
- PROBLEMS: Generic verbs without objects ("help", "fix")
|
|
359
|
-
- PROBLEMS: References without context ("this", "it", "the bug")
|
|
360
|
-
- PROBLEMS: Multiple unrelated tasks in one prompt
|
|
361
|
-
|
|
362
|
-
Examples:
|
|
363
|
-
|
|
364
|
-
Input: ["Help", "Fix bug in login.ts line 45 where users can't reset password"]
|
|
365
|
-
Output: [
|
|
366
|
-
{
|
|
367
|
-
"status": "problems",
|
|
368
|
-
"problems": ["Too vague", "No context", "No goal"],
|
|
369
|
-
"categories": ["vague-request", "missing-context", "unclear-goal"],
|
|
370
|
-
"example": "Help",
|
|
371
|
-
"suggestion": "Describe what you need help with, provide context (files, functions, errors), and state your goal"
|
|
372
|
-
},
|
|
373
|
-
{
|
|
374
|
-
"status": "correct",
|
|
375
|
-
"problems": [],
|
|
376
|
-
"categories": [],
|
|
377
|
-
"example": "Fix bug in login.ts line 45 where users can't reset password",
|
|
378
|
-
"suggestion": "Well-formed: specific file, location, and issue"
|
|
379
|
-
}
|
|
380
|
-
]
|
|
381
|
-
|
|
382
|
-
Analyze the following prompts:`;
|
|
383
|
-
var VALID_PATTERN_IDS = Object.keys(ISSUE_TAXONOMY);
|
|
384
|
-
function applyRulesConfig(rules, baseTaxonomy = ISSUE_TAXONOMY) {
|
|
385
|
-
if (!rules || Object.keys(rules).length === 0) {
|
|
386
|
-
return baseTaxonomy;
|
|
387
|
-
}
|
|
388
|
-
for (const ruleId of Object.keys(rules)) {
|
|
389
|
-
if (!VALID_PATTERN_IDS.includes(ruleId)) {
|
|
390
|
-
logger.warn(
|
|
391
|
-
`Unknown rule ID "${ruleId}" in configuration. Valid IDs are: ${VALID_PATTERN_IDS.join(", ")}`,
|
|
392
|
-
"config"
|
|
393
|
-
);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
const newTaxonomy = {};
|
|
397
|
-
for (const [patternId, metadata] of Object.entries(baseTaxonomy)) {
|
|
398
|
-
const ruleConfig = rules[patternId];
|
|
399
|
-
if (ruleConfig?.enabled === false) {
|
|
400
|
-
continue;
|
|
401
|
-
}
|
|
402
|
-
if (ruleConfig?.severity) {
|
|
403
|
-
newTaxonomy[patternId] = {
|
|
404
|
-
...metadata,
|
|
405
|
-
severity: ruleConfig.severity
|
|
406
|
-
};
|
|
407
|
-
} else {
|
|
408
|
-
newTaxonomy[patternId] = metadata;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if (Object.keys(newTaxonomy).length === 0) {
|
|
412
|
-
logger.collectWarning(
|
|
413
|
-
"All analysis rules are disabled in configuration. No patterns will be detected.",
|
|
414
|
-
"config"
|
|
415
|
-
);
|
|
416
|
-
}
|
|
417
|
-
return newTaxonomy;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// src/types/index.ts
|
|
421
|
-
var BATCH_STRATEGIES = {
|
|
422
|
-
// For models < 4GB
|
|
423
|
-
micro: {
|
|
424
|
-
maxTokensPerBatch: 500,
|
|
425
|
-
maxPromptsPerBatch: 3,
|
|
426
|
-
description: "For models < 4GB"
|
|
427
|
-
},
|
|
428
|
-
// For models 4-7GB
|
|
429
|
-
small: {
|
|
430
|
-
maxTokensPerBatch: 1500,
|
|
431
|
-
maxPromptsPerBatch: 10,
|
|
432
|
-
description: "For models 4-7GB"
|
|
433
|
-
},
|
|
434
|
-
// For models > 7GB
|
|
435
|
-
standard: {
|
|
436
|
-
maxTokensPerBatch: 3e3,
|
|
437
|
-
maxPromptsPerBatch: 50,
|
|
438
|
-
description: "For models > 7GB"
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
var PROVIDER_LIMITS = {
|
|
442
|
-
// Small local models struggle with large inputs - limit to ~10 short prompts
|
|
443
|
-
ollama: { maxTokensPerBatch: 3e3, prioritization: "longest-first" },
|
|
444
|
-
anthropic: { maxTokensPerBatch: 1e5, prioritization: "chronological" },
|
|
445
|
-
google: { maxTokensPerBatch: 5e5, prioritization: "chronological" }
|
|
446
|
-
};
|
|
447
|
-
var ENV_DEFAULTS = {
|
|
448
|
-
reminder: "7d",
|
|
449
|
-
ollama: {
|
|
450
|
-
model: "gemma3:4b",
|
|
451
|
-
host: "http://localhost:11434"
|
|
452
|
-
},
|
|
453
|
-
anthropic: {
|
|
454
|
-
model: "claude-3-5-haiku-latest"
|
|
455
|
-
},
|
|
456
|
-
google: {
|
|
457
|
-
model: "gemini-2.0-flash-exp"
|
|
458
|
-
}
|
|
459
|
-
};
|
|
460
|
-
var EXIT_CODES = {
|
|
461
|
-
SUCCESS: 0,
|
|
462
|
-
ERROR: 1,
|
|
463
|
-
NO_DATA: 2,
|
|
464
|
-
PROVIDER_UNAVAILABLE: 3
|
|
465
|
-
};
|
|
466
|
-
var CACHE_DEFAULTS = {
|
|
467
|
-
ttlMs: 7 * 24 * 60 * 60 * 1e3
|
|
468
|
-
// 7 days in milliseconds
|
|
469
|
-
};
|
|
470
|
-
var HOME = homedir();
|
|
471
|
-
var CLAUDE_PROJECTS_DIR = process.env["HYNTX_CLAUDE_PROJECTS_DIR"] ?? join(HOME, ".claude", "projects");
|
|
472
|
-
var LAST_RUN_FILE = join(HOME, ".hyntx-last-run");
|
|
473
|
-
var HYNTX_HISTORY_DIR = process.env["HYNTX_HISTORY_DIR"] ?? join(HOME, ".hyntx", "history");
|
|
474
|
-
var HYNTX_CACHE_DIR = process.env["HYNTX_CACHE_DIR"] ?? join(HOME, ".hyntx-cache");
|
|
475
|
-
var CACHE_ANALYSIS_DIR = join(HYNTX_CACHE_DIR, "analysis");
|
|
476
|
-
var CACHE_META_FILE = join(CACHE_ANALYSIS_DIR, ".metadata.json");
|
|
477
|
-
process.env["HYNTX_RESULTS_DIR"] ?? join(HOME, ".hyntx", "results");
|
|
478
|
-
|
|
479
|
-
// src/cache/analysis-cache.ts
|
|
480
|
-
async function ensureCacheDir() {
|
|
481
|
-
try {
|
|
482
|
-
await mkdir(CACHE_ANALYSIS_DIR, { recursive: true, mode: 448 });
|
|
483
|
-
} catch (error) {
|
|
484
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
485
|
-
throw new Error(
|
|
486
|
-
`Failed to create cache directory ${CACHE_ANALYSIS_DIR}: ${errorMessage}`
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
function getCacheFilePath(cacheKey) {
|
|
491
|
-
return join(CACHE_ANALYSIS_DIR, `${cacheKey}.json`);
|
|
492
|
-
}
|
|
493
|
-
function hashString(input) {
|
|
494
|
-
return createHash("sha256").update(input, "utf-8").digest("hex");
|
|
495
|
-
}
|
|
496
|
-
function generateCacheKey(prompts, model, schemaType = "full") {
|
|
497
|
-
const joined = prompts.join("\n---\n");
|
|
498
|
-
const input = `${model}:${schemaType}:${joined}`;
|
|
499
|
-
return hashString(input);
|
|
500
|
-
}
|
|
501
|
-
function hashSystemPrompt() {
|
|
502
|
-
return hashString(SYSTEM_PROMPT_FULL);
|
|
503
|
-
}
|
|
504
|
-
async function loadCacheMetadata() {
|
|
505
|
-
if (!existsSync(CACHE_META_FILE)) {
|
|
506
|
-
return null;
|
|
507
|
-
}
|
|
508
|
-
try {
|
|
509
|
-
const content = await readFile(CACHE_META_FILE, "utf-8");
|
|
510
|
-
const metadata = JSON.parse(content);
|
|
511
|
-
return metadata;
|
|
512
|
-
} catch (error) {
|
|
513
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
514
|
-
logger.warn(`Failed to load cache metadata: ${errorMessage}`, "cache");
|
|
515
|
-
return null;
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
async function saveCacheMetadata(metadata) {
|
|
519
|
-
try {
|
|
520
|
-
await ensureCacheDir();
|
|
521
|
-
const content = JSON.stringify(metadata, null, 2);
|
|
522
|
-
const tmpFile = `${CACHE_META_FILE}.tmp`;
|
|
523
|
-
await writeFile(tmpFile, content, "utf-8");
|
|
524
|
-
await rename(tmpFile, CACHE_META_FILE);
|
|
525
|
-
logger.debug("Saved cache metadata", "cache");
|
|
526
|
-
} catch (error) {
|
|
527
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
528
|
-
logger.warn(`Failed to save cache metadata: ${errorMessage}`, "cache");
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
async function validateSystemPrompt() {
|
|
532
|
-
const currentHash = hashSystemPrompt();
|
|
533
|
-
const metadata = await loadCacheMetadata();
|
|
534
|
-
if (!metadata) {
|
|
535
|
-
await saveCacheMetadata({
|
|
536
|
-
systemPromptHash: currentHash,
|
|
537
|
-
lastUpdated: Date.now()
|
|
538
|
-
});
|
|
539
|
-
return true;
|
|
540
|
-
}
|
|
541
|
-
if (metadata.systemPromptHash !== currentHash) {
|
|
542
|
-
logger.debug("System prompt changed - invalidating cache", "cache");
|
|
543
|
-
await clearCache();
|
|
544
|
-
await saveCacheMetadata({
|
|
545
|
-
systemPromptHash: currentHash,
|
|
546
|
-
lastUpdated: Date.now()
|
|
547
|
-
});
|
|
548
|
-
return false;
|
|
549
|
-
}
|
|
550
|
-
return true;
|
|
551
|
-
}
|
|
552
|
-
async function getCachedResult(prompts, model, options = CACHE_DEFAULTS) {
|
|
553
|
-
await validateSystemPrompt();
|
|
554
|
-
const cacheKey = generateCacheKey(prompts, model);
|
|
555
|
-
const filePath = getCacheFilePath(cacheKey);
|
|
556
|
-
if (!existsSync(filePath)) {
|
|
557
|
-
logger.debug(`Cache miss for key ${cacheKey}`, "cache");
|
|
558
|
-
return null;
|
|
559
|
-
}
|
|
560
|
-
try {
|
|
561
|
-
const content = await readFile(filePath, "utf-8");
|
|
562
|
-
const data = JSON.parse(content);
|
|
563
|
-
if (!data.metadata || !data.result) {
|
|
564
|
-
logger.debug(`Invalid cache entry for key ${cacheKey}`, "cache");
|
|
565
|
-
return null;
|
|
566
|
-
}
|
|
567
|
-
const cached = data;
|
|
568
|
-
const now = Date.now();
|
|
569
|
-
const age = now - cached.metadata.cachedAt;
|
|
570
|
-
if (age > options.ttlMs) {
|
|
571
|
-
logger.debug(
|
|
572
|
-
`Cache entry expired for key ${cacheKey} (age: ${String(age)}ms)`,
|
|
573
|
-
"cache"
|
|
574
|
-
);
|
|
575
|
-
return null;
|
|
576
|
-
}
|
|
577
|
-
if (cached.metadata.model !== model) {
|
|
578
|
-
logger.debug(
|
|
579
|
-
`Model mismatch for key ${cacheKey} (cached: ${cached.metadata.model}, requested: ${model})`,
|
|
580
|
-
"cache"
|
|
581
|
-
);
|
|
582
|
-
return null;
|
|
583
|
-
}
|
|
584
|
-
if (cached.metadata.promptCount !== prompts.length) {
|
|
585
|
-
logger.debug(`Prompt count mismatch for key ${cacheKey}`, "cache");
|
|
586
|
-
return null;
|
|
587
|
-
}
|
|
588
|
-
logger.debug(`Cache hit for key ${cacheKey}`, "cache");
|
|
589
|
-
return cached.result;
|
|
590
|
-
} catch (error) {
|
|
591
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
592
|
-
logger.warn(
|
|
593
|
-
`Failed to read cache entry ${cacheKey}: ${errorMessage}`,
|
|
594
|
-
"cache"
|
|
595
|
-
);
|
|
596
|
-
return null;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
async function setCachedResult(prompts, model, result) {
|
|
600
|
-
try {
|
|
601
|
-
await ensureCacheDir();
|
|
602
|
-
await validateSystemPrompt();
|
|
603
|
-
const cacheKey = generateCacheKey(prompts, model);
|
|
604
|
-
const filePath = getCacheFilePath(cacheKey);
|
|
605
|
-
const metadata = {
|
|
606
|
-
cachedAt: Date.now(),
|
|
607
|
-
promptCount: prompts.length,
|
|
608
|
-
model,
|
|
609
|
-
systemPromptHash: hashSystemPrompt()
|
|
610
|
-
};
|
|
611
|
-
const cached = {
|
|
612
|
-
result,
|
|
613
|
-
metadata
|
|
614
|
-
};
|
|
615
|
-
const content = JSON.stringify(cached, null, 2);
|
|
616
|
-
const tmpFile = `${filePath}.tmp`;
|
|
617
|
-
await writeFile(tmpFile, content, "utf-8");
|
|
618
|
-
await rename(tmpFile, filePath);
|
|
619
|
-
logger.debug(`Cached result for key ${cacheKey}`, "cache");
|
|
620
|
-
} catch (error) {
|
|
621
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
622
|
-
logger.warn(`Failed to cache result: ${errorMessage}`, "cache");
|
|
623
|
-
}
|
|
624
|
-
}
|
|
625
|
-
async function clearCache() {
|
|
626
|
-
if (!existsSync(CACHE_ANALYSIS_DIR)) {
|
|
627
|
-
logger.debug("Cache directory does not exist - nothing to clear", "cache");
|
|
628
|
-
return;
|
|
629
|
-
}
|
|
630
|
-
try {
|
|
631
|
-
await rm(CACHE_ANALYSIS_DIR, { recursive: true, force: true });
|
|
632
|
-
logger.debug("Cache cleared", "cache");
|
|
633
|
-
} catch (error) {
|
|
634
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
635
|
-
logger.warn(`Failed to clear cache: ${errorMessage}`, "cache");
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
async function cleanupExpiredEntries(options = CACHE_DEFAULTS) {
|
|
639
|
-
if (!existsSync(CACHE_ANALYSIS_DIR)) {
|
|
640
|
-
return 0;
|
|
641
|
-
}
|
|
642
|
-
try {
|
|
643
|
-
const files = readdirSync(CACHE_ANALYSIS_DIR);
|
|
644
|
-
const now = Date.now();
|
|
645
|
-
let removedCount = 0;
|
|
646
|
-
for (const file of files) {
|
|
647
|
-
if (!file.endsWith(".json") || file === ".metadata.json") {
|
|
648
|
-
continue;
|
|
649
|
-
}
|
|
650
|
-
const filePath = join(CACHE_ANALYSIS_DIR, file);
|
|
651
|
-
try {
|
|
652
|
-
const content = await readFile(filePath, "utf-8");
|
|
653
|
-
const cached = JSON.parse(content);
|
|
654
|
-
const age = now - cached.metadata.cachedAt;
|
|
655
|
-
if (age > options.ttlMs) {
|
|
656
|
-
await rm(filePath, { force: true });
|
|
657
|
-
removedCount++;
|
|
658
|
-
logger.debug(`Removed expired cache entry: ${file}`, "cache");
|
|
659
|
-
}
|
|
660
|
-
} catch {
|
|
661
|
-
await rm(filePath, { force: true });
|
|
662
|
-
removedCount++;
|
|
663
|
-
logger.debug(`Removed invalid cache entry: ${file}`, "cache");
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
if (removedCount > 0) {
|
|
667
|
-
logger.debug(
|
|
668
|
-
`Cleanup removed ${String(removedCount)} expired entries`,
|
|
669
|
-
"cache"
|
|
670
|
-
);
|
|
671
|
-
}
|
|
672
|
-
return removedCount;
|
|
673
|
-
} catch (error) {
|
|
674
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
675
|
-
logger.warn(`Failed to cleanup expired entries: ${errorMessage}`, "cache");
|
|
676
|
-
return 0;
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
// src/core/sanitizer.ts
|
|
681
|
-
function redactOpenAIKeys(text) {
|
|
682
|
-
return text.replace(/sk-[a-zA-Z0-9]{48,}/g, "[REDACTED_OPENAI_KEY]");
|
|
683
|
-
}
|
|
684
|
-
function redactAnthropicKeys(text) {
|
|
685
|
-
return text.replace(/sk-ant-[a-zA-Z0-9-]+/g, "[REDACTED_ANTHROPIC_KEY]");
|
|
686
|
-
}
|
|
687
|
-
function redactAWSCredentials(text) {
|
|
688
|
-
return text.replace(/AKIA[0-9A-Z]{16}/g, "[REDACTED_AWS_KEY]");
|
|
689
|
-
}
|
|
690
|
-
function redactBearerTokens(text) {
|
|
691
|
-
return text.replace(
|
|
692
|
-
/Bearer(?:\s+token)?:?\s+[a-zA-Z0-9._-]{20,}/gi,
|
|
693
|
-
"Bearer [REDACTED_TOKEN]"
|
|
694
|
-
);
|
|
695
|
-
}
|
|
696
|
-
function redactURLCredentials(text) {
|
|
697
|
-
return text.replace(
|
|
698
|
-
/(https?:\/\/)([^:\s@]+)(?::([^\s@]+))?@/g,
|
|
699
|
-
(match, protocol, _user, _pass) => {
|
|
700
|
-
return `${protocol}[REDACTED_URL_CREDENTIAL]@`;
|
|
701
|
-
}
|
|
702
|
-
);
|
|
703
|
-
}
|
|
704
|
-
function redactEmails(text) {
|
|
705
|
-
return text.replace(
|
|
706
|
-
/[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,
|
|
707
|
-
"[REDACTED_EMAIL]"
|
|
708
|
-
);
|
|
709
|
-
}
|
|
710
|
-
function redactPEMKeys(text) {
|
|
711
|
-
return text.replace(
|
|
712
|
-
/-----BEGIN\s+[A-Z\s]+PRIVATE\s+KEY-----[\s\S]*?-----END\s+[A-Z\s]+PRIVATE\s+KEY-----/g,
|
|
713
|
-
"[REDACTED_PEM_KEY]"
|
|
714
|
-
);
|
|
715
|
-
}
|
|
716
|
-
function redactNames(text) {
|
|
717
|
-
return text.replace(
|
|
718
|
-
/\b(?:Hi|Hello|Hey|Dear|Greetings|Good\s+(?:morning|afternoon|evening))\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)?(?:\s+[A-Z][a-z]+)?)\b/gi,
|
|
719
|
-
(match, name) => {
|
|
720
|
-
const commonWords = /\b(?:there|everyone|all|team|guys|folks|people)\b/i;
|
|
721
|
-
if (name && name.length >= 2 && !commonWords.test(name)) {
|
|
722
|
-
return match.replace(name, "[REDACTED_NAME]");
|
|
723
|
-
}
|
|
724
|
-
return match;
|
|
725
|
-
}
|
|
726
|
-
);
|
|
727
|
-
}
|
|
728
|
-
function redactPhoneNumbers(text) {
|
|
729
|
-
return text.replace(
|
|
730
|
-
/(?:\+?1[-.\s]?)?\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})\b/g,
|
|
731
|
-
"[REDACTED_PHONE]"
|
|
732
|
-
);
|
|
733
|
-
}
|
|
734
|
-
function redactCreditCards(text) {
|
|
735
|
-
return text.replace(
|
|
736
|
-
/\b(?:(?:4\d{3}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{1,4})|(?:5[1-5]\d{2}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4})|(?:3[47]\d{2}[\s-]?\d{6}[\s-]?\d{5})|(?:3[068]\d[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}))\b/g,
|
|
737
|
-
"[REDACTED_CREDIT_CARD]"
|
|
738
|
-
);
|
|
739
|
-
}
|
|
740
|
-
function redactSSN(text) {
|
|
741
|
-
return text.replace(
|
|
742
|
-
/\b(?!000|666|9\d{2})([0-9]{3})[-.\s]?(?!00)([0-9]{2})[-.\s]?(?!0000)([0-9]{4})\b/g,
|
|
743
|
-
"[REDACTED_SSN]"
|
|
744
|
-
);
|
|
745
|
-
}
|
|
746
|
-
function redactSpanishDNI(text) {
|
|
747
|
-
return text.replace(/\b\d{8}[A-Z]\b/gi, "[REDACTED_SPANISH_DNI]");
|
|
748
|
-
}
|
|
749
|
-
function redactSpanishNIE(text) {
|
|
750
|
-
return text.replace(/\b[XYZ]\d{7}[A-Z]\b/gi, "[REDACTED_SPANISH_NIE]");
|
|
751
|
-
}
|
|
752
|
-
function redactMexicanCURP(text) {
|
|
753
|
-
return text.replace(
|
|
754
|
-
/\b[A-Z]{4}\d{6}[HM][A-Z]{5}[0-9A-Z]{2}\b/gi,
|
|
755
|
-
"[REDACTED_MEXICAN_CURP]"
|
|
756
|
-
);
|
|
757
|
-
}
|
|
758
|
-
function redactBrazilianCPF(text) {
|
|
759
|
-
return text.replace(
|
|
760
|
-
/\b\d{3}\.?\d{3}\.?\d{3}-?\d{2}\b/g,
|
|
761
|
-
"[REDACTED_BRAZILIAN_CPF]"
|
|
762
|
-
);
|
|
763
|
-
}
|
|
764
|
-
function redactUKNINO(text) {
|
|
765
|
-
return text.replace(
|
|
766
|
-
/\b[A-Z]{2}\s?\d{2}\s?\d{2}\s?\d{2}\s?[A-Z]\b/gi,
|
|
767
|
-
"[REDACTED_UK_NINO]"
|
|
768
|
-
);
|
|
769
|
-
}
|
|
770
|
-
function redactChileanRUN(text) {
|
|
771
|
-
return text.replace(
|
|
772
|
-
/\b(?:RUN|RUT)[\s:]*([0-9]{7,8}-[0-9K]|[0-9]{8}[0-9K])\b/gi,
|
|
773
|
-
(match, run) => {
|
|
774
|
-
return match.replace(run, "[REDACTED_CHILEAN_RUN]");
|
|
775
|
-
}
|
|
776
|
-
);
|
|
777
|
-
}
|
|
778
|
-
function redactArgentineDNI(text) {
|
|
779
|
-
return text.replace(
|
|
780
|
-
/\b(?:DNI|dni)[\s:]*(\d{8})\b/gi,
|
|
781
|
-
(match, dni) => {
|
|
782
|
-
return match.replace(dni, "[REDACTED_ARGENTINE_DNI]");
|
|
783
|
-
}
|
|
784
|
-
);
|
|
785
|
-
}
|
|
786
|
-
function redactIPv4(text) {
|
|
787
|
-
return text.replace(
|
|
788
|
-
/\b(?!(?:127\.0\.0\.1|0\.0\.0\.0|255\.255\.255\.255))(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g,
|
|
789
|
-
"[REDACTED_IPV4]"
|
|
790
|
-
);
|
|
791
|
-
}
|
|
792
|
-
function redactIPv6(text) {
|
|
793
|
-
return text.replace(
|
|
794
|
-
/\b(?!(?:::1|::))(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b|\b(?:[0-9a-fA-F]{1,4}:){1,7}::\b|\b::(?:[0-9a-fA-F]{1,4}:){1,7}[0-9a-fA-F]{1,4}\b/g,
|
|
795
|
-
"[REDACTED_IPV6]"
|
|
796
|
-
);
|
|
797
|
-
}
|
|
798
|
-
function redactMACAddresses(text) {
|
|
799
|
-
return text.replace(
|
|
800
|
-
/\b(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})\b/g,
|
|
801
|
-
"[REDACTED_MAC_ADDRESS]"
|
|
802
|
-
);
|
|
803
|
-
}
|
|
804
|
-
function redactPassportNumbers(text) {
|
|
805
|
-
return text.replace(
|
|
806
|
-
/\b(?:passport|pasaporte)[\s:]*([A-Z0-9]{6,9})\b/gi,
|
|
807
|
-
(match, passport) => {
|
|
808
|
-
return match.replace(passport, "[REDACTED_PASSPORT]");
|
|
809
|
-
}
|
|
810
|
-
);
|
|
811
|
-
}
|
|
812
|
-
function redactDriverLicenseNumbers(text) {
|
|
813
|
-
return text.replace(
|
|
814
|
-
/\b(?:driver\s*license|licencia\s*de\s*conducir|DL|D\.L\.)[\s:]*([A-Z0-9]{8,16})\b/gi,
|
|
815
|
-
(match, license) => {
|
|
816
|
-
return match.replace(license, "[REDACTED_DRIVER_LICENSE]");
|
|
817
|
-
}
|
|
818
|
-
);
|
|
819
|
-
}
|
|
820
|
-
function redactAddresses(text) {
|
|
821
|
-
let sanitized = text;
|
|
822
|
-
sanitized = sanitized.replace(/\b\d{5}(?:-\d{4})?\b/g, "[REDACTED_ZIP_CODE]");
|
|
823
|
-
sanitized = sanitized.replace(
|
|
824
|
-
/\b[A-Z]{1,2}\d{1,2}[A-Z]?\s?\d[A-Z]{2}\b/gi,
|
|
825
|
-
"[REDACTED_POSTCODE]"
|
|
826
|
-
);
|
|
827
|
-
sanitized = sanitized.replace(
|
|
828
|
-
/\b[A-Z]\d[A-Z]\s?\d[A-Z]\d\b/gi,
|
|
829
|
-
"[REDACTED_POSTAL_CODE]"
|
|
830
|
-
);
|
|
831
|
-
sanitized = sanitized.replace(
|
|
832
|
-
/\b(?:address|direcci[oó]n|street|calle|avenue|ave)[\s:]*(\d+\s+[A-Za-z\s]+(?:street|st|avenue|ave|road|rd|boulevard|blvd|drive|dr|lane|ln|way|circle|cir))?\b/gi,
|
|
833
|
-
(match) => {
|
|
834
|
-
return match.replace(
|
|
835
|
-
/\d+\s+[A-Za-z\s]+(?:street|st|avenue|ave|road|rd|boulevard|blvd|drive|dr|lane|ln|way|circle|cir)/gi,
|
|
836
|
-
"[REDACTED_ADDRESS]"
|
|
837
|
-
);
|
|
838
|
-
}
|
|
839
|
-
);
|
|
840
|
-
return sanitized;
|
|
841
|
-
}
|
|
842
|
-
function redactBirthDates(text) {
|
|
843
|
-
return text.replace(
|
|
844
|
-
/\b(?:DOB|date\s+of\s+birth|fecha\s+de\s+nacimiento|born)[\s:]*(\d{1,2}[-/.]\d{1,2}[-/.]\d{2,4})\b/gi,
|
|
845
|
-
(match, date) => {
|
|
846
|
-
return match.replace(date, "[REDACTED_BIRTH_DATE]");
|
|
847
|
-
}
|
|
848
|
-
);
|
|
849
|
-
}
|
|
850
|
-
function countRedactions(original, sanitized) {
|
|
851
|
-
const redactionPattern = /\[REDACTED_[^\]]+\]/g;
|
|
852
|
-
const matches = sanitized.match(redactionPattern);
|
|
853
|
-
return matches ? matches.length : 0;
|
|
854
|
-
}
|
|
855
|
-
function sanitize(text) {
|
|
856
|
-
if (!text || text.trim().length === 0) {
|
|
857
|
-
return { text, redacted: 0 };
|
|
858
|
-
}
|
|
859
|
-
let sanitized = text;
|
|
860
|
-
sanitized = redactOpenAIKeys(sanitized);
|
|
861
|
-
sanitized = redactAnthropicKeys(sanitized);
|
|
862
|
-
sanitized = redactAWSCredentials(sanitized);
|
|
863
|
-
sanitized = redactBearerTokens(sanitized);
|
|
864
|
-
sanitized = redactURLCredentials(sanitized);
|
|
865
|
-
sanitized = redactPEMKeys(sanitized);
|
|
866
|
-
sanitized = redactEmails(sanitized);
|
|
867
|
-
sanitized = redactCreditCards(sanitized);
|
|
868
|
-
sanitized = redactPassportNumbers(sanitized);
|
|
869
|
-
sanitized = redactDriverLicenseNumbers(sanitized);
|
|
870
|
-
sanitized = redactSpanishDNI(sanitized);
|
|
871
|
-
sanitized = redactSpanishNIE(sanitized);
|
|
872
|
-
sanitized = redactMexicanCURP(sanitized);
|
|
873
|
-
sanitized = redactBrazilianCPF(sanitized);
|
|
874
|
-
sanitized = redactUKNINO(sanitized);
|
|
875
|
-
sanitized = redactChileanRUN(sanitized);
|
|
876
|
-
sanitized = redactArgentineDNI(sanitized);
|
|
877
|
-
sanitized = redactAddresses(sanitized);
|
|
878
|
-
sanitized = redactBirthDates(sanitized);
|
|
879
|
-
sanitized = redactNames(sanitized);
|
|
880
|
-
sanitized = redactSSN(sanitized);
|
|
881
|
-
sanitized = redactPhoneNumbers(sanitized);
|
|
882
|
-
sanitized = redactIPv4(sanitized);
|
|
883
|
-
sanitized = redactIPv6(sanitized);
|
|
884
|
-
sanitized = redactMACAddresses(sanitized);
|
|
885
|
-
const redacted = countRedactions(text, sanitized);
|
|
886
|
-
return {
|
|
887
|
-
text: sanitized,
|
|
888
|
-
redacted
|
|
889
|
-
};
|
|
890
|
-
}
|
|
891
|
-
function sanitizePrompts(prompts) {
|
|
892
|
-
if (prompts.length === 0) {
|
|
893
|
-
return { prompts: [], totalRedacted: 0 };
|
|
894
|
-
}
|
|
895
|
-
logger.debug(
|
|
896
|
-
`Sanitizing ${String(prompts.length)} prompts for secrets`,
|
|
897
|
-
"sanitizer"
|
|
898
|
-
);
|
|
899
|
-
const sanitizedPrompts = [];
|
|
900
|
-
let totalRedacted = 0;
|
|
901
|
-
for (const prompt of prompts) {
|
|
902
|
-
const result = sanitize(prompt);
|
|
903
|
-
sanitizedPrompts.push(result.text);
|
|
904
|
-
totalRedacted += result.redacted;
|
|
905
|
-
}
|
|
906
|
-
if (totalRedacted > 0) {
|
|
907
|
-
logger.debug(
|
|
908
|
-
`Redacted ${String(totalRedacted)} secret(s) from prompts`,
|
|
909
|
-
"sanitizer"
|
|
910
|
-
);
|
|
911
|
-
} else {
|
|
912
|
-
logger.debug("No secrets found to redact", "sanitizer");
|
|
913
|
-
}
|
|
914
|
-
return {
|
|
915
|
-
prompts: sanitizedPrompts,
|
|
916
|
-
totalRedacted
|
|
917
|
-
};
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
// src/core/analyzer.ts
|
|
921
|
-
var SYSTEM_OVERHEAD_TOKENS = 2e3;
|
|
922
|
-
var SEVERITY_RANK = {
|
|
923
|
-
high: 3,
|
|
924
|
-
medium: 2,
|
|
925
|
-
low: 1
|
|
926
|
-
};
|
|
927
|
-
var MAX_PATTERNS = 5;
|
|
928
|
-
var MAX_EXAMPLES_PER_PATTERN = 3;
|
|
929
|
-
var CHARS_PER_TOKEN = 4;
|
|
930
|
-
function hasDynamicLimits(provider) {
|
|
931
|
-
return typeof provider.getBatchLimits === "function";
|
|
932
|
-
}
|
|
933
|
-
function shouldFallback(error) {
|
|
934
|
-
const message = error.message.toLowerCase();
|
|
935
|
-
return message.includes("parse") || message.includes("schema") || message.includes("json") || message.includes("timeout") || message.includes("network") || message.includes("failed");
|
|
936
|
-
}
|
|
937
|
-
function average(numbers) {
|
|
938
|
-
if (numbers.length === 0) return 0;
|
|
939
|
-
return sum(numbers) / numbers.length;
|
|
940
|
-
}
|
|
941
|
-
function sum(numbers) {
|
|
942
|
-
return numbers.reduce((acc, n) => acc + n, 0);
|
|
943
|
-
}
|
|
944
|
-
function flattenAll(arrays) {
|
|
945
|
-
const result = [];
|
|
946
|
-
for (const arr of arrays) {
|
|
947
|
-
for (const item of arr) {
|
|
948
|
-
result.push(item);
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
return result;
|
|
952
|
-
}
|
|
953
|
-
function limitExamples(examples, max) {
|
|
954
|
-
return examples.slice(0, max);
|
|
955
|
-
}
|
|
956
|
-
function inferProviderType(providerName) {
|
|
957
|
-
const nameLower = providerName.toLowerCase();
|
|
958
|
-
if (nameLower.includes("ollama")) return "ollama";
|
|
959
|
-
if (nameLower.includes("anthropic") || nameLower.includes("claude"))
|
|
960
|
-
return "anthropic";
|
|
961
|
-
if (nameLower.includes("google") || nameLower.includes("gemini"))
|
|
962
|
-
return "google";
|
|
963
|
-
return "ollama";
|
|
964
|
-
}
|
|
965
|
-
function extractModelFromProvider(providerName) {
|
|
966
|
-
const match = /\((.*?)\)/.exec(providerName);
|
|
967
|
-
return match?.[1] ?? providerName;
|
|
968
|
-
}
|
|
969
|
-
function maxSeverity(severities) {
|
|
970
|
-
if (severities.length === 0) return "low";
|
|
971
|
-
let max = "low";
|
|
972
|
-
let maxRank = SEVERITY_RANK.low;
|
|
973
|
-
for (const severity of severities) {
|
|
974
|
-
const rank = SEVERITY_RANK[severity];
|
|
975
|
-
if (rank > maxRank) {
|
|
976
|
-
max = severity;
|
|
977
|
-
maxRank = rank;
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
return max;
|
|
981
|
-
}
|
|
982
|
-
async function analyzeWithFallback(provider, prompts, date, context, model, noCache) {
|
|
983
|
-
if (prompts.length === 0) {
|
|
984
|
-
return [];
|
|
985
|
-
}
|
|
986
|
-
try {
|
|
987
|
-
if (!noCache) {
|
|
988
|
-
const cachedResult = await getCachedResult(
|
|
989
|
-
prompts,
|
|
990
|
-
model,
|
|
991
|
-
CACHE_DEFAULTS
|
|
992
|
-
);
|
|
993
|
-
if (cachedResult) {
|
|
994
|
-
logger.debug(
|
|
995
|
-
`Using cached result for batch of ${String(prompts.length)} prompt(s)`,
|
|
996
|
-
"analyzer"
|
|
997
|
-
);
|
|
998
|
-
return [{ ...cachedResult, date }];
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
const result = await provider.analyze(prompts, date, context);
|
|
1002
|
-
if (!noCache) {
|
|
1003
|
-
await setCachedResult(prompts, model, result);
|
|
1004
|
-
}
|
|
1005
|
-
return [result];
|
|
1006
|
-
} catch (error) {
|
|
1007
|
-
const err = error instanceof Error ? error : new Error(String(error));
|
|
1008
|
-
if (shouldFallback(err)) {
|
|
1009
|
-
if (prompts.length > 1) {
|
|
1010
|
-
const mid = Math.ceil(prompts.length / 2);
|
|
1011
|
-
const leftPrompts = prompts.slice(0, mid);
|
|
1012
|
-
const rightPrompts = prompts.slice(mid);
|
|
1013
|
-
logger.debug(
|
|
1014
|
-
`Batch of ${String(prompts.length)} failed, splitting into ${String(leftPrompts.length)} + ${String(rightPrompts.length)}`,
|
|
1015
|
-
"analyzer"
|
|
1016
|
-
);
|
|
1017
|
-
const [leftResults, rightResults] = await Promise.all([
|
|
1018
|
-
analyzeWithFallback(
|
|
1019
|
-
provider,
|
|
1020
|
-
leftPrompts,
|
|
1021
|
-
date,
|
|
1022
|
-
context,
|
|
1023
|
-
model,
|
|
1024
|
-
noCache
|
|
1025
|
-
),
|
|
1026
|
-
analyzeWithFallback(
|
|
1027
|
-
provider,
|
|
1028
|
-
rightPrompts,
|
|
1029
|
-
date,
|
|
1030
|
-
context,
|
|
1031
|
-
model,
|
|
1032
|
-
noCache
|
|
1033
|
-
)
|
|
1034
|
-
]);
|
|
1035
|
-
return [...leftResults, ...rightResults];
|
|
1036
|
-
}
|
|
1037
|
-
const promptPreview = prompts[0]?.slice(0, 50) ?? "unknown";
|
|
1038
|
-
logger.warn(
|
|
1039
|
-
`Skipped single prompt due to error: ${promptPreview}... (${err.message})`,
|
|
1040
|
-
"analyzer"
|
|
1041
|
-
);
|
|
1042
|
-
return [];
|
|
1043
|
-
}
|
|
1044
|
-
throw err;
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
function estimateTokens(text) {
|
|
1048
|
-
if (!text || text.length === 0) return 0;
|
|
1049
|
-
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
1050
|
-
}
|
|
1051
|
-
function batchPrompts(options) {
|
|
1052
|
-
const { prompts, maxTokensPerBatch, maxPromptsPerBatch, prioritization } = options;
|
|
1053
|
-
if (prompts.length === 0) {
|
|
1054
|
-
return [];
|
|
1055
|
-
}
|
|
1056
|
-
const effectiveLimit = maxTokensPerBatch - SYSTEM_OVERHEAD_TOKENS;
|
|
1057
|
-
const promptsWithTokens = prompts.map((prompt) => ({
|
|
1058
|
-
prompt,
|
|
1059
|
-
tokens: estimateTokens(prompt)
|
|
1060
|
-
}));
|
|
1061
|
-
const sorted = prioritization === "longest-first" ? [...promptsWithTokens].sort((a, b) => b.tokens - a.tokens) : promptsWithTokens;
|
|
1062
|
-
const batches = [];
|
|
1063
|
-
let currentBatch = [];
|
|
1064
|
-
let currentTokens = 0;
|
|
1065
|
-
for (const { prompt, tokens } of sorted) {
|
|
1066
|
-
if (tokens > effectiveLimit) {
|
|
1067
|
-
if (currentBatch.length > 0) {
|
|
1068
|
-
batches.push({ prompts: currentBatch, tokens: currentTokens });
|
|
1069
|
-
currentBatch = [];
|
|
1070
|
-
currentTokens = 0;
|
|
1071
|
-
}
|
|
1072
|
-
batches.push({ prompts: [prompt], tokens });
|
|
1073
|
-
continue;
|
|
1074
|
-
}
|
|
1075
|
-
const wouldExceedTokens = currentTokens + tokens > effectiveLimit && currentBatch.length > 0;
|
|
1076
|
-
const wouldExceedPrompts = maxPromptsPerBatch !== void 0 && currentBatch.length >= maxPromptsPerBatch;
|
|
1077
|
-
if (wouldExceedTokens || wouldExceedPrompts) {
|
|
1078
|
-
batches.push({ prompts: currentBatch, tokens: currentTokens });
|
|
1079
|
-
currentBatch = [prompt];
|
|
1080
|
-
currentTokens = tokens;
|
|
1081
|
-
} else {
|
|
1082
|
-
currentBatch.push(prompt);
|
|
1083
|
-
currentTokens += tokens;
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
if (currentBatch.length > 0) {
|
|
1087
|
-
batches.push({ prompts: currentBatch, tokens: currentTokens });
|
|
1088
|
-
}
|
|
1089
|
-
return batches;
|
|
1090
|
-
}
|
|
1091
|
-
function mergeBatchResults(options) {
|
|
1092
|
-
const { results, date } = options;
|
|
1093
|
-
if (results.length === 0) {
|
|
1094
|
-
throw new Error("Cannot merge empty results array");
|
|
1095
|
-
}
|
|
1096
|
-
if (results.length === 1) {
|
|
1097
|
-
const result = results[0];
|
|
1098
|
-
if (!result) {
|
|
1099
|
-
throw new Error("Results array is empty");
|
|
1100
|
-
}
|
|
1101
|
-
const limitedPatterns = result.patterns.map((p) => ({
|
|
1102
|
-
...p,
|
|
1103
|
-
examples: limitExamples(p.examples, MAX_EXAMPLES_PER_PATTERN)
|
|
1104
|
-
})).sort((a, b) => {
|
|
1105
|
-
const severityDiff = SEVERITY_RANK[b.severity] - SEVERITY_RANK[a.severity];
|
|
1106
|
-
if (severityDiff !== 0) return severityDiff;
|
|
1107
|
-
return b.frequency - a.frequency;
|
|
1108
|
-
}).slice(0, MAX_PATTERNS);
|
|
1109
|
-
const topSuggestion2 = limitedPatterns.length > 0 && limitedPatterns[0] ? limitedPatterns[0].suggestion : result.topSuggestion;
|
|
1110
|
-
return {
|
|
1111
|
-
...result,
|
|
1112
|
-
date,
|
|
1113
|
-
patterns: limitedPatterns,
|
|
1114
|
-
topSuggestion: topSuggestion2
|
|
1115
|
-
};
|
|
1116
|
-
}
|
|
1117
|
-
const allPatterns = flattenAll(results.map((r) => r.patterns));
|
|
1118
|
-
const patternGroups = /* @__PURE__ */ new Map();
|
|
1119
|
-
for (const pattern of allPatterns) {
|
|
1120
|
-
const group = patternGroups.get(pattern.id) ?? [];
|
|
1121
|
-
group.push(pattern);
|
|
1122
|
-
patternGroups.set(pattern.id, group);
|
|
1123
|
-
}
|
|
1124
|
-
const mergedPatterns = [];
|
|
1125
|
-
for (const group of patternGroups.values()) {
|
|
1126
|
-
if (group.length === 0) continue;
|
|
1127
|
-
const firstPattern = group[0];
|
|
1128
|
-
if (!firstPattern) continue;
|
|
1129
|
-
if (group.length === 1) {
|
|
1130
|
-
mergedPatterns.push({
|
|
1131
|
-
...firstPattern,
|
|
1132
|
-
examples: limitExamples(
|
|
1133
|
-
firstPattern.examples,
|
|
1134
|
-
MAX_EXAMPLES_PER_PATTERN
|
|
1135
|
-
)
|
|
1136
|
-
});
|
|
1137
|
-
} else {
|
|
1138
|
-
const frequencies = group.map((p) => p.frequency);
|
|
1139
|
-
const severities = group.map((p) => p.severity);
|
|
1140
|
-
const allExamples = group.flatMap((p) => Array.from(p.examples));
|
|
1141
|
-
const mergedFrequency = Math.round(average(frequencies));
|
|
1142
|
-
const mergedSeverity = maxSeverity(severities);
|
|
1143
|
-
const mergedExamples = limitExamples(
|
|
1144
|
-
allExamples,
|
|
1145
|
-
MAX_EXAMPLES_PER_PATTERN
|
|
1146
|
-
);
|
|
1147
|
-
mergedPatterns.push({
|
|
1148
|
-
...firstPattern,
|
|
1149
|
-
frequency: mergedFrequency,
|
|
1150
|
-
severity: mergedSeverity,
|
|
1151
|
-
examples: mergedExamples
|
|
1152
|
-
});
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
const sortedPatterns = mergedPatterns.sort((a, b) => {
|
|
1156
|
-
const severityDiff = SEVERITY_RANK[b.severity] - SEVERITY_RANK[a.severity];
|
|
1157
|
-
if (severityDiff !== 0) return severityDiff;
|
|
1158
|
-
return b.frequency - a.frequency;
|
|
1159
|
-
});
|
|
1160
|
-
const topPatterns = sortedPatterns.slice(0, MAX_PATTERNS);
|
|
1161
|
-
const totalPrompts = sum(results.map((r) => r.stats.totalPrompts));
|
|
1162
|
-
const promptsWithIssues = sum(results.map((r) => r.stats.promptsWithIssues));
|
|
1163
|
-
const avgScore = Math.round(
|
|
1164
|
-
average(results.map((r) => r.stats.overallScore))
|
|
1165
|
-
);
|
|
1166
|
-
const topSuggestion = topPatterns.length > 0 && topPatterns[0] ? topPatterns[0].suggestion : results[0]?.topSuggestion ?? "No suggestions available";
|
|
1167
|
-
return {
|
|
1168
|
-
date,
|
|
1169
|
-
patterns: topPatterns,
|
|
1170
|
-
stats: {
|
|
1171
|
-
totalPrompts,
|
|
1172
|
-
promptsWithIssues,
|
|
1173
|
-
overallScore: avgScore
|
|
1174
|
-
},
|
|
1175
|
-
topSuggestion
|
|
1176
|
-
};
|
|
1177
|
-
}
|
|
1178
|
-
function applyRulesToResult(result, rules) {
|
|
1179
|
-
if (!rules || Object.keys(rules).length === 0) {
|
|
1180
|
-
return result;
|
|
1181
|
-
}
|
|
1182
|
-
const customTaxonomy = applyRulesConfig(rules, ISSUE_TAXONOMY);
|
|
1183
|
-
const enabledPatternIds = Object.keys(customTaxonomy);
|
|
1184
|
-
const filteredPatterns = result.patterns.filter(
|
|
1185
|
-
(pattern) => enabledPatternIds.includes(pattern.id)
|
|
1186
|
-
);
|
|
1187
|
-
const updatedPatterns = filteredPatterns.map((pattern) => {
|
|
1188
|
-
const ruleConfig = rules[pattern.id];
|
|
1189
|
-
if (ruleConfig?.severity) {
|
|
1190
|
-
return {
|
|
1191
|
-
...pattern,
|
|
1192
|
-
severity: ruleConfig.severity
|
|
1193
|
-
};
|
|
1194
|
-
}
|
|
1195
|
-
return pattern;
|
|
1196
|
-
});
|
|
1197
|
-
const topSuggestion = updatedPatterns.length > 0 && updatedPatterns[0] ? updatedPatterns[0].suggestion : result.topSuggestion;
|
|
1198
|
-
return {
|
|
1199
|
-
...result,
|
|
1200
|
-
patterns: updatedPatterns,
|
|
1201
|
-
topSuggestion
|
|
1202
|
-
};
|
|
1203
|
-
}
|
|
1204
|
-
function mergeWithCachedResults(newResults, cachedResults, date, rules) {
|
|
1205
|
-
if (!cachedResults || cachedResults.size === 0) {
|
|
1206
|
-
if (newResults.length === 1) {
|
|
1207
|
-
const result = newResults[0];
|
|
1208
|
-
if (result) {
|
|
1209
|
-
return applyRulesToResult(result, rules);
|
|
1210
|
-
}
|
|
1211
|
-
}
|
|
1212
|
-
const mergedResult2 = mergeBatchResults({ results: newResults, date });
|
|
1213
|
-
return applyRulesToResult(mergedResult2, rules);
|
|
1214
|
-
}
|
|
1215
|
-
const cachedArray = Array.from(cachedResults.values());
|
|
1216
|
-
const allResults = [...cachedArray, ...newResults];
|
|
1217
|
-
logger.debug(
|
|
1218
|
-
`Merging ${String(cachedArray.length)} cached + ${String(newResults.length)} new results`,
|
|
1219
|
-
"analyzer"
|
|
1220
|
-
);
|
|
1221
|
-
const mergedResult = mergeBatchResults({ results: allResults, date });
|
|
1222
|
-
return applyRulesToResult(mergedResult, rules);
|
|
1223
|
-
}
|
|
1224
|
-
async function analyzePrompts(options) {
|
|
1225
|
-
const {
|
|
1226
|
-
provider,
|
|
1227
|
-
prompts,
|
|
1228
|
-
date,
|
|
1229
|
-
context,
|
|
1230
|
-
rules,
|
|
1231
|
-
onProgress,
|
|
1232
|
-
noCache,
|
|
1233
|
-
cachedResults
|
|
1234
|
-
} = options;
|
|
1235
|
-
logger.debug(
|
|
1236
|
-
`Starting analysis of ${String(prompts.length)} prompts for ${date}`,
|
|
1237
|
-
"analyzer"
|
|
1238
|
-
);
|
|
1239
|
-
if (prompts.length === 0 && cachedResults && cachedResults.size > 0) {
|
|
1240
|
-
logger.debug(
|
|
1241
|
-
`Using ${String(cachedResults.size)} cached result(s), no new prompts to analyze`,
|
|
1242
|
-
"analyzer"
|
|
1243
|
-
);
|
|
1244
|
-
const cachedArray = Array.from(cachedResults.values());
|
|
1245
|
-
const mergedResult = mergeBatchResults({ results: cachedArray, date });
|
|
1246
|
-
return applyRulesToResult(mergedResult, rules);
|
|
1247
|
-
}
|
|
1248
|
-
if (prompts.length === 0) {
|
|
1249
|
-
throw new Error("Cannot analyze empty prompts array");
|
|
1250
|
-
}
|
|
1251
|
-
if (cachedResults && cachedResults.size > 0) {
|
|
1252
|
-
const totalPrompts = prompts.length + cachedResults.size;
|
|
1253
|
-
const cacheHitRate = (cachedResults.size / totalPrompts * 100).toFixed(1);
|
|
1254
|
-
logger.debug(
|
|
1255
|
-
`Results cache: ${String(cachedResults.size)} cached, ${String(prompts.length)} to analyze (${cacheHitRate}% hit rate)`,
|
|
1256
|
-
"analyzer"
|
|
1257
|
-
);
|
|
1258
|
-
}
|
|
1259
|
-
const { prompts: sanitizedPrompts, totalRedacted } = sanitizePrompts(prompts);
|
|
1260
|
-
if (totalRedacted > 0) {
|
|
1261
|
-
logger.debug(
|
|
1262
|
-
`Sanitizer redacted ${String(totalRedacted)} secrets`,
|
|
1263
|
-
"analyzer"
|
|
1264
|
-
);
|
|
1265
|
-
}
|
|
1266
|
-
const limits = hasDynamicLimits(provider) ? provider.getBatchLimits() : PROVIDER_LIMITS[inferProviderType(provider.name)];
|
|
1267
|
-
const limitsInfo = limits.maxPromptsPerBatch ? `${String(limits.maxTokensPerBatch)} tokens/batch, max ${String(limits.maxPromptsPerBatch)} prompts/batch` : `${String(limits.maxTokensPerBatch)} tokens/batch`;
|
|
1268
|
-
logger.debug(`Using batch limits: ${limitsInfo}`, "analyzer");
|
|
1269
|
-
const batches = batchPrompts({
|
|
1270
|
-
prompts: sanitizedPrompts,
|
|
1271
|
-
maxTokensPerBatch: limits.maxTokensPerBatch,
|
|
1272
|
-
maxPromptsPerBatch: limits.maxPromptsPerBatch,
|
|
1273
|
-
prioritization: limits.prioritization
|
|
1274
|
-
});
|
|
1275
|
-
logger.debug(
|
|
1276
|
-
`Created ${String(batches.length)} batch(es) for analysis`,
|
|
1277
|
-
"analyzer"
|
|
1278
|
-
);
|
|
1279
|
-
if (batches.length === 1) {
|
|
1280
|
-
const batch = batches[0];
|
|
1281
|
-
if (!batch) {
|
|
1282
|
-
throw new Error("Failed to create batches");
|
|
1283
|
-
}
|
|
1284
|
-
if (onProgress) {
|
|
1285
|
-
onProgress(0, 1);
|
|
1286
|
-
}
|
|
1287
|
-
const model2 = extractModelFromProvider(provider.name);
|
|
1288
|
-
logger.debug(
|
|
1289
|
-
`Processing single batch (${String(batch.tokens)} tokens, ${String(batch.prompts.length)} prompts)`,
|
|
1290
|
-
"analyzer"
|
|
1291
|
-
);
|
|
1292
|
-
const startTime = Date.now();
|
|
1293
|
-
const results2 = await analyzeWithFallback(
|
|
1294
|
-
provider,
|
|
1295
|
-
batch.prompts,
|
|
1296
|
-
date,
|
|
1297
|
-
context,
|
|
1298
|
-
model2,
|
|
1299
|
-
noCache ?? false
|
|
1300
|
-
);
|
|
1301
|
-
const elapsed = Date.now() - startTime;
|
|
1302
|
-
logger.debug(`Batch completed in ${String(elapsed)}ms`, "analyzer");
|
|
1303
|
-
if (onProgress) {
|
|
1304
|
-
onProgress(1, 1);
|
|
1305
|
-
}
|
|
1306
|
-
if (results2.length === 0) {
|
|
1307
|
-
throw new Error("All prompts failed analysis");
|
|
1308
|
-
}
|
|
1309
|
-
logger.debug(`Analysis complete, merging with cached results`, "analyzer");
|
|
1310
|
-
return mergeWithCachedResults(results2, cachedResults, date, rules);
|
|
1311
|
-
}
|
|
1312
|
-
const results = [];
|
|
1313
|
-
const totalBatches = batches.length;
|
|
1314
|
-
const model = extractModelFromProvider(provider.name);
|
|
1315
|
-
for (let i = 0; i < batches.length; i++) {
|
|
1316
|
-
const batch = batches[i];
|
|
1317
|
-
if (!batch) continue;
|
|
1318
|
-
if (onProgress) {
|
|
1319
|
-
onProgress(i, totalBatches);
|
|
1320
|
-
}
|
|
1321
|
-
logger.debug(
|
|
1322
|
-
`Processing batch ${String(i + 1)}/${String(totalBatches)} (${String(batch.tokens)} tokens, ${String(batch.prompts.length)} prompts)`,
|
|
1323
|
-
"analyzer"
|
|
1324
|
-
);
|
|
1325
|
-
const startTime = Date.now();
|
|
1326
|
-
const batchResults = await analyzeWithFallback(
|
|
1327
|
-
provider,
|
|
1328
|
-
batch.prompts,
|
|
1329
|
-
date,
|
|
1330
|
-
context,
|
|
1331
|
-
model,
|
|
1332
|
-
noCache ?? false
|
|
1333
|
-
);
|
|
1334
|
-
results.push(...batchResults);
|
|
1335
|
-
const elapsed = Date.now() - startTime;
|
|
1336
|
-
logger.debug(
|
|
1337
|
-
`Batch ${String(i + 1)}/${String(totalBatches)} completed in ${String(elapsed)}ms (${String(batchResults.length)} result(s))`,
|
|
1338
|
-
"analyzer"
|
|
1339
|
-
);
|
|
1340
|
-
}
|
|
1341
|
-
if (onProgress) {
|
|
1342
|
-
onProgress(totalBatches, totalBatches);
|
|
1343
|
-
}
|
|
1344
|
-
if (results.length === 0) {
|
|
1345
|
-
throw new Error("All batches failed analysis");
|
|
1346
|
-
}
|
|
1347
|
-
logger.debug(
|
|
1348
|
-
`Analysis complete, merging ${String(results.length)} batch results with cached results`,
|
|
1349
|
-
"analyzer"
|
|
1350
|
-
);
|
|
1351
|
-
return mergeWithCachedResults(results, cachedResults, date, rules);
|
|
1352
|
-
}
|
|
1353
|
-
|
|
1354
|
-
// src/core/schema-validator.ts
|
|
1355
|
-
var SUPPORTED_VERSIONS = ["1.0"];
|
|
1356
|
-
var CURRENT_VERSION = {
|
|
1357
|
-
major: 1,
|
|
1358
|
-
minor: 0,
|
|
1359
|
-
detected: "1.0"
|
|
1360
|
-
};
|
|
1361
|
-
var V1_0_REQUIRED_FIELDS = [
|
|
1362
|
-
"type",
|
|
1363
|
-
"message",
|
|
1364
|
-
"timestamp",
|
|
1365
|
-
"sessionId",
|
|
1366
|
-
"cwd"
|
|
1367
|
-
];
|
|
1368
|
-
var V1_0_MESSAGE_FIELDS = ["role", "content"];
|
|
1369
|
-
var V1_0_MESSAGE_TYPES = ["user", "assistant", "system"];
|
|
1370
|
-
function isObject(value) {
|
|
1371
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1372
|
-
}
|
|
1373
|
-
function hasRequiredFields(obj, fields) {
|
|
1374
|
-
return fields.every((field) => field in obj);
|
|
1375
|
-
}
|
|
1376
|
-
function isValidV1Message(message) {
|
|
1377
|
-
if (!isObject(message)) {
|
|
1378
|
-
return false;
|
|
1379
|
-
}
|
|
1380
|
-
if (!hasRequiredFields(message, V1_0_MESSAGE_FIELDS)) {
|
|
1381
|
-
return false;
|
|
1382
|
-
}
|
|
1383
|
-
const role = message["role"];
|
|
1384
|
-
if (typeof role !== "string" || !V1_0_MESSAGE_TYPES.includes(role)) {
|
|
1385
|
-
return false;
|
|
1386
|
-
}
|
|
1387
|
-
const content = message["content"];
|
|
1388
|
-
if (typeof content !== "string") {
|
|
1389
|
-
return false;
|
|
1390
|
-
}
|
|
1391
|
-
return true;
|
|
1392
|
-
}
|
|
1393
|
-
function isValidV1Type(type) {
|
|
1394
|
-
return typeof type === "string" && V1_0_MESSAGE_TYPES.includes(type);
|
|
1395
|
-
}
|
|
1396
|
-
function detectSchemaVersion(entry) {
|
|
1397
|
-
if (!isObject(entry)) {
|
|
1398
|
-
return null;
|
|
1399
|
-
}
|
|
1400
|
-
if (!hasRequiredFields(entry, V1_0_REQUIRED_FIELDS)) {
|
|
1401
|
-
return null;
|
|
1402
|
-
}
|
|
1403
|
-
if (!isValidV1Type(entry["type"])) {
|
|
1404
|
-
return null;
|
|
1405
|
-
}
|
|
1406
|
-
if (!isValidV1Message(entry["message"])) {
|
|
1407
|
-
return null;
|
|
1408
|
-
}
|
|
1409
|
-
if (typeof entry["timestamp"] !== "string") {
|
|
1410
|
-
return null;
|
|
1411
|
-
}
|
|
1412
|
-
if (typeof entry["sessionId"] !== "string") {
|
|
1413
|
-
return null;
|
|
1414
|
-
}
|
|
1415
|
-
if (typeof entry["cwd"] !== "string") {
|
|
1416
|
-
return null;
|
|
1417
|
-
}
|
|
1418
|
-
return CURRENT_VERSION;
|
|
1419
|
-
}
|
|
1420
|
-
function isSchemaSupported(version) {
|
|
1421
|
-
return SUPPORTED_VERSIONS.includes(version.detected);
|
|
1422
|
-
}
|
|
1423
|
-
function getSchemaWarning(version, context) {
|
|
1424
|
-
const contextSuffix = "";
|
|
1425
|
-
if (version === null) {
|
|
1426
|
-
return `Unknown log format detected${contextSuffix}. The log entry structure does not match any known Claude Code schema. This entry will be skipped.`;
|
|
1427
|
-
}
|
|
1428
|
-
if (!isSchemaSupported(version)) {
|
|
1429
|
-
return `Unsupported schema version ${version.detected}${contextSuffix}. Supported versions: ${SUPPORTED_VERSIONS.join(", ")}. This entry may not be processed correctly.`;
|
|
1430
|
-
}
|
|
1431
|
-
return "";
|
|
1432
|
-
}
|
|
1433
|
-
function validateLogEntry(entry) {
|
|
1434
|
-
const version = detectSchemaVersion(entry);
|
|
1435
|
-
if (version === null) {
|
|
1436
|
-
return {
|
|
1437
|
-
isValid: false,
|
|
1438
|
-
version: null,
|
|
1439
|
-
warning: getSchemaWarning(null)
|
|
1440
|
-
};
|
|
1441
|
-
}
|
|
1442
|
-
if (!isSchemaSupported(version)) {
|
|
1443
|
-
return {
|
|
1444
|
-
isValid: false,
|
|
1445
|
-
version,
|
|
1446
|
-
warning: getSchemaWarning(version)
|
|
1447
|
-
};
|
|
1448
|
-
}
|
|
1449
|
-
return {
|
|
1450
|
-
isValid: true,
|
|
1451
|
-
version,
|
|
1452
|
-
warning: null
|
|
1453
|
-
};
|
|
1454
|
-
}
|
|
1455
|
-
|
|
1456
|
-
// src/core/log-reader.ts
|
|
1457
|
-
function claudeProjectsExist() {
|
|
1458
|
-
return existsSync(CLAUDE_PROJECTS_DIR);
|
|
1459
|
-
}
|
|
1460
|
-
function extractProjectName(filePath) {
|
|
1461
|
-
const parts = filePath.split("/");
|
|
1462
|
-
const projectIndex = parts.findIndex((p) => p === "projects");
|
|
1463
|
-
const projectHash = parts[projectIndex + 1];
|
|
1464
|
-
if (projectIndex !== -1 && projectHash !== void 0) {
|
|
1465
|
-
return projectHash;
|
|
1466
|
-
}
|
|
1467
|
-
return basename(filePath, ".jsonl");
|
|
1468
|
-
}
|
|
1469
|
-
function parseDate(dateStr) {
|
|
1470
|
-
const normalized = dateStr.trim().toLowerCase();
|
|
1471
|
-
if (normalized === "today") {
|
|
1472
|
-
return startOfDay(/* @__PURE__ */ new Date());
|
|
1473
|
-
}
|
|
1474
|
-
if (normalized === "yesterday") {
|
|
1475
|
-
const yesterday = /* @__PURE__ */ new Date();
|
|
1476
|
-
yesterday.setDate(yesterday.getDate() - 1);
|
|
1477
|
-
return startOfDay(yesterday);
|
|
1478
|
-
}
|
|
1479
|
-
try {
|
|
1480
|
-
const parsed = parseISO(dateStr);
|
|
1481
|
-
if (isNaN(parsed.getTime())) {
|
|
1482
|
-
throw new Error(`Invalid date format: ${dateStr}`);
|
|
1483
|
-
}
|
|
1484
|
-
return startOfDay(parsed);
|
|
1485
|
-
} catch {
|
|
1486
|
-
throw new Error(
|
|
1487
|
-
`Invalid date format: ${dateStr}. Expected 'today', 'yesterday', or YYYY-MM-DD`
|
|
1488
|
-
);
|
|
1489
|
-
}
|
|
1490
|
-
}
|
|
1491
|
-
function extractDate(timestamp) {
|
|
1492
|
-
try {
|
|
1493
|
-
const date = parseISO(timestamp);
|
|
1494
|
-
const dateStr = date.toISOString().split("T")[0];
|
|
1495
|
-
return dateStr ?? "unknown";
|
|
1496
|
-
} catch {
|
|
1497
|
-
return "unknown";
|
|
1498
|
-
}
|
|
1499
|
-
}
|
|
1500
|
-
function isClaudeMessage(value) {
|
|
1501
|
-
if (typeof value !== "object" || value === null) {
|
|
1502
|
-
return false;
|
|
1503
|
-
}
|
|
1504
|
-
const obj = value;
|
|
1505
|
-
return typeof obj["timestamp"] === "string" && typeof obj["type"] === "string" && typeof obj["message"] === "object" && obj["message"] !== null && typeof obj["message"]["content"] === "string";
|
|
1506
|
-
}
|
|
1507
|
-
function parseLine(line, lineNumber, filePath) {
|
|
1508
|
-
if (!line.trim()) {
|
|
1509
|
-
return null;
|
|
1510
|
-
}
|
|
1511
|
-
const context = lineNumber !== void 0 && filePath ? `${filePath}:${String(lineNumber)}` : void 0;
|
|
1512
|
-
try {
|
|
1513
|
-
const parsed = JSON.parse(line);
|
|
1514
|
-
const validation = validateLogEntry(parsed);
|
|
1515
|
-
if (!validation.isValid) {
|
|
1516
|
-
logger.debug(
|
|
1517
|
-
`Schema validation failed: ${validation.warning ?? "Unknown format"}`,
|
|
1518
|
-
context
|
|
1519
|
-
);
|
|
1520
|
-
return null;
|
|
1521
|
-
}
|
|
1522
|
-
if (!isClaudeMessage(parsed)) {
|
|
1523
|
-
logger.debug("Type guard validation failed", context);
|
|
1524
|
-
return null;
|
|
1525
|
-
}
|
|
1526
|
-
return parsed;
|
|
1527
|
-
} catch (error) {
|
|
1528
|
-
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1529
|
-
logger.debug(`JSON parse failed: ${errorMessage}`, context);
|
|
1530
|
-
return null;
|
|
1531
|
-
}
|
|
1532
|
-
}
|
|
1533
|
-
function isUserMessage(message) {
|
|
1534
|
-
return message.type === "user" && message.message.role === "user";
|
|
1535
|
-
}
|
|
1536
|
-
function extractContent(message) {
|
|
1537
|
-
return message.message.content;
|
|
1538
|
-
}
|
|
1539
|
-
async function readJsonlFile(filePath) {
|
|
1540
|
-
const prompts = [];
|
|
1541
|
-
const warnings = [];
|
|
1542
|
-
const projectName = extractProjectName(filePath);
|
|
1543
|
-
try {
|
|
1544
|
-
const content = await readFile(filePath, "utf-8");
|
|
1545
|
-
const lines = content.split("\n");
|
|
1546
|
-
let skippedLines = 0;
|
|
1547
|
-
for (let i = 0; i < lines.length; i++) {
|
|
1548
|
-
const line = lines[i] ?? "";
|
|
1549
|
-
const lineNumber = i + 1;
|
|
1550
|
-
const message = parseLine(line, lineNumber, filePath);
|
|
1551
|
-
if (message === null) {
|
|
1552
|
-
if (line.trim()) {
|
|
1553
|
-
skippedLines++;
|
|
1554
|
-
}
|
|
1555
|
-
continue;
|
|
1556
|
-
}
|
|
1557
|
-
if (!isUserMessage(message)) {
|
|
1558
|
-
continue;
|
|
1559
|
-
}
|
|
1560
|
-
const extractedContent = extractContent(message);
|
|
1561
|
-
if (!extractedContent.trim()) {
|
|
1562
|
-
continue;
|
|
1563
|
-
}
|
|
1564
|
-
prompts.push({
|
|
1565
|
-
content: extractedContent,
|
|
1566
|
-
timestamp: message.timestamp,
|
|
1567
|
-
sessionId: message.sessionId,
|
|
1568
|
-
project: projectName,
|
|
1569
|
-
date: extractDate(message.timestamp)
|
|
1570
|
-
});
|
|
1571
|
-
}
|
|
1572
|
-
if (skippedLines > 0) {
|
|
1573
|
-
const warning = `Skipped ${String(skippedLines)} invalid line(s) in ${filePath}`;
|
|
1574
|
-
warnings.push(warning);
|
|
1575
|
-
logger.collectWarning(warning, "log-reader");
|
|
1576
|
-
}
|
|
1577
|
-
} catch (error) {
|
|
1578
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
1579
|
-
const warning = `Failed to read ${filePath}: ${errorMessage}`;
|
|
1580
|
-
warnings.push(warning);
|
|
1581
|
-
logger.error(warning, "log-reader");
|
|
1582
|
-
}
|
|
1583
|
-
return { prompts, warnings };
|
|
1584
|
-
}
|
|
1585
|
-
function filterByDateRange(prompts, fromDate, toDate) {
|
|
1586
|
-
return prompts.filter((prompt) => {
|
|
1587
|
-
try {
|
|
1588
|
-
const promptDate = parseISO(prompt.timestamp);
|
|
1589
|
-
const promptDay = startOfDay(promptDate);
|
|
1590
|
-
return (isSameDay(promptDay, fromDate) || isAfter(promptDay, fromDate)) && (isSameDay(promptDay, toDate) || isBefore(promptDay, toDate));
|
|
1591
|
-
} catch (error) {
|
|
1592
|
-
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1593
|
-
logger.debug(
|
|
1594
|
-
`Invalid timestamp in filterByDateRange: ${prompt.timestamp} - ${errorMessage}`,
|
|
1595
|
-
"log-reader"
|
|
1596
|
-
);
|
|
1597
|
-
return false;
|
|
1598
|
-
}
|
|
1599
|
-
});
|
|
1600
|
-
}
|
|
1601
|
-
function filterByDate(prompts, targetDate) {
|
|
1602
|
-
return prompts.filter((prompt) => {
|
|
1603
|
-
try {
|
|
1604
|
-
const promptDate = parseISO(prompt.timestamp);
|
|
1605
|
-
return isSameDay(startOfDay(promptDate), targetDate);
|
|
1606
|
-
} catch (error) {
|
|
1607
|
-
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1608
|
-
logger.debug(
|
|
1609
|
-
`Invalid timestamp in filterByDate: ${prompt.timestamp} - ${errorMessage}`,
|
|
1610
|
-
"log-reader"
|
|
1611
|
-
);
|
|
1612
|
-
return false;
|
|
1613
|
-
}
|
|
1614
|
-
});
|
|
1615
|
-
}
|
|
1616
|
-
function filterByProject(prompts, projectName) {
|
|
1617
|
-
const normalizedProject = projectName.toLowerCase().trim();
|
|
1618
|
-
return prompts.filter(
|
|
1619
|
-
(prompt) => prompt.project.toLowerCase().includes(normalizedProject)
|
|
1620
|
-
);
|
|
1621
|
-
}
|
|
1622
|
-
function groupByDay(prompts) {
|
|
1623
|
-
const groups = /* @__PURE__ */ new Map();
|
|
1624
|
-
for (const prompt of prompts) {
|
|
1625
|
-
const date = prompt.date;
|
|
1626
|
-
if (!groups.has(date)) {
|
|
1627
|
-
groups.set(date, { prompts: [], projects: /* @__PURE__ */ new Set() });
|
|
1628
|
-
}
|
|
1629
|
-
const group = groups.get(date);
|
|
1630
|
-
if (group) {
|
|
1631
|
-
group.prompts.push(prompt);
|
|
1632
|
-
group.projects.add(prompt.project);
|
|
1633
|
-
}
|
|
1634
|
-
}
|
|
1635
|
-
const result = Array.from(groups.entries()).map(([date, { prompts: groupPrompts, projects }]) => {
|
|
1636
|
-
const sortedPrompts = groupPrompts.sort((a, b) => {
|
|
1637
|
-
try {
|
|
1638
|
-
const dateA = parseISO(a.timestamp);
|
|
1639
|
-
const dateB = parseISO(b.timestamp);
|
|
1640
|
-
return dateA.getTime() - dateB.getTime();
|
|
1641
|
-
} catch (error) {
|
|
1642
|
-
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1643
|
-
logger.debug(
|
|
1644
|
-
`Invalid timestamp in groupByDay sort: ${a.timestamp} or ${b.timestamp} - ${errorMessage}`,
|
|
1645
|
-
"log-reader"
|
|
1646
|
-
);
|
|
1647
|
-
return 0;
|
|
1648
|
-
}
|
|
1649
|
-
});
|
|
1650
|
-
return {
|
|
1651
|
-
date,
|
|
1652
|
-
prompts: sortedPrompts,
|
|
1653
|
-
projects: Array.from(projects).sort()
|
|
1654
|
-
};
|
|
1655
|
-
}).sort((a, b) => {
|
|
1656
|
-
try {
|
|
1657
|
-
const dateA = parseISO(a.date + "T00:00:00Z");
|
|
1658
|
-
const dateB = parseISO(b.date + "T00:00:00Z");
|
|
1659
|
-
return dateA.getTime() - dateB.getTime();
|
|
1660
|
-
} catch (error) {
|
|
1661
|
-
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1662
|
-
logger.debug(
|
|
1663
|
-
`Invalid date in groupByDay sort: ${a.date} or ${b.date} - ${errorMessage}`,
|
|
1664
|
-
"log-reader"
|
|
1665
|
-
);
|
|
1666
|
-
return 0;
|
|
1667
|
-
}
|
|
1668
|
-
});
|
|
1669
|
-
return result;
|
|
1670
|
-
}
|
|
1671
|
-
async function readLogs(options) {
|
|
1672
|
-
logger.debug(`Scanning ${CLAUDE_PROJECTS_DIR}`, "log-reader");
|
|
1673
|
-
if (!claudeProjectsExist()) {
|
|
1674
|
-
logger.debug("Claude projects directory not found", "log-reader");
|
|
1675
|
-
return {
|
|
1676
|
-
prompts: [],
|
|
1677
|
-
warnings: [
|
|
1678
|
-
`Claude projects directory not found at ${CLAUDE_PROJECTS_DIR}`
|
|
1679
|
-
]
|
|
1680
|
-
};
|
|
1681
|
-
}
|
|
1682
|
-
const pattern = `${CLAUDE_PROJECTS_DIR}/**/*.jsonl`;
|
|
1683
|
-
let files;
|
|
1684
|
-
try {
|
|
1685
|
-
files = await glob(pattern);
|
|
1686
|
-
} catch (error) {
|
|
1687
|
-
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
1688
|
-
const warning = `Failed to search for JSONL files: ${errorMessage}`;
|
|
1689
|
-
logger.error(warning, "log-reader");
|
|
1690
|
-
return {
|
|
1691
|
-
prompts: [],
|
|
1692
|
-
warnings: [warning]
|
|
1693
|
-
};
|
|
1694
|
-
}
|
|
1695
|
-
logger.debug(`Found ${String(files.length)} JSONL files`, "log-reader");
|
|
1696
|
-
if (files.length === 0) {
|
|
1697
|
-
return {
|
|
1698
|
-
prompts: [],
|
|
1699
|
-
warnings: ["No JSONL files found in Claude projects directory"]
|
|
1700
|
-
};
|
|
1701
|
-
}
|
|
1702
|
-
const allPrompts = [];
|
|
1703
|
-
const allWarnings = [];
|
|
1704
|
-
const CONCURRENCY_LIMIT = 10;
|
|
1705
|
-
for (let i = 0; i < files.length; i += CONCURRENCY_LIMIT) {
|
|
1706
|
-
const chunk = files.slice(i, i + CONCURRENCY_LIMIT);
|
|
1707
|
-
const results = await Promise.all(chunk.map(readJsonlFile));
|
|
1708
|
-
for (const { prompts, warnings } of results) {
|
|
1709
|
-
allPrompts.push(...prompts);
|
|
1710
|
-
allWarnings.push(...warnings);
|
|
1711
|
-
}
|
|
1712
|
-
}
|
|
1713
|
-
logger.debug(
|
|
1714
|
-
`Parsed ${String(allPrompts.length)} total prompts from all files`,
|
|
1715
|
-
"log-reader"
|
|
1716
|
-
);
|
|
1717
|
-
let filteredPrompts = allPrompts;
|
|
1718
|
-
if (options) {
|
|
1719
|
-
if (options.from && options.to) {
|
|
1720
|
-
try {
|
|
1721
|
-
const fromDate = parseDate(options.from);
|
|
1722
|
-
const toDate = parseDate(options.to);
|
|
1723
|
-
if (isAfter(fromDate, toDate)) {
|
|
1724
|
-
return {
|
|
1725
|
-
prompts: [],
|
|
1726
|
-
warnings: [
|
|
1727
|
-
...allWarnings,
|
|
1728
|
-
`Invalid date range: --from (${options.from}) must be before or equal to --to (${options.to})`
|
|
1729
|
-
]
|
|
1730
|
-
};
|
|
1731
|
-
}
|
|
1732
|
-
filteredPrompts = filterByDateRange(filteredPrompts, fromDate, toDate);
|
|
1733
|
-
} catch (error) {
|
|
1734
|
-
const errorMessage = error instanceof Error ? error.message : "Invalid date range";
|
|
1735
|
-
return {
|
|
1736
|
-
prompts: [],
|
|
1737
|
-
warnings: [...allWarnings, errorMessage]
|
|
1738
|
-
};
|
|
1739
|
-
}
|
|
1740
|
-
} else if (options.date) {
|
|
1741
|
-
try {
|
|
1742
|
-
const targetDate = parseDate(options.date);
|
|
1743
|
-
filteredPrompts = filterByDate(filteredPrompts, targetDate);
|
|
1744
|
-
} catch (error) {
|
|
1745
|
-
const errorMessage = error instanceof Error ? error.message : "Invalid date";
|
|
1746
|
-
return {
|
|
1747
|
-
prompts: [],
|
|
1748
|
-
warnings: [...allWarnings, errorMessage]
|
|
1749
|
-
};
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
if (options.project) {
|
|
1753
|
-
filteredPrompts = filterByProject(filteredPrompts, options.project);
|
|
1754
|
-
}
|
|
1755
|
-
}
|
|
1756
|
-
if (options) {
|
|
1757
|
-
const filtered = allPrompts.length - filteredPrompts.length;
|
|
1758
|
-
if (filtered > 0) {
|
|
1759
|
-
logger.debug(
|
|
1760
|
-
`Filtered ${String(filtered)} prompts (${String(filteredPrompts.length)} remaining)`,
|
|
1761
|
-
"log-reader"
|
|
1762
|
-
);
|
|
1763
|
-
}
|
|
1764
|
-
}
|
|
1765
|
-
const sortedPrompts = filteredPrompts.sort((a, b) => {
|
|
1766
|
-
try {
|
|
1767
|
-
const dateA = parseISO(a.timestamp);
|
|
1768
|
-
const dateB = parseISO(b.timestamp);
|
|
1769
|
-
return dateA.getTime() - dateB.getTime();
|
|
1770
|
-
} catch (error) {
|
|
1771
|
-
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1772
|
-
logger.debug(
|
|
1773
|
-
`Invalid timestamp in readLogs sort: ${a.timestamp} or ${b.timestamp} - ${errorMessage}`,
|
|
1774
|
-
"log-reader"
|
|
1775
|
-
);
|
|
1776
|
-
return 0;
|
|
1777
|
-
}
|
|
1778
|
-
});
|
|
1779
|
-
logger.debug(
|
|
1780
|
-
`Returning ${String(sortedPrompts.length)} prompts for analysis`,
|
|
1781
|
-
"log-reader"
|
|
1782
|
-
);
|
|
1783
|
-
return {
|
|
1784
|
-
prompts: sortedPrompts,
|
|
1785
|
-
warnings: allWarnings
|
|
1786
|
-
};
|
|
1787
|
-
}
|
|
1788
|
-
function getHistoryDir() {
|
|
1789
|
-
return HYNTX_HISTORY_DIR;
|
|
1790
|
-
}
|
|
1791
|
-
async function ensureHistoryDir() {
|
|
1792
|
-
try {
|
|
1793
|
-
await mkdir(HYNTX_HISTORY_DIR, { recursive: true, mode: 448 });
|
|
1794
|
-
} catch (error) {
|
|
1795
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1796
|
-
throw new Error(
|
|
1797
|
-
`Failed to create history directory ${HYNTX_HISTORY_DIR}: ${errorMessage}`
|
|
1798
|
-
);
|
|
1799
|
-
}
|
|
1800
|
-
}
|
|
1801
|
-
function getHistoryFilePath(date) {
|
|
1802
|
-
return join(HYNTX_HISTORY_DIR, `${date}.json`);
|
|
1803
|
-
}
|
|
1804
|
-
async function saveAnalysisResult(result, metadata) {
|
|
1805
|
-
try {
|
|
1806
|
-
await ensureHistoryDir();
|
|
1807
|
-
const sanitizedResult = {
|
|
1808
|
-
...result,
|
|
1809
|
-
patterns: result.patterns.map((pattern) => ({
|
|
1810
|
-
...pattern,
|
|
1811
|
-
examples: pattern.examples.map((example) => sanitize(example).text)
|
|
1812
|
-
}))
|
|
1813
|
-
};
|
|
1814
|
-
const entry = {
|
|
1815
|
-
result: sanitizedResult,
|
|
1816
|
-
metadata
|
|
1817
|
-
};
|
|
1818
|
-
const filePath = getHistoryFilePath(result.date);
|
|
1819
|
-
const content = JSON.stringify(entry, null, 2);
|
|
1820
|
-
const tmpFile = `${filePath}.tmp`;
|
|
1821
|
-
await writeFile(tmpFile, content, "utf-8");
|
|
1822
|
-
await rename(tmpFile, filePath);
|
|
1823
|
-
logger.debug(`Saved history to ${filePath}`, "history");
|
|
1824
|
-
} catch (error) {
|
|
1825
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1826
|
-
logger.warn(
|
|
1827
|
-
`Failed to save analysis to history: ${errorMessage}`,
|
|
1828
|
-
"history"
|
|
1829
|
-
);
|
|
1830
|
-
}
|
|
1831
|
-
}
|
|
1832
|
-
async function loadAnalysisResult(date) {
|
|
1833
|
-
const filePath = getHistoryFilePath(date);
|
|
1834
|
-
if (!existsSync(filePath)) {
|
|
1835
|
-
return null;
|
|
1836
|
-
}
|
|
1837
|
-
try {
|
|
1838
|
-
const content = await readFile(filePath, "utf-8");
|
|
1839
|
-
const entry = JSON.parse(content);
|
|
1840
|
-
if (entry.result === void 0 || entry.metadata === void 0) {
|
|
1841
|
-
logger.warn(`Invalid history entry in ${filePath}`, "history");
|
|
1842
|
-
return null;
|
|
1843
|
-
}
|
|
1844
|
-
return entry;
|
|
1845
|
-
} catch (error) {
|
|
1846
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1847
|
-
logger.warn(
|
|
1848
|
-
`Failed to load history from ${filePath}: ${errorMessage}`,
|
|
1849
|
-
"history"
|
|
1850
|
-
);
|
|
1851
|
-
return null;
|
|
1852
|
-
}
|
|
1853
|
-
}
|
|
1854
|
-
async function listAvailableDates(options) {
|
|
1855
|
-
if (!existsSync(HYNTX_HISTORY_DIR)) {
|
|
1856
|
-
return [];
|
|
1857
|
-
}
|
|
1858
|
-
try {
|
|
1859
|
-
const files = readdirSync(HYNTX_HISTORY_DIR);
|
|
1860
|
-
const dates = [];
|
|
1861
|
-
for (const file of files) {
|
|
1862
|
-
if (!file.endsWith(".json")) {
|
|
1863
|
-
continue;
|
|
1864
|
-
}
|
|
1865
|
-
const date = file.slice(0, -5);
|
|
1866
|
-
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
|
1867
|
-
continue;
|
|
1868
|
-
}
|
|
1869
|
-
if (options) {
|
|
1870
|
-
const entry = await loadAnalysisResult(date);
|
|
1871
|
-
if (!entry) {
|
|
1872
|
-
continue;
|
|
1873
|
-
}
|
|
1874
|
-
if (options.provider && entry.metadata.provider !== options.provider) {
|
|
1875
|
-
continue;
|
|
1876
|
-
}
|
|
1877
|
-
const projectFilter = options.project;
|
|
1878
|
-
if (projectFilter && !entry.metadata.projects.some(
|
|
1879
|
-
(p) => p.toLowerCase().includes(projectFilter.toLowerCase())
|
|
1880
|
-
)) {
|
|
1881
|
-
continue;
|
|
1882
|
-
}
|
|
1883
|
-
const score = entry.result.stats.overallScore;
|
|
1884
|
-
if (options.minScore !== void 0 && score < options.minScore) {
|
|
1885
|
-
continue;
|
|
1886
|
-
}
|
|
1887
|
-
if (options.maxScore !== void 0 && score > options.maxScore) {
|
|
1888
|
-
continue;
|
|
1889
|
-
}
|
|
1890
|
-
}
|
|
1891
|
-
dates.push(date);
|
|
1892
|
-
}
|
|
1893
|
-
return dates.sort((a, b) => b.localeCompare(a));
|
|
1894
|
-
} catch (error) {
|
|
1895
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1896
|
-
logger.warn(`Failed to list history dates: ${errorMessage}`, "history");
|
|
1897
|
-
return [];
|
|
1898
|
-
}
|
|
1899
|
-
}
|
|
1900
|
-
function compareResults(before, after) {
|
|
1901
|
-
const scoreDelta = after.stats.overallScore - before.stats.overallScore;
|
|
1902
|
-
const beforePatterns = new Map(before.patterns.map((p) => [p.id, p]));
|
|
1903
|
-
const afterPatterns = new Map(after.patterns.map((p) => [p.id, p]));
|
|
1904
|
-
const newPatterns = [];
|
|
1905
|
-
for (const pattern of after.patterns) {
|
|
1906
|
-
if (!beforePatterns.has(pattern.id)) {
|
|
1907
|
-
newPatterns.push(pattern);
|
|
1908
|
-
}
|
|
1909
|
-
}
|
|
1910
|
-
const resolvedPatterns = [];
|
|
1911
|
-
for (const pattern of before.patterns) {
|
|
1912
|
-
if (!afterPatterns.has(pattern.id)) {
|
|
1913
|
-
resolvedPatterns.push(pattern);
|
|
1914
|
-
}
|
|
1915
|
-
}
|
|
1916
|
-
const changedPatterns = [];
|
|
1917
|
-
for (const [id, beforePattern] of beforePatterns) {
|
|
1918
|
-
const afterPattern = afterPatterns.get(id);
|
|
1919
|
-
if (!afterPattern) {
|
|
1920
|
-
continue;
|
|
1921
|
-
}
|
|
1922
|
-
const frequencyChanged = beforePattern.frequency !== afterPattern.frequency;
|
|
1923
|
-
const severityChanged = beforePattern.severity !== afterPattern.severity;
|
|
1924
|
-
if (frequencyChanged || severityChanged) {
|
|
1925
|
-
changedPatterns.push({
|
|
1926
|
-
id,
|
|
1927
|
-
name: beforePattern.name,
|
|
1928
|
-
status: "changed",
|
|
1929
|
-
frequencyBefore: beforePattern.frequency,
|
|
1930
|
-
frequencyAfter: afterPattern.frequency,
|
|
1931
|
-
severityBefore: beforePattern.severity,
|
|
1932
|
-
severityAfter: afterPattern.severity
|
|
1933
|
-
});
|
|
1934
|
-
}
|
|
1935
|
-
}
|
|
1936
|
-
const changes = {
|
|
1937
|
-
scoreDelta,
|
|
1938
|
-
newPatterns,
|
|
1939
|
-
resolvedPatterns,
|
|
1940
|
-
changedPatterns
|
|
1941
|
-
};
|
|
1942
|
-
return {
|
|
1943
|
-
before,
|
|
1944
|
-
after,
|
|
1945
|
-
changes
|
|
1946
|
-
};
|
|
1947
|
-
}
|
|
1948
|
-
function getDateOneWeekAgo(fromDate) {
|
|
1949
|
-
const date = typeof fromDate === "string" ? parseISO(fromDate) : fromDate;
|
|
1950
|
-
const weekAgo = subDays(date, 7);
|
|
1951
|
-
return format(weekAgo, "yyyy-MM-dd");
|
|
1952
|
-
}
|
|
1953
|
-
function getDateOneMonthAgo(fromDate) {
|
|
1954
|
-
const date = typeof fromDate === "string" ? parseISO(fromDate) : fromDate;
|
|
1955
|
-
const monthAgo = subMonths(date, 1);
|
|
1956
|
-
return format(monthAgo, "yyyy-MM-dd");
|
|
1957
|
-
}
|
|
1958
|
-
|
|
1959
|
-
// src/utils/rate-limiter.ts
|
|
1960
|
-
function createRateLimiter(options) {
|
|
1961
|
-
const minIntervalMs = 6e4 / options.requestsPerMinute;
|
|
1962
|
-
let lastRequestTime = 0;
|
|
1963
|
-
async function throttle(fn) {
|
|
1964
|
-
const now = Date.now();
|
|
1965
|
-
const timeSinceLastRequest = now - lastRequestTime;
|
|
1966
|
-
if (lastRequestTime > 0 && timeSinceLastRequest < minIntervalMs) {
|
|
1967
|
-
const waitTime = minIntervalMs - timeSinceLastRequest;
|
|
1968
|
-
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
1969
|
-
}
|
|
1970
|
-
lastRequestTime = Date.now();
|
|
1971
|
-
return fn();
|
|
1972
|
-
}
|
|
1973
|
-
function reset() {
|
|
1974
|
-
lastRequestTime = 0;
|
|
1975
|
-
}
|
|
1976
|
-
return { throttle, reset };
|
|
1977
|
-
}
|
|
1978
|
-
var DEFAULT_RATE_LIMITS = {
|
|
1979
|
-
anthropic: 50,
|
|
1980
|
-
google: 50
|
|
1981
|
-
};
|
|
1982
|
-
|
|
1983
|
-
// src/utils/retry.ts
|
|
1984
|
-
var DEFAULT_OPTIONS = {
|
|
1985
|
-
maxRetries: 3,
|
|
1986
|
-
baseDelayMs: 1e3,
|
|
1987
|
-
maxDelayMs: 3e4,
|
|
1988
|
-
isRetryable: isTransientError
|
|
1989
|
-
};
|
|
1990
|
-
function isTransientError(error) {
|
|
1991
|
-
if (!(error instanceof Error)) {
|
|
1992
|
-
return false;
|
|
1993
|
-
}
|
|
1994
|
-
const message = error.message.toLowerCase();
|
|
1995
|
-
if (message.includes("network") || message.includes("timeout") || message.includes("econnreset") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("abort")) {
|
|
1996
|
-
return true;
|
|
1997
|
-
}
|
|
1998
|
-
if (message.includes("500") || message.includes("502") || message.includes("503") || message.includes("504")) {
|
|
1999
|
-
return true;
|
|
2000
|
-
}
|
|
2001
|
-
if (message.includes("429")) {
|
|
2002
|
-
return true;
|
|
2003
|
-
}
|
|
2004
|
-
if (message.includes("401") || message.includes("403")) {
|
|
2005
|
-
return false;
|
|
2006
|
-
}
|
|
2007
|
-
if (message.includes("parse") || message.includes("schema")) {
|
|
2008
|
-
return false;
|
|
2009
|
-
}
|
|
2010
|
-
return false;
|
|
2011
|
-
}
|
|
2012
|
-
function sleep(ms) {
|
|
2013
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
2014
|
-
}
|
|
2015
|
-
async function withRetry(fn, options = {}) {
|
|
2016
|
-
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
2017
|
-
let lastError;
|
|
2018
|
-
for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
|
|
2019
|
-
try {
|
|
2020
|
-
return await fn();
|
|
2021
|
-
} catch (error) {
|
|
2022
|
-
lastError = error;
|
|
2023
|
-
const isLastAttempt = attempt === opts.maxRetries;
|
|
2024
|
-
const shouldRetry = opts.isRetryable(error);
|
|
2025
|
-
if (isLastAttempt || !shouldRetry) {
|
|
2026
|
-
throw error;
|
|
2027
|
-
}
|
|
2028
|
-
const delay = Math.min(
|
|
2029
|
-
opts.baseDelayMs * Math.pow(2, attempt),
|
|
2030
|
-
opts.maxDelayMs
|
|
2031
|
-
);
|
|
2032
|
-
await sleep(delay);
|
|
2033
|
-
}
|
|
2034
|
-
}
|
|
2035
|
-
throw lastError;
|
|
2036
|
-
}
|
|
2037
|
-
|
|
2038
|
-
// src/core/aggregator.ts
|
|
2039
|
-
function normalizeScore(score100) {
|
|
2040
|
-
const normalized = score100 / 10;
|
|
2041
|
-
return Math.max(0, Math.min(10, normalized));
|
|
2042
|
-
}
|
|
2043
|
-
function lookupIssueMetadata(issueId, taxonomy) {
|
|
2044
|
-
if (taxonomy[issueId]) {
|
|
2045
|
-
return taxonomy[issueId];
|
|
2046
|
-
}
|
|
2047
|
-
return {
|
|
2048
|
-
name: titleCase(issueId),
|
|
2049
|
-
severity: "medium",
|
|
2050
|
-
suggestion: "Review this pattern"
|
|
2051
|
-
};
|
|
2052
|
-
}
|
|
2053
|
-
function titleCase(str) {
|
|
2054
|
-
return str.split("-").map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" ");
|
|
2055
|
-
}
|
|
2056
|
-
function extractRealExamples(issueId, prompts, limit = 3) {
|
|
2057
|
-
const matches = [];
|
|
2058
|
-
for (const prompt of prompts) {
|
|
2059
|
-
if (matches.length >= limit) {
|
|
2060
|
-
break;
|
|
2061
|
-
}
|
|
2062
|
-
const trimmed = prompt.trim();
|
|
2063
|
-
const lowerPrompt = trimmed.toLowerCase();
|
|
2064
|
-
const wordCount = trimmed.split(/\s+/).length;
|
|
2065
|
-
let isMatch = false;
|
|
2066
|
-
switch (issueId) {
|
|
2067
|
-
case "vague":
|
|
2068
|
-
isMatch = trimmed.length < 50 && wordCount <= 5 && (lowerPrompt.includes("help") || lowerPrompt.includes("fix") || lowerPrompt.includes("do") || lowerPrompt.includes("make") || lowerPrompt.includes("create")) && /\w+\.(ts|js|tsx|jsx|py|go|java|rb|php)\b/.exec(lowerPrompt) === null;
|
|
2069
|
-
break;
|
|
2070
|
-
case "no-context":
|
|
2071
|
-
isMatch = (lowerPrompt.includes("this") || lowerPrompt.includes("it") || lowerPrompt.includes("the bug") || lowerPrompt.includes("that")) && /\w+\.(ts|js|tsx|jsx|py|go|java|rb|php)\b/.exec(lowerPrompt) === null && /function|component|method|class/.exec(lowerPrompt) === null;
|
|
2072
|
-
break;
|
|
2073
|
-
case "too-broad": {
|
|
2074
|
-
const andMatches = lowerPrompt.match(/\band\b/g);
|
|
2075
|
-
isMatch = trimmed.length > 100 && (andMatches?.length ?? 0) >= 2 && (lowerPrompt.includes("also") || lowerPrompt.includes("then") || lowerPrompt.includes("build") || lowerPrompt.includes("create"));
|
|
2076
|
-
break;
|
|
2077
|
-
}
|
|
2078
|
-
case "no-goal":
|
|
2079
|
-
isMatch = trimmed.length < 30 && wordCount <= 4 && /\b(how|what|why|when|where|create|build|fix|add)\b/.exec(
|
|
2080
|
-
lowerPrompt
|
|
2081
|
-
) === null && !lowerPrompt.includes("?");
|
|
2082
|
-
break;
|
|
2083
|
-
case "imperative":
|
|
2084
|
-
isMatch = trimmed.length < 20 && wordCount <= 3 && /^(add|delete|remove|update|change|modify)\b/.exec(lowerPrompt) !== null && !lowerPrompt.includes("?");
|
|
2085
|
-
break;
|
|
2086
|
-
case "missing-technical-details": {
|
|
2087
|
-
const hasFileRef = /\w+\.(ts|js|tsx|jsx|py|go|java|rb|php|json|yaml|yml)\b/.exec(
|
|
2088
|
-
lowerPrompt
|
|
2089
|
-
) !== null;
|
|
2090
|
-
const hasFunctionRef = /function|def |class |const |let |var |(\.\w+\()/.exec(
|
|
2091
|
-
lowerPrompt
|
|
2092
|
-
) !== null;
|
|
2093
|
-
const hasErrorRef = /error|exception|failed|crash|undefined|null/.exec(lowerPrompt) !== null;
|
|
2094
|
-
isMatch = !hasFileRef && !hasFunctionRef && !hasErrorRef && wordCount >= 3;
|
|
2095
|
-
break;
|
|
2096
|
-
}
|
|
2097
|
-
case "unclear-priorities": {
|
|
2098
|
-
const andMatches = lowerPrompt.match(/\band\b/g);
|
|
2099
|
-
const orMatches = lowerPrompt.match(/\bor\b/g);
|
|
2100
|
-
const alsoMatches = lowerPrompt.match(/\balso\b/g);
|
|
2101
|
-
const conjunctionCount = (andMatches?.length ?? 0) + (orMatches?.length ?? 0) + (alsoMatches?.length ?? 0);
|
|
2102
|
-
isMatch = conjunctionCount >= 2 && !/\b(first|second|third|then|next|finally|after|before)\b/.exec(
|
|
2103
|
-
lowerPrompt
|
|
2104
|
-
) && wordCount >= 10;
|
|
2105
|
-
break;
|
|
2106
|
-
}
|
|
2107
|
-
case "insufficient-constraints":
|
|
2108
|
-
isMatch = !/\b(constraint|requirement|must|should|edge case|boundary|limit|performance|compatib)\b/.exec(
|
|
2109
|
-
lowerPrompt
|
|
2110
|
-
) && !/\b(<|>|<=|>=|\d+ms|\d+mb|\d+kb)\b/.exec(lowerPrompt) && (lowerPrompt.includes("optimize") || lowerPrompt.includes("improve") || lowerPrompt.includes("make") || lowerPrompt.includes("add")) && wordCount >= 4;
|
|
2111
|
-
break;
|
|
2112
|
-
default:
|
|
2113
|
-
isMatch = false;
|
|
2114
|
-
}
|
|
2115
|
-
if (isMatch) {
|
|
2116
|
-
matches.push(sanitizeExample(trimmed));
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
return [...new Set(matches)];
|
|
2120
|
-
}
|
|
2121
|
-
function sanitizeExample(prompt) {
|
|
2122
|
-
return prompt.trim().replace(/\s+/g, " ");
|
|
2123
|
-
}
|
|
2124
|
-
function convertMinimalToAnalysisResult(minimal, date, taxonomy, prompts) {
|
|
2125
|
-
const issueCounts = /* @__PURE__ */ new Map();
|
|
2126
|
-
for (const issue of minimal.issues) {
|
|
2127
|
-
issueCounts.set(issue, (issueCounts.get(issue) ?? 0) + 1);
|
|
2128
|
-
}
|
|
2129
|
-
const patterns = [...issueCounts.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([issueId, count]) => {
|
|
2130
|
-
const metadata = lookupIssueMetadata(issueId, taxonomy);
|
|
2131
|
-
let examples = prompts ? extractRealExamples(issueId, prompts, 3) : metadata.exampleBefore ? [metadata.exampleBefore] : [];
|
|
2132
|
-
const beforeExample = metadata.exampleBefore ?? "Example not available";
|
|
2133
|
-
if (examples.length === 0) {
|
|
2134
|
-
examples = [beforeExample];
|
|
2135
|
-
}
|
|
2136
|
-
return {
|
|
2137
|
-
id: issueId,
|
|
2138
|
-
name: metadata.name,
|
|
2139
|
-
frequency: count,
|
|
2140
|
-
severity: metadata.severity,
|
|
2141
|
-
examples: [...examples],
|
|
2142
|
-
suggestion: metadata.suggestion,
|
|
2143
|
-
beforeAfter: {
|
|
2144
|
-
before: beforeExample,
|
|
2145
|
-
after: metadata.exampleAfter ?? metadata.suggestion
|
|
2146
|
-
}
|
|
2147
|
-
};
|
|
2148
|
-
});
|
|
2149
|
-
return {
|
|
2150
|
-
date,
|
|
2151
|
-
patterns,
|
|
2152
|
-
stats: {
|
|
2153
|
-
totalPrompts: 1,
|
|
2154
|
-
promptsWithIssues: minimal.issues.length > 0 ? 1 : 0,
|
|
2155
|
-
overallScore: normalizeScore(minimal.score)
|
|
2156
|
-
},
|
|
2157
|
-
topSuggestion: patterns.length > 0 && patterns[0] ? patterns[0].suggestion : "Your prompts look good!"
|
|
2158
|
-
};
|
|
2159
|
-
}
|
|
2160
|
-
|
|
2161
|
-
// src/providers/base.ts
|
|
2162
|
-
var PLACEHOLDER_PATTERNS = [
|
|
2163
|
-
"kebab-case-id",
|
|
2164
|
-
"Original problematic prompt",
|
|
2165
|
-
"Improved version addressing the issue",
|
|
2166
|
-
"<PLACEHOLDER_EXAMPLE>",
|
|
2167
|
-
"Human-Readable Issue Name"
|
|
2168
|
-
];
|
|
2169
|
-
function containsPlaceholders(response) {
|
|
2170
|
-
const lower = response.toLowerCase();
|
|
2171
|
-
return PLACEHOLDER_PATTERNS.some((p) => lower.includes(p.toLowerCase()));
|
|
2172
|
-
}
|
|
2173
|
-
function buildUserPrompt(prompts, date, context) {
|
|
2174
|
-
if (prompts.length === 0) {
|
|
2175
|
-
throw new Error("Cannot build prompt from empty array");
|
|
2176
|
-
}
|
|
2177
|
-
const promptsList = prompts.map((prompt, index) => `${String(index + 1)}. ${prompt}`).join("\n\n");
|
|
2178
|
-
const count = String(prompts.length);
|
|
2179
|
-
const plural = prompts.length === 1 ? "" : "s";
|
|
2180
|
-
let contextSection = "";
|
|
2181
|
-
if (context) {
|
|
2182
|
-
const contextParts = [];
|
|
2183
|
-
if (context.role) {
|
|
2184
|
-
contextParts.push(`Role: ${context.role}`);
|
|
2185
|
-
}
|
|
2186
|
-
if (context.projectType) {
|
|
2187
|
-
contextParts.push(`Project Type: ${context.projectType}`);
|
|
2188
|
-
}
|
|
2189
|
-
if (context.domain) {
|
|
2190
|
-
contextParts.push(`Domain: ${context.domain}`);
|
|
2191
|
-
}
|
|
2192
|
-
if (context.techStack && context.techStack.length > 0) {
|
|
2193
|
-
contextParts.push(`Tech Stack: ${context.techStack.join(", ")}`);
|
|
2194
|
-
}
|
|
2195
|
-
if (context.guidelines && context.guidelines.length > 0) {
|
|
2196
|
-
contextParts.push(
|
|
2197
|
-
`Guidelines:
|
|
2198
|
-
${context.guidelines.map((g) => `- ${g}`).join("\n")}`
|
|
2199
|
-
);
|
|
2200
|
-
}
|
|
2201
|
-
if (contextParts.length > 0) {
|
|
2202
|
-
contextSection = `
|
|
2203
|
-
|
|
2204
|
-
Project Context:
|
|
2205
|
-
${contextParts.join("\n")}
|
|
2206
|
-
`;
|
|
2207
|
-
}
|
|
2208
|
-
}
|
|
2209
|
-
return `Analyze the following ${count} prompt${plural} from ${date}:${contextSection}
|
|
2210
|
-
|
|
2211
|
-
${promptsList}
|
|
2212
|
-
|
|
2213
|
-
Please provide your analysis as a JSON object following the specified schema.`;
|
|
2214
|
-
}
|
|
2215
|
-
function isValidMinimalResponse(value) {
|
|
2216
|
-
if (typeof value !== "object" || value === null) {
|
|
2217
|
-
return false;
|
|
2218
|
-
}
|
|
2219
|
-
const obj = value;
|
|
2220
|
-
if (!Array.isArray(obj["issues"])) {
|
|
2221
|
-
return false;
|
|
2222
|
-
}
|
|
2223
|
-
if (!obj["issues"].every((item) => typeof item === "string")) {
|
|
2224
|
-
return false;
|
|
2225
|
-
}
|
|
2226
|
-
return true;
|
|
2227
|
-
}
|
|
2228
|
-
function extractMinimalResult(obj) {
|
|
2229
|
-
const issues = obj["issues"];
|
|
2230
|
-
const score = typeof obj["score"] === "number" ? obj["score"] : 50;
|
|
2231
|
-
return { issues, score };
|
|
2232
|
-
}
|
|
2233
|
-
function issueToPattern(issue, index) {
|
|
2234
|
-
const id = issue.name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
2235
|
-
return {
|
|
2236
|
-
id: id || `issue-${String(index)}`,
|
|
2237
|
-
name: issue.name,
|
|
2238
|
-
frequency: 1,
|
|
2239
|
-
severity: "medium",
|
|
2240
|
-
examples: [issue.example],
|
|
2241
|
-
suggestion: issue.fix,
|
|
2242
|
-
beforeAfter: {
|
|
2243
|
-
before: issue.example,
|
|
2244
|
-
after: issue.fix
|
|
2245
|
-
}
|
|
2246
|
-
};
|
|
2247
|
-
}
|
|
2248
|
-
function tryFixTruncatedJson(json) {
|
|
2249
|
-
let fixed = json.trim();
|
|
2250
|
-
let openBraces = 0;
|
|
2251
|
-
let openBrackets = 0;
|
|
2252
|
-
let inString = false;
|
|
2253
|
-
let escapeNext = false;
|
|
2254
|
-
for (const char of fixed) {
|
|
2255
|
-
if (escapeNext) {
|
|
2256
|
-
escapeNext = false;
|
|
2257
|
-
continue;
|
|
2258
|
-
}
|
|
2259
|
-
if (char === "\\") {
|
|
2260
|
-
escapeNext = true;
|
|
2261
|
-
continue;
|
|
2262
|
-
}
|
|
2263
|
-
if (char === '"') {
|
|
2264
|
-
inString = !inString;
|
|
2265
|
-
continue;
|
|
2266
|
-
}
|
|
2267
|
-
if (inString) continue;
|
|
2268
|
-
if (char === "{") openBraces++;
|
|
2269
|
-
if (char === "}") openBraces--;
|
|
2270
|
-
if (char === "[") openBrackets++;
|
|
2271
|
-
if (char === "]") openBrackets--;
|
|
2272
|
-
}
|
|
2273
|
-
if (inString) {
|
|
2274
|
-
fixed += '"';
|
|
2275
|
-
}
|
|
2276
|
-
fixed = fixed.replace(/,\s*$/, "");
|
|
2277
|
-
while (openBrackets > 0) {
|
|
2278
|
-
fixed += "]";
|
|
2279
|
-
openBrackets--;
|
|
2280
|
-
}
|
|
2281
|
-
while (openBraces > 0) {
|
|
2282
|
-
fixed += "}";
|
|
2283
|
-
openBraces--;
|
|
2284
|
-
}
|
|
2285
|
-
return fixed;
|
|
2286
|
-
}
|
|
2287
|
-
function parseResponse(response, date, taxonomy = ISSUE_TAXONOMY, prompts) {
|
|
2288
|
-
const codeBlockRegex = /```(?:json)?\s*\n?([\s\S]*?)\n?```/;
|
|
2289
|
-
const jsonMatch = codeBlockRegex.exec(response);
|
|
2290
|
-
let jsonString = jsonMatch?.[1]?.trim() ?? response.trim();
|
|
2291
|
-
let parsed;
|
|
2292
|
-
try {
|
|
2293
|
-
parsed = JSON.parse(jsonString);
|
|
2294
|
-
} catch {
|
|
2295
|
-
jsonString = tryFixTruncatedJson(jsonString);
|
|
2296
|
-
try {
|
|
2297
|
-
parsed = JSON.parse(jsonString);
|
|
2298
|
-
} catch (error) {
|
|
2299
|
-
throw new Error(
|
|
2300
|
-
`Failed to parse response as JSON: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2301
|
-
);
|
|
2302
|
-
}
|
|
2303
|
-
}
|
|
2304
|
-
if (containsPlaceholders(response)) {
|
|
2305
|
-
throw new Error(
|
|
2306
|
-
"Response contains placeholder text - model did not provide real analysis"
|
|
2307
|
-
);
|
|
2308
|
-
}
|
|
2309
|
-
if (isValidMinimalResponse(parsed)) {
|
|
2310
|
-
const minimal = extractMinimalResult(parsed);
|
|
2311
|
-
return convertMinimalToAnalysisResult(minimal, date, taxonomy, prompts);
|
|
2312
|
-
}
|
|
2313
|
-
if (isValidSimpleResponse(parsed)) {
|
|
2314
|
-
const simple = extractSimpleResponse(parsed);
|
|
2315
|
-
const patterns = simple.issues.map((issue, i) => issueToPattern(issue, i));
|
|
2316
|
-
return {
|
|
2317
|
-
date,
|
|
2318
|
-
patterns,
|
|
2319
|
-
stats: {
|
|
2320
|
-
totalPrompts: 0,
|
|
2321
|
-
// Will be filled by caller
|
|
2322
|
-
promptsWithIssues: simple.issues.length,
|
|
2323
|
-
overallScore: normalizeScore(simple.score)
|
|
2324
|
-
},
|
|
2325
|
-
topSuggestion: simple.tip
|
|
2326
|
-
};
|
|
2327
|
-
}
|
|
2328
|
-
if (isValidFullResponse(parsed)) {
|
|
2329
|
-
const normalizedPatterns = parsed.patterns.map((pattern) => {
|
|
2330
|
-
if (pattern.examples.length === 0) {
|
|
2331
|
-
return {
|
|
2332
|
-
...pattern,
|
|
2333
|
-
examples: [pattern.beforeAfter.before]
|
|
2334
|
-
};
|
|
2335
|
-
}
|
|
2336
|
-
return pattern;
|
|
2337
|
-
});
|
|
2338
|
-
return {
|
|
2339
|
-
date,
|
|
2340
|
-
patterns: normalizedPatterns,
|
|
2341
|
-
stats: {
|
|
2342
|
-
...parsed.stats,
|
|
2343
|
-
// Override totalPrompts with actual count if prompts provided
|
|
2344
|
-
totalPrompts: prompts?.length ?? parsed.stats.totalPrompts,
|
|
2345
|
-
overallScore: normalizeScore(parsed.stats.overallScore)
|
|
2346
|
-
},
|
|
2347
|
-
topSuggestion: parsed.topSuggestion
|
|
2348
|
-
};
|
|
2349
|
-
}
|
|
2350
|
-
throw new Error("Response does not match expected schema");
|
|
2351
|
-
}
|
|
2352
|
-
function isValidSimpleResponse(value) {
|
|
2353
|
-
if (typeof value !== "object" || value === null) {
|
|
2354
|
-
return false;
|
|
2355
|
-
}
|
|
2356
|
-
const obj = value;
|
|
2357
|
-
if (!Array.isArray(obj["issues"])) {
|
|
2358
|
-
return false;
|
|
2359
|
-
}
|
|
2360
|
-
for (const issue of obj["issues"]) {
|
|
2361
|
-
if (typeof issue !== "object" || issue === null) {
|
|
2362
|
-
return false;
|
|
2363
|
-
}
|
|
2364
|
-
const i = issue;
|
|
2365
|
-
if (typeof i["name"] !== "string" || typeof i["example"] !== "string" || typeof i["fix"] !== "string") {
|
|
2366
|
-
return false;
|
|
2367
|
-
}
|
|
2368
|
-
}
|
|
2369
|
-
return true;
|
|
2370
|
-
}
|
|
2371
|
-
function extractSimpleResponse(obj) {
|
|
2372
|
-
const issues = obj["issues"];
|
|
2373
|
-
const score = typeof obj["score"] === "number" ? obj["score"] : 50;
|
|
2374
|
-
const tip = typeof obj["tip"] === "string" ? obj["tip"] : issues.length > 0 && issues[0] ? issues[0].fix : "No suggestions";
|
|
2375
|
-
return { issues, score, tip };
|
|
2376
|
-
}
|
|
2377
|
-
function isValidFullResponse(value) {
|
|
2378
|
-
if (typeof value !== "object" || value === null) {
|
|
2379
|
-
return false;
|
|
2380
|
-
}
|
|
2381
|
-
const obj = value;
|
|
2382
|
-
if (!Array.isArray(obj["patterns"])) {
|
|
2383
|
-
return false;
|
|
2384
|
-
}
|
|
2385
|
-
for (const pattern of obj["patterns"]) {
|
|
2386
|
-
if (!isValidPattern(pattern)) {
|
|
2387
|
-
return false;
|
|
2388
|
-
}
|
|
2389
|
-
}
|
|
2390
|
-
if (typeof obj["stats"] !== "object" || obj["stats"] === null) {
|
|
2391
|
-
return false;
|
|
2392
|
-
}
|
|
2393
|
-
const stats = obj["stats"];
|
|
2394
|
-
if (typeof stats["totalPrompts"] !== "number" || typeof stats["promptsWithIssues"] !== "number" || typeof stats["overallScore"] !== "number") {
|
|
2395
|
-
return false;
|
|
2396
|
-
}
|
|
2397
|
-
if (typeof obj["topSuggestion"] !== "string") {
|
|
2398
|
-
return false;
|
|
2399
|
-
}
|
|
2400
|
-
return true;
|
|
2401
|
-
}
|
|
2402
|
-
function isValidPattern(value) {
|
|
2403
|
-
if (typeof value !== "object" || value === null) {
|
|
2404
|
-
return false;
|
|
2405
|
-
}
|
|
2406
|
-
const obj = value;
|
|
2407
|
-
if (typeof obj["id"] !== "string" || typeof obj["name"] !== "string" || typeof obj["frequency"] !== "number" || typeof obj["suggestion"] !== "string") {
|
|
2408
|
-
return false;
|
|
2409
|
-
}
|
|
2410
|
-
if (obj["severity"] !== "low" && obj["severity"] !== "medium" && obj["severity"] !== "high") {
|
|
2411
|
-
return false;
|
|
2412
|
-
}
|
|
2413
|
-
if (!Array.isArray(obj["examples"]) || !obj["examples"].every((e) => typeof e === "string")) {
|
|
2414
|
-
return false;
|
|
2415
|
-
}
|
|
2416
|
-
if (typeof obj["beforeAfter"] !== "object" || obj["beforeAfter"] === null) {
|
|
2417
|
-
return false;
|
|
2418
|
-
}
|
|
2419
|
-
const beforeAfter = obj["beforeAfter"];
|
|
2420
|
-
if (typeof beforeAfter["before"] !== "string" || typeof beforeAfter["after"] !== "string") {
|
|
2421
|
-
return false;
|
|
2422
|
-
}
|
|
2423
|
-
return true;
|
|
2424
|
-
}
|
|
2425
|
-
function parseBatchIndividualResponse(response, date, prompts) {
|
|
2426
|
-
logger.debug(
|
|
2427
|
-
`Parsing batch-individual response (${String(prompts.length)} prompts)`,
|
|
2428
|
-
"parser"
|
|
2429
|
-
);
|
|
2430
|
-
logger.debug(`Response length: ${String(response.length)} chars`, "parser");
|
|
2431
|
-
const codeBlockRegex = /```(?:json)?\s*\n?([\s\S]*?)\n?```/;
|
|
2432
|
-
const jsonMatch = codeBlockRegex.exec(response);
|
|
2433
|
-
let jsonString = jsonMatch?.[1]?.trim() ?? response.trim();
|
|
2434
|
-
let parsed;
|
|
2435
|
-
try {
|
|
2436
|
-
parsed = JSON.parse(jsonString);
|
|
2437
|
-
} catch {
|
|
2438
|
-
jsonString = tryFixTruncatedJson(jsonString);
|
|
2439
|
-
try {
|
|
2440
|
-
parsed = JSON.parse(jsonString);
|
|
2441
|
-
} catch (error) {
|
|
2442
|
-
throw new Error(
|
|
2443
|
-
`Failed to parse batch-individual response as JSON: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
2444
|
-
);
|
|
2445
|
-
}
|
|
2446
|
-
}
|
|
2447
|
-
let parsedArray;
|
|
2448
|
-
if (!Array.isArray(parsed)) {
|
|
2449
|
-
if (typeof parsed === "object" && parsed !== null && isValidIndividualPromptResult(parsed)) {
|
|
2450
|
-
logger.debug(
|
|
2451
|
-
"Model returned single object instead of array, wrapping it",
|
|
2452
|
-
"parser"
|
|
2453
|
-
);
|
|
2454
|
-
parsedArray = [parsed];
|
|
2455
|
-
} else {
|
|
2456
|
-
throw new Error(
|
|
2457
|
-
"Batch-individual response must be an array of individual results"
|
|
2458
|
-
);
|
|
2459
|
-
}
|
|
2460
|
-
} else {
|
|
2461
|
-
parsedArray = parsed;
|
|
2462
|
-
}
|
|
2463
|
-
const individualResults = [];
|
|
2464
|
-
for (let i = 0; i < parsedArray.length; i++) {
|
|
2465
|
-
const item = parsedArray[i];
|
|
2466
|
-
if (!isValidIndividualPromptResult(item)) {
|
|
2467
|
-
individualResults.push({
|
|
2468
|
-
status: "problems",
|
|
2469
|
-
problems: ["Invalid response format"],
|
|
2470
|
-
categories: ["other"],
|
|
2471
|
-
example: prompts[i] ?? `Prompt ${String(i + 1)}`,
|
|
2472
|
-
suggestion: "Could not analyze this prompt"
|
|
2473
|
-
});
|
|
2474
|
-
} else {
|
|
2475
|
-
individualResults.push(item);
|
|
2476
|
-
}
|
|
2477
|
-
}
|
|
2478
|
-
return convertIndividualResultsToAnalysis(individualResults, date);
|
|
2479
|
-
}
|
|
2480
|
-
function isValidIndividualPromptResult(value) {
|
|
2481
|
-
if (typeof value !== "object" || value === null) {
|
|
2482
|
-
return false;
|
|
2483
|
-
}
|
|
2484
|
-
const obj = value;
|
|
2485
|
-
return (obj["status"] === "correct" || obj["status"] === "problems") && Array.isArray(obj["problems"]) && Array.isArray(obj["categories"]) && typeof obj["example"] === "string" && typeof obj["suggestion"] === "string";
|
|
2486
|
-
}
|
|
2487
|
-
function convertIndividualResultsToAnalysis(results, date) {
|
|
2488
|
-
const totalPrompts = results.length;
|
|
2489
|
-
let promptsWithIssues = 0;
|
|
2490
|
-
const categoryMap = /* @__PURE__ */ new Map();
|
|
2491
|
-
for (const result of results) {
|
|
2492
|
-
if (result.status === "problems") {
|
|
2493
|
-
promptsWithIssues++;
|
|
2494
|
-
for (const category of result.categories) {
|
|
2495
|
-
if (!categoryMap.has(category)) {
|
|
2496
|
-
categoryMap.set(category, {
|
|
2497
|
-
examples: [],
|
|
2498
|
-
suggestions: [],
|
|
2499
|
-
count: 0
|
|
2500
|
-
});
|
|
2501
|
-
}
|
|
2502
|
-
const group = categoryMap.get(category);
|
|
2503
|
-
if (group) {
|
|
2504
|
-
group.count++;
|
|
2505
|
-
if (group.examples.length < 3) {
|
|
2506
|
-
group.examples.push(result.example);
|
|
2507
|
-
}
|
|
2508
|
-
if (!group.suggestions.includes(result.suggestion)) {
|
|
2509
|
-
group.suggestions.push(result.suggestion);
|
|
2510
|
-
}
|
|
2511
|
-
}
|
|
2512
|
-
}
|
|
2513
|
-
}
|
|
2514
|
-
}
|
|
2515
|
-
const patterns = [];
|
|
2516
|
-
for (const [categoryId, group] of categoryMap.entries()) {
|
|
2517
|
-
const metadata = getCategoryMetadata(categoryId);
|
|
2518
|
-
patterns.push({
|
|
2519
|
-
id: categoryId,
|
|
2520
|
-
name: metadata.name,
|
|
2521
|
-
frequency: group.count,
|
|
2522
|
-
severity: metadata.severity,
|
|
2523
|
-
examples: group.examples,
|
|
2524
|
-
suggestion: group.suggestions[0] ?? metadata.suggestion,
|
|
2525
|
-
beforeAfter: {
|
|
2526
|
-
before: group.examples[0] ?? "Example prompt",
|
|
2527
|
-
after: metadata.suggestion
|
|
2528
|
-
}
|
|
2529
|
-
});
|
|
2530
|
-
}
|
|
2531
|
-
patterns.sort((a, b) => {
|
|
2532
|
-
if (b.frequency !== a.frequency) {
|
|
2533
|
-
return b.frequency - a.frequency;
|
|
2534
|
-
}
|
|
2535
|
-
const severityRank = { high: 3, medium: 2, low: 1 };
|
|
2536
|
-
return severityRank[b.severity] - severityRank[a.severity];
|
|
2537
|
-
});
|
|
2538
|
-
const issueRate = promptsWithIssues / totalPrompts;
|
|
2539
|
-
const overallScore = Math.round((1 - issueRate) * 100);
|
|
2540
|
-
return {
|
|
2541
|
-
date,
|
|
2542
|
-
patterns: patterns.slice(0, 5),
|
|
2543
|
-
// Top 5 patterns
|
|
2544
|
-
stats: {
|
|
2545
|
-
totalPrompts,
|
|
2546
|
-
promptsWithIssues,
|
|
2547
|
-
overallScore: normalizeScore(overallScore)
|
|
2548
|
-
},
|
|
2549
|
-
topSuggestion: patterns.length > 0 && patterns[0] ? patterns[0].suggestion : "No issues detected"
|
|
2550
|
-
};
|
|
2551
|
-
}
|
|
2552
|
-
function getCategoryMetadata(category) {
|
|
2553
|
-
const categoryMetadata = {
|
|
2554
|
-
"vague-request": {
|
|
2555
|
-
name: "Vague Request",
|
|
2556
|
-
severity: "high",
|
|
2557
|
-
suggestion: "Be more specific - include function names, file paths, or describe the exact issue"
|
|
2558
|
-
},
|
|
2559
|
-
"missing-context": {
|
|
2560
|
-
name: "Missing Context",
|
|
2561
|
-
severity: "high",
|
|
2562
|
-
suggestion: "Provide necessary context - file paths, function names, error messages, or code snippets"
|
|
2563
|
-
},
|
|
2564
|
-
"too-broad": {
|
|
2565
|
-
name: "Too Broad",
|
|
2566
|
-
severity: "medium",
|
|
2567
|
-
suggestion: "Break down into smaller, focused requests - tackle one task at a time"
|
|
2568
|
-
},
|
|
2569
|
-
"unclear-goal": {
|
|
2570
|
-
name: "Unclear Goal",
|
|
2571
|
-
severity: "high",
|
|
2572
|
-
suggestion: "State what you want to achieve - specify success criteria and desired outcome"
|
|
2573
|
-
},
|
|
2574
|
-
other: {
|
|
2575
|
-
name: "Other Issues",
|
|
2576
|
-
severity: "low",
|
|
2577
|
-
suggestion: "Review and improve prompt clarity and specificity"
|
|
2578
|
-
}
|
|
2579
|
-
};
|
|
2580
|
-
return categoryMetadata[category] ?? {
|
|
2581
|
-
name: category,
|
|
2582
|
-
severity: "low",
|
|
2583
|
-
suggestion: "Improve prompt quality"
|
|
2584
|
-
};
|
|
2585
|
-
}
|
|
2586
|
-
|
|
2587
|
-
// src/providers/anthropic.ts
|
|
2588
|
-
var MAX_RETRIES = 3;
|
|
2589
|
-
var AVAILABILITY_TIMEOUT_MS = 5e3;
|
|
2590
|
-
var ANALYSIS_TIMEOUT_MS = 6e4;
|
|
2591
|
-
var BASE_DELAY_MS = 1e3;
|
|
2592
|
-
var MAX_DELAY_MS = 3e4;
|
|
2593
|
-
var API_BASE_URL = "https://api.anthropic.com";
|
|
2594
|
-
var API_VERSION = "2023-06-01";
|
|
2595
|
-
var MAX_TOKENS = 4096;
|
|
2596
|
-
var AnthropicProvider = class {
|
|
2597
|
-
name = "Anthropic";
|
|
2598
|
-
config;
|
|
2599
|
-
rateLimiter;
|
|
2600
|
-
/**
|
|
2601
|
-
* Creates a new AnthropicProvider instance.
|
|
2602
|
-
*
|
|
2603
|
-
* @param config - Anthropic configuration with model and API key
|
|
2604
|
-
*/
|
|
2605
|
-
constructor(config) {
|
|
2606
|
-
this.config = config;
|
|
2607
|
-
this.rateLimiter = createRateLimiter({
|
|
2608
|
-
requestsPerMinute: DEFAULT_RATE_LIMITS.anthropic
|
|
2609
|
-
});
|
|
2610
|
-
}
|
|
2611
|
-
/**
|
|
2612
|
-
* Checks if the Anthropic API is available by validating the API key.
|
|
2613
|
-
* Makes a minimal request to verify the key is valid.
|
|
2614
|
-
*
|
|
2615
|
-
* @returns Promise that resolves to true if available, false otherwise
|
|
2616
|
-
*/
|
|
2617
|
-
async isAvailable() {
|
|
2618
|
-
if (!this.config.apiKey) {
|
|
2619
|
-
logger.debug("No API key configured", "anthropic");
|
|
2620
|
-
return false;
|
|
2621
|
-
}
|
|
2622
|
-
logger.debug(
|
|
2623
|
-
`Validating API key with model ${this.config.model}`,
|
|
2624
|
-
"anthropic"
|
|
2625
|
-
);
|
|
2626
|
-
try {
|
|
2627
|
-
const controller = new AbortController();
|
|
2628
|
-
const timeoutId = setTimeout(() => {
|
|
2629
|
-
controller.abort();
|
|
2630
|
-
}, AVAILABILITY_TIMEOUT_MS);
|
|
2631
|
-
const response = await fetch(`${API_BASE_URL}/v1/messages`, {
|
|
2632
|
-
method: "POST",
|
|
2633
|
-
headers: {
|
|
2634
|
-
"Content-Type": "application/json",
|
|
2635
|
-
"x-api-key": this.config.apiKey,
|
|
2636
|
-
"anthropic-version": API_VERSION
|
|
2637
|
-
},
|
|
2638
|
-
body: JSON.stringify({
|
|
2639
|
-
model: this.config.model,
|
|
2640
|
-
max_tokens: 1,
|
|
2641
|
-
messages: [{ role: "user", content: "Hi" }]
|
|
2642
|
-
}),
|
|
2643
|
-
signal: controller.signal
|
|
2644
|
-
});
|
|
2645
|
-
clearTimeout(timeoutId);
|
|
2646
|
-
const isAvailable = response.ok || response.status === 429 || response.status === 400;
|
|
2647
|
-
logger.debug(
|
|
2648
|
-
`API responded with status ${String(response.status)} (available: ${String(isAvailable)})`,
|
|
2649
|
-
"anthropic"
|
|
2650
|
-
);
|
|
2651
|
-
return isAvailable;
|
|
2652
|
-
} catch {
|
|
2653
|
-
logger.debug("Connection failed", "anthropic");
|
|
2654
|
-
return false;
|
|
2655
|
-
}
|
|
2656
|
-
}
|
|
2657
|
-
/**
|
|
2658
|
-
* Analyzes prompts using the Anthropic Claude API.
|
|
2659
|
-
* Implements retry logic with exponential backoff for network errors.
|
|
2660
|
-
* Rate limited to prevent 429 errors during batch processing.
|
|
2661
|
-
*
|
|
2662
|
-
* @param prompts - Array of prompt strings to analyze
|
|
2663
|
-
* @param date - Date context for the analysis
|
|
2664
|
-
* @param context - Optional project context for analysis
|
|
2665
|
-
* @returns Promise resolving to AnalysisResult
|
|
2666
|
-
* @throws Error if analysis fails after retries or if response is invalid
|
|
2667
|
-
*/
|
|
2668
|
-
async analyze(prompts, date, context) {
|
|
2669
|
-
if (prompts.length === 0) {
|
|
2670
|
-
throw new Error("Cannot analyze empty prompts array");
|
|
2671
|
-
}
|
|
2672
|
-
const userPrompt = buildUserPrompt(prompts, date, context);
|
|
2673
|
-
return this.rateLimiter.throttle(
|
|
2674
|
-
() => withRetry(
|
|
2675
|
-
async () => {
|
|
2676
|
-
const controller = new AbortController();
|
|
2677
|
-
const timeoutId = setTimeout(() => {
|
|
2678
|
-
controller.abort();
|
|
2679
|
-
}, ANALYSIS_TIMEOUT_MS);
|
|
2680
|
-
try {
|
|
2681
|
-
const response = await fetch(`${API_BASE_URL}/v1/messages`, {
|
|
2682
|
-
method: "POST",
|
|
2683
|
-
headers: {
|
|
2684
|
-
"Content-Type": "application/json",
|
|
2685
|
-
"x-api-key": this.config.apiKey,
|
|
2686
|
-
"anthropic-version": API_VERSION
|
|
2687
|
-
},
|
|
2688
|
-
body: JSON.stringify({
|
|
2689
|
-
model: this.config.model,
|
|
2690
|
-
max_tokens: MAX_TOKENS,
|
|
2691
|
-
system: SYSTEM_PROMPT_FULL,
|
|
2692
|
-
messages: [{ role: "user", content: userPrompt }]
|
|
2693
|
-
}),
|
|
2694
|
-
signal: controller.signal
|
|
2695
|
-
});
|
|
2696
|
-
if (!response.ok) {
|
|
2697
|
-
const status = String(response.status);
|
|
2698
|
-
const errorBody = await response.text();
|
|
2699
|
-
throw new Error(
|
|
2700
|
-
`Anthropic API request failed: ${status} ${response.statusText} - ${errorBody}`
|
|
2701
|
-
);
|
|
2702
|
-
}
|
|
2703
|
-
const data = await response.json();
|
|
2704
|
-
const textContent = data.content?.find(
|
|
2705
|
-
(block) => block.type === "text"
|
|
2706
|
-
);
|
|
2707
|
-
if (!textContent?.text) {
|
|
2708
|
-
throw new Error("Invalid response format from Anthropic API");
|
|
2709
|
-
}
|
|
2710
|
-
return parseResponse(textContent.text, date, void 0, prompts);
|
|
2711
|
-
} finally {
|
|
2712
|
-
clearTimeout(timeoutId);
|
|
2713
|
-
}
|
|
2714
|
-
},
|
|
2715
|
-
{
|
|
2716
|
-
maxRetries: MAX_RETRIES,
|
|
2717
|
-
baseDelayMs: BASE_DELAY_MS,
|
|
2718
|
-
maxDelayMs: MAX_DELAY_MS,
|
|
2719
|
-
isRetryable: isTransientError
|
|
2720
|
-
}
|
|
2721
|
-
)
|
|
2722
|
-
);
|
|
2723
|
-
}
|
|
2724
|
-
};
|
|
2725
|
-
|
|
2726
|
-
// src/providers/google.ts
|
|
2727
|
-
var MAX_RETRIES2 = 3;
|
|
2728
|
-
var AVAILABILITY_TIMEOUT_MS2 = 5e3;
|
|
2729
|
-
var ANALYSIS_TIMEOUT_MS2 = 6e4;
|
|
2730
|
-
var BASE_DELAY_MS2 = 1e3;
|
|
2731
|
-
var MAX_DELAY_MS2 = 3e4;
|
|
2732
|
-
var API_BASE_URL2 = "https://generativelanguage.googleapis.com/v1beta";
|
|
2733
|
-
var GoogleProvider = class {
|
|
2734
|
-
name = "Google";
|
|
2735
|
-
config;
|
|
2736
|
-
rateLimiter;
|
|
2737
|
-
/**
|
|
2738
|
-
* Creates a new GoogleProvider instance.
|
|
2739
|
-
*
|
|
2740
|
-
* @param config - Google configuration with model and API key
|
|
2741
|
-
*/
|
|
2742
|
-
constructor(config) {
|
|
2743
|
-
this.config = config;
|
|
2744
|
-
this.rateLimiter = createRateLimiter({
|
|
2745
|
-
requestsPerMinute: DEFAULT_RATE_LIMITS.google
|
|
2746
|
-
});
|
|
2747
|
-
}
|
|
2748
|
-
/**
|
|
2749
|
-
* Checks if the Google Gemini API is available by validating the API key.
|
|
2750
|
-
* Makes a minimal request to verify the key is valid.
|
|
2751
|
-
*
|
|
2752
|
-
* @returns Promise that resolves to true if available, false otherwise
|
|
2753
|
-
*/
|
|
2754
|
-
async isAvailable() {
|
|
2755
|
-
if (!this.config.apiKey) {
|
|
2756
|
-
logger.debug("No API key configured", "google");
|
|
2757
|
-
return false;
|
|
2758
|
-
}
|
|
2759
|
-
logger.debug(
|
|
2760
|
-
`Validating API key with model ${this.config.model}`,
|
|
2761
|
-
"google"
|
|
2762
|
-
);
|
|
2763
|
-
try {
|
|
2764
|
-
const controller = new AbortController();
|
|
2765
|
-
const timeoutId = setTimeout(() => {
|
|
2766
|
-
controller.abort();
|
|
2767
|
-
}, AVAILABILITY_TIMEOUT_MS2);
|
|
2768
|
-
const url = `${API_BASE_URL2}/models/${this.config.model}:generateContent?key=${this.config.apiKey}`;
|
|
2769
|
-
const response = await fetch(url, {
|
|
2770
|
-
method: "POST",
|
|
2771
|
-
headers: {
|
|
2772
|
-
"Content-Type": "application/json"
|
|
2773
|
-
},
|
|
2774
|
-
body: JSON.stringify({
|
|
2775
|
-
contents: [
|
|
2776
|
-
{
|
|
2777
|
-
role: "user",
|
|
2778
|
-
parts: [{ text: "Hi" }]
|
|
2779
|
-
}
|
|
2780
|
-
],
|
|
2781
|
-
generationConfig: {
|
|
2782
|
-
maxOutputTokens: 1
|
|
2783
|
-
}
|
|
2784
|
-
}),
|
|
2785
|
-
signal: controller.signal
|
|
2786
|
-
});
|
|
2787
|
-
clearTimeout(timeoutId);
|
|
2788
|
-
const isAvailable = response.ok || response.status === 429 || response.status === 400;
|
|
2789
|
-
logger.debug(
|
|
2790
|
-
`API responded with status ${String(response.status)} (available: ${String(isAvailable)})`,
|
|
2791
|
-
"google"
|
|
2792
|
-
);
|
|
2793
|
-
return isAvailable;
|
|
2794
|
-
} catch {
|
|
2795
|
-
logger.debug("Connection failed", "google");
|
|
2796
|
-
return false;
|
|
2797
|
-
}
|
|
2798
|
-
}
|
|
2799
|
-
/**
|
|
2800
|
-
* Analyzes prompts using the Google Gemini API.
|
|
2801
|
-
* Implements retry logic with exponential backoff for network errors.
|
|
2802
|
-
* Rate limited to prevent 429 errors during batch processing.
|
|
2803
|
-
*
|
|
2804
|
-
* @param prompts - Array of prompt strings to analyze
|
|
2805
|
-
* @param date - Date context for the analysis
|
|
2806
|
-
* @param context - Optional project context for analysis
|
|
2807
|
-
* @returns Promise resolving to AnalysisResult
|
|
2808
|
-
* @throws Error if analysis fails after retries or if response is invalid
|
|
2809
|
-
*/
|
|
2810
|
-
async analyze(prompts, date, context) {
|
|
2811
|
-
if (prompts.length === 0) {
|
|
2812
|
-
throw new Error("Cannot analyze empty prompts array");
|
|
2813
|
-
}
|
|
2814
|
-
const userPrompt = buildUserPrompt(prompts, date, context);
|
|
2815
|
-
return this.rateLimiter.throttle(
|
|
2816
|
-
() => withRetry(
|
|
2817
|
-
async () => {
|
|
2818
|
-
const controller = new AbortController();
|
|
2819
|
-
const timeoutId = setTimeout(() => {
|
|
2820
|
-
controller.abort();
|
|
2821
|
-
}, ANALYSIS_TIMEOUT_MS2);
|
|
2822
|
-
try {
|
|
2823
|
-
const url = `${API_BASE_URL2}/models/${this.config.model}:generateContent?key=${this.config.apiKey}`;
|
|
2824
|
-
const response = await fetch(url, {
|
|
2825
|
-
method: "POST",
|
|
2826
|
-
headers: {
|
|
2827
|
-
"Content-Type": "application/json"
|
|
2828
|
-
},
|
|
2829
|
-
body: JSON.stringify({
|
|
2830
|
-
contents: [
|
|
2831
|
-
{
|
|
2832
|
-
role: "user",
|
|
2833
|
-
parts: [{ text: userPrompt }]
|
|
2834
|
-
}
|
|
2835
|
-
],
|
|
2836
|
-
systemInstruction: {
|
|
2837
|
-
parts: [{ text: SYSTEM_PROMPT_FULL }]
|
|
2838
|
-
},
|
|
2839
|
-
generationConfig: {
|
|
2840
|
-
responseMimeType: "application/json"
|
|
2841
|
-
}
|
|
2842
|
-
}),
|
|
2843
|
-
signal: controller.signal
|
|
2844
|
-
});
|
|
2845
|
-
if (!response.ok) {
|
|
2846
|
-
const status = String(response.status);
|
|
2847
|
-
const errorBody = await response.text();
|
|
2848
|
-
throw new Error(
|
|
2849
|
-
`Google API request failed: ${status} ${response.statusText} - ${errorBody}`
|
|
2850
|
-
);
|
|
2851
|
-
}
|
|
2852
|
-
const data = await response.json();
|
|
2853
|
-
const textContent = data.candidates?.[0]?.content?.parts?.[0]?.text;
|
|
2854
|
-
if (!textContent) {
|
|
2855
|
-
throw new Error("Invalid response format from Google API");
|
|
2856
|
-
}
|
|
2857
|
-
return parseResponse(textContent, date, void 0, prompts);
|
|
2858
|
-
} finally {
|
|
2859
|
-
clearTimeout(timeoutId);
|
|
2860
|
-
}
|
|
2861
|
-
},
|
|
2862
|
-
{
|
|
2863
|
-
maxRetries: MAX_RETRIES2,
|
|
2864
|
-
baseDelayMs: BASE_DELAY_MS2,
|
|
2865
|
-
maxDelayMs: MAX_DELAY_MS2,
|
|
2866
|
-
isRetryable: isTransientError
|
|
2867
|
-
}
|
|
2868
|
-
)
|
|
2869
|
-
);
|
|
2870
|
-
}
|
|
2871
|
-
};
|
|
2872
|
-
|
|
2873
|
-
// src/providers/ollama.ts
|
|
2874
|
-
var MAX_RETRIES3 = 2;
|
|
2875
|
-
var AVAILABILITY_TIMEOUT_MS3 = 3e3;
|
|
2876
|
-
var ANALYSIS_TIMEOUT_MS3 = 6e4;
|
|
2877
|
-
var BASE_RETRY_DELAY_MS = 1e3;
|
|
2878
|
-
var MODEL_STRATEGY_MAP = {
|
|
2879
|
-
// Micro (< 4GB)
|
|
2880
|
-
"llama3.2": "micro",
|
|
2881
|
-
"phi3:mini": "micro",
|
|
2882
|
-
"gemma3:4b": "micro",
|
|
2883
|
-
"gemma2:2b": "micro",
|
|
2884
|
-
// Small (4-7GB)
|
|
2885
|
-
"mistral:7b": "small",
|
|
2886
|
-
"llama3:8b": "small",
|
|
2887
|
-
"codellama:7b": "small",
|
|
2888
|
-
// Standard (> 7GB)
|
|
2889
|
-
"llama3:70b": "standard",
|
|
2890
|
-
mixtral: "standard",
|
|
2891
|
-
"qwen2.5:14b": "standard"
|
|
2892
|
-
};
|
|
2893
|
-
function detectBatchStrategy(modelName) {
|
|
2894
|
-
if (MODEL_STRATEGY_MAP[modelName]) {
|
|
2895
|
-
return MODEL_STRATEGY_MAP[modelName];
|
|
2896
|
-
}
|
|
2897
|
-
for (const [pattern, strategy] of Object.entries(MODEL_STRATEGY_MAP)) {
|
|
2898
|
-
if (modelName.includes(pattern)) {
|
|
2899
|
-
return strategy;
|
|
2900
|
-
}
|
|
2901
|
-
}
|
|
2902
|
-
return "micro";
|
|
2903
|
-
}
|
|
2904
|
-
var OllamaProvider = class {
|
|
2905
|
-
name = "Ollama";
|
|
2906
|
-
config;
|
|
2907
|
-
batchStrategy;
|
|
2908
|
-
schemaType;
|
|
2909
|
-
/**
|
|
2910
|
-
* Creates a new OllamaProvider instance.
|
|
2911
|
-
* Automatically detects the optimal batch strategy and schema type based on model name.
|
|
2912
|
-
*
|
|
2913
|
-
* @param config - Ollama configuration with model and host
|
|
2914
|
-
*/
|
|
2915
|
-
constructor(config) {
|
|
2916
|
-
this.config = config;
|
|
2917
|
-
this.batchStrategy = detectBatchStrategy(config.model);
|
|
2918
|
-
this.schemaType = this.selectSchemaType();
|
|
2919
|
-
const strategy = BATCH_STRATEGIES[this.batchStrategy];
|
|
2920
|
-
logger.debug(
|
|
2921
|
-
`Detected batch strategy: ${this.batchStrategy} (${strategy.description}), schema type: ${this.schemaType}`,
|
|
2922
|
-
"ollama"
|
|
2923
|
-
);
|
|
2924
|
-
}
|
|
2925
|
-
/**
|
|
2926
|
-
* Selects the appropriate schema type based on model size and user override.
|
|
2927
|
-
* Micro and small models use individual schema (hybrid approach) by default.
|
|
2928
|
-
* Standard models use full schema for detailed analysis.
|
|
2929
|
-
* User can override via CLI --analysis-mode flag.
|
|
2930
|
-
*
|
|
2931
|
-
* @returns Schema type identifier
|
|
2932
|
-
*/
|
|
2933
|
-
selectSchemaType() {
|
|
2934
|
-
if (this.config.schemaOverride) {
|
|
2935
|
-
return this.config.schemaOverride === "individual" ? "individual" : "full";
|
|
2936
|
-
}
|
|
2937
|
-
return ["micro", "small"].includes(this.batchStrategy) ? "individual" : "full";
|
|
2938
|
-
}
|
|
2939
|
-
/**
|
|
2940
|
-
* Returns dynamic batch limits based on detected model strategy.
|
|
2941
|
-
* When using individual schema, processes one prompt at a time for reliability.
|
|
2942
|
-
*
|
|
2943
|
-
* @returns Provider limits with model-specific constraints
|
|
2944
|
-
*/
|
|
2945
|
-
getBatchLimits() {
|
|
2946
|
-
const strategy = BATCH_STRATEGIES[this.batchStrategy];
|
|
2947
|
-
const maxPromptsPerBatch = this.schemaType === "individual" ? 1 : strategy.maxPromptsPerBatch;
|
|
2948
|
-
return {
|
|
2949
|
-
maxTokensPerBatch: strategy.maxTokensPerBatch,
|
|
2950
|
-
maxPromptsPerBatch,
|
|
2951
|
-
prioritization: "longest-first"
|
|
2952
|
-
};
|
|
2953
|
-
}
|
|
2954
|
-
/**
|
|
2955
|
-
* Checks if the Ollama service is available and has the required model.
|
|
2956
|
-
* Uses a 3-second timeout to avoid hanging on unreachable services.
|
|
2957
|
-
*
|
|
2958
|
-
* @returns Promise that resolves to true if available, false otherwise
|
|
2959
|
-
*/
|
|
2960
|
-
async isAvailable() {
|
|
2961
|
-
logger.debug(`Connecting to Ollama at ${this.config.host}`, "ollama");
|
|
2962
|
-
try {
|
|
2963
|
-
const controller = new AbortController();
|
|
2964
|
-
const timeoutId = setTimeout(() => {
|
|
2965
|
-
controller.abort();
|
|
2966
|
-
}, AVAILABILITY_TIMEOUT_MS3);
|
|
2967
|
-
const response = await fetch(`${this.config.host}/api/tags`, {
|
|
2968
|
-
signal: controller.signal
|
|
2969
|
-
});
|
|
2970
|
-
clearTimeout(timeoutId);
|
|
2971
|
-
if (!response.ok) {
|
|
2972
|
-
logger.debug(
|
|
2973
|
-
`Ollama API returned ${String(response.status)}`,
|
|
2974
|
-
"ollama"
|
|
2975
|
-
);
|
|
2976
|
-
return false;
|
|
2977
|
-
}
|
|
2978
|
-
const data = await response.json();
|
|
2979
|
-
if (!data.models || !Array.isArray(data.models)) {
|
|
2980
|
-
logger.debug("Ollama returned invalid model list", "ollama");
|
|
2981
|
-
return false;
|
|
2982
|
-
}
|
|
2983
|
-
const modelAvailable = data.models.some(
|
|
2984
|
-
(model) => model.name.includes(this.config.model)
|
|
2985
|
-
);
|
|
2986
|
-
if (modelAvailable) {
|
|
2987
|
-
logger.debug(
|
|
2988
|
-
`Model ${this.config.model} available (${String(data.models.length)} models found)`,
|
|
2989
|
-
"ollama"
|
|
2990
|
-
);
|
|
2991
|
-
} else {
|
|
2992
|
-
logger.debug(
|
|
2993
|
-
`Model ${this.config.model} not found in available models`,
|
|
2994
|
-
"ollama"
|
|
2995
|
-
);
|
|
2996
|
-
}
|
|
2997
|
-
return modelAvailable;
|
|
2998
|
-
} catch {
|
|
2999
|
-
logger.debug("Ollama connection failed", "ollama");
|
|
3000
|
-
return false;
|
|
3001
|
-
}
|
|
3002
|
-
}
|
|
3003
|
-
/**
|
|
3004
|
-
* Analyzes prompts using the Ollama service.
|
|
3005
|
-
* Implements retry logic with exponential backoff for network errors.
|
|
3006
|
-
*
|
|
3007
|
-
* @param prompts - Array of prompt strings to analyze
|
|
3008
|
-
* @param date - Date context for the analysis
|
|
3009
|
-
* @param context - Optional project context for analysis
|
|
3010
|
-
* @returns Promise resolving to AnalysisResult
|
|
3011
|
-
* @throws Error if analysis fails after retries or if response is invalid
|
|
3012
|
-
*/
|
|
3013
|
-
async analyze(prompts, date, context) {
|
|
3014
|
-
if (prompts.length === 0) {
|
|
3015
|
-
throw new Error("Cannot analyze empty prompts array");
|
|
3016
|
-
}
|
|
3017
|
-
const userPrompt = buildUserPrompt(prompts, date, context);
|
|
3018
|
-
const systemPrompt = this.schemaType === "individual" ? SYSTEM_PROMPT_BATCH_INDIVIDUAL : this.schemaType === "minimal" ? SYSTEM_PROMPT_MINIMAL : SYSTEM_PROMPT_FULL;
|
|
3019
|
-
let lastError;
|
|
3020
|
-
for (let attempt = 0; attempt <= MAX_RETRIES3; attempt++) {
|
|
3021
|
-
try {
|
|
3022
|
-
const controller = new AbortController();
|
|
3023
|
-
const timeoutId = setTimeout(() => {
|
|
3024
|
-
controller.abort();
|
|
3025
|
-
}, ANALYSIS_TIMEOUT_MS3);
|
|
3026
|
-
const response = await fetch(`${this.config.host}/api/generate`, {
|
|
3027
|
-
method: "POST",
|
|
3028
|
-
headers: {
|
|
3029
|
-
"Content-Type": "application/json"
|
|
3030
|
-
},
|
|
3031
|
-
body: JSON.stringify({
|
|
3032
|
-
model: this.config.model,
|
|
3033
|
-
prompt: userPrompt,
|
|
3034
|
-
system: systemPrompt,
|
|
3035
|
-
stream: false,
|
|
3036
|
-
format: "json",
|
|
3037
|
-
options: {
|
|
3038
|
-
temperature: 0.3
|
|
3039
|
-
}
|
|
3040
|
-
}),
|
|
3041
|
-
signal: controller.signal
|
|
3042
|
-
});
|
|
3043
|
-
clearTimeout(timeoutId);
|
|
3044
|
-
if (!response.ok) {
|
|
3045
|
-
const status = String(response.status);
|
|
3046
|
-
throw new Error(
|
|
3047
|
-
`Ollama API request failed: ${status} ${response.statusText}`
|
|
3048
|
-
);
|
|
3049
|
-
}
|
|
3050
|
-
const data = await response.json();
|
|
3051
|
-
if (typeof data.response !== "string") {
|
|
3052
|
-
throw new Error("Invalid response format from Ollama API");
|
|
3053
|
-
}
|
|
3054
|
-
if (this.schemaType === "individual") {
|
|
3055
|
-
return parseBatchIndividualResponse(data.response, date, prompts);
|
|
3056
|
-
}
|
|
3057
|
-
return parseResponse(data.response, date, void 0, prompts);
|
|
3058
|
-
} catch (error) {
|
|
3059
|
-
lastError = error instanceof Error ? error : new Error(String(error));
|
|
3060
|
-
if (lastError.message.includes("parse") || lastError.message.includes("schema")) {
|
|
3061
|
-
throw lastError;
|
|
3062
|
-
}
|
|
3063
|
-
if (attempt === MAX_RETRIES3) {
|
|
3064
|
-
break;
|
|
3065
|
-
}
|
|
3066
|
-
const delay = BASE_RETRY_DELAY_MS * Math.pow(2, attempt);
|
|
3067
|
-
logger.debug(
|
|
3068
|
-
`Retry attempt ${String(attempt + 1)}/${String(MAX_RETRIES3)}, waiting ${String(delay)}ms`,
|
|
3069
|
-
"ollama"
|
|
3070
|
-
);
|
|
3071
|
-
await sleep2(delay);
|
|
3072
|
-
}
|
|
3073
|
-
}
|
|
3074
|
-
const attempts = String(MAX_RETRIES3 + 1);
|
|
3075
|
-
throw new Error(
|
|
3076
|
-
`Ollama analysis failed after ${attempts} attempts: ${lastError?.message ?? "Unknown error"}`
|
|
3077
|
-
);
|
|
3078
|
-
}
|
|
3079
|
-
};
|
|
3080
|
-
function sleep2(ms) {
|
|
3081
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
3082
|
-
}
|
|
3083
|
-
|
|
3084
|
-
// src/providers/index.ts
|
|
3085
|
-
function createProvider(type, config, analysisMode) {
|
|
3086
|
-
switch (type) {
|
|
3087
|
-
case "ollama":
|
|
3088
|
-
return new OllamaProvider({
|
|
3089
|
-
...config.ollama,
|
|
3090
|
-
schemaOverride: analysisMode
|
|
3091
|
-
});
|
|
3092
|
-
case "anthropic":
|
|
3093
|
-
return new AnthropicProvider(config.anthropic);
|
|
3094
|
-
case "google":
|
|
3095
|
-
return new GoogleProvider(config.google);
|
|
3096
|
-
}
|
|
3097
|
-
}
|
|
3098
|
-
async function getAvailableProvider(config, onFallback, analysisMode) {
|
|
3099
|
-
if (config.services.length === 0) {
|
|
3100
|
-
throw new Error(
|
|
3101
|
-
"No providers configured. Please run hyntx setup to configure providers."
|
|
3102
|
-
);
|
|
3103
|
-
}
|
|
3104
|
-
logger.debug(
|
|
3105
|
-
`Checking ${String(config.services.length)} configured provider(s): ${config.services.join(", ")}`,
|
|
3106
|
-
"provider"
|
|
3107
|
-
);
|
|
3108
|
-
let firstProvider;
|
|
3109
|
-
for (const type of config.services) {
|
|
3110
|
-
const provider = createProvider(type, config, analysisMode);
|
|
3111
|
-
firstProvider ??= provider;
|
|
3112
|
-
logger.debug(`Checking availability of ${provider.name}...`, "provider");
|
|
3113
|
-
try {
|
|
3114
|
-
const isAvailable = await provider.isAvailable();
|
|
3115
|
-
if (isAvailable) {
|
|
3116
|
-
logger.debug(`${provider.name} is available`, "provider");
|
|
3117
|
-
if (onFallback && provider !== firstProvider) {
|
|
3118
|
-
onFallback(firstProvider.name, provider.name);
|
|
3119
|
-
}
|
|
3120
|
-
return provider;
|
|
3121
|
-
} else {
|
|
3122
|
-
logger.debug(`${provider.name} is not available`, "provider");
|
|
3123
|
-
}
|
|
3124
|
-
} catch {
|
|
3125
|
-
logger.debug(`${provider.name} availability check failed`, "provider");
|
|
3126
|
-
continue;
|
|
3127
|
-
}
|
|
3128
|
-
}
|
|
3129
|
-
throw new Error(
|
|
3130
|
-
"No providers are currently available. Please check your provider configurations."
|
|
3131
|
-
);
|
|
3132
|
-
}
|
|
3133
|
-
function getAllProviders(config, analysisMode) {
|
|
3134
|
-
if (config.services.length === 0) {
|
|
3135
|
-
throw new Error(
|
|
3136
|
-
"No providers configured. Please run hyntx setup to configure providers."
|
|
3137
|
-
);
|
|
3138
|
-
}
|
|
3139
|
-
return config.services.map(
|
|
3140
|
-
(type) => createProvider(type, config, analysisMode)
|
|
3141
|
-
);
|
|
3142
|
-
}
|
|
3143
|
-
var START_MARKER = "# >>> hyntx config >>>";
|
|
3144
|
-
var END_MARKER = "# <<< hyntx config <<<";
|
|
3145
|
-
function findMarkerPositions(content) {
|
|
3146
|
-
const startIndex = content.indexOf(START_MARKER);
|
|
3147
|
-
const endIndex = content.indexOf(END_MARKER);
|
|
3148
|
-
if (startIndex === -1 && endIndex === -1) {
|
|
3149
|
-
return { startIndex: -1, endIndex: -1, isValid: true };
|
|
3150
|
-
}
|
|
3151
|
-
if (startIndex !== -1 && endIndex === -1) {
|
|
3152
|
-
return { startIndex, endIndex: -1, isValid: false, issue: "missing_end" };
|
|
3153
|
-
}
|
|
3154
|
-
if (startIndex === -1 && endIndex !== -1) {
|
|
3155
|
-
return { startIndex: -1, endIndex, isValid: false, issue: "missing_start" };
|
|
3156
|
-
}
|
|
3157
|
-
if (startIndex > endIndex) {
|
|
3158
|
-
return { startIndex, endIndex, isValid: false, issue: "wrong_order" };
|
|
3159
|
-
}
|
|
3160
|
-
return { startIndex, endIndex, isValid: true };
|
|
3161
|
-
}
|
|
3162
|
-
function detectShellConfigFile() {
|
|
3163
|
-
const home = homedir();
|
|
3164
|
-
const shell = process.env["SHELL"] ?? "";
|
|
3165
|
-
if (shell.includes("zsh")) {
|
|
3166
|
-
return {
|
|
3167
|
-
shellType: "zsh",
|
|
3168
|
-
configFile: join(home, ".zshrc")
|
|
3169
|
-
};
|
|
3170
|
-
}
|
|
3171
|
-
if (shell.includes("bash")) {
|
|
3172
|
-
return {
|
|
3173
|
-
shellType: "bash",
|
|
3174
|
-
configFile: join(home, ".bashrc")
|
|
3175
|
-
};
|
|
3176
|
-
}
|
|
3177
|
-
if (shell.includes("fish")) {
|
|
3178
|
-
return {
|
|
3179
|
-
shellType: "fish",
|
|
3180
|
-
configFile: join(home, ".config", "fish", "config.fish")
|
|
3181
|
-
};
|
|
3182
|
-
}
|
|
3183
|
-
const defaultShell = process.platform === "darwin" ? "zsh" : "bash";
|
|
3184
|
-
return {
|
|
3185
|
-
shellType: defaultShell,
|
|
3186
|
-
configFile: join(home, `.${defaultShell}rc`)
|
|
3187
|
-
};
|
|
3188
|
-
}
|
|
3189
|
-
|
|
3190
|
-
// src/utils/env.ts
|
|
3191
|
-
var VALID_PROVIDERS = [
|
|
3192
|
-
"ollama",
|
|
3193
|
-
"anthropic",
|
|
3194
|
-
"google"
|
|
3195
|
-
];
|
|
3196
|
-
function isFirstRun() {
|
|
3197
|
-
if (process.env["HYNTX_SERVICES"]) {
|
|
3198
|
-
return false;
|
|
3199
|
-
}
|
|
3200
|
-
try {
|
|
3201
|
-
const { configFile } = detectShellConfigFile();
|
|
3202
|
-
if (existsSync(configFile)) {
|
|
3203
|
-
const content = readFileSync(configFile, "utf-8");
|
|
3204
|
-
const positions = findMarkerPositions(content);
|
|
3205
|
-
if (positions.startIndex !== -1 && positions.endIndex !== -1 && positions.isValid) {
|
|
3206
|
-
return false;
|
|
3207
|
-
}
|
|
3208
|
-
}
|
|
3209
|
-
} catch {
|
|
3210
|
-
}
|
|
3211
|
-
return true;
|
|
3212
|
-
}
|
|
3213
|
-
function parseServices(services) {
|
|
3214
|
-
if (!services || services.trim() === "") {
|
|
3215
|
-
return [];
|
|
3216
|
-
}
|
|
3217
|
-
return services.split(",").map((s) => s.trim().toLowerCase()).filter(
|
|
3218
|
-
(s) => VALID_PROVIDERS.includes(s)
|
|
3219
|
-
);
|
|
3220
|
-
}
|
|
3221
|
-
function getEnvConfig() {
|
|
3222
|
-
return {
|
|
3223
|
-
services: parseServices(process.env["HYNTX_SERVICES"]),
|
|
3224
|
-
reminder: process.env["HYNTX_REMINDER"] ?? ENV_DEFAULTS.reminder,
|
|
3225
|
-
ollama: {
|
|
3226
|
-
model: process.env["HYNTX_OLLAMA_MODEL"] ?? ENV_DEFAULTS.ollama.model,
|
|
3227
|
-
host: process.env["HYNTX_OLLAMA_HOST"] ?? ENV_DEFAULTS.ollama.host
|
|
3228
|
-
},
|
|
3229
|
-
anthropic: {
|
|
3230
|
-
model: process.env["HYNTX_ANTHROPIC_MODEL"] ?? ENV_DEFAULTS.anthropic.model,
|
|
3231
|
-
apiKey: process.env["HYNTX_ANTHROPIC_KEY"] ?? ""
|
|
3232
|
-
},
|
|
3233
|
-
google: {
|
|
3234
|
-
model: process.env["HYNTX_GOOGLE_MODEL"] ?? ENV_DEFAULTS.google.model,
|
|
3235
|
-
apiKey: process.env["HYNTX_GOOGLE_KEY"] ?? ""
|
|
3236
|
-
}
|
|
3237
|
-
};
|
|
3238
|
-
}
|
|
3239
|
-
var CONFIG_FILENAME = ".hyntxrc.json";
|
|
3240
|
-
var MAX_TRAVERSAL_DEPTH = 50;
|
|
3241
|
-
var PROJECT_CONTEXT_SCHEMA = z.object({
|
|
3242
|
-
role: z.string().optional(),
|
|
3243
|
-
techStack: z.array(z.string()).optional(),
|
|
3244
|
-
domain: z.string().optional(),
|
|
3245
|
-
guidelines: z.array(z.string()).optional(),
|
|
3246
|
-
projectType: z.string().optional()
|
|
3247
|
-
});
|
|
3248
|
-
var RULE_CONFIG_SCHEMA = z.object({
|
|
3249
|
-
enabled: z.boolean().optional(),
|
|
3250
|
-
severity: z.enum(["low", "medium", "high"]).optional()
|
|
3251
|
-
});
|
|
3252
|
-
var RULES_CONFIG_SCHEMA = z.record(z.string(), RULE_CONFIG_SCHEMA);
|
|
3253
|
-
var PROJECT_CONFIG_SCHEMA = z.object({
|
|
3254
|
-
context: PROJECT_CONTEXT_SCHEMA.optional(),
|
|
3255
|
-
rules: RULES_CONFIG_SCHEMA.optional()
|
|
3256
|
-
});
|
|
3257
|
-
var configCache = /* @__PURE__ */ new Map();
|
|
3258
|
-
function findProjectConfig(cwd) {
|
|
3259
|
-
let currentDir = cwd;
|
|
3260
|
-
const homeDirectory = homedir();
|
|
3261
|
-
let depth = 0;
|
|
3262
|
-
while (depth < MAX_TRAVERSAL_DEPTH) {
|
|
3263
|
-
const configPath = join(currentDir, CONFIG_FILENAME);
|
|
3264
|
-
try {
|
|
3265
|
-
readFileSync(configPath, "utf-8");
|
|
3266
|
-
return configPath;
|
|
3267
|
-
} catch {
|
|
3268
|
-
}
|
|
3269
|
-
if (currentDir === homeDirectory || currentDir === "/") {
|
|
3270
|
-
break;
|
|
3271
|
-
}
|
|
3272
|
-
const parentDir = dirname(currentDir);
|
|
3273
|
-
if (parentDir === currentDir) {
|
|
3274
|
-
break;
|
|
3275
|
-
}
|
|
3276
|
-
currentDir = parentDir;
|
|
3277
|
-
depth++;
|
|
3278
|
-
}
|
|
3279
|
-
return null;
|
|
3280
|
-
}
|
|
3281
|
-
function loadProjectConfig(configPath) {
|
|
3282
|
-
try {
|
|
3283
|
-
const fileContent = readFileSync(configPath, "utf-8");
|
|
3284
|
-
let parsed;
|
|
3285
|
-
try {
|
|
3286
|
-
parsed = JSON.parse(fileContent);
|
|
3287
|
-
} catch (error) {
|
|
3288
|
-
logger.collectWarning(
|
|
3289
|
-
`Invalid JSON in ${CONFIG_FILENAME}: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
3290
|
-
"config"
|
|
3291
|
-
);
|
|
3292
|
-
return null;
|
|
3293
|
-
}
|
|
3294
|
-
const result = PROJECT_CONFIG_SCHEMA.safeParse(parsed);
|
|
3295
|
-
if (!result.success) {
|
|
3296
|
-
const errorMessages = result.error.issues.map((issue) => {
|
|
3297
|
-
const pathStr = issue.path.map(String).join(".");
|
|
3298
|
-
return `${pathStr}: ${issue.message}`;
|
|
3299
|
-
}).join(", ");
|
|
3300
|
-
logger.collectWarning(
|
|
3301
|
-
`Invalid ${CONFIG_FILENAME} schema: ${errorMessages}`,
|
|
3302
|
-
"config"
|
|
3303
|
-
);
|
|
3304
|
-
return null;
|
|
3305
|
-
}
|
|
3306
|
-
return result.data;
|
|
3307
|
-
} catch (error) {
|
|
3308
|
-
logger.collectWarning(
|
|
3309
|
-
`Failed to read ${CONFIG_FILENAME}: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
3310
|
-
"config"
|
|
3311
|
-
);
|
|
3312
|
-
return null;
|
|
3313
|
-
}
|
|
3314
|
-
}
|
|
3315
|
-
function mergeConfigs(envConfig, projectConfig) {
|
|
3316
|
-
if (!projectConfig) {
|
|
3317
|
-
return envConfig;
|
|
3318
|
-
}
|
|
3319
|
-
const existingEnvConfig = envConfig;
|
|
3320
|
-
const merged = {
|
|
3321
|
-
...envConfig
|
|
3322
|
-
};
|
|
3323
|
-
if (projectConfig.context && !existingEnvConfig.context) {
|
|
3324
|
-
merged.context = projectConfig.context;
|
|
3325
|
-
} else if (existingEnvConfig.context) {
|
|
3326
|
-
merged.context = existingEnvConfig.context;
|
|
3327
|
-
}
|
|
3328
|
-
if (projectConfig.rules) {
|
|
3329
|
-
merged.rules = projectConfig.rules;
|
|
3330
|
-
}
|
|
3331
|
-
return merged;
|
|
3332
|
-
}
|
|
3333
|
-
function loadProjectConfigForCwd(cwd) {
|
|
3334
|
-
if (configCache.has(cwd)) {
|
|
3335
|
-
return configCache.get(cwd) ?? null;
|
|
3336
|
-
}
|
|
3337
|
-
const configPath = findProjectConfig(cwd);
|
|
3338
|
-
if (!configPath) {
|
|
3339
|
-
configCache.set(cwd, null);
|
|
3340
|
-
return null;
|
|
3341
|
-
}
|
|
3342
|
-
const config = loadProjectConfig(configPath);
|
|
3343
|
-
configCache.set(cwd, config);
|
|
3344
|
-
return config;
|
|
3345
|
-
}
|
|
3346
|
-
|
|
3347
|
-
export { BATCH_STRATEGIES, CACHE_ANALYSIS_DIR, CACHE_DEFAULTS, CACHE_META_FILE, CLAUDE_PROJECTS_DIR, ENV_DEFAULTS, EXIT_CODES, HYNTX_CACHE_DIR, HYNTX_HISTORY_DIR, LAST_RUN_FILE, PROVIDER_LIMITS, analyzePrompts, claudeProjectsExist, cleanupExpiredEntries, clearCache, compareResults, createProvider, ensureHistoryDir, extractProjectName, generateCacheKey, getAllProviders, getAvailableProvider, getCachedResult, getDateOneMonthAgo, getDateOneWeekAgo, getEnvConfig, getHistoryDir, groupByDay, hashSystemPrompt, isFirstRun, listAvailableDates, loadAnalysisResult, loadProjectConfig, loadProjectConfigForCwd, mergeConfigs, parseDate, parseServices, readLogs, sanitize, sanitizePrompts, saveAnalysisResult, setCachedResult, validateSystemPrompt };
|