Read skills from the global and workspace .agents/skills directories
- Discover skill directories under ~/.agents/skills and each workspace''s .agents/skills; a directory qualifies with a full skill.manifest.json or just a SKILL.md, whose manifest is derived (frontmatter name or the directory name). Paths resolve per platform on Windows, macOS, and Linux. - Sync at startup for every organization and again when the Skills page opens; skills the org already carries are skipped, and one broken directory cannot block the rest. - AGENTCOMPANY_NEW_SKILLS_ROOT points tests at a temporary library (overriding USERPROFILE crashes Electron on Windows).
This commit is contained in:
1 parent
256e0680c6
commit
2d43950123
14 files changed
+303
-12
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { basename, join } from "node:path";
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
import { RISK_LEVEL } from "../../shared/domain";
|
||||
import { loadSkillManifest, type SkillManifest } from "./skill-manifest";
|
||||
import type { SkillService } from "./skill-service";
|
||||
|
||||
/**
|
||||
* Skills live on disk the way developers keep them: one directory per skill
|
||||
* under the user's global `~/.agents/skills` and under each workspace's
|
||||
* `.agents/skills`. A directory qualifies with either a full
|
||||
* skill.manifest.json or just a SKILL.md; for the latter the manifest is
|
||||
* derived (name from frontmatter or the directory name, SKILL.md as the
|
||||
* entrypoint). Paths are joined per platform — Windows, macOS, and Linux
|
||||
* all resolve the same way.
|
||||
*/
|
||||
|
||||
export interface SkillSyncReport {
|
||||
imported: number;
|
||||
skipped: number;
|
||||
failures: string[];
|
||||
}
|
||||
|
||||
export function defaultGlobalSkillsRoot(): string {
|
||||
// Same isolation seam as AGENTCOMPANY_NEW_PROFILE_DIR: tests point this at
|
||||
// a temporary directory so they never read the developer's real library.
|
||||
return process.env.AGENTCOMPANY_NEW_SKILLS_ROOT ?? join(homedir(), ".agents", "skills");
|
||||
}
|
||||
|
||||
export function discoverSkillDirectories(workspaceRoots: string[], globalRoot: string = defaultGlobalSkillsRoot()): string[] {
|
||||
const roots = [globalRoot, ...workspaceRoots.map((root) => join(root, ".agents", "skills"))];
|
||||
const found: string[] = [];
|
||||
for (const root of roots) {
|
||||
if (!existsSync(root)) continue;
|
||||
for (const entry of readdirSync(root, { withFileTypes: true })) {
|
||||
if (!entry.isDirectory()) continue;
|
||||
const dir = join(root, entry.name);
|
||||
if (existsSync(join(dir, "skill.manifest.json")) || existsSync(join(dir, "SKILL.md"))) {
|
||||
found.push(dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/** Full manifest when present; otherwise derived from SKILL.md. */
|
||||
export function manifestForDirectory(directoryPath: string): SkillManifest {
|
||||
if (existsSync(join(directoryPath, "skill.manifest.json"))) {
|
||||
return loadSkillManifest(directoryPath);
|
||||
}
|
||||
const skillMd = readFileSync(join(directoryPath, "SKILL.md"), "utf8");
|
||||
return {
|
||||
name: frontmatterName(skillMd) ?? basename(directoryPath),
|
||||
version: "1.0.0",
|
||||
entrypoint: "SKILL.md",
|
||||
capabilities: [],
|
||||
requiredTools: [],
|
||||
compatibleModels: [],
|
||||
riskLevel: RISK_LEVEL.LOW,
|
||||
tests: [],
|
||||
inputs: {},
|
||||
outputs: {},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Imports every discovered skill the org does not already carry. Individual
|
||||
* bad directories are reported and skipped so one broken entry cannot block
|
||||
* the rest of the library.
|
||||
*/
|
||||
export function syncSkillsFromDisk(
|
||||
db: DatabaseSync,
|
||||
skills: SkillService,
|
||||
orgId: string,
|
||||
globalRoot: string = defaultGlobalSkillsRoot(),
|
||||
): SkillSyncReport {
|
||||
const workspaceRoots = (db.prepare("select local_path from workspaces where org_id = ?").all(orgId) as Array<{ local_path: string }>)
|
||||
.map((row) => row.local_path);
|
||||
const report: SkillSyncReport = { imported: 0, skipped: 0, failures: [] };
|
||||
for (const directory of discoverSkillDirectories(workspaceRoots, globalRoot)) {
|
||||
try {
|
||||
const manifest = manifestForDirectory(directory);
|
||||
if (skills.hasSkill(orgId, manifest.name)) {
|
||||
report.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
skills.importManifest(orgId, directory, manifest);
|
||||
report.imported += 1;
|
||||
} catch (error) {
|
||||
report.failures.push(`${directory}: ${error instanceof Error ? error.message : String(error)}`);
|
||||
}
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
function frontmatterName(markdown: string): string | null {
|
||||
const match = /^---\r?\n([\s\S]*?)\r?\n---/.exec(markdown);
|
||||
if (!match) return null;
|
||||
const nameLine = match[1].split(/\r?\n/).find((line) => /^name\s*:/.test(line.trim()));
|
||||
if (!nameLine) return null;
|
||||
const value = nameLine.split(":").slice(1).join(":").trim().replace(/^["']|["']$/g, "");
|
||||
return value || null;
|
||||
}
|
||||
@@ -16,12 +16,12 @@ export interface SkillManifest {
|
||||
outputs: unknown;
|
||||
}
|
||||
|
||||
export function loadSkillManifest(directoryPath: string, requestedVersion: string): SkillManifest {
|
||||
export function loadSkillManifest(directoryPath: string, requestedVersion?: string): SkillManifest {
|
||||
const manifestPath = join(directoryPath, "skill.manifest.json");
|
||||
if (!existsSync(manifestPath)) throw new Error("skill.manifest.json is required.");
|
||||
const raw = JSON.parse(readFileSync(manifestPath, "utf8")) as Record<string, unknown>;
|
||||
const manifest = normalizeManifest(raw);
|
||||
if (manifest.version !== requestedVersion) {
|
||||
if (requestedVersion !== undefined && manifest.version !== requestedVersion) {
|
||||
throw new Error("Skill manifest version must match the requested version.");
|
||||
}
|
||||
verifyDeclaredFile(directoryPath, manifest.entrypoint);
|
||||
|
||||
@@ -25,13 +25,23 @@ export class SkillService {
|
||||
|
||||
importDirectory(input: ImportSkillDirectoryInput): string {
|
||||
const manifest = loadSkillManifest(input.directoryPath, input.versionSemver);
|
||||
return this.importManifest(input.orgId, input.directoryPath, manifest);
|
||||
}
|
||||
|
||||
/** True when the org already carries a skill with this name. */
|
||||
hasSkill(orgId: string, name: string): boolean {
|
||||
const slug = slugify(name);
|
||||
return Boolean(this.db.prepare("select id from skill_definitions where org_id = ? and slug = ?").get(orgId, slug));
|
||||
}
|
||||
|
||||
importManifest(orgId: string, directoryPath: string, manifest: SkillManifest): string {
|
||||
const slug = slugify(manifest.name);
|
||||
if (!slug) throw new Error("Skill name could not be derived.");
|
||||
const now = new Date().toISOString();
|
||||
const skillId = createId("skl");
|
||||
const revisionId = createId("skr");
|
||||
const checksum = recursiveSkillChecksum(input.directoryPath);
|
||||
const copiedFiles = copySkillRevision(input.directoryPath, this.artifactRoot, revisionId);
|
||||
const checksum = recursiveSkillChecksum(directoryPath);
|
||||
const copiedFiles = copySkillRevision(directoryPath, this.artifactRoot, revisionId);
|
||||
const copiedEntrypoint = copiedFiles.find((file) => file.relativePath === manifest.entrypoint);
|
||||
if (!copiedEntrypoint) throw new Error("Skill manifest entrypoint was not found.");
|
||||
|
||||
@@ -39,10 +49,10 @@ export class SkillService {
|
||||
insert into skill_definitions(id, org_id, slug, name, owner_type, status, created_at)
|
||||
values (?, ?, ?, ?, ?, ?, ?)
|
||||
on conflict(org_id, slug) do update set name = excluded.name, status = 'active'
|
||||
`).run(skillId, input.orgId, slug, manifest.name, "organization", "active", now);
|
||||
`).run(skillId, orgId, slug, manifest.name, "organization", "active", now);
|
||||
|
||||
const existing = this.db.prepare("select id from skill_definitions where org_id = ? and slug = ?")
|
||||
.get(input.orgId, slug) as { id: string };
|
||||
.get(orgId, slug) as { id: string };
|
||||
|
||||
this.db.prepare(`
|
||||
insert into skill_revisions(
|
||||
@@ -54,7 +64,7 @@ export class SkillService {
|
||||
manifest.version,
|
||||
JSON.stringify(manifest),
|
||||
"local_directory",
|
||||
input.directoryPath,
|
||||
directoryPath,
|
||||
now,
|
||||
checksum,
|
||||
);
|
||||
@@ -72,7 +82,7 @@ export class SkillService {
|
||||
}
|
||||
|
||||
this.audit.record({
|
||||
orgId: input.orgId,
|
||||
orgId,
|
||||
entityType: "skill_revision",
|
||||
entityId: revisionId,
|
||||
actorType: "system",
|
||||
|
||||
Reference in new issue
Block a user