feat: add safe Javis startup script
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$UvPath,
|
||||
[string]$OllamaPath,
|
||||
[string]$OllamaModels,
|
||||
[string]$DataDir,
|
||||
[string]$UvCacheDir,
|
||||
[string]$PythonInstallDir
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
function Get-ConfiguredPath {
|
||||
param(
|
||||
[string]$ParameterValue,
|
||||
[string]$EnvironmentName,
|
||||
[string]$DefaultValue
|
||||
)
|
||||
|
||||
if (-not [string]::IsNullOrWhiteSpace($ParameterValue)) {
|
||||
return [System.IO.Path]::GetFullPath($ParameterValue)
|
||||
}
|
||||
$environmentValue = [System.Environment]::GetEnvironmentVariable($EnvironmentName)
|
||||
if (-not [string]::IsNullOrWhiteSpace($environmentValue)) {
|
||||
return [System.IO.Path]::GetFullPath($environmentValue)
|
||||
}
|
||||
return [System.IO.Path]::GetFullPath($DefaultValue)
|
||||
}
|
||||
|
||||
function Test-OllamaReady {
|
||||
try {
|
||||
$null = Invoke-RestMethod `
|
||||
-Uri 'http://127.0.0.1:11434/api/version' `
|
||||
-Method Get `
|
||||
-TimeoutSec 2
|
||||
return $true
|
||||
}
|
||||
catch {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
$repositoryRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..'))
|
||||
if (-not (Test-Path -LiteralPath (Join-Path $repositoryRoot 'pyproject.toml') -PathType Leaf)) {
|
||||
throw "Das Repository konnte relativ zum Startskript nicht verifiziert werden."
|
||||
}
|
||||
|
||||
$driveRoot = [System.IO.Path]::GetPathRoot($repositoryRoot)
|
||||
$resolvedUv = Get-ConfiguredPath `
|
||||
-ParameterValue $UvPath `
|
||||
-EnvironmentName 'JAVIS_UV_PATH' `
|
||||
-DefaultValue (Join-Path $driveRoot 'Javis-Tools\uv\uv.exe')
|
||||
$resolvedOllama = Get-ConfiguredPath `
|
||||
-ParameterValue $OllamaPath `
|
||||
-EnvironmentName 'JAVIS_OLLAMA_PATH' `
|
||||
-DefaultValue (Join-Path $driveRoot 'Javis-Tools\ollama\ollama.exe')
|
||||
$resolvedModels = Get-ConfiguredPath `
|
||||
-ParameterValue $OllamaModels `
|
||||
-EnvironmentName 'JAVIS_OLLAMA_MODELS' `
|
||||
-DefaultValue (Join-Path $driveRoot 'Javis-Data\ollama-models')
|
||||
$resolvedData = Get-ConfiguredPath `
|
||||
-ParameterValue $DataDir `
|
||||
-EnvironmentName 'JAVIS_DATA_DIR' `
|
||||
-DefaultValue (Join-Path $driveRoot 'Javis-Data\runtime')
|
||||
$resolvedCache = Get-ConfiguredPath `
|
||||
-ParameterValue $UvCacheDir `
|
||||
-EnvironmentName 'JAVIS_UV_CACHE_DIR' `
|
||||
-DefaultValue (Join-Path $driveRoot 'Javis-Tools\uv-cache')
|
||||
$resolvedPython = Get-ConfiguredPath `
|
||||
-ParameterValue $PythonInstallDir `
|
||||
-EnvironmentName 'JAVIS_PYTHON_INSTALL_DIR' `
|
||||
-DefaultValue (Join-Path $driveRoot 'Javis-Tools\python')
|
||||
|
||||
if (-not (Test-Path -LiteralPath $resolvedUv -PathType Leaf)) {
|
||||
throw "uv.exe wurde nicht gefunden: $resolvedUv"
|
||||
}
|
||||
if (-not (Test-Path -LiteralPath $resolvedModels -PathType Container)) {
|
||||
throw "Der externe Ollama-Modellpfad wurde nicht gefunden: $resolvedModels"
|
||||
}
|
||||
|
||||
$env:JAVIS_DATA_DIR = $resolvedData
|
||||
$env:OLLAMA_MODELS = $resolvedModels
|
||||
$env:OLLAMA_HOST = '127.0.0.1:11434'
|
||||
$env:UV_CACHE_DIR = $resolvedCache
|
||||
$env:UV_PYTHON_INSTALL_DIR = $resolvedPython
|
||||
|
||||
if (-not (Test-OllamaReady)) {
|
||||
if (-not (Test-Path -LiteralPath $resolvedOllama -PathType Leaf)) {
|
||||
throw "Ollama ist nicht erreichbar und ollama.exe wurde nicht gefunden: $resolvedOllama"
|
||||
}
|
||||
Write-Host "Ollama wird lokal auf 127.0.0.1:11434 gestartet ..."
|
||||
$ollamaProcess = Start-Process `
|
||||
-FilePath $resolvedOllama `
|
||||
-ArgumentList 'serve' `
|
||||
-WindowStyle Hidden `
|
||||
-PassThru
|
||||
|
||||
$ready = $false
|
||||
for ($attempt = 1; $attempt -le 30; $attempt++) {
|
||||
if ($ollamaProcess.HasExited) {
|
||||
throw "Ollama wurde beendet, bevor die lokale API bereit war."
|
||||
}
|
||||
Start-Sleep -Milliseconds 500
|
||||
if (Test-OllamaReady) {
|
||||
$ready = $true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (-not $ready) {
|
||||
throw "Ollama war nach 15 Sekunden nicht unter 127.0.0.1:11434 erreichbar."
|
||||
}
|
||||
}
|
||||
|
||||
Push-Location $repositoryRoot
|
||||
try {
|
||||
& $resolvedUv run javis chat
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
finally {
|
||||
Pop-Location
|
||||
}
|
||||
Reference in New Issue
Block a user