# -*- coding: utf-8 -*- """Runtime integration for the local unified agenda.""" from __future__ import annotations import asyncio from copy import deepcopy from datetime import datetime, timedelta from typing import Any from astrbot.api import logger try: from .activity_capture import ActivityCapture from .agenda_contracts import interval_overlaps_window, migrate_store, normalize_observed_activity, normalize_window_snapshot, window_bounds, window_for_datetime from .unified_agenda import build_unified_agenda, format_agenda_context from .world_gateway import NullWorldGateway, WorldGatewayContract from .calendar_system import plan_item_to_event except ImportError: from activity_capture import ActivityCapture from agenda_contracts import interval_overlaps_window, migrate_store, normalize_observed_activity, normalize_window_snapshot, window_bounds, window_for_datetime from unified_agenda import build_unified_agenda, format_agenda_context from world_gateway import NullWorldGateway, WorldGatewayContract from calendar_system import plan_item_to_event # 停机再长也不会无限往回补算;超出这个跨度的窗口会明确告警后放弃, # 而不是像原来那样默默消失在固定的 3 天扫描窗外。 AGENDA_SETTLE_MAX_CATCHUP_DAYS = 30 class AgendaRuntimeMixin: def _agenda_prepare_store(self) -> None: data, changed = migrate_store(self.data) self.data = data if not hasattr(self, "_agenda_capture"): self._agenda_capture = ActivityCapture() if not hasattr(self, "_world_gateway"): self._world_gateway = WorldGatewayContract(NullWorldGateway()) if changed and hasattr(self, "_schedule_data_save"): self._schedule_data_save(delay=0.0) def _agenda_activities_store(self) -> list[dict[str, Any]]: self._agenda_prepare_store() activities = self.data.setdefault("observed_activities", []) if not isinstance(activities, list): activities = [] self.data["observed_activities"] = activities return activities def _agenda_snapshots_store(self) -> list[dict[str, Any]]: self._agenda_prepare_store() snapshots = self.data.setdefault("window_snapshots", []) if not isinstance(snapshots, list): snapshots = [] self.data["window_snapshots"] = snapshots return snapshots def _agenda_schedule_observed_archive(self, activity: dict[str, Any]) -> None: recorder = getattr(self, "_memory_companion_record_observed_activity", None) if not callable(recorder) or not isinstance(activity, dict): return try: loop = asyncio.get_running_loop() except RuntimeError: return async def deliver() -> None: try: await recorder(deepcopy(activity)) except Exception: pass loop.create_task(deliver()) def _agenda_capture_inbound_message( self, *, text: str, event_time: datetime, source_ref: str, conversation_id: str, participant: str = "user", message_count: int = 1, ) -> dict[str, Any] | None: self._agenda_prepare_store() candidate = self._agenda_capture.capture_message( text=text, event_time=event_time, source_ref=source_ref, conversation_id=conversation_id, participant=participant, message_count=message_count, ) if candidate is None: return None activities = self._agenda_activities_store() source_ref = str(candidate.get("source_refs", [""])[0] or "") if isinstance(candidate.get("source_refs"), list) else "" duplicate = next((item for item in activities if source_ref and source_ref in (item.get("source_refs") or [])), None) existing = duplicate or next((item for item in activities if isinstance(item, dict) and item.get("activity_id") == candidate.get("activity_id")), None) if duplicate is not None: final_activity = deepcopy(existing) elif existing is not None: prior_refs = list(existing.get("source_refs") or []) prior_refs.extend(candidate.get("source_refs") or []) candidate["source_refs"] = list(dict.fromkeys(str(item) for item in prior_refs if str(item)))[:30] candidate["start_at"] = min(str(existing.get("start_at") or candidate.get("start_at")), str(candidate.get("start_at") or existing.get("start_at"))) candidate["end_at"] = max(str(existing.get("end_at") or candidate.get("end_at")), str(candidate.get("end_at") or existing.get("end_at"))) existing.update(candidate) existing["version"] = int(existing.get("version") or 1) + 1 final_activity = deepcopy(existing) else: candidate["version"] = max(1, int(candidate.get("version") or 1)) activities.append(candidate) final_activity = deepcopy(candidate) activities[:] = activities[-500:] if hasattr(self, "_schedule_data_save"): self._schedule_data_save() if duplicate is None: self._agenda_schedule_observed_archive(final_activity) return final_activity def _agenda_capture_hard_fact(self, activity: dict[str, Any]) -> dict[str, Any]: self._agenda_prepare_store() normalized = normalize_observed_activity(activity) activities = self._agenda_activities_store() existing = next((item for item in activities if item.get("activity_id") == normalized.get("activity_id")), None) if existing is None: normalized["version"] = max(1, int(normalized.get("version") or 1)) activities.append(normalized) final_activity = deepcopy(normalized) else: existing.update(normalized) existing["version"] = int(existing.get("version") or 1) + 1 final_activity = deepcopy(existing) activities[:] = activities[-500:] if hasattr(self, "_schedule_data_save"): self._schedule_data_save() self._agenda_schedule_observed_archive(final_activity) return final_activity def _agenda_current_plan_items(self) -> list[dict[str, Any]]: events = self.data.get("calendar_events") if isinstance(self.data.get("calendar_events"), list) else [] projected = [] for event in events: if not isinstance(event, dict): continue source = str(event.get("source") or "") source_kind = str(event.get("source_kind") or "") if source == "daily_plan_projection" or source_kind == "planned": item = dict(event) item["plan_id"] = str(item.get("plan_id") or item.get("event_id") or "") item["title"] = str(item.get("title") or item.get("description") or "") projected.append(item) if projected: return projected plan = self.data.get("daily_plan") if isinstance(self.data.get("daily_plan"), dict) else {} items = plan.get("items") if isinstance(plan.get("items"), list) else [] result = [] plan_date = str(plan.get("date") or "") now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone() for index, item in enumerate(items): if not isinstance(item, dict): continue try: event = plan_item_to_event(plan_date, item, timezone_name=getattr(self, "_calendar_timezone_name", lambda: "Asia/Shanghai")(), index=index, now=now) except Exception: continue event["plan_id"] = event.get("event_id") event["title"] = event.get("title") or event.get("description") result.append(event) return result def _agenda_build(self, *, date_key: str = "") -> dict[str, Any]: self._agenda_prepare_store() now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone() return build_unified_agenda( plans=self._agenda_current_plan_items(), activities=self._agenda_activities_store(), now=now, date_key=date_key, ) def _agenda_context_for_prompt(self, *, max_entries: int = 8) -> str: return format_agenda_context(self._agenda_build(), max_entries=max_entries) def _agenda_reconciliation_store(self) -> list[dict[str, Any]]: self._agenda_prepare_store() history = self.data.setdefault("agenda_reconciliation_history", []) if not isinstance(history, list): history = [] self.data["agenda_reconciliation_history"] = history return history def _agenda_timezone_name(self) -> str: getter = getattr(self, "_calendar_timezone_name", None) if callable(getter): try: return str(getter() or "Asia/Shanghai") except Exception: pass return str(getattr(self, "calendar_timezone", "Asia/Shanghai") or "Asia/Shanghai") def _agenda_window_items(self, *, date_key: str, window: str) -> tuple[list[dict[str, Any]], list[dict[str, Any]], datetime, datetime]: start, end = window_bounds(date_key, window, timezone_name=self._agenda_timezone_name()) plans = [item for item in self._agenda_current_plan_items() if interval_overlaps_window(item, start, end, timezone_name=self._agenda_timezone_name())] activities = [item for item in self._agenda_activities_store() if interval_overlaps_window(item, start, end, timezone_name=self._agenda_timezone_name())] return plans, activities, start, end def _agenda_snapshot_window(self, *, date_key: str, window: str, open_items: list[str] | None = None) -> dict[str, Any]: plans, activities, start, end = self._agenda_window_items(date_key=date_key, window=window) now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone() agenda = build_unified_agenda(plans=plans, activities=activities, now=now, date_key=date_key) reconciliation = [ { "plan_id": item.get("plan_id") or item.get("event_id"), "status": item.get("status") or "unknown", "reason": item.get("reconciliation_reason") or "", "activity_ids": list(item.get("reconciled_activity_ids") or []), } for item in agenda.get("plans", []) if isinstance(item, dict) ] snapshot = normalize_window_snapshot( { "snapshot_id": self._agenda_snapshot_id(date_key, window), "date": date_key, "window_date": date_key, "window": window, "start_at": start.isoformat(timespec="seconds"), "end_at": end.isoformat(timespec="seconds"), "timezone": self._agenda_timezone_name(), "planned": [item.get("title") or item.get("activity") for item in plans if isinstance(item, dict)], "observed": [item.get("title") for item in activities if isinstance(item, dict)], "reconciled": reconciliation, "open_items": list(open_items or []), "source_refs": [item.get("activity_id") for item in activities if item.get("activity_id")], "source_kind": "projection", "evidence_level": "L2", "certainty": "medium", }, now=now, ) snapshots = self._agenda_snapshots_store() existing = next((item for item in snapshots if item.get("snapshot_id") == snapshot.get("snapshot_id")), None) if existing is None: snapshots.append(snapshot) else: created_at = existing.get("created_at") or existing.get("generated_at") snapshot["version"] = int(existing.get("version") or 1) + 1 if created_at: snapshot["created_at"] = created_at existing.clear() existing.update(snapshot) snapshots[:] = snapshots[-120:] history = self._agenda_reconciliation_store() reconciliation_id = f"reconciliation:{date_key}:{window}" record = { "reconciliation_id": reconciliation_id, "date": date_key, "window_date": date_key, "window": window, "start_at": snapshot.get("start_at"), "end_at": snapshot.get("end_at"), "timezone": self._agenda_timezone_name(), "plans": reconciliation, "observed_activity_ids": list(snapshot.get("source_refs") or []), "source_refs": [snapshot.get("snapshot_id")], "source_kind": "real", "evidence_level": "L3", "certainty": "high" if reconciliation else "medium", "status": "completed", "generated_at": snapshot.get("generated_at"), "version": 1, } existing_record = next((item for item in history if item.get("reconciliation_id") == reconciliation_id), None) if existing_record is None: history.append(record) else: record["version"] = int(existing_record.get("version") or 1) + 1 existing_record.clear() existing_record.update(record) history[:] = history[-240:] if hasattr(self, "_schedule_data_save"): self._schedule_data_save() return snapshot def _agenda_window_for_now(self, now: datetime | None = None) -> tuple[str, int, int]: current = now or (self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone()) name, _, start, end = window_for_datetime(current, timezone_name=self._agenda_timezone_name()) return name, start.hour * 60 + start.minute, end.hour * 60 + end.minute def _agenda_snapshot_id(self, date_key: str, window: str) -> str: return f"snapshot-{date_key}-{window}" def _agenda_settle_cursor_date(self) -> str: """上次结算推进到的日期。空串表示还没有游标(首次运行或旧数据)。""" value = self.data.get("agenda_last_settled") if isinstance(self.data, dict) else None if isinstance(value, dict): value = value.get("date") text = str(value or "").strip()[:10] try: datetime.strptime(text, "%Y-%m-%d") except ValueError: return "" return text def _agenda_note_settled(self, date_key: str, window: str) -> None: """把游标推到已结算的这个窗口。只前进不后退。""" text = str(date_key or "").strip()[:10] if not text: return current = self._agenda_settle_cursor_date() if current and text < current: return self.data["agenda_last_settled"] = {"date": text, "window": str(window or "")} saver = getattr(self, "_schedule_data_save", None) if callable(saver): saver(delay=0.0) def _agenda_closed_windows(self, now: datetime) -> list[tuple[str, str]]: timezone_name = self._agenda_timezone_name() local_now = now if local_now.tzinfo is None: local_now = local_now.replace(tzinfo=window_bounds(now.date(), "早晨", timezone_name=timezone_name)[0].tzinfo) local_now = local_now.astimezone(window_bounds(local_now.date(), "早晨", timezone_name=timezone_name)[0].tzinfo) existing_ids = {str(item.get("snapshot_id")) for item in self._agenda_snapshots_store() if isinstance(item, dict)} # 这里原本固定扫 -3..+1 天:停机超过 3 天,中间的窗口永远不会被补算, # 而且不留任何痕迹。改成从上次结算游标往前扫,游标缺失时退回原来的 3 天。 cursor = self._agenda_settle_cursor_date() today = local_now.date() start = today - timedelta(days=3) if cursor: start = datetime.strptime(cursor, "%Y-%m-%d").date() earliest = today - timedelta(days=AGENDA_SETTLE_MAX_CATCHUP_DAYS) if start < earliest: logger.warning( "[PrivateCompanion] 窗口结算落后至 %s,超出 %s 天补算上限,%s 之前的窗口不再补算", start.isoformat(), AGENDA_SETTLE_MAX_CATCHUP_DAYS, earliest.isoformat(), ) start = earliest candidates: list[tuple[str, str]] = [] span = (today - start).days + 2 for offset in range(span): target = start + timedelta(days=offset) for name in ("深夜", "早晨", "下午", "晚上"): _, end = window_bounds(target, name, timezone_name=timezone_name) snapshot_id = self._agenda_snapshot_id(target.isoformat(), name) if end <= local_now and snapshot_id not in existing_ids: candidates.append((target.isoformat(), name)) return candidates async def _agenda_maintenance_tick(self) -> None: """Settle closed local windows, recover missed windows after restart, and flush optional archive delivery.""" self._agenda_prepare_store() now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone() for date_key, window in self._agenda_closed_windows(now): snapshot = self._agenda_snapshot_window(date_key=date_key, window=window) recorder = getattr(self, "_memory_companion_record_agenda_snapshot", None) if callable(recorder): try: await recorder(snapshot) except Exception: pass history = self._agenda_reconciliation_store() reconciliation = next((item for item in reversed(history) if item.get("reconciliation_id") == f"reconciliation:{date_key}:{window}"), None) reconciliation_recorder = getattr(self, "_memory_companion_record_reconciliation", None) if callable(reconciliation_recorder) and isinstance(reconciliation, dict): try: await reconciliation_recorder(reconciliation) except Exception: pass # 游标只在这一整套(快照 + 结算)都走完之后才推进 self._agenda_note_settled(date_key, window) sleep_text = str(getattr(self, "daily_diary_time", "23:10") or "23:10") try: hour_text, minute_text = sleep_text.split(":", 1) bedtime_minute = int(hour_text) * 60 + int(minute_text) except (TypeError, ValueError): bedtime_minute = 1390 lead = max(0, int(getattr(self, "sleep_summary_lead_minutes", 30) or 30)) if now.hour * 60 + now.minute >= max(0, bedtime_minute - lead): diary = self.data.get("bot_diaries", []) date_key = now.date().isoformat() today_has_diary = isinstance(diary, list) and any(isinstance(item, dict) and str(item.get("date") or "") == date_key for item in diary) if not today_has_diary and bool(getattr(self, "enable_daily_diary", True)): ensure_diary = getattr(self, "_ensure_daily_diary", None) if callable(ensure_diary): try: await ensure_diary() except Exception: pass flusher = getattr(self, "_memory_companion_flush_bot_personal_outbox", None) if callable(flusher): try: await flusher(limit=10) except Exception: pass def _world_get_context(self, subject_id: str) -> dict[str, Any]: """Return only cached World context; production networking is disabled in Q5.""" self._agenda_prepare_store() now = self._calendar_now() if hasattr(self, "_calendar_now") else datetime.now().astimezone() cache = self.data.get("world_projection_cache") if isinstance(self.data.get("world_projection_cache"), list) else [] for item in reversed(cache): if not isinstance(item, dict) or str(item.get("subject_id") or "") != str(subject_id): continue fresh_until = str(item.get("fresh_until") or "") try: expiry = datetime.fromisoformat(fresh_until.replace("Z", "+00:00")) if expiry.tzinfo is None: expiry = expiry.replace(tzinfo=now.tzinfo) if now <= expiry: return {"status": "cached", "fresh": True, "projection": item} except (TypeError, ValueError): continue return {"status": "offline", "fresh": False, "projection": None} def _world_publish_activity(self, activity: dict[str, Any]) -> dict[str, Any]: """Keep the future publish boundary explicit without sending to a server.""" if not bool(getattr(self, "enable_world_gateway", False)): return {"ok": False, "reason": "world_gateway_disabled"} result = self._world_gateway.publish_activity(activity) return {"ok": bool(result.ok), "reason": result.reason, "data": result.data}