# Avvia il motore di Neuronal: carica il modello, mappa il cervello e apre ws://127.0.0.1:/ws param( [int]$Port = 8765, [int]$Regions = 6, [switch]$NoAtlas, [switch]$Supervise # riavvia automaticamente il motore se termina ) $ErrorActionPreference = 'Stop' $root = Split-Path -Parent $PSScriptRoot $runtime = Join-Path $root 'runtime\llamacpp-b11200' $exe = Join-Path $root 'build\neuronald.exe' # i modelli: in models\ (installazione) oppure nella cartella principale (sviluppo) $models = Join-Path $root 'models' $model = Join-Path $models 'neuronal.gguf' $vision = Join-Path $models 'neuronal-visione.gguf' if (-not (Test-Path $model)) { $model = Join-Path $root 'neuronal.gguf'; $vision = Join-Path $root 'neuronal-visione.gguf' } foreach ($f in @($exe, $runtime, $model)) { if (-not (Test-Path $f)) { throw "Mancante: $f (hai compilato con 'cmake --build build'?)" } } if (Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue) { throw "La porta $Port è già in uso: il motore è forse già acceso?" } # Certificato HTTPS per il telefono (i browser danno il microfono solo su HTTPS). # Si rigenera quando cambiano gli indirizzi del PC nella rete di casa. function Update-Certificate { $tls = Join-Path $root 'state\tls' New-Item -ItemType Directory -Force $tls | Out-Null $ips = @(Get-NetIPAddress -AddressFamily IPv4 -ErrorAction SilentlyContinue | Where-Object { $_.IPAddress -notlike '127.*' -and $_.IPAddress -notlike '169.254.*' } | ForEach-Object IPAddress | Sort-Object) $hostName = "$($env:COMPUTERNAME.ToLower()).local" # nome fisso in rete: non cambia quando cambia l'IP $want = (@($hostName) + $ips) -join ',' $stamp = Join-Path $tls 'ips.txt' $have = if (Test-Path $stamp) { (Get-Content $stamp -Raw).Trim() } else { '' } if ((Test-Path "$tls\cert.pem") -and (Test-Path "$tls\key.pem") -and $have -eq $want) { return } $openssl = (Get-Command openssl -ErrorAction SilentlyContinue).Source foreach ($p in "$env:ProgramFiles\Git\usr\bin\openssl.exe", "$env:ProgramFiles\Git\mingw64\bin\openssl.exe") { if (-not $openssl -and (Test-Path $p)) { $openssl = $p } } if (-not $openssl) { # PC senza Git: il certificato lo crea il Python di Neuronal (installato con lui) $py = Join-Path $root 'python\python.exe' if (-not (Test-Path $py)) { $py = Join-Path $root 'mind\.venv\Scripts\python.exe' } if (Test-Path $py) { & $py (Join-Path $root 'scripts\crea_certificato.py') $tls $hostName @ips if (Test-Path "$tls\cert.pem") { Set-Content -Path $stamp -Value $want } } else { Write-Warning 'OpenSSL non trovato: niente HTTPS, il microfono del telefono non sarà disponibile.' } return } $san = (@('DNS:localhost', "DNS:$hostName", 'IP:127.0.0.1') + ($ips | ForEach-Object { "IP:$_" })) -join ',' & $openssl ecparam -name prime256v1 -genkey -noout -out "$tls\key.pem" 2>$null & $openssl req -new -x509 -key "$tls\key.pem" -out "$tls\cert.pem" -days 3650 -subj '/CN=Neuronal' -addext "subjectAltName=$san" 2>$null if (Test-Path "$tls\cert.pem") { Set-Content -Path $stamp -Value $want; Write-Host "Certificato HTTPS creato per: $want" } else { Write-Warning 'Creazione del certificato HTTPS non riuscita.' } } $ErrorActionPreference = 'Continue' Update-Certificate $ErrorActionPreference = 'Stop' $env:PATH = "$runtime;$env:PATH" # llama.dll e ggml*.dll $neuronalArgs = @('--port', $Port, '--regions', $Regions, '--backends', "`"$runtime`"", '--model', "`"$model`"", '--vision', "`"$vision`"") if ($NoAtlas) { $neuronalArgs += '--no-atlas' } Push-Location $root try { $restarts = 0 do { $p = Start-Process -FilePath $exe -ArgumentList $neuronalArgs -NoNewWindow -PassThru $null = $p.Handle # senza questo, in PowerShell 5.1 ExitCode resta vuoto $p.WaitForExit() if (-not $Supervise) { exit $p.ExitCode } $restarts++ $delay = [math]::Min(60, 2 * $restarts) Write-Warning "neuronald terminato (codice $($p.ExitCode)). Riavvio #$restarts tra $delay s." Start-Sleep -Seconds $delay } while ($true) } finally { Pop-Location }