param([switch]$NoBrowser, [int]$Port = 8765) $ErrorActionPreference = 'Stop' $projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path $runtimePath = Join-Path $projectRoot '.runtime' New-Item -ItemType Directory -Path $runtimePath -Force | Out-Null $pythonPath = Join-Path $projectRoot '.venv\Scripts\python.exe' if (-not (Test-Path -LiteralPath $pythonPath)) { if (Get-Command py.exe -ErrorAction SilentlyContinue) { & py.exe -3 -m venv (Join-Path $projectRoot '.venv') } elseif (Get-Command python.exe -ErrorAction SilentlyContinue) { & python.exe -m venv (Join-Path $projectRoot '.venv') } else { throw 'Please install Python 3.12+ first.' } if ($LASTEXITCODE -ne 0) { throw 'Could not create Python environment.' } } & $pythonPath -c "import sys; raise SystemExit(0 if sys.version_info >= (3, 12) else 1)" if ($LASTEXITCODE -ne 0) { throw 'Python 3.12+ is required; recreate .venv with a supported interpreter.' } & $pythonPath -c "import fastapi, uvicorn, requests, bs4, openpyxl, multipart, playwright" 2>$null if ($LASTEXITCODE -ne 0) { $env:PIP_CACHE_DIR = Join-Path $runtimePath 'pip-cache' & $pythonPath -m pip install -r (Join-Path $projectRoot 'requirements.lock') if ($LASTEXITCODE -ne 0) { throw 'Python dependency installation failed.' } } if (-not (Test-Path -LiteralPath (Join-Path $projectRoot 'frontend\dist\index.html'))) { Push-Location (Join-Path $projectRoot 'frontend') try { & npm.cmd ci --cache (Join-Path $runtimePath 'npm-cache') --no-audit --no-fund if ($LASTEXITCODE -ne 0) { throw 'npm dependency installation failed.' } & npm.cmd run build if ($LASTEXITCODE -ne 0) { throw 'Frontend build failed.' } } finally { Pop-Location } } $address = "http://127.0.0.1:$Port" function Get-StudioHealth { try { return Invoke-RestMethod -Uri "$address/api/health" -TimeoutSec 2 } catch { return $null } } $health = Get-StudioHealth if ($health) { if ($health.app -ne 'kaoyan-studio' -or $health.root -ne $projectRoot -or $health.data_dir -ne $runtimePath) { throw "Port $Port belongs to another instance; no process was changed." } } else { if (Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue) { throw "Port $Port is in use; choose another port with -Port." } $process = Start-Process -FilePath $pythonPath -ArgumentList @('-m', 'studio', '--port', $Port) -WorkingDirectory $projectRoot -WindowStyle Hidden -RedirectStandardOutput (Join-Path $runtimePath 'server.log') -RedirectStandardError (Join-Path $runtimePath 'server-error.log') -PassThru @{ pid = $process.Id; root = $projectRoot; port = $Port } | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $runtimePath 'server.json') -Encoding UTF8 $deadline = (Get-Date).AddSeconds(25) do { Start-Sleep -Milliseconds 400 $health = Get-StudioHealth if ($process.HasExited) { throw 'Startup failed; see .runtime/server-error.log.' } } until ($health -or (Get-Date) -gt $deadline) if (-not $health -or $health.app -ne 'kaoyan-studio' -or $health.root -ne $projectRoot) { throw 'Startup timed out; see .runtime/server-error.log.' } } Write-Output "Kaoyan Studio is running: $address" if (-not $NoBrowser) { Start-Process $address }