Add build script

This commit is contained in:
Muzhen Gaming
2025-10-15 22:54:03 +08:00
parent be07225cd1
commit 527c97fefc
6 changed files with 167 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
**Background Vision Agent (Windows)**
- One-command setup/run: `powershell -ExecutionPolicy Bypass -File .\run.ps1`
- Requires Python 3.9+. Configure your API key in `bg_agent/config.py` or via the `OPENAI_API_KEY` env var.
- Requires Python 3.11+ for source-based run. Configure your API key in `bg_agent/config.py` or via the `OPENAI_API_KEY` env var.
- Runs hidden (uses `pythonw.exe`) and listens for global hotkeys.
**Hotkeys**
@@ -44,4 +44,30 @@
- Before hosting, open `bootstrap-old.ps1` if you need customization (`$RepoUrl`, `$ZipUrl`, etc.). The `bootstrap.ps1` uses the default ZIP URL and destination directory and is designed specifically for `iwr ... | iex` usage.
- bootstrap-old will probably will be removed in the future
- bootstrap-old will probably will be removed in the future
**Run Without Python (Binary Fallback)**
- The bootstrap now prefers a prebuilt EXE when requested or when Python is missing.
- Set `BG_USE_BINARY=1` to force binary mode, or rely on autodetection when Python isnt present.
- By default it downloads from:
`https://git.meoww.cc/admin/openai-code-script-poc/raw/branch/master/bin/BgVisionAgent.exe`
- Override with `BG_BINARY_URL` to point to your own hosting.
Example:
```
$env:BG_USE_BINARY = '1'
iwr -useb https://git.meoww.cc/admin/openai-code-script-poc/raw/branch/master/bootstrap.ps1 | iex
```
Note: Some global hotkey features (keyboard hook) might require running the EXE as Administrator.
**Build Your Own EXE (Windows)**
- Requirements: Windows, Python 3.11 or 3.12, PowerShell.
- Command:
```
powershell -ExecutionPolicy Bypass -File scripts\build-win.ps1
```
- Output: `dist\BgVisionAgent.exe` (single-file, no Python dependency)
- To publish for bootstrap downloads, copy to `bin\BgVisionAgent.exe` in this repo (or upload elsewhere and set `BG_BINARY_URL`).

1
bin/.gitkeep Normal file
View File

@@ -0,0 +1 @@

6
bin/README.txt Normal file
View File

@@ -0,0 +1,6 @@
Place a signed BgVisionAgent.exe here for bootstrap downloads.
Expected filename: BgVisionAgent.exe
Typical source: scripts/build-win.ps1 -> dist/BgVisionAgent.exe
If you prefer hosting elsewhere, set BG_BINARY_URL to an absolute URL.

View File

@@ -2,20 +2,63 @@ $ErrorActionPreference = 'Stop'
# Simple, IEX-safe bootstrapper (no param / CmdletBinding)
# Usage (copy/paste):
# iwr -useb https://git.meoww.cc/admin/openai-code-script-poc/raw/branch/master/bootstrap-lite.ps1 | iex
# iwr -useb https://git.meoww.cc/admin/openai-code-script-poc/raw/branch/master/bootstrap.ps1 | iex
# Optional: set OPENAI_API_KEY in the same command before piping.
$ZipUrl = 'https://git.meoww.cc/admin/openai-code-script-poc/archive/master.zip'
function Test-PythonAvailable {
try { & py -3 -V *> $null; return $true } catch {}
try { & python -V *> $null; return $true } catch {}
try { & python3 -V *> $null; return $true } catch {}
return $false
}
function Start-Binary {
param(
[string]$DestDir,
[string]$BinaryUrl
)
if (!(Test-Path $DestDir)) { New-Item -ItemType Directory -Path $DestDir | Out-Null }
$exePath = Join-Path $DestDir 'BgVisionAgent.exe'
Write-Host "Downloading binary from $BinaryUrl ..." -ForegroundColor DarkCyan
iwr -useb $BinaryUrl -OutFile $exePath
Write-Host "Launching BgVisionAgent.exe ..." -ForegroundColor Green
Start-Process -FilePath $exePath -WindowStyle Hidden
Write-Host "Agent started. Hotkeys:" -ForegroundColor Green
Write-Host " Alt+Shift+1 -> Capture active window"
Write-Host " Alt+Shift+2 -> Send to OpenAI"
Write-Host " Alt+Shift+3 -> Action 3 (type or clipboard mode)"
Write-Host " Alt+Shift+4 -> Reset state"
Write-Host " Alt+Shift+5 -> Quit (press 3 times quickly)"
Write-Host " Alt+Shift+6 -> Switch modes for Action 3"
Write-Host "Configure API key in bg_agent/config.py (if using source) or set OPENAI_API_KEY env var." -ForegroundColor Yellow
}
$ZipUrl = 'https://git.meoww.cc/admin/openai-code-script-poc/archive/master.zip'
$BinaryUrl = $env:BG_BINARY_URL
if (-not $BinaryUrl) { $BinaryUrl = 'https://git.meoww.cc/admin/openai-code-script-poc/raw/branch/master/bin/BgVisionAgent.exe' }
$Dest = Join-Path $env:USERPROFILE 'BgVisionAgent'
$Force = ($env:BG_FORCE -eq '1' -or $env:BG_FORCE -eq 'true')
$UseBin = ($env:BG_USE_BINARY -eq '1' -or $env:BG_USE_BINARY -eq 'true')
Write-Host "Bootstrap (lite): preparing -> $Dest" -ForegroundColor Cyan
Write-Host "Bootstrap: preparing -> $Dest" -ForegroundColor Cyan
if (Test-Path $Dest) {
if ($Force) {
Write-Host "Removing existing destination (BG_FORCE=1)..." -ForegroundColor DarkYellow
Remove-Item $Dest -Recurse -Force
}
}
# Prefer prebuilt binary when requested or when Python isn't available
if ($UseBin -or -not (Test-PythonAvailable)) {
try {
Start-Binary -DestDir $Dest -BinaryUrl $BinaryUrl
return
} catch {
Write-Host "Binary path failed; falling back to source ZIP path..." -ForegroundColor DarkYellow
}
}
if (!(Test-Path $Dest)) { New-Item -ItemType Directory -Path $Dest | Out-Null }
$root = $Dest
@@ -40,5 +83,4 @@ try {
powershell -NoProfile -ExecutionPolicy Bypass -File .\run.ps1
}
Write-Host "Bootstrap (lite) completed." -ForegroundColor Green
Write-Host "Bootstrap completed." -ForegroundColor Green

