-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
258 lines (228 loc) · 10.3 KB
/
Copy pathinstall.ps1
File metadata and controls
258 lines (228 loc) · 10.3 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
# install.ps1 - Install claude-context-sync on Windows
#
# Usage (run as your normal user in PowerShell):
# .\install.ps1 -Key "YOUR_API_KEY"
# .\install.ps1 -Key "YOUR_API_KEY" -Url "https://claude-contexts.akka.io"
#
# The API key is provided by IT/DevEx - it is NOT a GitHub token.
#
# Requirements: Python 3.8+ (https://python.org/downloads)
#
# NOTE: This file is intentionally ASCII-only. Windows PowerShell 5.1 (the
# default powershell.exe) reads BOM-less scripts as the system ANSI code page,
# so non-ASCII characters in comments/strings become mojibake and break the
# parser before anything runs. Keep it ASCII unless the file is saved with a BOM.
param(
[string]$Key = "",
[string]$Url = "https://claude-contexts.akka.io"
)
$ErrorActionPreference = "Stop"
$ScriptName = "sync_claude_contexts.py"
$WatcherName = "watch_cowork_sessions.py"
$ClaudeDir = Join-Path $env:USERPROFILE ".claude"
$InstallPath = Join-Path $ClaudeDir $ScriptName
$WatcherPath = Join-Path $ClaudeDir $WatcherName
$ConfigFile = Join-Path $ClaudeDir "context-sync.conf"
$LogFile = Join-Path $ClaudeDir "context-sync.log"
$WatcherLog = Join-Path $ClaudeDir "cowork-watcher.log"
$TaskName = "ClaudeContextSync"
$WatcherTask = "ClaudeCoworkWatcher"
function Info { param($m) Write-Host " [INFO] $m" -ForegroundColor Cyan }
function Warn { param($m) Write-Host " [WARN] $m" -ForegroundColor Yellow }
function Fail { param($m) Write-Host " [ERROR] $m" -ForegroundColor Red; exit 1 }
# --- Python check -------------------------------------------------------------
function Find-Python {
foreach ($cmd in @("python", "python3", "py")) {
try {
$ver = & $cmd --version 2>&1
if ($ver -match "Python (\d+)\.(\d+)") {
$major = [int]$Matches[1]
$minor = [int]$Matches[2]
if ($major -ge 3 -and $minor -ge 8) {
return (Get-Command $cmd).Source
}
}
} catch {}
}
Fail "Python 3.8+ is required. Download from https://python.org/downloads"
}
Write-Host ""
Write-Host "==========================================" -ForegroundColor Green
Write-Host " Claude Context Sync - Windows Installer" -ForegroundColor Green
Write-Host "==========================================" -ForegroundColor Green
Write-Host ""
$PythonPath = Find-Python
Info "Using Python: $PythonPath"
# --- Create ~/.claude ---------------------------------------------------------
if (-not (Test-Path $ClaudeDir)) {
New-Item -ItemType Directory -Path $ClaudeDir | Out-Null
}
# --- Copy or download script --------------------------------------------------
$LocalScript = Join-Path $PSScriptRoot $ScriptName
if (Test-Path $LocalScript) {
Info "Copying $ScriptName from current directory..."
Copy-Item $LocalScript $InstallPath -Force
} else {
Info "Downloading $ScriptName from GitHub..."
$RawUrl = "https://raw.githubusercontent.com/akka/ai-context-sync/main/$ScriptName"
try {
Invoke-WebRequest -Uri $RawUrl -OutFile $InstallPath -UseBasicParsing
} catch {
Fail "Download failed: $_"
}
}
Info "Installed script to $InstallPath"
# --- Copy or download cowork watcher ------------------------------------------
$LocalWatcher = Join-Path $PSScriptRoot $WatcherName
if (Test-Path $LocalWatcher) {
Info "Copying $WatcherName from current directory..."
Copy-Item $LocalWatcher $WatcherPath -Force
} else {
Info "Downloading $WatcherName from GitHub..."
$WatcherUrl = "https://raw.githubusercontent.com/akka/ai-context-sync/main/$WatcherName"
try {
Invoke-WebRequest -Uri $WatcherUrl -OutFile $WatcherPath -UseBasicParsing
Info "Installed watcher to $WatcherPath"
} catch {
Warn "Could not download ${WatcherName} - cowork session sync unavailable."
}
}
# --- Write config file --------------------------------------------------------
# Write BOM-less UTF-8. Set-Content -Encoding UTF8 on PowerShell 5.1 emits a
# UTF-8 BOM, which the Python parser would read as part of the first key name
# (the BOM prefixes "SOURCE_URL") and fail to match. [IO.File]::WriteAllText with a
# UTF8Encoding($false) writes no BOM. (The Python side also reads with
# 'utf-8-sig' as a belt-and-suspenders guard.)
function Write-NoBom {
param($Path, $Content)
$utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($Path, $Content, $utf8NoBom)
}
if ($Key -ne "") {
Write-NoBom $ConfigFile "SOURCE_URL=$Url`nCONTEXT_API_KEY=$Key`n"
Info "Saved config to $ConfigFile"
} elseif (-not (Test-Path $ConfigFile)) {
Write-NoBom $ConfigFile @"
# Claude Context Sync configuration
# Contact IT for your CONTEXT_API_KEY - do NOT share it.
SOURCE_URL=$Url
CONTEXT_API_KEY=YOUR_KEY_HERE
"@
Warn "API key not provided - edit $ConfigFile and set CONTEXT_API_KEY."
}
# Restrict config file permissions to current user only
try {
$acl = Get-Acl $ConfigFile
$acl.SetAccessRuleProtection($true, $false)
$acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) | Out-Null }
$currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$currentUser, "FullControl", "Allow"
)
$acl.AddAccessRule($rule)
Set-Acl -Path $ConfigFile -AclObject $acl
} catch {
Warn "Could not restrict config file permissions: $_"
}
# --- Task Scheduler - daily at 08:00 ------------------------------------------
Info "Creating Windows Task Scheduler entry ($TaskName)..."
$Action = New-ScheduledTaskAction `
-Execute $PythonPath `
-Argument "`"$InstallPath`" >> `"$LogFile`" 2>&1"
$Trigger = New-ScheduledTaskTrigger -Daily -At "08:00"
$Settings = New-ScheduledTaskSettingsSet `
-ExecutionTimeLimit (New-TimeSpan -Hours 1) `
-RestartCount 2 `
-RestartInterval (New-TimeSpan -Minutes 5) `
-StartWhenAvailable
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false -ErrorAction SilentlyContinue
Register-ScheduledTask `
-TaskName $TaskName `
-Action $Action `
-Trigger $Trigger `
-Settings $Settings `
-Description "Daily sync of Claude AI context files from akka.io" `
-RunLevel Limited | Out-Null
Info "Scheduled task created - runs daily at 08:00."
# --- Task Scheduler - cowork watcher (persistent, restarts on failure) --------
if (Test-Path $WatcherPath) {
Info "Creating cowork watcher task ($WatcherTask)..."
$WatcherAction = New-ScheduledTaskAction `
-Execute $PythonPath `
-Argument "`"$WatcherPath`" >> `"$WatcherLog`" 2>&1"
# Trigger: at logon, runs indefinitely
$WatcherTrigger = New-ScheduledTaskTrigger -AtLogOn
$WatcherSettings = New-ScheduledTaskSettingsSet `
-ExecutionTimeLimit ([System.TimeSpan]::Zero) `
-RestartCount 10 `
-RestartInterval (New-TimeSpan -Minutes 1) `
-StartWhenAvailable
Unregister-ScheduledTask -TaskName $WatcherTask -Confirm:$false -ErrorAction SilentlyContinue
# Registering the AtLogOn / zero-timeout watcher can fail with "Access is
# denied" (HRESULT 0x80070005) on machines where policy requires elevation
# for that trigger type, even though the time-triggered daily task above is
# allowed. Catch it so the installer reports the truth instead of a false
# success, and so the (working) daily sync is not lost to an aborted script.
$watcherRegistered = $false
try {
Register-ScheduledTask `
-TaskName $WatcherTask `
-Action $WatcherAction `
-Trigger $WatcherTrigger `
-Settings $WatcherSettings `
-Description "Watches for new Claude cowork sessions and injects org context" `
-RunLevel Limited `
-ErrorAction Stop | Out-Null
$watcherRegistered = $true
} catch {
# Rebuild the exact command to re-run, so the user only has to paste it.
# Use the full script path ($PSCommandPath) because an elevated PowerShell
# opens in C:\Windows\System32, not the folder they downloaded this to.
# Use the full script path ($PSCommandPath) because an elevated PowerShell
# opens in C:\Windows\System32, not the folder they downloaded this to.
# We echo the real key so this is a true zero-thought copy-paste: the
# CONTEXT_API_KEY is a low-value, read-only shared key (it only fetches
# context files via the Worker; the sensitive GitHub PAT stays server-side),
# so it appearing in console/scrollback is not a meaningful exposure.
$scriptPath = if ($PSCommandPath) { $PSCommandPath } else { $InstallPath }
$reRun = "& `"$scriptPath`""
if ($Key -ne "") { $reRun += " -Key `"$Key`"" }
$reRun += " -Url `"$Url`""
Warn "Could not register the cowork watcher task (Access is denied)."
Warn ""
Warn "Good news: the daily context sync IS installed and working."
Warn "Only the cowork watcher needs Administrator rights on this machine,"
Warn "so cowork sessions won't get org context until you finish this step:"
Warn ""
Warn " 1. Press the Windows key, then type: powershell"
Warn " 2. In the results, RIGHT-CLICK 'Windows PowerShell'"
Warn " and choose 'Run as administrator'."
Warn " 3. Click 'Yes' on the 'User Account Control' pop-up."
Warn " 4. Copy the line below, paste it into that new (blue) window"
Warn " (right-click pastes), and press Enter:"
Warn ""
Warn " $reRun"
Warn ""
Warn "That re-runs this installer with admin rights and registers the watcher."
Warn "(Nothing else changes - it's safe to run again.)"
}
if ($watcherRegistered) {
# Start it immediately
Start-ScheduledTask -TaskName $WatcherTask -ErrorAction SilentlyContinue
Info "Cowork watcher task created and started."
}
}
# --- First run ----------------------------------------------------------------
if ($Key -ne "") {
Write-Host ""
Info "Running initial sync..."
& $PythonPath $InstallPath
} else {
Write-Host ""
Write-Host " Next steps:" -ForegroundColor Yellow
Write-Host " 1. Edit $ConfigFile and set CONTEXT_API_KEY=<key from IT>"
Write-Host " 2. Run manually: python `"$InstallPath`""
}
Write-Host ""
Write-Host " Done! Logs: $LogFile" -ForegroundColor Green
Write-Host ""