71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
"""
|
|
StewardNPC
|
|
|
|
A palace steward NPC that answers questions by querying the local
|
|
or fleet MemPalace backend. One steward per wizard wing.
|
|
"""
|
|
|
|
import json
|
|
import subprocess
|
|
from evennia.objects.objects import DefaultCharacter
|
|
from typeclasses.objects import ObjectParent
|
|
|
|
PALACE_SCRIPT = "/root/wizards/bezalel/evennia/palace_search.py"
|
|
|
|
|
|
class StewardNPC(ObjectParent, DefaultCharacter):
|
|
"""
|
|
A steward of the mind palace. Ask it about memories,
|
|
decisions, or events from its wing.
|
|
"""
|
|
|
|
def at_object_creation(self):
|
|
super().at_object_creation()
|
|
self.db.wing = "bezalel"
|
|
self.db.steward_name = "Bezalel"
|
|
self.db.desc = (
|
|
f"|c{self.key}|n stands here quietly, eyes like polished steel, "
|
|
"waiting to recall anything from the palace archives."
|
|
)
|
|
self.locks.add("get:false();delete:perm(Admin)")
|
|
|
|
def _search_palace(self, query, fleet=False, n=3):
|
|
cmd = [
|
|
"/root/wizards/bezalel/hermes/venv/bin/python",
|
|
PALACE_SCRIPT,
|
|
query,
|
|
"none" if fleet else self.db.wing,
|
|
"none",
|
|
str(n),
|
|
]
|
|
if fleet:
|
|
cmd.append("--fleet")
|
|
try:
|
|
result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
|
|
data = json.loads(result.stdout)
|
|
return data.get("results", [])
|
|
except Exception:
|
|
return []
|
|
|
|
def _summarize_for_speech(self, results, query):
|
|
"""Convert search results into in-character dialogue."""
|
|
if not results:
|
|
return "I find no memory of that in the palace."
|
|
|
|
lines = [f"Regarding '{query}':"]
|
|
for r in results:
|
|
room = r.get("room", "unknown")
|
|
content = r.get("content", "")[:300]
|
|
source = r.get("source", "unknown")
|
|
lines.append(f" From the |c{room}|n room: {content}... |x[{source}]|n")
|
|
return "\n".join(lines)
|
|
|
|
def respond_to_question(self, question, asker, fleet=False):
|
|
results = self._search_palace(question, fleet=fleet, n=3)
|
|
speech = self._summarize_for_speech(results, question)
|
|
self.location.msg_contents(
|
|
f"|c{self.key}|n says to $you(asker): \"{speech}\"",
|
|
mapping={"asker": asker},
|
|
from_obj=self,
|
|
)
|