@stigmer/plugin-package 0.1.0-dev.20261017020314.g644422724c27
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 +194 -0
- package/README.md +72 -0
- package/client/archive.d.ts +27 -0
- package/client/archive.d.ts.map +1 -0
- package/client/archive.js +38 -0
- package/client/archive.js.map +1 -0
- package/client/builtin.d.ts +37 -0
- package/client/builtin.d.ts.map +1 -0
- package/client/builtin.js +49 -0
- package/client/builtin.js.map +1 -0
- package/client/ignore/defaults.d.ts +2 -0
- package/client/ignore/defaults.d.ts.map +1 -0
- package/client/ignore/defaults.js +140 -0
- package/client/ignore/defaults.js.map +1 -0
- package/client/ignore/match.d.ts +3 -0
- package/client/ignore/match.d.ts.map +1 -0
- package/client/ignore/match.js +166 -0
- package/client/ignore/match.js.map +1 -0
- package/client/ignore/matcher.d.ts +50 -0
- package/client/ignore/matcher.d.ts.map +1 -0
- package/client/ignore/matcher.js +116 -0
- package/client/ignore/matcher.js.map +1 -0
- package/client/ignore/pattern.d.ts +13 -0
- package/client/ignore/pattern.d.ts.map +1 -0
- package/client/ignore/pattern.js +119 -0
- package/client/ignore/pattern.js.map +1 -0
- package/client/prepare-eval.d.ts +62 -0
- package/client/prepare-eval.d.ts.map +1 -0
- package/client/prepare-eval.js +60 -0
- package/client/prepare-eval.js.map +1 -0
- package/client/prepare.d.ts +99 -0
- package/client/prepare.d.ts.map +1 -0
- package/client/prepare.js +89 -0
- package/client/prepare.js.map +1 -0
- package/client/presentation.d.ts +19 -0
- package/client/presentation.d.ts.map +1 -0
- package/client/presentation.js +44 -0
- package/client/presentation.js.map +1 -0
- package/client/refs.d.ts +76 -0
- package/client/refs.d.ts.map +1 -0
- package/client/refs.js +123 -0
- package/client/refs.js.map +1 -0
- package/client/release.d.ts +20 -0
- package/client/release.d.ts.map +1 -0
- package/client/release.js +28 -0
- package/client/release.js.map +1 -0
- package/client/reroot.d.ts +21 -0
- package/client/reroot.d.ts.map +1 -0
- package/client/reroot.js +37 -0
- package/client/reroot.js.map +1 -0
- package/client/select.d.ts +74 -0
- package/client/select.d.ts.map +1 -0
- package/client/select.js +121 -0
- package/client/select.js.map +1 -0
- package/client/vocabulary.d.ts +13 -0
- package/client/vocabulary.d.ts.map +1 -0
- package/client/vocabulary.js +17 -0
- package/client/vocabulary.js.map +1 -0
- package/client.d.ts +51 -0
- package/client.d.ts.map +1 -0
- package/client.js +51 -0
- package/client.js.map +1 -0
- package/detect.d.ts +83 -0
- package/detect.d.ts.map +1 -0
- package/detect.js +210 -0
- package/detect.js.map +1 -0
- package/dialects/claude.d.ts +34 -0
- package/dialects/claude.d.ts.map +1 -0
- package/dialects/claude.js +84 -0
- package/dialects/claude.js.map +1 -0
- package/dialects/codex.d.ts +20 -0
- package/dialects/codex.d.ts.map +1 -0
- package/dialects/codex.js +21 -0
- package/dialects/codex.js.map +1 -0
- package/dialects/cursor.d.ts +25 -0
- package/dialects/cursor.d.ts.map +1 -0
- package/dialects/cursor.js +66 -0
- package/dialects/cursor.js.map +1 -0
- package/dialects/manifest.d.ts +164 -0
- package/dialects/manifest.d.ts.map +1 -0
- package/dialects/manifest.js +256 -0
- package/dialects/manifest.js.map +1 -0
- package/dialects/open.d.ts +19 -0
- package/dialects/open.d.ts.map +1 -0
- package/dialects/open.js +49 -0
- package/dialects/open.js.map +1 -0
- package/documents.d.ts +58 -0
- package/documents.d.ts.map +1 -0
- package/documents.js +121 -0
- package/documents.js.map +1 -0
- package/evals/eval-dir.d.ts +59 -0
- package/evals/eval-dir.d.ts.map +1 -0
- package/evals/eval-dir.js +84 -0
- package/evals/eval-dir.js.map +1 -0
- package/evals/fields.d.ts +78 -0
- package/evals/fields.d.ts.map +1 -0
- package/evals/fields.js +156 -0
- package/evals/fields.js.map +1 -0
- package/evals/glob.d.ts +40 -0
- package/evals/glob.d.ts.map +1 -0
- package/evals/glob.js +146 -0
- package/evals/glob.js.map +1 -0
- package/evals/graders.d.ts +52 -0
- package/evals/graders.d.ts.map +1 -0
- package/evals/graders.js +279 -0
- package/evals/graders.js.map +1 -0
- package/evals/markdown.d.ts +24 -0
- package/evals/markdown.d.ts.map +1 -0
- package/evals/markdown.js +48 -0
- package/evals/markdown.js.map +1 -0
- package/evals/mocks.d.ts +50 -0
- package/evals/mocks.d.ts.map +1 -0
- package/evals/mocks.js +182 -0
- package/evals/mocks.js.map +1 -0
- package/evals/read-eval-suite.d.ts +110 -0
- package/evals/read-eval-suite.d.ts.map +1 -0
- package/evals/read-eval-suite.js +603 -0
- package/evals/read-eval-suite.js.map +1 -0
- package/evals/summary.d.ts +61 -0
- package/evals/summary.d.ts.map +1 -0
- package/evals/summary.js +69 -0
- package/evals/summary.js.map +1 -0
- package/evals/types.d.ts +203 -0
- package/evals/types.d.ts.map +1 -0
- package/evals/types.js +21 -0
- package/evals/types.js.map +1 -0
- package/files.d.ts +135 -0
- package/files.d.ts.map +1 -0
- package/files.js +209 -0
- package/files.js.map +1 -0
- package/frontmatter.d.ts +51 -0
- package/frontmatter.d.ts.map +1 -0
- package/frontmatter.js +61 -0
- package/frontmatter.js.map +1 -0
- package/index.d.ts +37 -0
- package/index.d.ts.map +1 -0
- package/index.js +33 -0
- package/index.js.map +1 -0
- package/marketplace/messages.d.ts +20 -0
- package/marketplace/messages.d.ts.map +1 -0
- package/marketplace/messages.js +49 -0
- package/marketplace/messages.js.map +1 -0
- package/marketplace/outcome.d.ts +70 -0
- package/marketplace/outcome.d.ts.map +1 -0
- package/marketplace/outcome.js +29 -0
- package/marketplace/outcome.js.map +1 -0
- package/marketplace/read-marketplace.d.ts +52 -0
- package/marketplace/read-marketplace.d.ts.map +1 -0
- package/marketplace/read-marketplace.js +351 -0
- package/marketplace/read-marketplace.js.map +1 -0
- package/messages.d.ts +55 -0
- package/messages.d.ts.map +1 -0
- package/messages.js +167 -0
- package/messages.js.map +1 -0
- package/normalise/hooks.d.ts +91 -0
- package/normalise/hooks.d.ts.map +1 -0
- package/normalise/hooks.js +437 -0
- package/normalise/hooks.js.map +1 -0
- package/normalise/ignored.d.ts +34 -0
- package/normalise/ignored.d.ts.map +1 -0
- package/normalise/ignored.js +78 -0
- package/normalise/ignored.js.map +1 -0
- package/normalise/mcp-servers.d.ts +50 -0
- package/normalise/mcp-servers.d.ts.map +1 -0
- package/normalise/mcp-servers.js +398 -0
- package/normalise/mcp-servers.js.map +1 -0
- package/normalise/settings.d.ts +32 -0
- package/normalise/settings.d.ts.map +1 -0
- package/normalise/settings.js +72 -0
- package/normalise/settings.js.map +1 -0
- package/normalise/skills.d.ts +39 -0
- package/normalise/skills.d.ts.map +1 -0
- package/normalise/skills.js +135 -0
- package/normalise/skills.js.map +1 -0
- package/normalise/stigmer-folder.d.ts +17 -0
- package/normalise/stigmer-folder.d.ts.map +1 -0
- package/normalise/stigmer-folder.js +19 -0
- package/normalise/stigmer-folder.js.map +1 -0
- package/normalise/sub-agents.d.ts +54 -0
- package/normalise/sub-agents.d.ts.map +1 -0
- package/normalise/sub-agents.js +219 -0
- package/normalise/sub-agents.js.map +1 -0
- package/normalise/variables.d.ts +31 -0
- package/normalise/variables.d.ts.map +1 -0
- package/normalise/variables.js +170 -0
- package/normalise/variables.js.map +1 -0
- package/outcome.d.ts +59 -0
- package/outcome.d.ts.map +1 -0
- package/outcome.js +32 -0
- package/outcome.js.map +1 -0
- package/package.json +47 -0
- package/placeholders.d.ts +65 -0
- package/placeholders.d.ts.map +1 -0
- package/placeholders.js +99 -0
- package/placeholders.js.map +1 -0
- package/presentation.d.ts +53 -0
- package/presentation.d.ts.map +1 -0
- package/presentation.js +156 -0
- package/presentation.js.map +1 -0
- package/read-plugin-package.d.ts +20 -0
- package/read-plugin-package.d.ts.map +1 -0
- package/read-plugin-package.js +73 -0
- package/read-plugin-package.js.map +1 -0
- package/src/client/archive.ts +44 -0
- package/src/client/builtin.ts +60 -0
- package/src/client/ignore/defaults.ts +155 -0
- package/src/client/ignore/match.ts +158 -0
- package/src/client/ignore/matcher.ts +156 -0
- package/src/client/ignore/pattern.ts +132 -0
- package/src/client/prepare-eval.ts +92 -0
- package/src/client/prepare.ts +160 -0
- package/src/client/presentation.ts +47 -0
- package/src/client/refs.ts +170 -0
- package/src/client/release.ts +29 -0
- package/src/client/reroot.ts +35 -0
- package/src/client/select.ts +161 -0
- package/src/client/vocabulary.ts +19 -0
- package/src/client.ts +102 -0
- package/src/detect.ts +245 -0
- package/src/dialects/claude.ts +102 -0
- package/src/dialects/codex.ts +26 -0
- package/src/dialects/cursor.ts +77 -0
- package/src/dialects/manifest.ts +316 -0
- package/src/dialects/open.ts +50 -0
- package/src/documents.ts +158 -0
- package/src/evals/eval-dir.ts +101 -0
- package/src/evals/fields.ts +184 -0
- package/src/evals/glob.ts +141 -0
- package/src/evals/graders.ts +293 -0
- package/src/evals/markdown.ts +56 -0
- package/src/evals/mocks.ts +202 -0
- package/src/evals/read-eval-suite.ts +713 -0
- package/src/evals/summary.ts +100 -0
- package/src/evals/types.ts +217 -0
- package/src/files.ts +235 -0
- package/src/frontmatter.ts +70 -0
- package/src/index.ts +129 -0
- package/src/marketplace/messages.ts +61 -0
- package/src/marketplace/outcome.ts +91 -0
- package/src/marketplace/read-marketplace.ts +388 -0
- package/src/messages.ts +248 -0
- package/src/normalise/hooks.ts +469 -0
- package/src/normalise/ignored.ts +79 -0
- package/src/normalise/mcp-servers.ts +423 -0
- package/src/normalise/settings.ts +81 -0
- package/src/normalise/skills.ts +145 -0
- package/src/normalise/stigmer-folder.ts +23 -0
- package/src/normalise/sub-agents.ts +231 -0
- package/src/normalise/variables.ts +181 -0
- package/src/outcome.ts +158 -0
- package/src/placeholders.ts +110 -0
- package/src/presentation.ts +170 -0
- package/src/read-plugin-package.ts +80 -0
- package/src/testing.ts +347 -0
- package/src/tool-names.ts +30 -0
- package/src/trim.ts +10 -0
- package/src/types.ts +214 -0
- package/testing.d.ts +127 -0
- package/testing.d.ts.map +1 -0
- package/testing.js +266 -0
- package/testing.js.map +1 -0
- package/tool-names.d.ts +21 -0
- package/tool-names.d.ts.map +1 -0
- package/tool-names.js +28 -0
- package/tool-names.js.map +1 -0
- package/trim.d.ts +7 -0
- package/trim.d.ts.map +1 -0
- package/trim.js +12 -0
- package/trim.js.map +1 -0
- package/types.d.ts +179 -0
- package/types.d.ts.map +1 -0
- package/types.js +19 -0
- package/types.js.map +1 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The eval suite as a plugin stores it: what the plugin page and the CLI
|
|
3
|
+
* list before anyone runs it, bounded so the plugin's row stays within one
|
|
4
|
+
* API message.
|
|
5
|
+
*
|
|
6
|
+
* An archive may hold up to its entry limit of files with long names, and
|
|
7
|
+
* every case and finding would otherwise ride on the plugin's status. So
|
|
8
|
+
* the summary keeps the first {@link EVAL_SUMMARY_MAX_CASES} cases and the
|
|
9
|
+
* first {@link EVAL_SUMMARY_MAX_FINDINGS} findings, in the reader's order,
|
|
10
|
+
* the first {@link EVAL_SUMMARY_MAX_CASE_TAGS} tags of each case, and cuts
|
|
11
|
+
* every text it copies from the suite (a case's name, directory and tags,
|
|
12
|
+
* a finding's path) to {@link EVAL_SUMMARY_MAX_TEXT} characters and an
|
|
13
|
+
* ellipsis. A finding's path is cut in its message too, where the message
|
|
14
|
+
* starts with it, and the message is then cut to
|
|
15
|
+
* {@link EVAL_SUMMARY_MAX_MESSAGE}: longer than the other texts, since it
|
|
16
|
+
* holds the cut path and then the problem, which quotes up to
|
|
17
|
+
* {@link EVAL_SUMMARY_MAX_TEXT} characters of a value itself. The
|
|
18
|
+
* suite's tags are the listed cases' kept tags, cut, once each, sorted,
|
|
19
|
+
* and the first {@link EVAL_SUMMARY_MAX_SUITE_TAGS} of those. `caseCount`
|
|
20
|
+
* still counts every case, and the suite's directory is copied whole: the
|
|
21
|
+
* runner leaves it out of every mount, and the reader bounds it. The eval
|
|
22
|
+
* itself reads the archive again, so nothing a run needs is cut.
|
|
23
|
+
*
|
|
24
|
+
* Pinned by `__tests__/eval-summary.test.ts`.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { cutText, excerpt, QUOTED_TEXT_LIMIT } from "./fields.js";
|
|
28
|
+
import type { EvalSuite, EvalSuiteFinding } from "./types.js";
|
|
29
|
+
|
|
30
|
+
/** The most cases a stored summary lists. */
|
|
31
|
+
export const EVAL_SUMMARY_MAX_CASES = 200;
|
|
32
|
+
|
|
33
|
+
/** The most findings a stored summary keeps. */
|
|
34
|
+
export const EVAL_SUMMARY_MAX_FINDINGS = 50;
|
|
35
|
+
|
|
36
|
+
/** The most characters of any text a stored summary copies, then an ellipsis. */
|
|
37
|
+
export const EVAL_SUMMARY_MAX_TEXT = QUOTED_TEXT_LIMIT;
|
|
38
|
+
|
|
39
|
+
/** The most characters of a finding's path a stored summary keeps, then an ellipsis. */
|
|
40
|
+
export const EVAL_SUMMARY_MAX_PATH = EVAL_SUMMARY_MAX_TEXT;
|
|
41
|
+
|
|
42
|
+
/** The most characters of a finding's message a stored summary keeps, then an ellipsis. */
|
|
43
|
+
export const EVAL_SUMMARY_MAX_MESSAGE = 1000;
|
|
44
|
+
|
|
45
|
+
/** The most tags a stored summary lists for one case. */
|
|
46
|
+
export const EVAL_SUMMARY_MAX_CASE_TAGS = 32;
|
|
47
|
+
|
|
48
|
+
/** The most tags a stored summary lists for the suite. */
|
|
49
|
+
export const EVAL_SUMMARY_MAX_SUITE_TAGS = 200;
|
|
50
|
+
|
|
51
|
+
/** One case as the summary lists it. */
|
|
52
|
+
export interface EvalSuiteSummaryCase {
|
|
53
|
+
readonly name: string;
|
|
54
|
+
readonly dir: string;
|
|
55
|
+
readonly tags: readonly string[];
|
|
56
|
+
/** The feature Stigmer does not run yet; absent when the case runs. */
|
|
57
|
+
readonly unsupported?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The suite as a plugin stores it. */
|
|
61
|
+
export interface EvalSuiteSummary {
|
|
62
|
+
readonly dir: string;
|
|
63
|
+
/** Every case the suite holds, listed or not. */
|
|
64
|
+
readonly caseCount: number;
|
|
65
|
+
/** The listed cases' tags, once each, sorted, the first EVAL_SUMMARY_MAX_SUITE_TAGS of them. */
|
|
66
|
+
readonly caseTags: readonly string[];
|
|
67
|
+
readonly cases: readonly EvalSuiteSummaryCase[];
|
|
68
|
+
readonly findings: readonly EvalSuiteFinding[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The suite, bounded for storage on the plugin. */
|
|
72
|
+
export function summariseEvalSuite(suite: EvalSuite): EvalSuiteSummary {
|
|
73
|
+
const listed = suite.cases.slice(0, EVAL_SUMMARY_MAX_CASES);
|
|
74
|
+
return {
|
|
75
|
+
dir: suite.dir,
|
|
76
|
+
caseCount: suite.cases.length,
|
|
77
|
+
caseTags: [...new Set(listed.flatMap(keptTags))].sort().slice(0, EVAL_SUMMARY_MAX_SUITE_TAGS),
|
|
78
|
+
cases: listed.map((c) => ({
|
|
79
|
+
name: excerpt(c.name),
|
|
80
|
+
dir: excerpt(c.dir),
|
|
81
|
+
tags: keptTags(c),
|
|
82
|
+
...(c.unsupported !== undefined && { unsupported: c.unsupported }),
|
|
83
|
+
})),
|
|
84
|
+
findings: suite.findings.slice(0, EVAL_SUMMARY_MAX_FINDINGS).map(cutFinding),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A case's tags as the summary keeps them: the first few, each cut. */
|
|
89
|
+
function keptTags(evalCase: EvalSuite["cases"][number]): readonly string[] {
|
|
90
|
+
return evalCase.tags.slice(0, EVAL_SUMMARY_MAX_CASE_TAGS).map(excerpt);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A finding with its path cut, in the field and at the start of its message, then its message cut. */
|
|
94
|
+
function cutFinding(finding: EvalSuiteFinding): EvalSuiteFinding {
|
|
95
|
+
const path = excerpt(finding.path);
|
|
96
|
+
const prefix = `${finding.path}: `;
|
|
97
|
+
const named = path !== finding.path && finding.message.startsWith(prefix) ? `${path}: ${finding.message.slice(prefix.length)}` : finding.message;
|
|
98
|
+
const message = cutText(named, EVAL_SUMMARY_MAX_MESSAGE);
|
|
99
|
+
return path === finding.path && message === finding.message ? finding : { ...finding, path, message };
|
|
100
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The eval suite model: what `readEvalSuite` returns for a plugin's
|
|
3
|
+
* `evals/` folder, in Claude Code's plugin-eval format (case format
|
|
4
|
+
* `schema_version: "1.1"`).
|
|
5
|
+
*
|
|
6
|
+
* One normalised shape whichever file a field came from: a case may be a
|
|
7
|
+
* `prompt.md`, a `case.yaml`, or both, and its graders may be
|
|
8
|
+
* `graders/*.md` files or `case.yaml` entries. The reader applies the
|
|
9
|
+
* format's precedence (prompt.md frontmatter over case.yaml, graders from
|
|
10
|
+
* case.yaml first, then graders/*.md in path order) so a consumer never
|
|
11
|
+
* re-derives it. Defaults the format documents are applied here too (runs
|
|
12
|
+
* 3, max_turns 10, timeout 300 s, weight 1, focus last_message, tool_used
|
|
13
|
+
* min 1), so the server's graders and the CLI's offline listing read the
|
|
14
|
+
* same values.
|
|
15
|
+
*
|
|
16
|
+
* Features Stigmer does not run yet are still read, so a case that uses
|
|
17
|
+
* one is listed with the feature named (`unsupported`) rather than run
|
|
18
|
+
* without it: a degraded run is a wrong score.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { JsonObject } from "../documents.js";
|
|
22
|
+
|
|
23
|
+
/** What a `regex` grader's `target` or an `llm` grader's `focus` names. */
|
|
24
|
+
export type EvalFocus =
|
|
25
|
+
| { readonly kind: "last_message" }
|
|
26
|
+
| { readonly kind: "trace" }
|
|
27
|
+
| { readonly kind: "files" }
|
|
28
|
+
| { readonly kind: "file"; readonly path: string }
|
|
29
|
+
| { readonly kind: "mock_calls" };
|
|
30
|
+
|
|
31
|
+
/** A grader's `arm` setting; `undefined` when the grader leaves it unset. */
|
|
32
|
+
export type EvalGraderArm = "with-only" | "both";
|
|
33
|
+
|
|
34
|
+
/** A tool reference in `tool_order`: a name, or a name and an input regex. */
|
|
35
|
+
export interface EvalToolRef {
|
|
36
|
+
readonly tool: string;
|
|
37
|
+
/** JavaScript regex over the call's JSON-encoded input; absent matches any. */
|
|
38
|
+
readonly inputMatch?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The type-specific options of a grader. */
|
|
42
|
+
export type EvalGraderCheck =
|
|
43
|
+
| {
|
|
44
|
+
readonly type: "regex";
|
|
45
|
+
readonly pattern: string;
|
|
46
|
+
/** JavaScript regex flags, e.g. "i". Empty when unset. */
|
|
47
|
+
readonly flags: string;
|
|
48
|
+
/** `contains` (the default), `not_contains`, or exactly N matches. */
|
|
49
|
+
readonly match:
|
|
50
|
+
| { readonly kind: "contains" }
|
|
51
|
+
| { readonly kind: "not_contains" }
|
|
52
|
+
| { readonly kind: "count"; readonly count: number };
|
|
53
|
+
readonly target: EvalFocus;
|
|
54
|
+
}
|
|
55
|
+
| {
|
|
56
|
+
readonly type: "tool_used";
|
|
57
|
+
readonly tool: string;
|
|
58
|
+
readonly inputMatch?: string;
|
|
59
|
+
/** Default 1. */
|
|
60
|
+
readonly min: number;
|
|
61
|
+
/** Absent means unlimited. */
|
|
62
|
+
readonly max?: number;
|
|
63
|
+
}
|
|
64
|
+
| {
|
|
65
|
+
readonly type: "tool_order";
|
|
66
|
+
readonly before: EvalToolRef;
|
|
67
|
+
readonly after: EvalToolRef;
|
|
68
|
+
}
|
|
69
|
+
| {
|
|
70
|
+
readonly type: "file_exists";
|
|
71
|
+
/** A glob over the paths created during the run. */
|
|
72
|
+
readonly path: string;
|
|
73
|
+
/** Default true. */
|
|
74
|
+
readonly exists: boolean;
|
|
75
|
+
}
|
|
76
|
+
| {
|
|
77
|
+
readonly type: "llm";
|
|
78
|
+
readonly criteria: string;
|
|
79
|
+
readonly focus: EvalFocus;
|
|
80
|
+
}
|
|
81
|
+
| {
|
|
82
|
+
readonly type: "baseline";
|
|
83
|
+
/** A `.jsonl` transcript, relative to the case directory. */
|
|
84
|
+
readonly baselineFile: string;
|
|
85
|
+
readonly criteria: string;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export type EvalGraderType = EvalGraderCheck["type"];
|
|
89
|
+
|
|
90
|
+
/** One grader of a case. */
|
|
91
|
+
export interface EvalGrader {
|
|
92
|
+
/** The `graders/<name>.md` filename without `.md`, or the entry's `name`. */
|
|
93
|
+
readonly name: string;
|
|
94
|
+
/** Plugin-relative path of the file that declared it. */
|
|
95
|
+
readonly path: string;
|
|
96
|
+
/** Any positive number; default 1. */
|
|
97
|
+
readonly weight: number;
|
|
98
|
+
readonly arm?: EvalGraderArm;
|
|
99
|
+
readonly check: EvalGraderCheck;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The `context` block of a `case.yaml`. Each path is relative to the case
|
|
104
|
+
* directory, normalised (no `./`, no `..`), and checked against the case's
|
|
105
|
+
* files: the script a file, each directory a directory, the history a
|
|
106
|
+
* `.jsonl` file whose conversation can be resumed.
|
|
107
|
+
*/
|
|
108
|
+
export interface EvalCaseContext {
|
|
109
|
+
readonly scaffoldScript?: string;
|
|
110
|
+
readonly historyFile?: string;
|
|
111
|
+
readonly addDirs: readonly string[];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** One mocked tool of a server, as the case's tries answer it. */
|
|
115
|
+
export interface EvalMockTool {
|
|
116
|
+
readonly tool: string;
|
|
117
|
+
/** Plugin-relative path of the file whose body answers: the tool's own `<tool>.md`, or the server's `_server.md`. */
|
|
118
|
+
readonly file: string;
|
|
119
|
+
/** `fixed` returns the body; `agent` has the judge model answer from it. */
|
|
120
|
+
readonly kind: "fixed" | "agent";
|
|
121
|
+
/** `fixed` only: the body is returned as a tool error. */
|
|
122
|
+
readonly error: boolean;
|
|
123
|
+
/** The `expect:` block as written, already compiled once by the reader. */
|
|
124
|
+
readonly expect?: JsonObject;
|
|
125
|
+
/** `agent` only: the only conditions under which the agent may end the run. */
|
|
126
|
+
readonly abortWhen?: string;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** One plugin MCP server a case mocks: its tool mocks, suite and case merged file by file. */
|
|
130
|
+
export interface EvalMockServer {
|
|
131
|
+
/** The server's key in the plugin's MCP configuration. */
|
|
132
|
+
readonly server: string;
|
|
133
|
+
/** The mocked tools, sorted by name; empty when the server's directory holds no tool mock. */
|
|
134
|
+
readonly tools: readonly EvalMockTool[];
|
|
135
|
+
/** Plugin-relative path of the server's `_tools.json`, when there is one. */
|
|
136
|
+
readonly toolsFile?: string;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** One case of the suite, normalised. */
|
|
140
|
+
export interface EvalCase {
|
|
141
|
+
/** `name`, else the directory's name. */
|
|
142
|
+
readonly name: string;
|
|
143
|
+
/** The case directory, relative to the plugin's root, e.g. "evals/first-case". */
|
|
144
|
+
readonly dir: string;
|
|
145
|
+
readonly description?: string;
|
|
146
|
+
readonly tags: readonly string[];
|
|
147
|
+
/** The prompt sent to the agent, exactly as written. */
|
|
148
|
+
readonly prompt: string;
|
|
149
|
+
/** Tries per arm, 1 to 50; default 3. */
|
|
150
|
+
readonly runs: number;
|
|
151
|
+
/** As written; Stigmer's catalog decides whether it names a model. */
|
|
152
|
+
readonly model?: string;
|
|
153
|
+
/** Turn cap, 1 to 200; default 10. */
|
|
154
|
+
readonly maxTurns: number;
|
|
155
|
+
/** Wall-clock cap per try, 1 to 3600; default 300. */
|
|
156
|
+
readonly timeoutSeconds: number;
|
|
157
|
+
/** The case's `allowed_tools`, as written. */
|
|
158
|
+
readonly allowedTools: readonly string[];
|
|
159
|
+
readonly appendSystemPrompt?: string;
|
|
160
|
+
/** The case's `env`; keys match `EVAL_[A-Z0-9_]*`. */
|
|
161
|
+
readonly env: Readonly<Record<string, string>>;
|
|
162
|
+
/**
|
|
163
|
+
* `plugins` as written, relative to the case directory. An entry that
|
|
164
|
+
* resolves to the plugin's root names the plugin under test (the format's
|
|
165
|
+
* override of auto-detect); any other entry makes the case
|
|
166
|
+
* `unsupported: "plugins"`, since Stigmer evaluates the installed plugin
|
|
167
|
+
* alone.
|
|
168
|
+
*/
|
|
169
|
+
readonly plugins: readonly string[];
|
|
170
|
+
readonly context: EvalCaseContext;
|
|
171
|
+
/** The plugin-relative paths of every file under the case directory. */
|
|
172
|
+
readonly files: readonly string[];
|
|
173
|
+
readonly graders: readonly EvalGrader[];
|
|
174
|
+
/** The plugin's MCP servers this case has mock directories for, sorted by key. */
|
|
175
|
+
readonly mocks: readonly EvalMockServer[];
|
|
176
|
+
/** Whether the case's or the suite's mocks hold adopted replays (`mocks/.replay/`), which Stigmer does not read. */
|
|
177
|
+
readonly replays: boolean;
|
|
178
|
+
/**
|
|
179
|
+
* The first feature this case uses that Stigmer does not run yet, as a
|
|
180
|
+
* stable phrase the report prints after "not run: ", e.g. "plugins";
|
|
181
|
+
* absent when Stigmer runs the case.
|
|
182
|
+
*/
|
|
183
|
+
readonly unsupported?: string;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** A problem reading the suite: a case that cannot load, an unknown key. */
|
|
187
|
+
export interface EvalSuiteFinding {
|
|
188
|
+
readonly kind:
|
|
189
|
+
| "eval-dir-invalid"
|
|
190
|
+
| "eval-case-invalid"
|
|
191
|
+
| "eval-grader-invalid"
|
|
192
|
+
| "eval-field-unknown"
|
|
193
|
+
| "eval-mock-invalid"
|
|
194
|
+
| "eval-schema-version-unsupported";
|
|
195
|
+
/** One user-facing sentence naming the file and field, as the format reports it. */
|
|
196
|
+
readonly message: string;
|
|
197
|
+
/** The plugin-relative path the finding points at. */
|
|
198
|
+
readonly path: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The suite: the cases that loaded and the findings for those that did not. */
|
|
202
|
+
export interface EvalSuite {
|
|
203
|
+
/**
|
|
204
|
+
* A plugin's suite: "evals", or the manifest's `experimental.evals` when
|
|
205
|
+
* it is usable. A pushed folder: the empty string, the archive's root.
|
|
206
|
+
*/
|
|
207
|
+
readonly dir: string;
|
|
208
|
+
/** Cases in directory order (sorted by path). */
|
|
209
|
+
readonly cases: readonly EvalCase[];
|
|
210
|
+
readonly findings: readonly EvalSuiteFinding[];
|
|
211
|
+
/**
|
|
212
|
+
* The servers a mock may stand in for, sorted. A plugin's suite: the MCP
|
|
213
|
+
* server keys the plugin declares. A pushed folder: the mock directories
|
|
214
|
+
* its cases read, each resolved against an agent's servers when it runs.
|
|
215
|
+
*/
|
|
216
|
+
readonly servers: readonly string[];
|
|
217
|
+
}
|
package/src/files.ts
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reader the library is pure over, and the path discipline every path in
|
|
3
|
+
* a package obeys.
|
|
4
|
+
*
|
|
5
|
+
* `PluginFiles` is a sorted list of SIZED entries plus `read`. The size is
|
|
6
|
+
* declared up front so the library can refuse an over-cap document before a
|
|
7
|
+
* byte is read: an archive reader may hold a deflated entry whose declared
|
|
8
|
+
* size is the only defence against inflating a bomb, and a cap checked after
|
|
9
|
+
* `read` would be no defence at all. The reader's contract is that `read`
|
|
10
|
+
* never returns more than `size` bytes (a directory reader uses the file's
|
|
11
|
+
* stat size; an archive reader passes the central directory's declared size
|
|
12
|
+
* as its inflate limit). The library checks both sides anyway: the declared
|
|
13
|
+
* size before reading, the returned length after, refusing either as
|
|
14
|
+
* `document-too-large`. Nothing here touches the filesystem or any `node:*`
|
|
15
|
+
* module; the CLI's directory walker and the server's archive reader each
|
|
16
|
+
* implement this interface at their edge.
|
|
17
|
+
*
|
|
18
|
+
* Containment is lexical here and physical in each reader. A listed path
|
|
19
|
+
* that is absolute, contains `..` or a backslash, or has an empty segment is
|
|
20
|
+
* refused by the library; a reader never follows a symlink, and an archive
|
|
21
|
+
* has none. A path DECLARED in a manifest must begin with `./` (or be `.`),
|
|
22
|
+
* the open format's rule for every plugin-relative path.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { PluginErrorKind } from "./outcome.js";
|
|
26
|
+
import { trimTrailing } from "./trim.js";
|
|
27
|
+
|
|
28
|
+
export interface PluginFileEntry {
|
|
29
|
+
/** Plugin-relative POSIX path, no leading `./`, files only. */
|
|
30
|
+
readonly path: string;
|
|
31
|
+
/** The entry's declared size in bytes; `read` returns at most this many. */
|
|
32
|
+
readonly size: number;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PluginFiles {
|
|
36
|
+
/** Sorted by path (code-point order), files only, every path contained. */
|
|
37
|
+
readonly entries: readonly PluginFileEntry[];
|
|
38
|
+
/** The bytes of one listed path. */
|
|
39
|
+
read(path: string): Uint8Array;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Per-document byte caps, checked from declared sizes. A plugin's documents
|
|
44
|
+
* are small by nature; the caps exist so no reader can be made to inflate
|
|
45
|
+
* a large entry through the library, not to bound legitimate content.
|
|
46
|
+
*/
|
|
47
|
+
export const PLUGIN_DOCUMENT_LIMITS = {
|
|
48
|
+
/** A manifest (`plugin.json` in any dialect). Real manifests are a few KB. */
|
|
49
|
+
manifest: 256 * 1024,
|
|
50
|
+
/** An MCP configuration file. */
|
|
51
|
+
mcpConfig: 256 * 1024,
|
|
52
|
+
/** A hooks file (`hooks/hooks.json` or a declared one): JSON, the manifest's cap. */
|
|
53
|
+
hooks: 256 * 1024,
|
|
54
|
+
/** A Claude plugin's `settings.json`: JSON, the manifest's cap. */
|
|
55
|
+
settings: 256 * 1024,
|
|
56
|
+
/**
|
|
57
|
+
* A `SKILL.md`. The server's skill push gate inflates a `SKILL.md` alone
|
|
58
|
+
* under the same 1 MB cap, so a skill this library accepts is one the push
|
|
59
|
+
* gate accepts.
|
|
60
|
+
*/
|
|
61
|
+
skillMd: 1024 * 1024,
|
|
62
|
+
/** A sub-agent file (`agents/*.md`): a prompt, so the `SKILL.md` cap. */
|
|
63
|
+
subAgent: 1024 * 1024,
|
|
64
|
+
/**
|
|
65
|
+
* A plugin's logo, the image a storefront card shows. Checked from the
|
|
66
|
+
* listing's declared size before a client hands the file's URL to an
|
|
67
|
+
* `<img>`; measured across the three vendors' catalogues on 2026-09-18,
|
|
68
|
+
* the largest published logo is 361 KB and the median 7.5 KB.
|
|
69
|
+
*/
|
|
70
|
+
logo: 1024 * 1024,
|
|
71
|
+
/**
|
|
72
|
+
* A marketplace file in any dialect. The largest public catalogue
|
|
73
|
+
* (`cursor/plugins`, 79 entries with descriptions) is under 32 KB; the cap
|
|
74
|
+
* leaves room for catalogues an order of magnitude larger.
|
|
75
|
+
*/
|
|
76
|
+
marketplace: 1024 * 1024,
|
|
77
|
+
/**
|
|
78
|
+
* A file of a plugin's eval suite the reader opens: a `prompt.md`, a
|
|
79
|
+
* `case.yaml`, a `graders/*.md`. Prompts and rubrics, so the `SKILL.md`
|
|
80
|
+
* cap.
|
|
81
|
+
*/
|
|
82
|
+
evalDocument: 1024 * 1024,
|
|
83
|
+
} as const;
|
|
84
|
+
|
|
85
|
+
export type PluginDocumentClass = keyof typeof PLUGIN_DOCUMENT_LIMITS;
|
|
86
|
+
|
|
87
|
+
const encoder = new TextEncoder();
|
|
88
|
+
// `fatal: false` keeps the decoder from throwing on a malformed sequence: a
|
|
89
|
+
// document with bad UTF-8 reaches the JSON or YAML parser, which refuses it
|
|
90
|
+
// with its own sentence, rather than crashing the read.
|
|
91
|
+
const decoder = new TextDecoder("utf-8", { fatal: false });
|
|
92
|
+
|
|
93
|
+
/** An in-memory `PluginFiles` over path -> content; the testing builders' product. */
|
|
94
|
+
export function inMemoryPluginFiles(files: ReadonlyMap<string, Uint8Array | string>): PluginFiles {
|
|
95
|
+
const bytes = new Map<string, Uint8Array>();
|
|
96
|
+
for (const [path, content] of files) {
|
|
97
|
+
bytes.set(path, typeof content === "string" ? encoder.encode(content) : content);
|
|
98
|
+
}
|
|
99
|
+
const entries = [...bytes.entries()]
|
|
100
|
+
.map(([path, content]) => ({ path, size: content.length }))
|
|
101
|
+
.sort((a, b) => comparePaths(a.path, b.path));
|
|
102
|
+
return {
|
|
103
|
+
entries,
|
|
104
|
+
read(path) {
|
|
105
|
+
const content = bytes.get(path);
|
|
106
|
+
if (content === undefined) {
|
|
107
|
+
throw new Error(`plugin file '${path}' is not listed`);
|
|
108
|
+
}
|
|
109
|
+
return content;
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Code-point order, the order `entries` is sorted in. */
|
|
115
|
+
export function comparePaths(a: string, b: string): number {
|
|
116
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** UTF-8 text with a leading byte-order mark removed (a manifest saved with a BOM was a known load failure elsewhere). */
|
|
120
|
+
export function decodeUtf8(bytes: Uint8Array): string {
|
|
121
|
+
const text = decoder.decode(bytes);
|
|
122
|
+
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Lexical containment of a LISTED path: relative, forward slashes only, no
|
|
127
|
+
* `.` or `..` segments, no empty segments. Readers produce such paths; the
|
|
128
|
+
* library refuses a reader that did not.
|
|
129
|
+
*/
|
|
130
|
+
export function isContainedPath(path: string): boolean {
|
|
131
|
+
if (path === "" || path.startsWith("/") || path.includes("\\")) return false;
|
|
132
|
+
return path.split("/").every((segment) => segment !== "" && segment !== "." && segment !== "..");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export type DeclaredPathOutcome =
|
|
136
|
+
| { readonly ok: true; readonly path: string }
|
|
137
|
+
| { readonly ok: false; readonly kind: Extract<PluginErrorKind, "path-not-relative" | "path-escapes-root" | "path-glob-unsupported"> };
|
|
138
|
+
|
|
139
|
+
const GLOB_CHARACTERS = /[*?[\]{}]/;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Normalise a path DECLARED in a manifest (`"./skills/"`, `"."`,
|
|
143
|
+
* `"./agents/reviewer.md"`) to a plugin-relative path with no leading `./`
|
|
144
|
+
* and no trailing slash; the plugin root itself is the empty string. The
|
|
145
|
+
* open format requires the `./` prefix; the vendor dialects document the
|
|
146
|
+
* same rule; a glob is refused because no plugin in the wild uses one and
|
|
147
|
+
* expanding globs is a second path language the library would then have
|
|
148
|
+
* to own.
|
|
149
|
+
*/
|
|
150
|
+
export function resolveDeclaredPath(value: string): DeclaredPathOutcome {
|
|
151
|
+
if (GLOB_CHARACTERS.test(value)) return { ok: false, kind: "path-glob-unsupported" };
|
|
152
|
+
if (value === "." || value === "./") return { ok: true, path: "" };
|
|
153
|
+
if (!value.startsWith("./")) return { ok: false, kind: "path-not-relative" };
|
|
154
|
+
const trimmed = trimTrailing(value.slice(2), "/");
|
|
155
|
+
if (trimmed === "") return { ok: true, path: "" };
|
|
156
|
+
if (!isContainedPath(trimmed)) return { ok: false, kind: "path-escapes-root" };
|
|
157
|
+
return { ok: true, path: trimmed };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** `dir + "/" + name`, or `name` at the plugin root. */
|
|
161
|
+
export function joinPath(dir: string, name: string): string {
|
|
162
|
+
return dir === "" ? name : `${dir}/${name}`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** The last segment of a plugin-relative path. */
|
|
166
|
+
export function basename(path: string): string {
|
|
167
|
+
const slash = path.lastIndexOf("/");
|
|
168
|
+
return slash === -1 ? path : path.slice(slash + 1);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The path without its last segment; the empty string at the root. */
|
|
172
|
+
export function dirname(path: string): string {
|
|
173
|
+
const slash = path.lastIndexOf("/");
|
|
174
|
+
return slash === -1 ? "" : path.slice(0, slash);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Indexed access over a `PluginFiles`: membership, the files under a
|
|
179
|
+
* directory, a directory's immediate children. Built once per read; every
|
|
180
|
+
* lookup is a map or a prefix walk over the sorted entries.
|
|
181
|
+
*/
|
|
182
|
+
export class PluginFileIndex {
|
|
183
|
+
private readonly byPath: ReadonlyMap<string, PluginFileEntry>;
|
|
184
|
+
|
|
185
|
+
constructor(readonly files: PluginFiles) {
|
|
186
|
+
this.byPath = new Map(files.entries.map((entry) => [entry.path, entry]));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
has(path: string): boolean {
|
|
190
|
+
return this.byPath.has(path);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
entry(path: string): PluginFileEntry | undefined {
|
|
194
|
+
return this.byPath.get(path);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Every file under `dir` (recursively), in entry order; every file at the root when `dir` is empty. */
|
|
198
|
+
filesUnder(dir: string): readonly string[] {
|
|
199
|
+
if (dir === "") return this.files.entries.map((entry) => entry.path);
|
|
200
|
+
const prefix = `${dir}/`;
|
|
201
|
+
return this.files.entries.filter((entry) => entry.path.startsWith(prefix)).map((entry) => entry.path);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** True when at least one file lives under `dir`. */
|
|
205
|
+
isDirectory(dir: string): boolean {
|
|
206
|
+
if (dir === "") return this.files.entries.length > 0;
|
|
207
|
+
const prefix = `${dir}/`;
|
|
208
|
+
return this.files.entries.some((entry) => entry.path.startsWith(prefix));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** The immediate child directory names under `dir`, sorted, deduplicated. */
|
|
212
|
+
childDirectories(dir: string): readonly string[] {
|
|
213
|
+
const prefix = dir === "" ? "" : `${dir}/`;
|
|
214
|
+
const names = new Set<string>();
|
|
215
|
+
for (const entry of this.files.entries) {
|
|
216
|
+
if (!entry.path.startsWith(prefix)) continue;
|
|
217
|
+
const rest = entry.path.slice(prefix.length);
|
|
218
|
+
const slash = rest.indexOf("/");
|
|
219
|
+
if (slash !== -1) names.add(rest.slice(0, slash));
|
|
220
|
+
}
|
|
221
|
+
return [...names].sort(comparePaths);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The immediate child FILE names under `dir`, sorted. */
|
|
225
|
+
childFiles(dir: string): readonly string[] {
|
|
226
|
+
const prefix = dir === "" ? "" : `${dir}/`;
|
|
227
|
+
const names: string[] = [];
|
|
228
|
+
for (const entry of this.files.entries) {
|
|
229
|
+
if (!entry.path.startsWith(prefix)) continue;
|
|
230
|
+
const rest = entry.path.slice(prefix.length);
|
|
231
|
+
if (!rest.includes("/")) names.push(rest);
|
|
232
|
+
}
|
|
233
|
+
return names.sort(comparePaths);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* YAML frontmatter extraction for `SKILL.md` and sub-agent files, and the
|
|
3
|
+
* skill name rule.
|
|
4
|
+
*
|
|
5
|
+
* The delimiter discipline is the server's skill push gate's: the file must
|
|
6
|
+
* open with `---` on its first line and close with `---` on a line of its
|
|
7
|
+
* own, both compared after trimming whitespace. Extraction and parsing are
|
|
8
|
+
* separate steps because the two callers differ on what a missing block
|
|
9
|
+
* means: a `SKILL.md` without frontmatter is refused (the Agent Skills
|
|
10
|
+
* format requires it), while a sub-agent file without frontmatter is a
|
|
11
|
+
* prompt named after its file (the Claude Code posture).
|
|
12
|
+
*
|
|
13
|
+
* `SKILL_NAME_PATTERN` is the server's rule for a skill name (kebab-case,
|
|
14
|
+
* optionally dot-scoped), a strict superset of the Agent Skills rule
|
|
15
|
+
* (lowercase alphanumerics and hyphens), so no skill valid under the open
|
|
16
|
+
* format is refused here. This module is meant to become the pattern's one
|
|
17
|
+
* home; the server's `frontmatter.ts` carries the same regex today.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { parse as parseYaml } from "yaml";
|
|
21
|
+
|
|
22
|
+
import { isJsonObject, type JsonObject } from "./documents.js";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Kebab-case, optionally scoped with dot-separated namespaces. Every
|
|
26
|
+
* segment between separators is alphanumeric, so a name cannot start or end
|
|
27
|
+
* with a separator or contain consecutive separators. The derived slug
|
|
28
|
+
* renders dots as hyphens.
|
|
29
|
+
*/
|
|
30
|
+
export const SKILL_NAME_PATTERN = /^[a-z0-9]+([.-][a-z0-9]+)*$/;
|
|
31
|
+
|
|
32
|
+
export type FrontmatterExtraction =
|
|
33
|
+
| { readonly ok: true; readonly yaml: string; readonly body: string }
|
|
34
|
+
| { readonly ok: false; readonly reason: "missing" | "unclosed" };
|
|
35
|
+
|
|
36
|
+
/** Split a document into its frontmatter YAML and its body. */
|
|
37
|
+
export function extractFrontmatter(content: string): FrontmatterExtraction {
|
|
38
|
+
const lines = content.split(/\r?\n/);
|
|
39
|
+
if (lines.length === 0 || lines[0]?.trim() !== "---") {
|
|
40
|
+
return { ok: false, reason: "missing" };
|
|
41
|
+
}
|
|
42
|
+
for (let i = 1; i < lines.length; i++) {
|
|
43
|
+
if (lines[i]?.trim() === "---") {
|
|
44
|
+
return { ok: true, yaml: lines.slice(1, i).join("\n"), body: lines.slice(i + 1).join("\n") };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { ok: false, reason: "unclosed" };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type FrontmatterParse =
|
|
51
|
+
| { readonly ok: true; readonly fields: JsonObject }
|
|
52
|
+
| { readonly ok: false; readonly detail: string };
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Parse frontmatter YAML into a field map. An empty block is an empty map
|
|
56
|
+
* (the caller decides whether required fields are missing); a scalar or a
|
|
57
|
+
* list where a map was expected is a parse failure with its own detail.
|
|
58
|
+
* `yaml`'s defaults are the safe ones: no custom tags, aliases capped.
|
|
59
|
+
*/
|
|
60
|
+
export function parseFrontmatter(yaml: string): FrontmatterParse {
|
|
61
|
+
let value: unknown;
|
|
62
|
+
try {
|
|
63
|
+
value = parseYaml(yaml);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
return { ok: false, detail: error instanceof Error ? error.message : String(error) };
|
|
66
|
+
}
|
|
67
|
+
if (value === null || value === undefined) return { ok: true, fields: {} };
|
|
68
|
+
if (!isJsonObject(value)) return { ok: false, detail: "the frontmatter is not a mapping" };
|
|
69
|
+
return { ok: true, fields: value };
|
|
70
|
+
}
|