$ErrorActionPreference = 'Stop' $ProxyHostName = 'hproxy.local' $ProxyPort = 12334 $HttpProxy = "http://${ProxyHostName}:${ProxyPort}" $ProxyBypassList = "${ProxyHostName};localhost;127.0.0.1;[::1];*.concept-software.ru" function Get-CodexApplication { $Package = Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction SilentlyContinue | Select-Object -First 1 if ($null -eq $Package) { throw 'Codex Windows App is not installed. Expected AppX package: OpenAI.Codex.' } $Manifest = Get-AppxPackageManifest $Package $Application = @($Manifest.Package.Applications.Application) | Select-Object -First 1 if ($null -eq $Application -or [string]::IsNullOrWhiteSpace($Application.Executable)) { throw 'Codex Windows App manifest does not contain an executable entry.' } $Executable = Join-Path $Package.InstallLocation ([string]$Application.Executable) if (-not (Test-Path -LiteralPath $Executable)) { throw "Codex Windows App executable was not found: $Executable" } [PSCustomObject]@{ AppUserModelId = "$($Package.PackageFamilyName)!$([string]$Application.Id)" Executable = $Executable } } function Assert-CodexNotRunning { param( [Parameter(Mandatory)] [string]$Executable ) $RunningProcess = Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object { $_.ExecutablePath -eq $Executable } | Select-Object -First 1 if ($null -ne $RunningProcess) { throw 'Codex уже запущен. Полностью закройте приложение и повторно откройте ярлык Codex Proxy.' } } function Start-CodexApplication { param( [Parameter(Mandatory)] [string]$AppUserModelId, [Parameter(Mandatory)] [string]$Arguments ) if (-not ('CodexProxyLauncher.ApplicationActivationManager' -as [type])) { Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; namespace CodexProxyLauncher { [Flags] public enum ActivateOptions : uint { None = 0 } [ComImport] [Guid("2e941141-7f97-4756-ba1d-9decde894a3d")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IApplicationActivationManager { [PreserveSig] int ActivateApplication( [MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, [MarshalAs(UnmanagedType.LPWStr)] string arguments, ActivateOptions options, out uint processId); } [ComImport] [Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")] public class ApplicationActivationManager { } public static class ApplicationActivator { public static uint Activate(string appUserModelId, string arguments) { IApplicationActivationManager manager = (IApplicationActivationManager)new ApplicationActivationManager(); uint processId; int hResult = manager.ActivateApplication( appUserModelId, arguments, ActivateOptions.None, out processId); Marshal.ThrowExceptionForHR(hResult); return processId; } } } '@ } [CodexProxyLauncher.ApplicationActivator]::Activate( $AppUserModelId, $Arguments ) } $CodexApplication = Get-CodexApplication Assert-CodexNotRunning -Executable $CodexApplication.Executable $CodexArguments = "--proxy-server=${HttpProxy} --proxy-bypass-list=${ProxyBypassList}" Start-CodexApplication ` -AppUserModelId $CodexApplication.AppUserModelId ` -Arguments $CodexArguments | Out-Null