feat: per-POV summary and edge summary update on scene close
This commit is contained in:
@@ -0,0 +1,269 @@
|
||||
"""Per-POV scene summary and edge summary update on scene close (T27).
|
||||
|
||||
When a scene closes — either auto-detected by the hard-signal classifier
|
||||
in T26 or fired by the manual close button on the drawer — we run a
|
||||
single-shot classifier per present witness that produces three signals
|
||||
in one pass:
|
||||
|
||||
* ``summary`` — a 2-4 sentence per-POV recap of the scene from this
|
||||
witness's perspective. Different from omniscient narration; focuses on
|
||||
what the witness noticed/felt/remembers.
|
||||
* ``knowledge_facts`` — concrete new things this witness learned about
|
||||
the user during the scene. Promoted to the directed edge's
|
||||
``knowledge`` list via ``edge_update``.
|
||||
* ``relationship_summary`` — a 1-2 sentence delta on how the
|
||||
witness's relationship to the user shifted in this scene. v1
|
||||
combines this with the prior edge summary by simple concatenation —
|
||||
the LLM is asked to phrase ``relationship_summary`` as a merge-ready
|
||||
fragment, so the result reads naturally without a second classifier
|
||||
round-trip.
|
||||
|
||||
Phase 1 single-bot only the host bot is summarized; "you" doesn't have
|
||||
a memory store in v1 so per-POV writes for the user are deferred. The
|
||||
:func:`apply_scene_close_summary` driver is intentionally tolerant: if
|
||||
no memories belong to the closed scene it silently skips the rewrite,
|
||||
and a flapping classifier returns the empty default so the close flow
|
||||
keeps moving.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from sqlite3 import Connection
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from chat.eventlog.log import append_and_apply
|
||||
from chat.llm.classify import classify
|
||||
from chat.llm.client import LLMClient
|
||||
|
||||
|
||||
class ScenePOVSummary(BaseModel):
|
||||
"""Classifier output: one witness's view of a closing scene.
|
||||
|
||||
Defaults are an inert no-op so a classifier failure is harmless —
|
||||
callers can apply the result unconditionally and end up not
|
||||
rewriting anything when the model misbehaves.
|
||||
"""
|
||||
|
||||
summary: str = ""
|
||||
knowledge_facts: list[str] = Field(default_factory=list)
|
||||
relationship_summary: str = ""
|
||||
|
||||
|
||||
_SYSTEM_TEMPLATE = (
|
||||
"You are summarizing a roleplay scene from {bot_name}'s point of "
|
||||
"view. Read the dialogue, then output JSON with exactly three "
|
||||
"fields:\n"
|
||||
"- summary: 2-4 sentences, in {bot_name}'s POV, of what happened "
|
||||
"in the scene. This is NOT omniscient narration — focus on what "
|
||||
"{bot_name} noticed, felt, and would remember.\n"
|
||||
"- knowledge_facts: list of NEW factual things {bot_name} learned "
|
||||
"about the user during this scene. Use specific stated content; do "
|
||||
"not infer or interpret. Empty list is fine.\n"
|
||||
"- relationship_summary: a SHORT (1-2 sentence) summary of how "
|
||||
"{bot_name}'s relationship with the user changed or developed in "
|
||||
"this scene. Phrase it so it reads as a continuation of the prior "
|
||||
"summary; the caller will concatenate them.\n\n"
|
||||
"Be specific. Avoid generic phrases."
|
||||
)
|
||||
|
||||
|
||||
def _format_dialogue(dialogue: list[dict]) -> str:
|
||||
if not dialogue:
|
||||
return "(no dialogue)"
|
||||
return "\n".join(
|
||||
f"{turn.get('speaker', '?')}: {turn.get('text', '')}"
|
||||
for turn in dialogue
|
||||
)
|
||||
|
||||
|
||||
async def summarize_scene(
|
||||
client: LLMClient,
|
||||
*,
|
||||
model: str,
|
||||
bot_name: str,
|
||||
bot_persona: str,
|
||||
you_name: str,
|
||||
prior_edge_summary: str,
|
||||
dialogue: list[dict],
|
||||
timeout_s: float = 10.0,
|
||||
) -> ScenePOVSummary:
|
||||
"""Run the per-POV summary classifier for one witness.
|
||||
|
||||
The signature mirrors :func:`compute_state_update` — passing the
|
||||
bot's name and persona as separate fields lets the prompt address
|
||||
the model directly ("YOU are {bot_name}") rather than handing it an
|
||||
opaque id. ``prior_edge_summary`` is included so the classifier can
|
||||
phrase ``relationship_summary`` as an additive fragment.
|
||||
|
||||
Returns the empty default on classifier failure (after one retry)
|
||||
rather than raising, so the close pipeline keeps moving.
|
||||
"""
|
||||
system = _SYSTEM_TEMPLATE.format(bot_name=bot_name)
|
||||
user = (
|
||||
f"YOU are {bot_name}. {bot_persona or '(no persona on file)'}\n"
|
||||
f"USER name: {you_name}\n"
|
||||
f"PRIOR EDGE SUMMARY ({bot_name} -> {you_name}): "
|
||||
f"{prior_edge_summary or '(empty)'}\n\n"
|
||||
f"DIALOGUE:\n{_format_dialogue(dialogue)}\n\n"
|
||||
f"Produce the JSON summary in {bot_name}'s POV."
|
||||
)
|
||||
return await classify(
|
||||
client,
|
||||
model=model,
|
||||
system=system,
|
||||
user=user,
|
||||
schema=ScenePOVSummary,
|
||||
default=ScenePOVSummary(),
|
||||
timeout_s=timeout_s,
|
||||
)
|
||||
|
||||
|
||||
def _read_recent_dialogue(
|
||||
conn: Connection, chat_id: str, *, limit: int = 50
|
||||
) -> list[dict]:
|
||||
"""Pull the last ``limit`` user/assistant turns for ``chat_id``.
|
||||
|
||||
Phase 1 ``user_turn`` / ``assistant_turn`` events don't carry a
|
||||
``scene_id``, so we approximate the scene's transcript by taking
|
||||
the most recent turns of the chat. Superseded and hidden rows are
|
||||
filtered out so regenerated turns (T29) don't bleed into the
|
||||
summary.
|
||||
"""
|
||||
cur = conn.execute(
|
||||
"SELECT kind, payload_json FROM event_log "
|
||||
"WHERE kind IN ('user_turn', 'assistant_turn') "
|
||||
" AND superseded_by IS NULL AND hidden = 0 "
|
||||
"ORDER BY id DESC LIMIT ?",
|
||||
(limit,),
|
||||
)
|
||||
rows = list(reversed(cur.fetchall()))
|
||||
out: list[dict] = []
|
||||
for kind, payload_json in rows:
|
||||
p = json.loads(payload_json)
|
||||
if p.get("chat_id") != chat_id:
|
||||
continue
|
||||
if kind == "user_turn":
|
||||
out.append({"speaker": "you", "text": p.get("prose", "")})
|
||||
else:
|
||||
out.append(
|
||||
{
|
||||
"speaker": p.get("speaker_id", "bot"),
|
||||
"text": p.get("text", ""),
|
||||
}
|
||||
)
|
||||
return out
|
||||
|
||||
|
||||
async def apply_scene_close_summary(
|
||||
conn: Connection,
|
||||
client: LLMClient,
|
||||
*,
|
||||
classifier_model: str,
|
||||
chat_id: str,
|
||||
scene_id: int,
|
||||
host_bot_id: str,
|
||||
timeout_s: float = 10.0,
|
||||
) -> ScenePOVSummary:
|
||||
"""Drive the per-POV summary pipeline after ``scene_closed``.
|
||||
|
||||
Steps (Phase 1, single-bot):
|
||||
1. Gather the closing scene's dialogue from the event_log.
|
||||
2. Run :func:`summarize_scene` for the host bot.
|
||||
3. Rewrite each scene-bound memory's ``pov_summary`` via
|
||||
``manual_edit`` (target_kind ``memory_pov_summary``), capturing
|
||||
the prior value for §6.4 reversibility.
|
||||
4. Update the bot->you edge summary via ``manual_edit`` with the
|
||||
new ``edge_summary`` target_kind. v1 combines prior + new by
|
||||
concatenation — the classifier's ``relationship_summary`` is
|
||||
already phrased as a continuation.
|
||||
5. Append any new knowledge_facts to the same edge via
|
||||
``edge_update``.
|
||||
|
||||
Tolerant of missing pieces: no memories -> skip step 3 silently;
|
||||
no edge row -> skip step 4; empty knowledge_facts -> skip step 5.
|
||||
The classifier's empty default flows through harmlessly.
|
||||
"""
|
||||
# Local imports to keep the module-level surface tight and avoid
|
||||
# any chance of a circular dep through chat.state.*.
|
||||
from chat.state.edges import get_edge
|
||||
from chat.state.entities import get_bot, get_you
|
||||
|
||||
host_bot = get_bot(conn, host_bot_id) or {"name": host_bot_id, "persona": ""}
|
||||
you_entity = get_you(conn) or {"name": "you", "persona": ""}
|
||||
|
||||
dialogue = _read_recent_dialogue(conn, chat_id)
|
||||
|
||||
edge_b2y = get_edge(conn, host_bot_id, "you")
|
||||
prior_summary = (edge_b2y or {}).get("summary", "") or ""
|
||||
|
||||
pov = await summarize_scene(
|
||||
client,
|
||||
model=classifier_model,
|
||||
bot_name=host_bot.get("name", host_bot_id),
|
||||
bot_persona=host_bot.get("persona", "") or "",
|
||||
you_name=you_entity.get("name", "you") or "you",
|
||||
prior_edge_summary=prior_summary,
|
||||
dialogue=dialogue,
|
||||
timeout_s=timeout_s,
|
||||
)
|
||||
|
||||
# Update memories belonging to the closed scene for the host bot.
|
||||
cur = conn.execute(
|
||||
"SELECT id, pov_summary FROM memories "
|
||||
"WHERE scene_id = ? AND owner_id = ?",
|
||||
(scene_id, host_bot_id),
|
||||
)
|
||||
for memory_id, prior_pov in cur.fetchall():
|
||||
if not pov.summary:
|
||||
# Empty default -> skip the memory rewrite; the seeded
|
||||
# per-turn pov_summary stays in place.
|
||||
continue
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="manual_edit",
|
||||
payload={
|
||||
"target_kind": "memory_pov_summary",
|
||||
"target_id": int(memory_id),
|
||||
"prior_value": prior_pov,
|
||||
"new_value": pov.summary,
|
||||
},
|
||||
)
|
||||
|
||||
# Update the bot->you edge summary if we have an edge row and a
|
||||
# non-empty relationship_summary to merge.
|
||||
if edge_b2y is not None and pov.relationship_summary:
|
||||
new_summary = (
|
||||
f"{prior_summary} {pov.relationship_summary}".strip()
|
||||
if prior_summary
|
||||
else pov.relationship_summary
|
||||
)
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="manual_edit",
|
||||
payload={
|
||||
"target_kind": "edge_summary",
|
||||
"target_id": {
|
||||
"source_id": host_bot_id,
|
||||
"target_id": "you",
|
||||
},
|
||||
"prior_value": prior_summary,
|
||||
"new_value": new_summary,
|
||||
},
|
||||
)
|
||||
|
||||
# Append knowledge_facts to the bot->you edge if present.
|
||||
if pov.knowledge_facts:
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="edge_update",
|
||||
payload={
|
||||
"source_id": host_bot_id,
|
||||
"target_id": "you",
|
||||
"chat_id": chat_id,
|
||||
"knowledge_facts": list(pov.knowledge_facts),
|
||||
},
|
||||
)
|
||||
|
||||
return pov
|
||||
Reference in New Issue
Block a user