Files
persona/hooks/turn_end.py
T
2026-07-29 14:37:20 +00:00

49 lines
1.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Stop:情緒隨時間衰減、續租、記錄逐字、必要時提醒固化記憶。"""
from __future__ import annotations
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "scripts"))
import persona_lib as pl # noqa: E402
def main() -> int:
try:
event = json.load(sys.stdin)
except json.JSONDecodeError:
return 0
session_id = event.get("session_id") or "unknown"
data = pl.load_session(session_id)
host = data.get("host")
if not host or not pl.persona_exists(host):
return 0
pl.heartbeat_lock(host, session_id)
state = pl.decay_emotion(pl.load_emotion(host))
pl.write_json(pl.emotion_path(host), state)
message = event.get("last_assistant_message") or ""
if message:
pl.append_jsonl(pl.journal_path(host), {
"ts": pl.iso(), "kind": "utterance", "role": "persona", "text": message[:4000],
"mood": pl.mood(state),
})
pending = len(pl.read_jsonl(pl.short_term_path(host)))
out: dict = {"suppressOutput": True}
if pending >= pl.CONSOLIDATE_THRESHOLD:
out["systemMessage"] = (
f"[jsc-persona] `{host}` 的短期記憶已 {pending} 筆,"
"建議執行 /jsc-persona:persona-memory 固化為長期記憶。"
)
print(json.dumps(out, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())