Add skill governance and runtime routing

This commit is contained in:
dahyeon committed 2026-05-14 14:28:59 -07:00
1 parent 6f09408423
commit 41dcfcdef7
71 files changed
+7057 -102

No files matched your search

+243
View File
@@ -0,0 +1,243 @@
import { existsSync, readdirSync, readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
export type TrustTier = "T1" | "T2" | "T3" | "T4";
export interface SkillManifest {
bodyPath: string;
description: string;
intents: string[];
name: string;
resources: Record<string, string>;
tools: string[];
triggers: string[];
trustTier: TrustTier;
version: number;
}
interface ParsedSkillFile {
body: string;
manifest: SkillManifest;
}
const registryCache = new Map<string, SkillRegistry>();
function parseScalar(value: string): string | number {
const trimmed = value.trim();
const unquoted = trimmed.replace(/^["']|["']$/gu, "");
const numeric = Number(unquoted);
return Number.isFinite(numeric) && /^\d+$/u.test(unquoted) ? numeric : unquoted;
}
function parseInlineArray(value: string): string[] {
const trimmed = value.trim();
if (!trimmed.startsWith("[") || !trimmed.endsWith("]")) {
return [];
}
return trimmed
.slice(1, -1)
.split(",")
.map((item) => item.trim().replace(/^["']|["']$/gu, ""))
.filter(Boolean);
}
function parseFrontmatter(raw: string): {
body: string;
fields: Record<string, unknown>;
} | null {
if (!raw.startsWith("---\n")) {
return null;
}
const end = raw.indexOf("\n---", 4);
if (end === -1) {
return null;
}
const frontmatter = raw.slice(4, end).trimEnd();
const body = raw.slice(end + 4).trim();
const fields: Record<string, unknown> = {};
let currentMapKey: string | null = null;
for (const line of frontmatter.split(/\r?\n/u)) {
if (!line.trim()) {
continue;
}
if (/^\s{2,}[A-Za-z0-9_-]+:/u.test(line) && currentMapKey) {
const [key, ...rest] = line.trim().split(":");
if (key) {
const map = fields[currentMapKey] as Record<string, string>;
map[key] = rest.join(":").trim();
}
continue;
}
currentMapKey = null;
const [key, ...rest] = line.split(":");
if (!key) {
continue;
}
const value = rest.join(":").trim();
if (!value) {
fields[key] = {};
currentMapKey = key;
} else if (value.startsWith("[")) {
fields[key] = parseInlineArray(value);
} else {
fields[key] = parseScalar(value);
}
}
return { body, fields };
}
function readStringArray(fields: Record<string, unknown>, key: string): string[] {
const value = fields[key];
return Array.isArray(value)
? value.flatMap((item) => (typeof item === "string" ? [item] : []))
: [];
}
function readResources(
fields: Record<string, unknown>,
skillDir: string,
): Record<string, string> {
const raw = fields["resources"];
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
return {};
}
return Object.fromEntries(
Object.entries(raw as Record<string, unknown>).flatMap(([key, value]) =>
typeof value === "string" && value.trim()
? [[key, resolve(skillDir, value.trim())]]
: [],
),
);
}
function isTrustTier(value: unknown): value is TrustTier {
return value === "T1" || value === "T2" || value === "T3" || value === "T4";
}
function parseSkillFile(path: string): ParsedSkillFile | null {
const raw = readFileSync(path, "utf8");
const parsed = parseFrontmatter(raw);
if (!parsed) {
return null;
}
const skillDir = dirname(path);
const name = parsed.fields["name"];
const description = parsed.fields["description"];
const version = parsed.fields["version"];
const trustTier = parsed.fields["trustTier"];
if (
typeof name !== "string" ||
!name.trim() ||
typeof description !== "string" ||
!description.trim() ||
typeof version !== "number" ||
!isTrustTier(trustTier)
) {
return null;
}
return {
body: parsed.body,
manifest: {
bodyPath: path,
description,
intents: readStringArray(parsed.fields, "intents"),
name,
resources: readResources(parsed.fields, skillDir),
tools: readStringArray(parsed.fields, "tools"),
triggers: readStringArray(parsed.fields, "triggers"),
trustTier,
version,
},
};
}
function findSkillFiles(rootDir: string): string[] {
if (!existsSync(rootDir)) {
return [];
}
return readdirSync(rootDir, { withFileTypes: true }).flatMap((entry) => {
const path = resolve(rootDir, entry.name);
if (entry.isDirectory()) {
return findSkillFiles(path);
}
return entry.isFile() && entry.name === "SKILL.md" ? [path] : [];
});
}
export class SkillRegistry {
private constructor(
private readonly manifests: SkillManifest[],
private readonly bodies: Map<string, string>,
) {}
static load(rootDir: string): SkillRegistry {
const normalizedRoot = resolve(rootDir);
const cached = registryCache.get(normalizedRoot);
if (cached) {
return cached;
}
const parsed = findSkillFiles(normalizedRoot).flatMap((path) => {
const skillFile = parseSkillFile(path);
return skillFile ? [skillFile] : [];
});
const seen = new Set<string>();
const manifests: SkillManifest[] = [];
const bodies = new Map<string, string>();
for (const item of parsed) {
if (seen.has(item.manifest.name)) {
continue;
}
seen.add(item.manifest.name);
manifests.push(item.manifest);
bodies.set(item.manifest.name, item.body);
}
const registry = new SkillRegistry(manifests, bodies);
registryCache.set(normalizedRoot, registry);
return registry;
}
list(): SkillManifest[] {
return [...this.manifests];
}
get(name: string): SkillManifest | null {
return this.manifests.find((manifest) => manifest.name === name) ?? null;
}
loadBodySync(name: string): string {
return this.bodies.get(name) ?? "";
}
loadResourceSync(name: string, key: string): string {
const manifest = this.get(name);
const path = manifest?.resources[key];
if (!path || !existsSync(path)) {
return "";
}
return readFileSync(path, "utf8").trim();
}
async loadBody(name: string): Promise<string> {
return await Promise.resolve(this.loadBodySync(name));
}
async loadResource(name: string, key: string): Promise<string> {
return await Promise.resolve(this.loadResourceSync(name, key));
}
}