-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.ps1.backup
More file actions
329 lines (288 loc) · 13.2 KB
/
start.ps1.backup
File metadata and controls
329 lines (288 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# PowerShell version of start.sh
# Define colors for console output
function Write-ColorText {
param(
[string]$Text,
[string]$Color = "White"
)
Write-Host $Text -ForegroundColor $Color
}
Write-ColorText "Starting KrunchWrapper" "Green"
Write-Host "==================="
Write-ColorText "Using configuration from config\server.jsonc and config\config.jsonc" "Yellow"
# Get the directory of the script
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition
# Define the virtual environment directory relative to the script location
$VenvDir = Join-Path $ScriptDir ".venv"
# Check if virtual environment exists
if (-not (Test-Path $VenvDir)) {
Write-ColorText "Virtual environment not found at $VenvDir" "Yellow"
Write-Host "Please run .\install.bat first to set up the environment."
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
# Function to read config values
function Read-Config {
$escapedScriptDir = $ScriptDir.Replace('\', '\\')
$pythonScript = @"
import json
import os
import sys
import re
# Read server config (webui_enabled, webui_port)
server_config = {}
try:
server_config_path = os.path.join(r'$escapedScriptDir', 'config', 'server.jsonc')
with open(server_config_path, 'r', encoding='utf-8') as f:
content = f.read()
# Remove comments
content = re.sub(r'//.*', '', content)
content = re.sub(r'/\*.*?\*/', '', content, flags=re.DOTALL)
server_config = json.loads(content)
except Exception as e:
pass # use defaults
# Read main config (use_cline)
main_config = {}
try:
main_config_path = os.path.join(r'$escapedScriptDir', 'config', 'config.jsonc')
with open(main_config_path, 'r', encoding='utf-8') as f:
content = f.read()
# Remove comments
content = re.sub(r'//.*', '', content)
content = re.sub(r'/\*.*?\*/', '', content, flags=re.DOTALL)
main_config = json.loads(content)
except Exception as e:
pass # use defaults
# Get values with defaults
webui_enabled = server_config.get('webui_enabled', True)
webui_port = server_config.get('webui_port', 5173)
use_cline = main_config.get('system_prompt', {}).get('use_cline', False)
print('{}|{}|{}'.format(webui_enabled, webui_port, use_cline))
"@
try {
$configValues = python -c $pythonScript
return $configValues -split '\|'
} catch {
# Return defaults if python fails
return @("True", "5173", "False")
}
}
# Get config values
$ConfigValues = Read-Config
$WebuiEnabled = $ConfigValues[0]
$WebuiPort = $ConfigValues[1]
$UseCline = $ConfigValues[2]
# Create a new PowerShell session with activated virtual environment
$startScript = {
param($ScriptDir, $VenvDir, $WebuiEnabled, $WebuiPort, $UseCline)
Set-Location $ScriptDir
# Activate virtual environment (Windows)
$activateScript = Join-Path $VenvDir "Scripts\Activate.ps1"
if (Test-Path $activateScript) {
Write-Host "Activating virtual environment..." -ForegroundColor Yellow
try {
& $activateScript
if ($LASTEXITCODE -ne 0) {
throw "Activation script returned non-zero exit code: $LASTEXITCODE"
}
} catch {
Write-Host "Failed to activate using PowerShell script: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Trying batch file activation as fallback..." -ForegroundColor Yellow
# Try batch file activation as fallback
$activateBat = Join-Path $VenvDir "Scripts\activate.bat"
if (Test-Path $activateBat) {
cmd /c "`"$activateBat`" && set" | Where-Object { $_ -match '^([^=]+)=(.*)$' } | ForEach-Object {
$name = $matches[1]
$value = $matches[2]
Set-Item -Path "env:$name" -Value $value
}
} else {
Write-Host "Activation script not found at $activateScript" -ForegroundColor Red
Write-Host "Please run install.ps1 first to set up the environment properly." -ForegroundColor Yellow
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
}
} else {
# Try batch file activation as fallback
$activateBat = Join-Path $VenvDir "Scripts\activate.bat"
if (Test-Path $activateBat) {
Write-Host "PowerShell activation script not found, using batch file..." -ForegroundColor Yellow
cmd /c "`"$activateBat`" && set" | Where-Object { $_ -match '^([^=]+)=(.*)$' } | ForEach-Object {
$name = $matches[1]
$value = $matches[2]
Set-Item -Path "env:$name" -Value $value
}
} else {
Write-Host "Activation script not found at $activateScript" -ForegroundColor Red
Write-Host "Please run install.ps1 first to set up the environment properly." -ForegroundColor Yellow
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
}
# Verify virtual environment activation worked
Write-Host "Verifying virtual environment activation..." -ForegroundColor Yellow
try {
$pythonPath = (python -c "import sys; print(sys.executable)" 2>&1)
if ($LASTEXITCODE -eq 0 -and $pythonPath -like "*$VenvDir*") {
Write-Host "✅ Virtual environment activated successfully!" -ForegroundColor Green
Write-Host " Python path: $pythonPath" -ForegroundColor Cyan
} else {
throw "Python not using virtual environment. Path: $pythonPath"
}
} catch {
Write-Host "❌ Virtual environment activation verification failed: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Please run install.ps1 first to set up the environment properly." -ForegroundColor Yellow
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
# Verify critical dependencies are available
Write-Host "Verifying dependencies..." -ForegroundColor Yellow
try {
$uvicornCheck = python -c "import uvicorn; print('uvicorn OK')" 2>&1
if ($LASTEXITCODE -ne 0) {
throw "uvicorn import failed: $uvicornCheck"
}
$fastapiCheck = python -c "import fastapi; print('fastapi OK')" 2>&1
if ($LASTEXITCODE -ne 0) {
throw "fastapi import failed: $fastapiCheck"
}
$aiohttpCheck = python -c "import aiohttp; print('aiohttp OK')" 2>&1
if ($LASTEXITCODE -ne 0) {
throw "aiohttp import failed: $aiohttpCheck"
}
$tiktokenCheck = python -c "import tiktoken; print('tiktoken OK')" 2>&1
if ($LASTEXITCODE -ne 0) {
throw "tiktoken import failed: $tiktokenCheck"
}
$pydanticCheck = python -c "import pydantic; print('pydantic OK')" 2>&1
if ($LASTEXITCODE -ne 0) {
throw "pydantic import failed: $pydanticCheck"
}
Write-Host "✅ All critical dependencies are available!" -ForegroundColor Green
} catch {
Write-Host "❌ Dependency verification failed: $($_.Exception.Message)" -ForegroundColor Red
Write-Host "Installing missing dependencies..." -ForegroundColor Yellow
try {
pip install -r requirements.txt
if ($LASTEXITCODE -ne 0) {
throw "pip install failed"
}
Write-Host "✅ Dependencies installed successfully!" -ForegroundColor Green
} catch {
Write-Host "❌ Failed to install dependencies. Please run install.ps1 first." -ForegroundColor Red
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
}
Write-Host "KrunchWrapper virtual environment activated!" -ForegroundColor Green
# Define the server script path
$ServerScript = Join-Path $ScriptDir "server\run_server.py"
# Check if server script exists
if (-not (Test-Path $ServerScript)) {
Write-Host "Server script not found at $ServerScript" -ForegroundColor Yellow
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
Write-Host "Starting server..." -ForegroundColor Green
# Start the server in background
$ServerProcess = Start-Process -FilePath "python" -ArgumentList "`"$ServerScript`"" -PassThru -WindowStyle Hidden
# Handle different modes based on configuration
if ($WebuiEnabled -eq "True" -and $UseCline -eq "False" -and (Test-Path (Join-Path $ScriptDir "webui")) -and (Get-Command npm -ErrorAction SilentlyContinue)) {
# WebUI mode - wait for server to be ready before starting WebUI
Write-Host "Waiting for server to be ready..." -ForegroundColor Yellow
Start-Sleep -Seconds 5
# Check if server is running
if ($ServerProcess.HasExited) {
Write-Host "Server failed to start" -ForegroundColor Yellow
Write-Host "`nPress any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
Write-Host "Starting WebUI on port $WebuiPort..." -ForegroundColor Green
$WebuiDir = Join-Path $ScriptDir "webui"
Set-Location $WebuiDir
$WebuiProcess = Start-Process -FilePath "npm" -ArgumentList "run", "dev" -PassThru -WindowStyle Hidden -WorkingDirectory $WebuiDir
Set-Location $ScriptDir
# Wait a moment for webui to start, then open browser
Start-Sleep -Seconds 3
try {
Start-Process "http://localhost:$WebuiPort"
} catch {
Write-Host "Could not open browser automatically. Please visit http://localhost:$WebuiPort" -ForegroundColor Yellow
}
Write-Host "WebUI started! Browser should open automatically..." -ForegroundColor Green
Write-Host "WebUI URL: http://localhost:$WebuiPort" -ForegroundColor Yellow
# Function to cleanup on exit
$cleanup = {
Write-Host "Shutting down WebUI and server..." -ForegroundColor Yellow
try {
if ($WebuiProcess -and -not $WebuiProcess.HasExited) {
Stop-Process -Id $WebuiProcess.Id -Force -ErrorAction SilentlyContinue
}
} catch { }
try {
if ($ServerProcess -and -not $ServerProcess.HasExited) {
Stop-Process -Id $ServerProcess.Id -Force -ErrorAction SilentlyContinue
}
} catch { }
}
# Register cleanup on Ctrl+C
try {
[Console]::TreatControlCAsInput = $false
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action $cleanup
} catch { }
# Wait for either process to exit
while (-not $ServerProcess.HasExited -and -not $WebuiProcess.HasExited) {
Start-Sleep -Seconds 1
}
} elseif ($UseCline -eq "True") {
Write-Host "WebUI disabled (use_cline: true in config)" -ForegroundColor Yellow
Write-Host "Server running in Cline mode" -ForegroundColor Green
# Function to cleanup on exit
$cleanup = {
Write-Host "Shutting down server..." -ForegroundColor Yellow
try {
if ($ServerProcess -and -not $ServerProcess.HasExited) {
Stop-Process -Id $ServerProcess.Id -Force -ErrorAction SilentlyContinue
}
} catch { }
}
# Register cleanup on Ctrl+C
try {
[Console]::TreatControlCAsInput = $false
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action $cleanup
} catch { }
# Wait for server process
$ServerProcess.WaitForExit()
} else {
Write-Host "WebUI not started" -ForegroundColor Yellow
# Function to cleanup on exit
$cleanup = {
Write-Host "Shutting down server..." -ForegroundColor Yellow
try {
if ($ServerProcess -and -not $ServerProcess.HasExited) {
Stop-Process -Id $ServerProcess.Id -Force -ErrorAction SilentlyContinue
}
} catch { }
}
# Register cleanup on Ctrl+C
try {
[Console]::TreatControlCAsInput = $false
Register-EngineEvent -SourceIdentifier PowerShell.Exiting -Action $cleanup
} catch { }
# Wait for server process
$ServerProcess.WaitForExit()
}
Write-Host "Press any key to close this window..." -ForegroundColor Yellow
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
# Start new PowerShell window with the script
$encodedCommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("& {$startScript} -ScriptDir '$($ScriptDir.Replace("'", "''"))' -VenvDir '$($VenvDir.Replace("'", "''"))' -WebuiEnabled '$WebuiEnabled' -WebuiPort '$WebuiPort' -UseCline '$UseCline'"))
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoExit", "-EncodedCommand", $encodedCommand