feat: drawer guest add/remove + render
This commit is contained in:
+221
-1
@@ -32,9 +32,11 @@ from fastapi.responses import HTMLResponse
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
from chat.eventlog.log import append_and_apply
|
||||
from chat.services.relationship_seed import seed_inter_bot_edges
|
||||
from chat.services.scene_summarize import apply_scene_close_summary
|
||||
from chat.state.edges import get_edge
|
||||
from chat.state.entities import get_bot, get_you
|
||||
from chat.state.entities import get_bot, get_you, list_bots
|
||||
from chat.state.group_node import get_group_node
|
||||
from chat.state.memory import get_pinned
|
||||
from chat.state.world import active_scene, get_activity, get_chat, get_container
|
||||
from chat.web.bots import get_conn
|
||||
@@ -78,6 +80,32 @@ async def drawer(chat_id: str, request: Request, conn=Depends(get_conn)):
|
||||
edge_b2y = get_edge(conn, chat["host_bot_id"], "you")
|
||||
edge_y2b = get_edge(conn, "you", chat["host_bot_id"])
|
||||
|
||||
# T42: guest + group context. Empty defaults keep the template happy
|
||||
# when no guest is present (the relevant sections render conditionally).
|
||||
guest_bot = None
|
||||
guest_activity = None
|
||||
edge_h2g = None
|
||||
edge_g2h = None
|
||||
edge_y2g = None
|
||||
edge_g2y = None
|
||||
available_guests: list[dict] = []
|
||||
group_node = None
|
||||
if chat.get("guest_bot_id"):
|
||||
guest_bot_id = chat["guest_bot_id"]
|
||||
guest_bot = get_bot(conn, guest_bot_id)
|
||||
guest_activity = get_activity(conn, guest_bot_id)
|
||||
edge_h2g = get_edge(conn, chat["host_bot_id"], guest_bot_id)
|
||||
edge_g2h = get_edge(conn, guest_bot_id, chat["host_bot_id"])
|
||||
edge_y2g = get_edge(conn, "you", guest_bot_id)
|
||||
edge_g2y = get_edge(conn, guest_bot_id, "you")
|
||||
else:
|
||||
# Candidates for the "Add guest" dropdown — every authored bot
|
||||
# except the host (and "you", which is implicit, never a bot row).
|
||||
available_guests = [
|
||||
b for b in list_bots(conn) if b["id"] != chat["host_bot_id"]
|
||||
]
|
||||
group_node = get_group_node(conn, chat_id)
|
||||
|
||||
# Recent memories from host's POV (witness_host = 1), most recent first.
|
||||
# Raw query keeps this read self-contained — no projector helper exposes
|
||||
# "latest N for an owner" yet and the drawer is the only consumer.
|
||||
@@ -117,6 +145,14 @@ async def drawer(chat_id: str, request: Request, conn=Depends(get_conn)):
|
||||
"bot_activity": bot_activity,
|
||||
"edge_b2y": edge_b2y,
|
||||
"edge_y2b": edge_y2b,
|
||||
"guest_bot": guest_bot,
|
||||
"guest_activity": guest_activity,
|
||||
"edge_h2g": edge_h2g,
|
||||
"edge_g2h": edge_g2h,
|
||||
"edge_y2g": edge_y2g,
|
||||
"edge_g2y": edge_g2y,
|
||||
"available_guests": available_guests,
|
||||
"group_node": group_node,
|
||||
"recent_memories": recent_memories,
|
||||
"pinned": pinned,
|
||||
"pin_cap": PIN_CAP,
|
||||
@@ -304,3 +340,187 @@ async def toggle_memory_pin(
|
||||
},
|
||||
)
|
||||
return await drawer(chat_id, request, conn)
|
||||
|
||||
|
||||
# --- T42 guest add/remove -------------------------------------------------
|
||||
#
|
||||
# Adding a guest fans out into up to four events: a ``guest_added`` to flip
|
||||
# ``chats.guest_bot_id``, two ``edge_update`` events seeded from the
|
||||
# user-supplied prose (skipped when the prose is empty / the seed comes back
|
||||
# default), and a ``group_node_initialized`` if no row exists yet — three
|
||||
# entities now share the chat so the §8.4 group node becomes meaningful.
|
||||
#
|
||||
# Removing a guest first emits ``scene_closed`` for the active scene (so any
|
||||
# host -> you scene closes cleanly with the guest still in scope) before
|
||||
# clearing the guest_bot_id; per spec the next user message implicitly opens
|
||||
# a fresh you+host scene via Phase 1's mid-chat reset behavior.
|
||||
|
||||
|
||||
def _seed_is_default(seed) -> bool:
|
||||
"""Treat a seed as a no-op when both summaries are empty AND both
|
||||
delta pairs are zero AND both fact lists are empty.
|
||||
"""
|
||||
return (
|
||||
not seed.a_to_b_summary
|
||||
and not seed.b_to_a_summary
|
||||
and seed.a_to_b_affinity_delta == 0
|
||||
and seed.a_to_b_trust_delta == 0
|
||||
and seed.b_to_a_affinity_delta == 0
|
||||
and seed.b_to_a_trust_delta == 0
|
||||
and not seed.a_to_b_knowledge_facts
|
||||
and not seed.b_to_a_knowledge_facts
|
||||
)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/chats/{chat_id}/drawer/guest/add",
|
||||
response_class=HTMLResponse,
|
||||
)
|
||||
async def add_guest(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
guest_bot_id: str = Form(...),
|
||||
relationship_prose: str = Form(""),
|
||||
conn=Depends(get_conn),
|
||||
client=Depends(get_llm_client),
|
||||
):
|
||||
chat = get_chat(conn, chat_id)
|
||||
if chat is None:
|
||||
raise HTTPException(status_code=404, detail=f"chat not found: {chat_id}")
|
||||
|
||||
if chat.get("guest_bot_id") is not None:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="a guest is already present in this chat",
|
||||
)
|
||||
|
||||
if guest_bot_id == chat["host_bot_id"]:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="guest must differ from host"
|
||||
)
|
||||
|
||||
guest_bot = get_bot(conn, guest_bot_id)
|
||||
if guest_bot is None:
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f"guest bot not found: {guest_bot_id}"
|
||||
)
|
||||
|
||||
host_bot = get_bot(conn, chat["host_bot_id"])
|
||||
if host_bot is None:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=f"host bot not found: {chat['host_bot_id']}",
|
||||
)
|
||||
|
||||
settings = request.app.state.settings
|
||||
seed = await seed_inter_bot_edges(
|
||||
client,
|
||||
classifier_model=settings.classifier_model,
|
||||
bot_a_id=chat["host_bot_id"],
|
||||
bot_a_name=host_bot["name"],
|
||||
bot_b_id=guest_bot_id,
|
||||
bot_b_name=guest_bot["name"],
|
||||
relationship_prose=relationship_prose,
|
||||
timeout_s=settings.classifier_timeout_s,
|
||||
)
|
||||
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="guest_added",
|
||||
payload={"chat_id": chat_id, "guest_bot_id": guest_bot_id},
|
||||
)
|
||||
|
||||
# Emit edge_update only when the seed carries content. Empty prose
|
||||
# short-circuits inside ``seed_inter_bot_edges`` to a default seed,
|
||||
# so this skips the two extra log entries on the no-prose path.
|
||||
# NOTE: ``_apply_edge_update`` does not accept a ``summary`` field —
|
||||
# per-direction summary is set via the per-pov scene-close path
|
||||
# (T27), not direct edge_update. We therefore drop seed.*_summary
|
||||
# here; the deltas + knowledge_facts are what materializes.
|
||||
if not _seed_is_default(seed):
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="edge_update",
|
||||
payload={
|
||||
"source_id": chat["host_bot_id"],
|
||||
"target_id": guest_bot_id,
|
||||
"chat_id": chat_id,
|
||||
"affinity_delta": seed.a_to_b_affinity_delta,
|
||||
"trust_delta": seed.a_to_b_trust_delta,
|
||||
"knowledge_facts": seed.a_to_b_knowledge_facts,
|
||||
"last_interaction_at": chat.get("time"),
|
||||
"last_interaction_chat_id": chat_id,
|
||||
},
|
||||
)
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="edge_update",
|
||||
payload={
|
||||
"source_id": guest_bot_id,
|
||||
"target_id": chat["host_bot_id"],
|
||||
"chat_id": chat_id,
|
||||
"affinity_delta": seed.b_to_a_affinity_delta,
|
||||
"trust_delta": seed.b_to_a_trust_delta,
|
||||
"knowledge_facts": seed.b_to_a_knowledge_facts,
|
||||
"last_interaction_at": chat.get("time"),
|
||||
"last_interaction_chat_id": chat_id,
|
||||
},
|
||||
)
|
||||
|
||||
# Three entities now share the chat (you, host, guest) — initialize
|
||||
# the group node row if Wave 1's reader doesn't see one yet.
|
||||
if get_group_node(conn, chat_id) is None:
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="group_node_initialized",
|
||||
payload={
|
||||
"chat_id": chat_id,
|
||||
"members": ["you", chat["host_bot_id"], guest_bot_id],
|
||||
"summary": "",
|
||||
"dynamic": "",
|
||||
"threads": [],
|
||||
},
|
||||
)
|
||||
|
||||
return await drawer(chat_id, request, conn)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/chats/{chat_id}/drawer/guest/remove",
|
||||
response_class=HTMLResponse,
|
||||
)
|
||||
async def remove_guest(
|
||||
chat_id: str,
|
||||
request: Request,
|
||||
conn=Depends(get_conn),
|
||||
):
|
||||
chat = get_chat(conn, chat_id)
|
||||
if chat is None:
|
||||
raise HTTPException(status_code=404, detail=f"chat not found: {chat_id}")
|
||||
|
||||
if chat.get("guest_bot_id") is None:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="no guest present in this chat"
|
||||
)
|
||||
|
||||
# Close the active scene (if any) before flipping guest_bot_id so
|
||||
# the scene record carries the guest as a participant.
|
||||
scene = active_scene(conn, chat_id)
|
||||
if scene is not None:
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="scene_closed",
|
||||
payload={
|
||||
"scene_id": scene["id"],
|
||||
"ended_at": chat.get("time"),
|
||||
"significance": 0,
|
||||
},
|
||||
)
|
||||
|
||||
append_and_apply(
|
||||
conn,
|
||||
kind="guest_removed",
|
||||
payload={"chat_id": chat_id},
|
||||
)
|
||||
|
||||
return await drawer(chat_id, request, conn)
|
||||
|
||||
Reference in New Issue
Block a user