Files
MyAiConfiguration/scripts/lib/plugins.ps1
T
Julian LechnerandClaude Opus 5.5 adc4745d10 fix(plugins): detect npm global CLIs in plugin selection
The plugin selection checked CLI extensions such as MCPorter only on PATH,
so it showed them unchecked when npm's global bin directory was missing
from PATH, even though the installer already finds them there. Fall back
to the npm global prefix in both PowerShell and Bash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:23:45 +01:00

219 lines
12 KiB
PowerShell

. (Join-Path $PSScriptRoot '../../shared/hooks/scripts/Resolve-Python.ps1')
function Invoke-PluginCommand {
param(
[Parameter(Mandatory=$true)][string]$Command,
[Parameter(Mandatory=$true)][string[]]$Arguments,
[switch]$DryRun,
[switch]$Summary
)
$display = "$Command $($Arguments -join ' ')"
if ($DryRun) { if (-not $Summary) { Write-Output "DRYRUN $display" }; return }
$resolvedCommand = Get-Command $Command -ErrorAction Stop
if (-not $Summary) {
if ($script:PluginNonInteractive) { $null | & $resolvedCommand @Arguments } else { & $resolvedCommand @Arguments }
if ($LASTEXITCODE -ne 0) { throw "Plugin command failed: $display" }
return
}
$previousPreference = $ErrorActionPreference
try {
$ErrorActionPreference = 'Continue'
$output = if ($script:PluginNonInteractive) { @($null | & $resolvedCommand @Arguments 2>&1) } else { @(& $resolvedCommand @Arguments 2>&1) }
$exitCode = $LASTEXITCODE
} finally { $ErrorActionPreference = $previousPreference }
if ($exitCode -ne 0 -or -not $Summary -or $env:AI_CONFIG_VERBOSE -eq '1') { $output | Write-Output } else { $output | Where-Object { "$_" -match '(?i)warn|error|fail|deprecat' } }
if ($exitCode -ne 0) { throw "Plugin command failed: $display" }
}
function Get-ConfiguredPluginEntries {
param([Parameter(Mandatory=$true)][string]$RepositoryRoot)
$manifest = Join-Path $RepositoryRoot 'adapters/plugins.tsv'
if (-not (Test-Path -LiteralPath $manifest)) { throw "Plugin manifest is missing: $manifest" }
Import-Csv -LiteralPath $manifest -Delimiter ([char]9) | ForEach-Object {
$_.claude_plugin = $_.claude_plugin.Trim()
$_.codex_plugin = $_.codex_plugin.Trim()
$_
}
}
function Get-InstalledPlugins {
param(
[ValidateSet('Claude','Codex')][string]$Client,
[string]$HomePath = [Environment]::GetFolderPath('UserProfile')
)
$command = Get-Command $Client.ToLowerInvariant() -ErrorAction SilentlyContinue
if (-not $command) { return @() }
$environment = @{
HOME = $HomePath
USERPROFILE = $HomePath
CODEX_HOME = (Join-Path $HomePath '.codex')
CLAUDE_CONFIG_DIR = (Join-Path $HomePath '.claude')
}
$previousEnvironment = @{}
foreach ($name in $environment.Keys) { $previousEnvironment[$name] = [Environment]::GetEnvironmentVariable($name, 'Process') }
try {
foreach ($name in $environment.Keys) { [Environment]::SetEnvironmentVariable($name, $environment[$name], 'Process') }
$global:LASTEXITCODE = 0
$output = $null | & $command plugin list --json | Out-String
if ($LASTEXITCODE -ne 0) { throw "$Client plugin listing failed." }
$items = $output | ConvertFrom-Json
if ($Client -eq 'Claude') { return @($items) }
return @($items.installed)
} finally {
foreach ($name in $previousEnvironment.Keys) {
if ($null -eq $previousEnvironment[$name]) { Remove-Item -LiteralPath ("Env:" + $name) -ErrorAction SilentlyContinue }
else { [Environment]::SetEnvironmentVariable($name, $previousEnvironment[$name], 'Process') }
}
}
}
function Test-CliCommand {
param([Parameter(Mandatory=$true)][string]$Name)
if (Get-Command $Name -ErrorAction SilentlyContinue) { return $true }
# npm's global bin directory is often missing from PATH on Windows; match managed-extensions.py find_executable.
if (-not (Get-Command npm -ErrorAction SilentlyContinue)) { return $false }
$prefix = @(& npm prefix --global 2>$null | ForEach-Object { "$_".Trim() } | Where-Object { $_ }) | Select-Object -Last 1
if (-not $prefix) { return $false }
$directory = if ([Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT) { $prefix } else { Join-Path $prefix 'bin' }
return [bool](@('', '.cmd', '.exe', '.ps1') | Where-Object { Test-Path -LiteralPath (Join-Path $directory ($Name + $_)) -PathType Leaf })
}
function Test-InstalledMCPServer {
param([string]$HomePath, [string]$Name, [string]$Url)
$path = Join-Path $HomePath '.mcporter/mcporter.json'
foreach ($candidate in @((Split-Path $path), $path)) {
if ((Test-Path -LiteralPath $candidate) -and ((Get-Item -LiteralPath $candidate -Force).Attributes -band [System.IO.FileAttributes]::ReparsePoint)) { throw 'MCPorter configuration path contains a link.' }
}
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) { return $false }
$config = Get-Content -LiteralPath $path -Raw | ConvertFrom-Json
if ($config -isnot [pscustomobject]) { throw 'Invalid MCPorter server configuration.' }
$servers = $config.PSObject.Properties['mcpServers']
if ($null -eq $servers) { return $false }
if ($servers.Value -isnot [pscustomobject]) { throw 'Invalid MCPorter server configuration.' }
$server = $servers.Value.PSObject.Properties[$Name]
if ($null -eq $server) { return $false }
if ($server.Value -isnot [pscustomobject]) { throw 'Invalid MCPorter server entry.' }
return $server.Value.baseUrl -eq $Url
}
function Read-PluginToggleSelection {
param(
[Parameter(Mandatory=$true)][string[]]$Names,
[Parameter(Mandatory=$true)][bool[]]$Checked
)
$state = @($Checked)
if ([Console]::IsInputRedirected -or $env:CI -eq 'true' -or $env:AI_CONFIG_NO_INTERACTIVE -eq '1') { while($true){ Write-Host 'Select plugins (enter a number to toggle, "done" to confirm):'; for($i=0;$i -lt $Names.Count;$i++){Write-Host (" {0}) [{1}] {2}" -f ($i+1),$(if($state[$i]){'x'}else{' '}),$Names[$i])}; $answer=Read-Host 'Toggle number or "done"'; if([string]::IsNullOrWhiteSpace($answer)-or $answer -eq 'done'){return $state}; $index=0;if([int]::TryParse($answer,[ref]$index)-and $index -ge 1 -and $index -le $Names.Count){$state[$index-1]= -not $state[$index-1]} } }
$index=0; while($true){ Clear-Host; Write-Host 'Select plugins'; for($i=0;$i -lt $Names.Count;$i++){ $focus=if($i -eq $index){'>'}else{' '}; $mark=if($state[$i]){'x'}else{' '}; Write-Host "$focus [$mark] $($Names[$i])" }; Write-Host 'Up/Down Navigate Space Toggle Enter Confirm'; $key=[Console]::ReadKey($true); if($key.Key -eq 'UpArrow'){$index=($index+$Names.Count-1)%$Names.Count}elseif($key.Key -eq 'DownArrow'){$index=($index+1)%$Names.Count}elseif($key.Key -eq 'Spacebar'){$state[$index]= -not $state[$index]}elseif($key.Key -eq 'Enter'){Clear-Host;return $state} }
}
function Select-ConfiguredPlugins {
<#
.SYNOPSIS
Lets the user toggle each configured plugin on or off. Install mode starts
with everything checked; update mode starts checked to match what the
reference client (Claude, or Codex when only Codex is targeted) already
has installed, so unchecking a currently installed plugin uninstalls it.
#>
param(
[Parameter(Mandatory=$true)][string]$RepositoryRoot,
[ValidateSet('Codex','Claude','Both')][Parameter(Mandatory=$true)][string]$Client,
[ValidateSet('Install','Update')][Parameter(Mandatory=$true)][string]$Mode,
[string]$HomePath = [Environment]::GetFolderPath('UserProfile'),
[switch]$DryRun
)
$entries = @(Get-ConfiguredPluginEntries -RepositoryRoot $RepositoryRoot)
$toggleable = @($entries)
$fixed = @($entries | Where-Object { $toggleable -notcontains $_ })
if ($toggleable.Count -eq 0 -or ($DryRun -and $Mode -eq 'Install')) { return @{ Selected = $entries; Deselected = @() } }
$referenceClients = if ($Client -eq 'Both') { @('Claude','Codex') } else { @($Client) }
$installed = @{}
if ($Mode -eq 'Update') { foreach ($referenceClient in $referenceClients) { $installed[$referenceClient] = @(Get-InstalledPlugins -Client $referenceClient -HomePath $HomePath) } }
$checked = @($toggleable | ForEach-Object {
if ($Mode -eq 'Install') { $true; return }
if ($_.codex_method -eq 'cli') { return Test-CliCommand -Name $_.codex_source }
$entry = $_
$found = if ($entry.mcporter_name -and $entry.mcporter_url) {
Test-InstalledMCPServer -HomePath $HomePath -Name $entry.mcporter_name -Url $entry.mcporter_url
} else { $false }
foreach ($referenceClient in $referenceClients) {
$selector = if ($referenceClient -eq 'Claude') { $entry.claude_plugin } else { $entry.codex_plugin }
if ($referenceClient -eq 'Codex' -and $entry.codex_method -ne 'plugin') {
$found = $found -or (Test-Path -LiteralPath (Join-Path $HomePath ('.agents/skills/' + $entry.codex_skill))) -or (Test-Path -LiteralPath (Join-Path $HomePath ('.codex/skills/' + $entry.codex_skill)))
} else {
$idField = if ($referenceClient -eq 'Claude') { 'id' } else { 'pluginId' }
$found = $found -or [bool](@($installed[$referenceClient] | Where-Object { $_.$idField -eq $selector }).Count)
}
}
$found
})
$names = @($toggleable | ForEach-Object { $_.name })
if (-not $DryRun) { $checked = Read-PluginToggleSelection -Names $names -Checked $checked }
$selected = @()
$deselected = @()
for ($i = 0; $i -lt $toggleable.Count; $i++) {
if ($checked[$i]) { $selected += $toggleable[$i] } else { $deselected += $toggleable[$i] }
}
return @{ Selected = ($selected + $fixed); Deselected = $deselected }
}
function Invoke-ManagedExtensions {
param(
[Parameter(Mandatory=$true)][string]$RepositoryRoot,
[Parameter(Mandatory=$true)][string]$Client,
[string]$HomePath = $HOME,
[ValidateSet('sync','install','remove')][string]$Action = 'sync',
[object[]]$Entries,
[switch]$DryRun,
[switch]$Update,
[switch]$Summary
)
$arguments = @((Join-Path $PSScriptRoot 'managed-extensions.py'), $Action, '--home', $HomePath, '--manifest', (Join-Path $RepositoryRoot 'adapters/plugins.tsv'), '--client', $Client.ToLowerInvariant())
if ($DryRun) { $arguments += '--dry-run' }
if ($Update) { $arguments += '--update' }
if ($Summary) { $arguments += '--summary' }
if ($PSBoundParameters.ContainsKey('Entries')) { $arguments += '--selected'; $arguments += @($Entries | ForEach-Object { $_.name }) }
Invoke-Python @arguments
if ($LASTEXITCODE -ne 0) { throw 'Managed extension reconciliation failed.' }
}
function Sync-ConfiguredPlugins {
param(
[Parameter(Mandatory=$true)][string]$RepositoryRoot,
[ValidateSet('Codex','Claude','Both')][Parameter(Mandatory=$true)][string]$Client,
[string]$HomePath = $HOME,
[object[]]$Entries,
[switch]$DryRun,
[switch]$Update,
[switch]$Summary
)
Invoke-ManagedExtensions @PSBoundParameters -Action sync
}
function Install-ConfiguredPlugins {
param(
[Parameter(Mandatory=$true)][string]$RepositoryRoot,
[ValidateSet('Codex','Claude','Both')][Parameter(Mandatory=$true)][string]$Client,
[string]$HomePath = $HOME,
[object[]]$Entries,
[switch]$DryRun,
[switch]$Update,
[switch]$Summary
)
if ($PSBoundParameters.ContainsKey('Entries') -and $Entries.Count -eq 0) { return }
Invoke-ManagedExtensions @PSBoundParameters -Action install
}
function Uninstall-DeselectedPlugins {
param(
[Parameter(Mandatory=$true)][AllowEmptyCollection()][object[]]$Entries,
[ValidateSet('Codex','Claude','Both')][Parameter(Mandatory=$true)][string]$Client,
[string]$RepositoryRoot = (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent),
[string]$HomePath = $HOME,
[switch]$DryRun,
[switch]$Summary
)
if ($Entries.Count -eq 0) { return }
Invoke-ManagedExtensions -RepositoryRoot $RepositoryRoot -Client $Client -HomePath $HomePath -Entries $Entries -Action remove -DryRun:$DryRun -Summary:$Summary
}