Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- setlocal DisableDelayedExpansion
- set "pickerShell=%ProgramFiles%\PowerShell\7\pwsh.exe"
- if not exist "%pickerShell%" set "pickerShell=%USERPROFILE%\.cache\codex-runtimes\codex-primary-runtime\dependencies\native\powershell\pwsh.exe"
- if not exist "%pickerShell%" (
- where /q pwsh.exe
- if errorlevel 1 (
- echo PowerShell 7.2 or later is required to open Model Picker Settings.
- pause
- exit /b 1
- )
- set "pickerShell=pwsh.exe"
- )
- set "pickerBatch=%~f0"
- "%pickerShell%" -NoProfile -STA -Command "$ErrorActionPreference = 'Stop'; $parts = [regex]::Split([IO.File]::ReadAllText($env:pickerBatch), '(?m)^::POWERSHELL_SCRIPT\r?\n'); if ($parts.Count -ne 2) { throw 'The batch file is incomplete. Copy the full code and try again.' }; $script = Join-Path ([IO.Path]::GetTempPath()) ('model-picker-' + [guid]::NewGuid() + '.ps1'); try { [IO.File]::WriteAllText($script, $parts[1]); & $script } finally { [IO.File]::Delete($script) }"
- if errorlevel 1 (
- pause
- exit /b 1
- )
- exit /b 0
- ::POWERSHELL_SCRIPT
- #requires -Version 7.2
- $ErrorActionPreference = 'Stop'
- $settingsDir = if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $env:USERPROFILE '.codex' }
- $cacheFile = Join-Path $settingsDir 'models_cache.codex_work_desktop.json'
- $configFile = Join-Path $settingsDir 'config.toml'
- $catalogFile = Join-Path $settingsDir 'model-picker-settings.catalog.json'
- $catalogLine = 'model_catalog_json = "' + $catalogFile.Replace('\', '/') + '"'
- function Write-AtomicText($path, $value) {
- $temp = "$path.$([guid]::NewGuid()).tmp"
- try {
- [IO.File]::WriteAllText($temp, $value)
- [IO.File]::Move($temp, $path, $true)
- } finally {
- if ([IO.File]::Exists($temp)) { [IO.File]::Delete($temp) }
- }
- }
- function Save-ModelSelection([string[]]$hiddenModels) {
- $config = if ([IO.File]::Exists($configFile)) { [IO.File]::ReadAllText($configFile) } else { '' }
- $root = ($config -split '(?m)^\s*\[', 2)[0]
- $entry = [regex]::Match($root, '(?m)^[ \t]*model_catalog_json[ \t]*=[^\r\n]*')
- if ($entry.Success -and $entry.Value -cne $catalogLine) {
- throw 'A different custom model catalog is configured. Your configuration has been preserved.'
- }
- if (-not $hiddenModels.Count) {
- if ($entry.Success) {
- $tail = $config.Substring($entry.Index + $entry.Length) -replace '^\r?\n', ''
- Write-AtomicText $configFile ($config.Substring(0, $entry.Index) + $tail)
- }
- return
- }
- $models = (Get-Content -LiteralPath $cacheFile -Raw | ConvertFrom-Json -Depth 100 -AsHashtable).models
- if (-not @($models | Where-Object { $_.visibility -eq 'list' -and $_.slug -notin $hiddenModels }).Count) {
- throw 'Keep at least one model visible.'
- }
- foreach ($model in $models) {
- if ($model.slug -in $hiddenModels) { $model.visibility = 'hide' }
- }
- Write-AtomicText $catalogFile (ConvertTo-Json -InputObject @{models=$models} -Depth 100 -Compress)
- if (-not $entry.Success) { Write-AtomicText $configFile ($catalogLine + "`r`n" + $config) }
- }
- if ($MyInvocation.InvocationName -eq '.') { return }
- Add-Type -AssemblyName System.Windows.Forms
- Add-Type -AssemblyName System.Drawing
- [Windows.Forms.Application]::EnableVisualStyles()
- try {
- $choices = @((Get-Content -LiteralPath $cacheFile -Raw | ConvertFrom-Json -AsHashtable).models | Where-Object visibility -eq 'list')
- if (-not $choices.Count) { throw 'The desktop model cache has no visible models.' }
- $hidden = @()
- if ((Test-Path -LiteralPath $configFile) -and ((Get-Content -LiteralPath $configFile) -ccontains $catalogLine)) {
- $hidden = @((Get-Content -LiteralPath $catalogFile -Raw | ConvertFrom-Json -AsHashtable).models | Where-Object visibility -eq 'hide' | ForEach-Object slug)
- }
- $form = [Windows.Forms.Form]::new()
- $form.Text = 'Model Picker Settings'
- $form.Font = [Drawing.Font]::new('Segoe UI', 10)
- $form.AutoScaleMode = 'Dpi'
- $form.ClientSize = [Drawing.Size]::new(410, 365)
- $form.FormBorderStyle = 'FixedDialog'
- $form.MaximizeBox = $false
- $form.StartPosition = 'CenterScreen'
- $label = [Windows.Forms.Label]::new()
- $label.Text = 'Show these models in the picker'
- $label.SetBounds(16, 16, 378, 24)
- $list = [Windows.Forms.CheckedListBox]::new()
- $list.SetBounds(16, 46, 378, 240)
- $list.CheckOnClick = $true
- foreach ($model in $choices) { [void]$list.Items.Add($model.display_name, ($model.slug -notin $hidden)) }
- $hint = [Windows.Forms.Label]::new()
- $hint.Text = 'Restart the desktop app after saving.'
- $hint.SetBounds(16, 285, 378, 24)
- $defaults = [Windows.Forms.Button]::new()
- $defaults.Text = 'Show all'
- $defaults.SetBounds(16, 320, 100, 30)
- $defaults.Add_Click({ for ($i = 0; $i -lt $list.Items.Count; $i++) { $list.SetItemChecked($i, $true) } })
- $save = [Windows.Forms.Button]::new()
- $save.Text = 'Save'
- $save.SetBounds(300, 320, 94, 30)
- $save.Add_Click({
- try {
- $off = for ($i = 0; $i -lt $choices.Count; $i++) { if (-not $list.GetItemChecked($i)) { $choices[$i].slug } }
- Save-ModelSelection $off
- $form.Close()
- } catch { [void][Windows.Forms.MessageBox]::Show($form, $_.Exception.Message, 'Model Picker Settings') }
- })
- $form.Controls.AddRange(@($label, $list, $hint, $defaults, $save))
- $form.AcceptButton = $save
- [void]$form.ShowDialog()
- $form.Dispose()
- } catch { [void][Windows.Forms.MessageBox]::Show($_.Exception.Message, 'Model Picker Settings') }
Advertisement
Add Comment
Please, Sign In to add comment