$ErrorActionPreference = 'Stop' $scriptUrl = 'https://gitlab.com/comluojian/agent-tools/-/raw/main/codex/catalog/latest/Run-CodexModelsUpdate.ps1' $scriptPath = Join-Path ([System.IO.Path]::GetTempPath()) ('CodexModelsUpdate-' + [guid]::NewGuid().ToString('N') + '.ps1') function Invoke-CodexDownload { param( [Parameter(Mandatory = $true)] [string]$Uri, [Parameter(Mandatory = $true)] [string]$OutFile ) $curlCommand = Get-Command curl.exe -ErrorAction SilentlyContinue if ($curlCommand) { Remove-Item -LiteralPath $OutFile -Force -ErrorAction SilentlyContinue $curlArguments = @('--fail', '--silent', '--show-error', '--location', '--retry', '3', '--retry-delay', '1', '--connect-timeout', '15', '--max-time', '120') $curlHelp = (& $curlCommand.Source --help all 2>$null) -join "`n" if ($curlHelp -match '--retry-all-errors') { $curlArguments += '--retry-all-errors' } $curlArguments += @('--output', $OutFile, $Uri) & $curlCommand.Source @curlArguments if ($LASTEXITCODE -eq 0 -and (Test-Path -LiteralPath $OutFile -PathType Leaf) -and (Get-Item -LiteralPath $OutFile).Length -gt 0) { return } } $lastError = $null for ($attempt = 1; $attempt -le 3; $attempt++) { try { Remove-Item -LiteralPath $OutFile -Force -ErrorAction SilentlyContinue Invoke-WebRequest -UseBasicParsing -Uri $Uri -OutFile $OutFile if ((Test-Path -LiteralPath $OutFile -PathType Leaf) -and (Get-Item -LiteralPath $OutFile).Length -gt 0) { return } throw '下载结果为空。' } catch { $lastError = $_.Exception if ($attempt -lt 3) { Start-Sleep -Seconds 1 } } } throw "远程脚本下载失败:$Uri。$($lastError.Message)" } try { Invoke-CodexDownload -Uri $scriptUrl -OutFile $scriptPath & powershell.exe -NoProfile -ExecutionPolicy Bypass -File $scriptPath if ($LASTEXITCODE -ne 0) { throw "模型目录更新脚本执行失败,退出码:$LASTEXITCODE" } } finally { if (Test-Path -LiteralPath $scriptPath -PathType Leaf) { Remove-Item -LiteralPath $scriptPath -Force } }