@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
sh 975 lines 37.5 KB
Raw
1 #!/bin/sh
2 set -eu
3
4 error() {
5 printf 'error: %s\n' "$*" >&2
6 exit 1
7 }
8
9 warn() {
10 printf 'warning: %s\n' "$*" >&2
11 }
12
13 is_true() {
14 case ${1:-} in
15 1 | true) return 0 ;;
16 *) return 1 ;;
17 esac
18 }
19
20 info() {
21 if ! is_true "${COMPOSIO_QUIET:-}"; then
22 printf '%s\n' "$*"
23 fi
24 }
25
26 debug() {
27 if is_true "${COMPOSIO_DEBUG:-}"; then
28 printf '+ %s\n' "$*" >&2
29 fi
30 }
31
32 # Replays a captured subprocess log on the debug channel. The installer owns
33 # its own presentation, so delegated output is suppressed by default and stays
34 # available for troubleshooting through COMPOSIO_DEBUG.
35 debug_captured_output() {
36 debug_captured_label=$1
37 debug_captured_file=$2
38 if ! is_true "${COMPOSIO_DEBUG:-}" || [ ! -s "$debug_captured_file" ]; then
39 return 0
40 fi
41 debug "$debug_captured_label"
42 sed 's/^/+ /' "$debug_captured_file" >&2 || :
43 }
44
45 tildify() {
46 case $1 in
47 "$HOME"/*) printf '%s/%s\n' '~' "${1#"$HOME"/}" ;;
48 *) printf '%s\n' "$1" ;;
49 esac
50 }
51
52 print_usage() {
53 printf '%s\n' \
54 'Usage: install.sh [--agent] [--no-plugins] [version-tag]' \
55 '' \
56 'Options:' \
57 ' --agent Sign up or log in as a Composio agent after installation.' \
58 ' --no-plugins Skip agent plugin installation.' \
59 ' -h, --help Show this help.' \
60 '' \
61 'Set COMPOSIO_INSTALL_SHELL=auto|zsh|bash|fish|none to control automatic shell setup' \
62 "(default auto: detect the login shell from \$SHELL; none: install only)." \
63 'Version tags may be stable or beta, for example 0.3.1 or @composio/cli@0.3.1-beta.329.'
64 }
65
66 validate_identifier() {
67 printf '%s\n' "$2" | grep -Eq '^[A-Za-z0-9._-]+$' ||
68 error "$1 contains invalid characters (got \"$2\")"
69 }
70
71 url_authority() {
72 printf '%s\n' "$1" | sed -e 's#^[a-zA-Z][a-zA-Z0-9+.-]*://##' -e 's#[/?#].*$##'
73 }
74
75 is_allowed_http_authority() {
76 case $1 in
77 localhost | localhost:* | 127.0.0.1 | 127.0.0.1:* | '[::1]' | '[::1]':*) return 0 ;;
78 esac
79
80 if [ -n "${COMPOSIO_INSTALL_ALLOW_HTTP_HOST:-}" ]; then
81 case $1 in
82 "$COMPOSIO_INSTALL_ALLOW_HTTP_HOST" | "$COMPOSIO_INSTALL_ALLOW_HTTP_HOST":*) return 0 ;;
83 esac
84 fi
85
86 return 1
87 }
88
89 validate_url() {
90 validate_url_value=$1
91 case $validate_url_value in
92 *[![:print:]]* | *[[:space:]]*) return 1 ;;
93 esac
94
95 validate_url_authority=$(url_authority "$validate_url_value")
96 [ -n "$validate_url_authority" ] || return 1
97 case $validate_url_authority in
98 *@*) return 1 ;;
99 esac
100
101 case $validate_url_value in
102 https://*) return 0 ;;
103 http://*) is_allowed_http_authority "$validate_url_authority" ;;
104 *) return 1 ;;
105 esac
106 }
107
108 # Callers must validate_url first, so the scheme is https, or http on an allowed host.
109 curl_proto_flags() {
110 case $1 in
111 https://*) printf '%s\n' '=https' ;;
112 *) printf '%s\n' '=http,https' ;;
113 esac
114 }
115
116 curl_fetch() {
117 curl_fetch_url=$1
118 validate_url "$curl_fetch_url" || error "Refusing unsafe URL \"$curl_fetch_url\""
119 debug "curl GET $curl_fetch_url"
120 curl --fail --silent --location --proto "$(curl_proto_flags "$curl_fetch_url")" \
121 --proto-redir '=https' "$curl_fetch_url"
122 }
123
124 curl_download() {
125 curl_download_url=$1
126 curl_download_output=$2
127 curl_download_quiet=${3:-0}
128 validate_url "$curl_download_url" || error "Refusing unsafe URL \"$curl_download_url\""
129 debug "curl GET $curl_download_url -> $curl_download_output"
130
131 if [ "$curl_download_quiet" = 1 ] || is_true "${COMPOSIO_QUIET:-}"; then
132 curl_download_ui=--silent
133 else
134 curl_download_ui=--progress-bar
135 fi
136
137 curl --fail --location "$curl_download_ui" --proto "$(curl_proto_flags "$curl_download_url")" \
138 --proto-redir '=https' --output "$curl_download_output" "$curl_download_url"
139 }
140
141 normalize_version() {
142 normalize_version_value=$1
143 case $normalize_version_value in
144 @composio/cli@*) normalize_version_bare=${normalize_version_value#@composio/cli@} ;;
145 *) normalize_version_bare=$normalize_version_value ;;
146 esac
147
148 printf '%s\n' "$normalize_version_bare" |
149 grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+(-beta\.[0-9]+)?$' ||
150 error "Invalid Composio CLI version \"$normalize_version_value\". Expected X.Y.Z or X.Y.Z-beta.N."
151
152 printf '@composio/cli@%s\n' "$normalize_version_bare"
153 }
154
155 resolve_latest_cli_release() {
156 resolve_page=1
157 while [ "$resolve_page" -le 5 ]; do
158 resolve_url="$github_api_repo/releases?per_page=100&page=$resolve_page"
159 resolve_json=$(curl_fetch "$resolve_url") || return 1
160 resolve_release=$(printf '%s\n' "$resolve_json" |
161 sed 's/"tag_name"/\
162 "tag_name"/g; s/"browser_download_url"/\
163 "browser_download_url"/g' |
164 awk -v asset_name="$archive_name" '
165 BEGIN {
166 tag = ""
167 stable = "^@composio/cli@[0-9]+\\.[0-9]+\\.[0-9]+$"
168 }
169 /"tag_name":[[:space:]]*"/ {
170 tag = $0
171 sub(/^.*"tag_name":[[:space:]]*"/, "", tag)
172 sub(/".*$/, "", tag)
173 if (tag !~ stable) tag = ""
174 }
175 tag != "" && /"browser_download_url":[[:space:]]*"/ && index($0, "/" asset_name "\"") > 0 {
176 url = $0
177 sub(/^.*"browser_download_url":[[:space:]]*"/, "", url)
178 sub(/".*$/, "", url)
179 print tag
180 print url
181 exit
182 }
183 ')
184
185 if [ -n "$resolve_release" ]; then
186 printf '%s\n' "$resolve_release"
187 return 0
188 fi
189
190 printf '%s\n' "$resolve_json" | grep -q '"tag_name"' || break
191 resolve_page=$((resolve_page + 1))
192 done
193
194 return 1
195 }
196
197 detect_target() {
198 platform=$(uname -ms)
199 case $platform in
200 'MINGW64'* | 'MSYS'* | 'CYGWIN'*)
201 error 'Run this installer inside WSL, or try the experimental native build from PowerShell: irm https://raw.githubusercontent.com/ComposioHQ/composio/next/install.ps1 | iex'
202 ;;
203 'Darwin x86_64') target=darwin-x64 ;;
204 'Darwin arm64') target=darwin-aarch64 ;;
205 'Linux aarch64' | 'Linux arm64') target=linux-aarch64 ;;
206 'Linux x86_64') target=linux-x64 ;;
207 *) error "Unsupported platform: $platform" ;;
208 esac
209
210 if [ "$target" = darwin-x64 ]; then
211 translated=$(sysctl -n sysctl.proc_translated 2>/dev/null || printf '0')
212 if [ "$translated" = 1 ]; then
213 target=darwin-aarch64
214 info "Your shell is running in Rosetta 2. Downloading for $target instead"
215 fi
216 fi
217 }
218
219 verify_checksum() {
220 checksum_archive=$1
221 checksum_manifest=$2
222 checksum_name=$3
223 checksum_expected=$(awk -v name="$checksum_name" '$2 == name || $2 == "*" name { print $1; exit }' "$checksum_manifest")
224
225 if [ -z "$checksum_expected" ]; then
226 if [ "$official_release_source" = 1 ]; then
227 error "checksums.txt has no entry for $checksum_name. Official releases always publish complete checksums, so this release cannot be verified. Refusing to install."
228 fi
229 warn "No checksum entry found for $checksum_name; continuing without verification"
230 return 0
231 fi
232 printf '%s\n' "$checksum_expected" | grep -Eq '^[0-9a-fA-F]{64}$' ||
233 error "Malformed checksum for $checksum_name"
234
235 if command -v sha256sum >/dev/null 2>&1; then
236 checksum_actual=$(sha256sum "$checksum_archive" | awk '{ print $1 }')
237 elif command -v shasum >/dev/null 2>&1; then
238 checksum_actual=$(shasum -a 256 "$checksum_archive" | awk '{ print $1 }')
239 else
240 warn 'Checksum verification skipped: no SHA-256 utility (sha256sum or shasum) is available on this system'
241 return 0
242 fi
243
244 [ "$checksum_expected" = "$checksum_actual" ] ||
245 error "Checksum mismatch for $checksum_name (expected $checksum_expected, got $checksum_actual)"
246 info 'Checksum verified'
247 }
248
249 resolve_directory() {
250 resolve_directory_value=$1
251 mkdir -p "$resolve_directory_value" || error "Failed to create directory \"$resolve_directory_value\""
252 (cd "$resolve_directory_value" && pwd -P)
253 }
254
255 publish_staged_entry() {
256 publish_source=$1
257 publish_name=${publish_source##*/}
258 publish_target=$resolved_install_dir/$publish_name
259
260 if [ -d "$publish_source" ] && [ ! -L "$publish_source" ] &&
261 { [ -e "$publish_target" ] || [ -L "$publish_target" ]; }; then
262 publish_aside=$stage/.composio-aside.$publish_name
263 mv "$publish_target" "$publish_aside" ||
264 error "Failed to move existing install entry aside: $publish_target"
265 if mv "$publish_source" "$publish_target"; then
266 rm -rf "$publish_aside" ||
267 warn "Published install entry; previous contents retained at $publish_aside"
268 return 0
269 fi
270
271 if mv "$publish_aside" "$publish_target"; then
272 error "Failed to publish install entry: $publish_target"
273 fi
274
275 preserve_stage=1
276 error "Failed to publish install entry and restore the previous entry. Recover it from $publish_aside"
277 fi
278
279 mv "$publish_source" "$publish_target" || error "Failed to publish install entry: $publish_target"
280 }
281
282 install_bundle() {
283 install_bundle_root=$1
284 install_bundle_dir=$install_bundle_root/composio-$target
285
286 if [ ! -f "$install_bundle_dir/composio" ]; then
287 rm -f "$install_bundle_root/$archive_name" "$install_bundle_root/checksums.txt"
288 install_bundle_dir=$install_bundle_root
289 fi
290 [ -f "$install_bundle_dir/composio" ] || error 'Binary not found in extracted archive'
291
292 if ! find "$install_bundle_dir" -mindepth 1 ! -name composio -print -quit | grep -q .; then
293 warn 'This release archive has no bundled support files. Some CLI features may be unavailable.'
294 fi
295
296 stage=$(mktemp -d "$resolved_install_dir/.composio-install.XXXXXX") ||
297 error "Failed to create staging directory in \"$resolved_install_dir\""
298 debug "install staging directory: $stage"
299
300 # Staging briefly holds a second copy of the bundle until the binary is published last.
301 cp -Rp "$install_bundle_dir"/. "$stage/" ||
302 error "Failed to stage the CLI bundle in \"$resolved_install_dir\""
303 chmod +x "$stage/composio" || error 'Failed to set permissions on staged executable'
304 printf '%s\n' "$version" >"$stage/release-tag.txt" ||
305 error "Failed to stage install metadata in \"$resolved_install_dir\""
306
307 for staged_entry in "$stage"/* "$stage"/.[!.]* "$stage"/..?*; do
308 if [ ! -e "$staged_entry" ] && [ ! -L "$staged_entry" ]; then
309 continue
310 fi
311 case ${staged_entry##*/} in
312 composio | release-tag.txt) continue ;;
313 esac
314 publish_staged_entry "$staged_entry"
315 done
316
317 publish_staged_entry "$stage/release-tag.txt"
318 publish_staged_entry "$stage/composio"
319 rmdir "$stage" || warn "Published CLI; retained recovery staging directory at $stage"
320 stage=
321
322 # Remove legacy sidecars only when the selected release omits them.
323 if [ ! -d "$install_bundle_dir/local-tools-binaries" ]; then
324 rm -rf "$resolved_install_dir/local-tools-binaries" ||
325 warn "Could not remove obsolete $resolved_install_dir/local-tools-binaries"
326 fi
327 }
328
329 install_entry_point() {
330 entry_point=$resolved_bin_dir/composio
331 if [ "$resolved_bin_dir" = "$resolved_install_dir" ]; then
332 return 0
333 fi
334 if [ -d "$entry_point" ]; then
335 error "Cannot replace entry point \"$entry_point\" because it is a directory"
336 fi
337 ln -sf "$resolved_install_dir/composio" "$entry_point" ||
338 error "Failed to create entry point \"$entry_point\""
339 }
340
341 inherited_path_contains_bin_dir() {
342 case :$inherited_path: in
343 *:"$resolved_bin_dir":*) return 0 ;;
344 *) return 1 ;;
345 esac
346 }
347
348 # Renders a value as one POSIX-safe shell word so recovery commands stay
349 # copy-paste safe even when the installed path contains whitespace.
350 shell_quote() {
351 case $1 in
352 '') printf "''\n" ;;
353 *[!A-Za-z0-9_./-]*) printf '%s\n' "$1" | sed "s/'/'\\\\''/g; s/^/'/; s/\$/'/" ;;
354 *) printf '%s\n' "$1" ;;
355 esac
356 }
357
358 # Follows symlinks and resolves the parent directory physically, so two paths
359 # compare equal exactly when they name the same executable: the final block's
360 # installed-vs-shadowed verdict must not change just because one side reaches
361 # the binary through a symlink alias.
362 resolve_physical_path() {
363 resolve_physical_target=$1
364 resolve_physical_steps=0
365 while [ -L "$resolve_physical_target" ] && [ "$resolve_physical_steps" -lt 40 ]; do
366 resolve_physical_link=$(readlink "$resolve_physical_target") || break
367 case $resolve_physical_link in
368 /*) resolve_physical_target=$resolve_physical_link ;;
369 *) resolve_physical_target=$(dirname "$resolve_physical_target")/$resolve_physical_link ;;
370 esac
371 resolve_physical_steps=$((resolve_physical_steps + 1))
372 done
373 resolve_physical_base=$(basename "$resolve_physical_target")
374 if resolve_physical_dir=$(cd "$(dirname "$resolve_physical_target")" 2>/dev/null && pwd -P); then
375 printf '%s/%s\n' "$resolve_physical_dir" "$resolve_physical_base"
376 else
377 printf '%s\n' "$resolve_physical_target"
378 fi
379 }
380
381 # Resolves the composio command against the PATH snapshot taken before any
382 # installer code could modify PATH: what the invoking terminal can run.
383 resolve_inherited_command() {
384 PATH=$inherited_path command -v composio 2>/dev/null
385 }
386
387 compute_inherited_resolution() {
388 inherited_command=$(resolve_inherited_command) || inherited_command=
389 inherited_resolution=unresolved
390 if [ -n "$inherited_command" ]; then
391 if [ "$(resolve_physical_path "$inherited_command")" = "$(resolve_physical_path "$exe")" ]; then
392 inherited_resolution=installed
393 else
394 inherited_resolution=shadowed
395 fi
396 fi
397 }
398
399 # Reject bin dirs that cannot be embedded safely in a managed rc line. The dir
400 # is only ever emitted inside double quotes -- `export PATH="<dir>:$PATH"` for
401 # bash/zsh and `set --export PATH "<dir>" $PATH` for fish -- so the set is
402 # deliberately narrow and mirrors UNSAFE_PATH_CHARS in
403 # ts/packages/cli/src/services/shell-config.ts, keeping this inline fallback and
404 # `composio install` on one contract:
405 #
406 # ` $ " \ the only characters bash and zsh still expand inside double
407 # quotes (fish expands a strict subset). Everything else -- `;`,
408 # `|`, `&`, `(`, `)`, `'` -- is literal there, so a bin dir like
409 # /Users/o'brien/.local/bin is written verbatim, not rejected.
410 # CR LF structural: either would split the managed block into extra lines.
411 # : structural: the PATH separator would silently prepend two entries.
412 #
413 # Aborting rather than escaping is what keeps the two branches equivalent: a dir
414 # the CLI refuses must not slip into an rc file just because the CLI could not
415 # run. It also means the value reaching append_path_block can no longer carry a
416 # character that double quotes would interpret, so no escaping pass is needed
417 # (and `${var//x/y}` is a bashism this POSIX script cannot use anyway).
418 is_unsafe_path() {
419 case $1 in *':'* | *'`'* | *'$'* | *'"'* | *\\*) return 0 ;; esac
420 unsafe_path_cr=$(printf '\r')
421 case $1 in *"$unsafe_path_cr"* | *'
422 '*) return 0 ;; esac
423 return 1
424 }
425
426 # Renders the home directory itself and paths under it with a literal $HOME
427 # prefix. Must stay in lockstep with the CLI's renderWithHome (shell-config.ts):
428 # delegated_setup_verified compares the CLI-written line against this rendering
429 # byte for byte, so any disagreement makes every delegated install look stale
430 # and forces a needless inline rewrite.
431 #
432 # Callers must run is_unsafe_path first, so the only `$` in the result is the
433 # `$HOME` this function introduces -- it survives as a live variable reference
434 # in the rc line without any user-supplied `$` riding along with it.
435 render_bin_dir() {
436 render_bin_dir_value=$1
437 case $render_bin_dir_value in
438 "$HOME") render_bin_dir_value=\$HOME ;;
439 "$HOME"/*) render_bin_dir_value=\$HOME/${render_bin_dir_value#"$HOME"/} ;;
440 esac
441 printf '%s\n' "$render_bin_dir_value"
442 }
443
444 path_block_line() {
445 case $1 in
446 fish) printf "set --export PATH \"%s\" \$PATH\n" "$2" ;;
447 *) printf "export PATH=\"%s:\$PATH\"\n" "$2" ;;
448 esac
449 }
450
451 # Succeeds only when the file holds exactly one managed marker block and that
452 # block already names the expected line.
453 path_block_current() {
454 awk -v expected="$2" '
455 $0 == "# Composio CLI" { markers++; pending = 1; next }
456 pending { pending = 0; if ($0 == expected) matches++ }
457 END { exit !(markers == 1 && matches == 1) }
458 ' "$1" 2>/dev/null
459 }
460
461 # Reconciles the single managed PATH block in one startup file: keeps an
462 # already-current block, replaces stale managed blocks, preserves unmanaged
463 # content, and reports every failure through its return status.
464 write_path_block() {
465 write_path_file=$1
466 write_path_line=$(path_block_line "$2" "$3") || return 1
467 mkdir -p "$(dirname "$write_path_file")" 2>/dev/null || return 1
468 if [ -e "$write_path_file" ] && [ ! -f "$write_path_file" ]; then
469 return 1
470 fi
471 if [ ! -f "$write_path_file" ]; then
472 touch "$write_path_file" 2>/dev/null || return 1
473 fi
474 # Dotfile managers keep startup files as symlinks; rewrite the physical
475 # target so the tmp+rename replace below cannot detach the symlink.
476 write_path_target=$(resolve_physical_path "$write_path_file")
477 if path_block_current "$write_path_target" "$write_path_line"; then
478 info "$(tildify "$write_path_file") is already up to date."
479 return 0
480 fi
481 write_path_tmp=$write_path_target.composio.tmp.$$
482 # cp -p seeds the tmp with the original permission bits; the awk rewrite
483 # below truncates its content while the copied mode survives the rename.
484 if ! cp -p "$write_path_target" "$write_path_tmp" 2>/dev/null; then
485 rm -f "$write_path_tmp"
486 return 1
487 fi
488 if ! awk '
489 function is_managed_path_assignment(line) {
490 return line ~ /^[[:space:]]*export PATH=".*:\$PATH"[[:space:]]*$/ ||
491 line ~ /^[[:space:]]*set --export PATH ".*" \$PATH[[:space:]]*$/
492 }
493 # The previously released installer wrote a three-line managed block:
494 # the marker, an install-dir export, then a PATH line referencing it.
495 # Recognizing that exact pair migrates the whole legacy block instead
496 # of orphaning the two export lines once the marker is consumed.
497 function is_legacy_install_dir_assignment(line) {
498 return line ~ /^[[:space:]]*export COMPOSIO_INSTALL_DIR=".*"[[:space:]]*$/ ||
499 line ~ /^[[:space:]]*set --export COMPOSIO_INSTALL_DIR ".*"[[:space:]]*$/
500 }
501 function is_legacy_pair(first, second) {
502 if (first ~ /^[[:space:]]*export COMPOSIO_INSTALL_DIR=".*"[[:space:]]*$/)
503 return second ~ /^[[:space:]]*export PATH="\$COMPOSIO_INSTALL_DIR:\$PATH"[[:space:]]*$/
504 return second ~ /^[[:space:]]*set --export PATH \$COMPOSIO_INSTALL_DIR \$PATH[[:space:]]*$/
505 }
506 holding {
507 holding = 0
508 if (is_legacy_pair(held, $0)) { held = ""; next }
509 print held
510 held = ""
511 }
512 pending {
513 pending = 0
514 if (is_managed_path_assignment($0)) next
515 if (is_legacy_install_dir_assignment($0)) { held = $0; holding = 1; next }
516 }
517 $0 == "# Composio CLI" { pending = 1; next }
518 { print }
519 END { if (holding) print held }
520 ' "$write_path_target" >"$write_path_tmp" 2>/dev/null; then
521 rm -f "$write_path_tmp"
522 return 1
523 fi
524 if ! printf '\n# Composio CLI\n%s\n' "$write_path_line" >>"$write_path_tmp" 2>/dev/null; then
525 rm -f "$write_path_tmp"
526 return 1
527 fi
528 if ! mv "$write_path_tmp" "$write_path_target" 2>/dev/null; then
529 rm -f "$write_path_tmp"
530 return 1
531 fi
532 info "Updated $(tildify "$write_path_file")."
533 return 0
534 }
535
536 # Names the bash startup file a login shell reads the managed block from.
537 # A login bash reads /etc/profile and then only the first existing of
538 # ~/.bash_profile, ~/.bash_login, ~/.profile; it never reads ~/.bashrc, and
539 # macOS Terminal.app starts exactly such a shell. An override that already
540 # exists is reused; otherwise ~/.bash_profile is the file to create. ~/.profile
541 # is never a target: every POSIX shell shares it.
542 bash_login_path_file() {
543 if [ ! -f "$HOME/.bash_profile" ] && [ -f "$HOME/.bash_login" ]; then
544 printf '%s\n' "$HOME/.bash_login"
545 else
546 printf '%s\n' "$HOME/.bash_profile"
547 fi
548 }
549
550 # Single source of truth for the startup files the managed PATH block lands in,
551 # in write order. Both setup paths and the confirmation message read it, so the
552 # files the installer configures are exactly the files it verifies and names.
553 shell_path_files() {
554 case $1 in
555 zsh) printf '%s\n' "$HOME/.zshrc" ;;
556 fish) printf '%s\n' "$HOME/.config/fish/config.fish" ;;
557 bash) printf '%s\n' "$HOME/.bashrc" "$(bash_login_path_file)" ;;
558 *) return 1 ;;
559 esac
560 }
561
562 # A ~/.bash_profile the installer creates shadows an existing ~/.profile, which
563 # login bash read while no override existed. Seed the new file so it keeps
564 # sourcing it; ~/.profile itself is left untouched.
565 seed_bash_login_file() {
566 seed_login_shell=$1
567 seed_login_file=$2
568 if [ "$seed_login_shell" != bash ] || [ "$seed_login_file" != "$HOME/.bash_profile" ]; then
569 return 0
570 fi
571 if [ -e "$seed_login_file" ] || [ ! -f "$HOME/.profile" ]; then
572 return 0
573 fi
574 cat >"$seed_login_file" 2>/dev/null <<'SEED_BASH_LOGIN_FILE' || return 1
575 # Created by the Composio CLI installer.
576 # Bash reads this file instead of ~/.profile in login shells.
577 if [ -f "$HOME/.profile" ]; then
578 . "$HOME/.profile"
579 fi
580 SEED_BASH_LOGIN_FILE
581 return 0
582 }
583
584 inline_shell_setup() {
585 inline_setup_shell=$1
586 inline_setup_bin_dir=$2
587 if is_unsafe_path "$inline_setup_bin_dir"; then
588 return 1
589 fi
590 inline_setup_rendered=$(render_bin_dir "$inline_setup_bin_dir")
591 inline_setup_list=$(shell_path_files "$inline_setup_shell") || return 1
592 inline_setup_status=0
593 while IFS= read -r inline_setup_file; do
594 [ -n "$inline_setup_file" ] || continue
595 if ! seed_bash_login_file "$inline_setup_shell" "$inline_setup_file"; then
596 inline_setup_status=1
597 continue
598 fi
599 write_path_block "$inline_setup_file" "$inline_setup_shell" "$inline_setup_rendered" ||
600 inline_setup_status=1
601 done <<EOF
602 $inline_setup_list
603 EOF
604 [ "$inline_setup_status" -eq 0 ] || return 1
605 return 0
606 }
607
608 # Succeeds when one startup file already holds the current managed block.
609 delegated_file_current() {
610 [ -f "$1" ] && path_block_current "$1" "$2"
611 }
612
613 # Stable CLI --shell implementations skip any startup file that already
614 # carries the managed marker, so a delegated exit 0 can leave a stale block
615 # naming an old bin dir. Confirm every target startup file of the requested
616 # shell names the current bin dir before trusting the delegation.
617 delegated_setup_verified() {
618 delegated_rendered=$(render_bin_dir "$resolved_bin_dir")
619 delegated_line=$(path_block_line "$requested_shell" "$delegated_rendered") || return 1
620 delegated_list=$(shell_path_files "$requested_shell") || return 1
621 delegated_status=0
622 while IFS= read -r delegated_file; do
623 [ -n "$delegated_file" ] || continue
624 delegated_file_current "$delegated_file" "$delegated_line" || delegated_status=1
625 done <<EOF
626 $delegated_list
627 EOF
628 [ "$delegated_status" -eq 0 ]
629 }
630
631 # Both setup paths disclose the same fact: which startup files now carry the
632 # managed PATH block. Whether the CLI or the inline fallback wrote them is an
633 # implementation detail that stays on the debug channel.
634 report_configured_shell() {
635 report_configured_files=
636 report_configured_list=$(shell_path_files "$requested_shell") || report_configured_list=
637 while IFS= read -r report_configured_file; do
638 [ -n "$report_configured_file" ] || continue
639 if [ -z "$report_configured_files" ]; then
640 report_configured_files=$(tildify "$report_configured_file")
641 else
642 report_configured_files="$report_configured_files and $(tildify "$report_configured_file")"
643 fi
644 done <<EOF
645 $report_configured_list
646 EOF
647 if [ -n "$report_configured_files" ]; then
648 info "Configured $requested_shell shell setup in $report_configured_files."
649 else
650 info "Configured $requested_shell shell setup."
651 fi
652 }
653
654 setup_requested_shell() {
655 if is_unsafe_path "$resolved_bin_dir"; then
656 warn "Skipping automatic shell setup: the executable directory \"$resolved_bin_dir\" contains unsupported characters."
657 return 1
658 fi
659 if "$exe" install --help 2>&1 | grep -q -- '--shell'; then
660 debug "delegating shell setup to $exe install --shell $requested_shell"
661 # The delegated CLI prints its own branded report for a command the
662 # user never ran. Capture both of its streams so the installer keeps
663 # sole ownership of the presentation, and replay them under
664 # COMPOSIO_DEBUG so genuine failures stay diagnosable.
665 setup_delegated_log=$tmpdir/shell-setup-delegated.log
666 setup_delegated_status=0
667 COMPOSIO_CLI_INVOCATION_ORIGIN=installer COMPOSIO_BIN_DIR="$resolved_bin_dir" \
668 "$exe" install --shell "$requested_shell" >"$setup_delegated_log" 2>&1 ||
669 setup_delegated_status=$?
670 debug_captured_output \
671 "delegated shell setup exited $setup_delegated_status; captured output:" \
672 "$setup_delegated_log"
673 if [ "$setup_delegated_status" -eq 0 ] && delegated_setup_verified; then
674 debug "shell setup source: cli"
675 report_configured_shell
676 return 0
677 fi
678 fi
679 debug "falling back to inline $requested_shell shell setup"
680 if inline_shell_setup "$requested_shell" "$resolved_bin_dir"; then
681 debug "shell setup source: fallback"
682 report_configured_shell
683 return 0
684 fi
685 return 1
686 }
687
688 # Final action block for every non-failure flow: one truthful ending chosen
689 # from the inherited-resolution and setup-outcome state. It must be the last
690 # output — the closing block is the instruction users copy, so nothing may
691 # print after it. Suppressible because it only covers normal success.
692 print_post_install_help() {
693 [ "${COMPOSIO_INSTALL_HELP:-1}" != 0 ] || return 0
694 if is_true "${COMPOSIO_QUIET:-}"; then
695 return 0
696 fi
697
698 compute_inherited_resolution
699 printf '\n'
700
701 if [ "$install_agent" = 1 ]; then
702 printf 'Composio agent login complete.\n'
703 if [ "$inherited_resolution" != installed ]; then
704 if [ "$shell_setup_outcome" = success ]; then
705 printf 'Open a new terminal to use the composio command.\n'
706 else
707 printf 'Run composio from its installed location:\n\n %s --help\n' "$(shell_quote "$exe")"
708 fi
709 fi
710 return 0
711 fi
712
713 # Case A: the invoking terminal already resolves the installed executable.
714 if [ "$inherited_resolution" = installed ]; then
715 if [ "$shell_setup_outcome" = success ] || [ "$shell_setup_mode" = none ]; then
716 printf 'composio is ready.\n\n composio login\n'
717 return 0
718 fi
719 fi
720
721 if [ "$shell_setup_outcome" = success ]; then
722 if [ "$inherited_resolution" = shadowed ]; then
723 printf 'Another composio command at %s takes precedence in this terminal.\n' "$inherited_command"
724 printf 'To use the newly installed CLI, run:\n\n %s login\n' "$(shell_quote "$exe")"
725 else
726 # Case B: configured for future terminals, vocabulary-free.
727 printf 'Open a new terminal, then run:\n\n composio login\n'
728 fi
729 return 0
730 fi
731
732 # Install-only guidance: COMPOSIO_INSTALL_SHELL=none or an unrecognized
733 # login shell. Never point the user at a shadowed bare command.
734 if [ "$shell_setup_mode" = none ]; then
735 printf 'Shell setup was skipped (COMPOSIO_INSTALL_SHELL=none).\n'
736 else
737 printf 'Automatic shell setup is not available for your shell.\n'
738 fi
739 case $inherited_resolution in
740 shadowed) printf 'Another composio command at %s takes precedence in this terminal.\n' "$inherited_command" ;;
741 unresolved)
742 if ! inherited_path_contains_bin_dir; then
743 printf 'Add %s to your PATH to use composio in new terminals.\n' "$(tildify "$resolved_bin_dir")"
744 fi
745 ;;
746 esac
747 printf 'To get started now, run:\n\n %s login\n' "$(shell_quote "$exe")"
748 }
749
750 # Setup failure never fails the install. Recovery travels the stderr warn
751 # channel so quiet mode and COMPOSIO_INSTALL_HELP=0 cannot suppress it, and the
752 # trusted --version-verified installed executable is always the last output.
753 print_setup_failure_ending() {
754 if [ "$install_agent" = 1 ] &&
755 [ "${COMPOSIO_INSTALL_HELP:-1}" != 0 ] && ! is_true "${COMPOSIO_QUIET:-}"; then
756 printf '\nComposio agent login complete.\n'
757 fi
758 warn "Automatic PATH setup for $requested_shell failed. The Composio CLI is installed and unaffected."
759 # The delegated CLI's own output is captured, so point at the channel that
760 # replays it instead of leaving the failure undiagnosable.
761 if ! is_true "${COMPOSIO_DEBUG:-}"; then
762 warn 'Re-run with COMPOSIO_DEBUG=1 for details.'
763 fi
764 if [ "$install_agent" = 1 ]; then
765 printf '\nRun composio from its installed location:\n\n %s --help\n' "$(shell_quote "$exe")" >&2
766 else
767 printf '\nTo get started, run:\n\n %s login\n' "$(shell_quote "$exe")" >&2
768 fi
769 }
770
771 cleanup() {
772 if [ -n "${tmpdir:-}" ] && [ -d "$tmpdir" ]; then
773 rm -rf "$tmpdir"
774 fi
775 if [ "${preserve_stage:-0}" != 1 ] && [ -n "${stage:-}" ] && [ -d "$stage" ]; then
776 rm -rf "$stage"
777 fi
778 }
779
780 cleanup_on_signal() {
781 cleanup_signal=$1
782 trap - 0 1 2 3 15
783 cleanup || :
784 exit $((128 + cleanup_signal))
785 }
786
787 main() {
788 # Snapshot the PATH the invoking terminal handed us before any installer
789 # code can modify it; every final-state decision uses only this snapshot.
790 inherited_path=${PATH:-}
791
792 install_agent=0
793 install_plugins=${COMPOSIO_INSTALL_PLUGINS:-1}
794 version_arg=
795 requested_shell=${COMPOSIO_INSTALL_SHELL:-}
796
797 case $install_plugins in
798 0 | 1) ;;
799 *) error 'COMPOSIO_INSTALL_PLUGINS must be 1 or 0' ;;
800 esac
801
802 case $requested_shell in
803 '' | auto | zsh | bash | fish | none) ;;
804 *) error "COMPOSIO_INSTALL_SHELL must be auto, zsh, bash, fish, or none (got \"$requested_shell\")" ;;
805 esac
806
807 while [ "$#" -gt 0 ]; do
808 case $1 in
809 --agent) install_agent=1 ;;
810 --no-plugins) install_plugins=0 ;;
811 -h | --help)
812 print_usage
813 return 0
814 ;;
815 --*) error "Unknown option: $1" ;;
816 *)
817 [ -z "$version_arg" ] || error 'Too many arguments. Expected at most one version tag.'
818 version_arg=$1
819 ;;
820 esac
821 shift
822 done
823
824 # Resolve the setup mode right after argument parsing: auto (the default)
825 # infers the login shell from $SHELL and degrades to install-only when it
826 # is unset or unrecognized; none is the documented install-only opt-out.
827 shell_setup_mode=${requested_shell:-auto}
828 case $shell_setup_mode in
829 auto)
830 login_shell=$(basename "${SHELL:-}" 2>/dev/null) || login_shell=
831 case $login_shell in
832 zsh | bash | fish) requested_shell=$login_shell ;;
833 *) requested_shell= ;;
834 esac
835 ;;
836 none) requested_shell= ;;
837 esac
838
839 COMPOSIO_GITHUB_OWNER=${COMPOSIO_GITHUB_OWNER-ComposioHQ}
840 COMPOSIO_GITHUB_REPO=${COMPOSIO_GITHUB_REPO-composio}
841 COMPOSIO_GITHUB_URL=${COMPOSIO_GITHUB_URL-https://github.com}
842 COMPOSIO_GITHUB_API_BASE_URL=${COMPOSIO_GITHUB_API_BASE_URL:-}
843 COMPOSIO_INSTALL_DIR=${COMPOSIO_INSTALL_DIR:-"$HOME/.composio"}
844 COMPOSIO_BIN_DIR=${COMPOSIO_BIN_DIR:-"$HOME/.local/bin"}
845
846 validate_identifier COMPOSIO_GITHUB_OWNER "$COMPOSIO_GITHUB_OWNER"
847 validate_identifier COMPOSIO_GITHUB_REPO "$COMPOSIO_GITHUB_REPO"
848 validate_url "$COMPOSIO_GITHUB_URL" ||
849 error "COMPOSIO_GITHUB_URL must use https or an explicitly allowed test host (got \"$COMPOSIO_GITHUB_URL\")"
850 if [ -n "$COMPOSIO_GITHUB_API_BASE_URL" ]; then
851 validate_url "$COMPOSIO_GITHUB_API_BASE_URL" ||
852 error "COMPOSIO_GITHUB_API_BASE_URL must use https or an explicitly allowed test host (got \"$COMPOSIO_GITHUB_API_BASE_URL\")"
853 fi
854
855 detect_target
856 command -v curl >/dev/null 2>&1 || error 'curl is required to install Composio CLI'
857 command -v unzip >/dev/null 2>&1 || error 'unzip is required to install Composio CLI'
858
859 github_repo=${COMPOSIO_GITHUB_URL%/}/$COMPOSIO_GITHUB_OWNER/$COMPOSIO_GITHUB_REPO
860 if [ -n "$COMPOSIO_GITHUB_API_BASE_URL" ]; then
861 github_api_base=${COMPOSIO_GITHUB_API_BASE_URL%/}
862 elif [ "$COMPOSIO_GITHUB_URL" = https://github.com ]; then
863 github_api_base=https://api.github.com
864 else
865 github_api_base=${COMPOSIO_GITHUB_URL%/}/api/v3
866 fi
867 github_api_repo=$github_api_base/repos/$COMPOSIO_GITHUB_OWNER/$COMPOSIO_GITHUB_REPO
868 archive_name=composio-$target.zip
869
870 # Official ComposioHQ releases always publish a complete checksums.txt, so
871 # a missing manifest or entry is a hard error there. Any overridden source
872 # (mirror, test host, custom API base) keeps the lenient warn-and-continue
873 # behavior, since its manifests are outside our control.
874 official_release_source=0
875 if [ "$COMPOSIO_GITHUB_URL" = https://github.com ] &&
876 [ "$COMPOSIO_GITHUB_OWNER" = ComposioHQ ] &&
877 [ "$COMPOSIO_GITHUB_REPO" = composio ] &&
878 [ -z "$COMPOSIO_GITHUB_API_BASE_URL" ]; then
879 official_release_source=1
880 fi
881
882 requested_version=$version_arg
883 if [ -z "$requested_version" ]; then
884 requested_version=${COMPOSIO_INSTALL_VERSION:-}
885 fi
886
887 if [ -n "$requested_version" ]; then
888 version=$(normalize_version "$requested_version")
889 archive_url=$github_repo/releases/download/$version/$archive_name
890 else
891 info 'Finding latest stable CLI release...'
892 latest_release=$(resolve_latest_cli_release) ||
893 error "Failed to determine the latest CLI release with a $archive_name asset. Specify a version manually."
894 version=$(printf '%s\n' "$latest_release" | sed -n '1p')
895 archive_url=$(printf '%s\n' "$latest_release" | sed -n '2p')
896 if [ -z "$version" ] || [ -z "$archive_url" ]; then
897 error 'The release API returned an incomplete CLI release'
898 fi
899 info "Found latest version: $version"
900 fi
901 validate_url "$archive_url" || error "Release API returned an unsafe archive URL \"$archive_url\""
902
903 checksums_url=$github_repo/releases/download/$version/checksums.txt
904 validate_url "$checksums_url" || error "Refusing unsafe checksum URL \"$checksums_url\""
905
906 tmpdir=$(mktemp -d) || error 'Failed to create a temporary directory'
907 trap cleanup 0
908 trap 'cleanup_on_signal 1' 1
909 trap 'cleanup_on_signal 2' 2
910 trap 'cleanup_on_signal 3' 3
911 trap 'cleanup_on_signal 15' 15
912 debug "temporary directory: $tmpdir"
913
914 info "Installing Composio CLI $version for $target"
915 curl_download "$archive_url" "$tmpdir/$archive_name" 0 ||
916 error "Failed to download from \"$archive_url\""
917
918 if curl_download "$checksums_url" "$tmpdir/checksums.txt" 1; then
919 verify_checksum "$tmpdir/$archive_name" "$tmpdir/checksums.txt" "$archive_name"
920 elif [ "$official_release_source" = 1 ]; then
921 error "Failed to download checksums.txt from \"$checksums_url\". Official releases always publish checksums, so this release cannot be verified. Refusing to install."
922 else
923 warn 'No checksums.txt in this release; continuing without verification'
924 fi
925
926 info 'Extracting bundle...'
927 unzip -oqd "$tmpdir" "$tmpdir/$archive_name" || error 'Failed to extract archive'
928
929 resolved_install_dir=$(resolve_directory "$COMPOSIO_INSTALL_DIR")
930 resolved_bin_dir=$(resolve_directory "$COMPOSIO_BIN_DIR")
931 install_bundle "$tmpdir"
932 install_entry_point
933
934 exe=$resolved_install_dir/composio
935 "$exe" --version >/dev/null 2>&1 || error 'The installed Composio CLI failed its version check'
936 info "Composio CLI was installed to $(tildify "$exe")"
937 if [ "$resolved_bin_dir" != "$resolved_install_dir" ]; then
938 info "The composio entry point is $(tildify "$resolved_bin_dir/composio")"
939 fi
940
941 # Delegated CLI invocations below may spawn composio subprocesses; make the
942 # fresh entry point resolvable for them. Final-state decisions keep using
943 # the inherited snapshot taken at the top of main().
944 PATH=$resolved_bin_dir:$PATH
945 export PATH
946
947 if [ "$install_plugins" = 1 ]; then
948 info 'Installing plugins for detected agent hosts...'
949 COMPOSIO_CLI_INVOCATION_ORIGIN=installer "$exe" setup --target auto --yes --if-present ||
950 error "Composio CLI was installed, but agent plugin setup failed. Retry with \`composio setup --target auto --yes\`."
951 fi
952
953 if [ "$install_agent" = 1 ]; then
954 info 'Setting up Composio agent login...'
955 COMPOSIO_CLI_INVOCATION_ORIGIN=installer "$exe" login --agent --no-skill-install ||
956 error 'Failed to sign up or log in as a Composio agent.'
957 fi
958
959 shell_setup_outcome=skipped
960 if [ -n "$requested_shell" ]; then
961 if setup_requested_shell; then
962 shell_setup_outcome=success
963 else
964 shell_setup_outcome=failure
965 fi
966 fi
967
968 if [ "$shell_setup_outcome" = failure ]; then
969 print_setup_failure_ending
970 else
971 print_post_install_help
972 fi
973 }
974
975 main "$@"