-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshared.ts
More file actions
175 lines (155 loc) · 5.49 KB
/
Copy pathshared.ts
File metadata and controls
175 lines (155 loc) · 5.49 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { constants } from 'node:fs';
import { access, stat } from 'node:fs/promises';
import path from 'node:path';
import { isTypeScriptEvalConfigFileName, typeScriptEvalConfigGlob } from '@agentv/core';
import fg from 'fast-glob';
import { isAgentSkillsEvalsJsonFile } from '../read-adapters/agent-skills-evals.js';
export interface ResolveEvalPathOptions {
readonly allowReadAdapters?: boolean;
}
function isNativeEvalFile(filePath: string): boolean {
return /\.(ya?ml|jsonl)$/i.test(filePath) || isTypeScriptEvalConfigFileName(filePath);
}
function shouldInspectJsonPath(filePath: string): boolean {
const base = path.basename(filePath).toLowerCase();
return base === 'evals.json' || base.endsWith('.evals.json');
}
function jsonEvalPathError(pattern: string): Error {
return new Error(
`Unsupported .json eval file: ${pattern}. Agent Skills evals.json read adapters require top-level 'skill_name' and 'evals'. Use YAML, JSONL, TypeScript, or run 'agentv convert ${pattern} --out EVAL.yaml'.`,
);
}
export async function resolveEvalPaths(
evalPaths: string[],
cwd: string,
options: ResolveEvalPathOptions = {},
): Promise<string[]> {
const normalizedInputs = evalPaths.map((value) => value?.trim()).filter((value) => value);
if (normalizedInputs.length === 0) {
throw new Error('No eval paths provided.');
}
// Separate negation patterns (!glob) from include patterns.
// Negation patterns are passed to fast-glob as `ignore`.
const includePatterns: string[] = [];
const ignorePatterns: string[] = [];
for (const input of normalizedInputs) {
if (input.startsWith('!')) {
ignorePatterns.push(input.slice(1));
} else {
includePatterns.push(input);
}
}
if (includePatterns.length === 0) {
throw new Error('No eval paths provided (only negation patterns found).');
}
const results = new Set<string>();
for (const pattern of includePatterns) {
// If the pattern points to an existing file or directory, short-circuit globbing
const candidatePath = path.isAbsolute(pattern)
? path.normalize(pattern)
: path.resolve(cwd, pattern);
let candidateStats: Awaited<ReturnType<typeof stat>> | undefined;
try {
candidateStats = await stat(candidatePath);
} catch {
candidateStats = undefined;
}
if (candidateStats) {
if (candidateStats.isFile() && path.extname(candidatePath).toLowerCase() === '.json') {
if (options.allowReadAdapters && isAgentSkillsEvalsJsonFile(candidatePath)) {
results.add(candidatePath);
continue;
}
throw jsonEvalPathError(pattern);
}
if (candidateStats.isFile() && isNativeEvalFile(candidatePath)) {
results.add(candidatePath);
continue;
}
if (candidateStats.isDirectory()) {
// Auto-expand directory to recursive eval file glob
const filePattern = options.allowReadAdapters
? `{suite.yaml,suite.yml,*.eval.yaml,*.eval.yml,eval.yaml,eval.yml,${typeScriptEvalConfigGlob()},evals.json,*.evals.json}`
: `{suite.yaml,suite.yml,*.eval.yaml,*.eval.yml,eval.yaml,eval.yml,${typeScriptEvalConfigGlob()}}`;
const dirGlob = path.posix.join(candidatePath.replace(/\\/g, '/'), `**/${filePattern}`);
const dirMatches = await fg(dirGlob, {
absolute: true,
onlyFiles: true,
unique: true,
dot: true,
followSymbolicLinks: true,
ignore: ignorePatterns,
});
for (const filePath of dirMatches) {
results.add(path.normalize(filePath));
}
continue;
}
}
const globPattern = pattern.includes('\\') ? pattern.replace(/\\/g, '/') : pattern;
const matches = await fg(globPattern, {
cwd,
absolute: true,
onlyFiles: true,
unique: true,
dot: true,
followSymbolicLinks: true,
ignore: ignorePatterns,
});
const supportedMatches = matches.filter((filePath) => {
if (isNativeEvalFile(filePath)) {
return true;
}
return (
options.allowReadAdapters &&
path.extname(filePath).toLowerCase() === '.json' &&
shouldInspectJsonPath(filePath) &&
isAgentSkillsEvalsJsonFile(filePath)
);
});
for (const filePath of supportedMatches) {
results.add(path.normalize(filePath));
}
}
if (ignorePatterns.length > 0 && results.size > 0) {
const ignoredMatches = await fg(ignorePatterns, {
cwd,
absolute: true,
onlyFiles: true,
unique: true,
dot: true,
followSymbolicLinks: true,
});
for (const filePath of ignoredMatches) {
results.delete(path.normalize(filePath));
}
}
if (results.size === 0) {
throw new Error(
`No eval files matched any provided paths or globs: ${includePatterns.join(
', ',
)}. Provide YAML, JSONL, TypeScript, or supported read-adapter paths/globs (e.g., "evals/**/suite.yaml", "evals/**/*.eval.ts", "skills/**/evals.json").`,
);
}
const sorted = Array.from(results);
sorted.sort();
return sorted;
}
export async function findRepoRoot(start: string): Promise<string> {
const fallback = path.resolve(start);
let current: string | undefined = fallback;
while (current !== undefined) {
const candidate = path.join(current, '.git');
try {
await access(candidate, constants.F_OK);
return current;
} catch {
const parent = path.dirname(current);
if (parent === current) {
break;
}
current = parent;
}
}
return fallback;
}