aisop/tools/Start-HandAiD3.ps1

59 lines
2.7 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[CmdletBinding()]
param(
[string]$D3Root = 'E:\D3Lite',
[ValidateRange(1,65535)][int]$Port = 8766,
[switch]$CheckOnly
)
$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
$healthUrl = "http://127.0.0.1:$Port/health"
function Get-D3Health {
try {
$health = Invoke-RestMethod -Uri $healthUrl -TimeoutSec 2
if ($health.status -eq 'ready' -and $health.model -in @('d3-lite', 'd3-nano') -and 'image' -in $health.modalities) { return $health }
} catch { }
return $null
}
$health = Get-D3Health
if ($null -eq $health) {
$python = Join-Path $D3Root '.venv\Scripts\python.exe'
$server = Join-Path $repoRoot 'tools\start_d3_lite_local.py'
if (!(Test-Path -LiteralPath $python) -or !(Test-Path -LiteralPath (Join-Path $D3Root 'model\decision_config.json'))) {
throw "djev 本地环境缺失:$D3Root"
}
$logs = Join-Path $repoRoot 'artifacts\d3-integration'
New-Item -ItemType Directory -Force -Path $logs | Out-Null
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss-fff'
$process = Start-Process -FilePath $python -ArgumentList @('-u', ('"' + $server + '"'), '--root', ('"' + $D3Root + '"'), '--port', $Port) `
-WorkingDirectory $repoRoot -WindowStyle Hidden -PassThru `
-RedirectStandardOutput (Join-Path $logs "$stamp.stdout.log") `
-RedirectStandardError (Join-Path $logs "$stamp.stderr.log")
Set-Content -LiteralPath (Join-Path $D3Root 'server.pid') -Value $process.Id -Encoding ascii
Write-Host "正在启动 djev,PID=$($process.Id),日志:$logs"
$deadline = [DateTime]::UtcNow.AddSeconds(120)
do {
$health = Get-D3Health
if ($null -ne $health) { break }
if ($process.HasExited) { throw "djev 启动失败,请查看 $logs" }
Start-Sleep -Milliseconds 500
} while ([DateTime]::UtcNow -lt $deadline)
if ($null -eq $health) { throw "等待 djev 就绪超时,请查看 $logs" }
}
Write-Host "djev 图片接口已就绪:http://127.0.0.1:$Port/v1/systemone"
if ($CheckOnly) { return }
$previousProvider = [Environment]::GetEnvironmentVariable('SOPHELPER_VISION_PROVIDER')
$previousEndpoint = [Environment]::GetEnvironmentVariable('SOPHELPER_VISION_ENDPOINT')
$env:SOPHELPER_VISION_PROVIDER = 'd3-lite'
$env:SOPHELPER_VISION_ENDPOINT = "http://127.0.0.1:$Port/v1/systemone"
Push-Location $repoRoot
try {
& dotnet run --project src/SopHelper.Avalonia
if ($LASTEXITCODE -ne 0) { throw "SOP 程序退出,代码:$LASTEXITCODE" }
} finally {
Pop-Location
[Environment]::SetEnvironmentVariable('SOPHELPER_VISION_PROVIDER', $previousProvider)
[Environment]::SetEnvironmentVariable('SOPHELPER_VISION_ENDPOINT', $previousEndpoint)
}