diff --git a/core/bridge.py b/core/bridge.py index 85e481c..c8dd263 100644 --- a/core/bridge.py +++ b/core/bridge.py @@ -1177,6 +1177,44 @@ class MemoryCompanionBridge: async def get_emotion_trace(self, trace_id: str, *, limit: int = 100) -> list[dict[str, Any]]: return await self._plugin.store.get_emotion_trace(trace_id, limit=limit) + async def get_emotion_trace_diagnostic( + self, + trace_id: str, + requester_context: dict[str, Any], + *, + limit: int = 100, + ) -> dict[str, Any]: + context = requester_context if type(requester_context) is dict else {} + if context.get("is_admin") is not True: + return {"state": "forbidden", "read_only": True, "items": [], "error_code": "admin_required"} + items = await self._plugin.store.get_emotion_trace_diagnostic( + trace_id, + bot_id=clean_text(context.get("bot_id"), 160), + scope=clean_text(context.get("scope"), 24), + session_id=clean_text(context.get("session_id"), 220), + limit=max(1, min(100, int(limit or 100))), + ) + return {"state": "ready", "read_only": True, "items": items} + + async def get_emotion_trace_summary( + self, + requester_context: dict[str, Any], + *, + cursor: str = "", + limit: int = 20, + ) -> dict[str, Any]: + context = requester_context if type(requester_context) is dict else {} + if context.get("is_admin") is not True: + return {"state": "forbidden", "read_only": True, "items": [], "next_cursor": "", "error_code": "admin_required"} + result = await self._plugin.store.get_emotion_trace_summary( + bot_id=clean_text(context.get("bot_id"), 160), + scope=clean_text(context.get("scope"), 24), + session_id=clean_text(context.get("session_id"), 220), + cursor=clean_text(cursor, 20), + limit=max(1, min(100, int(limit or 20))), + ) + return {"state": "ready", "read_only": True, **result} + async def search_open_loops(self, *, session_id: str = "", limit: int = 3) -> list[dict[str, Any]]: """Search for unresolved open-loop / promise memories for proactive companionship.""" return await self._plugin.bridge_search_open_loops(session_id=session_id, limit=limit) diff --git a/core/store.py b/core/store.py index 842f1b4..b2fb60a 100644 --- a/core/store.py +++ b/core/store.py @@ -1,6 +1,7 @@ from __future__ import annotations import asyncio +import hashlib from contextlib import closing, contextmanager from copy import deepcopy import re @@ -5638,6 +5639,166 @@ class MemoryStore: ).fetchall() return [self._emotion_event_row(row) for row in rows] + async def get_emotion_trace_diagnostic( + self, + trace_id: str, + *, + bot_id: str = "", + scope: str = "", + session_id: str = "", + limit: int = 100, + ) -> list[dict[str, Any]]: + return await self._run_recoverable_database_operation( + self._get_emotion_trace_diagnostic_sync, trace_id, bot_id, scope, session_id, limit + ) + + def _get_emotion_trace_diagnostic_sync( + self, + trace_id: str, + bot_id: str, + scope: str, + session_id: str, + limit: int, + ) -> list[dict[str, Any]]: + trace = clean_text(trace_id, 96) + if not trace: + return [] + clauses = ["e.trace_id=?"] + params: list[Any] = [trace] + for column, value, size in (("bot_id", bot_id, 160), ("scope", scope, 24), ("session_id", session_id, 220)): + cleaned = clean_text(value, size) + if cleaned: + clauses.append(f"e.{column}=?") + params.append(cleaned) + params.append(max(1, min(100, int(limit or 100)))) + with self._lock: + rows = self._conn.execute( + f"SELECT e.* FROM emotion_events e WHERE {' AND '.join(clauses)} ORDER BY e.revision ASC LIMIT ?", + params, + ).fetchall() + result: list[dict[str, Any]] = [] + for row in rows: + event = self._emotion_event_row(row) + deliveries = self._conn.execute( + """ + SELECT consumer_id, attempts, first_delivered_at, last_delivered_at, acked_at + FROM emotion_event_deliveries + WHERE event_id=? AND revision=? + ORDER BY consumer_id ASC LIMIT 20 + """, + (event["event_id"], event["revision"]), + ).fetchall() + result.append(self._emotion_diagnostic_projection(event, deliveries)) + return result + + async def get_emotion_trace_summary( + self, + *, + bot_id: str = "", + scope: str = "", + session_id: str = "", + cursor: str = "", + limit: int = 20, + ) -> dict[str, Any]: + return await self._run_recoverable_database_operation( + self._get_emotion_trace_summary_sync, bot_id, scope, session_id, cursor, limit + ) + + def _get_emotion_trace_summary_sync( + self, + bot_id: str, + scope: str, + session_id: str, + cursor: str, + limit: int, + ) -> dict[str, Any]: + clauses = ["1=1"] + params: list[Any] = [] + for column, value, size in (("bot_id", bot_id, 160), ("scope", scope, 24), ("session_id", session_id, 220)): + cleaned = clean_text(value, size) + if cleaned: + clauses.append(f"e.{column}=?") + params.append(cleaned) + try: + offset = max(0, min(100000, int(cursor or 0))) + except (TypeError, ValueError): + offset = 0 + bounded = max(1, min(100, int(limit or 20))) + params.extend((bounded + 1, offset)) + with self._lock: + rows = self._conn.execute( + f""" + SELECT e.* FROM emotion_events e + JOIN (SELECT event_id, MAX(revision) revision FROM emotion_events GROUP BY event_id) latest + ON latest.event_id=e.event_id AND latest.revision=e.revision + WHERE {' AND '.join(clauses)} + ORDER BY e.occurred_at DESC, e.event_id DESC + LIMIT ? OFFSET ? + """, + params, + ).fetchall() + page = [self._emotion_event_row(row) for row in rows[:bounded]] + return { + "items": [{ + "trace_id": clean_text(item.get("trace_id"), 96), + "event_id": clean_text(item.get("event_id"), 96), + "revision": int(item.get("revision") or 1), + "event_type": clean_text(item.get("event_type"), 48), + "status": clean_text(item.get("status"), 24), + "occurred_at": clean_text(item.get("occurred_at"), 48), + "session_hash": self._diagnostic_hash(item.get("session_id")), + } for item in page], + "next_cursor": str(offset + bounded) if len(rows) > bounded else "", + "has_more": len(rows) > bounded, + } + + @classmethod + def _emotion_diagnostic_projection(cls, event: dict[str, Any], deliveries: list[sqlite3.Row]) -> dict[str, Any]: + return { + "schema_version": "emotion_trace_diagnostic.v1", + "event_id": clean_text(event.get("event_id"), 96), + "trace_id": clean_text(event.get("trace_id"), 96), + "revision": int(event.get("revision") or 1), + "producer_plugin": clean_text(event.get("producer_plugin"), 80), + "origin_kind": clean_text(event.get("origin_kind"), 40), + "event_type": clean_text(event.get("event_type"), 48), + "intensity": float(event.get("intensity") or 0.0), + "confidence": float(event.get("confidence") or 0.0), + "status": clean_text(event.get("status"), 24), + "source_rule": clean_text(event.get("source_rule"), 80), + "occurred_at": clean_text(event.get("occurred_at"), 48), + "applied_interaction": clean_text(event.get("applied_interaction"), 32), + "applied_energy_delta": float(event.get("applied_energy_delta") or 0.0), + "correction_of": clean_text(event.get("correction_of"), 96), + "actor": cls._diagnostic_ref(event.get("actor_ref")), + "target": cls._diagnostic_ref(event.get("target_ref")), + "quoted_target": cls._diagnostic_ref(event.get("quoted_target_ref")), + "scope": clean_text(event.get("scope"), 24), + "session_hash": cls._diagnostic_hash(event.get("session_id")), + "deliveries": [{ + "consumer_id": clean_text(row["consumer_id"], 80), + "attempts": max(0, int(row["attempts"] or 0)), + "first_delivered_at": clean_text(row["first_delivered_at"], 48), + "last_delivered_at": clean_text(row["last_delivered_at"], 48), + "acked": bool(row["acked_at"]), + "acked_at": clean_text(row["acked_at"], 48), + } for row in deliveries], + } + + @classmethod + def _diagnostic_ref(cls, value: Any) -> dict[str, str]: + source = value if isinstance(value, dict) else {} + return { + "kind": clean_text(source.get("kind"), 24), + "role": clean_text(source.get("role"), 40), + "id_hash": cls._diagnostic_hash(source.get("id")), + } + + @staticmethod + def _diagnostic_hash(value: Any) -> str: + cleaned = clean_text(value, 220) + return hashlib.sha256(cleaned.encode("utf-8", errors="ignore")).hexdigest()[:12] if cleaned else "" + async def list_emotion_events( self, *, diff --git a/page_api.py b/page_api.py index d1da978..a072ffa 100644 --- a/page_api.py +++ b/page_api.py @@ -79,6 +79,8 @@ class PluginPageApi: ("/config/module/update", self.config_module_update, ["POST"], "MemoryCompanion Page config module update"), ("/retrieval/config/update", self.retrieval_config_update, ["POST"], "MemoryCompanion Page retrieval config update"), ("/operations/diagnostics", self.operations_diagnostics, ["GET"], "MemoryCompanion operations diagnostics"), + ("/emotion/trace", self.emotion_trace, ["GET"], "MemoryCompanion redacted emotion trace"), + ("/emotion/traces", self.emotion_traces, ["GET"], "MemoryCompanion redacted emotion trace list"), ("/coordination/status", self.coordination_status, ["GET"], "MemoryCompanion safe coordination status"), ("/operations/preset", self.operations_preset, ["GET", "POST"], "MemoryCompanion operations preset"), ("/data/export", self.data_export, ["POST"], "MemoryCompanion portable data export"), @@ -195,6 +197,39 @@ class PluginPageApi: except Exception as exc: return self._err(f"运维诊断失败: {exc}", 500) + async def emotion_trace(self): + trace_id = clean_text(request.args.get("trace_id", ""), 96) + if not trace_id: + return self._err("缺少 trace_id", 400) + context = { + "is_admin": True, + "bot_id": clean_text(request.args.get("bot_id", ""), 160), + "scope": clean_text(request.args.get("scope", ""), 24), + "session_id": clean_text(request.args.get("session_id", ""), 220), + } + bridge = getattr(self.plugin, "memory_companion", None) + getter = getattr(bridge, "get_emotion_trace_diagnostic", None) + if not callable(getter): + return self._ok({"result": {"state": "degraded", "read_only": True, "items": [], "error_code": "bridge_method_unavailable"}}) + return self._ok({"result": await getter(trace_id, context, limit=100)}) + + async def emotion_traces(self): + context = { + "is_admin": True, + "bot_id": clean_text(request.args.get("bot_id", ""), 160), + "scope": clean_text(request.args.get("scope", ""), 24), + "session_id": clean_text(request.args.get("session_id", ""), 220), + } + bridge = getattr(self.plugin, "memory_companion", None) + getter = getattr(bridge, "get_emotion_trace_summary", None) + if not callable(getter): + return self._ok({"result": {"state": "degraded", "read_only": True, "items": [], "error_code": "bridge_method_unavailable"}}) + return self._ok({"result": await getter( + context, + cursor=clean_text(request.args.get("cursor", ""), 20), + limit=max(1, min(100, self._int(request.args.get("limit", "20"), 20))), + )}) + async def coordination_status(self): """Expose a fixed, read-only coordination projection.""" try: diff --git a/pages/记忆面板/app.js b/pages/记忆面板/app.js index d8cef87..44f8313 100644 --- a/pages/记忆面板/app.js +++ b/pages/记忆面板/app.js @@ -6231,11 +6231,32 @@ async function loadPersonaState() { let data; try { data = await apiGet("/persona-state"); + try { + const traces = await apiGet("/emotion/traces?scope=private&limit=20"); + data.emotion_trace_diagnostics = traces?.result || traces || {}; + } catch (_) { + data.emotion_trace_diagnostics = { state: "degraded", items: [], error_code: "trace_summary_unavailable" }; + } } catch (err) { panel.innerHTML = `
暂无可展示的脱敏 trace。
'} +选择一条 trace 查看事件 revision 与投递确认状态。+