mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-09-04 02:01:48 +03:00
37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""Helpers for validated session-summary metadata."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from datetime import datetime
|
|
from typing import TypedDict, cast
|
|
|
|
|
|
class SessionSummary(TypedDict):
|
|
text: str
|
|
last_active: str
|
|
|
|
|
|
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:
|
|
datetime.fromisoformat(raw_last_active)
|
|
last_active = raw_last_active
|
|
except ValueError:
|
|
last_active = fallback_last_active.isoformat()
|
|
else:
|
|
last_active = fallback_last_active.isoformat()
|
|
return {"text": text, "last_active": last_active}
|