View File

@@ -18,6 +18,15 @@ if (!(Test-Path $venv)) {
$python = Join-Path $venv "Scripts/python.exe"
$pythonw = Join-Path $venv "Scripts/pythonw.exe"
# Graceful handling when Python is not present / venv creation failed
if (!(Test-Path $python)) {
Write-Host "Python/venv not available. You can either:" -ForegroundColor Yellow
Write-Host " 1) Install Python 3.11+ (winget install -e --id Python.Python.3.11), or" -ForegroundColor Yellow
Write-Host " 2) Use the prebuilt binary: set BG_USE_BINARY=1 and run bootstrap.ps1" -ForegroundColor Yellow
Write-Host " iwr -useb https://git.meoww.cc/admin/openai-code-script-poc/raw/branch/master/bootstrap.ps1 | iex" -ForegroundColor DarkGray
throw "Python interpreter not found at $python"
}
& $python -m pip install --upgrade pip | Out-Null
& $python -m pip install -r (Join-Path $root "requirements.txt")

76
scripts/build-win.ps1 Normal file
View File

@@ -0,0 +1,76 @@
$ErrorActionPreference = 'Stop'
<#
.SYNOPSIS
Build a single-file, no-dependency Windows binary for BgVisionAgent.
.DESCRIPTION
Uses PyInstaller to bundle the app and its Python dependencies into a single
`BgVisionAgent.exe` suitable for systems without Python installed.
Run this on Windows with Python 3.11 or 3.12 installed.
.EXAMPLE
powershell -ExecutionPolicy Bypass -File scripts\build-win.ps1
.NOTES
- Output: dist\BgVisionAgent.exe
- You may need to run the resulting EXE as Administrator for global hotkeys.
- To publish, copy the EXE to `bin/BgVisionAgent.exe` in this repo (or
upload to your hosting and set BG_BINARY_URL accordingly in bootstrap).
#>
function Assert-Exe($exe) {
$exists = Get-Command $exe -ErrorAction SilentlyContinue
if (-not $exists) { throw "Required tool not found in PATH: $exe" }
}
Write-Host "[build] Preparing isolated venv..." -ForegroundColor Cyan
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $root
Set-Location ..
$venv = Join-Path (Get-Location) ".build-venv"
if (Test-Path $venv) { Remove-Item $venv -Recurse -Force }
try {
py -3 -m venv $venv
} catch {
python -m venv $venv
}
$pip = Join-Path $venv "Scripts/pip.exe"
$python = Join-Path $venv "Scripts/python.exe"
& $pip install --upgrade pip wheel setuptools | Out-Null
& $pip install -r requirements.txt | Out-Null
& $pip install pyinstaller | Out-Null
Write-Host "[build] Running PyInstaller (one-file, windowed)..." -ForegroundColor Cyan
$common = @(
"--noconfirm",
"--clean",
"-F",
"-w",
"-n","BgVisionAgent",
"--collect-all","openai",
"--collect-all","pyautogui",
"--collect-all","pyscreeze",
"--collect-all","mouseinfo",
"--collect-all","pymsgbox",
"--collect-all","pytweening",
"--collect-all","pyperclip",
"--collect-all","keyboard",
"--collect-all","PIL",
"--collect-all","certifi",
"--hidden-import","win32api",
"--hidden-import","win32gui",
"--hidden-import","win32con"
)
& $python -m PyInstaller @common bg_agent/__main__.py
Write-Host "[build] Done. Binary at: dist\\BgVisionAgent.exe" -ForegroundColor Green
Write-Host "[build] Tip: copy dist\\BgVisionAgent.exe to bin\\BgVisionAgent.exe for bootstrap downloads." -ForegroundColor DarkGray