Repository navigation
Expand file tree
/
Copy pathinstall-dev.ps1
More file actions
361 lines (331 loc) · 14.5 KB
/
Copy pathinstall-dev.ps1
File metadata and controls
361 lines (331 loc) · 14.5 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
[CmdletBinding()]
param(
[string]$OpenCodeConfigDir = $env:OPENCODE_CONFIG_DIR,
[string]$PanelA,
[string]$PanelB,
[string]$Decider,
[switch]$NonInteractive,
[switch]$SkipChecks,
[switch]$SkipRuntimeCheck,
[switch]$Remove
)
$ErrorActionPreference = "Stop"
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
throw "node is required for OpenUltraCode install."
}
if (-not $OpenCodeConfigDir) {
$OpenCodeConfigDir = Join-Path $HOME ".config\opencode"
}
$repoRoot = $PSScriptRoot
if (-not $repoRoot) {
$repoRoot = (Get-Location).Path
}
$InstallerLib = Join-Path $repoRoot "scripts\installer-lib.mjs"
$FusionAgents = @(
@{ Name = "ultracode-fusion-panel-a.md"; Prompt = "Choose fusion panel A model" },
@{ Name = "ultracode-fusion-panel-b.md"; Prompt = "Choose fusion panel B model" },
@{ Name = "ultracode-fusion-arbiter.md"; Prompt = "Choose fusion arbiter model" }
)
# Files OpenUltraCode ships; tests/installer-assets.test.ts keeps these lists in sync with .opencode.
$ShippedCommands = @(
"ultracode-debug.md",
"ultracode-fusion-models.md",
"ultracode-fusion.md",
"ultracode-research.md",
"ultracode-spec-audit.md",
"ultracode-verify.md",
"ultracode.md",
"ultrathink-fusion.md",
"ultrathink.md"
)
$ShippedAgents = @(
"open-ultracode-adversary.md",
"open-ultracode-implementer.md",
"open-ultracode-planner.md",
"open-ultracode-reconciler.md",
"open-ultracode-researcher.md",
"open-ultracode-verifier.md",
"open-ultracode.md",
"ultracode-fusion-arbiter.md",
"ultracode-fusion-panel-a.md",
"ultracode-fusion-panel-b.md"
)
$ModelIdPattern = '^[A-Za-z0-9][A-Za-z0-9._-]*/[^\s#]+(#max)?$'
# Native commands do not honor $ErrorActionPreference (and Windows PowerShell 5.1 turns
# their stderr into terminating errors when output is redirected), so every native call
# goes through this helper: stderr is passed through and a non-zero exit code throws.
function Invoke-Native {
param([Parameter(Mandatory)][string]$What, [Parameter(Mandatory)][scriptblock]$Command)
$ErrorActionPreference = "Continue"
& $Command
if ($LASTEXITCODE -ne 0) { throw "$What failed with exit code $LASTEXITCODE" }
}
function Invoke-InstallerLib {
param([Parameter(Mandatory)][string]$What, [Parameter(ValueFromRemainingArguments)][string[]]$Arguments)
Invoke-Native $What { node $InstallerLib @Arguments }
}
function Get-AgentModel([string]$AgentFile) {
$model = Invoke-InstallerLib "Reading $AgentFile" get-agent-model $AgentFile
if ($model) { return [string]$model }
return $null
}
function Assert-ModelId([string]$Model, [string]$Name) {
if ($Model -notmatch $ModelIdPattern) { throw "$Name '$Model' must be provider/model or provider/model#max." }
}
# Your providers' models first, then opencode's hosted catalog; see list-models in
# scripts/installer-lib.mjs (it retries `opencode models` and adds models from your config).
function Get-OpenCodeModels {
$ErrorActionPreference = "Continue"
$output = node $InstallerLib list-models "--config-dir" $OpenCodeConfigDir 2>$null
if ($LASTEXITCODE -ne 0 -or -not $output) { return @() }
return @($output | ForEach-Object { "$_".Trim() } | Where-Object { $_ -match $ModelIdPattern })
}
function Test-InteractiveConsole {
if ($NonInteractive -or $Host.Name -ne "ConsoleHost") { return $false }
try {
return -not ([Console]::IsInputRedirected -or [Console]::IsOutputRedirected)
} catch {
return $false
}
}
function Select-FusionModel([string]$Prompt, [string]$DefaultModel, [string]$CurrentModel, [string[]]$AvailableModels) {
if ($CurrentModel) { $DefaultModel = $CurrentModel }
if (-not (Test-InteractiveConsole)) {
Write-Host "${Prompt}: using $DefaultModel"
return $DefaultModel
}
$previousCtrlC = $null
try {
$previousCtrlC = [Console]::TreatControlCAsInput
[Console]::TreatControlCAsInput = $true
return Show-FusionModelMenu $Prompt $DefaultModel $AvailableModels
} catch [System.IO.IOException] {
Write-Host "${Prompt}: console menu unavailable, using $DefaultModel"
return $DefaultModel
} finally {
if ($null -ne $previousCtrlC) { try { [Console]::TreatControlCAsInput = $previousCtrlC } catch { } }
}
}
# Chooses all fusion models before anything is copied, so a cancelled or failed menu
# leaves the existing installation untouched.
function Resolve-FusionModels([string]$SourceAgentsDir, [string]$TargetAgentsDir, [hashtable]$Overrides) {
$availableModels = @()
if (Test-InteractiveConsole) { $availableModels = Get-OpenCodeModels }
$chosen = @{}
foreach ($agent in $FusionAgents) {
$override = $Overrides[$agent.Name]
if ($override) {
Assert-ModelId $override $agent.Name
$chosen[$agent.Name] = $override
continue
}
$default = Get-AgentModel (Join-Path $SourceAgentsDir $agent.Name)
if (-not $default) { throw "Shipped agent $($agent.Name) has no default model." }
$current = Get-AgentModel (Join-Path $TargetAgentsDir $agent.Name)
$model = Select-FusionModel $agent.Prompt $default $current $availableModels
Assert-ModelId $model $agent.Name
$chosen[$agent.Name] = $model
}
return $chosen
}
function Remove-LegacyGlobalFiles {
# OpenCode 2 does not load ~/.config/opencode/tools; the plugin registers the fusion tool itself.
Remove-Item -Path (Join-Path $OpenCodeConfigDir "tools\open_ultracode_configure_fusion.ts") -Force -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path $OpenCodeConfigDir "lib\fusion-config.ts") -Force -ErrorAction SilentlyContinue
}
function Install-GlobalAssets([string]$SourceDir, [hashtable]$Overrides) {
$skillsDir = Join-Path $OpenCodeConfigDir "skills"
$commandsDir = Join-Path $OpenCodeConfigDir "commands"
$agentsDir = Join-Path $OpenCodeConfigDir "agents"
$models = Resolve-FusionModels (Join-Path $SourceDir "assets\agents") $agentsDir $Overrides
New-Item -ItemType Directory -Path $skillsDir, $commandsDir, $agentsDir -Force | Out-Null
$targetSkill = Join-Path $skillsDir "open-ultracode"
if (Test-Path $targetSkill) { Remove-Item -Path $targetSkill -Recurse -Force }
Copy-Item -Path (Join-Path $SourceDir ".opencode\skills\open-ultracode") -Destination $targetSkill -Recurse -Force
foreach ($name in $ShippedCommands) {
Copy-Item -Path (Join-Path $SourceDir ".opencode\commands\$name") -Destination $commandsDir -Force
}
foreach ($name in $ShippedAgents) {
Copy-Item -Path (Join-Path $SourceDir "assets\agents\$name") -Destination $agentsDir -Force
}
foreach ($agent in $FusionAgents) {
Invoke-InstallerLib "Setting the $($agent.Name) model" set-agent-model (Join-Path $agentsDir $agent.Name) $models[$agent.Name]
}
Remove-LegacyGlobalFiles
$pluginPath = Join-Path $SourceDir ".opencode\plugins\open-ultracode"
Invoke-InstallerLib "Registering the OpenUltraCode plugin" register "--config-dir" $OpenCodeConfigDir "--plugin-path" $pluginPath
}
function Remove-GlobalAssets {
$targetSkill = Join-Path $OpenCodeConfigDir "skills\open-ultracode"
if (Test-Path $targetSkill) { Remove-Item -Path $targetSkill -Recurse -Force }
# Remove only the files OpenUltraCode ships, never a user's own ultracode-* agents or commands.
foreach ($name in $ShippedCommands) {
Remove-Item -Path (Join-Path $OpenCodeConfigDir "commands\$name") -Force -ErrorAction SilentlyContinue
}
foreach ($name in $ShippedAgents) {
Remove-Item -Path (Join-Path $OpenCodeConfigDir "agents\$name") -Force -ErrorAction SilentlyContinue
}
Remove-LegacyGlobalFiles
Invoke-InstallerLib "Unregistering the OpenUltraCode plugin" unregister "--config-dir" $OpenCodeConfigDir
}
function Show-FusionModelMenu([string]$Prompt, [string]$DefaultModel, [string[]]$AvailableModels) {
$choices = [System.Collections.Generic.List[string]]::new()
$choices.Add($DefaultModel)
foreach ($model in $AvailableModels) {
if ($model -ne $DefaultModel -and -not $choices.Contains($model)) { $choices.Add($model) }
}
$choices.Add("Custom model ID")
$n = $choices.Count
$bufferWidth = [Console]::BufferWidth
$bufferHeight = [Console]::BufferHeight
$width = [int][Math]::Min([Console]::WindowWidth, $bufferWidth)
if ($width -lt 40) { $width = [int][Math]::Min(80, $bufferWidth) }
$maxLen = 0
foreach ($c in $choices) { if ($c.Length -gt $maxLen) { $maxLen = $c.Length } }
$colWidth = [int][Math]::Max(12, [Math]::Min(64, $maxLen + 4))
$cols = [int][Math]::Max(1, [Math]::Floor($width / $colWidth))
$rows = [int][Math]::Ceiling($n / $cols)
$windowRows = [int][Math]::Min([Console]::WindowHeight, $bufferHeight)
$visibleRows = [int][Math]::Min($rows, [Math]::Max(5, $windowRows - 6))
$needed = 2 + $visibleRows
$top = [Console]::CursorTop
if ($top + $needed -ge $bufferHeight) {
$scroll = $top + $needed - $bufferHeight + 1
for ($i = 0; $i -lt $scroll; $i++) { [Console]::WriteLine() }
$top = [Math]::Min([Console]::CursorTop, $bufferHeight - $needed)
}
if ($top -lt 0) { $top = 0 }
$bottomLine = [Math]::Min($top + 2 + $visibleRows, $bufferHeight - 1)
$index = 0
$offset = 0
$origFg = [Console]::ForegroundColor
$origBg = [Console]::BackgroundColor
[Console]::SetCursorPosition(0, $top)
[Console]::Write($Prompt.PadRight($width))
while ($true) {
for ($r = 0; $r -lt $visibleRows; $r++) {
$y = [Math]::Min($top + 1 + $r, $bufferHeight - 1)
[Console]::SetCursorPosition(0, $y)
$x = 0
for ($c = 0; $c -lt $cols; $c++) {
$i = $c * $rows + $offset + $r
if ($i -ge $n) { break }
$text = $choices[$i]
if ($text.Length -gt $colWidth - 4) { $text = $text.Substring(0, $colWidth - 7) + "..." }
$cell = $text.PadRight($colWidth - 2)
if ($i -eq $index) {
[Console]::ForegroundColor = [ConsoleColor]::Black
[Console]::BackgroundColor = [ConsoleColor]::Cyan
[Console]::Write($cell)
[Console]::ForegroundColor = $origFg
[Console]::BackgroundColor = $origBg
} else {
[Console]::Write($cell)
}
$x += $cell.Length
}
$rest = [Math]::Max(0, $width - $x)
if ($rest -gt 0) { [Console]::Write(" " * $rest) }
}
$above = $offset
$below = $rows - $offset - $visibleRows
$hint = " "
if ($above -gt 0) { $hint += "^ $above more above " }
if ($below -gt 0) { $hint += "v $below more below" }
$hy = [Math]::Min($top + 1 + $visibleRows, $bufferHeight - 1)
[Console]::SetCursorPosition(0, $hy)
[Console]::Write($hint.PadRight($width))
[Console]::SetCursorPosition(0, $bottomLine)
$key = [Console]::ReadKey($true)
if ($key.Key -eq [ConsoleKey]::C -and ($key.Modifiers -band [ConsoleModifiers]::Control)) {
[Console]::SetCursorPosition(0, $bottomLine)
throw "OpenUltraCode installation cancelled."
}
switch ($key.Key) {
UpArrow { $index = ($index - 1 + $n) % $n }
DownArrow { $index = ($index + 1) % $n }
LeftArrow { $index = (($index - $rows) % $n + $n) % $n }
RightArrow { $index = ($index + $rows) % $n }
Enter {
[Console]::SetCursorPosition(0, $bottomLine)
$picked = $choices[$index]
if ($picked -eq "Custom model ID") {
$custom = Read-Host "Enter provider/model or provider/model#max"
if ($custom -match $ModelIdPattern) { return $custom }
Write-Host "Invalid model ID: $custom"
continue
}
return $picked
}
default { }
}
$row = $index % $rows
if ($row -lt $offset) { $offset = $row }
if ($row -ge $offset + $visibleRows) { $offset = $row - $visibleRows + 1 }
}
}
# Makes the running opencode pick up the install (`opencode reload`), then checks the REAL
# installation: the coordinator and every fusion agent must name a provider, model, and variant
# that opencode offers; with -Live each fusion agent must also answer through a real dispatch.
function Invoke-RuntimeCheck([string]$SourceDir, [bool]$Live) {
if ($SkipRuntimeCheck) {
Write-Host "OpenUltraCode: runtime check skipped (-SkipRuntimeCheck)."
return
}
if (-not (Get-Command opencode -ErrorAction SilentlyContinue)) {
Write-Warning "opencode is not on PATH; skipped the runtime check."
return
}
$ErrorActionPreference = "Continue"
opencode reload *> $null
if ($LASTEXITCODE -ne 0) { Write-Warning "'opencode reload' failed; restart opencode to load the changes." }
$checkArgs = @((Join-Path $SourceDir "scripts\runtime-check.mjs"), "--config-dir", $OpenCodeConfigDir)
if ($Live) { $checkArgs += "--live" }
node @checkArgs
$checkStatus = $LASTEXITCODE
if ($checkStatus -eq 1) { throw "The runtime check found problems (see above). Fix them with -PanelA/-PanelB/-Decider or /ultracode-fusion-models." }
if ($checkStatus -ne 0) { Write-Warning "The runtime check could not reach opencode; run 'node $($checkArgs[0])' after starting it." }
}
function Enable-GitHooks {
if (-not (Get-Command git -ErrorAction SilentlyContinue)) { return }
$ErrorActionPreference = "Continue"
git -C $repoRoot rev-parse --is-inside-work-tree *> $null
if ($LASTEXITCODE -ne 0) { return }
Invoke-Native "Configuring git hooks" { git -C $repoRoot config core.hooksPath .githooks }
}
if ($Remove) {
Remove-GlobalAssets
Write-Host "OpenUltraCode has been removed from opencode. Restart opencode to unload the plugin, commands, skills, and agents. The checkout at $repoRoot was left in place."
return
}
# Validate model overrides before the slow checks so a typo fails immediately.
$overrides = @{
"ultracode-fusion-panel-a.md" = $PanelA
"ultracode-fusion-panel-b.md" = $PanelB
"ultracode-fusion-arbiter.md" = $Decider
}
foreach ($entry in $overrides.GetEnumerator()) {
if ($entry.Value) { Assert-ModelId $entry.Value $entry.Key }
}
if (-not (Get-Command pnpm -ErrorAction SilentlyContinue) -and (Get-Command corepack -ErrorAction SilentlyContinue)) {
try { Invoke-Native "corepack enable" { corepack enable } } catch { Write-Warning $_ }
}
if (-not (Get-Command pnpm -ErrorAction SilentlyContinue)) {
throw "pnpm is required. Enable it with 'corepack enable' or install pnpm, then rerun .\install-dev.ps1."
}
Push-Location $repoRoot
try {
if (-not $SkipChecks) {
Invoke-Native "pnpm install" { pnpm install --frozen-lockfile }
Invoke-Native "pnpm run check" { pnpm run check }
}
Install-GlobalAssets $repoRoot $overrides
Enable-GitHooks
# Developers get the live check by default: every fusion agent is dispatched once.
Invoke-RuntimeCheck $repoRoot $true
# Show what is actually installed, read back from disk.
Write-Host ""
Invoke-InstallerLib "Summarizing the installation" summary "--config-dir" $OpenCodeConfigDir "--source-dir" $repoRoot | ForEach-Object { Write-Host $_ }
} finally {
Pop-Location
}