57 lines
3.1 KiB
PowerShell
57 lines
3.1 KiB
PowerShell
[CmdletBinding()]
|
|
param([int]$Port = 0, [switch]$NoBrowser)
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false)
|
|
$appRoot = [IO.Path]::GetFullPath((Split-Path -Parent $PSScriptRoot))
|
|
if ($Port -eq 0) { $Port = if ($env:AGENT_WORKBENCH_PORT) { [int]$env:AGENT_WORKBENCH_PORT } else { 4318 } }
|
|
if ($Port -lt 1 -or $Port -gt 65535) { throw 'Please use a port between 1 and 65535.' }
|
|
$workbenchUrl = "http://127.0.0.1:$Port"
|
|
|
|
function Read-WorkbenchHealth {
|
|
try { return Invoke-RestMethod -Uri "$workbenchUrl/api/health" -TimeoutSec 2 }
|
|
catch { return $null }
|
|
}
|
|
|
|
$health = Read-WorkbenchHealth
|
|
if ($health) {
|
|
if ($health.app -ne 'agent-workbench' -or -not $health.ready) { throw "The port is in use by another service: $Port" }
|
|
Write-Output "Agent Workbench is already running: $workbenchUrl"
|
|
if (-not $NoBrowser) { Start-Process -FilePath $workbenchUrl }
|
|
exit 0
|
|
}
|
|
|
|
$serverFile = Join-Path $appRoot 'dist-server\server.mjs'
|
|
if (-not (Test-Path -LiteralPath $serverFile)) { throw 'Build the source first with npm.cmd run build, or use the portable package.' }
|
|
$nodeFile = Join-Path $appRoot 'runtime\node.exe'
|
|
if (-not (Test-Path -LiteralPath $nodeFile)) {
|
|
$installedNode = Get-Command node.exe -ErrorAction SilentlyContinue
|
|
if (-not $installedNode) { throw 'Node.js 24 was not found. Use the portable package with its bundled runtime.' }
|
|
$nodeFile = $installedNode.Source
|
|
}
|
|
$dataRoot = if ($env:AGENT_WORKBENCH_DATA_DIR) { [IO.Path]::GetFullPath($env:AGENT_WORKBENCH_DATA_DIR) } else { Join-Path $env:LOCALAPPDATA 'AgentWorkbench' }
|
|
$logsRoot = Join-Path $dataRoot 'logs'
|
|
[IO.Directory]::CreateDirectory($logsRoot) | Out-Null
|
|
$stdoutFile = Join-Path $logsRoot 'service.stdout.log'
|
|
$stderrFile = Join-Path $logsRoot 'service.stderr.log'
|
|
$env:AGENT_WORKBENCH_APP_DIR = $appRoot
|
|
$env:AGENT_WORKBENCH_DATA_DIR = $dataRoot
|
|
$env:AGENT_WORKBENCH_PORT = [string]$Port
|
|
$childProcess = Start-Process -FilePath $nodeFile -ArgumentList ('"' + $serverFile + '"') -WorkingDirectory $appRoot -WindowStyle Hidden -PassThru -RedirectStandardOutput $stdoutFile -RedirectStandardError $stderrFile
|
|
$deadline = [DateTime]::UtcNow.AddSeconds(15)
|
|
do {
|
|
Start-Sleep -Milliseconds 200
|
|
$childProcess.Refresh()
|
|
if ($childProcess.HasExited) {
|
|
$diagnostic = if (Test-Path -LiteralPath $stderrFile) { (Get-Content -LiteralPath $stderrFile -Tail 8) -join "`n" } else { '' }
|
|
throw "Agent Workbench could not start. $diagnostic"
|
|
}
|
|
$health = Read-WorkbenchHealth
|
|
} while ((-not $health -or -not $health.ready) -and [DateTime]::UtcNow -lt $deadline)
|
|
if (-not $health -or $health.app -ne 'agent-workbench' -or -not $health.ready) { throw "Service did not become ready. See $stderrFile" }
|
|
$serviceInfo = @{ app = 'agent-workbench'; pid = $childProcess.Id; port = $Port; appRoot = $appRoot; dataRoot = $dataRoot; startedAt = [DateTime]::UtcNow.ToString('o') } | ConvertTo-Json
|
|
[IO.File]::WriteAllText((Join-Path $dataRoot 'service-info.json'), $serviceInfo, [Text.UTF8Encoding]::new($false))
|
|
Write-Output "Agent Workbench is ready: $workbenchUrl"
|
|
Write-Output "Data: $dataRoot"
|
|
if (-not $NoBrowser) { Start-Process -FilePath $workbenchUrl }
|