|
| 1 | +/** |
| 2 | + * Shared Agents adapter |
| 3 | + */ |
| 4 | + |
| 5 | +import { unlinkSync } from "node:fs"; |
| 6 | +import { join } from "node:path"; |
| 7 | +import { |
| 8 | + copyDir, |
| 9 | + createSymlink, |
| 10 | + ensureDir, |
| 11 | + exists, |
| 12 | + getSymlinkTarget, |
| 13 | + isSymlink, |
| 14 | + removeDir, |
| 15 | +} from "../utils/fs"; |
| 16 | +import { contractHome } from "../utils/paths"; |
| 17 | +import type { |
| 18 | + AgentAdapter, |
| 19 | + ExportResult, |
| 20 | + ImportResult, |
| 21 | + Platform, |
| 22 | +} from "./types"; |
| 23 | + |
| 24 | +export class AgentsAdapter implements AgentAdapter { |
| 25 | + readonly id = "agents"; |
| 26 | + readonly name = "Shared Agents"; |
| 27 | + readonly version = "1.0.0"; |
| 28 | + readonly syncStrategy = { |
| 29 | + import: "copy" as const, |
| 30 | + export: "symlink" as const, |
| 31 | + }; |
| 32 | + |
| 33 | + getConfigPath(_platform: Platform): string { |
| 34 | + return join(process.env.HOME || "", ".agents"); |
| 35 | + } |
| 36 | + |
| 37 | + getRepoPath(repoRoot: string): string { |
| 38 | + return join(repoRoot, ".agents"); |
| 39 | + } |
| 40 | + |
| 41 | + getSkillsPath(_platform: Platform): string { |
| 42 | + return join(this.getConfigPath(_platform), "skills"); |
| 43 | + } |
| 44 | + |
| 45 | + isInstalled(platform: Platform): boolean { |
| 46 | + return exists(this.getConfigPath(platform)); |
| 47 | + } |
| 48 | + |
| 49 | + detect(): boolean { |
| 50 | + const platform = |
| 51 | + process.platform === "darwin" |
| 52 | + ? "macos" |
| 53 | + : process.platform === "win32" |
| 54 | + ? "windows" |
| 55 | + : "linux"; |
| 56 | + return this.isInstalled(platform); |
| 57 | + } |
| 58 | + |
| 59 | + isLinked(systemPath: string, repoPath: string): boolean { |
| 60 | + if (!exists(systemPath) || !exists(repoPath) || !isSymlink(systemPath)) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + return getSymlinkTarget(systemPath) === repoPath; |
| 64 | + } |
| 65 | + |
| 66 | + async import(systemPath: string, repoPath: string): Promise<ImportResult> { |
| 67 | + if (!exists(systemPath)) { |
| 68 | + return { |
| 69 | + success: false, |
| 70 | + message: "Shared agents config not found on system", |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + if (isSymlink(systemPath)) { |
| 75 | + return { |
| 76 | + success: true, |
| 77 | + message: "Already linked to repo - no import needed", |
| 78 | + }; |
| 79 | + } |
| 80 | + |
| 81 | + if (exists(repoPath)) { |
| 82 | + return { |
| 83 | + success: true, |
| 84 | + message: "Configs already in repo - no import needed", |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + ensureDir(repoPath); |
| 89 | + copyDir(systemPath, repoPath); |
| 90 | + |
| 91 | + return { |
| 92 | + success: true, |
| 93 | + message: "Imported shared agents configs to repo", |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + async export(repoPath: string, systemPath: string): Promise<ExportResult> { |
| 98 | + if (!exists(repoPath)) { |
| 99 | + return { |
| 100 | + success: false, |
| 101 | + message: "Shared agents configs not found in repo", |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + ensureDir(join(repoPath, "skills")); |
| 106 | + |
| 107 | + if (isSymlink(systemPath)) { |
| 108 | + const target = getSymlinkTarget(systemPath); |
| 109 | + if (target === repoPath) { |
| 110 | + return { |
| 111 | + success: true, |
| 112 | + message: "Already linked to repo - no export needed", |
| 113 | + linkedTo: repoPath, |
| 114 | + }; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (exists(systemPath)) { |
| 119 | + if (isSymlink(systemPath)) { |
| 120 | + unlinkSync(systemPath); |
| 121 | + } else { |
| 122 | + const backupPath = `${systemPath}.backup`; |
| 123 | + if (exists(backupPath)) { |
| 124 | + if (isSymlink(backupPath)) { |
| 125 | + unlinkSync(backupPath); |
| 126 | + } else { |
| 127 | + removeDir(backupPath); |
| 128 | + } |
| 129 | + } |
| 130 | + require("node:fs").renameSync(systemPath, backupPath); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + createSymlink(repoPath, systemPath); |
| 135 | + |
| 136 | + return { |
| 137 | + success: true, |
| 138 | + message: `Linked shared agents configs to ${contractHome(systemPath)}`, |
| 139 | + linkedTo: repoPath, |
| 140 | + }; |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +export const agentsAdapter = new AgentsAdapter(); |
0 commit comments