# Alter Ego: journal hooks for Claude Code and Codex on Windows. Run it yourself, it changes only the hooks of your own config. # irm https://alterego.cafeboon.com/install-hooks.ps1 | iex # & ([scriptblock]::Create((irm https://alterego.cafeboon.com/install-hooks.ps1))) -Uninstall # Options: -Claude or -Codex (only that tool), -Uninstall, -Server NAME (Claude Code connector name). # Works in Windows PowerShell 5.1 and PowerShell 7, nothing else needed. Safe to run again. param([switch]$Uninstall, [switch]$Claude, [switch]$Codex, [string]$Server = 'claude.ai Alter Ego') $ErrorActionPreference = 'Stop' function New-Hook($server, $turnId, $role, $message, $extra) { $h = [ordered]@{ type = 'mcp_tool'; server = $server; tool = 'record_turn' input = [ordered]@{ turn_id = $turnId; role = $role; message = $message }; timeout = 30 } foreach ($k in $extra.Keys) { $h[$k] = $extra[$k] } $h } function Test-Ours($group, $servers) { if (-not $group.hooks -or @($group.hooks).Count -eq 0) { return $false } foreach ($x in @($group.hooks)) { if (-not ($x.type -eq 'mcp_tool' -and $x.tool -eq 'record_turn' -and ($servers -contains $x.server))) { return $false } } $true } function Edit-Config($path, $wanted, $servers, $label) { $config = [pscustomobject]@{} if (Test-Path $path) { # Read as UTF-8 on purpose: Windows PowerShell 5.1 would read a file without BOM in the ANSI code page $raw = [System.IO.File]::ReadAllText($path, (New-Object System.Text.UTF8Encoding($false))) if ($raw.Trim()) { try { $config = $raw | ConvertFrom-Json } catch { throw "Could not read $path as JSON. Nothing was changed." } } } if (-not $config.PSObject.Properties['hooks']) { $config | Add-Member -NotePropertyName hooks -NotePropertyValue ([pscustomobject]@{}) } $hooks = $config.hooks $changed = $false foreach ($event in $wanted.Keys) { $existing = @(); if ($hooks.PSObject.Properties[$event]) { $existing = @($hooks.$event) } if ($Uninstall) { $kept = @($existing | Where-Object { -not (Test-Ours $_ $servers) }) if ($kept.Count -ne $existing.Count) { $changed = $true } $new = $kept } else { $new = $existing $mine = $wanted[$event] | ConvertTo-Json -Depth 10 -Compress if (-not ($existing | Where-Object { ($_ | ConvertTo-Json -Depth 10 -Compress) -eq $mine })) { $new = $existing + @($wanted[$event] | ConvertTo-Json -Depth 10 | ConvertFrom-Json); $changed = $true } } if ($hooks.PSObject.Properties[$event]) { $hooks.PSObject.Properties.Remove($event) } if ($new.Count -gt 0) { $hooks | Add-Member -NotePropertyName $event -NotePropertyValue @($new) } } if (@($hooks.PSObject.Properties).Count -eq 0) { $config.PSObject.Properties.Remove('hooks') } if ($changed) { New-Item -ItemType Directory -Force -Path (Split-Path $path) | Out-Null $tmp = "$path.tmp" $json = $config | ConvertTo-Json -Depth 20 [System.IO.File]::WriteAllText($tmp, $json + "`n", (New-Object System.Text.UTF8Encoding($false))) Move-Item -Force $tmp $path $verb = if ($Uninstall) { 'hooks removed' } else { 'hooks installed' } Write-Host "${label}: $verb in $path" } else { Write-Host "${label}: nothing to change in $path" } } $home_ = [Environment]::GetFolderPath('UserProfile') $claudeDir = if ($env:CLAUDE_CONFIG_DIR) { $env:CLAUDE_CONFIG_DIR } else { Join-Path $home_ '.claude' } $codexDir = if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $home_ '.codex' } $doClaude = $Claude -or (-not $Codex -and (Test-Path $claudeDir)) $doCodex = $Codex -or (-not $Claude -and (Test-Path $codexDir)) if (-not ($doClaude -or $doCodex)) { throw "Neither $claudeDir nor $codexDir exists. Use -Claude or -Codex." } if ($doClaude) { $t = 'claude-${session_id}' $cont = @{ onFailure = 'continue' } $wanted = [ordered]@{ UserPromptSubmit = [ordered]@{ hooks = @(New-Hook $Server $t 'user' '${prompt}' $cont) } Stop = [ordered]@{ hooks = @(New-Hook $Server $t 'assistant' '${last_assistant_message}' $cont) } SubagentStop = [ordered]@{ matcher = '.'; hooks = @(New-Hook $Server "$t-subagent" 'assistant' '${last_assistant_message}' $cont) } } Edit-Config (Join-Path $claudeDir 'settings.json') $wanted @($Server) 'Claude Code' } if ($doCodex) { $wanted = [ordered]@{ UserPromptSubmit = [ordered]@{ hooks = @(New-Hook 'alter_ego' '${turn_id}' 'user' '${prompt}' @{ statusMessage = 'Saving user message to Alter Ego' }) } Stop = [ordered]@{ hooks = @(New-Hook 'alter_ego' '${turn_id}' 'assistant' '${last_assistant_message}' @{ statusMessage = 'Saving assistant message to Alter Ego' }) } } Edit-Config (Join-Path $codexDir 'hooks.json') $wanted @('alter_ego') 'Codex' } if (-not $Uninstall) { if ($doClaude) { Write-Host "Claude Code: restart it. The connector must be called '$Server' in /mcp, or pass -Server NAME." } if ($doCodex) { Write-Host "Codex: run 'codex mcp add alter_ego --url https://alterego.cafeboon.com/mcp' and 'codex mcp login alter_ego', then trust the hooks in Codex." } }