# lib/providers.ps1 - every gateway provider, read from providers.json, and lookups into them. # # Part of claude-mode.ps1, which dot-sources it into its own script scope after # the settings at its top. Not meant to run on its own. ASCII only: Windows # PowerShell 5.1 reads a .ps1 without a BOM as ANSI. $PSScriptRoot here would be # lib\, so paths beside the main script go through $script:Here. # --------------------------------------------------------------------------- # Providers # # Every gateway provider is an entry in providers.json, next to this script and # shared with the POSIX build: endpoint, auth, how its model list is fetched, # which doctor checks apply. This script only knows the *kinds* of behaviour # (Get-ProviderCatalogue, Test-PresetCatalogue) and picks one by name from the # entry, so a provider that reuses them needs no change here. anthropic is # built in: it is the native login, not a gateway. # --------------------------------------------------------------------------- $script:ProvidersPath = Join-Path $script:Here 'providers.json' $script:Providers = @() try { $__pj = Get-Content -LiteralPath $script:ProvidersPath -Raw -Encoding UTF8 | ConvertFrom-Json $script:Providers = @($__pj.providers | Where-Object { $_.id }) } catch { } if ($script:Providers.Count -eq 0) { Write-Host " FAIL providers.json missing or unreadable at $($script:ProvidersPath) - re-run install.ps1" -ForegroundColor Red exit 1 } $script:Modes = @('anthropic') + @($script:Providers | ForEach-Object { [string]$_.id }) $script:ModeLabel = @{ 'anthropic' = 'Anthropic - your subscription login, no gateway' } # `claude-mode ` with no preset named uses this one. Deliberately a # fixed choice rather than "most recently used", so the command is predictable. $script:ProviderDefaultPreset = @{} foreach ($__p in $script:Providers) { $script:ModeLabel[[string]$__p.id] = [string]$__p.label $script:ProviderDefaultPreset[[string]$__p.id] = $(if ($__p.defaultPreset) { [string]$__p.defaultPreset } else { [string]$__p.id }) } function Get-Provider { param([string] $Id) return ($script:Providers | Where-Object { $_.id -eq $Id } | Select-Object -First 1) } # An id or an alias (z.ai, z-ai) to the provider id; $null if neither. function Resolve-ProviderId { param([string] $Word) $w = ([string]$Word).ToLower() foreach ($p in $script:Providers) { if ([string]$p.id -eq $w -or (@($p.aliases) -contains $w)) { return [string]$p.id } } return $null } function Test-ProviderDoctor { param([string] $Id, [string] $Check) $p = Get-Provider $Id return [bool]($p -and (@($p.doctor) -contains $Check)) }