refactor(memory): unify structured consolidation flow
This commit is contained in:
1 parent
d4de0e4e3d
commit
16be6b0f36
14 files changed
+257
-340
No files matched your search
+24
-35
@@ -1,47 +1,36 @@
|
||||
"""Structured session-summary values used while building model context."""
|
||||
"""Helpers for validated session-summary metadata."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import cast
|
||||
from typing import TypedDict, cast
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class SessionSummary:
|
||||
"""A consolidated session checkpoint before presentation formatting."""
|
||||
|
||||
class SessionSummary(TypedDict):
|
||||
text: str
|
||||
last_active: datetime
|
||||
last_active: str
|
||||
|
||||
def for_prompt(self) -> str:
|
||||
return (
|
||||
f"Previous conversation summary (last active {self.last_active.isoformat()}):\n"
|
||||
f"{self.text}"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_metadata(
|
||||
cls,
|
||||
metadata: Mapping[str, object] | None,
|
||||
*,
|
||||
fallback_last_active: datetime,
|
||||
) -> SessionSummary | None:
|
||||
raw: object = metadata.get("_last_summary") if metadata is not None else None
|
||||
if not isinstance(raw, Mapping):
|
||||
return None
|
||||
summary_data = cast(Mapping[str, object], raw)
|
||||
text = summary_data.get("text")
|
||||
if not isinstance(text, str) or not text:
|
||||
return None
|
||||
raw_last_active = summary_data.get("last_active")
|
||||
def session_summary_from_metadata(
|
||||
metadata: Mapping[str, object] | None,
|
||||
*,
|
||||
fallback_last_active: datetime,
|
||||
) -> SessionSummary | None:
|
||||
raw: object = metadata.get("_last_summary") if metadata is not None else None
|
||||
if not isinstance(raw, Mapping):
|
||||
return None
|
||||
summary_data = cast(Mapping[str, object], raw)
|
||||
text = summary_data.get("text")
|
||||
if not isinstance(text, str) or not text:
|
||||
return None
|
||||
raw_last_active = summary_data.get("last_active")
|
||||
if isinstance(raw_last_active, str):
|
||||
try:
|
||||
last_active = (
|
||||
datetime.fromisoformat(raw_last_active)
|
||||
if isinstance(raw_last_active, str)
|
||||
else fallback_last_active
|
||||
)
|
||||
datetime.fromisoformat(raw_last_active)
|
||||
last_active = raw_last_active
|
||||
except ValueError:
|
||||
last_active = fallback_last_active
|
||||
return cls(text=text, last_active=last_active)
|
||||
last_active = fallback_last_active.isoformat()
|
||||
else:
|
||||
last_active = fallback_last_active.isoformat()
|
||||
return {"text": text, "last_active": last_active}
|
||||
Reference in new issue
Block a user