61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
||
"""prompt_audit_log — 每次提交 prompt 时生成独立日志文件。
|
||
|
||
替代原 PowerShell 版本,避免 Windows PowerShell 5.1 解析器 bug。
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
import sys
|
||
from datetime import datetime, timezone, timedelta
|
||
|
||
TZ_TAIPEI = timezone(timedelta(hours=8))
|
||
|
||
|
||
def main():
|
||
now = datetime.now(TZ_TAIPEI)
|
||
prompt_id = f"P{now.strftime('%Y%m%d-%H%M%S')}"
|
||
prompt_raw = os.environ.get("USER_PROMPT", "")
|
||
|
||
# 截断过长的 prompt(避免展开的 #context 占用过多空间)
|
||
if len(prompt_raw) > 20000:
|
||
prompt_raw = prompt_raw[:5000] + "\n[TRUNCATED: prompt too long; possible expanded #context]"
|
||
|
||
summary = " ".join(prompt_raw.split()).strip()
|
||
if len(summary) > 120:
|
||
summary = summary[:120] + "…"
|
||
if not summary:
|
||
summary = "(empty prompt)"
|
||
|
||
# 写独立日志文件
|
||
log_dir = os.path.join("docs", "audit", "prompt_logs")
|
||
os.makedirs(log_dir, exist_ok=True)
|
||
|
||
filename = f"prompt_log_{now.strftime('%Y%m%d_%H%M%S')}.md"
|
||
target = os.path.join(log_dir, filename)
|
||
|
||
timestamp = now.strftime("%Y-%m-%d %H:%M:%S %z")
|
||
entry = f"""- [{prompt_id}] {timestamp}
|
||
- summary: {summary}
|
||
- prompt:
|
||
```text
|
||
{prompt_raw}
|
||
```
|
||
"""
|
||
with open(target, "w", encoding="utf-8") as f:
|
||
f.write(entry)
|
||
|
||
# 保存 last prompt id 供 /audit 溯源
|
||
os.makedirs(".kiro", exist_ok=True)
|
||
last_prompt = {"prompt_id": prompt_id, "at": now.isoformat()}
|
||
with open(os.path.join(".kiro", ".last_prompt_id.json"), "w", encoding="utf-8") as f:
|
||
json.dump(last_prompt, f, indent=2, ensure_ascii=False)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
try:
|
||
main()
|
||
except Exception:
|
||
# 不阻塞 prompt 提交
|
||
pass
|