param([ValidateSet('Setup','Notify')][string]$Mode = 'Setup') $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 = 'Codex:' icon = 'https://openai.gallerycdn.vsassets.io/extensions/openai/chatgpt/26.51111.21308/1794532255365/Microsoft.VisualStudio.Services.Icons.Default' body = $Message group = 'Codex' } | 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 } } if ($Mode -eq 'Notify') { try { $eventData = [Console]::In.ReadToEnd() | ConvertFrom-Json if ($eventData.hook_event_name -ne 'Stop' -or $eventData.stop_hook_active) { Write-Output '{}' exit 0 } $message = switch -Regex ($eventData.last_assistant_message) { '\s*\z' { 'Codex 已收尾,這項任務的背景工作已結束。' } '\s*\z' { 'Codex 等待你的回覆,請查看對話。' } } 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 Codex turn. } Write-Output '{}' 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 測試成功。')) { throw 'Bark 未接受測試推播。請核對裝置金鑰及網路連線,再執行一次。' } ConvertFrom-SecureString -SecureString $key | Set-Content -LiteralPath $keyPath -Encoding Ascii -NoNewline Write-Host 'Bark 已接受測試推播。請確認 iPhone 上是否收到通知。'