-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap.sh
More file actions
1851 lines (1686 loc) · 70.2 KB
/
Copy pathbootstrap.sh
File metadata and controls
1851 lines (1686 loc) · 70.2 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# =============================================================================
# Hermes Multi-Gateway Bootstrap
# =============================================================================
# One-command setup for N Telegram-bot gateways with one of three sharing
# strategies. Works for fresh installs (Mode A), extending an existing setup
# (Mode B), and non-interactive automation (Mode C — flag-driven).
#
# Quick start (interactive):
# curl -fsSL https://raw.githubusercontent.com/Demonbane18/hermes-agent-setup/main/bootstrap.sh | bash
#
# Or from a clone:
# ./bootstrap.sh
#
# Non-interactive examples:
# ./bootstrap.sh --parent ~/gateways --count 3 --names alpha,beta,gamma --strategy isolated
# ./bootstrap.sh --add --parent ~/gateways --names delta,epsilon
#
# See --help for the full flag list.
# =============================================================================
set -euo pipefail
# -----------------------------------------------------------------------------
# Version
# -----------------------------------------------------------------------------
# Bump on every meaningful change so users running `curl ... | bash` can see
# whether their copy matches the latest.
BOOTSTRAP_VERSION="0.7.2"
BOOTSTRAP_RELEASED="2026-05-08"
# -----------------------------------------------------------------------------
# Globals
# -----------------------------------------------------------------------------
SCRIPT_NAME="${0##*/}"
SCRIPT_PATH=""
SCRIPT_DIR=""
if [ -n "${BASH_SOURCE[0]:-}" ] && [ -f "${BASH_SOURCE[0]}" ]; then
SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
fi
DEFAULT_REPO_URL="https://raw.githubusercontent.com/Demonbane18/hermes-agent-setup/main"
REPO_URL="${HERMES_BOOTSTRAP_REPO_URL:-$DEFAULT_REPO_URL}"
PARENT=""
COUNT=""
NAMES_RAW=""
STRATEGY=""
MODE="" # "" | "fresh" | "add"
DRY_RUN=0
NON_INTERACTIVE=0
EXTRA_SCAN_PATHS=()
CREATE_SOUL=0
# Provider selection (defaults match the templates: xiaomi-mimo primary,
# openrouter fallback). Override interactively or via --provider/--model/etc.
PROVIDER="" # e.g. xiaomi-mimo, anthropic, openai, gemini, openrouter,
# groq, deepseek, minimax, zai, ollama, custom
MODEL="" # default model id for the primary provider
BASE_URL="" # only for OpenAI-compatible custom providers
KEY_ENV="" # env var name holding the API key (e.g. ANTHROPIC_API_KEY)
FALLBACK_PROVIDER="" # fallback provider name; set to "none" or empty to skip
FALLBACK_MODEL=""
# Logged operations (so a final summary works even in --dry-run)
OPS=()
trap 'echo "[bootstrap] error on line $LINENO" >&2' ERR
# -----------------------------------------------------------------------------
# Helpers
# -----------------------------------------------------------------------------
log() { echo "[bootstrap] $*"; }
warn() { echo "[bootstrap] WARN: $*" >&2; }
die() { echo "[bootstrap] FATAL: $*" >&2; exit 1; }
op() { OPS+=("$1"); if [ "$DRY_RUN" = 1 ]; then echo "[dry-run] $1"; else echo "[do] $1"; fi; }
usage() {
cat <<'EOF'
hermes-agent-setup bootstrap.sh — N-gateway Hermes setup
USAGE
bootstrap.sh [flags]
INTERACTIVE
Run with no flags. The script scans your $HOME, /root, /home/* for an
existing Hermes parent folder. If one is found you'll be offered to extend
it (Mode B); otherwise you'll be walked through a fresh setup (Mode A).
FLAGS
--parent PATH Parent folder for gateways (default: ~/gateways)
--count N Number of gateways (fresh setup only)
--names a,b,c Comma-separated gateway names (overrides --count)
--strategy STRATEGY isolated | shared-skills | shared-both
Aliases: none|skills|both|all
Default: isolated (fresh) or auto-detected (--add)
--provider NAME LLM provider for the default model.
Built-in: xiaomi-mimo (default), openrouter,
anthropic, openai, gemini.
Custom OpenAI-compatible: groq, deepseek,
minimax, zai, ollama, custom.
--model ID Model ID for the primary provider. Defaults per
provider (e.g. mimo-v2.5-pro, claude-sonnet-4-6,
gpt-4o, gemini-2.5-pro).
--base-url URL Override the provider base URL (mostly for
region-specific MiMo endpoints or custom
providers).
--key-env VAR Override the env var name that holds the API key
(default per provider, e.g. ANTHROPIC_API_KEY).
--fallback-provider NAME Fallback provider (default: openrouter unless
primary is openrouter; pass 'none' to skip).
--fallback-model ID Fallback model id (default per provider).
--no-fallback Shortcut for --fallback-provider none.
--add Force Mode B (extend existing setup)
--soul Also create a SOUL.md scaffold per gateway
--scan-path PATH Additional path to scan for existing setups
(repeatable)
--repo-url URL Base URL for fetching run.sh / inject_config.py
when no local templates/ directory exists
(default: $HERMES_BOOTSTRAP_REPO_URL or the
Demonbane18/hermes-agent-setup main branch)
--dry-run Print every action without executing
--non-interactive Fail loudly on missing required values instead
of prompting
-V, --version Print bootstrap version and exit
-h, --help This help
STRATEGIES
isolated Each gateway has its own memories/ and skills/. Default.
Maximum context separation. No _shared/ folder created.
shared-skills Each gateway has its own memories/. skills/ symlinked to
<parent>/_shared/skills/. Useful for separate personas
sharing one tool library.
shared-both memories/ AND skills/ symlinked to <parent>/_shared/.
Multiple Telegram front-ends to the same logical agent.
PROVIDERS (hard-coded model lists; check provider for newest)
xiaomi-mimo mimo-v2.5-pro, mimo-v2-flash
base_url: https://token-plan-{sgp,ams,cn}.xiaomimimo.com/v1
key_env: XIAOMI_MIMO_API_KEY
openrouter anthropic/claude-sonnet-4, openai/gpt-4o, google/gemini-2.5-pro
(built-in provider, no base_url)
key_env: OPENROUTER_API_KEY
anthropic claude-opus-4-7, claude-sonnet-4-6, claude-haiku-4-5
(built-in) key_env: ANTHROPIC_API_KEY
openai gpt-4o, gpt-4o-mini, o1, o3-mini
(built-in) key_env: OPENAI_API_KEY
gemini gemini-2.5-pro, gemini-2.5-flash
(built-in) key_env: GEMINI_API_KEY
groq llama-3.3-70b-versatile, mixtral-8x7b-32768
base_url: https://api.groq.com/openai/v1
key_env: GROQ_API_KEY
deepseek deepseek-chat, deepseek-reasoner
base_url: https://api.deepseek.com/v1
key_env: DEEPSEEK_API_KEY
minimax minimax-m2.7
base_url: https://api.minimax.chat/v1
key_env: MINIMAX_API_KEY
zai glm-4-plus, glm-4-flash
base_url: https://open.bigmodel.cn/api/paas/v4
key_env: ZAI_API_KEY
ollama llama3.1:8b, qwen2.5:14b, deepseek-r1:14b (local, no key)
base_url: http://localhost:11434/v1
custom provide --model, --base-url, --key-env yourself
NAME RULES
Gateway names match ^[A-Za-z0-9][A-Za-z0-9_-]*$. No leading dot or
underscore (those clash with _shared/ and dotfiles).
SAFETY
Never overwrites existing .env or config.yaml.
Backs up run.sh / inject_config.py to .bak exactly once when replacing
with a non-matching universal template.
EXAMPLES
# Fresh setup with the defaults — xiaomi-mimo + openrouter fallback
bootstrap.sh --parent ~/gateways --count 3 --names alpha,beta,gamma \\
--strategy isolated
# Anthropic primary, OpenRouter fallback
bootstrap.sh --parent ~/agents --count 1 --names assistant \\
--strategy isolated --provider anthropic --model claude-opus-4-7 \\
--non-interactive
# OpenAI primary, no fallback
bootstrap.sh --parent ~/bots --count 2 --names work,personal \\
--strategy shared-both --provider openai --model gpt-4o \\
--no-fallback --non-interactive
# Local Ollama, no API keys at all
bootstrap.sh --parent ~/local-bots --count 1 --names dev \\
--strategy isolated --provider ollama --model llama3.1:8b \\
--no-fallback --non-interactive
# Custom OpenAI-compatible provider
bootstrap.sh --parent ~/gateways --count 1 --names myagent \\
--provider custom --model my-llm-v1 \\
--base-url https://api.example.com/v1 --key-env EXAMPLE_API_KEY \\
--no-fallback --non-interactive
# Add 2 gateways to an existing parent (strategy + provider auto-inherited
# from the existing config.yaml; pass --provider to override)
bootstrap.sh --add --parent ~/gateways --names delta,epsilon \\
--non-interactive
# Curl-piped, non-interactive
curl -fsSL .../bootstrap.sh | bash -s -- \\
--parent ~/agents --count 2 --names work,personal \\
--strategy shared-both --non-interactive
EOF
}
# Strategy alias normalizer
normalize_strategy() {
case "${1:-}" in
""|isolated|none) echo "isolated" ;;
skills|shared-skills) echo "shared-skills" ;;
both|all|shared-both) echo "shared-both" ;;
*) die "unknown strategy: $1 (use isolated|shared-skills|shared-both)" ;;
esac
}
validate_name() {
local n="$1"
if ! [[ "$n" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]]; then
die "invalid gateway name: '$n' (must match ^[A-Za-z0-9][A-Za-z0-9_-]*$)"
fi
if [[ "$n" == _* ]]; then
die "gateway name '$n' starts with underscore (reserved for _shared/)"
fi
if [[ "$n" == .* ]]; then
die "gateway name '$n' starts with dot (reserved for dotfiles)"
fi
return 0
}
# When the script is piped (`curl ... | bash`), stdin is the script source —
# any plain `read` returns empty string. Read from /dev/tty instead so prompts
# still work. If /dev/tty isn't available (some CI), fall back to the script's
# own stdin and warn the user up-front.
TTY_FD=""
init_tty() {
if [ -t 0 ]; then
TTY_FD="0" # stdin is already a tty
elif [ -r /dev/tty ] && [ -w /dev/tty ]; then
TTY_FD="tty" # use /dev/tty directly
else
TTY_FD="" # no interactive input possible
fi
}
read_tty() {
# Args: <prompt-msg>; sets REPLY
local msg="$1"
if [ "$TTY_FD" = "0" ]; then
read -r -p "$msg" REPLY
elif [ "$TTY_FD" = "tty" ]; then
read -r -p "$msg" REPLY </dev/tty
else
die "no interactive terminal available (stdin is piped, /dev/tty unreachable). Re-run with --non-interactive plus --parent/--count/--names/--strategy, or download the script first: curl -fsSL .../bootstrap.sh -o bootstrap.sh && bash bootstrap.sh"
fi
}
prompt() {
local msg="$1" default="${2:-}"
if [ "$NON_INTERACTIVE" = 1 ]; then
die "missing required value: $msg (running --non-interactive)"
fi
local label
if [ -n "$default" ]; then
label="$msg [$default]: "
else
label="$msg: "
fi
read_tty "$label"
echo "${REPLY:-$default}"
}
confirm() {
local msg="$1" default="${2:-Y}"
if [ "$NON_INTERACTIVE" = 1 ]; then
echo "$default"
return
fi
read_tty "$msg [$default]: "
echo "${REPLY:-$default}"
}
# yes_no — returns 0 for yes, 1 for no. Default Y shows `[Y/n]`,
# default N shows `[y/N]`. Empty input = default.
yes_no() {
local msg="$1" default="${2:-Y}"
local label
case "$default" in
Y|y) label="$msg [Y/n]: " ;;
N|n) label="$msg [y/N]: " ;;
*) label="$msg [Y/n]: "; default="Y" ;;
esac
if [ "$NON_INTERACTIVE" = 1 ]; then
case "$default" in Y|y) return 0 ;; *) return 1 ;; esac
fi
read_tty "$label"
local reply="${REPLY:-$default}"
[[ "$reply" =~ ^[Yy] ]]
}
# Suggest a non-colliding parent path when the user wants a fresh setup
# alongside an existing one. Picks $HOME/gateways-2, -3, … until free.
suggest_fresh_parent() {
local existing="$1"
local base="$HOME/gateways"
[ "$existing" = "$base" ] || base="$HOME/gateways"
local i=2
local candidate="$base-$i"
while [ -e "$candidate" ]; do
i=$((i+1))
candidate="$base-$i"
done
echo "$candidate"
}
# -----------------------------------------------------------------------------
# Provider registry
# -----------------------------------------------------------------------------
# Each entry: <default_model>|<base_url>|<key_env>|<is_builtin>
# built-in providers are referenced as `provider: <name>` in config.yaml.
# non-built-in providers go under `custom_providers:` with `name`/`base_url`/`key_env`.
provider_info() {
case "$1" in
xiaomi-mimo) echo "mimo-v2.5-pro|https://token-plan-sgp.xiaomimimo.com/v1|XIAOMI_MIMO_API_KEY|0" ;;
openrouter) echo "anthropic/claude-sonnet-4||OPENROUTER_API_KEY|1" ;;
anthropic) echo "claude-sonnet-4-6||ANTHROPIC_API_KEY|1" ;;
openai) echo "gpt-4o||OPENAI_API_KEY|1" ;;
gemini) echo "gemini-2.5-pro||GEMINI_API_KEY|1" ;;
groq) echo "llama-3.3-70b-versatile|https://api.groq.com/openai/v1|GROQ_API_KEY|0" ;;
deepseek) echo "deepseek-chat|https://api.deepseek.com/v1|DEEPSEEK_API_KEY|0" ;;
minimax) echo "minimax-m2.7|https://api.minimax.chat/v1|MINIMAX_API_KEY|0" ;;
zai) echo "glm-4-plus|https://open.bigmodel.cn/api/paas/v4|ZAI_API_KEY|0" ;;
ollama) echo "llama3.1:8b|http://localhost:11434/v1||0" ;;
custom) echo "|||0" ;;
*) return 1 ;;
esac
}
# Hard-coded model catalog. As-of bootstrap-script date — providers add and
# deprecate models constantly. Hit the provider's /v1/models endpoint for the
# live list, then edit config.yaml manually.
provider_models() {
case "$1" in
xiaomi-mimo) echo "mimo-v2.5-pro mimo-v2-flash" ;;
openrouter)
# Mirrors the live model list visible in Hermes Agent's own
# `hermes config` model picker as of script release date. Pricing
# changes constantly — this list is just for the picker UI.
# Provider mints/deprecates models often; pick "(type your own)"
# for anything missing or browse https://openrouter.ai/models.
echo "anthropic/claude-opus-4.6 anthropic/claude-sonnet-4.6 anthropic/claude-sonnet-4.5 anthropic/claude-haiku-4.5 anthropic/claude-opus-4 anthropic/claude-sonnet-4 openai/gpt-5.4 openai/gpt-5.4-mini openai/gpt-5.4-pro openai/gpt-5.4-nano openai/gpt-5.3-codex openai/gpt-4o openai/gpt-4o-mini openai/o1 openai/o3-mini google/gemini-3-pro-image-preview google/gemini-3-flash-preview google/gemini-3.1-pro-preview google/gemini-3.1-flash-lite-preview google/gemini-2.5-pro google/gemini-2.5-flash xiaomi/mimo-v2-pro qwen/qwen3.6-plus qwen/qwen3.5-plus-02-15 qwen/qwen3.5-35b-a3b minimax/minimax-m2.7 minimax/minimax-m2.5 z-ai/glm-5.1 z-ai/glm-5v-turbo z-ai/glm-5-turbo moonshotai/kimi-k2.5 x-ai/grok-4.20 stepfun/step-3.5-flash deepseek/deepseek-chat deepseek/deepseek-reasoner nvidia/nemotron-3-super-120b-a12b nvidia/nemotron-3-super-120b-a12b:free arcee-ai/trinity-large-thinking meta-llama/llama-3.3-70b-instruct"
;;
anthropic) echo "claude-opus-4-7 claude-sonnet-4-6 claude-haiku-4-5" ;;
openai) echo "gpt-4o gpt-4o-mini o1 o3-mini" ;;
gemini) echo "gemini-2.5-pro gemini-2.5-flash" ;;
groq) echo "llama-3.3-70b-versatile mixtral-8x7b-32768 llama-3.1-8b-instant" ;;
deepseek) echo "deepseek-chat deepseek-reasoner" ;;
minimax) echo "minimax-m2.7" ;;
zai) echo "glm-4-plus glm-4-flash" ;;
ollama) echo "llama3.1:8b qwen2.5:14b deepseek-r1:14b mistral:7b" ;;
*) echo "" ;;
esac
}
provider_is_builtin() {
case "$1" in
anthropic|openai|openrouter|gemini) return 0 ;;
*) return 1 ;;
esac
}
yaml_provider_ref() {
if provider_is_builtin "$1"; then echo "$1"; else echo "custom:$1"; fi
}
# Detect whether the terminal supports raw mode + ANSI escapes for the arrow
# picker. Falls back to "no" on dumb terminals, when /dev/tty isn't a real
# tty, or when stty can't read the current settings.
has_arrow_capability() {
[ "$NON_INTERACTIVE" = 1 ] && return 1
[ "$TTY_FD" = "tty" ] || [ "$TTY_FD" = "0" ] || return 1
[ "${TERM:-dumb}" != "dumb" ] || return 1
stty -g </dev/tty >/dev/null 2>&1 || return 1
return 0
}
# arrow_pick <title> <option>...
# Interactive arrow-key picker.
# ↑/↓ or k/j move the cursor (with wraparound)
# PgUp/PgDn jump a viewport
# Home/End or g/G top / bottom
# Enter or Space select
# q or Esc cancel (returns 1, no output)
# Echoes the selected option to STDOUT. All UI on STDERR.
# Falls back: caller should detect "no arrow capability" first via has_arrow_capability.
arrow_pick() {
local title="$1"; shift
local options=("$@")
local n=${#options[@]}
[ "$n" -eq 0 ] && return 1
local viewport=15
[ "$n" -lt "$viewport" ] && viewport="$n"
local idx=0
local top=0 # first visible item
# Save tty state, set raw mode (no canonical, no echo).
local stty_saved
if ! stty_saved="$(stty -g </dev/tty 2>/dev/null)"; then
return 1
fi
# Hide cursor, switch to raw mode.
stty -icanon -echo min 1 time 0 </dev/tty 2>/dev/null
printf '\x1b[?25l' >&2
# Restore handler — must always run so we don't leave the terminal hosed.
_arrow_restore() {
printf '\x1b[?25h' >&2 # show cursor
stty "$stty_saved" </dev/tty 2>/dev/null
}
trap '_arrow_restore' EXIT
trap '_arrow_restore; trap - EXIT INT TERM; return 130' INT TERM
local rendered_lines=0
_arrow_render() {
# Erase prior render
if [ "$rendered_lines" -gt 0 ]; then
printf '\x1b[%dA' "$rendered_lines" >&2
printf '\x1b[J' >&2
fi
local lines=0 i
printf '%s\n' "$title" >&2; lines=$((lines+1))
printf ' ↑/↓ move · Enter select · q cancel [%d/%d]\n' "$((idx+1))" "$n" >&2
lines=$((lines+1))
for ((i=top; i<top+viewport && i<n; i++)); do
if [ "$i" = "$idx" ]; then
printf '\x1b[7m> %s\x1b[0m\n' "${options[$i]}" >&2
else
printf ' %s\n' "${options[$i]}" >&2
fi
lines=$((lines+1))
done
# Pad so erase math is stable when the last viewport is short.
local printed=$((i - top))
while [ "$printed" -lt "$viewport" ]; do
printf '\n' >&2
printed=$((printed+1)); lines=$((lines+1))
done
rendered_lines="$lines"
}
_scroll_to_visible() {
if [ "$idx" -lt "$top" ]; then
top="$idx"
elif [ "$idx" -ge "$((top + viewport))" ]; then
top=$((idx - viewport + 1))
fi
}
_arrow_render
local k1 k2 k3
while true; do
IFS= read -rsn1 k1 </dev/tty || break
case "$k1" in
$'\x1b')
IFS= read -rsn2 -t 0.05 k2 </dev/tty 2>/dev/null
case "$k2" in
'[A'|'OA') # Up arrow
idx=$(( (idx - 1 + n) % n )); _scroll_to_visible ;;
'[B'|'OB') # Down arrow
idx=$(( (idx + 1) % n )); _scroll_to_visible ;;
'[5') # PageUp (followed by ~)
IFS= read -rsn1 -t 0.05 k3 </dev/tty 2>/dev/null
idx=$((idx - viewport)); [ "$idx" -lt 0 ] && idx=0
_scroll_to_visible ;;
'[6') # PageDown
IFS= read -rsn1 -t 0.05 k3 </dev/tty 2>/dev/null
idx=$((idx + viewport)); [ "$idx" -ge "$n" ] && idx=$((n-1))
_scroll_to_visible ;;
'[H'|'OH') # Home
idx=0; top=0 ;;
'[F'|'OF') # End
idx=$((n-1)); _scroll_to_visible ;;
'[D'|'OD') # Left arrow — reserved for text-input cursor in prompts
: ;; # no-op inside the picker
'[C'|'OC') # Right arrow — same
: ;;
'') # bare Esc — cancel
trap - EXIT INT TERM; _arrow_restore; printf '\n' >&2; return 1 ;;
esac ;;
$'\n'|$'\r'|'') # Enter / NL
trap - EXIT INT TERM; _arrow_restore; printf '\n' >&2
printf '%s' "${options[$idx]}"
return 0 ;;
' ') # Space
trap - EXIT INT TERM; _arrow_restore; printf '\n' >&2
printf '%s' "${options[$idx]}"
return 0 ;;
'q'|'Q')
trap - EXIT INT TERM; _arrow_restore; printf '\n' >&2; return 1 ;;
'k') # vim: up
idx=$(( (idx - 1 + n) % n )); _scroll_to_visible ;;
'j') # vim: down
idx=$(( (idx + 1) % n )); _scroll_to_visible ;;
'g') # vim: home
idx=0; top=0 ;;
'G') # vim: end
idx=$((n-1)); _scroll_to_visible ;;
esac
_arrow_render
done
trap - EXIT INT TERM; _arrow_restore; printf '\n' >&2; return 1
}
# pick_model_for <provider> <current-or-empty>
# Echoes the chosen model id ON STDOUT (so $(pick_model_for ...) works).
# All UI — menu, prompts, warnings — goes to STDERR so it's still visible
# to the user when the function is called inside a command substitution.
# Marks the current selection with "(current)" and the registry default with "(default)".
pick_model_for() {
local p="$1" current="${2:-}"
local def_info; def_info="$(provider_info "$p" 2>/dev/null || echo "|||0")"
local def_model; IFS='|' read -r def_model _ _ _ <<<"$def_info"
# OpenRouter's registry default is empty by design — substitute a sensible one
if [ -z "$def_model" ] && [ "$p" = "openrouter" ]; then
def_model="anthropic/claude-sonnet-4.6"
fi
local fallback_default="${current:-$def_model}"
if [ "$NON_INTERACTIVE" = 1 ]; then
echo "$fallback_default"
return
fi
local models; models="$(provider_models "$p")"
if [ -z "$models" ]; then
local nm; nm="$(prompt "Model id" "$fallback_default")"
echo "$nm"
return
fi
# Build labelled options array (current/default markers + special entries)
local opts=() raw_models=() m label
for m in $models; do
label="$m"
if [ "$m" = "$current" ] && [ "$m" = "$def_model" ]; then
label="$m (current, default)"
elif [ "$m" = "$current" ]; then
label="$m (current)"
elif [ "$m" = "$def_model" ]; then
label="$m (default)"
fi
opts+=("$label")
raw_models+=("$m")
done
local CUSTOM_LABEL="✏ Enter custom model name"
opts+=("$CUSTOM_LABEL")
local SKIP_LABEL=""
if [ -n "$current" ]; then
SKIP_LABEL="⏭ Skip (keep current: $current)"
opts+=("$SKIP_LABEL")
fi
local PREV_LABEL="← Previous step"
opts+=("$PREV_LABEL")
# ─── Arrow-key picker (preferred) ─────────────────────────────────────────
if has_arrow_capability; then
local picked
picked="$(arrow_pick "Select model for $p (current: ${current:-<unset>})" "${opts[@]}")"
local arc=$?
if [ "$arc" = 2 ]; then
return 2 # propagate "back" up to the orchestrator
fi
if [ "$arc" = 0 ]; then
case "$picked" in
"$PREV_LABEL")
return 2 ;;
"$CUSTOM_LABEL")
local nm; nm="$(prompt "Type model id" "$fallback_default")"
echo "$nm"; return ;;
"$SKIP_LABEL")
echo "$current"; return ;;
*)
# Strip " (current)" / " (default)" suffix back to bare model id
echo "${picked%% (*}"; return ;;
esac
fi
# arc=1 → q/Esc cancel — keep the current value (or default if unset)
echo "$fallback_default"
return
fi
# ─── Numbered fallback (dumb terminal / piped / Windows) ─────────────────
{
echo
echo "Select model for $p (current: ${current:-<unset>}):"
local i=1
for m in $models; do
label="$m"
if [ "$m" = "$current" ] && [ "$m" = "$def_model" ]; then
label="$m (current, default)"
elif [ "$m" = "$current" ]; then
label="$m (current)"
elif [ "$m" = "$def_model" ]; then
label="$m (default)"
fi
printf " %2d) %s\n" "$i" "$label"
i=$((i+1))
done
local custom_idx="$i"
printf " %2d) Enter custom model name\n" "$custom_idx"
i=$((i+1))
local skip_idx=""
if [ -n "$current" ]; then
skip_idx="$i"
printf " %2d) Skip (keep current: %s)\n" "$skip_idx" "$current"
i=$((i+1))
fi
} >&2
local total=0; for m in $models; do total=$((total+1)); done
local custom_idx=$((total + 1))
local skip_idx=""
[ -n "$current" ] && skip_idx=$((custom_idx + 1))
local default_sel="1"
if [ -n "$current" ]; then
local idx=1
for m in $models; do
[ "$m" = "$current" ] && { default_sel="$idx"; break; }
idx=$((idx+1))
done
fi
local sel; sel="$(prompt "Pick" "$default_sel")"
if [[ "$sel" =~ ^[0-9]+$ ]]; then
if [ "$sel" -ge 1 ] && [ "$sel" -lt "$custom_idx" ]; then
echo "$models" | awk -v n="$sel" '{print $n}'
return
elif [ "$sel" = "$custom_idx" ]; then
local nm; nm="$(prompt "Type model id" "$fallback_default")"
echo "$nm"
return
elif [ -n "$skip_idx" ] && [ "$sel" = "$skip_idx" ]; then
echo "$current"
return
fi
fi
warn "invalid pick — keeping ${current:-default}"
echo "$fallback_default"
}
# Resolve PROVIDER, MODEL, BASE_URL, KEY_ENV, FALLBACK_PROVIDER, FALLBACK_MODEL.
# Honours flags first, prompts only for what's missing in interactive mode.
prompt_provider() {
# Default in non-interactive mode if still unset.
if [ -z "$PROVIDER" ]; then
if [ "$NON_INTERACTIVE" = 1 ]; then
PROVIDER="xiaomi-mimo"
else
local P_MIMO="xiaomi-mimo — Token Plan / Orbit, free tokens via Orbit program (default)"
local P_OR="openrouter — multi-model gateway (sk-or-v1-...)"
local P_ANT="anthropic — Claude API"
local P_OAI="openai — OpenAI API"
local P_GEM="gemini — Google Gemini"
local P_GRQ="groq — fast Llama / Mixtral"
local P_DS="deepseek — DeepSeek chat & reasoner"
local P_MM="minimax — MiniMax"
local P_ZAI="zai — Z.AI / GLM"
local P_OLL="ollama — local Ollama, no API key"
local P_CUS="custom — you supply name + base_url + key_env"
local P_PREV="← Previous step"
local picked=""
if has_arrow_capability; then
picked="$(arrow_pick "Default LLM provider" \
"$P_MIMO" "$P_OR" "$P_ANT" "$P_OAI" "$P_GEM" \
"$P_GRQ" "$P_DS" "$P_MM" "$P_ZAI" "$P_OLL" "$P_CUS" "$P_PREV")"
local arc=$?
[ "$arc" = 0 ] || picked=""
[ "$picked" = "$P_PREV" ] && return 2
fi
if [ -z "$picked" ]; then
{
cat <<'EOF'
Default LLM provider for these gateways:
1) xiaomi-mimo (default — Token Plan / Orbit, free tokens via Orbit program)
2) openrouter (multi-model gateway, sk-or-v1-...)
3) anthropic (Claude API)
4) openai (OpenAI API)
5) gemini (Google Gemini)
6) groq (fast Llama / Mixtral)
7) deepseek (DeepSeek chat & reasoner)
8) minimax (MiniMax)
9) zai (Z.AI / GLM)
10) ollama (local Ollama, no API key)
11) custom (you supply name + base_url + key_env)
EOF
} >&2
local sel; sel="$(prompt "Pick 1-11" "1")"
case "$sel" in
1|xiaomi-mimo) PROVIDER="xiaomi-mimo" ;;
2|openrouter) PROVIDER="openrouter" ;;
3|anthropic) PROVIDER="anthropic" ;;
4|openai) PROVIDER="openai" ;;
5|gemini) PROVIDER="gemini" ;;
6|groq) PROVIDER="groq" ;;
7|deepseek) PROVIDER="deepseek" ;;
8|minimax) PROVIDER="minimax" ;;
9|zai) PROVIDER="zai" ;;
10|ollama) PROVIDER="ollama" ;;
11|custom) PROVIDER="custom" ;;
*) die "invalid pick: $sel" ;;
esac
else
case "$picked" in
"$P_MIMO") PROVIDER="xiaomi-mimo" ;;
"$P_OR") PROVIDER="openrouter" ;;
"$P_ANT") PROVIDER="anthropic" ;;
"$P_OAI") PROVIDER="openai" ;;
"$P_GEM") PROVIDER="gemini" ;;
"$P_GRQ") PROVIDER="groq" ;;
"$P_DS") PROVIDER="deepseek" ;;
"$P_MM") PROVIDER="minimax" ;;
"$P_ZAI") PROVIDER="zai" ;;
"$P_OLL") PROVIDER="ollama" ;;
"$P_CUS") PROVIDER="custom" ;;
esac
fi
fi
fi
# Custom provider — gather everything from the user (or flags).
if [ "$PROVIDER" = "custom" ]; then
if [ "$NON_INTERACTIVE" = 1 ]; then
[ -n "$MODEL" ] && [ -n "$BASE_URL" ] && [ -n "$KEY_ENV" ] \
|| die "--provider custom requires --model, --base-url, --key-env"
fi
local cname=""
if [ "$NON_INTERACTIVE" = 0 ]; then
cname="$(prompt "Provider short name (a-z0-9-)" "myprovider")"
else
cname="${HERMES_CUSTOM_PROVIDER_NAME:-myprovider}"
fi
[[ "$cname" =~ ^[a-z0-9][a-z0-9_-]*$ ]] || die "invalid custom provider name: $cname"
[ -z "$MODEL" ] && MODEL="$(prompt "Default model id" "")"
[ -z "$BASE_URL" ] && BASE_URL="$(prompt "Base URL (OpenAI-compatible /v1)" "")"
[ -z "$KEY_ENV" ] && KEY_ENV="$(prompt "Env var name for API key" "MY_API_KEY")"
PROVIDER="$cname"
else
local info; info="$(provider_info "$PROVIDER")" \
|| die "unknown provider: $PROVIDER (see --help)"
local def_model def_url def_key _builtin
IFS='|' read -r def_model def_url def_key _builtin <<<"$info"
# Model
if [ -z "$MODEL" ]; then
MODEL="$(pick_model_for "$PROVIDER" "")"
local mrc=$?
[ "$mrc" = 2 ] && { PROVIDER=""; return 2; }
fi
# Base URL (skip for built-in providers — they don't need one)
if [ -z "$BASE_URL" ]; then
BASE_URL="$def_url"
# Xiaomi MiMo region picker
if [ "$PROVIDER" = "xiaomi-mimo" ] && [ "$NON_INTERACTIVE" = 0 ]; then
echo
echo "Xiaomi MiMo region (must match your dashboard's dedicated URL):"
echo " 1) Singapore (sgp) [default]"
echo " 2) Amsterdam (ams)"
echo " 3) China (cn)"
local r; r="$(prompt "Pick" "1")"
case "$r" in
2) BASE_URL="https://token-plan-ams.xiaomimimo.com/v1" ;;
3) BASE_URL="https://token-plan-cn.xiaomimimo.com/v1" ;;
*) BASE_URL="https://token-plan-sgp.xiaomimimo.com/v1" ;;
esac
fi
fi
# Key env var
[ -z "$KEY_ENV" ] && KEY_ENV="$def_key"
fi
# Fallback provider
if [ -z "$FALLBACK_PROVIDER" ]; then
if [ "$NON_INTERACTIVE" = 1 ]; then
# Sensible default: openrouter unless the primary IS openrouter
if [ "$PROVIDER" = "openrouter" ]; then
FALLBACK_PROVIDER="none"
else
FALLBACK_PROVIDER="openrouter"
fi
else
local default_fb="openrouter"
[ "$PROVIDER" = "openrouter" ] && default_fb="none"
local fb; fb="$(prompt "Fallback provider (or 'none')" "$default_fb")"
FALLBACK_PROVIDER="${fb:-none}"
fi
fi
if [ "$FALLBACK_PROVIDER" != "none" ] && [ -n "$FALLBACK_PROVIDER" ]; then
if [ -z "$FALLBACK_MODEL" ]; then
FALLBACK_MODEL="$(pick_model_for "$FALLBACK_PROVIDER" "")"
local frc=$?
[ "$frc" = 2 ] && { FALLBACK_PROVIDER=""; return 2; }
fi
fi
log "Provider: primary=$PROVIDER model=$MODEL${BASE_URL:+ base=$BASE_URL}${KEY_ENV:+ key=$KEY_ENV}"
if [ "$FALLBACK_PROVIDER" != "none" ] && [ -n "$FALLBACK_PROVIDER" ]; then
log "Provider: fallback=$FALLBACK_PROVIDER model=$FALLBACK_MODEL"
else
log "Provider: no fallback"
fi
}
# -----------------------------------------------------------------------------
# Generate config.yaml content for the chosen provider(s).
# Always writes a complete config.yaml — independent of templates/ — so the
# provider section is in sync with the .env keys.
# -----------------------------------------------------------------------------
generate_config_yaml() {
local out="$1"
{
cat <<EOF
# =============================================================================
# Hermes Gateway config — generated by bootstrap.sh
# =============================================================================
# Safe to commit. The Telegram bot token gets injected into
# platforms.telegram.token at startup by inject_config.py — DO NOT paste
# tokens here directly.
#
# To swap providers / models manually: edit the model + custom_providers
# blocks below. See README §"LLM Provider Reference" for the supported
# provider list, base URLs, and example model IDs.
# =============================================================================
model:
default: $MODEL
provider: $(yaml_provider_ref "$PROVIDER")
api_mode: chat_completions
EOF
if [ "$FALLBACK_PROVIDER" != "none" ] && [ -n "$FALLBACK_PROVIDER" ]; then
cat <<EOF
fallback_providers:
- provider: $(yaml_provider_ref "$FALLBACK_PROVIDER")
model: $FALLBACK_MODEL
EOF
fi
# custom_providers block — only emitted when at least one of the
# primary/fallback is non-built-in.
local need_custom=0
provider_is_builtin "$PROVIDER" || need_custom=1
if [ "$FALLBACK_PROVIDER" != "none" ] && [ -n "$FALLBACK_PROVIDER" ] \
&& [ "$FALLBACK_PROVIDER" != "$PROVIDER" ] \
&& ! provider_is_builtin "$FALLBACK_PROVIDER"; then
need_custom=1
fi
echo
if [ "$need_custom" = 1 ]; then
echo "custom_providers:"
if ! provider_is_builtin "$PROVIDER"; then
cat <<EOF
- name: $PROVIDER
base_url: $BASE_URL
EOF
if [ -n "$KEY_ENV" ]; then
echo " key_env: $KEY_ENV"
fi
fi
if [ "$FALLBACK_PROVIDER" != "none" ] && [ -n "$FALLBACK_PROVIDER" ] \
&& [ "$FALLBACK_PROVIDER" != "$PROVIDER" ] \
&& ! provider_is_builtin "$FALLBACK_PROVIDER"; then
local fb_info fb_url fb_key
fb_info="$(provider_info "$FALLBACK_PROVIDER")"
IFS='|' read -r _ fb_url fb_key _ <<<"$fb_info"
cat <<EOF
- name: $FALLBACK_PROVIDER
base_url: $fb_url
key_env: $fb_key
EOF
fi
fi
# providers block — built-in providers may be configured here.
echo
echo "providers: {}"
# Auxiliary tasks (compression + title generation) — route to the
# primary provider's model so the context window matches.
cat <<EOF
auxiliary:
compression:
provider: $(yaml_provider_ref "$PROVIDER")
model: $MODEL
title_generation:
provider: $(yaml_provider_ref "$PROVIDER")
model: $MODEL
EOF
# Standard rest-of-config (matches templates/config.yaml.template).
cat <<'EOF'
agent:
max_turns: 90
gateway_timeout: 1800 # 30 minutes per turn
restart_drain_timeout: 60
service_tier: ''
tool_use_enforcement: auto
gateway_timeout_warning: 900
gateway_notify_interval: 600
verbose: false
reasoning_effort: medium
system_prompt: '' # leave empty — HERMES_EPHEMERAL_SYSTEM_PROMPT in .env wins
platforms:
telegram:
enabled: true
token: '' # injected by inject_config.py at startup
extra:
require_pairing: false
platform_toolsets:
cli:
- hermes-cli
telegram:
- hermes-telegram
sessions_dir: ./sessions
session_reset:
mode: both
idle_minutes: 1440 # auto-reset session after 24h idle
at_hour: 4 # …or daily at 04:00 local time
streaming:
enabled: false
approvals:
mode: manual
timeout: 60
cron_mode: deny
command_allowlist: []
quick_commands: {}
hooks: {}
hooks_auto_accept: false
privacy:
redact_pii: false
cron:
wrap_response: true
max_parallel_jobs: null
code_execution:
mode: project
timeout: 300
max_tool_calls: 50
logging:
level: INFO
max_size_mb: 5
backup_count: 3
network:
force_ipv4: false
_config_version: 22
group_sessions_per_user: true
# Set this to your own Telegram chat ID if you want the bot to post home-channel
# notifications (cron summaries, errors, etc.). Leave blank to disable.
TELEGRAM_HOME_CHANNEL: ''
EOF
} > "$out"
}
# -----------------------------------------------------------------------------
# Generate .env content with the right API-key placeholders for the chosen
# provider(s). Mirrors templates/.env.template, but swaps the provider keys.
# -----------------------------------------------------------------------------
generate_env_content() {
local out="$1"
{