-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·488 lines (407 loc) · 12.5 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·488 lines (407 loc) · 12.5 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
#!/usr/bin/env bash
set -euo pipefail
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Script directory (agentation root)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OPENCODE_SUBMODULE_DIR="$SCRIPT_DIR/external/opencode/packages/opencode"
OPENCODE_BIN_DIR="$SCRIPT_DIR/.opencode"
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/opencode"
CONFIG_FILE="$CONFIG_DIR/agentation.json"
# Options
BUILD_FROM_SOURCE=false
SKIP_BUILD=false
FORCE=false
PKG_MANAGER=""
print_step() {
echo -e "\n${BLUE}==>${NC} ${1}"
}
print_success() {
echo -e "${GREEN}✓${NC} ${1}"
}
print_warning() {
echo -e "${YELLOW}!${NC} ${1}"
}
print_error() {
echo -e "${RED}✗${NC} ${1}"
}
usage() {
cat <<EOF
Usage: ./setup.sh [OPTIONS]
Options:
--source Build OpenCode from source instead of downloading binary
--skip-build Skip agentation build (use if already built)
--force Force re-download/rebuild even if already installed
-h, --help Show this help message
Examples:
./setup.sh # Download pre-built OpenCode binary
./setup.sh --source # Build OpenCode from source (requires bun)
./setup.sh --force # Re-download even if already installed
EOF
exit 0
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--source)
BUILD_FROM_SOURCE=true
shift
;;
--skip-build)
SKIP_BUILD=true
shift
;;
--force)
FORCE=true
shift
;;
-h|--help)
usage
;;
*)
print_error "Unknown option: $1"
usage
;;
esac
done
# Detect OS and architecture
detect_platform() {
local os arch target
case "$(uname -s)" in
Darwin)
os="darwin"
;;
Linux)
os="linux"
;;
MINGW*|MSYS*|CYGWIN*)
os="windows"
;;
*)
print_error "Unsupported OS: $(uname -s)"
exit 1
;;
esac
case "$(uname -m)" in
x86_64|amd64)
arch="x64"
;;
arm64|aarch64)
arch="arm64"
;;
*)
print_error "Unsupported architecture: $(uname -m)"
exit 1
;;
esac
# macOS Intel doesn't have pre-built binary
if [[ "$os" == "darwin" && "$arch" == "x64" ]]; then
print_warning "macOS Intel (x64) doesn't have pre-built binary. Forcing source build."
BUILD_FROM_SOURCE=true
fi
echo "${os}-${arch}"
}
# Check required commands
check_dependencies() {
print_step "Checking dependencies..."
local missing=()
if ! command -v node &> /dev/null; then
missing+=("node")
else
local node_version=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [[ $node_version -lt 20 ]]; then
print_error "Node.js 20+ required, found v$node_version"
exit 1
fi
print_success "Node.js $(node -v)"
fi
if command -v pnpm &> /dev/null; then
PKG_MANAGER="pnpm"
print_success "pnpm $(pnpm -v)"
elif command -v npm &> /dev/null; then
PKG_MANAGER="npm"
print_success "npm $(npm -v)"
else
missing+=("npm or pnpm")
fi
if [[ "$BUILD_FROM_SOURCE" == true ]]; then
if ! command -v bun &> /dev/null; then
missing+=("bun")
else
print_success "bun $(bun -v)"
fi
fi
if [[ ${#missing[@]} -gt 0 ]]; then
print_error "Missing dependencies: ${missing[*]}"
echo ""
echo "Install missing dependencies:"
for dep in "${missing[@]}"; do
case $dep in
node)
echo " Node.js: https://nodejs.org/ or use nvm"
;;
"npm or pnpm")
echo " npm: comes with Node.js"
echo " pnpm (recommended): npm install -g pnpm"
;;
bun)
echo " bun: curl -fsSL https://bun.sh/install | bash"
;;
esac
done
exit 1
fi
}
# Build agentation packages
build_agentation() {
if [[ "$SKIP_BUILD" == true ]]; then
print_step "Skipping agentation build (--skip-build)"
return
fi
local mcp_dist="$SCRIPT_DIR/packages/mcp-server/dist"
if [[ -d "$mcp_dist" && "$FORCE" != true ]]; then
print_step "Agentation already built"
print_success "Found: $mcp_dist"
echo " Use --force to rebuild"
return
fi
print_step "Building agentation..."
cd "$SCRIPT_DIR"
if [[ ! -d "node_modules" ]]; then
echo "Installing dependencies with $PKG_MANAGER..."
$PKG_MANAGER install
fi
echo "Building packages..."
if [[ "$PKG_MANAGER" == "npm" ]]; then
echo "Building with npm workspaces..."
npm run build -w @agentation/shared
npm run build -w @agentation/mcp-server
npm run build -w @agentation/extension
else
pnpm build
fi
print_success "Agentation built successfully"
}
# Download pre-built OpenCode binary
download_opencode() {
local platform="$1"
local release_url="https://github.com/GutMutCode/opencode/releases/latest/download"
local binary_dir="$OPENCODE_BIN_DIR/opencode-$platform"
if [[ -d "$binary_dir" && "$FORCE" != true ]]; then
print_step "OpenCode binary already exists"
print_success "Found: $binary_dir"
echo " Use --force to re-download"
return
fi
print_step "Downloading OpenCode binary for $platform..."
mkdir -p "$OPENCODE_BIN_DIR"
local archive_name
local extract_cmd
if [[ "$platform" == "windows-x64" ]]; then
archive_name="opencode-${platform}.zip"
extract_cmd="unzip -o -q"
else
archive_name="opencode-${platform}.tar.gz"
extract_cmd="tar -xzf"
fi
local download_url="${release_url}/${archive_name}"
local archive_path="${OPENCODE_BIN_DIR}/${archive_name}"
echo "Downloading from: $download_url"
if command -v curl &> /dev/null; then
curl -fSL "$download_url" -o "$archive_path"
elif command -v wget &> /dev/null; then
wget -q "$download_url" -O "$archive_path"
else
print_error "Neither curl nor wget found"
exit 1
fi
echo "Extracting..."
cd "$OPENCODE_BIN_DIR"
$extract_cmd "$archive_name"
rm "$archive_name"
local version_file="$OPENCODE_BIN_DIR/version"
local latest_version
if command -v curl &> /dev/null; then
latest_version=$(curl -fsSL "https://api.github.com/repos/GutMutCode/opencode/releases/latest" 2>/dev/null | grep -o '"tag_name"[^,]*' | head -1 | cut -d'"' -f4)
fi
[[ -n "$latest_version" ]] && echo "$latest_version" > "$version_file"
print_success "OpenCode binary installed to $binary_dir"
}
# Build OpenCode from source
build_opencode() {
local platform="$1"
local binary_dir="$OPENCODE_SUBMODULE_DIR/dist/opencode-$platform"
if [[ ! -d "$OPENCODE_SUBMODULE_DIR" ]]; then
print_error "OpenCode submodule not found at: $OPENCODE_SUBMODULE_DIR"
echo ""
echo "To build from source, clone with --recursive:"
echo " git clone --recursive https://github.com/GutMutCode/agentation.git"
echo ""
echo "Or initialize submodule:"
echo " git submodule update --init --recursive"
exit 1
fi
if [[ -d "$binary_dir" && "$FORCE" != true ]]; then
print_step "OpenCode already built"
print_success "Found: $binary_dir"
echo " Use --force to rebuild"
return
fi
print_step "Building OpenCode from source..."
cd "$OPENCODE_SUBMODULE_DIR"
if [[ ! -d "node_modules" ]]; then
echo "Installing dependencies..."
bun install
fi
echo "Building..."
bun run build
print_success "OpenCode built successfully"
}
# Configure agentation.json
configure_opencode() {
print_step "Configuring Agentation..."
mkdir -p "$CONFIG_DIR"
local mcp_command="$SCRIPT_DIR/packages/mcp-server/dist/cli.js"
if [[ -f "$CONFIG_FILE" && "$FORCE" != true ]]; then
print_success "Config already exists: $CONFIG_FILE"
echo " Use --force to overwrite"
return
fi
cat > "$CONFIG_FILE" <<EOF
{
"mcp": {
"agentation": {
"type": "local",
"command": ["node", "$mcp_command"]
}
},
"sampling": {
"agentation": {
"mode": "auto",
"maxTokens": 4096
}
}
}
EOF
print_success "Created config: $CONFIG_FILE"
echo " This will be merged with your existing opencode.json settings"
}
# Create wrapper script for easy access
create_symlink() {
local platform="$1"
local bin_dir="$HOME/.local/bin"
local source_bin
mkdir -p "$bin_dir"
if [[ "$BUILD_FROM_SOURCE" == true ]]; then
source_bin="$OPENCODE_SUBMODULE_DIR/dist/opencode-$platform/bin/opencode"
else
source_bin="$OPENCODE_BIN_DIR/opencode-$platform/bin/opencode"
fi
local update_script="$SCRIPT_DIR/update.sh"
if [[ "$platform" == "windows-x64" ]]; then
source_bin="${source_bin}.exe"
local target_cmd="$bin_dir/agentation.cmd"
print_step "Creating wrapper script..."
cat > "$target_cmd" <<EOF
@echo off
if exist "$update_script" (
bash "$update_script" --inline
)
set OPENCODE_CONFIG=$CONFIG_FILE
"$source_bin" %*
EOF
print_success "Created: $target_cmd"
local win_bin_dir
win_bin_dir=$(cygpath -w "$bin_dir" 2>/dev/null || echo "$bin_dir")
echo ""
print_warning "Add to PATH (run in PowerShell as Admin):"
echo ""
echo " [Environment]::SetEnvironmentVariable('Path', \$env:Path + ';$win_bin_dir', 'User')"
echo ""
echo " Or manually: Settings → System → About → Advanced → Environment Variables"
echo ""
return
fi
local target_bin="$bin_dir/agentation"
print_step "Creating wrapper script..."
if [[ -L "$target_bin" || -e "$target_bin" ]]; then
rm -f "$target_bin"
fi
cat > "$target_bin" <<EOF
#!/bin/bash
if [[ -f "$update_script" ]]; then
"$update_script" --inline || true
fi
export OPENCODE_CONFIG="$CONFIG_FILE"
exec "$source_bin" "\$@"
EOF
chmod +x "$target_bin"
print_success "Created: $target_bin"
if [[ ":$PATH:" != *":$bin_dir:"* ]]; then
echo ""
print_warning "~/.local/bin is not in PATH"
echo " Add to your shell config (~/.bashrc, ~/.zshrc, etc.):"
echo ""
echo " export PATH=\"\$HOME/.local/bin:\$PATH\""
echo ""
fi
}
# Print final instructions
print_instructions() {
local platform="$1"
echo ""
echo -e "${GREEN}============================================${NC}"
echo -e "${GREEN} Setup Complete!${NC}"
echo -e "${GREEN}============================================${NC}"
echo ""
echo -e "${YELLOW}Remaining manual step:${NC} Load Chrome Extension"
echo ""
echo " 1. Open Chrome and go to: chrome://extensions/"
echo " 2. Enable 'Developer mode' (top right toggle)"
echo " 3. Click 'Load unpacked'"
echo " 4. Select: $SCRIPT_DIR/packages/extension"
echo ""
echo -e "${YELLOW}To start:${NC}"
echo ""
echo " agentation"
echo ""
echo -e "${YELLOW}Usage:${NC}"
echo ""
echo " 1. Open any webpage in Chrome"
echo " 2. Click the floating button (bottom-right) to expand toolbar"
echo " 3. Enable annotation mode (click the crosshair toggle)"
echo " 4. Click elements to annotate and enter feedback"
echo " 5. Click 'Send to AI' to submit feedback to OpenCode"
echo ""
}
# Main
main() {
echo -e "${BLUE}"
echo " _ _ _ _ "
echo " / \ __ _ ___ _ __ | |_ __ _| |_(_) ___ _ __ "
echo " / _ \ / _\` |/ _ \ '_ \| __/ _\` | __| |/ _ \| '_ \ "
echo " / ___ \ (_| | __/ | | | || (_| | |_| | (_) | | | |"
echo "/_/ \_\__, |\___|_| |_|\__\__,_|\__|_|\___/|_| |_|"
echo " |___/ "
echo -e "${NC}"
echo "AI-powered UI Feedback System"
echo ""
local platform
platform=$(detect_platform)
echo "Detected platform: $platform"
check_dependencies
build_agentation
if [[ "$BUILD_FROM_SOURCE" == true ]]; then
build_opencode "$platform"
else
download_opencode "$platform"
fi
configure_opencode
create_symlink "$platform"
print_instructions "$platform"
}
main "$@"