coding-agent-benchmarks 0.5.1 → 0.5.2
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/dist/lcs.d.ts +33 -0
- package/dist/lcs.d.ts.map +1 -0
- package/dist/lcs.js +147 -0
- package/dist/lcs.js.map +1 -0
- package/dist/utils/lcs.d.ts +23 -18
- package/dist/utils/lcs.d.ts.map +1 -1
- package/dist/utils/lcs.js +72 -76
- package/dist/utils/lcs.js.map +1 -1
- package/dist/validators/llmJudge.d.ts.map +1 -1
- package/dist/validators/llmJudge.js +10 -6
- package/dist/validators/llmJudge.js.map +1 -1
- package/package.json +1 -1
package/dist/lcs.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Longest Common Subsequence (LCS) Algorithm
|
|
3
|
+
*
|
|
4
|
+
* Finds the longest subsequence common to two strings using dynamic programming.
|
|
5
|
+
* A subsequence is a sequence that appears in the same relative order, but not
|
|
6
|
+
* necessarily contiguous.
|
|
7
|
+
*/
|
|
8
|
+
interface LCSResult {
|
|
9
|
+
readonly length: number;
|
|
10
|
+
readonly subsequence: string;
|
|
11
|
+
readonly matrix: readonly (readonly number[])[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Computes the longest common subsequence between two strings
|
|
15
|
+
*
|
|
16
|
+
* Time Complexity: O(m * n) where m and n are the lengths of the input strings
|
|
17
|
+
* Space Complexity: O(m * n) for the DP table
|
|
18
|
+
*/
|
|
19
|
+
export declare const findLongestCommonSubsequence: (str1: string, str2: string) => LCSResult;
|
|
20
|
+
/**
|
|
21
|
+
* Computes only the length of the LCS with optimized space complexity
|
|
22
|
+
*
|
|
23
|
+
* Time Complexity: O(m * n)
|
|
24
|
+
* Space Complexity: O(min(m, n)) - uses only two rows instead of full matrix
|
|
25
|
+
*/
|
|
26
|
+
export declare const findLCSLengthOptimized: (str1: string, str2: string) => number;
|
|
27
|
+
/**
|
|
28
|
+
* Finds all possible longest common subsequences between two strings
|
|
29
|
+
* Note: There can be multiple LCS of the same length
|
|
30
|
+
*/
|
|
31
|
+
export declare const findAllLCS: (str1: string, str2: string) => readonly string[];
|
|
32
|
+
export {};
|
|
33
|
+
//# sourceMappingURL=lcs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lcs.d.ts","sourceRoot":"","sources":["../src/lcs.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,UAAU,SAAS;IACjB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC;CACjD;AAED;;;;;GAKG;AACH,eAAO,MAAM,4BAA4B,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SA+BzE,CAAC;AAiCF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,MAwBnE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,UAAU,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,SAAS,MAAM,EAyCtE,CAAC"}
|
package/dist/lcs.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Longest Common Subsequence (LCS) Algorithm
|
|
4
|
+
*
|
|
5
|
+
* Finds the longest subsequence common to two strings using dynamic programming.
|
|
6
|
+
* A subsequence is a sequence that appears in the same relative order, but not
|
|
7
|
+
* necessarily contiguous.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.findAllLCS = exports.findLCSLengthOptimized = exports.findLongestCommonSubsequence = void 0;
|
|
11
|
+
/**
|
|
12
|
+
* Computes the longest common subsequence between two strings
|
|
13
|
+
*
|
|
14
|
+
* Time Complexity: O(m * n) where m and n are the lengths of the input strings
|
|
15
|
+
* Space Complexity: O(m * n) for the DP table
|
|
16
|
+
*/
|
|
17
|
+
const findLongestCommonSubsequence = (str1, str2) => {
|
|
18
|
+
const m = str1.length;
|
|
19
|
+
const n = str2.length;
|
|
20
|
+
// Initialize DP table with dimensions (m+1) x (n+1)
|
|
21
|
+
// dp[i][j] represents the length of LCS for str1[0...i-1] and str2[0...j-1]
|
|
22
|
+
const dp = Array(m + 1)
|
|
23
|
+
.fill(null)
|
|
24
|
+
.map(() => Array(n + 1).fill(0));
|
|
25
|
+
// Build the DP table bottom-up
|
|
26
|
+
for (let i = 1; i <= m; i++) {
|
|
27
|
+
for (let j = 1; j <= n; j++) {
|
|
28
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
29
|
+
// Characters match: extend the LCS by 1
|
|
30
|
+
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// Characters don't match: take the maximum from either direction
|
|
34
|
+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// Backtrack through the DP table to construct the actual subsequence
|
|
39
|
+
const subsequence = reconstructSubsequence(str1, str2, dp);
|
|
40
|
+
return {
|
|
41
|
+
length: dp[m][n],
|
|
42
|
+
subsequence,
|
|
43
|
+
matrix: dp,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
exports.findLongestCommonSubsequence = findLongestCommonSubsequence;
|
|
47
|
+
/**
|
|
48
|
+
* Reconstructs the actual LCS string by backtracking through the DP table
|
|
49
|
+
*/
|
|
50
|
+
const reconstructSubsequence = (str1, str2, dp) => {
|
|
51
|
+
const result = [];
|
|
52
|
+
let i = str1.length;
|
|
53
|
+
let j = str2.length;
|
|
54
|
+
// Traverse from bottom-right to top-left
|
|
55
|
+
while (i > 0 && j > 0) {
|
|
56
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
57
|
+
// Character is part of LCS: add to result and move diagonally
|
|
58
|
+
result.unshift(str1[i - 1]);
|
|
59
|
+
i--;
|
|
60
|
+
j--;
|
|
61
|
+
}
|
|
62
|
+
else if (dp[i - 1][j] > dp[i][j - 1]) {
|
|
63
|
+
// Move up (exclude current character from str1)
|
|
64
|
+
i--;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
// Move left (exclude current character from str2)
|
|
68
|
+
j--;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return result.join('');
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Computes only the length of the LCS with optimized space complexity
|
|
75
|
+
*
|
|
76
|
+
* Time Complexity: O(m * n)
|
|
77
|
+
* Space Complexity: O(min(m, n)) - uses only two rows instead of full matrix
|
|
78
|
+
*/
|
|
79
|
+
const findLCSLengthOptimized = (str1, str2) => {
|
|
80
|
+
// Ensure str2 is the shorter string to minimize space usage
|
|
81
|
+
if (str1.length < str2.length) {
|
|
82
|
+
[str1, str2] = [str2, str1];
|
|
83
|
+
}
|
|
84
|
+
const n = str2.length;
|
|
85
|
+
let prev = Array(n + 1).fill(0);
|
|
86
|
+
let curr = Array(n + 1).fill(0);
|
|
87
|
+
for (let i = 1; i <= str1.length; i++) {
|
|
88
|
+
for (let j = 1; j <= n; j++) {
|
|
89
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
90
|
+
curr[j] = prev[j - 1] + 1;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
curr[j] = Math.max(prev[j], curr[j - 1]);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Swap rows: current becomes previous for next iteration
|
|
97
|
+
[prev, curr] = [curr, prev];
|
|
98
|
+
curr.fill(0);
|
|
99
|
+
}
|
|
100
|
+
return prev[n];
|
|
101
|
+
};
|
|
102
|
+
exports.findLCSLengthOptimized = findLCSLengthOptimized;
|
|
103
|
+
/**
|
|
104
|
+
* Finds all possible longest common subsequences between two strings
|
|
105
|
+
* Note: There can be multiple LCS of the same length
|
|
106
|
+
*/
|
|
107
|
+
const findAllLCS = (str1, str2) => {
|
|
108
|
+
const m = str1.length;
|
|
109
|
+
const n = str2.length;
|
|
110
|
+
// Build DP table
|
|
111
|
+
const dp = Array(m + 1)
|
|
112
|
+
.fill(null)
|
|
113
|
+
.map(() => Array(n + 1).fill(0));
|
|
114
|
+
for (let i = 1; i <= m; i++) {
|
|
115
|
+
for (let j = 1; j <= n; j++) {
|
|
116
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
117
|
+
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Backtrack to find all possible LCS
|
|
125
|
+
const results = new Set();
|
|
126
|
+
const backtrack = (i, j, current) => {
|
|
127
|
+
if (i === 0 || j === 0) {
|
|
128
|
+
results.add(current.split('').reverse().join(''));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
132
|
+
backtrack(i - 1, j - 1, current + str1[i - 1]);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
if (dp[i - 1][j] >= dp[i][j - 1]) {
|
|
136
|
+
backtrack(i - 1, j, current);
|
|
137
|
+
}
|
|
138
|
+
if (dp[i][j - 1] >= dp[i - 1][j]) {
|
|
139
|
+
backtrack(i, j - 1, current);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
backtrack(m, n, '');
|
|
144
|
+
return Array.from(results);
|
|
145
|
+
};
|
|
146
|
+
exports.findAllLCS = findAllLCS;
|
|
147
|
+
//# sourceMappingURL=lcs.js.map
|
package/dist/lcs.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lcs.js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
5
|
readonly subsequence: string;
|
|
6
6
|
readonly length: number;
|
|
7
|
-
readonly
|
|
7
|
+
readonly indices1: readonly number[];
|
|
8
|
+
readonly indices2: readonly number[];
|
|
8
9
|
}
|
|
9
10
|
/**
|
|
10
11
|
* Finds the longest common subsequence (LCS) between two strings using dynamic programming.
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* Space complexity: O(m * n) for the DP matrix.
|
|
13
|
+
* Time Complexity: O(m * n) where m and n are string lengths
|
|
14
|
+
* Space Complexity: O(m * n) for the DP table
|
|
15
15
|
*
|
|
16
|
-
* @param str1
|
|
17
|
-
* @param str2
|
|
18
|
-
* @returns
|
|
16
|
+
* @param str1 First string
|
|
17
|
+
* @param str2 Second string
|
|
18
|
+
* @returns LCS result with subsequence, length, and character indices
|
|
19
19
|
*/
|
|
20
20
|
export declare const findLongestCommonSubsequence: (str1: string, str2: string) => LCSResult;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
* Space complexity: O(min(m, n))
|
|
22
|
+
* Computes similarity ratio between two strings based on their LCS.
|
|
23
|
+
* Returns value between 0 (no similarity) and 1 (identical).
|
|
25
24
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
25
|
+
* @param str1 First string
|
|
26
|
+
* @param str2 Second string
|
|
27
|
+
* @returns Similarity ratio (0-1)
|
|
28
28
|
*/
|
|
29
|
-
export declare const
|
|
29
|
+
export declare const computeLCSSimilarity: (str1: string, str2: string) => number;
|
|
30
30
|
/**
|
|
31
|
-
* Finds all
|
|
32
|
-
* There can be multiple LCS
|
|
31
|
+
* Finds all longest common subsequences between two strings.
|
|
32
|
+
* Note: There can be multiple LCS of the same length.
|
|
33
33
|
*
|
|
34
|
-
*
|
|
34
|
+
* This is more computationally expensive than findLongestCommonSubsequence
|
|
35
|
+
* as it explores all possible paths.
|
|
36
|
+
*
|
|
37
|
+
* @param str1 First string
|
|
38
|
+
* @param str2 Second string
|
|
39
|
+
* @returns Array of all LCS strings
|
|
35
40
|
*/
|
|
36
|
-
export declare const
|
|
41
|
+
export declare const findAllLongestCommonSubsequences: (str1: string, str2: string) => readonly string[];
|
|
37
42
|
//# sourceMappingURL=lcs.d.ts.map
|
package/dist/utils/lcs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lcs.d.ts","sourceRoot":"","sources":["../../src/utils/lcs.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,
|
|
1
|
+
{"version":3,"file":"lcs.d.ts","sourceRoot":"","sources":["../../src/utils/lcs.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,4BAA4B,GACvC,MAAM,MAAM,EACZ,MAAM,MAAM,KACX,SAuDF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,KAAG,MAajE,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,gCAAgC,GAC3C,MAAM,MAAM,EACZ,MAAM,MAAM,KACX,SAAS,MAAM,EA2CjB,CAAC"}
|
package/dist/utils/lcs.js
CHANGED
|
@@ -1,108 +1,104 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.findAllLongestCommonSubsequences = exports.computeLCSSimilarity = exports.findLongestCommonSubsequence = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Finds the longest common subsequence (LCS) between two strings using dynamic programming.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* Space complexity: O(m * n) for the DP matrix.
|
|
7
|
+
* Time Complexity: O(m * n) where m and n are string lengths
|
|
8
|
+
* Space Complexity: O(m * n) for the DP table
|
|
10
9
|
*
|
|
11
|
-
* @param str1
|
|
12
|
-
* @param str2
|
|
13
|
-
* @returns
|
|
10
|
+
* @param str1 First string
|
|
11
|
+
* @param str2 Second string
|
|
12
|
+
* @returns LCS result with subsequence, length, and character indices
|
|
14
13
|
*/
|
|
15
14
|
const findLongestCommonSubsequence = (str1, str2) => {
|
|
16
15
|
const m = str1.length;
|
|
17
16
|
const n = str2.length;
|
|
18
|
-
//
|
|
17
|
+
// DP table: dp[i][j] stores length of LCS of str1[0..i-1] and str2[0..j-1]
|
|
19
18
|
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
|
20
|
-
// Build
|
|
19
|
+
// Build DP table bottom-up
|
|
21
20
|
for (let i = 1; i <= m; i++) {
|
|
22
21
|
for (let j = 1; j <= n; j++) {
|
|
23
22
|
if (str1[i - 1] === str2[j - 1]) {
|
|
24
|
-
// Characters match: extend
|
|
23
|
+
// Characters match: extend previous LCS by 1
|
|
25
24
|
dp[i][j] = dp[i - 1][j - 1] + 1;
|
|
26
25
|
}
|
|
27
26
|
else {
|
|
28
|
-
// Characters don't match: take max from
|
|
27
|
+
// Characters don't match: take max from excluding either character
|
|
29
28
|
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
|
-
// Backtrack
|
|
34
|
-
const
|
|
32
|
+
// Backtrack to reconstruct the actual subsequence and indices
|
|
33
|
+
const lcsLength = dp[m][n];
|
|
34
|
+
const subsequenceChars = [];
|
|
35
|
+
const indices1 = [];
|
|
36
|
+
const indices2 = [];
|
|
37
|
+
let i = m;
|
|
38
|
+
let j = n;
|
|
39
|
+
// Trace back from dp[m][n] to dp[0][0] to build the LCS
|
|
40
|
+
while (i > 0 && j > 0) {
|
|
41
|
+
if (str1[i - 1] === str2[j - 1]) {
|
|
42
|
+
// This character is part of LCS
|
|
43
|
+
subsequenceChars.unshift(str1[i - 1]);
|
|
44
|
+
indices1.unshift(i - 1);
|
|
45
|
+
indices2.unshift(j - 1);
|
|
46
|
+
i--;
|
|
47
|
+
j--;
|
|
48
|
+
}
|
|
49
|
+
else if (dp[i - 1][j] > dp[i][j - 1]) {
|
|
50
|
+
// LCS came from excluding current char in str1
|
|
51
|
+
i--;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
// LCS came from excluding current char in str2
|
|
55
|
+
j--;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
35
58
|
return {
|
|
36
|
-
subsequence,
|
|
37
|
-
length:
|
|
38
|
-
|
|
59
|
+
subsequence: subsequenceChars.join(''),
|
|
60
|
+
length: lcsLength,
|
|
61
|
+
indices1,
|
|
62
|
+
indices2,
|
|
39
63
|
};
|
|
40
64
|
};
|
|
41
65
|
exports.findLongestCommonSubsequence = findLongestCommonSubsequence;
|
|
42
66
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
67
|
+
* Computes similarity ratio between two strings based on their LCS.
|
|
68
|
+
* Returns value between 0 (no similarity) and 1 (identical).
|
|
69
|
+
*
|
|
70
|
+
* @param str1 First string
|
|
71
|
+
* @param str2 Second string
|
|
72
|
+
* @returns Similarity ratio (0-1)
|
|
45
73
|
*/
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return "";
|
|
50
|
-
}
|
|
51
|
-
// If characters match, this character is part of LCS
|
|
52
|
-
if (str1[i - 1] === str2[j - 1]) {
|
|
53
|
-
return backtrackLCS(str1, str2, dp, i - 1, j - 1) + str1[i - 1];
|
|
74
|
+
const computeLCSSimilarity = (str1, str2) => {
|
|
75
|
+
if (str1.length === 0 && str2.length === 0) {
|
|
76
|
+
return 1.0;
|
|
54
77
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return backtrackLCS(str1, str2, dp, i - 1, j);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
return backtrackLCS(str1, str2, dp, i, j - 1);
|
|
78
|
+
if (str1.length === 0 || str2.length === 0) {
|
|
79
|
+
return 0.0;
|
|
61
80
|
}
|
|
81
|
+
const lcsLength = (0, exports.findLongestCommonSubsequence)(str1, str2).length;
|
|
82
|
+
const maxLength = Math.max(str1.length, str2.length);
|
|
83
|
+
return lcsLength / maxLength;
|
|
62
84
|
};
|
|
85
|
+
exports.computeLCSSimilarity = computeLCSSimilarity;
|
|
63
86
|
/**
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* Space complexity: O(min(m, n))
|
|
87
|
+
* Finds all longest common subsequences between two strings.
|
|
88
|
+
* Note: There can be multiple LCS of the same length.
|
|
67
89
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*/
|
|
71
|
-
const findLCSLengthOptimized = (str1, str2) => {
|
|
72
|
-
// Ensure str2 is the shorter string to minimize space usage
|
|
73
|
-
if (str1.length < str2.length) {
|
|
74
|
-
[str1, str2] = [str2, str1];
|
|
75
|
-
}
|
|
76
|
-
const n = str2.length;
|
|
77
|
-
let prev = Array(n + 1).fill(0);
|
|
78
|
-
let curr = Array(n + 1).fill(0);
|
|
79
|
-
str1.split("").forEach((char1) => {
|
|
80
|
-
for (let j = 1; j <= n; j++) {
|
|
81
|
-
if (char1 === str2[j - 1]) {
|
|
82
|
-
curr[j] = prev[j - 1] + 1;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
curr[j] = Math.max(prev[j], curr[j - 1]);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// Swap rows for next iteration
|
|
89
|
-
[prev, curr] = [curr, prev];
|
|
90
|
-
curr.fill(0);
|
|
91
|
-
});
|
|
92
|
-
return prev[n];
|
|
93
|
-
};
|
|
94
|
-
exports.findLCSLengthOptimized = findLCSLengthOptimized;
|
|
95
|
-
/**
|
|
96
|
-
* Finds all possible longest common subsequences between two strings.
|
|
97
|
-
* There can be multiple LCS with the same maximum length.
|
|
90
|
+
* This is more computationally expensive than findLongestCommonSubsequence
|
|
91
|
+
* as it explores all possible paths.
|
|
98
92
|
*
|
|
99
|
-
* @
|
|
93
|
+
* @param str1 First string
|
|
94
|
+
* @param str2 Second string
|
|
95
|
+
* @returns Array of all LCS strings
|
|
100
96
|
*/
|
|
101
|
-
const
|
|
97
|
+
const findAllLongestCommonSubsequences = (str1, str2) => {
|
|
102
98
|
const m = str1.length;
|
|
103
99
|
const n = str2.length;
|
|
100
|
+
// Build DP table same as before
|
|
104
101
|
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
|
|
105
|
-
// Build DP matrix
|
|
106
102
|
for (let i = 1; i <= m; i++) {
|
|
107
103
|
for (let j = 1; j <= n; j++) {
|
|
108
104
|
if (str1[i - 1] === str2[j - 1]) {
|
|
@@ -113,27 +109,27 @@ const findAllLCS = (str1, str2) => {
|
|
|
113
109
|
}
|
|
114
110
|
}
|
|
115
111
|
}
|
|
116
|
-
// Collect all LCS using DFS with memoization to avoid duplicates
|
|
117
112
|
const allLCS = new Set();
|
|
118
|
-
|
|
113
|
+
// Recursive backtracking to find all possible LCS paths
|
|
114
|
+
const backtrack = (i, j, current) => {
|
|
119
115
|
if (i === 0 || j === 0) {
|
|
120
|
-
allLCS.add(current);
|
|
116
|
+
allLCS.add(current.split('').reverse().join(''));
|
|
121
117
|
return;
|
|
122
118
|
}
|
|
123
119
|
if (str1[i - 1] === str2[j - 1]) {
|
|
124
|
-
|
|
120
|
+
backtrack(i - 1, j - 1, current + str1[i - 1]);
|
|
125
121
|
}
|
|
126
122
|
else {
|
|
127
123
|
if (dp[i - 1][j] >= dp[i][j - 1]) {
|
|
128
|
-
|
|
124
|
+
backtrack(i - 1, j, current);
|
|
129
125
|
}
|
|
130
126
|
if (dp[i][j - 1] >= dp[i - 1][j]) {
|
|
131
|
-
|
|
127
|
+
backtrack(i, j - 1, current);
|
|
132
128
|
}
|
|
133
129
|
}
|
|
134
130
|
};
|
|
135
|
-
|
|
131
|
+
backtrack(m, n, '');
|
|
136
132
|
return Array.from(allLCS);
|
|
137
133
|
};
|
|
138
|
-
exports.
|
|
134
|
+
exports.findAllLongestCommonSubsequences = findAllLongestCommonSubsequences;
|
|
139
135
|
//# sourceMappingURL=lcs.js.map
|
package/dist/utils/lcs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lcs.js","sourceRoot":"","sources":["../../src/utils/lcs.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"lcs.js","sourceRoot":"","sources":["../../src/utils/lcs.ts"],"names":[],"mappings":";;;AAUA;;;;;;;;;GASG;AACI,MAAM,4BAA4B,GAAG,CAC1C,IAAY,EACZ,IAAY,EACD,EAAE;IACb,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IACtB,MAAM,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC;IAEtB,2EAA2E;IAC3E,MAAM,EAAE,GAAe,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,GAAG,EAAE,CACxD,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CACrB,CAAC;IAEF,2BAA2B;IAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC5B,IAAI,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG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B,aAAa,CAAC,EAAE,MAAM,EAAE,KAAK,GAAE,MAAuB;IAMlE;;OAEG;IACG,QAAQ,CACZ,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"llmJudge.d.ts","sourceRoot":"","sources":["../../src/validators/llmJudge.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EACL,aAAa,EACb,YAAY,EACZ,gBAAgB,EAEjB,MAAM,UAAU,CAAC;AA6DlB,qBAAa,iBAAkB,YAAW,aAAa;IACrD,SAAgB,IAAI,EAAG,WAAW,CAAU;IAC5C,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAqB;IACrC,OAAO,CAAC,YAAY,CAAS;gBAEjB,aAAa,CAAC,EAAE,MAAM,EAAE,KAAK,GAAE,MAAuB;IAMlE;;OAEG;IACG,QAAQ,CACZ,KAAK,EAAE,SAAS,MAAM,EAAE,EACxB,QAAQ,EAAE,YAAY,GACrB,OAAO,CAAC,gBAAgB,CAAC;IA4E5B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IA+B3B;;OAEG;YACW,UAAU;IAsFxB;;OAEG;IACG,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAYjE"}
|
|
@@ -103,9 +103,7 @@ class LLMJudgeValidator {
|
|
|
103
103
|
const relativePath = path.relative(this.workspaceRoot, filePath);
|
|
104
104
|
fileContents.push({ path: relativePath, content });
|
|
105
105
|
}
|
|
106
|
-
// Build judgment prompt
|
|
107
106
|
const judgmentPrompt = this.buildJudgmentPrompt(scenario, fileContents, llmConfig.judgmentPrompt);
|
|
108
|
-
// Calling LLM API
|
|
109
107
|
const model = llmConfig.model || this.defaultModel;
|
|
110
108
|
const judgment = await this.callLLMAPI(judgmentPrompt, model);
|
|
111
109
|
// Convert judgment to violations
|
|
@@ -126,8 +124,8 @@ class LLMJudgeValidator {
|
|
|
126
124
|
}
|
|
127
125
|
catch (error) {
|
|
128
126
|
return {
|
|
129
|
-
passed:
|
|
130
|
-
score:
|
|
127
|
+
passed: true,
|
|
128
|
+
score: -1,
|
|
131
129
|
violations: [],
|
|
132
130
|
validatorType: "llm-judge",
|
|
133
131
|
error: `LLM judge failed: ${error}`,
|
|
@@ -177,10 +175,15 @@ Be strict but fair in your evaluation.`;
|
|
|
177
175
|
{ role: "system", content: judgeSystemPrompt },
|
|
178
176
|
{ role: "user", content: prompt },
|
|
179
177
|
],
|
|
180
|
-
max_completion_tokens: 1000,
|
|
181
178
|
response_format: { type: "json_object" },
|
|
182
179
|
}),
|
|
183
180
|
});
|
|
181
|
+
if (response.status === 401) {
|
|
182
|
+
throw new Error("Unauthorized: Invalid GITHUB_TOKEN");
|
|
183
|
+
}
|
|
184
|
+
if (response.status === 429) {
|
|
185
|
+
throw new Error("Rate limit exceeded: Too many requests to LLM API");
|
|
186
|
+
}
|
|
184
187
|
if (!response.ok) {
|
|
185
188
|
const errorText = await response.text();
|
|
186
189
|
throw new Error(`GitHub Models API error: ${response.status} ${errorText}`);
|
|
@@ -188,7 +191,8 @@ Be strict but fair in your evaluation.`;
|
|
|
188
191
|
const data = (await response.json());
|
|
189
192
|
const content = data.choices[0]?.message?.content;
|
|
190
193
|
if (!content) {
|
|
191
|
-
|
|
194
|
+
const finishReason = data.choices[0]?.finish_reason ?? "unknown";
|
|
195
|
+
throw new Error(`No content in LLM response (finish_reason: ${finishReason})`);
|
|
192
196
|
}
|
|
193
197
|
// Parse JSON response
|
|
194
198
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"llmJudge.js","sourceRoot":"","sources":["../../src/validators/llmJudge.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,uCAAyB;AACzB,2CAA6B;AAO7B,4DAGiC;AAC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EAAE,KAAc;QAC5C,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,OAAO,+BAA+B,CAAC;QACzC,CAAC;QAED,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,KAAK,IAAI,IAAI,CAAC,YAAY,CAAC,CAAC;YACzE,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QACzC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,UAAU,KAAK,EAAE,CAAC;QAC3B,CAAC;IACH,CAAC;CACF;AAxOD,8CAwOC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "coding-agent-benchmarks",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Testing coding agents (GitHub Copilot CLI, Claude Code, etc.) with your repo's context to evaluate their code generation quality.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|