"""运行时目标上下文构建(纯函数,无 IO)。""" from typing import Any _LEVEL_LABEL: dict[str, str] = { "highest": "最高目标", "long_term": "长期目标", "session": "会话目标", "task": "任务目标", "action": "行动", } def build_goals_context(goals: list[dict[str, Any]]) -> str: """把活跃目标列表格式化为 system prompt 片段。 按 priority 降序排列;空列表返回空串。 """ if not goals: return "" sorted_goals = sorted(goals, key=lambda g: g.get("priority", 0), reverse=True) lines: list[str] = ["当前活跃目标:"] for g in sorted_goals: level_label = _LEVEL_LABEL.get(g.get("level", ""), g.get("level", "")) progress = g.get("progress", 0.0) bar = _progress_bar(progress) line = f"- [{level_label}] {g['title']} [{bar} {int(progress * 100)}%]" lines.append(line) return "\n".join(lines) def _progress_bar(value: float, width: int = 8) -> str: filled = int(value * width) return "■" * filled + "□" * (width - filled)