forked from aditya-adiga/live_conversational_threads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.command
More file actions
executable file
·434 lines (358 loc) · 12.4 KB
/
start.command
File metadata and controls
executable file
·434 lines (358 loc) · 12.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash
set -euo pipefail
cd "$(dirname "$0")"
ROOT_DIR="$PWD"
RUN_DIR="$ROOT_DIR/.run"
mkdir -p "$RUN_DIR"
PID_BACKEND="$RUN_DIR/backend.pid"
PID_FRONTEND="$RUN_DIR/frontend.pid"
VENV_PY="$ROOT_DIR/.venv/bin/python3"
ENV_FILE="$ROOT_DIR/lct_python_backend/.env"
PG_DATA="$ROOT_DIR/.postgres_data"
PG_LOG="$ROOT_DIR/.postgres.log"
PG_PORT="${PG_PORT:-5433}"
BACKEND_PORT="${BACKEND_PORT:-43180}"
FRONTEND_PORT="${FRONTEND_PORT:-43173}"
DB_URL_DEFAULT="postgresql://lct_user:lct_password@localhost:${PG_PORT}/lct_dev"
POSTGRES_BIN_ARM="/opt/homebrew/opt/postgresql@15/bin"
POSTGRES_BIN_INTEL="/usr/local/opt/postgresql@15/bin"
BACKEND_HEALTH_URL="http://localhost:${BACKEND_PORT}/api/import/health"
FRONTEND_HEALTH_URL="http://localhost:${FRONTEND_PORT}"
PARAKEET_HEALTH_URL="${PARAKEET_HEALTH_URL:-http://localhost:5092/health}"
WHISPER_HTTP_HEALTH_URL="${WHISPER_HTTP_HEALTH_URL:-}"
WHISPERX_HTTP_HEALTH_URL="${WHISPERX_HTTP_HEALTH_URL:-}"
STT_AUTOSTART="${STT_AUTOSTART:-1}"
STT_AUTOSTART_PROVIDER="${STT_AUTOSTART_PROVIDER:-parakeet}"
SHARED_PARAKEET_DIR="${SHARED_PARAKEET_DIR:-$ROOT_DIR/../parakeet-tdt-0.6b-v3-fastapi-openai}"
STT_START_TIMEOUT_S="${STT_START_TIMEOUT_S:-120}"
BACKEND_PID=""
FRONTEND_PID=""
CLEANED_UP=0
log() {
printf '[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*"
}
fail() {
log "ERROR: $*"
exit 1
}
write_port_files() {
printf '%s\n' "$BACKEND_PORT" > "$ROOT_DIR/.backend-port"
printf '%s\n' "$FRONTEND_PORT" > "$ROOT_DIR/.frontend-port"
log "Resolved local ports: backend=${BACKEND_PORT}, frontend=${FRONTEND_PORT}"
}
ensure_postgres_path() {
if [ -d "$POSTGRES_BIN_ARM" ]; then
export PATH="$POSTGRES_BIN_ARM:$PATH"
elif [ -d "$POSTGRES_BIN_INTEL" ]; then
export PATH="$POSTGRES_BIN_INTEL:$PATH"
fi
command -v pg_ctl >/dev/null 2>&1 || fail "pg_ctl not found. Run ./setup-once.command first."
}
kill_pid_gracefully() {
local pid="$1"
local label="$2"
if ! kill -0 "$pid" >/dev/null 2>&1; then
return
fi
log "Stopping ${label} (pid ${pid})..."
kill "$pid" >/dev/null 2>&1 || true
for _ in $(seq 1 20); do
if ! kill -0 "$pid" >/dev/null 2>&1; then
return
fi
sleep 0.2
done
if kill -0 "$pid" >/dev/null 2>&1; then
log "Force stopping ${label} (pid ${pid})..."
kill -9 "$pid" >/dev/null 2>&1 || true
fi
}
stop_pidfile_process() {
local pid_file="$1"
local label="$2"
if [ ! -f "$pid_file" ]; then
return 0
fi
local pid
pid="$(cat "$pid_file" 2>/dev/null || true)"
if [ -n "$pid" ]; then
kill_pid_gracefully "$pid" "$label"
fi
rm -f "$pid_file"
}
cleanup_port() {
local port="$1"
local label="$2"
local pid
pid="$(lsof -nP -iTCP:"$port" -sTCP:LISTEN -t 2>/dev/null | head -n 1 || true)"
if [ -z "$pid" ]; then
return 0
fi
local cmd
cmd="$(ps -p "$pid" -o command= 2>/dev/null || true)"
local cwd
cwd="$(lsof -a -p "$pid" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p' | head -n 1 || true)"
# Treat listeners started from this repo as safe to clean up even when
# the command line does not include $ROOT_DIR (common with uvicorn --reload).
if [[ "$cmd" == *"$ROOT_DIR"* ]] || [[ "$cwd" == "$ROOT_DIR"* ]]; then
kill_pid_gracefully "$pid" "$label on :$port"
else
fail "Port $port is already in use by an external process: ${cmd:-unknown}"
fi
}
load_env_file() {
if [ ! -f "$ENV_FILE" ]; then
fail "Missing $ENV_FILE. Run ./setup-once.command first."
fi
set -a
# shellcheck disable=SC1090
source "$ENV_FILE"
set +a
export DATABASE_URL="${DATABASE_URL:-$DB_URL_DEFAULT}"
export LOCAL_LLM_BASE_URL="${LOCAL_LLM_BASE_URL:-http://100.81.65.74:1234}"
export DEFAULT_LLM_MODE="${DEFAULT_LLM_MODE:-local}"
}
ensure_prereqs() {
[ -x "$VENV_PY" ] || fail "Missing Python venv at .venv. Run ./setup-once.command first."
[ -d "$PG_DATA" ] || fail "Missing .postgres_data. Run ./setup-once.command first."
[ -d "$ROOT_DIR/lct_app/node_modules" ] || fail "Missing frontend dependencies. Run ./setup-once.command first."
command -v npm >/dev/null 2>&1 || fail "npm is required but not installed."
command -v curl >/dev/null 2>&1 || fail "curl is required but not installed."
command -v lsof >/dev/null 2>&1 || fail "lsof is required but not installed."
}
start_postgres() {
if ! pg_ctl -D "$PG_DATA" status >/dev/null 2>&1; then
log "Starting local PostgreSQL on port ${PG_PORT}..."
pg_ctl -D "$PG_DATA" -l "$PG_LOG" -o "-p ${PG_PORT}" start >/dev/null
sleep 2
fi
pg_ctl -D "$PG_DATA" status >/dev/null 2>&1 || fail "PostgreSQL failed to start. Check $PG_LOG"
}
run_migrations() {
if [ "${SKIP_MIGRATIONS:-0}" = "1" ]; then
log "Skipping migrations (SKIP_MIGRATIONS=1)."
return 0
fi
log "Running migrations (alembic upgrade head)..."
if (
cd "$ROOT_DIR/lct_python_backend"
DATABASE_URL="$DATABASE_URL" "$VENV_PY" -m alembic upgrade head
); then
return 0
fi
fail "Migration failed. If schema is already initialized, run with SKIP_MIGRATIONS=1."
}
wait_for_http() {
local name="$1"
local url="$2"
local timeout_s="$3"
for _ in $(seq 1 "$timeout_s"); do
if curl -fsS --max-time 2 "$url" >/dev/null 2>&1; then
log "$name is ready: $url"
return 0
fi
sleep 1
done
return 1
}
start_shared_parakeet_if_needed() {
if curl -fsS --max-time 2 "$PARAKEET_HEALTH_URL" >/dev/null 2>&1; then
log "Shared Parakeet STT already healthy: $PARAKEET_HEALTH_URL"
return 0
fi
if ! command -v docker >/dev/null 2>&1; then
log "NOTE: docker is unavailable; skipping shared Parakeet autostart."
return 0
fi
if ! docker info >/dev/null 2>&1; then
log "NOTE: Docker daemon is not reachable; skipping shared Parakeet autostart."
return 0
fi
if [ ! -f "$SHARED_PARAKEET_DIR/docker-compose.yml" ]; then
log "NOTE: Shared Parakeet repo missing at $SHARED_PARAKEET_DIR; skipping autostart."
return 0
fi
log "Starting shared Parakeet STT (parakeet-cpu) from $SHARED_PARAKEET_DIR..."
if ! (
cd "$SHARED_PARAKEET_DIR"
docker compose up -d parakeet-cpu >/dev/null
); then
log "NOTE: Failed to start Parakeet container."
return 0
fi
if wait_for_http "Parakeet STT" "$PARAKEET_HEALTH_URL" "$STT_START_TIMEOUT_S"; then
log "Parakeet STT is ready and reuses Docker volume 'parakeet-models' (shared cache)."
else
log "NOTE: Parakeet container started but health check failed at $PARAKEET_HEALTH_URL"
fi
}
start_optional_stt_services() {
if [ "$STT_AUTOSTART" != "1" ]; then
return 0
fi
case "$STT_AUTOSTART_PROVIDER" in
parakeet)
start_shared_parakeet_if_needed
;;
none)
log "STT autostart requested with provider=none; skipping."
;;
*)
log "NOTE: Unknown STT_AUTOSTART_PROVIDER='$STT_AUTOSTART_PROVIDER'; skipping STT autostart."
;;
esac
}
resolve_stt_urls_from_backend() {
# Read provider URLs from the backend's live settings so health checks
# match what the app will actually use (instead of hardcoded defaults).
local settings_url="http://localhost:${BACKEND_PORT}/api/settings/stt"
local json
json="$(curl -fsS --max-time 3 "$settings_url" 2>/dev/null || true)"
if [ -z "$json" ]; then
return 0
fi
# Extract provider_http_urls using Python (always available via venv)
eval "$("$VENV_PY" -c "
import json, sys
try:
d = json.loads(sys.argv[1])
urls = d.get('provider_http_urls', {})
for name in ('parakeet', 'whisper', 'whisperx'):
url = urls.get(name, '')
if url:
# Derive health URL: strip path, append /health
from urllib.parse import urlparse
p = urlparse(url)
print(f'{name.upper()}_URL={p.scheme}://{p.netloc}/health')
except Exception:
pass
" "$json" 2>/dev/null || true)"
if [ -n "${PARAKEET_URL:-}" ]; then
PARAKEET_HEALTH_URL="$PARAKEET_URL"
fi
if [ -n "${WHISPER_URL:-}" ]; then
WHISPER_HTTP_HEALTH_URL="$WHISPER_URL"
fi
if [ -n "${WHISPERX_URL:-}" ]; then
WHISPERX_HTTP_HEALTH_URL="$WHISPERX_URL"
fi
return 0
}
report_known_stt_endpoints() {
if lsof -nP -iTCP:43001 -sTCP:LISTEN >/dev/null 2>&1; then
log "STT WS status (legacy optional): listener detected on ws://localhost:43001/stream"
else
log "STT WS status (legacy optional): no listener on ws://localhost:43001/stream"
fi
if curl -fsS --max-time 2 "$PARAKEET_HEALTH_URL" >/dev/null 2>&1; then
log "STT HTTP status: Parakeet healthy at $PARAKEET_HEALTH_URL"
else
log "STT HTTP status: Parakeet unreachable at $PARAKEET_HEALTH_URL"
fi
if [ -n "$WHISPER_HTTP_HEALTH_URL" ]; then
if curl -fsS --max-time 2 "$WHISPER_HTTP_HEALTH_URL" >/dev/null 2>&1; then
log "STT HTTP status: Whisper healthy at $WHISPER_HTTP_HEALTH_URL"
else
log "STT HTTP status: Whisper unreachable at $WHISPER_HTTP_HEALTH_URL"
fi
fi
if [ -n "$WHISPERX_HTTP_HEALTH_URL" ]; then
if curl -fsS --max-time 2 "$WHISPERX_HTTP_HEALTH_URL" >/dev/null 2>&1; then
log "STT HTTP status: WhisperX healthy at $WHISPERX_HTTP_HEALTH_URL"
else
log "STT HTTP status: WhisperX unreachable at $WHISPERX_HTTP_HEALTH_URL"
fi
fi
}
report_llm_endpoint() {
local models_url="${LOCAL_LLM_BASE_URL%/}/v1/models"
if curl -fsS --max-time 3 "$models_url" >/dev/null 2>&1; then
log "LLM endpoint status: reachable at $models_url (mode=${DEFAULT_LLM_MODE})"
else
log "LLM endpoint status: unreachable at $models_url (mode=${DEFAULT_LLM_MODE})"
fi
}
start_backend() {
log "Starting backend on port ${BACKEND_PORT}..."
(
export DATABASE_URL="$DATABASE_URL"
export BACKEND_PORT="$BACKEND_PORT"
export FRONTEND_PORT="$FRONTEND_PORT"
export FRONTEND_URL="${FRONTEND_URL:-http://localhost:${FRONTEND_PORT}}"
export PYTHONPATH="$ROOT_DIR:${PYTHONPATH:-}"
exec "$VENV_PY" -m uvicorn lct_python_backend.backend:lct_app --host 0.0.0.0 --port "$BACKEND_PORT" --reload
) > >(sed -u 's/^/[backend] /') 2>&1 &
BACKEND_PID="$!"
echo "$BACKEND_PID" > "$PID_BACKEND"
wait_for_http "Backend" "$BACKEND_HEALTH_URL" 60 || fail "Backend failed health check at $BACKEND_HEALTH_URL"
}
start_frontend() {
log "Starting frontend on port ${FRONTEND_PORT}..."
(
cd "$ROOT_DIR/lct_app"
export VITE_BACKEND_API_URL="http://localhost:${BACKEND_PORT}"
export VITE_BACKEND_PORT="$BACKEND_PORT"
exec npm run dev -- --host 0.0.0.0 --port "$FRONTEND_PORT" --strictPort
) > >(sed -u 's/^/[frontend] /') 2>&1 &
FRONTEND_PID="$!"
echo "$FRONTEND_PID" > "$PID_FRONTEND"
wait_for_http "Frontend" "$FRONTEND_HEALTH_URL" 60 || fail "Frontend failed health check at $FRONTEND_HEALTH_URL"
}
stt_readiness_hint() {
if curl -fsS --max-time 2 "$PARAKEET_HEALTH_URL" >/dev/null 2>&1; then
log "Detected local STT HTTP provider (Parakeet) at $PARAKEET_HEALTH_URL"
return
fi
if [ -n "$WHISPER_HTTP_HEALTH_URL" ] && curl -fsS --max-time 2 "$WHISPER_HTTP_HEALTH_URL" >/dev/null 2>&1; then
log "Detected local STT HTTP provider (Whisper) at $WHISPER_HTTP_HEALTH_URL"
return
fi
if [ -n "$WHISPERX_HTTP_HEALTH_URL" ] && curl -fsS --max-time 2 "$WHISPERX_HTTP_HEALTH_URL" >/dev/null 2>&1; then
log "Detected local STT HTTP provider (WhisperX) at $WHISPERX_HTTP_HEALTH_URL"
return
fi
log "NOTE: No reachable local STT HTTP provider detected."
log " Backend-owned transcription expects a reachable provider HTTP URL in Settings."
}
cleanup_on_exit() {
if [ "$CLEANED_UP" -eq 1 ]; then
return
fi
CLEANED_UP=1
local code="$?"
log "Shutting down local services started by start.command..."
if [ -n "$FRONTEND_PID" ]; then
kill_pid_gracefully "$FRONTEND_PID" "frontend"
fi
stop_pidfile_process "$PID_FRONTEND" "frontend"
if [ -n "$BACKEND_PID" ]; then
kill_pid_gracefully "$BACKEND_PID" "backend"
fi
stop_pidfile_process "$PID_BACKEND" "backend"
log "Shutdown complete."
exit "$code"
}
trap cleanup_on_exit INT TERM EXIT
log "Preparing clean start for Live Conversational Threads..."
ensure_postgres_path
load_env_file
ensure_prereqs
stop_pidfile_process "$PID_BACKEND" "backend"
stop_pidfile_process "$PID_FRONTEND" "frontend"
cleanup_port "$BACKEND_PORT" "backend"
cleanup_port "$FRONTEND_PORT" "frontend"
write_port_files
start_postgres
run_migrations
start_backend
start_frontend
start_optional_stt_services
resolve_stt_urls_from_backend
report_known_stt_endpoints
report_llm_endpoint
stt_readiness_hint
log "All services are up."
log "Backend: http://localhost:${BACKEND_PORT}"
log "Frontend: http://localhost:${FRONTEND_PORT}"
log "Press Ctrl+C to stop both services."
wait "$BACKEND_PID" "$FRONTEND_PID"