From fb673f754e3fd154774cbd2969a1367f89301498 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Fri, 18 Sep 2026 22:20:18 +0800 Subject: [PATCH] Add multi-host skill bridge for WorkBuddy, Claude, and Qoder Decouple agent host skill bridging into dedicated module src/core/agent-hosts/: - antigravity.ts: manage ~/.gemini/config/skills.json - symlink-hosts.ts: detect and bridge ~/.workbuddy, ~/.claude, and ~/.qoder - types.ts: explicit types for host results and statuses - agent-hosts.test.ts: isolated unit tests for host discovery and symlinks Respect existing parent symlinks, replace legacy real directories safely, prevent dotfile pollution by only acting when host roots exist, and expose unified hosts array in seal-use skill install & status. --- src/cli/commands/skill.ts | 16 +- src/core/agent-hosts/agent-hosts.test.ts | 295 +++++++++++++++++++++++ src/core/agent-hosts/antigravity.ts | 127 ++++++++++ src/core/agent-hosts/index.ts | 48 ++++ src/core/agent-hosts/symlink-hosts.ts | 241 ++++++++++++++++++ src/core/agent-hosts/types.ts | 58 +++++ src/core/embedded-skill.test.ts | 114 +-------- src/core/embedded-skill.ts | 140 +---------- 8 files changed, 785 insertions(+), 254 deletions(-) create mode 100644 src/core/agent-hosts/agent-hosts.test.ts create mode 100644 src/core/agent-hosts/antigravity.ts create mode 100644 src/core/agent-hosts/index.ts create mode 100644 src/core/agent-hosts/symlink-hosts.ts create mode 100644 src/core/agent-hosts/types.ts diff --git a/src/cli/commands/skill.ts b/src/cli/commands/skill.ts index bb4308a..d84ff1f 100644 --- a/src/cli/commands/skill.ts +++ b/src/cli/commands/skill.ts @@ -1,11 +1,15 @@ import { existsSync } from "node:fs"; import { join } from "node:path"; import { + getAllLinkableHostsStatus, getAntigravitySkillsStatus, + syncAllLinkableHosts, + syncAntigravitySkillsConfig +} from "../../core/agent-hosts/index.js"; +import { getDefaultGlobalSkillDir, getSkillContent, - installSkillFiles, - syncAntigravitySkillsConfig + installSkillFiles } from "../../core/embedded-skill.js"; import { booleanOption, stringOption } from "../parser.js"; import { printJson } from "../printer.js"; @@ -29,13 +33,15 @@ export function handleSkillCommand(action: string | undefined, maybeName: string const result = installSkillFiles(targetDir); const isGlobal = !isLocal && !customDir; const antigravity = isGlobal ? syncAntigravitySkillsConfig() : undefined; + const hosts = isGlobal ? syncAllLinkableHosts() : undefined; printJson({ installed: true, targetDir: result.targetDir, fileCount: result.files.length, files: result.files, - ...(antigravity ? { antigravity } : {}) + ...(antigravity ? { antigravity } : {}), + ...(hosts ? { hosts } : {}) }); return; } @@ -44,6 +50,7 @@ export function handleSkillCommand(action: string | undefined, maybeName: string const globalDir = getDefaultGlobalSkillDir(); const localDir = join(process.cwd(), ".agents", "skills", "seal-use"); const antigravity = getAntigravitySkillsStatus(); + const hosts = getAllLinkableHostsStatus(); printJson({ global: { dir: globalDir, @@ -53,7 +60,8 @@ export function handleSkillCommand(action: string | undefined, maybeName: string dir: localDir, installed: existsSync(join(localDir, "SKILL.md")) }, - antigravity + antigravity, + hosts }); return; } diff --git a/src/core/agent-hosts/agent-hosts.test.ts b/src/core/agent-hosts/agent-hosts.test.ts new file mode 100644 index 0000000..0277ce7 --- /dev/null +++ b/src/core/agent-hosts/agent-hosts.test.ts @@ -0,0 +1,295 @@ +import { describe, expect, test } from "bun:test"; +import { + existsSync, + lstatSync, + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + rmSync, + symlinkSync, + writeFileSync +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + getAllAgentHostsStatus, + getAllLinkableHostsStatus, + getAntigravitySkillsStatus, + getHostSkillsStatus, + KNOWN_LINKABLE_HOSTS, + syncAllAgentHosts, + syncAntigravitySkillsConfig, + syncHostSkills +} from "./index.js"; + +describe("antigravity skills config sync", () => { + test("creates skills.json when it does not exist", () => { + const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); + const configFile = join(tmp, "skills.json"); + const targetDir = "/custom/test/path/.agents/skills"; + + try { + const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); + expect(res.configured).toBe(true); + expect(res.action).toBe("created"); + expect(res.targetDir).toBe(targetDir); + + const status = getAntigravitySkillsStatus({ configFile, targetSkillsDir: targetDir }); + expect(status.configured).toBe(true); + + const content = JSON.parse(readFileSync(configFile, "utf8")); + expect(content.entries).toEqual([{ path: targetDir }]); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("does nothing when target directory is already present", () => { + const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); + const configFile = join(tmp, "skills.json"); + const targetDir = "/custom/test/path/.agents/skills"; + + try { + const initial = { + entries: [ + { path: "/other/skill/dir" }, + { path: targetDir } + ] + }; + writeFileSync(configFile, JSON.stringify(initial, null, 2)); + + const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); + expect(res.configured).toBe(true); + expect(res.action).toBe("already_present"); + + const content = JSON.parse(readFileSync(configFile, "utf8")); + expect(content.entries.length).toBe(2); + expect(content.entries[1].path).toBe(targetDir); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("prepends target directory to first position when absent", () => { + const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); + const configFile = join(tmp, "skills.json"); + const targetDir = "/custom/test/path/.agents/skills"; + + try { + const initial = { + entries: [ + { path: "/existing/first/dir" }, + { path: "/existing/second/dir" } + ] + }; + writeFileSync(configFile, JSON.stringify(initial, null, 2)); + + const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); + expect(res.configured).toBe(true); + expect(res.action).toBe("prepended"); + + const content = JSON.parse(readFileSync(configFile, "utf8")); + expect(content.entries.length).toBe(3); + expect(content.entries[0].path).toBe(targetDir); + expect(content.entries[1].path).toBe("/existing/first/dir"); + expect(content.entries[2].path).toBe("/existing/second/dir"); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("updates legacy tilde path to absolute target directory", () => { + const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); + const configFile = join(tmp, "skills.json"); + const targetDir = "/custom/test/path/.agents/skills"; + + try { + const initial = { + entries: [ + { path: "~/.agents/skills" }, + { path: "/other/dir" } + ] + }; + writeFileSync(configFile, JSON.stringify(initial, null, 2)); + + const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); + expect(res.configured).toBe(true); + expect(res.action).toBe("updated_tilde"); + + const content = JSON.parse(readFileSync(configFile, "utf8")); + expect(content.entries.length).toBe(2); + expect(content.entries[0].path).toBe(targetDir); + expect(content.entries[1].path).toBe("/other/dir"); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); +}); + +describe("symlink-based host skill bridge", () => { + const host = KNOWN_LINKABLE_HOSTS.find((h) => h.id === "workbuddy")!; + + test("skips host cleanly if host root directory does not exist", () => { + const tmp = mkdtempSync(join(tmpdir(), "host-test-")); + try { + const res = syncHostSkills(host, { baseHomeDir: tmp }); + expect(res.detected).toBe(false); + expect(res.configured).toBe(false); + expect(res.mode).toBe("skipped_not_detected"); + + const status = getHostSkillsStatus(host, { baseHomeDir: tmp }); + expect(status.detected).toBe(false); + expect(status.mode).toBe("not_detected"); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("detects parent directory symlink (~//skills -> ~/.agents/skills) and does not mutate", () => { + const tmp = mkdtempSync(join(tmpdir(), "host-parent-")); + try { + const agentsSkillsDir = join(tmp, ".agents", "skills"); + const sourceSkill = join(agentsSkillsDir, "seal-use"); + mkdirSync(sourceSkill, { recursive: true }); + + const hostRoot = join(tmp, host.rootDirName); + mkdirSync(hostRoot, { recursive: true }); + const hostSkills = join(hostRoot, "skills"); + + symlinkSync(agentsSkillsDir, hostSkills, "dir"); + + const res = syncHostSkills(host, { + baseHomeDir: tmp, + sourceSkillDir: sourceSkill + }); + expect(res.detected).toBe(true); + expect(res.configured).toBe(true); + expect(res.mode).toBe("parent_symlinked"); + + const status = getHostSkillsStatus(host, { + baseHomeDir: tmp, + sourceSkillDir: sourceSkill + }); + expect(status.configured).toBe(true); + expect(status.mode).toBe("parent_symlinked"); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("creates child symlink when host skills is an independent directory", () => { + const tmp = mkdtempSync(join(tmpdir(), "host-child-")); + try { + const sourceSkill = join(tmp, ".agents", "skills", "seal-use"); + mkdirSync(sourceSkill, { recursive: true }); + writeFileSync(join(sourceSkill, "SKILL.md"), "# Seal Skill"); + + const hostRoot = join(tmp, host.rootDirName); + mkdirSync(join(hostRoot, "skills"), { recursive: true }); + + const res = syncHostSkills(host, { + baseHomeDir: tmp, + sourceSkillDir: sourceSkill + }); + expect(res.detected).toBe(true); + expect(res.configured).toBe(true); + expect(res.mode).toBe("linked"); + + const linkPath = join(hostRoot, "skills", "seal-use"); + expect(lstatSync(linkPath).isSymbolicLink()).toBe(true); + expect(realpathSync(linkPath)).toBe(realpathSync(sourceSkill)); + + // Running sync again should be idempotent + const resAgain = syncHostSkills(host, { + baseHomeDir: tmp, + sourceSkillDir: sourceSkill + }); + expect(resAgain.mode).toBe("already_linked"); + + const status = getHostSkillsStatus(host, { + baseHomeDir: tmp, + sourceSkillDir: sourceSkill + }); + expect(status.configured).toBe(true); + expect(status.mode).toBe("single_symlinked"); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("heals broken dead symlinks by unlinking and recreating", () => { + const tmp = mkdtempSync(join(tmpdir(), "host-broken-")); + try { + const sourceSkill = join(tmp, ".agents", "skills", "seal-use"); + mkdirSync(sourceSkill, { recursive: true }); + + const hostRoot = join(tmp, host.rootDirName); + const hostSkills = join(hostRoot, "skills"); + mkdirSync(hostSkills, { recursive: true }); + + // Create broken link + const targetLink = join(hostSkills, "seal-use"); + symlinkSync(join(tmp, "non-existent-old-target"), targetLink, "dir"); + + const res = syncHostSkills(host, { + baseHomeDir: tmp, + sourceSkillDir: sourceSkill + }); + expect(res.detected).toBe(true); + expect(res.configured).toBe(true); + expect(res.mode).toBe("linked"); + expect(realpathSync(targetLink)).toBe(realpathSync(sourceSkill)); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("safely replaces legacy real directory with symlink to prevent version drift", () => { + const tmp = mkdtempSync(join(tmpdir(), "host-realdir-")); + try { + const sourceSkill = join(tmp, ".agents", "skills", "seal-use"); + mkdirSync(sourceSkill, { recursive: true }); + + const hostRoot = join(tmp, host.rootDirName); + const targetDir = join(hostRoot, "skills", "seal-use"); + mkdirSync(targetDir, { recursive: true }); + writeFileSync(join(targetDir, "SKILL.md"), "# Old Stale Skill"); + + const res = syncHostSkills(host, { + baseHomeDir: tmp, + sourceSkillDir: sourceSkill + }); + expect(res.detected).toBe(true); + expect(res.configured).toBe(true); + expect(res.mode).toBe("replaced_real_dir"); + + expect(lstatSync(targetDir).isSymbolicLink()).toBe(true); + expect(realpathSync(targetDir)).toBe(realpathSync(sourceSkill)); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); + + test("syncAllAgentHosts aggregates Antigravity and all known hosts", () => { + const tmp = mkdtempSync(join(tmpdir(), "all-hosts-")); + try { + const result = syncAllAgentHosts({ + baseHomeDir: tmp, + antigravityConfigFile: join(tmp, "gemini-skills.json") + }); + expect(result.antigravity.configured).toBe(true); + expect(result.hosts.length).toBe(KNOWN_LINKABLE_HOSTS.length); + expect(result.hosts.every((h) => h.mode === "skipped_not_detected")).toBe(true); + + const status = getAllAgentHostsStatus({ + baseHomeDir: tmp, + antigravityConfigFile: join(tmp, "gemini-skills.json") + }); + expect(status.antigravity.configured).toBe(true); + expect(status.hosts.length).toBe(KNOWN_LINKABLE_HOSTS.length); + } finally { + rmSync(tmp, { recursive: true, force: true }); + } + }); +}); diff --git a/src/core/agent-hosts/antigravity.ts b/src/core/agent-hosts/antigravity.ts new file mode 100644 index 0000000..4a0a217 --- /dev/null +++ b/src/core/agent-hosts/antigravity.ts @@ -0,0 +1,127 @@ +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import { homedir } from "node:os"; +import { dirname, join } from "node:path"; +import type { AntigravitySkillsStatus, AntigravitySkillsSyncResult } from "./types.js"; + +export function getGlobalAgentsSkillsDir(): string { + return join(homedir(), ".agents", "skills"); +} + +export function getAntigravitySkillsConfigFile(): string { + return join(homedir(), ".gemini", "config", "skills.json"); +} + +export function isAntigravityDetected(): boolean { + const home = homedir(); + return ( + existsSync(join(home, ".gemini", "antigravity")) || + existsSync(join(home, ".gemini", "antigravity-cli")) || + existsSync(join(home, ".local", "bin", "agy")) + ); +} + +export function syncAntigravitySkillsConfig(options?: { + configFile?: string; + targetSkillsDir?: string; +}): AntigravitySkillsSyncResult { + const targetDir = options?.targetSkillsDir ?? getGlobalAgentsSkillsDir(); + const configPath = options?.configFile ?? getAntigravitySkillsConfigFile(); + const detected = isAntigravityDetected(); + + if (!existsSync(configPath)) { + mkdirSync(dirname(configPath), { recursive: true }); + const initialConfig = { + entries: [{ path: targetDir }] + }; + writeFileSync(configPath, JSON.stringify(initialConfig, null, 2) + "\n", "utf8"); + return { + detected, + configured: true, + configPath, + targetDir, + action: "created" + }; + } + + let parsed: { entries?: Array<{ path?: string; [key: string]: unknown }>; [key: string]: unknown } = {}; + try { + parsed = JSON.parse(readFileSync(configPath, "utf8")); + } catch { + parsed = { entries: [] }; + } + + if (!Array.isArray(parsed.entries)) { + parsed.entries = []; + } + + const exactIndex = parsed.entries.findIndex( + (entry) => entry && typeof entry.path === "string" && entry.path === targetDir + ); + if (exactIndex !== -1) { + return { + detected, + configured: true, + configPath, + targetDir, + action: "already_present" + }; + } + + const tildeIndex = parsed.entries.findIndex( + (entry) => + entry && + typeof entry.path === "string" && + (entry.path === "~/.agents/skills" || entry.path === "~/.agents/skills/") + ); + if (tildeIndex !== -1) { + parsed.entries[tildeIndex] = { ...parsed.entries[tildeIndex], path: targetDir }; + writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n", "utf8"); + return { + detected, + configured: true, + configPath, + targetDir, + action: "updated_tilde" + }; + } + + parsed.entries.unshift({ path: targetDir }); + writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n", "utf8"); + return { + detected, + configured: true, + configPath, + targetDir, + action: "prepended" + }; +} + +export function getAntigravitySkillsStatus(options?: { + configFile?: string; + targetSkillsDir?: string; +}): AntigravitySkillsStatus { + const targetDir = options?.targetSkillsDir ?? getGlobalAgentsSkillsDir(); + const configPath = options?.configFile ?? getAntigravitySkillsConfigFile(); + const detected = isAntigravityDetected(); + let configured = false; + + if (existsSync(configPath)) { + try { + const parsed = JSON.parse(readFileSync(configPath, "utf8")); + if (Array.isArray(parsed?.entries)) { + configured = parsed.entries.some( + (e: { path?: string }) => e?.path === targetDir + ); + } + } catch { + // ignore + } + } + + return { + detected, + configured, + configPath, + targetDir + }; +} diff --git a/src/core/agent-hosts/index.ts b/src/core/agent-hosts/index.ts new file mode 100644 index 0000000..0882ac1 --- /dev/null +++ b/src/core/agent-hosts/index.ts @@ -0,0 +1,48 @@ +export * from "./types.js"; +export * from "./antigravity.js"; +export * from "./symlink-hosts.js"; + +import { + getAntigravitySkillsStatus, + syncAntigravitySkillsConfig +} from "./antigravity.js"; +import { + getAllLinkableHostsStatus, + syncAllLinkableHosts +} from "./symlink-hosts.js"; +import type { AllAgentHostsStatus, AllAgentHostsSyncResult } from "./types.js"; + +export function syncAllAgentHosts(options?: { + baseHomeDir?: string; + sourceSkillDir?: string; + antigravityConfigFile?: string; +}): AllAgentHostsSyncResult { + const antigravity = syncAntigravitySkillsConfig({ + configFile: options?.antigravityConfigFile, + targetSkillsDir: options?.sourceSkillDir + ? undefined + : undefined + }); + const hosts = syncAllLinkableHosts(options); + + return { + antigravity, + hosts + }; +} + +export function getAllAgentHostsStatus(options?: { + baseHomeDir?: string; + sourceSkillDir?: string; + antigravityConfigFile?: string; +}): AllAgentHostsStatus { + const antigravity = getAntigravitySkillsStatus({ + configFile: options?.antigravityConfigFile + }); + const hosts = getAllLinkableHostsStatus(options); + + return { + antigravity, + hosts + }; +} diff --git a/src/core/agent-hosts/symlink-hosts.ts b/src/core/agent-hosts/symlink-hosts.ts new file mode 100644 index 0000000..a200d18 --- /dev/null +++ b/src/core/agent-hosts/symlink-hosts.ts @@ -0,0 +1,241 @@ +import { + cpSync, + existsSync, + lstatSync, + mkdirSync, + realpathSync, + rmSync, + symlinkSync, + unlinkSync +} from "node:fs"; +import { homedir } from "node:os"; +import { dirname, join } from "node:path"; +import type { + HostSkillsStatus, + HostSkillsSyncResult, + LinkableHostDefinition +} from "./types.js"; + +export const KNOWN_LINKABLE_HOSTS: LinkableHostDefinition[] = [ + { id: "workbuddy", name: "WorkBuddy", rootDirName: ".workbuddy" }, + { id: "claude", name: "Claude", rootDirName: ".claude" }, + { id: "qoder", name: "Qoder", rootDirName: ".qoder" } +]; + +export function syncHostSkills( + host: LinkableHostDefinition, + options?: { baseHomeDir?: string; sourceSkillDir?: string } +): HostSkillsSyncResult { + const home = options?.baseHomeDir ?? homedir(); + const hostRootDir = join(home, host.rootDirName); + const sourceSkill = options?.sourceSkillDir ?? join(home, ".agents", "skills", "seal-use"); + const agentsSkillsDir = dirname(sourceSkill); + const hostSkillsDir = join(hostRootDir, "skills"); + const targetSkillLink = join(hostSkillsDir, "seal-use"); + + // Rule 1: No dotfile pollution. Skip if host root directory doesn't exist. + if (!existsSync(hostRootDir)) { + return { + id: host.id, + name: host.name, + detected: false, + configured: false, + mode: "skipped_not_detected" + }; + } + + // Rule 2: Check parent directory symlink (~//skills -> ~/.agents/skills) + if (existsSync(hostSkillsDir) && existsSync(agentsSkillsDir)) { + try { + if (realpathSync(hostSkillsDir) === realpathSync(agentsSkillsDir)) { + return { + id: host.id, + name: host.name, + detected: true, + configured: true, + mode: "parent_symlinked", + targetPath: hostSkillsDir + }; + } + } catch { + // ignore + } + } + + // Ensure ~//skills directory exists + mkdirSync(hostSkillsDir, { recursive: true }); + + // Rule 3: Inspect child entry ~//skills/seal-use (dead symlink, real dir, etc.) + let stat: ReturnType | undefined; + try { + stat = lstatSync(targetSkillLink); + } catch { + stat = undefined; + } + + let replacedRealDir = false; + + if (stat) { + if (stat.isSymbolicLink()) { + try { + if (realpathSync(targetSkillLink) === realpathSync(sourceSkill)) { + return { + id: host.id, + name: host.name, + detected: true, + configured: true, + mode: "already_linked", + targetPath: targetSkillLink + }; + } + } catch { + // Dead symlink: target disappeared or broken + } + try { + unlinkSync(targetSkillLink); + } catch { + // ignore + } + } else if (stat.isDirectory()) { + // Rule 4: Existing real directory: replace with symlink to prevent version drift + rmSync(targetSkillLink, { recursive: true, force: true }); + replacedRealDir = true; + } else { + // Stray file or other artifact + unlinkSync(targetSkillLink); + } + } + + // Rule 5: Create symlink with Windows junction fallback for non-admin privileges + const symlinkType = process.platform === "win32" ? "junction" : "dir"; + try { + symlinkSync(sourceSkill, targetSkillLink, symlinkType); + return { + id: host.id, + name: host.name, + detected: true, + configured: true, + mode: replacedRealDir ? "replaced_real_dir" : "linked", + targetPath: targetSkillLink + }; + } catch { + try { + cpSync(sourceSkill, targetSkillLink, { recursive: true }); + return { + id: host.id, + name: host.name, + detected: true, + configured: true, + mode: "fallback_copied", + targetPath: targetSkillLink + }; + } catch (cpErr: any) { + return { + id: host.id, + name: host.name, + detected: true, + configured: false, + mode: "error", + targetPath: targetSkillLink, + error: cpErr?.message || String(cpErr) + }; + } + } +} + +export function getHostSkillsStatus( + host: LinkableHostDefinition, + options?: { baseHomeDir?: string; sourceSkillDir?: string } +): HostSkillsStatus { + const home = options?.baseHomeDir ?? homedir(); + const hostRootDir = join(home, host.rootDirName); + const sourceSkill = options?.sourceSkillDir ?? join(home, ".agents", "skills", "seal-use"); + const agentsSkillsDir = dirname(sourceSkill); + const hostSkillsDir = join(hostRootDir, "skills"); + const targetSkillLink = join(hostSkillsDir, "seal-use"); + + if (!existsSync(hostRootDir)) { + return { + id: host.id, + name: host.name, + detected: false, + configured: false, + mode: "not_detected" + }; + } + + if (existsSync(hostSkillsDir) && existsSync(agentsSkillsDir)) { + try { + if (realpathSync(hostSkillsDir) === realpathSync(agentsSkillsDir)) { + return { + id: host.id, + name: host.name, + detected: true, + configured: true, + mode: "parent_symlinked", + targetPath: hostSkillsDir + }; + } + } catch { + // ignore + } + } + + let stat: ReturnType | undefined; + try { + stat = lstatSync(targetSkillLink); + } catch { + stat = undefined; + } + + if (stat) { + if (stat.isSymbolicLink()) { + try { + if (realpathSync(targetSkillLink) === realpathSync(sourceSkill)) { + return { + id: host.id, + name: host.name, + detected: true, + configured: true, + mode: "single_symlinked", + targetPath: targetSkillLink + }; + } + } catch { + // dead symlink + } + } else if (stat.isDirectory()) { + return { + id: host.id, + name: host.name, + detected: true, + configured: true, + mode: "real_directory", + targetPath: targetSkillLink + }; + } + } + + return { + id: host.id, + name: host.name, + detected: true, + configured: false, + mode: "not_configured", + targetPath: targetSkillLink + }; +} + +export function syncAllLinkableHosts(options?: { + baseHomeDir?: string; + sourceSkillDir?: string; +}): HostSkillsSyncResult[] { + return KNOWN_LINKABLE_HOSTS.map((host) => syncHostSkills(host, options)); +} + +export function getAllLinkableHostsStatus(options?: { + baseHomeDir?: string; + sourceSkillDir?: string; +}): HostSkillsStatus[] { + return KNOWN_LINKABLE_HOSTS.map((host) => getHostSkillsStatus(host, options)); +} diff --git a/src/core/agent-hosts/types.ts b/src/core/agent-hosts/types.ts new file mode 100644 index 0000000..cd5ecde --- /dev/null +++ b/src/core/agent-hosts/types.ts @@ -0,0 +1,58 @@ +export type AntigravitySkillsSyncResult = { + detected: boolean; + configured: boolean; + configPath: string; + targetDir: string; + action: "created" | "prepended" | "already_present" | "updated_tilde"; +}; + +export type AntigravitySkillsStatus = { + detected: boolean; + configured: boolean; + configPath: string; + targetDir: string; +}; + +export type LinkableHostId = "workbuddy" | "claude" | "qoder"; + +export type LinkableHostDefinition = { + id: LinkableHostId; + name: string; + rootDirName: string; +}; + +export type HostSkillsSyncResult = { + id: LinkableHostId; + name: string; + detected: boolean; + configured: boolean; + mode: + | "skipped_not_detected" + | "parent_symlinked" + | "linked" + | "already_linked" + | "replaced_real_dir" + | "fallback_copied" + | "error"; + targetPath?: string; + error?: string; +}; + +export type HostSkillsStatus = { + id: LinkableHostId; + name: string; + detected: boolean; + configured: boolean; + mode: "not_detected" | "parent_symlinked" | "single_symlinked" | "real_directory" | "not_configured"; + targetPath?: string; +}; + +export type AllAgentHostsSyncResult = { + antigravity: AntigravitySkillsSyncResult; + hosts: HostSkillsSyncResult[]; +}; + +export type AllAgentHostsStatus = { + antigravity: AntigravitySkillsStatus; + hosts: HostSkillsStatus[]; +}; diff --git a/src/core/embedded-skill.test.ts b/src/core/embedded-skill.test.ts index 393b706..dca85fb 100644 --- a/src/core/embedded-skill.test.ts +++ b/src/core/embedded-skill.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "bun:test"; -import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import packageJson from "../../package.json" with { type: "json" }; @@ -8,12 +8,10 @@ import { EMBEDDED_SKILL_ASSETS, EMBEDDED_SKILL_FILES, EMBEDDED_SKILL_VERSION, - getAntigravitySkillsStatus, getSkillContent, installSkillFiles, parseSkillVersion, - readInstalledSkillVersion, - syncAntigravitySkillsConfig + readInstalledSkillVersion } from "./embedded-skill.js"; describe("embedded-skill", () => { @@ -46,10 +44,6 @@ describe("embedded-skill", () => { }); describe("embedded-skill version contract", () => { - // A version-tagged document that nothing asserts on drifts silently: `seal-use update` shipped - // the previous release's skill for several versions, because "the file was written" looked like - // success from every angle. Keeping the skill and the package in step is a failing test now - // rather than a discovery. test("the skill's version matches the package version", () => { expect(EMBEDDED_SKILL_VERSION).toBe(packageJson.version); }); @@ -70,107 +64,3 @@ describe("embedded-skill version contract", () => { } }); }); - -describe("antigravity skills config sync", () => { - test("creates skills.json when it does not exist", async () => { - const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); - const configFile = join(tmp, "skills.json"); - const targetDir = "/custom/test/path/.agents/skills"; - - try { - const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); - expect(res.configured).toBe(true); - expect(res.action).toBe("created"); - expect(res.targetDir).toBe(targetDir); - - const status = getAntigravitySkillsStatus({ configFile, targetSkillsDir: targetDir }); - expect(status.configured).toBe(true); - - const content = JSON.parse(readFileSync(configFile, "utf8")); - expect(content.entries).toEqual([{ path: targetDir }]); - } finally { - rmSync(tmp, { recursive: true, force: true }); - } - }); - - test("does nothing when target directory is already present", () => { - const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); - const configFile = join(tmp, "skills.json"); - const targetDir = "/custom/test/path/.agents/skills"; - - try { - const initial = { - entries: [ - { path: "/other/skill/dir" }, - { path: targetDir } - ] - }; - writeFileSync(configFile, JSON.stringify(initial, null, 2)); - - const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); - expect(res.configured).toBe(true); - expect(res.action).toBe("already_present"); - - const content = JSON.parse(readFileSync(configFile, "utf8")); - expect(content.entries.length).toBe(2); - expect(content.entries[1].path).toBe(targetDir); - } finally { - rmSync(tmp, { recursive: true, force: true }); - } - }); - - test("prepends target directory to first position when absent", () => { - const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); - const configFile = join(tmp, "skills.json"); - const targetDir = "/custom/test/path/.agents/skills"; - - try { - const initial = { - entries: [ - { path: "/existing/first/dir" }, - { path: "/existing/second/dir" } - ] - }; - writeFileSync(configFile, JSON.stringify(initial, null, 2)); - - const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); - expect(res.configured).toBe(true); - expect(res.action).toBe("prepended"); - - const content = JSON.parse(readFileSync(configFile, "utf8")); - expect(content.entries.length).toBe(3); - expect(content.entries[0].path).toBe(targetDir); - expect(content.entries[1].path).toBe("/existing/first/dir"); - expect(content.entries[2].path).toBe("/existing/second/dir"); - } finally { - rmSync(tmp, { recursive: true, force: true }); - } - }); - - test("updates legacy tilde path to absolute target directory", () => { - const tmp = mkdtempSync(join(tmpdir(), "agy-config-")); - const configFile = join(tmp, "skills.json"); - const targetDir = "/custom/test/path/.agents/skills"; - - try { - const initial = { - entries: [ - { path: "~/.agents/skills" }, - { path: "/other/dir" } - ] - }; - writeFileSync(configFile, JSON.stringify(initial, null, 2)); - - const res = syncAntigravitySkillsConfig({ configFile, targetSkillsDir: targetDir }); - expect(res.configured).toBe(true); - expect(res.action).toBe("updated_tilde"); - - const content = JSON.parse(readFileSync(configFile, "utf8")); - expect(content.entries.length).toBe(2); - expect(content.entries[0].path).toBe(targetDir); - expect(content.entries[1].path).toBe("/other/dir"); - } finally { - rmSync(tmp, { recursive: true, force: true }); - } - }); -}); diff --git a/src/core/embedded-skill.ts b/src/core/embedded-skill.ts index c13d261..2d2d676 100644 --- a/src/core/embedded-skill.ts +++ b/src/core/embedded-skill.ts @@ -105,141 +105,5 @@ export function syncGlobalSkillIfPresent(): { synced: boolean; targetDir?: strin return { synced: false }; } -export type AntigravitySkillsSyncResult = { - detected: boolean; - configured: boolean; - configPath: string; - targetDir: string; - action: "created" | "prepended" | "already_present" | "updated_tilde"; -}; - -export type AntigravitySkillsStatus = { - detected: boolean; - configured: boolean; - configPath: string; - targetDir: string; -}; - -export function getGlobalAgentsSkillsDir(): string { - return join(homedir(), ".agents", "skills"); -} - -export function getAntigravitySkillsConfigFile(): string { - return join(homedir(), ".gemini", "config", "skills.json"); -} - -export function isAntigravityDetected(): boolean { - const home = homedir(); - return ( - existsSync(join(home, ".gemini", "antigravity")) || - existsSync(join(home, ".gemini", "antigravity-cli")) || - existsSync(join(home, ".local", "bin", "agy")) - ); -} - -export function syncAntigravitySkillsConfig(options?: { - configFile?: string; - targetSkillsDir?: string; -}): AntigravitySkillsSyncResult { - const targetDir = options?.targetSkillsDir ?? getGlobalAgentsSkillsDir(); - const configPath = options?.configFile ?? getAntigravitySkillsConfigFile(); - const detected = isAntigravityDetected(); - - if (!existsSync(configPath)) { - mkdirSync(dirname(configPath), { recursive: true }); - const initialConfig = { - entries: [{ path: targetDir }] - }; - writeFileSync(configPath, JSON.stringify(initialConfig, null, 2) + "\n", "utf8"); - return { - detected, - configured: true, - configPath, - targetDir, - action: "created" - }; - } - - let parsed: { entries?: Array<{ path?: string; [key: string]: unknown }>; [key: string]: unknown } = {}; - try { - parsed = JSON.parse(readFileSync(configPath, "utf8")); - } catch { - parsed = { entries: [] }; - } - - if (!Array.isArray(parsed.entries)) { - parsed.entries = []; - } - - const exactIndex = parsed.entries.findIndex( - (entry) => entry && typeof entry.path === "string" && entry.path === targetDir - ); - if (exactIndex !== -1) { - return { - detected, - configured: true, - configPath, - targetDir, - action: "already_present" - }; - } - - const tildeIndex = parsed.entries.findIndex( - (entry) => - entry && - typeof entry.path === "string" && - (entry.path === "~/.agents/skills" || entry.path === "~/.agents/skills/") - ); - if (tildeIndex !== -1) { - parsed.entries[tildeIndex] = { ...parsed.entries[tildeIndex], path: targetDir }; - writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n", "utf8"); - return { - detected, - configured: true, - configPath, - targetDir, - action: "updated_tilde" - }; - } - - parsed.entries.unshift({ path: targetDir }); - writeFileSync(configPath, JSON.stringify(parsed, null, 2) + "\n", "utf8"); - return { - detected, - configured: true, - configPath, - targetDir, - action: "prepended" - }; -} - -export function getAntigravitySkillsStatus(options?: { - configFile?: string; - targetSkillsDir?: string; -}): AntigravitySkillsStatus { - const targetDir = options?.targetSkillsDir ?? getGlobalAgentsSkillsDir(); - const configPath = options?.configFile ?? getAntigravitySkillsConfigFile(); - const detected = isAntigravityDetected(); - let configured = false; - - if (existsSync(configPath)) { - try { - const parsed = JSON.parse(readFileSync(configPath, "utf8")); - if (Array.isArray(parsed?.entries)) { - configured = parsed.entries.some( - (e: { path?: string }) => e?.path === targetDir - ); - } - } catch { - // ignore - } - } - - return { - detected, - configured, - configPath, - targetDir - }; -} - +// Re-export agent host skill bridging capabilities +export * from "./agent-hosts/index.js";