-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·2524 lines (2201 loc) · 89.7 KB
/
Copy pathstart.sh
File metadata and controls
executable file
·2524 lines (2201 loc) · 89.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# SPDX-FileCopyrightText: 2025 Weibo, Inc.
#
# SPDX-License-Identifier: Apache-2.0
# Wegent One-Click Startup Script (Local Development)
# Start all services: Backend, Frontend, Chat Shell, Executor Manager
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'
# Configuration file path (use .env file, same as docker-compose)
CONFIG_FILE="$SCRIPT_DIR/.env"
# Load configuration from .env file
load_config() {
if [ -f "$CONFIG_FILE" ]; then
echo -e "${BLUE}Loading configuration from .env...${NC}"
# Read config file line by line, skip comments and empty lines
while IFS='=' read -r key value || [ -n "$key" ]; do
# Skip empty lines and comments
[[ -z "$key" || "$key" =~ ^[[:space:]]*# ]] && continue
# Remove leading/trailing whitespace
key=$(echo "$key" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
value=$(echo "$value" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# Remove quotes from value if present
value=$(echo "$value" | sed 's/^["'"'"']//;s/["'"'"']$//')
# Export the variable
if [ -n "$key" ] && [ -n "$value" ]; then
export "$key=$value"
echo -e " ${GREEN}✓${NC} Loaded: $key"
fi
done < "$CONFIG_FILE"
echo ""
# Backfill any missing port variables added after initial config was created
local needs_save=false
if ! grep -q "^KNOWLEDGE_RUNTIME_PORT=" "$CONFIG_FILE" 2>/dev/null; then
echo -e " ${YELLOW}↳${NC} Adding missing KNOWLEDGE_RUNTIME_PORT=$DEFAULT_KNOWLEDGE_RUNTIME_PORT to .env"
needs_save=true
fi
if ! grep -q "^WEWORK_PORT=" "$CONFIG_FILE" 2>/dev/null; then
echo -e " ${YELLOW}↳${NC} Adding missing WEWORK_PORT=$DEFAULT_WEWORK_PORT to .env"
needs_save=true
fi
if [ "$needs_save" = true ]; then
save_config
echo ""
fi
fi
}
persist_internal_service_token() {
local token="$1"
local temp_file
temp_file=$(mktemp)
if [ -f "$CONFIG_FILE" ]; then
grep -v "^INTERNAL_SERVICE_TOKEN=" "$CONFIG_FILE" > "$temp_file" || true
else
cat > "$temp_file" << 'EOF'
# Wegent Configuration
# This file is used by both docker-compose and start.sh
# Copy from .env.example and customize as needed
EOF
fi
cat >> "$temp_file" << EOF
# Internal service authentication token shared by Backend, Chat Shell, and Knowledge Runtime.
# Generated automatically by start.sh for local development.
INTERNAL_SERVICE_TOKEN=$token
EOF
mv "$temp_file" "$CONFIG_FILE"
}
generate_internal_service_token() {
if command -v openssl >/dev/null 2>&1; then
openssl rand -hex 32
return
fi
if command -v python3 >/dev/null 2>&1; then
python3 - << 'PY'
import secrets
print(secrets.token_hex(32))
PY
return
fi
echo ""
}
ensure_internal_service_token() {
if [ -n "${INTERNAL_SERVICE_TOKEN:-}" ]; then
return
fi
local generated_token
generated_token=$(generate_internal_service_token)
if [ -z "$generated_token" ]; then
echo -e "${RED}Unable to generate INTERNAL_SERVICE_TOKEN. Install openssl or python3, or set INTERNAL_SERVICE_TOKEN manually.${NC}"
exit 1
fi
export INTERNAL_SERVICE_TOKEN="$generated_token"
persist_internal_service_token "$generated_token"
echo -e "${GREEN}✓ Generated INTERNAL_SERVICE_TOKEN for local development and saved it to .env${NC}"
}
# Save configuration to .env file
# Only saves start.sh specific variables, preserves existing content
save_config() {
local temp_file=$(mktemp)
# If .env exists, copy it but remove the variables we're going to update
if [ -f "$CONFIG_FILE" ]; then
grep -v "^BACKEND_PORT=" "$CONFIG_FILE" | \
grep -v "^CHAT_SHELL_PORT=" | \
grep -v "^EXECUTOR_MANAGER_PORT=" | \
grep -v "^KNOWLEDGE_RUNTIME_PORT=" | \
grep -v "^WEGENT_FRONTEND_PORT=" | \
grep -v "^WEWORK_PORT=" | \
grep -v "^EXECUTOR_IMAGE=" | \
grep -v "^WEGENT_SOCKET_URL=" > "$temp_file" || true
fi
# Check if the start.sh section header exists
if ! grep -q "# START.SH CONFIGURATION" "$temp_file" 2>/dev/null; then
# Add header if .env is empty or doesn't have our section
if [ ! -s "$temp_file" ]; then
cat > "$temp_file" << 'EOF'
# Wegent Configuration
# This file is used by both docker-compose and start.sh
# Copy from .env.example and customize as needed
EOF
fi
cat >> "$temp_file" << EOF
# =============================================================================
# START.SH CONFIGURATION (Local Development)
# =============================================================================
# Service Ports
BACKEND_PORT=$BACKEND_PORT
CHAT_SHELL_PORT=$CHAT_SHELL_PORT
EXECUTOR_MANAGER_PORT=$EXECUTOR_MANAGER_PORT
KNOWLEDGE_RUNTIME_PORT=$KNOWLEDGE_RUNTIME_PORT
WEGENT_FRONTEND_PORT=$WEGENT_FRONTEND_PORT
WEWORK_PORT=$WEWORK_PORT
# Executor Docker image
EXECUTOR_IMAGE=$EXECUTOR_IMAGE
# Socket URL (for WebSocket connections, should be accessible from browser)
# For remote access, use your machine's IP address instead of localhost
WEGENT_SOCKET_URL=$WEGENT_SOCKET_URL
EOF
else
# Update existing values
echo "" >> "$temp_file"
echo "BACKEND_PORT=$BACKEND_PORT" >> "$temp_file"
echo "CHAT_SHELL_PORT=$CHAT_SHELL_PORT" >> "$temp_file"
echo "EXECUTOR_MANAGER_PORT=$EXECUTOR_MANAGER_PORT" >> "$temp_file"
echo "KNOWLEDGE_RUNTIME_PORT=$KNOWLEDGE_RUNTIME_PORT" >> "$temp_file"
echo "WEGENT_FRONTEND_PORT=$WEGENT_FRONTEND_PORT" >> "$temp_file"
echo "WEWORK_PORT=$WEWORK_PORT" >> "$temp_file"
echo "EXECUTOR_IMAGE=$EXECUTOR_IMAGE" >> "$temp_file"
echo "WEGENT_SOCKET_URL=$WEGENT_SOCKET_URL" >> "$temp_file"
fi
mv "$temp_file" "$CONFIG_FILE"
echo -e "${GREEN}✓ Configuration saved to .env${NC}"
}
# Interactive configuration initialization
# Args:
# $1 - "standalone" (default): exit after completion, show full messages
# "embedded": don't exit, used when called from start_services
init_config() {
local mode="${1:-standalone}"
shift || true
local init_backend=false
local init_frontend=false
local init_chat_shell=false
local init_executor_manager=false
local init_knowledge_runtime=false
local init_wework=false
if [ $# -eq 0 ]; then
init_backend=true
init_frontend=true
init_chat_shell=true
init_executor_manager=true
init_knowledge_runtime=true
init_wework=true
else
local init_service
for init_service in "$@"; do
case "$init_service" in
backend)
init_backend=true
;;
frontend)
init_frontend=true
;;
chat_shell)
init_chat_shell=true
;;
executor_manager)
init_executor_manager=true
;;
knowledge_runtime)
init_knowledge_runtime=true
;;
wework)
init_wework=true
;;
esac
done
fi
echo -e "${BLUE}╔════════════════════════════════════════════════════════╗${NC}"
echo -e "${BLUE}║ Wegent Configuration Initialization Wizard ║${NC}"
echo -e "${BLUE}╚════════════════════════════════════════════════════════╝${NC}"
echo ""
# Check if config file already exists (only in standalone mode)
if [ "$mode" = "standalone" ] && [ -f "$CONFIG_FILE" ]; then
echo -e "${YELLOW}⚠️ Configuration file .env already exists.${NC}"
echo -e "Current configuration:"
echo ""
cat "$CONFIG_FILE" | grep -v "^#" | grep -v "^$" | while read line; do
echo -e " ${CYAN}$line${NC}"
done
echo ""
read -p "Do you want to overwrite it? [y/N]: " overwrite
if [[ ! "$overwrite" =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Configuration initialization cancelled.${NC}"
exit 0
fi
echo ""
fi
echo -e "${GREEN}Please configure the following settings:${NC}"
echo -e "${YELLOW}(Press Enter to use default value)${NC}"
echo ""
# Get local IP for default socket URL
local local_ip=$(get_local_ip)
# === Service Ports ===
echo -e "${BLUE}━━━ Service Ports ━━━${NC}"
echo ""
local setting_index=1
# Backend Port
if [ "$init_backend" = true ]; then
echo -e "${CYAN}${setting_index}. Backend Port${NC}"
echo -e " The port for Backend API service."
read -p " Backend Port [$DEFAULT_BACKEND_PORT]: " input_backend_port
BACKEND_PORT=${input_backend_port:-$DEFAULT_BACKEND_PORT}
setting_index=$((setting_index + 1))
echo ""
fi
# Chat Shell Port
if [ "$init_chat_shell" = true ]; then
echo -e "${CYAN}${setting_index}. Chat Shell Port${NC}"
echo -e " The port for Chat Shell service."
read -p " Chat Shell Port [$DEFAULT_CHAT_SHELL_PORT]: " input_chat_shell_port
CHAT_SHELL_PORT=${input_chat_shell_port:-$DEFAULT_CHAT_SHELL_PORT}
setting_index=$((setting_index + 1))
echo ""
fi
# Executor Manager Port
if [ "$init_executor_manager" = true ]; then
echo -e "${CYAN}${setting_index}. Executor Manager Port${NC}"
echo -e " The port for Executor Manager service."
read -p " Executor Manager Port [$DEFAULT_EXECUTOR_MANAGER_PORT]: " input_executor_manager_port
EXECUTOR_MANAGER_PORT=${input_executor_manager_port:-$DEFAULT_EXECUTOR_MANAGER_PORT}
setting_index=$((setting_index + 1))
echo ""
fi
# Knowledge Runtime Port
if [ "$init_knowledge_runtime" = true ]; then
echo -e "${CYAN}${setting_index}. Knowledge Runtime Port${NC}"
echo -e " The port for Knowledge Runtime service (RAG operations)."
read -p " Knowledge Runtime Port [$DEFAULT_KNOWLEDGE_RUNTIME_PORT]: " input_knowledge_runtime_port
KNOWLEDGE_RUNTIME_PORT=${input_knowledge_runtime_port:-$DEFAULT_KNOWLEDGE_RUNTIME_PORT}
setting_index=$((setting_index + 1))
echo ""
fi
# Frontend Port
if [ "$init_frontend" = true ]; then
echo -e "${CYAN}${setting_index}. Frontend Port${NC}"
echo -e " The port where the web UI will be accessible."
read -p " Frontend Port [$DEFAULT_WEGENT_FRONTEND_PORT]: " input_frontend_port
WEGENT_FRONTEND_PORT=${input_frontend_port:-$DEFAULT_WEGENT_FRONTEND_PORT}
setting_index=$((setting_index + 1))
echo ""
fi
# WeWork Port
if [ "$init_wework" = true ]; then
echo -e "${CYAN}${setting_index}. WeWork Port${NC}"
echo -e " The port for WeWork multi-platform workspace app."
read -p " WeWork Port [$DEFAULT_WEWORK_PORT]: " input_wework_port
WEWORK_PORT=${input_wework_port:-$DEFAULT_WEWORK_PORT}
setting_index=$((setting_index + 1))
echo ""
fi
# === Other Settings ===
echo -e "${BLUE}━━━ Other Settings ━━━${NC}"
echo ""
# Executor Image
if [ "$init_executor_manager" = true ]; then
echo -e "${CYAN}${setting_index}. Executor Docker Image${NC}"
echo -e " The Docker image used for task execution."
read -p " Executor Image [$DEFAULT_EXECUTOR_IMAGE]: " input_image
EXECUTOR_IMAGE=${input_image:-$DEFAULT_EXECUTOR_IMAGE}
setting_index=$((setting_index + 1))
echo ""
fi
# Socket URL
if [ "$init_backend" = true ] || [ "$init_frontend" = true ]; then
echo -e "${CYAN}${setting_index}. Socket URL${NC}"
echo -e " The WebSocket URL for real-time communication."
echo -e " For remote access, use your machine's IP address."
echo -e " Detected local IP: ${GREEN}$local_ip${NC}"
local default_socket="http://$local_ip:$BACKEND_PORT"
read -p " Socket URL [$default_socket]: " input_socket_url
WEGENT_SOCKET_URL=${input_socket_url:-$default_socket}
setting_index=$((setting_index + 1))
echo ""
fi
# Show summary
echo -e "${BLUE}════════════════════════════════════════════════════════${NC}"
echo -e "${GREEN}Configuration Summary:${NC}"
echo -e " ${YELLOW}Service Ports:${NC}"
echo -e " Backend Port: ${CYAN}$BACKEND_PORT${NC}"
echo -e " Chat Shell Port: ${CYAN}$CHAT_SHELL_PORT${NC}"
echo -e " Executor Mgr Port: ${CYAN}$EXECUTOR_MANAGER_PORT${NC}"
echo -e " Knowledge Rtm Port: ${CYAN}$KNOWLEDGE_RUNTIME_PORT${NC}"
echo -e " Frontend Port: ${CYAN}$WEGENT_FRONTEND_PORT${NC}"
echo -e " WeWork Port: ${CYAN}$WEWORK_PORT${NC}"
echo -e " ${YELLOW}Other Settings:${NC}"
echo -e " Executor Image: ${CYAN}$EXECUTOR_IMAGE${NC}"
echo -e " Socket URL: ${CYAN}$WEGENT_SOCKET_URL${NC}"
echo -e "${BLUE}════════════════════════════════════════════════════════${NC}"
echo ""
read -p "Save this configuration? [Y/n]: " confirm
if [[ "$confirm" =~ ^[Nn]$ ]]; then
echo -e "${YELLOW}Configuration not saved.${NC}"
if [ "$mode" = "standalone" ]; then
exit 0
else
return 1
fi
fi
save_config
echo ""
echo -e "${GREEN}✓ Configuration initialized successfully!${NC}"
# Only show these messages in standalone mode
if [ "$mode" = "standalone" ]; then
echo -e "${YELLOW}You can now run './start.sh' to start all services.${NC}"
echo -e "${YELLOW}To modify configuration, edit .env or run './start.sh --init' again.${NC}"
fi
}
# Detect Python command and version
detect_python() {
local python_cmd=""
local python_version=""
# Check python3 first
if command -v python3 &> /dev/null; then
python_cmd="python3"
python_version=$(python3 --version 2>&1 | grep -oE '[0-9]+\.[0-9]+' | head -1)
elif command -v python &> /dev/null; then
# Check if it's Python 2 or 3
local ver=$(python --version 2>&1 | grep -oE '[0-9]+' | head -1)
if [ "$ver" = "3" ]; then
python_cmd="python"
python_version=$(python --version 2>&1 | grep -oE '[0-9]+\.[0-9]+' | head -1)
elif [ "$ver" = "2" ]; then
echo -e "${RED}Error: Only Python 2.x is detected, but Python 3.x is required.${NC}"
echo ""
echo -e "${YELLOW}Please install Python 3:${NC}"
echo -e " ${BLUE}macOS:${NC} brew install python3"
echo -e " ${BLUE}Ubuntu:${NC} sudo apt install python3"
echo -e " ${BLUE}CentOS:${NC} sudo yum install python3"
exit 1
fi
else
echo -e "${RED}Error: Python is not installed.${NC}"
echo ""
echo -e "${YELLOW}Please install Python 3:${NC}"
echo -e " ${BLUE}macOS:${NC} brew install python3"
echo -e " ${BLUE}Ubuntu:${NC} sudo apt install python3"
echo -e " ${BLUE}CentOS:${NC} sudo yum install python3"
exit 1
fi
echo "$python_cmd"
}
# Check if uv is installed
check_uv_installed() {
if command -v uv &> /dev/null; then
return 0
fi
return 1
}
# Show uv installation instructions
show_uv_install_instructions() {
echo -e "${RED}Error: uv is not installed.${NC}"
echo ""
echo -e "${YELLOW}uv is a fast Python package manager required by Wegent.${NC}"
echo -e "${YELLOW}Please install uv using one of the following methods:${NC}"
echo ""
echo -e " ${GREEN}Method 1: Official install script (Recommended)${NC}"
echo -e " ${BLUE}curl -LsSf https://astral.sh/uv/install.sh | sh${NC}"
echo ""
echo -e " ${GREEN}Method 2: Using Homebrew (macOS/Linux)${NC}"
echo -e " ${BLUE}brew install uv${NC}"
echo ""
echo -e " ${GREEN}Method 3: Using pip${NC}"
# Check which pip command to recommend
if command -v pip3 &> /dev/null; then
echo -e " ${BLUE}pip3 install uv${NC}"
elif command -v pip &> /dev/null; then
# Check if pip is Python 3
local pip_python_ver=$(pip --version 2>&1 | grep -oE 'python [0-9]+' | grep -oE '[0-9]+')
if [ "$pip_python_ver" = "3" ]; then
echo -e " ${BLUE}pip install uv${NC}"
else
echo -e " ${BLUE}pip3 install uv${NC} ${YELLOW}(requires pip for Python 3)${NC}"
fi
else
echo -e " ${BLUE}pip3 install uv${NC} ${YELLOW}(requires pip for Python 3)${NC}"
fi
echo ""
echo -e "${YELLOW}After installation, please restart your terminal or run:${NC}"
echo -e " ${BLUE}source ~/.bashrc${NC} or ${BLUE}source ~/.zshrc${NC}"
echo ""
exit 1
}
# Detect if running in WSL
is_wsl() {
if grep -qi microsoft /proc/version 2>/dev/null; then
return 0
fi
return 1
}
# Check if docker is installed
check_docker_installed() {
if command -v docker &> /dev/null; then
return 0
fi
return 1
}
# Check if Docker daemon is reachable
check_docker_ready() {
if docker info &> /dev/null; then
return 0
fi
return 1
}
# Show docker installation instructions
show_docker_install_instructions() {
echo -e "${RED}Error: Docker is not installed or not running.${NC}"
echo ""
echo -e "${YELLOW}Docker is required by Wegent to run backend/executor services in containers.${NC}"
echo -e "${YELLOW}Please install Docker using one of the following methods:${NC}"
echo ""
echo -e " ${GREEN}Method 1: Docker Desktop (macOS / Windows)${NC}"
echo -e " ${BLUE}https://www.docker.com/products/docker-desktop/${NC}"
echo ""
echo -e " ${GREEN}Method 2: Linux package manager${NC}"
echo -e " ${BLUE}Ubuntu / Debian:${NC}"
echo -e " sudo apt update"
echo -e " sudo apt install -y docker.io"
echo ""
echo -e " ${BLUE}CentOS / RHEL / Alma / Rocky:${NC}"
echo -e " sudo dnf install -y docker-ce docker-ce-cli containerd.io"
echo ""
echo -e " ${GREEN}Method 3: Official Docker convenience script${NC}"
echo -e " ${BLUE}curl -fsSL https://get.docker.com | sh${NC}"
echo ""
echo -e "${YELLOW}After installation, please ensure the Docker daemon is running, e.g.:${NC}"
echo -e " ${BLUE}sudo systemctl enable --now docker${NC}"
echo ""
echo -e "${YELLOW}Then re-run this script.${NC}"
echo ""
exit 1
}
# Show Docker daemon troubleshooting instructions
show_docker_ready_instructions() {
echo -e "${RED}Error: Docker is installed, but the Docker daemon is not reachable.${NC}"
echo ""
echo -e "${YELLOW}Please ensure Docker is running and your user can access it.${NC}"
echo ""
echo -e " ${GREEN}Linux: start Docker${NC}"
echo -e " ${BLUE}sudo systemctl enable --now docker${NC}"
echo ""
echo -e " ${GREEN}Linux: allow current user to access Docker${NC}"
echo -e " ${BLUE}sudo usermod -aG docker \$USER${NC}"
echo -e " ${BLUE}newgrp docker${NC}"
echo ""
echo -e " ${GREEN}macOS / Windows:${NC}"
echo -e " ${BLUE}Start Docker Desktop and wait until it is ready.${NC}"
echo ""
echo -e "${YELLOW}Then re-run this script.${NC}"
echo ""
exit 1
}
# Detect Docker Compose command
detect_docker_compose() {
if docker compose version &> /dev/null; then
DOCKER_COMPOSE_CMD="docker compose"
return 0
fi
if command -v docker-compose &> /dev/null && docker-compose version &> /dev/null; then
DOCKER_COMPOSE_CMD="docker-compose"
return 0
fi
return 1
}
# Run Docker Compose with the detected command
run_docker_compose() {
if [ -z "$DOCKER_COMPOSE_CMD" ]; then
detect_docker_compose || return 127
fi
# DOCKER_COMPOSE_CMD intentionally contains either "docker compose" or "docker-compose".
# shellcheck disable=SC2086
$DOCKER_COMPOSE_CMD "$@"
}
# Show Docker Compose installation instructions
show_docker_compose_install_instructions() {
echo -e "${RED}Error: Docker Compose is not installed.${NC}"
echo ""
echo -e "${YELLOW}Docker Compose is required to start MySQL and Redis from docker-compose.yml.${NC}"
echo -e "${YELLOW}Install the Compose plugin, then re-run this script.${NC}"
echo ""
echo -e " ${GREEN}Ubuntu / Debian using Docker official packages:${NC}"
echo -e " ${BLUE}sudo apt update${NC}"
echo -e " ${BLUE}sudo apt install -y docker-compose-plugin${NC}"
echo ""
echo -e " ${GREEN}Ubuntu / Debian using distro Docker packages:${NC}"
echo -e " ${BLUE}sudo apt update${NC}"
echo -e " ${BLUE}sudo apt install -y docker-compose-v2${NC}"
echo ""
echo -e " ${GREEN}macOS / Windows:${NC}"
echo -e " ${BLUE}Docker Desktop includes Docker Compose.${NC}"
echo ""
exit 1
}
# Check if MySQL and Redis are running
check_mysql_redis() {
local mysql_running=false
local redis_running=false
if [ -z "$DOCKER_COMPOSE_CMD" ] && ! detect_docker_compose; then
show_docker_compose_install_instructions
fi
# Check if MySQL container is running
if docker ps --format '{{.Names}}' | grep -q "^wegent-mysql$"; then
mysql_running=true
fi
# Check if Redis container is running
if docker ps --format '{{.Names}}' | grep -q "^wegent-redis$"; then
redis_running=true
fi
if [ "$mysql_running" = true ] && [ "$redis_running" = true ]; then
echo -e "${GREEN}✓ MySQL and Redis are already running${NC}"
return 0
fi
# Start MySQL and Redis if not running
echo -e "${YELLOW}MySQL or Redis is not running. Starting them with $DOCKER_COMPOSE_CMD...${NC}"
if ! run_docker_compose up -d mysql redis; then
echo -e "${RED}Error: Failed to start MySQL and Redis${NC}"
echo -e "${YELLOW}Please check docker-compose.yml and ensure Docker is running${NC}"
exit 1
fi
# Wait for services to be healthy
echo -e "${YELLOW}Waiting for MySQL and Redis to be ready...${NC}"
local max_wait=60
local waited=0
while [ $waited -lt $max_wait ]; do
local mysql_healthy=false
local redis_healthy=false
# Check MySQL health
if docker inspect wegent-mysql --format='{{.State.Health.Status}}' 2>/dev/null | grep -q "healthy"; then
mysql_healthy=true
fi
# Check Redis health
if docker inspect wegent-redis --format='{{.State.Health.Status}}' 2>/dev/null | grep -q "healthy"; then
redis_healthy=true
fi
if [ "$mysql_healthy" = true ] && [ "$redis_healthy" = true ]; then
echo -e "${GREEN}✓ MySQL and Redis are ready${NC}"
return 0
fi
sleep 2
waited=$((waited + 2))
echo -e " Waiting... (${waited}s/${max_wait}s)"
done
echo -e "${RED}Error: MySQL or Redis failed to become healthy within ${max_wait}s${NC}"
echo -e "${YELLOW}You can check the logs with:${NC}"
echo -e " ${BLUE}docker logs wegent-mysql${NC}"
echo -e " ${BLUE}docker logs wegent-redis${NC}"
exit 1
}
# Check if Node.js and pnpm are installed
check_node_installed() {
if ! command -v node &> /dev/null; then
echo -e "${RED}Error: Node.js is not installed.${NC}"
echo ""
echo -e "${YELLOW}Please install Node.js:${NC}"
echo -e " ${BLUE}macOS:${NC} brew install node"
echo -e " ${BLUE}Ubuntu:${NC} sudo apt install nodejs npm"
echo -e " ${BLUE}Or:${NC} https://nodejs.org/"
exit 1
fi
# Check Node.js version (require >= 20)
NODE_MAJOR=$(node -v | sed 's/^v//' | cut -d. -f1)
if [ "$NODE_MAJOR" -lt 20 ]; then
echo -e "${RED}Error: Node.js v20 or higher is required (found $(node -v)).${NC}"
echo -e "${YELLOW}Please upgrade Node.js:${NC}"
echo -e " ${BLUE}macOS:${NC} brew install node@20"
echo -e " ${BLUE}Ubuntu:${NC} use NodeSource or nvm to install Node 20"
exit 1
fi
if ! command -v pnpm &> /dev/null; then
if command -v corepack &> /dev/null; then
echo -e " ${YELLOW}pnpm not found. Activating pnpm with corepack...${NC}"
corepack enable
corepack prepare pnpm@10.34.3 --activate
fi
fi
if ! command -v pnpm &> /dev/null; then
echo -e "${RED}Error: pnpm is not installed.${NC}"
echo ""
echo -e "${YELLOW}This repository uses pnpm workspace dependencies.${NC}"
echo -e "${YELLOW}Please install pnpm:${NC}"
echo -e " ${BLUE}Corepack:${NC} corepack enable && corepack prepare pnpm@10.34.3 --activate"
echo -e " ${BLUE}npm:${NC} npm install -g pnpm@10.34.3"
exit 1
fi
}
# Check if libmagic is installed
check_libmagic_installed() {
# Try to find libmagic library
local found=false
# Check common library paths
if [ -f "/usr/local/lib/libmagic.dylib" ] || \
[ -f "/opt/homebrew/lib/libmagic.dylib" ] || \
[ -f "/usr/lib/libmagic.so.1" ] || \
[ -f "/usr/lib/x86_64-linux-gnu/libmagic.so.1" ] || \
[ -f "/usr/lib64/libmagic.so.1" ]; then
found=true
fi
# Also check if file command exists (usually comes with libmagic)
if command -v file &> /dev/null; then
# Verify file command works (indicates libmagic is functional)
if file --version &> /dev/null; then
found=true
fi
fi
if [ "$found" = false ]; then
echo -e "${RED}Error: libmagic is not installed.${NC}"
echo ""
echo -e "${YELLOW}libmagic is required for file type detection.${NC}"
echo -e "${YELLOW}Please install it using one of the following methods:${NC}"
echo ""
echo -e " ${GREEN}macOS:${NC}"
echo -e " ${BLUE}brew install libmagic${NC}"
echo ""
echo -e " ${GREEN}Debian/Ubuntu:${NC}"
echo -e " ${BLUE}sudo apt-get install libmagic1${NC}"
echo ""
echo -e " ${GREEN}RHEL/CentOS/Fedora:${NC}"
echo -e " ${BLUE}sudo yum install file-libs${NC}"
echo ""
exit 1
fi
}
# Sync Python dependencies for a directory
sync_python_deps() {
local dir=$1
local name=$2
cd "$SCRIPT_DIR/$dir"
# Check if .venv exists and has the shared module installed
local need_sync=false
if [ ! -d ".venv" ]; then
need_sync=true
elif [ ! -f ".venv/pyvenv.cfg" ]; then
need_sync=true
else
# Check if shared module is installed
if ! .venv/bin/python -c "import shared" 2>/dev/null; then
need_sync=true
fi
# Check if pyproject.toml is newer than .venv
if [ "pyproject.toml" -nt ".venv" ]; then
need_sync=true
fi
# Check if uv.lock exists and is newer than .venv
if [ -f "uv.lock" ] && [ "uv.lock" -nt ".venv" ]; then
need_sync=true
fi
fi
if [ "$need_sync" = true ]; then
echo -e " ${YELLOW}Syncing dependencies for $name...${NC}"
# Use --frozen to avoid modifying uv.lock file
uv sync --frozen
echo -e " ${GREEN}✓${NC} $name dependencies synced"
else
echo -e " ${GREEN}✓${NC} $name dependencies are up to date"
fi
cd "$SCRIPT_DIR"
}
check_python_env() {
local dir=$1
local name=$2
cd "$SCRIPT_DIR/$dir"
if [ ! -f ".env" ]; then
if [ ! -f ".env.example" ]; then
echo -e "${RED} $name Error: .env.example not found${NC}"
exit 1
fi
cp .env.example .env
echo -e "${GREEN}✓ $name Created .env from .env.example${NC}"
else
echo -e "${GREEN}✓ $name .env file already exists${NC}"
fi
cd "$SCRIPT_DIR"
}
# Patch backend DATABASE_URL and REDIS_URL to use configured MYSQL_PORT and REDIS_PORT.
# When start.sh runs services locally, MySQL/Redis are exposed on host ports from
# MYSQL_PORT/REDIS_PORT in .env, but backend/.env hardcodes the default ports (3306/6379).
# This function reads backend/.env, patches the ports, and exports them as env vars
# so pydantic-settings (which prefers env vars over .env file values) picks them up.
patch_backend_service_urls() {
local backend_env="$SCRIPT_DIR/backend/.env"
if [ ! -f "$backend_env" ]; then
return
fi
# Patch DATABASE_URL with MYSQL_PORT if configured
if [ -n "$MYSQL_PORT" ]; then
local db_url
db_url=$(grep "^DATABASE_URL=" "$backend_env" | cut -d'=' -f2-)
if [ -n "$db_url" ]; then
# Replace port in @host:PORT pattern (supports optional trailing slash)
# e.g., @localhost:3306/db -> @localhost:23306/db
local patched_db_url
patched_db_url=$(echo "$db_url" | sed -E "s#(@[^/:]+):[0-9]+(/|$)#\1:$MYSQL_PORT\2#")
export DATABASE_URL="$patched_db_url"
echo -e " ${GREEN}✓${NC} DATABASE_URL patched to use MYSQL_PORT=$MYSQL_PORT"
fi
fi
# Patch REDIS_URL with REDIS_PORT if configured
if [ -n "$REDIS_PORT" ]; then
local redis_url
redis_url=$(grep "^REDIS_URL=" "$backend_env" | cut -d'=' -f2-)
if [ -n "$redis_url" ]; then
# Replace port in host:PORT pattern (supports optional credentials and trailing slash)
# e.g., redis://127.0.0.1:6379/0 or redis://:pwd@127.0.0.1:6379/0
local patched_redis_url
patched_redis_url=$(echo "$redis_url" | sed -E "s#(//([^/@]+@)?[^/:]+):[0-9]+(/|$)#\1:$REDIS_PORT\3#")
export REDIS_URL="$patched_redis_url"
echo -e " ${GREEN}✓${NC} REDIS_URL patched to use REDIS_PORT=$REDIS_PORT"
fi
fi
}
# Check frontend dependencies
compute_workspace_dependency_fingerprint() {
node - "$SCRIPT_DIR" <<'NODE'
const crypto = require('crypto')
const fs = require('fs')
const path = require('path')
const root = process.argv[2]
const manifests = [
'package.json',
'frontend/package.json',
'wework/package.json',
'packages/chat-core/package.json',
]
const manifestKeys = [
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
'peerDependenciesMeta',
'packageManager',
'pnpm',
'resolutions',
'overrides',
'engines',
]
const files = ['pnpm-lock.yaml', 'pnpm-workspace.yaml']
function sortValue(value) {
if (Array.isArray(value)) {
return value.map(sortValue)
}
if (value && typeof value === 'object') {
return Object.fromEntries(
Object.entries(value)
.sort(([left], [right]) => left.localeCompare(right))
.map(([key, nested]) => [key, sortValue(nested)]),
)
}
return value
}
const payload = { files: {}, manifests: {} }
for (const manifest of manifests) {
const filePath = path.join(root, manifest)
const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'))
payload.manifests[manifest] = {}
for (const key of manifestKeys) {
if (Object.prototype.hasOwnProperty.call(parsed, key)) {
payload.manifests[manifest][key] = parsed[key]
}
}
}
for (const file of files) {
const filePath = path.join(root, file)
payload.files[file] = fs.existsSync(filePath)
? crypto.createHash('sha256').update(fs.readFileSync(filePath)).digest('hex')
: null
}
const fingerprint = crypto
.createHash('sha256')
.update(JSON.stringify(sortValue(payload)))
.digest('hex')
console.log(fingerprint)
NODE
}
mark_workspace_dependencies_installed() {
local fingerprint="$1"
local workspace_marker="$SCRIPT_DIR/node_modules/.install-marker"
local fingerprint_file="$SCRIPT_DIR/node_modules/.install-fingerprint"
mkdir -p "$SCRIPT_DIR/node_modules"
printf "%s\n" "$fingerprint" > "$fingerprint_file"
touch "$workspace_marker"
if [ -d "$SCRIPT_DIR/frontend/node_modules" ]; then
touch "$SCRIPT_DIR/frontend/node_modules/.install-marker"
fi
if [ -d "$SCRIPT_DIR/wework/node_modules" ]; then
touch "$SCRIPT_DIR/wework/node_modules/.install-marker"
fi
}
ensure_workspace_dependencies() {
local label="$1"
local node_modules_dir="$2"
local workspace_marker="$SCRIPT_DIR/node_modules/.install-marker"
local fingerprint_file="$SCRIPT_DIR/node_modules/.install-fingerprint"
local package_marker="$node_modules_dir/.install-marker"
local current_fingerprint=""
local installed_fingerprint=""
local reason=""
if [ ! -d "$SCRIPT_DIR/node_modules" ]; then
reason="Workspace dependencies not installed"
elif [ ! -d "$node_modules_dir" ]; then
reason="$label dependencies not installed"
fi
current_fingerprint=$(compute_workspace_dependency_fingerprint)
if [ -z "$reason" ]; then
if [ ! -f "$workspace_marker" ] || [ ! -f "$fingerprint_file" ]; then
if [ -f "$package_marker" ]; then
mark_workspace_dependencies_installed "$current_fingerprint"
else
reason="Workspace dependency install marker missing"
fi
else
installed_fingerprint=$(cat "$fingerprint_file" 2>/dev/null || true)
if [ "$installed_fingerprint" != "$current_fingerprint" ]; then
reason="Workspace dependency metadata changed"
fi
fi
fi
if [ -n "$reason" ]; then
echo -e "${YELLOW}$reason. Updating...${NC}"
cd "$SCRIPT_DIR"
pnpm install --frozen-lockfile
mark_workspace_dependencies_installed "$current_fingerprint"
cd "$SCRIPT_DIR"
echo -e "${GREEN}✓ $label dependencies updated${NC}"
return
fi
echo -e "${GREEN}✓ $label dependencies are up to date${NC}"
}
check_frontend_dependencies() {
ensure_workspace_dependencies "Frontend" "$SCRIPT_DIR/frontend/node_modules"
}
clean_frontend_cache() {
local frontend_cache_dir="$SCRIPT_DIR/frontend/.next"
echo -e "${BLUE}Cleaning frontend cache...${NC}"
if [ -d "$frontend_cache_dir" ]; then
rm -rf "$frontend_cache_dir"
echo -e " ${GREEN}✓${NC} Removed $frontend_cache_dir"
else
echo -e " ${GREEN}✓${NC} Frontend cache is already clean"