-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·200 lines (184 loc) · 7.12 KB
/
start.sh
File metadata and controls
executable file
·200 lines (184 loc) · 7.12 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
#!/usr/bin/env bash
# Parrot Assistant — Start all services
# Usage: ./start.sh [all|stt|tts|bridge|stop|end|restart|status]
# (memory is in-process via mem0; animation is CSV-driven via fbx_functions —
# neither has a standalone service to start.)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VENV="$SCRIPT_DIR/.venv"
PIDS_DIR="$SCRIPT_DIR/.pids"
LOG_DIR="$SCRIPT_DIR/logs"
CONFIG="$SCRIPT_DIR/config.yaml"
mkdir -p "$PIDS_DIR" "$LOG_DIR"
# External API keys — exported into the environment for services that spawn
# below, so tools can read them via os.getenv without being committed.
# Fill in your own key and uncomment (or export it from your shell rc).
# export POLYGON_API_KEY="your-key-here"
# Read network addresses from config.yaml (single source of truth).
EXTERNAL_HOST=$(python3 -c "
import yaml, pathlib
c = yaml.safe_load(pathlib.Path('$CONFIG').read_text())
print(c.get('network',{}).get('external_host','127.0.0.1'))
" 2>/dev/null || echo "127.0.0.1")
INTERNAL_HOST=$(python3 -c "
import yaml, pathlib
c = yaml.safe_load(pathlib.Path('$CONFIG').read_text())
print(c.get('network',{}).get('internal_host','127.0.0.1'))
" 2>/dev/null || echo "127.0.0.1")
activate_venv() {
if [ ! -d "$VENV" ]; then
echo "ERROR: Virtual environment not found at $VENV"
echo "Run setup first:"
echo " python3 -m venv --without-pip $VENV"
echo " curl -sS https://bootstrap.pypa.io/get-pip.py | $VENV/bin/python3"
echo " $VENV/bin/pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124"
echo " $VENV/bin/pip install -r requirements.txt"
exit 1
fi
source "$VENV/bin/activate"
}
start_service() {
local name="$1"
local module="$2"
local port="$3"
local pidfile="$PIDS_DIR/$name.pid"
local logfile="$LOG_DIR/$name.log"
if [ -f "$pidfile" ] && kill -0 "$(cat "$pidfile")" 2>/dev/null; then
echo "[$name] Already running (PID $(cat "$pidfile"))"
return
fi
# Kill any zombie process squatting on this port
local squatter=""
squatter=$(ss -tlnp "sport = :$port" 2>/dev/null | grep -oP 'pid=\K[0-9]+' | head -1 || true)
if [ -n "$squatter" ]; then
echo "[$name] Killing stale process $squatter on port $port"
kill "$squatter" 2>/dev/null || true
sleep 1
fi
# Auto-load .env so tools that read os.environ (BRAVE_API_KEY,
# POLYGON_API_KEY, ANTHROPIC_API_KEY, etc.) get their values without the
# caller having to source it manually first.
local env_arg=()
if [ -f "$SCRIPT_DIR/.env" ]; then
env_arg=(--env-file "$SCRIPT_DIR/.env")
fi
echo "[$name] Starting on port $port..."
cd "$SCRIPT_DIR"
python -m uvicorn "$module" --host 0.0.0.0 --port "$port" "${env_arg[@]}" &> "$logfile" &
local pid=$!
echo "$pid" > "$pidfile"
echo "[$name] Started (PID $pid) — log: $logfile"
}
stop_all() {
echo "Stopping all services..."
local timeout_s=6
# First: stop by pidfiles
for pidfile in "$PIDS_DIR"/*.pid; do
[ -f "$pidfile" ] || continue
name="$(basename "$pidfile" .pid)"
pid="$(cat "$pidfile")"
if kill -0 "$pid" 2>/dev/null; then
echo "[$name] Stopping (PID $pid)..."
kill "$pid" 2>/dev/null || true
# Give it a moment to shut down cleanly
for _ in $(seq 1 "$timeout_s"); do
if ! kill -0 "$pid" 2>/dev/null; then
break
fi
sleep 1
done
if kill -0 "$pid" 2>/dev/null; then
echo "[$name] Force killing (PID $pid)..."
kill -9 "$pid" 2>/dev/null || true
fi
echo "[$name] Stopped."
fi
rm -f "$pidfile"
done
# Second: kill anything still listening on known ports (stale processes / no pidfile)
for port in 8000 8001 8002 8080; do
squatter="$(ss -tlnp "sport = :$port" 2>/dev/null | grep -oP 'pid=\K[0-9]+' | head -1 || true)"
if [ -n "$squatter" ] && kill -0 "$squatter" 2>/dev/null; then
echo "[port:$port] Killing leftover process $squatter"
kill -9 "$squatter" 2>/dev/null || true
fi
done
}
status() {
echo "=== Parrot Assistant Status ==="
for pidfile in "$PIDS_DIR"/*.pid; do
[ -f "$pidfile" ] || continue
name="$(basename "$pidfile" .pid)"
pid="$(cat "$pidfile")"
if kill -0 "$pid" 2>/dev/null; then
echo " [$name] Running (PID $pid)"
else
echo " [$name] Dead (stale PID $pid)"
rm -f "$pidfile"
fi
done
if ! ls "$PIDS_DIR"/*.pid &>/dev/null; then
echo " No services running."
fi
}
case "${1:-all}" in
all)
activate_venv
start_service stt "stt.service:app" 8001
start_service tts "tts.service:app" 8002
echo " Waiting for STT/TTS models to load..."
sleep 5
start_service bridge "bridge.server:app" 8000
start_service web "web.app:app" 8080
echo ""
echo "All services started. (external_host=$EXTERNAL_HOST)"
echo " Bridge: http://$EXTERNAL_HOST:8000"
echo " Web: http://$EXTERNAL_HOST:8080 (dashboard)"
echo " STT: http://$INTERNAL_HOST:8001 (internal)"
echo " TTS: http://$INTERNAL_HOST:8002 (internal)"
echo " Memory: in-process (mem0, inside bridge)"
echo " Animation: in-process (fbx_functions CSV, inside bridge)"
echo ""
echo "Health check: curl http://$INTERNAL_HOST:8000/health (waiting up to ~10s)"
for i in {1..10}; do
if curl -fsS "http://$INTERNAL_HOST:8000/health" >/dev/null 2>&1; then
echo "Health OK: $(curl -sS "http://$INTERNAL_HOST:8000/health")"
break
fi
echo " waiting for bridge... ($i/10)"
sleep 1
done
echo ""
echo "Channels (configured in config.yaml):"
echo " Telegram, Discord — set enabled: true + bot_token"
echo " CLI REPL — enabled by default (if bridge runs in foreground)"
echo ""
echo "Dashboard: http://$EXTERNAL_HOST:8000/monitor"
;;
stt) activate_venv; start_service stt "stt.service:app" 8001 ;;
tts) activate_venv; start_service tts "tts.service:app" 8002 ;;
web) activate_venv; start_service web "web.app:app" 8080 ;;
bridge)
# Restart the bridge AND ensure the webapp is up. Cloudflare's tunnel
# routes to port 8080 (the webapp), so leaving web dead while only
# bridge runs gives a 502 to anyone hitting the public hostname.
activate_venv
start_service bridge "bridge.server:app" 8000
start_service web "web.app:app" 8080
echo ""
echo "Dashboard: http://$EXTERNAL_HOST:8000/monitor"
;;
stop) stop_all ;;
end) stop_all ;;
restart)
stop_all
# Small delay helps ports fully release.
sleep 1
exec "$0" all
;;
status) status ;;
*)
echo "Usage: $0 {all|stt|tts|bridge|web|stop|end|restart|status}"
exit 1
;;
esac