-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathgenerate-stack-catalog.ts
More file actions
317 lines (281 loc) · 12.7 KB
/
Copy pathgenerate-stack-catalog.ts
File metadata and controls
317 lines (281 loc) · 12.7 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env npx tsx
/**
* Validate and generate the Kilo Stack catalog served artifact.
*
* Reads `stack/catalog.yaml`, enforces integrity rules (resource ID existence,
* taxonomy structure, trust-policy compliance), stamps `curated: true` on all
* associations, and writes the serve artifact to `stack/marketplace.json`.
*
* Usage: npx tsx bin/generate-stack-catalog.ts [--check]
* --check Exit with a non-zero code if the artifact would change (CI mode).
*
* Environment:
* KILO_MARKETPLACE_STRICT If set, reject resources not in the marketplace MCP/skill
* inventories (for post-seeding validation). By default,
* unknown IDs are logged as warnings only.
*/
import * as fs from "fs"
import * as path from "path"
import * as yaml from "yaml"
import { repoPathFromBin, listVisibleDirectories, loadMcpIds } from "./marketplace-generator-utils.ts"
const CHECK_MODE = process.argv.includes("--check")
const STRICT = Boolean(process.env.KILO_MARKETPLACE_STRICT)
const catalogPath = repoPathFromBin("stack", "catalog.yaml")
const artifactPath = repoPathFromBin("stack", "marketplace.json")
const mcpsDir = repoPathFromBin("mcps")
const skillsDir = repoPathFromBin("skills")
// ── Types (loose — validated manually below) ─────────────────────────────────
interface CatalogAssociation {
ref: string
default: boolean
curated?: boolean
trust: string
maturity: string
source: string
rationale: string
warnings: string[]
deprecated?: boolean
replacement?: string
}
interface CatalogTechnology {
id: string
name: string
resources: CatalogAssociation[]
}
interface CatalogPlacement {
technology: string
note?: string
}
interface CatalogCategory {
id: string
name: string
technologies: CatalogPlacement[]
categories: CatalogCategory[]
}
interface CatalogResource {
ref: string
id: string
kind: "skill" | "mcp"
name: string
trust: string
maturity: string
source: string
warnings: string[]
}
interface CatalogVertical {
id: string
name: string
technologies: CatalogTechnology[]
categories: CatalogCategory[]
}
interface Catalog {
revision: string
verticals: CatalogVertical[]
resources: CatalogResource[]
}
// ── Validation ───────────────────────────────────────────────────────────────
const VALID_MATURITY = new Set(["stable", "preview", "beta", "experimental", "alpha", "unsupported"])
const VALID_TRUST = new Set(["official", "provider", "community"])
const KEBAB = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
const REVISION = /^\d{4}-\d{2}-\d{2}\.\d+$/
const HTTPS = /^https:\/\//
const QUALIFIED_REF = /^(?:skill|mcp):[a-z0-9]+(?:-[a-z0-9]+)*$/
function requireKebab(value: unknown, path: string): string {
if (typeof value !== "string" || !KEBAB.test(value)) {
throw new Error(`${path}: expected kebab-case string, got ${JSON.stringify(value)}`)
}
return value
}
function requireString(value: unknown, path: string): string {
if (typeof value !== "string" || !value.trim()) {
throw new Error(`${path}: expected non-empty string`)
}
return value
}
function requireRef(value: unknown, path: string): string {
if (typeof value !== "string" || !QUALIFIED_REF.test(value)) {
throw new Error(`${path}: expected qualified ref (skill:id or mcp:id), got ${JSON.stringify(value)}`)
}
return value
}
function validateResource(raw: unknown, path: string): CatalogResource {
if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new Error(`${path}: expected object`)
const r = raw as Record<string, unknown>
const ref = requireRef(r.ref, `${path}.ref`)
const id = requireKebab(r.id, `${path}.id`)
if (r.kind !== "skill" && r.kind !== "mcp") throw new Error(`${path}.kind: must be "skill" or "mcp"`)
if (ref !== `${r.kind}:${id}`) throw new Error(`${path}: ref "${ref}" must match kind:id "${r.kind}:${id}"`)
if (!VALID_TRUST.has(r.trust as string)) throw new Error(`${path}.trust: invalid value "${r.trust}"`)
if (!VALID_MATURITY.has(r.maturity as string)) throw new Error(`${path}.maturity: invalid value "${r.maturity}"`)
if (!HTTPS.test(r.source as string)) throw new Error(`${path}.source: must be an HTTPS URL`)
requireString(r.name, `${path}.name`)
if (!Array.isArray(r.warnings)) throw new Error(`${path}.warnings: must be an array`)
return r as unknown as CatalogResource
}
/**
* Validate and enrich an association entry.
* The YAML source only needs `ref`, `default`, and optionally `rationale`,
* `deprecated`, and `replacement`. All other fields (trust, maturity, source,
* warnings) are auto-filled from the resources registry.
*/
function validateAssociation(raw: unknown, path: string, registered: Map<string, CatalogResource>): CatalogAssociation {
if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new Error(`${path}: expected object`)
const a = raw as Record<string, unknown>
const ref = requireRef(a.ref, `${path}.ref`)
if (typeof a.default !== "boolean") throw new Error(`${path}.default: must be boolean`)
const resource = registered.get(ref)
if (!resource) throw new Error(`${path}: ref "${ref}" is not in the resources registry`)
// Trust-policy enforcement: only stable, official resources may carry default:true.
// This applies uniformly to both Skills and MCPs — an unstable (preview/beta/
// experimental/...) or non-official (provider/community) resource must never be
// enabled by default for end users, regardless of kind.
if (a.default === true) {
if (resource.trust !== "official" || resource.maturity !== "stable") {
throw new Error(
`${path}: ref "${ref}" has default:true but is not a stable official resource (kind=${resource.kind}, trust=${resource.trust}, maturity=${resource.maturity})`,
)
}
}
// Auto-fill metadata from the resources registry if not provided in the source.
const trust = typeof a.trust === "string" ? a.trust : resource.trust
const maturity = typeof a.maturity === "string" ? a.maturity : resource.maturity
const source = typeof a.source === "string" ? a.source : resource.source
const warnings = Array.isArray(a.warnings) ? a.warnings as string[] : resource.warnings
const rationale = typeof a.rationale === "string" && a.rationale.trim()
? a.rationale
: a.default === true
? resource.kind === "mcp"
? "MCP server enabled by default; complete authentication after installation."
: "Stable first-party Skill recommended by the Data Engineering catalog."
: resource.kind === "mcp"
? "Optional MCP server; enable explicitly and complete authentication after installation."
: "Optional Skill candidate requiring explicit review and selection."
return {
ref,
default: a.default as boolean,
trust,
maturity,
source,
rationale,
warnings,
...(a.deprecated !== undefined ? { deprecated: a.deprecated as boolean } : {}),
...(a.replacement !== undefined ? { replacement: a.replacement as string } : {}),
} as unknown as CatalogAssociation
}
function validateCategories(
cats: unknown[],
path: string,
inVertical: Set<string>,
seen: Set<string>,
counts: Map<string, number>,
): void {
for (const cat of cats) {
if (!cat || typeof cat !== "object" || Array.isArray(cat)) throw new Error(`${path}: category must be an object`)
const c = cat as Record<string, unknown>
const id = requireKebab(c.id, `${path}.id`)
requireString(c.name, `${path}.name`)
if (seen.has(id)) throw new Error(`${path}: category id "${id}" is duplicated`)
seen.add(id)
if (!Array.isArray(c.technologies)) throw new Error(`${path}.technologies: must be an array`)
const placed = new Set<string>()
for (const placement of c.technologies as unknown[]) {
if (!placement || typeof placement !== "object") throw new Error(`${path}: placement must be an object`)
const p = placement as Record<string, unknown>
const tech = requireKebab(p.technology, `${path}.technology`)
if (placed.has(tech)) throw new Error(`${path}: technology "${tech}" placed more than once in category "${id}"`)
placed.add(tech)
if (!inVertical.has(tech)) throw new Error(`${path}: technology "${tech}" is not registered in this vertical`)
counts.set(tech, (counts.get(tech) ?? 0) + 1)
}
if (!Array.isArray(c.categories)) throw new Error(`${path}.categories: must be an array`)
validateCategories(c.categories, `${path}.categories`, inVertical, seen, counts)
}
}
function validate(catalog: Catalog, mcpIds: Set<string>, skillIds: Set<string>): string[] {
const warnings: string[] = []
if (!REVISION.test(catalog.revision)) {
throw new Error(`revision: must match YYYY-MM-DD.N, got "${catalog.revision}"`)
}
// Register all resources
const registered = new Map<string, CatalogResource>()
const associated = new Set<string>()
for (const r of catalog.resources) {
const res = validateResource(r, `resources.${r.ref}`)
if (registered.has(res.ref)) throw new Error(`resources: duplicate ref "${res.ref}"`)
registered.set(res.ref, res)
// Check existence in marketplace inventories
if (res.kind === "mcp" && !mcpIds.has(res.id)) {
const msg = `resources.${res.ref}: MCP id "${res.id}" is not in mcps/`
if (STRICT) throw new Error(msg)
else warnings.push(`WARN ${msg}`)
}
if (res.kind === "skill" && !skillIds.has(res.id)) {
const msg = `resources.${res.ref}: Skill id "${res.id}" is not in skills/`
if (STRICT) throw new Error(msg)
else warnings.push(`WARN ${msg}`)
}
}
// Validate verticals
const allTechs = new Map<string, string>() // id → vertical id
for (const v of catalog.verticals) {
requireKebab(v.id, `verticals.${v.id}.id`)
requireString(v.name, `verticals.${v.id}.name`)
const inVertical = new Set<string>()
for (const tech of v.technologies) {
requireKebab(tech.id, `verticals.${v.id}.technologies.${tech.id}.id`)
requireString(tech.name, `verticals.${v.id}.technologies.${tech.id}.name`)
if (allTechs.has(tech.id)) {
throw new Error(`verticals.${v.id}: technology id "${tech.id}" is already registered in vertical "${allTechs.get(tech.id)}"`)
}
allTechs.set(tech.id, v.id)
inVertical.add(tech.id)
const assocRefs = new Set<string>()
for (let i = 0; i < tech.resources.length; i++) {
const a = validateAssociation(tech.resources[i], `verticals.${v.id}.technologies.${tech.id}.resources.${tech.resources[i].ref}`, registered)
if (assocRefs.has(a.ref)) throw new Error(`verticals.${v.id}.technologies.${tech.id}: ref "${a.ref}" repeated`)
assocRefs.add(a.ref)
associated.add(a.ref)
tech.resources[i] = a
}
}
const catSeen = new Set<string>()
const catCounts = new Map<string, number>()
validateCategories(v.categories, `verticals.${v.id}.categories`, inVertical, catSeen, catCounts)
for (const id of inVertical) {
if (!catCounts.has(id)) warnings.push(`WARN verticals.${v.id}: technology "${id}" has no category placement`)
}
}
// Every resource must be referenced by at least one technology
for (const ref of registered.keys()) {
if (!associated.has(ref)) warnings.push(`WARN resources.${ref}: not associated with any technology`)
}
return warnings
}
// ── Main ─────────────────────────────────────────────────────────────────────
const raw = fs.readFileSync(catalogPath, "utf-8")
const parsed = yaml.parse(raw) as Catalog
const mcpIds = loadMcpIds(mcpsDir)
const skillIds = new Set(listVisibleDirectories(skillsDir))
console.log(`\nValidating stack/catalog.yaml (revision: ${parsed.revision})`)
const warnings = validate(parsed, mcpIds, skillIds)
for (const w of warnings) console.log(w)
// Stamp curated:true on all associations before publishing
for (const vertical of parsed.verticals) {
for (const tech of vertical.technologies) {
for (const assoc of tech.resources) {
assoc.curated = true
}
}
}
const artifact = JSON.stringify(parsed, null, 2)
if (CHECK_MODE) {
const existing = fs.existsSync(artifactPath) ? fs.readFileSync(artifactPath, "utf-8") : ""
if (existing !== `${artifact}\n`) {
console.error("\nstack/marketplace.json is stale. Run: npx tsx bin/generate-stack-catalog.ts")
process.exit(1)
}
console.log("\nstack/marketplace.json is up to date.")
} else {
fs.writeFileSync(artifactPath, `${artifact}\n`)
console.log(`\nWrote stack/marketplace.json (${parsed.verticals.length} vertical(s), ${parsed.resources.length} resource(s))`)
}