-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-opencode.sh
More file actions
361 lines (328 loc) · 11.9 KB
/
setup-opencode.sh
File metadata and controls
361 lines (328 loc) · 11.9 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
#!/bin/bash
# ============================================================================
# OpenCode 一键配置脚本
# 使用 Bun 作为运行时,自动安装所有依赖
# 支持 Linux / macOS / WSL
# ============================================================================
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} OpenCode 一键配置脚本 (Bun 版)${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
# ------------------------------------------------------------------
# 目录配置(支持环境变量覆盖)
# ------------------------------------------------------------------
CONFIG_DIR="${OPENCODE_CONFIG_DIR:-$HOME/.config/opencode}"
CLAUDE_DIR="${CLAUDE_CONFIG_DIR:-$HOME/.claude}"
echo -e "${BLUE}目标目录:${NC}"
echo " OpenCode: $CONFIG_DIR"
echo " Claude: $CLAUDE_DIR"
echo ""
# ------------------------------------------------------------------
# 步骤 1: 检测已有配置
# ------------------------------------------------------------------
echo -e "${YELLOW}[1/8] 检测已有配置...${NC}"
if [ -f "$CONFIG_DIR/opencode.json" ] || [ -f "$CONFIG_DIR/oh-my-openagent.json" ]; then
echo -e "${YELLOW}⚠ 发现现有配置文件${NC}"
echo -n "是否备份后重新生成? (y/n) [n]: "
read -r overwrite
overwrite=${overwrite:-n}
if [[ $overwrite =~ ^[Yy]$ ]]; then
backup_dir="$HOME/opencode-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$backup_dir"
[ -f "$CONFIG_DIR/opencode.json" ] && cp "$CONFIG_DIR/opencode.json" "$backup_dir/"
[ -f "$CONFIG_DIR/oh-my-openagent.json" ] && cp "$CONFIG_DIR/oh-my-openagent.json" "$backup_dir/"
[ -f "$CONFIG_DIR/package.json" ] && cp "$CONFIG_DIR/package.json" "$backup_dir/"
echo -e "${GREEN}✓ 已备份到: $backup_dir${NC}"
else
echo "跳过配置生成,使用现有配置。"
SKIP_CONFIG=1
fi
fi
# ------------------------------------------------------------------
# 步骤 2: 创建目录结构
# ------------------------------------------------------------------
echo -e "${YELLOW}[2/8] 创建配置目录...${NC}"
mkdir -p "$CONFIG_DIR"
mkdir -p "$CONFIG_DIR/skills"
mkdir -p "$CLAUDE_DIR"
echo -e "${GREEN}✓ 目录已创建${NC}"
# ------------------------------------------------------------------
# 步骤 3: 生成配置文件
# ------------------------------------------------------------------
if [ "${SKIP_CONFIG:-0}" != "1" ]; then
echo -e "${YELLOW}[3/8] 生成配置文件...${NC}"
# opencode.json
cat > "$CONFIG_DIR/opencode.json" << EOF
{
"\$schema": "https://opencode.ai/config.json",
"plugin": [
"oh-my-openagent@latest",
"superpowers@git+https://github.com/obra/superpowers.git"
],
"provider": {
"anthropic": {
"options": {
"apiKey": "YOUR_API_KEY_HERE",
"baseURL": "https://api.anthropic.com"
}
}
},
"permission": {
"read": {
"~/.config/opencode/*": "allow",
"~/.claude/*": "allow"
},
"external_directory": {
"~/.config/opencode/*": "allow",
"~/.claude/*": "allow"
}
}
}
EOF
echo -e "${GREEN} ✓ opencode.json${NC}"
# oh-my-openagent.json
# 只注册 agent/category 结构,不设 model = 使用源码内置默认模型 + 回退链
# 需要自定义时取消注释或添加 model 字段
cat > "$CONFIG_DIR/oh-my-openagent.json" << 'EOF'
{
"$schema": "https://raw.githubusercontent.com/code-yeongyu/oh-my-openagent/dev/assets/oh-my-opencode.schema.json",
"agents": {
"hephaestus": {},
"oracle": {},
"librarian": {},
"explore": {},
"multimodal-looker": {},
"prometheus": {},
"metis": {},
"momus": {},
"atlas": {},
"sisyphus-junior": {}
},
"categories": {
"visual-engineering": {},
"ultrabrain": {},
"deep": {},
"artistry": {},
"quick": {},
"unspecified-low": {},
"unspecified-high": {},
"writing": {}
}
}
EOF
echo -e "${GREEN} ✓ oh-my-openagent.json(agent 已注册,model 留空=内置默认)${NC}"
# Claude settings
if [ ! -f "$CLAUDE_DIR/settings.json" ]; then
cat > "$CLAUDE_DIR/settings.json" << 'EOF'
{
"hooks": {
"SessionStart": [],
"PostToolUse": [],
"PreToolUse": []
}
}
EOF
echo -e "${GREEN} ✓ settings.json (Claude)${NC}"
else
echo -e "${BLUE} - settings.json 已存在,跳过${NC}"
fi
fi
# ------------------------------------------------------------------
# 步骤 4: 检查前置依赖
# ------------------------------------------------------------------
echo -e "${YELLOW}[4/8] 检查前置依赖...${NC}"
# Bun 安装脚本需要 unzip
if ! command -v unzip &> /dev/null; then
echo -e "${YELLOW}⚠ 缺少 unzip,正在安装...${NC}"
if command -v apt-get &> /dev/null; then
sudo apt-get install -y unzip
elif command -v yum &> /dev/null; then
sudo yum install -y unzip
elif command -v brew &> /dev/null; then
brew install unzip
elif command -v apk &> /dev/null; then
apk add unzip
else
echo -e "${RED}✗ 无法自动安装 unzip${NC}"
echo " 请手动安装后重新运行"
exit 1
fi
echo -e "${GREEN}✓ unzip 安装成功${NC}"
else
echo -e "${GREEN}✓ unzip 已就绪${NC}"
fi
# OpenCode 引导脚本需要 node(找到原生二进制后会切换到原生运行)
if ! command -v node &> /dev/null; then
echo -e "${YELLOW}⚠ 缺少 node,正在安装...${NC}"
if command -v apt-get &> /dev/null; then
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - 2>/dev/null
sudo apt-get install -y nodejs
elif command -v yum &> /dev/null; then
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo -E bash - 2>/dev/null
sudo yum install -y nodejs
elif command -v brew &> /dev/null; then
brew install node
else
echo -e "${RED}✗ 无法自动安装 node${NC}"
echo " 请手动安装后重新运行"
exit 1
fi
if command -v node &> /dev/null; then
echo -e "${GREEN}✓ node 安装成功 ($(node --version))${NC}"
else
echo -e "${RED}✗ node 安装失败,请手动安装${NC}"
exit 1
fi
else
echo -e "${GREEN}✓ node 已就绪 ($(node --version))${NC}"
fi
# ------------------------------------------------------------------
# 步骤 5: 安装 Bun 运行时
# ------------------------------------------------------------------
echo -e "${YELLOW}[5/8] 安装 Bun 运行时...${NC}"
ensure_bun() {
if command -v bun &> /dev/null; then
echo -e "${GREEN}✓ Bun 已安装 ($(bun --version))${NC}"
return 0
fi
if [ -f "$HOME/.bun/bin/bun" ]; then
export PATH="$HOME/.bun/bin:$PATH"
echo -e "${GREEN}✓ Bun 已安装 ($(bun --version))${NC}"
return 0
fi
if [ -f "/usr/local/bin/bun" ]; then
echo -e "${GREEN}✓ Bun 已安装 ($(bun --version))${NC}"
return 0
fi
echo "正在安装 Bun..."
curl -fsSL https://bun.sh/install | bash
if [ -f "$HOME/.bun/bin/bun" ]; then
export PATH="$HOME/.bun/bin:$PATH"
echo -e "${GREEN}✓ Bun 安装成功 ($(bun --version))${NC}"
else
echo -e "${RED}✗ Bun 安装失败,请手动安装: curl -fsSL https://bun.sh/install | bash${NC}"
exit 1
fi
}
ensure_bun
# 确保 bun 在 PATH 中
if ! command -v bun &> /dev/null; then
export PATH="$HOME/.bun/bin:$PATH"
fi
# 如果 shell 配置中还没有 bun 路径,自动写入
BUN_PATH_LINE='export PATH="$HOME/.bun/bin:$PATH"'
if ! grep -q '\.bun/bin' "$HOME/.bashrc" 2>/dev/null; then
echo "" >> "$HOME/.bashrc"
echo "# Bun" >> "$HOME/.bashrc"
echo "$BUN_PATH_LINE" >> "$HOME/.bashrc"
echo -e "${GREEN}✓ Bun 路径已写入 ~/.bashrc${NC}"
fi
# 确保当前会话也能用
export PATH="$HOME/.bun/bin:$PATH"
# ------------------------------------------------------------------
# 步骤 6: 安装 OpenCode
# ------------------------------------------------------------------
echo -e "${YELLOW}[6/8] 安装 OpenCode...${NC}"
# 确保 Bun 路径优先(避免 WSL 下 Windows npm 版本抢在前)
export PATH="$HOME/.bun/bin:$PATH"
if [ -f "$HOME/.bun/bin/opencode" ]; then
echo -e "${GREEN}✓ OpenCode 已安装 ($(opencode --version))${NC}"
else
echo "正在通过 Bun 安装 OpenCode..."
bun install -g opencode-ai
if [ -f "$HOME/.bun/bin/opencode" ]; then
echo -e "${GREEN}✓ OpenCode 安装成功 ($(opencode --version))${NC}"
else
echo -e "${RED}✗ OpenCode 安装失败${NC}"
echo " 请手动安装: bun install -g opencode-ai"
exit 1
fi
fi
# 检查是否有 Windows npm 安装的 opencode 冲突
WINDOWS_OPENCODE=$(command -v opencode 2>/dev/null || true)
if [ -n "$WINDOWS_OPENCODE" ] && echo "$WINDOWS_OPENCODE" | grep -q "/mnt/"; then
echo -e "${YELLOW}⚠ 检测到 WSL 下存在 Windows npm 安装的 opencode${NC}"
echo " 当前优先级: $HOME/.bun/bin > $WINDOWS_OPENCODE"
echo " 如果输入 opencode 仍报错,请检查 PATH 顺序"
fi
# ------------------------------------------------------------------
# 步骤 7: 安装 oh-my-openagent 插件
# ------------------------------------------------------------------
echo -e "${YELLOW}[7/8] 安装 oh-my-openagent 插件...${NC}"
cd "$CONFIG_DIR"
if [ ! -d "node_modules" ] || [ ! -d "node_modules/oh-my-openagent" ]; then
bun add oh-my-openagent@latest 2>&1 | tail -3
echo -e "${GREEN}✓ oh-my-openagent 插件安装完成${NC}"
else
echo -e "${GREEN}✓ oh-my-openagent 插件已存在${NC}"
fi
# ------------------------------------------------------------------
# 步骤 8: 安装 GSD 工作流(可选)
# ------------------------------------------------------------------
echo -e "${YELLOW}[8/8] 安装 GSD 工作流...${NC}"
GSD_DIR="$CONFIG_DIR/get-shit-done"
if [ ! -d "$GSD_DIR" ]; then
if command -v git &> /dev/null; then
echo "正在克隆 GSD 仓库..."
git clone https://github.com/OpenAgentsInc/gsd.git "$GSD_DIR" 2>/dev/null || \
git clone https://github.com/OpenAgentsInc/get-shit-done.git "$GSD_DIR" 2>/dev/null || \
echo -e "${YELLOW}⚠ GSD 克隆失败,可稍后手动安装${NC}"
if [ -d "$GSD_DIR" ]; then
echo -e "${GREEN}✓ GSD 安装完成${NC}"
fi
else
echo -e "${YELLOW}⚠ git 未安装,跳过 GSD${NC}"
echo " 安装 git 后手动执行:"
echo " git clone https://github.com/OpenAgentsInc/gsd.git $GSD_DIR"
fi
else
echo -e "${GREEN}✓ GSD 已存在${NC}"
fi
# 让当前终端也能用 Bun(.bashrc 刚写入的 PATH)
# shellcheck source=/dev/null
. "$HOME/.bashrc" 2>/dev/null || true
# ------------------------------------------------------------------
# 完成
# ------------------------------------------------------------------
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} OpenCode 配置完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}下一步:${NC}"
echo ""
echo " 1. 编辑 API 密钥:"
echo " $EDITOR $CONFIG_DIR/opencode.json"
echo " 将 YOUR_API_KEY_HERE 替换为你的 API 密钥"
echo ""
echo " 2. 如使用 DeepSeek 等兼容 API,修改 baseURL:"
echo ' "baseURL": "https://api.deepseek.com/anthropic"'
echo ""
echo " 3. 调整模型路由(可选):"
echo " $EDITOR $CONFIG_DIR/oh-my-openagent.json"
echo " 为 agent 添加 model 字段即可覆盖默认模型,例如:"
echo ' "oracle": {"model": "deepseek/deepseek-v4-flash"}'
echo ""
echo " 4. 运行 OpenCode:"
echo " opencode"
echo ""
echo " 5. 查看已安装的 skills:"
echo ' skill({name: "superpowers/brainstorming"})'
echo ""
echo -e "${BLUE}配置文件位置:${NC}"
echo " OpenCode: $CONFIG_DIR/opencode.json"
echo " 模型路由: $CONFIG_DIR/oh-my-openagent.json"
echo " Claude 配置: $CLAUDE_DIR/settings.json"
echo " GSD 工作流: $GSD_DIR"
echo ""
echo -e "${YELLOW}⚠ WSL 注意事项:${NC}"
echo " Bun 路径已写入 ~/.bashrc,新终端自动生效"
echo " 如果输入 'opencode' 仍报错 'node: not found',请执行:"
echo " source ~/.bashrc"
echo " 或重启终端"