-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeQuality.ts
More file actions
65 lines (59 loc) · 1.94 KB
/
codeQuality.ts
File metadata and controls
65 lines (59 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { ai } from '../config.js';
import type { BaseEvalDataPoint } from 'genkit/evaluator';
/**
* Heuristic evaluator for the coder flow outputs.
*
* Returns a simple numeric score between 0.0 and 1.0 and a details object.
*/
export const codeQualityEvaluator = ai.defineEvaluator(
{
name: 'codeQuality',
displayName: 'Code Quality (heuristic)',
definition: 'Heuristic check: ensures one or more files were produced and that filenames are non-empty when present.',
isBilled: false,
},
async (datapoint: BaseEvalDataPoint) => {
const out = datapoint.output;
// Support two common output shapes:
// 1) { filenames: string[] } (used by coderEvalFlow)
// 2) { files: [{ filename, language, content }, ...] }
const maybeOut = (out ?? {}) as { filenames?: unknown; files?: unknown };
let filenames: string[] = [];
if (Array.isArray(maybeOut.filenames)) {
const arr = maybeOut.filenames as unknown[];
filenames = arr.filter((x): x is string => typeof x === 'string');
} else if (Array.isArray(maybeOut.files)) {
const arr = maybeOut.files as unknown[];
filenames = arr.map((f) => {
const fileObj = f as { filename?: unknown };
if (typeof fileObj.filename === 'string') {
return fileObj.filename;
}
return '';
});
}
const fileCount = filenames.length;
let missingFilenames = 0;
for (const fn of filenames) {
if (typeof fn !== 'string' || fn.trim() === '') {
missingFilenames++;
}
}
let score = 0;
if (fileCount === 0) {
score = 0;
} else {
score = 1 - Math.min(1, (missingFilenames / Math.max(1, fileCount)) * 0.5);
}
// Build score object following Genkit ScoreSchema
const evaluation = {
id: 'codeQuality',
score,
details: { fileCount, missingFilenames },
};
return {
testCaseId: datapoint.testCaseId ?? 'unknown',
evaluation,
};
}
);