-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-dev.command
More file actions
executable file
·299 lines (261 loc) · 7.7 KB
/
Copy pathstart-dev.command
File metadata and controls
executable file
·299 lines (261 loc) · 7.7 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
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT_DIR"
PYTHON_OVERRIDE=""
NO_BROWSER=0
RESTART=0
while [[ $# -gt 0 ]]; do
case "$1" in
--python)
if [[ $# -lt 2 ]]; then
echo "--python requires a path" >&2
exit 1
fi
PYTHON_OVERRIDE="$2"
shift 2
;;
--no-browser)
NO_BROWSER=1
shift
;;
--restart)
RESTART=1
shift
;;
-h|--help)
echo "Usage: ./start-dev.command [--python /path/to/python] [--no-browser] [--restart]"
exit 0
;;
*)
echo "Unknown argument: $1" >&2
exit 1
;;
esac
done
BACKEND_PORT=8000
FRONTEND_START_PORT=5173
FRONTEND_END_PORT=5199
BACKEND_BASE_URL="http://127.0.0.1:$BACKEND_PORT"
FRONTEND_DIR="$ROOT_DIR/frontend"
DEV_LOGS_DIR="$ROOT_DIR/logs/dev"
BACKEND_STDOUT_LOG="$DEV_LOGS_DIR/backend.out.log"
BACKEND_STDERR_LOG="$DEV_LOGS_DIR/backend.err.log"
FRONTEND_STDOUT_LOG="$DEV_LOGS_DIR/frontend.out.log"
FRONTEND_STDERR_LOG="$DEV_LOGS_DIR/frontend.err.log"
PIDS=()
mkdir -p "$DEV_LOGS_DIR"
cleanup() {
for pid in "${PIDS[@]:-}"; do
if kill -0 "$pid" 2>/dev/null; then
stop_process_tree "$pid"
fi
done
}
terminate() {
cleanup
trap - EXIT
exit 130
}
trap cleanup EXIT
trap terminate INT TERM
resolve_python_cmd() {
if [[ -n "$PYTHON_OVERRIDE" ]]; then
if [[ -x "$PYTHON_OVERRIDE" ]]; then
printf '%s\n' "$PYTHON_OVERRIDE"
return 0
fi
echo "Python executable not found or not executable: $PYTHON_OVERRIDE" >&2
exit 1
fi
if command -v conda >/dev/null 2>&1 && conda run -n torch python -c 'import sys' >/dev/null 2>&1; then
printf '%s\n' "conda run -n torch python"
return 0
fi
for candidate in \
"$HOME/miniconda3/envs/torch/bin/python" \
"$HOME/anaconda3/envs/torch/bin/python" \
"$HOME/miniforge3/envs/torch/bin/python" \
"$HOME/mambaforge/envs/torch/bin/python" \
"/opt/anaconda3/envs/torch/bin/python" \
"/opt/miniconda3/envs/torch/bin/python"
do
if [[ -x "$candidate" ]]; then
printf '%s\n' "$candidate"
return 0
fi
done
if command -v python3 >/dev/null 2>&1; then
command -v python3
return 0
fi
if command -v python >/dev/null 2>&1; then
command -v python
return 0
fi
echo "Python executable not found. Install Python or pass --python /path/to/python." >&2
exit 1
}
python_cmd_to_array() {
local cmd="$1"
if [[ "$cmd" == "conda run -n torch python" ]]; then
PYTHON_CMD=(conda run -n torch python)
else
PYTHON_CMD=("$cmd")
fi
}
test_port_free() {
local port="$1"
! lsof -nP -iTCP:"$port" -sTCP:LISTEN >/dev/null 2>&1
}
find_free_port() {
local port
for ((port = FRONTEND_START_PORT; port <= FRONTEND_END_PORT; port++)); do
if test_port_free "$port"; then
printf '%s\n' "$port"
return 0
fi
done
echo "No free port found in range $FRONTEND_START_PORT-$FRONTEND_END_PORT." >&2
exit 1
}
wait_http_ready() {
local url="$1"
local timeout_seconds="${2:-60}"
local deadline=$((SECONDS + timeout_seconds))
while ((SECONDS < deadline)); do
if curl -fsS --max-time 3 "$url" >/dev/null 2>&1; then
return 0
fi
sleep 1
done
return 1
}
test_backend_running() {
local url="$1"
curl -fsS --max-time 3 "$url" 2>/dev/null | grep -q '"ok"[[:space:]]*:[[:space:]]*true'
}
test_backend_token_authenticated() {
local token="$1"
curl -fsS --max-time 3 -H "x-novel-agent-token: $token" "$BACKEND_BASE_URL/roles" 2>/dev/null | grep -q '"ok"[[:space:]]*:[[:space:]]*true'
}
make_api_token() {
if command -v openssl >/dev/null 2>&1; then
openssl rand -base64 32
return 0
fi
LC_ALL=C tr -dc 'A-Za-z0-9' </dev/urandom | head -c 43
printf '\n'
}
stop_process_tree() {
local pid="$1"
local child
while IFS= read -r child; do
[[ -n "$child" ]] && stop_process_tree "$child"
done < <(pgrep -P "$pid" 2>/dev/null || true)
kill "$pid" 2>/dev/null || true
}
is_project_dev_process() {
local pid="$1"
local command_line
command_line="$(ps -p "$pid" -o command= 2>/dev/null || true)"
[[ "$command_line" == *"uvicorn"*"api.routes:app"* ]] || \
[[ "$command_line" == *"vite"*"--host"*"127.0.0.1"* ]] || \
[[ "$command_line" == *"npm"*"run"*"dev"* ]]
}
project_dev_root_pid() {
local pid="$1"
local parent
while [[ -n "$pid" && "$pid" != "0" ]]; do
if is_project_dev_process "$pid"; then
printf '%s\n' "$pid"
return 0
fi
parent="$(ps -p "$pid" -o ppid= 2>/dev/null | tr -d ' ' || true)"
pid="$parent"
done
return 1
}
stop_existing_dev_processes() {
local port pid root_pid seen=" "
for port in "$BACKEND_PORT" $(seq "$FRONTEND_START_PORT" "$FRONTEND_END_PORT"); do
while IFS= read -r pid; do
[[ -z "$pid" ]] && continue
if root_pid="$(project_dev_root_pid "$pid")"; then
if [[ "$seen" != *" $root_pid "* ]]; then
seen="$seen$root_pid "
echo "Stopping existing dev process tree (PID $root_pid) ..."
stop_process_tree "$root_pid"
fi
fi
done < <(lsof -nP -tiTCP:"$port" -sTCP:LISTEN 2>/dev/null || true)
done
}
API_TOKEN="${NOVEL_AGENT_API_TOKEN:-}"
if [[ -z "$API_TOKEN" ]]; then
API_TOKEN="$(make_api_token)"
fi
export NOVEL_AGENT_API_TOKEN="$API_TOKEN"
export VITE_NOVEL_AGENT_API_TOKEN="$API_TOKEN"
export NOVEL_AGENT_ALLOW_PRIVATE_PROVIDER_BASE_URLS="${NOVEL_AGENT_ALLOW_PRIVATE_PROVIDER_BASE_URLS:-true}"
if [[ "$RESTART" -eq 1 ]]; then
stop_existing_dev_processes
sleep 1
fi
PYTHON_CMD_TEXT="$(resolve_python_cmd)"
python_cmd_to_array "$PYTHON_CMD_TEXT"
FRONTEND_PORT="$(find_free_port)"
FRONTEND_URL="http://127.0.0.1:$FRONTEND_PORT"
if test_backend_running "$BACKEND_BASE_URL/status"; then
if ! test_backend_token_authenticated "$API_TOKEN"; then
echo "Backend already running at $BACKEND_BASE_URL but rejected the configured API token. Rerun with --restart, or set NOVEL_AGENT_API_TOKEN to match the existing backend." >&2
exit 1
fi
echo "Backend already running at $BACKEND_BASE_URL"
else
if ! test_port_free "$BACKEND_PORT"; then
echo "Port $BACKEND_PORT is already in use by another process. Stop that process and rerun this launcher." >&2
exit 1
fi
echo "Starting backend at $BACKEND_BASE_URL ..."
"${PYTHON_CMD[@]}" -m uvicorn api.routes:app --reload --host 127.0.0.1 --port "$BACKEND_PORT" >"$BACKEND_STDOUT_LOG" 2>"$BACKEND_STDERR_LOG" &
backend_pid="$!"
PIDS+=("$backend_pid")
if ! wait_http_ready "$BACKEND_BASE_URL/status" 60; then
echo "Backend did not become ready. Check $BACKEND_STDOUT_LOG and $BACKEND_STDERR_LOG" >&2
exit 1
fi
echo "Backend started (PID $backend_pid)"
fi
VITE_JS="$FRONTEND_DIR/node_modules/vite/bin/vite.js"
if [[ ! -d "$FRONTEND_DIR/node_modules" || ! -f "$VITE_JS" ]]; then
echo "Installing frontend dependencies ..."
(cd "$FRONTEND_DIR" && npm install)
fi
echo "Starting frontend at $FRONTEND_URL ..."
(cd "$FRONTEND_DIR" && node "$VITE_JS" --host 127.0.0.1 --port "$FRONTEND_PORT" --strictPort >"$FRONTEND_STDOUT_LOG" 2>"$FRONTEND_STDERR_LOG") &
PIDS+=("$!")
if ! wait_http_ready "$FRONTEND_URL/" 60; then
echo "Frontend did not become ready. Check $FRONTEND_STDOUT_LOG and $FRONTEND_STDERR_LOG" >&2
exit 1
fi
if [[ "$NO_BROWSER" -eq 0 ]]; then
open "$FRONTEND_URL"
fi
echo "Backend: $BACKEND_BASE_URL"
echo "Frontend: $FRONTEND_URL"
echo "Backend logs: $BACKEND_STDOUT_LOG"
echo " $BACKEND_STDERR_LOG"
echo "Frontend logs: $FRONTEND_STDOUT_LOG"
echo " $FRONTEND_STDERR_LOG"
echo "Press Ctrl+C or close this terminal to stop the backend and frontend."
while true; do
for pid in "${PIDS[@]:-}"; do
if ! kill -0 "$pid" 2>/dev/null; then
cleanup
echo "A managed process stopped unexpectedly." >&2
exit 1
fi
done
sleep 1
done