update-agent-context.sh
25.4 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
#!/usr/bin/env bash
# 根据 plan.md 更新各类智能体的上下文文件
#
# 本脚本通过解析功能规格与实现计划,更新不同智能体对应的上下文/规则文件,
# 以便在后续命令(如 /speckit.tasks、/speckit.implement)中提供一致的项目背景信息。
#
# 主要功能:
# 1. 环境校验
# - 校验 git 仓库结构与分支信息
# - 校验所需的 plan.md 与模板文件是否存在
# - 校验文件权限与可访问性
#
# 2. 计划数据提取
# - 解析 plan.md 提取项目元信息
# - 识别语言/版本、主要依赖、存储与项目类型
# - 对缺失或不完整数据做容错处理
#
# 3. 智能体文件管理
# - 需要时基于模板创建新的上下文文件
# - 更新既有上下文文件并追加新的项目信息
# - 保留手工补充内容与自定义配置
# - 支持多种智能体格式与目录结构
#
# 4. 内容生成
# - 生成与语言相关的构建/测试命令
# - 生成合理的项目目录结构占位
# - 更新“当前技术栈”和“最近变更”等章节
# - 保持格式与时间戳一致
#
# 5. 多智能体支持
# - 处理不同智能体的路径与命名约定
# - 支持:Claude、Gemini、Copilot、Cursor、Qwen、opencode、Codex、Windsurf、Kilo Code、Auggie CLI、Roo Code、CodeBuddy CLI、Qoder CLI、Amp、SHAI、Amazon Q Developer CLI 等
# - 支持更新指定智能体或更新所有已存在的智能体文件
# - 若当前没有任何智能体文件,则创建默认的 Claude 文件
#
# 用法:./update-agent-context.sh [agent_type]
# agent_type:claude|gemini|copilot|cursor-agent|qwen|opencode|codex|windsurf|kilocode|auggie|shai|q|bob|qoder
# 不传参则更新所有已存在的智能体文件
set -e
# 开启更严格的错误处理
set -u
set -o pipefail
#==============================================================================
# 配置与全局变量
#==============================================================================
# 获取脚本目录并加载通用函数
SCRIPT_DIR="$(CDPATH="" cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common.sh"
# 从通用函数获取所有路径与变量
eval $(get_feature_paths)
NEW_PLAN="$IMPL_PLAN" # 兼容历史代码的别名
AGENT_TYPE="${1:-}"
# 不同智能体的文件路径
CLAUDE_FILE="$REPO_ROOT/CLAUDE.md"
GEMINI_FILE="$REPO_ROOT/GEMINI.md"
COPILOT_FILE="$REPO_ROOT/.github/agents/copilot-instructions.md"
CURSOR_FILE="$REPO_ROOT/.cursor/rules/specify-rules.mdc"
QWEN_FILE="$REPO_ROOT/QWEN.md"
AGENTS_FILE="$REPO_ROOT/AGENTS.md"
WINDSURF_FILE="$REPO_ROOT/.windsurf/rules/specify-rules.md"
KILOCODE_FILE="$REPO_ROOT/.kilocode/rules/specify-rules.md"
AUGGIE_FILE="$REPO_ROOT/.augment/rules/specify-rules.md"
ROO_FILE="$REPO_ROOT/.roo/rules/specify-rules.md"
CODEBUDDY_FILE="$REPO_ROOT/CODEBUDDY.md"
QODER_FILE="$REPO_ROOT/QODER.md"
AMP_FILE="$REPO_ROOT/AGENTS.md"
SHAI_FILE="$REPO_ROOT/SHAI.md"
Q_FILE="$REPO_ROOT/AGENTS.md"
BOB_FILE="$REPO_ROOT/AGENTS.md"
# 模板文件
TEMPLATE_FILE="$REPO_ROOT/.specify/templates/agent-file-template.md"
# 从 plan.md 解析得到的全局变量
NEW_LANG=""
NEW_FRAMEWORK=""
NEW_DB=""
NEW_PROJECT_TYPE=""
#==============================================================================
# 工具函数
#==============================================================================
log_info() {
echo "信息:$1"
}
log_success() {
echo "✓ $1"
}
log_error() {
echo "错误:$1" >&2
}
log_warning() {
echo "警告:$1" >&2
}
# 清理临时文件
cleanup() {
local exit_code=$?
rm -f /tmp/agent_update_*_$$
rm -f /tmp/manual_additions_$$
exit $exit_code
}
# 设置清理 trap
trap cleanup EXIT INT TERM
#==============================================================================
# 校验函数
#==============================================================================
validate_environment() {
# 检查是否能获取当前分支/功能(git 或非 git)
if [[ -z "$CURRENT_BRANCH" ]]; then
log_error "无法确定当前功能/分支"
if [[ "$HAS_GIT" == "true" ]]; then
log_info "请确认当前在功能分支上"
else
log_info "请设置 SPECIFY_FEATURE 环境变量,或先创建功能目录"
fi
exit 1
fi
# 检查 plan.md 是否存在
if [[ ! -f "$NEW_PLAN" ]]; then
log_error "未找到 plan.md:$NEW_PLAN"
log_info "请确认当前功能存在对应的 specs 目录"
if [[ "$HAS_GIT" != "true" ]]; then
log_info "可使用:export SPECIFY_FEATURE=your-feature-name,或先创建新功能"
fi
exit 1
fi
# 检查模板文件是否存在(创建新文件时需要)
if [[ ! -f "$TEMPLATE_FILE" ]]; then
log_warning "未找到模板文件:$TEMPLATE_FILE"
log_warning "创建新的智能体文件将会失败"
fi
}
#==============================================================================
# plan.md 解析函数
#==============================================================================
extract_plan_field() {
local field_pattern="$1"
local plan_file="$2"
grep "^\*\*${field_pattern}\*\*: " "$plan_file" 2>/dev/null | \
head -1 | \
sed "s|^\*\*${field_pattern}\*\*: ||" | \
sed 's/^[ \t]*//;s/[ \t]*$//' | \
grep -v "NEEDS CLARIFICATION" | \
grep -v "^N/A$" || echo ""
}
extract_plan_field_any() {
local plan_file="$1"
shift
local field_pattern=""
for field_pattern in "$@"; do
local value
value=$(extract_plan_field "$field_pattern" "$plan_file")
if [[ -n "$value" ]]; then
echo "$value"
return 0
fi
done
echo ""
}
parse_plan_data() {
local plan_file="$1"
if [[ ! -f "$plan_file" ]]; then
log_error "未找到 plan 文件:$plan_file"
return 1
fi
if [[ ! -r "$plan_file" ]]; then
log_error "plan 文件不可读:$plan_file"
return 1
fi
log_info "正在解析 plan 数据:$plan_file"
NEW_LANG=$(extract_plan_field_any "$plan_file" "语言/版本" "Language/Version")
NEW_FRAMEWORK=$(extract_plan_field_any "$plan_file" "主要依赖" "Primary Dependencies")
NEW_DB=$(extract_plan_field_any "$plan_file" "存储" "Storage")
NEW_PROJECT_TYPE=$(extract_plan_field_any "$plan_file" "工程类型" "Project Type")
# 输出解析结果
if [[ -n "$NEW_LANG" ]]; then
log_info "识别到语言:$NEW_LANG"
else
log_warning "plan 中未找到语言信息"
fi
if [[ -n "$NEW_FRAMEWORK" ]]; then
log_info "识别到主要依赖:$NEW_FRAMEWORK"
fi
if [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]]; then
log_info "识别到存储:$NEW_DB"
fi
if [[ -n "$NEW_PROJECT_TYPE" ]]; then
log_info "识别到项目类型:$NEW_PROJECT_TYPE"
fi
}
format_technology_stack() {
local lang="$1"
local framework="$2"
local parts=()
# 追加非空项
[[ -n "$lang" && "$lang" != "NEEDS CLARIFICATION" ]] && parts+=("$lang")
[[ -n "$framework" && "$framework" != "NEEDS CLARIFICATION" && "$framework" != "N/A" ]] && parts+=("$framework")
# 按合适的格式拼接
if [[ ${#parts[@]} -eq 0 ]]; then
echo ""
elif [[ ${#parts[@]} -eq 1 ]]; then
echo "${parts[0]}"
else
# 多项用 " + " 连接
local result="${parts[0]}"
for ((i=1; i<${#parts[@]}; i++)); do
result="$result + ${parts[i]}"
done
echo "$result"
fi
}
#==============================================================================
# 模板与内容生成函数
#==============================================================================
get_project_structure() {
local project_type="$1"
if [[ "$project_type" == *"web"* ]]; then
echo "backend/\\nfrontend/\\ntests/"
else
echo "src/\\ntests/"
fi
}
get_commands_for_language() {
local lang="$1"
case "$lang" in
*"Python"*)
echo "cd src && pytest && ruff check ."
;;
*"Rust"*)
echo "cargo test && cargo clippy"
;;
*"JavaScript"*|*"TypeScript"*)
echo "npm test \\&\\& npm run lint"
;;
*)
echo "# 请补充 $lang 对应的命令"
;;
esac
}
get_language_conventions() {
local lang="$1"
echo "$lang:遵循该语言的通用约定"
}
create_new_agent_file() {
local target_file="$1"
local temp_file="$2"
local project_name="$3"
local current_date="$4"
if [[ ! -f "$TEMPLATE_FILE" ]]; then
log_error "未找到模板文件:$TEMPLATE_FILE"
return 1
fi
if [[ ! -r "$TEMPLATE_FILE" ]]; then
log_error "模板文件不可读:$TEMPLATE_FILE"
return 1
fi
log_info "正在从模板创建新的智能体上下文文件..."
if ! cp "$TEMPLATE_FILE" "$temp_file"; then
log_error "复制模板文件失败"
return 1
fi
# 替换模板占位符
local project_structure
project_structure=$(get_project_structure "$NEW_PROJECT_TYPE")
local commands
commands=$(get_commands_for_language "$NEW_LANG")
local language_conventions
language_conventions=$(get_language_conventions "$NEW_LANG")
# 使用更安全的方式执行替换,并处理潜在的特殊字符
local escaped_lang=$(printf '%s\n' "$NEW_LANG" | sed 's/[\[\.*^$()+{}|]/\\&/g')
local escaped_framework=$(printf '%s\n' "$NEW_FRAMEWORK" | sed 's/[\[\.*^$()+{}|]/\\&/g')
local escaped_branch=$(printf '%s\n' "$CURRENT_BRANCH" | sed 's/[\[\.*^$()+{}|]/\\&/g')
# 按条件构建“当前技术栈”和“最近变更”的写入内容
local tech_stack
if [[ -n "$escaped_lang" && -n "$escaped_framework" ]]; then
tech_stack="- $escaped_lang + $escaped_framework ($escaped_branch)"
elif [[ -n "$escaped_lang" ]]; then
tech_stack="- $escaped_lang ($escaped_branch)"
elif [[ -n "$escaped_framework" ]]; then
tech_stack="- $escaped_framework ($escaped_branch)"
else
tech_stack="- ($escaped_branch)"
fi
local recent_change
if [[ -n "$escaped_lang" && -n "$escaped_framework" ]]; then
recent_change="- $escaped_branch:新增 $escaped_lang + $escaped_framework"
elif [[ -n "$escaped_lang" ]]; then
recent_change="- $escaped_branch:新增 $escaped_lang"
elif [[ -n "$escaped_framework" ]]; then
recent_change="- $escaped_branch:新增 $escaped_framework"
else
recent_change="- $escaped_branch:新增"
fi
local substitutions=(
"s|\[PROJECT NAME\]|$project_name|"
"s|\[项目名称\]|$project_name|"
"s|\[DATE\]|$current_date|"
"s|\[EXTRACTED FROM ALL PLAN.MD FILES\]|$tech_stack|"
"s|\[从所有 plan.md 中提取\]|$tech_stack|"
"s|\[ACTUAL STRUCTURE FROM PLANS\]|$project_structure|g"
"s|\[从计划中提取的真实结构\]|$project_structure|g"
"s|\[ONLY COMMANDS FOR ACTIVE TECHNOLOGIES\]|$commands|"
"s|\[仅保留与当前技术栈相关的命令\]|$commands|"
"s|\[LANGUAGE-SPECIFIC, ONLY FOR LANGUAGES IN USE\]|$language_conventions|"
"s|\[语言相关规范(仅保留当前使用的语言)\]|$language_conventions|"
"s|\[LAST 3 FEATURES AND WHAT THEY ADDED\]|$recent_change|"
"s|\[最近 3 个功能及其变更\]|$recent_change|"
)
for substitution in "${substitutions[@]}"; do
if ! sed -i.bak -e "$substitution" "$temp_file"; then
log_error "执行替换失败:$substitution"
rm -f "$temp_file" "$temp_file.bak"
return 1
fi
done
# 将 \n 序列转换为真实换行
newline=$(printf '\n')
sed -i.bak2 "s/\\\\n/${newline}/g" "$temp_file"
# 清理备份文件
rm -f "$temp_file.bak" "$temp_file.bak2"
return 0
}
update_existing_agent_file() {
local target_file="$1"
local current_date="$2"
log_info "正在更新已有的智能体上下文文件..."
# 使用单个临时文件实现原子更新
local temp_file
temp_file=$(mktemp) || {
log_error "创建临时文件失败"
return 1
}
# 单次扫描处理文件
local tech_stack=$(format_technology_stack "$NEW_LANG" "$NEW_FRAMEWORK")
local new_tech_entries=()
local new_change_entry=""
# 准备需要追加的技术条目
if [[ -n "$tech_stack" ]] && ! grep -q "$tech_stack" "$target_file"; then
new_tech_entries+=("- $tech_stack ($CURRENT_BRANCH)")
fi
if [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]] && [[ "$NEW_DB" != "NEEDS CLARIFICATION" ]] && ! grep -q "$NEW_DB" "$target_file"; then
new_tech_entries+=("- $NEW_DB ($CURRENT_BRANCH)")
fi
# 准备需要追加的变更条目
if [[ -n "$tech_stack" ]]; then
new_change_entry="- $CURRENT_BRANCH:新增 $tech_stack"
elif [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]] && [[ "$NEW_DB" != "NEEDS CLARIFICATION" ]]; then
new_change_entry="- $CURRENT_BRANCH:新增 $NEW_DB"
fi
# 检查文件中是否已存在对应章节
local has_active_technologies=0
local has_recent_changes=0
if grep -qE "^(## Active Technologies|## 当前技术栈)$" "$target_file" 2>/dev/null; then
has_active_technologies=1
fi
if grep -qE "^(## Recent Changes|## 最近变更)$" "$target_file" 2>/dev/null; then
has_recent_changes=1
fi
# 逐行处理文件内容
local in_tech_section=false
local in_changes_section=false
local tech_entries_added=false
local changes_entries_added=false
local existing_changes_count=0
while IFS= read -r line || [[ -n "$line" ]]; do
# 处理“当前技术栈 / Active Technologies”章节
if [[ "$line" == "## Active Technologies" ]] || [[ "$line" == "## 当前技术栈" ]]; then
echo "$line" >> "$temp_file"
in_tech_section=true
continue
elif [[ $in_tech_section == true ]] && [[ "$line" =~ ^##[[:space:]] ]]; then
# 章节结束前补充新条目
if [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
tech_entries_added=true
fi
echo "$line" >> "$temp_file"
in_tech_section=false
continue
elif [[ $in_tech_section == true ]] && [[ -z "$line" ]]; then
# 在章节内遇到空行前补充新条目
if [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
tech_entries_added=true
fi
echo "$line" >> "$temp_file"
continue
fi
# 处理“最近变更 / Recent Changes”章节
if [[ "$line" == "## Recent Changes" ]] || [[ "$line" == "## 最近变更" ]]; then
echo "$line" >> "$temp_file"
# 在标题行后立刻写入新的变更条目
if [[ -n "$new_change_entry" ]]; then
echo "$new_change_entry" >> "$temp_file"
fi
in_changes_section=true
changes_entries_added=true
continue
elif [[ $in_changes_section == true ]] && [[ "$line" =~ ^##[[:space:]] ]]; then
echo "$line" >> "$temp_file"
in_changes_section=false
continue
elif [[ $in_changes_section == true ]] && [[ "$line" == "- "* ]]; then
# 仅保留原有的前 2 条变更(加上新写入的 1 条,总计最多 3 条)
if [[ $existing_changes_count -lt 2 ]]; then
echo "$line" >> "$temp_file"
((existing_changes_count++))
fi
continue
fi
# 更新时间戳
if [[ "$line" =~ (Last[[:space:]]updated:|最近更新:).*([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]) ]]; then
echo "$line" | sed "s/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/$current_date/" >> "$temp_file"
else
echo "$line" >> "$temp_file"
fi
done < "$target_file"
# 循环结束后检查:若仍在“当前技术栈 / Active Technologies”章节且未追加,则补充新条目
if [[ $in_tech_section == true ]] && [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
tech_entries_added=true
fi
# 若章节不存在,则在文件末尾补充
if [[ $has_active_technologies -eq 0 ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
echo "" >> "$temp_file"
echo "## 当前技术栈" >> "$temp_file"
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
tech_entries_added=true
fi
if [[ $has_recent_changes -eq 0 ]] && [[ -n "$new_change_entry" ]]; then
echo "" >> "$temp_file"
echo "## 最近变更" >> "$temp_file"
echo "$new_change_entry" >> "$temp_file"
changes_entries_added=true
fi
# 原子替换写入
if ! mv "$temp_file" "$target_file"; then
log_error "更新目标文件失败"
rm -f "$temp_file"
return 1
fi
return 0
}
#==============================================================================
# 智能体文件更新入口
#==============================================================================
update_agent_file() {
local target_file="$1"
local agent_name="$2"
if [[ -z "$target_file" ]] || [[ -z "$agent_name" ]]; then
log_error "update_agent_file 需要 target_file 与 agent_name 参数"
return 1
fi
log_info "正在更新 $agent_name 上下文文件:$target_file"
local project_name
project_name=$(basename "$REPO_ROOT")
local current_date
current_date=$(date +%Y-%m-%d)
# 目录不存在则创建
local target_dir
target_dir=$(dirname "$target_file")
if [[ ! -d "$target_dir" ]]; then
if ! mkdir -p "$target_dir"; then
log_error "创建目录失败:$target_dir"
return 1
fi
fi
if [[ ! -f "$target_file" ]]; then
# 基于模板创建新文件
local temp_file
temp_file=$(mktemp) || {
log_error "创建临时文件失败"
return 1
}
if create_new_agent_file "$target_file" "$temp_file" "$project_name" "$current_date"; then
if mv "$temp_file" "$target_file"; then
log_success "已创建新的 $agent_name 上下文文件"
else
log_error "移动临时文件到目标路径失败:$target_file"
rm -f "$temp_file"
return 1
fi
else
log_error "创建新的智能体文件失败"
rm -f "$temp_file"
return 1
fi
else
# 更新已有文件
if [[ ! -r "$target_file" ]]; then
log_error "无法读取已有文件:$target_file"
return 1
fi
if [[ ! -w "$target_file" ]]; then
log_error "无法写入已有文件:$target_file"
return 1
fi
if update_existing_agent_file "$target_file" "$current_date"; then
log_success "已更新 $agent_name 上下文文件"
else
log_error "更新已有智能体文件失败"
return 1
fi
fi
return 0
}
#==============================================================================
# 智能体选择与处理
#==============================================================================
update_specific_agent() {
local agent_type="$1"
case "$agent_type" in
claude)
update_agent_file "$CLAUDE_FILE" "Claude Code"
;;
gemini)
update_agent_file "$GEMINI_FILE" "Gemini CLI"
;;
copilot)
update_agent_file "$COPILOT_FILE" "GitHub Copilot"
;;
cursor-agent)
update_agent_file "$CURSOR_FILE" "Cursor IDE"
;;
qwen)
update_agent_file "$QWEN_FILE" "Qwen Code"
;;
opencode)
update_agent_file "$AGENTS_FILE" "opencode"
;;
codex)
update_agent_file "$AGENTS_FILE" "Codex CLI"
;;
windsurf)
update_agent_file "$WINDSURF_FILE" "Windsurf"
;;
kilocode)
update_agent_file "$KILOCODE_FILE" "Kilo Code"
;;
auggie)
update_agent_file "$AUGGIE_FILE" "Auggie CLI"
;;
roo)
update_agent_file "$ROO_FILE" "Roo Code"
;;
codebuddy)
update_agent_file "$CODEBUDDY_FILE" "CodeBuddy CLI"
;;
qoder)
update_agent_file "$QODER_FILE" "Qoder CLI"
;;
amp)
update_agent_file "$AMP_FILE" "Amp"
;;
shai)
update_agent_file "$SHAI_FILE" "SHAI"
;;
q)
update_agent_file "$Q_FILE" "Amazon Q Developer CLI"
;;
bob)
update_agent_file "$BOB_FILE" "IBM Bob"
;;
*)
log_error "未知的 agent type:'$agent_type'"
log_error "可选值:claude|gemini|copilot|cursor-agent|qwen|opencode|codex|windsurf|kilocode|auggie|roo|amp|shai|q|bob|qoder"
exit 1
;;
esac
}
update_all_existing_agents() {
local found_agent=false
# 检查各类智能体文件:存在则更新
if [[ -f "$CLAUDE_FILE" ]]; then
update_agent_file "$CLAUDE_FILE" "Claude Code"
found_agent=true
fi
if [[ -f "$GEMINI_FILE" ]]; then
update_agent_file "$GEMINI_FILE" "Gemini CLI"
found_agent=true
fi
if [[ -f "$COPILOT_FILE" ]]; then
update_agent_file "$COPILOT_FILE" "GitHub Copilot"
found_agent=true
fi
if [[ -f "$CURSOR_FILE" ]]; then
update_agent_file "$CURSOR_FILE" "Cursor IDE"
found_agent=true
fi
if [[ -f "$QWEN_FILE" ]]; then
update_agent_file "$QWEN_FILE" "Qwen Code"
found_agent=true
fi
if [[ -f "$AGENTS_FILE" ]]; then
update_agent_file "$AGENTS_FILE" "Codex/opencode"
found_agent=true
fi
if [[ -f "$WINDSURF_FILE" ]]; then
update_agent_file "$WINDSURF_FILE" "Windsurf"
found_agent=true
fi
if [[ -f "$KILOCODE_FILE" ]]; then
update_agent_file "$KILOCODE_FILE" "Kilo Code"
found_agent=true
fi
if [[ -f "$AUGGIE_FILE" ]]; then
update_agent_file "$AUGGIE_FILE" "Auggie CLI"
found_agent=true
fi
if [[ -f "$ROO_FILE" ]]; then
update_agent_file "$ROO_FILE" "Roo Code"
found_agent=true
fi
if [[ -f "$CODEBUDDY_FILE" ]]; then
update_agent_file "$CODEBUDDY_FILE" "CodeBuddy CLI"
found_agent=true
fi
if [[ -f "$SHAI_FILE" ]]; then
update_agent_file "$SHAI_FILE" "SHAI"
found_agent=true
fi
if [[ -f "$QODER_FILE" ]]; then
update_agent_file "$QODER_FILE" "Qoder CLI"
found_agent=true
fi
if [[ -f "$Q_FILE" ]]; then
update_agent_file "$Q_FILE" "Amazon Q Developer CLI"
found_agent=true
fi
if [[ -f "$BOB_FILE" ]]; then
update_agent_file "$BOB_FILE" "IBM Bob"
found_agent=true
fi
# 若未发现任何智能体文件,则创建默认的 Claude 文件
if [[ "$found_agent" == false ]]; then
log_info "未发现已有智能体文件,正在创建默认 Claude 文件..."
update_agent_file "$CLAUDE_FILE" "Claude Code"
fi
}
print_summary() {
echo
log_info "变更摘要:"
if [[ -n "$NEW_LANG" ]]; then
echo " - 新增语言:$NEW_LANG"
fi
if [[ -n "$NEW_FRAMEWORK" ]]; then
echo " - 新增主要依赖:$NEW_FRAMEWORK"
fi
if [[ -n "$NEW_DB" ]] && [[ "$NEW_DB" != "N/A" ]]; then
echo " - 新增存储:$NEW_DB"
fi
echo
log_info "用法:$0 [claude|gemini|copilot|cursor-agent|qwen|opencode|codex|windsurf|kilocode|auggie|codebuddy|shai|q|bob|qoder]"
}
#==============================================================================
# 主执行流程
#==============================================================================
main() {
# 执行前校验环境
validate_environment
log_info "=== 正在更新功能 $CURRENT_BRANCH 的智能体上下文文件 ==="
# 解析 plan 文件以提取项目信息
if ! parse_plan_data "$NEW_PLAN"; then
log_error "解析 plan 数据失败"
exit 1
fi
# 根据传入的 agent type 决定更新范围
local success=true
if [[ -z "$AGENT_TYPE" ]]; then
# 未指定智能体:更新所有已存在的智能体文件
log_info "未指定智能体,正在更新所有已存在的智能体文件..."
if ! update_all_existing_agents; then
success=false
fi
else
# 指定了智能体:仅更新该智能体
log_info "正在更新指定智能体:$AGENT_TYPE"
if ! update_specific_agent "$AGENT_TYPE"; then
success=false
fi
fi
# 打印摘要
print_summary
if [[ "$success" == true ]]; then
log_success "智能体上下文更新完成"
exit 0
else
log_error "智能体上下文更新完成,但存在错误"
exit 1
fi
}
# Execute main function if script is run directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi