$ErrorActionPreference = "Stop" function Get-TaipeiNow { try { $tz = [TimeZoneInfo]::FindSystemTimeZoneById("Taipei Standard Time") return [TimeZoneInfo]::ConvertTime([DateTimeOffset]::Now, $tz) } catch { return [DateTimeOffset]::Now } } try { $now = Get-TaipeiNow $promptId = "P{0}" -f $now.ToString("yyyyMMdd-HHmmss") $promptRaw = $env:USER_PROMPT if ($null -eq $promptRaw) { $promptRaw = "" } # 截断过长的 prompt(避免意外记录展开的 #context) if ($promptRaw.Length -gt 20000) { $promptRaw = $promptRaw.Substring(0, 5000) + "`n[TRUNCATED: prompt too long; possible expanded #context]" } $summary = ($promptRaw -replace "\s+", " ").Trim() if ($summary.Length -gt 120) { $summary = $summary.Substring(0, 120) + "…" } if ([string]::IsNullOrWhiteSpace($summary)) { $summary = "(empty prompt)" } # CHANGE [2026-02-15] intent: 简化为每次直接写独立文件到 prompt_logs/,不再维护 prompt_log.md 中间文件 $logDir = Join-Path "docs" "audit" "prompt_logs" $null = New-Item -ItemType Directory -Force -Path $logDir 2>$null $filename = "prompt_log_{0}.md" -f $now.ToString("yyyyMMdd_HHmmss") $targetLog = Join-Path $logDir $filename $timestamp = $now.ToString("yyyy-MM-dd HH:mm:ss zzz") $entry = @" - [$promptId] $timestamp - summary: $summary - prompt: ``````text $promptRaw `````` "@ Set-Content -Path $targetLog -Value $entry -Encoding UTF8 # 保存 last prompt id 供下游 /audit 溯源 $stateDir = ".kiro" $null = New-Item -ItemType Directory -Force -Path $stateDir 2>$null $lastPrompt = @{ prompt_id = $promptId at = $now.ToString("o") } | ConvertTo-Json -Depth 4 Set-Content -Path (Join-Path $stateDir ".last_prompt_id.json") -Value $lastPrompt -Encoding UTF8 exit 0 } catch { # 不阻塞 prompt 提交 exit 0 }