$ErrorActionPreference = 'Stop' $RubyExitCode = 0 $RubyTemporaryInstaller = $null $RubySessionKey = $null $RubySavedEnvironment = @{} foreach ($Name in @('RUBY_API_KEY', 'RUBY_CONFIGURE_ONLY', 'RUBY_DESKTOP')) { $RubySavedEnvironment[$Name] = [Environment]::GetEnvironmentVariable($Name, 'Process') } try { Write-Host '' Write-Host ' RUBYCLI | Agentes de codigo' -ForegroundColor Magenta Write-Host ' [1] Claude Code: configurar e iniciar, com backup das preferencias.' Write-Host ' [2] Codex: iniciar uma sessao Ruby, preservando seu config.toml.' $RubyClient = $env:RUBY_CLIENT if (-not $RubyClient) { $RubyClient = Read-Host ' Escolha 1 ou 2' } switch ($RubyClient) { { $_ -in @('1', 'claude') } { $RubyClient = 'claude' } { $_ -in @('2', 'codex') } { $RubyClient = 'codex' } default { throw 'Escolha Claude Code (1) ou Codex (2).' } } if (-not (Get-Command $RubyClient -ErrorAction SilentlyContinue)) { throw "$RubyClient nao foi encontrado. Instale o cliente e abra um terminal novo." } if ($RubyClient -eq 'claude') { Write-Host ' Sua chave sera salva na configuracao local do Claude Code pelo instalador RubyCLI.' -ForegroundColor DarkGray $RubyInstaller = Join-Path (Split-Path -Parent $PSScriptRoot) 'install.ps1' if (-not (Test-Path -LiteralPath $RubyInstaller)) { $RubyTemporaryInstaller = Join-Path ([IO.Path]::GetTempPath()) ('rubycli-setup-' + [Guid]::NewGuid().ToString('N') + '.ps1') try { Invoke-WebRequest -UseBasicParsing -Uri 'https://app.rubycli.cloud/install.ps1' -OutFile $RubyTemporaryInstaller -MaximumRedirection 0 -TimeoutSec 60 } catch { throw 'Nao foi possivel baixar o instalador RubyCLI. Tente novamente em instantes.' } $RubyInstaller = $RubyTemporaryInstaller } $env:RUBY_CONFIGURE_ONLY = '1' $env:RUBY_DESKTOP = '0' & $RubyInstaller -ConfigureOnly & claude if ($null -ne $LASTEXITCODE) { $RubyExitCode = $LASTEXITCODE } } else { $RubySessionKey = $env:RUBY_API_KEY if ([string]::IsNullOrWhiteSpace($RubySessionKey)) { $RubySecureKey = Read-Host ' Chave RubyCLI (entrada oculta)' -AsSecureString $RubySecurePointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($RubySecureKey) try { $RubySessionKey = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($RubySecurePointer) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($RubySecurePointer); $RubySecureKey.Dispose() } } $RubySessionKey = $RubySessionKey.Trim() if ($RubySessionKey -notmatch '^sk-ruby-[A-Za-z0-9_-]{20,200}$') { throw 'Informe a chave RubyCLI completa, que comeca com sk-ruby-.' } try { $RubyCatalogResponse = Invoke-WebRequest -UseBasicParsing -Uri 'https://app.rubycli.cloud/v1/models' -Headers @{ Authorization = "Bearer $RubySessionKey"; Accept = 'application/json' } -MaximumRedirection 0 -TimeoutSec 30 } catch { throw 'Nao foi possivel validar sua chave. Confira a conexao e sua chave no painel RubyCLI.' } try { $RubyCatalogText = if ($RubyCatalogResponse.Content -is [byte[]]) { [Text.Encoding]::UTF8.GetString($RubyCatalogResponse.Content) } else { [string]$RubyCatalogResponse.Content } $RubyCatalog = $RubyCatalogText | ConvertFrom-Json if ($RubyCatalog.data -isnot [System.Array]) { throw 'Invalid catalog' } $RubyModels = @($RubyCatalog.data | ForEach-Object { if ($_.id -isnot [string] -or $_.id -notmatch '^[A-Za-z0-9][A-Za-z0-9._:/-]{0,254}$') { throw 'Invalid model' } $_.id } | Select-Object -Unique) } catch { throw 'A RubyCLI retornou um catalogo inesperado. Nenhuma configuracao foi alterada.' } if ($RubyModels.Count -eq 0) { throw 'Nenhum modelo esta liberado para esta chave. Confira seu saldo ou plano no painel.' } $RubySelectedModel = $env:RUBY_MODEL if (-not $RubySelectedModel) { for ($Index = 0; $Index -lt $RubyModels.Count; $Index++) { Write-Host (" {0,3} {1}" -f ($Index + 1), $RubyModels[$Index]) } $RubySelection = Read-Host ' Numero do modelo [1]' $RubyModelIndex = 1 if ($RubySelection -and (-not [int]::TryParse($RubySelection, [ref]$RubyModelIndex) -or $RubyModelIndex -lt 1 -or $RubyModelIndex -gt $RubyModels.Count)) { throw 'Escolha um numero da lista de modelos.' } $RubySelectedModel = $RubyModels[$RubyModelIndex - 1] } if ($RubySelectedModel -notin $RubyModels) { throw 'RUBY_MODEL precisa ser um ID disponivel no catalogo desta chave.' } $env:RUBY_API_KEY = $RubySessionKey $RubyArguments = @( '-c', 'model_provider="ruby"', '-c', ('model="' + $RubySelectedModel + '"'), '-c', 'model_providers.ruby.name="RubyCLI"', '-c', 'model_providers.ruby.base_url="https://app.rubycli.cloud/v1"', '-c', 'model_providers.ruby.env_key="RUBY_API_KEY"', '-c', 'model_providers.ruby.wire_api="responses"', '-c', 'model_providers.ruby.requires_openai_auth=false', '-c', 'model_providers.ruby.supports_websockets=false', '-c', 'model_supports_reasoning_summaries=false', '-c', 'web_search="disabled"' ) Write-Host " Iniciando Codex com $RubySelectedModel. Seu config.toml foi preservado." -ForegroundColor Green & codex @RubyArguments if ($null -ne $LASTEXITCODE) { $RubyExitCode = $LASTEXITCODE } } } catch { Write-Host (" RubyCLI: {0}" -f $_.Exception.Message) -ForegroundColor Red $RubyExitCode = 1 } finally { $RubySessionKey = $null foreach ($Name in $RubySavedEnvironment.Keys) { [Environment]::SetEnvironmentVariable($Name, $RubySavedEnvironment[$Name], 'Process') } if ($RubyTemporaryInstaller -and (Test-Path -LiteralPath $RubyTemporaryInstaller)) { Remove-Item -LiteralPath $RubyTemporaryInstaller -Force } } exit $RubyExitCode