-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpre-tool-use.ps1
More file actions
75 lines (62 loc) · 2.44 KB
/
Copy pathpre-tool-use.ps1
File metadata and controls
75 lines (62 loc) · 2.44 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
# pre-tool-use.ps1 — Windows PreToolUse hook for codeact enforcement
$ErrorActionPreference = "SilentlyContinue"
$Mode = if ($env:CODEACT_MODE) { $env:CODEACT_MODE } else { "off" }
# Fast path
if ($Mode -eq "off" -or $Mode -eq "") {
$null = [Console]::In.ReadToEnd()
exit 0
}
$InputRaw = [Console]::In.ReadToEnd()
$InputObj = $InputRaw | ConvertFrom-Json
$ToolName = if ($InputObj.toolName) { $InputObj.toolName } elseif ($InputObj.tool_name) { $InputObj.tool_name } else { "" }
$ToolArgs = if ($InputObj.toolInput) { $InputObj.toolInput | ConvertTo-Json -Compress } elseif ($InputObj.input) { $InputObj.input | ConvertTo-Json -Compress } else { "{}" }
function Test-CodeActCall {
if ($ToolName -ne "bash" -and $ToolName -ne "shell") { return $false }
return $ToolArgs -match 'codeact\.py|scripts/codeact'
}
# Read discovered tool list from install-time manifest
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$PluginDir = Split-Path -Parent $ScriptDir
$ToolsFile = Join-Path $PluginDir ".codeact-tools.json"
$InstalledTools = "view, create, edit, glob, bash, sql"
if (Test-Path $ToolsFile) {
try {
$manifest = Get-Content $ToolsFile -Raw | ConvertFrom-Json
$InstalledTools = ($manifest.tools | ForEach-Object { $_.name }) -join ', '
} catch {}
}
$DenyReason = @"
CodeAct enforcement active (CODEACT_MODE=$Mode). Collapse this work into one sandboxed Python run:
bash plugins/codeact/scripts/codeact --code '<your python>'
Sandbox tools: ${InstalledTools}.
Disable enforcement: unset CODEACT_MODE.
"@
function Send-Deny {
@{ permissionDecision = "deny"; permissionDecisionReason = $DenyReason } | ConvertTo-Json -Compress
exit 0
}
function Send-Allow {
Write-Output '{}'
exit 0
}
$CounterFile = "$env:TEMP\codeact-$PID.count"
switch ($Mode) {
"nudge" {
if (Test-CodeActCall) { "0" | Set-Content $CounterFile; Send-Allow }
$readOnly = @("view", "glob", "grep", "rg", "read_file", "file_search")
if ($readOnly -contains $ToolName) {
$count = if (Test-Path $CounterFile) { [int](Get-Content $CounterFile) } else { 0 }
$count++
"$count" | Set-Content $CounterFile
if ($count -ge 3) { "0" | Set-Content $CounterFile; Send-Deny }
} else {
"0" | Set-Content $CounterFile
}
Send-Allow
}
"exclusive" {
if (Test-CodeActCall) { Send-Allow }
Send-Deny
}
default { Send-Allow }
}