#requires -Version 5.1 [CmdletBinding()] param( [Parameter(Mandatory = $true)] [ValidateSet('ClaudeCode', 'DeepSeekHarness', 'Check')] [string]$Client, [Parameter(Mandatory = $true)] [string]$BaseUrl ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' $gateway = $null if (-not [Uri]::TryCreate($BaseUrl, [UriKind]::Absolute, [ref]$gateway) -or $gateway.Scheme -notin @('http', 'https') -or $gateway.UserInfo -or $gateway.Query -or $gateway.Fragment -or $gateway.AbsolutePath.TrimEnd('/') -notin @('', '/v1') -or $gateway.Host -notmatch '^[0-9A-Za-z.:[\]-]+$') { throw 'Укажите адрес Амипорта, например https://gateway.example.com.' } $localHost = $gateway.IsLoopback -or $gateway.Host.EndsWith('.localhost', [StringComparison]::OrdinalIgnoreCase) if ($gateway.Scheme -eq 'http' -and -not $localHost) { throw 'Для удалённого адреса нужен HTTPS.' } $origin = $gateway.GetLeftPart([UriPartial]::Authority) if ($gateway.Scheme -eq 'http' -and $gateway.Host.EndsWith('.localhost', [StringComparison]::OrdinalIgnoreCase)) { $origin = 'http://127.0.0.1:' + $gateway.Port } function Get-GatewayData { param([string]$Path, [hashtable]$Headers = @{}) try { Invoke-RestMethod -Uri ($origin + $Path) -Headers $Headers -TimeoutSec 15 -MaximumRedirection 0 -UseBasicParsing } catch { throw 'Амипорт не ответил или отклонил ключ. Проверьте адрес, HTTPS и ключ в панели.' } } if ($Client -ne 'Check') { $runner = if ($Client -eq 'ClaudeCode') { 'claude' } else { 'dsh' } $command = Get-Command -Name $runner -CommandType Application,ExternalScript -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $command) { $instructions = if ($Client -eq 'ClaudeCode') { 'https://code.claude.com/docs/en/overview' } else { 'https://www.deepseek.com/en/harness/' } throw "Команда $runner не найдена. Установите клиент и добавьте его в PATH: $instructions" } $route = Get-GatewayData '/agent-info' if ($route.model -ne 'deepseek-flash' -or -not $route.configured) { throw 'Маршрут для клиента ещё не открыт на сервере. Проверить ключ можно с -Client Check.' } } $key = [Environment]::GetEnvironmentVariable('AMIPORT_API_KEY', 'Process') if (-not $key) { $hidden = Read-Host 'Ключ Амипорта' -AsSecureString $pointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($hidden) try { $key = [Runtime.InteropServices.Marshal]::PtrToStringBSTR($pointer) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($pointer) $hidden.Dispose() } } if (-not $key -or $key -notmatch '^sk-ami-[A-Za-z0-9_-]{10,100}$') { throw 'Нужен пользовательский ключ Амипорта, начинающийся на sk-ami-.' } $catalog = Get-GatewayData '/v1/models' @{ Authorization = 'Bearer ' + $key } Write-Host 'Ключ принят. Проверка не расходует баланс.' if ($Client -eq 'Check') { $catalog.data | Select-Object -ExpandProperty id return } $limit = [int]$route.max_tokens if ($limit -lt 1 -or $limit -gt 32768) { throw 'Некорректный лимит вывода, полученный от шлюза.' } $profileRoot = Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'Amiport' $saved = @{} function Set-ClientVariable { param([string]$Name, [AllowNull()][string]$Value) if (-not $saved.ContainsKey($Name)) { $saved[$Name] = [Environment]::GetEnvironmentVariable($Name, 'Process') } [Environment]::SetEnvironmentVariable($Name, $Value, 'Process') } $overlay = $null try { if ($Client -eq 'ClaudeCode') { $clientHome = Join-Path $profileRoot 'ClaudeCode' New-Item -Path $clientHome -ItemType Directory -Force | Out-Null Set-ClientVariable 'CLAUDE_CONFIG_DIR' $clientHome Set-ClientVariable 'ANTHROPIC_BASE_URL' $origin Set-ClientVariable 'ANTHROPIC_API_KEY' $null Set-ClientVariable 'ANTHROPIC_CUSTOM_HEADERS' $null Set-ClientVariable 'ANTHROPIC_BETAS' $null Set-ClientVariable 'CLAUDE_CODE_EXTRA_BODY' $null foreach ($externalProvider in @('BEDROCK', 'VERTEX', 'FOUNDRY')) { Set-ClientVariable ("CLAUDE_CODE_USE_" + $externalProvider) $null } Set-ClientVariable 'ANTHROPIC_AUTH_TOKEN' $key Set-ClientVariable 'CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC' '1' Set-ClientVariable 'CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS' '1' Set-ClientVariable 'CLAUDE_CODE_MAX_CONTEXT_TOKENS' '100000' Set-ClientVariable 'CLAUDE_CODE_MAX_OUTPUT_TOKENS' ([string]$limit) Set-ClientVariable 'CLAUDE_CODE_SUBAGENT_MODEL' $route.model foreach ($family in @('FABLE', 'OPUS', 'SONNET', 'HAIKU')) { Set-ClientVariable ("ANTHROPIC_DEFAULT_" + $family + "_MODEL") $route.model } & $command.Source --model $route.model --setting-sources user } else { $clientHome = Join-Path $profileRoot 'DeepSeekHarness' New-Item -Path $clientHome -ItemType Directory -Force | Out-Null Set-ClientVariable 'DSH_HOME' $clientHome Set-ClientVariable 'DEEPSEEK_BASE_URL' $origin Set-ClientVariable 'DEEPSEEK_API_KEY' $null Set-ClientVariable 'AMIPORT_DSH_KEY' $key Set-ClientVariable 'DSH_TELEMETRY_MODE' 'DISABLED' $overlay = New-TemporaryFile $patch = @" - id: session-log-deepseek config: enabled: false - id: plugin-package-inventory-deepseek config: enabled: false - id: session-title-llm disabled: true - id: llm-deepseek config: baseURL: '$origin' apiKeyEnv: AMIPORT_DSH_KEY maxTokens: $limit "@ Set-Content -LiteralPath $overlay.FullName -Value $patch -Encoding utf8 & $command.Source --profile web --patch $overlay.FullName --host 127.0.0.1 } } finally { foreach ($name in $saved.Keys) { [Environment]::SetEnvironmentVariable($name, $saved[$name], 'Process') } if ($overlay) { Remove-Item -LiteralPath $overlay.FullName } }