@forge/cli
14.2.015.0.0-next.23
out/service/skills-service.js+
out/service/skills-service.jsNew file+149
Index: package/out/service/skills-service.js
===================================================================
--- package/out/service/skills-service.js
+++ package/out/service/skills-service.js
@@ -0,0 +1,149 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.SkillsService = void 0;
+const fs_1 = require("fs");
+const path_1 = require("path");
+const cli_shared_1 = require("@forge/cli-shared");
+const skills_agents_1 = require("./skills-agents");
+const skills_download_1 = require("./skills-download");
+const skills_mcp_config_1 = require("./skills-mcp-config");
+const MARKER_FILE = '.forge-skills.json';
+class SkillsService {
+ downloader;
+ locations;
+ constructor(downloader, locations = {}) {
+ this.downloader = downloader;
+ this.locations = locations;
+ }
+ detectAgents() {
+ return (0, skills_agents_1.detectAgents)(this.locations);
+ }
+ async install(agents, options) {
+ const release = await this.downloader.fetchLatest();
+ const selected = [...new Set(options.skills ?? release.skills)].sort();
+ const unknown = selected.filter((skill) => !release.skills.includes(skill));
+ if (unknown.length)
+ throw new cli_shared_1.ValidationError(cli_shared_1.Text.skills.errors.unknownSkills(unknown));
+ return agents.map((agent) => this.attempt(agent, options.scope, () => this.installAgent(agent, release, selected, options)));
+ }
+ status() {
+ const detected = this.detectAgents();
+ return skills_agents_1.SKILLS_AGENTS.flatMap((agent) => skills_agents_1.SKILLS_SCOPES.map((scope) => ({
+ agent,
+ scope,
+ detected: detected.includes(agent),
+ skills: readMarker(this.skillsPath(agent, scope))[agent] ?? [],
+ mcpConfigured: (0, skills_mcp_config_1.hasForgeMcpServers)(agent, scope, this.locations)
+ })));
+ }
+ installedAgents(scope) {
+ return this.status()
+ .filter((entry) => entry.scope === scope && (entry.skills.length > 0 || entry.mcpConfigured))
+ .map((entry) => entry.agent);
+ }
+ remove(agents, scope) {
+ return agents.map((agent) => this.attempt(agent, scope, () => this.removeAgent(agent, scope)));
+ }
+ shouldSuggestInstall() {
+ try {
+ const status = this.status();
+ return (status.some((entry) => entry.detected) && !status.some((entry) => entry.skills.length || entry.mcpConfigured));
+ }
+ catch {
+ return false;
+ }
+ }
+ skillsPath(agent, scope) {
+ return (0, skills_agents_1.resolveAgentPath)(skills_agents_1.SKILLS_AGENT_REGISTRY[agent][scope].skills, this.locations);
+ }
+ installAgent(agent, release, selected, { scope, mcp, dryRun }) {
+ const skillsPath = this.skillsPath(agent, scope);
+ const files = release.files.filter((file) => selected.includes((0, skills_download_1.skillOf)(file.path)));
+ const result = { agent, scope, skillsPath, skills: selected };
+ if (dryRun)
+ result.files = files.map((file) => (0, path_1.join)(skillsPath, file.path));
+ const setMcp = (plan) => (0, skills_mcp_config_1.setForgeMcpServers)(agent, scope, release.servers, { ...this.locations, dryRun: plan });
+ let change = mcp ? setMcp(true) : undefined;
+ if (!dryRun) {
+ writeSkills(skillsPath, agent, selected, files);
+ try {
+ if (mcp)
+ change = setMcp(false);
+ }
+ catch (error) {
+ return { ...result, error: errorMessage(error) };
+ }
+ }
+ if (mcp)
+ result.mcp = change ? [change] : [];
+ return result;
+ }
+ removeAgent(agent, scope) {
+ const skillsPath = this.skillsPath(agent, scope);
+ (0, skills_mcp_config_1.removeForgeMcpServers)(agent, scope, { ...this.locations, dryRun: true });
+ const marker = readMarker(skillsPath);
+ const installed = marker[agent] ?? [];
+ const keptSkills = installed.filter((skill) => usedByOthers(marker, agent, skill));
+ const skills = installed.filter((skill) => !keptSkills.includes(skill));
+ for (const skill of skills)
+ (0, fs_1.rmSync)((0, path_1.join)(skillsPath, skill), { recursive: true, force: true });
+ delete marker[agent];
+ writeMarker(skillsPath, marker);
+ const result = { agent, scope, skillsPath, skills, keptSkills };
+ try {
+ return { ...result, mcp: (0, skills_mcp_config_1.removeForgeMcpServers)(agent, scope, this.locations) };
+ }
+ catch (error) {
+ return { ...result, error: errorMessage(error) };
+ }
+ }
+ attempt(agent, scope, work) {
+ try {
+ return work();
+ }
+ catch (error) {
+ return { agent, scope, skillsPath: this.skillsPath(agent, scope), skills: [], error: errorMessage(error) };
+ }
+ }
+}
+exports.SkillsService = SkillsService;
+function writeSkills(skillsPath, agent, selected, files) {
+ const marker = readMarker(skillsPath);
+ const stale = (marker[agent] ?? []).filter((skill) => !selected.includes(skill) && !usedByOthers(marker, agent, skill));
+ for (const skill of [...selected, ...stale])
+ (0, fs_1.rmSync)((0, path_1.join)(skillsPath, skill), { recursive: true, force: true });
+ for (const file of files) {
+ const path = (0, path_1.join)(skillsPath, file.path);
+ (0, fs_1.mkdirSync)((0, path_1.dirname)(path), { recursive: true });
+ (0, fs_1.writeFileSync)(path, file.data, { mode: file.mode });
+ }
+ writeMarker(skillsPath, { ...marker, [agent]: selected });
+}
+const errorMessage = (error) => (error instanceof Error ? error.message : String(error));
+const usedByOthers = (marker, agent, skill) => Object.entries(marker).some(([owner, skills]) => owner !== agent && skills?.includes(skill));
+function readMarker(skillsPath) {
+ let parsed;
+ try {
+ parsed = JSON.parse((0, fs_1.readFileSync)((0, path_1.join)(skillsPath, MARKER_FILE), 'utf8'));
+ }
+ catch {
+ return {};
+ }
+ const marker = {};
+ for (const agent of skills_agents_1.SKILLS_AGENTS) {
+ const skills = parsed?.[agent];
+ if (Array.isArray(skills))
+ marker[agent] = skills.filter((skill) => typeof skill === 'string' && skills_download_1.SKILL_NAME.test(skill));
+ }
+ return marker;
+}
+function writeMarker(skillsPath, marker) {
+ const path = (0, path_1.join)(skillsPath, MARKER_FILE);
+ if (!Object.keys(marker).length) {
+ (0, fs_1.rmSync)(path, { force: true });
+ return;
+ }
+ (0, fs_1.mkdirSync)(skillsPath, { recursive: true });
+ (0, fs_1.writeFileSync)(path, `${JSON.stringify(marker, null, 2)}\n`);
+}
+//# sourceMappingURL=skills-service.js.map
\ No newline at end of file