param([ValidateSet('Setup','Notify','ImportCodexKey')][string]$Mode = 'Setup') # Claude Code -> Bark (iPhone) push. Modeled on Codex/bark/Invoke-CodexBark.ps1. # Notify mode reads the hook event JSON from stdin: # Stop: push only when this turn really ended work. # Desktop app: the turn called mcp__ccd_turn__wrap_up (ask/blocked -> waiting, else done). # CLI: the last reply ends with or . # Notification: push when Claude waits for a permission approval. $ErrorActionPreference = 'Stop' $keyPath = Join-Path $PSScriptRoot 'device-key.dpapi' function Send-Bark([Security.SecureString]$Key, [string]$Message) { $body = @{ device_key = [System.Net.NetworkCredential]::new('', $Key).Password title = 'Claude:' icon = 'https://claude.ai/apple-touch-icon.png' body = $Message group = 'Claude' } | ConvertTo-Json -Compress try { $parameters = @{ Uri = 'https://api.day.app/push' Method = 'Post' ContentType = 'application/json; charset=utf-8' Body = [Text.Encoding]::UTF8.GetBytes($body) TimeoutSec = 8 } $reply = Invoke-RestMethod @parameters return ($reply.code -eq 200) } catch { return $false } } function Get-TurnSignal([string]$TranscriptPath, [string]$LastMessage) { # Returns 'complete', 'input' or $null for the turn that just ended. $lines = @() if ($TranscriptPath -and (Test-Path -LiteralPath $TranscriptPath)) { $lines = [IO.File]::ReadAllLines($TranscriptPath, [Text.Encoding]::UTF8) } $lastText = $LastMessage for ($i = $lines.Count - 1; $i -ge 0; $i--) { $line = $lines[$i] if ($line -notmatch '"type":"(user|assistant)"') { continue } $entry = $line | ConvertFrom-Json if ($entry.isSidechain) { continue } $content = $entry.message.content if ($entry.type -eq 'user') { if ($entry.isMeta) { continue } $isToolResult = $content -is [array] -and @($content | Where-Object { $_.type -eq 'tool_result' }).Count -gt 0 if ($isToolResult) { continue } break # reached the user's message that started this turn } foreach ($block in @($content)) { if ($block.type -eq 'tool_use' -and $block.name -eq 'mcp__ccd_turn__wrap_up') { if ($block.input.ask -or $block.input.blocked) { return 'input' } return 'complete' } if (-not $lastText -and $block.type -eq 'text' -and $block.text) { $lastText = $block.text } } } if ($lastText -match '\s*\z') { return 'complete' } if ($lastText -match '\s*\z') { return 'input' } return $null } if ($Mode -eq 'Notify') { try { $raw = [Console]::In.ReadToEnd() $eventData = $raw | ConvertFrom-Json $message = $null if ($eventData.hook_event_name -eq 'Stop' -and -not $eventData.stop_hook_active) { switch (Get-TurnSignal $eventData.transcript_path $eventData.last_assistant_message) { 'complete' { $message = 'Claude 已完成這項工作,請查看結果。' } 'input' { $message = 'Claude 等待你的回覆或決定,請查看對話。' } } } elseif ($eventData.hook_event_name -eq 'Notification' -and $eventData.notification_type -eq 'permission_prompt') { $message = 'Claude 需要你核准操作,請查看對話。' } if ($env:CLAUDE_BARK_DRYRUN) { Write-Output "DRYRUN: $message"; exit 0 } if ($message -and (Test-Path -LiteralPath $keyPath)) { $key = Get-Content -LiteralPath $keyPath -Raw | ConvertTo-SecureString $null = Send-Bark $key $message } } catch { # Notification errors must not interrupt the Claude turn. } exit 0 } if ($Mode -eq 'ImportCodexKey') { # Reuse the key already paired for Codex (same Windows user, DPAPI-encrypted). $codexKey = Join-Path $env:LOCALAPPDATA 'CodexBark\device-key.dpapi' if (-not (Test-Path -LiteralPath $codexKey)) { throw '找不到 Codex 的 Bark 金鑰,請改用 -Mode Setup。' } $key = Get-Content -LiteralPath $codexKey -Raw | ConvertTo-SecureString if (-not (Send-Bark $key 'Bark 測試成功(Claude)。')) { throw 'Bark 未接受測試推播。請改用 -Mode Setup 重新配對。' } Copy-Item -LiteralPath $codexKey -Destination $keyPath -Force Write-Host 'Bark 已接受測試推播。請確認 iPhone 上是否收到通知。' exit 0 } $key = Read-Host '請貼上 Bark 裝置金鑰或 api.day.app 推播網址' -AsSecureString $inputText = [System.Net.NetworkCredential]::new('', $key).Password.Trim() if ($inputText -match '^https://api\.day\.app/([A-Za-z0-9_-]+)(?:[/?#].*)?$') { $inputText = $Matches[1] } if ($inputText -cnotmatch '^[A-Za-z0-9_-]+$') { throw '請輸入有效的 Bark 裝置金鑰,或 api.day.app 的 Bark 推播網址。' } $key = ConvertTo-SecureString -String $inputText -AsPlainText -Force if (-not (Send-Bark $key 'Bark 測試成功(Claude)。')) { throw 'Bark 未接受測試推播。請核對裝置金鑰及網路連線,再執行一次。' } ConvertFrom-SecureString -SecureString $key | Set-Content -LiteralPath $keyPath -Encoding Ascii -NoNewline Write-Host 'Bark 已接受測試推播。請確認 iPhone 上是否收到通知。'