hyntx 1.4.0 → 2.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 +499 -6
- package/dist/api/index.d.ts +665 -0
- package/dist/api/index.js +2797 -0
- package/dist/cli.d.ts +225 -0
- package/dist/cli.js +5716 -0
- package/dist/index-B6r9mP1J.d.ts +394 -0
- package/dist/index.d.ts +3 -237
- package/dist/index.js +2313 -3594
- package/package.json +23 -4
|
@@ -0,0 +1,2797 @@
|
|
|
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 { glob } from 'glob';
|
|
7
|
+
import { startOfDay, parseISO, isAfter, subDays, format, subMonths, isSameDay, isBefore } from 'date-fns';
|
|
8
|
+
import { z } from 'zod';
|
|
9
|
+
|
|
10
|
+
// src/utils/logger-base.ts
|
|
11
|
+
var BaseLogger = class {
|
|
12
|
+
warnings = [];
|
|
13
|
+
verboseEnabled = false;
|
|
14
|
+
/**
|
|
15
|
+
* Enables or disables verbose (debug) mode.
|
|
16
|
+
*
|
|
17
|
+
* @param enabled - Whether to enable verbose mode
|
|
18
|
+
*/
|
|
19
|
+
setVerbose(enabled) {
|
|
20
|
+
this.verboseEnabled = enabled;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns whether verbose mode is enabled.
|
|
24
|
+
*
|
|
25
|
+
* @returns true if verbose mode is enabled
|
|
26
|
+
*/
|
|
27
|
+
isVerbose() {
|
|
28
|
+
return this.verboseEnabled;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Logs an error message to stderr.
|
|
32
|
+
*
|
|
33
|
+
* @param message - Error message
|
|
34
|
+
* @param context - Optional context (e.g., file name, operation)
|
|
35
|
+
*/
|
|
36
|
+
error(message, context) {
|
|
37
|
+
const prefix = context ? `[${context}] ` : "";
|
|
38
|
+
process.stderr.write(`ERROR: ${prefix}${message}
|
|
39
|
+
`);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Logs a warning message to stderr.
|
|
43
|
+
*
|
|
44
|
+
* @param message - Warning message
|
|
45
|
+
* @param context - Optional context (e.g., file name, operation)
|
|
46
|
+
*/
|
|
47
|
+
warn(message, context) {
|
|
48
|
+
const prefix = context ? `[${context}] ` : "";
|
|
49
|
+
process.stderr.write(`WARN: ${prefix}${message}
|
|
50
|
+
`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Logs an info message to stderr.
|
|
54
|
+
*
|
|
55
|
+
* @param message - Info message
|
|
56
|
+
*/
|
|
57
|
+
info(message) {
|
|
58
|
+
process.stderr.write(`INFO: ${message}
|
|
59
|
+
`);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Logs a debug message to stderr (only when verbose mode is enabled).
|
|
63
|
+
*
|
|
64
|
+
* @param message - Debug message
|
|
65
|
+
* @param context - Optional context
|
|
66
|
+
*/
|
|
67
|
+
debug(message, context) {
|
|
68
|
+
if (this.verboseEnabled) {
|
|
69
|
+
const prefix = context ? `[${context}] ` : "";
|
|
70
|
+
process.stderr.write(`[DEBUG] ${prefix}${message}
|
|
71
|
+
`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Collects a warning for later reporting.
|
|
76
|
+
* Use this for non-fatal issues that should be displayed at the end of execution.
|
|
77
|
+
*
|
|
78
|
+
* @param message - Warning message
|
|
79
|
+
* @param context - Optional context (e.g., file name, operation)
|
|
80
|
+
*/
|
|
81
|
+
collectWarning(message, context) {
|
|
82
|
+
this.warnings.push({ message, context });
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns all collected warnings.
|
|
86
|
+
*
|
|
87
|
+
* @returns Array of collected warnings
|
|
88
|
+
*/
|
|
89
|
+
getWarnings() {
|
|
90
|
+
return [...this.warnings];
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Returns the number of collected warnings.
|
|
94
|
+
*
|
|
95
|
+
* @returns Warning count
|
|
96
|
+
*/
|
|
97
|
+
getWarningCount() {
|
|
98
|
+
return this.warnings.length;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Clears all collected warnings.
|
|
102
|
+
*/
|
|
103
|
+
clearWarnings() {
|
|
104
|
+
this.warnings = [];
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Reports all collected warnings to stderr and clears them.
|
|
108
|
+
* Does nothing if there are no warnings.
|
|
109
|
+
*/
|
|
110
|
+
reportWarnings() {
|
|
111
|
+
if (this.warnings.length === 0) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
process.stderr.write(`
|
|
115
|
+
\u26A0\uFE0F ${String(this.warnings.length)} warning(s):
|
|
116
|
+
`);
|
|
117
|
+
for (const warning of this.warnings) {
|
|
118
|
+
const prefix = warning.context ? `[${warning.context}] ` : "";
|
|
119
|
+
process.stderr.write(` - ${prefix}${warning.message}
|
|
120
|
+
`);
|
|
121
|
+
}
|
|
122
|
+
this.clearWarnings();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Formats collected warnings as plain text (without ANSI colors).
|
|
126
|
+
* Useful for JSON output or testing.
|
|
127
|
+
*
|
|
128
|
+
* @returns Array of formatted warning strings
|
|
129
|
+
*/
|
|
130
|
+
formatWarnings() {
|
|
131
|
+
return this.warnings.map((w) => {
|
|
132
|
+
const prefix = w.context ? `[${w.context}] ` : "";
|
|
133
|
+
return `${prefix}${w.message}`;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
var logger = new BaseLogger();
|
|
138
|
+
|
|
139
|
+
// src/core/sanitizer.ts
|
|
140
|
+
function redactOpenAIKeys(text) {
|
|
141
|
+
return text.replace(/sk-[a-zA-Z0-9]{48,}/g, "[REDACTED_OPENAI_KEY]");
|
|
142
|
+
}
|
|
143
|
+
function redactAnthropicKeys(text) {
|
|
144
|
+
return text.replace(/sk-ant-[a-zA-Z0-9-]+/g, "[REDACTED_ANTHROPIC_KEY]");
|
|
145
|
+
}
|
|
146
|
+
function redactAWSCredentials(text) {
|
|
147
|
+
return text.replace(/AKIA[0-9A-Z]{16}/g, "[REDACTED_AWS_KEY]");
|
|
148
|
+
}
|
|
149
|
+
function redactBearerTokens(text) {
|
|
150
|
+
return text.replace(
|
|
151
|
+
/Bearer(?:\s+token)?:?\s+[a-zA-Z0-9._-]{20,}/gi,
|
|
152
|
+
"Bearer [REDACTED_TOKEN]"
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
function redactURLCredentials(text) {
|
|
156
|
+
return text.replace(
|
|
157
|
+
/(https?:\/\/)([^:\s@]+)(?::([^\s@]+))?@/g,
|
|
158
|
+
(match, protocol, _user, _pass) => {
|
|
159
|
+
return `${protocol}[REDACTED_URL_CREDENTIAL]@`;
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
function redactEmails(text) {
|
|
164
|
+
return text.replace(
|
|
165
|
+
/[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g,
|
|
166
|
+
"[REDACTED_EMAIL]"
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
function redactPEMKeys(text) {
|
|
170
|
+
return text.replace(
|
|
171
|
+
/-----BEGIN\s+[A-Z\s]+PRIVATE\s+KEY-----[\s\S]*?-----END\s+[A-Z\s]+PRIVATE\s+KEY-----/g,
|
|
172
|
+
"[REDACTED_PEM_KEY]"
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
function redactNames(text) {
|
|
176
|
+
return text.replace(
|
|
177
|
+
/\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,
|
|
178
|
+
(match, name) => {
|
|
179
|
+
const commonWords = /\b(?:there|everyone|all|team|guys|folks|people)\b/i;
|
|
180
|
+
if (name && name.length >= 2 && !commonWords.test(name)) {
|
|
181
|
+
return match.replace(name, "[REDACTED_NAME]");
|
|
182
|
+
}
|
|
183
|
+
return match;
|
|
184
|
+
}
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
function redactPhoneNumbers(text) {
|
|
188
|
+
return text.replace(
|
|
189
|
+
/(?:\+?1[-.\s]?)?\(?([0-9]{3})\)?[-.\s]?([0-9]{3})[-.\s]?([0-9]{4})\b/g,
|
|
190
|
+
"[REDACTED_PHONE]"
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
function redactCreditCards(text) {
|
|
194
|
+
return text.replace(
|
|
195
|
+
/\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,
|
|
196
|
+
"[REDACTED_CREDIT_CARD]"
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
function redactSSN(text) {
|
|
200
|
+
return text.replace(
|
|
201
|
+
/\b(?!000|666|9\d{2})([0-9]{3})[-.\s]?(?!00)([0-9]{2})[-.\s]?(?!0000)([0-9]{4})\b/g,
|
|
202
|
+
"[REDACTED_SSN]"
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
function redactSpanishDNI(text) {
|
|
206
|
+
return text.replace(/\b\d{8}[A-Z]\b/gi, "[REDACTED_SPANISH_DNI]");
|
|
207
|
+
}
|
|
208
|
+
function redactSpanishNIE(text) {
|
|
209
|
+
return text.replace(/\b[XYZ]\d{7}[A-Z]\b/gi, "[REDACTED_SPANISH_NIE]");
|
|
210
|
+
}
|
|
211
|
+
function redactMexicanCURP(text) {
|
|
212
|
+
return text.replace(
|
|
213
|
+
/\b[A-Z]{4}\d{6}[HM][A-Z]{5}[0-9A-Z]{2}\b/gi,
|
|
214
|
+
"[REDACTED_MEXICAN_CURP]"
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
function redactBrazilianCPF(text) {
|
|
218
|
+
return text.replace(
|
|
219
|
+
/\b\d{3}\.?\d{3}\.?\d{3}-?\d{2}\b/g,
|
|
220
|
+
"[REDACTED_BRAZILIAN_CPF]"
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
function redactUKNINO(text) {
|
|
224
|
+
return text.replace(
|
|
225
|
+
/\b[A-Z]{2}\s?\d{2}\s?\d{2}\s?\d{2}\s?[A-Z]\b/gi,
|
|
226
|
+
"[REDACTED_UK_NINO]"
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
function redactChileanRUN(text) {
|
|
230
|
+
return text.replace(
|
|
231
|
+
/\b(?:RUN|RUT)[\s:]*([0-9]{7,8}-[0-9K]|[0-9]{8}[0-9K])\b/gi,
|
|
232
|
+
(match, run) => {
|
|
233
|
+
return match.replace(run, "[REDACTED_CHILEAN_RUN]");
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
function redactArgentineDNI(text) {
|
|
238
|
+
return text.replace(
|
|
239
|
+
/\b(?:DNI|dni)[\s:]*(\d{8})\b/gi,
|
|
240
|
+
(match, dni) => {
|
|
241
|
+
return match.replace(dni, "[REDACTED_ARGENTINE_DNI]");
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
function redactIPv4(text) {
|
|
246
|
+
return text.replace(
|
|
247
|
+
/\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,
|
|
248
|
+
"[REDACTED_IPV4]"
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
function redactIPv6(text) {
|
|
252
|
+
return text.replace(
|
|
253
|
+
/\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,
|
|
254
|
+
"[REDACTED_IPV6]"
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
function redactMACAddresses(text) {
|
|
258
|
+
return text.replace(
|
|
259
|
+
/\b(?:[0-9A-Fa-f]{2}[:-]){5}(?:[0-9A-Fa-f]{2})\b/g,
|
|
260
|
+
"[REDACTED_MAC_ADDRESS]"
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
function redactPassportNumbers(text) {
|
|
264
|
+
return text.replace(
|
|
265
|
+
/\b(?:passport|pasaporte)[\s:]*([A-Z0-9]{6,9})\b/gi,
|
|
266
|
+
(match, passport) => {
|
|
267
|
+
return match.replace(passport, "[REDACTED_PASSPORT]");
|
|
268
|
+
}
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
function redactDriverLicenseNumbers(text) {
|
|
272
|
+
return text.replace(
|
|
273
|
+
/\b(?:driver\s*license|licencia\s*de\s*conducir|DL|D\.L\.)[\s:]*([A-Z0-9]{8,16})\b/gi,
|
|
274
|
+
(match, license) => {
|
|
275
|
+
return match.replace(license, "[REDACTED_DRIVER_LICENSE]");
|
|
276
|
+
}
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
function redactAddresses(text) {
|
|
280
|
+
let sanitized = text;
|
|
281
|
+
sanitized = sanitized.replace(/\b\d{5}(?:-\d{4})?\b/g, "[REDACTED_ZIP_CODE]");
|
|
282
|
+
sanitized = sanitized.replace(
|
|
283
|
+
/\b[A-Z]{1,2}\d{1,2}[A-Z]?\s?\d[A-Z]{2}\b/gi,
|
|
284
|
+
"[REDACTED_POSTCODE]"
|
|
285
|
+
);
|
|
286
|
+
sanitized = sanitized.replace(
|
|
287
|
+
/\b[A-Z]\d[A-Z]\s?\d[A-Z]\d\b/gi,
|
|
288
|
+
"[REDACTED_POSTAL_CODE]"
|
|
289
|
+
);
|
|
290
|
+
sanitized = sanitized.replace(
|
|
291
|
+
/\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,
|
|
292
|
+
(match) => {
|
|
293
|
+
return match.replace(
|
|
294
|
+
/\d+\s+[A-Za-z\s]+(?:street|st|avenue|ave|road|rd|boulevard|blvd|drive|dr|lane|ln|way|circle|cir)/gi,
|
|
295
|
+
"[REDACTED_ADDRESS]"
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
);
|
|
299
|
+
return sanitized;
|
|
300
|
+
}
|
|
301
|
+
function redactBirthDates(text) {
|
|
302
|
+
return text.replace(
|
|
303
|
+
/\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,
|
|
304
|
+
(match, date) => {
|
|
305
|
+
return match.replace(date, "[REDACTED_BIRTH_DATE]");
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
function countRedactions(original, sanitized) {
|
|
310
|
+
const redactionPattern = /\[REDACTED_[^\]]+\]/g;
|
|
311
|
+
const matches = sanitized.match(redactionPattern);
|
|
312
|
+
return matches ? matches.length : 0;
|
|
313
|
+
}
|
|
314
|
+
function sanitize(text) {
|
|
315
|
+
if (!text || text.trim().length === 0) {
|
|
316
|
+
return { text, redacted: 0 };
|
|
317
|
+
}
|
|
318
|
+
let sanitized = text;
|
|
319
|
+
sanitized = redactOpenAIKeys(sanitized);
|
|
320
|
+
sanitized = redactAnthropicKeys(sanitized);
|
|
321
|
+
sanitized = redactAWSCredentials(sanitized);
|
|
322
|
+
sanitized = redactBearerTokens(sanitized);
|
|
323
|
+
sanitized = redactURLCredentials(sanitized);
|
|
324
|
+
sanitized = redactPEMKeys(sanitized);
|
|
325
|
+
sanitized = redactEmails(sanitized);
|
|
326
|
+
sanitized = redactCreditCards(sanitized);
|
|
327
|
+
sanitized = redactPassportNumbers(sanitized);
|
|
328
|
+
sanitized = redactDriverLicenseNumbers(sanitized);
|
|
329
|
+
sanitized = redactSpanishDNI(sanitized);
|
|
330
|
+
sanitized = redactSpanishNIE(sanitized);
|
|
331
|
+
sanitized = redactMexicanCURP(sanitized);
|
|
332
|
+
sanitized = redactBrazilianCPF(sanitized);
|
|
333
|
+
sanitized = redactUKNINO(sanitized);
|
|
334
|
+
sanitized = redactChileanRUN(sanitized);
|
|
335
|
+
sanitized = redactArgentineDNI(sanitized);
|
|
336
|
+
sanitized = redactAddresses(sanitized);
|
|
337
|
+
sanitized = redactBirthDates(sanitized);
|
|
338
|
+
sanitized = redactNames(sanitized);
|
|
339
|
+
sanitized = redactSSN(sanitized);
|
|
340
|
+
sanitized = redactPhoneNumbers(sanitized);
|
|
341
|
+
sanitized = redactIPv4(sanitized);
|
|
342
|
+
sanitized = redactIPv6(sanitized);
|
|
343
|
+
sanitized = redactMACAddresses(sanitized);
|
|
344
|
+
const redacted = countRedactions(text, sanitized);
|
|
345
|
+
return {
|
|
346
|
+
text: sanitized,
|
|
347
|
+
redacted
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function sanitizePrompts(prompts) {
|
|
351
|
+
if (prompts.length === 0) {
|
|
352
|
+
return { prompts: [], totalRedacted: 0 };
|
|
353
|
+
}
|
|
354
|
+
logger.debug(
|
|
355
|
+
`Sanitizing ${String(prompts.length)} prompts for secrets`,
|
|
356
|
+
"sanitizer"
|
|
357
|
+
);
|
|
358
|
+
const sanitizedPrompts = [];
|
|
359
|
+
let totalRedacted = 0;
|
|
360
|
+
for (const prompt of prompts) {
|
|
361
|
+
const result = sanitize(prompt);
|
|
362
|
+
sanitizedPrompts.push(result.text);
|
|
363
|
+
totalRedacted += result.redacted;
|
|
364
|
+
}
|
|
365
|
+
if (totalRedacted > 0) {
|
|
366
|
+
logger.debug(
|
|
367
|
+
`Redacted ${String(totalRedacted)} secret(s) from prompts`,
|
|
368
|
+
"sanitizer"
|
|
369
|
+
);
|
|
370
|
+
} else {
|
|
371
|
+
logger.debug("No secrets found to redact", "sanitizer");
|
|
372
|
+
}
|
|
373
|
+
return {
|
|
374
|
+
prompts: sanitizedPrompts,
|
|
375
|
+
totalRedacted
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
var HOME = homedir();
|
|
379
|
+
var CLAUDE_PROJECTS_DIR = process.env["HYNTX_CLAUDE_PROJECTS_DIR"] ?? join(HOME, ".claude", "projects");
|
|
380
|
+
var LAST_RUN_FILE = join(HOME, ".hyntx-last-run");
|
|
381
|
+
var HYNTX_HISTORY_DIR = process.env["HYNTX_HISTORY_DIR"] ?? join(HOME, ".hyntx", "history");
|
|
382
|
+
var HYNTX_CACHE_DIR = process.env["HYNTX_CACHE_DIR"] ?? join(HOME, ".hyntx-cache");
|
|
383
|
+
var CACHE_ANALYSIS_DIR = join(HYNTX_CACHE_DIR, "analysis");
|
|
384
|
+
var CACHE_META_FILE = join(CACHE_ANALYSIS_DIR, ".metadata.json");
|
|
385
|
+
|
|
386
|
+
// src/providers/schemas.ts
|
|
387
|
+
var ISSUE_TAXONOMY = {
|
|
388
|
+
vague: {
|
|
389
|
+
name: "Vague Request",
|
|
390
|
+
severity: "high",
|
|
391
|
+
suggestion: "Be more specific about what you need",
|
|
392
|
+
exampleBefore: "Help me with my code",
|
|
393
|
+
exampleAfter: "Help me debug this TypeScript function that returns undefined"
|
|
394
|
+
},
|
|
395
|
+
"no-context": {
|
|
396
|
+
name: "Missing Context",
|
|
397
|
+
severity: "high",
|
|
398
|
+
suggestion: "Provide relevant background information",
|
|
399
|
+
exampleBefore: "Fix the bug",
|
|
400
|
+
exampleAfter: "Fix the bug in the authentication flow where users are logged out after 5 minutes"
|
|
401
|
+
},
|
|
402
|
+
"too-broad": {
|
|
403
|
+
name: "Too Broad",
|
|
404
|
+
severity: "medium",
|
|
405
|
+
suggestion: "Break down into smaller, focused requests",
|
|
406
|
+
exampleBefore: "Build me an app",
|
|
407
|
+
exampleAfter: "Create a React component for displaying user profiles with avatar and bio"
|
|
408
|
+
},
|
|
409
|
+
"no-goal": {
|
|
410
|
+
name: "No Clear Goal",
|
|
411
|
+
severity: "high",
|
|
412
|
+
suggestion: "State what outcome you want to achieve",
|
|
413
|
+
exampleBefore: "Look at this file",
|
|
414
|
+
exampleAfter: "Review this file for security vulnerabilities in the authentication logic"
|
|
415
|
+
},
|
|
416
|
+
imperative: {
|
|
417
|
+
name: "Command Without Context",
|
|
418
|
+
severity: "low",
|
|
419
|
+
suggestion: "Explain why you need this",
|
|
420
|
+
exampleBefore: "Add a button",
|
|
421
|
+
exampleAfter: 'Add a "Submit" button to trigger form validation and API submission'
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
var SYSTEM_PROMPT_MINIMAL = `You analyze prompts for quality issues.
|
|
425
|
+
Respond with JSON only: {"issues": ["issue-id", ...], "score": 0-100}
|
|
426
|
+
|
|
427
|
+
Valid issue IDs: vague, no-context, too-broad, no-goal, imperative
|
|
428
|
+
|
|
429
|
+
Examples:
|
|
430
|
+
Input: "Help me with code"
|
|
431
|
+
Output: {"issues": ["vague", "no-context"], "score": 40}
|
|
432
|
+
|
|
433
|
+
Input: "Debug this TypeScript function that returns undefined"
|
|
434
|
+
Output: {"issues": [], "score": 85}`;
|
|
435
|
+
var SYSTEM_PROMPT_SIMPLE = `Analyze prompts for quality issues. Return ONLY JSON, no other text.
|
|
436
|
+
|
|
437
|
+
Schema:
|
|
438
|
+
{"issues":[{"name":"issue name","example":"bad prompt","fix":"better prompt"}],"score":75,"tip":"main suggestion"}
|
|
439
|
+
|
|
440
|
+
Rules:
|
|
441
|
+
- issues: array of problems found (empty if none)
|
|
442
|
+
- score: 0-100 quality score (100=perfect)
|
|
443
|
+
- tip: single most important suggestion
|
|
444
|
+
- name: short issue name (e.g. "vague request", "missing context")
|
|
445
|
+
- example: actual prompt text showing the issue
|
|
446
|
+
- fix: improved version of that prompt`;
|
|
447
|
+
var SYSTEM_PROMPT_FULL = SYSTEM_PROMPT_SIMPLE;
|
|
448
|
+
var VALID_PATTERN_IDS = Object.keys(ISSUE_TAXONOMY);
|
|
449
|
+
function applyRulesConfig(rules, baseTaxonomy = ISSUE_TAXONOMY) {
|
|
450
|
+
if (!rules || Object.keys(rules).length === 0) {
|
|
451
|
+
return baseTaxonomy;
|
|
452
|
+
}
|
|
453
|
+
for (const ruleId of Object.keys(rules)) {
|
|
454
|
+
if (!VALID_PATTERN_IDS.includes(ruleId)) {
|
|
455
|
+
logger.warn(
|
|
456
|
+
`Unknown rule ID "${ruleId}" in configuration. Valid IDs are: ${VALID_PATTERN_IDS.join(", ")}`,
|
|
457
|
+
"config"
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const newTaxonomy = {};
|
|
462
|
+
for (const [patternId, metadata] of Object.entries(baseTaxonomy)) {
|
|
463
|
+
const ruleConfig = rules[patternId];
|
|
464
|
+
if (ruleConfig?.enabled === false) {
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (ruleConfig?.severity) {
|
|
468
|
+
newTaxonomy[patternId] = {
|
|
469
|
+
...metadata,
|
|
470
|
+
severity: ruleConfig.severity
|
|
471
|
+
};
|
|
472
|
+
} else {
|
|
473
|
+
newTaxonomy[patternId] = metadata;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (Object.keys(newTaxonomy).length === 0) {
|
|
477
|
+
logger.collectWarning(
|
|
478
|
+
"All analysis rules are disabled in configuration. No patterns will be detected.",
|
|
479
|
+
"config"
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
return newTaxonomy;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
// src/types/index.ts
|
|
486
|
+
var BATCH_STRATEGIES = {
|
|
487
|
+
// For models < 4GB
|
|
488
|
+
micro: {
|
|
489
|
+
maxTokensPerBatch: 500,
|
|
490
|
+
maxPromptsPerBatch: 3,
|
|
491
|
+
description: "For models < 4GB"
|
|
492
|
+
},
|
|
493
|
+
// For models 4-7GB
|
|
494
|
+
small: {
|
|
495
|
+
maxTokensPerBatch: 1500,
|
|
496
|
+
maxPromptsPerBatch: 10,
|
|
497
|
+
description: "For models 4-7GB"
|
|
498
|
+
},
|
|
499
|
+
// For models > 7GB
|
|
500
|
+
standard: {
|
|
501
|
+
maxTokensPerBatch: 3e3,
|
|
502
|
+
maxPromptsPerBatch: 50,
|
|
503
|
+
description: "For models > 7GB"
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
var PROVIDER_LIMITS = {
|
|
507
|
+
// Small local models struggle with large inputs - limit to ~10 short prompts
|
|
508
|
+
ollama: { maxTokensPerBatch: 3e3, prioritization: "longest-first" },
|
|
509
|
+
anthropic: { maxTokensPerBatch: 1e5, prioritization: "chronological" },
|
|
510
|
+
google: { maxTokensPerBatch: 5e5, prioritization: "chronological" }
|
|
511
|
+
};
|
|
512
|
+
var ENV_DEFAULTS = {
|
|
513
|
+
reminder: "7d",
|
|
514
|
+
ollama: {
|
|
515
|
+
model: "llama3.2",
|
|
516
|
+
host: "http://localhost:11434"
|
|
517
|
+
},
|
|
518
|
+
anthropic: {
|
|
519
|
+
model: "claude-3-5-haiku-latest"
|
|
520
|
+
},
|
|
521
|
+
google: {
|
|
522
|
+
model: "gemini-2.0-flash-exp"
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
var EXIT_CODES = {
|
|
526
|
+
SUCCESS: 0,
|
|
527
|
+
ERROR: 1,
|
|
528
|
+
NO_DATA: 2,
|
|
529
|
+
PROVIDER_UNAVAILABLE: 3
|
|
530
|
+
};
|
|
531
|
+
var CACHE_DEFAULTS = {
|
|
532
|
+
ttlMs: 7 * 24 * 60 * 60 * 1e3
|
|
533
|
+
// 7 days in milliseconds
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// src/cache/analysis-cache.ts
|
|
537
|
+
async function ensureCacheDir() {
|
|
538
|
+
try {
|
|
539
|
+
await mkdir(CACHE_ANALYSIS_DIR, { recursive: true, mode: 448 });
|
|
540
|
+
} catch (error) {
|
|
541
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
542
|
+
throw new Error(
|
|
543
|
+
`Failed to create cache directory ${CACHE_ANALYSIS_DIR}: ${errorMessage}`
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
function getCacheFilePath(cacheKey) {
|
|
548
|
+
return join(CACHE_ANALYSIS_DIR, `${cacheKey}.json`);
|
|
549
|
+
}
|
|
550
|
+
function hashString(input) {
|
|
551
|
+
return createHash("sha256").update(input, "utf-8").digest("hex");
|
|
552
|
+
}
|
|
553
|
+
function generateCacheKey(prompts, model, schemaType = "full") {
|
|
554
|
+
const joined = prompts.join("\n---\n");
|
|
555
|
+
const input = `${model}:${schemaType}:${joined}`;
|
|
556
|
+
return hashString(input);
|
|
557
|
+
}
|
|
558
|
+
function hashSystemPrompt() {
|
|
559
|
+
return hashString(SYSTEM_PROMPT_FULL);
|
|
560
|
+
}
|
|
561
|
+
async function loadCacheMetadata() {
|
|
562
|
+
if (!existsSync(CACHE_META_FILE)) {
|
|
563
|
+
return null;
|
|
564
|
+
}
|
|
565
|
+
try {
|
|
566
|
+
const content = await readFile(CACHE_META_FILE, "utf-8");
|
|
567
|
+
const metadata = JSON.parse(content);
|
|
568
|
+
return metadata;
|
|
569
|
+
} catch (error) {
|
|
570
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
571
|
+
logger.warn(`Failed to load cache metadata: ${errorMessage}`, "cache");
|
|
572
|
+
return null;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
async function saveCacheMetadata(metadata) {
|
|
576
|
+
try {
|
|
577
|
+
await ensureCacheDir();
|
|
578
|
+
const content = JSON.stringify(metadata, null, 2);
|
|
579
|
+
const tmpFile = `${CACHE_META_FILE}.tmp`;
|
|
580
|
+
await writeFile(tmpFile, content, "utf-8");
|
|
581
|
+
await rename(tmpFile, CACHE_META_FILE);
|
|
582
|
+
logger.debug("Saved cache metadata", "cache");
|
|
583
|
+
} catch (error) {
|
|
584
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
585
|
+
logger.warn(`Failed to save cache metadata: ${errorMessage}`, "cache");
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
async function validateSystemPrompt() {
|
|
589
|
+
const currentHash = hashSystemPrompt();
|
|
590
|
+
const metadata = await loadCacheMetadata();
|
|
591
|
+
if (!metadata) {
|
|
592
|
+
await saveCacheMetadata({
|
|
593
|
+
systemPromptHash: currentHash,
|
|
594
|
+
lastUpdated: Date.now()
|
|
595
|
+
});
|
|
596
|
+
return true;
|
|
597
|
+
}
|
|
598
|
+
if (metadata.systemPromptHash !== currentHash) {
|
|
599
|
+
logger.debug("System prompt changed - invalidating cache", "cache");
|
|
600
|
+
await clearCache();
|
|
601
|
+
await saveCacheMetadata({
|
|
602
|
+
systemPromptHash: currentHash,
|
|
603
|
+
lastUpdated: Date.now()
|
|
604
|
+
});
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
async function getCachedResult(prompts, model, options = CACHE_DEFAULTS) {
|
|
610
|
+
await validateSystemPrompt();
|
|
611
|
+
const cacheKey = generateCacheKey(prompts, model);
|
|
612
|
+
const filePath = getCacheFilePath(cacheKey);
|
|
613
|
+
if (!existsSync(filePath)) {
|
|
614
|
+
logger.debug(`Cache miss for key ${cacheKey}`, "cache");
|
|
615
|
+
return null;
|
|
616
|
+
}
|
|
617
|
+
try {
|
|
618
|
+
const content = await readFile(filePath, "utf-8");
|
|
619
|
+
const data = JSON.parse(content);
|
|
620
|
+
if (!data.metadata || !data.result) {
|
|
621
|
+
logger.debug(`Invalid cache entry for key ${cacheKey}`, "cache");
|
|
622
|
+
return null;
|
|
623
|
+
}
|
|
624
|
+
const cached = data;
|
|
625
|
+
const now = Date.now();
|
|
626
|
+
const age = now - cached.metadata.cachedAt;
|
|
627
|
+
if (age > options.ttlMs) {
|
|
628
|
+
logger.debug(
|
|
629
|
+
`Cache entry expired for key ${cacheKey} (age: ${String(age)}ms)`,
|
|
630
|
+
"cache"
|
|
631
|
+
);
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
if (cached.metadata.model !== model) {
|
|
635
|
+
logger.debug(
|
|
636
|
+
`Model mismatch for key ${cacheKey} (cached: ${cached.metadata.model}, requested: ${model})`,
|
|
637
|
+
"cache"
|
|
638
|
+
);
|
|
639
|
+
return null;
|
|
640
|
+
}
|
|
641
|
+
if (cached.metadata.promptCount !== prompts.length) {
|
|
642
|
+
logger.debug(`Prompt count mismatch for key ${cacheKey}`, "cache");
|
|
643
|
+
return null;
|
|
644
|
+
}
|
|
645
|
+
logger.debug(`Cache hit for key ${cacheKey}`, "cache");
|
|
646
|
+
return cached.result;
|
|
647
|
+
} catch (error) {
|
|
648
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
649
|
+
logger.warn(
|
|
650
|
+
`Failed to read cache entry ${cacheKey}: ${errorMessage}`,
|
|
651
|
+
"cache"
|
|
652
|
+
);
|
|
653
|
+
return null;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
async function setCachedResult(prompts, model, result) {
|
|
657
|
+
try {
|
|
658
|
+
await ensureCacheDir();
|
|
659
|
+
await validateSystemPrompt();
|
|
660
|
+
const cacheKey = generateCacheKey(prompts, model);
|
|
661
|
+
const filePath = getCacheFilePath(cacheKey);
|
|
662
|
+
const metadata = {
|
|
663
|
+
cachedAt: Date.now(),
|
|
664
|
+
promptCount: prompts.length,
|
|
665
|
+
model,
|
|
666
|
+
systemPromptHash: hashSystemPrompt()
|
|
667
|
+
};
|
|
668
|
+
const cached = {
|
|
669
|
+
result,
|
|
670
|
+
metadata
|
|
671
|
+
};
|
|
672
|
+
const content = JSON.stringify(cached, null, 2);
|
|
673
|
+
const tmpFile = `${filePath}.tmp`;
|
|
674
|
+
await writeFile(tmpFile, content, "utf-8");
|
|
675
|
+
await rename(tmpFile, filePath);
|
|
676
|
+
logger.debug(`Cached result for key ${cacheKey}`, "cache");
|
|
677
|
+
} catch (error) {
|
|
678
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
679
|
+
logger.warn(`Failed to cache result: ${errorMessage}`, "cache");
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
async function clearCache() {
|
|
683
|
+
if (!existsSync(CACHE_ANALYSIS_DIR)) {
|
|
684
|
+
logger.debug("Cache directory does not exist - nothing to clear", "cache");
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
try {
|
|
688
|
+
await rm(CACHE_ANALYSIS_DIR, { recursive: true, force: true });
|
|
689
|
+
logger.debug("Cache cleared", "cache");
|
|
690
|
+
} catch (error) {
|
|
691
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
692
|
+
logger.warn(`Failed to clear cache: ${errorMessage}`, "cache");
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
async function cleanupExpiredEntries(options = CACHE_DEFAULTS) {
|
|
696
|
+
if (!existsSync(CACHE_ANALYSIS_DIR)) {
|
|
697
|
+
return 0;
|
|
698
|
+
}
|
|
699
|
+
try {
|
|
700
|
+
const files = readdirSync(CACHE_ANALYSIS_DIR);
|
|
701
|
+
const now = Date.now();
|
|
702
|
+
let removedCount = 0;
|
|
703
|
+
for (const file of files) {
|
|
704
|
+
if (!file.endsWith(".json") || file === ".metadata.json") {
|
|
705
|
+
continue;
|
|
706
|
+
}
|
|
707
|
+
const filePath = join(CACHE_ANALYSIS_DIR, file);
|
|
708
|
+
try {
|
|
709
|
+
const content = await readFile(filePath, "utf-8");
|
|
710
|
+
const cached = JSON.parse(content);
|
|
711
|
+
const age = now - cached.metadata.cachedAt;
|
|
712
|
+
if (age > options.ttlMs) {
|
|
713
|
+
await rm(filePath, { force: true });
|
|
714
|
+
removedCount++;
|
|
715
|
+
logger.debug(`Removed expired cache entry: ${file}`, "cache");
|
|
716
|
+
}
|
|
717
|
+
} catch {
|
|
718
|
+
await rm(filePath, { force: true });
|
|
719
|
+
removedCount++;
|
|
720
|
+
logger.debug(`Removed invalid cache entry: ${file}`, "cache");
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
if (removedCount > 0) {
|
|
724
|
+
logger.debug(
|
|
725
|
+
`Cleanup removed ${String(removedCount)} expired entries`,
|
|
726
|
+
"cache"
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
return removedCount;
|
|
730
|
+
} catch (error) {
|
|
731
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
732
|
+
logger.warn(`Failed to cleanup expired entries: ${errorMessage}`, "cache");
|
|
733
|
+
return 0;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// src/core/analyzer.ts
|
|
738
|
+
var SYSTEM_OVERHEAD_TOKENS = 2e3;
|
|
739
|
+
var SEVERITY_RANK = {
|
|
740
|
+
high: 3,
|
|
741
|
+
medium: 2,
|
|
742
|
+
low: 1
|
|
743
|
+
};
|
|
744
|
+
var MAX_PATTERNS = 5;
|
|
745
|
+
var MAX_EXAMPLES_PER_PATTERN = 3;
|
|
746
|
+
var CHARS_PER_TOKEN = 4;
|
|
747
|
+
function hasDynamicLimits(provider) {
|
|
748
|
+
return typeof provider.getBatchLimits === "function";
|
|
749
|
+
}
|
|
750
|
+
function shouldFallback(error) {
|
|
751
|
+
const message = error.message.toLowerCase();
|
|
752
|
+
return message.includes("parse") || message.includes("schema") || message.includes("json") || message.includes("timeout") || message.includes("network") || message.includes("failed");
|
|
753
|
+
}
|
|
754
|
+
function average(numbers) {
|
|
755
|
+
if (numbers.length === 0) return 0;
|
|
756
|
+
return sum(numbers) / numbers.length;
|
|
757
|
+
}
|
|
758
|
+
function sum(numbers) {
|
|
759
|
+
return numbers.reduce((acc, n) => acc + n, 0);
|
|
760
|
+
}
|
|
761
|
+
function flattenAll(arrays) {
|
|
762
|
+
const result = [];
|
|
763
|
+
for (const arr of arrays) {
|
|
764
|
+
for (const item of arr) {
|
|
765
|
+
result.push(item);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return result;
|
|
769
|
+
}
|
|
770
|
+
function limitExamples(examples, max) {
|
|
771
|
+
return examples.slice(0, max);
|
|
772
|
+
}
|
|
773
|
+
function inferProviderType(providerName) {
|
|
774
|
+
const nameLower = providerName.toLowerCase();
|
|
775
|
+
if (nameLower.includes("ollama")) return "ollama";
|
|
776
|
+
if (nameLower.includes("anthropic") || nameLower.includes("claude"))
|
|
777
|
+
return "anthropic";
|
|
778
|
+
if (nameLower.includes("google") || nameLower.includes("gemini"))
|
|
779
|
+
return "google";
|
|
780
|
+
return "ollama";
|
|
781
|
+
}
|
|
782
|
+
function extractModelFromProvider(providerName) {
|
|
783
|
+
const match = /\((.*?)\)/.exec(providerName);
|
|
784
|
+
return match?.[1] ?? providerName;
|
|
785
|
+
}
|
|
786
|
+
function maxSeverity(severities) {
|
|
787
|
+
if (severities.length === 0) return "low";
|
|
788
|
+
let max = "low";
|
|
789
|
+
let maxRank = SEVERITY_RANK.low;
|
|
790
|
+
for (const severity of severities) {
|
|
791
|
+
const rank = SEVERITY_RANK[severity];
|
|
792
|
+
if (rank > maxRank) {
|
|
793
|
+
max = severity;
|
|
794
|
+
maxRank = rank;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return max;
|
|
798
|
+
}
|
|
799
|
+
async function analyzeWithFallback(provider, prompts, date, context, model, noCache) {
|
|
800
|
+
if (prompts.length === 0) {
|
|
801
|
+
return [];
|
|
802
|
+
}
|
|
803
|
+
try {
|
|
804
|
+
if (!noCache) {
|
|
805
|
+
const cachedResult = await getCachedResult(
|
|
806
|
+
prompts,
|
|
807
|
+
model,
|
|
808
|
+
CACHE_DEFAULTS
|
|
809
|
+
);
|
|
810
|
+
if (cachedResult) {
|
|
811
|
+
logger.debug(
|
|
812
|
+
`Using cached result for batch of ${String(prompts.length)} prompt(s)`,
|
|
813
|
+
"analyzer"
|
|
814
|
+
);
|
|
815
|
+
return [{ ...cachedResult, date }];
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
const result = await provider.analyze(prompts, date, context);
|
|
819
|
+
if (!noCache) {
|
|
820
|
+
await setCachedResult(prompts, model, result);
|
|
821
|
+
}
|
|
822
|
+
return [result];
|
|
823
|
+
} catch (error) {
|
|
824
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
825
|
+
if (shouldFallback(err)) {
|
|
826
|
+
if (prompts.length > 1) {
|
|
827
|
+
const mid = Math.ceil(prompts.length / 2);
|
|
828
|
+
const leftPrompts = prompts.slice(0, mid);
|
|
829
|
+
const rightPrompts = prompts.slice(mid);
|
|
830
|
+
logger.debug(
|
|
831
|
+
`Batch of ${String(prompts.length)} failed, splitting into ${String(leftPrompts.length)} + ${String(rightPrompts.length)}`,
|
|
832
|
+
"analyzer"
|
|
833
|
+
);
|
|
834
|
+
const [leftResults, rightResults] = await Promise.all([
|
|
835
|
+
analyzeWithFallback(
|
|
836
|
+
provider,
|
|
837
|
+
leftPrompts,
|
|
838
|
+
date,
|
|
839
|
+
context,
|
|
840
|
+
model,
|
|
841
|
+
noCache
|
|
842
|
+
),
|
|
843
|
+
analyzeWithFallback(
|
|
844
|
+
provider,
|
|
845
|
+
rightPrompts,
|
|
846
|
+
date,
|
|
847
|
+
context,
|
|
848
|
+
model,
|
|
849
|
+
noCache
|
|
850
|
+
)
|
|
851
|
+
]);
|
|
852
|
+
return [...leftResults, ...rightResults];
|
|
853
|
+
}
|
|
854
|
+
const promptPreview = prompts[0]?.slice(0, 50) ?? "unknown";
|
|
855
|
+
logger.warn(
|
|
856
|
+
`Skipped single prompt due to error: ${promptPreview}... (${err.message})`,
|
|
857
|
+
"analyzer"
|
|
858
|
+
);
|
|
859
|
+
return [];
|
|
860
|
+
}
|
|
861
|
+
throw err;
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
function estimateTokens(text) {
|
|
865
|
+
if (!text || text.length === 0) return 0;
|
|
866
|
+
return Math.ceil(text.length / CHARS_PER_TOKEN);
|
|
867
|
+
}
|
|
868
|
+
function batchPrompts(options) {
|
|
869
|
+
const { prompts, maxTokensPerBatch, maxPromptsPerBatch, prioritization } = options;
|
|
870
|
+
if (prompts.length === 0) {
|
|
871
|
+
return [];
|
|
872
|
+
}
|
|
873
|
+
const effectiveLimit = maxTokensPerBatch - SYSTEM_OVERHEAD_TOKENS;
|
|
874
|
+
const promptsWithTokens = prompts.map((prompt) => ({
|
|
875
|
+
prompt,
|
|
876
|
+
tokens: estimateTokens(prompt)
|
|
877
|
+
}));
|
|
878
|
+
const sorted = prioritization === "longest-first" ? [...promptsWithTokens].sort((a, b) => b.tokens - a.tokens) : promptsWithTokens;
|
|
879
|
+
const batches = [];
|
|
880
|
+
let currentBatch = [];
|
|
881
|
+
let currentTokens = 0;
|
|
882
|
+
for (const { prompt, tokens } of sorted) {
|
|
883
|
+
if (tokens > effectiveLimit) {
|
|
884
|
+
if (currentBatch.length > 0) {
|
|
885
|
+
batches.push({ prompts: currentBatch, tokens: currentTokens });
|
|
886
|
+
currentBatch = [];
|
|
887
|
+
currentTokens = 0;
|
|
888
|
+
}
|
|
889
|
+
batches.push({ prompts: [prompt], tokens });
|
|
890
|
+
continue;
|
|
891
|
+
}
|
|
892
|
+
const wouldExceedTokens = currentTokens + tokens > effectiveLimit && currentBatch.length > 0;
|
|
893
|
+
const wouldExceedPrompts = maxPromptsPerBatch !== void 0 && currentBatch.length >= maxPromptsPerBatch;
|
|
894
|
+
if (wouldExceedTokens || wouldExceedPrompts) {
|
|
895
|
+
batches.push({ prompts: currentBatch, tokens: currentTokens });
|
|
896
|
+
currentBatch = [prompt];
|
|
897
|
+
currentTokens = tokens;
|
|
898
|
+
} else {
|
|
899
|
+
currentBatch.push(prompt);
|
|
900
|
+
currentTokens += tokens;
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
if (currentBatch.length > 0) {
|
|
904
|
+
batches.push({ prompts: currentBatch, tokens: currentTokens });
|
|
905
|
+
}
|
|
906
|
+
return batches;
|
|
907
|
+
}
|
|
908
|
+
function mergeBatchResults(options) {
|
|
909
|
+
const { results, date } = options;
|
|
910
|
+
if (results.length === 0) {
|
|
911
|
+
throw new Error("Cannot merge empty results array");
|
|
912
|
+
}
|
|
913
|
+
if (results.length === 1) {
|
|
914
|
+
const result = results[0];
|
|
915
|
+
if (!result) {
|
|
916
|
+
throw new Error("Results array is empty");
|
|
917
|
+
}
|
|
918
|
+
const limitedPatterns = result.patterns.map((p) => ({
|
|
919
|
+
...p,
|
|
920
|
+
examples: limitExamples(p.examples, MAX_EXAMPLES_PER_PATTERN)
|
|
921
|
+
})).sort((a, b) => {
|
|
922
|
+
const severityDiff = SEVERITY_RANK[b.severity] - SEVERITY_RANK[a.severity];
|
|
923
|
+
if (severityDiff !== 0) return severityDiff;
|
|
924
|
+
return b.frequency - a.frequency;
|
|
925
|
+
}).slice(0, MAX_PATTERNS);
|
|
926
|
+
const topSuggestion2 = limitedPatterns.length > 0 && limitedPatterns[0] ? limitedPatterns[0].suggestion : result.topSuggestion;
|
|
927
|
+
return {
|
|
928
|
+
...result,
|
|
929
|
+
date,
|
|
930
|
+
patterns: limitedPatterns,
|
|
931
|
+
topSuggestion: topSuggestion2
|
|
932
|
+
};
|
|
933
|
+
}
|
|
934
|
+
const allPatterns = flattenAll(results.map((r) => r.patterns));
|
|
935
|
+
const patternGroups = /* @__PURE__ */ new Map();
|
|
936
|
+
for (const pattern of allPatterns) {
|
|
937
|
+
const group = patternGroups.get(pattern.id) ?? [];
|
|
938
|
+
group.push(pattern);
|
|
939
|
+
patternGroups.set(pattern.id, group);
|
|
940
|
+
}
|
|
941
|
+
const mergedPatterns = [];
|
|
942
|
+
for (const group of patternGroups.values()) {
|
|
943
|
+
if (group.length === 0) continue;
|
|
944
|
+
const firstPattern = group[0];
|
|
945
|
+
if (!firstPattern) continue;
|
|
946
|
+
if (group.length === 1) {
|
|
947
|
+
mergedPatterns.push({
|
|
948
|
+
...firstPattern,
|
|
949
|
+
examples: limitExamples(
|
|
950
|
+
firstPattern.examples,
|
|
951
|
+
MAX_EXAMPLES_PER_PATTERN
|
|
952
|
+
)
|
|
953
|
+
});
|
|
954
|
+
} else {
|
|
955
|
+
const frequencies = group.map((p) => p.frequency);
|
|
956
|
+
const severities = group.map((p) => p.severity);
|
|
957
|
+
const allExamples = group.flatMap((p) => Array.from(p.examples));
|
|
958
|
+
const mergedFrequency = Math.round(average(frequencies));
|
|
959
|
+
const mergedSeverity = maxSeverity(severities);
|
|
960
|
+
const mergedExamples = limitExamples(
|
|
961
|
+
allExamples,
|
|
962
|
+
MAX_EXAMPLES_PER_PATTERN
|
|
963
|
+
);
|
|
964
|
+
mergedPatterns.push({
|
|
965
|
+
...firstPattern,
|
|
966
|
+
frequency: mergedFrequency,
|
|
967
|
+
severity: mergedSeverity,
|
|
968
|
+
examples: mergedExamples
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
const sortedPatterns = mergedPatterns.sort((a, b) => {
|
|
973
|
+
const severityDiff = SEVERITY_RANK[b.severity] - SEVERITY_RANK[a.severity];
|
|
974
|
+
if (severityDiff !== 0) return severityDiff;
|
|
975
|
+
return b.frequency - a.frequency;
|
|
976
|
+
});
|
|
977
|
+
const topPatterns = sortedPatterns.slice(0, MAX_PATTERNS);
|
|
978
|
+
const totalPrompts = sum(results.map((r) => r.stats.totalPrompts));
|
|
979
|
+
const promptsWithIssues = sum(results.map((r) => r.stats.promptsWithIssues));
|
|
980
|
+
const avgScore = Math.round(
|
|
981
|
+
average(results.map((r) => r.stats.overallScore))
|
|
982
|
+
);
|
|
983
|
+
const topSuggestion = topPatterns.length > 0 && topPatterns[0] ? topPatterns[0].suggestion : results[0]?.topSuggestion ?? "No suggestions available";
|
|
984
|
+
return {
|
|
985
|
+
date,
|
|
986
|
+
patterns: topPatterns,
|
|
987
|
+
stats: {
|
|
988
|
+
totalPrompts,
|
|
989
|
+
promptsWithIssues,
|
|
990
|
+
overallScore: avgScore
|
|
991
|
+
},
|
|
992
|
+
topSuggestion
|
|
993
|
+
};
|
|
994
|
+
}
|
|
995
|
+
function applyRulesToResult(result, rules) {
|
|
996
|
+
if (!rules || Object.keys(rules).length === 0) {
|
|
997
|
+
return result;
|
|
998
|
+
}
|
|
999
|
+
const customTaxonomy = applyRulesConfig(rules, ISSUE_TAXONOMY);
|
|
1000
|
+
const enabledPatternIds = Object.keys(customTaxonomy);
|
|
1001
|
+
const filteredPatterns = result.patterns.filter(
|
|
1002
|
+
(pattern) => enabledPatternIds.includes(pattern.id)
|
|
1003
|
+
);
|
|
1004
|
+
const updatedPatterns = filteredPatterns.map((pattern) => {
|
|
1005
|
+
const ruleConfig = rules[pattern.id];
|
|
1006
|
+
if (ruleConfig?.severity) {
|
|
1007
|
+
return {
|
|
1008
|
+
...pattern,
|
|
1009
|
+
severity: ruleConfig.severity
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
return pattern;
|
|
1013
|
+
});
|
|
1014
|
+
const topSuggestion = updatedPatterns.length > 0 && updatedPatterns[0] ? updatedPatterns[0].suggestion : result.topSuggestion;
|
|
1015
|
+
return {
|
|
1016
|
+
...result,
|
|
1017
|
+
patterns: updatedPatterns,
|
|
1018
|
+
topSuggestion
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
1021
|
+
async function analyzePrompts(options) {
|
|
1022
|
+
const { provider, prompts, date, context, rules, onProgress, noCache } = options;
|
|
1023
|
+
logger.debug(
|
|
1024
|
+
`Starting analysis of ${String(prompts.length)} prompts for ${date}`,
|
|
1025
|
+
"analyzer"
|
|
1026
|
+
);
|
|
1027
|
+
if (prompts.length === 0) {
|
|
1028
|
+
throw new Error("Cannot analyze empty prompts array");
|
|
1029
|
+
}
|
|
1030
|
+
const { prompts: sanitizedPrompts, totalRedacted } = sanitizePrompts(prompts);
|
|
1031
|
+
if (totalRedacted > 0) {
|
|
1032
|
+
logger.debug(
|
|
1033
|
+
`Sanitizer redacted ${String(totalRedacted)} secrets`,
|
|
1034
|
+
"analyzer"
|
|
1035
|
+
);
|
|
1036
|
+
}
|
|
1037
|
+
const limits = hasDynamicLimits(provider) ? provider.getBatchLimits() : PROVIDER_LIMITS[inferProviderType(provider.name)];
|
|
1038
|
+
const limitsInfo = limits.maxPromptsPerBatch ? `${String(limits.maxTokensPerBatch)} tokens/batch, max ${String(limits.maxPromptsPerBatch)} prompts/batch` : `${String(limits.maxTokensPerBatch)} tokens/batch`;
|
|
1039
|
+
logger.debug(`Using batch limits: ${limitsInfo}`, "analyzer");
|
|
1040
|
+
const batches = batchPrompts({
|
|
1041
|
+
prompts: sanitizedPrompts,
|
|
1042
|
+
maxTokensPerBatch: limits.maxTokensPerBatch,
|
|
1043
|
+
maxPromptsPerBatch: limits.maxPromptsPerBatch,
|
|
1044
|
+
prioritization: limits.prioritization
|
|
1045
|
+
});
|
|
1046
|
+
logger.debug(
|
|
1047
|
+
`Created ${String(batches.length)} batch(es) for analysis`,
|
|
1048
|
+
"analyzer"
|
|
1049
|
+
);
|
|
1050
|
+
if (batches.length === 1) {
|
|
1051
|
+
const batch = batches[0];
|
|
1052
|
+
if (!batch) {
|
|
1053
|
+
throw new Error("Failed to create batches");
|
|
1054
|
+
}
|
|
1055
|
+
if (onProgress) {
|
|
1056
|
+
onProgress(0, 1);
|
|
1057
|
+
}
|
|
1058
|
+
const model2 = extractModelFromProvider(provider.name);
|
|
1059
|
+
logger.debug(
|
|
1060
|
+
`Processing single batch (${String(batch.tokens)} tokens, ${String(batch.prompts.length)} prompts)`,
|
|
1061
|
+
"analyzer"
|
|
1062
|
+
);
|
|
1063
|
+
const startTime = Date.now();
|
|
1064
|
+
const results2 = await analyzeWithFallback(
|
|
1065
|
+
provider,
|
|
1066
|
+
batch.prompts,
|
|
1067
|
+
date,
|
|
1068
|
+
context,
|
|
1069
|
+
model2,
|
|
1070
|
+
noCache ?? false
|
|
1071
|
+
);
|
|
1072
|
+
const elapsed = Date.now() - startTime;
|
|
1073
|
+
logger.debug(`Batch completed in ${String(elapsed)}ms`, "analyzer");
|
|
1074
|
+
if (onProgress) {
|
|
1075
|
+
onProgress(1, 1);
|
|
1076
|
+
}
|
|
1077
|
+
if (results2.length === 0) {
|
|
1078
|
+
throw new Error("All prompts failed analysis");
|
|
1079
|
+
}
|
|
1080
|
+
if (results2.length === 1) {
|
|
1081
|
+
const result = results2[0];
|
|
1082
|
+
if (!result) {
|
|
1083
|
+
throw new Error("Analysis returned empty result");
|
|
1084
|
+
}
|
|
1085
|
+
logger.debug(
|
|
1086
|
+
`Analysis complete: ${String(result.patterns.length)} patterns found`,
|
|
1087
|
+
"analyzer"
|
|
1088
|
+
);
|
|
1089
|
+
return applyRulesToResult(result, rules);
|
|
1090
|
+
}
|
|
1091
|
+
logger.debug(
|
|
1092
|
+
`Merging ${String(results2.length)} fallback results`,
|
|
1093
|
+
"analyzer"
|
|
1094
|
+
);
|
|
1095
|
+
const mergedResult2 = mergeBatchResults({ results: results2, date });
|
|
1096
|
+
logger.debug(
|
|
1097
|
+
`Analysis complete: ${String(mergedResult2.patterns.length)} patterns found`,
|
|
1098
|
+
"analyzer"
|
|
1099
|
+
);
|
|
1100
|
+
return applyRulesToResult(mergedResult2, rules);
|
|
1101
|
+
}
|
|
1102
|
+
const results = [];
|
|
1103
|
+
const totalBatches = batches.length;
|
|
1104
|
+
const model = extractModelFromProvider(provider.name);
|
|
1105
|
+
for (let i = 0; i < batches.length; i++) {
|
|
1106
|
+
const batch = batches[i];
|
|
1107
|
+
if (!batch) continue;
|
|
1108
|
+
if (onProgress) {
|
|
1109
|
+
onProgress(i, totalBatches);
|
|
1110
|
+
}
|
|
1111
|
+
logger.debug(
|
|
1112
|
+
`Processing batch ${String(i + 1)}/${String(totalBatches)} (${String(batch.tokens)} tokens, ${String(batch.prompts.length)} prompts)`,
|
|
1113
|
+
"analyzer"
|
|
1114
|
+
);
|
|
1115
|
+
const startTime = Date.now();
|
|
1116
|
+
const batchResults = await analyzeWithFallback(
|
|
1117
|
+
provider,
|
|
1118
|
+
batch.prompts,
|
|
1119
|
+
date,
|
|
1120
|
+
context,
|
|
1121
|
+
model,
|
|
1122
|
+
noCache ?? false
|
|
1123
|
+
);
|
|
1124
|
+
results.push(...batchResults);
|
|
1125
|
+
const elapsed = Date.now() - startTime;
|
|
1126
|
+
logger.debug(
|
|
1127
|
+
`Batch ${String(i + 1)}/${String(totalBatches)} completed in ${String(elapsed)}ms (${String(batchResults.length)} result(s))`,
|
|
1128
|
+
"analyzer"
|
|
1129
|
+
);
|
|
1130
|
+
}
|
|
1131
|
+
if (onProgress) {
|
|
1132
|
+
onProgress(totalBatches, totalBatches);
|
|
1133
|
+
}
|
|
1134
|
+
if (results.length === 0) {
|
|
1135
|
+
throw new Error("All batches failed analysis");
|
|
1136
|
+
}
|
|
1137
|
+
logger.debug(`Merging ${String(results.length)} batch results`, "analyzer");
|
|
1138
|
+
const mergedResult = mergeBatchResults({ results, date });
|
|
1139
|
+
logger.debug(
|
|
1140
|
+
`Analysis complete: ${String(mergedResult.patterns.length)} patterns found`,
|
|
1141
|
+
"analyzer"
|
|
1142
|
+
);
|
|
1143
|
+
return applyRulesToResult(mergedResult, rules);
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
// src/core/schema-validator.ts
|
|
1147
|
+
var SUPPORTED_VERSIONS = ["1.0"];
|
|
1148
|
+
var CURRENT_VERSION = {
|
|
1149
|
+
major: 1,
|
|
1150
|
+
minor: 0,
|
|
1151
|
+
detected: "1.0"
|
|
1152
|
+
};
|
|
1153
|
+
var V1_0_REQUIRED_FIELDS = [
|
|
1154
|
+
"type",
|
|
1155
|
+
"message",
|
|
1156
|
+
"timestamp",
|
|
1157
|
+
"sessionId",
|
|
1158
|
+
"cwd"
|
|
1159
|
+
];
|
|
1160
|
+
var V1_0_MESSAGE_FIELDS = ["role", "content"];
|
|
1161
|
+
var V1_0_MESSAGE_TYPES = ["user", "assistant", "system"];
|
|
1162
|
+
function isObject(value) {
|
|
1163
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1164
|
+
}
|
|
1165
|
+
function hasRequiredFields(obj, fields) {
|
|
1166
|
+
return fields.every((field) => field in obj);
|
|
1167
|
+
}
|
|
1168
|
+
function isValidV1Message(message) {
|
|
1169
|
+
if (!isObject(message)) {
|
|
1170
|
+
return false;
|
|
1171
|
+
}
|
|
1172
|
+
if (!hasRequiredFields(message, V1_0_MESSAGE_FIELDS)) {
|
|
1173
|
+
return false;
|
|
1174
|
+
}
|
|
1175
|
+
const role = message["role"];
|
|
1176
|
+
if (typeof role !== "string" || !V1_0_MESSAGE_TYPES.includes(role)) {
|
|
1177
|
+
return false;
|
|
1178
|
+
}
|
|
1179
|
+
const content = message["content"];
|
|
1180
|
+
if (typeof content !== "string") {
|
|
1181
|
+
return false;
|
|
1182
|
+
}
|
|
1183
|
+
return true;
|
|
1184
|
+
}
|
|
1185
|
+
function isValidV1Type(type) {
|
|
1186
|
+
return typeof type === "string" && V1_0_MESSAGE_TYPES.includes(type);
|
|
1187
|
+
}
|
|
1188
|
+
function detectSchemaVersion(entry) {
|
|
1189
|
+
if (!isObject(entry)) {
|
|
1190
|
+
return null;
|
|
1191
|
+
}
|
|
1192
|
+
if (!hasRequiredFields(entry, V1_0_REQUIRED_FIELDS)) {
|
|
1193
|
+
return null;
|
|
1194
|
+
}
|
|
1195
|
+
if (!isValidV1Type(entry["type"])) {
|
|
1196
|
+
return null;
|
|
1197
|
+
}
|
|
1198
|
+
if (!isValidV1Message(entry["message"])) {
|
|
1199
|
+
return null;
|
|
1200
|
+
}
|
|
1201
|
+
if (typeof entry["timestamp"] !== "string") {
|
|
1202
|
+
return null;
|
|
1203
|
+
}
|
|
1204
|
+
if (typeof entry["sessionId"] !== "string") {
|
|
1205
|
+
return null;
|
|
1206
|
+
}
|
|
1207
|
+
if (typeof entry["cwd"] !== "string") {
|
|
1208
|
+
return null;
|
|
1209
|
+
}
|
|
1210
|
+
return CURRENT_VERSION;
|
|
1211
|
+
}
|
|
1212
|
+
function isSchemaSupported(version) {
|
|
1213
|
+
return SUPPORTED_VERSIONS.includes(version.detected);
|
|
1214
|
+
}
|
|
1215
|
+
function getSchemaWarning(version, context) {
|
|
1216
|
+
const contextSuffix = "";
|
|
1217
|
+
if (version === null) {
|
|
1218
|
+
return `Unknown log format detected${contextSuffix}. The log entry structure does not match any known Claude Code schema. This entry will be skipped.`;
|
|
1219
|
+
}
|
|
1220
|
+
if (!isSchemaSupported(version)) {
|
|
1221
|
+
return `Unsupported schema version ${version.detected}${contextSuffix}. Supported versions: ${SUPPORTED_VERSIONS.join(", ")}. This entry may not be processed correctly.`;
|
|
1222
|
+
}
|
|
1223
|
+
return "";
|
|
1224
|
+
}
|
|
1225
|
+
function validateLogEntry(entry) {
|
|
1226
|
+
const version = detectSchemaVersion(entry);
|
|
1227
|
+
if (version === null) {
|
|
1228
|
+
return {
|
|
1229
|
+
isValid: false,
|
|
1230
|
+
version: null,
|
|
1231
|
+
warning: getSchemaWarning(null)
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
if (!isSchemaSupported(version)) {
|
|
1235
|
+
return {
|
|
1236
|
+
isValid: false,
|
|
1237
|
+
version,
|
|
1238
|
+
warning: getSchemaWarning(version)
|
|
1239
|
+
};
|
|
1240
|
+
}
|
|
1241
|
+
return {
|
|
1242
|
+
isValid: true,
|
|
1243
|
+
version,
|
|
1244
|
+
warning: null
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
// src/core/log-reader.ts
|
|
1249
|
+
function claudeProjectsExist() {
|
|
1250
|
+
return existsSync(CLAUDE_PROJECTS_DIR);
|
|
1251
|
+
}
|
|
1252
|
+
function extractProjectName(filePath) {
|
|
1253
|
+
const parts = filePath.split("/");
|
|
1254
|
+
const projectIndex = parts.findIndex((p) => p === "projects");
|
|
1255
|
+
const projectHash = parts[projectIndex + 1];
|
|
1256
|
+
if (projectIndex !== -1 && projectHash !== void 0) {
|
|
1257
|
+
return projectHash;
|
|
1258
|
+
}
|
|
1259
|
+
return basename(filePath, ".jsonl");
|
|
1260
|
+
}
|
|
1261
|
+
function parseDate(dateStr) {
|
|
1262
|
+
const normalized = dateStr.trim().toLowerCase();
|
|
1263
|
+
if (normalized === "today") {
|
|
1264
|
+
return startOfDay(/* @__PURE__ */ new Date());
|
|
1265
|
+
}
|
|
1266
|
+
if (normalized === "yesterday") {
|
|
1267
|
+
const yesterday = /* @__PURE__ */ new Date();
|
|
1268
|
+
yesterday.setDate(yesterday.getDate() - 1);
|
|
1269
|
+
return startOfDay(yesterday);
|
|
1270
|
+
}
|
|
1271
|
+
try {
|
|
1272
|
+
const parsed = parseISO(dateStr);
|
|
1273
|
+
if (isNaN(parsed.getTime())) {
|
|
1274
|
+
throw new Error(`Invalid date format: ${dateStr}`);
|
|
1275
|
+
}
|
|
1276
|
+
return startOfDay(parsed);
|
|
1277
|
+
} catch {
|
|
1278
|
+
throw new Error(
|
|
1279
|
+
`Invalid date format: ${dateStr}. Expected 'today', 'yesterday', or YYYY-MM-DD`
|
|
1280
|
+
);
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
function extractDate(timestamp) {
|
|
1284
|
+
try {
|
|
1285
|
+
const date = parseISO(timestamp);
|
|
1286
|
+
const dateStr = date.toISOString().split("T")[0];
|
|
1287
|
+
return dateStr ?? "unknown";
|
|
1288
|
+
} catch {
|
|
1289
|
+
return "unknown";
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
function isClaudeMessage(value) {
|
|
1293
|
+
if (typeof value !== "object" || value === null) {
|
|
1294
|
+
return false;
|
|
1295
|
+
}
|
|
1296
|
+
const obj = value;
|
|
1297
|
+
return typeof obj["timestamp"] === "string" && typeof obj["type"] === "string" && typeof obj["message"] === "object" && obj["message"] !== null && typeof obj["message"]["content"] === "string";
|
|
1298
|
+
}
|
|
1299
|
+
function parseLine(line, lineNumber, filePath) {
|
|
1300
|
+
if (!line.trim()) {
|
|
1301
|
+
return null;
|
|
1302
|
+
}
|
|
1303
|
+
const context = lineNumber !== void 0 && filePath ? `${filePath}:${String(lineNumber)}` : void 0;
|
|
1304
|
+
try {
|
|
1305
|
+
const parsed = JSON.parse(line);
|
|
1306
|
+
const validation = validateLogEntry(parsed);
|
|
1307
|
+
if (!validation.isValid) {
|
|
1308
|
+
logger.debug(
|
|
1309
|
+
`Schema validation failed: ${validation.warning ?? "Unknown format"}`,
|
|
1310
|
+
context
|
|
1311
|
+
);
|
|
1312
|
+
return null;
|
|
1313
|
+
}
|
|
1314
|
+
if (!isClaudeMessage(parsed)) {
|
|
1315
|
+
logger.debug("Type guard validation failed", context);
|
|
1316
|
+
return null;
|
|
1317
|
+
}
|
|
1318
|
+
return parsed;
|
|
1319
|
+
} catch (error) {
|
|
1320
|
+
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1321
|
+
logger.debug(`JSON parse failed: ${errorMessage}`, context);
|
|
1322
|
+
return null;
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
function isUserMessage(message) {
|
|
1326
|
+
return message.type === "user" && message.message.role === "user";
|
|
1327
|
+
}
|
|
1328
|
+
function extractContent(message) {
|
|
1329
|
+
return message.message.content;
|
|
1330
|
+
}
|
|
1331
|
+
async function readJsonlFile(filePath) {
|
|
1332
|
+
const prompts = [];
|
|
1333
|
+
const warnings = [];
|
|
1334
|
+
const projectName = extractProjectName(filePath);
|
|
1335
|
+
try {
|
|
1336
|
+
const content = await readFile(filePath, "utf-8");
|
|
1337
|
+
const lines = content.split("\n");
|
|
1338
|
+
let skippedLines = 0;
|
|
1339
|
+
for (let i = 0; i < lines.length; i++) {
|
|
1340
|
+
const line = lines[i] ?? "";
|
|
1341
|
+
const lineNumber = i + 1;
|
|
1342
|
+
const message = parseLine(line, lineNumber, filePath);
|
|
1343
|
+
if (message === null) {
|
|
1344
|
+
if (line.trim()) {
|
|
1345
|
+
skippedLines++;
|
|
1346
|
+
}
|
|
1347
|
+
continue;
|
|
1348
|
+
}
|
|
1349
|
+
if (!isUserMessage(message)) {
|
|
1350
|
+
continue;
|
|
1351
|
+
}
|
|
1352
|
+
const extractedContent = extractContent(message);
|
|
1353
|
+
if (!extractedContent.trim()) {
|
|
1354
|
+
continue;
|
|
1355
|
+
}
|
|
1356
|
+
prompts.push({
|
|
1357
|
+
content: extractedContent,
|
|
1358
|
+
timestamp: message.timestamp,
|
|
1359
|
+
sessionId: message.sessionId,
|
|
1360
|
+
project: projectName,
|
|
1361
|
+
date: extractDate(message.timestamp)
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
if (skippedLines > 0) {
|
|
1365
|
+
const warning = `Skipped ${String(skippedLines)} invalid line(s) in ${filePath}`;
|
|
1366
|
+
warnings.push(warning);
|
|
1367
|
+
logger.collectWarning(warning, "log-reader");
|
|
1368
|
+
}
|
|
1369
|
+
} catch (error) {
|
|
1370
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
1371
|
+
const warning = `Failed to read ${filePath}: ${errorMessage}`;
|
|
1372
|
+
warnings.push(warning);
|
|
1373
|
+
logger.error(warning, "log-reader");
|
|
1374
|
+
}
|
|
1375
|
+
return { prompts, warnings };
|
|
1376
|
+
}
|
|
1377
|
+
function filterByDateRange(prompts, fromDate, toDate) {
|
|
1378
|
+
return prompts.filter((prompt) => {
|
|
1379
|
+
try {
|
|
1380
|
+
const promptDate = parseISO(prompt.timestamp);
|
|
1381
|
+
const promptDay = startOfDay(promptDate);
|
|
1382
|
+
return (isSameDay(promptDay, fromDate) || isAfter(promptDay, fromDate)) && (isSameDay(promptDay, toDate) || isBefore(promptDay, toDate));
|
|
1383
|
+
} catch (error) {
|
|
1384
|
+
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1385
|
+
logger.debug(
|
|
1386
|
+
`Invalid timestamp in filterByDateRange: ${prompt.timestamp} - ${errorMessage}`,
|
|
1387
|
+
"log-reader"
|
|
1388
|
+
);
|
|
1389
|
+
return false;
|
|
1390
|
+
}
|
|
1391
|
+
});
|
|
1392
|
+
}
|
|
1393
|
+
function filterByDate(prompts, targetDate) {
|
|
1394
|
+
return prompts.filter((prompt) => {
|
|
1395
|
+
try {
|
|
1396
|
+
const promptDate = parseISO(prompt.timestamp);
|
|
1397
|
+
return isSameDay(startOfDay(promptDate), targetDate);
|
|
1398
|
+
} catch (error) {
|
|
1399
|
+
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1400
|
+
logger.debug(
|
|
1401
|
+
`Invalid timestamp in filterByDate: ${prompt.timestamp} - ${errorMessage}`,
|
|
1402
|
+
"log-reader"
|
|
1403
|
+
);
|
|
1404
|
+
return false;
|
|
1405
|
+
}
|
|
1406
|
+
});
|
|
1407
|
+
}
|
|
1408
|
+
function filterByProject(prompts, projectName) {
|
|
1409
|
+
const normalizedProject = projectName.toLowerCase().trim();
|
|
1410
|
+
return prompts.filter(
|
|
1411
|
+
(prompt) => prompt.project.toLowerCase().includes(normalizedProject)
|
|
1412
|
+
);
|
|
1413
|
+
}
|
|
1414
|
+
function groupByDay(prompts) {
|
|
1415
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1416
|
+
for (const prompt of prompts) {
|
|
1417
|
+
const date = prompt.date;
|
|
1418
|
+
if (!groups.has(date)) {
|
|
1419
|
+
groups.set(date, { prompts: [], projects: /* @__PURE__ */ new Set() });
|
|
1420
|
+
}
|
|
1421
|
+
const group = groups.get(date);
|
|
1422
|
+
if (group) {
|
|
1423
|
+
group.prompts.push(prompt);
|
|
1424
|
+
group.projects.add(prompt.project);
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
const result = Array.from(groups.entries()).map(([date, { prompts: groupPrompts, projects }]) => {
|
|
1428
|
+
const sortedPrompts = groupPrompts.sort((a, b) => {
|
|
1429
|
+
try {
|
|
1430
|
+
const dateA = parseISO(a.timestamp);
|
|
1431
|
+
const dateB = parseISO(b.timestamp);
|
|
1432
|
+
return dateA.getTime() - dateB.getTime();
|
|
1433
|
+
} catch (error) {
|
|
1434
|
+
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1435
|
+
logger.debug(
|
|
1436
|
+
`Invalid timestamp in groupByDay sort: ${a.timestamp} or ${b.timestamp} - ${errorMessage}`,
|
|
1437
|
+
"log-reader"
|
|
1438
|
+
);
|
|
1439
|
+
return 0;
|
|
1440
|
+
}
|
|
1441
|
+
});
|
|
1442
|
+
return {
|
|
1443
|
+
date,
|
|
1444
|
+
prompts: sortedPrompts,
|
|
1445
|
+
projects: Array.from(projects).sort()
|
|
1446
|
+
};
|
|
1447
|
+
}).sort((a, b) => {
|
|
1448
|
+
try {
|
|
1449
|
+
const dateA = parseISO(a.date + "T00:00:00Z");
|
|
1450
|
+
const dateB = parseISO(b.date + "T00:00:00Z");
|
|
1451
|
+
return dateA.getTime() - dateB.getTime();
|
|
1452
|
+
} catch (error) {
|
|
1453
|
+
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1454
|
+
logger.debug(
|
|
1455
|
+
`Invalid date in groupByDay sort: ${a.date} or ${b.date} - ${errorMessage}`,
|
|
1456
|
+
"log-reader"
|
|
1457
|
+
);
|
|
1458
|
+
return 0;
|
|
1459
|
+
}
|
|
1460
|
+
});
|
|
1461
|
+
return result;
|
|
1462
|
+
}
|
|
1463
|
+
async function readLogs(options) {
|
|
1464
|
+
logger.debug(`Scanning ${CLAUDE_PROJECTS_DIR}`, "log-reader");
|
|
1465
|
+
if (!claudeProjectsExist()) {
|
|
1466
|
+
logger.debug("Claude projects directory not found", "log-reader");
|
|
1467
|
+
return {
|
|
1468
|
+
prompts: [],
|
|
1469
|
+
warnings: [
|
|
1470
|
+
`Claude projects directory not found at ${CLAUDE_PROJECTS_DIR}`
|
|
1471
|
+
]
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
const pattern = `${CLAUDE_PROJECTS_DIR}/**/*.jsonl`;
|
|
1475
|
+
let files;
|
|
1476
|
+
try {
|
|
1477
|
+
files = await glob(pattern);
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
1480
|
+
const warning = `Failed to search for JSONL files: ${errorMessage}`;
|
|
1481
|
+
logger.error(warning, "log-reader");
|
|
1482
|
+
return {
|
|
1483
|
+
prompts: [],
|
|
1484
|
+
warnings: [warning]
|
|
1485
|
+
};
|
|
1486
|
+
}
|
|
1487
|
+
logger.debug(`Found ${String(files.length)} JSONL files`, "log-reader");
|
|
1488
|
+
if (files.length === 0) {
|
|
1489
|
+
return {
|
|
1490
|
+
prompts: [],
|
|
1491
|
+
warnings: ["No JSONL files found in Claude projects directory"]
|
|
1492
|
+
};
|
|
1493
|
+
}
|
|
1494
|
+
const allPrompts = [];
|
|
1495
|
+
const allWarnings = [];
|
|
1496
|
+
const CONCURRENCY_LIMIT = 10;
|
|
1497
|
+
for (let i = 0; i < files.length; i += CONCURRENCY_LIMIT) {
|
|
1498
|
+
const chunk = files.slice(i, i + CONCURRENCY_LIMIT);
|
|
1499
|
+
const results = await Promise.all(chunk.map(readJsonlFile));
|
|
1500
|
+
for (const { prompts, warnings } of results) {
|
|
1501
|
+
allPrompts.push(...prompts);
|
|
1502
|
+
allWarnings.push(...warnings);
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
logger.debug(
|
|
1506
|
+
`Parsed ${String(allPrompts.length)} total prompts from all files`,
|
|
1507
|
+
"log-reader"
|
|
1508
|
+
);
|
|
1509
|
+
let filteredPrompts = allPrompts;
|
|
1510
|
+
if (options) {
|
|
1511
|
+
if (options.from && options.to) {
|
|
1512
|
+
try {
|
|
1513
|
+
const fromDate = parseDate(options.from);
|
|
1514
|
+
const toDate = parseDate(options.to);
|
|
1515
|
+
if (isAfter(fromDate, toDate)) {
|
|
1516
|
+
return {
|
|
1517
|
+
prompts: [],
|
|
1518
|
+
warnings: [
|
|
1519
|
+
...allWarnings,
|
|
1520
|
+
`Invalid date range: --from (${options.from}) must be before or equal to --to (${options.to})`
|
|
1521
|
+
]
|
|
1522
|
+
};
|
|
1523
|
+
}
|
|
1524
|
+
filteredPrompts = filterByDateRange(filteredPrompts, fromDate, toDate);
|
|
1525
|
+
} catch (error) {
|
|
1526
|
+
const errorMessage = error instanceof Error ? error.message : "Invalid date range";
|
|
1527
|
+
return {
|
|
1528
|
+
prompts: [],
|
|
1529
|
+
warnings: [...allWarnings, errorMessage]
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
} else if (options.date) {
|
|
1533
|
+
try {
|
|
1534
|
+
const targetDate = parseDate(options.date);
|
|
1535
|
+
filteredPrompts = filterByDate(filteredPrompts, targetDate);
|
|
1536
|
+
} catch (error) {
|
|
1537
|
+
const errorMessage = error instanceof Error ? error.message : "Invalid date";
|
|
1538
|
+
return {
|
|
1539
|
+
prompts: [],
|
|
1540
|
+
warnings: [...allWarnings, errorMessage]
|
|
1541
|
+
};
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
if (options.project) {
|
|
1545
|
+
filteredPrompts = filterByProject(filteredPrompts, options.project);
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
if (options) {
|
|
1549
|
+
const filtered = allPrompts.length - filteredPrompts.length;
|
|
1550
|
+
if (filtered > 0) {
|
|
1551
|
+
logger.debug(
|
|
1552
|
+
`Filtered ${String(filtered)} prompts (${String(filteredPrompts.length)} remaining)`,
|
|
1553
|
+
"log-reader"
|
|
1554
|
+
);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
const sortedPrompts = filteredPrompts.sort((a, b) => {
|
|
1558
|
+
try {
|
|
1559
|
+
const dateA = parseISO(a.timestamp);
|
|
1560
|
+
const dateB = parseISO(b.timestamp);
|
|
1561
|
+
return dateA.getTime() - dateB.getTime();
|
|
1562
|
+
} catch (error) {
|
|
1563
|
+
const errorMessage = error instanceof Error ? error.message : "Parse error";
|
|
1564
|
+
logger.debug(
|
|
1565
|
+
`Invalid timestamp in readLogs sort: ${a.timestamp} or ${b.timestamp} - ${errorMessage}`,
|
|
1566
|
+
"log-reader"
|
|
1567
|
+
);
|
|
1568
|
+
return 0;
|
|
1569
|
+
}
|
|
1570
|
+
});
|
|
1571
|
+
logger.debug(
|
|
1572
|
+
`Returning ${String(sortedPrompts.length)} prompts for analysis`,
|
|
1573
|
+
"log-reader"
|
|
1574
|
+
);
|
|
1575
|
+
return {
|
|
1576
|
+
prompts: sortedPrompts,
|
|
1577
|
+
warnings: allWarnings
|
|
1578
|
+
};
|
|
1579
|
+
}
|
|
1580
|
+
function getHistoryDir() {
|
|
1581
|
+
return HYNTX_HISTORY_DIR;
|
|
1582
|
+
}
|
|
1583
|
+
async function ensureHistoryDir() {
|
|
1584
|
+
try {
|
|
1585
|
+
await mkdir(HYNTX_HISTORY_DIR, { recursive: true, mode: 448 });
|
|
1586
|
+
} catch (error) {
|
|
1587
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1588
|
+
throw new Error(
|
|
1589
|
+
`Failed to create history directory ${HYNTX_HISTORY_DIR}: ${errorMessage}`
|
|
1590
|
+
);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
function getHistoryFilePath(date) {
|
|
1594
|
+
return join(HYNTX_HISTORY_DIR, `${date}.json`);
|
|
1595
|
+
}
|
|
1596
|
+
async function saveAnalysisResult(result, metadata) {
|
|
1597
|
+
try {
|
|
1598
|
+
await ensureHistoryDir();
|
|
1599
|
+
const sanitizedResult = {
|
|
1600
|
+
...result,
|
|
1601
|
+
patterns: result.patterns.map((pattern) => ({
|
|
1602
|
+
...pattern,
|
|
1603
|
+
examples: pattern.examples.map((example) => sanitize(example).text)
|
|
1604
|
+
}))
|
|
1605
|
+
};
|
|
1606
|
+
const entry = {
|
|
1607
|
+
result: sanitizedResult,
|
|
1608
|
+
metadata
|
|
1609
|
+
};
|
|
1610
|
+
const filePath = getHistoryFilePath(result.date);
|
|
1611
|
+
const content = JSON.stringify(entry, null, 2);
|
|
1612
|
+
const tmpFile = `${filePath}.tmp`;
|
|
1613
|
+
await writeFile(tmpFile, content, "utf-8");
|
|
1614
|
+
await rename(tmpFile, filePath);
|
|
1615
|
+
logger.debug(`Saved history to ${filePath}`, "history");
|
|
1616
|
+
} catch (error) {
|
|
1617
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1618
|
+
logger.warn(
|
|
1619
|
+
`Failed to save analysis to history: ${errorMessage}`,
|
|
1620
|
+
"history"
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
async function loadAnalysisResult(date) {
|
|
1625
|
+
const filePath = getHistoryFilePath(date);
|
|
1626
|
+
if (!existsSync(filePath)) {
|
|
1627
|
+
return null;
|
|
1628
|
+
}
|
|
1629
|
+
try {
|
|
1630
|
+
const content = await readFile(filePath, "utf-8");
|
|
1631
|
+
const entry = JSON.parse(content);
|
|
1632
|
+
if (entry.result === void 0 || entry.metadata === void 0) {
|
|
1633
|
+
logger.warn(`Invalid history entry in ${filePath}`, "history");
|
|
1634
|
+
return null;
|
|
1635
|
+
}
|
|
1636
|
+
return entry;
|
|
1637
|
+
} catch (error) {
|
|
1638
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1639
|
+
logger.warn(
|
|
1640
|
+
`Failed to load history from ${filePath}: ${errorMessage}`,
|
|
1641
|
+
"history"
|
|
1642
|
+
);
|
|
1643
|
+
return null;
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
async function listAvailableDates(options) {
|
|
1647
|
+
if (!existsSync(HYNTX_HISTORY_DIR)) {
|
|
1648
|
+
return [];
|
|
1649
|
+
}
|
|
1650
|
+
try {
|
|
1651
|
+
const files = readdirSync(HYNTX_HISTORY_DIR);
|
|
1652
|
+
const dates = [];
|
|
1653
|
+
for (const file of files) {
|
|
1654
|
+
if (!file.endsWith(".json")) {
|
|
1655
|
+
continue;
|
|
1656
|
+
}
|
|
1657
|
+
const date = file.slice(0, -5);
|
|
1658
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
|
|
1659
|
+
continue;
|
|
1660
|
+
}
|
|
1661
|
+
if (options) {
|
|
1662
|
+
const entry = await loadAnalysisResult(date);
|
|
1663
|
+
if (!entry) {
|
|
1664
|
+
continue;
|
|
1665
|
+
}
|
|
1666
|
+
if (options.provider && entry.metadata.provider !== options.provider) {
|
|
1667
|
+
continue;
|
|
1668
|
+
}
|
|
1669
|
+
const projectFilter = options.project;
|
|
1670
|
+
if (projectFilter && !entry.metadata.projects.some(
|
|
1671
|
+
(p) => p.toLowerCase().includes(projectFilter.toLowerCase())
|
|
1672
|
+
)) {
|
|
1673
|
+
continue;
|
|
1674
|
+
}
|
|
1675
|
+
const score = entry.result.stats.overallScore;
|
|
1676
|
+
if (options.minScore !== void 0 && score < options.minScore) {
|
|
1677
|
+
continue;
|
|
1678
|
+
}
|
|
1679
|
+
if (options.maxScore !== void 0 && score > options.maxScore) {
|
|
1680
|
+
continue;
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
dates.push(date);
|
|
1684
|
+
}
|
|
1685
|
+
return dates.sort((a, b) => b.localeCompare(a));
|
|
1686
|
+
} catch (error) {
|
|
1687
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
1688
|
+
logger.warn(`Failed to list history dates: ${errorMessage}`, "history");
|
|
1689
|
+
return [];
|
|
1690
|
+
}
|
|
1691
|
+
}
|
|
1692
|
+
function compareResults(before, after) {
|
|
1693
|
+
const scoreDelta = after.stats.overallScore - before.stats.overallScore;
|
|
1694
|
+
const beforePatterns = new Map(before.patterns.map((p) => [p.id, p]));
|
|
1695
|
+
const afterPatterns = new Map(after.patterns.map((p) => [p.id, p]));
|
|
1696
|
+
const newPatterns = [];
|
|
1697
|
+
for (const pattern of after.patterns) {
|
|
1698
|
+
if (!beforePatterns.has(pattern.id)) {
|
|
1699
|
+
newPatterns.push(pattern);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
const resolvedPatterns = [];
|
|
1703
|
+
for (const pattern of before.patterns) {
|
|
1704
|
+
if (!afterPatterns.has(pattern.id)) {
|
|
1705
|
+
resolvedPatterns.push(pattern);
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
const changedPatterns = [];
|
|
1709
|
+
for (const [id, beforePattern] of beforePatterns) {
|
|
1710
|
+
const afterPattern = afterPatterns.get(id);
|
|
1711
|
+
if (!afterPattern) {
|
|
1712
|
+
continue;
|
|
1713
|
+
}
|
|
1714
|
+
const frequencyChanged = beforePattern.frequency !== afterPattern.frequency;
|
|
1715
|
+
const severityChanged = beforePattern.severity !== afterPattern.severity;
|
|
1716
|
+
if (frequencyChanged || severityChanged) {
|
|
1717
|
+
changedPatterns.push({
|
|
1718
|
+
id,
|
|
1719
|
+
name: beforePattern.name,
|
|
1720
|
+
status: "changed",
|
|
1721
|
+
frequencyBefore: beforePattern.frequency,
|
|
1722
|
+
frequencyAfter: afterPattern.frequency,
|
|
1723
|
+
severityBefore: beforePattern.severity,
|
|
1724
|
+
severityAfter: afterPattern.severity
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
const changes = {
|
|
1729
|
+
scoreDelta,
|
|
1730
|
+
newPatterns,
|
|
1731
|
+
resolvedPatterns,
|
|
1732
|
+
changedPatterns
|
|
1733
|
+
};
|
|
1734
|
+
return {
|
|
1735
|
+
before,
|
|
1736
|
+
after,
|
|
1737
|
+
changes
|
|
1738
|
+
};
|
|
1739
|
+
}
|
|
1740
|
+
function getDateOneWeekAgo(fromDate) {
|
|
1741
|
+
const date = typeof fromDate === "string" ? parseISO(fromDate) : fromDate;
|
|
1742
|
+
const weekAgo = subDays(date, 7);
|
|
1743
|
+
return format(weekAgo, "yyyy-MM-dd");
|
|
1744
|
+
}
|
|
1745
|
+
function getDateOneMonthAgo(fromDate) {
|
|
1746
|
+
const date = typeof fromDate === "string" ? parseISO(fromDate) : fromDate;
|
|
1747
|
+
const monthAgo = subMonths(date, 1);
|
|
1748
|
+
return format(monthAgo, "yyyy-MM-dd");
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
// src/core/aggregator.ts
|
|
1752
|
+
function lookupIssueMetadata(issueId, taxonomy) {
|
|
1753
|
+
if (taxonomy[issueId]) {
|
|
1754
|
+
return taxonomy[issueId];
|
|
1755
|
+
}
|
|
1756
|
+
return {
|
|
1757
|
+
name: titleCase(issueId),
|
|
1758
|
+
severity: "medium",
|
|
1759
|
+
suggestion: "Review this pattern"
|
|
1760
|
+
};
|
|
1761
|
+
}
|
|
1762
|
+
function titleCase(str) {
|
|
1763
|
+
return str.split("-").map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" ");
|
|
1764
|
+
}
|
|
1765
|
+
function convertMinimalToAnalysisResult(minimal, date, taxonomy) {
|
|
1766
|
+
const issueCounts = /* @__PURE__ */ new Map();
|
|
1767
|
+
for (const issue of minimal.issues) {
|
|
1768
|
+
issueCounts.set(issue, (issueCounts.get(issue) ?? 0) + 1);
|
|
1769
|
+
}
|
|
1770
|
+
const patterns = [...issueCounts.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5).map(([issueId, count]) => {
|
|
1771
|
+
const metadata = lookupIssueMetadata(issueId, taxonomy);
|
|
1772
|
+
return {
|
|
1773
|
+
id: issueId,
|
|
1774
|
+
name: metadata.name,
|
|
1775
|
+
frequency: count,
|
|
1776
|
+
severity: metadata.severity,
|
|
1777
|
+
examples: metadata.exampleBefore ? [metadata.exampleBefore] : [],
|
|
1778
|
+
suggestion: metadata.suggestion,
|
|
1779
|
+
beforeAfter: {
|
|
1780
|
+
before: metadata.exampleBefore ?? "Example not available",
|
|
1781
|
+
after: metadata.exampleAfter ?? metadata.suggestion
|
|
1782
|
+
}
|
|
1783
|
+
};
|
|
1784
|
+
});
|
|
1785
|
+
return {
|
|
1786
|
+
date,
|
|
1787
|
+
patterns,
|
|
1788
|
+
stats: {
|
|
1789
|
+
totalPrompts: 1,
|
|
1790
|
+
promptsWithIssues: minimal.issues.length > 0 ? 1 : 0,
|
|
1791
|
+
overallScore: minimal.score
|
|
1792
|
+
},
|
|
1793
|
+
topSuggestion: patterns.length > 0 && patterns[0] ? patterns[0].suggestion : "Your prompts look good!"
|
|
1794
|
+
};
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
// src/providers/base.ts
|
|
1798
|
+
function buildUserPrompt(prompts, date, context) {
|
|
1799
|
+
if (prompts.length === 0) {
|
|
1800
|
+
throw new Error("Cannot build prompt from empty array");
|
|
1801
|
+
}
|
|
1802
|
+
const promptsList = prompts.map((prompt, index) => `${String(index + 1)}. ${prompt}`).join("\n\n");
|
|
1803
|
+
const count = String(prompts.length);
|
|
1804
|
+
const plural = prompts.length === 1 ? "" : "s";
|
|
1805
|
+
let contextSection = "";
|
|
1806
|
+
if (context) {
|
|
1807
|
+
const contextParts = [];
|
|
1808
|
+
if (context.role) {
|
|
1809
|
+
contextParts.push(`Role: ${context.role}`);
|
|
1810
|
+
}
|
|
1811
|
+
if (context.projectType) {
|
|
1812
|
+
contextParts.push(`Project Type: ${context.projectType}`);
|
|
1813
|
+
}
|
|
1814
|
+
if (context.domain) {
|
|
1815
|
+
contextParts.push(`Domain: ${context.domain}`);
|
|
1816
|
+
}
|
|
1817
|
+
if (context.techStack && context.techStack.length > 0) {
|
|
1818
|
+
contextParts.push(`Tech Stack: ${context.techStack.join(", ")}`);
|
|
1819
|
+
}
|
|
1820
|
+
if (context.guidelines && context.guidelines.length > 0) {
|
|
1821
|
+
contextParts.push(
|
|
1822
|
+
`Guidelines:
|
|
1823
|
+
${context.guidelines.map((g) => `- ${g}`).join("\n")}`
|
|
1824
|
+
);
|
|
1825
|
+
}
|
|
1826
|
+
if (contextParts.length > 0) {
|
|
1827
|
+
contextSection = `
|
|
1828
|
+
|
|
1829
|
+
Project Context:
|
|
1830
|
+
${contextParts.join("\n")}
|
|
1831
|
+
`;
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
return `Analyze the following ${count} prompt${plural} from ${date}:${contextSection}
|
|
1835
|
+
|
|
1836
|
+
${promptsList}
|
|
1837
|
+
|
|
1838
|
+
Please provide your analysis as a JSON object following the specified schema.`;
|
|
1839
|
+
}
|
|
1840
|
+
function isValidMinimalResponse(value) {
|
|
1841
|
+
if (typeof value !== "object" || value === null) {
|
|
1842
|
+
return false;
|
|
1843
|
+
}
|
|
1844
|
+
const obj = value;
|
|
1845
|
+
if (!Array.isArray(obj["issues"])) {
|
|
1846
|
+
return false;
|
|
1847
|
+
}
|
|
1848
|
+
if (!obj["issues"].every((item) => typeof item === "string")) {
|
|
1849
|
+
return false;
|
|
1850
|
+
}
|
|
1851
|
+
return true;
|
|
1852
|
+
}
|
|
1853
|
+
function extractMinimalResult(obj) {
|
|
1854
|
+
const issues = obj["issues"];
|
|
1855
|
+
const score = typeof obj["score"] === "number" ? obj["score"] : 50;
|
|
1856
|
+
return { issues, score };
|
|
1857
|
+
}
|
|
1858
|
+
function issueToPattern(issue, index) {
|
|
1859
|
+
const id = issue.name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
1860
|
+
return {
|
|
1861
|
+
id: id || `issue-${String(index)}`,
|
|
1862
|
+
name: issue.name,
|
|
1863
|
+
frequency: 1,
|
|
1864
|
+
severity: "medium",
|
|
1865
|
+
examples: [issue.example],
|
|
1866
|
+
suggestion: issue.fix,
|
|
1867
|
+
beforeAfter: {
|
|
1868
|
+
before: issue.example,
|
|
1869
|
+
after: issue.fix
|
|
1870
|
+
}
|
|
1871
|
+
};
|
|
1872
|
+
}
|
|
1873
|
+
function tryFixTruncatedJson(json) {
|
|
1874
|
+
let fixed = json.trim();
|
|
1875
|
+
let openBraces = 0;
|
|
1876
|
+
let openBrackets = 0;
|
|
1877
|
+
let inString = false;
|
|
1878
|
+
let escapeNext = false;
|
|
1879
|
+
for (const char of fixed) {
|
|
1880
|
+
if (escapeNext) {
|
|
1881
|
+
escapeNext = false;
|
|
1882
|
+
continue;
|
|
1883
|
+
}
|
|
1884
|
+
if (char === "\\") {
|
|
1885
|
+
escapeNext = true;
|
|
1886
|
+
continue;
|
|
1887
|
+
}
|
|
1888
|
+
if (char === '"') {
|
|
1889
|
+
inString = !inString;
|
|
1890
|
+
continue;
|
|
1891
|
+
}
|
|
1892
|
+
if (inString) continue;
|
|
1893
|
+
if (char === "{") openBraces++;
|
|
1894
|
+
if (char === "}") openBraces--;
|
|
1895
|
+
if (char === "[") openBrackets++;
|
|
1896
|
+
if (char === "]") openBrackets--;
|
|
1897
|
+
}
|
|
1898
|
+
if (inString) {
|
|
1899
|
+
fixed += '"';
|
|
1900
|
+
}
|
|
1901
|
+
fixed = fixed.replace(/,\s*$/, "");
|
|
1902
|
+
while (openBrackets > 0) {
|
|
1903
|
+
fixed += "]";
|
|
1904
|
+
openBrackets--;
|
|
1905
|
+
}
|
|
1906
|
+
while (openBraces > 0) {
|
|
1907
|
+
fixed += "}";
|
|
1908
|
+
openBraces--;
|
|
1909
|
+
}
|
|
1910
|
+
return fixed;
|
|
1911
|
+
}
|
|
1912
|
+
function parseResponse(response, date, taxonomy = ISSUE_TAXONOMY) {
|
|
1913
|
+
const codeBlockRegex = /```(?:json)?\s*\n?([\s\S]*?)\n?```/;
|
|
1914
|
+
const jsonMatch = codeBlockRegex.exec(response);
|
|
1915
|
+
let jsonString = jsonMatch?.[1]?.trim() ?? response.trim();
|
|
1916
|
+
let parsed;
|
|
1917
|
+
try {
|
|
1918
|
+
parsed = JSON.parse(jsonString);
|
|
1919
|
+
} catch {
|
|
1920
|
+
jsonString = tryFixTruncatedJson(jsonString);
|
|
1921
|
+
try {
|
|
1922
|
+
parsed = JSON.parse(jsonString);
|
|
1923
|
+
} catch (error) {
|
|
1924
|
+
throw new Error(
|
|
1925
|
+
`Failed to parse response as JSON: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
1926
|
+
);
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
if (isValidMinimalResponse(parsed)) {
|
|
1930
|
+
const minimal = extractMinimalResult(parsed);
|
|
1931
|
+
return convertMinimalToAnalysisResult(minimal, date, taxonomy);
|
|
1932
|
+
}
|
|
1933
|
+
if (isValidSimpleResponse(parsed)) {
|
|
1934
|
+
const simple = extractSimpleResponse(parsed);
|
|
1935
|
+
const patterns = simple.issues.map((issue, i) => issueToPattern(issue, i));
|
|
1936
|
+
return {
|
|
1937
|
+
date,
|
|
1938
|
+
patterns,
|
|
1939
|
+
stats: {
|
|
1940
|
+
totalPrompts: 0,
|
|
1941
|
+
// Will be filled by caller
|
|
1942
|
+
promptsWithIssues: simple.issues.length,
|
|
1943
|
+
overallScore: simple.score
|
|
1944
|
+
},
|
|
1945
|
+
topSuggestion: simple.tip
|
|
1946
|
+
};
|
|
1947
|
+
}
|
|
1948
|
+
if (isValidFullResponse(parsed)) {
|
|
1949
|
+
return {
|
|
1950
|
+
date,
|
|
1951
|
+
patterns: parsed.patterns,
|
|
1952
|
+
stats: parsed.stats,
|
|
1953
|
+
topSuggestion: parsed.topSuggestion
|
|
1954
|
+
};
|
|
1955
|
+
}
|
|
1956
|
+
throw new Error("Response does not match expected schema");
|
|
1957
|
+
}
|
|
1958
|
+
function isValidSimpleResponse(value) {
|
|
1959
|
+
if (typeof value !== "object" || value === null) {
|
|
1960
|
+
return false;
|
|
1961
|
+
}
|
|
1962
|
+
const obj = value;
|
|
1963
|
+
if (!Array.isArray(obj["issues"])) {
|
|
1964
|
+
return false;
|
|
1965
|
+
}
|
|
1966
|
+
for (const issue of obj["issues"]) {
|
|
1967
|
+
if (typeof issue !== "object" || issue === null) {
|
|
1968
|
+
return false;
|
|
1969
|
+
}
|
|
1970
|
+
const i = issue;
|
|
1971
|
+
if (typeof i["name"] !== "string" || typeof i["example"] !== "string" || typeof i["fix"] !== "string") {
|
|
1972
|
+
return false;
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
return true;
|
|
1976
|
+
}
|
|
1977
|
+
function extractSimpleResponse(obj) {
|
|
1978
|
+
const issues = obj["issues"];
|
|
1979
|
+
const score = typeof obj["score"] === "number" ? obj["score"] : 50;
|
|
1980
|
+
const tip = typeof obj["tip"] === "string" ? obj["tip"] : issues.length > 0 && issues[0] ? issues[0].fix : "No suggestions";
|
|
1981
|
+
return { issues, score, tip };
|
|
1982
|
+
}
|
|
1983
|
+
function isValidFullResponse(value) {
|
|
1984
|
+
if (typeof value !== "object" || value === null) {
|
|
1985
|
+
return false;
|
|
1986
|
+
}
|
|
1987
|
+
const obj = value;
|
|
1988
|
+
if (!Array.isArray(obj["patterns"])) {
|
|
1989
|
+
return false;
|
|
1990
|
+
}
|
|
1991
|
+
for (const pattern of obj["patterns"]) {
|
|
1992
|
+
if (!isValidPattern(pattern)) {
|
|
1993
|
+
return false;
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
if (typeof obj["stats"] !== "object" || obj["stats"] === null) {
|
|
1997
|
+
return false;
|
|
1998
|
+
}
|
|
1999
|
+
const stats = obj["stats"];
|
|
2000
|
+
if (typeof stats["totalPrompts"] !== "number" || typeof stats["promptsWithIssues"] !== "number" || typeof stats["overallScore"] !== "number") {
|
|
2001
|
+
return false;
|
|
2002
|
+
}
|
|
2003
|
+
if (typeof obj["topSuggestion"] !== "string") {
|
|
2004
|
+
return false;
|
|
2005
|
+
}
|
|
2006
|
+
return true;
|
|
2007
|
+
}
|
|
2008
|
+
function isValidPattern(value) {
|
|
2009
|
+
if (typeof value !== "object" || value === null) {
|
|
2010
|
+
return false;
|
|
2011
|
+
}
|
|
2012
|
+
const obj = value;
|
|
2013
|
+
if (typeof obj["id"] !== "string" || typeof obj["name"] !== "string" || typeof obj["frequency"] !== "number" || typeof obj["suggestion"] !== "string") {
|
|
2014
|
+
return false;
|
|
2015
|
+
}
|
|
2016
|
+
if (obj["severity"] !== "low" && obj["severity"] !== "medium" && obj["severity"] !== "high") {
|
|
2017
|
+
return false;
|
|
2018
|
+
}
|
|
2019
|
+
if (!Array.isArray(obj["examples"]) || !obj["examples"].every((e) => typeof e === "string")) {
|
|
2020
|
+
return false;
|
|
2021
|
+
}
|
|
2022
|
+
if (typeof obj["beforeAfter"] !== "object" || obj["beforeAfter"] === null) {
|
|
2023
|
+
return false;
|
|
2024
|
+
}
|
|
2025
|
+
const beforeAfter = obj["beforeAfter"];
|
|
2026
|
+
if (typeof beforeAfter["before"] !== "string" || typeof beforeAfter["after"] !== "string") {
|
|
2027
|
+
return false;
|
|
2028
|
+
}
|
|
2029
|
+
return true;
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
// src/providers/ollama.ts
|
|
2033
|
+
var MAX_RETRIES = 2;
|
|
2034
|
+
var AVAILABILITY_TIMEOUT_MS = 3e3;
|
|
2035
|
+
var ANALYSIS_TIMEOUT_MS = 6e4;
|
|
2036
|
+
var BASE_RETRY_DELAY_MS = 1e3;
|
|
2037
|
+
var MODEL_STRATEGY_MAP = {
|
|
2038
|
+
// Micro (< 4GB)
|
|
2039
|
+
"llama3.2": "micro",
|
|
2040
|
+
"phi3:mini": "micro",
|
|
2041
|
+
"gemma3:4b": "micro",
|
|
2042
|
+
"gemma2:2b": "micro",
|
|
2043
|
+
// Small (4-7GB)
|
|
2044
|
+
"mistral:7b": "small",
|
|
2045
|
+
"llama3:8b": "small",
|
|
2046
|
+
"codellama:7b": "small",
|
|
2047
|
+
// Standard (> 7GB)
|
|
2048
|
+
"llama3:70b": "standard",
|
|
2049
|
+
mixtral: "standard",
|
|
2050
|
+
"qwen2.5:14b": "standard"
|
|
2051
|
+
};
|
|
2052
|
+
function detectBatchStrategy(modelName) {
|
|
2053
|
+
if (MODEL_STRATEGY_MAP[modelName]) {
|
|
2054
|
+
return MODEL_STRATEGY_MAP[modelName];
|
|
2055
|
+
}
|
|
2056
|
+
for (const [pattern, strategy] of Object.entries(MODEL_STRATEGY_MAP)) {
|
|
2057
|
+
if (modelName.includes(pattern)) {
|
|
2058
|
+
return strategy;
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
return "micro";
|
|
2062
|
+
}
|
|
2063
|
+
var OllamaProvider = class {
|
|
2064
|
+
name = "Ollama";
|
|
2065
|
+
config;
|
|
2066
|
+
batchStrategy;
|
|
2067
|
+
schemaType;
|
|
2068
|
+
/**
|
|
2069
|
+
* Creates a new OllamaProvider instance.
|
|
2070
|
+
* Automatically detects the optimal batch strategy and schema type based on model name.
|
|
2071
|
+
*
|
|
2072
|
+
* @param config - Ollama configuration with model and host
|
|
2073
|
+
*/
|
|
2074
|
+
constructor(config) {
|
|
2075
|
+
this.config = config;
|
|
2076
|
+
this.batchStrategy = detectBatchStrategy(config.model);
|
|
2077
|
+
this.schemaType = this.selectSchemaType();
|
|
2078
|
+
const strategy = BATCH_STRATEGIES[this.batchStrategy];
|
|
2079
|
+
logger.debug(
|
|
2080
|
+
`Detected batch strategy: ${this.batchStrategy} (${strategy.description}), schema type: ${this.schemaType}`,
|
|
2081
|
+
"ollama"
|
|
2082
|
+
);
|
|
2083
|
+
}
|
|
2084
|
+
/**
|
|
2085
|
+
* Selects the appropriate schema type based on model size.
|
|
2086
|
+
* Micro and small models use minimal schema for better reliability.
|
|
2087
|
+
* Standard models use full schema for detailed analysis.
|
|
2088
|
+
*
|
|
2089
|
+
* @returns Schema type identifier
|
|
2090
|
+
*/
|
|
2091
|
+
selectSchemaType() {
|
|
2092
|
+
return ["micro", "small"].includes(this.batchStrategy) ? "minimal" : "full";
|
|
2093
|
+
}
|
|
2094
|
+
/**
|
|
2095
|
+
* Returns dynamic batch limits based on detected model strategy.
|
|
2096
|
+
*
|
|
2097
|
+
* @returns Provider limits with model-specific constraints
|
|
2098
|
+
*/
|
|
2099
|
+
getBatchLimits() {
|
|
2100
|
+
const strategy = BATCH_STRATEGIES[this.batchStrategy];
|
|
2101
|
+
return {
|
|
2102
|
+
maxTokensPerBatch: strategy.maxTokensPerBatch,
|
|
2103
|
+
maxPromptsPerBatch: strategy.maxPromptsPerBatch,
|
|
2104
|
+
prioritization: "longest-first"
|
|
2105
|
+
};
|
|
2106
|
+
}
|
|
2107
|
+
/**
|
|
2108
|
+
* Checks if the Ollama service is available and has the required model.
|
|
2109
|
+
* Uses a 3-second timeout to avoid hanging on unreachable services.
|
|
2110
|
+
*
|
|
2111
|
+
* @returns Promise that resolves to true if available, false otherwise
|
|
2112
|
+
*/
|
|
2113
|
+
async isAvailable() {
|
|
2114
|
+
logger.debug(`Connecting to Ollama at ${this.config.host}`, "ollama");
|
|
2115
|
+
try {
|
|
2116
|
+
const controller = new AbortController();
|
|
2117
|
+
const timeoutId = setTimeout(() => {
|
|
2118
|
+
controller.abort();
|
|
2119
|
+
}, AVAILABILITY_TIMEOUT_MS);
|
|
2120
|
+
const response = await fetch(`${this.config.host}/api/tags`, {
|
|
2121
|
+
signal: controller.signal
|
|
2122
|
+
});
|
|
2123
|
+
clearTimeout(timeoutId);
|
|
2124
|
+
if (!response.ok) {
|
|
2125
|
+
logger.debug(
|
|
2126
|
+
`Ollama API returned ${String(response.status)}`,
|
|
2127
|
+
"ollama"
|
|
2128
|
+
);
|
|
2129
|
+
return false;
|
|
2130
|
+
}
|
|
2131
|
+
const data = await response.json();
|
|
2132
|
+
if (!data.models || !Array.isArray(data.models)) {
|
|
2133
|
+
logger.debug("Ollama returned invalid model list", "ollama");
|
|
2134
|
+
return false;
|
|
2135
|
+
}
|
|
2136
|
+
const modelAvailable = data.models.some(
|
|
2137
|
+
(model) => model.name.includes(this.config.model)
|
|
2138
|
+
);
|
|
2139
|
+
if (modelAvailable) {
|
|
2140
|
+
logger.debug(
|
|
2141
|
+
`Model ${this.config.model} available (${String(data.models.length)} models found)`,
|
|
2142
|
+
"ollama"
|
|
2143
|
+
);
|
|
2144
|
+
} else {
|
|
2145
|
+
logger.debug(
|
|
2146
|
+
`Model ${this.config.model} not found in available models`,
|
|
2147
|
+
"ollama"
|
|
2148
|
+
);
|
|
2149
|
+
}
|
|
2150
|
+
return modelAvailable;
|
|
2151
|
+
} catch {
|
|
2152
|
+
logger.debug("Ollama connection failed", "ollama");
|
|
2153
|
+
return false;
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
/**
|
|
2157
|
+
* Analyzes prompts using the Ollama service.
|
|
2158
|
+
* Implements retry logic with exponential backoff for network errors.
|
|
2159
|
+
*
|
|
2160
|
+
* @param prompts - Array of prompt strings to analyze
|
|
2161
|
+
* @param date - Date context for the analysis
|
|
2162
|
+
* @param context - Optional project context for analysis
|
|
2163
|
+
* @returns Promise resolving to AnalysisResult
|
|
2164
|
+
* @throws Error if analysis fails after retries or if response is invalid
|
|
2165
|
+
*/
|
|
2166
|
+
async analyze(prompts, date, context) {
|
|
2167
|
+
if (prompts.length === 0) {
|
|
2168
|
+
throw new Error("Cannot analyze empty prompts array");
|
|
2169
|
+
}
|
|
2170
|
+
const userPrompt = buildUserPrompt(prompts, date, context);
|
|
2171
|
+
const systemPrompt = this.schemaType === "minimal" ? SYSTEM_PROMPT_MINIMAL : SYSTEM_PROMPT_FULL;
|
|
2172
|
+
let lastError;
|
|
2173
|
+
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
|
|
2174
|
+
try {
|
|
2175
|
+
const controller = new AbortController();
|
|
2176
|
+
const timeoutId = setTimeout(() => {
|
|
2177
|
+
controller.abort();
|
|
2178
|
+
}, ANALYSIS_TIMEOUT_MS);
|
|
2179
|
+
const response = await fetch(`${this.config.host}/api/generate`, {
|
|
2180
|
+
method: "POST",
|
|
2181
|
+
headers: {
|
|
2182
|
+
"Content-Type": "application/json"
|
|
2183
|
+
},
|
|
2184
|
+
body: JSON.stringify({
|
|
2185
|
+
model: this.config.model,
|
|
2186
|
+
prompt: userPrompt,
|
|
2187
|
+
system: systemPrompt,
|
|
2188
|
+
stream: false,
|
|
2189
|
+
format: "json",
|
|
2190
|
+
options: {
|
|
2191
|
+
temperature: 0.3
|
|
2192
|
+
}
|
|
2193
|
+
}),
|
|
2194
|
+
signal: controller.signal
|
|
2195
|
+
});
|
|
2196
|
+
clearTimeout(timeoutId);
|
|
2197
|
+
if (!response.ok) {
|
|
2198
|
+
const status = String(response.status);
|
|
2199
|
+
throw new Error(
|
|
2200
|
+
`Ollama API request failed: ${status} ${response.statusText}`
|
|
2201
|
+
);
|
|
2202
|
+
}
|
|
2203
|
+
const data = await response.json();
|
|
2204
|
+
if (typeof data.response !== "string") {
|
|
2205
|
+
throw new Error("Invalid response format from Ollama API");
|
|
2206
|
+
}
|
|
2207
|
+
return parseResponse(data.response, date);
|
|
2208
|
+
} catch (error) {
|
|
2209
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
2210
|
+
if (lastError.message.includes("parse") || lastError.message.includes("schema")) {
|
|
2211
|
+
throw lastError;
|
|
2212
|
+
}
|
|
2213
|
+
if (attempt === MAX_RETRIES) {
|
|
2214
|
+
break;
|
|
2215
|
+
}
|
|
2216
|
+
const delay = BASE_RETRY_DELAY_MS * Math.pow(2, attempt);
|
|
2217
|
+
logger.debug(
|
|
2218
|
+
`Retry attempt ${String(attempt + 1)}/${String(MAX_RETRIES)}, waiting ${String(delay)}ms`,
|
|
2219
|
+
"ollama"
|
|
2220
|
+
);
|
|
2221
|
+
await sleep(delay);
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
const attempts = String(MAX_RETRIES + 1);
|
|
2225
|
+
throw new Error(
|
|
2226
|
+
`Ollama analysis failed after ${attempts} attempts: ${lastError?.message ?? "Unknown error"}`
|
|
2227
|
+
);
|
|
2228
|
+
}
|
|
2229
|
+
};
|
|
2230
|
+
function sleep(ms) {
|
|
2231
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
// src/utils/rate-limiter.ts
|
|
2235
|
+
function createRateLimiter(options) {
|
|
2236
|
+
const minIntervalMs = 6e4 / options.requestsPerMinute;
|
|
2237
|
+
let lastRequestTime = 0;
|
|
2238
|
+
async function throttle(fn) {
|
|
2239
|
+
const now = Date.now();
|
|
2240
|
+
const timeSinceLastRequest = now - lastRequestTime;
|
|
2241
|
+
if (lastRequestTime > 0 && timeSinceLastRequest < minIntervalMs) {
|
|
2242
|
+
const waitTime = minIntervalMs - timeSinceLastRequest;
|
|
2243
|
+
await new Promise((resolve) => setTimeout(resolve, waitTime));
|
|
2244
|
+
}
|
|
2245
|
+
lastRequestTime = Date.now();
|
|
2246
|
+
return fn();
|
|
2247
|
+
}
|
|
2248
|
+
function reset() {
|
|
2249
|
+
lastRequestTime = 0;
|
|
2250
|
+
}
|
|
2251
|
+
return { throttle, reset };
|
|
2252
|
+
}
|
|
2253
|
+
var DEFAULT_RATE_LIMITS = {
|
|
2254
|
+
anthropic: 50,
|
|
2255
|
+
google: 50
|
|
2256
|
+
};
|
|
2257
|
+
|
|
2258
|
+
// src/utils/retry.ts
|
|
2259
|
+
var DEFAULT_OPTIONS = {
|
|
2260
|
+
maxRetries: 3,
|
|
2261
|
+
baseDelayMs: 1e3,
|
|
2262
|
+
maxDelayMs: 3e4,
|
|
2263
|
+
isRetryable: isTransientError
|
|
2264
|
+
};
|
|
2265
|
+
function isTransientError(error) {
|
|
2266
|
+
if (!(error instanceof Error)) {
|
|
2267
|
+
return false;
|
|
2268
|
+
}
|
|
2269
|
+
const message = error.message.toLowerCase();
|
|
2270
|
+
if (message.includes("network") || message.includes("timeout") || message.includes("econnreset") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("abort")) {
|
|
2271
|
+
return true;
|
|
2272
|
+
}
|
|
2273
|
+
if (message.includes("500") || message.includes("502") || message.includes("503") || message.includes("504")) {
|
|
2274
|
+
return true;
|
|
2275
|
+
}
|
|
2276
|
+
if (message.includes("429")) {
|
|
2277
|
+
return true;
|
|
2278
|
+
}
|
|
2279
|
+
if (message.includes("401") || message.includes("403")) {
|
|
2280
|
+
return false;
|
|
2281
|
+
}
|
|
2282
|
+
if (message.includes("parse") || message.includes("schema")) {
|
|
2283
|
+
return false;
|
|
2284
|
+
}
|
|
2285
|
+
return false;
|
|
2286
|
+
}
|
|
2287
|
+
function sleep2(ms) {
|
|
2288
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
2289
|
+
}
|
|
2290
|
+
async function withRetry(fn, options = {}) {
|
|
2291
|
+
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
2292
|
+
let lastError;
|
|
2293
|
+
for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
|
|
2294
|
+
try {
|
|
2295
|
+
return await fn();
|
|
2296
|
+
} catch (error) {
|
|
2297
|
+
lastError = error;
|
|
2298
|
+
const isLastAttempt = attempt === opts.maxRetries;
|
|
2299
|
+
const shouldRetry = opts.isRetryable(error);
|
|
2300
|
+
if (isLastAttempt || !shouldRetry) {
|
|
2301
|
+
throw error;
|
|
2302
|
+
}
|
|
2303
|
+
const delay = Math.min(
|
|
2304
|
+
opts.baseDelayMs * Math.pow(2, attempt),
|
|
2305
|
+
opts.maxDelayMs
|
|
2306
|
+
);
|
|
2307
|
+
await sleep2(delay);
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
throw lastError;
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
// src/providers/anthropic.ts
|
|
2314
|
+
var MAX_RETRIES2 = 3;
|
|
2315
|
+
var AVAILABILITY_TIMEOUT_MS2 = 5e3;
|
|
2316
|
+
var ANALYSIS_TIMEOUT_MS2 = 6e4;
|
|
2317
|
+
var BASE_DELAY_MS = 1e3;
|
|
2318
|
+
var MAX_DELAY_MS = 3e4;
|
|
2319
|
+
var API_BASE_URL = "https://api.anthropic.com";
|
|
2320
|
+
var API_VERSION = "2023-06-01";
|
|
2321
|
+
var MAX_TOKENS = 4096;
|
|
2322
|
+
var AnthropicProvider = class {
|
|
2323
|
+
name = "Anthropic";
|
|
2324
|
+
config;
|
|
2325
|
+
rateLimiter;
|
|
2326
|
+
/**
|
|
2327
|
+
* Creates a new AnthropicProvider instance.
|
|
2328
|
+
*
|
|
2329
|
+
* @param config - Anthropic configuration with model and API key
|
|
2330
|
+
*/
|
|
2331
|
+
constructor(config) {
|
|
2332
|
+
this.config = config;
|
|
2333
|
+
this.rateLimiter = createRateLimiter({
|
|
2334
|
+
requestsPerMinute: DEFAULT_RATE_LIMITS.anthropic
|
|
2335
|
+
});
|
|
2336
|
+
}
|
|
2337
|
+
/**
|
|
2338
|
+
* Checks if the Anthropic API is available by validating the API key.
|
|
2339
|
+
* Makes a minimal request to verify the key is valid.
|
|
2340
|
+
*
|
|
2341
|
+
* @returns Promise that resolves to true if available, false otherwise
|
|
2342
|
+
*/
|
|
2343
|
+
async isAvailable() {
|
|
2344
|
+
if (!this.config.apiKey) {
|
|
2345
|
+
logger.debug("No API key configured", "anthropic");
|
|
2346
|
+
return false;
|
|
2347
|
+
}
|
|
2348
|
+
logger.debug(
|
|
2349
|
+
`Validating API key with model ${this.config.model}`,
|
|
2350
|
+
"anthropic"
|
|
2351
|
+
);
|
|
2352
|
+
try {
|
|
2353
|
+
const controller = new AbortController();
|
|
2354
|
+
const timeoutId = setTimeout(() => {
|
|
2355
|
+
controller.abort();
|
|
2356
|
+
}, AVAILABILITY_TIMEOUT_MS2);
|
|
2357
|
+
const response = await fetch(`${API_BASE_URL}/v1/messages`, {
|
|
2358
|
+
method: "POST",
|
|
2359
|
+
headers: {
|
|
2360
|
+
"Content-Type": "application/json",
|
|
2361
|
+
"x-api-key": this.config.apiKey,
|
|
2362
|
+
"anthropic-version": API_VERSION
|
|
2363
|
+
},
|
|
2364
|
+
body: JSON.stringify({
|
|
2365
|
+
model: this.config.model,
|
|
2366
|
+
max_tokens: 1,
|
|
2367
|
+
messages: [{ role: "user", content: "Hi" }]
|
|
2368
|
+
}),
|
|
2369
|
+
signal: controller.signal
|
|
2370
|
+
});
|
|
2371
|
+
clearTimeout(timeoutId);
|
|
2372
|
+
const isAvailable = response.ok || response.status === 429 || response.status === 400;
|
|
2373
|
+
logger.debug(
|
|
2374
|
+
`API responded with status ${String(response.status)} (available: ${String(isAvailable)})`,
|
|
2375
|
+
"anthropic"
|
|
2376
|
+
);
|
|
2377
|
+
return isAvailable;
|
|
2378
|
+
} catch {
|
|
2379
|
+
logger.debug("Connection failed", "anthropic");
|
|
2380
|
+
return false;
|
|
2381
|
+
}
|
|
2382
|
+
}
|
|
2383
|
+
/**
|
|
2384
|
+
* Analyzes prompts using the Anthropic Claude API.
|
|
2385
|
+
* Implements retry logic with exponential backoff for network errors.
|
|
2386
|
+
* Rate limited to prevent 429 errors during batch processing.
|
|
2387
|
+
*
|
|
2388
|
+
* @param prompts - Array of prompt strings to analyze
|
|
2389
|
+
* @param date - Date context for the analysis
|
|
2390
|
+
* @param context - Optional project context for analysis
|
|
2391
|
+
* @returns Promise resolving to AnalysisResult
|
|
2392
|
+
* @throws Error if analysis fails after retries or if response is invalid
|
|
2393
|
+
*/
|
|
2394
|
+
async analyze(prompts, date, context) {
|
|
2395
|
+
if (prompts.length === 0) {
|
|
2396
|
+
throw new Error("Cannot analyze empty prompts array");
|
|
2397
|
+
}
|
|
2398
|
+
const userPrompt = buildUserPrompt(prompts, date, context);
|
|
2399
|
+
return this.rateLimiter.throttle(
|
|
2400
|
+
() => withRetry(
|
|
2401
|
+
async () => {
|
|
2402
|
+
const controller = new AbortController();
|
|
2403
|
+
const timeoutId = setTimeout(() => {
|
|
2404
|
+
controller.abort();
|
|
2405
|
+
}, ANALYSIS_TIMEOUT_MS2);
|
|
2406
|
+
try {
|
|
2407
|
+
const response = await fetch(`${API_BASE_URL}/v1/messages`, {
|
|
2408
|
+
method: "POST",
|
|
2409
|
+
headers: {
|
|
2410
|
+
"Content-Type": "application/json",
|
|
2411
|
+
"x-api-key": this.config.apiKey,
|
|
2412
|
+
"anthropic-version": API_VERSION
|
|
2413
|
+
},
|
|
2414
|
+
body: JSON.stringify({
|
|
2415
|
+
model: this.config.model,
|
|
2416
|
+
max_tokens: MAX_TOKENS,
|
|
2417
|
+
system: SYSTEM_PROMPT_FULL,
|
|
2418
|
+
messages: [{ role: "user", content: userPrompt }]
|
|
2419
|
+
}),
|
|
2420
|
+
signal: controller.signal
|
|
2421
|
+
});
|
|
2422
|
+
if (!response.ok) {
|
|
2423
|
+
const status = String(response.status);
|
|
2424
|
+
const errorBody = await response.text();
|
|
2425
|
+
throw new Error(
|
|
2426
|
+
`Anthropic API request failed: ${status} ${response.statusText} - ${errorBody}`
|
|
2427
|
+
);
|
|
2428
|
+
}
|
|
2429
|
+
const data = await response.json();
|
|
2430
|
+
const textContent = data.content?.find(
|
|
2431
|
+
(block) => block.type === "text"
|
|
2432
|
+
);
|
|
2433
|
+
if (!textContent?.text) {
|
|
2434
|
+
throw new Error("Invalid response format from Anthropic API");
|
|
2435
|
+
}
|
|
2436
|
+
return parseResponse(textContent.text, date);
|
|
2437
|
+
} finally {
|
|
2438
|
+
clearTimeout(timeoutId);
|
|
2439
|
+
}
|
|
2440
|
+
},
|
|
2441
|
+
{
|
|
2442
|
+
maxRetries: MAX_RETRIES2,
|
|
2443
|
+
baseDelayMs: BASE_DELAY_MS,
|
|
2444
|
+
maxDelayMs: MAX_DELAY_MS,
|
|
2445
|
+
isRetryable: isTransientError
|
|
2446
|
+
}
|
|
2447
|
+
)
|
|
2448
|
+
);
|
|
2449
|
+
}
|
|
2450
|
+
};
|
|
2451
|
+
|
|
2452
|
+
// src/providers/google.ts
|
|
2453
|
+
var MAX_RETRIES3 = 3;
|
|
2454
|
+
var AVAILABILITY_TIMEOUT_MS3 = 5e3;
|
|
2455
|
+
var ANALYSIS_TIMEOUT_MS3 = 6e4;
|
|
2456
|
+
var BASE_DELAY_MS2 = 1e3;
|
|
2457
|
+
var MAX_DELAY_MS2 = 3e4;
|
|
2458
|
+
var API_BASE_URL2 = "https://generativelanguage.googleapis.com/v1beta";
|
|
2459
|
+
var GoogleProvider = class {
|
|
2460
|
+
name = "Google";
|
|
2461
|
+
config;
|
|
2462
|
+
rateLimiter;
|
|
2463
|
+
/**
|
|
2464
|
+
* Creates a new GoogleProvider instance.
|
|
2465
|
+
*
|
|
2466
|
+
* @param config - Google configuration with model and API key
|
|
2467
|
+
*/
|
|
2468
|
+
constructor(config) {
|
|
2469
|
+
this.config = config;
|
|
2470
|
+
this.rateLimiter = createRateLimiter({
|
|
2471
|
+
requestsPerMinute: DEFAULT_RATE_LIMITS.google
|
|
2472
|
+
});
|
|
2473
|
+
}
|
|
2474
|
+
/**
|
|
2475
|
+
* Checks if the Google Gemini API is available by validating the API key.
|
|
2476
|
+
* Makes a minimal request to verify the key is valid.
|
|
2477
|
+
*
|
|
2478
|
+
* @returns Promise that resolves to true if available, false otherwise
|
|
2479
|
+
*/
|
|
2480
|
+
async isAvailable() {
|
|
2481
|
+
if (!this.config.apiKey) {
|
|
2482
|
+
logger.debug("No API key configured", "google");
|
|
2483
|
+
return false;
|
|
2484
|
+
}
|
|
2485
|
+
logger.debug(
|
|
2486
|
+
`Validating API key with model ${this.config.model}`,
|
|
2487
|
+
"google"
|
|
2488
|
+
);
|
|
2489
|
+
try {
|
|
2490
|
+
const controller = new AbortController();
|
|
2491
|
+
const timeoutId = setTimeout(() => {
|
|
2492
|
+
controller.abort();
|
|
2493
|
+
}, AVAILABILITY_TIMEOUT_MS3);
|
|
2494
|
+
const url = `${API_BASE_URL2}/models/${this.config.model}:generateContent?key=${this.config.apiKey}`;
|
|
2495
|
+
const response = await fetch(url, {
|
|
2496
|
+
method: "POST",
|
|
2497
|
+
headers: {
|
|
2498
|
+
"Content-Type": "application/json"
|
|
2499
|
+
},
|
|
2500
|
+
body: JSON.stringify({
|
|
2501
|
+
contents: [
|
|
2502
|
+
{
|
|
2503
|
+
role: "user",
|
|
2504
|
+
parts: [{ text: "Hi" }]
|
|
2505
|
+
}
|
|
2506
|
+
],
|
|
2507
|
+
generationConfig: {
|
|
2508
|
+
maxOutputTokens: 1
|
|
2509
|
+
}
|
|
2510
|
+
}),
|
|
2511
|
+
signal: controller.signal
|
|
2512
|
+
});
|
|
2513
|
+
clearTimeout(timeoutId);
|
|
2514
|
+
const isAvailable = response.ok || response.status === 429 || response.status === 400;
|
|
2515
|
+
logger.debug(
|
|
2516
|
+
`API responded with status ${String(response.status)} (available: ${String(isAvailable)})`,
|
|
2517
|
+
"google"
|
|
2518
|
+
);
|
|
2519
|
+
return isAvailable;
|
|
2520
|
+
} catch {
|
|
2521
|
+
logger.debug("Connection failed", "google");
|
|
2522
|
+
return false;
|
|
2523
|
+
}
|
|
2524
|
+
}
|
|
2525
|
+
/**
|
|
2526
|
+
* Analyzes prompts using the Google Gemini API.
|
|
2527
|
+
* Implements retry logic with exponential backoff for network errors.
|
|
2528
|
+
* Rate limited to prevent 429 errors during batch processing.
|
|
2529
|
+
*
|
|
2530
|
+
* @param prompts - Array of prompt strings to analyze
|
|
2531
|
+
* @param date - Date context for the analysis
|
|
2532
|
+
* @param context - Optional project context for analysis
|
|
2533
|
+
* @returns Promise resolving to AnalysisResult
|
|
2534
|
+
* @throws Error if analysis fails after retries or if response is invalid
|
|
2535
|
+
*/
|
|
2536
|
+
async analyze(prompts, date, context) {
|
|
2537
|
+
if (prompts.length === 0) {
|
|
2538
|
+
throw new Error("Cannot analyze empty prompts array");
|
|
2539
|
+
}
|
|
2540
|
+
const userPrompt = buildUserPrompt(prompts, date, context);
|
|
2541
|
+
return this.rateLimiter.throttle(
|
|
2542
|
+
() => withRetry(
|
|
2543
|
+
async () => {
|
|
2544
|
+
const controller = new AbortController();
|
|
2545
|
+
const timeoutId = setTimeout(() => {
|
|
2546
|
+
controller.abort();
|
|
2547
|
+
}, ANALYSIS_TIMEOUT_MS3);
|
|
2548
|
+
try {
|
|
2549
|
+
const url = `${API_BASE_URL2}/models/${this.config.model}:generateContent?key=${this.config.apiKey}`;
|
|
2550
|
+
const response = await fetch(url, {
|
|
2551
|
+
method: "POST",
|
|
2552
|
+
headers: {
|
|
2553
|
+
"Content-Type": "application/json"
|
|
2554
|
+
},
|
|
2555
|
+
body: JSON.stringify({
|
|
2556
|
+
contents: [
|
|
2557
|
+
{
|
|
2558
|
+
role: "user",
|
|
2559
|
+
parts: [{ text: userPrompt }]
|
|
2560
|
+
}
|
|
2561
|
+
],
|
|
2562
|
+
systemInstruction: {
|
|
2563
|
+
parts: [{ text: SYSTEM_PROMPT_FULL }]
|
|
2564
|
+
},
|
|
2565
|
+
generationConfig: {
|
|
2566
|
+
responseMimeType: "application/json"
|
|
2567
|
+
}
|
|
2568
|
+
}),
|
|
2569
|
+
signal: controller.signal
|
|
2570
|
+
});
|
|
2571
|
+
if (!response.ok) {
|
|
2572
|
+
const status = String(response.status);
|
|
2573
|
+
const errorBody = await response.text();
|
|
2574
|
+
throw new Error(
|
|
2575
|
+
`Google API request failed: ${status} ${response.statusText} - ${errorBody}`
|
|
2576
|
+
);
|
|
2577
|
+
}
|
|
2578
|
+
const data = await response.json();
|
|
2579
|
+
const textContent = data.candidates?.[0]?.content?.parts?.[0]?.text;
|
|
2580
|
+
if (!textContent) {
|
|
2581
|
+
throw new Error("Invalid response format from Google API");
|
|
2582
|
+
}
|
|
2583
|
+
return parseResponse(textContent, date);
|
|
2584
|
+
} finally {
|
|
2585
|
+
clearTimeout(timeoutId);
|
|
2586
|
+
}
|
|
2587
|
+
},
|
|
2588
|
+
{
|
|
2589
|
+
maxRetries: MAX_RETRIES3,
|
|
2590
|
+
baseDelayMs: BASE_DELAY_MS2,
|
|
2591
|
+
maxDelayMs: MAX_DELAY_MS2,
|
|
2592
|
+
isRetryable: isTransientError
|
|
2593
|
+
}
|
|
2594
|
+
)
|
|
2595
|
+
);
|
|
2596
|
+
}
|
|
2597
|
+
};
|
|
2598
|
+
|
|
2599
|
+
// src/providers/index.ts
|
|
2600
|
+
function createProvider(type, config) {
|
|
2601
|
+
switch (type) {
|
|
2602
|
+
case "ollama":
|
|
2603
|
+
return new OllamaProvider(config.ollama);
|
|
2604
|
+
case "anthropic":
|
|
2605
|
+
return new AnthropicProvider(config.anthropic);
|
|
2606
|
+
case "google":
|
|
2607
|
+
return new GoogleProvider(config.google);
|
|
2608
|
+
}
|
|
2609
|
+
}
|
|
2610
|
+
async function getAvailableProvider(config, onFallback) {
|
|
2611
|
+
if (config.services.length === 0) {
|
|
2612
|
+
throw new Error(
|
|
2613
|
+
"No providers configured. Please run hyntx setup to configure providers."
|
|
2614
|
+
);
|
|
2615
|
+
}
|
|
2616
|
+
logger.debug(
|
|
2617
|
+
`Checking ${String(config.services.length)} configured provider(s): ${config.services.join(", ")}`,
|
|
2618
|
+
"provider"
|
|
2619
|
+
);
|
|
2620
|
+
let firstProvider;
|
|
2621
|
+
for (const type of config.services) {
|
|
2622
|
+
const provider = createProvider(type, config);
|
|
2623
|
+
firstProvider ??= provider;
|
|
2624
|
+
logger.debug(`Checking availability of ${provider.name}...`, "provider");
|
|
2625
|
+
try {
|
|
2626
|
+
const isAvailable = await provider.isAvailable();
|
|
2627
|
+
if (isAvailable) {
|
|
2628
|
+
logger.debug(`${provider.name} is available`, "provider");
|
|
2629
|
+
if (onFallback && provider !== firstProvider) {
|
|
2630
|
+
onFallback(firstProvider.name, provider.name);
|
|
2631
|
+
}
|
|
2632
|
+
return provider;
|
|
2633
|
+
} else {
|
|
2634
|
+
logger.debug(`${provider.name} is not available`, "provider");
|
|
2635
|
+
}
|
|
2636
|
+
} catch {
|
|
2637
|
+
logger.debug(`${provider.name} availability check failed`, "provider");
|
|
2638
|
+
continue;
|
|
2639
|
+
}
|
|
2640
|
+
}
|
|
2641
|
+
throw new Error(
|
|
2642
|
+
"No providers are currently available. Please check your provider configurations."
|
|
2643
|
+
);
|
|
2644
|
+
}
|
|
2645
|
+
function getAllProviders(config) {
|
|
2646
|
+
if (config.services.length === 0) {
|
|
2647
|
+
throw new Error(
|
|
2648
|
+
"No providers configured. Please run hyntx setup to configure providers."
|
|
2649
|
+
);
|
|
2650
|
+
}
|
|
2651
|
+
return config.services.map((type) => createProvider(type, config));
|
|
2652
|
+
}
|
|
2653
|
+
|
|
2654
|
+
// src/utils/env.ts
|
|
2655
|
+
var VALID_PROVIDERS = [
|
|
2656
|
+
"ollama",
|
|
2657
|
+
"anthropic",
|
|
2658
|
+
"google"
|
|
2659
|
+
];
|
|
2660
|
+
function isFirstRun() {
|
|
2661
|
+
return !process.env["HYNTX_SERVICES"];
|
|
2662
|
+
}
|
|
2663
|
+
function parseServices(services) {
|
|
2664
|
+
if (!services || services.trim() === "") {
|
|
2665
|
+
return [];
|
|
2666
|
+
}
|
|
2667
|
+
return services.split(",").map((s) => s.trim().toLowerCase()).filter(
|
|
2668
|
+
(s) => VALID_PROVIDERS.includes(s)
|
|
2669
|
+
);
|
|
2670
|
+
}
|
|
2671
|
+
function getEnvConfig() {
|
|
2672
|
+
return {
|
|
2673
|
+
services: parseServices(process.env["HYNTX_SERVICES"]),
|
|
2674
|
+
reminder: process.env["HYNTX_REMINDER"] ?? ENV_DEFAULTS.reminder,
|
|
2675
|
+
ollama: {
|
|
2676
|
+
model: process.env["HYNTX_OLLAMA_MODEL"] ?? ENV_DEFAULTS.ollama.model,
|
|
2677
|
+
host: process.env["HYNTX_OLLAMA_HOST"] ?? ENV_DEFAULTS.ollama.host
|
|
2678
|
+
},
|
|
2679
|
+
anthropic: {
|
|
2680
|
+
model: process.env["HYNTX_ANTHROPIC_MODEL"] ?? ENV_DEFAULTS.anthropic.model,
|
|
2681
|
+
apiKey: process.env["HYNTX_ANTHROPIC_KEY"] ?? ""
|
|
2682
|
+
},
|
|
2683
|
+
google: {
|
|
2684
|
+
model: process.env["HYNTX_GOOGLE_MODEL"] ?? ENV_DEFAULTS.google.model,
|
|
2685
|
+
apiKey: process.env["HYNTX_GOOGLE_KEY"] ?? ""
|
|
2686
|
+
}
|
|
2687
|
+
};
|
|
2688
|
+
}
|
|
2689
|
+
var CONFIG_FILENAME = ".hyntxrc.json";
|
|
2690
|
+
var MAX_TRAVERSAL_DEPTH = 50;
|
|
2691
|
+
var PROJECT_CONTEXT_SCHEMA = z.object({
|
|
2692
|
+
role: z.string().optional(),
|
|
2693
|
+
techStack: z.array(z.string()).optional(),
|
|
2694
|
+
domain: z.string().optional(),
|
|
2695
|
+
guidelines: z.array(z.string()).optional(),
|
|
2696
|
+
projectType: z.string().optional()
|
|
2697
|
+
});
|
|
2698
|
+
var RULE_CONFIG_SCHEMA = z.object({
|
|
2699
|
+
enabled: z.boolean().optional(),
|
|
2700
|
+
severity: z.enum(["low", "medium", "high"]).optional()
|
|
2701
|
+
});
|
|
2702
|
+
var RULES_CONFIG_SCHEMA = z.record(z.string(), RULE_CONFIG_SCHEMA);
|
|
2703
|
+
var PROJECT_CONFIG_SCHEMA = z.object({
|
|
2704
|
+
context: PROJECT_CONTEXT_SCHEMA.optional(),
|
|
2705
|
+
rules: RULES_CONFIG_SCHEMA.optional()
|
|
2706
|
+
});
|
|
2707
|
+
var configCache = /* @__PURE__ */ new Map();
|
|
2708
|
+
function findProjectConfig(cwd) {
|
|
2709
|
+
let currentDir = cwd;
|
|
2710
|
+
const homeDirectory = homedir();
|
|
2711
|
+
let depth = 0;
|
|
2712
|
+
while (depth < MAX_TRAVERSAL_DEPTH) {
|
|
2713
|
+
const configPath = join(currentDir, CONFIG_FILENAME);
|
|
2714
|
+
try {
|
|
2715
|
+
readFileSync(configPath, "utf-8");
|
|
2716
|
+
return configPath;
|
|
2717
|
+
} catch {
|
|
2718
|
+
}
|
|
2719
|
+
if (currentDir === homeDirectory || currentDir === "/") {
|
|
2720
|
+
break;
|
|
2721
|
+
}
|
|
2722
|
+
const parentDir = dirname(currentDir);
|
|
2723
|
+
if (parentDir === currentDir) {
|
|
2724
|
+
break;
|
|
2725
|
+
}
|
|
2726
|
+
currentDir = parentDir;
|
|
2727
|
+
depth++;
|
|
2728
|
+
}
|
|
2729
|
+
return null;
|
|
2730
|
+
}
|
|
2731
|
+
function loadProjectConfig(configPath) {
|
|
2732
|
+
try {
|
|
2733
|
+
const fileContent = readFileSync(configPath, "utf-8");
|
|
2734
|
+
let parsed;
|
|
2735
|
+
try {
|
|
2736
|
+
parsed = JSON.parse(fileContent);
|
|
2737
|
+
} catch (error) {
|
|
2738
|
+
logger.collectWarning(
|
|
2739
|
+
`Invalid JSON in ${CONFIG_FILENAME}: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
2740
|
+
"config"
|
|
2741
|
+
);
|
|
2742
|
+
return null;
|
|
2743
|
+
}
|
|
2744
|
+
const result = PROJECT_CONFIG_SCHEMA.safeParse(parsed);
|
|
2745
|
+
if (!result.success) {
|
|
2746
|
+
const errorMessages = result.error.issues.map((issue) => {
|
|
2747
|
+
const pathStr = issue.path.map(String).join(".");
|
|
2748
|
+
return `${pathStr}: ${issue.message}`;
|
|
2749
|
+
}).join(", ");
|
|
2750
|
+
logger.collectWarning(
|
|
2751
|
+
`Invalid ${CONFIG_FILENAME} schema: ${errorMessages}`,
|
|
2752
|
+
"config"
|
|
2753
|
+
);
|
|
2754
|
+
return null;
|
|
2755
|
+
}
|
|
2756
|
+
return result.data;
|
|
2757
|
+
} catch (error) {
|
|
2758
|
+
logger.collectWarning(
|
|
2759
|
+
`Failed to read ${CONFIG_FILENAME}: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
2760
|
+
"config"
|
|
2761
|
+
);
|
|
2762
|
+
return null;
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
function mergeConfigs(envConfig, projectConfig) {
|
|
2766
|
+
if (!projectConfig) {
|
|
2767
|
+
return envConfig;
|
|
2768
|
+
}
|
|
2769
|
+
const existingEnvConfig = envConfig;
|
|
2770
|
+
const merged = {
|
|
2771
|
+
...envConfig
|
|
2772
|
+
};
|
|
2773
|
+
if (projectConfig.context && !existingEnvConfig.context) {
|
|
2774
|
+
merged.context = projectConfig.context;
|
|
2775
|
+
} else if (existingEnvConfig.context) {
|
|
2776
|
+
merged.context = existingEnvConfig.context;
|
|
2777
|
+
}
|
|
2778
|
+
if (projectConfig.rules) {
|
|
2779
|
+
merged.rules = projectConfig.rules;
|
|
2780
|
+
}
|
|
2781
|
+
return merged;
|
|
2782
|
+
}
|
|
2783
|
+
function loadProjectConfigForCwd(cwd) {
|
|
2784
|
+
if (configCache.has(cwd)) {
|
|
2785
|
+
return configCache.get(cwd) ?? null;
|
|
2786
|
+
}
|
|
2787
|
+
const configPath = findProjectConfig(cwd);
|
|
2788
|
+
if (!configPath) {
|
|
2789
|
+
configCache.set(cwd, null);
|
|
2790
|
+
return null;
|
|
2791
|
+
}
|
|
2792
|
+
const config = loadProjectConfig(configPath);
|
|
2793
|
+
configCache.set(cwd, config);
|
|
2794
|
+
return config;
|
|
2795
|
+
}
|
|
2796
|
+
|
|
2797
|
+
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 };
|