start_local_stack.ps1
8.93 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
[CmdletBinding()]
param(
[string]$EnvFile,
[switch]$Bootstrap,
[switch]$BuildFrontend,
[switch]$SkipPlaywright,
[string]$FrontendHost = "127.0.0.1",
[int]$FrontendPort = 9527
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$RepoRoot = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path
$BackendPort = 5000
$FrontendUrl = "http://${FrontendHost}:${FrontendPort}"
$BackendUrl = "http://127.0.0.1:${BackendPort}"
Set-Location $RepoRoot
function Write-Step {
param([string]$Message)
Write-Host "[BettaFish] $Message" -ForegroundColor Cyan
}
function Get-PythonCommand {
$python = Get-Command python -ErrorAction SilentlyContinue
if ($null -eq $python) {
throw "python was not found in PATH. Install Python and try again."
}
return $python.Source
}
function Get-NpmCommand {
$npm = Get-Command npm -ErrorAction SilentlyContinue
if ($null -eq $npm) {
throw "npm was not found in PATH. Install Node.js and try again."
}
return $npm.Source
}
function Invoke-External {
param(
[string]$Command,
[string[]]$Arguments
)
& $Command @Arguments
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
$rendered = @($Command) + $Arguments
throw "Command failed with exit code ${exitCode}: $($rendered -join ' ')"
}
}
function Resolve-EnvFilePath {
param([string]$ExplicitEnvFile)
if ($ExplicitEnvFile) {
$resolved = Resolve-Path -LiteralPath $ExplicitEnvFile -ErrorAction Stop
return $resolved.Path
}
$localEnv = Join-Path $RepoRoot ".env.local"
$localExample = Join-Path $RepoRoot ".env.local.example"
$fallbackEnv = Join-Path $RepoRoot ".env"
if (Test-Path -LiteralPath $localEnv) {
return $localEnv
}
if (Test-Path -LiteralPath $localExample) {
Copy-Item -LiteralPath $localExample -Destination $localEnv
Write-Step "Created .env.local from .env.local.example."
Write-Warning "Review .env.local before using research flows that depend on real model or database settings."
return $localEnv
}
if (Test-Path -LiteralPath $fallbackEnv) {
Write-Warning ".env.local was not found. Falling back to .env."
return $fallbackEnv
}
throw "No env file was found. Expected .env.local, .env.local.example, or .env."
}
function Test-LocalRuntimeDependencies {
param([string]$PythonCommand)
$probe = @(
"-c",
"import flask, flask_socketio, streamlit, loguru, pydantic_settings, requests"
)
& $PythonCommand @probe *> $null
return ($LASTEXITCODE -eq 0)
}
function Ensure-Bootstrap {
param([string]$PythonCommand)
$nodeModulesPath = Join-Path $RepoRoot "apps\web_ui\node_modules"
$needFrontendInstall = -not (Test-Path -LiteralPath $nodeModulesPath)
$needBootstrap = $Bootstrap.IsPresent -or $needFrontendInstall -or -not (Test-LocalRuntimeDependencies -PythonCommand $PythonCommand)
if (-not $needBootstrap) {
return
}
Write-Step "Running bootstrap_local because dependencies or frontend packages are missing."
$arguments = @(
"-X", "utf8",
"-m", "scripts.dev.bootstrap_local",
"--skip-pip-tools-upgrade"
)
if (-not $needFrontendInstall) {
$arguments += "--skip-frontend"
}
if ($SkipPlaywright) {
$arguments += "--skip-playwright"
}
Invoke-External -Command $PythonCommand -Arguments $arguments
}
function Ensure-PostgreSqlService {
$services = @(
Get-Service | Where-Object {
$_.Name -match "postgres" -or $_.DisplayName -match "PostgreSQL"
}
)
if ($services.Count -eq 0) {
return
}
foreach ($service in $services) {
if ($service.Status -eq "Running") {
continue
}
try {
Write-Step "Trying to start PostgreSQL service $($service.Name)."
Start-Service -Name $service.Name
}
catch {
Write-Warning "Failed to start PostgreSQL service $($service.Name): $($_.Exception.Message)"
}
}
}
function Get-ListeningConnections {
param([int]$Port)
try {
return @(Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction Stop)
}
catch {
return @()
}
}
function Get-ProcessCommandLine {
param([int]$ProcessId)
try {
return (Get-CimInstance Win32_Process -Filter "ProcessId = $ProcessId").CommandLine
}
catch {
return ""
}
}
function Test-ManagedBackend {
param([string]$CommandLine)
return (
$CommandLine -match "apps\.web_api" -or
$CommandLine -match "scripts\.dev\.start_local" -or
$CommandLine -match "app\.py"
)
}
function Test-ManagedFrontend {
param([string]$CommandLine)
return (
$CommandLine -match "vite" -or
$CommandLine -match "apps\\web_ui" -or
$CommandLine -match "apps/web_ui"
)
}
function Get-PortOwner {
param(
[int]$Port,
[scriptblock]$Matcher
)
$listeners = @(Get-ListeningConnections -Port $Port)
if ($listeners.Count -eq 0) {
return $null
}
$owner = $listeners[0].OwningProcess
$commandLine = Get-ProcessCommandLine -ProcessId $owner
return [pscustomobject]@{
Port = $Port
Pid = $owner
CommandLine = $commandLine
Managed = (& $Matcher $commandLine)
}
}
try {
Write-Step "Repo root: $RepoRoot"
$pythonCommand = Get-PythonCommand
$npmCommand = Get-NpmCommand
$resolvedEnvFile = Resolve-EnvFilePath -ExplicitEnvFile $EnvFile
$frontendMode = if ($BuildFrontend.IsPresent) { "build" } else { "dev" }
Write-Step "Using env file: $resolvedEnvFile"
Write-Step "Frontend mode: $frontendMode"
Ensure-Bootstrap -PythonCommand $pythonCommand
Ensure-PostgreSqlService
$backendOwner = Get-PortOwner -Port $BackendPort -Matcher ${function:Test-ManagedBackend}
if ($null -ne $backendOwner -and -not $backendOwner.Managed) {
throw "Port $BackendPort is already in use by another process. PID=$($backendOwner.Pid) CommandLine=$($backendOwner.CommandLine)"
}
if ($frontendMode -eq "build") {
if ($null -ne $backendOwner) {
Write-Step "BettaFish backend is already running."
Write-Host "Frontend: $BackendUrl" -ForegroundColor Green
Write-Host "Backend API: $BackendUrl" -ForegroundColor Green
Write-Host "PID: $($backendOwner.Pid)" -ForegroundColor Green
exit 0
}
$startArguments = @(
"-X", "utf8",
"-m", "scripts.dev.start_local",
"--env-file", $resolvedEnvFile,
"--frontend-mode", "build"
)
Write-Step "Starting backend with deployment-compatible built frontend assets."
Invoke-External -Command $pythonCommand -Arguments $startArguments
exit 0
}
$frontendOwner = Get-PortOwner -Port $FrontendPort -Matcher ${function:Test-ManagedFrontend}
if ($null -ne $frontendOwner -and -not $frontendOwner.Managed) {
throw "Port $FrontendPort is already in use by another process. PID=$($frontendOwner.Pid) CommandLine=$($frontendOwner.CommandLine)"
}
if (($null -ne $backendOwner) -and ($null -ne $frontendOwner)) {
Write-Step "BettaFish local dev stack is already running."
Write-Host "Frontend: $FrontendUrl" -ForegroundColor Green
Write-Host "Backend API: $BackendUrl" -ForegroundColor Green
Write-Host "Backend PID: $($backendOwner.Pid)" -ForegroundColor Green
Write-Host "Frontend PID: $($frontendOwner.Pid)" -ForegroundColor Green
exit 0
}
if (($null -ne $backendOwner) -and ($null -eq $frontendOwner)) {
Write-Step "Backend is already running. Starting only the Vite dev server."
$frontendArguments = @(
"--prefix", "apps/web_ui",
"run", "dev",
"--",
"--host", $FrontendHost,
"--port", [string]$FrontendPort,
"--strictPort"
)
Invoke-External -Command $npmCommand -Arguments $frontendArguments
exit 0
}
if (($null -eq $backendOwner) -and ($null -ne $frontendOwner)) {
Write-Step "Frontend dev server is already running. Starting only the backend API."
$backendArguments = @(
"-X", "utf8",
"-m", "scripts.dev.start_local",
"--env-file", $resolvedEnvFile,
"--frontend-mode", "skip"
)
Invoke-External -Command $pythonCommand -Arguments $backendArguments
exit 0
}
$startArguments = @(
"-X", "utf8",
"-m", "scripts.dev.start_local",
"--env-file", $resolvedEnvFile,
"--frontend-mode", "dev",
"--frontend-host", $FrontendHost,
"--frontend-port", [string]$FrontendPort
)
Write-Step "Starting BettaFish local dev stack."
Invoke-External -Command $pythonCommand -Arguments $startArguments
}
catch {
Write-Error $_.Exception.Message
exit 1
}