-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathralph.ps1
More file actions
247 lines (200 loc) · 7.47 KB
/
ralph.ps1
File metadata and controls
247 lines (200 loc) · 7.47 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
#!/usr/bin/env pwsh
#requires -Version 5.1
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$Prompt,
[Parameter(Mandatory=$false)]
[string]$Prd,
[Parameter(Mandatory=$false)]
[string]$Skill,
[Parameter(Mandatory=$false)]
[ValidateSet('safe', 'dev', 'locked')]
[string]$AllowProfile,
[Parameter(Mandatory=$false)]
[string[]]$AllowTools = @(),
[Parameter(Mandatory=$false)]
[string[]]$DenyTools = @(),
[Parameter(Mandatory=$false, Position=0)]
[int]$Iterations,
[Parameter(Mandatory=$false)]
[switch]$Help
)
$ErrorActionPreference = 'Stop'
$RALPH_VERSION = "1.1.0"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
function Show-Usage {
@"
Usage:
ralph.ps1 -Prompt <file> [-Prd <file>] [-Skill <a[,b,...]>] [-AllowProfile <safe|dev|locked>] [-AllowTools <toolSpec> ...] [-DenyTools <toolSpec> ...] <iterations>
Options:
-Prompt <file> Load prompt text from file (required).
-Prd <file> Optionally attach a PRD JSON file.
-Skill <a[,b,...]> Prepend one or more skills from skills/<name>/SKILL.md (comma-separated).
-AllowProfile <name> Tool permission profile: safe | dev | locked.
-AllowTools <toolSpec> Allow a specific tool (repeatable). Example: -AllowTools write
Use quotes if the spec includes spaces: -AllowTools 'shell(git push)'
-DenyTools <toolSpec> Deny a specific tool (repeatable). Example: -DenyTools 'shell(rm)'
-Help Show this help.
Notes:
- You must pass -AllowProfile or at least one -AllowTools.
Examples:
ralph.ps1 -Prompt prompts/default.txt -AllowProfile safe 5
ralph.ps1 -Prompt prompts/default.txt -Prd plans/prd.json -Skill wp-plugin-development -AllowProfile dev 10
"@
}
if ($Help) {
Show-Usage
exit 0
}
if (-not $Prompt) {
Write-Error "Error: -Prompt is required"
Show-Usage
exit 1
}
if ($Iterations -le 0) {
Write-Error "Error: <iterations> must be a positive integer"
Show-Usage
exit 1
}
if (-not (Test-Path $Prompt -PathType Leaf)) {
Write-Error "Error: prompt file not readable: $Prompt"
exit 1
}
if ($Prd -and -not (Test-Path $Prd -PathType Leaf)) {
Write-Error "Error: PRD file not readable: $Prd"
exit 1
}
$ProgressFile = "activity.txt"
if (-not (Test-Path $ProgressFile -PathType Leaf)) {
Write-Error "Error: progress file not readable: $ProgressFile"
exit 1
}
# Parse skills
$SkillsList = @()
if ($Skill) {
$SkillsList = $Skill -split ',' | ForEach-Object { $_.Trim() } | Where-Object { $_ }
}
# Validate tool permissions
if (-not $AllowProfile -and $AllowTools.Count -eq 0) {
Write-Error "Error: you must specify -AllowProfile or at least one -AllowTools"
Show-Usage
exit 1
}
# Build copilot tool arguments
$CopilotToolArgs = @()
# Always deny dangerous commands
$CopilotToolArgs += '--deny-tool', 'shell(rm)'
$CopilotToolArgs += '--deny-tool', 'shell(git push)'
if ($AllowTools.Count -eq 0 -and $AllowProfile) {
switch ($AllowProfile) {
'dev' {
$CopilotToolArgs += '--allow-all-tools'
$CopilotToolArgs += '--allow-tool', 'write'
$CopilotToolArgs += '--allow-tool', 'shell(pnpm:*)'
$CopilotToolArgs += '--allow-tool', 'shell(git:*)'
}
'safe' {
$CopilotToolArgs += '--allow-tool', 'write'
$CopilotToolArgs += '--allow-tool', 'shell(pnpm:*)'
$CopilotToolArgs += '--allow-tool', 'shell(git:*)'
}
'locked' {
$CopilotToolArgs += '--allow-tool', 'write'
}
default {
Write-Error "Error: unknown -AllowProfile: $AllowProfile"
Show-Usage
exit 1
}
}
}
foreach ($tool in $AllowTools) {
$CopilotToolArgs += '--allow-tool', $tool
}
foreach ($tool in $DenyTools) {
$CopilotToolArgs += '--deny-tool', $tool
}
# Use MODEL environment variable if set, otherwise let copilot use its default
$Model = $env:MODEL
for ($i = 1; $i -le $Iterations; $i++) {
Write-Host "`nIteration $i"
Write-Host "------------------------------------"
# Create temporary context file
$ContextFile = Join-Path $env:TEMP "ralph_context_$([System.Guid]::NewGuid()).txt"
$ContextContent = New-Object System.Text.StringBuilder
[void]$ContextContent.AppendLine("# Context")
[void]$ContextContent.AppendLine()
if ($SkillsList.Count -gt 0) {
[void]$ContextContent.AppendLine("## Skills")
foreach ($skillName in $SkillsList) {
if (-not $skillName) { continue }
$SkillFile = "skills/$skillName/SKILL.md"
if (-not (Test-Path $SkillFile -PathType Leaf)) {
Write-Error "Error: skill not found/readable: $SkillFile"
exit 1
}
[void]$ContextContent.AppendLine()
[void]$ContextContent.AppendLine("### $skillName")
[void]$ContextContent.AppendLine()
[void]$ContextContent.Append((Get-Content $SkillFile -Raw))
}
[void]$ContextContent.AppendLine()
}
if ($Prd) {
[void]$ContextContent.AppendLine("## PRD ($Prd)")
[void]$ContextContent.Append((Get-Content $Prd -Raw))
[void]$ContextContent.AppendLine()
}
[void]$ContextContent.AppendLine("## activity.txt")
[void]$ContextContent.Append((Get-Content $ProgressFile -Raw))
[void]$ContextContent.AppendLine()
Set-Content -Path $ContextFile -Value $ContextContent.ToString() -NoNewline -Force
# Create combined prompt file
$CombinedPromptFile = Join-Path $env:TEMP "ralph_prompt_$([System.Guid]::NewGuid()).txt"
$CombinedContent = New-Object System.Text.StringBuilder
[void]$CombinedContent.Append((Get-Content $ContextFile -Raw))
[void]$CombinedContent.AppendLine()
[void]$CombinedContent.AppendLine("# Prompt")
[void]$CombinedContent.AppendLine()
[void]$CombinedContent.Append((Get-Content $Prompt -Raw))
[void]$CombinedContent.AppendLine()
Set-Content -Path $CombinedPromptFile -Value $CombinedContent.ToString() -NoNewline -Force
# Run copilot command
$CopilotArgs = @(
'--add-dir', $PWD.Path
'--no-color'
'--stream', 'off'
'--silent'
'-p', "@$CombinedPromptFile Follow the attached prompt."
)
if ($Model) {
$CopilotArgs += '--model', $Model
}
$CopilotArgs += $CopilotToolArgs
try {
$Result = & copilot @CopilotArgs 2>&1 | Out-String
$Status = $LASTEXITCODE
}
catch {
$Result = $_.Exception.Message
$Status = 1
}
# Clean up temp files
Remove-Item $ContextFile -Force -ErrorAction SilentlyContinue
Remove-Item $CombinedPromptFile -Force -ErrorAction SilentlyContinue
Write-Host $Result
if ($Status -ne 0) {
Write-Host "Copilot exited with status $Status; continuing to next iteration."
continue
}
if ($Result -match '<promise>COMPLETE</promise>') {
Write-Host "PRD complete, exiting."
# Optional: notify using tt if available
if (Get-Command tt -ErrorAction SilentlyContinue) {
& tt notify "PRD complete after $i iterations"
}
exit 0
}
}
Write-Host "Finished $Iterations iterations without receiving the completion signal."