-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-opencode.sh
More file actions
executable file
·301 lines (254 loc) · 8.97 KB
/
Copy pathsetup-opencode.sh
File metadata and controls
executable file
·301 lines (254 loc) · 8.97 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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'USAGE'
Usage:
./setup-opencode.sh --global
./setup-opencode.sh /absolute/or/relative/path/to/target-project [--force]
What it does:
1. In global mode, installs agents, skills, and core runtime files into the legacy host config paths:
$HOME/.config/opencode/agents/
$HOME/.config/opencode/skills/
$HOME/.config/opencode/config.json
$HOME/.config/opencode/platforms.json
2. In workspace mode, symlinks the legacy host entrypoint .opencode/ -> this runtime/ directory
so the host can discover agents, skills, and config from the stack directly.
3. In workspace mode, symlinks design pack content at the target project root:
<target>/design-systems/ -> <xx-stack>/packs/design/design-systems/
<target>/design-skills/ -> <xx-stack>/packs/design/design-skills/
<target>/DESIGN-CATALOG.md -> <xx-stack>/packs/design/DESIGN-CATALOG.md
Options:
--global Install agents and skills into the legacy host runtime at user level.
--force Overwrite existing symlinks and files without confirmation.
Notes:
- Existing symlinks/files at destination are skipped unless --force is used.
- Global install does not link the design pack; use workspace mode for that.
- Existing user config is merged where possible instead of blindly replaced.
- Repo-managed agent profiles are migrated to host-native execution by clearing legacy per-agent model pins when the stack no longer declares one.
USAGE
}
TARGET_PATH=""
FORCE=0
GLOBAL=0
for arg in "$@"; do
case "$arg" in
--global)
GLOBAL=1
;;
--force)
FORCE=1
;;
-h|--help)
usage
exit 0
;;
*)
if [[ -z "$TARGET_PATH" ]]; then
TARGET_PATH="$arg"
else
echo "Error: unexpected argument '$arg'"
usage
exit 1
fi
;;
esac
done
if [[ $GLOBAL -eq 1 && -n "$TARGET_PATH" ]]; then
echo "Error: use either --global or a target project path, not both."
usage
exit 1
fi
if [[ $GLOBAL -eq 0 && -z "$TARGET_PATH" ]]; then
echo "Error: target project path is required unless --global is used."
usage
exit 1
fi
STACK_DIR="$(cd "$(dirname "$0")" && pwd -P)"
RUNTIME_DIR="$STACK_DIR/runtime"
DESIGN_PACK_DIR="$STACK_DIR/packs/design"
USER_HOST_DIR="$HOME/.config/opencode"
STAMP="$(date +%Y%m%d-%H%M%S)"
if [[ ! -d "$RUNTIME_DIR" ]]; then
echo "Error: runtime directory not found under: $STACK_DIR"
exit 1
fi
if [[ ! -d "$DESIGN_PACK_DIR" ]]; then
echo "Error: packs/design not found under: $STACK_DIR"
exit 1
fi
# ─── Global install ───────────────────────────────────────────────────────────
copy_runtime_file() {
local src="$1"
local dst="$2"
mkdir -p "$(dirname "$dst")"
if [[ -e "$dst" && $FORCE -ne 1 ]]; then
cp "$dst" "$dst.bak.$STAMP"
echo "[xx-stack] Backed up existing file to $dst.bak.$STAMP"
fi
cp -f "$src" "$dst"
}
merge_host_config() {
local repo_config="$1"
local user_config="$2"
node --input-type=module - "$repo_config" "$user_config" <<'NODE'
import fs from "node:fs";
const [repoConfigPath, userConfigPath] = process.argv.slice(2);
const repoConfig = JSON.parse(fs.readFileSync(repoConfigPath, "utf8"));
let userConfig = {};
if (fs.existsSync(userConfigPath)) {
const raw = fs.readFileSync(userConfigPath, "utf8");
if (raw.trim().length > 0) {
userConfig = JSON.parse(raw);
}
}
const next = typeof userConfig === "object" && userConfig !== null && !Array.isArray(userConfig)
? { ...userConfig }
: {};
const repoAgents = typeof repoConfig.agent === "object" && repoConfig.agent !== null ? repoConfig.agent : {};
const userAgents = typeof next.agent === "object" && next.agent !== null ? next.agent : {};
const asRecord = (value) => (value && typeof value === "object" && !Array.isArray(value) ? value : {});
const mergeAgent = (userAgent, repoAgent) => {
const user = asRecord(userAgent);
const repo = asRecord(repoAgent);
const merged = {
...user,
...repo,
toolPolicy: {
...asRecord(user.toolPolicy),
...asRecord(repo.toolPolicy),
},
memory: {
...asRecord(user.memory),
...asRecord(repo.memory),
},
coordinator: {
...asRecord(user.coordinator),
...asRecord(repo.coordinator),
},
permission: {
...asRecord(user.permission),
...asRecord(repo.permission),
skill: {
...asRecord(asRecord(user.permission).skill),
...asRecord(asRecord(repo.permission).skill),
},
},
options: {
...asRecord(user.options),
...asRecord(repo.options),
},
};
if (!("model" in repo)) {
delete merged.model;
}
return merged;
};
next.agent = { ...userAgents };
for (const [agentId, repoAgent] of Object.entries(repoAgents)) {
next.agent[agentId] = mergeAgent(userAgents[agentId], repoAgent);
}
if (typeof next.$schema !== "string" && typeof repoConfig.$schema === "string") {
next.$schema = repoConfig.$schema;
}
fs.writeFileSync(userConfigPath, `${JSON.stringify(next, null, 2)}\n`, "utf8");
NODE
}
if [[ $GLOBAL -eq 1 ]]; then
mkdir -p "$USER_HOST_DIR/agents"
mkdir -p "$USER_HOST_DIR/skills"
echo "[xx-stack] Syncing core runtime files..."
for runtime_file in "$RUNTIME_DIR"/*; do
if [[ -f "$runtime_file" && "$(basename "$runtime_file")" != "config.json" ]]; then
copy_runtime_file "$runtime_file" "$USER_HOST_DIR/$(basename "$runtime_file")"
fi
done
if [[ -f "$USER_HOST_DIR/config.json" && $FORCE -ne 1 ]]; then
cp "$USER_HOST_DIR/config.json" "$USER_HOST_DIR/config.json.bak.$STAMP"
echo "[xx-stack] Backed up existing file to $USER_HOST_DIR/config.json.bak.$STAMP"
fi
echo "[xx-stack] Merging config registry into the user-level host config..."
merge_host_config "$RUNTIME_DIR/config.json" "$USER_HOST_DIR/config.json"
echo "[xx-stack] Installing agents to the user-level host config..."
cp -f "$RUNTIME_DIR"/agents/*.md "$USER_HOST_DIR/agents"/
echo "[xx-stack] Installing skills to the user-level host config..."
# Copy each skill directory. An unmatched glob would stay a literal "*",
# so bail before any partial install instead of failing mid-copy.
shopt -s nullglob
skill_dirs=("$RUNTIME_DIR"/skills/*/)
shopt -u nullglob
if [ "${#skill_dirs[@]}" -eq 0 ]; then
echo "Error: no skill directories found under: $RUNTIME_DIR/skills" >&2
echo "Nothing was installed beyond the runtime files and agents already copied." >&2
exit 1
fi
for skill_dir in "${skill_dirs[@]}"; do
skill_name="$(basename "$skill_dir")"
dest="$USER_HOST_DIR/skills/$skill_name"
rm -rf "$dest"
cp -R -L "$skill_dir" "$dest"
done
echo "[xx-stack] Installation complete."
echo "[xx-stack] Installed to: $USER_HOST_DIR"
exit 0
fi
# ─── Workspace install ────────────────────────────────────────────────────────
TARGET_DIR="$(cd "$TARGET_PATH" && pwd -P)"
SKIP_LEGACY_HOST_LINK=0
SKIP_DESIGN_PACK_LINKS=0
if [[ ! -d "$TARGET_DIR" ]]; then
echo "Error: target directory not found: $TARGET_DIR"
exit 1
fi
if [[ "$TARGET_DIR" == "$STACK_DIR" ]]; then
SKIP_LEGACY_HOST_LINK=1
SKIP_DESIGN_PACK_LINKS=1
fi
LEGACY_HOST_LINK="$TARGET_DIR/.opencode"
# Legacy host symlink
if [[ $SKIP_LEGACY_HOST_LINK -eq 0 ]]; then
if [[ -e "$LEGACY_HOST_LINK" || -L "$LEGACY_HOST_LINK" ]]; then
if [[ $FORCE -ne 1 ]]; then
echo "[xx-stack] $LEGACY_HOST_LINK already exists — skipping (use --force to overwrite)"
else
rm -rf "$LEGACY_HOST_LINK"
ln -s "$RUNTIME_DIR" "$LEGACY_HOST_LINK"
echo "[xx-stack] Linked: $LEGACY_HOST_LINK -> $RUNTIME_DIR"
fi
else
ln -s "$RUNTIME_DIR" "$LEGACY_HOST_LINK"
echo "[xx-stack] Linked: $LEGACY_HOST_LINK -> $RUNTIME_DIR"
fi
else
echo "[xx-stack] Skipped .opencode symlink for the source repo target."
fi
# Design pack symlinks
if [[ $SKIP_DESIGN_PACK_LINKS -eq 0 ]]; then
for entry in design-systems design-skills DESIGN-CATALOG.md; do
src="$DESIGN_PACK_DIR/$entry"
dst="$TARGET_DIR/$entry"
if [[ ! -e "$src" ]]; then
echo "[xx-stack] Warning: design pack source not found, skipping: $src"
continue
fi
if [[ -e "$dst" || -L "$dst" ]]; then
if [[ $FORCE -ne 1 ]]; then
echo "[xx-stack] $dst already exists — skipping (use --force to overwrite)"
continue
fi
rm -rf "$dst"
fi
ln -s "$src" "$dst"
echo "[xx-stack] Linked: $dst -> $src"
done
else
echo "[xx-stack] Skipped design pack symlinks for the source repo target."
fi
echo "[xx-stack] Legacy host workspace ready: $TARGET_DIR"
if [[ $SKIP_LEGACY_HOST_LINK -eq 1 ]]; then
echo "[xx-stack] Source repo target uses runtime in place; no .opencode link created."
else
echo "[xx-stack] Agents and skills discoverable via .opencode -> $RUNTIME_DIR"
fi
if [[ $SKIP_DESIGN_PACK_LINKS -eq 0 ]]; then
echo "[xx-stack] Design pack symlinked at project root."
fi