@andyqiu/codeforge 0.5.29 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/install.ps1 DELETED
@@ -1,559 +0,0 @@
1
- <#
2
- .SYNOPSIS
3
- CodeForge installer (PowerShell) — 零侵入安装到 opencode(v2 单 bundle 架构)
4
-
5
- .DESCRIPTION
6
- CodeForge v0.1+ 改为 OMO 风格的单 plugin bundle 架构:
7
- 1. 17 个能力被 bun build 编译成 dist/index.js 一个 ESM bundle
8
- 2. 在 opencode.json 里只占 1 行 plugin entry
9
- 3. 永久避免 opencode 1.14+ 早期版本的 zod 跨实例 bug(issue #12336/#21155)
10
-
11
- 本脚本职责:
12
- 1. 检测 opencode CLI 与 KH MCP
13
- 2. 把 dist/index.js 复制到 ~/.config/opencode/codeforge/index.js
14
- 3. 在 ~/.config/opencode/opencode.json 的 "plugin" 数组追加 file:// URL
15
- 4. 把 agents/commands 用 file-by-file copy 注入(带白名单)
16
- 5. 把 workflows/context-templates/review-profiles 拷贝过去
17
- 5b. 把 skills/ 目录拷贝到 $TargetRoot/skills/(与 install.sh 对称)
18
- 6. 智能合并 AGENTS.md:
19
- - 项目模式 + 已有 AGENTS.md → 替换 <!-- knowledge-hub:start -->...<!-- knowledge-hub:end --> 块(带 .bak 备份)
20
- - 项目模式 + 无 AGENTS.md → 生成含 marker 块的短版骨架
21
- - 全局模式 → 跳过(context-templates 已经放进 ~/.config/opencode/context-templates/ 给所有项目复用)
22
- 7. -Global 时生成 ~/.config/codeforge/kh.json 模板(不含 token,硬约束 #2)
23
- 8. 输出验证清单
24
-
25
- .PARAMETER Global
26
- 装到全局 (~/.config/opencode/),默认装到当前项目 (.opencode/)
27
-
28
- .PARAMETER Uninstall
29
- 卸载 CodeForge 注入物(不动 opencode 自身和 AGENTS.md,会清掉 opencode.json 里的 plugin entry)
30
-
31
- .PARAMETER DryRun
32
- 仅打印将要执行的操作,不真正执行
33
-
34
- .PARAMETER SkipBuild
35
- 跳过 npm run build。默认每次安装前会先 build 确保 dist 是最新的,加 -SkipBuild 跳过
36
-
37
- .EXAMPLE
38
- .\install.ps1
39
- .\install.ps1 -Global
40
- .\install.ps1 -Uninstall
41
- .\install.ps1 -DryRun
42
- .\install.ps1 -SkipBuild
43
- #>
44
- [CmdletBinding()]
45
- param(
46
- [switch]$Global,
47
- [switch]$Uninstall,
48
- [switch]$DryRun,
49
- [switch]$SkipBuild
50
- )
51
-
52
- $ErrorActionPreference = 'Stop'
53
-
54
- # ────────────── 工具函数 ──────────────
55
- function Write-Section([string]$Text) {
56
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray
57
- Write-Host "[codeforge] $Text" -ForegroundColor Cyan
58
- }
59
- function Write-Ok([string]$Text) { Write-Host "$([char]0x2713) $Text" -ForegroundColor Green }
60
- function Write-Warn2([string]$Text) { Write-Host "$([char]0x26A0) $Text" -ForegroundColor Yellow }
61
- function Write-Err2([string]$Text) { Write-Host "$([char]0x2717) $Text" -ForegroundColor Red }
62
-
63
- function Invoke-Step([string]$Description, [scriptblock]$Action) {
64
- if ($DryRun) {
65
- Write-Host " [dry-run] $Description" -ForegroundColor Blue
66
- } else {
67
- & $Action
68
- }
69
- }
70
-
71
- # ────────────── 路径解析 ──────────────
72
- $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
73
- $SourceRoot = $ScriptDir
74
-
75
- if ($Global) {
76
- $XdgConfig = if ($env:XDG_CONFIG_HOME) { $env:XDG_CONFIG_HOME } else { Join-Path $HOME '.config' }
77
- $TargetRoot = Join-Path $XdgConfig 'opencode'
78
- $Mode = 'global'
79
- } else {
80
- $TargetRoot = Join-Path (Get-Location).Path '.opencode'
81
- $Mode = 'project'
82
- }
83
-
84
- # CodeForge 自身的全局配置目录(与 opencode 配置目录同级)
85
- $XdgConfigBase = if ($env:XDG_CONFIG_HOME) { $env:XDG_CONFIG_HOME } else { Join-Path $HOME '.config' }
86
- $CodeforgeCfgDir = Join-Path $XdgConfigBase 'codeforge'
87
-
88
- # v0.1 之前 install.ps1 装的目录(卸载时一并清掉)
89
- $LegacyDirs = @('agent', 'command', 'tool', 'tools', 'plugin', 'plugins', 'lib')
90
- # v0.1+ 才有的目录(review-profiles 由 ADR:reviewer-multi-profile 引入)
91
- # agent-templates 由 ADR:agent-templates-move-to-root 移到根目录(supersedes agent-templates-distribution)
92
- $ManagedDirs = @('codeforge', 'agents', 'commands', 'workflows', 'context-templates', 'review-profiles', 'agent-templates')
93
-
94
- # v0.1+ 文件分发计划
95
- $BundleSrcRel = 'dist/index.js'
96
- $BundleDstRel = 'codeforge/index.js' # 相对 $TargetRoot
97
-
98
- # B3 KH 行为规范模板(智能合并的输入)
99
- $KhTemplateRel = 'context-templates/kh-instructions.md'
100
-
101
- # .md file-by-file copy(白名单 *.md,排除 README/_*/.bak)
102
- $MdCopyMap = @(
103
- @{ Src='agents'; Dst='agents' },
104
- @{ Src='commands'; Dst='commands' }
105
- )
106
- # 普通整目录 copy(review-profiles 由 ADR:reviewer-multi-profile 引入)
107
- # ADR:agent-templates-move-to-root — agent-templates 随 install 分发到全局目录
108
- $CopyMap = @(
109
- @{ Src='workflows'; Dst='workflows' },
110
- @{ Src='context-templates'; Dst='context-templates' },
111
- @{ Src='review-profiles'; Dst='review-profiles' },
112
- @{ Src='agent-templates'; Dst='agent-templates' }
113
- )
114
-
115
- # ────────────── opencode.json 管理 ──────────────
116
- function Get-OpencodeConfigPath {
117
- Join-Path $TargetRoot 'opencode.json'
118
- }
119
-
120
- function Get-PluginUri {
121
- # opencode 接受 file:// URL 也接受裸绝对路径,统一用 file:// 更明确
122
- $abs = Join-Path $TargetRoot $BundleDstRel
123
- $abs = $abs -replace '\\', '/'
124
- if ($abs -notmatch '^[A-Za-z]:/') {
125
- return "file://$abs"
126
- }
127
- return "file:///$abs"
128
- }
129
-
130
- function Add-PluginEntry {
131
- $cfgPath = Get-OpencodeConfigPath
132
- $uri = Get-PluginUri
133
-
134
- if (-not (Test-Path -LiteralPath $TargetRoot)) {
135
- Invoke-Step "mkdir $TargetRoot" {
136
- New-Item -ItemType Directory -Path $TargetRoot -Force | Out-Null
137
- }
138
- }
139
-
140
- if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
141
- Write-Err2 "需要 node 才能改写 opencode.json,请安装 Node.js >= 20"
142
- exit 1
143
- }
144
-
145
- Invoke-Step "write $cfgPath plugin entry: $uri" {
146
- $env:CODEFORGE_CFG = $cfgPath
147
- $env:CODEFORGE_URI = $uri
148
- $nodeScript = @'
149
- const fs = require("node:fs");
150
- const path = process.env.CODEFORGE_CFG;
151
- const uri = process.env.CODEFORGE_URI;
152
- let cfg = {};
153
- if (fs.existsSync(path)) {
154
- try { cfg = JSON.parse(fs.readFileSync(path, "utf8")); }
155
- catch { fs.copyFileSync(path, path + ".bak." + Date.now()); cfg = {}; }
156
- }
157
- if (!cfg.$schema) cfg.$schema = "https://opencode.ai/config.json";
158
- if (!Array.isArray(cfg.plugin)) cfg.plugin = [];
159
- const cleaned = [];
160
- for (const e of cfg.plugin) {
161
- const s = String(e);
162
- if (/\/codeforge\/index\.js$/.test(s)) continue;
163
- if (/\/plugins\/[^/]+\.ts$/.test(s) && /opencode/.test(s)) continue;
164
- if (/\/\.opencode\/plugins\//.test(s)) continue;
165
- cleaned.push(e);
166
- }
167
- cleaned.push(uri);
168
- cfg.plugin = cleaned;
169
- fs.writeFileSync(path, JSON.stringify(cfg, null, 2) + "\n", "utf8");
170
- '@
171
- & node -e $nodeScript
172
- Remove-Item Env:\CODEFORGE_CFG -ErrorAction SilentlyContinue
173
- Remove-Item Env:\CODEFORGE_URI -ErrorAction SilentlyContinue
174
- }
175
- Write-Ok "opencode.json 已写入 plugin entry: $uri"
176
- }
177
-
178
- function Remove-PluginEntry {
179
- $cfgPath = Get-OpencodeConfigPath
180
- if (-not (Test-Path -LiteralPath $cfgPath)) { return }
181
- if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
182
- Write-Warn2 "未找到 node,跳过 opencode.json plugin entry 清理"
183
- return
184
- }
185
-
186
- Invoke-Step "rewrite $cfgPath without codeforge plugin entry" {
187
- $env:CODEFORGE_CFG = $cfgPath
188
- $nodeScript = @'
189
- const fs = require("node:fs");
190
- const path = process.env.CODEFORGE_CFG;
191
- let cfg;
192
- try { cfg = JSON.parse(fs.readFileSync(path, "utf8")); }
193
- catch { return; }
194
- if (!Array.isArray(cfg.plugin)) return;
195
- cfg.plugin = cfg.plugin.filter(e => {
196
- const s = String(e);
197
- if (/\/codeforge\/index\.js$/.test(s)) return false;
198
- if (/\/plugins\/[^/]+\.ts$/.test(s)) return false;
199
- if (/\/\.opencode\/plugins\//.test(s)) return false;
200
- return true;
201
- });
202
- fs.writeFileSync(path, JSON.stringify(cfg, null, 2) + "\n", "utf8");
203
- '@
204
- & node -e $nodeScript
205
- Remove-Item Env:\CODEFORGE_CFG -ErrorAction SilentlyContinue
206
- }
207
- Write-Ok "opencode.json 已移除 codeforge plugin entry"
208
- }
209
-
210
- # ────────────── KH 全局模板(仅 -Global) ──────────────
211
- #
212
- # 硬约束 #2:API key 绝不能落盘,模板里**不写** token / apiKey 字段。
213
- # 用户必须通过环境变量 KNOWLEDGE_API_KEY 提供。
214
- # 已存在则跳过,避免覆盖用户自定义。
215
- function Write-KhTemplate {
216
- $khFile = Join-Path $CodeforgeCfgDir 'kh.json'
217
- if (-not (Test-Path -LiteralPath $CodeforgeCfgDir)) {
218
- Invoke-Step "mkdir $CodeforgeCfgDir" {
219
- New-Item -ItemType Directory -Path $CodeforgeCfgDir -Force | Out-Null
220
- }
221
- }
222
- if (Test-Path -LiteralPath $khFile) {
223
- Write-Ok "已存在 $khFile,跳过覆盖"
224
- return
225
- }
226
- Invoke-Step "write $khFile (no token)" {
227
- $body = @"
228
- {
229
- "url": "http://10.5.60.26:8900/mcp",
230
- "timeoutMs": 5000,
231
- "maxRetries": 1
232
- }
233
- "@
234
- # 用 .NET WriteAllText 强制无 BOM 的 UTF8(PowerShell 5.1 默认 BOM 会被 KhConfig 容错,但保持干净)
235
- [System.IO.File]::WriteAllText($khFile, $body, (New-Object System.Text.UTF8Encoding $false))
236
- }
237
- Write-Ok "已生成 $khFile(API key 请通过环境变量 KNOWLEDGE_API_KEY 提供,禁止写入文件)"
238
- }
239
-
240
- # ────────────── AGENTS.md 智能合并(B3 新增) ──────────────
241
- #
242
- # 实现委托给 scripts/merge-agents-md.mjs CLI(C.4 单一入口),
243
- # 算法源自 lib/agents-merge.ts,sh/ps1 共用同一份 Node 实现,
244
- # 避免 install 脚本各自维护算法副本造成漂移。
245
- function Merge-ProjectAgentsMd {
246
- param(
247
- [Parameter(Mandatory=$true)] [string] $AgentsTarget,
248
- [Parameter(Mandatory=$true)] [string] $TemplatePath
249
- )
250
- $cliPath = Join-Path $SourceRoot 'scripts/merge-agents-md.mjs'
251
- if (-not (Test-Path -LiteralPath $TemplatePath)) {
252
- Write-Warn2 "模板不存在,跳过 AGENTS.md 合并: $TemplatePath"
253
- return
254
- }
255
- if (-not (Test-Path -LiteralPath $cliPath)) {
256
- Write-Warn2 "merge CLI 不存在,跳过 AGENTS.md 智能合并: $cliPath"
257
- return
258
- }
259
- if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
260
- Write-Warn2 "未找到 node,跳过 AGENTS.md 智能合并(请安装 Node.js >= 20)"
261
- return
262
- }
263
- $cliArgs = @('--target', $AgentsTarget, '--template', $TemplatePath)
264
- if ($DryRun) { $cliArgs += '--dry-run' }
265
- try {
266
- & node $cliPath @cliArgs
267
- } catch {
268
- Write-Warn2 "AGENTS.md 合并失败(不阻塞 install 主流程): $_"
269
- }
270
- }
271
-
272
- # ────────────── 卸载 ──────────────
273
- function Invoke-Uninstall {
274
- Write-Section "卸载 CodeForge from: $TargetRoot"
275
- Remove-PluginEntry
276
- $candidates = $LegacyDirs + $ManagedDirs | Select-Object -Unique
277
- foreach ($name in $candidates) {
278
- $path = Join-Path $TargetRoot $name
279
- if (Test-Path -LiteralPath $path) {
280
- Invoke-Step "remove $path" {
281
- # Junction 不能用 Remove-Item 直接删(会删源),需要先 rmdir
282
- $item = Get-Item -LiteralPath $path -Force
283
- if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
284
- [IO.Directory]::Delete($path, $false)
285
- } else {
286
- Remove-Item -LiteralPath $path -Recurse -Force
287
- }
288
- }
289
- Write-Ok "已删除 $path"
290
- }
291
- }
292
- # 细粒度删除 skills:只删 CodeForge 自己的 skill,不删用户自装的
293
- $ownedSkills = @('ambiguity-gate', 'devils-advocate', 'ears-zh', 'example-mapping', 'success-criteria', 'weighted-dimensions')
294
- foreach ($skillName in $ownedSkills) {
295
- $skillPath = Join-Path $TargetRoot "skills\$skillName"
296
- if (Test-Path -LiteralPath $skillPath) {
297
- Invoke-Step "remove skill $skillName" {
298
- Remove-Item -LiteralPath $skillPath -Recurse -Force
299
- }
300
- Write-Ok "已删除 skill: $skillPath"
301
- }
302
- }
303
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray
304
- Write-Ok "卸载完成(opencode 自身、AGENTS.md、~/.config/codeforge/kh.json 不会被动)"
305
- }
306
-
307
- # ────────────── 环境检测 ──────────────
308
- function Test-Opencode {
309
- $cmd = Get-Command opencode -ErrorAction SilentlyContinue
310
- if ($cmd) {
311
- try {
312
- $v = & opencode --version 2>$null
313
- Write-Ok "检测到 opencode: $v"
314
- } catch {
315
- Write-Ok "检测到 opencode (版本未知)"
316
- }
317
- } else {
318
- Write-Warn2 "未检测到 opencode CLI"
319
- Write-Warn2 " 安装方式:https://github.com/sst/opencode#installation"
320
- }
321
- }
322
-
323
- function Test-KhMcp {
324
- $xdg = if ($env:XDG_CONFIG_HOME) { $env:XDG_CONFIG_HOME } else { Join-Path $HOME '.config' }
325
- $cfg = Join-Path (Join-Path $xdg 'opencode') 'opencode.json'
326
- if ((Test-Path -LiteralPath $cfg) -and ((Get-Content -LiteralPath $cfg -Raw -Encoding UTF8) -match 'knowledge-hub|code-forge-knowledge-hub')) {
327
- Write-Ok "检测到 Knowledge Hub MCP 已注册"
328
- } else {
329
- Write-Warn2 "未在 $cfg 中找到 Knowledge Hub MCP"
330
- Write-Warn2 " 配置示例见 docs/PRD.md §6.2"
331
- }
332
- }
333
-
334
- # ────────────── 主流程 ──────────────
335
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray
336
- Write-Host "[codeforge] CodeForge installer (mode=$Mode, uninstall=$Uninstall, dry-run=$DryRun)" -ForegroundColor Cyan
337
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray
338
- Write-Host "Source : $SourceRoot"
339
- Write-Host "Target : $TargetRoot"
340
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray
341
-
342
- if ($Uninstall) {
343
- Invoke-Uninstall
344
- exit 0
345
- }
346
-
347
- # Step 1/7: 环境检测
348
- Write-Section 'Step 1/7: 环境检测'
349
- Test-Opencode
350
- Test-KhMcp
351
-
352
- # Step 2/7: build dist bundle
353
- Write-Section 'Step 2/7: 构建 dist/index.js 单 bundle'
354
- $bundleSrc = Join-Path $SourceRoot $BundleSrcRel
355
- if ($SkipBuild) {
356
- Write-Warn2 '已跳过 build(-SkipBuild),使用现有 dist/index.js'
357
- } else {
358
- Invoke-Step "npm run build" {
359
- Push-Location $SourceRoot
360
- try {
361
- $out = npm run build 2>&1 | Out-String
362
- if ($LASTEXITCODE -ne 0) {
363
- Write-Err2 'npm run build 失败'
364
- Write-Host $out
365
- exit 1
366
- }
367
- } finally {
368
- Pop-Location
369
- }
370
- }
371
- }
372
- if (-not (Test-Path -LiteralPath $bundleSrc)) {
373
- if ($SkipBuild) {
374
- Write-Err2 "找不到 $bundleSrc(npm 包可能损坏)"
375
- Write-Err2 " 请尝试重装:npx @andyqiu/codeforge install [--global]"
376
- } else {
377
- Write-Err2 "找不到 $bundleSrc,请先成功执行 npm run build"
378
- }
379
- exit 1
380
- }
381
- $bundleSize = (Get-Item -LiteralPath $bundleSrc).Length
382
- Write-Ok "bundle 已就绪: $bundleSrc ($([math]::Round($bundleSize/1024,1)) KB)"
383
-
384
- # Step 3/7: 准备目标目录
385
- Write-Section 'Step 3/7: 准备目标目录'
386
- if (-not (Test-Path -LiteralPath $TargetRoot)) {
387
- Invoke-Step "mkdir $TargetRoot" {
388
- New-Item -ItemType Directory -Path $TargetRoot -Force | Out-Null
389
- }
390
- }
391
- # 清理 legacy 单数目录 + 老的 17 个 plugin / lib / tools junction
392
- foreach ($legacy in $LegacyDirs) {
393
- $dst = Join-Path $TargetRoot $legacy
394
- if (Test-Path -LiteralPath $dst) {
395
- Invoke-Step "cleanup legacy $dst" {
396
- $item = Get-Item -LiteralPath $dst -Force
397
- if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
398
- [IO.Directory]::Delete($dst, $false)
399
- } else {
400
- Remove-Item -LiteralPath $dst -Recurse -Force
401
- }
402
- }
403
- Write-Warn2 "已清理 legacy 目录: $dst"
404
- }
405
- }
406
-
407
- # Step 4/7: 装 bundle 到 codeforge/index.js
408
- Write-Section 'Step 4/7: 装入 dist/index.js bundle'
409
- $bundleDst = Join-Path $TargetRoot $BundleDstRel
410
- $bundleDstDir = Split-Path -Parent $bundleDst
411
- if (-not (Test-Path -LiteralPath $bundleDstDir)) {
412
- Invoke-Step "mkdir $bundleDstDir" {
413
- New-Item -ItemType Directory -Path $bundleDstDir -Force | Out-Null
414
- }
415
- }
416
- Invoke-Step "copy $BundleSrcRel -> $BundleDstRel" {
417
- Copy-Item -LiteralPath $bundleSrc -Destination $bundleDst -Force
418
- }
419
- Write-Ok "bundle -> $bundleDst"
420
- Add-PluginEntry
421
-
422
- # 写 VERSION marker 文件(用户 cat 一行查版本,不依赖 grep bundle)
423
- $cfVersion = "unknown"
424
- try {
425
- $pkgPath = Join-Path $SourceRoot "package.json"
426
- if (Test-Path -LiteralPath $pkgPath) {
427
- $pkg = Get-Content -LiteralPath $pkgPath -Raw | ConvertFrom-Json
428
- if ($pkg.version) { $cfVersion = $pkg.version }
429
- }
430
- } catch { $cfVersion = "unknown" }
431
- $versionFile = Join-Path $TargetRoot "codeforge/VERSION"
432
- if (-not $DryRun) {
433
- Set-Content -LiteralPath $versionFile -Value $cfVersion -NoNewline:$false -Encoding utf8
434
- }
435
- Write-Ok "VERSION -> $versionFile ($cfVersion)"
436
-
437
- # Step 5/7: 装 agents / commands / workflows / context-templates / review-profiles
438
- Write-Section 'Step 5/7: 装 agents / commands / workflows / context-templates / review-profiles'
439
- foreach ($entry in $MdCopyMap) {
440
- $src = Join-Path $SourceRoot $entry.Src
441
- $dst = Join-Path $TargetRoot $entry.Dst
442
- if (-not (Test-Path -LiteralPath $src)) {
443
- Write-Warn2 "源目录不存在,跳过: $src"
444
- continue
445
- }
446
- if (Test-Path -LiteralPath $dst) {
447
- Invoke-Step "cleanup old $dst" {
448
- $item = Get-Item -LiteralPath $dst -Force
449
- if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
450
- [IO.Directory]::Delete($dst, $false)
451
- } else {
452
- Remove-Item -LiteralPath $dst -Recurse -Force
453
- }
454
- }
455
- }
456
- Invoke-Step "mkdir $dst" {
457
- New-Item -ItemType Directory -Path $dst -Force | Out-Null
458
- }
459
- $files = Get-ChildItem -LiteralPath $src -Filter '*.md' -File | Where-Object {
460
- $_.Name -ne 'README.md' -and
461
- $_.Name -notmatch '^_' -and
462
- $_.Name -notmatch '\.bak$' -and
463
- $_.Name -notmatch '^\.'
464
- }
465
- foreach ($f in $files) {
466
- Invoke-Step "copy $($entry.Src)/$($f.Name)" {
467
- Copy-Item -LiteralPath $f.FullName -Destination (Join-Path $dst $f.Name) -Force
468
- }
469
- }
470
- Write-Ok "$($entry.Src)/ -> $dst ($($files.Count) 个 .md)"
471
- }
472
- foreach ($entry in $CopyMap) {
473
- $src = Join-Path $SourceRoot $entry.Src
474
- $dst = Join-Path $TargetRoot $entry.Dst
475
- if (-not (Test-Path -LiteralPath $src)) {
476
- Write-Warn2 "源目录不存在,跳过: $src"
477
- continue
478
- }
479
- Invoke-Step "copy $($entry.Src)/ -> $dst" {
480
- if (-not (Test-Path -LiteralPath $dst)) {
481
- New-Item -ItemType Directory -Path $dst -Force | Out-Null
482
- }
483
- Copy-Item -Path (Join-Path $src '*') -Destination $dst -Recurse -Force
484
- }
485
- Write-Ok "$($entry.Src)/ -> $dst (整目录拷贝)"
486
- }
487
-
488
- # Step 5b/7: 装 skills/(opencode skill 目录)
489
- Write-Section 'Step 5b/7: 装 skills/'
490
- $skillsSrc = Join-Path $SourceRoot 'skills'
491
- $skillsDst = Join-Path $TargetRoot 'skills'
492
- if (Test-Path -LiteralPath $skillsSrc) {
493
- if (-not (Test-Path -LiteralPath $skillsDst)) {
494
- New-Item -ItemType Directory -Path $skillsDst -Force | Out-Null
495
- }
496
- $skillDirs = Get-ChildItem -LiteralPath $skillsSrc -Directory
497
- $skillCount = 0
498
- foreach ($skillDir in $skillDirs) {
499
- $dstSkill = Join-Path $skillsDst $skillDir.Name
500
- if (Test-Path -LiteralPath $dstSkill) {
501
- Invoke-Step "remove old skill $($skillDir.Name)" {
502
- Remove-Item -LiteralPath $dstSkill -Recurse -Force
503
- }
504
- }
505
- Invoke-Step "copy skill $($skillDir.Name)" {
506
- Copy-Item -LiteralPath $skillDir.FullName -Destination $dstSkill -Recurse -Force
507
- }
508
- $skillCount++
509
- }
510
- Write-Ok "skills/ -> $skillsDst ($skillCount 个 skill)"
511
- } else {
512
- Write-Warn2 "skills/ 目录不存在,跳过(发布包未含 skills)"
513
- }
514
-
515
- # Step 6/7: AGENTS.md 智能合并(仅项目模式)
516
- Write-Section 'Step 6/7: AGENTS.md 智能合并'
517
- if ($Mode -eq 'project') {
518
- $agentsTarget = Join-Path (Get-Location).Path 'AGENTS.md'
519
- $templatePath = Join-Path $SourceRoot $KhTemplateRel
520
- Merge-ProjectAgentsMd -AgentsTarget $agentsTarget -TemplatePath $templatePath
521
- } else {
522
- Write-Ok "全局模式:跳过项目 AGENTS.md 合并(context-templates 已装到 $TargetRoot\context-templates)"
523
- }
524
-
525
- # Step 7/7: KH 全局配置模板(仅 -Global)
526
- Write-Section 'Step 7/7: KH 全局配置模板'
527
- if ($Global) {
528
- Write-KhTemplate
529
- } else {
530
- Write-Ok "项目级安装,跳过 KH 全局模板(要装请加 -Global)"
531
- }
532
-
533
- # 验证清单
534
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray
535
- Write-Ok "CodeForge v$cfVersion 安装完成"
536
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray
537
- Write-Host @"
538
- 验证清单:
539
- 1. 列出注入的扩展点:
540
- PS> Get-ChildItem '$TargetRoot' -Force
541
- 2. 看 opencode.json 里 plugin entry:
542
- PS> Get-Content '$TargetRoot\opencode.json'
543
- 3. 让 opencode 校验配置:
544
- PS> opencode debug config
545
- -> 期望看到 1 个 codeforge plugin entry(不是 17 个)
546
- 4. 跑一次最小 dogfood,看 plugin import + activation 日志:
547
- PS> opencode run "hello"
548
- PS> Get-Content `$HOME\.cache\codeforge\plugins.log -Tail 50
549
- 5. KH 配置(仅 -Global 安装时生成):
550
- PS> Get-Content '$CodeforgeCfgDir\kh.json'
551
- PS> `$env:KNOWLEDGE_API_KEY = '<your-token>' # ← API key 必须走环境变量
552
- 6. AGENTS.md 智能合并(仅项目级):
553
- - 已有 AGENTS.md → KH 块被替换成模板内容,原文件备份成 *.bak.<timestamp>
554
- - 模板源:$SourceRoot\$KhTemplateRel(修改后请重新 install)
555
-
556
- 卸载:
557
- PS> .\install.ps1 -Uninstall$(if ($Global) { ' -Global' })
558
- "@
559
- Write-Host '────────────────────────────────────────────────' -ForegroundColor DarkGray