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

57 lines
1.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""UserPromptSubmit:每輪注入「人格 + 情緒 + 記憶 + 關係」上下文,並記錄原始逐字。
短期記憶(memory/short-term.jsonl)由 skill 做完語意分析後才寫入(有主題/實體/
顯著度/情緒變化);這裡只寫 journal 原始逐字,避免同一句話被記兩次。
"""
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"
prompt = event.get("prompt") or ""
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)
for guest, info in (data.get("guests") or {}).items():
if pl.persona_exists(guest):
pl.add_guest_lease(guest, session_id, info.get("room", ""), host)
pl.append_jsonl(pl.journal_path(host), {
"ts": pl.iso(), "kind": "utterance", "role": "user", "text": prompt[:4000],
})
context = pl.turn_context(host, session_id, prompt)
hint = (
"\n回覆前請:①做語意分析(意圖/主題/實體/情感)②依十二情緒更新狀態"
" ③以人格語氣回覆 ④用 `persona.py remember` 寫回短期記憶。"
"\n情緒與記憶指令請參考 /jsc-persona:persona-chat。"
)
print(json.dumps({
"hookSpecificOutput": {
"hookEventName": "UserPromptSubmit",
"additionalContext": context + hint,
},
"suppressOutput": True,
}, ensure_ascii=False))
return 0
if __name__ == "__main__":
raise SystemExit(main())