feat(relationship): add read-only phase projection
这个提交存在于:
父节点
fc55d73fca
当前提交
81faa8c508
共有 3 个文件被更改,包括 112 次插入 和 0 次删除
|
|
@ -923,6 +923,33 @@ class MemoryCompanionBridge:
|
|||
ctx = normalizer(payload) if callable(normalizer) else SessionContext(**payload)
|
||||
return getter(ctx)
|
||||
|
||||
def peek_relationship_phase(
|
||||
self,
|
||||
*,
|
||||
session_id: str = "",
|
||||
scope: str = "private",
|
||||
platform: str = "",
|
||||
user_id: str = "",
|
||||
group_id: str = "",
|
||||
bot_id: str = "",
|
||||
) -> dict[str, Any]:
|
||||
"""Read an existing phase projection without creating default state."""
|
||||
getter = getattr(self._plugin, "_peek_relationship_phase", None)
|
||||
if not callable(getter):
|
||||
return {"observed": False, "phase": "unknown", "momentum_band": "unknown"}
|
||||
normalizer = getattr(self._plugin, "session_context_from_bridge", None)
|
||||
payload = {
|
||||
"session_id": session_id,
|
||||
"scope": scope,
|
||||
"platform": platform,
|
||||
"user_id": user_id,
|
||||
"group_id": group_id,
|
||||
"bot_id": bot_id,
|
||||
}
|
||||
ctx = normalizer(payload) if callable(normalizer) else SessionContext(**payload)
|
||||
result = getter(ctx)
|
||||
return result if isinstance(result, dict) else {"observed": False, "phase": "unknown", "momentum_band": "unknown"}
|
||||
|
||||
def get_recent_emotional_state(self) -> dict[str, Any]:
|
||||
"""Return a summary of recent emotional events across ALL sessions.
|
||||
|
||||
|
|
|
|||
|
|
@ -8489,6 +8489,55 @@ class MemoryCompanionService:
|
|||
state["_identity"] = identity
|
||||
return state
|
||||
|
||||
def _peek_relationship_phase(self, ctx: SessionContext) -> dict[str, Any]:
|
||||
"""Project an existing relationship phase without creating or saving it."""
|
||||
identity = self._phase_identity(ctx)
|
||||
key = self._phase_key(ctx)
|
||||
state = self._relationship_phase_state.get(key)
|
||||
if not isinstance(state, dict) and not identity["bot_id"]:
|
||||
matches = [
|
||||
candidate
|
||||
for candidate in self._relationship_phase_state.values()
|
||||
if isinstance(candidate, dict)
|
||||
and isinstance(candidate.get("_identity"), dict)
|
||||
and all(
|
||||
candidate["_identity"].get(field, "") == identity[field]
|
||||
for field in ("platform", "scope", "target_id", "member_id")
|
||||
)
|
||||
]
|
||||
if len(matches) == 1:
|
||||
state = matches[0]
|
||||
if not isinstance(state, dict):
|
||||
legacy_key = clean_text(f"{ctx.scope}:{ctx.current_target_id or ctx.session_id}", 120)
|
||||
legacy_state = self._relationship_phase_state.get(legacy_key)
|
||||
if isinstance(legacy_state, dict):
|
||||
state = legacy_state
|
||||
if not isinstance(state, dict):
|
||||
return {"observed": False, "phase": "unknown", "momentum_band": "unknown"}
|
||||
phase = clean_text(state.get("phase"), 40)
|
||||
if phase not in self._PHASES:
|
||||
phase = "unknown"
|
||||
try:
|
||||
momentum = float(state.get("momentum"))
|
||||
except (TypeError, ValueError):
|
||||
momentum = 0.0
|
||||
if momentum >= 0.08:
|
||||
momentum_band = "rising"
|
||||
elif momentum <= -0.08:
|
||||
momentum_band = "cooling"
|
||||
else:
|
||||
momentum_band = "steady"
|
||||
try:
|
||||
touch_count = int(state.get("touch_count") or 0)
|
||||
except (TypeError, ValueError):
|
||||
touch_count = 0
|
||||
return {
|
||||
"observed": phase != "unknown",
|
||||
"phase": phase,
|
||||
"momentum_band": momentum_band,
|
||||
"touch_count": max(0, min(256, touch_count)),
|
||||
}
|
||||
|
||||
def _update_relationship_phase_momentum(
|
||||
self,
|
||||
ctx: SessionContext,
|
||||
|
|
|
|||
|
|
@ -953,5 +953,41 @@ class SummaryAndRelationshipTests(unittest.IsolatedAsyncioTestCase):
|
|||
self.assertIs(state, bridged)
|
||||
|
||||
|
||||
async def test_peek_relationship_phase_is_read_only_and_does_not_create_state(self) -> None:
|
||||
service = self.make_service()
|
||||
ctx = SessionContext(
|
||||
session_id="qq:FriendMessage:missing-user",
|
||||
scope="private",
|
||||
platform="qq",
|
||||
user_id="missing-user",
|
||||
bot_id="bot-1",
|
||||
)
|
||||
before = dict(service._relationship_phase_state)
|
||||
missing = service._peek_relationship_phase(ctx)
|
||||
self.assertEqual({"observed": False, "phase": "unknown", "momentum_band": "unknown"}, missing)
|
||||
self.assertEqual(before, service._relationship_phase_state)
|
||||
bridged_missing = MemoryCompanionBridge(service).peek_relationship_phase(
|
||||
session_id=ctx.session_id,
|
||||
scope=ctx.scope,
|
||||
platform=ctx.platform,
|
||||
user_id=ctx.user_id,
|
||||
bot_id=ctx.bot_id,
|
||||
)
|
||||
self.assertEqual(missing, bridged_missing)
|
||||
|
||||
state = service._get_relationship_phase(ctx)
|
||||
state["phase"] = "close"
|
||||
state["momentum"] = 0.2
|
||||
state["touch_count"] = 7
|
||||
service._save_relationship_phase_state()
|
||||
after = dict(service._relationship_phase_state)
|
||||
observed = service._peek_relationship_phase(ctx)
|
||||
self.assertEqual(
|
||||
{"observed": True, "phase": "close", "momentum_band": "rising", "touch_count": 7},
|
||||
observed,
|
||||
)
|
||||
self.assertEqual(after, service._relationship_phase_state)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用