Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·337 lines (314 loc) · 10.8 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·337 lines (314 loc) · 10.8 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
#!/usr/bin/env bash
set -euo pipefail
root=$(cd -- "$(dirname -- "$0")" && pwd)
agent=auto
install_tools=1
upgrade_tools=0
binaries=0
usage() {
printf 'usage: %s [--agent auto|claude|codex|cursor|gemini|copilot|windsurf|grok|opencode|qwen|cline|kilo|crush|goose] [--configure-only|--upgrade] [--binaries]\n' "$0" >&2
}
while [ "$#" -gt 0 ]; do
case "$1" in
--agent)
[ "$#" -ge 2 ] || { usage; exit 2; }
agent="$2"
shift 2
;;
--configure-only)
install_tools=0
shift
;;
--upgrade)
upgrade_tools=1
shift
;;
--binaries)
binaries=1
shift
;;
*)
usage
exit 2
;;
esac
done
[ "${install_tools}" -eq 1 ] || { [ "${upgrade_tools}" -eq 0 ] && [ "${binaries}" -eq 0 ]; } || { usage; exit 2; }
case "${agent}" in
claude-code) agent=claude ;;
cursor-agent) agent=cursor ;;
gemini-cli) agent=gemini ;;
github-copilot) agent=copilot ;;
grok-build) agent=grok ;;
open-code) agent=opencode ;;
qwen-code) agent=qwen ;;
kilocode | kilo-code) agent=kilo ;;
esac
case "${agent}" in
auto | claude | codex | cursor | gemini | copilot | windsurf | grok | opencode | qwen | cline | kilo | crush | goose) ;;
*) usage; exit 2 ;;
esac
# On Linux the tools come from upstream release binaries unless Homebrew is
# already there; --binaries chooses them even then. macOS always uses Homebrew.
os="${AGENT_READY_OS:-$(uname -s)}"
if [ "${install_tools}" -eq 1 ] && [ "${os}" = Linux ] &&
{ [ "${binaries}" -eq 1 ] || ! command -v brew >/dev/null 2>&1; }; then
if [ "${upgrade_tools}" -eq 1 ]; then
bash "${root}/install-binaries.sh" --upgrade
else
bash "${root}/install-binaries.sh"
fi
PATH="${AGENT_READY_BIN_DIR:-${HOME}/.local/bin}:${PATH}"
elif [ "${install_tools}" -eq 1 ]; then
[ "${binaries}" -eq 0 ] || {
printf -- '--binaries is supported on Linux only; use Homebrew on %s\n' "${os}" >&2
exit 2
}
command -v brew >/dev/null 2>&1 || {
printf 'Homebrew is required: https://brew.sh\n' >&2
exit 1
}
if [ "${upgrade_tools}" -eq 1 ]; then
brew bundle --file "${root}/Brewfile"
else
brew bundle --no-upgrade --file "${root}/Brewfile"
fi
fi
# Installing a tool is not the same as the agent using it. Measured on real
# session transcripts, an agent reaches for the command it already knows —
# `grep` over `rg`, `git worktree` over a wrapper — unless something tells it
# otherwise. This writes that "something" into each harness's instruction file,
# between markers, leaving everything else in the file untouched.
base_routing="${root}/agent-routing.md"
[ -f "${base_routing}" ] || {
printf 'missing %s\n' "${base_routing}" >&2
exit 1
}
routing="${base_routing}"
routing_tmp=
agent_ready_config="${XDG_CONFIG_HOME:-${HOME}/.config}/agent-ready"
if [ -f "${agent_ready_config}/languages/go" ]; then
go_routing="${root}/languages/go/agent-routing.md"
[ -f "${go_routing}" ] || {
printf 'missing %s\n' "${go_routing}" >&2
exit 1
}
routing_tmp=$(mktemp)
{
cat "${base_routing}"
printf '\n'
cat "${go_routing}"
} >"${routing_tmp}"
routing="${routing_tmp}"
fi
cleanup_routing() { [ -z "${routing_tmp}" ] || rm -f "${routing_tmp}"; }
trap cleanup_routing EXIT
command -v rtk >/dev/null 2>&1 || {
printf 'rtk is not installed; run %s first\n' "$0" >&2
exit 1
}
start='<!-- agent-ready:start -->'
end='<!-- agent-ready:end -->'
resolve_target() {
local path="$1" link dir hops=0
while [ -L "${path}" ]; do
hops=$((hops + 1))
[ "${hops}" -le 20 ] || {
printf 'too many symlinks while resolving %s\n' "$1" >&2
return 1
}
link=$(readlink "${path}")
case "${link}" in
/*) path="${link}" ;;
*)
dir=$(cd -- "$(dirname -- "${path}")" && pwd -P)
path="${dir}/${link}"
;;
esac
done
printf '%s\n' "${path}"
}
block_state() {
awk -v s="${start}" -v e="${end}" '
$0 == s { starts++; start_line = NR }
$0 == e { ends++; end_line = NR }
END {
if (starts == 0 && ends == 0) print "absent"
else if (starts == 1 && ends == 1 && start_line < end_line) print "present"
else print "invalid"
}
' "$1"
}
install_block() {
local requested="$1" label="$2" target tmp state
mkdir -p "$(dirname -- "${requested}")"
target=$(resolve_target "${requested}")
mkdir -p "$(dirname -- "${target}")"
[ -f "${target}" ] || : >"${target}"
state=$(block_state "${target}")
[ "${state}" != invalid ] || {
printf '%s: malformed agent-ready markers in %s\n' "${label}" "${requested}" >&2
return 1
}
tmp=$(mktemp "${target}.XXXXXX") || return 1
cp -p "${target}" "${tmp}"
if [ "${state}" = present ]; then
awk -v s="${start}" -v e="${end}" -v f="${routing}" '
$0 == s { print; while ((getline line < f) > 0) print line; skip = 1; next }
$0 == e { skip = 0 }
!skip { print }
' "${target}" >"${tmp}"
else
{
cat "${target}"
[ -s "${target}" ] && printf '\n'
printf '%s\n' "${start}"
cat "${routing}"
printf '%s\n' "${end}"
} >"${tmp}"
fi
if cmp -s "${target}" "${tmp}"; then
rm -f "${tmp}"
printf '%s: routing already configured in %s\n' "${label}" "${requested}"
return 0
fi
mv "${tmp}" "${target}"
printf '%s: %s routing block in %s\n' "${label}" "$([ "${state}" = present ] && printf refreshed || printf added)" "${requested}"
}
remove_block() {
local requested="$1" label="$2" target tmp state
[ -e "${requested}" ] || [ -L "${requested}" ] || return 0
target=$(resolve_target "${requested}")
[ -f "${target}" ] || return 0
state=$(block_state "${target}")
[ "${state}" != invalid ] || {
printf '%s: malformed agent-ready markers in %s\n' "${label}" "${requested}" >&2
return 1
}
[ "${state}" = present ] || return 0
tmp=$(mktemp "${target}.XXXXXX") || return 1
cp -p "${target}" "${tmp}"
awk -v s="${start}" -v e="${end}" '
$0 == s { skip = 1; next }
$0 == e { skip = 0; next }
!skip { print }
' "${target}" >"${tmp}"
mv "${tmp}" "${target}"
printf '%s: removed legacy routing block from %s\n' "${label}" "${requested}"
}
configure_rtk() {
local label="$1"
shift
rtk init "$@"
printf '%s: configured RTK integration\n' "${label}"
}
install_cursor_hook() {
local cursor_dir="${HOME}/.cursor" response hooks requested tmp command
command -v jq >/dev/null 2>&1 || {
printf 'Cursor: jq is required to merge hooks.json\n' >&2
return 1
}
mkdir -p "${cursor_dir}/hooks"
response="${cursor_dir}/hooks/agent-ready-session-start.json"
tmp=$(mktemp "${response}.XXXXXX") || return 1
jq -Rs '{additional_context: .}' "${routing}" >"${tmp}"
if [ -f "${response}" ] && cmp -s "${response}" "${tmp}"; then
rm -f "${tmp}"
else
mv "${tmp}" "${response}"
fi
requested="${cursor_dir}/hooks.json"
hooks=$(resolve_target "${requested}")
mkdir -p "$(dirname -- "${hooks}")"
tmp=$(mktemp "${hooks}.XXXXXX") || return 1
command='cat ./hooks/agent-ready-session-start.json'
if [ -f "${hooks}" ]; then
cp -p "${hooks}" "${tmp}"
jq --arg command "${command}" '
if type != "object" then error("hooks.json root must be an object") else . end
| .hooks = (.hooks // {})
| if ((.hooks.sessionStart // []) | type) != "array"
then error("hooks.sessionStart must be an array") else . end
| .version = (.version // 1)
| .hooks.sessionStart = ([.hooks.sessionStart[]?
| select((type != "object") or (.command != $command))]
+ [{"command": $command}])
' "${hooks}" >"${tmp}" || {
rm -f "${tmp}"
return 1
}
else
jq -n --arg command "${command}" '{version: 1, hooks: {sessionStart: [{command: $command}]}}' >"${tmp}"
fi
if [ -f "${hooks}" ] && cmp -s "${hooks}" "${tmp}"; then
rm -f "${tmp}"
printf 'Cursor: routing hook already configured in %s\n' "${requested}"
else
chmod 600 "${tmp}"
mv "${tmp}" "${hooks}"
printf 'Cursor: configured routing hook in %s\n' "${requested}"
fi
remove_block "${cursor_dir}/AGENTS.md" "Cursor"
}
found=0
if [ "${agent}" = claude ] || { [ "${agent}" = auto ] && command -v claude >/dev/null 2>&1; }; then
found=1
configure_rtk "Claude Code" -g --hook-only --auto-patch --no-trust-filters
install_block "${CLAUDE_CONFIG_DIR:-${HOME}/.claude}/CLAUDE.md" "Claude Code"
fi
if [ "${agent}" = codex ] || { [ "${agent}" = auto ] && command -v codex >/dev/null 2>&1; }; then
found=1
configure_rtk "Codex" -g --codex --no-trust-filters
install_block "${CODEX_HOME:-${HOME}/.codex}/AGENTS.md" "Codex"
fi
if [ "${agent}" = cursor ] || { [ "${agent}" = auto ] && { command -v cursor >/dev/null 2>&1 || command -v cursor-agent >/dev/null 2>&1 || [ -d /Applications/Cursor.app ] || [ -d "${HOME}/Applications/Cursor.app" ]; }; }; then
found=1
mkdir -p "${HOME}/.cursor"
configure_rtk "Cursor" -g --agent cursor --hook-only --no-patch --no-trust-filters >/dev/null
printf 'Cursor: configured RTK integration\n'
install_cursor_hook
fi
if [ "${agent}" = gemini ] || { [ "${agent}" = auto ] && command -v gemini >/dev/null 2>&1; }; then
found=1
configure_rtk "Gemini CLI" -g --gemini --hook-only --auto-patch --no-trust-filters
install_block "${HOME}/.gemini/GEMINI.md" "Gemini CLI"
fi
if [ "${agent}" = copilot ] || { [ "${agent}" = auto ] && command -v copilot >/dev/null 2>&1; }; then
found=1
configure_rtk "GitHub Copilot CLI" -g --copilot --hook-only --auto-patch --no-trust-filters
install_block "${COPILOT_HOME:-${HOME}/.copilot}/copilot-instructions.md" "GitHub Copilot CLI"
fi
if [ "${agent}" = windsurf ] || { [ "${agent}" = auto ] && { command -v windsurf >/dev/null 2>&1 || [ -d /Applications/Windsurf.app ] || [ -d "${HOME}/Applications/Windsurf.app" ]; }; }; then
found=1
install_block "${HOME}/.codeium/windsurf/memories/global_rules.md" "Windsurf"
fi
if [ "${agent}" = grok ] || { [ "${agent}" = auto ] && command -v grok >/dev/null 2>&1; }; then
found=1
install_block "${GROK_HOME:-${HOME}/.grok}/rules/agent-ready.md" "Grok Build"
fi
if [ "${agent}" = opencode ] || { [ "${agent}" = auto ] && command -v opencode >/dev/null 2>&1; }; then
found=1
configure_rtk "OpenCode" -g --opencode --no-trust-filters
install_block "${XDG_CONFIG_HOME:-${HOME}/.config}/opencode/AGENTS.md" "OpenCode"
fi
if [ "${agent}" = qwen ] || { [ "${agent}" = auto ] && command -v qwen >/dev/null 2>&1; }; then
found=1
install_block "${QWEN_HOME:-${HOME}/.qwen}/QWEN.md" "Qwen Code"
fi
if [ "${agent}" = cline ] || { [ "${agent}" = auto ] && command -v cline >/dev/null 2>&1; }; then
found=1
install_block "${CLINE_DATA_DIR:-${HOME}/.cline/data}/settings/rules/agent-ready.md" "Cline CLI"
fi
if [ "${agent}" = kilo ] || { [ "${agent}" = auto ] && command -v kilo >/dev/null 2>&1; }; then
found=1
install_block "${XDG_CONFIG_HOME:-${HOME}/.config}/kilo/AGENTS.md" "Kilo Code"
fi
if [ "${agent}" = crush ] || { [ "${agent}" = auto ] && command -v crush >/dev/null 2>&1; }; then
found=1
install_block "${XDG_CONFIG_HOME:-${HOME}/.config}/crush/CRUSH.md" "Crush"
fi
if [ "${agent}" = goose ] || { [ "${agent}" = auto ] && command -v goose >/dev/null 2>&1; }; then
found=1
install_block "${XDG_CONFIG_HOME:-${HOME}/.config}/goose/AGENTS.md" "goose"
fi
[ "${found}" -eq 1 ] || printf 'No supported harness found; rerun %s --configure-only after installing one\n' "$0"