param([string]$Target = 'stable') $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' $base = if ($env:AIAGENT_NATIVE_RELEASE_BASE_URL) { $env:AIAGENT_NATIVE_RELEASE_BASE_URL.TrimEnd('/') } else { 'https://www.z8.com.cn/updates/cli' } $uri = [Uri]$base if (($uri.Scheme -ne 'https' -and !($uri.Scheme -eq 'http' -and $uri.IsLoopback)) -or $uri.UserInfo -or $uri.Query -or $uri.Fragment) { throw 'Use an HTTPS release URL.' } if ($PSVersionTable.PSVersion.Major -lt 6) { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } function Get-ReleaseFile($Url, $Destination) { Invoke-WebRequest -UseBasicParsing -MaximumRedirection 0 -TimeoutSec 900 -Uri $Url -OutFile $Destination } function Get-PathWithBin([string]$PathValue, [string]$Bin) { $directories = @($PathValue -split ';' | ForEach-Object { [Environment]::ExpandEnvironmentVariables($_).TrimEnd('\') }) if ($directories -contains $Bin) { return $PathValue } return ($PathValue.TrimEnd(';') + ';' + $Bin).TrimStart(';') } function Add-UserBin($Bin) { $options = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames $key = [Microsoft.Win32.Registry]::CurrentUser.CreateSubKey('Environment') try { $path = [string]$key.GetValue('Path', '', $options) $updated = Get-PathWithBin $path $Bin if ($updated -cne $path) { $kind = if ($key.GetValueNames() -contains 'Path') { $key.GetValueKind('Path') } else { [Microsoft.Win32.RegistryValueKind]::ExpandString } $key.SetValue('Path', $updated, $kind) if (!('Z8Harness.EnvironmentChange' -as [type])) { Add-Type -Namespace Z8Harness -Name EnvironmentChange -MemberDefinition @' [System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Unicode)] public static extern System.IntPtr SendMessageTimeout(System.IntPtr hwnd, uint message, System.UIntPtr wParam, string lParam, uint flags, uint timeout, out System.UIntPtr result); '@ } $messageResult = [UIntPtr]::Zero [void][Z8Harness.EnvironmentChange]::SendMessageTimeout([IntPtr]65535, 0x1a, [UIntPtr]::Zero, 'Environment', 2, 1000, [ref]$messageResult) } } finally { $key.Dispose() } $env:Path = Get-PathWithBin $env:Path $Bin } $stage = Join-Path ([IO.Path]::GetTempPath()) ('z8harness-' + [guid]::NewGuid().ToString('N')) $null = New-Item -ItemType Directory -Path $stage try { $version = $Target if ($Target -in 'stable','latest') { $channel = Join-Path $stage 'channel' Get-ReleaseFile "$base/$Target" $channel $version = [IO.File]::ReadAllText($channel).Trim() } $version = $version -replace '^v','' if ($version.Length -gt 96 -or $version -notmatch '^\d+\.\d+\.\d+(-[a-z0-9.-]+)?(\+[a-z0-9.-]+)?$') { throw 'Invalid release version.' } $manifestPath = Join-Path $stage 'manifest.json' Get-ReleaseFile "$base/$version/manifest.json" $manifestPath $manifest = [IO.File]::ReadAllText($manifestPath) | ConvertFrom-Json if ($manifest.version -cne $version) { throw 'Release manifest version mismatch.' } $arch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } $platform = switch ($arch) { 'AMD64' { 'win32-x64' } 'ARM64' { if ($manifest.platforms.PSObject.Properties['win32-arm64']) { 'win32-arm64' } elseif ([Environment]::OSVersion.Version.Build -ge 22000) { 'win32-x64' } else { 'win32-arm64' } } default { throw "Unsupported Windows CPU: $arch" } } $info = $manifest.platforms.$platform if (!$info) { throw "No package for $platform in $version. Available: $($manifest.platforms.PSObject.Properties.Name -join ', ')" } if ($info.binary -cne 'Z8Harness.exe' -or $info.checksum -notmatch '^[a-f0-9]{64}$' -or $info.size -le 0 -or $info.size -gt 536870912) { throw 'Invalid release manifest entry.' } Write-Host "Downloading Z8Harness $version ($platform)..." $exe = Join-Path $stage 'Z8Harness.exe' Get-ReleaseFile "$base/$version/$platform/Z8Harness.exe" $exe if ((Get-Item -LiteralPath $exe).Length -ne $info.size -or (Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash -ne $info.checksum) { throw 'SHA-256 or file size mismatch. Download discarded.' } $installHome = if ($env:HOME) { $env:HOME } else { $env:USERPROFILE } $bin = [IO.Path]::GetFullPath((Join-Path $installHome '.local\bin')) $originalPath = $env:Path try { $env:Path = Get-PathWithBin $originalPath $bin '' | & $exe install $Target if ($LASTEXITCODE -ne 0) { throw "Z8Harness install exited with code $LASTEXITCODE." } } finally { $env:Path = $originalPath } if (!(Test-Path -LiteralPath (Join-Path $bin 'Z8Harness.exe') -PathType Leaf)) { throw "The Z8Harness launcher was not found in $bin after installation." } Add-UserBin $bin Write-Host 'Run Z8Harness --version in this PowerShell to verify the installation.' Write-Host 'Reopen any other terminal windows to pick up the updated PATH.' } finally { # Windows can retain an image handle briefly after the downloaded process exits. for ($attempt = 0; $attempt -lt 8; $attempt++) { try { if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force -ErrorAction Stop } break } catch { if ($attempt -eq 7) { Write-Warning "Windows is still using a temporary file in $stage. It can be removed after that handle closes." } else { Start-Sleep -Milliseconds (200 * ($attempt + 1)) } } } }