@stigmer/eval-runtime 0.0.0-stage → 3.41.1-dev.20261011143907.gb2e0845f35f1
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/LICENSE +190 -0
- package/expect.d.ts +116 -0
- package/expect.d.ts.map +1 -0
- package/expect.js +363 -0
- package/expect.js.map +1 -0
- package/index.d.ts +5 -0
- package/index.d.ts.map +1 -0
- package/index.js +9 -0
- package/index.js.map +1 -0
- package/mock.d.ts +56 -0
- package/mock.d.ts.map +1 -0
- package/mock.js +167 -0
- package/mock.js.map +1 -0
- package/package.json +29 -4
- package/src/expect.ts +395 -0
- package/src/index.ts +33 -0
- package/src/mock.ts +176 -0
- package/src/transcript.ts +254 -0
- package/src/try-stop.ts +94 -0
- package/transcript.d.ts +84 -0
- package/transcript.d.ts.map +1 -0
- package/transcript.js +230 -0
- package/transcript.js.map +1 -0
- package/try-stop.d.ts +49 -0
- package/try-stop.d.ts.map +1 -0
- package/try-stop.js +84 -0
- package/try-stop.js.map +1 -0
- package/README.md +0 -3
package/src/expect.ts
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mock's `expect:` guard: what a call's input must look like, checked
|
|
3
|
+
* before the mock answers. A call that violates it stops the try.
|
|
4
|
+
*
|
|
5
|
+
* The guard is a map from dotted input paths (`issue.title`) to one rule:
|
|
6
|
+
* - a type name: `string`, `number`, `boolean`, `array` or `object`;
|
|
7
|
+
* - a `/regex/flags` string, in the format's small dialect (below);
|
|
8
|
+
* - any other string, number, boolean or null: the value must equal it;
|
|
9
|
+
* - a list of such literals: the value must equal one of them.
|
|
10
|
+
* A path the input does not carry is a violation.
|
|
11
|
+
*
|
|
12
|
+
* The regex dialect is the format's: literal characters, `.`, the escapes
|
|
13
|
+
* `\d \w \s \D \W \S` and an escaped punctuation character, classes such
|
|
14
|
+
* as `[a-z]` or `[^0-9]`, the quantifiers `*`, `+`, `?`, `{m}`, `{m,}` and
|
|
15
|
+
* `{m,n}` on one character, escape or class, an optional `^` first and `$`
|
|
16
|
+
* last, and the flags `i` and `s` only. Anything else (a group, an
|
|
17
|
+
* alternation, a backreference, a lookaround, another flag) is refused at
|
|
18
|
+
* compile with a sentence naming the pattern, which the suite reader turns
|
|
19
|
+
* into the case's load error.
|
|
20
|
+
*
|
|
21
|
+
* Matching is linear: the pattern is expanded into a sequence of units
|
|
22
|
+
* (a quantifier's minimum as required units, the rest optional, an
|
|
23
|
+
* unbounded tail as one repeating unit) and simulated as a set of
|
|
24
|
+
* positions over the value, so a value of length n costs n times the
|
|
25
|
+
* units, never a backtracking blow-up. The expansion is capped at
|
|
26
|
+
* EXPECT_MAX_UNITS, and a quantifier is sized against the cap before it is
|
|
27
|
+
* expanded, so a `{m,n}` cannot make a pattern large.
|
|
28
|
+
*
|
|
29
|
+
* Unlike Claude Code, a value is matched whole, whatever its length: the
|
|
30
|
+
* format bounds the length a pattern checks without documenting the bound,
|
|
31
|
+
* and the matcher here has no need for one. The guide states it.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/** The type names a rule may give. */
|
|
35
|
+
const TYPE_NAMES = ["string", "number", "boolean", "array", "object"] as const;
|
|
36
|
+
type TypeName = (typeof TYPE_NAMES)[number];
|
|
37
|
+
|
|
38
|
+
/** The most units a pattern may expand to. */
|
|
39
|
+
export const EXPECT_MAX_UNITS = 1024;
|
|
40
|
+
|
|
41
|
+
/** The most characters a pattern may hold. */
|
|
42
|
+
export const EXPECT_MAX_PATTERN_LENGTH = 512;
|
|
43
|
+
|
|
44
|
+
/** One compiled rule of a guard. */
|
|
45
|
+
type Rule =
|
|
46
|
+
| { readonly kind: "type"; readonly type: TypeName }
|
|
47
|
+
| { readonly kind: "regex"; readonly source: string; readonly pattern: CompiledPattern }
|
|
48
|
+
| { readonly kind: "literal"; readonly value: Literal }
|
|
49
|
+
| { readonly kind: "one-of"; readonly values: readonly Literal[] };
|
|
50
|
+
|
|
51
|
+
type Literal = string | number | boolean | null;
|
|
52
|
+
|
|
53
|
+
/** A compiled guard: each dotted path and its rule, in the order written. */
|
|
54
|
+
export interface CompiledExpect {
|
|
55
|
+
readonly rules: readonly { readonly path: string; readonly rule: Rule }[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** A compiled guard, or why the block is not one. */
|
|
59
|
+
export type ExpectCompile =
|
|
60
|
+
| { readonly ok: true; readonly expect: CompiledExpect }
|
|
61
|
+
| { readonly ok: false; readonly error: string };
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Compile an `expect:` block as parsed from YAML (or its JSON form, as a
|
|
65
|
+
* run carries it). Refuses a block that is not a map, a path that is not
|
|
66
|
+
* dotted names, and any rule outside the list in the module header.
|
|
67
|
+
*/
|
|
68
|
+
export function compileExpect(block: unknown): ExpectCompile {
|
|
69
|
+
if (!isRecord(block)) return { ok: false, error: "expect must be a map from input paths to rules" };
|
|
70
|
+
const rules: { path: string; rule: Rule }[] = [];
|
|
71
|
+
for (const [path, value] of Object.entries(block)) {
|
|
72
|
+
if (!/^[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]+)*$/.test(path)) {
|
|
73
|
+
return { ok: false, error: `expect path '${path}' is not a dotted input path` };
|
|
74
|
+
}
|
|
75
|
+
const rule = ruleOf(value);
|
|
76
|
+
if (typeof rule === "string") return { ok: false, error: `expect.${path}: ${rule}` };
|
|
77
|
+
rules.push({ path, rule });
|
|
78
|
+
}
|
|
79
|
+
return { ok: true, expect: { rules } };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Why `input` violates the guard, as one sentence; `undefined` when it holds. */
|
|
83
|
+
export function checkExpect(expect: CompiledExpect, input: unknown): string | undefined {
|
|
84
|
+
for (const { path, rule } of expect.rules) {
|
|
85
|
+
const found = valueAt(input, path);
|
|
86
|
+
if (!found.present) return `input.${path} is missing`;
|
|
87
|
+
const violation = violationOf(rule, found.value);
|
|
88
|
+
if (violation !== undefined) return `input.${path} ${violation}`;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function ruleOf(value: unknown): Rule | string {
|
|
94
|
+
if (Array.isArray(value)) {
|
|
95
|
+
const values: Literal[] = [];
|
|
96
|
+
for (const item of value) {
|
|
97
|
+
if (!isLiteral(item)) return "a list must hold literals only";
|
|
98
|
+
values.push(item);
|
|
99
|
+
}
|
|
100
|
+
if (values.length === 0) return "a list must name at least one value";
|
|
101
|
+
return { kind: "one-of", values };
|
|
102
|
+
}
|
|
103
|
+
if (typeof value === "string") {
|
|
104
|
+
if ((TYPE_NAMES as readonly string[]).includes(value)) return { kind: "type", type: value as TypeName };
|
|
105
|
+
const regex = /^\/(.*)\/([a-z]*)$/s.exec(value);
|
|
106
|
+
if (regex !== null) {
|
|
107
|
+
const compiled = compilePattern(regex[1] ?? "", regex[2] ?? "");
|
|
108
|
+
if (typeof compiled === "string") return `the pattern ${value} ${compiled}`;
|
|
109
|
+
return { kind: "regex", source: value, pattern: compiled };
|
|
110
|
+
}
|
|
111
|
+
return { kind: "literal", value };
|
|
112
|
+
}
|
|
113
|
+
if (isLiteral(value)) return { kind: "literal", value };
|
|
114
|
+
return "a rule must be a type name, a /regex/, a literal or a list of literals";
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function violationOf(rule: Rule, value: unknown): string | undefined {
|
|
118
|
+
switch (rule.kind) {
|
|
119
|
+
case "type":
|
|
120
|
+
return typeOf(value) === rule.type ? undefined : `must be ${article(rule.type)}`;
|
|
121
|
+
case "regex":
|
|
122
|
+
return matchPattern(rule.pattern, textOf(value)) ? undefined : `must match ${rule.source}`;
|
|
123
|
+
case "literal":
|
|
124
|
+
return value === rule.value ? undefined : `must be ${JSON.stringify(rule.value)}`;
|
|
125
|
+
case "one-of":
|
|
126
|
+
return rule.values.some((v) => v === value)
|
|
127
|
+
? undefined
|
|
128
|
+
: `must be one of ${rule.values.map((v) => (typeof v === "string" ? v : JSON.stringify(v))).join(", ")}`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function typeOf(value: unknown): string {
|
|
133
|
+
if (Array.isArray(value)) return "array";
|
|
134
|
+
if (value === null) return "null";
|
|
135
|
+
return typeof value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function article(type: TypeName): string {
|
|
139
|
+
return type === "array" || type === "object" ? `an ${type}` : `a ${type}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function textOf(value: unknown): string {
|
|
143
|
+
return typeof value === "string" ? value : JSON.stringify(value) ?? "";
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function valueAt(input: unknown, path: string): { readonly present: boolean; readonly value?: unknown } {
|
|
147
|
+
let current: unknown = input;
|
|
148
|
+
for (const key of path.split(".")) {
|
|
149
|
+
if (!isRecord(current) || !Object.hasOwn(current, key)) return { present: false };
|
|
150
|
+
current = current[key];
|
|
151
|
+
}
|
|
152
|
+
return { present: true, value: current };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// --- The pattern dialect -------------------------------------------------
|
|
156
|
+
|
|
157
|
+
/** What one unit consumes: one character. */
|
|
158
|
+
type Atom =
|
|
159
|
+
| { readonly kind: "char"; readonly char: string }
|
|
160
|
+
| { readonly kind: "any" }
|
|
161
|
+
| { readonly kind: "class"; readonly negated: boolean; readonly items: readonly ClassItem[] };
|
|
162
|
+
|
|
163
|
+
type ClassItem =
|
|
164
|
+
| { readonly kind: "range"; readonly from: number; readonly to: number }
|
|
165
|
+
| { readonly kind: "escape"; readonly letter: string };
|
|
166
|
+
|
|
167
|
+
/** One step of the expanded pattern: required, optional, or repeating any number of times. */
|
|
168
|
+
interface Unit {
|
|
169
|
+
readonly atom: Atom;
|
|
170
|
+
readonly mode: "one" | "optional" | "star";
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A pattern compiled for {@link matchPattern}. */
|
|
174
|
+
export interface CompiledPattern {
|
|
175
|
+
readonly units: readonly Unit[];
|
|
176
|
+
readonly anchoredStart: boolean;
|
|
177
|
+
readonly anchoredEnd: boolean;
|
|
178
|
+
readonly ignoreCase: boolean;
|
|
179
|
+
readonly dotAll: boolean;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Compile a pattern in the dialect, or say why it is outside it. */
|
|
183
|
+
export function compilePattern(source: string, flags: string): CompiledPattern | string {
|
|
184
|
+
if ([...flags].some((f) => f !== "i" && f !== "s") || new Set(flags).size !== flags.length) {
|
|
185
|
+
return `uses a flag other than i and s`;
|
|
186
|
+
}
|
|
187
|
+
if (source.length > EXPECT_MAX_PATTERN_LENGTH) {
|
|
188
|
+
return `is longer than ${EXPECT_MAX_PATTERN_LENGTH} characters`;
|
|
189
|
+
}
|
|
190
|
+
const chars = [...source];
|
|
191
|
+
let i = 0;
|
|
192
|
+
const anchoredStart = chars[0] === "^";
|
|
193
|
+
if (anchoredStart) i = 1;
|
|
194
|
+
let end = chars.length;
|
|
195
|
+
const anchoredEnd = end > i && chars[end - 1] === "$" && !escapedAt(chars, end - 1);
|
|
196
|
+
if (anchoredEnd) end -= 1;
|
|
197
|
+
const units: Unit[] = [];
|
|
198
|
+
while (i < end) {
|
|
199
|
+
const read = atomAt(chars, i, end);
|
|
200
|
+
if (typeof read === "string") return read;
|
|
201
|
+
i = read.next;
|
|
202
|
+
const quantifier = quantifierAt(chars, i, end);
|
|
203
|
+
if (typeof quantifier === "string") return quantifier;
|
|
204
|
+
i = quantifier.next;
|
|
205
|
+
// Sized before it is expanded: a {m,n} of millions must be refused, not built.
|
|
206
|
+
const adds = quantifier.min + (quantifier.max === Infinity ? 1 : quantifier.max - quantifier.min);
|
|
207
|
+
if (units.length + adds > EXPECT_MAX_UNITS) return `expands to more than ${EXPECT_MAX_UNITS} steps`;
|
|
208
|
+
for (let n = 0; n < quantifier.min; n++) units.push({ atom: read.atom, mode: "one" });
|
|
209
|
+
if (quantifier.max === Infinity) {
|
|
210
|
+
units.push({ atom: read.atom, mode: "star" });
|
|
211
|
+
} else {
|
|
212
|
+
for (let n = quantifier.min; n < quantifier.max; n++) units.push({ atom: read.atom, mode: "optional" });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return { units, anchoredStart, anchoredEnd, ignoreCase: flags.includes("i"), dotAll: flags.includes("s") };
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function escapedAt(chars: readonly string[], index: number): boolean {
|
|
219
|
+
let slashes = 0;
|
|
220
|
+
for (let j = index - 1; j >= 0 && chars[j] === "\\"; j--) slashes++;
|
|
221
|
+
return slashes % 2 === 1;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const ESCAPE_CLASSES = new Set(["d", "w", "s", "D", "W", "S"]);
|
|
225
|
+
const SPECIALS = new Set(["(", ")", "|", "{", "}", "*", "+", "?", "^", "$"]);
|
|
226
|
+
|
|
227
|
+
function atomAt(
|
|
228
|
+
chars: readonly string[],
|
|
229
|
+
i: number,
|
|
230
|
+
end: number,
|
|
231
|
+
): { readonly atom: Atom; readonly next: number } | string {
|
|
232
|
+
const c = chars[i] ?? "";
|
|
233
|
+
if (c === "\\") {
|
|
234
|
+
const e = chars[i + 1];
|
|
235
|
+
if (e === undefined || i + 1 >= end) return "ends with a lone backslash";
|
|
236
|
+
if (ESCAPE_CLASSES.has(e)) return { atom: { kind: "class", negated: false, items: [{ kind: "escape", letter: e }] }, next: i + 2 };
|
|
237
|
+
if (/^[^A-Za-z0-9]$/.test(e)) return { atom: { kind: "char", char: e }, next: i + 2 };
|
|
238
|
+
if (e === "n") return { atom: { kind: "char", char: "\n" }, next: i + 2 };
|
|
239
|
+
if (e === "t") return { atom: { kind: "char", char: "\t" }, next: i + 2 };
|
|
240
|
+
return `uses the escape \\${e}, which the dialect does not have`;
|
|
241
|
+
}
|
|
242
|
+
if (c === "[") return classAt(chars, i, end);
|
|
243
|
+
if (c === ".") return { atom: { kind: "any" }, next: i + 1 };
|
|
244
|
+
if (c === "(" || c === ")") return "uses a group";
|
|
245
|
+
if (c === "|") return "uses an alternation";
|
|
246
|
+
if (SPECIALS.has(c)) return `has a ${c} with nothing to apply to`;
|
|
247
|
+
return { atom: { kind: "char", char: c }, next: i + 1 };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function classAt(
|
|
251
|
+
chars: readonly string[],
|
|
252
|
+
start: number,
|
|
253
|
+
end: number,
|
|
254
|
+
): { readonly atom: Atom; readonly next: number } | string {
|
|
255
|
+
let i = start + 1;
|
|
256
|
+
const negated = chars[i] === "^";
|
|
257
|
+
if (negated) i++;
|
|
258
|
+
const items: ClassItem[] = [];
|
|
259
|
+
let first = true;
|
|
260
|
+
while (i < end) {
|
|
261
|
+
const c = chars[i] ?? "";
|
|
262
|
+
if (c === "]" && !first) return { atom: { kind: "class", negated, items }, next: i + 1 };
|
|
263
|
+
first = false;
|
|
264
|
+
let from: number;
|
|
265
|
+
if (c === "\\") {
|
|
266
|
+
const e = chars[i + 1];
|
|
267
|
+
if (e === undefined || i + 1 >= end) return "has a class that never closes";
|
|
268
|
+
if (ESCAPE_CLASSES.has(e)) {
|
|
269
|
+
items.push({ kind: "escape", letter: e });
|
|
270
|
+
i += 2;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
from = (e === "n" ? "\n" : e === "t" ? "\t" : e).codePointAt(0) ?? 0;
|
|
274
|
+
i += 2;
|
|
275
|
+
} else {
|
|
276
|
+
from = c.codePointAt(0) ?? 0;
|
|
277
|
+
i += 1;
|
|
278
|
+
}
|
|
279
|
+
if (chars[i] === "-" && i + 1 < end && chars[i + 1] !== "]") {
|
|
280
|
+
const t = chars[i + 1] ?? "";
|
|
281
|
+
const to = (t === "\\" ? (chars[i + 2] ?? "") : t).codePointAt(0) ?? 0;
|
|
282
|
+
if (to < from) return "has a reversed range in a class";
|
|
283
|
+
items.push({ kind: "range", from, to });
|
|
284
|
+
i += t === "\\" ? 3 : 2;
|
|
285
|
+
} else {
|
|
286
|
+
items.push({ kind: "range", from, to: from });
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return "has a class that never closes";
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function quantifierAt(
|
|
293
|
+
chars: readonly string[],
|
|
294
|
+
i: number,
|
|
295
|
+
end: number,
|
|
296
|
+
): { readonly min: number; readonly max: number; readonly next: number } | string {
|
|
297
|
+
const c = chars[i];
|
|
298
|
+
let read: { min: number; max: number; next: number } = { min: 1, max: 1, next: i };
|
|
299
|
+
if (i < end && c === "*") read = { min: 0, max: Infinity, next: i + 1 };
|
|
300
|
+
else if (i < end && c === "+") read = { min: 1, max: Infinity, next: i + 1 };
|
|
301
|
+
else if (i < end && c === "?") read = { min: 0, max: 1, next: i + 1 };
|
|
302
|
+
else if (i < end && c === "{") {
|
|
303
|
+
let j = i + 1;
|
|
304
|
+
let body = "";
|
|
305
|
+
while (j < end && chars[j] !== "}") body += chars[j++];
|
|
306
|
+
const m = /^(\d+)(,(\d*))?$/.exec(body);
|
|
307
|
+
if (j >= end || m === null) return "has a { that is not a {m,n} quantifier";
|
|
308
|
+
const min = Number(m[1]);
|
|
309
|
+
const max = m[2] === undefined ? min : m[3] === "" ? Infinity : Number(m[3]);
|
|
310
|
+
if (max < min) return "has a {m,n} quantifier whose n is below its m";
|
|
311
|
+
read = { min, max, next: j + 1 };
|
|
312
|
+
}
|
|
313
|
+
const after = chars[read.next];
|
|
314
|
+
if (read.next < end && read.next !== i && (after === "*" || after === "+" || after === "?" || after === "{")) {
|
|
315
|
+
return "quantifies a quantifier";
|
|
316
|
+
}
|
|
317
|
+
return read;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Whether `pattern` is found in `value` (anywhere, unless anchored). */
|
|
321
|
+
export function matchPattern(pattern: CompiledPattern, value: string): boolean {
|
|
322
|
+
const text = [...value];
|
|
323
|
+
const n = pattern.units.length;
|
|
324
|
+
let states = closure(new Set([0]), pattern.units);
|
|
325
|
+
for (let pos = 0; pos <= text.length; pos++) {
|
|
326
|
+
if (states.has(n) && (!pattern.anchoredEnd || pos === text.length)) return true;
|
|
327
|
+
if (pos === text.length) break;
|
|
328
|
+
const c = text[pos] ?? "";
|
|
329
|
+
const next = new Set<number>();
|
|
330
|
+
for (const s of states) {
|
|
331
|
+
const unit = pattern.units[s];
|
|
332
|
+
if (unit === undefined || !atomMatches(unit.atom, c, pattern)) continue;
|
|
333
|
+
next.add(unit.mode === "star" ? s : s + 1);
|
|
334
|
+
}
|
|
335
|
+
if (!pattern.anchoredStart) next.add(0);
|
|
336
|
+
states = closure(next, pattern.units);
|
|
337
|
+
if (states.size === 0) return false;
|
|
338
|
+
}
|
|
339
|
+
return false;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* States reachable by skipping optional and repeating units. A walk stops
|
|
344
|
+
* at a state already reached, since everything past it was reached with
|
|
345
|
+
* it, so one closure visits each state once and a step costs at most the
|
|
346
|
+
* pattern's units.
|
|
347
|
+
*/
|
|
348
|
+
function closure(states: Set<number>, units: readonly Unit[]): Set<number> {
|
|
349
|
+
const out = new Set<number>();
|
|
350
|
+
for (const start of states) {
|
|
351
|
+
let s = start;
|
|
352
|
+
if (out.has(s)) continue;
|
|
353
|
+
out.add(s);
|
|
354
|
+
while (s < units.length && units[s]?.mode !== "one") {
|
|
355
|
+
s += 1;
|
|
356
|
+
if (out.has(s)) break;
|
|
357
|
+
out.add(s);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return out;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function atomMatches(atom: Atom, c: string, pattern: CompiledPattern): boolean {
|
|
364
|
+
switch (atom.kind) {
|
|
365
|
+
case "any":
|
|
366
|
+
return pattern.dotAll || (c !== "\n" && c !== "\r");
|
|
367
|
+
case "char":
|
|
368
|
+
return pattern.ignoreCase ? c.toLowerCase() === atom.char.toLowerCase() : c === atom.char;
|
|
369
|
+
case "class": {
|
|
370
|
+
const hit = atom.items.some((item) => classItemMatches(item, c, pattern.ignoreCase));
|
|
371
|
+
return atom.negated ? !hit : hit;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function classItemMatches(item: ClassItem, c: string, ignoreCase: boolean): boolean {
|
|
377
|
+
if (item.kind === "escape") {
|
|
378
|
+
const hit =
|
|
379
|
+
item.letter.toLowerCase() === "d" ? /\d/.test(c) : item.letter.toLowerCase() === "w" ? /\w/.test(c) : /\s/.test(c);
|
|
380
|
+
return item.letter === item.letter.toUpperCase() ? !hit : hit;
|
|
381
|
+
}
|
|
382
|
+
const inRange = (ch: string): boolean => {
|
|
383
|
+
const code = ch.codePointAt(0) ?? -1;
|
|
384
|
+
return code >= item.from && code <= item.to;
|
|
385
|
+
};
|
|
386
|
+
return inRange(c) || (ignoreCase && (inRange(c.toLowerCase()) || inRange(c.toUpperCase())));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function isLiteral(value: unknown): value is Literal {
|
|
390
|
+
return value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
394
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
395
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// The package entry: re-exports what a plugin eval's server and runner both
|
|
2
|
+
// execute with (the transcript reader, the expect guard, mock rendering and
|
|
3
|
+
// the try-stop wording), so neither reaches into a file path. Each module's
|
|
4
|
+
// header carries its rules and trade-offs.
|
|
5
|
+
|
|
6
|
+
export {
|
|
7
|
+
parseTranscript,
|
|
8
|
+
renderTranscript,
|
|
9
|
+
type TranscriptBlock,
|
|
10
|
+
type TranscriptMessage,
|
|
11
|
+
type TranscriptParse,
|
|
12
|
+
type TranscriptParseOptions,
|
|
13
|
+
} from "./transcript.js";
|
|
14
|
+
export {
|
|
15
|
+
EXPECT_MAX_PATTERN_LENGTH,
|
|
16
|
+
EXPECT_MAX_UNITS,
|
|
17
|
+
checkExpect,
|
|
18
|
+
compileExpect,
|
|
19
|
+
compilePattern,
|
|
20
|
+
matchPattern,
|
|
21
|
+
type CompiledExpect,
|
|
22
|
+
type CompiledPattern,
|
|
23
|
+
type ExpectCompile,
|
|
24
|
+
} from "./expect.js";
|
|
25
|
+
export {
|
|
26
|
+
fixturePath,
|
|
27
|
+
mockBodyOf,
|
|
28
|
+
parseToolsList,
|
|
29
|
+
renderMockAnswer,
|
|
30
|
+
type MockRender,
|
|
31
|
+
type SavedTool,
|
|
32
|
+
} from "./mock.js";
|
|
33
|
+
export { COST_LIMIT_ERROR_PREFIX, formatTryStop, parseTryStop, type TryStop } from "./try-stop.js";
|
package/src/mock.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mock's answer, as the plugin-eval format writes it: a Markdown file
|
|
3
|
+
* whose frontmatter configures the mock and whose body is what the tool
|
|
4
|
+
* returns, with two substitutions.
|
|
5
|
+
*
|
|
6
|
+
* - `{{input.<path>}}` inserts a field of the call's input, a dotted path;
|
|
7
|
+
* a string as written, anything else as JSON, a missing field as
|
|
8
|
+
* nothing.
|
|
9
|
+
* - `{{file:fixtures/<name>}}` inserts a file from the `fixtures/`
|
|
10
|
+
* directory beside the mock. The name may itself carry
|
|
11
|
+
* `{input.<path>}` in single braces, as in
|
|
12
|
+
* `{{file:fixtures/{input.order_id}.json}}`. The resolved name must
|
|
13
|
+
* stay inside `fixtures/`: an input that climbs out (`../x`) or names
|
|
14
|
+
* an absolute path is refused, and so is a missing file, as a tool
|
|
15
|
+
* error naming the fixture.
|
|
16
|
+
*
|
|
17
|
+
* The server reads frontmatter with the suite reader's YAML; the runner
|
|
18
|
+
* only needs the body, which {@link mockBodyOf} cuts from the file text,
|
|
19
|
+
* so this library carries no YAML parser.
|
|
20
|
+
*
|
|
21
|
+
* `_tools.json` is the server's saved `tools/list` response;
|
|
22
|
+
* {@link parseToolsList} reads each tool's description and input schema
|
|
23
|
+
* from it, so a mocked tool is offered as the real one is.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** A rendered answer, or why it cannot be rendered. */
|
|
27
|
+
export type MockRender =
|
|
28
|
+
| { readonly ok: true; readonly text: string }
|
|
29
|
+
| { readonly ok: false; readonly error: string };
|
|
30
|
+
|
|
31
|
+
/** The body of a mock file: everything after its frontmatter, when it has one. */
|
|
32
|
+
export function mockBodyOf(fileText: string): string {
|
|
33
|
+
const match = /^---\r?\n[\s\S]*?\r?\n---[ \t]*(\r?\n|$)/.exec(fileText);
|
|
34
|
+
return (match === null ? fileText : fileText.slice(match[0].length)).trim();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Render a fixed mock's body for one call. `readFixture` reads a path
|
|
39
|
+
* relative to the mock's server directory (always under `fixtures/`), or
|
|
40
|
+
* returns `undefined` when there is no such file.
|
|
41
|
+
*/
|
|
42
|
+
export function renderMockAnswer(
|
|
43
|
+
body: string,
|
|
44
|
+
input: unknown,
|
|
45
|
+
readFixture: (path: string) => string | undefined,
|
|
46
|
+
): MockRender {
|
|
47
|
+
let failure: string | undefined;
|
|
48
|
+
const text = replacePlaceholders(body, (ref) => {
|
|
49
|
+
if (failure !== undefined) return "";
|
|
50
|
+
if (ref.startsWith("input.")) return inputText(input, ref.slice("input.".length));
|
|
51
|
+
const named = ref
|
|
52
|
+
.slice("file:".length)
|
|
53
|
+
.replace(/\{input\.([^}]+)\}/g, (_w, path: string) => inputText(input, path));
|
|
54
|
+
const path = fixturePath(named);
|
|
55
|
+
if (path === undefined) {
|
|
56
|
+
failure = `the fixture '${named}' is outside the mock's fixtures directory`;
|
|
57
|
+
return "";
|
|
58
|
+
}
|
|
59
|
+
const content = readFixture(path);
|
|
60
|
+
if (content === undefined) {
|
|
61
|
+
failure = `the fixture '${path}' does not exist`;
|
|
62
|
+
return "";
|
|
63
|
+
}
|
|
64
|
+
return content;
|
|
65
|
+
});
|
|
66
|
+
return failure === undefined ? { ok: true, text } : { ok: false, error: failure };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Each `{{ ref }}` in `body` replaced by `render(ref)`: a ref is
|
|
71
|
+
* `input.<path>` with no space or brace, or `file:...` holding at most
|
|
72
|
+
* single-level `{...}` groups. Anything else stays as written. A scan from
|
|
73
|
+
* one `{{` stops at its closing `}}`, at the next `{{` or at a stray `}`, so
|
|
74
|
+
* a body costs time linear in its length whatever it holds, which a
|
|
75
|
+
* pattern with overlapping quantifiers cannot promise.
|
|
76
|
+
*/
|
|
77
|
+
function replacePlaceholders(body: string, render: (ref: string) => string): string {
|
|
78
|
+
let out = "";
|
|
79
|
+
let from = 0;
|
|
80
|
+
for (let open = body.indexOf("{{", from); open !== -1; open = body.indexOf("{{", from)) {
|
|
81
|
+
const close = closingOf(body, open + 2);
|
|
82
|
+
const ref = close === -1 ? "" : body.slice(open + 2, close).trim();
|
|
83
|
+
if (/^input\.[^{}\s]+$/.test(ref) || ref.startsWith("file:")) {
|
|
84
|
+
out += body.slice(from, open) + render(ref);
|
|
85
|
+
from = close + 2;
|
|
86
|
+
} else {
|
|
87
|
+
out += body.slice(from, open + 1);
|
|
88
|
+
from = open + 1;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return out + body.slice(from);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The index of the `}}` that closes a placeholder opened before `start`, or -1. */
|
|
95
|
+
function closingOf(body: string, start: number): number {
|
|
96
|
+
let depth = 0;
|
|
97
|
+
for (let k = start; k < body.length; k++) {
|
|
98
|
+
const c = body[k];
|
|
99
|
+
if (c === "{") {
|
|
100
|
+
if (depth === 1) return -1;
|
|
101
|
+
depth = 1;
|
|
102
|
+
} else if (c === "}") {
|
|
103
|
+
if (depth === 1) depth = 0;
|
|
104
|
+
else return body[k + 1] === "}" ? k : -1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return -1;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** `name` normalised under `fixtures/`, or `undefined` when it leaves it. */
|
|
111
|
+
export function fixturePath(name: string): string | undefined {
|
|
112
|
+
if (name.startsWith("/") || name.includes("\\") || name.includes("\0")) return undefined;
|
|
113
|
+
const parts: string[] = [];
|
|
114
|
+
for (const part of name.split("/")) {
|
|
115
|
+
if (part === "" || part === ".") continue;
|
|
116
|
+
if (part === "..") return undefined;
|
|
117
|
+
parts.push(part);
|
|
118
|
+
}
|
|
119
|
+
if (parts[0] !== "fixtures" || parts.length < 2) return undefined;
|
|
120
|
+
return parts.join("/");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function inputText(input: unknown, path: string): string {
|
|
124
|
+
let current: unknown = input;
|
|
125
|
+
for (const key of path.split(".")) {
|
|
126
|
+
if (typeof current !== "object" || current === null || Array.isArray(current) || !Object.hasOwn(current, key)) {
|
|
127
|
+
return "";
|
|
128
|
+
}
|
|
129
|
+
current = (current as Record<string, unknown>)[key];
|
|
130
|
+
}
|
|
131
|
+
if (typeof current === "string") return current;
|
|
132
|
+
return JSON.stringify(current) ?? "";
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** One tool of a saved `tools/list` response. */
|
|
136
|
+
export interface SavedTool {
|
|
137
|
+
readonly name: string;
|
|
138
|
+
readonly description?: string;
|
|
139
|
+
readonly inputSchema?: Readonly<Record<string, unknown>>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Read a `_tools.json`: a `tools/list` result (`{"tools": [...]}`), the
|
|
144
|
+
* JSON-RPC response around it (`{"result": {"tools": [...]}}`), or the bare
|
|
145
|
+
* list. A tool without a string name is skipped; a file of none of these
|
|
146
|
+
* shapes reads as no tools.
|
|
147
|
+
*/
|
|
148
|
+
export function parseToolsList(text: string): readonly SavedTool[] {
|
|
149
|
+
let parsed: unknown;
|
|
150
|
+
try {
|
|
151
|
+
parsed = JSON.parse(text);
|
|
152
|
+
} catch {
|
|
153
|
+
return [];
|
|
154
|
+
}
|
|
155
|
+
const list = Array.isArray(parsed)
|
|
156
|
+
? parsed
|
|
157
|
+
: isRecord(parsed) && Array.isArray(parsed["tools"])
|
|
158
|
+
? parsed["tools"]
|
|
159
|
+
: isRecord(parsed) && isRecord(parsed["result"]) && Array.isArray(parsed["result"]["tools"])
|
|
160
|
+
? parsed["result"]["tools"]
|
|
161
|
+
: [];
|
|
162
|
+
const tools: SavedTool[] = [];
|
|
163
|
+
for (const entry of list) {
|
|
164
|
+
if (!isRecord(entry) || typeof entry["name"] !== "string") continue;
|
|
165
|
+
tools.push({
|
|
166
|
+
name: entry["name"],
|
|
167
|
+
...(typeof entry["description"] === "string" ? { description: entry["description"] } : {}),
|
|
168
|
+
...(isRecord(entry["inputSchema"]) ? { inputSchema: entry["inputSchema"] } : {}),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
return tools;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
175
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
176
|
+
}
|