-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrok-wrapper.ps1
More file actions
217 lines (201 loc) · 8.27 KB
/
Copy pathgrok-wrapper.ps1
File metadata and controls
217 lines (201 loc) · 8.27 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
# SuppperGrok launcher.
# Dot-source this file, then route the `grok` function to Invoke-SuppperGrok.
$global:__SUPPPERGROK_ROOT = $PSScriptRoot
function Invoke-SuppperGrok {
$rawArgs = @($args | ForEach-Object { [string]$_ })
$native = $env:GROK_NATIVE
if ([string]::IsNullOrWhiteSpace($native)) {
$homeCandidate = Join-Path (Join-Path $HOME '.grok') 'bin\grok.exe'
if (Test-Path -LiteralPath $homeCandidate) {
$native = $homeCandidate
} else {
$command = Get-Command grok.exe -CommandType Application -ErrorAction SilentlyContinue |
Select-Object -First 1
if ($command) {
$native = $command.Source
}
}
}
if ([string]::IsNullOrWhiteSpace($native) -or -not (Test-Path -LiteralPath $native)) {
throw 'Set GROK_NATIVE or install grok.exe under ~/.grok/bin.'
}
$stockPrompt = $env:GROK_STOCK_PROMPT -eq '1'
$filteredArgs = [System.Collections.Generic.List[string]]::new()
foreach ($arg in $rawArgs) {
if ($arg -eq '--stock-prompt') {
$stockPrompt = $true
continue
}
$filteredArgs.Add($arg)
}
$hasExplicitOverride = $false
foreach ($arg in $filteredArgs) {
if (
$arg -eq '--system-prompt-override' -or
$arg -eq '--system-prompt' -or
$arg.StartsWith('--system-prompt-override=') -or
$arg.StartsWith('--system-prompt=')
) {
$hasExplicitOverride = $true
break
}
}
$managementCommands = @(
'agent', 'completions', 'dashboard', 'export', 'help', 'import',
'inspect', 'leader', 'login', 'logout', 'mcp', 'memory', 'models',
'plugin', 'sessions', 'setup', 'trace', 'update', 'version',
'worktree', 'wrap'
)
$managementInvocation = $false
foreach ($arg in $filteredArgs) {
if ($managementCommands -contains $arg.ToLowerInvariant()) {
$managementInvocation = $true
break
}
}
$effectiveArgs = [System.Collections.Generic.List[string]]::new()
if (-not $stockPrompt -and -not $hasExplicitOverride -and -not $managementInvocation) {
$promptPath = if ([string]::IsNullOrWhiteSpace($env:SUPPPERGROK_PROMPT_FILE)) {
Join-Path $global:__SUPPPERGROK_ROOT 'grok-4.5-unrestricted.md'
} else {
$env:SUPPPERGROK_PROMPT_FILE
}
if (-not (Test-Path -LiteralPath $promptPath)) {
throw "SuppperGrok prompt not found: $promptPath"
}
$effectiveCwd = (Get-Location).Path
for ($i = 0; $i -lt $filteredArgs.Count; $i++) {
if ($filteredArgs[$i] -eq '--cwd' -and $i + 1 -lt $filteredArgs.Count) {
$effectiveCwd = $filteredArgs[$i + 1]
break
}
if ($filteredArgs[$i].StartsWith('--cwd=')) {
$effectiveCwd = $filteredArgs[$i].Substring('--cwd='.Length)
break
}
}
try {
$effectiveCwd = (Resolve-Path -LiteralPath $effectiveCwd -ErrorAction Stop).Path
} catch {
$effectiveCwd = (Get-Location).Path
}
$runtimeRules = [System.Text.StringBuilder]::new()
if ($env:SUPPPERGROK_INCLUDE_PROJECT_RULES -eq '1') {
$recognized = @('Agents.md', 'Claude.md', 'AGENT.md', 'AGENTS.md')
$ruleFiles = [System.Collections.Generic.List[string]]::new()
$grokHome = if ([string]::IsNullOrWhiteSpace($env:GROK_HOME)) {
Join-Path $HOME '.grok'
} else {
$env:GROK_HOME
}
foreach ($name in $recognized) {
$candidate = Join-Path $grokHome $name
if (Test-Path -LiteralPath $candidate) {
$ruleFiles.Add((Resolve-Path -LiteralPath $candidate).Path)
}
}
$gitRoot = $null
try {
$rootOutput = & git -C $effectiveCwd rev-parse --show-toplevel 2>$null
if ($LASTEXITCODE -eq 0 -and $rootOutput) {
$gitRoot = (Resolve-Path -LiteralPath ([string]$rootOutput[0])).Path
}
} catch { }
$ruleDirs = [System.Collections.Generic.List[string]]::new()
if ($gitRoot -and $effectiveCwd.StartsWith($gitRoot, [StringComparison]::OrdinalIgnoreCase)) {
$cursor = $effectiveCwd
$reverse = [System.Collections.Generic.List[string]]::new()
while ($cursor.StartsWith($gitRoot, [StringComparison]::OrdinalIgnoreCase)) {
$reverse.Add($cursor)
if ($cursor -ieq $gitRoot) { break }
$parent = Split-Path -Parent $cursor
if ([string]::IsNullOrWhiteSpace($parent) -or $parent -ieq $cursor) { break }
$cursor = $parent
}
for ($i = $reverse.Count - 1; $i -ge 0; $i--) {
$ruleDirs.Add($reverse[$i])
}
} else {
$ruleDirs.Add($effectiveCwd)
}
foreach ($dir in $ruleDirs) {
foreach ($name in $recognized) {
$candidate = Join-Path $dir $name
if (Test-Path -LiteralPath $candidate) {
$resolved = (Resolve-Path -LiteralPath $candidate).Path
if (-not $ruleFiles.Contains($resolved)) {
$ruleFiles.Add($resolved)
}
}
}
}
$ruleIndex = 0
foreach ($ruleFile in $ruleFiles) {
if ($runtimeRules.Length -ge 16000) { break }
$ruleIndex++
$ruleText = Get-Content -LiteralPath $ruleFile -Raw -Encoding UTF8
$ruleText = [regex]::Replace(
$ruleText,
'(?ms)^## Safety\s*\r?\n.*?(?=^## |\z)',
''
)
$ruleText = [regex]::Replace(
$ruleText,
'(?ms)^## Safe Capability Boost\s*\r?\n.*?(?=^## |\z)',
''
)
$remaining = 16000 - $runtimeRules.Length
if ($ruleText.Length -gt $remaining) {
$ruleText = $ruleText.Substring(0, $remaining)
}
$ruleLabel = [IO.Path]::GetFileName($ruleFile)
[void]$runtimeRules.AppendLine(
('===== rule {0}: {1} =====' -f $ruleIndex, $ruleLabel)
)
[void]$runtimeRules.AppendLine($ruleText)
}
}
$basePrompt = Get-Content -LiteralPath $promptPath -Raw -Encoding UTF8
$systemPrompt = if ($runtimeRules.Length -gt 0) {
"<runtime_rules>`n$runtimeRules</runtime_rules>`n`n$basePrompt"
} else {
$basePrompt
}
$effectiveArgs.Add('--system-prompt-override')
$effectiveArgs.Add($systemPrompt)
}
foreach ($arg in $filteredArgs) {
$effectiveArgs.Add($arg)
}
$proxy = $env:SUPPPERGROK_PROXY
$proxyNames = @('HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY')
$previous = @{}
foreach ($name in $proxyNames) {
$previous[$name] = [Environment]::GetEnvironmentVariable($name, 'Process')
}
try {
if (-not [string]::IsNullOrWhiteSpace($proxy)) {
$env:HTTP_PROXY = $proxy
$env:HTTPS_PROXY = $proxy
$env:ALL_PROXY = $proxy
if (-not [string]::IsNullOrWhiteSpace($env:SUPPPERGROK_NO_PROXY)) {
$env:NO_PROXY = if ([string]::IsNullOrWhiteSpace($previous.NO_PROXY)) {
$env:SUPPPERGROK_NO_PROXY
} else {
"$($previous.NO_PROXY),$($env:SUPPPERGROK_NO_PROXY)"
}
}
}
& $native @effectiveArgs
$nativeExitCode = $LASTEXITCODE
} finally {
foreach ($name in $proxyNames) {
if ($null -eq $previous[$name]) {
Remove-Item -LiteralPath "Env:$name" -ErrorAction SilentlyContinue
} else {
Set-Item -LiteralPath "Env:$name" -Value $previous[$name]
}
}
}
$global:LASTEXITCODE = $nativeExitCode
}