forked from the-crypt-keeper/tldw
-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathquick-launch.ps1
More file actions
286 lines (241 loc) · 8.83 KB
/
Copy pathquick-launch.ps1
File metadata and controls
286 lines (241 loc) · 8.83 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
# Quick-launch local single-user tldw_server without Docker or Make.
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[ValidateSet("api", "webui", "all", "help")]
[string]$Mode = "all",
[string]$HostAddress,
[int]$Port,
[int]$WebUIPort,
[switch]$SkipInstall,
[switch]$ForceInstall
)
$ErrorActionPreference = "Stop"
function Invoke-CheckedNative {
param(
[string]$FilePath,
[string[]]$Arguments
)
& $FilePath @Arguments
if ($LASTEXITCODE -ne 0) {
throw "[quick-launch] Command failed with exit code $LASTEXITCODE`: $FilePath $($Arguments -join ' ')"
}
}
function Resolve-QuickLaunchPort {
param(
[bool]$HasPortParameter,
[int]$PortParameter
)
if ($HasPortParameter) {
return $PortParameter
}
if ($env:TLDW_API_PORT) {
if ($env:TLDW_API_PORT -match '^\d+$') {
return [int]$env:TLDW_API_PORT
}
Write-Warning "[quick-launch] Ignoring invalid TLDW_API_PORT='$($env:TLDW_API_PORT)'; checking TLDW_PORT."
}
if ($env:TLDW_PORT) {
if ($env:TLDW_PORT -match '^\d+$') {
return [int]$env:TLDW_PORT
}
Write-Warning "[quick-launch] Ignoring invalid TLDW_PORT='$($env:TLDW_PORT)'; using 8000."
}
return 8000
}
function Resolve-QuickLaunchWebUIPort {
param(
[bool]$HasPortParameter,
[int]$PortParameter
)
if ($HasPortParameter) {
return $PortParameter
}
if ($env:TLDW_WEBUI_PORT) {
if ($env:TLDW_WEBUI_PORT -match '^\d+$') {
return [int]$env:TLDW_WEBUI_PORT
}
Write-Warning "[quick-launch] Ignoring invalid TLDW_WEBUI_PORT='$($env:TLDW_WEBUI_PORT)'; using 8080."
}
return 8080
}
function Resolve-QuickLaunchApiUrl {
if ($env:NEXT_PUBLIC_API_URL) {
return $env:NEXT_PUBLIC_API_URL
}
$ApiUrlHost = $HostAddress
if ($ApiUrlHost -eq "0.0.0.0") {
$ApiUrlHost = "127.0.0.1"
Write-Host "[quick-launch] API is bound to 0.0.0.0; using 127.0.0.1 for local browser requests."
Write-Host "[quick-launch] Set NEXT_PUBLIC_API_URL to your LAN URL for non-local browser clients."
}
return "http://$ApiUrlHost`:$Port"
}
function Resolve-QuickLaunchApiStartDelay {
if ($env:TLDW_API_START_DELAY) {
if ($env:TLDW_API_START_DELAY -match '^\d+$') {
return [int]$env:TLDW_API_START_DELAY
}
Write-Warning "[quick-launch] Ignoring invalid TLDW_API_START_DELAY='$($env:TLDW_API_START_DELAY)'; using 2."
}
return 2
}
function Show-Usage {
Write-Host "Usage: .\quick-launch.ps1 [api|webui|all] [-HostAddress 127.0.0.1] [-Port 8000] [-WebUIPort 8080]"
Write-Host ""
Write-Host "Modes:"
Write-Host " api Start the FastAPI backend only on http://$HostAddress`:$Port"
Write-Host " webui Start the Next.js WebUI only on http://127.0.0.1:$WebUIPort"
Write-Host " all Start the backend and WebUI (default)"
Write-Host ""
Write-Host "Environment:"
Write-Host " TLDW_PYTHON Python executable for venv creation"
Write-Host " TLDW_VENV_DIR Virtualenv directory (default: .venv)"
Write-Host " TLDW_ENV_FILE Env file path"
Write-Host " TLDW_HOST Backend host (default: 127.0.0.1)"
Write-Host " TLDW_API_PORT Backend port (default: TLDW_PORT or 8000)"
Write-Host " TLDW_PORT Legacy backend port override"
Write-Host " TLDW_WEBUI_PORT WebUI port (default: 8080)"
Write-Host " NEXT_PUBLIC_API_URL Override WebUI API URL"
}
function Test-Bun {
if (-not (Get-Command bun -ErrorAction SilentlyContinue)) {
throw "[quick-launch] Bun is required to launch the WebUI but was not found in PATH. Install Bun from https://bun.sh/docs/installation, then rerun this launcher."
}
}
function Test-WebUIDirectory {
if (-not (Test-Path $WebUIDir)) {
throw "[quick-launch] WebUI directory not found: $WebUIDir. Update your checkout before launching the WebUI."
}
}
function Initialize-ApiEnvironment {
Invoke-CheckedNative -FilePath $PythonExe -Arguments ($PythonArgs + @("-c", "import sys; raise SystemExit(0 if sys.version_info >= (3, 10) else 1)"))
$VenvCreated = $false
if (-not (Test-Path $VenvPython)) {
Write-Host "[quick-launch] Creating virtualenv at $VenvDir"
Invoke-CheckedNative -FilePath $PythonExe -Arguments ($PythonArgs + @("-m", "venv", $VenvDir))
$VenvCreated = $true
}
if (
-not $SkipInstall `
-and $env:TLDW_SKIP_INSTALL -ne "1" `
-and ($ForceInstall -or $env:TLDW_FORCE_INSTALL -eq "1" -or $VenvCreated -or -not (Test-Path $InstallMarker))
) {
Write-Host "[quick-launch] Installing/updating local Python dependencies..."
Invoke-CheckedNative -FilePath $VenvPython -Arguments @("-m", "pip", "install", "--upgrade", "pip", "setuptools", "wheel")
Invoke-CheckedNative -FilePath $VenvPython -Arguments @("-m", "pip", "install", "-e", ".")
New-Item -Path $InstallMarker -ItemType File -Force | Out-Null
} elseif ($SkipInstall -or $env:TLDW_SKIP_INSTALL -eq "1") {
Write-Host "[quick-launch] Skipping dependency install"
} else {
Write-Host "[quick-launch] Dependency setup already completed; set TLDW_FORCE_INSTALL=1 or pass -ForceInstall to reinstall/update."
}
Write-Host "[quick-launch] Configuring local single-user profile..."
Invoke-CheckedNative -FilePath $VenvPython -Arguments @(
"-m",
"tldw_Server_API.cli.wizard.cli",
"init",
"--profile",
"local-single",
"--env-file",
$EnvFile,
"--default",
"--yes"
)
}
function run_api {
Write-Host "=== tldw_server quick launch: API ==="
Write-Host ""
Initialize-ApiEnvironment
Write-Host ""
Write-Host "[quick-launch] Starting API at http://$HostAddress`:$Port"
Write-Host "[quick-launch] Docs: http://$HostAddress`:$Port/docs"
Write-Host "[quick-launch] Health: http://$HostAddress`:$Port/health"
Write-Host ""
$env:TLDW_ENV_FILE = $EnvFile
Invoke-CheckedNative -FilePath $VenvPython -Arguments @(
"-m",
"uvicorn",
"tldw_Server_API.app.main:app",
"--host",
$HostAddress,
"--port",
"$Port"
)
}
function run_webui {
Test-Bun
Test-WebUIDirectory
$env:NEXT_PUBLIC_API_URL = Resolve-QuickLaunchApiUrl
Write-Host ""
Write-Host "[quick-launch] Starting WebUI at http://127.0.0.1:$WebUIPort"
Write-Host "[quick-launch] Using API URL: $($env:NEXT_PUBLIC_API_URL)"
Write-Host ""
Set-Location $WebUIDir
Invoke-CheckedNative -FilePath "bun" -Arguments @("run", "dev", "--", "-p", "$WebUIPort")
}
function run_all {
Initialize-ApiEnvironment
Write-Host "=== tldw_server quick launch: API + WebUI ==="
Write-Host "[quick-launch] Starting API in a new PowerShell window at http://$HostAddress`:$Port"
Write-Host "[quick-launch] Starting WebUI in this window at http://127.0.0.1:$WebUIPort"
$apiArgs = @(
"-NoExit",
"-ExecutionPolicy",
"Bypass",
"-File",
$PSCommandPath,
"api",
"-HostAddress",
$HostAddress,
"-Port",
"$Port",
"-WebUIPort",
"$WebUIPort",
"-SkipInstall"
)
$PsExe = if ($PSVersionTable.PSEdition -eq "Core") { "pwsh" } else { "powershell" }
Start-Process -FilePath $PsExe -ArgumentList $apiArgs | Out-Null
$ApiStartDelay = Resolve-QuickLaunchApiStartDelay
Start-Sleep -Seconds $ApiStartDelay
run_webui
}
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $ScriptDir
if (-not $HostAddress) {
$HostAddress = if ($env:TLDW_HOST) { $env:TLDW_HOST } else { "127.0.0.1" }
}
$HasPortParameter = $PSBoundParameters.ContainsKey("Port")
$Port = Resolve-QuickLaunchPort -HasPortParameter $HasPortParameter -PortParameter $Port
$HasWebUIPortParameter = $PSBoundParameters.ContainsKey("WebUIPort")
$WebUIPort = Resolve-QuickLaunchWebUIPort -HasPortParameter $HasWebUIPortParameter -PortParameter $WebUIPort
$VenvDir = if ($env:TLDW_VENV_DIR) { $env:TLDW_VENV_DIR } else { ".venv" }
$VenvPython = Join-Path $VenvDir "Scripts/python.exe"
$InstallMarker = Join-Path $VenvDir ".initialized"
$EnvFile = if ($env:TLDW_ENV_FILE) { $env:TLDW_ENV_FILE } else { "tldw_Server_API/Config_Files/.env" }
$WebUIDir = Join-Path $ScriptDir "apps/tldw-frontend"
if ($env:TLDW_PYTHON) {
$PythonExe = $env:TLDW_PYTHON
$PythonArgs = @()
} elseif (Get-Command py -ErrorAction SilentlyContinue) {
$PythonExe = "py"
$PythonArgs = @("-3")
} else {
$PythonExe = "python"
$PythonArgs = @()
}
switch ($Mode) {
"api" {
run_api
}
"webui" {
Write-Host "=== tldw_server quick launch: WebUI ==="
run_webui
}
"all" {
run_all
}
"help" {
Show-Usage
}
}