@namzu/sdk 15.0.0 → 16.0.0
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/CHANGELOG.md +151 -0
- package/dist/public-runtime.d.ts +1 -0
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +5 -0
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-types.d.ts +1 -0
- package/dist/public-types.d.ts.map +1 -1
- package/dist/skills/loader.d.ts.map +1 -1
- package/dist/skills/loader.js +70 -112
- package/dist/skills/loader.js.map +1 -1
- package/dist/utils/frontmatter.d.ts +90 -0
- package/dist/utils/frontmatter.d.ts.map +1 -0
- package/dist/utils/frontmatter.js +212 -0
- package/dist/utils/frontmatter.js.map +1 -0
- package/package.json +1 -1
- package/src/public-runtime.ts +5 -0
- package/src/public-types.ts +1 -0
- package/src/skills/loader.ts +67 -119
- package/src/utils/frontmatter.ts +265 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one frontmatter reader.
|
|
3
|
+
*
|
|
4
|
+
* A `SKILL.md`, a command file, and anything else this kernel reads from a
|
|
5
|
+
* markdown file with a `---` fence all come through here. There were three
|
|
6
|
+
* readers before this one and two of them disagreed on the same input: this
|
|
7
|
+
* one **threw** on malformed frontmatter, and a second **silently returned no
|
|
8
|
+
* metadata**, so the same file was a hard error in one code path and a skill
|
|
9
|
+
* named after its own directory with "(no description)" in the other. Refuse
|
|
10
|
+
* versus degrade, on one file shape, is the divergence this module exists to
|
|
11
|
+
* end — see `docs/conventions/refuse-do-not-degrade.md`.
|
|
12
|
+
*
|
|
13
|
+
* **Deliberately not a YAML parser.** It is a flat key/value splitter with one
|
|
14
|
+
* level of nesting, and it refuses the constructs in {@link UNSUPPORTED_YAML}
|
|
15
|
+
* rather than mangling them. That refusal is the design: a reader that
|
|
16
|
+
* half-understands YAML produces a value that passes validation and means
|
|
17
|
+
* nothing.
|
|
18
|
+
*
|
|
19
|
+
* That refusal is now total for lists. A *block* sequence
|
|
20
|
+
*
|
|
21
|
+
* ```yaml
|
|
22
|
+
* allowed-tools:
|
|
23
|
+
* - Read
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* used to be silently dropped — its lines carry no `:` and were skipped, so the
|
|
27
|
+
* key came back **absent** — while the flow form `[Read, Grep]` threw. One
|
|
28
|
+
* spelling of a list was a hard error and the other was silence, and the silent
|
|
29
|
+
* one is the shape an author actually writes, because the block form is the
|
|
30
|
+
* natural YAML for a list.
|
|
31
|
+
*
|
|
32
|
+
* It matters most for a key like `allowed-tools`: a skill that asked for `Bash`
|
|
33
|
+
* and silently did not get it is indistinguishable from one that never asked,
|
|
34
|
+
* which is a capability quietly not granted rather than a formatting nicety.
|
|
35
|
+
* Both readers this replaced behaved that way, so it was inherited rather than
|
|
36
|
+
* introduced; it is now refused, naming the key.
|
|
37
|
+
*
|
|
38
|
+
* **Vocabulary belongs to the caller.** This returns the parsed map; it does
|
|
39
|
+
* not know what a skill needs or what a command needs, and it validates no
|
|
40
|
+
* field names. Widening one caller's metadata type to cover another's is how a
|
|
41
|
+
* skill-shaped API comes to mean something it does not.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A closing fence is a line of its own, not `---` wherever it appears.
|
|
46
|
+
*
|
|
47
|
+
* An unanchored search found `---` inside a quoted value, inside a URL, inside
|
|
48
|
+
* prose — and cut the frontmatter there, which both truncated the metadata AND
|
|
49
|
+
* spilled the rest of it into the body, where it reaches the system prompt
|
|
50
|
+
* verbatim.
|
|
51
|
+
*
|
|
52
|
+
* `\r?` is explicit rather than incidental. `$` under `/m` already matches
|
|
53
|
+
* before a `\r` because JavaScript counts `\r` as a line terminator, so this
|
|
54
|
+
* pattern worked on CRLF by accident before it worked on purpose. Naming the
|
|
55
|
+
* carriage return keeps the next edit from removing a property nobody knew was
|
|
56
|
+
* being relied on — a file authored on Windows is the ordinary case, not the
|
|
57
|
+
* exotic one.
|
|
58
|
+
*/
|
|
59
|
+
const FRONTMATTER_FENCE = /^---[ \t]*\r?$/m
|
|
60
|
+
|
|
61
|
+
const FRONTMATTER_DELIMITER = '---'
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Splits on any of the three line endings — CRLF, LF, and a lone CR.
|
|
65
|
+
*
|
|
66
|
+
* The `\r?\n` half is defence in depth and was measured as such: reducing it to
|
|
67
|
+
* `/\n/` fails no test, because {@link normalizeScalar} trims the stray `\r`
|
|
68
|
+
* off every value anyway. The fence is the load-bearing half for CRLF, and that
|
|
69
|
+
* one has a mutation profile.
|
|
70
|
+
*
|
|
71
|
+
* The lone `\r` is not decoration. Without it a CR-only file is one single
|
|
72
|
+
* "line", and the whole frontmatter collapses into the first key: `name` came
|
|
73
|
+
* back as `"a-skill\rdescription: d"` — a *wrong value*, silently, which is the
|
|
74
|
+
* failure this module exists to end.
|
|
75
|
+
*
|
|
76
|
+
* `loadSkill` was accidentally protected, though not in the way first written
|
|
77
|
+
* here: the collapse leaves no `description` key at all, so the required-field
|
|
78
|
+
* check refused the file before any value could be used. A caller that
|
|
79
|
+
* validates nothing — which is every caller this is now exported for — would
|
|
80
|
+
* have taken the mangled name.
|
|
81
|
+
*/
|
|
82
|
+
const LINE_SPLIT = /\r\n|\r|\n/
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* YAML this reader does not implement, refused rather than mangled.
|
|
86
|
+
*
|
|
87
|
+
* The documented contract says "YAML frontmatter" with no restriction — so an
|
|
88
|
+
* author has every reason to write a block scalar or a flow sequence, and no
|
|
89
|
+
* reason to expect what happened next. A `description: >-` followed by an
|
|
90
|
+
* indented paragraph produced the literal string `">-"`, which passed
|
|
91
|
+
* validation and registered with no warning; the skill then existed and was
|
|
92
|
+
* never selected, because its description said nothing. A `[Read, Grep]`
|
|
93
|
+
* became that literal text and was interpolated straight into the prompt.
|
|
94
|
+
*
|
|
95
|
+
* Refusing names the line and the file. That is worse for exactly one file —
|
|
96
|
+
* the one already silently broken — and better for everyone looking for it.
|
|
97
|
+
*/
|
|
98
|
+
const UNSUPPORTED_YAML = [
|
|
99
|
+
{ pattern: /^[>|][-+]?\s*$/, what: 'a block scalar (`>` or `|`)' },
|
|
100
|
+
{ pattern: /^\[.*\]$/, what: 'a flow sequence (`[a, b]`)' },
|
|
101
|
+
{ pattern: /^\{.*\}$/, what: 'a flow mapping (`{a: b}`)' },
|
|
102
|
+
] as const
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* What one frontmatter key holds: a scalar, or a block of indented pairs.
|
|
106
|
+
*
|
|
107
|
+
* A discriminated union rather than two parallel maps, because the source
|
|
108
|
+
* format cannot express both at once. The first shape of this type had
|
|
109
|
+
* `data: Record<string, string>` beside `blocks: Record<string, Record<…>>`,
|
|
110
|
+
* which let one key sit in both — a state no YAML file can produce. Every
|
|
111
|
+
* caller would then have had to decide a precedence for a case that cannot
|
|
112
|
+
* arrive, and the ones who did not would be carrying a latent bug against a
|
|
113
|
+
* shape that told them the case existed. Removing the state beats documenting
|
|
114
|
+
* it.
|
|
115
|
+
*/
|
|
116
|
+
export type FrontmatterValue =
|
|
117
|
+
| { readonly kind: 'scalar'; readonly value: string }
|
|
118
|
+
| { readonly kind: 'mapping'; readonly entries: Readonly<Record<string, string>> }
|
|
119
|
+
|
|
120
|
+
export interface ParsedFrontmatter {
|
|
121
|
+
/**
|
|
122
|
+
* Every top-level key, in the order the file declared it.
|
|
123
|
+
*
|
|
124
|
+
* A key whose value is empty and which has no indented lines under it is
|
|
125
|
+
* absent: it declared nothing. Narrow on `kind` to read it —
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* const d = values.description
|
|
129
|
+
* if (d?.kind !== 'scalar') throw new Error('description must be a scalar')
|
|
130
|
+
* use(d.value)
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
readonly values: Readonly<Record<string, FrontmatterValue>>
|
|
134
|
+
|
|
135
|
+
/** Everything after the closing fence, trimmed. */
|
|
136
|
+
readonly body: string
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Parse a markdown file's `---` frontmatter.
|
|
141
|
+
*
|
|
142
|
+
* @param raw The file's full contents. LF and CRLF both parse.
|
|
143
|
+
* @param source A label for error messages — a path, or a phrase naming the
|
|
144
|
+
* file. Used verbatim, so the caller controls how its own errors read.
|
|
145
|
+
* @throws If the frontmatter is absent, unclosed, or uses YAML this reader
|
|
146
|
+
* does not implement. It never returns a partial or empty result to stand in
|
|
147
|
+
* for a file it could not read.
|
|
148
|
+
*/
|
|
149
|
+
export function parseFrontmatter(raw: string, source: string): ParsedFrontmatter {
|
|
150
|
+
const trimmed = raw.trimStart()
|
|
151
|
+
|
|
152
|
+
if (!trimmed.startsWith(FRONTMATTER_DELIMITER)) {
|
|
153
|
+
throw new Error(`${source} has no YAML frontmatter`)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const closing = FRONTMATTER_FENCE.exec(trimmed.slice(FRONTMATTER_DELIMITER.length))
|
|
157
|
+
if (!closing) {
|
|
158
|
+
throw new Error(`${source} has unclosed frontmatter`)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const endIdx = FRONTMATTER_DELIMITER.length + closing.index
|
|
162
|
+
const frontmatterRaw = trimmed.slice(FRONTMATTER_DELIMITER.length, endIdx).trim()
|
|
163
|
+
const body = trimmed.slice(endIdx + closing[0].length).trim()
|
|
164
|
+
|
|
165
|
+
// `Map`, not an object literal, because the keys come from an untrusted
|
|
166
|
+
// file. `blocks[key] = …` on a plain object with `key === '__proto__'`
|
|
167
|
+
// reaches `Object.prototype` through the inheritance chain and writes
|
|
168
|
+
// **there** — a frontmatter file could set `Object.prototype.metadata` and
|
|
169
|
+
// poison every object in the process. That is not theoretical: it was
|
|
170
|
+
// caught here by an adversarial pass, and the poisoned prototype then
|
|
171
|
+
// showed up in the metadata of an unrelated skill loaded afterwards.
|
|
172
|
+
// A `Map` has no prototype chain for string keys, and `Object.fromEntries`
|
|
173
|
+
// *defines* own properties rather than assigning through setters, so the
|
|
174
|
+
// round trip is safe at both ends.
|
|
175
|
+
const data = new Map<string, string>()
|
|
176
|
+
const blocks = new Map<string, Map<string, string>>()
|
|
177
|
+
let currentKey: string | undefined
|
|
178
|
+
|
|
179
|
+
for (const line of frontmatterRaw.split(LINE_SPLIT)) {
|
|
180
|
+
if (!line.trim() || line.trimStart().startsWith('#')) continue
|
|
181
|
+
|
|
182
|
+
if (/^\s/.test(line)) {
|
|
183
|
+
if (!currentKey) continue
|
|
184
|
+
|
|
185
|
+
// A block sequence item. Refused, not skipped.
|
|
186
|
+
//
|
|
187
|
+
// These lines carry no `:`, so the `continue` below used to drop them
|
|
188
|
+
// and the key — having no scalar value and no mapping entries — came
|
|
189
|
+
// back ABSENT. The flow form `[Read, Grep]` already threw, so one
|
|
190
|
+
// spelling of a list was a hard error and the other was silence.
|
|
191
|
+
//
|
|
192
|
+
// The block form is the more natural YAML for a list, which is what
|
|
193
|
+
// made this worth closing: `allowed-tools` is a list, so this is the
|
|
194
|
+
// shape an author actually writes, and a skill that asked for `Bash`
|
|
195
|
+
// and silently did not get it is indistinguishable from one that never
|
|
196
|
+
// asked. A capability quietly not granted is the worst thing this
|
|
197
|
+
// reader can produce.
|
|
198
|
+
if (/^\s*-\s/.test(line)) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
`${source}: "${currentKey}" uses a block sequence (a "- " list), which this reader does not support. Write it as a single-line value instead. Refusing rather than reading "${currentKey}" as absent, which is what silently dropping the list would mean.`,
|
|
201
|
+
)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const colonIdx = line.indexOf(':')
|
|
205
|
+
if (colonIdx === -1) continue
|
|
206
|
+
const key = line.slice(0, colonIdx).trim()
|
|
207
|
+
const value = normalizeScalar(line.slice(colonIdx + 1))
|
|
208
|
+
if (!key || !value) continue
|
|
209
|
+
let block = blocks.get(currentKey)
|
|
210
|
+
if (!block) {
|
|
211
|
+
block = new Map<string, string>()
|
|
212
|
+
blocks.set(currentKey, block)
|
|
213
|
+
}
|
|
214
|
+
block.set(key, value)
|
|
215
|
+
continue
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const colonIdx = line.indexOf(':')
|
|
219
|
+
if (colonIdx === -1) continue
|
|
220
|
+
const key = line.slice(0, colonIdx).trim()
|
|
221
|
+
const value = normalizeScalar(line.slice(colonIdx + 1))
|
|
222
|
+
|
|
223
|
+
assertReadableScalar(key, value, source)
|
|
224
|
+
|
|
225
|
+
currentKey = key
|
|
226
|
+
if (value) data.set(key, value)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// A key cannot be a scalar and a mapping at once — no YAML file can say
|
|
230
|
+
// that — so refusing here is what makes the illegal state unrepresentable
|
|
231
|
+
// in the returned type rather than merely undocumented. The alternative,
|
|
232
|
+
// picking a precedence, would silently drop half of what the author wrote.
|
|
233
|
+
for (const key of blocks.keys()) {
|
|
234
|
+
if (!data.has(key)) continue
|
|
235
|
+
throw new Error(
|
|
236
|
+
`${source}: "${key}" has both a value and an indented block. A key is one or the other — remove the value, or un-indent the lines beneath it.`,
|
|
237
|
+
)
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const values = new Map<string, FrontmatterValue>()
|
|
241
|
+
for (const [key, value] of data) {
|
|
242
|
+
values.set(key, { kind: 'scalar', value })
|
|
243
|
+
}
|
|
244
|
+
for (const [key, entries] of blocks) {
|
|
245
|
+
values.set(key, { kind: 'mapping', entries: Object.fromEntries(entries) })
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return { values: Object.fromEntries(values), body }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function normalizeScalar(value: string): string {
|
|
252
|
+
return value
|
|
253
|
+
.trim()
|
|
254
|
+
.replace(/^["']|["']$/g, '')
|
|
255
|
+
.trim()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function assertReadableScalar(key: string, value: string, source: string): void {
|
|
259
|
+
for (const { pattern, what } of UNSUPPORTED_YAML) {
|
|
260
|
+
if (!pattern.test(value)) continue
|
|
261
|
+
throw new Error(
|
|
262
|
+
`${source}: "${key}" uses ${what}, which this reader does not support. Write it as a single-line value instead. Refusing rather than accepting a "${key}" that would read as ${JSON.stringify(value)}.`,
|
|
263
|
+
)
|
|
264
|
+
}
|
|
265
|
+
}
|