chunhualin1
2026-07-24 dfcc38b940873e2d5baa99b4c153e505388bf2ad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
[CmdletBinding()]
param(
    [Parameter()]
    [string]$CodexHome
)
 
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
 
if ([string]::IsNullOrWhiteSpace($CodexHome)) {
    $CodexHome = if ([string]::IsNullOrWhiteSpace($env:CODEX_HOME)) {
        Join-Path ([Environment]::GetFolderPath('UserProfile')) '.codex'
    }
    else {
        $env:CODEX_HOME
    }
}
 
$suiteRoot = [IO.Path]::GetFullPath($PSScriptRoot)
$sourceSkillsRoot = Join-Path $suiteRoot 'skills'
$sourceSharedRoot = Join-Path $suiteRoot 'shared'
$sourceBundle = Join-Path $suiteRoot 'bundle.yaml'
$targetCodexRoot = [IO.Path]::GetFullPath($CodexHome)
$targetSkillsRoot = [IO.Path]::GetFullPath((Join-Path $targetCodexRoot 'skills'))
$skillNames = @(
    'ai-agent-system-bootstrap'
    'ai-agent-system-inspect'
    'ai-agent-system-design'
    'ai-agent-system-render'
    'ai-agent-system-validate'
)
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
 
foreach ($requiredPath in @($sourceSkillsRoot, $sourceSharedRoot, $sourceBundle)) {
    if (-not (Test-Path -LiteralPath $requiredPath)) {
        throw "Required suite path is missing: $requiredPath"
    }
}
 
New-Item -ItemType Directory -Path $targetSkillsRoot -Force | Out-Null
$resolvedSkillsRoot = (Get-Item -LiteralPath $targetSkillsRoot).FullName.TrimEnd('\', '/')
 
foreach ($skillName in $skillNames) {
    $sourceSkill = Join-Path $sourceSkillsRoot $skillName
    $sourceSkillFile = Join-Path $sourceSkill 'SKILL.md'
    if (-not (Test-Path -LiteralPath $sourceSkillFile -PathType Leaf)) {
        throw "Skill source is incomplete: $sourceSkillFile"
    }
 
    $targetSkill = [IO.Path]::GetFullPath((Join-Path $resolvedSkillsRoot $skillName))
    if ([IO.Path]::GetDirectoryName($targetSkill).TrimEnd('\', '/') -ne $resolvedSkillsRoot) {
        throw "Refusing to install outside the global Skill directory: $targetSkill"
    }
 
    if (Test-Path -LiteralPath $targetSkill) {
        $existing = Get-Item -LiteralPath $targetSkill -Force
        if (-not $existing.PSIsContainer) {
            throw "Skill target exists and is not a directory: $targetSkill"
        }
        if (($existing.Attributes -band [IO.FileAttributes]::ReparsePoint) -ne 0) {
            throw "Skill target is a symlink or junction; remove it manually first: $targetSkill"
        }
        Remove-Item -LiteralPath $targetSkill -Recurse -Force
    }
 
    New-Item -ItemType Directory -Path $targetSkill | Out-Null
    Copy-Item -LiteralPath (Join-Path $sourceSkill 'SKILL.md') -Destination $targetSkill
    if (Test-Path -LiteralPath (Join-Path $sourceSkill 'agents')) {
        Copy-Item -LiteralPath (Join-Path $sourceSkill 'agents') -Destination $targetSkill -Recurse
    }
    Copy-Item -LiteralPath $sourceSharedRoot -Destination $targetSkill -Recurse
    Copy-Item -LiteralPath $sourceBundle -Destination (Join-Path $targetSkill 'bundle.yaml')
 
    $installedSkillFile = Join-Path $targetSkill 'SKILL.md'
    $skillText = [IO.File]::ReadAllText($installedSkillFile)
    $skillText = $skillText.Replace('../../shared/', 'shared/').Replace('../../bundle.yaml', 'bundle.yaml')
    [IO.File]::WriteAllText($installedSkillFile, $skillText, $utf8NoBom)
 
    if ($skillText -notmatch "(?m)^name:\s*$([regex]::Escape($skillName))\s*$") {
        throw "Installed Skill frontmatter name does not match its directory: $installedSkillFile"
    }
 
    $localLinks = [regex]::Matches($skillText, '\]\((?!https?://|#)([^)]+)\)')
    foreach ($match in $localLinks) {
        $relativePath = $match.Groups[1].Value.Replace('/', [IO.Path]::DirectorySeparatorChar)
        $linkedPath = [IO.Path]::GetFullPath((Join-Path $targetSkill $relativePath))
        if (-not (Test-Path -LiteralPath $linkedPath)) {
            throw "Installed Skill contains a broken local reference: $($match.Groups[1].Value)"
        }
    }
 
    Write-Host "Installed $skillName -> $targetSkill"
}
 
Write-Host "Installed $($skillNames.Count) Skills into $resolvedSkillsRoot"
Write-Host 'The updated Skills are available to new Codex turns.'