-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·653 lines (545 loc) · 20 KB
/
setup.sh
File metadata and controls
executable file
·653 lines (545 loc) · 20 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
#!/usr/bin/env bash
set -euo pipefail
# SourceBridge.ai — One-command setup
# https://github.com/sourcebridge/sourcebridge
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
readonly BOLD='\033[1m'
readonly DIM='\033[2m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[0;33m'
readonly RED='\033[0;31m'
readonly CYAN='\033[0;36m'
readonly RESET='\033[0m'
info() { printf "${CYAN}[info]${RESET} %s\n" "$*"; }
ok() { printf "${GREEN}[ok]${RESET} %s\n" "$*"; }
warn() { printf "${YELLOW}[warn]${RESET} %s\n" "$*"; }
err() { printf "${RED}[error]${RESET} %s\n" "$*" >&2; }
step() { printf "\n${BOLD}--- %s${RESET}\n" "$*"; }
prompt() { printf "${BOLD}%s${RESET}" "$*"; }
die() { err "$@"; exit 1; }
# ---------------------------------------------------------------------------
# Banner
# ---------------------------------------------------------------------------
print_banner() {
cat <<'BANNER'
SourceBridge.ai
===============
Requirement-aware code comprehension platform
This script will walk you through first-time setup.
It is safe to run multiple times.
BANNER
}
# ---------------------------------------------------------------------------
# Prerequisite checks
# ---------------------------------------------------------------------------
HAS_DOCKER=false
HAS_COMPOSE=false
HAS_GO=false
HAS_PYTHON=false
HAS_UV=false
HAS_NODE=false
check_command() {
command -v "$1" >/dev/null 2>&1
}
version_ge() {
# Returns 0 if $1 >= $2 (dot-separated versions)
printf '%s\n%s' "$2" "$1" | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -g | head -n1 | grep -qx "$2"
}
check_prerequisites() {
step "Checking prerequisites"
# Docker
if check_command docker; then
local docker_version
docker_version=$(docker version --format '{{.Client.Version}}' 2>/dev/null || echo "0")
ok "Docker ${docker_version}"
HAS_DOCKER=true
else
warn "Docker not found (required for Docker Compose mode)"
info "Install: https://docs.docker.com/get-docker/"
fi
# Docker Compose (v2 plugin or standalone)
if docker compose version >/dev/null 2>&1; then
local compose_version
compose_version=$(docker compose version --short 2>/dev/null || echo "unknown")
ok "Docker Compose ${compose_version}"
HAS_COMPOSE=true
elif check_command docker-compose; then
ok "Docker Compose (standalone)"
HAS_COMPOSE=true
else
warn "Docker Compose not found (required for Docker Compose mode)"
info "Install: https://docs.docker.com/compose/install/"
fi
# Go
if check_command go; then
local go_version
go_version=$(go version | sed -E 's/.*go([0-9]+\.[0-9]+(\.[0-9]+)?).*/\1/')
if version_ge "${go_version}" "1.25"; then
ok "Go ${go_version}"
HAS_GO=true
else
warn "Go ${go_version} found, but 1.25+ recommended for source builds"
fi
else
info "Go not found (optional, needed for source builds)"
fi
# Python
if check_command python3; then
local py_version
py_version=$(python3 --version 2>/dev/null | sed -E 's/Python //')
if version_ge "${py_version}" "3.12"; then
ok "Python ${py_version}"
HAS_PYTHON=true
else
warn "Python ${py_version} found, but 3.12+ required for worker"
fi
else
info "Python 3 not found (optional, needed for worker in source mode)"
fi
# uv
if check_command uv; then
local uv_version
uv_version=$(uv --version 2>/dev/null | sed -E 's/uv //')
ok "uv ${uv_version}"
HAS_UV=true
else
if [ "$HAS_PYTHON" = true ]; then
info "uv not found (recommended for Python dependency management)"
info "Install: curl -LsSf https://astral.sh/uv/install.sh | sh"
fi
fi
# Node.js
if check_command node; then
local node_version
node_version=$(node --version 2>/dev/null | sed 's/^v//')
local node_major
node_major=$(echo "$node_version" | cut -d. -f1)
if [ "$node_major" -ge 22 ] 2>/dev/null; then
ok "Node.js ${node_version}"
HAS_NODE=true
else
warn "Node.js ${node_version} found, but 22+ recommended for web UI"
fi
else
info "Node.js not found (optional, needed for web UI in source mode)"
fi
}
# ---------------------------------------------------------------------------
# Mode selection
# ---------------------------------------------------------------------------
ask_mode() {
step "Setup mode"
echo ""
echo " 1) Docker Compose (recommended -- quickest way to try SourceBridge)"
echo " 2) Local development (for contributing and development)"
echo ""
prompt "Choose [1/2] (default: 1): "
read -r mode_choice
mode_choice="${mode_choice:-1}"
case "$mode_choice" in
1) SETUP_MODE="docker" ;;
2) SETUP_MODE="local" ;;
*) warn "Invalid choice, defaulting to Docker Compose"; SETUP_MODE="docker" ;;
esac
}
# ---------------------------------------------------------------------------
# LLM provider selection
# ---------------------------------------------------------------------------
LLM_PROVIDER=""
LLM_API_KEY=""
LLM_BASE_URL=""
LLM_MODEL=""
ask_llm_provider() {
step "LLM provider"
echo ""
echo " SourceBridge uses an LLM for code reasoning, review, and discussion."
echo ""
echo " 1) Anthropic (cloud -- requires API key)"
echo " 2) OpenAI (cloud -- requires API key)"
echo " 3) Ollama (local -- free, runs on your machine)"
echo " 4) Other (configure manually later)"
echo ""
prompt "Choose [1/2/3/4] (default: 1): "
read -r llm_choice
llm_choice="${llm_choice:-1}"
case "$llm_choice" in
1) configure_anthropic ;;
2) configure_openai ;;
3) configure_ollama ;;
4) configure_other ;;
*) warn "Invalid choice, defaulting to Anthropic"; configure_anthropic ;;
esac
}
configure_anthropic() {
LLM_PROVIDER="anthropic"
LLM_MODEL="claude-sonnet-4-20250514"
LLM_BASE_URL=""
echo ""
prompt "Anthropic API key: "
read -r api_key
if [ -z "$api_key" ]; then
warn "No API key provided. You can set ANTHROPIC_API_KEY later in .env or config.toml."
warn "LLM-powered features (review, discussion, explanation) will not work without it."
fi
LLM_API_KEY="$api_key"
ok "Configured Anthropic (model: ${LLM_MODEL})"
}
configure_openai() {
LLM_PROVIDER="openai"
LLM_MODEL="gpt-4o"
LLM_BASE_URL=""
echo ""
prompt "OpenAI API key: "
read -r api_key
if [ -z "$api_key" ]; then
warn "No API key provided. You can set OPENAI_API_KEY later in .env or config.toml."
warn "LLM-powered features (review, discussion, explanation) will not work without it."
fi
LLM_API_KEY="$api_key"
ok "Configured OpenAI (model: ${LLM_MODEL})"
}
configure_ollama() {
LLM_PROVIDER="ollama"
LLM_MODEL="qwen3:32b"
LLM_API_KEY="not-needed"
# Determine the Ollama base URL based on setup mode.
# In Docker Compose mode, the worker container cannot reach the host via
# "localhost". On Docker Desktop for macOS/Windows the special hostname
# "host.docker.internal" resolves to the host machine. On native Linux
# we fall back to the docker bridge gateway (172.17.0.1).
local default_url="http://localhost:11434/v1"
if [ "${SETUP_MODE:-}" = "docker" ]; then
if [ "$(uname -s)" = "Linux" ]; then
default_url="http://172.17.0.1:11434/v1"
else
default_url="http://host.docker.internal:11434/v1"
fi
fi
echo ""
info "Checking if Ollama is running..."
# Determine the correct health-check URL. We test the host-local endpoint
# (localhost) regardless of the Docker URL we will write to the config.
local check_url="http://localhost:11434/v1/models"
if curl -sf "$check_url" >/dev/null 2>&1; then
ok "Ollama is running at localhost:11434"
else
warn "Ollama does not appear to be running at localhost:11434."
info "Install Ollama: https://ollama.ai/download"
info "Then run: ollama serve"
fi
echo ""
prompt "Ollama URL (default: ${default_url}): "
read -r custom_url
LLM_BASE_URL="${custom_url:-$default_url}"
echo ""
prompt "Model name (default: ${LLM_MODEL}): "
read -r custom_model
LLM_MODEL="${custom_model:-$LLM_MODEL}"
# Offer to pull the model
if check_command ollama; then
echo ""
prompt "Pull model '${LLM_MODEL}' now? [y/N]: "
read -r pull_choice
if [ "$pull_choice" = "y" ] || [ "$pull_choice" = "Y" ]; then
info "Pulling ${LLM_MODEL} (this may take a while)..."
ollama pull "$LLM_MODEL" || warn "Failed to pull model. You can try later: ollama pull ${LLM_MODEL}"
fi
fi
ok "Configured Ollama (model: ${LLM_MODEL}, url: ${LLM_BASE_URL})"
}
configure_other() {
LLM_PROVIDER=""
LLM_MODEL=""
LLM_BASE_URL=""
LLM_API_KEY=""
echo ""
info "Skipping LLM configuration."
info "Edit .env (Docker mode) or config.toml (local mode) to configure your provider."
info "See docs/installation.md for supported providers and configuration options."
}
# ---------------------------------------------------------------------------
# Docker Compose setup
# ---------------------------------------------------------------------------
setup_docker() {
step "Setting up Docker Compose"
if [ "$HAS_DOCKER" = false ] || [ "$HAS_COMPOSE" = false ]; then
die "Docker and Docker Compose are required for this mode. Please install them first."
fi
generate_env_file
build_and_start_containers
wait_for_healthy
print_docker_success
}
generate_env_file() {
local env_file=".env"
info "Generating ${env_file}..."
# Preserve existing values if .env already exists
local existing_grpc_secret=""
local existing_jwt_secret=""
local existing_encryption_key=""
if [ -f "$env_file" ]; then
existing_grpc_secret=$(grep -E '^SOURCEBRIDGE_GRPC_SECRET=' "$env_file" 2>/dev/null | cut -d= -f2- || true)
existing_jwt_secret=$(grep -E '^SOURCEBRIDGE_JWT_SECRET=' "$env_file" 2>/dev/null | cut -d= -f2- || true)
existing_encryption_key=$(grep -E '^SOURCEBRIDGE_SECURITY_ENCRYPTION_KEY=' "$env_file" 2>/dev/null | cut -d= -f2- || true)
fi
local grpc_secret="${existing_grpc_secret:-$(openssl rand -hex 16 2>/dev/null || head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')}"
local jwt_secret="${existing_jwt_secret:-$(openssl rand -hex 32 2>/dev/null || head -c 64 /dev/urandom | od -An -tx1 | tr -d ' \n')}"
# r1 M4: use openssl rand -hex 32 (macOS-portable; matches JWT/gRPC pattern).
# NOT base64 -w0 (GNU-only). Generates 64 hex chars = 32 bytes of entropy.
local encryption_key="${existing_encryption_key:-$(openssl rand -hex 32 2>/dev/null || head -c 64 /dev/urandom | od -An -tx1 | tr -d ' \n')}"
cat > "$env_file" <<EOF
# SourceBridge.ai — Docker Compose environment
# Generated by setup.sh on $(date -u +"%Y-%m-%dT%H:%M:%SZ")
#
# Re-run ./setup.sh to regenerate, or edit this file directly.
# --- Security ---
SOURCEBRIDGE_GRPC_SECRET=${grpc_secret}
SOURCEBRIDGE_JWT_SECRET=${jwt_secret}
# Encryption key for LLM API keys and git tokens stored in the database.
# 32+ bytes of cryptographic random, hex-encoded. Do NOT share or commit
# this value. Back it up separately — losing it means re-entering all
# encrypted secrets via the admin UI. See docs/admin/llm-config.md.
SOURCEBRIDGE_SECURITY_ENCRYPTION_KEY=${encryption_key}
# --- LLM Provider ---
SOURCEBRIDGE_LLM_PROVIDER=${LLM_PROVIDER}
SOURCEBRIDGE_LLM_MODEL=${LLM_MODEL}
SOURCEBRIDGE_LLM_BASE_URL=${LLM_BASE_URL}
SOURCEBRIDGE_LLM_API_KEY=${LLM_API_KEY}
# --- Embedding Provider ---
# Options: voyage, openai, ollama
# For Ollama embeddings, set:
# SOURCEBRIDGE_EMBEDDING_PROVIDER=ollama
# SOURCEBRIDGE_EMBEDDING_BASE_URL=http://host.docker.internal:11434
# For Voyage AI (recommended for production):
# SOURCEBRIDGE_EMBEDDING_PROVIDER=voyage
# VOYAGE_API_KEY=your-voyage-api-key
SOURCEBRIDGE_EMBEDDING_PROVIDER=${SOURCEBRIDGE_EMBEDDING_PROVIDER:-}
VOYAGE_API_KEY=${VOYAGE_API_KEY:-}
EOF
ok "Created ${env_file}"
}
build_and_start_containers() {
info "Building and starting containers..."
info "This may take several minutes on first run."
echo ""
docker compose up -d --build 2>&1 | while IFS= read -r line; do
printf " %s\n" "$line"
done
echo ""
}
wait_for_healthy() {
local url="http://localhost:8080/healthz"
local timeout=120
local interval=3
local elapsed=0
info "Waiting for API server to become healthy (timeout: ${timeout}s)..."
while [ "$elapsed" -lt "$timeout" ]; do
if curl -sf "$url" >/dev/null 2>&1; then
ok "API server is healthy"
return 0
fi
sleep "$interval"
elapsed=$((elapsed + interval))
printf " ... waiting (%ds / %ds)\n" "$elapsed" "$timeout"
done
warn "API server did not become healthy within ${timeout}s."
warn "This might be normal if images are still building."
echo ""
info "Check container status: docker compose ps"
info "Check API logs: docker compose logs sourcebridge"
info "Check worker logs: docker compose logs worker"
return 1
}
print_docker_success() {
cat <<EOF
${BOLD}=======================================${RESET}
SourceBridge.ai is running.
${BOLD}=======================================${RESET}
Web UI: ${GREEN}http://localhost:3300${RESET}
API server: http://localhost:8080
Health: http://localhost:8080/healthz
Useful commands:
docker compose ps Show container status
docker compose logs -f Follow all logs
docker compose down Stop all services
docker compose up -d Start all services
Configuration:
.env Environment variables
EOF
if [ -z "$LLM_PROVIDER" ]; then
warn "No LLM provider configured. Edit .env to enable AI features."
fi
}
# ---------------------------------------------------------------------------
# Local development setup
# ---------------------------------------------------------------------------
setup_local() {
step "Setting up for local development"
local missing=false
if [ "$HAS_GO" = false ]; then
err "Go 1.25+ is required for source builds."
info "Install: https://go.dev/dl/"
missing=true
fi
if [ "$HAS_PYTHON" = false ] || [ "$HAS_UV" = false ]; then
warn "Python 3.12+ and uv are needed for the worker."
if [ "$HAS_PYTHON" = false ]; then
info "Install Python: https://www.python.org/downloads/"
fi
if [ "$HAS_UV" = false ]; then
info "Install uv: curl -LsSf https://astral.sh/uv/install.sh | sh"
fi
fi
if [ "$HAS_NODE" = false ]; then
warn "Node.js 22+ is needed for the web UI."
info "Install: https://nodejs.org/"
fi
if [ "$missing" = true ]; then
die "Required prerequisites are missing. Install them and re-run this script."
fi
build_local
generate_config_toml
print_local_success
}
build_local() {
info "Building Go API server..."
make build-go 2>&1 | while IFS= read -r line; do printf " %s\n" "$line"; done
ok "Built bin/sourcebridge"
if [ "$HAS_PYTHON" = true ] && [ "$HAS_UV" = true ]; then
info "Installing Python worker dependencies..."
make build-worker 2>&1 | while IFS= read -r line; do printf " %s\n" "$line"; done
ok "Worker dependencies installed"
fi
if [ "$HAS_NODE" = true ]; then
info "Installing web UI dependencies..."
(cd web && npm ci) 2>&1 | while IFS= read -r line; do printf " %s\n" "$line"; done
ok "Web UI dependencies installed"
fi
}
generate_config_toml() {
local config_file="config.toml"
if [ -f "$config_file" ]; then
info "${config_file} already exists. Leaving it unchanged."
return
fi
info "Generating ${config_file}..."
cat > "$config_file" <<EOF
# SourceBridge.ai configuration
# Generated by setup.sh on $(date -u +"%Y-%m-%dT%H:%M:%SZ")
#
# Environment variables override file values. The env prefix is SOURCEBRIDGE_
# with nested keys joined by underscores, e.g. SOURCEBRIDGE_LLM_BASE_URL.
[llm]
EOF
if [ "$LLM_PROVIDER" = "anthropic" ]; then
cat >> "$config_file" <<EOF
provider = "anthropic"
base_url = ""
summary_model = "${LLM_MODEL}"
review_model = "${LLM_MODEL}"
ask_model = "${LLM_MODEL}"
# Set ANTHROPIC_API_KEY in your environment.
EOF
elif [ "$LLM_PROVIDER" = "openai" ]; then
cat >> "$config_file" <<EOF
provider = "openai"
base_url = ""
summary_model = "${LLM_MODEL}"
review_model = "${LLM_MODEL}"
ask_model = "${LLM_MODEL}"
# Set OPENAI_API_KEY in your environment.
EOF
elif [ "$LLM_PROVIDER" = "ollama" ]; then
cat >> "$config_file" <<EOF
provider = "ollama"
base_url = "${LLM_BASE_URL}"
summary_model = "${LLM_MODEL}"
review_model = "${LLM_MODEL}"
ask_model = "${LLM_MODEL}"
EOF
else
cat >> "$config_file" <<EOF
# Uncomment and configure your provider:
# provider = "anthropic"
# base_url = ""
# summary_model = "claude-sonnet-4-20250514"
# review_model = "claude-sonnet-4-20250514"
# ask_model = "claude-sonnet-4-20250514"
EOF
fi
cat >> "$config_file" <<EOF
[server]
http_port = 8080
grpc_port = 50051
[storage]
surreal_mode = "embedded"
[security]
mode = "oss"
EOF
ok "Created ${config_file}"
}
print_local_success() {
cat <<EOF
${BOLD}=======================================${RESET}
SourceBridge.ai is ready for development.
${BOLD}=======================================${RESET}
Start each component in a separate terminal:
${BOLD}Terminal 1 -- API server:${RESET}
make dev
${BOLD}Terminal 2 -- Web UI:${RESET}
make dev-web
${BOLD}Terminal 3 -- Worker:${RESET}
cd workers && uv run python -m workers
Once running:
Web UI: ${GREEN}http://localhost:3300${RESET}
API server: http://localhost:8080
Health: http://localhost:8080/healthz
Configuration:
config.toml Local configuration
Useful commands:
make test Run all tests
make lint Run all linters
make build Build all components
make help Show all make targets
EOF
if [ -z "$LLM_PROVIDER" ]; then
warn "No LLM provider configured. Edit config.toml to enable AI features."
fi
if [ "$LLM_PROVIDER" = "anthropic" ] && [ -z "$LLM_API_KEY" ]; then
warn "Remember to export ANTHROPIC_API_KEY before starting the worker."
fi
if [ "$LLM_PROVIDER" = "openai" ] && [ -z "$LLM_API_KEY" ]; then
warn "Remember to export OPENAI_API_KEY before starting the worker."
fi
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
main() {
print_banner
check_prerequisites
ask_mode
# Validate prerequisites for chosen mode
if [ "$SETUP_MODE" = "docker" ] && { [ "$HAS_DOCKER" = false ] || [ "$HAS_COMPOSE" = false ]; }; then
err "Docker and Docker Compose are required for Docker Compose mode."
echo ""
info "Install Docker: https://docs.docker.com/get-docker/"
info "Install Docker Compose: https://docs.docker.com/compose/install/"
echo ""
prompt "Switch to local development mode instead? [y/N]: "
read -r switch_choice
if [ "$switch_choice" = "y" ] || [ "$switch_choice" = "Y" ]; then
SETUP_MODE="local"
else
die "Cannot proceed without Docker. Install Docker and re-run this script."
fi
fi
ask_llm_provider
case "$SETUP_MODE" in
docker) setup_docker ;;
local) setup_local ;;
esac
}
main "$@"