60 行
2.4 KiB
Python
60 行
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Read model for the local unified agenda."""
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime
|
||
from typing import Any
|
||
|
||
try:
|
||
from .schedule_reconciler import reconcile
|
||
except ImportError:
|
||
from schedule_reconciler import reconcile
|
||
|
||
|
||
def build_unified_agenda(
|
||
*,
|
||
plans: list[dict[str, Any]],
|
||
activities: list[dict[str, Any]],
|
||
now: datetime,
|
||
date_key: str = "",
|
||
) -> dict[str, Any]:
|
||
result = reconcile(plans, activities, now=now)
|
||
entries = result["entries"]
|
||
if date_key:
|
||
entries = [item for item in entries if str(item.get("date") or item.get("start_at", ""))[:10] == date_key]
|
||
current = None
|
||
ordered_entries = sorted(entries, key=lambda item: (0 if item.get("kind") == "observed_activity" else 1, str(item.get("start_at") or "")))
|
||
for entry in ordered_entries:
|
||
if entry.get("status") not in {"cancelled", "unknown"}:
|
||
start = str(entry.get("start_at") or "")
|
||
end = str(entry.get("end_at") or "")
|
||
if start <= now.isoformat() <= end:
|
||
current = entry
|
||
break
|
||
return {
|
||
"generated_at": now.isoformat(timespec="seconds"),
|
||
"date": date_key or now.date().isoformat(),
|
||
"current": current,
|
||
"entries": entries,
|
||
"plans": result["plans"],
|
||
"activities": result["activities"],
|
||
"matched": result["matched"],
|
||
}
|
||
|
||
|
||
def format_agenda_context(agenda: dict[str, Any], *, max_entries: int = 8) -> str:
|
||
if not isinstance(agenda, dict):
|
||
return ""
|
||
lines = [f"当前统一日程日期:{agenda.get('date') or '未知'}"]
|
||
current = agenda.get("current")
|
||
if isinstance(current, dict):
|
||
lines.append(f"当前实际活动:{current.get('title') or '未命名'}(证据等级 {current.get('evidence_level') or '未知'})")
|
||
entries = agenda.get("entries") if isinstance(agenda.get("entries"), list) else []
|
||
for item in entries[:max_entries]:
|
||
title = str(item.get("title") or "未命名")[:80]
|
||
status = str(item.get("status") or "unknown")
|
||
kind = str(item.get("kind") or "")
|
||
prefix = "实际" if kind == "observed_activity" else "计划"
|
||
reason = str((item.get("reconciliation") or {}).get("reason") or "")
|
||
lines.append(f"- {prefix}:{title}({status})" + (f";{reason}" if reason else ""))
|
||
return "\n".join(lines)
|