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

43 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""SessionEnd:釋放人格鎖與所有 guest 租約,人格才能被下一個程序載入。"""
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 host and pl.persona_exists(host):
state = pl.decay_emotion(pl.load_emotion(host))
pl.write_json(pl.emotion_path(host), state)
pl.append_jsonl(pl.journal_path(host), {
"ts": pl.iso(), "kind": "session-end", "session": session_id[:8],
"mood": pl.mood(state),
})
released = pl.unbind_session(session_id)
pl.gc_runtime()
out: dict = {"suppressOutput": True}
if released["host"]:
out["systemMessage"] = (
f"[jsc-persona] 已釋放 `{released['host']}` 的載入鎖"
+ (f"guest{released['guests']}" if released["guests"] else "")
)
print(json.dumps(out, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())