|
| 1 | +import { access } from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import { isLockInSync } from '../config/compareSkillsLock' |
| 4 | +import { readSkillsLock } from '../config/readSkillsLock' |
| 5 | +import { readSkillsManifest } from '../config/readSkillsManifest' |
| 6 | +import { |
| 7 | + getBundledSelfSkillSpecifier, |
| 8 | + SELF_SKILL_NAME, |
| 9 | + shouldInjectBundledSelfSkill, |
| 10 | +} from '../config/skillsManifest' |
| 11 | +import { resolveLockEntry, syncSkillsLock } from '../config/syncSkillsLock' |
| 12 | +import type { InstallProgressListener, SkillsLock, SkillsManifest } from '../config/types' |
| 13 | +import { writeSkillsLock } from '../config/writeSkillsLock' |
| 14 | +import { fetchSkill } from '../fetchers' |
| 15 | +import { applySkillPatch } from '../patches/skillPatch' |
| 16 | +import { createFileSystemCache } from '../pipeline/cache' |
| 17 | +import { sha256 } from '../utils/hash' |
| 18 | +import { readInstallState, writeInstallState } from './installState' |
| 19 | +import { linkSkill } from './links' |
| 20 | +import { pruneManagedSkills } from './pruneManagedSkills' |
| 21 | + |
| 22 | +async function areManagedSkillsInstalled( |
| 23 | + rootDir: string, |
| 24 | + installDir: string, |
| 25 | + skillNames: string[], |
| 26 | +): Promise<boolean> { |
| 27 | + for (const skillName of skillNames) { |
| 28 | + try { |
| 29 | + await access(path.join(rootDir, installDir, skillName, 'SKILL.md')) |
| 30 | + } catch { |
| 31 | + return false |
| 32 | + } |
| 33 | + } |
| 34 | + return true |
| 35 | +} |
| 36 | + |
| 37 | +export async function withBundledSelfSkillLock( |
| 38 | + rootDir: string, |
| 39 | + manifest: SkillsManifest, |
| 40 | + lockfile: SkillsLock, |
| 41 | +): Promise<SkillsLock> { |
| 42 | + if (!shouldInjectBundledSelfSkill(manifest) || lockfile.skills[SELF_SKILL_NAME]) { |
| 43 | + return lockfile |
| 44 | + } |
| 45 | + |
| 46 | + const { entry } = await resolveLockEntry(rootDir, getBundledSelfSkillSpecifier(), SELF_SKILL_NAME) |
| 47 | + |
| 48 | + return { |
| 49 | + ...lockfile, |
| 50 | + skills: { |
| 51 | + ...lockfile.skills, |
| 52 | + [SELF_SKILL_NAME]: entry, |
| 53 | + }, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +export async function fetchSkillsFromLock( |
| 58 | + rootDir: string, |
| 59 | + manifest: SkillsManifest, |
| 60 | + lockfile: SkillsLock, |
| 61 | + options?: { |
| 62 | + onProgress?: InstallProgressListener |
| 63 | + }, |
| 64 | +) { |
| 65 | + const installDir = manifest.installDir ?? '.agents/skills' |
| 66 | + const linkTargets = manifest.linkTargets ?? [] |
| 67 | + |
| 68 | + await pruneManagedSkills(rootDir, installDir, linkTargets, Object.keys(lockfile.skills)) |
| 69 | + |
| 70 | + const lockDigest = sha256(JSON.stringify(lockfile)) |
| 71 | + const state = await readInstallState(rootDir, installDir) |
| 72 | + if ( |
| 73 | + state?.lockDigest === lockDigest && |
| 74 | + (await areManagedSkillsInstalled(rootDir, installDir, Object.keys(lockfile.skills))) |
| 75 | + ) { |
| 76 | + return { status: 'skipped', reason: 'up-to-date' } as const |
| 77 | + } |
| 78 | + |
| 79 | + const cache = createFileSystemCache(rootDir) |
| 80 | + |
| 81 | + try { |
| 82 | + for (const [skillName, entry] of Object.entries(lockfile.skills)) { |
| 83 | + const { installPath } = await fetchSkill(rootDir, skillName, entry, installDir, cache) |
| 84 | + if (entry.patch) { |
| 85 | + await applySkillPatch(installPath, path.resolve(rootDir, entry.patch.path)) |
| 86 | + } |
| 87 | + options?.onProgress?.({ type: 'added', skillName }) |
| 88 | + } |
| 89 | + |
| 90 | + await writeInstallState(rootDir, installDir, { |
| 91 | + lockDigest, |
| 92 | + installDir, |
| 93 | + linkTargets, |
| 94 | + installerVersion: '0.1.0', |
| 95 | + installedAt: new Date().toISOString(), |
| 96 | + }) |
| 97 | + } catch (error) { |
| 98 | + throw error |
| 99 | + } |
| 100 | + |
| 101 | + return { status: 'fetched', fetched: Object.keys(lockfile.skills) } as const |
| 102 | +} |
| 103 | + |
| 104 | +export async function linkSkillsFromLock( |
| 105 | + rootDir: string, |
| 106 | + manifest: SkillsManifest, |
| 107 | + lockfile: SkillsLock, |
| 108 | + options?: { |
| 109 | + onProgress?: InstallProgressListener |
| 110 | + }, |
| 111 | +) { |
| 112 | + const installDir = manifest.installDir ?? '.agents/skills' |
| 113 | + const linkTargets = manifest.linkTargets ?? [] |
| 114 | + |
| 115 | + for (const skillName of Object.keys(lockfile.skills)) { |
| 116 | + for (const linkTarget of linkTargets) { |
| 117 | + await linkSkill(rootDir, installDir, linkTarget, skillName) |
| 118 | + } |
| 119 | + options?.onProgress?.({ type: 'installed', skillName }) |
| 120 | + } |
| 121 | + |
| 122 | + return { status: 'linked', linked: Object.keys(lockfile.skills) } as const |
| 123 | +} |
| 124 | + |
| 125 | +export async function installSkills( |
| 126 | + rootDir: string, |
| 127 | + options?: { frozenLockfile?: boolean; onProgress?: InstallProgressListener }, |
| 128 | +) { |
| 129 | + const manifest = await readSkillsManifest(rootDir) |
| 130 | + if (!manifest) { |
| 131 | + return { status: 'skipped', reason: 'manifest-missing' } as const |
| 132 | + } |
| 133 | + |
| 134 | + const currentLock = await readSkillsLock(rootDir) |
| 135 | + |
| 136 | + let lockfile: SkillsLock |
| 137 | + |
| 138 | + if (options?.frozenLockfile) { |
| 139 | + if (!currentLock) { |
| 140 | + throw new Error('Lockfile is required in frozen mode but none was found') |
| 141 | + } |
| 142 | + if (!(await isLockInSync(rootDir, manifest, currentLock))) { |
| 143 | + throw new Error( |
| 144 | + 'Lockfile is out of sync with manifest. Run install without --frozen-lockfile to update.', |
| 145 | + ) |
| 146 | + } |
| 147 | + lockfile = currentLock |
| 148 | + for (const skillName of Object.keys(lockfile.skills)) { |
| 149 | + options?.onProgress?.({ type: 'resolved', skillName }) |
| 150 | + } |
| 151 | + } else { |
| 152 | + lockfile = await syncSkillsLock(rootDir, manifest, currentLock, { |
| 153 | + onProgress: options?.onProgress, |
| 154 | + }) |
| 155 | + } |
| 156 | + |
| 157 | + const runtimeLock = await withBundledSelfSkillLock(rootDir, manifest, lockfile) |
| 158 | + |
| 159 | + await fetchSkillsFromLock(rootDir, manifest, runtimeLock, { |
| 160 | + onProgress: options?.onProgress, |
| 161 | + }) |
| 162 | + await linkSkillsFromLock(rootDir, manifest, runtimeLock, { |
| 163 | + onProgress: options?.onProgress, |
| 164 | + }) |
| 165 | + |
| 166 | + if (!options?.frozenLockfile) { |
| 167 | + await writeSkillsLock(rootDir, lockfile) |
| 168 | + } |
| 169 | + |
| 170 | + return { status: 'installed', installed: Object.keys(runtimeLock.skills) } as const |
| 171 | +} |
0 commit comments