-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathafx-cli
More file actions
executable file
·2202 lines (1931 loc) · 76.2 KB
/
afx-cli
File metadata and controls
executable file
·2202 lines (1931 loc) · 76.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
#!/bin/bash
# AFX CLI — install, update, and manage AFX skills
# Usage:
# Install: ./afx-cli /path/to/project
# Update: ./afx-cli --update /path/to/project
# Local: ./afx-cli --source /path/to/afx --target /path/to/project
# Remote: curl -sL https://raw.githubusercontent.com/rixrix/afx/main/afx-cli | bash -s -- .
#
# Options:
# --update Update existing AFX installation (preserves user content)
# --skills-only Only sync skills to .agents/skills/ (skip templates, docs, MD)
# --no-claude-md Skip CLAUDE.md snippet integration
# --no-agents-md Skip AGENTS.md snippet integration
# --with-gemini-md Include GEMINI.md snippet integration (opt-in)
# --no-docs Skip copying AFX documentation to docs/agenticflowx/
# --force Overwrite all existing files (fresh install)
# --dry-run Show what would be changed without making changes
# --yes Skip all confirmation prompts (non-interactive mode)
# --reset Reset AFX: recreate .afx/ folder, .afx.yaml defaults
set -e
# ============================================================================
# Section 1: Constants & Colors
# ============================================================================
AFX_REPO="rixrix/afx"
# Boundary markers
AFX_START_MARKER="<!-- AFX:START - Managed by AFX. Do not edit manually. -->"
AFX_END_MARKER="<!-- AFX:END -->"
AFX_AGENTS_START_MARKER="<!-- AFX-CODEX:START - Managed by AFX. Do not edit manually. -->"
AFX_AGENTS_END_MARKER="<!-- AFX-CODEX:END -->"
AFX_GEMINI_START_MARKER="<!-- AFX-GEMINI:START - Managed by AFX. Do not edit manually. -->"
AFX_GEMINI_END_MARKER="<!-- AFX-GEMINI:END -->"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
# ============================================================================
# Section 2: Default Options
# ============================================================================
UPDATE_MODE=false
SKILLS_ONLY=false
NO_CLAUDE_MD=""
NO_AGENTS_MD=""
NO_GEMINI_MD=""
NO_DOCS=false
FORCE=false
DRY_RUN=false
YES=false
RESET=false
TARGET_DIR=""
# Pack management options
# @see docs/specs/afx-packs/design.md#33-new-arguments
PACK_NAMES=()
PACK_REMOVE=""
PACK_LIST=false
SKILL_DISABLE=""
SKILL_ENABLE=""
UPDATE_PACKS=false
ADD_SKILL=""
BRANCH=""
VERSION=""
SOURCE_DIR=""
VERBOSE=false
# Change tracking
INSTALLED=()
UPDATED=()
SKIPPED=()
# ============================================================================
# Section 3: Argument Parsing
# ============================================================================
while [[ $# -gt 0 ]]; do
case $1 in
--update) UPDATE_MODE=true; shift ;;
--skills-only) SKILLS_ONLY=true; shift ;;
--commands-only) SKILLS_ONLY=true; shift ;; # backward compat alias
--no-claude-md) NO_CLAUDE_MD=true; NO_CLAUDE_MD_FLAG=true; shift ;;
--no-agents-md) NO_AGENTS_MD=true; NO_AGENTS_MD_FLAG=true; shift ;;
--with-gemini-md) NO_GEMINI_MD=false; NO_GEMINI_MD_FLAG=true; shift ;;
--no-docs) NO_DOCS=true; shift ;;
--force) FORCE=true; shift ;;
--dry-run) DRY_RUN=true; shift ;;
--yes|-y) YES=true; shift ;;
--reset) RESET=true; shift ;;
--pack) PACK_NAMES+=("$2"); shift 2 ;;
--pack-remove) PACK_REMOVE="$2"; shift 2 ;;
--pack-list) PACK_LIST=true; shift ;;
--skill-disable) SKILL_DISABLE="$2"; shift 2 ;;
--skill-enable) SKILL_ENABLE="$2"; shift 2 ;;
--packs) UPDATE_PACKS=true; shift ;;
--add-skill) ADD_SKILL="$2"; shift 2 ;;
--branch) BRANCH="$2"; shift 2 ;;
--version) VERSION="$2"; shift 2 ;;
--source) SOURCE_DIR="$2"; shift 2 ;;
--target) TARGET_DIR="$2"; shift 2 ;;
--verbose) VERBOSE=true; shift ;;
-h|--help)
cat <<'HELPEOF'
AFX Installer
Usage: ./afx-cli [OPTIONS] [target-project-path]
Options:
--update Update existing AFX installation
--skills-only Only sync skills to .agents/skills/ (skip templates, docs, MD)
--commands-only Alias for --skills-only (backward compat)
--no-claude-md Skip CLAUDE.md snippet integration
--no-agents-md Skip AGENTS.md snippet integration
--with-gemini-md Include GEMINI.md snippet integration (opt-in)
--no-docs Skip copying AFX documentation to docs/agenticflowx/
--force Overwrite all files (fresh install)
--dry-run Preview changes without applying
--yes, -y Skip all confirmation prompts
--reset Reset AFX: recreate .afx/ folder and config files
--branch NAME Use a specific branch (default: main)
--version TAG Use a specific version tag (e.g., 1.5.3 or v1.5.3)
--source PATH Use local AFX checkout instead of downloading from GitHub.
Useful for development, testing, or offline installs.
--target PATH Target project directory (alternative to positional argument)
--verbose Show detailed logging (curl, tar, file copies, git operations)
-h, --help Show this help message
Pack Management:
--pack NAME Install a pack
--pack-remove NAME Remove a pack entirely
--pack-list List installed packs and skills
--skill-disable NAME Disable a skill (remove from .agents/skills/)
--skill-enable NAME Re-enable a skill (sync to .agents/skills/)
--update --packs Update all installed packs
--add-skill REPO:PATH/SKILL Install a single skill (no pack)
Examples:
# Fresh install
./afx-cli .
# Non-interactive install
./afx-cli --yes .
# Update existing installation
./afx-cli --update .
# Remote install
curl -sL https://raw.githubusercontent.com/rixrix/afx/main/afx-cli | bash -s -- .
# Install QA pack (short or full name)
./afx-cli --pack qa .
./afx-cli --pack afx-pack-qa .
# Install from version
./afx-cli --version 1.5.3 --pack qa .
# Multiple packs
./afx-cli --pack qa --pack security .
# Manage packs (short or full name)
./afx-cli --pack-disable afx-pack-qa .
./afx-cli --pack-enable qa .
./afx-cli --pack-list .
# Update all packs
./afx-cli --update --packs .
# Install from local AFX checkout (dev/testing/offline)
./afx-cli --source ../afx .
./afx-cli --source ../afx --target /path/to/project
./afx-cli --update --source ../afx .
./afx-cli --pack qa --source ../afx .
# Debug install issues
./afx-cli --verbose .
# Reset AFX (recreate .afx/ folder and config)
./afx-cli --reset .
HELPEOF
exit 0
;;
*)
TARGET_DIR="$1"
shift
;;
esac
done
# ============================================================================
# Section 4: Core Helper Functions
# ============================================================================
# Log a message only when --verbose is set.
# Outputs to stderr so it's safe to use inside $() command substitutions.
# Usage: vlog "downloading from $url"
vlog() {
[[ "$VERBOSE" == "true" ]] && echo -e "${DIM} [verbose] $*${NC}" >&2
return 0
}
# Create a temp directory inside .afx/.cache/ (project-local, not /tmp/).
# Falls back to system mktemp if TARGET_DIR is not yet set.
afx_mktemp_dir() {
if [[ -n "$TARGET_DIR" && -d "$TARGET_DIR" ]]; then
local cache_dir="$TARGET_DIR/.afx/.cache"
mkdir -p "$cache_dir"
local dir
dir=$(mktemp -d "$cache_dir/tmp.XXXXXXXXXX")
vlog "mktemp -d -> $dir"
echo "$dir"
else
mktemp -d
fi
}
# Create a temp file inside .afx/.cache/ (project-local, not /tmp/).
# Falls back to system mktemp if TARGET_DIR is not yet set.
afx_mktemp() {
if [[ -n "$TARGET_DIR" && -d "$TARGET_DIR" ]]; then
local cache_dir="$TARGET_DIR/.afx/.cache"
mkdir -p "$cache_dir"
local file
file=$(mktemp "$cache_dir/tmp.XXXXXXXXXX")
vlog "mktemp -> $file"
echo "$file"
else
mktemp
fi
}
# Ask for confirmation. Returns 0 (yes) or 1 (no).
# Usage: confirm "Do something?" && do_it
# Defaults to Yes if user just presses Enter.
# Skipped entirely if --yes flag is set.
confirm() {
local prompt="$1"
if [[ "$YES" == "true" || "$DRY_RUN" == "true" ]]; then
return 0
fi
echo -en "${BOLD}${prompt}${NC} ${DIM}[Y/n]${NC} "
read -r answer </dev/tty
case "$answer" in
[nN]|[nN][oO]) return 1 ;;
*) return 0 ;;
esac
}
# Install or update a single file
install_file() {
local src="$1"
local dest="$2"
local desc="$3"
local always_update="${4:-false}"
if [ "$DRY_RUN" = "true" ]; then
if [ -e "$dest" ]; then
if [ "$UPDATE_MODE" = "true" ] || [ "$FORCE" = "true" ] || [ "$always_update" = "true" ]; then
UPDATED+=("$desc (would update)")
else
SKIPPED+=("$desc (exists)")
fi
else
INSTALLED+=("$desc (would create)")
fi
return 0
fi
if [ -e "$dest" ]; then
if [ "$UPDATE_MODE" = "true" ] || [ "$FORCE" = "true" ] || [ "$always_update" = "true" ]; then
mkdir -p "$(dirname "$dest")"
vlog "cp -r $src -> $dest (update)"
cp -r "$src" "$dest"
UPDATED+=("$desc")
else
vlog "skip $dest (exists)"
SKIPPED+=("$desc (exists)")
fi
else
mkdir -p "$(dirname "$dest")"
vlog "cp -r $src -> $dest (new)"
cp -r "$src" "$dest"
INSTALLED+=("$desc")
fi
}
# Install or update a directory by replacing destination contents
install_directory() {
local src="$1"
local dest="$2"
local desc="$3"
local always_update="${4:-false}"
if [ "$DRY_RUN" = "true" ]; then
if [ -e "$dest" ]; then
if [ "$UPDATE_MODE" = "true" ] || [ "$FORCE" = "true" ] || [ "$always_update" = "true" ]; then
UPDATED+=("$desc (would update)")
else
SKIPPED+=("$desc (exists)")
fi
else
INSTALLED+=("$desc (would create)")
fi
return 0
fi
if [ -e "$dest" ]; then
if [ "$UPDATE_MODE" = "true" ] || [ "$FORCE" = "true" ] || [ "$always_update" = "true" ]; then
rm -rf "$dest"
mkdir -p "$(dirname "$dest")"
cp -R "$src" "$dest"
UPDATED+=("$desc")
else
SKIPPED+=("$desc (exists)")
fi
else
mkdir -p "$(dirname "$dest")"
cp -R "$src" "$dest"
INSTALLED+=("$desc")
fi
}
# Update a markdown file with AFX boundary markers (reusable for CLAUDE.md, AGENTS.md, etc.)
# Usage: update_md_with_markers <file> <start_marker> <end_marker> <snippet_content> <label> <header>
update_md_with_markers() {
local md_file="$1"
local start_marker="$2"
local end_marker="$3"
local snippet_content="$4"
local label="$5"
local header_content="$6"
vlog "update_md_with_markers: $md_file ($label)"
local section="${start_marker}
<!-- AFX Version: ${AFX_VERSION} -->
${snippet_content}
${end_marker}"
if [ "$DRY_RUN" = "true" ]; then
if [ -f "$md_file" ]; then
if grep -q "$start_marker" "$md_file" 2>/dev/null; then
UPDATED+=("$label AFX section (would update)")
elif [[ "$label" == "CLAUDE.md" ]] && grep -q "## Documentation References\|## AgenticFlow" "$md_file" 2>/dev/null; then
SKIPPED+=("$label (has old AFX section - run with --force to migrate)")
else
INSTALLED+=("$label AFX section (would append)")
fi
else
INSTALLED+=("$label (would create)")
fi
return 0
fi
if [ -f "$md_file" ]; then
if grep -q "$start_marker" "$md_file"; then
# Has boundary markers — replace the section
awk -v start="$start_marker" '$0 == start { exit } { print }' "$md_file" > "$md_file.tmp"
echo "$section" >> "$md_file.tmp"
awk -v end="$end_marker" 'BEGIN { skip=1 } $0 == end { skip=0; next } !skip { print }' "$md_file" >> "$md_file.tmp"
mv "$md_file.tmp" "$md_file"
UPDATED+=("$label AFX section")
elif [[ "$label" == "CLAUDE.md" ]] && grep -q "## Documentation References\|## AgenticFlow" "$md_file"; then
# Old-style section without markers
if [ "$FORCE" = "true" ]; then
echo -e "${YELLOW}Warning: Migrating old AFX section to use boundary markers${NC}"
echo "" >> "$md_file"
echo "$section" >> "$md_file"
UPDATED+=("$label (migrated - please remove old AFX section manually)")
else
SKIPPED+=("$label (has old AFX section - use --force to migrate)")
fi
else
# No AFX section — append
echo "" >> "$md_file"
echo "$section" >> "$md_file"
INSTALLED+=("$label AFX section")
fi
else
# Create new file
mkdir -p "$(dirname "$md_file")"
echo "$header_content" > "$md_file"
echo "" >> "$md_file"
echo "$section" >> "$md_file"
INSTALLED+=("$label (created)")
fi
}
# Read agent selection from existing .afx.yaml (if present).
# Sets NO_CLAUDE_MD/NO_AGENTS_MD/NO_GEMINI_MD only if still empty (not set by flags).
read_agent_config() {
local yaml="$TARGET_DIR/.afx.yaml"
[[ -f "$yaml" ]] || return 0
if [[ -z "$NO_CLAUDE_MD" ]]; then
local val
val=$(sed -n '/^agents:/,/^[^ #]/{ s/^ claude: *//p; }' "$yaml" | head -1)
case "$val" in
true) NO_CLAUDE_MD=false ;;
false) NO_CLAUDE_MD=true ;;
esac
fi
if [[ -z "$NO_AGENTS_MD" ]]; then
local val
val=$(sed -n '/^agents:/,/^[^ #]/{ s/^ agents: *//p; }' "$yaml" | head -1)
case "$val" in
true) NO_AGENTS_MD=false ;;
false) NO_AGENTS_MD=true ;;
esac
fi
if [[ -z "$NO_GEMINI_MD" ]]; then
local val
val=$(sed -n '/^agents:/,/^[^ #]/{ s/^ gemini: *//p; }' "$yaml" | head -1)
case "$val" in
true) NO_GEMINI_MD=false ;;
false) NO_GEMINI_MD=true ;;
esac
fi
}
# Update the agents: block in .afx.yaml with current selection.
# Creates the block if it doesn't exist, replaces it if it does.
update_agent_config() {
local yaml="$TARGET_DIR/.afx.yaml"
[[ -f "$yaml" ]] || return 0
[[ "$DRY_RUN" == "true" ]] && return 0
local claude_val="true" agents_val="true" gemini_val="false"
[[ "$NO_CLAUDE_MD" == "true" ]] && claude_val="false"
[[ "$NO_AGENTS_MD" == "true" ]] && agents_val="false"
[[ "$NO_GEMINI_MD" != "true" ]] && gemini_val="true"
local block
block="agents:\n claude: ${claude_val}\n agents: ${agents_val}\n gemini: ${gemini_val}"
local tmp="$yaml.tmp.$$"
if grep -q '^agents:' "$yaml"; then
# Remove existing agents block first
awk '
/^agents:/ { skip=1; next }
skip && /^ [a-z]/ { next }
skip && /^[^ #]/ { skip=0 }
skip { next }
{ print }
' "$yaml" > "$tmp"
else
cp "$yaml" "$tmp"
fi
# Insert agents block before skills: line (or append)
if grep -q '^skills:' "$tmp"; then
awk -v b="agents:\n claude: ${claude_val}\n agents: ${agents_val}\n gemini: ${gemini_val}" '
/^skills:/ { n=split(b, lines, "\\n"); for(i=1;i<=n;i++) print lines[i]; print ""; }
{ print }
' "$tmp" > "$tmp.2"
mv "$tmp.2" "$tmp"
else
printf '\n%b\n' "$block" >> "$tmp"
fi
mv "$tmp" "$yaml"
}
# Write minimal user-facing .afx.yaml (version + agents + packs + override guide)
# All defaults live in .afx/.afx.yaml — user only adds overrides here.
write_minimal_user_config() {
local claude_val="true" agents_val="true" gemini_val="false"
[[ "$NO_CLAUDE_MD" == "true" ]] && claude_val="false"
[[ "$NO_AGENTS_MD" == "true" ]] && agents_val="false"
[[ "$NO_GEMINI_MD" != "true" ]] && gemini_val="true"
cat > "$TARGET_DIR/.afx.yaml" <<YAMLEOF
# ┌─────────────────────────────────────────────────────────────────────────┐
# │ AFX Configuration │
# │ │
# │ Defaults are in .afx/.afx.yaml (managed by AFX — do not edit). │
# │ Add overrides below — they take precedence over defaults. │
# │ │
# │ Docs: docs/agenticflowx/agenticflowx.md │
# │ Help: /afx-help (Claude) or afx-help (Codex) │
# └─────────────────────────────────────────────────────────────────────────┘
# AFX version — controls which branch/tag afx-cli fetches from.
# Accepts: semver (e.g. '1.5.3' → tag v1.5.3), branch name, or 'main'.
version: main
# ── Agent Selection ────────────────────────────────────────────────────
# Which AI agents are enabled. Managed by afx-cli during install/update.
agents:
claude: ${claude_val}
agents: ${agents_val}
gemini: ${gemini_val}
# ── Installed Packs & Skills ─────────────────────────────────────────────
# Managed by afx-cli. Add packs with:
# ./afx-cli install afx-pack-qa
# ./afx-cli install afx-pack-security
skills:
packs:
# ── Your Overrides ───────────────────────────────────────────────────────
# Override any value from .afx/.afx.yaml here. Examples:
#
# paths:
# specs: my-specs # Custom spec directory
#
# features:
# - user-auth # Active features
#
# quality_gates:
# require_path_check: false
YAMLEOF
}
# ============================================================================
# Section 5: Pack Management Functions
# @see docs/specs/afx-packs/design.md#3-installsh-architecture
# @see docs/specs/afx-packs/tasks.md#phase-3-installsh--download--detection
# ============================================================================
# Resolve the git ref for AFX repo fetches
# @see docs/specs/afx-packs/design.md#34-download-strategy
resolve_ref() {
if [[ -n "$VERSION" && -n "$BRANCH" ]]; then
echo -e "${RED}Error: --version and --branch are mutually exclusive${NC}" >&2
return 1
fi
if [[ -n "$VERSION" ]]; then
[[ "$VERSION" == v* ]] && echo "$VERSION" || echo "v$VERSION"
elif [[ -n "$BRANCH" ]]; then
echo "$BRANCH"
else
local yaml_version=""
if [[ -f "$TARGET_DIR/.afx.yaml" ]]; then
yaml_version=$(grep '^version:' "$TARGET_DIR/.afx.yaml" 2>/dev/null | awk '{print $2}' | tr -d "'\"")
fi
if [[ -n "$yaml_version" && "$yaml_version" != "main" ]]; then
if [[ "$yaml_version" =~ ^[0-9] ]]; then
[[ "$yaml_version" == v* ]] && echo "$yaml_version" || echo "v$yaml_version"
else
echo "$yaml_version"
fi
else
echo "main"
fi
fi
}
# Fetch a pack manifest YAML from raw.githubusercontent.com
# @see docs/specs/afx-packs/design.md#34-download-strategy
fetch_manifest() {
local pack_name="$1"
local ref="$2"
local temp_manifest=$(afx_mktemp)
# Prefer local manifest when running from the AFX repo (dev/testing)
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd || echo "")"
if [[ -n "$script_dir" && -f "$script_dir/packs/${pack_name}.yaml" ]]; then
vlog "fetch_manifest: using local $script_dir/packs/${pack_name}.yaml"
cp "$script_dir/packs/${pack_name}.yaml" "$temp_manifest"
echo "$temp_manifest"
return 0
fi
vlog "fetch_manifest: curl https://raw.githubusercontent.com/${AFX_REPO}/${ref}/packs/${pack_name}.yaml"
curl -sfL "https://raw.githubusercontent.com/${AFX_REPO}/${ref}/packs/${pack_name}.yaml" \
-o "$temp_manifest" 2>/dev/null
if [[ ! -s "$temp_manifest" ]]; then
rm -f "$temp_manifest"
echo -e "${RED}Error: Failed to fetch manifest for '${pack_name}' (ref: ${ref})${NC}" >&2
echo "Check pack name and try again." >&2
return 1
fi
echo "$temp_manifest"
}
# Download and extract specific paths from a GitHub repo tarball
# @see docs/specs/afx-packs/design.md#34-download-strategy
download_items() {
local repo="$1"
local ref="$2"
local base_path="$3"
shift 3
local items=("$@")
local url="https://codeload.github.com/${repo}/tar.gz/${ref}"
local temp_dir=$(afx_mktemp_dir)
vlog "download_items: curl -sL $url | tar xz -C $temp_dir"
curl -sL "$url" | tar xz -C "$temp_dir" --strip-components=1 2>/dev/null
echo "$temp_dir"
}
# Parse manifest includes[] — outputs one line per include block: "repo path item1 item2 ..."
# @see docs/specs/afx-packs/design.md#35-yaml-parsing
for_each_include() {
local manifest="$1"
local in_includes=false
local current_repo="" current_path=""
local items=()
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "includes:" ]]; then
in_includes=true; continue
fi
if $in_includes && [[ "$line" =~ ^[a-z] ]]; then
[[ -n "$current_repo" ]] && echo "$current_repo $current_path ${items[*]}"
break
fi
if $in_includes; then
if [[ "$line" =~ ^\ \ -\ repo:\ (.+) ]]; then
[[ -n "$current_repo" ]] && echo "$current_repo $current_path ${items[*]}"
current_repo="${BASH_REMATCH[1]}"
current_path="" ; items=()
elif [[ "$line" =~ ^\ \ \ \ path:\ (.+) ]]; then
current_path="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^\ \ \ \ \ \ -\ ([^#]+) ]]; then
local item_name="${BASH_REMATCH[1]}"
item_name="${item_name%%#*}"
item_name="${item_name%% }"
item_name="${item_name%% }"
items+=("$item_name")
fi
fi
done < "$manifest"
[[ -n "$current_repo" ]] && echo "$current_repo $current_path ${items[*]}"
}
# Detect skill type
# @see docs/specs/afx-packs/design.md#36-type-detection--routing
detect_type() {
local item_dir="$1"
local source_repo="$2"
if [[ "$source_repo" == "${AFX_REPO}" ]]; then
echo "afx"
elif [[ -d "$item_dir/.claude-plugin" ]]; then
echo "plugin"
elif [[ -f "$item_dir/agents/openai.yaml" ]]; then
echo "openai"
elif [[ -f "$item_dir/SKILL.md" ]]; then
echo "skill"
else
echo "unknown"
fi
}
# Route a detected item to .afx/skills/{category}/{name}/ (canonical storage)
# @see docs/specs/afx-packs/design.md#36-type-detection--routing
route_item() {
local item_dir="$1"
local item_name="$2"
local type="$3"
local category="$4"
local canonical_dir="$TARGET_DIR/.afx/skills/$category/$item_name"
vlog "route_item: $item_name ($type) -> $canonical_dir"
case "$type" in
skill|openai)
mkdir -p "$canonical_dir"
cp -r "$item_dir"/. "$canonical_dir/"
;;
plugin)
mkdir -p "$canonical_dir"
cp -r "$item_dir"/. "$canonical_dir/"
;;
afx)
if [[ ! -f "$item_dir/SKILL.md" ]]; then
echo -e "${YELLOW}Warning: No SKILL.md in AFX skill '$item_name' — skipping${NC}"
return 1
fi
mkdir -p "$canonical_dir"
cp -r "$item_dir"/. "$canonical_dir/"
;;
*)
echo -e "${YELLOW}Warning: Unknown skill type for '$item_name' — skipping${NC}"
return 1
;;
esac
}
# Derive category from pack name: afx-pack-dev → dev, afx-pack-agenticflowx → agenticflowx
category_from_pack() {
local pack_name="$1"
echo "${pack_name#afx-pack-}"
}
# Check name collision in canonical skills store
# @see docs/specs/afx-packs/design.md#38-name-collision-detection
check_collision() {
local item_name="$1"
local category="$2"
local current_pack="$3"
local canonical="$TARGET_DIR/.afx/skills/$category/$item_name"
if [[ -d "$canonical" ]] && [[ "$FORCE" != "true" ]]; then
echo -e "${RED}Error: '$item_name' already installed in category '$category'${NC}"
echo "Use --force to overwrite."
return 1
fi
return 0
}
# ============================================================================
# Section 6: .afx.yaml Read/Write Helpers
# @see docs/specs/afx-packs/design.md#311-afxyaml-readwrite
# ============================================================================
# List all installed packs (name:ref format)
afx_yaml_all_packs() {
local yaml="$TARGET_DIR/.afx.yaml"
[[ -f "$yaml" ]] || return 0
local name="" ref="" in_packs=false
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "packs:" ]]; then
in_packs=true; continue
fi
if $in_packs && [[ -n "$line" ]] && [[ ! "$line" =~ ^\ \ ]]; then
[[ -n "$name" ]] && echo "$name:$ref"
name=""
break
fi
if $in_packs; then
if [[ "$line" =~ ^\ \ -\ name:\ (.+) ]]; then
[[ -n "$name" ]] && echo "$name:$ref"
name="${BASH_REMATCH[1]}"
ref="main"
elif [[ "$line" =~ ^\ \ \ \ installed_ref:\ (.+) ]]; then
ref="${BASH_REMATCH[1]}"
fi
fi
done < "$yaml"
[[ -n "$name" ]] && echo "$name:$ref"
}
afx_yaml_pack_ref() {
local pack_name="$1"
local yaml="$TARGET_DIR/.afx.yaml"
[[ -f "$yaml" ]] || { echo "main"; return 0; }
local found_pack=false
while IFS= read -r line; do
if [[ "$line" =~ name:\ $pack_name$ ]]; then
found_pack=true; continue
fi
if $found_pack && [[ "$line" =~ ^\ \ -\ name: ]]; then break; fi
if $found_pack && [[ "$line" =~ ^\ \ \ \ installed_ref:\ (.+) ]]; then
echo "${BASH_REMATCH[1]}"; return 0
fi
done < "$yaml"
echo "main"
}
# Add or update a pack entry in .afx.yaml
afx_yaml_set_pack() {
local pack_name="$1"
local ref="${2:-main}"
local yaml="$TARGET_DIR/.afx.yaml"
if [[ "$DRY_RUN" == "true" ]]; then
echo -e " ${CYAN}(would update .afx.yaml: pack $pack_name ref=$ref)${NC}"
return 0
fi
if [[ ! -f "$yaml" ]]; then
echo "packs:" > "$yaml"
fi
if grep -q "name: $pack_name" "$yaml" 2>/dev/null; then
# Update existing entry
local temp=$(afx_mktemp)
local in_target=false
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^\ \ -\ name:\ $pack_name$ ]]; then
in_target=true
echo "$line" >> "$temp"
elif $in_target && [[ "$line" =~ ^\ \ -\ name: || ! "$line" =~ ^\ \ ]]; then
in_target=false
echo "$line" >> "$temp"
elif $in_target && [[ "$line" =~ ^\ \ \ \ installed_ref: ]]; then
echo " installed_ref: $ref" >> "$temp"
elif $in_target && [[ "$line" =~ ^\ \ \ \ installed_at: ]]; then
echo " installed_at: $(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$temp"
else
echo "$line" >> "$temp"
fi
done < "$yaml"
mv "$temp" "$yaml"
else
if ! grep -q "^packs:" "$yaml" 2>/dev/null; then
echo "" >> "$yaml"
echo "packs:" >> "$yaml"
fi
# Insert into packs: section (after existing pack entries, before next section)
local entry
entry=$(cat <<PACKEOF
- name: $pack_name
installed_ref: $ref
installed_at: $(date -u +%Y-%m-%dT%H:%M:%SZ)
PACKEOF
)
local temp=$(afx_mktemp)
local inserted=false
local in_packs=false
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "packs:" ]]; then
in_packs=true
echo "$line" >> "$temp"
continue
fi
if $in_packs && ! $inserted; then
# Still in packs section if line is indented or empty
if [[ -z "$line" ]] || [[ "$line" =~ ^\ \ ]]; then
echo "$line" >> "$temp"
else
# Leaving packs section — insert entry before this line
echo "$entry" >> "$temp"
echo "$line" >> "$temp"
inserted=true
in_packs=false
fi
else
echo "$line" >> "$temp"
fi
done < "$yaml"
if ! $inserted; then
echo "$entry" >> "$temp"
fi
mv "$temp" "$yaml"
fi
}
# Remove a pack entry from .afx.yaml
afx_yaml_remove_pack() {
local pack_name="$1"
local yaml="$TARGET_DIR/.afx.yaml"
[[ -f "$yaml" ]] || return 0
if [[ "$DRY_RUN" == "true" ]]; then
echo -e " ${CYAN}(would remove pack $pack_name from .afx.yaml)${NC}"
return 0
fi
local temp=$(afx_mktemp)
local skip=false
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^\ \ -\ name:\ $pack_name$ ]]; then skip=true; continue; fi
if $skip && [[ "$line" =~ ^\ \ -\ name: ]]; then skip=false; fi
if $skip && [[ -n "$line" ]] && [[ ! "$line" =~ ^\ \ ]]; then skip=false; fi
$skip || echo "$line"
done < "$yaml" > "$temp"
mv "$temp" "$yaml"
}
# Add a skill entry to .afx.yaml skills[] section
afx_yaml_add_skill() {
local skill_name="$1"
local category="$2"
local source="$3"
local item_type="$4"
local yaml="$TARGET_DIR/.afx.yaml"
[[ "$DRY_RUN" == "true" ]] && return 0
# Skip if skill already exists (idempotent)
if grep -q "^ - name: ${skill_name}$" "$yaml" 2>/dev/null; then
return 0
fi
# Ensure skills: section exists
if ! grep -q "^skills:" "$yaml" 2>/dev/null; then
# Insert skills: before packs: if it exists, else append
if grep -q "^packs:" "$yaml" 2>/dev/null; then
sed -i.bak '/^packs:/i\
skills:\
' "$yaml"
rm -f "$yaml.bak"
else
echo "" >> "$yaml"
echo "skills:" >> "$yaml"
fi
fi
# Insert into skills: section (after existing skill entries, before next section)
local entry
entry=$(cat <<EOF
- name: $skill_name
category: $category
source: $source
item_type: $item_type
EOF
)
local temp=$(afx_mktemp)
local inserted=false
local in_skills=false
local pending_line=""
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "skills:" ]]; then
in_skills=true
echo "$line" >> "$temp"
continue
fi
if $in_skills && ! $inserted; then
# Still in skills section if line is indented or empty
if [[ -z "$line" ]] || [[ "$line" =~ ^\ \ ]]; then
echo "$line" >> "$temp"
else
# Leaving skills section — insert entry before this line
echo "$entry" >> "$temp"
echo "$line" >> "$temp"
inserted=true
in_skills=false
fi
else
echo "$line" >> "$temp"
fi
done < "$yaml"
if ! $inserted; then
echo "$entry" >> "$temp"
fi
mv "$temp" "$yaml"
}
# Remove a skill entry from .afx.yaml skills[] section
afx_yaml_remove_skill() {
local skill_name="$1"
local yaml="$TARGET_DIR/.afx.yaml"
[[ -f "$yaml" ]] || return 0
[[ "$DRY_RUN" == "true" ]] && return 0
local temp=$(afx_mktemp)
local skip=false
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^\ \ -\ name:\ $skill_name$ ]]; then skip=true; continue; fi
if $skip && [[ "$line" =~ ^\ \ -\ name: ]]; then skip=false; fi
if $skip && [[ -n "$line" ]] && [[ ! "$line" =~ ^\ \ ]]; then skip=false; fi
$skip || echo "$line"
done < "$yaml" > "$temp"
mv "$temp" "$yaml"
}
# Read skills for a given source pack from .afx.yaml
afx_yaml_skills_for_pack() {
local source_pack="$1"
local yaml="$TARGET_DIR/.afx.yaml"
[[ -f "$yaml" ]] || return 0
local name="" source="" category="" in_skills=false
while IFS= read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "skills:" ]]; then
in_skills=true; continue
fi
if $in_skills && [[ -n "$line" ]] && [[ ! "$line" =~ ^\ \ ]]; then
[[ -n "$name" && "$source" == "$source_pack" ]] && echo "$name:$category"
name=""
break
fi
if $in_skills; then
if [[ "$line" =~ ^\ \ -\ name:\ (.+) ]]; then
[[ -n "$name" && "$source" == "$source_pack" ]] && echo "$name:$category"
name="${BASH_REMATCH[1]}"
source="" ; category=""
elif [[ "$line" =~ ^\ \ \ \ source:\ (.+) ]]; then
source="${BASH_REMATCH[1]}"
elif [[ "$line" =~ ^\ \ \ \ category:\ (.+) ]]; then
category="${BASH_REMATCH[1]}"
fi
fi
done < "$yaml"
[[ -n "$name" && "$source" == "$source_pack" ]] && echo "$name:$category"
}
# ============================================================================
# Section 7: Pack Lifecycle Functions
# @see docs/specs/afx-packs/design.md#39-state-transitions
# ============================================================================
# Sync a single skill from canonical store to .agents/skills/ and .claude/skills/
# Reads from .afx/skills/{category}/{name}/ and copies to both targets
skill_sync() {
local skill_name="$1"
local category="$2"